Debian Codegraph @

net/ceph/osdmap.c

raw
1 // SPDX-License-Identifier: GPL-2.0
2
3 #include <linux/ceph/ceph_debug.h>
4
5 #include <linux/module.h>
6 #include <linux/slab.h>
7
8 #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>
13
14 static __printf(2, 3)
15 void osdmap_info(const struct ceph_osdmap *map, const char *fmt, ...)
16 {
17 struct va_format vaf;
18 va_list args;
19
20 va_start(args, fmt);
21 vaf.fmt = fmt;
22 vaf.va = &args;
23
24 printk(KERN_INFO "%s (%pU e%u): %pV", KBUILD_MODNAME, &map->fsid,
25 map->epoch, &vaf);
26
27 va_end(args);
28 }
29
30 char *ceph_osdmap_state_str(char *str, int len, u32 state)
31 {
32 if (!len)
33 return str;
34
35 if ((state & CEPH_OSD_EXISTS) && (state & CEPH_OSD_UP))
36 snprintf(str, len, "exists, up");
37 else if (state & CEPH_OSD_EXISTS)
38 snprintf(str, len, "exists");
39 else if (state & CEPH_OSD_UP)
40 snprintf(str, len, "up");
41 else
42 snprintf(str, len, "doesn't exist");
43
44 return str;
45 }
46
47 /* maps */
48
49 static int calc_bits_of(unsigned int t)
50 {
51 int b = 0;
52 while (t) {
53 t = t >> 1;
54 b++;
55 }
56 return b;
57 }
58
59 /*
60 * the foo_mask is the smallest value 2^n-1 that is >= foo.
61 */
62 static void calc_pg_masks(struct ceph_pg_pool_info *pi)
63 {
64 pi->pg_num_mask = (1 << calc_bits_of(pi->pg_num-1)) - 1;
65 pi->pgp_num_mask = (1 << calc_bits_of(pi->pgp_num-1)) - 1;
66 }
67
68 /*
69 * decode crush map
70 */
71 static int crush_decode_uniform_bucket(void **p, void *end,
72 struct crush_bucket_uniform *b)
73 {
74 dout("crush_decode_uniform_bucket %p to %p\n", *p, end);
75 ceph_decode_need(p, end, (1+b->h.size) * sizeof(u32), bad);
76 b->item_weight = ceph_decode_32(p);
77 return 0;
78 bad:
79 return -EINVAL;
80 }
81
82 static int crush_decode_list_bucket(void **p, void *end,
83 struct crush_bucket_list *b)
84 {
85 int j;
86 dout("crush_decode_list_bucket %p to %p\n", *p, end);
87 b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
88 if (b->item_weights == NULL)
89 return -ENOMEM;
90 b->sum_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
91 if (b->sum_weights == NULL)
92 return -ENOMEM;
93 ceph_decode_need(p, end, 2 * b->h.size * sizeof(u32), bad);
94 for (j = 0; j < b->h.size; j++) {
95 b->item_weights[j] = ceph_decode_32(p);
96 b->sum_weights[j] = ceph_decode_32(p);
97 }
98 return 0;
99 bad:
100 return -EINVAL;
101 }
102
103 static int crush_decode_tree_bucket(void **p, void *end,
104 struct crush_bucket_tree *b)
105 {
106 int j;
107 dout("crush_decode_tree_bucket %p to %p\n", *p, end);
108 ceph_decode_8_safe(p, end, b->num_nodes, bad);
109 b->node_weights = kcalloc(b->num_nodes, sizeof(u32), GFP_NOFS);
110 if (b->node_weights == NULL)
111 return -ENOMEM;
112 ceph_decode_need(p, end, b->num_nodes * sizeof(u32), bad);
113 for (j = 0; j < b->num_nodes; j++)
114 b->node_weights[j] = ceph_decode_32(p);
115 return 0;
116 bad:
117 return -EINVAL;
118 }
119
120 static int crush_decode_straw_bucket(void **p, void *end,
121 struct crush_bucket_straw *b)
122 {
123 int j;
124 dout("crush_decode_straw_bucket %p to %p\n", *p, end);
125 b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
126 if (b->item_weights == NULL)
127 return -ENOMEM;
128 b->straws = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
129 if (b->straws == NULL)
130 return -ENOMEM;
131 ceph_decode_need(p, end, 2 * b->h.size * sizeof(u32), bad);
132 for (j = 0; j < b->h.size; j++) {
133 b->item_weights[j] = ceph_decode_32(p);
134 b->straws[j] = ceph_decode_32(p);
135 }
136 return 0;
137 bad:
138 return -EINVAL;
139 }
140
141 static int crush_decode_straw2_bucket(void **p, void *end,
142 struct crush_bucket_straw2 *b)
143 {
144 int j;
145 dout("crush_decode_straw2_bucket %p to %p\n", *p, end);
146 b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
147 if (b->item_weights == NULL)
148 return -ENOMEM;
149 ceph_decode_need(p, end, b->h.size * sizeof(u32), bad);
150 for (j = 0; j < b->h.size; j++)
151 b->item_weights[j] = ceph_decode_32(p);
152 return 0;
153 bad:
154 return -EINVAL;
155 }
156
157 struct crush_name_node {
158 struct rb_node cn_node;
159 int cn_id;
160 char cn_name[];
161 };
162
163 static struct crush_name_node *alloc_crush_name(size_t name_len)
164 {
165 struct crush_name_node *cn;
166
167 cn = kmalloc(sizeof(*cn) + name_len + 1, GFP_NOIO);
168 if (!cn)
169 return NULL;
170
171 RB_CLEAR_NODE(&cn->cn_node);
172 return cn;
173 }
174
175 static void free_crush_name(struct crush_name_node *cn)
176 {
177 WARN_ON(!RB_EMPTY_NODE(&cn->cn_node));
178
179 kfree(cn);
180 }
181
182 DEFINE_RB_FUNCS(crush_name, struct crush_name_node, cn_id, cn_node)
183
184 static int decode_crush_names(void **p, void *end, struct rb_root *root)
185 {
186 u32 n;
187
188 ceph_decode_32_safe(p, end, n, e_inval);
189 while (n--) {
190 struct crush_name_node *cn;
191 int id;
192 u32 name_len;
193
194 ceph_decode_32_safe(p, end, id, e_inval);
195 ceph_decode_32_safe(p, end, name_len, e_inval);
196 ceph_decode_need(p, end, name_len, e_inval);
197
198 cn = alloc_crush_name(name_len);
199 if (!cn)
200 return -ENOMEM;
201
202 cn->cn_id = id;
203 memcpy(cn->cn_name, *p, name_len);
204 cn->cn_name[name_len] = '\0';
205 *p += name_len;
206
207 if (!__insert_crush_name(root, cn)) {
208 free_crush_name(cn);
209 return -EEXIST;
210 }
211 }
212
213 return 0;
214
215 e_inval:
216 return -EINVAL;
217 }
218
219 void clear_crush_names(struct rb_root *root)
220 {
221 while (!RB_EMPTY_ROOT(root)) {
222 struct crush_name_node *cn =
223 rb_entry(rb_first(root), struct crush_name_node, cn_node);
224
225 erase_crush_name(root, cn);
226 free_crush_name(cn);
227 }
228 }
229
230 static struct crush_choose_arg_map *alloc_choose_arg_map(void)
231 {
232 struct crush_choose_arg_map *arg_map;
233
234 arg_map = kzalloc_obj(*arg_map, GFP_NOIO);
235 if (!arg_map)
236 return NULL;
237
238 RB_CLEAR_NODE(&arg_map->node);
239 return arg_map;
240 }
241
242 static void free_choose_arg_map(struct crush_choose_arg_map *arg_map)
243 {
244 int i, j;
245
246 if (!arg_map)
247 return;
248
249 WARN_ON(!RB_EMPTY_NODE(&arg_map->node));
250
251 if (arg_map->args) {
252 for (i = 0; i < arg_map->size; i++) {
253 struct crush_choose_arg *arg = &arg_map->args[i];
254 if (arg->weight_set) {
255 for (j = 0; j < arg->weight_set_size; j++)
256 kfree(arg->weight_set[j].weights);
257 kfree(arg->weight_set);
258 }
259 kfree(arg->ids);
260 }
261 kfree(arg_map->args);
262 }
263 kfree(arg_map);
264 }
265
266 DEFINE_RB_FUNCS(choose_arg_map, struct crush_choose_arg_map, choose_args_index,
267 node);
268
269 void clear_choose_args(struct crush_map *c)
270 {
271 while (!RB_EMPTY_ROOT(&c->choose_args)) {
272 struct crush_choose_arg_map *arg_map =
273 rb_entry(rb_first(&c->choose_args),
274 struct crush_choose_arg_map, node);
275
276 erase_choose_arg_map(&c->choose_args, arg_map);
277 free_choose_arg_map(arg_map);
278 }
279 }
280
281 static u32 *decode_array_32_alloc(void **p, void *end, u32 *plen)
282 {
283 u32 *a = NULL;
284 u32 len;
285 int ret;
286
287 ceph_decode_32_safe(p, end, len, e_inval);
288 if (len) {
289 u32 i;
290
291 a = kmalloc_array(len, sizeof(u32), GFP_NOIO);
292 if (!a) {
293 ret = -ENOMEM;
294 goto fail;
295 }
296
297 ceph_decode_need(p, end, len * sizeof(u32), e_inval);
298 for (i = 0; i < len; i++)
299 a[i] = ceph_decode_32(p);
300 }
301
302 *plen = len;
303 return a;
304
305 e_inval:
306 ret = -EINVAL;
307 fail:
308 kfree(a);
309 return ERR_PTR(ret);
310 }
311
312 /*
313 * Assumes @arg is zero-initialized.
314 */
315 static int decode_choose_arg(void **p, void *end, struct crush_choose_arg *arg)
316 {
317 int ret;
318
319 ceph_decode_32_safe(p, end, arg->weight_set_size, e_inval);
320 if (arg->weight_set_size) {
321 u32 i;
322
323 arg->weight_set = kmalloc_objs(*arg->weight_set,
324 arg->weight_set_size, GFP_NOIO);
325 if (!arg->weight_set)
326 return -ENOMEM;
327
328 for (i = 0; i < arg->weight_set_size; i++) {
329 struct crush_weight_set *w = &arg->weight_set[i];
330
331 w->weights = decode_array_32_alloc(p, end, &w->size);
332 if (IS_ERR(w->weights)) {
333 ret = PTR_ERR(w->weights);
334 w->weights = NULL;
335 return ret;
336 }
337 }
338 }
339
340 arg->ids = decode_array_32_alloc(p, end, &arg->ids_size);
341 if (IS_ERR(arg->ids)) {
342 ret = PTR_ERR(arg->ids);
343 arg->ids = NULL;
344 return ret;
345 }
346
347 return 0;
348
349 e_inval:
350 return -EINVAL;
351 }
352
353 static int decode_choose_args(void **p, void *end, struct crush_map *c)
354 {
355 struct crush_choose_arg_map *arg_map = NULL;
356 u32 num_choose_arg_maps, num_buckets;
357 int ret;
358
359 ceph_decode_32_safe(p, end, num_choose_arg_maps, e_inval);
360 while (num_choose_arg_maps--) {
361 arg_map = alloc_choose_arg_map();
362 if (!arg_map) {
363 ret = -ENOMEM;
364 goto fail;
365 }
366
367 ceph_decode_64_safe(p, end, arg_map->choose_args_index,
368 e_inval);
369 arg_map->size = c->max_buckets;
370 arg_map->args = kzalloc_objs(*arg_map->args, arg_map->size,
371 GFP_NOIO);
372 if (!arg_map->args) {
373 ret = -ENOMEM;
374 goto fail;
375 }
376
377 ceph_decode_32_safe(p, end, num_buckets, e_inval);
378 while (num_buckets--) {
379 struct crush_choose_arg *arg;
380 u32 bucket_index;
381
382 ceph_decode_32_safe(p, end, bucket_index, e_inval);
383 if (bucket_index >= arg_map->size)
384 goto e_inval;
385
386 arg = &arg_map->args[bucket_index];
387 ret = decode_choose_arg(p, end, arg);
388 if (ret)
389 goto fail;
390
391 if (arg->ids_size &&
392 (!c->buckets[bucket_index] ||
393 arg->ids_size != c->buckets[bucket_index]->size))
394 goto e_inval;
395 }
396
397 if (!__insert_choose_arg_map(&c->choose_args, arg_map)) {
398 ret = -EEXIST;
399 goto fail;
400 }
401 }
402
403 return 0;
404
405 e_inval:
406 ret = -EINVAL;
407 fail:
408 free_choose_arg_map(arg_map);
409 return ret;
410 }
411
412 static void crush_finalize(struct crush_map *c)
413 {
414 __s32 b;
415
416 /* Space for the array of pointers to per-bucket workspace */
417 c->working_size = sizeof(struct crush_work) +
418 c->max_buckets * sizeof(struct crush_work_bucket *);
419
420 for (b = 0; b < c->max_buckets; b++) {
421 if (!c->buckets[b])
422 continue;
423
424 switch (c->buckets[b]->alg) {
425 default:
426 /*
427 * The base case, permutation variables and
428 * the pointer to the permutation array.
429 */
430 c->working_size += sizeof(struct crush_work_bucket);
431 break;
432 }
433 /* Every bucket has a permutation array. */
434 c->working_size += c->buckets[b]->size * sizeof(__u32);
435 }
436 }
437
438 static struct crush_map *crush_decode(void *pbyval, void *end)
439 {
440 struct crush_map *c;
441 int err;
442 int i, j;
443 void **p = &pbyval;
444 void *start = pbyval;
445 u32 magic;
446
447 dout("crush_decode %p to %p len %d\n", *p, end, (int)(end - *p));
448
449 c = kzalloc_obj(*c, GFP_NOFS);
450 if (c == NULL)
451 return ERR_PTR(-ENOMEM);
452
453 c->type_names = RB_ROOT;
454 c->names = RB_ROOT;
455 c->choose_args = RB_ROOT;
456
457 /* set tunables to default values */
458 c->choose_local_tries = 2;
459 c->choose_local_fallback_tries = 5;
460 c->choose_total_tries = 19;
461 c->chooseleaf_descend_once = 0;
462
463 ceph_decode_need(p, end, 4*sizeof(u32), bad);
464 magic = ceph_decode_32(p);
465 if (magic != CRUSH_MAGIC) {
466 pr_err("crush_decode magic %x != current %x\n",
467 (unsigned int)magic, (unsigned int)CRUSH_MAGIC);
468 goto bad;
469 }
470 c->max_buckets = ceph_decode_32(p);
471 c->max_rules = ceph_decode_32(p);
472 c->max_devices = ceph_decode_32(p);
473
474 c->buckets = kzalloc_objs(*c->buckets, c->max_buckets, GFP_NOFS);
475 if (c->buckets == NULL)
476 goto badmem;
477 c->rules = kzalloc_objs(*c->rules, c->max_rules, GFP_NOFS);
478 if (c->rules == NULL)
479 goto badmem;
480
481 /* buckets */
482 for (i = 0; i < c->max_buckets; i++) {
483 int size = 0;
484 u32 alg;
485 struct crush_bucket *b;
486
487 ceph_decode_32_safe(p, end, alg, bad);
488 if (alg == 0) {
489 c->buckets[i] = NULL;
490 continue;
491 }
492 dout("crush_decode bucket %d off %x %p to %p\n",
493 i, (int)(*p-start), *p, end);
494
495 switch (alg) {
496 case CRUSH_BUCKET_UNIFORM:
497 size = sizeof(struct crush_bucket_uniform);
498 break;
499 case CRUSH_BUCKET_LIST:
500 size = sizeof(struct crush_bucket_list);
501 break;
502 case CRUSH_BUCKET_TREE:
503 size = sizeof(struct crush_bucket_tree);
504 break;
505 case CRUSH_BUCKET_STRAW:
506 size = sizeof(struct crush_bucket_straw);
507 break;
508 case CRUSH_BUCKET_STRAW2:
509 size = sizeof(struct crush_bucket_straw2);
510 break;
511 default:
512 goto bad;
513 }
514 BUG_ON(size == 0);
515 b = c->buckets[i] = kzalloc(size, GFP_NOFS);
516 if (b == NULL)
517 goto badmem;
518
519 ceph_decode_need(p, end, 4*sizeof(u32), bad);
520 b->id = ceph_decode_32(p);
521 b->type = ceph_decode_16(p);
522 b->alg = ceph_decode_8(p);
523 if (b->alg != alg) {
524 b->alg = 0;
525 goto bad;
526 }
527 b->hash = ceph_decode_8(p);
528 b->weight = ceph_decode_32(p);
529 b->size = ceph_decode_32(p);
530
531 dout("crush_decode bucket size %d off %x %p to %p\n",
532 b->size, (int)(*p-start), *p, end);
533
534 b->items = kzalloc_objs(__s32, b->size, GFP_NOFS);
535 if (b->items == NULL)
536 goto badmem;
537
538 ceph_decode_need(p, end, b->size*sizeof(u32), bad);
539 for (j = 0; j < b->size; j++)
540 b->items[j] = ceph_decode_32(p);
541
542 switch (b->alg) {
543 case CRUSH_BUCKET_UNIFORM:
544 err = crush_decode_uniform_bucket(p, end,
545 (struct crush_bucket_uniform *)b);
546 if (err < 0)
547 goto fail;
548 break;
549 case CRUSH_BUCKET_LIST:
550 err = crush_decode_list_bucket(p, end,
551 (struct crush_bucket_list *)b);
552 if (err < 0)
553 goto fail;
554 break;
555 case CRUSH_BUCKET_TREE:
556 err = crush_decode_tree_bucket(p, end,
557 (struct crush_bucket_tree *)b);
558 if (err < 0)
559 goto fail;
560 break;
561 case CRUSH_BUCKET_STRAW:
562 err = crush_decode_straw_bucket(p, end,
563 (struct crush_bucket_straw *)b);
564 if (err < 0)
565 goto fail;
566 break;
567 case CRUSH_BUCKET_STRAW2:
568 err = crush_decode_straw2_bucket(p, end,
569 (struct crush_bucket_straw2 *)b);
570 if (err < 0)
571 goto fail;
572 break;
573 }
574 }
575
576 /* rules */
577 dout("rule vec is %p\n", c->rules);
578 for (i = 0; i < c->max_rules; i++) {
579 u32 yes;
580 struct crush_rule *r;
581
582 ceph_decode_32_safe(p, end, yes, bad);
583 if (!yes) {
584 dout("crush_decode NO rule %d off %x %p to %p\n",
585 i, (int)(*p-start), *p, end);
586 c->rules[i] = NULL;
587 continue;
588 }
589
590 dout("crush_decode rule %d off %x %p to %p\n",
591 i, (int)(*p-start), *p, end);
592
593 /* len */
594 ceph_decode_32_safe(p, end, yes, bad);
595 #if BITS_PER_LONG == 32
596 if (yes > (ULONG_MAX - sizeof(*r))
597 / sizeof(struct crush_rule_step))
598 goto bad;
599 #endif
600 r = kmalloc_flex(*r, steps, yes, GFP_NOFS);
601 if (r == NULL)
602 goto badmem;
603 dout(" rule %d is at %p\n", i, r);
604 c->rules[i] = r;
605 r->len = yes;
606 ceph_decode_copy_safe(p, end, &r->mask, 4, bad); /* 4 u8's */
607 ceph_decode_need(p, end, r->len*3*sizeof(u32), bad);
608 for (j = 0; j < r->len; j++) {
609 r->steps[j].op = ceph_decode_32(p);
610 r->steps[j].arg1 = ceph_decode_32(p);
611 r->steps[j].arg2 = ceph_decode_32(p);
612 }
613 }
614
615 err = decode_crush_names(p, end, &c->type_names);
616 if (err)
617 goto fail;
618
619 err = decode_crush_names(p, end, &c->names);
620 if (err)
621 goto fail;
622
623 ceph_decode_skip_map(p, end, 32, string, bad); /* rule_name_map */
624
625 /* tunables */
626 ceph_decode_need(p, end, 3*sizeof(u32), done);
627 c->choose_local_tries = ceph_decode_32(p);
628 c->choose_local_fallback_tries = ceph_decode_32(p);
629 c->choose_total_tries = ceph_decode_32(p);
630 dout("crush decode tunable choose_local_tries = %d\n",
631 c->choose_local_tries);
632 dout("crush decode tunable choose_local_fallback_tries = %d\n",
633 c->choose_local_fallback_tries);
634 dout("crush decode tunable choose_total_tries = %d\n",
635 c->choose_total_tries);
636
637 ceph_decode_need(p, end, sizeof(u32), done);
638 c->chooseleaf_descend_once = ceph_decode_32(p);
639 dout("crush decode tunable chooseleaf_descend_once = %d\n",
640 c->chooseleaf_descend_once);
641
642 ceph_decode_need(p, end, sizeof(u8), done);
643 c->chooseleaf_vary_r = ceph_decode_8(p);
644 dout("crush decode tunable chooseleaf_vary_r = %d\n",
645 c->chooseleaf_vary_r);
646
647 /* skip straw_calc_version, allowed_bucket_algs */
648 ceph_decode_need(p, end, sizeof(u8) + sizeof(u32), done);
649 *p += sizeof(u8) + sizeof(u32);
650
651 ceph_decode_need(p, end, sizeof(u8), done);
652 c->chooseleaf_stable = ceph_decode_8(p);
653 dout("crush decode tunable chooseleaf_stable = %d\n",
654 c->chooseleaf_stable);
655
656 if (*p != end) {
657 /* class_map */
658 ceph_decode_skip_map(p, end, 32, 32, bad);
659 /* class_name */
660 ceph_decode_skip_map(p, end, 32, string, bad);
661 /* class_bucket */
662 ceph_decode_skip_map_of_map(p, end, 32, 32, 32, bad);
663 }
664
665 if (*p != end) {
666 err = decode_choose_args(p, end, c);
667 if (err)
668 goto fail;
669 }
670
671 done:
672 crush_finalize(c);
673 dout("crush_decode success\n");
674 return c;
675
676 badmem:
677 err = -ENOMEM;
678 fail:
679 dout("crush_decode fail %d\n", err);
680 crush_destroy(c);
681 return ERR_PTR(err);
682
683 bad:
684 err = -EINVAL;
685 goto fail;
686 }
687
688 int ceph_pg_compare(const struct ceph_pg *lhs, const struct ceph_pg *rhs)
689 {
690 if (lhs->pool < rhs->pool)
691 return -1;
692 if (lhs->pool > rhs->pool)
693 return 1;
694 if (lhs->seed < rhs->seed)
695 return -1;
696 if (lhs->seed > rhs->seed)
697 return 1;
698
699 return 0;
700 }
701
702 int ceph_spg_compare(const struct ceph_spg *lhs, const struct ceph_spg *rhs)
703 {
704 int ret;
705
706 ret = ceph_pg_compare(&lhs->pgid, &rhs->pgid);
707 if (ret)
708 return ret;
709
710 if (lhs->shard < rhs->shard)
711 return -1;
712 if (lhs->shard > rhs->shard)
713 return 1;
714
715 return 0;
716 }
717
718 static struct ceph_pg_mapping *alloc_pg_mapping(size_t payload_len)
719 {
720 struct ceph_pg_mapping *pg;
721
722 pg = kmalloc(sizeof(*pg) + payload_len, GFP_NOIO);
723 if (!pg)
724 return NULL;
725
726 RB_CLEAR_NODE(&pg->node);
727 return pg;
728 }
729
730 static void free_pg_mapping(struct ceph_pg_mapping *pg)
731 {
732 WARN_ON(!RB_EMPTY_NODE(&pg->node));
733
734 kfree(pg);
735 }
736
737 /*
738 * rbtree of pg_mapping for handling pg_temp (explicit mapping of pgid
739 * to a set of osds) and primary_temp (explicit primary setting)
740 */
741 DEFINE_RB_FUNCS2(pg_mapping, struct ceph_pg_mapping, pgid, ceph_pg_compare,
742 RB_BYPTR, const struct ceph_pg *, node)
743
744 /*
745 * rbtree of pg pool info
746 */
747 DEFINE_RB_FUNCS(pg_pool, struct ceph_pg_pool_info, id, node)
748
749 struct ceph_pg_pool_info *ceph_pg_pool_by_id(struct ceph_osdmap *map, u64 id)
750 {
751 return lookup_pg_pool(&map->pg_pools, id);
752 }
753
754 const char *ceph_pg_pool_name_by_id(struct ceph_osdmap *map, u64 id)
755 {
756 struct ceph_pg_pool_info *pi;
757
758 if (id == CEPH_NOPOOL)
759 return NULL;
760
761 if (WARN_ON_ONCE(id > (u64) INT_MAX))
762 return NULL;
763
764 pi = lookup_pg_pool(&map->pg_pools, id);
765 return pi ? pi->name : NULL;
766 }
767 EXPORT_SYMBOL(ceph_pg_pool_name_by_id);
768
769 int ceph_pg_poolid_by_name(struct ceph_osdmap *map, const char *name)
770 {
771 struct rb_node *rbp;
772
773 for (rbp = rb_first(&map->pg_pools); rbp; rbp = rb_next(rbp)) {
774 struct ceph_pg_pool_info *pi =
775 rb_entry(rbp, struct ceph_pg_pool_info, node);
776 if (pi->name && strcmp(pi->name, name) == 0)
777 return pi->id;
778 }
779 return -ENOENT;
780 }
781 EXPORT_SYMBOL(ceph_pg_poolid_by_name);
782
783 u64 ceph_pg_pool_flags(struct ceph_osdmap *map, u64 id)
784 {
785 struct ceph_pg_pool_info *pi;
786
787 pi = lookup_pg_pool(&map->pg_pools, id);
788 return pi ? pi->flags : 0;
789 }
790 EXPORT_SYMBOL(ceph_pg_pool_flags);
791
792 static void __remove_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *pi)
793 {
794 erase_pg_pool(root, pi);
795 kfree(pi->name);
796 kfree(pi);
797 }
798
799 static int decode_pool(void **p, void *end, struct ceph_pg_pool_info *pi)
800 {
801 u8 ev, cv;
802 unsigned len, num;
803 void *pool_end;
804
805 ceph_decode_need(p, end, 2 + 4, bad);
806 ev = ceph_decode_8(p); /* encoding version */
807 cv = ceph_decode_8(p); /* compat version */
808 if (ev < 5) {
809 pr_warn("got v %d < 5 cv %d of ceph_pg_pool\n", ev, cv);
810 return -EINVAL;
811 }
812 if (cv > 9) {
813 pr_warn("got v %d cv %d > 9 of ceph_pg_pool\n", ev, cv);
814 return -EINVAL;
815 }
816 len = ceph_decode_32(p);
817 ceph_decode_need(p, end, len, bad);
818 pool_end = *p + len;
819
820 ceph_decode_need(p, end, 4 + 4 + 4, bad);
821 pi->type = ceph_decode_8(p);
822 pi->size = ceph_decode_8(p);
823 pi->crush_ruleset = ceph_decode_8(p);
824 pi->object_hash = ceph_decode_8(p);
825 pi->pg_num = ceph_decode_32(p);
826 pi->pgp_num = ceph_decode_32(p);
827
828 /* lpg*, last_change, snap_seq, snap_epoch */
829 ceph_decode_skip_n(p, end, 8 + 4 + 8 + 4, bad);
830
831 /* skip snaps */
832 ceph_decode_32_safe(p, end, num, bad);
833 while (num--) {
834 /* snapid key, pool snap (with versions) */
835 ceph_decode_skip_n(p, end, 8 + 2, bad);
836 ceph_decode_skip_string(p, end, bad);
837 }
838
839 /* removed_snaps */
840 ceph_decode_skip_map(p, end, 64, 64, bad);
841
842 ceph_decode_need(p, end, 8 + 8 + 4, bad);
843 *p += 8; /* skip auid */
844 pi->flags = ceph_decode_64(p);
845 *p += 4; /* skip crash_replay_interval */
846
847 if (ev >= 7)
848 ceph_decode_8_safe(p, end, pi->min_size, bad);
849 else
850 pi->min_size = pi->size - pi->size / 2;
851
852 if (ev >= 8)
853 /* quota_max_* */
854 ceph_decode_skip_n(p, end, 8 + 8, bad);
855
856 if (ev >= 9) {
857 /* tiers */
858 ceph_decode_skip_set(p, end, 64, bad);
859
860 ceph_decode_need(p, end, 8 + 1 + 8 + 8, bad);
861 *p += 8; /* skip tier_of */
862 *p += 1; /* skip cache_mode */
863 pi->read_tier = ceph_decode_64(p);
864 pi->write_tier = ceph_decode_64(p);
865 } else {
866 pi->read_tier = -1;
867 pi->write_tier = -1;
868 }
869
870 if (ev >= 10)
871 /* properties */
872 ceph_decode_skip_map(p, end, string, string, bad);
873
874 if (ev >= 11) {
875 /* hit_set_params (with versions) */
876 ceph_decode_skip_n(p, end, 2, bad);
877 ceph_decode_skip_string(p, end, bad);
878
879 /* hit_set_period, hit_set_count */
880 ceph_decode_skip_n(p, end, 4 + 4, bad);
881 }
882
883 if (ev >= 12)
884 /* stripe_width */
885 ceph_decode_skip_32(p, end, bad);
886
887 if (ev >= 13)
888 /* target_max_*, cache_target_*, cache_min_* */
889 ceph_decode_skip_n(p, end, 16 + 8 + 8, bad);
890
891 if (ev >= 14)
892 /* erasure_code_profile */
893 ceph_decode_skip_string(p, end, bad);
894
895 /*
896 * last_force_op_resend_preluminous, will be overridden if the
897 * map was encoded with RESEND_ON_SPLIT
898 */
899 if (ev >= 15)
900 ceph_decode_32_safe(p, end, pi->last_force_request_resend, bad);
901 else
902 pi->last_force_request_resend = 0;
903
904 if (ev >= 16)
905 /* min_read_recency_for_promote */
906 ceph_decode_skip_32(p, end, bad);
907
908 if (ev >= 17)
909 /* expected_num_objects */
910 ceph_decode_skip_64(p, end, bad);
911
912 if (ev >= 19)
913 /* cache_target_dirty_high_ratio_micro */
914 ceph_decode_skip_32(p, end, bad);
915
916 if (ev >= 20)
917 /* min_write_recency_for_promote */
918 ceph_decode_skip_32(p, end, bad);
919
920 if (ev >= 21)
921 /* use_gmt_hitset */
922 ceph_decode_skip_8(p, end, bad);
923
924 if (ev >= 22)
925 /* fast_read */
926 ceph_decode_skip_8(p, end, bad);
927
928 if (ev >= 23)
929 /* hit_set_grade_decay_rate, hit_set_search_last_n */
930 ceph_decode_skip_n(p, end, 4 + 4, bad);
931
932 if (ev >= 24) {
933 /* opts (with versions) */
934 ceph_decode_skip_n(p, end, 2, bad);
935 ceph_decode_skip_string(p, end, bad);
936 }
937
938 if (ev >= 25)
939 ceph_decode_32_safe(p, end, pi->last_force_request_resend, bad);
940
941 /* ignore the rest */
942
943 *p = pool_end;
944 calc_pg_masks(pi);
945 return 0;
946
947 bad:
948 return -EINVAL;
949 }
950
951 static int decode_pool_names(void **p, void *end, struct ceph_osdmap *map)
952 {
953 struct ceph_pg_pool_info *pi;
954 u32 num, len;
955 u64 pool;
956
957 ceph_decode_32_safe(p, end, num, bad);
958 dout(" %d pool names\n", num);
959 while (num--) {
960 ceph_decode_64_safe(p, end, pool, bad);
961 ceph_decode_32_safe(p, end, len, bad);
962 dout(" pool %llu len %d\n", pool, len);
963 ceph_decode_need(p, end, len, bad);
964 pi = lookup_pg_pool(&map->pg_pools, pool);
965 if (pi) {
966 char *name = kstrndup(*p, len, GFP_NOFS);
967
968 if (!name)
969 return -ENOMEM;
970 kfree(pi->name);
971 pi->name = name;
972 dout(" name is %s\n", pi->name);
973 }
974 *p += len;
975 }
976 return 0;
977
978 bad:
979 return -EINVAL;
980 }
981
982 /*
983 * CRUSH workspaces
984 *
985 * workspace_manager framework borrowed from fs/btrfs/compression.c.
986 * Two simplifications: there is only one type of workspace and there
987 * is always at least one workspace.
988 */
989 static struct crush_work *alloc_workspace(const struct crush_map *c)
990 {
991 struct crush_work *work;
992 size_t work_size;
993
994 WARN_ON(!c->working_size);
995 work_size = crush_work_size(c, CEPH_PG_MAX_SIZE);
996 dout("%s work_size %zu bytes\n", __func__, work_size);
997
998 work = kvmalloc(work_size, GFP_NOIO);
999 if (!work)
1000 return NULL;
1001
1002 INIT_LIST_HEAD(&work->item);
1003 crush_init_workspace(c, work);
1004 return work;
1005 }
1006
1007 static void free_workspace(struct crush_work *work)
1008 {
1009 WARN_ON(!list_empty(&work->item));
1010 kvfree(work);
1011 }
1012
1013 static void init_workspace_manager(struct workspace_manager *wsm)
1014 {
1015 INIT_LIST_HEAD(&wsm->idle_ws);
1016 spin_lock_init(&wsm->ws_lock);
1017 atomic_set(&wsm->total_ws, 0);
1018 wsm->free_ws = 0;
1019 init_waitqueue_head(&wsm->ws_wait);
1020 }
1021
1022 static void add_initial_workspace(struct workspace_manager *wsm,
1023 struct crush_work *work)
1024 {
1025 WARN_ON(!list_empty(&wsm->idle_ws));
1026
1027 list_add(&work->item, &wsm->idle_ws);
1028 atomic_set(&wsm->total_ws, 1);
1029 wsm->free_ws = 1;
1030 }
1031
1032 static void cleanup_workspace_manager(struct workspace_manager *wsm)
1033 {
1034 struct crush_work *work;
1035
1036 while (!list_empty(&wsm->idle_ws)) {
1037 work = list_first_entry(&wsm->idle_ws, struct crush_work,
1038 item);
1039 list_del_init(&work->item);
1040 free_workspace(work);
1041 }
1042 atomic_set(&wsm->total_ws, 0);
1043 wsm->free_ws = 0;
1044 }
1045
1046 /*
1047 * Finds an available workspace or allocates a new one. If it's not
1048 * possible to allocate a new one, waits until there is one.
1049 */
1050 static struct crush_work *get_workspace(struct workspace_manager *wsm,
1051 const struct crush_map *c)
1052 {
1053 struct crush_work *work;
1054 int cpus = num_online_cpus();
1055
1056 again:
1057 spin_lock(&wsm->ws_lock);
1058 if (!list_empty(&wsm->idle_ws)) {
1059 work = list_first_entry(&wsm->idle_ws, struct crush_work,
1060 item);
1061 list_del_init(&work->item);
1062 wsm->free_ws--;
1063 spin_unlock(&wsm->ws_lock);
1064 return work;
1065
1066 }
1067 if (atomic_read(&wsm->total_ws) > cpus) {
1068 DEFINE_WAIT(wait);
1069
1070 spin_unlock(&wsm->ws_lock);
1071 prepare_to_wait(&wsm->ws_wait, &wait, TASK_UNINTERRUPTIBLE);
1072 if (atomic_read(&wsm->total_ws) > cpus && !wsm->free_ws)
1073 schedule();
1074 finish_wait(&wsm->ws_wait, &wait);
1075 goto again;
1076 }
1077 atomic_inc(&wsm->total_ws);
1078 spin_unlock(&wsm->ws_lock);
1079
1080 work = alloc_workspace(c);
1081 if (!work) {
1082 atomic_dec(&wsm->total_ws);
1083 wake_up(&wsm->ws_wait);
1084
1085 /*
1086 * Do not return the error but go back to waiting. We
1087 * have the initial workspace and the CRUSH computation
1088 * time is bounded so we will get it eventually.
1089 */
1090 WARN_ON(atomic_read(&wsm->total_ws) < 1);
1091 goto again;
1092 }
1093 return work;
1094 }
1095
1096 /*
1097 * Puts a workspace back on the list or frees it if we have enough
1098 * idle ones sitting around.
1099 */
1100 static void put_workspace(struct workspace_manager *wsm,
1101 struct crush_work *work)
1102 {
1103 spin_lock(&wsm->ws_lock);
1104 if (wsm->free_ws <= num_online_cpus()) {
1105 list_add(&work->item, &wsm->idle_ws);
1106 wsm->free_ws++;
1107 spin_unlock(&wsm->ws_lock);
1108 goto wake;
1109 }
1110 spin_unlock(&wsm->ws_lock);
1111
1112 free_workspace(work);
1113 atomic_dec(&wsm->total_ws);
1114 wake:
1115 if (wq_has_sleeper(&wsm->ws_wait))
1116 wake_up(&wsm->ws_wait);
1117 }
1118
1119 /*
1120 * osd map
1121 */
1122 struct ceph_osdmap *ceph_osdmap_alloc(void)
1123 {
1124 struct ceph_osdmap *map;
1125
1126 map = kzalloc_obj(*map, GFP_NOIO);
1127 if (!map)
1128 return NULL;
1129
1130 map->pg_pools = RB_ROOT;
1131 map->pool_max = -1;
1132 map->pg_temp = RB_ROOT;
1133 map->primary_temp = RB_ROOT;
1134 map->pg_upmap = RB_ROOT;
1135 map->pg_upmap_items = RB_ROOT;
1136
1137 init_workspace_manager(&map->crush_wsm);
1138
1139 return map;
1140 }
1141
1142 void ceph_osdmap_destroy(struct ceph_osdmap *map)
1143 {
1144 dout("osdmap_destroy %p\n", map);
1145
1146 if (map->crush)
1147 crush_destroy(map->crush);
1148 cleanup_workspace_manager(&map->crush_wsm);
1149
1150 while (!RB_EMPTY_ROOT(&map->pg_temp)) {
1151 struct ceph_pg_mapping *pg =
1152 rb_entry(rb_first(&map->pg_temp),
1153 struct ceph_pg_mapping, node);
1154 erase_pg_mapping(&map->pg_temp, pg);
1155 free_pg_mapping(pg);
1156 }
1157 while (!RB_EMPTY_ROOT(&map->primary_temp)) {
1158 struct ceph_pg_mapping *pg =
1159 rb_entry(rb_first(&map->primary_temp),
1160 struct ceph_pg_mapping, node);
1161 erase_pg_mapping(&map->primary_temp, pg);
1162 free_pg_mapping(pg);
1163 }
1164 while (!RB_EMPTY_ROOT(&map->pg_upmap)) {
1165 struct ceph_pg_mapping *pg =
1166 rb_entry(rb_first(&map->pg_upmap),
1167 struct ceph_pg_mapping, node);
1168 rb_erase(&pg->node, &map->pg_upmap);
1169 kfree(pg);
1170 }
1171 while (!RB_EMPTY_ROOT(&map->pg_upmap_items)) {
1172 struct ceph_pg_mapping *pg =
1173 rb_entry(rb_first(&map->pg_upmap_items),
1174 struct ceph_pg_mapping, node);
1175 rb_erase(&pg->node, &map->pg_upmap_items);
1176 kfree(pg);
1177 }
1178 while (!RB_EMPTY_ROOT(&map->pg_pools)) {
1179 struct ceph_pg_pool_info *pi =
1180 rb_entry(rb_first(&map->pg_pools),
1181 struct ceph_pg_pool_info, node);
1182 __remove_pg_pool(&map->pg_pools, pi);
1183 }
1184 kvfree(map->osd_state);
1185 kvfree(map->osd_weight);
1186 kvfree(map->osd_addr);
1187 kvfree(map->osd_primary_affinity);
1188 kfree(map);
1189 }
1190
1191 /*
1192 * Adjust max_osd value, (re)allocate arrays.
1193 *
1194 * The new elements are properly initialized.
1195 */
1196 static int osdmap_set_max_osd(struct ceph_osdmap *map, u32 max)
1197 {
1198 u32 *state;
1199 u32 *weight;
1200 struct ceph_entity_addr *addr;
1201 u32 to_copy;
1202 int i;
1203
1204 dout("%s old %u new %u\n", __func__, map->max_osd, max);
1205 if (max == map->max_osd)
1206 return 0;
1207
1208 state = kvmalloc(array_size(max, sizeof(*state)), GFP_NOFS);
1209 weight = kvmalloc(array_size(max, sizeof(*weight)), GFP_NOFS);
1210 addr = kvmalloc(array_size(max, sizeof(*addr)), GFP_NOFS);
1211 if (!state || !weight || !addr) {
1212 kvfree(state);
1213 kvfree(weight);
1214 kvfree(addr);
1215 return -ENOMEM;
1216 }
1217
1218 to_copy = min(map->max_osd, max);
1219 if (map->osd_state) {
1220 memcpy(state, map->osd_state, to_copy * sizeof(*state));
1221 memcpy(weight, map->osd_weight, to_copy * sizeof(*weight));
1222 memcpy(addr, map->osd_addr, to_copy * sizeof(*addr));
1223 kvfree(map->osd_state);
1224 kvfree(map->osd_weight);
1225 kvfree(map->osd_addr);
1226 }
1227
1228 map->osd_state = state;
1229 map->osd_weight = weight;
1230 map->osd_addr = addr;
1231 for (i = map->max_osd; i < max; i++) {
1232 map->osd_state[i] = 0;
1233 map->osd_weight[i] = CEPH_OSD_OUT;
1234 memset(map->osd_addr + i, 0, sizeof(*map->osd_addr));
1235 }
1236
1237 if (map->osd_primary_affinity) {
1238 u32 *affinity;
1239
1240 affinity = kvmalloc(array_size(max, sizeof(*affinity)),
1241 GFP_NOFS);
1242 if (!affinity)
1243 return -ENOMEM;
1244
1245 memcpy(affinity, map->osd_primary_affinity,
1246 to_copy * sizeof(*affinity));
1247 kvfree(map->osd_primary_affinity);
1248
1249 map->osd_primary_affinity = affinity;
1250 for (i = map->max_osd; i < max; i++)
1251 map->osd_primary_affinity[i] =
1252 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
1253 }
1254
1255 map->max_osd = max;
1256
1257 return 0;
1258 }
1259
1260 static int osdmap_set_crush(struct ceph_osdmap *map, struct crush_map *crush)
1261 {
1262 struct crush_work *work;
1263
1264 if (IS_ERR(crush))
1265 return PTR_ERR(crush);
1266
1267 work = alloc_workspace(crush);
1268 if (!work) {
1269 crush_destroy(crush);
1270 return -ENOMEM;
1271 }
1272
1273 if (map->crush)
1274 crush_destroy(map->crush);
1275 cleanup_workspace_manager(&map->crush_wsm);
1276 map->crush = crush;
1277 add_initial_workspace(&map->crush_wsm, work);
1278 return 0;
1279 }
1280
1281 #define OSDMAP_WRAPPER_COMPAT_VER 7
1282 #define OSDMAP_CLIENT_DATA_COMPAT_VER 1
1283
1284 /*
1285 * Return 0 or error. On success, *v is set to 0 for old (v6) osdmaps,
1286 * to struct_v of the client_data section for new (v7 and above)
1287 * osdmaps.
1288 */
1289 static int get_osdmap_client_data_v(void **p, void *end,
1290 const char *prefix, u8 *v)
1291 {
1292 u8 struct_v;
1293
1294 ceph_decode_8_safe(p, end, struct_v, e_inval);
1295 if (struct_v >= 7) {
1296 u8 struct_compat;
1297
1298 ceph_decode_8_safe(p, end, struct_compat, e_inval);
1299 if (struct_compat > OSDMAP_WRAPPER_COMPAT_VER) {
1300 pr_warn("got v %d cv %d > %d of %s ceph_osdmap\n",
1301 struct_v, struct_compat,
1302 OSDMAP_WRAPPER_COMPAT_VER, prefix);
1303 return -EINVAL;
1304 }
1305 *p += 4; /* ignore wrapper struct_len */
1306
1307 ceph_decode_8_safe(p, end, struct_v, e_inval);
1308 ceph_decode_8_safe(p, end, struct_compat, e_inval);
1309 if (struct_compat > OSDMAP_CLIENT_DATA_COMPAT_VER) {
1310 pr_warn("got v %d cv %d > %d of %s ceph_osdmap client data\n",
1311 struct_v, struct_compat,
1312 OSDMAP_CLIENT_DATA_COMPAT_VER, prefix);
1313 return -EINVAL;
1314 }
1315 *p += 4; /* ignore client data struct_len */
1316 } else {
1317 u16 version;
1318
1319 *p -= 1;
1320 ceph_decode_16_safe(p, end, version, e_inval);
1321 if (version < 6) {
1322 pr_warn("got v %d < 6 of %s ceph_osdmap\n",
1323 version, prefix);
1324 return -EINVAL;
1325 }
1326
1327 /* old osdmap encoding */
1328 struct_v = 0;
1329 }
1330
1331 *v = struct_v;
1332 return 0;
1333
1334 e_inval:
1335 return -EINVAL;
1336 }
1337
1338 static int __decode_pools(void **p, void *end, struct ceph_osdmap *map,
1339 bool incremental)
1340 {
1341 u32 n;
1342
1343 ceph_decode_32_safe(p, end, n, e_inval);
1344 while (n--) {
1345 struct ceph_pg_pool_info *pi;
1346 u64 pool;
1347 int ret;
1348
1349 ceph_decode_64_safe(p, end, pool, e_inval);
1350
1351 pi = lookup_pg_pool(&map->pg_pools, pool);
1352 if (!incremental || !pi) {
1353 pi = kzalloc_obj(*pi, GFP_NOFS);
1354 if (!pi)
1355 return -ENOMEM;
1356
1357 RB_CLEAR_NODE(&pi->node);
1358 pi->id = pool;
1359
1360 if (!__insert_pg_pool(&map->pg_pools, pi)) {
1361 kfree(pi);
1362 return -EEXIST;
1363 }
1364 }
1365
1366 ret = decode_pool(p, end, pi);
1367 if (ret)
1368 return ret;
1369 }
1370
1371 return 0;
1372
1373 e_inval:
1374 return -EINVAL;
1375 }
1376
1377 static int decode_pools(void **p, void *end, struct ceph_osdmap *map)
1378 {
1379 return __decode_pools(p, end, map, false);
1380 }
1381
1382 static int decode_new_pools(void **p, void *end, struct ceph_osdmap *map)
1383 {
1384 return __decode_pools(p, end, map, true);
1385 }
1386
1387 typedef struct ceph_pg_mapping *(*decode_mapping_fn_t)(void **, void *, bool);
1388
1389 static int decode_pg_mapping(void **p, void *end, struct rb_root *mapping_root,
1390 decode_mapping_fn_t fn, bool incremental)
1391 {
1392 u32 n;
1393
1394 WARN_ON(!incremental && !fn);
1395
1396 ceph_decode_32_safe(p, end, n, e_inval);
1397 while (n--) {
1398 struct ceph_pg_mapping *pg;
1399 struct ceph_pg pgid;
1400 int ret;
1401
1402 ret = ceph_decode_pgid(p, end, &pgid);
1403 if (ret)
1404 return ret;
1405
1406 pg = lookup_pg_mapping(mapping_root, &pgid);
1407 if (pg) {
1408 WARN_ON(!incremental);
1409 erase_pg_mapping(mapping_root, pg);
1410 free_pg_mapping(pg);
1411 }
1412
1413 if (fn) {
1414 pg = fn(p, end, incremental);
1415 if (IS_ERR(pg))
1416 return PTR_ERR(pg);
1417
1418 if (pg) {
1419 pg->pgid = pgid; /* struct */
1420 insert_pg_mapping(mapping_root, pg);
1421 }
1422 }
1423 }
1424
1425 return 0;
1426
1427 e_inval:
1428 return -EINVAL;
1429 }
1430
1431 static struct ceph_pg_mapping *__decode_pg_temp(void **p, void *end,
1432 bool incremental)
1433 {
1434 struct ceph_pg_mapping *pg;
1435 u32 len, i;
1436
1437 ceph_decode_32_safe(p, end, len, e_inval);
1438 if (len == 0 && incremental)
1439 return NULL; /* new_pg_temp: [] to remove */
1440 if ((size_t)len > (SIZE_MAX - sizeof(*pg)) / sizeof(u32))
1441 return ERR_PTR(-EINVAL);
1442
1443 ceph_decode_need(p, end, len * sizeof(u32), e_inval);
1444 pg = alloc_pg_mapping(len * sizeof(u32));
1445 if (!pg)
1446 return ERR_PTR(-ENOMEM);
1447
1448 pg->pg_temp.len = len;
1449 for (i = 0; i < len; i++)
1450 pg->pg_temp.osds[i] = ceph_decode_32(p);
1451
1452 return pg;
1453
1454 e_inval:
1455 return ERR_PTR(-EINVAL);
1456 }
1457
1458 static int decode_pg_temp(void **p, void *end, struct ceph_osdmap *map)
1459 {
1460 return decode_pg_mapping(p, end, &map->pg_temp, __decode_pg_temp,
1461 false);
1462 }
1463
1464 static int decode_new_pg_temp(void **p, void *end, struct ceph_osdmap *map)
1465 {
1466 return decode_pg_mapping(p, end, &map->pg_temp, __decode_pg_temp,
1467 true);
1468 }
1469
1470 static struct ceph_pg_mapping *__decode_primary_temp(void **p, void *end,
1471 bool incremental)
1472 {
1473 struct ceph_pg_mapping *pg;
1474 u32 osd;
1475
1476 ceph_decode_32_safe(p, end, osd, e_inval);
1477 if (osd == (u32)-1 && incremental)
1478 return NULL; /* new_primary_temp: -1 to remove */
1479
1480 pg = alloc_pg_mapping(0);
1481 if (!pg)
1482 return ERR_PTR(-ENOMEM);
1483
1484 pg->primary_temp.osd = osd;
1485 return pg;
1486
1487 e_inval:
1488 return ERR_PTR(-EINVAL);
1489 }
1490
1491 static int decode_primary_temp(void **p, void *end, struct ceph_osdmap *map)
1492 {
1493 return decode_pg_mapping(p, end, &map->primary_temp,
1494 __decode_primary_temp, false);
1495 }
1496
1497 static int decode_new_primary_temp(void **p, void *end,
1498 struct ceph_osdmap *map)
1499 {
1500 return decode_pg_mapping(p, end, &map->primary_temp,
1501 __decode_primary_temp, true);
1502 }
1503
1504 u32 ceph_get_primary_affinity(struct ceph_osdmap *map, int osd)
1505 {
1506 if (!map->osd_primary_affinity)
1507 return CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
1508
1509 return map->osd_primary_affinity[osd];
1510 }
1511
1512 static int set_primary_affinity(struct ceph_osdmap *map, int osd, u32 aff)
1513 {
1514 if (!map->osd_primary_affinity) {
1515 int i;
1516
1517 map->osd_primary_affinity = kvmalloc(
1518 array_size(map->max_osd, sizeof(*map->osd_primary_affinity)),
1519 GFP_NOFS);
1520 if (!map->osd_primary_affinity)
1521 return -ENOMEM;
1522
1523 for (i = 0; i < map->max_osd; i++)
1524 map->osd_primary_affinity[i] =
1525 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
1526 }
1527
1528 map->osd_primary_affinity[osd] = aff;
1529
1530 return 0;
1531 }
1532
1533 static int decode_primary_affinity(void **p, void *end,
1534 struct ceph_osdmap *map)
1535 {
1536 u32 len, i;
1537
1538 ceph_decode_32_safe(p, end, len, e_inval);
1539 if (len == 0) {
1540 kvfree(map->osd_primary_affinity);
1541 map->osd_primary_affinity = NULL;
1542 return 0;
1543 }
1544 if (len != map->max_osd)
1545 goto e_inval;
1546
1547 ceph_decode_need(p, end, map->max_osd*sizeof(u32), e_inval);
1548
1549 for (i = 0; i < map->max_osd; i++) {
1550 int ret;
1551
1552 ret = set_primary_affinity(map, i, ceph_decode_32(p));
1553 if (ret)
1554 return ret;
1555 }
1556
1557 return 0;
1558
1559 e_inval:
1560 return -EINVAL;
1561 }
1562
1563 static int decode_new_primary_affinity(void **p, void *end,
1564 struct ceph_osdmap *map)
1565 {
1566 u32 n;
1567
1568 ceph_decode_32_safe(p, end, n, e_inval);
1569 while (n--) {
1570 u32 osd, aff;
1571 int ret;
1572
1573 ceph_decode_32_safe(p, end, osd, e_inval);
1574 ceph_decode_32_safe(p, end, aff, e_inval);
1575 if (osd >= map->max_osd)
1576 goto e_inval;
1577
1578 ret = set_primary_affinity(map, osd, aff);
1579 if (ret)
1580 return ret;
1581
1582 osdmap_info(map, "osd%d primary-affinity 0x%x\n", osd, aff);
1583 }
1584
1585 return 0;
1586
1587 e_inval:
1588 return -EINVAL;
1589 }
1590
1591 static struct ceph_pg_mapping *__decode_pg_upmap(void **p, void *end,
1592 bool __unused)
1593 {
1594 return __decode_pg_temp(p, end, false);
1595 }
1596
1597 static int decode_pg_upmap(void **p, void *end, struct ceph_osdmap *map)
1598 {
1599 return decode_pg_mapping(p, end, &map->pg_upmap, __decode_pg_upmap,
1600 false);
1601 }
1602
1603 static int decode_new_pg_upmap(void **p, void *end, struct ceph_osdmap *map)
1604 {
1605 return decode_pg_mapping(p, end, &map->pg_upmap, __decode_pg_upmap,
1606 true);
1607 }
1608
1609 static int decode_old_pg_upmap(void **p, void *end, struct ceph_osdmap *map)
1610 {
1611 return decode_pg_mapping(p, end, &map->pg_upmap, NULL, true);
1612 }
1613
1614 static struct ceph_pg_mapping *__decode_pg_upmap_items(void **p, void *end,
1615 bool __unused)
1616 {
1617 struct ceph_pg_mapping *pg;
1618 u32 len, i;
1619
1620 ceph_decode_32_safe(p, end, len, e_inval);
1621 if ((size_t)len > (SIZE_MAX - sizeof(*pg)) / (2 * sizeof(u32)))
1622 return ERR_PTR(-EINVAL);
1623
1624 ceph_decode_need(p, end, 2 * len * sizeof(u32), e_inval);
1625 pg = alloc_pg_mapping(2 * len * sizeof(u32));
1626 if (!pg)
1627 return ERR_PTR(-ENOMEM);
1628
1629 pg->pg_upmap_items.len = len;
1630 for (i = 0; i < len; i++) {
1631 pg->pg_upmap_items.from_to[i][0] = ceph_decode_32(p);
1632 pg->pg_upmap_items.from_to[i][1] = ceph_decode_32(p);
1633 }
1634
1635 return pg;
1636
1637 e_inval:
1638 return ERR_PTR(-EINVAL);
1639 }
1640
1641 static int decode_pg_upmap_items(void **p, void *end, struct ceph_osdmap *map)
1642 {
1643 return decode_pg_mapping(p, end, &map->pg_upmap_items,
1644 __decode_pg_upmap_items, false);
1645 }
1646
1647 static int decode_new_pg_upmap_items(void **p, void *end,
1648 struct ceph_osdmap *map)
1649 {
1650 return decode_pg_mapping(p, end, &map->pg_upmap_items,
1651 __decode_pg_upmap_items, true);
1652 }
1653
1654 static int decode_old_pg_upmap_items(void **p, void *end,
1655 struct ceph_osdmap *map)
1656 {
1657 return decode_pg_mapping(p, end, &map->pg_upmap_items, NULL, true);
1658 }
1659
1660 /*
1661 * decode a full map.
1662 */
1663 static int osdmap_decode(void **p, void *end, bool msgr2,
1664 struct ceph_osdmap *map)
1665 {
1666 u8 struct_v;
1667 u32 epoch = 0;
1668 void *start = *p;
1669 u32 max;
1670 u32 len, i;
1671 int err;
1672
1673 dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p));
1674
1675 err = get_osdmap_client_data_v(p, end, "full", &struct_v);
1676 if (err)
1677 goto bad;
1678
1679 /* fsid, epoch, created, modified */
1680 ceph_decode_need(p, end, sizeof(map->fsid) + sizeof(u32) +
1681 sizeof(map->created) + sizeof(map->modified), e_inval);
1682 ceph_decode_copy(p, &map->fsid, sizeof(map->fsid));
1683 epoch = map->epoch = ceph_decode_32(p);
1684 ceph_decode_copy(p, &map->created, sizeof(map->created));
1685 ceph_decode_copy(p, &map->modified, sizeof(map->modified));
1686
1687 /* pools */
1688 err = decode_pools(p, end, map);
1689 if (err)
1690 goto bad;
1691
1692 /* pool_name */
1693 err = decode_pool_names(p, end, map);
1694 if (err)
1695 goto bad;
1696
1697 ceph_decode_32_safe(p, end, map->pool_max, e_inval);
1698
1699 ceph_decode_32_safe(p, end, map->flags, e_inval);
1700
1701 /* max_osd */
1702 ceph_decode_32_safe(p, end, max, e_inval);
1703
1704 /* (re)alloc osd arrays */
1705 err = osdmap_set_max_osd(map, max);
1706 if (err)
1707 goto bad;
1708
1709 /* osd_state, osd_weight, osd_addrs->client_addr */
1710 ceph_decode_need(p, end, 3*sizeof(u32) +
1711 map->max_osd*(struct_v >= 5 ? sizeof(u32) :
1712 sizeof(u8)) +
1713 map->max_osd*sizeof(*map->osd_weight), e_inval);
1714 if (ceph_decode_32(p) != map->max_osd)
1715 goto e_inval;
1716
1717 if (struct_v >= 5) {
1718 for (i = 0; i < map->max_osd; i++)
1719 map->osd_state[i] = ceph_decode_32(p);
1720 } else {
1721 for (i = 0; i < map->max_osd; i++)
1722 map->osd_state[i] = ceph_decode_8(p);
1723 }
1724
1725 if (ceph_decode_32(p) != map->max_osd)
1726 goto e_inval;
1727
1728 for (i = 0; i < map->max_osd; i++)
1729 map->osd_weight[i] = ceph_decode_32(p);
1730
1731 if (ceph_decode_32(p) != map->max_osd)
1732 goto e_inval;
1733
1734 for (i = 0; i < map->max_osd; i++) {
1735 struct ceph_entity_addr *addr = &map->osd_addr[i];
1736
1737 if (struct_v >= 8)
1738 err = ceph_decode_entity_addrvec(p, end, msgr2, addr);
1739 else
1740 err = ceph_decode_entity_addr(p, end, addr);
1741 if (err)
1742 goto bad;
1743
1744 dout("%s osd%d addr %s\n", __func__, i, ceph_pr_addr(addr));
1745 }
1746
1747 /* pg_temp */
1748 err = decode_pg_temp(p, end, map);
1749 if (err)
1750 goto bad;
1751
1752 /* primary_temp */
1753 if (struct_v >= 1) {
1754 err = decode_primary_temp(p, end, map);
1755 if (err)
1756 goto bad;
1757 }
1758
1759 /* primary_affinity */
1760 if (struct_v >= 2) {
1761 err = decode_primary_affinity(p, end, map);
1762 if (err)
1763 goto bad;
1764 } else {
1765 WARN_ON(map->osd_primary_affinity);
1766 }
1767
1768 /* crush */
1769 ceph_decode_32_safe(p, end, len, e_inval);
1770 err = osdmap_set_crush(map, crush_decode(*p, min(*p + len, end)));
1771 if (err)
1772 goto bad;
1773
1774 *p += len;
1775 if (struct_v >= 3) {
1776 /* erasure_code_profiles */
1777 ceph_decode_skip_map_of_map(p, end, string, string, string,
1778 e_inval);
1779 }
1780
1781 if (struct_v >= 4) {
1782 err = decode_pg_upmap(p, end, map);
1783 if (err)
1784 goto bad;
1785
1786 err = decode_pg_upmap_items(p, end, map);
1787 if (err)
1788 goto bad;
1789 } else {
1790 WARN_ON(!RB_EMPTY_ROOT(&map->pg_upmap));
1791 WARN_ON(!RB_EMPTY_ROOT(&map->pg_upmap_items));
1792 }
1793
1794 /* ignore the rest */
1795 *p = end;
1796
1797 dout("full osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd);
1798 return 0;
1799
1800 e_inval:
1801 err = -EINVAL;
1802 bad:
1803 pr_err("corrupt full osdmap (%d) epoch %d off %d (%p of %p-%p)\n",
1804 err, epoch, (int)(*p - start), *p, start, end);
1805 print_hex_dump(KERN_DEBUG, "osdmap: ",
1806 DUMP_PREFIX_OFFSET, 16, 1,
1807 start, end - start, true);
1808 return err;
1809 }
1810
1811 /*
1812 * Allocate and decode a full map.
1813 */
1814 struct ceph_osdmap *ceph_osdmap_decode(void **p, void *end, bool msgr2)
1815 {
1816 struct ceph_osdmap *map;
1817 int ret;
1818
1819 map = ceph_osdmap_alloc();
1820 if (!map)
1821 return ERR_PTR(-ENOMEM);
1822
1823 ret = osdmap_decode(p, end, msgr2, map);
1824 if (ret) {
1825 ceph_osdmap_destroy(map);
1826 return ERR_PTR(ret);
1827 }
1828
1829 return map;
1830 }
1831
1832 /*
1833 * Encoding order is (new_up_client, new_state, new_weight). Need to
1834 * apply in the (new_weight, new_state, new_up_client) order, because
1835 * an incremental map may look like e.g.
1836 *
1837 * new_up_client: { osd=6, addr=... } # set osd_state and addr
1838 * new_state: { osd=6, xorstate=EXISTS } # clear osd_state
1839 */
1840 static int decode_new_up_state_weight(void **p, void *end, u8 struct_v,
1841 bool msgr2, struct ceph_osdmap *map)
1842 {
1843 void *new_up_client;
1844 void *new_state;
1845 void *new_weight_end;
1846 u32 len;
1847 int ret;
1848 int i;
1849
1850 new_up_client = *p;
1851 ceph_decode_32_safe(p, end, len, e_inval);
1852 for (i = 0; i < len; ++i) {
1853 struct ceph_entity_addr addr;
1854
1855 ceph_decode_skip_32(p, end, e_inval);
1856 if (struct_v >= 7)
1857 ret = ceph_decode_entity_addrvec(p, end, msgr2, &addr);
1858 else
1859 ret = ceph_decode_entity_addr(p, end, &addr);
1860 if (ret)
1861 return ret;
1862 }
1863
1864 new_state = *p;
1865 ceph_decode_32_safe(p, end, len, e_inval);
1866 len *= sizeof(u32) + (struct_v >= 5 ? sizeof(u32) : sizeof(u8));
1867 ceph_decode_need(p, end, len, e_inval);
1868 *p += len;
1869
1870 /* new_weight */
1871 ceph_decode_32_safe(p, end, len, e_inval);
1872 while (len--) {
1873 s32 osd;
1874 u32 w;
1875
1876 ceph_decode_need(p, end, 2*sizeof(u32), e_inval);
1877 osd = ceph_decode_32(p);
1878 w = ceph_decode_32(p);
1879 if (osd >= map->max_osd)
1880 goto e_inval;
1881
1882 osdmap_info(map, "osd%d weight 0x%x %s\n", osd, w,
1883 w == CEPH_OSD_IN ? "(in)" :
1884 (w == CEPH_OSD_OUT ? "(out)" : ""));
1885 map->osd_weight[osd] = w;
1886
1887 /*
1888 * If we are marking in, set the EXISTS, and clear the
1889 * AUTOOUT and NEW bits.
1890 */
1891 if (w) {
1892 map->osd_state[osd] |= CEPH_OSD_EXISTS;
1893 map->osd_state[osd] &= ~(CEPH_OSD_AUTOOUT |
1894 CEPH_OSD_NEW);
1895 }
1896 }
1897 new_weight_end = *p;
1898
1899 /* new_state (up/down) */
1900 *p = new_state;
1901 len = ceph_decode_32(p);
1902 while (len--) {
1903 s32 osd;
1904 u32 xorstate;
1905
1906 osd = ceph_decode_32(p);
1907 if (osd >= map->max_osd)
1908 goto e_inval;
1909
1910 if (struct_v >= 5)
1911 xorstate = ceph_decode_32(p);
1912 else
1913 xorstate = ceph_decode_8(p);
1914 if (xorstate == 0)
1915 xorstate = CEPH_OSD_UP;
1916 if ((map->osd_state[osd] & CEPH_OSD_UP) &&
1917 (xorstate & CEPH_OSD_UP))
1918 osdmap_info(map, "osd%d down\n", osd);
1919 if ((map->osd_state[osd] & CEPH_OSD_EXISTS) &&
1920 (xorstate & CEPH_OSD_EXISTS)) {
1921 osdmap_info(map, "osd%d does not exist\n", osd);
1922 ret = set_primary_affinity(map, osd,
1923 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY);
1924 if (ret)
1925 return ret;
1926 memset(map->osd_addr + osd, 0, sizeof(*map->osd_addr));
1927 map->osd_state[osd] = 0;
1928 } else {
1929 map->osd_state[osd] ^= xorstate;
1930 }
1931 }
1932
1933 /* new_up_client */
1934 *p = new_up_client;
1935 len = ceph_decode_32(p);
1936 while (len--) {
1937 s32 osd;
1938 struct ceph_entity_addr addr;
1939
1940 osd = ceph_decode_32(p);
1941 if (osd >= map->max_osd)
1942 goto e_inval;
1943
1944 if (struct_v >= 7)
1945 ret = ceph_decode_entity_addrvec(p, end, msgr2, &addr);
1946 else
1947 ret = ceph_decode_entity_addr(p, end, &addr);
1948 if (ret)
1949 return ret;
1950
1951 dout("%s osd%d addr %s\n", __func__, osd, ceph_pr_addr(&addr));
1952
1953 osdmap_info(map, "osd%d up\n", osd);
1954 map->osd_state[osd] |= CEPH_OSD_EXISTS | CEPH_OSD_UP;
1955 map->osd_addr[osd] = addr;
1956 }
1957
1958 *p = new_weight_end;
1959 return 0;
1960
1961 e_inval:
1962 return -EINVAL;
1963 }
1964
1965 /*
1966 * decode and apply an incremental map update.
1967 */
1968 struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end, bool msgr2,
1969 struct ceph_osdmap *map)
1970 {
1971 struct ceph_fsid fsid;
1972 u32 epoch = 0;
1973 struct ceph_timespec modified;
1974 s32 len;
1975 u64 pool;
1976 __s64 new_pool_max;
1977 __s32 new_flags, max;
1978 void *start = *p;
1979 int err;
1980 u8 struct_v;
1981
1982 dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p));
1983
1984 err = get_osdmap_client_data_v(p, end, "inc", &struct_v);
1985 if (err)
1986 goto bad;
1987
1988 /* fsid, epoch, modified, new_pool_max, new_flags */
1989 ceph_decode_need(p, end, sizeof(fsid) + sizeof(u32) + sizeof(modified) +
1990 sizeof(u64) + sizeof(u32), e_inval);
1991 ceph_decode_copy(p, &fsid, sizeof(fsid));
1992 epoch = ceph_decode_32(p);
1993 ceph_decode_copy(p, &modified, sizeof(modified));
1994 new_pool_max = ceph_decode_64(p);
1995 new_flags = ceph_decode_32(p);
1996
1997 if (epoch != map->epoch + 1)
1998 goto e_inval;
1999
2000 /* full map? */
2001 ceph_decode_32_safe(p, end, len, e_inval);
2002 if (len > 0) {
2003 dout("apply_incremental full map len %d, %p to %p\n",
2004 len, *p, end);
2005 return ceph_osdmap_decode(p, min(*p+len, end), msgr2);
2006 }
2007
2008 /* new crush? */
2009 ceph_decode_32_safe(p, end, len, e_inval);
2010 if (len > 0) {
2011 err = osdmap_set_crush(map,
2012 crush_decode(*p, min(*p + len, end)));
2013 if (err)
2014 goto bad;
2015 *p += len;
2016 }
2017
2018 /* new flags? */
2019 if (new_flags >= 0)
2020 map->flags = new_flags;
2021 if (new_pool_max >= 0)
2022 map->pool_max = new_pool_max;
2023
2024 /* new max? */
2025 ceph_decode_32_safe(p, end, max, e_inval);
2026 if (max >= 0) {
2027 err = osdmap_set_max_osd(map, max);
2028 if (err)
2029 goto bad;
2030 }
2031
2032 map->epoch++;
2033 map->modified = modified;
2034
2035 /* new_pools */
2036 err = decode_new_pools(p, end, map);
2037 if (err)
2038 goto bad;
2039
2040 /* new_pool_names */
2041 err = decode_pool_names(p, end, map);
2042 if (err)
2043 goto bad;
2044
2045 /* old_pool */
2046 ceph_decode_32_safe(p, end, len, e_inval);
2047 while (len--) {
2048 struct ceph_pg_pool_info *pi;
2049
2050 ceph_decode_64_safe(p, end, pool, e_inval);
2051 pi = lookup_pg_pool(&map->pg_pools, pool);
2052 if (pi)
2053 __remove_pg_pool(&map->pg_pools, pi);
2054 }
2055
2056 /* new_up_client, new_state, new_weight */
2057 err = decode_new_up_state_weight(p, end, struct_v, msgr2, map);
2058 if (err)
2059 goto bad;
2060
2061 /* new_pg_temp */
2062 err = decode_new_pg_temp(p, end, map);
2063 if (err)
2064 goto bad;
2065
2066 /* new_primary_temp */
2067 if (struct_v >= 1) {
2068 err = decode_new_primary_temp(p, end, map);
2069 if (err)
2070 goto bad;
2071 }
2072
2073 /* new_primary_affinity */
2074 if (struct_v >= 2) {
2075 err = decode_new_primary_affinity(p, end, map);
2076 if (err)
2077 goto bad;
2078 }
2079
2080 if (struct_v >= 3) {
2081 /* new_erasure_code_profiles */
2082 ceph_decode_skip_map_of_map(p, end, string, string, string,
2083 e_inval);
2084 /* old_erasure_code_profiles */
2085 ceph_decode_skip_set(p, end, string, e_inval);
2086 }
2087
2088 if (struct_v >= 4) {
2089 err = decode_new_pg_upmap(p, end, map);
2090 if (err)
2091 goto bad;
2092
2093 err = decode_old_pg_upmap(p, end, map);
2094 if (err)
2095 goto bad;
2096
2097 err = decode_new_pg_upmap_items(p, end, map);
2098 if (err)
2099 goto bad;
2100
2101 err = decode_old_pg_upmap_items(p, end, map);
2102 if (err)
2103 goto bad;
2104 }
2105
2106 /* ignore the rest */
2107 *p = end;
2108
2109 dout("inc osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd);
2110 return map;
2111
2112 e_inval:
2113 err = -EINVAL;
2114 bad:
2115 pr_err("corrupt inc osdmap (%d) epoch %d off %d (%p of %p-%p)\n",
2116 err, epoch, (int)(*p - start), *p, start, end);
2117 print_hex_dump(KERN_DEBUG, "osdmap: ",
2118 DUMP_PREFIX_OFFSET, 16, 1,
2119 start, end - start, true);
2120 return ERR_PTR(err);
2121 }
2122
2123 void ceph_oloc_copy(struct ceph_object_locator *dest,
2124 const struct ceph_object_locator *src)
2125 {
2126 ceph_oloc_destroy(dest);
2127
2128 dest->pool = src->pool;
2129 if (src->pool_ns)
2130 dest->pool_ns = ceph_get_string(src->pool_ns);
2131 else
2132 dest->pool_ns = NULL;
2133 }
2134 EXPORT_SYMBOL(ceph_oloc_copy);
2135
2136 void ceph_oloc_destroy(struct ceph_object_locator *oloc)
2137 {
2138 ceph_put_string(oloc->pool_ns);
2139 }
2140 EXPORT_SYMBOL(ceph_oloc_destroy);
2141
2142 void ceph_oid_copy(struct ceph_object_id *dest,
2143 const struct ceph_object_id *src)
2144 {
2145 ceph_oid_destroy(dest);
2146
2147 if (src->name != src->inline_name) {
2148 /* very rare, see ceph_object_id definition */
2149 dest->name = kmalloc(src->name_len + 1,
2150 GFP_NOIO | __GFP_NOFAIL);
2151 } else {
2152 dest->name = dest->inline_name;
2153 }
2154 memcpy(dest->name, src->name, src->name_len + 1);
2155 dest->name_len = src->name_len;
2156 }
2157 EXPORT_SYMBOL(ceph_oid_copy);
2158
2159 static __printf(2, 0)
2160 int oid_printf_vargs(struct ceph_object_id *oid, const char *fmt, va_list ap)
2161 {
2162 int len;
2163
2164 WARN_ON(!ceph_oid_empty(oid));
2165
2166 len = vsnprintf(oid->inline_name, sizeof(oid->inline_name), fmt, ap);
2167 if (len >= sizeof(oid->inline_name))
2168 return len;
2169
2170 oid->name_len = len;
2171 return 0;
2172 }
2173
2174 /*
2175 * If oid doesn't fit into inline buffer, BUG.
2176 */
2177 void ceph_oid_printf(struct ceph_object_id *oid, const char *fmt, ...)
2178 {
2179 va_list ap;
2180
2181 va_start(ap, fmt);
2182 BUG_ON(oid_printf_vargs(oid, fmt, ap));
2183 va_end(ap);
2184 }
2185 EXPORT_SYMBOL(ceph_oid_printf);
2186
2187 static __printf(3, 0)
2188 int oid_aprintf_vargs(struct ceph_object_id *oid, gfp_t gfp,
2189 const char *fmt, va_list ap)
2190 {
2191 va_list aq;
2192 int len;
2193
2194 va_copy(aq, ap);
2195 len = oid_printf_vargs(oid, fmt, aq);
2196 va_end(aq);
2197
2198 if (len) {
2199 char *external_name;
2200
2201 external_name = kmalloc(len + 1, gfp);
2202 if (!external_name)
2203 return -ENOMEM;
2204
2205 oid->name = external_name;
2206 WARN_ON(vsnprintf(oid->name, len + 1, fmt, ap) != len);
2207 oid->name_len = len;
2208 }
2209
2210 return 0;
2211 }
2212
2213 /*
2214 * If oid doesn't fit into inline buffer, allocate.
2215 */
2216 int ceph_oid_aprintf(struct ceph_object_id *oid, gfp_t gfp,
2217 const char *fmt, ...)
2218 {
2219 va_list ap;
2220 int ret;
2221
2222 va_start(ap, fmt);
2223 ret = oid_aprintf_vargs(oid, gfp, fmt, ap);
2224 va_end(ap);
2225
2226 return ret;
2227 }
2228 EXPORT_SYMBOL(ceph_oid_aprintf);
2229
2230 void ceph_oid_destroy(struct ceph_object_id *oid)
2231 {
2232 if (oid->name != oid->inline_name)
2233 kfree(oid->name);
2234 }
2235 EXPORT_SYMBOL(ceph_oid_destroy);
2236
2237 /*
2238 * osds only
2239 */
2240 static bool __osds_equal(const struct ceph_osds *lhs,
2241 const struct ceph_osds *rhs)
2242 {
2243 if (lhs->size == rhs->size &&
2244 !memcmp(lhs->osds, rhs->osds, rhs->size * sizeof(rhs->osds[0])))
2245 return true;
2246
2247 return false;
2248 }
2249
2250 /*
2251 * osds + primary
2252 */
2253 static bool osds_equal(const struct ceph_osds *lhs,
2254 const struct ceph_osds *rhs)
2255 {
2256 if (__osds_equal(lhs, rhs) &&
2257 lhs->primary == rhs->primary)
2258 return true;
2259
2260 return false;
2261 }
2262
2263 static bool osds_valid(const struct ceph_osds *set)
2264 {
2265 /* non-empty set */
2266 if (set->size > 0 && set->primary >= 0)
2267 return true;
2268
2269 /* empty can_shift_osds set */
2270 if (!set->size && set->primary == -1)
2271 return true;
2272
2273 /* empty !can_shift_osds set - all NONE */
2274 if (set->size > 0 && set->primary == -1) {
2275 int i;
2276
2277 for (i = 0; i < set->size; i++) {
2278 if (set->osds[i] != CRUSH_ITEM_NONE)
2279 break;
2280 }
2281 if (i == set->size)
2282 return true;
2283 }
2284
2285 return false;
2286 }
2287
2288 void ceph_osds_copy(struct ceph_osds *dest, const struct ceph_osds *src)
2289 {
2290 memcpy(dest->osds, src->osds, src->size * sizeof(src->osds[0]));
2291 dest->size = src->size;
2292 dest->primary = src->primary;
2293 }
2294
2295 bool ceph_pg_is_split(const struct ceph_pg *pgid, u32 old_pg_num,
2296 u32 new_pg_num)
2297 {
2298 int old_bits = calc_bits_of(old_pg_num);
2299 int old_mask = (1 << old_bits) - 1;
2300 int n;
2301
2302 WARN_ON(pgid->seed >= old_pg_num);
2303 if (new_pg_num <= old_pg_num)
2304 return false;
2305
2306 for (n = 1; ; n++) {
2307 int next_bit = n << (old_bits - 1);
2308 u32 s = next_bit | pgid->seed;
2309
2310 if (s < old_pg_num || s == pgid->seed)
2311 continue;
2312 if (s >= new_pg_num)
2313 break;
2314
2315 s = ceph_stable_mod(s, old_pg_num, old_mask);
2316 if (s == pgid->seed)
2317 return true;
2318 }
2319
2320 return false;
2321 }
2322
2323 bool ceph_is_new_interval(const struct ceph_osds *old_acting,
2324 const struct ceph_osds *new_acting,
2325 const struct ceph_osds *old_up,
2326 const struct ceph_osds *new_up,
2327 int old_size,
2328 int new_size,
2329 int old_min_size,
2330 int new_min_size,
2331 u32 old_pg_num,
2332 u32 new_pg_num,
2333 bool old_sort_bitwise,
2334 bool new_sort_bitwise,
2335 bool old_recovery_deletes,
2336 bool new_recovery_deletes,
2337 const struct ceph_pg *pgid)
2338 {
2339 return !osds_equal(old_acting, new_acting) ||
2340 !osds_equal(old_up, new_up) ||
2341 old_size != new_size ||
2342 old_min_size != new_min_size ||
2343 ceph_pg_is_split(pgid, old_pg_num, new_pg_num) ||
2344 old_sort_bitwise != new_sort_bitwise ||
2345 old_recovery_deletes != new_recovery_deletes;
2346 }
2347
2348 static int calc_pg_rank(int osd, const struct ceph_osds *acting)
2349 {
2350 int i;
2351
2352 for (i = 0; i < acting->size; i++) {
2353 if (acting->osds[i] == osd)
2354 return i;
2355 }
2356
2357 return -1;
2358 }
2359
2360 static bool primary_changed(const struct ceph_osds *old_acting,
2361 const struct ceph_osds *new_acting)
2362 {
2363 if (!old_acting->size && !new_acting->size)
2364 return false; /* both still empty */
2365
2366 if (!old_acting->size ^ !new_acting->size)
2367 return true; /* was empty, now not, or vice versa */
2368
2369 if (old_acting->primary != new_acting->primary)
2370 return true; /* primary changed */
2371
2372 if (calc_pg_rank(old_acting->primary, old_acting) !=
2373 calc_pg_rank(new_acting->primary, new_acting))
2374 return true;
2375
2376 return false; /* same primary (tho replicas may have changed) */
2377 }
2378
2379 bool ceph_osds_changed(const struct ceph_osds *old_acting,
2380 const struct ceph_osds *new_acting,
2381 bool any_change)
2382 {
2383 if (primary_changed(old_acting, new_acting))
2384 return true;
2385
2386 if (any_change && !__osds_equal(old_acting, new_acting))
2387 return true;
2388
2389 return false;
2390 }
2391
2392 /*
2393 * Map an object into a PG.
2394 *
2395 * Should only be called with target_oid and target_oloc (as opposed to
2396 * base_oid and base_oloc), since tiering isn't taken into account.
2397 */
2398 void __ceph_object_locator_to_pg(struct ceph_pg_pool_info *pi,
2399 const struct ceph_object_id *oid,
2400 const struct ceph_object_locator *oloc,
2401 struct ceph_pg *raw_pgid)
2402 {
2403 WARN_ON(pi->id != oloc->pool);
2404
2405 if (!oloc->pool_ns) {
2406 raw_pgid->pool = oloc->pool;
2407 raw_pgid->seed = ceph_str_hash(pi->object_hash, oid->name,
2408 oid->name_len);
2409 dout("%s %s -> raw_pgid %llu.%x\n", __func__, oid->name,
2410 raw_pgid->pool, raw_pgid->seed);
2411 } else {
2412 char stack_buf[256];
2413 char *buf = stack_buf;
2414 int nsl = oloc->pool_ns->len;
2415 size_t total = nsl + 1 + oid->name_len;
2416
2417 if (total > sizeof(stack_buf))
2418 buf = kmalloc(total, GFP_NOIO | __GFP_NOFAIL);
2419 memcpy(buf, oloc->pool_ns->str, nsl);
2420 buf[nsl] = '\037';
2421 memcpy(buf + nsl + 1, oid->name, oid->name_len);
2422 raw_pgid->pool = oloc->pool;
2423 raw_pgid->seed = ceph_str_hash(pi->object_hash, buf, total);
2424 if (buf != stack_buf)
2425 kfree(buf);
2426 dout("%s %s ns %.*s -> raw_pgid %llu.%x\n", __func__,
2427 oid->name, nsl, oloc->pool_ns->str,
2428 raw_pgid->pool, raw_pgid->seed);
2429 }
2430 }
2431
2432 int ceph_object_locator_to_pg(struct ceph_osdmap *osdmap,
2433 const struct ceph_object_id *oid,
2434 const struct ceph_object_locator *oloc,
2435 struct ceph_pg *raw_pgid)
2436 {
2437 struct ceph_pg_pool_info *pi;
2438
2439 pi = ceph_pg_pool_by_id(osdmap, oloc->pool);
2440 if (!pi)
2441 return -ENOENT;
2442
2443 __ceph_object_locator_to_pg(pi, oid, oloc, raw_pgid);
2444 return 0;
2445 }
2446 EXPORT_SYMBOL(ceph_object_locator_to_pg);
2447
2448 /*
2449 * Map a raw PG (full precision ps) into an actual PG.
2450 */
2451 static void raw_pg_to_pg(struct ceph_pg_pool_info *pi,
2452 const struct ceph_pg *raw_pgid,
2453 struct ceph_pg *pgid)
2454 {
2455 pgid->pool = raw_pgid->pool;
2456 pgid->seed = ceph_stable_mod(raw_pgid->seed, pi->pg_num,
2457 pi->pg_num_mask);
2458 }
2459
2460 /*
2461 * Map a raw PG (full precision ps) into a placement ps (placement
2462 * seed). Include pool id in that value so that different pools don't
2463 * use the same seeds.
2464 */
2465 static u32 raw_pg_to_pps(struct ceph_pg_pool_info *pi,
2466 const struct ceph_pg *raw_pgid)
2467 {
2468 if (pi->flags & CEPH_POOL_FLAG_HASHPSPOOL) {
2469 /* hash pool id and seed so that pool PGs do not overlap */
2470 return crush_hash32_2(CRUSH_HASH_RJENKINS1,
2471 ceph_stable_mod(raw_pgid->seed,
2472 pi->pgp_num,
2473 pi->pgp_num_mask),
2474 raw_pgid->pool);
2475 } else {
2476 /*
2477 * legacy behavior: add ps and pool together. this is
2478 * not a great approach because the PGs from each pool
2479 * will overlap on top of each other: 0.5 == 1.4 ==
2480 * 2.3 == ...
2481 */
2482 return ceph_stable_mod(raw_pgid->seed, pi->pgp_num,
2483 pi->pgp_num_mask) +
2484 (unsigned)raw_pgid->pool;
2485 }
2486 }
2487
2488 /*
2489 * Magic value used for a "default" fallback choose_args, used if the
2490 * crush_choose_arg_map passed to do_crush() does not exist. If this
2491 * also doesn't exist, fall back to canonical weights.
2492 */
2493 #define CEPH_DEFAULT_CHOOSE_ARGS -1
2494
2495 static int do_crush(struct ceph_osdmap *map, int ruleno, int x,
2496 int *result, int result_max,
2497 const __u32 *weight, int weight_max,
2498 s64 choose_args_index)
2499 {
2500 struct crush_choose_arg_map *arg_map;
2501 struct crush_work *work;
2502 int r;
2503
2504 BUG_ON(result_max > CEPH_PG_MAX_SIZE);
2505
2506 arg_map = lookup_choose_arg_map(&map->crush->choose_args,
2507 choose_args_index);
2508 if (!arg_map)
2509 arg_map = lookup_choose_arg_map(&map->crush->choose_args,
2510 CEPH_DEFAULT_CHOOSE_ARGS);
2511
2512 work = get_workspace(&map->crush_wsm, map->crush);
2513 r = crush_do_rule(map->crush, ruleno, x, result, result_max,
2514 weight, weight_max, work,
2515 arg_map ? arg_map->args : NULL);
2516 put_workspace(&map->crush_wsm, work);
2517 return r;
2518 }
2519
2520 static void remove_nonexistent_osds(struct ceph_osdmap *osdmap,
2521 struct ceph_pg_pool_info *pi,
2522 struct ceph_osds *set)
2523 {
2524 int i;
2525
2526 if (ceph_can_shift_osds(pi)) {
2527 int removed = 0;
2528
2529 /* shift left */
2530 for (i = 0; i < set->size; i++) {
2531 if (!ceph_osd_exists(osdmap, set->osds[i])) {
2532 removed++;
2533 continue;
2534 }
2535 if (removed)
2536 set->osds[i - removed] = set->osds[i];
2537 }
2538 set->size -= removed;
2539 } else {
2540 /* set dne devices to NONE */
2541 for (i = 0; i < set->size; i++) {
2542 if (!ceph_osd_exists(osdmap, set->osds[i]))
2543 set->osds[i] = CRUSH_ITEM_NONE;
2544 }
2545 }
2546 }
2547
2548 /*
2549 * Calculate raw set (CRUSH output) for given PG and filter out
2550 * nonexistent OSDs. ->primary is undefined for a raw set.
2551 *
2552 * Placement seed (CRUSH input) is returned through @ppps.
2553 */
2554 static void pg_to_raw_osds(struct ceph_osdmap *osdmap,
2555 struct ceph_pg_pool_info *pi,
2556 const struct ceph_pg *raw_pgid,
2557 struct ceph_osds *raw,
2558 u32 *ppps)
2559 {
2560 u32 pps = raw_pg_to_pps(pi, raw_pgid);
2561 int ruleno;
2562 int len;
2563
2564 ceph_osds_init(raw);
2565 if (ppps)
2566 *ppps = pps;
2567
2568 ruleno = crush_find_rule(osdmap->crush, pi->crush_ruleset, pi->type,
2569 pi->size);
2570 if (ruleno < 0) {
2571 pr_err("no crush rule: pool %lld ruleset %d type %d size %d\n",
2572 pi->id, pi->crush_ruleset, pi->type, pi->size);
2573 return;
2574 }
2575
2576 if (pi->size > ARRAY_SIZE(raw->osds)) {
2577 pr_err_ratelimited("pool %lld ruleset %d type %d too wide: size %d > %zu\n",
2578 pi->id, pi->crush_ruleset, pi->type, pi->size,
2579 ARRAY_SIZE(raw->osds));
2580 return;
2581 }
2582
2583 len = do_crush(osdmap, ruleno, pps, raw->osds, pi->size,
2584 osdmap->osd_weight, osdmap->max_osd, pi->id);
2585 if (len < 0) {
2586 pr_err("error %d from crush rule %d: pool %lld ruleset %d type %d size %d\n",
2587 len, ruleno, pi->id, pi->crush_ruleset, pi->type,
2588 pi->size);
2589 return;
2590 }
2591
2592 raw->size = len;
2593 remove_nonexistent_osds(osdmap, pi, raw);
2594 }
2595
2596 /* apply pg_upmap[_items] mappings */
2597 static void apply_upmap(struct ceph_osdmap *osdmap,
2598 const struct ceph_pg *pgid,
2599 struct ceph_osds *raw)
2600 {
2601 struct ceph_pg_mapping *pg;
2602 int i, j;
2603
2604 pg = lookup_pg_mapping(&osdmap->pg_upmap, pgid);
2605 if (pg) {
2606 /* make sure targets aren't marked out */
2607 for (i = 0; i < pg->pg_upmap.len; i++) {
2608 int osd = pg->pg_upmap.osds[i];
2609
2610 if (osd != CRUSH_ITEM_NONE &&
2611 osd < osdmap->max_osd &&
2612 osdmap->osd_weight[osd] == 0) {
2613 /* reject/ignore explicit mapping */
2614 return;
2615 }
2616 }
2617 for (i = 0; i < pg->pg_upmap.len; i++)
2618 raw->osds[i] = pg->pg_upmap.osds[i];
2619 raw->size = pg->pg_upmap.len;
2620 /* check and apply pg_upmap_items, if any */
2621 }
2622
2623 pg = lookup_pg_mapping(&osdmap->pg_upmap_items, pgid);
2624 if (pg) {
2625 /*
2626 * Note: this approach does not allow a bidirectional swap,
2627 * e.g., [[1,2],[2,1]] applied to [0,1,2] -> [0,2,1].
2628 */
2629 for (i = 0; i < pg->pg_upmap_items.len; i++) {
2630 int from = pg->pg_upmap_items.from_to[i][0];
2631 int to = pg->pg_upmap_items.from_to[i][1];
2632 int pos = -1;
2633 bool exists = false;
2634
2635 /* make sure replacement doesn't already appear */
2636 for (j = 0; j < raw->size; j++) {
2637 int osd = raw->osds[j];
2638
2639 if (osd == to) {
2640 exists = true;
2641 break;
2642 }
2643 /* ignore mapping if target is marked out */
2644 if (osd == from && pos < 0 &&
2645 !(to != CRUSH_ITEM_NONE &&
2646 to < osdmap->max_osd &&
2647 osdmap->osd_weight[to] == 0)) {
2648 pos = j;
2649 }
2650 }
2651 if (!exists && pos >= 0)
2652 raw->osds[pos] = to;
2653 }
2654 }
2655 }
2656
2657 /*
2658 * Given raw set, calculate up set and up primary. By definition of an
2659 * up set, the result won't contain nonexistent or down OSDs.
2660 *
2661 * This is done in-place - on return @set is the up set. If it's
2662 * empty, ->primary will remain undefined.
2663 */
2664 static void raw_to_up_osds(struct ceph_osdmap *osdmap,
2665 struct ceph_pg_pool_info *pi,
2666 struct ceph_osds *set)
2667 {
2668 int i;
2669
2670 /* ->primary is undefined for a raw set */
2671 BUG_ON(set->primary != -1);
2672
2673 if (ceph_can_shift_osds(pi)) {
2674 int removed = 0;
2675
2676 /* shift left */
2677 for (i = 0; i < set->size; i++) {
2678 if (ceph_osd_is_down(osdmap, set->osds[i])) {
2679 removed++;
2680 continue;
2681 }
2682 if (removed)
2683 set->osds[i - removed] = set->osds[i];
2684 }
2685 set->size -= removed;
2686 if (set->size > 0)
2687 set->primary = set->osds[0];
2688 } else {
2689 /* set down/dne devices to NONE */
2690 for (i = set->size - 1; i >= 0; i--) {
2691 if (ceph_osd_is_down(osdmap, set->osds[i]))
2692 set->osds[i] = CRUSH_ITEM_NONE;
2693 else
2694 set->primary = set->osds[i];
2695 }
2696 }
2697 }
2698
2699 static void apply_primary_affinity(struct ceph_osdmap *osdmap,
2700 struct ceph_pg_pool_info *pi,
2701 u32 pps,
2702 struct ceph_osds *up)
2703 {
2704 int i;
2705 int pos = -1;
2706
2707 /*
2708 * Do we have any non-default primary_affinity values for these
2709 * osds?
2710 */
2711 if (!osdmap->osd_primary_affinity)
2712 return;
2713
2714 for (i = 0; i < up->size; i++) {
2715 int osd = up->osds[i];
2716
2717 if (osd != CRUSH_ITEM_NONE &&
2718 osdmap->osd_primary_affinity[osd] !=
2719 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY) {
2720 break;
2721 }
2722 }
2723 if (i == up->size)
2724 return;
2725
2726 /*
2727 * Pick the primary. Feed both the seed (for the pg) and the
2728 * osd into the hash/rng so that a proportional fraction of an
2729 * osd's pgs get rejected as primary.
2730 */
2731 for (i = 0; i < up->size; i++) {
2732 int osd = up->osds[i];
2733 u32 aff;
2734
2735 if (osd == CRUSH_ITEM_NONE)
2736 continue;
2737
2738 aff = osdmap->osd_primary_affinity[osd];
2739 if (aff < CEPH_OSD_MAX_PRIMARY_AFFINITY &&
2740 (crush_hash32_2(CRUSH_HASH_RJENKINS1,
2741 pps, osd) >> 16) >= aff) {
2742 /*
2743 * We chose not to use this primary. Note it
2744 * anyway as a fallback in case we don't pick
2745 * anyone else, but keep looking.
2746 */
2747 if (pos < 0)
2748 pos = i;
2749 } else {
2750 pos = i;
2751 break;
2752 }
2753 }
2754 if (pos < 0)
2755 return;
2756
2757 up->primary = up->osds[pos];
2758
2759 if (ceph_can_shift_osds(pi) && pos > 0) {
2760 /* move the new primary to the front */
2761 for (i = pos; i > 0; i--)
2762 up->osds[i] = up->osds[i - 1];
2763 up->osds[0] = up->primary;
2764 }
2765 }
2766
2767 /*
2768 * Get pg_temp and primary_temp mappings for given PG.
2769 *
2770 * Note that a PG may have none, only pg_temp, only primary_temp or
2771 * both pg_temp and primary_temp mappings. This means @temp isn't
2772 * always a valid OSD set on return: in the "only primary_temp" case,
2773 * @temp will have its ->primary >= 0 but ->size == 0.
2774 */
2775 static void get_temp_osds(struct ceph_osdmap *osdmap,
2776 struct ceph_pg_pool_info *pi,
2777 const struct ceph_pg *pgid,
2778 struct ceph_osds *temp)
2779 {
2780 struct ceph_pg_mapping *pg;
2781 int i;
2782
2783 ceph_osds_init(temp);
2784
2785 /* pg_temp? */
2786 pg = lookup_pg_mapping(&osdmap->pg_temp, pgid);
2787 if (pg) {
2788 for (i = 0; i < pg->pg_temp.len; i++) {
2789 if (ceph_osd_is_down(osdmap, pg->pg_temp.osds[i])) {
2790 if (ceph_can_shift_osds(pi))
2791 continue;
2792
2793 temp->osds[temp->size++] = CRUSH_ITEM_NONE;
2794 } else {
2795 temp->osds[temp->size++] = pg->pg_temp.osds[i];
2796 }
2797 }
2798
2799 /* apply pg_temp's primary */
2800 for (i = 0; i < temp->size; i++) {
2801 if (temp->osds[i] != CRUSH_ITEM_NONE) {
2802 temp->primary = temp->osds[i];
2803 break;
2804 }
2805 }
2806 }
2807
2808 /* primary_temp? */
2809 pg = lookup_pg_mapping(&osdmap->primary_temp, pgid);
2810 if (pg)
2811 temp->primary = pg->primary_temp.osd;
2812 }
2813
2814 /*
2815 * Map a PG to its acting set as well as its up set.
2816 *
2817 * Acting set is used for data mapping purposes, while up set can be
2818 * recorded for detecting interval changes and deciding whether to
2819 * resend a request.
2820 */
2821 void ceph_pg_to_up_acting_osds(struct ceph_osdmap *osdmap,
2822 struct ceph_pg_pool_info *pi,
2823 const struct ceph_pg *raw_pgid,
2824 struct ceph_osds *up,
2825 struct ceph_osds *acting)
2826 {
2827 struct ceph_pg pgid;
2828 u32 pps;
2829
2830 WARN_ON(pi->id != raw_pgid->pool);
2831 raw_pg_to_pg(pi, raw_pgid, &pgid);
2832
2833 pg_to_raw_osds(osdmap, pi, raw_pgid, up, &pps);
2834 apply_upmap(osdmap, &pgid, up);
2835 raw_to_up_osds(osdmap, pi, up);
2836 apply_primary_affinity(osdmap, pi, pps, up);
2837 get_temp_osds(osdmap, pi, &pgid, acting);
2838 if (!acting->size) {
2839 memcpy(acting->osds, up->osds, up->size * sizeof(up->osds[0]));
2840 acting->size = up->size;
2841 if (acting->primary == -1)
2842 acting->primary = up->primary;
2843 }
2844 WARN_ON(!osds_valid(up) || !osds_valid(acting));
2845 }
2846
2847 bool ceph_pg_to_primary_shard(struct ceph_osdmap *osdmap,
2848 struct ceph_pg_pool_info *pi,
2849 const struct ceph_pg *raw_pgid,
2850 struct ceph_spg *spgid)
2851 {
2852 struct ceph_pg pgid;
2853 struct ceph_osds up, acting;
2854 int i;
2855
2856 WARN_ON(pi->id != raw_pgid->pool);
2857 raw_pg_to_pg(pi, raw_pgid, &pgid);
2858
2859 if (ceph_can_shift_osds(pi)) {
2860 spgid->pgid = pgid; /* struct */
2861 spgid->shard = CEPH_SPG_NOSHARD;
2862 return true;
2863 }
2864
2865 ceph_pg_to_up_acting_osds(osdmap, pi, &pgid, &up, &acting);
2866 for (i = 0; i < acting.size; i++) {
2867 if (acting.osds[i] == acting.primary) {
2868 spgid->pgid = pgid; /* struct */
2869 spgid->shard = i;
2870 return true;
2871 }
2872 }
2873
2874 return false;
2875 }
2876
2877 /*
2878 * Return acting primary for given PG, or -1 if none.
2879 */
2880 int ceph_pg_to_acting_primary(struct ceph_osdmap *osdmap,
2881 const struct ceph_pg *raw_pgid)
2882 {
2883 struct ceph_pg_pool_info *pi;
2884 struct ceph_osds up, acting;
2885
2886 pi = ceph_pg_pool_by_id(osdmap, raw_pgid->pool);
2887 if (!pi)
2888 return -1;
2889
2890 ceph_pg_to_up_acting_osds(osdmap, pi, raw_pgid, &up, &acting);
2891 return acting.primary;
2892 }
2893 EXPORT_SYMBOL(ceph_pg_to_acting_primary);
2894
2895 static struct crush_loc_node *alloc_crush_loc(size_t type_name_len,
2896 size_t name_len)
2897 {
2898 struct crush_loc_node *loc;
2899
2900 loc = kmalloc(sizeof(*loc) + type_name_len + name_len + 2, GFP_NOIO);
2901 if (!loc)
2902 return NULL;
2903
2904 RB_CLEAR_NODE(&loc->cl_node);
2905 return loc;
2906 }
2907
2908 static void free_crush_loc(struct crush_loc_node *loc)
2909 {
2910 WARN_ON(!RB_EMPTY_NODE(&loc->cl_node));
2911
2912 kfree(loc);
2913 }
2914
2915 static int crush_loc_compare(const struct crush_loc *loc1,
2916 const struct crush_loc *loc2)
2917 {
2918 return strcmp(loc1->cl_type_name, loc2->cl_type_name) ?:
2919 strcmp(loc1->cl_name, loc2->cl_name);
2920 }
2921
2922 DEFINE_RB_FUNCS2(crush_loc, struct crush_loc_node, cl_loc, crush_loc_compare,
2923 RB_BYPTR, const struct crush_loc *, cl_node)
2924
2925 /*
2926 * Parses a set of <bucket type name>':'<bucket name> pairs separated
2927 * by '|', e.g. "rack:foo1|rack:foo2|datacenter:bar".
2928 *
2929 * Note that @crush_location is modified by strsep().
2930 */
2931 int ceph_parse_crush_location(char *crush_location, struct rb_root *locs)
2932 {
2933 struct crush_loc_node *loc;
2934 const char *type_name, *name, *colon;
2935 size_t type_name_len, name_len;
2936
2937 dout("%s '%s'\n", __func__, crush_location);
2938 while ((type_name = strsep(&crush_location, "|"))) {
2939 colon = strchr(type_name, ':');
2940 if (!colon)
2941 return -EINVAL;
2942
2943 type_name_len = colon - type_name;
2944 if (type_name_len == 0)
2945 return -EINVAL;
2946
2947 name = colon + 1;
2948 name_len = strlen(name);
2949 if (name_len == 0)
2950 return -EINVAL;
2951
2952 loc = alloc_crush_loc(type_name_len, name_len);
2953 if (!loc)
2954 return -ENOMEM;
2955
2956 loc->cl_loc.cl_type_name = loc->cl_data;
2957 memcpy(loc->cl_loc.cl_type_name, type_name, type_name_len);
2958 loc->cl_loc.cl_type_name[type_name_len] = '\0';
2959
2960 loc->cl_loc.cl_name = loc->cl_data + type_name_len + 1;
2961 memcpy(loc->cl_loc.cl_name, name, name_len);
2962 loc->cl_loc.cl_name[name_len] = '\0';
2963
2964 if (!__insert_crush_loc(locs, loc)) {
2965 free_crush_loc(loc);
2966 return -EEXIST;
2967 }
2968
2969 dout("%s type_name '%s' name '%s'\n", __func__,
2970 loc->cl_loc.cl_type_name, loc->cl_loc.cl_name);
2971 }
2972
2973 return 0;
2974 }
2975
2976 int ceph_compare_crush_locs(struct rb_root *locs1, struct rb_root *locs2)
2977 {
2978 struct rb_node *n1 = rb_first(locs1);
2979 struct rb_node *n2 = rb_first(locs2);
2980 int ret;
2981
2982 for ( ; n1 && n2; n1 = rb_next(n1), n2 = rb_next(n2)) {
2983 struct crush_loc_node *loc1 =
2984 rb_entry(n1, struct crush_loc_node, cl_node);
2985 struct crush_loc_node *loc2 =
2986 rb_entry(n2, struct crush_loc_node, cl_node);
2987
2988 ret = crush_loc_compare(&loc1->cl_loc, &loc2->cl_loc);
2989 if (ret)
2990 return ret;
2991 }
2992
2993 if (!n1 && n2)
2994 return -1;
2995 if (n1 && !n2)
2996 return 1;
2997 return 0;
2998 }
2999
3000 void ceph_clear_crush_locs(struct rb_root *locs)
3001 {
3002 while (!RB_EMPTY_ROOT(locs)) {
3003 struct crush_loc_node *loc =
3004 rb_entry(rb_first(locs), struct crush_loc_node, cl_node);
3005
3006 erase_crush_loc(locs, loc);
3007 free_crush_loc(loc);
3008 }
3009 }
3010
3011 /*
3012 * [a-zA-Z0-9-_.]+
3013 */
3014 static bool is_valid_crush_name(const char *name)
3015 {
3016 do {
3017 if (!('a' <= *name && *name <= 'z') &&
3018 !('A' <= *name && *name <= 'Z') &&
3019 !('0' <= *name && *name <= '9') &&
3020 *name != '-' && *name != '_' && *name != '.')
3021 return false;
3022 } while (*++name != '\0');
3023
3024 return true;
3025 }
3026
3027 /*
3028 * Gets the parent of an item. Returns its id (<0 because the
3029 * parent is always a bucket), type id (>0 for the same reason,
3030 * via @parent_type_id) and location (via @parent_loc). If no
3031 * parent, returns 0.
3032 *
3033 * Does a linear search, as there are no parent pointers of any
3034 * kind. Note that the result is ambiguous for items that occur
3035 * multiple times in the map.
3036 */
3037 static int get_immediate_parent(struct crush_map *c, int id,
3038 u16 *parent_type_id,
3039 struct crush_loc *parent_loc)
3040 {
3041 struct crush_bucket *b;
3042 struct crush_name_node *type_cn, *cn;
3043 int i, j;
3044
3045 for (i = 0; i < c->max_buckets; i++) {
3046 b = c->buckets[i];
3047 if (!b)
3048 continue;
3049
3050 /* ignore per-class shadow hierarchy */
3051 cn = lookup_crush_name(&c->names, b->id);
3052 if (!cn || !is_valid_crush_name(cn->cn_name))
3053 continue;
3054
3055 for (j = 0; j < b->size; j++) {
3056 if (b->items[j] != id)
3057 continue;
3058
3059 *parent_type_id = b->type;
3060 type_cn = lookup_crush_name(&c->type_names, b->type);
3061 parent_loc->cl_type_name = type_cn->cn_name;
3062 parent_loc->cl_name = cn->cn_name;
3063 return b->id;
3064 }
3065 }
3066
3067 return 0; /* no parent */
3068 }
3069
3070 /*
3071 * Calculates the locality/distance from an item to a client
3072 * location expressed in terms of CRUSH hierarchy as a set of
3073 * (bucket type name, bucket name) pairs. Specifically, looks
3074 * for the lowest-valued bucket type for which the location of
3075 * @id matches one of the locations in @locs, so for standard
3076 * bucket types (host = 1, rack = 3, datacenter = 8, zone = 9)
3077 * a matching host is closer than a matching rack and a matching
3078 * data center is closer than a matching zone.
3079 *
3080 * Specifying multiple locations (a "multipath" location) such
3081 * as "rack=foo1 rack=foo2 datacenter=bar" is allowed -- @locs
3082 * is a multimap. The locality will be:
3083 *
3084 * - 3 for OSDs in racks foo1 and foo2
3085 * - 8 for OSDs in data center bar
3086 * - -1 for all other OSDs
3087 *
3088 * The lowest possible bucket type is 1, so the best locality
3089 * for an OSD is 1 (i.e. a matching host). Locality 0 would be
3090 * the OSD itself.
3091 */
3092 int ceph_get_crush_locality(struct ceph_osdmap *osdmap, int id,
3093 struct rb_root *locs)
3094 {
3095 struct crush_loc loc;
3096 u16 type_id;
3097
3098 /*
3099 * Instead of repeated get_immediate_parent() calls,
3100 * the location of @id could be obtained with a single
3101 * depth-first traversal.
3102 */
3103 for (;;) {
3104 id = get_immediate_parent(osdmap->crush, id, &type_id, &loc);
3105 if (id >= 0)
3106 return -1; /* not local */
3107
3108 if (lookup_crush_loc(locs, &loc))
3109 return type_id;
3110 }
3111 }