blob: 64ae9f89773a1c44f84858eb7622599a81f25de6 [file] [log] [blame]
Sage Weilf24e9982009-10-06 11:31:10 -07001
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002#include <linux/ceph/ceph_debug.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09003
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09005#include <linux/slab.h>
Sage Weilf24e9982009-10-06 11:31:10 -07006#include <asm/div64.h>
7
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;
298 }
299
300 insert_choose_arg_map(&c->choose_args, arg_map);
301 }
302
303 return 0;
304
305e_inval:
306 ret = -EINVAL;
307fail:
308 free_choose_arg_map(arg_map);
309 return ret;
310}
311
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100312static void crush_finalize(struct crush_map *c)
313{
314 __s32 b;
315
316 /* Space for the array of pointers to per-bucket workspace */
317 c->working_size = sizeof(struct crush_work) +
318 c->max_buckets * sizeof(struct crush_work_bucket *);
319
320 for (b = 0; b < c->max_buckets; b++) {
321 if (!c->buckets[b])
322 continue;
323
324 switch (c->buckets[b]->alg) {
325 default:
326 /*
327 * The base case, permutation variables and
328 * the pointer to the permutation array.
329 */
330 c->working_size += sizeof(struct crush_work_bucket);
331 break;
332 }
333 /* Every bucket has a permutation array. */
334 c->working_size += c->buckets[b]->size * sizeof(__u32);
335 }
336}
337
Sage Weilf24e9982009-10-06 11:31:10 -0700338static struct crush_map *crush_decode(void *pbyval, void *end)
339{
340 struct crush_map *c;
Ilya Dryomovc2acfd92017-07-13 15:57:26 +0200341 int err;
Sage Weilf24e9982009-10-06 11:31:10 -0700342 int i, j;
343 void **p = &pbyval;
344 void *start = pbyval;
345 u32 magic;
346
347 dout("crush_decode %p to %p len %d\n", *p, end, (int)(end - *p));
348
349 c = kzalloc(sizeof(*c), GFP_NOFS);
350 if (c == NULL)
351 return ERR_PTR(-ENOMEM);
352
Ilya Dryomov5cf9c4a2017-06-22 19:44:05 +0200353 c->choose_args = RB_ROOT;
354
Sage Weil546f04e2012-07-30 18:15:23 -0700355 /* set tunables to default values */
356 c->choose_local_tries = 2;
357 c->choose_local_fallback_tries = 5;
358 c->choose_total_tries = 19;
Jim Schutt1604f482012-11-30 09:15:25 -0700359 c->chooseleaf_descend_once = 0;
Sage Weil546f04e2012-07-30 18:15:23 -0700360
Sage Weilf24e9982009-10-06 11:31:10 -0700361 ceph_decode_need(p, end, 4*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700362 magic = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700363 if (magic != CRUSH_MAGIC) {
364 pr_err("crush_decode magic %x != current %x\n",
Eric Dumazet95c96172012-04-15 05:58:06 +0000365 (unsigned int)magic, (unsigned int)CRUSH_MAGIC);
Sage Weilf24e9982009-10-06 11:31:10 -0700366 goto bad;
367 }
Sage Weilc89136e2009-10-14 09:59:09 -0700368 c->max_buckets = ceph_decode_32(p);
369 c->max_rules = ceph_decode_32(p);
370 c->max_devices = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700371
Sage Weilf24e9982009-10-06 11:31:10 -0700372 c->buckets = kcalloc(c->max_buckets, sizeof(*c->buckets), GFP_NOFS);
373 if (c->buckets == NULL)
374 goto badmem;
375 c->rules = kcalloc(c->max_rules, sizeof(*c->rules), GFP_NOFS);
376 if (c->rules == NULL)
377 goto badmem;
378
379 /* buckets */
380 for (i = 0; i < c->max_buckets; i++) {
381 int size = 0;
382 u32 alg;
383 struct crush_bucket *b;
384
385 ceph_decode_32_safe(p, end, alg, bad);
386 if (alg == 0) {
387 c->buckets[i] = NULL;
388 continue;
389 }
390 dout("crush_decode bucket %d off %x %p to %p\n",
391 i, (int)(*p-start), *p, end);
392
393 switch (alg) {
394 case CRUSH_BUCKET_UNIFORM:
395 size = sizeof(struct crush_bucket_uniform);
396 break;
397 case CRUSH_BUCKET_LIST:
398 size = sizeof(struct crush_bucket_list);
399 break;
400 case CRUSH_BUCKET_TREE:
401 size = sizeof(struct crush_bucket_tree);
402 break;
403 case CRUSH_BUCKET_STRAW:
404 size = sizeof(struct crush_bucket_straw);
405 break;
Ilya Dryomov958a2762015-04-14 16:54:52 +0300406 case CRUSH_BUCKET_STRAW2:
407 size = sizeof(struct crush_bucket_straw2);
408 break;
Sage Weilf24e9982009-10-06 11:31:10 -0700409 default:
410 goto bad;
411 }
412 BUG_ON(size == 0);
413 b = c->buckets[i] = kzalloc(size, GFP_NOFS);
414 if (b == NULL)
415 goto badmem;
416
417 ceph_decode_need(p, end, 4*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700418 b->id = ceph_decode_32(p);
419 b->type = ceph_decode_16(p);
Sage Weilfb690392009-11-07 20:18:22 -0800420 b->alg = ceph_decode_8(p);
421 b->hash = ceph_decode_8(p);
Sage Weilc89136e2009-10-14 09:59:09 -0700422 b->weight = ceph_decode_32(p);
423 b->size = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700424
425 dout("crush_decode bucket size %d off %x %p to %p\n",
426 b->size, (int)(*p-start), *p, end);
427
428 b->items = kcalloc(b->size, sizeof(__s32), GFP_NOFS);
429 if (b->items == NULL)
430 goto badmem;
Sage Weilf24e9982009-10-06 11:31:10 -0700431
432 ceph_decode_need(p, end, b->size*sizeof(u32), bad);
433 for (j = 0; j < b->size; j++)
Sage Weilc89136e2009-10-14 09:59:09 -0700434 b->items[j] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700435
436 switch (b->alg) {
437 case CRUSH_BUCKET_UNIFORM:
438 err = crush_decode_uniform_bucket(p, end,
439 (struct crush_bucket_uniform *)b);
440 if (err < 0)
Ilya Dryomovc2acfd92017-07-13 15:57:26 +0200441 goto fail;
Sage Weilf24e9982009-10-06 11:31:10 -0700442 break;
443 case CRUSH_BUCKET_LIST:
444 err = crush_decode_list_bucket(p, end,
445 (struct crush_bucket_list *)b);
446 if (err < 0)
Ilya Dryomovc2acfd92017-07-13 15:57:26 +0200447 goto fail;
Sage Weilf24e9982009-10-06 11:31:10 -0700448 break;
449 case CRUSH_BUCKET_TREE:
450 err = crush_decode_tree_bucket(p, end,
451 (struct crush_bucket_tree *)b);
452 if (err < 0)
Ilya Dryomovc2acfd92017-07-13 15:57:26 +0200453 goto fail;
Sage Weilf24e9982009-10-06 11:31:10 -0700454 break;
455 case CRUSH_BUCKET_STRAW:
456 err = crush_decode_straw_bucket(p, end,
457 (struct crush_bucket_straw *)b);
458 if (err < 0)
Ilya Dryomovc2acfd92017-07-13 15:57:26 +0200459 goto fail;
Sage Weilf24e9982009-10-06 11:31:10 -0700460 break;
Ilya Dryomov958a2762015-04-14 16:54:52 +0300461 case CRUSH_BUCKET_STRAW2:
462 err = crush_decode_straw2_bucket(p, end,
463 (struct crush_bucket_straw2 *)b);
464 if (err < 0)
Ilya Dryomovc2acfd92017-07-13 15:57:26 +0200465 goto fail;
Ilya Dryomov958a2762015-04-14 16:54:52 +0300466 break;
Sage Weilf24e9982009-10-06 11:31:10 -0700467 }
468 }
469
470 /* rules */
471 dout("rule vec is %p\n", c->rules);
472 for (i = 0; i < c->max_rules; i++) {
473 u32 yes;
474 struct crush_rule *r;
475
476 ceph_decode_32_safe(p, end, yes, bad);
477 if (!yes) {
478 dout("crush_decode NO rule %d off %x %p to %p\n",
479 i, (int)(*p-start), *p, end);
480 c->rules[i] = NULL;
481 continue;
482 }
483
484 dout("crush_decode rule %d off %x %p to %p\n",
485 i, (int)(*p-start), *p, end);
486
487 /* len */
488 ceph_decode_32_safe(p, end, yes, bad);
489#if BITS_PER_LONG == 32
Xi Wang64486692012-02-16 11:55:48 -0500490 if (yes > (ULONG_MAX - sizeof(*r))
491 / sizeof(struct crush_rule_step))
Sage Weilf24e9982009-10-06 11:31:10 -0700492 goto bad;
493#endif
494 r = c->rules[i] = kmalloc(sizeof(*r) +
495 yes*sizeof(struct crush_rule_step),
496 GFP_NOFS);
497 if (r == NULL)
498 goto badmem;
499 dout(" rule %d is at %p\n", i, r);
500 r->len = yes;
501 ceph_decode_copy_safe(p, end, &r->mask, 4, bad); /* 4 u8's */
502 ceph_decode_need(p, end, r->len*3*sizeof(u32), bad);
503 for (j = 0; j < r->len; j++) {
Sage Weilc89136e2009-10-14 09:59:09 -0700504 r->steps[j].op = ceph_decode_32(p);
505 r->steps[j].arg1 = ceph_decode_32(p);
506 r->steps[j].arg2 = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700507 }
508 }
509
Ilya Dryomov278b1d702017-06-21 17:27:17 +0200510 ceph_decode_skip_map(p, end, 32, string, bad); /* type_map */
511 ceph_decode_skip_map(p, end, 32, string, bad); /* name_map */
512 ceph_decode_skip_map(p, end, 32, string, bad); /* rule_name_map */
Sage Weilf24e9982009-10-06 11:31:10 -0700513
Sage Weil546f04e2012-07-30 18:15:23 -0700514 /* tunables */
515 ceph_decode_need(p, end, 3*sizeof(u32), done);
516 c->choose_local_tries = ceph_decode_32(p);
517 c->choose_local_fallback_tries = ceph_decode_32(p);
518 c->choose_total_tries = ceph_decode_32(p);
Ilya Dryomovb9b519b2016-02-01 16:57:16 +0100519 dout("crush decode tunable choose_local_tries = %d\n",
Sage Weil546f04e2012-07-30 18:15:23 -0700520 c->choose_local_tries);
Ilya Dryomovb9b519b2016-02-01 16:57:16 +0100521 dout("crush decode tunable choose_local_fallback_tries = %d\n",
Sage Weil546f04e2012-07-30 18:15:23 -0700522 c->choose_local_fallback_tries);
Ilya Dryomovb9b519b2016-02-01 16:57:16 +0100523 dout("crush decode tunable choose_total_tries = %d\n",
Sage Weil546f04e2012-07-30 18:15:23 -0700524 c->choose_total_tries);
525
Jim Schutt1604f482012-11-30 09:15:25 -0700526 ceph_decode_need(p, end, sizeof(u32), done);
527 c->chooseleaf_descend_once = ceph_decode_32(p);
Ilya Dryomovb9b519b2016-02-01 16:57:16 +0100528 dout("crush decode tunable chooseleaf_descend_once = %d\n",
Jim Schutt1604f482012-11-30 09:15:25 -0700529 c->chooseleaf_descend_once);
530
Ilya Dryomovf1406622014-05-09 18:27:34 +0400531 ceph_decode_need(p, end, sizeof(u8), done);
532 c->chooseleaf_vary_r = ceph_decode_8(p);
Ilya Dryomovb9b519b2016-02-01 16:57:16 +0100533 dout("crush decode tunable chooseleaf_vary_r = %d\n",
Ilya Dryomovf1406622014-05-09 18:27:34 +0400534 c->chooseleaf_vary_r);
535
Ilya Dryomovb9b519b2016-02-01 16:57:16 +0100536 /* skip straw_calc_version, allowed_bucket_algs */
537 ceph_decode_need(p, end, sizeof(u8) + sizeof(u32), done);
538 *p += sizeof(u8) + sizeof(u32);
539
540 ceph_decode_need(p, end, sizeof(u8), done);
541 c->chooseleaf_stable = ceph_decode_8(p);
542 dout("crush decode tunable chooseleaf_stable = %d\n",
543 c->chooseleaf_stable);
544
Ilya Dryomov5cf9c4a2017-06-22 19:44:05 +0200545 if (*p != end) {
546 /* class_map */
547 ceph_decode_skip_map(p, end, 32, 32, bad);
548 /* class_name */
549 ceph_decode_skip_map(p, end, 32, string, bad);
550 /* class_bucket */
551 ceph_decode_skip_map_of_map(p, end, 32, 32, 32, bad);
552 }
553
554 if (*p != end) {
555 err = decode_choose_args(p, end, c);
556 if (err)
Ilya Dryomovc2acfd92017-07-13 15:57:26 +0200557 goto fail;
Ilya Dryomov5cf9c4a2017-06-22 19:44:05 +0200558 }
559
Sage Weil546f04e2012-07-30 18:15:23 -0700560done:
Ilya Dryomov9afd30d2017-02-28 18:53:53 +0100561 crush_finalize(c);
Sage Weilf24e9982009-10-06 11:31:10 -0700562 dout("crush_decode success\n");
563 return c;
564
565badmem:
566 err = -ENOMEM;
Ilya Dryomovc2acfd92017-07-13 15:57:26 +0200567fail:
Sage Weilf24e9982009-10-06 11:31:10 -0700568 dout("crush_decode fail %d\n", err);
569 crush_destroy(c);
570 return ERR_PTR(err);
Ilya Dryomovc2acfd92017-07-13 15:57:26 +0200571
572bad:
573 err = -EINVAL;
574 goto fail;
Sage Weilf24e9982009-10-06 11:31:10 -0700575}
576
Ilya Dryomovf984cb72016-04-28 16:07:23 +0200577int ceph_pg_compare(const struct ceph_pg *lhs, const struct ceph_pg *rhs)
578{
579 if (lhs->pool < rhs->pool)
580 return -1;
581 if (lhs->pool > rhs->pool)
582 return 1;
583 if (lhs->seed < rhs->seed)
584 return -1;
585 if (lhs->seed > rhs->seed)
586 return 1;
587
588 return 0;
589}
590
Ilya Dryomova02a9462017-06-19 12:18:05 +0200591int ceph_spg_compare(const struct ceph_spg *lhs, const struct ceph_spg *rhs)
592{
593 int ret;
594
595 ret = ceph_pg_compare(&lhs->pgid, &rhs->pgid);
596 if (ret)
597 return ret;
598
599 if (lhs->shard < rhs->shard)
600 return -1;
601 if (lhs->shard > rhs->shard)
602 return 1;
603
604 return 0;
605}
606
Ilya Dryomova303bb02017-06-21 17:27:17 +0200607static struct ceph_pg_mapping *alloc_pg_mapping(size_t payload_len)
608{
609 struct ceph_pg_mapping *pg;
610
611 pg = kmalloc(sizeof(*pg) + payload_len, GFP_NOIO);
612 if (!pg)
613 return NULL;
614
615 RB_CLEAR_NODE(&pg->node);
616 return pg;
617}
618
619static void free_pg_mapping(struct ceph_pg_mapping *pg)
620{
621 WARN_ON(!RB_EMPTY_NODE(&pg->node));
622
623 kfree(pg);
624}
625
Sage Weilf24e9982009-10-06 11:31:10 -0700626/*
Sage Weil9794b142010-02-16 15:53:32 -0800627 * rbtree of pg_mapping for handling pg_temp (explicit mapping of pgid
Ilya Dryomov9686f942014-03-21 19:05:29 +0200628 * to a set of osds) and primary_temp (explicit primary setting)
Sage Weilf24e9982009-10-06 11:31:10 -0700629 */
Ilya Dryomovab751442017-06-21 17:27:17 +0200630DEFINE_RB_FUNCS2(pg_mapping, struct ceph_pg_mapping, pgid, ceph_pg_compare,
631 RB_BYPTR, const struct ceph_pg *, node)
Sage Weil8adc8b32011-09-28 10:11:04 -0700632
Sage Weilf24e9982009-10-06 11:31:10 -0700633/*
Sage Weil4fc51be2010-02-16 15:55:03 -0800634 * rbtree of pg pool info
635 */
636static int __insert_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *new)
637{
638 struct rb_node **p = &root->rb_node;
639 struct rb_node *parent = NULL;
640 struct ceph_pg_pool_info *pi = NULL;
641
642 while (*p) {
643 parent = *p;
644 pi = rb_entry(parent, struct ceph_pg_pool_info, node);
645 if (new->id < pi->id)
646 p = &(*p)->rb_left;
647 else if (new->id > pi->id)
648 p = &(*p)->rb_right;
649 else
650 return -EEXIST;
651 }
652
653 rb_link_node(&new->node, parent, p);
654 rb_insert_color(&new->node, root);
655 return 0;
656}
657
Sage Weil4f6a7e52013-02-23 10:41:09 -0800658static struct ceph_pg_pool_info *__lookup_pg_pool(struct rb_root *root, u64 id)
Sage Weil4fc51be2010-02-16 15:55:03 -0800659{
660 struct ceph_pg_pool_info *pi;
661 struct rb_node *n = root->rb_node;
662
663 while (n) {
664 pi = rb_entry(n, struct ceph_pg_pool_info, node);
665 if (id < pi->id)
666 n = n->rb_left;
667 else if (id > pi->id)
668 n = n->rb_right;
669 else
670 return pi;
671 }
672 return NULL;
673}
674
Ilya Dryomovce7f6a22014-01-27 17:40:19 +0200675struct ceph_pg_pool_info *ceph_pg_pool_by_id(struct ceph_osdmap *map, u64 id)
676{
677 return __lookup_pg_pool(&map->pg_pools, id);
678}
679
Alex Elder72afc712012-10-30 19:40:33 -0500680const char *ceph_pg_pool_name_by_id(struct ceph_osdmap *map, u64 id)
681{
682 struct ceph_pg_pool_info *pi;
683
684 if (id == CEPH_NOPOOL)
685 return NULL;
686
687 if (WARN_ON_ONCE(id > (u64) INT_MAX))
688 return NULL;
689
690 pi = __lookup_pg_pool(&map->pg_pools, (int) id);
691
692 return pi ? pi->name : NULL;
693}
694EXPORT_SYMBOL(ceph_pg_pool_name_by_id);
695
Yehuda Sadeh7669a2c2010-05-17 12:31:35 -0700696int ceph_pg_poolid_by_name(struct ceph_osdmap *map, const char *name)
697{
698 struct rb_node *rbp;
699
700 for (rbp = rb_first(&map->pg_pools); rbp; rbp = rb_next(rbp)) {
701 struct ceph_pg_pool_info *pi =
702 rb_entry(rbp, struct ceph_pg_pool_info, node);
703 if (pi->name && strcmp(pi->name, name) == 0)
704 return pi->id;
705 }
706 return -ENOENT;
707}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700708EXPORT_SYMBOL(ceph_pg_poolid_by_name);
Yehuda Sadeh7669a2c2010-05-17 12:31:35 -0700709
Sage Weil2844a762010-04-09 15:46:42 -0700710static void __remove_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *pi)
711{
712 rb_erase(&pi->node, root);
713 kfree(pi->name);
714 kfree(pi);
715}
716
Ilya Dryomov0f70c7e2014-03-21 19:05:27 +0200717static int decode_pool(void **p, void *end, struct ceph_pg_pool_info *pi)
Sage Weilefd75762010-03-17 10:05:28 -0700718{
Sage Weil4f6a7e52013-02-23 10:41:09 -0800719 u8 ev, cv;
720 unsigned len, num;
721 void *pool_end;
Sage Weil73a7e692010-08-02 11:00:55 -0700722
Sage Weil4f6a7e52013-02-23 10:41:09 -0800723 ceph_decode_need(p, end, 2 + 4, bad);
724 ev = ceph_decode_8(p); /* encoding version */
725 cv = ceph_decode_8(p); /* compat version */
726 if (ev < 5) {
Joe Perchesb9a67892014-09-09 21:17:29 -0700727 pr_warn("got v %d < 5 cv %d of ceph_pg_pool\n", ev, cv);
Sage Weil4f6a7e52013-02-23 10:41:09 -0800728 return -EINVAL;
729 }
Ilya Dryomov17a13e402014-01-27 17:40:19 +0200730 if (cv > 9) {
Joe Perchesb9a67892014-09-09 21:17:29 -0700731 pr_warn("got v %d cv %d > 9 of ceph_pg_pool\n", ev, cv);
Sage Weil4f6a7e52013-02-23 10:41:09 -0800732 return -EINVAL;
733 }
734 len = ceph_decode_32(p);
735 ceph_decode_need(p, end, len, bad);
736 pool_end = *p + len;
Sage Weil73a7e692010-08-02 11:00:55 -0700737
Sage Weil4f6a7e52013-02-23 10:41:09 -0800738 pi->type = ceph_decode_8(p);
739 pi->size = ceph_decode_8(p);
740 pi->crush_ruleset = ceph_decode_8(p);
741 pi->object_hash = ceph_decode_8(p);
742
743 pi->pg_num = ceph_decode_32(p);
744 pi->pgp_num = ceph_decode_32(p);
745
746 *p += 4 + 4; /* skip lpg* */
747 *p += 4; /* skip last_change */
748 *p += 8 + 4; /* skip snap_seq, snap_epoch */
749
750 /* skip snaps */
751 num = ceph_decode_32(p);
752 while (num--) {
753 *p += 8; /* snapid key */
754 *p += 1 + 1; /* versions */
755 len = ceph_decode_32(p);
756 *p += len;
Sage Weil73a7e692010-08-02 11:00:55 -0700757 }
758
Ilya Dryomov17a13e402014-01-27 17:40:19 +0200759 /* skip removed_snaps */
Sage Weil4f6a7e52013-02-23 10:41:09 -0800760 num = ceph_decode_32(p);
761 *p += num * (8 + 8);
762
763 *p += 8; /* skip auid */
764 pi->flags = ceph_decode_64(p);
Ilya Dryomov17a13e402014-01-27 17:40:19 +0200765 *p += 4; /* skip crash_replay_interval */
766
767 if (ev >= 7)
Ilya Dryomov04812ac2016-04-28 16:07:23 +0200768 pi->min_size = ceph_decode_8(p);
769 else
770 pi->min_size = pi->size - pi->size / 2;
Ilya Dryomov17a13e402014-01-27 17:40:19 +0200771
772 if (ev >= 8)
773 *p += 8 + 8; /* skip quota_max_* */
774
775 if (ev >= 9) {
776 /* skip tiers */
777 num = ceph_decode_32(p);
778 *p += num * 8;
779
780 *p += 8; /* skip tier_of */
781 *p += 1; /* skip cache_mode */
782
783 pi->read_tier = ceph_decode_64(p);
784 pi->write_tier = ceph_decode_64(p);
785 } else {
786 pi->read_tier = -1;
787 pi->write_tier = -1;
788 }
Sage Weil4f6a7e52013-02-23 10:41:09 -0800789
Ilya Dryomov04812ac2016-04-28 16:07:23 +0200790 if (ev >= 10) {
791 /* skip properties */
792 num = ceph_decode_32(p);
793 while (num--) {
794 len = ceph_decode_32(p);
795 *p += len; /* key */
796 len = ceph_decode_32(p);
797 *p += len; /* val */
798 }
799 }
800
801 if (ev >= 11) {
802 /* skip hit_set_params */
803 *p += 1 + 1; /* versions */
804 len = ceph_decode_32(p);
805 *p += len;
806
807 *p += 4; /* skip hit_set_period */
808 *p += 4; /* skip hit_set_count */
809 }
810
811 if (ev >= 12)
812 *p += 4; /* skip stripe_width */
813
814 if (ev >= 13) {
815 *p += 8; /* skip target_max_bytes */
816 *p += 8; /* skip target_max_objects */
817 *p += 4; /* skip cache_target_dirty_ratio_micro */
818 *p += 4; /* skip cache_target_full_ratio_micro */
819 *p += 4; /* skip cache_min_flush_age */
820 *p += 4; /* skip cache_min_evict_age */
821 }
822
823 if (ev >= 14) {
824 /* skip erasure_code_profile */
825 len = ceph_decode_32(p);
826 *p += len;
827 }
828
Ilya Dryomov8e48cf02017-06-05 14:45:00 +0200829 /*
830 * last_force_op_resend_preluminous, will be overridden if the
831 * map was encoded with RESEND_ON_SPLIT
832 */
Ilya Dryomov04812ac2016-04-28 16:07:23 +0200833 if (ev >= 15)
834 pi->last_force_request_resend = ceph_decode_32(p);
835 else
836 pi->last_force_request_resend = 0;
837
Ilya Dryomov8e48cf02017-06-05 14:45:00 +0200838 if (ev >= 16)
839 *p += 4; /* skip min_read_recency_for_promote */
840
841 if (ev >= 17)
842 *p += 8; /* skip expected_num_objects */
843
844 if (ev >= 19)
845 *p += 4; /* skip cache_target_dirty_high_ratio_micro */
846
847 if (ev >= 20)
848 *p += 4; /* skip min_write_recency_for_promote */
849
850 if (ev >= 21)
851 *p += 1; /* skip use_gmt_hitset */
852
853 if (ev >= 22)
854 *p += 1; /* skip fast_read */
855
856 if (ev >= 23) {
857 *p += 4; /* skip hit_set_grade_decay_rate */
858 *p += 4; /* skip hit_set_search_last_n */
859 }
860
861 if (ev >= 24) {
862 /* skip opts */
863 *p += 1 + 1; /* versions */
864 len = ceph_decode_32(p);
865 *p += len;
866 }
867
868 if (ev >= 25)
869 pi->last_force_request_resend = ceph_decode_32(p);
870
Sage Weil4f6a7e52013-02-23 10:41:09 -0800871 /* ignore the rest */
872
873 *p = pool_end;
874 calc_pg_masks(pi);
Sage Weil73a7e692010-08-02 11:00:55 -0700875 return 0;
876
877bad:
878 return -EINVAL;
Sage Weilefd75762010-03-17 10:05:28 -0700879}
880
Ilya Dryomov0f70c7e2014-03-21 19:05:27 +0200881static int decode_pool_names(void **p, void *end, struct ceph_osdmap *map)
Sage Weil2844a762010-04-09 15:46:42 -0700882{
883 struct ceph_pg_pool_info *pi;
Sage Weil4f6a7e52013-02-23 10:41:09 -0800884 u32 num, len;
885 u64 pool;
Sage Weil2844a762010-04-09 15:46:42 -0700886
887 ceph_decode_32_safe(p, end, num, bad);
888 dout(" %d pool names\n", num);
889 while (num--) {
Sage Weil4f6a7e52013-02-23 10:41:09 -0800890 ceph_decode_64_safe(p, end, pool, bad);
Sage Weil2844a762010-04-09 15:46:42 -0700891 ceph_decode_32_safe(p, end, len, bad);
Sage Weil4f6a7e52013-02-23 10:41:09 -0800892 dout(" pool %llu len %d\n", pool, len);
Xi Wangad3b9042012-06-06 19:35:55 -0500893 ceph_decode_need(p, end, len, bad);
Sage Weil2844a762010-04-09 15:46:42 -0700894 pi = __lookup_pg_pool(&map->pg_pools, pool);
895 if (pi) {
Xi Wangad3b9042012-06-06 19:35:55 -0500896 char *name = kstrndup(*p, len, GFP_NOFS);
897
898 if (!name)
899 return -ENOMEM;
Sage Weil2844a762010-04-09 15:46:42 -0700900 kfree(pi->name);
Xi Wangad3b9042012-06-06 19:35:55 -0500901 pi->name = name;
902 dout(" name is %s\n", pi->name);
Sage Weil2844a762010-04-09 15:46:42 -0700903 }
904 *p += len;
905 }
906 return 0;
907
908bad:
909 return -EINVAL;
910}
911
912/*
913 * osd map
914 */
Ilya Dryomove5253a72016-04-28 16:07:25 +0200915struct ceph_osdmap *ceph_osdmap_alloc(void)
916{
917 struct ceph_osdmap *map;
918
919 map = kzalloc(sizeof(*map), GFP_NOIO);
920 if (!map)
921 return NULL;
922
923 map->pg_pools = RB_ROOT;
924 map->pool_max = -1;
925 map->pg_temp = RB_ROOT;
926 map->primary_temp = RB_ROOT;
Ilya Dryomov6f428df2017-06-21 17:27:18 +0200927 map->pg_upmap = RB_ROOT;
928 map->pg_upmap_items = RB_ROOT;
Ilya Dryomov743efcf2017-01-31 15:55:06 +0100929 mutex_init(&map->crush_workspace_mutex);
Ilya Dryomove5253a72016-04-28 16:07:25 +0200930
931 return map;
932}
933
Sage Weil2844a762010-04-09 15:46:42 -0700934void ceph_osdmap_destroy(struct ceph_osdmap *map)
935{
936 dout("osdmap_destroy %p\n", map);
937 if (map->crush)
938 crush_destroy(map->crush);
939 while (!RB_EMPTY_ROOT(&map->pg_temp)) {
940 struct ceph_pg_mapping *pg =
941 rb_entry(rb_first(&map->pg_temp),
942 struct ceph_pg_mapping, node);
Ilya Dryomovab751442017-06-21 17:27:17 +0200943 erase_pg_mapping(&map->pg_temp, pg);
944 free_pg_mapping(pg);
Sage Weil2844a762010-04-09 15:46:42 -0700945 }
Ilya Dryomov9686f942014-03-21 19:05:29 +0200946 while (!RB_EMPTY_ROOT(&map->primary_temp)) {
947 struct ceph_pg_mapping *pg =
948 rb_entry(rb_first(&map->primary_temp),
949 struct ceph_pg_mapping, node);
Ilya Dryomovab751442017-06-21 17:27:17 +0200950 erase_pg_mapping(&map->primary_temp, pg);
951 free_pg_mapping(pg);
Ilya Dryomov9686f942014-03-21 19:05:29 +0200952 }
Ilya Dryomov6f428df2017-06-21 17:27:18 +0200953 while (!RB_EMPTY_ROOT(&map->pg_upmap)) {
954 struct ceph_pg_mapping *pg =
955 rb_entry(rb_first(&map->pg_upmap),
956 struct ceph_pg_mapping, node);
957 rb_erase(&pg->node, &map->pg_upmap);
958 kfree(pg);
959 }
960 while (!RB_EMPTY_ROOT(&map->pg_upmap_items)) {
961 struct ceph_pg_mapping *pg =
962 rb_entry(rb_first(&map->pg_upmap_items),
963 struct ceph_pg_mapping, node);
964 rb_erase(&pg->node, &map->pg_upmap_items);
965 kfree(pg);
966 }
Sage Weil2844a762010-04-09 15:46:42 -0700967 while (!RB_EMPTY_ROOT(&map->pg_pools)) {
968 struct ceph_pg_pool_info *pi =
969 rb_entry(rb_first(&map->pg_pools),
970 struct ceph_pg_pool_info, node);
971 __remove_pg_pool(&map->pg_pools, pi);
972 }
973 kfree(map->osd_state);
974 kfree(map->osd_weight);
975 kfree(map->osd_addr);
Ilya Dryomov2cfa34f2014-03-21 19:05:30 +0200976 kfree(map->osd_primary_affinity);
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100977 kfree(map->crush_workspace);
Sage Weil2844a762010-04-09 15:46:42 -0700978 kfree(map);
979}
980
981/*
Ilya Dryomov4d603512014-03-21 19:05:28 +0200982 * Adjust max_osd value, (re)allocate arrays.
983 *
984 * The new elements are properly initialized.
Sage Weil2844a762010-04-09 15:46:42 -0700985 */
986static int osdmap_set_max_osd(struct ceph_osdmap *map, int max)
987{
Ilya Dryomov0bb05da2017-06-22 19:44:06 +0200988 u32 *state;
Sage Weil2844a762010-04-09 15:46:42 -0700989 u32 *weight;
Ilya Dryomov4d603512014-03-21 19:05:28 +0200990 struct ceph_entity_addr *addr;
991 int i;
Sage Weil2844a762010-04-09 15:46:42 -0700992
Ilya Dryomov4d603512014-03-21 19:05:28 +0200993 state = krealloc(map->osd_state, max*sizeof(*state), GFP_NOFS);
Li RongQing589506f2014-09-07 18:10:51 +0800994 if (!state)
Sage Weil2844a762010-04-09 15:46:42 -0700995 return -ENOMEM;
Li RongQing589506f2014-09-07 18:10:51 +0800996 map->osd_state = state;
997
998 weight = krealloc(map->osd_weight, max*sizeof(*weight), GFP_NOFS);
999 if (!weight)
1000 return -ENOMEM;
1001 map->osd_weight = weight;
1002
1003 addr = krealloc(map->osd_addr, max*sizeof(*addr), GFP_NOFS);
1004 if (!addr)
1005 return -ENOMEM;
1006 map->osd_addr = addr;
Sage Weil2844a762010-04-09 15:46:42 -07001007
Ilya Dryomov4d603512014-03-21 19:05:28 +02001008 for (i = map->max_osd; i < max; i++) {
Li RongQing589506f2014-09-07 18:10:51 +08001009 map->osd_state[i] = 0;
1010 map->osd_weight[i] = CEPH_OSD_OUT;
1011 memset(map->osd_addr + i, 0, sizeof(*map->osd_addr));
Sage Weil2844a762010-04-09 15:46:42 -07001012 }
1013
Ilya Dryomov2cfa34f2014-03-21 19:05:30 +02001014 if (map->osd_primary_affinity) {
1015 u32 *affinity;
1016
1017 affinity = krealloc(map->osd_primary_affinity,
1018 max*sizeof(*affinity), GFP_NOFS);
1019 if (!affinity)
1020 return -ENOMEM;
Li RongQing589506f2014-09-07 18:10:51 +08001021 map->osd_primary_affinity = affinity;
Ilya Dryomov2cfa34f2014-03-21 19:05:30 +02001022
1023 for (i = map->max_osd; i < max; i++)
Li RongQing589506f2014-09-07 18:10:51 +08001024 map->osd_primary_affinity[i] =
1025 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
Ilya Dryomov2cfa34f2014-03-21 19:05:30 +02001026 }
1027
Sage Weil2844a762010-04-09 15:46:42 -07001028 map->max_osd = max;
Ilya Dryomov4d603512014-03-21 19:05:28 +02001029
Sage Weil2844a762010-04-09 15:46:42 -07001030 return 0;
1031}
1032
Ilya Dryomov1b6a78b2017-01-31 15:55:06 +01001033static int osdmap_set_crush(struct ceph_osdmap *map, struct crush_map *crush)
1034{
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +01001035 void *workspace;
Ilya Dryomov743efcf2017-01-31 15:55:06 +01001036 size_t work_size;
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +01001037
Ilya Dryomov1b6a78b2017-01-31 15:55:06 +01001038 if (IS_ERR(crush))
1039 return PTR_ERR(crush);
1040
Ilya Dryomov743efcf2017-01-31 15:55:06 +01001041 work_size = crush_work_size(crush, CEPH_PG_MAX_SIZE);
1042 dout("%s work_size %zu bytes\n", __func__, work_size);
1043 workspace = kmalloc(work_size, GFP_NOIO);
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +01001044 if (!workspace) {
1045 crush_destroy(crush);
1046 return -ENOMEM;
1047 }
1048 crush_init_workspace(crush, workspace);
1049
Ilya Dryomov1b6a78b2017-01-31 15:55:06 +01001050 if (map->crush)
1051 crush_destroy(map->crush);
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +01001052 kfree(map->crush_workspace);
Ilya Dryomov1b6a78b2017-01-31 15:55:06 +01001053 map->crush = crush;
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +01001054 map->crush_workspace = workspace;
Ilya Dryomov1b6a78b2017-01-31 15:55:06 +01001055 return 0;
1056}
1057
Ilya Dryomovec7af972014-03-21 19:05:29 +02001058#define OSDMAP_WRAPPER_COMPAT_VER 7
1059#define OSDMAP_CLIENT_DATA_COMPAT_VER 1
1060
1061/*
1062 * Return 0 or error. On success, *v is set to 0 for old (v6) osdmaps,
1063 * to struct_v of the client_data section for new (v7 and above)
1064 * osdmaps.
1065 */
1066static int get_osdmap_client_data_v(void **p, void *end,
1067 const char *prefix, u8 *v)
1068{
1069 u8 struct_v;
1070
1071 ceph_decode_8_safe(p, end, struct_v, e_inval);
1072 if (struct_v >= 7) {
1073 u8 struct_compat;
1074
1075 ceph_decode_8_safe(p, end, struct_compat, e_inval);
1076 if (struct_compat > OSDMAP_WRAPPER_COMPAT_VER) {
Joe Perchesb9a67892014-09-09 21:17:29 -07001077 pr_warn("got v %d cv %d > %d of %s ceph_osdmap\n",
1078 struct_v, struct_compat,
1079 OSDMAP_WRAPPER_COMPAT_VER, prefix);
Ilya Dryomovec7af972014-03-21 19:05:29 +02001080 return -EINVAL;
1081 }
1082 *p += 4; /* ignore wrapper struct_len */
1083
1084 ceph_decode_8_safe(p, end, struct_v, e_inval);
1085 ceph_decode_8_safe(p, end, struct_compat, e_inval);
1086 if (struct_compat > OSDMAP_CLIENT_DATA_COMPAT_VER) {
Joe Perchesb9a67892014-09-09 21:17:29 -07001087 pr_warn("got v %d cv %d > %d of %s ceph_osdmap client data\n",
1088 struct_v, struct_compat,
1089 OSDMAP_CLIENT_DATA_COMPAT_VER, prefix);
Ilya Dryomovec7af972014-03-21 19:05:29 +02001090 return -EINVAL;
1091 }
1092 *p += 4; /* ignore client data struct_len */
1093 } else {
1094 u16 version;
1095
1096 *p -= 1;
1097 ceph_decode_16_safe(p, end, version, e_inval);
1098 if (version < 6) {
Joe Perchesb9a67892014-09-09 21:17:29 -07001099 pr_warn("got v %d < 6 of %s ceph_osdmap\n",
1100 version, prefix);
Ilya Dryomovec7af972014-03-21 19:05:29 +02001101 return -EINVAL;
1102 }
1103
1104 /* old osdmap enconding */
1105 struct_v = 0;
1106 }
1107
1108 *v = struct_v;
1109 return 0;
1110
1111e_inval:
1112 return -EINVAL;
1113}
1114
Ilya Dryomov433fbdd2014-03-21 19:05:27 +02001115static int __decode_pools(void **p, void *end, struct ceph_osdmap *map,
1116 bool incremental)
1117{
1118 u32 n;
1119
1120 ceph_decode_32_safe(p, end, n, e_inval);
1121 while (n--) {
1122 struct ceph_pg_pool_info *pi;
1123 u64 pool;
1124 int ret;
1125
1126 ceph_decode_64_safe(p, end, pool, e_inval);
1127
1128 pi = __lookup_pg_pool(&map->pg_pools, pool);
1129 if (!incremental || !pi) {
1130 pi = kzalloc(sizeof(*pi), GFP_NOFS);
1131 if (!pi)
1132 return -ENOMEM;
1133
1134 pi->id = pool;
1135
1136 ret = __insert_pg_pool(&map->pg_pools, pi);
1137 if (ret) {
1138 kfree(pi);
1139 return ret;
1140 }
1141 }
1142
1143 ret = decode_pool(p, end, pi);
1144 if (ret)
1145 return ret;
1146 }
1147
1148 return 0;
1149
1150e_inval:
1151 return -EINVAL;
1152}
1153
1154static int decode_pools(void **p, void *end, struct ceph_osdmap *map)
1155{
1156 return __decode_pools(p, end, map, false);
1157}
1158
1159static int decode_new_pools(void **p, void *end, struct ceph_osdmap *map)
1160{
1161 return __decode_pools(p, end, map, true);
1162}
1163
Ilya Dryomova303bb02017-06-21 17:27:17 +02001164typedef struct ceph_pg_mapping *(*decode_mapping_fn_t)(void **, void *, bool);
1165
1166static int decode_pg_mapping(void **p, void *end, struct rb_root *mapping_root,
1167 decode_mapping_fn_t fn, bool incremental)
Ilya Dryomov10db6342014-03-21 19:05:28 +02001168{
1169 u32 n;
1170
Ilya Dryomova303bb02017-06-21 17:27:17 +02001171 WARN_ON(!incremental && !fn);
1172
Ilya Dryomov10db6342014-03-21 19:05:28 +02001173 ceph_decode_32_safe(p, end, n, e_inval);
1174 while (n--) {
Ilya Dryomova303bb02017-06-21 17:27:17 +02001175 struct ceph_pg_mapping *pg;
Ilya Dryomov10db6342014-03-21 19:05:28 +02001176 struct ceph_pg pgid;
Ilya Dryomov10db6342014-03-21 19:05:28 +02001177 int ret;
1178
1179 ret = ceph_decode_pgid(p, end, &pgid);
1180 if (ret)
1181 return ret;
1182
Ilya Dryomovab751442017-06-21 17:27:17 +02001183 pg = lookup_pg_mapping(mapping_root, &pgid);
1184 if (pg) {
1185 WARN_ON(!incremental);
1186 erase_pg_mapping(mapping_root, pg);
1187 free_pg_mapping(pg);
1188 }
Ilya Dryomov10db6342014-03-21 19:05:28 +02001189
Ilya Dryomova303bb02017-06-21 17:27:17 +02001190 if (fn) {
1191 pg = fn(p, end, incremental);
1192 if (IS_ERR(pg))
1193 return PTR_ERR(pg);
Ilya Dryomov10db6342014-03-21 19:05:28 +02001194
Ilya Dryomova303bb02017-06-21 17:27:17 +02001195 if (pg) {
1196 pg->pgid = pgid; /* struct */
Ilya Dryomovab751442017-06-21 17:27:17 +02001197 insert_pg_mapping(mapping_root, pg);
Ilya Dryomov10db6342014-03-21 19:05:28 +02001198 }
1199 }
1200 }
1201
1202 return 0;
1203
1204e_inval:
1205 return -EINVAL;
1206}
1207
Ilya Dryomova303bb02017-06-21 17:27:17 +02001208static struct ceph_pg_mapping *__decode_pg_temp(void **p, void *end,
1209 bool incremental)
1210{
1211 struct ceph_pg_mapping *pg;
1212 u32 len, i;
1213
1214 ceph_decode_32_safe(p, end, len, e_inval);
1215 if (len == 0 && incremental)
1216 return NULL; /* new_pg_temp: [] to remove */
1217 if (len > (SIZE_MAX - sizeof(*pg)) / sizeof(u32))
1218 return ERR_PTR(-EINVAL);
1219
1220 ceph_decode_need(p, end, len * sizeof(u32), e_inval);
1221 pg = alloc_pg_mapping(len * sizeof(u32));
1222 if (!pg)
1223 return ERR_PTR(-ENOMEM);
1224
1225 pg->pg_temp.len = len;
1226 for (i = 0; i < len; i++)
1227 pg->pg_temp.osds[i] = ceph_decode_32(p);
1228
1229 return pg;
1230
1231e_inval:
1232 return ERR_PTR(-EINVAL);
1233}
1234
Ilya Dryomov10db6342014-03-21 19:05:28 +02001235static int decode_pg_temp(void **p, void *end, struct ceph_osdmap *map)
1236{
Ilya Dryomova303bb02017-06-21 17:27:17 +02001237 return decode_pg_mapping(p, end, &map->pg_temp, __decode_pg_temp,
1238 false);
Ilya Dryomov10db6342014-03-21 19:05:28 +02001239}
1240
1241static int decode_new_pg_temp(void **p, void *end, struct ceph_osdmap *map)
1242{
Ilya Dryomova303bb02017-06-21 17:27:17 +02001243 return decode_pg_mapping(p, end, &map->pg_temp, __decode_pg_temp,
1244 true);
Ilya Dryomov10db6342014-03-21 19:05:28 +02001245}
1246
Ilya Dryomova303bb02017-06-21 17:27:17 +02001247static struct ceph_pg_mapping *__decode_primary_temp(void **p, void *end,
1248 bool incremental)
Ilya Dryomovd286de72014-03-21 19:05:30 +02001249{
Ilya Dryomova303bb02017-06-21 17:27:17 +02001250 struct ceph_pg_mapping *pg;
1251 u32 osd;
Ilya Dryomovd286de72014-03-21 19:05:30 +02001252
Ilya Dryomova303bb02017-06-21 17:27:17 +02001253 ceph_decode_32_safe(p, end, osd, e_inval);
1254 if (osd == (u32)-1 && incremental)
1255 return NULL; /* new_primary_temp: -1 to remove */
Ilya Dryomovd286de72014-03-21 19:05:30 +02001256
Ilya Dryomova303bb02017-06-21 17:27:17 +02001257 pg = alloc_pg_mapping(0);
1258 if (!pg)
1259 return ERR_PTR(-ENOMEM);
Ilya Dryomovd286de72014-03-21 19:05:30 +02001260
Ilya Dryomova303bb02017-06-21 17:27:17 +02001261 pg->primary_temp.osd = osd;
1262 return pg;
Ilya Dryomovd286de72014-03-21 19:05:30 +02001263
1264e_inval:
Ilya Dryomova303bb02017-06-21 17:27:17 +02001265 return ERR_PTR(-EINVAL);
Ilya Dryomovd286de72014-03-21 19:05:30 +02001266}
1267
1268static int decode_primary_temp(void **p, void *end, struct ceph_osdmap *map)
1269{
Ilya Dryomova303bb02017-06-21 17:27:17 +02001270 return decode_pg_mapping(p, end, &map->primary_temp,
1271 __decode_primary_temp, false);
Ilya Dryomovd286de72014-03-21 19:05:30 +02001272}
1273
1274static int decode_new_primary_temp(void **p, void *end,
1275 struct ceph_osdmap *map)
1276{
Ilya Dryomova303bb02017-06-21 17:27:17 +02001277 return decode_pg_mapping(p, end, &map->primary_temp,
1278 __decode_primary_temp, true);
Ilya Dryomovd286de72014-03-21 19:05:30 +02001279}
1280
Ilya Dryomov2cfa34f2014-03-21 19:05:30 +02001281u32 ceph_get_primary_affinity(struct ceph_osdmap *map, int osd)
1282{
1283 BUG_ON(osd >= map->max_osd);
1284
1285 if (!map->osd_primary_affinity)
1286 return CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
1287
1288 return map->osd_primary_affinity[osd];
1289}
1290
1291static int set_primary_affinity(struct ceph_osdmap *map, int osd, u32 aff)
1292{
1293 BUG_ON(osd >= map->max_osd);
1294
1295 if (!map->osd_primary_affinity) {
1296 int i;
1297
1298 map->osd_primary_affinity = kmalloc(map->max_osd*sizeof(u32),
1299 GFP_NOFS);
1300 if (!map->osd_primary_affinity)
1301 return -ENOMEM;
1302
1303 for (i = 0; i < map->max_osd; i++)
1304 map->osd_primary_affinity[i] =
1305 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
1306 }
1307
1308 map->osd_primary_affinity[osd] = aff;
1309
1310 return 0;
1311}
1312
Ilya Dryomov63a69932014-03-21 19:05:30 +02001313static int decode_primary_affinity(void **p, void *end,
1314 struct ceph_osdmap *map)
1315{
1316 u32 len, i;
1317
1318 ceph_decode_32_safe(p, end, len, e_inval);
1319 if (len == 0) {
1320 kfree(map->osd_primary_affinity);
1321 map->osd_primary_affinity = NULL;
1322 return 0;
1323 }
1324 if (len != map->max_osd)
1325 goto e_inval;
1326
1327 ceph_decode_need(p, end, map->max_osd*sizeof(u32), e_inval);
1328
1329 for (i = 0; i < map->max_osd; i++) {
1330 int ret;
1331
1332 ret = set_primary_affinity(map, i, ceph_decode_32(p));
1333 if (ret)
1334 return ret;
1335 }
1336
1337 return 0;
1338
1339e_inval:
1340 return -EINVAL;
1341}
1342
1343static int decode_new_primary_affinity(void **p, void *end,
1344 struct ceph_osdmap *map)
1345{
1346 u32 n;
1347
1348 ceph_decode_32_safe(p, end, n, e_inval);
1349 while (n--) {
1350 u32 osd, aff;
1351 int ret;
1352
1353 ceph_decode_32_safe(p, end, osd, e_inval);
1354 ceph_decode_32_safe(p, end, aff, e_inval);
1355
1356 ret = set_primary_affinity(map, osd, aff);
1357 if (ret)
1358 return ret;
Ilya Dryomovf31da0f2014-04-02 20:34:04 +04001359
1360 pr_info("osd%d primary-affinity 0x%x\n", osd, aff);
Ilya Dryomov63a69932014-03-21 19:05:30 +02001361 }
1362
1363 return 0;
1364
1365e_inval:
1366 return -EINVAL;
1367}
1368
Ilya Dryomov6f428df2017-06-21 17:27:18 +02001369static struct ceph_pg_mapping *__decode_pg_upmap(void **p, void *end,
1370 bool __unused)
1371{
1372 return __decode_pg_temp(p, end, false);
1373}
1374
1375static int decode_pg_upmap(void **p, void *end, struct ceph_osdmap *map)
1376{
1377 return decode_pg_mapping(p, end, &map->pg_upmap, __decode_pg_upmap,
1378 false);
1379}
1380
1381static int decode_new_pg_upmap(void **p, void *end, struct ceph_osdmap *map)
1382{
1383 return decode_pg_mapping(p, end, &map->pg_upmap, __decode_pg_upmap,
1384 true);
1385}
1386
1387static int decode_old_pg_upmap(void **p, void *end, struct ceph_osdmap *map)
1388{
1389 return decode_pg_mapping(p, end, &map->pg_upmap, NULL, true);
1390}
1391
1392static struct ceph_pg_mapping *__decode_pg_upmap_items(void **p, void *end,
1393 bool __unused)
1394{
1395 struct ceph_pg_mapping *pg;
1396 u32 len, i;
1397
1398 ceph_decode_32_safe(p, end, len, e_inval);
1399 if (len > (SIZE_MAX - sizeof(*pg)) / (2 * sizeof(u32)))
1400 return ERR_PTR(-EINVAL);
1401
1402 ceph_decode_need(p, end, 2 * len * sizeof(u32), e_inval);
Ilya Dryomovf5cc6892017-07-07 16:14:45 +02001403 pg = alloc_pg_mapping(2 * len * sizeof(u32));
Ilya Dryomov6f428df2017-06-21 17:27:18 +02001404 if (!pg)
1405 return ERR_PTR(-ENOMEM);
1406
1407 pg->pg_upmap_items.len = len;
1408 for (i = 0; i < len; i++) {
1409 pg->pg_upmap_items.from_to[i][0] = ceph_decode_32(p);
1410 pg->pg_upmap_items.from_to[i][1] = ceph_decode_32(p);
1411 }
1412
1413 return pg;
1414
1415e_inval:
1416 return ERR_PTR(-EINVAL);
1417}
1418
1419static int decode_pg_upmap_items(void **p, void *end, struct ceph_osdmap *map)
1420{
1421 return decode_pg_mapping(p, end, &map->pg_upmap_items,
1422 __decode_pg_upmap_items, false);
1423}
1424
1425static int decode_new_pg_upmap_items(void **p, void *end,
1426 struct ceph_osdmap *map)
1427{
1428 return decode_pg_mapping(p, end, &map->pg_upmap_items,
1429 __decode_pg_upmap_items, true);
1430}
1431
1432static int decode_old_pg_upmap_items(void **p, void *end,
1433 struct ceph_osdmap *map)
1434{
1435 return decode_pg_mapping(p, end, &map->pg_upmap_items, NULL, true);
1436}
1437
Sage Weil4fc51be2010-02-16 15:55:03 -08001438/*
Sage Weilf24e9982009-10-06 11:31:10 -07001439 * decode a full map.
1440 */
Ilya Dryomova2505d62014-03-13 16:36:13 +02001441static int osdmap_decode(void **p, void *end, struct ceph_osdmap *map)
Sage Weilf24e9982009-10-06 11:31:10 -07001442{
Ilya Dryomovec7af972014-03-21 19:05:29 +02001443 u8 struct_v;
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001444 u32 epoch = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001445 void *start = *p;
Ilya Dryomov39770582014-03-13 16:36:14 +02001446 u32 max;
1447 u32 len, i;
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001448 int err;
Sage Weilf24e9982009-10-06 11:31:10 -07001449
Ilya Dryomova2505d62014-03-13 16:36:13 +02001450 dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p));
Sage Weilf24e9982009-10-06 11:31:10 -07001451
Ilya Dryomovec7af972014-03-21 19:05:29 +02001452 err = get_osdmap_client_data_v(p, end, "full", &struct_v);
1453 if (err)
1454 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001455
Ilya Dryomov53bbaba2014-03-13 16:36:16 +02001456 /* fsid, epoch, created, modified */
1457 ceph_decode_need(p, end, sizeof(map->fsid) + sizeof(u32) +
1458 sizeof(map->created) + sizeof(map->modified), e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001459 ceph_decode_copy(p, &map->fsid, sizeof(map->fsid));
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001460 epoch = map->epoch = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -07001461 ceph_decode_copy(p, &map->created, sizeof(map->created));
1462 ceph_decode_copy(p, &map->modified, sizeof(map->modified));
1463
Ilya Dryomov433fbdd2014-03-21 19:05:27 +02001464 /* pools */
1465 err = decode_pools(p, end, map);
1466 if (err)
1467 goto bad;
Sage Weil2844a762010-04-09 15:46:42 -07001468
Ilya Dryomov0f70c7e2014-03-21 19:05:27 +02001469 /* pool_name */
1470 err = decode_pool_names(p, end, map);
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001471 if (err)
Sage Weil4f6a7e52013-02-23 10:41:09 -08001472 goto bad;
Sage Weil2844a762010-04-09 15:46:42 -07001473
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001474 ceph_decode_32_safe(p, end, map->pool_max, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001475
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001476 ceph_decode_32_safe(p, end, map->flags, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001477
Ilya Dryomov39770582014-03-13 16:36:14 +02001478 /* max_osd */
1479 ceph_decode_32_safe(p, end, max, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001480
1481 /* (re)alloc osd arrays */
1482 err = osdmap_set_max_osd(map, max);
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001483 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001484 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001485
Ilya Dryomov2d88b2e2014-03-13 16:36:14 +02001486 /* osd_state, osd_weight, osd_addrs->client_addr */
Sage Weilf24e9982009-10-06 11:31:10 -07001487 ceph_decode_need(p, end, 3*sizeof(u32) +
Ilya Dryomov0bb05da2017-06-22 19:44:06 +02001488 map->max_osd*((struct_v >= 5 ? sizeof(u32) :
1489 sizeof(u8)) +
1490 sizeof(*map->osd_weight) +
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001491 sizeof(*map->osd_addr)), e_inval);
1492
Ilya Dryomov2d88b2e2014-03-13 16:36:14 +02001493 if (ceph_decode_32(p) != map->max_osd)
1494 goto e_inval;
1495
Ilya Dryomov0bb05da2017-06-22 19:44:06 +02001496 if (struct_v >= 5) {
1497 for (i = 0; i < map->max_osd; i++)
1498 map->osd_state[i] = ceph_decode_32(p);
1499 } else {
1500 for (i = 0; i < map->max_osd; i++)
1501 map->osd_state[i] = ceph_decode_8(p);
1502 }
Sage Weilf24e9982009-10-06 11:31:10 -07001503
Ilya Dryomov2d88b2e2014-03-13 16:36:14 +02001504 if (ceph_decode_32(p) != map->max_osd)
1505 goto e_inval;
1506
Sage Weilf24e9982009-10-06 11:31:10 -07001507 for (i = 0; i < map->max_osd; i++)
Sage Weilc89136e2009-10-14 09:59:09 -07001508 map->osd_weight[i] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -07001509
Ilya Dryomov2d88b2e2014-03-13 16:36:14 +02001510 if (ceph_decode_32(p) != map->max_osd)
1511 goto e_inval;
1512
Sage Weilf24e9982009-10-06 11:31:10 -07001513 ceph_decode_copy(p, map->osd_addr, map->max_osd*sizeof(*map->osd_addr));
Sage Weil63f2d212009-11-03 15:17:56 -08001514 for (i = 0; i < map->max_osd; i++)
1515 ceph_decode_addr(&map->osd_addr[i]);
Sage Weilf24e9982009-10-06 11:31:10 -07001516
1517 /* pg_temp */
Ilya Dryomov10db6342014-03-21 19:05:28 +02001518 err = decode_pg_temp(p, end, map);
1519 if (err)
1520 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001521
Ilya Dryomovd286de72014-03-21 19:05:30 +02001522 /* primary_temp */
1523 if (struct_v >= 1) {
1524 err = decode_primary_temp(p, end, map);
1525 if (err)
1526 goto bad;
1527 }
1528
Ilya Dryomov63a69932014-03-21 19:05:30 +02001529 /* primary_affinity */
1530 if (struct_v >= 2) {
1531 err = decode_primary_affinity(p, end, map);
1532 if (err)
1533 goto bad;
1534 } else {
Ilya Dryomov6f428df2017-06-21 17:27:18 +02001535 WARN_ON(map->osd_primary_affinity);
Ilya Dryomov63a69932014-03-21 19:05:30 +02001536 }
1537
Sage Weilf24e9982009-10-06 11:31:10 -07001538 /* crush */
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001539 ceph_decode_32_safe(p, end, len, e_inval);
Ilya Dryomov1b6a78b2017-01-31 15:55:06 +01001540 err = osdmap_set_crush(map, crush_decode(*p, min(*p + len, end)));
1541 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001542 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001543
Ilya Dryomov6f428df2017-06-21 17:27:18 +02001544 *p += len;
1545 if (struct_v >= 3) {
1546 /* erasure_code_profiles */
1547 ceph_decode_skip_map_of_map(p, end, string, string, string,
Dan Carpenter00c8ebb32017-07-13 10:45:17 +03001548 e_inval);
Ilya Dryomov6f428df2017-06-21 17:27:18 +02001549 }
1550
1551 if (struct_v >= 4) {
1552 err = decode_pg_upmap(p, end, map);
1553 if (err)
1554 goto bad;
1555
1556 err = decode_pg_upmap_items(p, end, map);
1557 if (err)
1558 goto bad;
1559 } else {
1560 WARN_ON(!RB_EMPTY_ROOT(&map->pg_upmap));
1561 WARN_ON(!RB_EMPTY_ROOT(&map->pg_upmap_items));
1562 }
1563
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001564 /* ignore the rest */
Sage Weilf24e9982009-10-06 11:31:10 -07001565 *p = end;
1566
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001567 dout("full osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd);
Ilya Dryomova2505d62014-03-13 16:36:13 +02001568 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001569
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001570e_inval:
1571 err = -EINVAL;
Sage Weilf24e9982009-10-06 11:31:10 -07001572bad:
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001573 pr_err("corrupt full osdmap (%d) epoch %d off %d (%p of %p-%p)\n",
1574 err, epoch, (int)(*p - start), *p, start, end);
1575 print_hex_dump(KERN_DEBUG, "osdmap: ",
1576 DUMP_PREFIX_OFFSET, 16, 1,
1577 start, end - start, true);
Ilya Dryomova2505d62014-03-13 16:36:13 +02001578 return err;
1579}
1580
1581/*
1582 * Allocate and decode a full map.
1583 */
1584struct ceph_osdmap *ceph_osdmap_decode(void **p, void *end)
1585{
1586 struct ceph_osdmap *map;
1587 int ret;
1588
Ilya Dryomove5253a72016-04-28 16:07:25 +02001589 map = ceph_osdmap_alloc();
Ilya Dryomova2505d62014-03-13 16:36:13 +02001590 if (!map)
1591 return ERR_PTR(-ENOMEM);
1592
Ilya Dryomova2505d62014-03-13 16:36:13 +02001593 ret = osdmap_decode(p, end, map);
1594 if (ret) {
1595 ceph_osdmap_destroy(map);
1596 return ERR_PTR(ret);
1597 }
1598
1599 return map;
Sage Weilf24e9982009-10-06 11:31:10 -07001600}
1601
1602/*
Ilya Dryomov930c5322016-07-19 03:50:28 +02001603 * Encoding order is (new_up_client, new_state, new_weight). Need to
1604 * apply in the (new_weight, new_state, new_up_client) order, because
1605 * an incremental map may look like e.g.
1606 *
1607 * new_up_client: { osd=6, addr=... } # set osd_state and addr
1608 * new_state: { osd=6, xorstate=EXISTS } # clear osd_state
1609 */
Ilya Dryomov0bb05da2017-06-22 19:44:06 +02001610static int decode_new_up_state_weight(void **p, void *end, u8 struct_v,
Ilya Dryomov930c5322016-07-19 03:50:28 +02001611 struct ceph_osdmap *map)
1612{
1613 void *new_up_client;
1614 void *new_state;
1615 void *new_weight_end;
1616 u32 len;
1617
1618 new_up_client = *p;
1619 ceph_decode_32_safe(p, end, len, e_inval);
1620 len *= sizeof(u32) + sizeof(struct ceph_entity_addr);
1621 ceph_decode_need(p, end, len, e_inval);
1622 *p += len;
1623
1624 new_state = *p;
1625 ceph_decode_32_safe(p, end, len, e_inval);
Ilya Dryomov0bb05da2017-06-22 19:44:06 +02001626 len *= sizeof(u32) + (struct_v >= 5 ? sizeof(u32) : sizeof(u8));
Ilya Dryomov930c5322016-07-19 03:50:28 +02001627 ceph_decode_need(p, end, len, e_inval);
1628 *p += len;
1629
1630 /* new_weight */
1631 ceph_decode_32_safe(p, end, len, e_inval);
1632 while (len--) {
1633 s32 osd;
1634 u32 w;
1635
1636 ceph_decode_need(p, end, 2*sizeof(u32), e_inval);
1637 osd = ceph_decode_32(p);
1638 w = ceph_decode_32(p);
1639 BUG_ON(osd >= map->max_osd);
1640 pr_info("osd%d weight 0x%x %s\n", osd, w,
1641 w == CEPH_OSD_IN ? "(in)" :
1642 (w == CEPH_OSD_OUT ? "(out)" : ""));
1643 map->osd_weight[osd] = w;
1644
1645 /*
1646 * If we are marking in, set the EXISTS, and clear the
1647 * AUTOOUT and NEW bits.
1648 */
1649 if (w) {
1650 map->osd_state[osd] |= CEPH_OSD_EXISTS;
1651 map->osd_state[osd] &= ~(CEPH_OSD_AUTOOUT |
1652 CEPH_OSD_NEW);
1653 }
1654 }
1655 new_weight_end = *p;
1656
1657 /* new_state (up/down) */
1658 *p = new_state;
1659 len = ceph_decode_32(p);
1660 while (len--) {
1661 s32 osd;
Ilya Dryomov0bb05da2017-06-22 19:44:06 +02001662 u32 xorstate;
Ilya Dryomov930c5322016-07-19 03:50:28 +02001663 int ret;
1664
1665 osd = ceph_decode_32(p);
Ilya Dryomov0bb05da2017-06-22 19:44:06 +02001666 if (struct_v >= 5)
1667 xorstate = ceph_decode_32(p);
1668 else
1669 xorstate = ceph_decode_8(p);
Ilya Dryomov930c5322016-07-19 03:50:28 +02001670 if (xorstate == 0)
1671 xorstate = CEPH_OSD_UP;
1672 BUG_ON(osd >= map->max_osd);
1673 if ((map->osd_state[osd] & CEPH_OSD_UP) &&
1674 (xorstate & CEPH_OSD_UP))
1675 pr_info("osd%d down\n", osd);
1676 if ((map->osd_state[osd] & CEPH_OSD_EXISTS) &&
1677 (xorstate & CEPH_OSD_EXISTS)) {
1678 pr_info("osd%d does not exist\n", osd);
Ilya Dryomov930c5322016-07-19 03:50:28 +02001679 ret = set_primary_affinity(map, osd,
1680 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY);
1681 if (ret)
1682 return ret;
1683 memset(map->osd_addr + osd, 0, sizeof(*map->osd_addr));
1684 map->osd_state[osd] = 0;
1685 } else {
1686 map->osd_state[osd] ^= xorstate;
1687 }
1688 }
1689
1690 /* new_up_client */
1691 *p = new_up_client;
1692 len = ceph_decode_32(p);
1693 while (len--) {
1694 s32 osd;
1695 struct ceph_entity_addr addr;
1696
1697 osd = ceph_decode_32(p);
1698 ceph_decode_copy(p, &addr, sizeof(addr));
1699 ceph_decode_addr(&addr);
1700 BUG_ON(osd >= map->max_osd);
1701 pr_info("osd%d up\n", osd);
1702 map->osd_state[osd] |= CEPH_OSD_EXISTS | CEPH_OSD_UP;
1703 map->osd_addr[osd] = addr;
1704 }
1705
1706 *p = new_weight_end;
1707 return 0;
1708
1709e_inval:
1710 return -EINVAL;
1711}
1712
1713/*
Sage Weilf24e9982009-10-06 11:31:10 -07001714 * decode and apply an incremental map update.
1715 */
1716struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end,
Ilya Dryomov0c0a8de2016-04-28 16:07:21 +02001717 struct ceph_osdmap *map)
Sage Weilf24e9982009-10-06 11:31:10 -07001718{
Sage Weilf24e9982009-10-06 11:31:10 -07001719 struct ceph_fsid fsid;
1720 u32 epoch = 0;
1721 struct ceph_timespec modified;
Sage Weil4f6a7e52013-02-23 10:41:09 -08001722 s32 len;
1723 u64 pool;
1724 __s64 new_pool_max;
1725 __s32 new_flags, max;
Sage Weilf24e9982009-10-06 11:31:10 -07001726 void *start = *p;
Ilya Dryomov86f17422014-03-13 16:36:15 +02001727 int err;
Ilya Dryomovec7af972014-03-21 19:05:29 +02001728 u8 struct_v;
Sage Weilf24e9982009-10-06 11:31:10 -07001729
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001730 dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p));
1731
Ilya Dryomovec7af972014-03-21 19:05:29 +02001732 err = get_osdmap_client_data_v(p, end, "inc", &struct_v);
1733 if (err)
1734 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001735
Ilya Dryomov53bbaba2014-03-13 16:36:16 +02001736 /* fsid, epoch, modified, new_pool_max, new_flags */
1737 ceph_decode_need(p, end, sizeof(fsid) + sizeof(u32) + sizeof(modified) +
1738 sizeof(u64) + sizeof(u32), e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001739 ceph_decode_copy(p, &fsid, sizeof(fsid));
Sage Weilc89136e2009-10-14 09:59:09 -07001740 epoch = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -07001741 BUG_ON(epoch != map->epoch+1);
1742 ceph_decode_copy(p, &modified, sizeof(modified));
Sage Weil4f6a7e52013-02-23 10:41:09 -08001743 new_pool_max = ceph_decode_64(p);
Sage Weilc89136e2009-10-14 09:59:09 -07001744 new_flags = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -07001745
1746 /* full map? */
Ilya Dryomov86f17422014-03-13 16:36:15 +02001747 ceph_decode_32_safe(p, end, len, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001748 if (len > 0) {
1749 dout("apply_incremental full map len %d, %p to %p\n",
1750 len, *p, end);
Ilya Dryomova2505d62014-03-13 16:36:13 +02001751 return ceph_osdmap_decode(p, min(*p+len, end));
Sage Weilf24e9982009-10-06 11:31:10 -07001752 }
1753
1754 /* new crush? */
Ilya Dryomov86f17422014-03-13 16:36:15 +02001755 ceph_decode_32_safe(p, end, len, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001756 if (len > 0) {
Ilya Dryomov1b6a78b2017-01-31 15:55:06 +01001757 err = osdmap_set_crush(map,
1758 crush_decode(*p, min(*p + len, end)));
1759 if (err)
Ilya Dryomov86f17422014-03-13 16:36:15 +02001760 goto bad;
Sage Weilcebc5be2010-06-17 10:22:48 -07001761 *p += len;
Sage Weilf24e9982009-10-06 11:31:10 -07001762 }
1763
1764 /* new flags? */
1765 if (new_flags >= 0)
1766 map->flags = new_flags;
Sage Weil4fc51be2010-02-16 15:55:03 -08001767 if (new_pool_max >= 0)
1768 map->pool_max = new_pool_max;
Sage Weilf24e9982009-10-06 11:31:10 -07001769
Sage Weilf24e9982009-10-06 11:31:10 -07001770 /* new max? */
Ilya Dryomov53bbaba2014-03-13 16:36:16 +02001771 ceph_decode_32_safe(p, end, max, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001772 if (max >= 0) {
1773 err = osdmap_set_max_osd(map, max);
Ilya Dryomov86f17422014-03-13 16:36:15 +02001774 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001775 goto bad;
1776 }
1777
1778 map->epoch++;
Sage Weil31456662011-05-12 15:18:43 -07001779 map->modified = modified;
Sage Weilf24e9982009-10-06 11:31:10 -07001780
Ilya Dryomov433fbdd2014-03-21 19:05:27 +02001781 /* new_pools */
1782 err = decode_new_pools(p, end, map);
1783 if (err)
1784 goto bad;
Ilya Dryomov9464d002014-03-13 16:36:16 +02001785
Ilya Dryomov0f70c7e2014-03-21 19:05:27 +02001786 /* new_pool_names */
1787 err = decode_pool_names(p, end, map);
Ilya Dryomov9464d002014-03-13 16:36:16 +02001788 if (err)
1789 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001790
Sage Weil4fc51be2010-02-16 15:55:03 -08001791 /* old_pool */
Ilya Dryomov86f17422014-03-13 16:36:15 +02001792 ceph_decode_32_safe(p, end, len, e_inval);
Sage Weil4fc51be2010-02-16 15:55:03 -08001793 while (len--) {
1794 struct ceph_pg_pool_info *pi;
1795
Ilya Dryomov86f17422014-03-13 16:36:15 +02001796 ceph_decode_64_safe(p, end, pool, e_inval);
Sage Weil4fc51be2010-02-16 15:55:03 -08001797 pi = __lookup_pg_pool(&map->pg_pools, pool);
Sage Weil2844a762010-04-09 15:46:42 -07001798 if (pi)
1799 __remove_pg_pool(&map->pg_pools, pi);
Sage Weil4fc51be2010-02-16 15:55:03 -08001800 }
Sage Weilf24e9982009-10-06 11:31:10 -07001801
Ilya Dryomov930c5322016-07-19 03:50:28 +02001802 /* new_up_client, new_state, new_weight */
Ilya Dryomov0bb05da2017-06-22 19:44:06 +02001803 err = decode_new_up_state_weight(p, end, struct_v, map);
Ilya Dryomov930c5322016-07-19 03:50:28 +02001804 if (err)
1805 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001806
1807 /* new_pg_temp */
Ilya Dryomov10db6342014-03-21 19:05:28 +02001808 err = decode_new_pg_temp(p, end, map);
1809 if (err)
1810 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001811
Ilya Dryomovd286de72014-03-21 19:05:30 +02001812 /* new_primary_temp */
1813 if (struct_v >= 1) {
1814 err = decode_new_primary_temp(p, end, map);
1815 if (err)
1816 goto bad;
1817 }
1818
Ilya Dryomov63a69932014-03-21 19:05:30 +02001819 /* new_primary_affinity */
1820 if (struct_v >= 2) {
1821 err = decode_new_primary_affinity(p, end, map);
1822 if (err)
1823 goto bad;
1824 }
1825
Ilya Dryomov6f428df2017-06-21 17:27:18 +02001826 if (struct_v >= 3) {
1827 /* new_erasure_code_profiles */
1828 ceph_decode_skip_map_of_map(p, end, string, string, string,
Dan Carpenter00c8ebb32017-07-13 10:45:17 +03001829 e_inval);
Ilya Dryomov6f428df2017-06-21 17:27:18 +02001830 /* old_erasure_code_profiles */
Dan Carpenter00c8ebb32017-07-13 10:45:17 +03001831 ceph_decode_skip_set(p, end, string, e_inval);
Ilya Dryomov6f428df2017-06-21 17:27:18 +02001832 }
1833
1834 if (struct_v >= 4) {
1835 err = decode_new_pg_upmap(p, end, map);
1836 if (err)
1837 goto bad;
1838
1839 err = decode_old_pg_upmap(p, end, map);
1840 if (err)
1841 goto bad;
1842
1843 err = decode_new_pg_upmap_items(p, end, map);
1844 if (err)
1845 goto bad;
1846
1847 err = decode_old_pg_upmap_items(p, end, map);
1848 if (err)
1849 goto bad;
1850 }
1851
Sage Weilf24e9982009-10-06 11:31:10 -07001852 /* ignore the rest */
1853 *p = end;
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001854
1855 dout("inc osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001856 return map;
1857
Ilya Dryomov86f17422014-03-13 16:36:15 +02001858e_inval:
1859 err = -EINVAL;
Sage Weilf24e9982009-10-06 11:31:10 -07001860bad:
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001861 pr_err("corrupt inc osdmap (%d) epoch %d off %d (%p of %p-%p)\n",
1862 err, epoch, (int)(*p - start), *p, start, end);
Sage Weil9ec7cab2009-12-14 15:13:47 -08001863 print_hex_dump(KERN_DEBUG, "osdmap: ",
1864 DUMP_PREFIX_OFFSET, 16, 1,
1865 start, end - start, true);
Sage Weilf24e9982009-10-06 11:31:10 -07001866 return ERR_PTR(err);
1867}
1868
Yan, Zheng30c156d2016-02-14 11:24:31 +08001869void ceph_oloc_copy(struct ceph_object_locator *dest,
1870 const struct ceph_object_locator *src)
1871{
Ilya Dryomovca35ffe2017-06-05 14:44:59 +02001872 ceph_oloc_destroy(dest);
Yan, Zheng30c156d2016-02-14 11:24:31 +08001873
1874 dest->pool = src->pool;
1875 if (src->pool_ns)
1876 dest->pool_ns = ceph_get_string(src->pool_ns);
Ilya Dryomovca35ffe2017-06-05 14:44:59 +02001877 else
1878 dest->pool_ns = NULL;
Yan, Zheng30c156d2016-02-14 11:24:31 +08001879}
1880EXPORT_SYMBOL(ceph_oloc_copy);
1881
1882void ceph_oloc_destroy(struct ceph_object_locator *oloc)
1883{
1884 ceph_put_string(oloc->pool_ns);
1885}
1886EXPORT_SYMBOL(ceph_oloc_destroy);
1887
Ilya Dryomovd30291b2016-04-29 19:54:20 +02001888void ceph_oid_copy(struct ceph_object_id *dest,
1889 const struct ceph_object_id *src)
1890{
Ilya Dryomovca35ffe2017-06-05 14:44:59 +02001891 ceph_oid_destroy(dest);
Sage Weilf24e9982009-10-06 11:31:10 -07001892
Ilya Dryomovd30291b2016-04-29 19:54:20 +02001893 if (src->name != src->inline_name) {
1894 /* very rare, see ceph_object_id definition */
1895 dest->name = kmalloc(src->name_len + 1,
1896 GFP_NOIO | __GFP_NOFAIL);
Ilya Dryomovca35ffe2017-06-05 14:44:59 +02001897 } else {
1898 dest->name = dest->inline_name;
Ilya Dryomovd30291b2016-04-29 19:54:20 +02001899 }
Ilya Dryomovd30291b2016-04-29 19:54:20 +02001900 memcpy(dest->name, src->name, src->name_len + 1);
1901 dest->name_len = src->name_len;
1902}
1903EXPORT_SYMBOL(ceph_oid_copy);
1904
1905static __printf(2, 0)
1906int oid_printf_vargs(struct ceph_object_id *oid, const char *fmt, va_list ap)
1907{
1908 int len;
1909
1910 WARN_ON(!ceph_oid_empty(oid));
1911
1912 len = vsnprintf(oid->inline_name, sizeof(oid->inline_name), fmt, ap);
1913 if (len >= sizeof(oid->inline_name))
1914 return len;
1915
1916 oid->name_len = len;
1917 return 0;
1918}
1919
1920/*
1921 * If oid doesn't fit into inline buffer, BUG.
1922 */
1923void ceph_oid_printf(struct ceph_object_id *oid, const char *fmt, ...)
1924{
1925 va_list ap;
1926
1927 va_start(ap, fmt);
1928 BUG_ON(oid_printf_vargs(oid, fmt, ap));
1929 va_end(ap);
1930}
1931EXPORT_SYMBOL(ceph_oid_printf);
1932
1933static __printf(3, 0)
1934int oid_aprintf_vargs(struct ceph_object_id *oid, gfp_t gfp,
1935 const char *fmt, va_list ap)
1936{
1937 va_list aq;
1938 int len;
1939
1940 va_copy(aq, ap);
1941 len = oid_printf_vargs(oid, fmt, aq);
1942 va_end(aq);
1943
1944 if (len) {
1945 char *external_name;
1946
1947 external_name = kmalloc(len + 1, gfp);
1948 if (!external_name)
1949 return -ENOMEM;
1950
1951 oid->name = external_name;
1952 WARN_ON(vsnprintf(oid->name, len + 1, fmt, ap) != len);
1953 oid->name_len = len;
1954 }
1955
1956 return 0;
1957}
1958
1959/*
1960 * If oid doesn't fit into inline buffer, allocate.
1961 */
1962int ceph_oid_aprintf(struct ceph_object_id *oid, gfp_t gfp,
1963 const char *fmt, ...)
1964{
1965 va_list ap;
1966 int ret;
1967
1968 va_start(ap, fmt);
1969 ret = oid_aprintf_vargs(oid, gfp, fmt, ap);
1970 va_end(ap);
1971
1972 return ret;
1973}
1974EXPORT_SYMBOL(ceph_oid_aprintf);
1975
1976void ceph_oid_destroy(struct ceph_object_id *oid)
1977{
1978 if (oid->name != oid->inline_name)
1979 kfree(oid->name);
1980}
1981EXPORT_SYMBOL(ceph_oid_destroy);
Sage Weilf24e9982009-10-06 11:31:10 -07001982
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001983/*
1984 * osds only
1985 */
1986static bool __osds_equal(const struct ceph_osds *lhs,
1987 const struct ceph_osds *rhs)
1988{
1989 if (lhs->size == rhs->size &&
1990 !memcmp(lhs->osds, rhs->osds, rhs->size * sizeof(rhs->osds[0])))
1991 return true;
1992
1993 return false;
1994}
1995
1996/*
1997 * osds + primary
1998 */
1999static bool osds_equal(const struct ceph_osds *lhs,
2000 const struct ceph_osds *rhs)
2001{
2002 if (__osds_equal(lhs, rhs) &&
2003 lhs->primary == rhs->primary)
2004 return true;
2005
2006 return false;
2007}
2008
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002009static bool osds_valid(const struct ceph_osds *set)
2010{
2011 /* non-empty set */
2012 if (set->size > 0 && set->primary >= 0)
2013 return true;
2014
2015 /* empty can_shift_osds set */
2016 if (!set->size && set->primary == -1)
2017 return true;
2018
2019 /* empty !can_shift_osds set - all NONE */
2020 if (set->size > 0 && set->primary == -1) {
2021 int i;
2022
2023 for (i = 0; i < set->size; i++) {
2024 if (set->osds[i] != CRUSH_ITEM_NONE)
2025 break;
2026 }
2027 if (i == set->size)
2028 return true;
2029 }
2030
2031 return false;
2032}
2033
2034void ceph_osds_copy(struct ceph_osds *dest, const struct ceph_osds *src)
2035{
2036 memcpy(dest->osds, src->osds, src->size * sizeof(src->osds[0]));
2037 dest->size = src->size;
2038 dest->primary = src->primary;
2039}
2040
Ilya Dryomov7de030d2017-06-15 16:30:54 +02002041bool ceph_pg_is_split(const struct ceph_pg *pgid, u32 old_pg_num,
2042 u32 new_pg_num)
Ilya Dryomov63244fa2016-04-28 16:07:23 +02002043{
2044 int old_bits = calc_bits_of(old_pg_num);
2045 int old_mask = (1 << old_bits) - 1;
2046 int n;
2047
2048 WARN_ON(pgid->seed >= old_pg_num);
2049 if (new_pg_num <= old_pg_num)
2050 return false;
2051
2052 for (n = 1; ; n++) {
2053 int next_bit = n << (old_bits - 1);
2054 u32 s = next_bit | pgid->seed;
2055
2056 if (s < old_pg_num || s == pgid->seed)
2057 continue;
2058 if (s >= new_pg_num)
2059 break;
2060
2061 s = ceph_stable_mod(s, old_pg_num, old_mask);
2062 if (s == pgid->seed)
2063 return true;
2064 }
2065
2066 return false;
2067}
2068
2069bool ceph_is_new_interval(const struct ceph_osds *old_acting,
2070 const struct ceph_osds *new_acting,
2071 const struct ceph_osds *old_up,
2072 const struct ceph_osds *new_up,
2073 int old_size,
2074 int new_size,
2075 int old_min_size,
2076 int new_min_size,
2077 u32 old_pg_num,
2078 u32 new_pg_num,
2079 bool old_sort_bitwise,
2080 bool new_sort_bitwise,
2081 const struct ceph_pg *pgid)
2082{
2083 return !osds_equal(old_acting, new_acting) ||
2084 !osds_equal(old_up, new_up) ||
2085 old_size != new_size ||
2086 old_min_size != new_min_size ||
Ilya Dryomov7de030d2017-06-15 16:30:54 +02002087 ceph_pg_is_split(pgid, old_pg_num, new_pg_num) ||
Ilya Dryomov63244fa2016-04-28 16:07:23 +02002088 old_sort_bitwise != new_sort_bitwise;
2089}
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/*
2136 * calculate file layout from given offset, length.
2137 * fill in correct oid, logical length, and object extent
2138 * offset, length.
2139 *
2140 * for now, we write only a single su, until we can
2141 * pass a stride back to the caller.
2142 */
Sage Weild63b77f2012-09-24 20:59:48 -07002143int ceph_calc_file_object_mapping(struct ceph_file_layout *layout,
Alex Eldere8afad62012-11-14 09:38:19 -06002144 u64 off, u64 len,
Sage Weil645a1022009-10-28 15:15:05 -07002145 u64 *ono,
Sage Weilf24e9982009-10-06 11:31:10 -07002146 u64 *oxoff, u64 *oxlen)
2147{
Yan, Zheng76271512016-02-03 21:24:49 +08002148 u32 osize = layout->object_size;
2149 u32 su = layout->stripe_unit;
2150 u32 sc = layout->stripe_count;
Sage Weilf24e9982009-10-06 11:31:10 -07002151 u32 bl, stripeno, stripepos, objsetno;
2152 u32 su_per_object;
Noah Watkinsff1d1f72009-10-30 12:57:30 -07002153 u64 t, su_offset;
Sage Weilf24e9982009-10-06 11:31:10 -07002154
Alex Eldere8afad62012-11-14 09:38:19 -06002155 dout("mapping %llu~%llu osize %u fl_su %u\n", off, len,
Sage Weilf24e9982009-10-06 11:31:10 -07002156 osize, su);
Sage Weild63b77f2012-09-24 20:59:48 -07002157 if (su == 0 || sc == 0)
2158 goto invalid;
Noah Watkins35e054a2009-10-28 14:04:48 -07002159 su_per_object = osize / su;
Sage Weild63b77f2012-09-24 20:59:48 -07002160 if (su_per_object == 0)
2161 goto invalid;
Sage Weilf24e9982009-10-06 11:31:10 -07002162 dout("osize %u / su %u = su_per_object %u\n", osize, su,
2163 su_per_object);
2164
Sage Weild63b77f2012-09-24 20:59:48 -07002165 if ((su & ~PAGE_MASK) != 0)
2166 goto invalid;
2167
Sage Weilf24e9982009-10-06 11:31:10 -07002168 /* bl = *off / su; */
2169 t = off;
2170 do_div(t, su);
2171 bl = t;
2172 dout("off %llu / su %u = bl %u\n", off, su, bl);
2173
2174 stripeno = bl / sc;
2175 stripepos = bl % sc;
2176 objsetno = stripeno / su_per_object;
2177
Sage Weil645a1022009-10-28 15:15:05 -07002178 *ono = objsetno * sc + stripepos;
Eric Dumazet95c96172012-04-15 05:58:06 +00002179 dout("objset %u * sc %u = ono %u\n", objsetno, sc, (unsigned int)*ono);
Sage Weil645a1022009-10-28 15:15:05 -07002180
2181 /* *oxoff = *off % layout->fl_stripe_unit; # offset in su */
Sage Weilf24e9982009-10-06 11:31:10 -07002182 t = off;
Noah Watkinsff1d1f72009-10-30 12:57:30 -07002183 su_offset = do_div(t, su);
2184 *oxoff = su_offset + (stripeno % su_per_object) * su;
Sage Weil645a1022009-10-28 15:15:05 -07002185
Noah Watkinsff1d1f72009-10-30 12:57:30 -07002186 /*
2187 * Calculate the length of the extent being written to the selected
Alex Eldere8afad62012-11-14 09:38:19 -06002188 * object. This is the minimum of the full length requested (len) or
Noah Watkinsff1d1f72009-10-30 12:57:30 -07002189 * the remainder of the current stripe being written to.
2190 */
Alex Eldere8afad62012-11-14 09:38:19 -06002191 *oxlen = min_t(u64, len, su - su_offset);
Sage Weilf24e9982009-10-06 11:31:10 -07002192
2193 dout(" obj extent %llu~%llu\n", *oxoff, *oxlen);
Sage Weild63b77f2012-09-24 20:59:48 -07002194 return 0;
2195
2196invalid:
2197 dout(" invalid layout\n");
2198 *ono = 0;
2199 *oxoff = 0;
2200 *oxlen = 0;
2201 return -EINVAL;
Sage Weilf24e9982009-10-06 11:31:10 -07002202}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002203EXPORT_SYMBOL(ceph_calc_file_object_mapping);
Sage Weilf24e9982009-10-06 11:31:10 -07002204
2205/*
Ilya Dryomovd9591f52016-04-28 16:07:22 +02002206 * Map an object into a PG.
2207 *
2208 * Should only be called with target_oid and target_oloc (as opposed to
2209 * base_oid and base_oloc), since tiering isn't taken into account.
Sage Weilf24e9982009-10-06 11:31:10 -07002210 */
Ilya Dryomovdf281522017-06-15 16:30:56 +02002211int __ceph_object_locator_to_pg(struct ceph_pg_pool_info *pi,
2212 const struct ceph_object_id *oid,
2213 const struct ceph_object_locator *oloc,
2214 struct ceph_pg *raw_pgid)
Sage Weilf24e9982009-10-06 11:31:10 -07002215{
Ilya Dryomovdf281522017-06-15 16:30:56 +02002216 WARN_ON(pi->id != oloc->pool);
Sage Weilf24e9982009-10-06 11:31:10 -07002217
Yan, Zheng30c156d2016-02-14 11:24:31 +08002218 if (!oloc->pool_ns) {
2219 raw_pgid->pool = oloc->pool;
2220 raw_pgid->seed = ceph_str_hash(pi->object_hash, oid->name,
2221 oid->name_len);
2222 dout("%s %s -> raw_pgid %llu.%x\n", __func__, oid->name,
2223 raw_pgid->pool, raw_pgid->seed);
2224 } else {
2225 char stack_buf[256];
2226 char *buf = stack_buf;
2227 int nsl = oloc->pool_ns->len;
2228 size_t total = nsl + 1 + oid->name_len;
Ilya Dryomov7c13cb62014-01-27 17:40:19 +02002229
Yan, Zheng30c156d2016-02-14 11:24:31 +08002230 if (total > sizeof(stack_buf)) {
2231 buf = kmalloc(total, GFP_NOIO);
2232 if (!buf)
2233 return -ENOMEM;
2234 }
2235 memcpy(buf, oloc->pool_ns->str, nsl);
2236 buf[nsl] = '\037';
2237 memcpy(buf + nsl + 1, oid->name, oid->name_len);
2238 raw_pgid->pool = oloc->pool;
2239 raw_pgid->seed = ceph_str_hash(pi->object_hash, buf, total);
2240 if (buf != stack_buf)
2241 kfree(buf);
2242 dout("%s %s ns %.*s -> raw_pgid %llu.%x\n", __func__,
2243 oid->name, nsl, oloc->pool_ns->str,
2244 raw_pgid->pool, raw_pgid->seed);
2245 }
Sage Weilf24e9982009-10-06 11:31:10 -07002246 return 0;
2247}
Ilya Dryomovdf281522017-06-15 16:30:56 +02002248
2249int ceph_object_locator_to_pg(struct ceph_osdmap *osdmap,
2250 const struct ceph_object_id *oid,
2251 const struct ceph_object_locator *oloc,
2252 struct ceph_pg *raw_pgid)
2253{
2254 struct ceph_pg_pool_info *pi;
2255
2256 pi = ceph_pg_pool_by_id(osdmap, oloc->pool);
2257 if (!pi)
2258 return -ENOENT;
2259
2260 return __ceph_object_locator_to_pg(pi, oid, oloc, raw_pgid);
2261}
Ilya Dryomovd9591f52016-04-28 16:07:22 +02002262EXPORT_SYMBOL(ceph_object_locator_to_pg);
Sage Weilf24e9982009-10-06 11:31:10 -07002263
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002264/*
2265 * Map a raw PG (full precision ps) into an actual PG.
2266 */
2267static void raw_pg_to_pg(struct ceph_pg_pool_info *pi,
2268 const struct ceph_pg *raw_pgid,
2269 struct ceph_pg *pgid)
2270{
2271 pgid->pool = raw_pgid->pool;
2272 pgid->seed = ceph_stable_mod(raw_pgid->seed, pi->pg_num,
2273 pi->pg_num_mask);
2274}
2275
2276/*
2277 * Map a raw PG (full precision ps) into a placement ps (placement
2278 * seed). Include pool id in that value so that different pools don't
2279 * use the same seeds.
2280 */
2281static u32 raw_pg_to_pps(struct ceph_pg_pool_info *pi,
2282 const struct ceph_pg *raw_pgid)
2283{
2284 if (pi->flags & CEPH_POOL_FLAG_HASHPSPOOL) {
2285 /* hash pool id and seed so that pool PGs do not overlap */
2286 return crush_hash32_2(CRUSH_HASH_RJENKINS1,
2287 ceph_stable_mod(raw_pgid->seed,
2288 pi->pgp_num,
2289 pi->pgp_num_mask),
2290 raw_pgid->pool);
2291 } else {
2292 /*
2293 * legacy behavior: add ps and pool together. this is
2294 * not a great approach because the PGs from each pool
2295 * will overlap on top of each other: 0.5 == 1.4 ==
2296 * 2.3 == ...
2297 */
2298 return ceph_stable_mod(raw_pgid->seed, pi->pgp_num,
2299 pi->pgp_num_mask) +
2300 (unsigned)raw_pgid->pool;
2301 }
2302}
2303
Ilya Dryomov9d521472014-01-31 17:54:26 +02002304static int do_crush(struct ceph_osdmap *map, int ruleno, int x,
2305 int *result, int result_max,
Ilya Dryomov5cf9c4a2017-06-22 19:44:05 +02002306 const __u32 *weight, int weight_max,
2307 u64 choose_args_index)
Ilya Dryomove8ef19c2013-12-24 21:19:24 +02002308{
Ilya Dryomov5cf9c4a2017-06-22 19:44:05 +02002309 struct crush_choose_arg_map *arg_map;
Ilya Dryomov9d521472014-01-31 17:54:26 +02002310 int r;
Ilya Dryomove8ef19c2013-12-24 21:19:24 +02002311
Ilya Dryomov9d521472014-01-31 17:54:26 +02002312 BUG_ON(result_max > CEPH_PG_MAX_SIZE);
2313
Ilya Dryomov5cf9c4a2017-06-22 19:44:05 +02002314 arg_map = lookup_choose_arg_map(&map->crush->choose_args,
2315 choose_args_index);
2316
Ilya Dryomov743efcf2017-01-31 15:55:06 +01002317 mutex_lock(&map->crush_workspace_mutex);
Ilya Dryomov9d521472014-01-31 17:54:26 +02002318 r = crush_do_rule(map->crush, ruleno, x, result, result_max,
Ilya Dryomov5cf9c4a2017-06-22 19:44:05 +02002319 weight, weight_max, map->crush_workspace,
2320 arg_map ? arg_map->args : NULL);
Ilya Dryomov743efcf2017-01-31 15:55:06 +01002321 mutex_unlock(&map->crush_workspace_mutex);
Ilya Dryomov9d521472014-01-31 17:54:26 +02002322
2323 return r;
Ilya Dryomove8ef19c2013-12-24 21:19:24 +02002324}
2325
Ilya Dryomov1c2e7b42017-06-21 17:27:18 +02002326static void remove_nonexistent_osds(struct ceph_osdmap *osdmap,
2327 struct ceph_pg_pool_info *pi,
2328 struct ceph_osds *set)
2329{
2330 int i;
2331
2332 if (ceph_can_shift_osds(pi)) {
2333 int removed = 0;
2334
2335 /* shift left */
2336 for (i = 0; i < set->size; i++) {
2337 if (!ceph_osd_exists(osdmap, set->osds[i])) {
2338 removed++;
2339 continue;
2340 }
2341 if (removed)
2342 set->osds[i - removed] = set->osds[i];
2343 }
2344 set->size -= removed;
2345 } else {
2346 /* set dne devices to NONE */
2347 for (i = 0; i < set->size; i++) {
2348 if (!ceph_osd_exists(osdmap, set->osds[i]))
2349 set->osds[i] = CRUSH_ITEM_NONE;
2350 }
2351 }
2352}
2353
Sage Weilf24e9982009-10-06 11:31:10 -07002354/*
Ilya Dryomov1c2e7b42017-06-21 17:27:18 +02002355 * Calculate raw set (CRUSH output) for given PG and filter out
2356 * nonexistent OSDs. ->primary is undefined for a raw set.
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002357 *
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002358 * Placement seed (CRUSH input) is returned through @ppps.
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002359 */
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002360static void pg_to_raw_osds(struct ceph_osdmap *osdmap,
2361 struct ceph_pg_pool_info *pi,
2362 const struct ceph_pg *raw_pgid,
2363 struct ceph_osds *raw,
2364 u32 *ppps)
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002365{
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002366 u32 pps = raw_pg_to_pps(pi, raw_pgid);
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002367 int ruleno;
2368 int len;
2369
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002370 ceph_osds_init(raw);
2371 if (ppps)
2372 *ppps = pps;
2373
2374 ruleno = crush_find_rule(osdmap->crush, pi->crush_ruleset, pi->type,
2375 pi->size);
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002376 if (ruleno < 0) {
2377 pr_err("no crush rule: pool %lld ruleset %d type %d size %d\n",
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002378 pi->id, pi->crush_ruleset, pi->type, pi->size);
2379 return;
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002380 }
2381
Ilya Dryomovef9324bb2017-02-08 18:57:48 +01002382 if (pi->size > ARRAY_SIZE(raw->osds)) {
2383 pr_err_ratelimited("pool %lld ruleset %d type %d too wide: size %d > %zu\n",
2384 pi->id, pi->crush_ruleset, pi->type, pi->size,
2385 ARRAY_SIZE(raw->osds));
2386 return;
2387 }
2388
2389 len = do_crush(osdmap, ruleno, pps, raw->osds, pi->size,
Ilya Dryomov5cf9c4a2017-06-22 19:44:05 +02002390 osdmap->osd_weight, osdmap->max_osd, pi->id);
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002391 if (len < 0) {
2392 pr_err("error %d from crush rule %d: pool %lld ruleset %d type %d size %d\n",
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002393 len, ruleno, pi->id, pi->crush_ruleset, pi->type,
2394 pi->size);
2395 return;
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002396 }
2397
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002398 raw->size = len;
Ilya Dryomov1c2e7b42017-06-21 17:27:18 +02002399 remove_nonexistent_osds(osdmap, pi, raw);
2400}
2401
2402/* apply pg_upmap[_items] mappings */
2403static void apply_upmap(struct ceph_osdmap *osdmap,
2404 const struct ceph_pg *pgid,
2405 struct ceph_osds *raw)
2406{
2407 struct ceph_pg_mapping *pg;
2408 int i, j;
2409
2410 pg = lookup_pg_mapping(&osdmap->pg_upmap, pgid);
2411 if (pg) {
2412 /* make sure targets aren't marked out */
2413 for (i = 0; i < pg->pg_upmap.len; i++) {
2414 int osd = pg->pg_upmap.osds[i];
2415
2416 if (osd != CRUSH_ITEM_NONE &&
2417 osd < osdmap->max_osd &&
2418 osdmap->osd_weight[osd] == 0) {
2419 /* reject/ignore explicit mapping */
2420 return;
2421 }
2422 }
2423 for (i = 0; i < pg->pg_upmap.len; i++)
2424 raw->osds[i] = pg->pg_upmap.osds[i];
2425 raw->size = pg->pg_upmap.len;
2426 return;
2427 }
2428
2429 pg = lookup_pg_mapping(&osdmap->pg_upmap_items, pgid);
2430 if (pg) {
2431 /*
2432 * Note: this approach does not allow a bidirectional swap,
2433 * e.g., [[1,2],[2,1]] applied to [0,1,2] -> [0,2,1].
2434 */
2435 for (i = 0; i < pg->pg_upmap_items.len; i++) {
2436 int from = pg->pg_upmap_items.from_to[i][0];
2437 int to = pg->pg_upmap_items.from_to[i][1];
2438 int pos = -1;
2439 bool exists = false;
2440
2441 /* make sure replacement doesn't already appear */
2442 for (j = 0; j < raw->size; j++) {
2443 int osd = raw->osds[j];
2444
2445 if (osd == to) {
2446 exists = true;
2447 break;
2448 }
2449 /* ignore mapping if target is marked out */
2450 if (osd == from && pos < 0 &&
2451 !(to != CRUSH_ITEM_NONE &&
2452 to < osdmap->max_osd &&
2453 osdmap->osd_weight[to] == 0)) {
2454 pos = j;
2455 }
2456 }
2457 if (!exists && pos >= 0) {
2458 raw->osds[pos] = to;
2459 return;
2460 }
2461 }
2462 }
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002463}
2464
2465/*
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002466 * Given raw set, calculate up set and up primary. By definition of an
2467 * up set, the result won't contain nonexistent or down OSDs.
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002468 *
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002469 * This is done in-place - on return @set is the up set. If it's
2470 * empty, ->primary will remain undefined.
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002471 */
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002472static void raw_to_up_osds(struct ceph_osdmap *osdmap,
2473 struct ceph_pg_pool_info *pi,
2474 struct ceph_osds *set)
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002475{
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002476 int i;
2477
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002478 /* ->primary is undefined for a raw set */
2479 BUG_ON(set->primary != -1);
2480
2481 if (ceph_can_shift_osds(pi)) {
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002482 int removed = 0;
2483
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002484 /* shift left */
2485 for (i = 0; i < set->size; i++) {
2486 if (ceph_osd_is_down(osdmap, set->osds[i])) {
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002487 removed++;
2488 continue;
2489 }
2490 if (removed)
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002491 set->osds[i - removed] = set->osds[i];
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002492 }
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002493 set->size -= removed;
2494 if (set->size > 0)
2495 set->primary = set->osds[0];
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002496 } else {
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002497 /* set down/dne devices to NONE */
2498 for (i = set->size - 1; i >= 0; i--) {
2499 if (ceph_osd_is_down(osdmap, set->osds[i]))
2500 set->osds[i] = CRUSH_ITEM_NONE;
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002501 else
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002502 set->primary = set->osds[i];
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002503 }
2504 }
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002505}
2506
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002507static void apply_primary_affinity(struct ceph_osdmap *osdmap,
2508 struct ceph_pg_pool_info *pi,
2509 u32 pps,
2510 struct ceph_osds *up)
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002511{
2512 int i;
2513 int pos = -1;
2514
2515 /*
2516 * Do we have any non-default primary_affinity values for these
2517 * osds?
2518 */
2519 if (!osdmap->osd_primary_affinity)
2520 return;
2521
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002522 for (i = 0; i < up->size; i++) {
2523 int osd = up->osds[i];
Ilya Dryomov92b2e752014-04-10 18:09:41 +04002524
2525 if (osd != CRUSH_ITEM_NONE &&
2526 osdmap->osd_primary_affinity[osd] !=
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002527 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY) {
2528 break;
2529 }
2530 }
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002531 if (i == up->size)
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002532 return;
2533
2534 /*
2535 * Pick the primary. Feed both the seed (for the pg) and the
2536 * osd into the hash/rng so that a proportional fraction of an
2537 * osd's pgs get rejected as primary.
2538 */
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002539 for (i = 0; i < up->size; i++) {
2540 int osd = up->osds[i];
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002541 u32 aff;
2542
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002543 if (osd == CRUSH_ITEM_NONE)
2544 continue;
2545
2546 aff = osdmap->osd_primary_affinity[osd];
2547 if (aff < CEPH_OSD_MAX_PRIMARY_AFFINITY &&
2548 (crush_hash32_2(CRUSH_HASH_RJENKINS1,
2549 pps, osd) >> 16) >= aff) {
2550 /*
2551 * We chose not to use this primary. Note it
2552 * anyway as a fallback in case we don't pick
2553 * anyone else, but keep looking.
2554 */
2555 if (pos < 0)
2556 pos = i;
2557 } else {
2558 pos = i;
2559 break;
2560 }
2561 }
2562 if (pos < 0)
2563 return;
2564
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002565 up->primary = up->osds[pos];
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002566
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002567 if (ceph_can_shift_osds(pi) && pos > 0) {
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002568 /* move the new primary to the front */
2569 for (i = pos; i > 0; i--)
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002570 up->osds[i] = up->osds[i - 1];
2571 up->osds[0] = up->primary;
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002572 }
2573}
2574
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002575/*
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002576 * Get pg_temp and primary_temp mappings for given PG.
Ilya Dryomov45966c32014-03-24 17:12:47 +02002577 *
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002578 * Note that a PG may have none, only pg_temp, only primary_temp or
2579 * both pg_temp and primary_temp mappings. This means @temp isn't
2580 * always a valid OSD set on return: in the "only primary_temp" case,
2581 * @temp will have its ->primary >= 0 but ->size == 0.
Ilya Dryomov45966c32014-03-24 17:12:47 +02002582 */
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002583static void get_temp_osds(struct ceph_osdmap *osdmap,
2584 struct ceph_pg_pool_info *pi,
Ilya Dryomov463bb8d2017-06-21 17:27:18 +02002585 const struct ceph_pg *pgid,
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002586 struct ceph_osds *temp)
Ilya Dryomov45966c32014-03-24 17:12:47 +02002587{
2588 struct ceph_pg_mapping *pg;
Ilya Dryomov45966c32014-03-24 17:12:47 +02002589 int i;
2590
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002591 ceph_osds_init(temp);
Ilya Dryomov45966c32014-03-24 17:12:47 +02002592
2593 /* pg_temp? */
Ilya Dryomov463bb8d2017-06-21 17:27:18 +02002594 pg = lookup_pg_mapping(&osdmap->pg_temp, pgid);
Ilya Dryomov45966c32014-03-24 17:12:47 +02002595 if (pg) {
Ilya Dryomov45966c32014-03-24 17:12:47 +02002596 for (i = 0; i < pg->pg_temp.len; i++) {
2597 if (ceph_osd_is_down(osdmap, pg->pg_temp.osds[i])) {
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002598 if (ceph_can_shift_osds(pi))
Ilya Dryomov45966c32014-03-24 17:12:47 +02002599 continue;
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002600
2601 temp->osds[temp->size++] = CRUSH_ITEM_NONE;
Ilya Dryomov45966c32014-03-24 17:12:47 +02002602 } else {
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002603 temp->osds[temp->size++] = pg->pg_temp.osds[i];
Ilya Dryomov45966c32014-03-24 17:12:47 +02002604 }
2605 }
2606
2607 /* apply pg_temp's primary */
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002608 for (i = 0; i < temp->size; i++) {
2609 if (temp->osds[i] != CRUSH_ITEM_NONE) {
2610 temp->primary = temp->osds[i];
Ilya Dryomov45966c32014-03-24 17:12:47 +02002611 break;
2612 }
2613 }
Ilya Dryomov45966c32014-03-24 17:12:47 +02002614 }
2615
Ilya Dryomov5e8d4d32014-03-24 17:12:48 +02002616 /* primary_temp? */
Ilya Dryomov463bb8d2017-06-21 17:27:18 +02002617 pg = lookup_pg_mapping(&osdmap->primary_temp, pgid);
Ilya Dryomov5e8d4d32014-03-24 17:12:48 +02002618 if (pg)
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002619 temp->primary = pg->primary_temp.osd;
Ilya Dryomov45966c32014-03-24 17:12:47 +02002620}
2621
2622/*
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002623 * Map a PG to its acting set as well as its up set.
Ilya Dryomovac972232014-03-24 17:12:48 +02002624 *
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002625 * Acting set is used for data mapping purposes, while up set can be
2626 * recorded for detecting interval changes and deciding whether to
2627 * resend a request.
Sage Weild85b7052010-05-10 10:24:48 -07002628 */
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002629void ceph_pg_to_up_acting_osds(struct ceph_osdmap *osdmap,
Ilya Dryomovdf281522017-06-15 16:30:56 +02002630 struct ceph_pg_pool_info *pi,
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002631 const struct ceph_pg *raw_pgid,
2632 struct ceph_osds *up,
2633 struct ceph_osds *acting)
Sage Weild85b7052010-05-10 10:24:48 -07002634{
Ilya Dryomov463bb8d2017-06-21 17:27:18 +02002635 struct ceph_pg pgid;
Ilya Dryomovac972232014-03-24 17:12:48 +02002636 u32 pps;
Sage Weild85b7052010-05-10 10:24:48 -07002637
Ilya Dryomovdf281522017-06-15 16:30:56 +02002638 WARN_ON(pi->id != raw_pgid->pool);
Ilya Dryomov463bb8d2017-06-21 17:27:18 +02002639 raw_pg_to_pg(pi, raw_pgid, &pgid);
Sage Weild85b7052010-05-10 10:24:48 -07002640
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002641 pg_to_raw_osds(osdmap, pi, raw_pgid, up, &pps);
Ilya Dryomov1c2e7b42017-06-21 17:27:18 +02002642 apply_upmap(osdmap, &pgid, up);
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002643 raw_to_up_osds(osdmap, pi, up);
2644 apply_primary_affinity(osdmap, pi, pps, up);
Ilya Dryomov463bb8d2017-06-21 17:27:18 +02002645 get_temp_osds(osdmap, pi, &pgid, acting);
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002646 if (!acting->size) {
2647 memcpy(acting->osds, up->osds, up->size * sizeof(up->osds[0]));
2648 acting->size = up->size;
2649 if (acting->primary == -1)
2650 acting->primary = up->primary;
Ilya Dryomovac972232014-03-24 17:12:48 +02002651 }
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002652 WARN_ON(!osds_valid(up) || !osds_valid(acting));
Sage Weild85b7052010-05-10 10:24:48 -07002653}
2654
Ilya Dryomovdc98ff72017-06-15 16:30:53 +02002655bool ceph_pg_to_primary_shard(struct ceph_osdmap *osdmap,
Ilya Dryomovdf281522017-06-15 16:30:56 +02002656 struct ceph_pg_pool_info *pi,
Ilya Dryomovdc98ff72017-06-15 16:30:53 +02002657 const struct ceph_pg *raw_pgid,
2658 struct ceph_spg *spgid)
2659{
Ilya Dryomovdc98ff72017-06-15 16:30:53 +02002660 struct ceph_pg pgid;
2661 struct ceph_osds up, acting;
2662 int i;
2663
Ilya Dryomovdf281522017-06-15 16:30:56 +02002664 WARN_ON(pi->id != raw_pgid->pool);
Ilya Dryomovdc98ff72017-06-15 16:30:53 +02002665 raw_pg_to_pg(pi, raw_pgid, &pgid);
2666
2667 if (ceph_can_shift_osds(pi)) {
2668 spgid->pgid = pgid; /* struct */
2669 spgid->shard = CEPH_SPG_NOSHARD;
2670 return true;
2671 }
2672
Ilya Dryomovdf281522017-06-15 16:30:56 +02002673 ceph_pg_to_up_acting_osds(osdmap, pi, &pgid, &up, &acting);
Ilya Dryomovdc98ff72017-06-15 16:30:53 +02002674 for (i = 0; i < acting.size; i++) {
2675 if (acting.osds[i] == acting.primary) {
2676 spgid->pgid = pgid; /* struct */
2677 spgid->shard = i;
2678 return true;
2679 }
2680 }
2681
2682 return false;
2683}
2684
Sage Weild85b7052010-05-10 10:24:48 -07002685/*
Ilya Dryomovf81f1632016-04-28 16:07:23 +02002686 * Return acting primary for given PG, or -1 if none.
Sage Weilf24e9982009-10-06 11:31:10 -07002687 */
Ilya Dryomovf81f1632016-04-28 16:07:23 +02002688int ceph_pg_to_acting_primary(struct ceph_osdmap *osdmap,
2689 const struct ceph_pg *raw_pgid)
Sage Weilf24e9982009-10-06 11:31:10 -07002690{
Ilya Dryomovdf281522017-06-15 16:30:56 +02002691 struct ceph_pg_pool_info *pi;
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002692 struct ceph_osds up, acting;
Sage Weilf24e9982009-10-06 11:31:10 -07002693
Ilya Dryomovdf281522017-06-15 16:30:56 +02002694 pi = ceph_pg_pool_by_id(osdmap, raw_pgid->pool);
2695 if (!pi)
2696 return -1;
2697
2698 ceph_pg_to_up_acting_osds(osdmap, pi, raw_pgid, &up, &acting);
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002699 return acting.primary;
Sage Weilf24e9982009-10-06 11:31:10 -07002700}
Ilya Dryomovf81f1632016-04-28 16:07:23 +02002701EXPORT_SYMBOL(ceph_pg_to_acting_primary);