1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/ceph/ceph_debug.h>
5 #include <linux/module.h>
6 #include <linux/slab.h>
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>
14 char *ceph_osdmap_state_str(char *str
, int len
, u32 state
)
19 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");
26 snprintf(str
, len
, "doesn't exist");
33 static int calc_bits_of(unsigned int t
)
44 * the foo_mask is the smallest value 2^n-1 that is >= foo.
46 static void calc_pg_masks(struct ceph_pg_pool_info
*pi
)
48 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;
55 static int crush_decode_uniform_bucket(void **p
, void *end
,
56 struct crush_bucket_uniform
*b
)
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
);
60 b
->item_weight
= ceph_decode_32(p
);
66 static int crush_decode_list_bucket(void **p
, void *end
,
67 struct crush_bucket_list
*b
)
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
)
74 b
->sum_weights
= kcalloc(b
->h
.size
, sizeof(u32
), GFP_NOFS
);
75 if (b
->sum_weights
== NULL
)
77 ceph_decode_need(p
, end
, 2 * b
->h
.size
* sizeof(u32
), bad
);
78 for (j
= 0; j
< b
->h
.size
; j
++) {
79 b
->item_weights
[j
] = ceph_decode_32(p
);
80 b
->sum_weights
[j
] = ceph_decode_32(p
);
87 static int crush_decode_tree_bucket(void **p
, void *end
,
88 struct crush_bucket_tree
*b
)
91 dout("crush_decode_tree_bucket %p to %p\n", *p
, end
);
92 ceph_decode_8_safe(p
, end
, b
->num_nodes
, bad
);
93 b
->node_weights
= kcalloc(b
->num_nodes
, sizeof(u32
), GFP_NOFS
);
94 if (b
->node_weights
== NULL
)
96 ceph_decode_need(p
, end
, b
->num_nodes
* sizeof(u32
), bad
);
97 for (j
= 0; j
< b
->num_nodes
; j
++)
98 b
->node_weights
[j
] = ceph_decode_32(p
);
104 static int crush_decode_straw_bucket(void **p
, void *end
,
105 struct crush_bucket_straw
*b
)
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
)
112 b
->straws
= kcalloc(b
->h
.size
, sizeof(u32
), GFP_NOFS
);
113 if (b
->straws
== NULL
)
115 ceph_decode_need(p
, end
, 2 * b
->h
.size
* sizeof(u32
), bad
);
116 for (j
= 0; j
< b
->h
.size
; j
++) {
117 b
->item_weights
[j
] = ceph_decode_32(p
);
118 b
->straws
[j
] = ceph_decode_32(p
);
125 static int crush_decode_straw2_bucket(void **p
, void *end
,
126 struct crush_bucket_straw2
*b
)
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
)
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
);
141 struct crush_name_node
{
142 struct rb_node cn_node
;
147 static struct crush_name_node
*alloc_crush_name(size_t name_len
)
149 struct crush_name_node
*cn
;
151 cn
= kmalloc(sizeof(*cn
) + name_len
+ 1, GFP_NOIO
);
155 RB_CLEAR_NODE(&cn
->cn_node
);
159 static void free_crush_name(struct crush_name_node
*cn
)
161 WARN_ON(!RB_EMPTY_NODE(&cn
->cn_node
));
166 DEFINE_RB_FUNCS(crush_name
, struct crush_name_node
, cn_id
, cn_node
)
168 static int decode_crush_names(void **p
, void *end
, struct rb_root
*root
)
172 ceph_decode_32_safe(p
, end
, n
, e_inval
);
174 struct crush_name_node
*cn
;
178 ceph_decode_32_safe(p
, end
, id
, e_inval
);
179 ceph_decode_32_safe(p
, end
, name_len
, e_inval
);
180 ceph_decode_need(p
, end
, name_len
, e_inval
);
182 cn
= alloc_crush_name(name_len
);
187 memcpy(cn
->cn_name
, *p
, name_len
);
188 cn
->cn_name
[name_len
] = '\0';
191 if (!__insert_crush_name(root
, cn
)) {
203 void clear_crush_names(struct rb_root
*root
)
205 while (!RB_EMPTY_ROOT(root
)) {
206 struct crush_name_node
*cn
=
207 rb_entry(rb_first(root
), struct crush_name_node
, cn_node
);
209 erase_crush_name(root
, cn
);
214 static struct crush_choose_arg_map
*alloc_choose_arg_map(void)
216 struct crush_choose_arg_map
*arg_map
;
218 arg_map
= kzalloc(sizeof(*arg_map
), GFP_NOIO
);
222 RB_CLEAR_NODE(&arg_map
->node
);
226 static void free_choose_arg_map(struct crush_choose_arg_map
*arg_map
)
231 WARN_ON(!RB_EMPTY_NODE(&arg_map
->node
));
233 for (i
= 0; i
< arg_map
->size
; i
++) {
234 struct crush_choose_arg
*arg
= &arg_map
->args
[i
];
236 for (j
= 0; j
< arg
->weight_set_size
; j
++)
237 kfree(arg
->weight_set
[j
].weights
);
238 kfree(arg
->weight_set
);
241 kfree(arg_map
->args
);
246 DEFINE_RB_FUNCS(choose_arg_map
, struct crush_choose_arg_map
, choose_args_index
,
249 void clear_choose_args(struct crush_map
*c
)
251 while (!RB_EMPTY_ROOT(&c
->choose_args
)) {
252 struct crush_choose_arg_map
*arg_map
=
253 rb_entry(rb_first(&c
->choose_args
),
254 struct crush_choose_arg_map
, node
);
256 erase_choose_arg_map(&c
->choose_args
, arg_map
);
257 free_choose_arg_map(arg_map
);
261 static u32
*decode_array_32_alloc(void **p
, void *end
, u32
*plen
)
267 ceph_decode_32_safe(p
, end
, len
, e_inval
);
271 a
= kmalloc_array(len
, sizeof(u32
), GFP_NOIO
);
277 ceph_decode_need(p
, end
, len
* sizeof(u32
), e_inval
);
278 for (i
= 0; i
< len
; i
++)
279 a
[i
] = ceph_decode_32(p
);
293 * Assumes @arg is zero-initialized.
295 static int decode_choose_arg(void **p
, void *end
, struct crush_choose_arg
*arg
)
299 ceph_decode_32_safe(p
, end
, arg
->weight_set_size
, e_inval
);
300 if (arg
->weight_set_size
) {
303 arg
->weight_set
= kmalloc_array(arg
->weight_set_size
,
304 sizeof(*arg
->weight_set
),
306 if (!arg
->weight_set
)
309 for (i
= 0; i
< arg
->weight_set_size
; i
++) {
310 struct crush_weight_set
*w
= &arg
->weight_set
[i
];
312 w
->weights
= decode_array_32_alloc(p
, end
, &w
->size
);
313 if (IS_ERR(w
->weights
)) {
314 ret
= PTR_ERR(w
->weights
);
321 arg
->ids
= decode_array_32_alloc(p
, end
, &arg
->ids_size
);
322 if (IS_ERR(arg
->ids
)) {
323 ret
= PTR_ERR(arg
->ids
);
334 static int decode_choose_args(void **p
, void *end
, struct crush_map
*c
)
336 struct crush_choose_arg_map
*arg_map
= NULL
;
337 u32 num_choose_arg_maps
, num_buckets
;
340 ceph_decode_32_safe(p
, end
, num_choose_arg_maps
, e_inval
);
341 while (num_choose_arg_maps
--) {
342 arg_map
= alloc_choose_arg_map();
348 ceph_decode_64_safe(p
, end
, arg_map
->choose_args_index
,
350 arg_map
->size
= c
->max_buckets
;
351 arg_map
->args
= kcalloc(arg_map
->size
, sizeof(*arg_map
->args
),
353 if (!arg_map
->args
) {
358 ceph_decode_32_safe(p
, end
, num_buckets
, e_inval
);
359 while (num_buckets
--) {
360 struct crush_choose_arg
*arg
;
363 ceph_decode_32_safe(p
, end
, bucket_index
, e_inval
);
364 if (bucket_index
>= arg_map
->size
)
367 arg
= &arg_map
->args
[bucket_index
];
368 ret
= decode_choose_arg(p
, end
, arg
);
373 arg
->ids_size
!= c
->buckets
[bucket_index
]->size
)
377 insert_choose_arg_map(&c
->choose_args
, arg_map
);
385 free_choose_arg_map(arg_map
);
389 static void crush_finalize(struct crush_map
*c
)
393 /* Space for the array of pointers to per-bucket workspace */
394 c
->working_size
= sizeof(struct crush_work
) +
395 c
->max_buckets
* sizeof(struct crush_work_bucket
*);
397 for (b
= 0; b
< c
->max_buckets
; b
++) {
401 switch (c
->buckets
[b
]->alg
) {
404 * The base case, permutation variables and
405 * the pointer to the permutation array.
407 c
->working_size
+= sizeof(struct crush_work_bucket
);
410 /* Every bucket has a permutation array. */
411 c
->working_size
+= c
->buckets
[b
]->size
* sizeof(__u32
);
415 static struct crush_map
*crush_decode(void *pbyval
, void *end
)
421 void *start
= pbyval
;
424 dout("crush_decode %p to %p len %d\n", *p
, end
, (int)(end
- *p
));
426 c
= kzalloc(sizeof(*c
), GFP_NOFS
);
428 return ERR_PTR(-ENOMEM
);
430 c
->type_names
= RB_ROOT
;
432 c
->choose_args
= RB_ROOT
;
434 /* set tunables to default values */
435 c
->choose_local_tries
= 2;
436 c
->choose_local_fallback_tries
= 5;
437 c
->choose_total_tries
= 19;
438 c
->chooseleaf_descend_once
= 0;
440 ceph_decode_need(p
, end
, 4*sizeof(u32
), bad
);
441 magic
= ceph_decode_32(p
);
442 if (magic
!= CRUSH_MAGIC
) {
443 pr_err("crush_decode magic %x != current %x\n",
444 (unsigned int)magic
, (unsigned int)CRUSH_MAGIC
);
447 c
->max_buckets
= ceph_decode_32(p
);
448 c
->max_rules
= ceph_decode_32(p
);
449 c
->max_devices
= ceph_decode_32(p
);
451 c
->buckets
= kcalloc(c
->max_buckets
, sizeof(*c
->buckets
), GFP_NOFS
);
452 if (c
->buckets
== NULL
)
454 c
->rules
= kcalloc(c
->max_rules
, sizeof(*c
->rules
), GFP_NOFS
);
455 if (c
->rules
== NULL
)
459 for (i
= 0; i
< c
->max_buckets
; i
++) {
462 struct crush_bucket
*b
;
464 ceph_decode_32_safe(p
, end
, alg
, bad
);
466 c
->buckets
[i
] = NULL
;
469 dout("crush_decode bucket %d off %x %p to %p\n",
470 i
, (int)(*p
-start
), *p
, end
);
473 case CRUSH_BUCKET_UNIFORM
:
474 size
= sizeof(struct crush_bucket_uniform
);
476 case CRUSH_BUCKET_LIST
:
477 size
= sizeof(struct crush_bucket_list
);
479 case CRUSH_BUCKET_TREE
:
480 size
= sizeof(struct crush_bucket_tree
);
482 case CRUSH_BUCKET_STRAW
:
483 size
= sizeof(struct crush_bucket_straw
);
485 case CRUSH_BUCKET_STRAW2
:
486 size
= sizeof(struct crush_bucket_straw2
);
492 b
= c
->buckets
[i
] = kzalloc(size
, GFP_NOFS
);
496 ceph_decode_need(p
, end
, 4*sizeof(u32
), bad
);
497 b
->id
= ceph_decode_32(p
);
498 b
->type
= ceph_decode_16(p
);
499 b
->alg
= ceph_decode_8(p
);
500 b
->hash
= ceph_decode_8(p
);
501 b
->weight
= ceph_decode_32(p
);
502 b
->size
= ceph_decode_32(p
);
504 dout("crush_decode bucket size %d off %x %p to %p\n",
505 b
->size
, (int)(*p
-start
), *p
, end
);
507 b
->items
= kcalloc(b
->size
, sizeof(__s32
), GFP_NOFS
);
508 if (b
->items
== NULL
)
511 ceph_decode_need(p
, end
, b
->size
*sizeof(u32
), bad
);
512 for (j
= 0; j
< b
->size
; j
++)
513 b
->items
[j
] = ceph_decode_32(p
);
516 case CRUSH_BUCKET_UNIFORM
:
517 err
= crush_decode_uniform_bucket(p
, end
,
518 (struct crush_bucket_uniform
*)b
);
522 case CRUSH_BUCKET_LIST
:
523 err
= crush_decode_list_bucket(p
, end
,
524 (struct crush_bucket_list
*)b
);
528 case CRUSH_BUCKET_TREE
:
529 err
= crush_decode_tree_bucket(p
, end
,
530 (struct crush_bucket_tree
*)b
);
534 case CRUSH_BUCKET_STRAW
:
535 err
= crush_decode_straw_bucket(p
, end
,
536 (struct crush_bucket_straw
*)b
);
540 case CRUSH_BUCKET_STRAW2
:
541 err
= crush_decode_straw2_bucket(p
, end
,
542 (struct crush_bucket_straw2
*)b
);
550 dout("rule vec is %p\n", c
->rules
);
551 for (i
= 0; i
< c
->max_rules
; i
++) {
553 struct crush_rule
*r
;
555 ceph_decode_32_safe(p
, end
, yes
, bad
);
557 dout("crush_decode NO rule %d off %x %p to %p\n",
558 i
, (int)(*p
-start
), *p
, end
);
563 dout("crush_decode rule %d off %x %p to %p\n",
564 i
, (int)(*p
-start
), *p
, end
);
567 ceph_decode_32_safe(p
, end
, yes
, bad
);
568 #if BITS_PER_LONG == 32
569 if (yes
> (ULONG_MAX
- sizeof(*r
))
570 / sizeof(struct crush_rule_step
))
573 r
= kmalloc(struct_size(r
, steps
, yes
), GFP_NOFS
);
577 dout(" rule %d is at %p\n", i
, r
);
579 ceph_decode_copy_safe(p
, end
, &r
->mask
, 4, bad
); /* 4 u8's */
580 ceph_decode_need(p
, end
, r
->len
*3*sizeof(u32
), bad
);
581 for (j
= 0; j
< r
->len
; j
++) {
582 r
->steps
[j
].op
= ceph_decode_32(p
);
583 r
->steps
[j
].arg1
= ceph_decode_32(p
);
584 r
->steps
[j
].arg2
= ceph_decode_32(p
);
588 err
= decode_crush_names(p
, end
, &c
->type_names
);
592 err
= decode_crush_names(p
, end
, &c
->names
);
596 ceph_decode_skip_map(p
, end
, 32, string
, bad
); /* rule_name_map */
599 ceph_decode_need(p
, end
, 3*sizeof(u32
), done
);
600 c
->choose_local_tries
= ceph_decode_32(p
);
601 c
->choose_local_fallback_tries
= ceph_decode_32(p
);
602 c
->choose_total_tries
= ceph_decode_32(p
);
603 dout("crush decode tunable choose_local_tries = %d\n",
604 c
->choose_local_tries
);
605 dout("crush decode tunable choose_local_fallback_tries = %d\n",
606 c
->choose_local_fallback_tries
);
607 dout("crush decode tunable choose_total_tries = %d\n",
608 c
->choose_total_tries
);
610 ceph_decode_need(p
, end
, sizeof(u32
), done
);
611 c
->chooseleaf_descend_once
= ceph_decode_32(p
);
612 dout("crush decode tunable chooseleaf_descend_once = %d\n",
613 c
->chooseleaf_descend_once
);
615 ceph_decode_need(p
, end
, sizeof(u8
), done
);
616 c
->chooseleaf_vary_r
= ceph_decode_8(p
);
617 dout("crush decode tunable chooseleaf_vary_r = %d\n",
618 c
->chooseleaf_vary_r
);
620 /* skip straw_calc_version, allowed_bucket_algs */
621 ceph_decode_need(p
, end
, sizeof(u8
) + sizeof(u32
), done
);
622 *p
+= sizeof(u8
) + sizeof(u32
);
624 ceph_decode_need(p
, end
, sizeof(u8
), done
);
625 c
->chooseleaf_stable
= ceph_decode_8(p
);
626 dout("crush decode tunable chooseleaf_stable = %d\n",
627 c
->chooseleaf_stable
);
631 ceph_decode_skip_map(p
, end
, 32, 32, bad
);
633 ceph_decode_skip_map(p
, end
, 32, string
, bad
);
635 ceph_decode_skip_map_of_map(p
, end
, 32, 32, 32, bad
);
639 err
= decode_choose_args(p
, end
, c
);
646 dout("crush_decode success\n");
652 dout("crush_decode fail %d\n", err
);
661 int ceph_pg_compare(const struct ceph_pg
*lhs
, const struct ceph_pg
*rhs
)
663 if (lhs
->pool
< rhs
->pool
)
665 if (lhs
->pool
> rhs
->pool
)
667 if (lhs
->seed
< rhs
->seed
)
669 if (lhs
->seed
> rhs
->seed
)
675 int ceph_spg_compare(const struct ceph_spg
*lhs
, const struct ceph_spg
*rhs
)
679 ret
= ceph_pg_compare(&lhs
->pgid
, &rhs
->pgid
);
683 if (lhs
->shard
< rhs
->shard
)
685 if (lhs
->shard
> rhs
->shard
)
691 static struct ceph_pg_mapping
*alloc_pg_mapping(size_t payload_len
)
693 struct ceph_pg_mapping
*pg
;
695 pg
= kmalloc(sizeof(*pg
) + payload_len
, GFP_NOIO
);
699 RB_CLEAR_NODE(&pg
->node
);
703 static void free_pg_mapping(struct ceph_pg_mapping
*pg
)
705 WARN_ON(!RB_EMPTY_NODE(&pg
->node
));
711 * rbtree of pg_mapping for handling pg_temp (explicit mapping of pgid
712 * to a set of osds) and primary_temp (explicit primary setting)
714 DEFINE_RB_FUNCS2(pg_mapping
, struct ceph_pg_mapping
, pgid
, ceph_pg_compare
,
715 RB_BYPTR
, const struct ceph_pg
*, node
)
718 * rbtree of pg pool info
720 DEFINE_RB_FUNCS(pg_pool
, struct ceph_pg_pool_info
, id
, node
)
722 struct ceph_pg_pool_info
*ceph_pg_pool_by_id(struct ceph_osdmap
*map
, u64 id
)
724 return lookup_pg_pool(&map
->pg_pools
, id
);
727 const char *ceph_pg_pool_name_by_id(struct ceph_osdmap
*map
, u64 id
)
729 struct ceph_pg_pool_info
*pi
;
731 if (id
== CEPH_NOPOOL
)
734 if (WARN_ON_ONCE(id
> (u64
) INT_MAX
))
737 pi
= lookup_pg_pool(&map
->pg_pools
, id
);
738 return pi
? pi
->name
: NULL
;
740 EXPORT_SYMBOL(ceph_pg_pool_name_by_id
);
742 int ceph_pg_poolid_by_name(struct ceph_osdmap
*map
, const char *name
)
746 for (rbp
= rb_first(&map
->pg_pools
); rbp
; rbp
= rb_next(rbp
)) {
747 struct ceph_pg_pool_info
*pi
=
748 rb_entry(rbp
, struct ceph_pg_pool_info
, node
);
749 if (pi
->name
&& strcmp(pi
->name
, name
) == 0)
754 EXPORT_SYMBOL(ceph_pg_poolid_by_name
);
756 u64
ceph_pg_pool_flags(struct ceph_osdmap
*map
, u64 id
)
758 struct ceph_pg_pool_info
*pi
;
760 pi
= lookup_pg_pool(&map
->pg_pools
, id
);
761 return pi
? pi
->flags
: 0;
763 EXPORT_SYMBOL(ceph_pg_pool_flags
);
765 static void __remove_pg_pool(struct rb_root
*root
, struct ceph_pg_pool_info
*pi
)
767 erase_pg_pool(root
, pi
);
772 static int decode_pool(void **p
, void *end
, struct ceph_pg_pool_info
*pi
)
778 ceph_decode_need(p
, end
, 2 + 4, bad
);
779 ev
= ceph_decode_8(p
); /* encoding version */
780 cv
= ceph_decode_8(p
); /* compat version */
782 pr_warn("got v %d < 5 cv %d of ceph_pg_pool\n", ev
, cv
);
786 pr_warn("got v %d cv %d > 9 of ceph_pg_pool\n", ev
, cv
);
789 len
= ceph_decode_32(p
);
790 ceph_decode_need(p
, end
, len
, bad
);
793 pi
->type
= ceph_decode_8(p
);
794 pi
->size
= ceph_decode_8(p
);
795 pi
->crush_ruleset
= ceph_decode_8(p
);
796 pi
->object_hash
= ceph_decode_8(p
);
798 pi
->pg_num
= ceph_decode_32(p
);
799 pi
->pgp_num
= ceph_decode_32(p
);
801 *p
+= 4 + 4; /* skip lpg* */
802 *p
+= 4; /* skip last_change */
803 *p
+= 8 + 4; /* skip snap_seq, snap_epoch */
806 num
= ceph_decode_32(p
);
808 *p
+= 8; /* snapid key */
809 *p
+= 1 + 1; /* versions */
810 len
= ceph_decode_32(p
);
814 /* skip removed_snaps */
815 num
= ceph_decode_32(p
);
818 *p
+= 8; /* skip auid */
819 pi
->flags
= ceph_decode_64(p
);
820 *p
+= 4; /* skip crash_replay_interval */
823 pi
->min_size
= ceph_decode_8(p
);
825 pi
->min_size
= pi
->size
- pi
->size
/ 2;
828 *p
+= 8 + 8; /* skip quota_max_* */
832 num
= ceph_decode_32(p
);
835 *p
+= 8; /* skip tier_of */
836 *p
+= 1; /* skip cache_mode */
838 pi
->read_tier
= ceph_decode_64(p
);
839 pi
->write_tier
= ceph_decode_64(p
);
846 /* skip properties */
847 num
= ceph_decode_32(p
);
849 len
= ceph_decode_32(p
);
851 len
= ceph_decode_32(p
);
857 /* skip hit_set_params */
858 *p
+= 1 + 1; /* versions */
859 len
= ceph_decode_32(p
);
862 *p
+= 4; /* skip hit_set_period */
863 *p
+= 4; /* skip hit_set_count */
867 *p
+= 4; /* skip stripe_width */
870 *p
+= 8; /* skip target_max_bytes */
871 *p
+= 8; /* skip target_max_objects */
872 *p
+= 4; /* skip cache_target_dirty_ratio_micro */
873 *p
+= 4; /* skip cache_target_full_ratio_micro */
874 *p
+= 4; /* skip cache_min_flush_age */
875 *p
+= 4; /* skip cache_min_evict_age */
879 /* skip erasure_code_profile */
880 len
= ceph_decode_32(p
);
885 * last_force_op_resend_preluminous, will be overridden if the
886 * map was encoded with RESEND_ON_SPLIT
889 pi
->last_force_request_resend
= ceph_decode_32(p
);
891 pi
->last_force_request_resend
= 0;
894 *p
+= 4; /* skip min_read_recency_for_promote */
897 *p
+= 8; /* skip expected_num_objects */
900 *p
+= 4; /* skip cache_target_dirty_high_ratio_micro */
903 *p
+= 4; /* skip min_write_recency_for_promote */
906 *p
+= 1; /* skip use_gmt_hitset */
909 *p
+= 1; /* skip fast_read */
912 *p
+= 4; /* skip hit_set_grade_decay_rate */
913 *p
+= 4; /* skip hit_set_search_last_n */
918 *p
+= 1 + 1; /* versions */
919 len
= ceph_decode_32(p
);
924 pi
->last_force_request_resend
= ceph_decode_32(p
);
926 /* ignore the rest */
936 static int decode_pool_names(void **p
, void *end
, struct ceph_osdmap
*map
)
938 struct ceph_pg_pool_info
*pi
;
942 ceph_decode_32_safe(p
, end
, num
, bad
);
943 dout(" %d pool names\n", num
);
945 ceph_decode_64_safe(p
, end
, pool
, bad
);
946 ceph_decode_32_safe(p
, end
, len
, bad
);
947 dout(" pool %llu len %d\n", pool
, len
);
948 ceph_decode_need(p
, end
, len
, bad
);
949 pi
= lookup_pg_pool(&map
->pg_pools
, pool
);
951 char *name
= kstrndup(*p
, len
, GFP_NOFS
);
957 dout(" name is %s\n", pi
->name
);
970 * workspace_manager framework borrowed from fs/btrfs/compression.c.
971 * Two simplifications: there is only one type of workspace and there
972 * is always at least one workspace.
974 static struct crush_work
*alloc_workspace(const struct crush_map
*c
)
976 struct crush_work
*work
;
979 WARN_ON(!c
->working_size
);
980 work_size
= crush_work_size(c
, CEPH_PG_MAX_SIZE
);
981 dout("%s work_size %zu bytes\n", __func__
, work_size
);
983 work
= ceph_kvmalloc(work_size
, GFP_NOIO
);
987 INIT_LIST_HEAD(&work
->item
);
988 crush_init_workspace(c
, work
);
992 static void free_workspace(struct crush_work
*work
)
994 WARN_ON(!list_empty(&work
->item
));
998 static void init_workspace_manager(struct workspace_manager
*wsm
)
1000 INIT_LIST_HEAD(&wsm
->idle_ws
);
1001 spin_lock_init(&wsm
->ws_lock
);
1002 atomic_set(&wsm
->total_ws
, 0);
1004 init_waitqueue_head(&wsm
->ws_wait
);
1007 static void add_initial_workspace(struct workspace_manager
*wsm
,
1008 struct crush_work
*work
)
1010 WARN_ON(!list_empty(&wsm
->idle_ws
));
1012 list_add(&work
->item
, &wsm
->idle_ws
);
1013 atomic_set(&wsm
->total_ws
, 1);
1017 static void cleanup_workspace_manager(struct workspace_manager
*wsm
)
1019 struct crush_work
*work
;
1021 while (!list_empty(&wsm
->idle_ws
)) {
1022 work
= list_first_entry(&wsm
->idle_ws
, struct crush_work
,
1024 list_del_init(&work
->item
);
1025 free_workspace(work
);
1027 atomic_set(&wsm
->total_ws
, 0);
1032 * Finds an available workspace or allocates a new one. If it's not
1033 * possible to allocate a new one, waits until there is one.
1035 static struct crush_work
*get_workspace(struct workspace_manager
*wsm
,
1036 const struct crush_map
*c
)
1038 struct crush_work
*work
;
1039 int cpus
= num_online_cpus();
1042 spin_lock(&wsm
->ws_lock
);
1043 if (!list_empty(&wsm
->idle_ws
)) {
1044 work
= list_first_entry(&wsm
->idle_ws
, struct crush_work
,
1046 list_del_init(&work
->item
);
1048 spin_unlock(&wsm
->ws_lock
);
1052 if (atomic_read(&wsm
->total_ws
) > cpus
) {
1055 spin_unlock(&wsm
->ws_lock
);
1056 prepare_to_wait(&wsm
->ws_wait
, &wait
, TASK_UNINTERRUPTIBLE
);
1057 if (atomic_read(&wsm
->total_ws
) > cpus
&& !wsm
->free_ws
)
1059 finish_wait(&wsm
->ws_wait
, &wait
);
1062 atomic_inc(&wsm
->total_ws
);
1063 spin_unlock(&wsm
->ws_lock
);
1065 work
= alloc_workspace(c
);
1067 atomic_dec(&wsm
->total_ws
);
1068 wake_up(&wsm
->ws_wait
);
1071 * Do not return the error but go back to waiting. We
1072 * have the inital workspace and the CRUSH computation
1073 * time is bounded so we will get it eventually.
1075 WARN_ON(atomic_read(&wsm
->total_ws
) < 1);
1082 * Puts a workspace back on the list or frees it if we have enough
1083 * idle ones sitting around.
1085 static void put_workspace(struct workspace_manager
*wsm
,
1086 struct crush_work
*work
)
1088 spin_lock(&wsm
->ws_lock
);
1089 if (wsm
->free_ws
<= num_online_cpus()) {
1090 list_add(&work
->item
, &wsm
->idle_ws
);
1092 spin_unlock(&wsm
->ws_lock
);
1095 spin_unlock(&wsm
->ws_lock
);
1097 free_workspace(work
);
1098 atomic_dec(&wsm
->total_ws
);
1100 if (wq_has_sleeper(&wsm
->ws_wait
))
1101 wake_up(&wsm
->ws_wait
);
1107 struct ceph_osdmap
*ceph_osdmap_alloc(void)
1109 struct ceph_osdmap
*map
;
1111 map
= kzalloc(sizeof(*map
), GFP_NOIO
);
1115 map
->pg_pools
= RB_ROOT
;
1117 map
->pg_temp
= RB_ROOT
;
1118 map
->primary_temp
= RB_ROOT
;
1119 map
->pg_upmap
= RB_ROOT
;
1120 map
->pg_upmap_items
= RB_ROOT
;
1122 init_workspace_manager(&map
->crush_wsm
);
1127 void ceph_osdmap_destroy(struct ceph_osdmap
*map
)
1129 dout("osdmap_destroy %p\n", map
);
1132 crush_destroy(map
->crush
);
1133 cleanup_workspace_manager(&map
->crush_wsm
);
1135 while (!RB_EMPTY_ROOT(&map
->pg_temp
)) {
1136 struct ceph_pg_mapping
*pg
=
1137 rb_entry(rb_first(&map
->pg_temp
),
1138 struct ceph_pg_mapping
, node
);
1139 erase_pg_mapping(&map
->pg_temp
, pg
);
1140 free_pg_mapping(pg
);
1142 while (!RB_EMPTY_ROOT(&map
->primary_temp
)) {
1143 struct ceph_pg_mapping
*pg
=
1144 rb_entry(rb_first(&map
->primary_temp
),
1145 struct ceph_pg_mapping
, node
);
1146 erase_pg_mapping(&map
->primary_temp
, pg
);
1147 free_pg_mapping(pg
);
1149 while (!RB_EMPTY_ROOT(&map
->pg_upmap
)) {
1150 struct ceph_pg_mapping
*pg
=
1151 rb_entry(rb_first(&map
->pg_upmap
),
1152 struct ceph_pg_mapping
, node
);
1153 rb_erase(&pg
->node
, &map
->pg_upmap
);
1156 while (!RB_EMPTY_ROOT(&map
->pg_upmap_items
)) {
1157 struct ceph_pg_mapping
*pg
=
1158 rb_entry(rb_first(&map
->pg_upmap_items
),
1159 struct ceph_pg_mapping
, node
);
1160 rb_erase(&pg
->node
, &map
->pg_upmap_items
);
1163 while (!RB_EMPTY_ROOT(&map
->pg_pools
)) {
1164 struct ceph_pg_pool_info
*pi
=
1165 rb_entry(rb_first(&map
->pg_pools
),
1166 struct ceph_pg_pool_info
, node
);
1167 __remove_pg_pool(&map
->pg_pools
, pi
);
1169 kvfree(map
->osd_state
);
1170 kvfree(map
->osd_weight
);
1171 kvfree(map
->osd_addr
);
1172 kvfree(map
->osd_primary_affinity
);
1177 * Adjust max_osd value, (re)allocate arrays.
1179 * The new elements are properly initialized.
1181 static int osdmap_set_max_osd(struct ceph_osdmap
*map
, u32 max
)
1185 struct ceph_entity_addr
*addr
;
1189 dout("%s old %u new %u\n", __func__
, map
->max_osd
, max
);
1190 if (max
== map
->max_osd
)
1193 state
= ceph_kvmalloc(array_size(max
, sizeof(*state
)), GFP_NOFS
);
1194 weight
= ceph_kvmalloc(array_size(max
, sizeof(*weight
)), GFP_NOFS
);
1195 addr
= ceph_kvmalloc(array_size(max
, sizeof(*addr
)), GFP_NOFS
);
1196 if (!state
|| !weight
|| !addr
) {
1203 to_copy
= min(map
->max_osd
, max
);
1204 if (map
->osd_state
) {
1205 memcpy(state
, map
->osd_state
, to_copy
* sizeof(*state
));
1206 memcpy(weight
, map
->osd_weight
, to_copy
* sizeof(*weight
));
1207 memcpy(addr
, map
->osd_addr
, to_copy
* sizeof(*addr
));
1208 kvfree(map
->osd_state
);
1209 kvfree(map
->osd_weight
);
1210 kvfree(map
->osd_addr
);
1213 map
->osd_state
= state
;
1214 map
->osd_weight
= weight
;
1215 map
->osd_addr
= addr
;
1216 for (i
= map
->max_osd
; i
< max
; i
++) {
1217 map
->osd_state
[i
] = 0;
1218 map
->osd_weight
[i
] = CEPH_OSD_OUT
;
1219 memset(map
->osd_addr
+ i
, 0, sizeof(*map
->osd_addr
));
1222 if (map
->osd_primary_affinity
) {
1225 affinity
= ceph_kvmalloc(array_size(max
, sizeof(*affinity
)),
1230 memcpy(affinity
, map
->osd_primary_affinity
,
1231 to_copy
* sizeof(*affinity
));
1232 kvfree(map
->osd_primary_affinity
);
1234 map
->osd_primary_affinity
= affinity
;
1235 for (i
= map
->max_osd
; i
< max
; i
++)
1236 map
->osd_primary_affinity
[i
] =
1237 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY
;
1245 static int osdmap_set_crush(struct ceph_osdmap
*map
, struct crush_map
*crush
)
1247 struct crush_work
*work
;
1250 return PTR_ERR(crush
);
1252 work
= alloc_workspace(crush
);
1254 crush_destroy(crush
);
1259 crush_destroy(map
->crush
);
1260 cleanup_workspace_manager(&map
->crush_wsm
);
1262 add_initial_workspace(&map
->crush_wsm
, work
);
1266 #define OSDMAP_WRAPPER_COMPAT_VER 7
1267 #define OSDMAP_CLIENT_DATA_COMPAT_VER 1
1270 * Return 0 or error. On success, *v is set to 0 for old (v6) osdmaps,
1271 * to struct_v of the client_data section for new (v7 and above)
1274 static int get_osdmap_client_data_v(void **p
, void *end
,
1275 const char *prefix
, u8
*v
)
1279 ceph_decode_8_safe(p
, end
, struct_v
, e_inval
);
1280 if (struct_v
>= 7) {
1283 ceph_decode_8_safe(p
, end
, struct_compat
, e_inval
);
1284 if (struct_compat
> OSDMAP_WRAPPER_COMPAT_VER
) {
1285 pr_warn("got v %d cv %d > %d of %s ceph_osdmap\n",
1286 struct_v
, struct_compat
,
1287 OSDMAP_WRAPPER_COMPAT_VER
, prefix
);
1290 *p
+= 4; /* ignore wrapper struct_len */
1292 ceph_decode_8_safe(p
, end
, struct_v
, e_inval
);
1293 ceph_decode_8_safe(p
, end
, struct_compat
, e_inval
);
1294 if (struct_compat
> OSDMAP_CLIENT_DATA_COMPAT_VER
) {
1295 pr_warn("got v %d cv %d > %d of %s ceph_osdmap client data\n",
1296 struct_v
, struct_compat
,
1297 OSDMAP_CLIENT_DATA_COMPAT_VER
, prefix
);
1300 *p
+= 4; /* ignore client data struct_len */
1305 ceph_decode_16_safe(p
, end
, version
, e_inval
);
1307 pr_warn("got v %d < 6 of %s ceph_osdmap\n",
1312 /* old osdmap enconding */
1323 static int __decode_pools(void **p
, void *end
, struct ceph_osdmap
*map
,
1328 ceph_decode_32_safe(p
, end
, n
, e_inval
);
1330 struct ceph_pg_pool_info
*pi
;
1334 ceph_decode_64_safe(p
, end
, pool
, e_inval
);
1336 pi
= lookup_pg_pool(&map
->pg_pools
, pool
);
1337 if (!incremental
|| !pi
) {
1338 pi
= kzalloc(sizeof(*pi
), GFP_NOFS
);
1342 RB_CLEAR_NODE(&pi
->node
);
1345 if (!__insert_pg_pool(&map
->pg_pools
, pi
)) {
1351 ret
= decode_pool(p
, end
, pi
);
1362 static int decode_pools(void **p
, void *end
, struct ceph_osdmap
*map
)
1364 return __decode_pools(p
, end
, map
, false);
1367 static int decode_new_pools(void **p
, void *end
, struct ceph_osdmap
*map
)
1369 return __decode_pools(p
, end
, map
, true);
1372 typedef struct ceph_pg_mapping
*(*decode_mapping_fn_t
)(void **, void *, bool);
1374 static int decode_pg_mapping(void **p
, void *end
, struct rb_root
*mapping_root
,
1375 decode_mapping_fn_t fn
, bool incremental
)
1379 WARN_ON(!incremental
&& !fn
);
1381 ceph_decode_32_safe(p
, end
, n
, e_inval
);
1383 struct ceph_pg_mapping
*pg
;
1384 struct ceph_pg pgid
;
1387 ret
= ceph_decode_pgid(p
, end
, &pgid
);
1391 pg
= lookup_pg_mapping(mapping_root
, &pgid
);
1393 WARN_ON(!incremental
);
1394 erase_pg_mapping(mapping_root
, pg
);
1395 free_pg_mapping(pg
);
1399 pg
= fn(p
, end
, incremental
);
1404 pg
->pgid
= pgid
; /* struct */
1405 insert_pg_mapping(mapping_root
, pg
);
1416 static struct ceph_pg_mapping
*__decode_pg_temp(void **p
, void *end
,
1419 struct ceph_pg_mapping
*pg
;
1422 ceph_decode_32_safe(p
, end
, len
, e_inval
);
1423 if (len
== 0 && incremental
)
1424 return NULL
; /* new_pg_temp: [] to remove */
1425 if (len
> (SIZE_MAX
- sizeof(*pg
)) / sizeof(u32
))
1426 return ERR_PTR(-EINVAL
);
1428 ceph_decode_need(p
, end
, len
* sizeof(u32
), e_inval
);
1429 pg
= alloc_pg_mapping(len
* sizeof(u32
));
1431 return ERR_PTR(-ENOMEM
);
1433 pg
->pg_temp
.len
= len
;
1434 for (i
= 0; i
< len
; i
++)
1435 pg
->pg_temp
.osds
[i
] = ceph_decode_32(p
);
1440 return ERR_PTR(-EINVAL
);
1443 static int decode_pg_temp(void **p
, void *end
, struct ceph_osdmap
*map
)
1445 return decode_pg_mapping(p
, end
, &map
->pg_temp
, __decode_pg_temp
,
1449 static int decode_new_pg_temp(void **p
, void *end
, struct ceph_osdmap
*map
)
1451 return decode_pg_mapping(p
, end
, &map
->pg_temp
, __decode_pg_temp
,
1455 static struct ceph_pg_mapping
*__decode_primary_temp(void **p
, void *end
,
1458 struct ceph_pg_mapping
*pg
;
1461 ceph_decode_32_safe(p
, end
, osd
, e_inval
);
1462 if (osd
== (u32
)-1 && incremental
)
1463 return NULL
; /* new_primary_temp: -1 to remove */
1465 pg
= alloc_pg_mapping(0);
1467 return ERR_PTR(-ENOMEM
);
1469 pg
->primary_temp
.osd
= osd
;
1473 return ERR_PTR(-EINVAL
);
1476 static int decode_primary_temp(void **p
, void *end
, struct ceph_osdmap
*map
)
1478 return decode_pg_mapping(p
, end
, &map
->primary_temp
,
1479 __decode_primary_temp
, false);
1482 static int decode_new_primary_temp(void **p
, void *end
,
1483 struct ceph_osdmap
*map
)
1485 return decode_pg_mapping(p
, end
, &map
->primary_temp
,
1486 __decode_primary_temp
, true);
1489 u32
ceph_get_primary_affinity(struct ceph_osdmap
*map
, int osd
)
1491 BUG_ON(osd
>= map
->max_osd
);
1493 if (!map
->osd_primary_affinity
)
1494 return CEPH_OSD_DEFAULT_PRIMARY_AFFINITY
;
1496 return map
->osd_primary_affinity
[osd
];
1499 static int set_primary_affinity(struct ceph_osdmap
*map
, int osd
, u32 aff
)
1501 BUG_ON(osd
>= map
->max_osd
);
1503 if (!map
->osd_primary_affinity
) {
1506 map
->osd_primary_affinity
= ceph_kvmalloc(
1507 array_size(map
->max_osd
, sizeof(*map
->osd_primary_affinity
)),
1509 if (!map
->osd_primary_affinity
)
1512 for (i
= 0; i
< map
->max_osd
; i
++)
1513 map
->osd_primary_affinity
[i
] =
1514 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY
;
1517 map
->osd_primary_affinity
[osd
] = aff
;
1522 static int decode_primary_affinity(void **p
, void *end
,
1523 struct ceph_osdmap
*map
)
1527 ceph_decode_32_safe(p
, end
, len
, e_inval
);
1529 kvfree(map
->osd_primary_affinity
);
1530 map
->osd_primary_affinity
= NULL
;
1533 if (len
!= map
->max_osd
)
1536 ceph_decode_need(p
, end
, map
->max_osd
*sizeof(u32
), e_inval
);
1538 for (i
= 0; i
< map
->max_osd
; i
++) {
1541 ret
= set_primary_affinity(map
, i
, ceph_decode_32(p
));
1552 static int decode_new_primary_affinity(void **p
, void *end
,
1553 struct ceph_osdmap
*map
)
1557 ceph_decode_32_safe(p
, end
, n
, e_inval
);
1562 ceph_decode_32_safe(p
, end
, osd
, e_inval
);
1563 ceph_decode_32_safe(p
, end
, aff
, e_inval
);
1565 ret
= set_primary_affinity(map
, osd
, aff
);
1569 pr_info("osd%d primary-affinity 0x%x\n", osd
, aff
);
1578 static struct ceph_pg_mapping
*__decode_pg_upmap(void **p
, void *end
,
1581 return __decode_pg_temp(p
, end
, false);
1584 static int decode_pg_upmap(void **p
, void *end
, struct ceph_osdmap
*map
)
1586 return decode_pg_mapping(p
, end
, &map
->pg_upmap
, __decode_pg_upmap
,
1590 static int decode_new_pg_upmap(void **p
, void *end
, struct ceph_osdmap
*map
)
1592 return decode_pg_mapping(p
, end
, &map
->pg_upmap
, __decode_pg_upmap
,
1596 static int decode_old_pg_upmap(void **p
, void *end
, struct ceph_osdmap
*map
)
1598 return decode_pg_mapping(p
, end
, &map
->pg_upmap
, NULL
, true);
1601 static struct ceph_pg_mapping
*__decode_pg_upmap_items(void **p
, void *end
,
1604 struct ceph_pg_mapping
*pg
;
1607 ceph_decode_32_safe(p
, end
, len
, e_inval
);
1608 if (len
> (SIZE_MAX
- sizeof(*pg
)) / (2 * sizeof(u32
)))
1609 return ERR_PTR(-EINVAL
);
1611 ceph_decode_need(p
, end
, 2 * len
* sizeof(u32
), e_inval
);
1612 pg
= alloc_pg_mapping(2 * len
* sizeof(u32
));
1614 return ERR_PTR(-ENOMEM
);
1616 pg
->pg_upmap_items
.len
= len
;
1617 for (i
= 0; i
< len
; i
++) {
1618 pg
->pg_upmap_items
.from_to
[i
][0] = ceph_decode_32(p
);
1619 pg
->pg_upmap_items
.from_to
[i
][1] = ceph_decode_32(p
);
1625 return ERR_PTR(-EINVAL
);
1628 static int decode_pg_upmap_items(void **p
, void *end
, struct ceph_osdmap
*map
)
1630 return decode_pg_mapping(p
, end
, &map
->pg_upmap_items
,
1631 __decode_pg_upmap_items
, false);
1634 static int decode_new_pg_upmap_items(void **p
, void *end
,
1635 struct ceph_osdmap
*map
)
1637 return decode_pg_mapping(p
, end
, &map
->pg_upmap_items
,
1638 __decode_pg_upmap_items
, true);
1641 static int decode_old_pg_upmap_items(void **p
, void *end
,
1642 struct ceph_osdmap
*map
)
1644 return decode_pg_mapping(p
, end
, &map
->pg_upmap_items
, NULL
, true);
1648 * decode a full map.
1650 static int osdmap_decode(void **p
, void *end
, bool msgr2
,
1651 struct ceph_osdmap
*map
)
1660 dout("%s %p to %p len %d\n", __func__
, *p
, end
, (int)(end
- *p
));
1662 err
= get_osdmap_client_data_v(p
, end
, "full", &struct_v
);
1666 /* fsid, epoch, created, modified */
1667 ceph_decode_need(p
, end
, sizeof(map
->fsid
) + sizeof(u32
) +
1668 sizeof(map
->created
) + sizeof(map
->modified
), e_inval
);
1669 ceph_decode_copy(p
, &map
->fsid
, sizeof(map
->fsid
));
1670 epoch
= map
->epoch
= ceph_decode_32(p
);
1671 ceph_decode_copy(p
, &map
->created
, sizeof(map
->created
));
1672 ceph_decode_copy(p
, &map
->modified
, sizeof(map
->modified
));
1675 err
= decode_pools(p
, end
, map
);
1680 err
= decode_pool_names(p
, end
, map
);
1684 ceph_decode_32_safe(p
, end
, map
->pool_max
, e_inval
);
1686 ceph_decode_32_safe(p
, end
, map
->flags
, e_inval
);
1689 ceph_decode_32_safe(p
, end
, max
, e_inval
);
1691 /* (re)alloc osd arrays */
1692 err
= osdmap_set_max_osd(map
, max
);
1696 /* osd_state, osd_weight, osd_addrs->client_addr */
1697 ceph_decode_need(p
, end
, 3*sizeof(u32
) +
1698 map
->max_osd
*(struct_v
>= 5 ? sizeof(u32
) :
1700 sizeof(*map
->osd_weight
), e_inval
);
1701 if (ceph_decode_32(p
) != map
->max_osd
)
1704 if (struct_v
>= 5) {
1705 for (i
= 0; i
< map
->max_osd
; i
++)
1706 map
->osd_state
[i
] = ceph_decode_32(p
);
1708 for (i
= 0; i
< map
->max_osd
; i
++)
1709 map
->osd_state
[i
] = ceph_decode_8(p
);
1712 if (ceph_decode_32(p
) != map
->max_osd
)
1715 for (i
= 0; i
< map
->max_osd
; i
++)
1716 map
->osd_weight
[i
] = ceph_decode_32(p
);
1718 if (ceph_decode_32(p
) != map
->max_osd
)
1721 for (i
= 0; i
< map
->max_osd
; i
++) {
1722 struct ceph_entity_addr
*addr
= &map
->osd_addr
[i
];
1725 err
= ceph_decode_entity_addrvec(p
, end
, msgr2
, addr
);
1727 err
= ceph_decode_entity_addr(p
, end
, addr
);
1731 dout("%s osd%d addr %s\n", __func__
, i
, ceph_pr_addr(addr
));
1735 err
= decode_pg_temp(p
, end
, map
);
1740 if (struct_v
>= 1) {
1741 err
= decode_primary_temp(p
, end
, map
);
1746 /* primary_affinity */
1747 if (struct_v
>= 2) {
1748 err
= decode_primary_affinity(p
, end
, map
);
1752 WARN_ON(map
->osd_primary_affinity
);
1756 ceph_decode_32_safe(p
, end
, len
, e_inval
);
1757 err
= osdmap_set_crush(map
, crush_decode(*p
, min(*p
+ len
, end
)));
1762 if (struct_v
>= 3) {
1763 /* erasure_code_profiles */
1764 ceph_decode_skip_map_of_map(p
, end
, string
, string
, string
,
1768 if (struct_v
>= 4) {
1769 err
= decode_pg_upmap(p
, end
, map
);
1773 err
= decode_pg_upmap_items(p
, end
, map
);
1777 WARN_ON(!RB_EMPTY_ROOT(&map
->pg_upmap
));
1778 WARN_ON(!RB_EMPTY_ROOT(&map
->pg_upmap_items
));
1781 /* ignore the rest */
1784 dout("full osdmap epoch %d max_osd %d\n", map
->epoch
, map
->max_osd
);
1790 pr_err("corrupt full osdmap (%d) epoch %d off %d (%p of %p-%p)\n",
1791 err
, epoch
, (int)(*p
- start
), *p
, start
, end
);
1792 print_hex_dump(KERN_DEBUG
, "osdmap: ",
1793 DUMP_PREFIX_OFFSET
, 16, 1,
1794 start
, end
- start
, true);
1799 * Allocate and decode a full map.
1801 struct ceph_osdmap
*ceph_osdmap_decode(void **p
, void *end
, bool msgr2
)
1803 struct ceph_osdmap
*map
;
1806 map
= ceph_osdmap_alloc();
1808 return ERR_PTR(-ENOMEM
);
1810 ret
= osdmap_decode(p
, end
, msgr2
, map
);
1812 ceph_osdmap_destroy(map
);
1813 return ERR_PTR(ret
);
1820 * Encoding order is (new_up_client, new_state, new_weight). Need to
1821 * apply in the (new_weight, new_state, new_up_client) order, because
1822 * an incremental map may look like e.g.
1824 * new_up_client: { osd=6, addr=... } # set osd_state and addr
1825 * new_state: { osd=6, xorstate=EXISTS } # clear osd_state
1827 static int decode_new_up_state_weight(void **p
, void *end
, u8 struct_v
,
1828 bool msgr2
, struct ceph_osdmap
*map
)
1830 void *new_up_client
;
1832 void *new_weight_end
;
1838 ceph_decode_32_safe(p
, end
, len
, e_inval
);
1839 for (i
= 0; i
< len
; ++i
) {
1840 struct ceph_entity_addr addr
;
1842 ceph_decode_skip_32(p
, end
, e_inval
);
1844 ret
= ceph_decode_entity_addrvec(p
, end
, msgr2
, &addr
);
1846 ret
= ceph_decode_entity_addr(p
, end
, &addr
);
1852 ceph_decode_32_safe(p
, end
, len
, e_inval
);
1853 len
*= sizeof(u32
) + (struct_v
>= 5 ? sizeof(u32
) : sizeof(u8
));
1854 ceph_decode_need(p
, end
, len
, e_inval
);
1858 ceph_decode_32_safe(p
, end
, len
, e_inval
);
1863 ceph_decode_need(p
, end
, 2*sizeof(u32
), e_inval
);
1864 osd
= ceph_decode_32(p
);
1865 w
= ceph_decode_32(p
);
1866 BUG_ON(osd
>= map
->max_osd
);
1867 pr_info("osd%d weight 0x%x %s\n", osd
, w
,
1868 w
== CEPH_OSD_IN
? "(in)" :
1869 (w
== CEPH_OSD_OUT
? "(out)" : ""));
1870 map
->osd_weight
[osd
] = w
;
1873 * If we are marking in, set the EXISTS, and clear the
1874 * AUTOOUT and NEW bits.
1877 map
->osd_state
[osd
] |= CEPH_OSD_EXISTS
;
1878 map
->osd_state
[osd
] &= ~(CEPH_OSD_AUTOOUT
|
1882 new_weight_end
= *p
;
1884 /* new_state (up/down) */
1886 len
= ceph_decode_32(p
);
1891 osd
= ceph_decode_32(p
);
1893 xorstate
= ceph_decode_32(p
);
1895 xorstate
= ceph_decode_8(p
);
1897 xorstate
= CEPH_OSD_UP
;
1898 BUG_ON(osd
>= map
->max_osd
);
1899 if ((map
->osd_state
[osd
] & CEPH_OSD_UP
) &&
1900 (xorstate
& CEPH_OSD_UP
))
1901 pr_info("osd%d down\n", osd
);
1902 if ((map
->osd_state
[osd
] & CEPH_OSD_EXISTS
) &&
1903 (xorstate
& CEPH_OSD_EXISTS
)) {
1904 pr_info("osd%d does not exist\n", osd
);
1905 ret
= set_primary_affinity(map
, osd
,
1906 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY
);
1909 memset(map
->osd_addr
+ osd
, 0, sizeof(*map
->osd_addr
));
1910 map
->osd_state
[osd
] = 0;
1912 map
->osd_state
[osd
] ^= xorstate
;
1918 len
= ceph_decode_32(p
);
1921 struct ceph_entity_addr addr
;
1923 osd
= ceph_decode_32(p
);
1924 BUG_ON(osd
>= map
->max_osd
);
1926 ret
= ceph_decode_entity_addrvec(p
, end
, msgr2
, &addr
);
1928 ret
= ceph_decode_entity_addr(p
, end
, &addr
);
1932 dout("%s osd%d addr %s\n", __func__
, osd
, ceph_pr_addr(&addr
));
1934 pr_info("osd%d up\n", osd
);
1935 map
->osd_state
[osd
] |= CEPH_OSD_EXISTS
| CEPH_OSD_UP
;
1936 map
->osd_addr
[osd
] = addr
;
1939 *p
= new_weight_end
;
1947 * decode and apply an incremental map update.
1949 struct ceph_osdmap
*osdmap_apply_incremental(void **p
, void *end
, bool msgr2
,
1950 struct ceph_osdmap
*map
)
1952 struct ceph_fsid fsid
;
1954 struct ceph_timespec modified
;
1958 __s32 new_flags
, max
;
1963 dout("%s %p to %p len %d\n", __func__
, *p
, end
, (int)(end
- *p
));
1965 err
= get_osdmap_client_data_v(p
, end
, "inc", &struct_v
);
1969 /* fsid, epoch, modified, new_pool_max, new_flags */
1970 ceph_decode_need(p
, end
, sizeof(fsid
) + sizeof(u32
) + sizeof(modified
) +
1971 sizeof(u64
) + sizeof(u32
), e_inval
);
1972 ceph_decode_copy(p
, &fsid
, sizeof(fsid
));
1973 epoch
= ceph_decode_32(p
);
1974 BUG_ON(epoch
!= map
->epoch
+1);
1975 ceph_decode_copy(p
, &modified
, sizeof(modified
));
1976 new_pool_max
= ceph_decode_64(p
);
1977 new_flags
= ceph_decode_32(p
);
1980 ceph_decode_32_safe(p
, end
, len
, e_inval
);
1982 dout("apply_incremental full map len %d, %p to %p\n",
1984 return ceph_osdmap_decode(p
, min(*p
+len
, end
), msgr2
);
1988 ceph_decode_32_safe(p
, end
, len
, e_inval
);
1990 err
= osdmap_set_crush(map
,
1991 crush_decode(*p
, min(*p
+ len
, end
)));
1999 map
->flags
= new_flags
;
2000 if (new_pool_max
>= 0)
2001 map
->pool_max
= new_pool_max
;
2004 ceph_decode_32_safe(p
, end
, max
, e_inval
);
2006 err
= osdmap_set_max_osd(map
, max
);
2012 map
->modified
= modified
;
2015 err
= decode_new_pools(p
, end
, map
);
2019 /* new_pool_names */
2020 err
= decode_pool_names(p
, end
, map
);
2025 ceph_decode_32_safe(p
, end
, len
, e_inval
);
2027 struct ceph_pg_pool_info
*pi
;
2029 ceph_decode_64_safe(p
, end
, pool
, e_inval
);
2030 pi
= lookup_pg_pool(&map
->pg_pools
, pool
);
2032 __remove_pg_pool(&map
->pg_pools
, pi
);
2035 /* new_up_client, new_state, new_weight */
2036 err
= decode_new_up_state_weight(p
, end
, struct_v
, msgr2
, map
);
2041 err
= decode_new_pg_temp(p
, end
, map
);
2045 /* new_primary_temp */
2046 if (struct_v
>= 1) {
2047 err
= decode_new_primary_temp(p
, end
, map
);
2052 /* new_primary_affinity */
2053 if (struct_v
>= 2) {
2054 err
= decode_new_primary_affinity(p
, end
, map
);
2059 if (struct_v
>= 3) {
2060 /* new_erasure_code_profiles */
2061 ceph_decode_skip_map_of_map(p
, end
, string
, string
, string
,
2063 /* old_erasure_code_profiles */
2064 ceph_decode_skip_set(p
, end
, string
, e_inval
);
2067 if (struct_v
>= 4) {
2068 err
= decode_new_pg_upmap(p
, end
, map
);
2072 err
= decode_old_pg_upmap(p
, end
, map
);
2076 err
= decode_new_pg_upmap_items(p
, end
, map
);
2080 err
= decode_old_pg_upmap_items(p
, end
, map
);
2085 /* ignore the rest */
2088 dout("inc osdmap epoch %d max_osd %d\n", map
->epoch
, map
->max_osd
);
2094 pr_err("corrupt inc osdmap (%d) epoch %d off %d (%p of %p-%p)\n",
2095 err
, epoch
, (int)(*p
- start
), *p
, start
, end
);
2096 print_hex_dump(KERN_DEBUG
, "osdmap: ",
2097 DUMP_PREFIX_OFFSET
, 16, 1,
2098 start
, end
- start
, true);
2099 return ERR_PTR(err
);
2102 void ceph_oloc_copy(struct ceph_object_locator
*dest
,
2103 const struct ceph_object_locator
*src
)
2105 ceph_oloc_destroy(dest
);
2107 dest
->pool
= src
->pool
;
2109 dest
->pool_ns
= ceph_get_string(src
->pool_ns
);
2111 dest
->pool_ns
= NULL
;
2113 EXPORT_SYMBOL(ceph_oloc_copy
);
2115 void ceph_oloc_destroy(struct ceph_object_locator
*oloc
)
2117 ceph_put_string(oloc
->pool_ns
);
2119 EXPORT_SYMBOL(ceph_oloc_destroy
);
2121 void ceph_oid_copy(struct ceph_object_id
*dest
,
2122 const struct ceph_object_id
*src
)
2124 ceph_oid_destroy(dest
);
2126 if (src
->name
!= src
->inline_name
) {
2127 /* very rare, see ceph_object_id definition */
2128 dest
->name
= kmalloc(src
->name_len
+ 1,
2129 GFP_NOIO
| __GFP_NOFAIL
);
2131 dest
->name
= dest
->inline_name
;
2133 memcpy(dest
->name
, src
->name
, src
->name_len
+ 1);
2134 dest
->name_len
= src
->name_len
;
2136 EXPORT_SYMBOL(ceph_oid_copy
);
2138 static __printf(2, 0)
2139 int oid_printf_vargs(struct ceph_object_id
*oid
, const char *fmt
, va_list ap
)
2143 WARN_ON(!ceph_oid_empty(oid
));
2145 len
= vsnprintf(oid
->inline_name
, sizeof(oid
->inline_name
), fmt
, ap
);
2146 if (len
>= sizeof(oid
->inline_name
))
2149 oid
->name_len
= len
;
2154 * If oid doesn't fit into inline buffer, BUG.
2156 void ceph_oid_printf(struct ceph_object_id
*oid
, const char *fmt
, ...)
2161 BUG_ON(oid_printf_vargs(oid
, fmt
, ap
));
2164 EXPORT_SYMBOL(ceph_oid_printf
);
2166 static __printf(3, 0)
2167 int oid_aprintf_vargs(struct ceph_object_id
*oid
, gfp_t gfp
,
2168 const char *fmt
, va_list ap
)
2174 len
= oid_printf_vargs(oid
, fmt
, aq
);
2178 char *external_name
;
2180 external_name
= kmalloc(len
+ 1, gfp
);
2184 oid
->name
= external_name
;
2185 WARN_ON(vsnprintf(oid
->name
, len
+ 1, fmt
, ap
) != len
);
2186 oid
->name_len
= len
;
2193 * If oid doesn't fit into inline buffer, allocate.
2195 int ceph_oid_aprintf(struct ceph_object_id
*oid
, gfp_t gfp
,
2196 const char *fmt
, ...)
2202 ret
= oid_aprintf_vargs(oid
, gfp
, fmt
, ap
);
2207 EXPORT_SYMBOL(ceph_oid_aprintf
);
2209 void ceph_oid_destroy(struct ceph_object_id
*oid
)
2211 if (oid
->name
!= oid
->inline_name
)
2214 EXPORT_SYMBOL(ceph_oid_destroy
);
2219 static bool __osds_equal(const struct ceph_osds
*lhs
,
2220 const struct ceph_osds
*rhs
)
2222 if (lhs
->size
== rhs
->size
&&
2223 !memcmp(lhs
->osds
, rhs
->osds
, rhs
->size
* sizeof(rhs
->osds
[0])))
2232 static bool osds_equal(const struct ceph_osds
*lhs
,
2233 const struct ceph_osds
*rhs
)
2235 if (__osds_equal(lhs
, rhs
) &&
2236 lhs
->primary
== rhs
->primary
)
2242 static bool osds_valid(const struct ceph_osds
*set
)
2245 if (set
->size
> 0 && set
->primary
>= 0)
2248 /* empty can_shift_osds set */
2249 if (!set
->size
&& set
->primary
== -1)
2252 /* empty !can_shift_osds set - all NONE */
2253 if (set
->size
> 0 && set
->primary
== -1) {
2256 for (i
= 0; i
< set
->size
; i
++) {
2257 if (set
->osds
[i
] != CRUSH_ITEM_NONE
)
2267 void ceph_osds_copy(struct ceph_osds
*dest
, const struct ceph_osds
*src
)
2269 memcpy(dest
->osds
, src
->osds
, src
->size
* sizeof(src
->osds
[0]));
2270 dest
->size
= src
->size
;
2271 dest
->primary
= src
->primary
;
2274 bool ceph_pg_is_split(const struct ceph_pg
*pgid
, u32 old_pg_num
,
2277 int old_bits
= calc_bits_of(old_pg_num
);
2278 int old_mask
= (1 << old_bits
) - 1;
2281 WARN_ON(pgid
->seed
>= old_pg_num
);
2282 if (new_pg_num
<= old_pg_num
)
2285 for (n
= 1; ; n
++) {
2286 int next_bit
= n
<< (old_bits
- 1);
2287 u32 s
= next_bit
| pgid
->seed
;
2289 if (s
< old_pg_num
|| s
== pgid
->seed
)
2291 if (s
>= new_pg_num
)
2294 s
= ceph_stable_mod(s
, old_pg_num
, old_mask
);
2295 if (s
== pgid
->seed
)
2302 bool ceph_is_new_interval(const struct ceph_osds
*old_acting
,
2303 const struct ceph_osds
*new_acting
,
2304 const struct ceph_osds
*old_up
,
2305 const struct ceph_osds
*new_up
,
2312 bool old_sort_bitwise
,
2313 bool new_sort_bitwise
,
2314 bool old_recovery_deletes
,
2315 bool new_recovery_deletes
,
2316 const struct ceph_pg
*pgid
)
2318 return !osds_equal(old_acting
, new_acting
) ||
2319 !osds_equal(old_up
, new_up
) ||
2320 old_size
!= new_size
||
2321 old_min_size
!= new_min_size
||
2322 ceph_pg_is_split(pgid
, old_pg_num
, new_pg_num
) ||
2323 old_sort_bitwise
!= new_sort_bitwise
||
2324 old_recovery_deletes
!= new_recovery_deletes
;
2327 static int calc_pg_rank(int osd
, const struct ceph_osds
*acting
)
2331 for (i
= 0; i
< acting
->size
; i
++) {
2332 if (acting
->osds
[i
] == osd
)
2339 static bool primary_changed(const struct ceph_osds
*old_acting
,
2340 const struct ceph_osds
*new_acting
)
2342 if (!old_acting
->size
&& !new_acting
->size
)
2343 return false; /* both still empty */
2345 if (!old_acting
->size
^ !new_acting
->size
)
2346 return true; /* was empty, now not, or vice versa */
2348 if (old_acting
->primary
!= new_acting
->primary
)
2349 return true; /* primary changed */
2351 if (calc_pg_rank(old_acting
->primary
, old_acting
) !=
2352 calc_pg_rank(new_acting
->primary
, new_acting
))
2355 return false; /* same primary (tho replicas may have changed) */
2358 bool ceph_osds_changed(const struct ceph_osds
*old_acting
,
2359 const struct ceph_osds
*new_acting
,
2362 if (primary_changed(old_acting
, new_acting
))
2365 if (any_change
&& !__osds_equal(old_acting
, new_acting
))
2372 * Map an object into a PG.
2374 * Should only be called with target_oid and target_oloc (as opposed to
2375 * base_oid and base_oloc), since tiering isn't taken into account.
2377 void __ceph_object_locator_to_pg(struct ceph_pg_pool_info
*pi
,
2378 const struct ceph_object_id
*oid
,
2379 const struct ceph_object_locator
*oloc
,
2380 struct ceph_pg
*raw_pgid
)
2382 WARN_ON(pi
->id
!= oloc
->pool
);
2384 if (!oloc
->pool_ns
) {
2385 raw_pgid
->pool
= oloc
->pool
;
2386 raw_pgid
->seed
= ceph_str_hash(pi
->object_hash
, oid
->name
,
2388 dout("%s %s -> raw_pgid %llu.%x\n", __func__
, oid
->name
,
2389 raw_pgid
->pool
, raw_pgid
->seed
);
2391 char stack_buf
[256];
2392 char *buf
= stack_buf
;
2393 int nsl
= oloc
->pool_ns
->len
;
2394 size_t total
= nsl
+ 1 + oid
->name_len
;
2396 if (total
> sizeof(stack_buf
))
2397 buf
= kmalloc(total
, GFP_NOIO
| __GFP_NOFAIL
);
2398 memcpy(buf
, oloc
->pool_ns
->str
, nsl
);
2400 memcpy(buf
+ nsl
+ 1, oid
->name
, oid
->name_len
);
2401 raw_pgid
->pool
= oloc
->pool
;
2402 raw_pgid
->seed
= ceph_str_hash(pi
->object_hash
, buf
, total
);
2403 if (buf
!= stack_buf
)
2405 dout("%s %s ns %.*s -> raw_pgid %llu.%x\n", __func__
,
2406 oid
->name
, nsl
, oloc
->pool_ns
->str
,
2407 raw_pgid
->pool
, raw_pgid
->seed
);
2411 int ceph_object_locator_to_pg(struct ceph_osdmap
*osdmap
,
2412 const struct ceph_object_id
*oid
,
2413 const struct ceph_object_locator
*oloc
,
2414 struct ceph_pg
*raw_pgid
)
2416 struct ceph_pg_pool_info
*pi
;
2418 pi
= ceph_pg_pool_by_id(osdmap
, oloc
->pool
);
2422 __ceph_object_locator_to_pg(pi
, oid
, oloc
, raw_pgid
);
2425 EXPORT_SYMBOL(ceph_object_locator_to_pg
);
2428 * Map a raw PG (full precision ps) into an actual PG.
2430 static void raw_pg_to_pg(struct ceph_pg_pool_info
*pi
,
2431 const struct ceph_pg
*raw_pgid
,
2432 struct ceph_pg
*pgid
)
2434 pgid
->pool
= raw_pgid
->pool
;
2435 pgid
->seed
= ceph_stable_mod(raw_pgid
->seed
, pi
->pg_num
,
2440 * Map a raw PG (full precision ps) into a placement ps (placement
2441 * seed). Include pool id in that value so that different pools don't
2442 * use the same seeds.
2444 static u32
raw_pg_to_pps(struct ceph_pg_pool_info
*pi
,
2445 const struct ceph_pg
*raw_pgid
)
2447 if (pi
->flags
& CEPH_POOL_FLAG_HASHPSPOOL
) {
2448 /* hash pool id and seed so that pool PGs do not overlap */
2449 return crush_hash32_2(CRUSH_HASH_RJENKINS1
,
2450 ceph_stable_mod(raw_pgid
->seed
,
2456 * legacy behavior: add ps and pool together. this is
2457 * not a great approach because the PGs from each pool
2458 * will overlap on top of each other: 0.5 == 1.4 ==
2461 return ceph_stable_mod(raw_pgid
->seed
, pi
->pgp_num
,
2463 (unsigned)raw_pgid
->pool
;
2468 * Magic value used for a "default" fallback choose_args, used if the
2469 * crush_choose_arg_map passed to do_crush() does not exist. If this
2470 * also doesn't exist, fall back to canonical weights.
2472 #define CEPH_DEFAULT_CHOOSE_ARGS -1
2474 static int do_crush(struct ceph_osdmap
*map
, int ruleno
, int x
,
2475 int *result
, int result_max
,
2476 const __u32
*weight
, int weight_max
,
2477 s64 choose_args_index
)
2479 struct crush_choose_arg_map
*arg_map
;
2480 struct crush_work
*work
;
2483 BUG_ON(result_max
> CEPH_PG_MAX_SIZE
);
2485 arg_map
= lookup_choose_arg_map(&map
->crush
->choose_args
,
2488 arg_map
= lookup_choose_arg_map(&map
->crush
->choose_args
,
2489 CEPH_DEFAULT_CHOOSE_ARGS
);
2491 work
= get_workspace(&map
->crush_wsm
, map
->crush
);
2492 r
= crush_do_rule(map
->crush
, ruleno
, x
, result
, result_max
,
2493 weight
, weight_max
, work
,
2494 arg_map
? arg_map
->args
: NULL
);
2495 put_workspace(&map
->crush_wsm
, work
);
2499 static void remove_nonexistent_osds(struct ceph_osdmap
*osdmap
,
2500 struct ceph_pg_pool_info
*pi
,
2501 struct ceph_osds
*set
)
2505 if (ceph_can_shift_osds(pi
)) {
2509 for (i
= 0; i
< set
->size
; i
++) {
2510 if (!ceph_osd_exists(osdmap
, set
->osds
[i
])) {
2515 set
->osds
[i
- removed
] = set
->osds
[i
];
2517 set
->size
-= removed
;
2519 /* set dne devices to NONE */
2520 for (i
= 0; i
< set
->size
; i
++) {
2521 if (!ceph_osd_exists(osdmap
, set
->osds
[i
]))
2522 set
->osds
[i
] = CRUSH_ITEM_NONE
;
2528 * Calculate raw set (CRUSH output) for given PG and filter out
2529 * nonexistent OSDs. ->primary is undefined for a raw set.
2531 * Placement seed (CRUSH input) is returned through @ppps.
2533 static void pg_to_raw_osds(struct ceph_osdmap
*osdmap
,
2534 struct ceph_pg_pool_info
*pi
,
2535 const struct ceph_pg
*raw_pgid
,
2536 struct ceph_osds
*raw
,
2539 u32 pps
= raw_pg_to_pps(pi
, raw_pgid
);
2543 ceph_osds_init(raw
);
2547 ruleno
= crush_find_rule(osdmap
->crush
, pi
->crush_ruleset
, pi
->type
,
2550 pr_err("no crush rule: pool %lld ruleset %d type %d size %d\n",
2551 pi
->id
, pi
->crush_ruleset
, pi
->type
, pi
->size
);
2555 if (pi
->size
> ARRAY_SIZE(raw
->osds
)) {
2556 pr_err_ratelimited("pool %lld ruleset %d type %d too wide: size %d > %zu\n",
2557 pi
->id
, pi
->crush_ruleset
, pi
->type
, pi
->size
,
2558 ARRAY_SIZE(raw
->osds
));
2562 len
= do_crush(osdmap
, ruleno
, pps
, raw
->osds
, pi
->size
,
2563 osdmap
->osd_weight
, osdmap
->max_osd
, pi
->id
);
2565 pr_err("error %d from crush rule %d: pool %lld ruleset %d type %d size %d\n",
2566 len
, ruleno
, pi
->id
, pi
->crush_ruleset
, pi
->type
,
2572 remove_nonexistent_osds(osdmap
, pi
, raw
);
2575 /* apply pg_upmap[_items] mappings */
2576 static void apply_upmap(struct ceph_osdmap
*osdmap
,
2577 const struct ceph_pg
*pgid
,
2578 struct ceph_osds
*raw
)
2580 struct ceph_pg_mapping
*pg
;
2583 pg
= lookup_pg_mapping(&osdmap
->pg_upmap
, pgid
);
2585 /* make sure targets aren't marked out */
2586 for (i
= 0; i
< pg
->pg_upmap
.len
; i
++) {
2587 int osd
= pg
->pg_upmap
.osds
[i
];
2589 if (osd
!= CRUSH_ITEM_NONE
&&
2590 osd
< osdmap
->max_osd
&&
2591 osdmap
->osd_weight
[osd
] == 0) {
2592 /* reject/ignore explicit mapping */
2596 for (i
= 0; i
< pg
->pg_upmap
.len
; i
++)
2597 raw
->osds
[i
] = pg
->pg_upmap
.osds
[i
];
2598 raw
->size
= pg
->pg_upmap
.len
;
2599 /* check and apply pg_upmap_items, if any */
2602 pg
= lookup_pg_mapping(&osdmap
->pg_upmap_items
, pgid
);
2605 * Note: this approach does not allow a bidirectional swap,
2606 * e.g., [[1,2],[2,1]] applied to [0,1,2] -> [0,2,1].
2608 for (i
= 0; i
< pg
->pg_upmap_items
.len
; i
++) {
2609 int from
= pg
->pg_upmap_items
.from_to
[i
][0];
2610 int to
= pg
->pg_upmap_items
.from_to
[i
][1];
2612 bool exists
= false;
2614 /* make sure replacement doesn't already appear */
2615 for (j
= 0; j
< raw
->size
; j
++) {
2616 int osd
= raw
->osds
[j
];
2622 /* ignore mapping if target is marked out */
2623 if (osd
== from
&& pos
< 0 &&
2624 !(to
!= CRUSH_ITEM_NONE
&&
2625 to
< osdmap
->max_osd
&&
2626 osdmap
->osd_weight
[to
] == 0)) {
2630 if (!exists
&& pos
>= 0)
2631 raw
->osds
[pos
] = to
;
2637 * Given raw set, calculate up set and up primary. By definition of an
2638 * up set, the result won't contain nonexistent or down OSDs.
2640 * This is done in-place - on return @set is the up set. If it's
2641 * empty, ->primary will remain undefined.
2643 static void raw_to_up_osds(struct ceph_osdmap
*osdmap
,
2644 struct ceph_pg_pool_info
*pi
,
2645 struct ceph_osds
*set
)
2649 /* ->primary is undefined for a raw set */
2650 BUG_ON(set
->primary
!= -1);
2652 if (ceph_can_shift_osds(pi
)) {
2656 for (i
= 0; i
< set
->size
; i
++) {
2657 if (ceph_osd_is_down(osdmap
, set
->osds
[i
])) {
2662 set
->osds
[i
- removed
] = set
->osds
[i
];
2664 set
->size
-= removed
;
2666 set
->primary
= set
->osds
[0];
2668 /* set down/dne devices to NONE */
2669 for (i
= set
->size
- 1; i
>= 0; i
--) {
2670 if (ceph_osd_is_down(osdmap
, set
->osds
[i
]))
2671 set
->osds
[i
] = CRUSH_ITEM_NONE
;
2673 set
->primary
= set
->osds
[i
];
2678 static void apply_primary_affinity(struct ceph_osdmap
*osdmap
,
2679 struct ceph_pg_pool_info
*pi
,
2681 struct ceph_osds
*up
)
2687 * Do we have any non-default primary_affinity values for these
2690 if (!osdmap
->osd_primary_affinity
)
2693 for (i
= 0; i
< up
->size
; i
++) {
2694 int osd
= up
->osds
[i
];
2696 if (osd
!= CRUSH_ITEM_NONE
&&
2697 osdmap
->osd_primary_affinity
[osd
] !=
2698 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY
) {
2706 * Pick the primary. Feed both the seed (for the pg) and the
2707 * osd into the hash/rng so that a proportional fraction of an
2708 * osd's pgs get rejected as primary.
2710 for (i
= 0; i
< up
->size
; i
++) {
2711 int osd
= up
->osds
[i
];
2714 if (osd
== CRUSH_ITEM_NONE
)
2717 aff
= osdmap
->osd_primary_affinity
[osd
];
2718 if (aff
< CEPH_OSD_MAX_PRIMARY_AFFINITY
&&
2719 (crush_hash32_2(CRUSH_HASH_RJENKINS1
,
2720 pps
, osd
) >> 16) >= aff
) {
2722 * We chose not to use this primary. Note it
2723 * anyway as a fallback in case we don't pick
2724 * anyone else, but keep looking.
2736 up
->primary
= up
->osds
[pos
];
2738 if (ceph_can_shift_osds(pi
) && pos
> 0) {
2739 /* move the new primary to the front */
2740 for (i
= pos
; i
> 0; i
--)
2741 up
->osds
[i
] = up
->osds
[i
- 1];
2742 up
->osds
[0] = up
->primary
;
2747 * Get pg_temp and primary_temp mappings for given PG.
2749 * Note that a PG may have none, only pg_temp, only primary_temp or
2750 * both pg_temp and primary_temp mappings. This means @temp isn't
2751 * always a valid OSD set on return: in the "only primary_temp" case,
2752 * @temp will have its ->primary >= 0 but ->size == 0.
2754 static void get_temp_osds(struct ceph_osdmap
*osdmap
,
2755 struct ceph_pg_pool_info
*pi
,
2756 const struct ceph_pg
*pgid
,
2757 struct ceph_osds
*temp
)
2759 struct ceph_pg_mapping
*pg
;
2762 ceph_osds_init(temp
);
2765 pg
= lookup_pg_mapping(&osdmap
->pg_temp
, pgid
);
2767 for (i
= 0; i
< pg
->pg_temp
.len
; i
++) {
2768 if (ceph_osd_is_down(osdmap
, pg
->pg_temp
.osds
[i
])) {
2769 if (ceph_can_shift_osds(pi
))
2772 temp
->osds
[temp
->size
++] = CRUSH_ITEM_NONE
;
2774 temp
->osds
[temp
->size
++] = pg
->pg_temp
.osds
[i
];
2778 /* apply pg_temp's primary */
2779 for (i
= 0; i
< temp
->size
; i
++) {
2780 if (temp
->osds
[i
] != CRUSH_ITEM_NONE
) {
2781 temp
->primary
= temp
->osds
[i
];
2788 pg
= lookup_pg_mapping(&osdmap
->primary_temp
, pgid
);
2790 temp
->primary
= pg
->primary_temp
.osd
;
2794 * Map a PG to its acting set as well as its up set.
2796 * Acting set is used for data mapping purposes, while up set can be
2797 * recorded for detecting interval changes and deciding whether to
2800 void ceph_pg_to_up_acting_osds(struct ceph_osdmap
*osdmap
,
2801 struct ceph_pg_pool_info
*pi
,
2802 const struct ceph_pg
*raw_pgid
,
2803 struct ceph_osds
*up
,
2804 struct ceph_osds
*acting
)
2806 struct ceph_pg pgid
;
2809 WARN_ON(pi
->id
!= raw_pgid
->pool
);
2810 raw_pg_to_pg(pi
, raw_pgid
, &pgid
);
2812 pg_to_raw_osds(osdmap
, pi
, raw_pgid
, up
, &pps
);
2813 apply_upmap(osdmap
, &pgid
, up
);
2814 raw_to_up_osds(osdmap
, pi
, up
);
2815 apply_primary_affinity(osdmap
, pi
, pps
, up
);
2816 get_temp_osds(osdmap
, pi
, &pgid
, acting
);
2817 if (!acting
->size
) {
2818 memcpy(acting
->osds
, up
->osds
, up
->size
* sizeof(up
->osds
[0]));
2819 acting
->size
= up
->size
;
2820 if (acting
->primary
== -1)
2821 acting
->primary
= up
->primary
;
2823 WARN_ON(!osds_valid(up
) || !osds_valid(acting
));
2826 bool ceph_pg_to_primary_shard(struct ceph_osdmap
*osdmap
,
2827 struct ceph_pg_pool_info
*pi
,
2828 const struct ceph_pg
*raw_pgid
,
2829 struct ceph_spg
*spgid
)
2831 struct ceph_pg pgid
;
2832 struct ceph_osds up
, acting
;
2835 WARN_ON(pi
->id
!= raw_pgid
->pool
);
2836 raw_pg_to_pg(pi
, raw_pgid
, &pgid
);
2838 if (ceph_can_shift_osds(pi
)) {
2839 spgid
->pgid
= pgid
; /* struct */
2840 spgid
->shard
= CEPH_SPG_NOSHARD
;
2844 ceph_pg_to_up_acting_osds(osdmap
, pi
, &pgid
, &up
, &acting
);
2845 for (i
= 0; i
< acting
.size
; i
++) {
2846 if (acting
.osds
[i
] == acting
.primary
) {
2847 spgid
->pgid
= pgid
; /* struct */
2857 * Return acting primary for given PG, or -1 if none.
2859 int ceph_pg_to_acting_primary(struct ceph_osdmap
*osdmap
,
2860 const struct ceph_pg
*raw_pgid
)
2862 struct ceph_pg_pool_info
*pi
;
2863 struct ceph_osds up
, acting
;
2865 pi
= ceph_pg_pool_by_id(osdmap
, raw_pgid
->pool
);
2869 ceph_pg_to_up_acting_osds(osdmap
, pi
, raw_pgid
, &up
, &acting
);
2870 return acting
.primary
;
2872 EXPORT_SYMBOL(ceph_pg_to_acting_primary
);
2874 static struct crush_loc_node
*alloc_crush_loc(size_t type_name_len
,
2877 struct crush_loc_node
*loc
;
2879 loc
= kmalloc(sizeof(*loc
) + type_name_len
+ name_len
+ 2, GFP_NOIO
);
2883 RB_CLEAR_NODE(&loc
->cl_node
);
2887 static void free_crush_loc(struct crush_loc_node
*loc
)
2889 WARN_ON(!RB_EMPTY_NODE(&loc
->cl_node
));
2894 static int crush_loc_compare(const struct crush_loc
*loc1
,
2895 const struct crush_loc
*loc2
)
2897 return strcmp(loc1
->cl_type_name
, loc2
->cl_type_name
) ?:
2898 strcmp(loc1
->cl_name
, loc2
->cl_name
);
2901 DEFINE_RB_FUNCS2(crush_loc
, struct crush_loc_node
, cl_loc
, crush_loc_compare
,
2902 RB_BYPTR
, const struct crush_loc
*, cl_node
)
2905 * Parses a set of <bucket type name>':'<bucket name> pairs separated
2906 * by '|', e.g. "rack:foo1|rack:foo2|datacenter:bar".
2908 * Note that @crush_location is modified by strsep().
2910 int ceph_parse_crush_location(char *crush_location
, struct rb_root
*locs
)
2912 struct crush_loc_node
*loc
;
2913 const char *type_name
, *name
, *colon
;
2914 size_t type_name_len
, name_len
;
2916 dout("%s '%s'\n", __func__
, crush_location
);
2917 while ((type_name
= strsep(&crush_location
, "|"))) {
2918 colon
= strchr(type_name
, ':');
2922 type_name_len
= colon
- type_name
;
2923 if (type_name_len
== 0)
2927 name_len
= strlen(name
);
2931 loc
= alloc_crush_loc(type_name_len
, name_len
);
2935 loc
->cl_loc
.cl_type_name
= loc
->cl_data
;
2936 memcpy(loc
->cl_loc
.cl_type_name
, type_name
, type_name_len
);
2937 loc
->cl_loc
.cl_type_name
[type_name_len
] = '\0';
2939 loc
->cl_loc
.cl_name
= loc
->cl_data
+ type_name_len
+ 1;
2940 memcpy(loc
->cl_loc
.cl_name
, name
, name_len
);
2941 loc
->cl_loc
.cl_name
[name_len
] = '\0';
2943 if (!__insert_crush_loc(locs
, loc
)) {
2944 free_crush_loc(loc
);
2948 dout("%s type_name '%s' name '%s'\n", __func__
,
2949 loc
->cl_loc
.cl_type_name
, loc
->cl_loc
.cl_name
);
2955 int ceph_compare_crush_locs(struct rb_root
*locs1
, struct rb_root
*locs2
)
2957 struct rb_node
*n1
= rb_first(locs1
);
2958 struct rb_node
*n2
= rb_first(locs2
);
2961 for ( ; n1
&& n2
; n1
= rb_next(n1
), n2
= rb_next(n2
)) {
2962 struct crush_loc_node
*loc1
=
2963 rb_entry(n1
, struct crush_loc_node
, cl_node
);
2964 struct crush_loc_node
*loc2
=
2965 rb_entry(n2
, struct crush_loc_node
, cl_node
);
2967 ret
= crush_loc_compare(&loc1
->cl_loc
, &loc2
->cl_loc
);
2979 void ceph_clear_crush_locs(struct rb_root
*locs
)
2981 while (!RB_EMPTY_ROOT(locs
)) {
2982 struct crush_loc_node
*loc
=
2983 rb_entry(rb_first(locs
), struct crush_loc_node
, cl_node
);
2985 erase_crush_loc(locs
, loc
);
2986 free_crush_loc(loc
);
2993 static bool is_valid_crush_name(const char *name
)
2996 if (!('a' <= *name
&& *name
<= 'z') &&
2997 !('A' <= *name
&& *name
<= 'Z') &&
2998 !('0' <= *name
&& *name
<= '9') &&
2999 *name
!= '-' && *name
!= '_' && *name
!= '.')
3001 } while (*++name
!= '\0');
3007 * Gets the parent of an item. Returns its id (<0 because the
3008 * parent is always a bucket), type id (>0 for the same reason,
3009 * via @parent_type_id) and location (via @parent_loc). If no
3010 * parent, returns 0.
3012 * Does a linear search, as there are no parent pointers of any
3013 * kind. Note that the result is ambigous for items that occur
3014 * multiple times in the map.
3016 static int get_immediate_parent(struct crush_map
*c
, int id
,
3017 u16
*parent_type_id
,
3018 struct crush_loc
*parent_loc
)
3020 struct crush_bucket
*b
;
3021 struct crush_name_node
*type_cn
, *cn
;
3024 for (i
= 0; i
< c
->max_buckets
; i
++) {
3029 /* ignore per-class shadow hierarchy */
3030 cn
= lookup_crush_name(&c
->names
, b
->id
);
3031 if (!cn
|| !is_valid_crush_name(cn
->cn_name
))
3034 for (j
= 0; j
< b
->size
; j
++) {
3035 if (b
->items
[j
] != id
)
3038 *parent_type_id
= b
->type
;
3039 type_cn
= lookup_crush_name(&c
->type_names
, b
->type
);
3040 parent_loc
->cl_type_name
= type_cn
->cn_name
;
3041 parent_loc
->cl_name
= cn
->cn_name
;
3046 return 0; /* no parent */
3050 * Calculates the locality/distance from an item to a client
3051 * location expressed in terms of CRUSH hierarchy as a set of
3052 * (bucket type name, bucket name) pairs. Specifically, looks
3053 * for the lowest-valued bucket type for which the location of
3054 * @id matches one of the locations in @locs, so for standard
3055 * bucket types (host = 1, rack = 3, datacenter = 8, zone = 9)
3056 * a matching host is closer than a matching rack and a matching
3057 * data center is closer than a matching zone.
3059 * Specifying multiple locations (a "multipath" location) such
3060 * as "rack=foo1 rack=foo2 datacenter=bar" is allowed -- @locs
3061 * is a multimap. The locality will be:
3063 * - 3 for OSDs in racks foo1 and foo2
3064 * - 8 for OSDs in data center bar
3065 * - -1 for all other OSDs
3067 * The lowest possible bucket type is 1, so the best locality
3068 * for an OSD is 1 (i.e. a matching host). Locality 0 would be
3071 int ceph_get_crush_locality(struct ceph_osdmap
*osdmap
, int id
,
3072 struct rb_root
*locs
)
3074 struct crush_loc loc
;
3078 * Instead of repeated get_immediate_parent() calls,
3079 * the location of @id could be obtained with a single
3080 * depth-first traversal.
3083 id
= get_immediate_parent(osdmap
->crush
, id
, &type_id
, &loc
);
3085 return -1; /* not local */
3087 if (lookup_crush_loc(locs
, &loc
))