2 #include <linux/ceph/ceph_debug.h>
4 #include <linux/module.h>
5 #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
, int 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 static int skip_name_map(void **p
, void *end
)
144 ceph_decode_32_safe(p
, end
, len
,bad
);
148 ceph_decode_32_safe(p
, end
, strlen
, bad
);
156 static struct crush_map
*crush_decode(void *pbyval
, void *end
)
162 void *start
= pbyval
;
166 dout("crush_decode %p to %p len %d\n", *p
, end
, (int)(end
- *p
));
168 c
= kzalloc(sizeof(*c
), GFP_NOFS
);
170 return ERR_PTR(-ENOMEM
);
172 /* set tunables to default values */
173 c
->choose_local_tries
= 2;
174 c
->choose_local_fallback_tries
= 5;
175 c
->choose_total_tries
= 19;
176 c
->chooseleaf_descend_once
= 0;
178 ceph_decode_need(p
, end
, 4*sizeof(u32
), bad
);
179 magic
= ceph_decode_32(p
);
180 if (magic
!= CRUSH_MAGIC
) {
181 pr_err("crush_decode magic %x != current %x\n",
182 (unsigned int)magic
, (unsigned int)CRUSH_MAGIC
);
185 c
->max_buckets
= ceph_decode_32(p
);
186 c
->max_rules
= ceph_decode_32(p
);
187 c
->max_devices
= ceph_decode_32(p
);
189 c
->buckets
= kcalloc(c
->max_buckets
, sizeof(*c
->buckets
), GFP_NOFS
);
190 if (c
->buckets
== NULL
)
192 c
->rules
= kcalloc(c
->max_rules
, sizeof(*c
->rules
), GFP_NOFS
);
193 if (c
->rules
== NULL
)
197 for (i
= 0; i
< c
->max_buckets
; i
++) {
200 struct crush_bucket
*b
;
202 ceph_decode_32_safe(p
, end
, alg
, bad
);
204 c
->buckets
[i
] = NULL
;
207 dout("crush_decode bucket %d off %x %p to %p\n",
208 i
, (int)(*p
-start
), *p
, end
);
211 case CRUSH_BUCKET_UNIFORM
:
212 size
= sizeof(struct crush_bucket_uniform
);
214 case CRUSH_BUCKET_LIST
:
215 size
= sizeof(struct crush_bucket_list
);
217 case CRUSH_BUCKET_TREE
:
218 size
= sizeof(struct crush_bucket_tree
);
220 case CRUSH_BUCKET_STRAW
:
221 size
= sizeof(struct crush_bucket_straw
);
223 case CRUSH_BUCKET_STRAW2
:
224 size
= sizeof(struct crush_bucket_straw2
);
231 b
= c
->buckets
[i
] = kzalloc(size
, GFP_NOFS
);
235 ceph_decode_need(p
, end
, 4*sizeof(u32
), bad
);
236 b
->id
= ceph_decode_32(p
);
237 b
->type
= ceph_decode_16(p
);
238 b
->alg
= ceph_decode_8(p
);
239 b
->hash
= ceph_decode_8(p
);
240 b
->weight
= ceph_decode_32(p
);
241 b
->size
= ceph_decode_32(p
);
243 dout("crush_decode bucket size %d off %x %p to %p\n",
244 b
->size
, (int)(*p
-start
), *p
, end
);
246 b
->items
= kcalloc(b
->size
, sizeof(__s32
), GFP_NOFS
);
247 if (b
->items
== NULL
)
249 b
->perm
= kcalloc(b
->size
, sizeof(u32
), GFP_NOFS
);
254 ceph_decode_need(p
, end
, b
->size
*sizeof(u32
), bad
);
255 for (j
= 0; j
< b
->size
; j
++)
256 b
->items
[j
] = ceph_decode_32(p
);
259 case CRUSH_BUCKET_UNIFORM
:
260 err
= crush_decode_uniform_bucket(p
, end
,
261 (struct crush_bucket_uniform
*)b
);
265 case CRUSH_BUCKET_LIST
:
266 err
= crush_decode_list_bucket(p
, end
,
267 (struct crush_bucket_list
*)b
);
271 case CRUSH_BUCKET_TREE
:
272 err
= crush_decode_tree_bucket(p
, end
,
273 (struct crush_bucket_tree
*)b
);
277 case CRUSH_BUCKET_STRAW
:
278 err
= crush_decode_straw_bucket(p
, end
,
279 (struct crush_bucket_straw
*)b
);
283 case CRUSH_BUCKET_STRAW2
:
284 err
= crush_decode_straw2_bucket(p
, end
,
285 (struct crush_bucket_straw2
*)b
);
293 dout("rule vec is %p\n", c
->rules
);
294 for (i
= 0; i
< c
->max_rules
; i
++) {
296 struct crush_rule
*r
;
298 ceph_decode_32_safe(p
, end
, yes
, bad
);
300 dout("crush_decode NO rule %d off %x %p to %p\n",
301 i
, (int)(*p
-start
), *p
, end
);
306 dout("crush_decode rule %d off %x %p to %p\n",
307 i
, (int)(*p
-start
), *p
, end
);
310 ceph_decode_32_safe(p
, end
, yes
, bad
);
311 #if BITS_PER_LONG == 32
313 if (yes
> (ULONG_MAX
- sizeof(*r
))
314 / sizeof(struct crush_rule_step
))
317 r
= c
->rules
[i
] = kmalloc(sizeof(*r
) +
318 yes
*sizeof(struct crush_rule_step
),
322 dout(" rule %d is at %p\n", i
, r
);
324 ceph_decode_copy_safe(p
, end
, &r
->mask
, 4, bad
); /* 4 u8's */
325 ceph_decode_need(p
, end
, r
->len
*3*sizeof(u32
), bad
);
326 for (j
= 0; j
< r
->len
; j
++) {
327 r
->steps
[j
].op
= ceph_decode_32(p
);
328 r
->steps
[j
].arg1
= ceph_decode_32(p
);
329 r
->steps
[j
].arg2
= ceph_decode_32(p
);
333 /* ignore trailing name maps. */
334 for (num_name_maps
= 0; num_name_maps
< 3; num_name_maps
++) {
335 err
= skip_name_map(p
, end
);
341 ceph_decode_need(p
, end
, 3*sizeof(u32
), done
);
342 c
->choose_local_tries
= ceph_decode_32(p
);
343 c
->choose_local_fallback_tries
= ceph_decode_32(p
);
344 c
->choose_total_tries
= ceph_decode_32(p
);
345 dout("crush decode tunable choose_local_tries = %d\n",
346 c
->choose_local_tries
);
347 dout("crush decode tunable choose_local_fallback_tries = %d\n",
348 c
->choose_local_fallback_tries
);
349 dout("crush decode tunable choose_total_tries = %d\n",
350 c
->choose_total_tries
);
352 ceph_decode_need(p
, end
, sizeof(u32
), done
);
353 c
->chooseleaf_descend_once
= ceph_decode_32(p
);
354 dout("crush decode tunable chooseleaf_descend_once = %d\n",
355 c
->chooseleaf_descend_once
);
357 ceph_decode_need(p
, end
, sizeof(u8
), done
);
358 c
->chooseleaf_vary_r
= ceph_decode_8(p
);
359 dout("crush decode tunable chooseleaf_vary_r = %d\n",
360 c
->chooseleaf_vary_r
);
362 /* skip straw_calc_version, allowed_bucket_algs */
363 ceph_decode_need(p
, end
, sizeof(u8
) + sizeof(u32
), done
);
364 *p
+= sizeof(u8
) + sizeof(u32
);
366 ceph_decode_need(p
, end
, sizeof(u8
), done
);
367 c
->chooseleaf_stable
= ceph_decode_8(p
);
368 dout("crush decode tunable chooseleaf_stable = %d\n",
369 c
->chooseleaf_stable
);
372 dout("crush_decode success\n");
378 dout("crush_decode fail %d\n", err
);
384 * rbtree of pg_mapping for handling pg_temp (explicit mapping of pgid
385 * to a set of osds) and primary_temp (explicit primary setting)
387 static int pgid_cmp(struct ceph_pg l
, struct ceph_pg r
)
400 static int __insert_pg_mapping(struct ceph_pg_mapping
*new,
401 struct rb_root
*root
)
403 struct rb_node
**p
= &root
->rb_node
;
404 struct rb_node
*parent
= NULL
;
405 struct ceph_pg_mapping
*pg
= NULL
;
408 dout("__insert_pg_mapping %llx %p\n", *(u64
*)&new->pgid
, new);
411 pg
= rb_entry(parent
, struct ceph_pg_mapping
, node
);
412 c
= pgid_cmp(new->pgid
, pg
->pgid
);
421 rb_link_node(&new->node
, parent
, p
);
422 rb_insert_color(&new->node
, root
);
426 static struct ceph_pg_mapping
*__lookup_pg_mapping(struct rb_root
*root
,
429 struct rb_node
*n
= root
->rb_node
;
430 struct ceph_pg_mapping
*pg
;
434 pg
= rb_entry(n
, struct ceph_pg_mapping
, node
);
435 c
= pgid_cmp(pgid
, pg
->pgid
);
441 dout("__lookup_pg_mapping %lld.%x got %p\n",
442 pgid
.pool
, pgid
.seed
, pg
);
449 static int __remove_pg_mapping(struct rb_root
*root
, struct ceph_pg pgid
)
451 struct ceph_pg_mapping
*pg
= __lookup_pg_mapping(root
, pgid
);
454 dout("__remove_pg_mapping %lld.%x %p\n", pgid
.pool
, pgid
.seed
,
456 rb_erase(&pg
->node
, root
);
460 dout("__remove_pg_mapping %lld.%x dne\n", pgid
.pool
, pgid
.seed
);
465 * rbtree of pg pool info
467 static int __insert_pg_pool(struct rb_root
*root
, struct ceph_pg_pool_info
*new)
469 struct rb_node
**p
= &root
->rb_node
;
470 struct rb_node
*parent
= NULL
;
471 struct ceph_pg_pool_info
*pi
= NULL
;
475 pi
= rb_entry(parent
, struct ceph_pg_pool_info
, node
);
476 if (new->id
< pi
->id
)
478 else if (new->id
> pi
->id
)
484 rb_link_node(&new->node
, parent
, p
);
485 rb_insert_color(&new->node
, root
);
489 static struct ceph_pg_pool_info
*__lookup_pg_pool(struct rb_root
*root
, u64 id
)
491 struct ceph_pg_pool_info
*pi
;
492 struct rb_node
*n
= root
->rb_node
;
495 pi
= rb_entry(n
, struct ceph_pg_pool_info
, node
);
498 else if (id
> pi
->id
)
506 struct ceph_pg_pool_info
*ceph_pg_pool_by_id(struct ceph_osdmap
*map
, u64 id
)
508 return __lookup_pg_pool(&map
->pg_pools
, id
);
511 const char *ceph_pg_pool_name_by_id(struct ceph_osdmap
*map
, u64 id
)
513 struct ceph_pg_pool_info
*pi
;
515 if (id
== CEPH_NOPOOL
)
518 if (WARN_ON_ONCE(id
> (u64
) INT_MAX
))
521 pi
= __lookup_pg_pool(&map
->pg_pools
, (int) id
);
523 return pi
? pi
->name
: NULL
;
525 EXPORT_SYMBOL(ceph_pg_pool_name_by_id
);
527 int ceph_pg_poolid_by_name(struct ceph_osdmap
*map
, const char *name
)
531 for (rbp
= rb_first(&map
->pg_pools
); rbp
; rbp
= rb_next(rbp
)) {
532 struct ceph_pg_pool_info
*pi
=
533 rb_entry(rbp
, struct ceph_pg_pool_info
, node
);
534 if (pi
->name
&& strcmp(pi
->name
, name
) == 0)
539 EXPORT_SYMBOL(ceph_pg_poolid_by_name
);
541 static void __remove_pg_pool(struct rb_root
*root
, struct ceph_pg_pool_info
*pi
)
543 rb_erase(&pi
->node
, root
);
548 static int decode_pool(void **p
, void *end
, struct ceph_pg_pool_info
*pi
)
554 ceph_decode_need(p
, end
, 2 + 4, bad
);
555 ev
= ceph_decode_8(p
); /* encoding version */
556 cv
= ceph_decode_8(p
); /* compat version */
558 pr_warn("got v %d < 5 cv %d of ceph_pg_pool\n", ev
, cv
);
562 pr_warn("got v %d cv %d > 9 of ceph_pg_pool\n", ev
, cv
);
565 len
= ceph_decode_32(p
);
566 ceph_decode_need(p
, end
, len
, bad
);
569 pi
->type
= ceph_decode_8(p
);
570 pi
->size
= ceph_decode_8(p
);
571 pi
->crush_ruleset
= ceph_decode_8(p
);
572 pi
->object_hash
= ceph_decode_8(p
);
574 pi
->pg_num
= ceph_decode_32(p
);
575 pi
->pgp_num
= ceph_decode_32(p
);
577 *p
+= 4 + 4; /* skip lpg* */
578 *p
+= 4; /* skip last_change */
579 *p
+= 8 + 4; /* skip snap_seq, snap_epoch */
582 num
= ceph_decode_32(p
);
584 *p
+= 8; /* snapid key */
585 *p
+= 1 + 1; /* versions */
586 len
= ceph_decode_32(p
);
590 /* skip removed_snaps */
591 num
= ceph_decode_32(p
);
594 *p
+= 8; /* skip auid */
595 pi
->flags
= ceph_decode_64(p
);
596 *p
+= 4; /* skip crash_replay_interval */
599 *p
+= 1; /* skip min_size */
602 *p
+= 8 + 8; /* skip quota_max_* */
606 num
= ceph_decode_32(p
);
609 *p
+= 8; /* skip tier_of */
610 *p
+= 1; /* skip cache_mode */
612 pi
->read_tier
= ceph_decode_64(p
);
613 pi
->write_tier
= ceph_decode_64(p
);
619 /* ignore the rest */
629 static int decode_pool_names(void **p
, void *end
, struct ceph_osdmap
*map
)
631 struct ceph_pg_pool_info
*pi
;
635 ceph_decode_32_safe(p
, end
, num
, bad
);
636 dout(" %d pool names\n", num
);
638 ceph_decode_64_safe(p
, end
, pool
, bad
);
639 ceph_decode_32_safe(p
, end
, len
, bad
);
640 dout(" pool %llu len %d\n", pool
, len
);
641 ceph_decode_need(p
, end
, len
, bad
);
642 pi
= __lookup_pg_pool(&map
->pg_pools
, pool
);
644 char *name
= kstrndup(*p
, len
, GFP_NOFS
);
650 dout(" name is %s\n", pi
->name
);
663 void ceph_osdmap_destroy(struct ceph_osdmap
*map
)
665 dout("osdmap_destroy %p\n", map
);
667 crush_destroy(map
->crush
);
668 while (!RB_EMPTY_ROOT(&map
->pg_temp
)) {
669 struct ceph_pg_mapping
*pg
=
670 rb_entry(rb_first(&map
->pg_temp
),
671 struct ceph_pg_mapping
, node
);
672 rb_erase(&pg
->node
, &map
->pg_temp
);
675 while (!RB_EMPTY_ROOT(&map
->primary_temp
)) {
676 struct ceph_pg_mapping
*pg
=
677 rb_entry(rb_first(&map
->primary_temp
),
678 struct ceph_pg_mapping
, node
);
679 rb_erase(&pg
->node
, &map
->primary_temp
);
682 while (!RB_EMPTY_ROOT(&map
->pg_pools
)) {
683 struct ceph_pg_pool_info
*pi
=
684 rb_entry(rb_first(&map
->pg_pools
),
685 struct ceph_pg_pool_info
, node
);
686 __remove_pg_pool(&map
->pg_pools
, pi
);
688 kfree(map
->osd_state
);
689 kfree(map
->osd_weight
);
690 kfree(map
->osd_addr
);
691 kfree(map
->osd_primary_affinity
);
696 * Adjust max_osd value, (re)allocate arrays.
698 * The new elements are properly initialized.
700 static int osdmap_set_max_osd(struct ceph_osdmap
*map
, int max
)
704 struct ceph_entity_addr
*addr
;
707 state
= krealloc(map
->osd_state
, max
*sizeof(*state
), GFP_NOFS
);
710 map
->osd_state
= state
;
712 weight
= krealloc(map
->osd_weight
, max
*sizeof(*weight
), GFP_NOFS
);
715 map
->osd_weight
= weight
;
717 addr
= krealloc(map
->osd_addr
, max
*sizeof(*addr
), GFP_NOFS
);
720 map
->osd_addr
= addr
;
722 for (i
= map
->max_osd
; i
< max
; i
++) {
723 map
->osd_state
[i
] = 0;
724 map
->osd_weight
[i
] = CEPH_OSD_OUT
;
725 memset(map
->osd_addr
+ i
, 0, sizeof(*map
->osd_addr
));
728 if (map
->osd_primary_affinity
) {
731 affinity
= krealloc(map
->osd_primary_affinity
,
732 max
*sizeof(*affinity
), GFP_NOFS
);
735 map
->osd_primary_affinity
= affinity
;
737 for (i
= map
->max_osd
; i
< max
; i
++)
738 map
->osd_primary_affinity
[i
] =
739 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY
;
747 #define OSDMAP_WRAPPER_COMPAT_VER 7
748 #define OSDMAP_CLIENT_DATA_COMPAT_VER 1
751 * Return 0 or error. On success, *v is set to 0 for old (v6) osdmaps,
752 * to struct_v of the client_data section for new (v7 and above)
755 static int get_osdmap_client_data_v(void **p
, void *end
,
756 const char *prefix
, u8
*v
)
760 ceph_decode_8_safe(p
, end
, struct_v
, e_inval
);
764 ceph_decode_8_safe(p
, end
, struct_compat
, e_inval
);
765 if (struct_compat
> OSDMAP_WRAPPER_COMPAT_VER
) {
766 pr_warn("got v %d cv %d > %d of %s ceph_osdmap\n",
767 struct_v
, struct_compat
,
768 OSDMAP_WRAPPER_COMPAT_VER
, prefix
);
771 *p
+= 4; /* ignore wrapper struct_len */
773 ceph_decode_8_safe(p
, end
, struct_v
, e_inval
);
774 ceph_decode_8_safe(p
, end
, struct_compat
, e_inval
);
775 if (struct_compat
> OSDMAP_CLIENT_DATA_COMPAT_VER
) {
776 pr_warn("got v %d cv %d > %d of %s ceph_osdmap client data\n",
777 struct_v
, struct_compat
,
778 OSDMAP_CLIENT_DATA_COMPAT_VER
, prefix
);
781 *p
+= 4; /* ignore client data struct_len */
786 ceph_decode_16_safe(p
, end
, version
, e_inval
);
788 pr_warn("got v %d < 6 of %s ceph_osdmap\n",
793 /* old osdmap enconding */
804 static int __decode_pools(void **p
, void *end
, struct ceph_osdmap
*map
,
809 ceph_decode_32_safe(p
, end
, n
, e_inval
);
811 struct ceph_pg_pool_info
*pi
;
815 ceph_decode_64_safe(p
, end
, pool
, e_inval
);
817 pi
= __lookup_pg_pool(&map
->pg_pools
, pool
);
818 if (!incremental
|| !pi
) {
819 pi
= kzalloc(sizeof(*pi
), GFP_NOFS
);
825 ret
= __insert_pg_pool(&map
->pg_pools
, pi
);
832 ret
= decode_pool(p
, end
, pi
);
843 static int decode_pools(void **p
, void *end
, struct ceph_osdmap
*map
)
845 return __decode_pools(p
, end
, map
, false);
848 static int decode_new_pools(void **p
, void *end
, struct ceph_osdmap
*map
)
850 return __decode_pools(p
, end
, map
, true);
853 static int __decode_pg_temp(void **p
, void *end
, struct ceph_osdmap
*map
,
858 ceph_decode_32_safe(p
, end
, n
, e_inval
);
864 ret
= ceph_decode_pgid(p
, end
, &pgid
);
868 ceph_decode_32_safe(p
, end
, len
, e_inval
);
870 ret
= __remove_pg_mapping(&map
->pg_temp
, pgid
);
871 BUG_ON(!incremental
&& ret
!= -ENOENT
);
873 if (!incremental
|| len
> 0) {
874 struct ceph_pg_mapping
*pg
;
876 ceph_decode_need(p
, end
, len
*sizeof(u32
), e_inval
);
878 if (len
> (UINT_MAX
- sizeof(*pg
)) / sizeof(u32
))
881 pg
= kzalloc(sizeof(*pg
) + len
*sizeof(u32
), GFP_NOFS
);
886 pg
->pg_temp
.len
= len
;
887 for (i
= 0; i
< len
; i
++)
888 pg
->pg_temp
.osds
[i
] = ceph_decode_32(p
);
890 ret
= __insert_pg_mapping(pg
, &map
->pg_temp
);
904 static int decode_pg_temp(void **p
, void *end
, struct ceph_osdmap
*map
)
906 return __decode_pg_temp(p
, end
, map
, false);
909 static int decode_new_pg_temp(void **p
, void *end
, struct ceph_osdmap
*map
)
911 return __decode_pg_temp(p
, end
, map
, true);
914 static int __decode_primary_temp(void **p
, void *end
, struct ceph_osdmap
*map
,
919 ceph_decode_32_safe(p
, end
, n
, e_inval
);
925 ret
= ceph_decode_pgid(p
, end
, &pgid
);
929 ceph_decode_32_safe(p
, end
, osd
, e_inval
);
931 ret
= __remove_pg_mapping(&map
->primary_temp
, pgid
);
932 BUG_ON(!incremental
&& ret
!= -ENOENT
);
934 if (!incremental
|| osd
!= (u32
)-1) {
935 struct ceph_pg_mapping
*pg
;
937 pg
= kzalloc(sizeof(*pg
), GFP_NOFS
);
942 pg
->primary_temp
.osd
= osd
;
944 ret
= __insert_pg_mapping(pg
, &map
->primary_temp
);
958 static int decode_primary_temp(void **p
, void *end
, struct ceph_osdmap
*map
)
960 return __decode_primary_temp(p
, end
, map
, false);
963 static int decode_new_primary_temp(void **p
, void *end
,
964 struct ceph_osdmap
*map
)
966 return __decode_primary_temp(p
, end
, map
, true);
969 u32
ceph_get_primary_affinity(struct ceph_osdmap
*map
, int osd
)
971 BUG_ON(osd
>= map
->max_osd
);
973 if (!map
->osd_primary_affinity
)
974 return CEPH_OSD_DEFAULT_PRIMARY_AFFINITY
;
976 return map
->osd_primary_affinity
[osd
];
979 static int set_primary_affinity(struct ceph_osdmap
*map
, int osd
, u32 aff
)
981 BUG_ON(osd
>= map
->max_osd
);
983 if (!map
->osd_primary_affinity
) {
986 map
->osd_primary_affinity
= kmalloc(map
->max_osd
*sizeof(u32
),
988 if (!map
->osd_primary_affinity
)
991 for (i
= 0; i
< map
->max_osd
; i
++)
992 map
->osd_primary_affinity
[i
] =
993 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY
;
996 map
->osd_primary_affinity
[osd
] = aff
;
1001 static int decode_primary_affinity(void **p
, void *end
,
1002 struct ceph_osdmap
*map
)
1006 ceph_decode_32_safe(p
, end
, len
, e_inval
);
1008 kfree(map
->osd_primary_affinity
);
1009 map
->osd_primary_affinity
= NULL
;
1012 if (len
!= map
->max_osd
)
1015 ceph_decode_need(p
, end
, map
->max_osd
*sizeof(u32
), e_inval
);
1017 for (i
= 0; i
< map
->max_osd
; i
++) {
1020 ret
= set_primary_affinity(map
, i
, ceph_decode_32(p
));
1031 static int decode_new_primary_affinity(void **p
, void *end
,
1032 struct ceph_osdmap
*map
)
1036 ceph_decode_32_safe(p
, end
, n
, e_inval
);
1041 ceph_decode_32_safe(p
, end
, osd
, e_inval
);
1042 ceph_decode_32_safe(p
, end
, aff
, e_inval
);
1044 ret
= set_primary_affinity(map
, osd
, aff
);
1048 pr_info("osd%d primary-affinity 0x%x\n", osd
, aff
);
1058 * decode a full map.
1060 static int osdmap_decode(void **p
, void *end
, struct ceph_osdmap
*map
)
1069 dout("%s %p to %p len %d\n", __func__
, *p
, end
, (int)(end
- *p
));
1071 err
= get_osdmap_client_data_v(p
, end
, "full", &struct_v
);
1075 /* fsid, epoch, created, modified */
1076 ceph_decode_need(p
, end
, sizeof(map
->fsid
) + sizeof(u32
) +
1077 sizeof(map
->created
) + sizeof(map
->modified
), e_inval
);
1078 ceph_decode_copy(p
, &map
->fsid
, sizeof(map
->fsid
));
1079 epoch
= map
->epoch
= ceph_decode_32(p
);
1080 ceph_decode_copy(p
, &map
->created
, sizeof(map
->created
));
1081 ceph_decode_copy(p
, &map
->modified
, sizeof(map
->modified
));
1084 err
= decode_pools(p
, end
, map
);
1089 err
= decode_pool_names(p
, end
, map
);
1093 ceph_decode_32_safe(p
, end
, map
->pool_max
, e_inval
);
1095 ceph_decode_32_safe(p
, end
, map
->flags
, e_inval
);
1098 ceph_decode_32_safe(p
, end
, max
, e_inval
);
1100 /* (re)alloc osd arrays */
1101 err
= osdmap_set_max_osd(map
, max
);
1105 /* osd_state, osd_weight, osd_addrs->client_addr */
1106 ceph_decode_need(p
, end
, 3*sizeof(u32
) +
1107 map
->max_osd
*(1 + sizeof(*map
->osd_weight
) +
1108 sizeof(*map
->osd_addr
)), e_inval
);
1110 if (ceph_decode_32(p
) != map
->max_osd
)
1113 ceph_decode_copy(p
, map
->osd_state
, map
->max_osd
);
1115 if (ceph_decode_32(p
) != map
->max_osd
)
1118 for (i
= 0; i
< map
->max_osd
; i
++)
1119 map
->osd_weight
[i
] = ceph_decode_32(p
);
1121 if (ceph_decode_32(p
) != map
->max_osd
)
1124 ceph_decode_copy(p
, map
->osd_addr
, map
->max_osd
*sizeof(*map
->osd_addr
));
1125 for (i
= 0; i
< map
->max_osd
; i
++)
1126 ceph_decode_addr(&map
->osd_addr
[i
]);
1129 err
= decode_pg_temp(p
, end
, map
);
1134 if (struct_v
>= 1) {
1135 err
= decode_primary_temp(p
, end
, map
);
1140 /* primary_affinity */
1141 if (struct_v
>= 2) {
1142 err
= decode_primary_affinity(p
, end
, map
);
1146 /* XXX can this happen? */
1147 kfree(map
->osd_primary_affinity
);
1148 map
->osd_primary_affinity
= NULL
;
1152 ceph_decode_32_safe(p
, end
, len
, e_inval
);
1153 map
->crush
= crush_decode(*p
, min(*p
+ len
, end
));
1154 if (IS_ERR(map
->crush
)) {
1155 err
= PTR_ERR(map
->crush
);
1161 /* ignore the rest */
1164 dout("full osdmap epoch %d max_osd %d\n", map
->epoch
, map
->max_osd
);
1170 pr_err("corrupt full osdmap (%d) epoch %d off %d (%p of %p-%p)\n",
1171 err
, epoch
, (int)(*p
- start
), *p
, start
, end
);
1172 print_hex_dump(KERN_DEBUG
, "osdmap: ",
1173 DUMP_PREFIX_OFFSET
, 16, 1,
1174 start
, end
- start
, true);
1179 * Allocate and decode a full map.
1181 struct ceph_osdmap
*ceph_osdmap_decode(void **p
, void *end
)
1183 struct ceph_osdmap
*map
;
1186 map
= kzalloc(sizeof(*map
), GFP_NOFS
);
1188 return ERR_PTR(-ENOMEM
);
1190 map
->pg_temp
= RB_ROOT
;
1191 map
->primary_temp
= RB_ROOT
;
1192 mutex_init(&map
->crush_scratch_mutex
);
1194 ret
= osdmap_decode(p
, end
, map
);
1196 ceph_osdmap_destroy(map
);
1197 return ERR_PTR(ret
);
1204 * decode and apply an incremental map update.
1206 struct ceph_osdmap
*osdmap_apply_incremental(void **p
, void *end
,
1207 struct ceph_osdmap
*map
,
1208 struct ceph_messenger
*msgr
)
1210 struct crush_map
*newcrush
= NULL
;
1211 struct ceph_fsid fsid
;
1213 struct ceph_timespec modified
;
1217 __s32 new_flags
, max
;
1222 dout("%s %p to %p len %d\n", __func__
, *p
, end
, (int)(end
- *p
));
1224 err
= get_osdmap_client_data_v(p
, end
, "inc", &struct_v
);
1228 /* fsid, epoch, modified, new_pool_max, new_flags */
1229 ceph_decode_need(p
, end
, sizeof(fsid
) + sizeof(u32
) + sizeof(modified
) +
1230 sizeof(u64
) + sizeof(u32
), e_inval
);
1231 ceph_decode_copy(p
, &fsid
, sizeof(fsid
));
1232 epoch
= ceph_decode_32(p
);
1233 BUG_ON(epoch
!= map
->epoch
+1);
1234 ceph_decode_copy(p
, &modified
, sizeof(modified
));
1235 new_pool_max
= ceph_decode_64(p
);
1236 new_flags
= ceph_decode_32(p
);
1239 ceph_decode_32_safe(p
, end
, len
, e_inval
);
1241 dout("apply_incremental full map len %d, %p to %p\n",
1243 return ceph_osdmap_decode(p
, min(*p
+len
, end
));
1247 ceph_decode_32_safe(p
, end
, len
, e_inval
);
1249 newcrush
= crush_decode(*p
, min(*p
+len
, end
));
1250 if (IS_ERR(newcrush
)) {
1251 err
= PTR_ERR(newcrush
);
1260 map
->flags
= new_flags
;
1261 if (new_pool_max
>= 0)
1262 map
->pool_max
= new_pool_max
;
1265 ceph_decode_32_safe(p
, end
, max
, e_inval
);
1267 err
= osdmap_set_max_osd(map
, max
);
1273 map
->modified
= modified
;
1276 crush_destroy(map
->crush
);
1277 map
->crush
= newcrush
;
1282 err
= decode_new_pools(p
, end
, map
);
1286 /* new_pool_names */
1287 err
= decode_pool_names(p
, end
, map
);
1292 ceph_decode_32_safe(p
, end
, len
, e_inval
);
1294 struct ceph_pg_pool_info
*pi
;
1296 ceph_decode_64_safe(p
, end
, pool
, e_inval
);
1297 pi
= __lookup_pg_pool(&map
->pg_pools
, pool
);
1299 __remove_pg_pool(&map
->pg_pools
, pi
);
1303 ceph_decode_32_safe(p
, end
, len
, e_inval
);
1306 struct ceph_entity_addr addr
;
1307 ceph_decode_32_safe(p
, end
, osd
, e_inval
);
1308 ceph_decode_copy_safe(p
, end
, &addr
, sizeof(addr
), e_inval
);
1309 ceph_decode_addr(&addr
);
1310 pr_info("osd%d up\n", osd
);
1311 BUG_ON(osd
>= map
->max_osd
);
1312 map
->osd_state
[osd
] |= CEPH_OSD_UP
| CEPH_OSD_EXISTS
;
1313 map
->osd_addr
[osd
] = addr
;
1317 ceph_decode_32_safe(p
, end
, len
, e_inval
);
1321 ceph_decode_32_safe(p
, end
, osd
, e_inval
);
1322 xorstate
= **(u8
**)p
;
1323 (*p
)++; /* clean flag */
1325 xorstate
= CEPH_OSD_UP
;
1326 if (xorstate
& CEPH_OSD_UP
)
1327 pr_info("osd%d down\n", osd
);
1328 if (osd
< map
->max_osd
)
1329 map
->osd_state
[osd
] ^= xorstate
;
1333 ceph_decode_32_safe(p
, end
, len
, e_inval
);
1336 ceph_decode_need(p
, end
, sizeof(u32
)*2, e_inval
);
1337 osd
= ceph_decode_32(p
);
1338 off
= ceph_decode_32(p
);
1339 pr_info("osd%d weight 0x%x %s\n", osd
, off
,
1340 off
== CEPH_OSD_IN
? "(in)" :
1341 (off
== CEPH_OSD_OUT
? "(out)" : ""));
1342 if (osd
< map
->max_osd
)
1343 map
->osd_weight
[osd
] = off
;
1347 err
= decode_new_pg_temp(p
, end
, map
);
1351 /* new_primary_temp */
1352 if (struct_v
>= 1) {
1353 err
= decode_new_primary_temp(p
, end
, map
);
1358 /* new_primary_affinity */
1359 if (struct_v
>= 2) {
1360 err
= decode_new_primary_affinity(p
, end
, map
);
1365 /* ignore the rest */
1368 dout("inc osdmap epoch %d max_osd %d\n", map
->epoch
, map
->max_osd
);
1374 pr_err("corrupt inc osdmap (%d) epoch %d off %d (%p of %p-%p)\n",
1375 err
, epoch
, (int)(*p
- start
), *p
, start
, end
);
1376 print_hex_dump(KERN_DEBUG
, "osdmap: ",
1377 DUMP_PREFIX_OFFSET
, 16, 1,
1378 start
, end
- start
, true);
1380 crush_destroy(newcrush
);
1381 return ERR_PTR(err
);
1388 * calculate file layout from given offset, length.
1389 * fill in correct oid, logical length, and object extent
1392 * for now, we write only a single su, until we can
1393 * pass a stride back to the caller.
1395 int ceph_calc_file_object_mapping(struct ceph_file_layout
*layout
,
1398 u64
*oxoff
, u64
*oxlen
)
1400 u32 osize
= le32_to_cpu(layout
->fl_object_size
);
1401 u32 su
= le32_to_cpu(layout
->fl_stripe_unit
);
1402 u32 sc
= le32_to_cpu(layout
->fl_stripe_count
);
1403 u32 bl
, stripeno
, stripepos
, objsetno
;
1407 dout("mapping %llu~%llu osize %u fl_su %u\n", off
, len
,
1409 if (su
== 0 || sc
== 0)
1411 su_per_object
= osize
/ su
;
1412 if (su_per_object
== 0)
1414 dout("osize %u / su %u = su_per_object %u\n", osize
, su
,
1417 if ((su
& ~PAGE_MASK
) != 0)
1420 /* bl = *off / su; */
1424 dout("off %llu / su %u = bl %u\n", off
, su
, bl
);
1427 stripepos
= bl
% sc
;
1428 objsetno
= stripeno
/ su_per_object
;
1430 *ono
= objsetno
* sc
+ stripepos
;
1431 dout("objset %u * sc %u = ono %u\n", objsetno
, sc
, (unsigned int)*ono
);
1433 /* *oxoff = *off % layout->fl_stripe_unit; # offset in su */
1435 su_offset
= do_div(t
, su
);
1436 *oxoff
= su_offset
+ (stripeno
% su_per_object
) * su
;
1439 * Calculate the length of the extent being written to the selected
1440 * object. This is the minimum of the full length requested (len) or
1441 * the remainder of the current stripe being written to.
1443 *oxlen
= min_t(u64
, len
, su
- su_offset
);
1445 dout(" obj extent %llu~%llu\n", *oxoff
, *oxlen
);
1449 dout(" invalid layout\n");
1455 EXPORT_SYMBOL(ceph_calc_file_object_mapping
);
1458 * Calculate mapping of a (oloc, oid) pair to a PG. Should only be
1459 * called with target's (oloc, oid), since tiering isn't taken into
1462 int ceph_oloc_oid_to_pg(struct ceph_osdmap
*osdmap
,
1463 struct ceph_object_locator
*oloc
,
1464 struct ceph_object_id
*oid
,
1465 struct ceph_pg
*pg_out
)
1467 struct ceph_pg_pool_info
*pi
;
1469 pi
= __lookup_pg_pool(&osdmap
->pg_pools
, oloc
->pool
);
1473 pg_out
->pool
= oloc
->pool
;
1474 pg_out
->seed
= ceph_str_hash(pi
->object_hash
, oid
->name
,
1477 dout("%s '%.*s' pgid %llu.%x\n", __func__
, oid
->name_len
, oid
->name
,
1478 pg_out
->pool
, pg_out
->seed
);
1481 EXPORT_SYMBOL(ceph_oloc_oid_to_pg
);
1483 static int do_crush(struct ceph_osdmap
*map
, int ruleno
, int x
,
1484 int *result
, int result_max
,
1485 const __u32
*weight
, int weight_max
)
1489 BUG_ON(result_max
> CEPH_PG_MAX_SIZE
);
1491 mutex_lock(&map
->crush_scratch_mutex
);
1492 r
= crush_do_rule(map
->crush
, ruleno
, x
, result
, result_max
,
1493 weight
, weight_max
, map
->crush_scratch_ary
);
1494 mutex_unlock(&map
->crush_scratch_mutex
);
1500 * Calculate raw (crush) set for given pgid.
1502 * Return raw set length, or error.
1504 static int pg_to_raw_osds(struct ceph_osdmap
*osdmap
,
1505 struct ceph_pg_pool_info
*pool
,
1506 struct ceph_pg pgid
, u32 pps
, int *osds
)
1512 ruleno
= crush_find_rule(osdmap
->crush
, pool
->crush_ruleset
,
1513 pool
->type
, pool
->size
);
1515 pr_err("no crush rule: pool %lld ruleset %d type %d size %d\n",
1516 pgid
.pool
, pool
->crush_ruleset
, pool
->type
,
1521 len
= do_crush(osdmap
, ruleno
, pps
, osds
,
1522 min_t(int, pool
->size
, CEPH_PG_MAX_SIZE
),
1523 osdmap
->osd_weight
, osdmap
->max_osd
);
1525 pr_err("error %d from crush rule %d: pool %lld ruleset %d type %d size %d\n",
1526 len
, ruleno
, pgid
.pool
, pool
->crush_ruleset
,
1527 pool
->type
, pool
->size
);
1535 * Given raw set, calculate up set and up primary.
1537 * Return up set length. *primary is set to up primary osd id, or -1
1538 * if up set is empty.
1540 static int raw_to_up_osds(struct ceph_osdmap
*osdmap
,
1541 struct ceph_pg_pool_info
*pool
,
1542 int *osds
, int len
, int *primary
)
1544 int up_primary
= -1;
1547 if (ceph_can_shift_osds(pool
)) {
1550 for (i
= 0; i
< len
; i
++) {
1551 if (ceph_osd_is_down(osdmap
, osds
[i
])) {
1556 osds
[i
- removed
] = osds
[i
];
1561 up_primary
= osds
[0];
1563 for (i
= len
- 1; i
>= 0; i
--) {
1564 if (ceph_osd_is_down(osdmap
, osds
[i
]))
1565 osds
[i
] = CRUSH_ITEM_NONE
;
1567 up_primary
= osds
[i
];
1571 *primary
= up_primary
;
1575 static void apply_primary_affinity(struct ceph_osdmap
*osdmap
, u32 pps
,
1576 struct ceph_pg_pool_info
*pool
,
1577 int *osds
, int len
, int *primary
)
1583 * Do we have any non-default primary_affinity values for these
1586 if (!osdmap
->osd_primary_affinity
)
1589 for (i
= 0; i
< len
; i
++) {
1592 if (osd
!= CRUSH_ITEM_NONE
&&
1593 osdmap
->osd_primary_affinity
[osd
] !=
1594 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY
) {
1602 * Pick the primary. Feed both the seed (for the pg) and the
1603 * osd into the hash/rng so that a proportional fraction of an
1604 * osd's pgs get rejected as primary.
1606 for (i
= 0; i
< len
; i
++) {
1610 if (osd
== CRUSH_ITEM_NONE
)
1613 aff
= osdmap
->osd_primary_affinity
[osd
];
1614 if (aff
< CEPH_OSD_MAX_PRIMARY_AFFINITY
&&
1615 (crush_hash32_2(CRUSH_HASH_RJENKINS1
,
1616 pps
, osd
) >> 16) >= aff
) {
1618 * We chose not to use this primary. Note it
1619 * anyway as a fallback in case we don't pick
1620 * anyone else, but keep looking.
1632 *primary
= osds
[pos
];
1634 if (ceph_can_shift_osds(pool
) && pos
> 0) {
1635 /* move the new primary to the front */
1636 for (i
= pos
; i
> 0; i
--)
1637 osds
[i
] = osds
[i
- 1];
1643 * Given up set, apply pg_temp and primary_temp mappings.
1645 * Return acting set length. *primary is set to acting primary osd id,
1646 * or -1 if acting set is empty.
1648 static int apply_temps(struct ceph_osdmap
*osdmap
,
1649 struct ceph_pg_pool_info
*pool
, struct ceph_pg pgid
,
1650 int *osds
, int len
, int *primary
)
1652 struct ceph_pg_mapping
*pg
;
1658 pgid
.seed
= ceph_stable_mod(pgid
.seed
, pool
->pg_num
,
1662 pg
= __lookup_pg_mapping(&osdmap
->pg_temp
, pgid
);
1667 for (i
= 0; i
< pg
->pg_temp
.len
; i
++) {
1668 if (ceph_osd_is_down(osdmap
, pg
->pg_temp
.osds
[i
])) {
1669 if (ceph_can_shift_osds(pool
))
1672 osds
[temp_len
++] = CRUSH_ITEM_NONE
;
1674 osds
[temp_len
++] = pg
->pg_temp
.osds
[i
];
1678 /* apply pg_temp's primary */
1679 for (i
= 0; i
< temp_len
; i
++) {
1680 if (osds
[i
] != CRUSH_ITEM_NONE
) {
1681 temp_primary
= osds
[i
];
1687 temp_primary
= *primary
;
1691 pg
= __lookup_pg_mapping(&osdmap
->primary_temp
, pgid
);
1693 temp_primary
= pg
->primary_temp
.osd
;
1695 *primary
= temp_primary
;
1700 * Calculate acting set for given pgid.
1702 * Return acting set length, or error. *primary is set to acting
1703 * primary osd id, or -1 if acting set is empty or on error.
1705 int ceph_calc_pg_acting(struct ceph_osdmap
*osdmap
, struct ceph_pg pgid
,
1706 int *osds
, int *primary
)
1708 struct ceph_pg_pool_info
*pool
;
1712 pool
= __lookup_pg_pool(&osdmap
->pg_pools
, pgid
.pool
);
1718 if (pool
->flags
& CEPH_POOL_FLAG_HASHPSPOOL
) {
1719 /* hash pool id and seed so that pool PGs do not overlap */
1720 pps
= crush_hash32_2(CRUSH_HASH_RJENKINS1
,
1721 ceph_stable_mod(pgid
.seed
, pool
->pgp_num
,
1722 pool
->pgp_num_mask
),
1726 * legacy behavior: add ps and pool together. this is
1727 * not a great approach because the PGs from each pool
1728 * will overlap on top of each other: 0.5 == 1.4 ==
1731 pps
= ceph_stable_mod(pgid
.seed
, pool
->pgp_num
,
1732 pool
->pgp_num_mask
) +
1733 (unsigned)pgid
.pool
;
1736 len
= pg_to_raw_osds(osdmap
, pool
, pgid
, pps
, osds
);
1742 len
= raw_to_up_osds(osdmap
, pool
, osds
, len
, primary
);
1744 apply_primary_affinity(osdmap
, pps
, pool
, osds
, len
, primary
);
1746 len
= apply_temps(osdmap
, pool
, pgid
, osds
, len
, primary
);
1752 * Return primary osd for given pgid, or -1 if none.
1754 int ceph_calc_pg_primary(struct ceph_osdmap
*osdmap
, struct ceph_pg pgid
)
1756 int osds
[CEPH_PG_MAX_SIZE
];
1759 ceph_calc_pg_acting(osdmap
, pgid
, osds
, &primary
);
1763 EXPORT_SYMBOL(ceph_calc_pg_primary
);