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",
346 c
->choose_local_tries
);
347 dout("crush decode tunable choose_local_fallback_tries = %d",
348 c
->choose_local_fallback_tries
);
349 dout("crush decode tunable choose_total_tries = %d",
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",
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",
360 c
->chooseleaf_vary_r
);
363 dout("crush_decode success\n");
369 dout("crush_decode fail %d\n", err
);
375 * rbtree of pg_mapping for handling pg_temp (explicit mapping of pgid
376 * to a set of osds) and primary_temp (explicit primary setting)
378 static int pgid_cmp(struct ceph_pg l
, struct ceph_pg r
)
391 static int __insert_pg_mapping(struct ceph_pg_mapping
*new,
392 struct rb_root
*root
)
394 struct rb_node
**p
= &root
->rb_node
;
395 struct rb_node
*parent
= NULL
;
396 struct ceph_pg_mapping
*pg
= NULL
;
399 dout("__insert_pg_mapping %llx %p\n", *(u64
*)&new->pgid
, new);
402 pg
= rb_entry(parent
, struct ceph_pg_mapping
, node
);
403 c
= pgid_cmp(new->pgid
, pg
->pgid
);
412 rb_link_node(&new->node
, parent
, p
);
413 rb_insert_color(&new->node
, root
);
417 static struct ceph_pg_mapping
*__lookup_pg_mapping(struct rb_root
*root
,
420 struct rb_node
*n
= root
->rb_node
;
421 struct ceph_pg_mapping
*pg
;
425 pg
= rb_entry(n
, struct ceph_pg_mapping
, node
);
426 c
= pgid_cmp(pgid
, pg
->pgid
);
432 dout("__lookup_pg_mapping %lld.%x got %p\n",
433 pgid
.pool
, pgid
.seed
, pg
);
440 static int __remove_pg_mapping(struct rb_root
*root
, struct ceph_pg pgid
)
442 struct ceph_pg_mapping
*pg
= __lookup_pg_mapping(root
, pgid
);
445 dout("__remove_pg_mapping %lld.%x %p\n", pgid
.pool
, pgid
.seed
,
447 rb_erase(&pg
->node
, root
);
451 dout("__remove_pg_mapping %lld.%x dne\n", pgid
.pool
, pgid
.seed
);
456 * rbtree of pg pool info
458 static int __insert_pg_pool(struct rb_root
*root
, struct ceph_pg_pool_info
*new)
460 struct rb_node
**p
= &root
->rb_node
;
461 struct rb_node
*parent
= NULL
;
462 struct ceph_pg_pool_info
*pi
= NULL
;
466 pi
= rb_entry(parent
, struct ceph_pg_pool_info
, node
);
467 if (new->id
< pi
->id
)
469 else if (new->id
> pi
->id
)
475 rb_link_node(&new->node
, parent
, p
);
476 rb_insert_color(&new->node
, root
);
480 static struct ceph_pg_pool_info
*__lookup_pg_pool(struct rb_root
*root
, u64 id
)
482 struct ceph_pg_pool_info
*pi
;
483 struct rb_node
*n
= root
->rb_node
;
486 pi
= rb_entry(n
, struct ceph_pg_pool_info
, node
);
489 else if (id
> pi
->id
)
497 struct ceph_pg_pool_info
*ceph_pg_pool_by_id(struct ceph_osdmap
*map
, u64 id
)
499 return __lookup_pg_pool(&map
->pg_pools
, id
);
502 const char *ceph_pg_pool_name_by_id(struct ceph_osdmap
*map
, u64 id
)
504 struct ceph_pg_pool_info
*pi
;
506 if (id
== CEPH_NOPOOL
)
509 if (WARN_ON_ONCE(id
> (u64
) INT_MAX
))
512 pi
= __lookup_pg_pool(&map
->pg_pools
, (int) id
);
514 return pi
? pi
->name
: NULL
;
516 EXPORT_SYMBOL(ceph_pg_pool_name_by_id
);
518 int ceph_pg_poolid_by_name(struct ceph_osdmap
*map
, const char *name
)
522 for (rbp
= rb_first(&map
->pg_pools
); rbp
; rbp
= rb_next(rbp
)) {
523 struct ceph_pg_pool_info
*pi
=
524 rb_entry(rbp
, struct ceph_pg_pool_info
, node
);
525 if (pi
->name
&& strcmp(pi
->name
, name
) == 0)
530 EXPORT_SYMBOL(ceph_pg_poolid_by_name
);
532 static void __remove_pg_pool(struct rb_root
*root
, struct ceph_pg_pool_info
*pi
)
534 rb_erase(&pi
->node
, root
);
539 static int decode_pool(void **p
, void *end
, struct ceph_pg_pool_info
*pi
)
545 ceph_decode_need(p
, end
, 2 + 4, bad
);
546 ev
= ceph_decode_8(p
); /* encoding version */
547 cv
= ceph_decode_8(p
); /* compat version */
549 pr_warn("got v %d < 5 cv %d of ceph_pg_pool\n", ev
, cv
);
553 pr_warn("got v %d cv %d > 9 of ceph_pg_pool\n", ev
, cv
);
556 len
= ceph_decode_32(p
);
557 ceph_decode_need(p
, end
, len
, bad
);
560 pi
->type
= ceph_decode_8(p
);
561 pi
->size
= ceph_decode_8(p
);
562 pi
->crush_ruleset
= ceph_decode_8(p
);
563 pi
->object_hash
= ceph_decode_8(p
);
565 pi
->pg_num
= ceph_decode_32(p
);
566 pi
->pgp_num
= ceph_decode_32(p
);
568 *p
+= 4 + 4; /* skip lpg* */
569 *p
+= 4; /* skip last_change */
570 *p
+= 8 + 4; /* skip snap_seq, snap_epoch */
573 num
= ceph_decode_32(p
);
575 *p
+= 8; /* snapid key */
576 *p
+= 1 + 1; /* versions */
577 len
= ceph_decode_32(p
);
581 /* skip removed_snaps */
582 num
= ceph_decode_32(p
);
585 *p
+= 8; /* skip auid */
586 pi
->flags
= ceph_decode_64(p
);
587 *p
+= 4; /* skip crash_replay_interval */
590 *p
+= 1; /* skip min_size */
593 *p
+= 8 + 8; /* skip quota_max_* */
597 num
= ceph_decode_32(p
);
600 *p
+= 8; /* skip tier_of */
601 *p
+= 1; /* skip cache_mode */
603 pi
->read_tier
= ceph_decode_64(p
);
604 pi
->write_tier
= ceph_decode_64(p
);
610 /* ignore the rest */
620 static int decode_pool_names(void **p
, void *end
, struct ceph_osdmap
*map
)
622 struct ceph_pg_pool_info
*pi
;
626 ceph_decode_32_safe(p
, end
, num
, bad
);
627 dout(" %d pool names\n", num
);
629 ceph_decode_64_safe(p
, end
, pool
, bad
);
630 ceph_decode_32_safe(p
, end
, len
, bad
);
631 dout(" pool %llu len %d\n", pool
, len
);
632 ceph_decode_need(p
, end
, len
, bad
);
633 pi
= __lookup_pg_pool(&map
->pg_pools
, pool
);
635 char *name
= kstrndup(*p
, len
, GFP_NOFS
);
641 dout(" name is %s\n", pi
->name
);
654 void ceph_osdmap_destroy(struct ceph_osdmap
*map
)
656 dout("osdmap_destroy %p\n", map
);
658 crush_destroy(map
->crush
);
659 while (!RB_EMPTY_ROOT(&map
->pg_temp
)) {
660 struct ceph_pg_mapping
*pg
=
661 rb_entry(rb_first(&map
->pg_temp
),
662 struct ceph_pg_mapping
, node
);
663 rb_erase(&pg
->node
, &map
->pg_temp
);
666 while (!RB_EMPTY_ROOT(&map
->primary_temp
)) {
667 struct ceph_pg_mapping
*pg
=
668 rb_entry(rb_first(&map
->primary_temp
),
669 struct ceph_pg_mapping
, node
);
670 rb_erase(&pg
->node
, &map
->primary_temp
);
673 while (!RB_EMPTY_ROOT(&map
->pg_pools
)) {
674 struct ceph_pg_pool_info
*pi
=
675 rb_entry(rb_first(&map
->pg_pools
),
676 struct ceph_pg_pool_info
, node
);
677 __remove_pg_pool(&map
->pg_pools
, pi
);
679 kfree(map
->osd_state
);
680 kfree(map
->osd_weight
);
681 kfree(map
->osd_addr
);
682 kfree(map
->osd_primary_affinity
);
687 * Adjust max_osd value, (re)allocate arrays.
689 * The new elements are properly initialized.
691 static int osdmap_set_max_osd(struct ceph_osdmap
*map
, int max
)
695 struct ceph_entity_addr
*addr
;
698 state
= krealloc(map
->osd_state
, max
*sizeof(*state
), GFP_NOFS
);
701 map
->osd_state
= state
;
703 weight
= krealloc(map
->osd_weight
, max
*sizeof(*weight
), GFP_NOFS
);
706 map
->osd_weight
= weight
;
708 addr
= krealloc(map
->osd_addr
, max
*sizeof(*addr
), GFP_NOFS
);
711 map
->osd_addr
= addr
;
713 for (i
= map
->max_osd
; i
< max
; i
++) {
714 map
->osd_state
[i
] = 0;
715 map
->osd_weight
[i
] = CEPH_OSD_OUT
;
716 memset(map
->osd_addr
+ i
, 0, sizeof(*map
->osd_addr
));
719 if (map
->osd_primary_affinity
) {
722 affinity
= krealloc(map
->osd_primary_affinity
,
723 max
*sizeof(*affinity
), GFP_NOFS
);
726 map
->osd_primary_affinity
= affinity
;
728 for (i
= map
->max_osd
; i
< max
; i
++)
729 map
->osd_primary_affinity
[i
] =
730 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY
;
738 #define OSDMAP_WRAPPER_COMPAT_VER 7
739 #define OSDMAP_CLIENT_DATA_COMPAT_VER 1
742 * Return 0 or error. On success, *v is set to 0 for old (v6) osdmaps,
743 * to struct_v of the client_data section for new (v7 and above)
746 static int get_osdmap_client_data_v(void **p
, void *end
,
747 const char *prefix
, u8
*v
)
751 ceph_decode_8_safe(p
, end
, struct_v
, e_inval
);
755 ceph_decode_8_safe(p
, end
, struct_compat
, e_inval
);
756 if (struct_compat
> OSDMAP_WRAPPER_COMPAT_VER
) {
757 pr_warn("got v %d cv %d > %d of %s ceph_osdmap\n",
758 struct_v
, struct_compat
,
759 OSDMAP_WRAPPER_COMPAT_VER
, prefix
);
762 *p
+= 4; /* ignore wrapper struct_len */
764 ceph_decode_8_safe(p
, end
, struct_v
, e_inval
);
765 ceph_decode_8_safe(p
, end
, struct_compat
, e_inval
);
766 if (struct_compat
> OSDMAP_CLIENT_DATA_COMPAT_VER
) {
767 pr_warn("got v %d cv %d > %d of %s ceph_osdmap client data\n",
768 struct_v
, struct_compat
,
769 OSDMAP_CLIENT_DATA_COMPAT_VER
, prefix
);
772 *p
+= 4; /* ignore client data struct_len */
777 ceph_decode_16_safe(p
, end
, version
, e_inval
);
779 pr_warn("got v %d < 6 of %s ceph_osdmap\n",
784 /* old osdmap enconding */
795 static int __decode_pools(void **p
, void *end
, struct ceph_osdmap
*map
,
800 ceph_decode_32_safe(p
, end
, n
, e_inval
);
802 struct ceph_pg_pool_info
*pi
;
806 ceph_decode_64_safe(p
, end
, pool
, e_inval
);
808 pi
= __lookup_pg_pool(&map
->pg_pools
, pool
);
809 if (!incremental
|| !pi
) {
810 pi
= kzalloc(sizeof(*pi
), GFP_NOFS
);
816 ret
= __insert_pg_pool(&map
->pg_pools
, pi
);
823 ret
= decode_pool(p
, end
, pi
);
834 static int decode_pools(void **p
, void *end
, struct ceph_osdmap
*map
)
836 return __decode_pools(p
, end
, map
, false);
839 static int decode_new_pools(void **p
, void *end
, struct ceph_osdmap
*map
)
841 return __decode_pools(p
, end
, map
, true);
844 static int __decode_pg_temp(void **p
, void *end
, struct ceph_osdmap
*map
,
849 ceph_decode_32_safe(p
, end
, n
, e_inval
);
855 ret
= ceph_decode_pgid(p
, end
, &pgid
);
859 ceph_decode_32_safe(p
, end
, len
, e_inval
);
861 ret
= __remove_pg_mapping(&map
->pg_temp
, pgid
);
862 BUG_ON(!incremental
&& ret
!= -ENOENT
);
864 if (!incremental
|| len
> 0) {
865 struct ceph_pg_mapping
*pg
;
867 ceph_decode_need(p
, end
, len
*sizeof(u32
), e_inval
);
869 if (len
> (UINT_MAX
- sizeof(*pg
)) / sizeof(u32
))
872 pg
= kzalloc(sizeof(*pg
) + len
*sizeof(u32
), GFP_NOFS
);
877 pg
->pg_temp
.len
= len
;
878 for (i
= 0; i
< len
; i
++)
879 pg
->pg_temp
.osds
[i
] = ceph_decode_32(p
);
881 ret
= __insert_pg_mapping(pg
, &map
->pg_temp
);
895 static int decode_pg_temp(void **p
, void *end
, struct ceph_osdmap
*map
)
897 return __decode_pg_temp(p
, end
, map
, false);
900 static int decode_new_pg_temp(void **p
, void *end
, struct ceph_osdmap
*map
)
902 return __decode_pg_temp(p
, end
, map
, true);
905 static int __decode_primary_temp(void **p
, void *end
, struct ceph_osdmap
*map
,
910 ceph_decode_32_safe(p
, end
, n
, e_inval
);
916 ret
= ceph_decode_pgid(p
, end
, &pgid
);
920 ceph_decode_32_safe(p
, end
, osd
, e_inval
);
922 ret
= __remove_pg_mapping(&map
->primary_temp
, pgid
);
923 BUG_ON(!incremental
&& ret
!= -ENOENT
);
925 if (!incremental
|| osd
!= (u32
)-1) {
926 struct ceph_pg_mapping
*pg
;
928 pg
= kzalloc(sizeof(*pg
), GFP_NOFS
);
933 pg
->primary_temp
.osd
= osd
;
935 ret
= __insert_pg_mapping(pg
, &map
->primary_temp
);
949 static int decode_primary_temp(void **p
, void *end
, struct ceph_osdmap
*map
)
951 return __decode_primary_temp(p
, end
, map
, false);
954 static int decode_new_primary_temp(void **p
, void *end
,
955 struct ceph_osdmap
*map
)
957 return __decode_primary_temp(p
, end
, map
, true);
960 u32
ceph_get_primary_affinity(struct ceph_osdmap
*map
, int osd
)
962 BUG_ON(osd
>= map
->max_osd
);
964 if (!map
->osd_primary_affinity
)
965 return CEPH_OSD_DEFAULT_PRIMARY_AFFINITY
;
967 return map
->osd_primary_affinity
[osd
];
970 static int set_primary_affinity(struct ceph_osdmap
*map
, int osd
, u32 aff
)
972 BUG_ON(osd
>= map
->max_osd
);
974 if (!map
->osd_primary_affinity
) {
977 map
->osd_primary_affinity
= kmalloc(map
->max_osd
*sizeof(u32
),
979 if (!map
->osd_primary_affinity
)
982 for (i
= 0; i
< map
->max_osd
; i
++)
983 map
->osd_primary_affinity
[i
] =
984 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY
;
987 map
->osd_primary_affinity
[osd
] = aff
;
992 static int decode_primary_affinity(void **p
, void *end
,
993 struct ceph_osdmap
*map
)
997 ceph_decode_32_safe(p
, end
, len
, e_inval
);
999 kfree(map
->osd_primary_affinity
);
1000 map
->osd_primary_affinity
= NULL
;
1003 if (len
!= map
->max_osd
)
1006 ceph_decode_need(p
, end
, map
->max_osd
*sizeof(u32
), e_inval
);
1008 for (i
= 0; i
< map
->max_osd
; i
++) {
1011 ret
= set_primary_affinity(map
, i
, ceph_decode_32(p
));
1022 static int decode_new_primary_affinity(void **p
, void *end
,
1023 struct ceph_osdmap
*map
)
1027 ceph_decode_32_safe(p
, end
, n
, e_inval
);
1032 ceph_decode_32_safe(p
, end
, osd
, e_inval
);
1033 ceph_decode_32_safe(p
, end
, aff
, e_inval
);
1035 ret
= set_primary_affinity(map
, osd
, aff
);
1039 pr_info("osd%d primary-affinity 0x%x\n", osd
, aff
);
1049 * decode a full map.
1051 static int osdmap_decode(void **p
, void *end
, struct ceph_osdmap
*map
)
1060 dout("%s %p to %p len %d\n", __func__
, *p
, end
, (int)(end
- *p
));
1062 err
= get_osdmap_client_data_v(p
, end
, "full", &struct_v
);
1066 /* fsid, epoch, created, modified */
1067 ceph_decode_need(p
, end
, sizeof(map
->fsid
) + sizeof(u32
) +
1068 sizeof(map
->created
) + sizeof(map
->modified
), e_inval
);
1069 ceph_decode_copy(p
, &map
->fsid
, sizeof(map
->fsid
));
1070 epoch
= map
->epoch
= ceph_decode_32(p
);
1071 ceph_decode_copy(p
, &map
->created
, sizeof(map
->created
));
1072 ceph_decode_copy(p
, &map
->modified
, sizeof(map
->modified
));
1075 err
= decode_pools(p
, end
, map
);
1080 err
= decode_pool_names(p
, end
, map
);
1084 ceph_decode_32_safe(p
, end
, map
->pool_max
, e_inval
);
1086 ceph_decode_32_safe(p
, end
, map
->flags
, e_inval
);
1089 ceph_decode_32_safe(p
, end
, max
, e_inval
);
1091 /* (re)alloc osd arrays */
1092 err
= osdmap_set_max_osd(map
, max
);
1096 /* osd_state, osd_weight, osd_addrs->client_addr */
1097 ceph_decode_need(p
, end
, 3*sizeof(u32
) +
1098 map
->max_osd
*(1 + sizeof(*map
->osd_weight
) +
1099 sizeof(*map
->osd_addr
)), e_inval
);
1101 if (ceph_decode_32(p
) != map
->max_osd
)
1104 ceph_decode_copy(p
, map
->osd_state
, map
->max_osd
);
1106 if (ceph_decode_32(p
) != map
->max_osd
)
1109 for (i
= 0; i
< map
->max_osd
; i
++)
1110 map
->osd_weight
[i
] = ceph_decode_32(p
);
1112 if (ceph_decode_32(p
) != map
->max_osd
)
1115 ceph_decode_copy(p
, map
->osd_addr
, map
->max_osd
*sizeof(*map
->osd_addr
));
1116 for (i
= 0; i
< map
->max_osd
; i
++)
1117 ceph_decode_addr(&map
->osd_addr
[i
]);
1120 err
= decode_pg_temp(p
, end
, map
);
1125 if (struct_v
>= 1) {
1126 err
= decode_primary_temp(p
, end
, map
);
1131 /* primary_affinity */
1132 if (struct_v
>= 2) {
1133 err
= decode_primary_affinity(p
, end
, map
);
1137 /* XXX can this happen? */
1138 kfree(map
->osd_primary_affinity
);
1139 map
->osd_primary_affinity
= NULL
;
1143 ceph_decode_32_safe(p
, end
, len
, e_inval
);
1144 map
->crush
= crush_decode(*p
, min(*p
+ len
, end
));
1145 if (IS_ERR(map
->crush
)) {
1146 err
= PTR_ERR(map
->crush
);
1152 /* ignore the rest */
1155 dout("full osdmap epoch %d max_osd %d\n", map
->epoch
, map
->max_osd
);
1161 pr_err("corrupt full osdmap (%d) epoch %d off %d (%p of %p-%p)\n",
1162 err
, epoch
, (int)(*p
- start
), *p
, start
, end
);
1163 print_hex_dump(KERN_DEBUG
, "osdmap: ",
1164 DUMP_PREFIX_OFFSET
, 16, 1,
1165 start
, end
- start
, true);
1170 * Allocate and decode a full map.
1172 struct ceph_osdmap
*ceph_osdmap_decode(void **p
, void *end
)
1174 struct ceph_osdmap
*map
;
1177 map
= kzalloc(sizeof(*map
), GFP_NOFS
);
1179 return ERR_PTR(-ENOMEM
);
1181 map
->pg_temp
= RB_ROOT
;
1182 map
->primary_temp
= RB_ROOT
;
1183 mutex_init(&map
->crush_scratch_mutex
);
1185 ret
= osdmap_decode(p
, end
, map
);
1187 ceph_osdmap_destroy(map
);
1188 return ERR_PTR(ret
);
1195 * decode and apply an incremental map update.
1197 struct ceph_osdmap
*osdmap_apply_incremental(void **p
, void *end
,
1198 struct ceph_osdmap
*map
,
1199 struct ceph_messenger
*msgr
)
1201 struct crush_map
*newcrush
= NULL
;
1202 struct ceph_fsid fsid
;
1204 struct ceph_timespec modified
;
1208 __s32 new_flags
, max
;
1213 dout("%s %p to %p len %d\n", __func__
, *p
, end
, (int)(end
- *p
));
1215 err
= get_osdmap_client_data_v(p
, end
, "inc", &struct_v
);
1219 /* fsid, epoch, modified, new_pool_max, new_flags */
1220 ceph_decode_need(p
, end
, sizeof(fsid
) + sizeof(u32
) + sizeof(modified
) +
1221 sizeof(u64
) + sizeof(u32
), e_inval
);
1222 ceph_decode_copy(p
, &fsid
, sizeof(fsid
));
1223 epoch
= ceph_decode_32(p
);
1224 BUG_ON(epoch
!= map
->epoch
+1);
1225 ceph_decode_copy(p
, &modified
, sizeof(modified
));
1226 new_pool_max
= ceph_decode_64(p
);
1227 new_flags
= ceph_decode_32(p
);
1230 ceph_decode_32_safe(p
, end
, len
, e_inval
);
1232 dout("apply_incremental full map len %d, %p to %p\n",
1234 return ceph_osdmap_decode(p
, min(*p
+len
, end
));
1238 ceph_decode_32_safe(p
, end
, len
, e_inval
);
1240 newcrush
= crush_decode(*p
, min(*p
+len
, end
));
1241 if (IS_ERR(newcrush
)) {
1242 err
= PTR_ERR(newcrush
);
1251 map
->flags
= new_flags
;
1252 if (new_pool_max
>= 0)
1253 map
->pool_max
= new_pool_max
;
1256 ceph_decode_32_safe(p
, end
, max
, e_inval
);
1258 err
= osdmap_set_max_osd(map
, max
);
1264 map
->modified
= modified
;
1267 crush_destroy(map
->crush
);
1268 map
->crush
= newcrush
;
1273 err
= decode_new_pools(p
, end
, map
);
1277 /* new_pool_names */
1278 err
= decode_pool_names(p
, end
, map
);
1283 ceph_decode_32_safe(p
, end
, len
, e_inval
);
1285 struct ceph_pg_pool_info
*pi
;
1287 ceph_decode_64_safe(p
, end
, pool
, e_inval
);
1288 pi
= __lookup_pg_pool(&map
->pg_pools
, pool
);
1290 __remove_pg_pool(&map
->pg_pools
, pi
);
1294 ceph_decode_32_safe(p
, end
, len
, e_inval
);
1297 struct ceph_entity_addr addr
;
1298 ceph_decode_32_safe(p
, end
, osd
, e_inval
);
1299 ceph_decode_copy_safe(p
, end
, &addr
, sizeof(addr
), e_inval
);
1300 ceph_decode_addr(&addr
);
1301 pr_info("osd%d up\n", osd
);
1302 BUG_ON(osd
>= map
->max_osd
);
1303 map
->osd_state
[osd
] |= CEPH_OSD_UP
| CEPH_OSD_EXISTS
;
1304 map
->osd_addr
[osd
] = addr
;
1308 ceph_decode_32_safe(p
, end
, len
, e_inval
);
1312 ceph_decode_32_safe(p
, end
, osd
, e_inval
);
1313 xorstate
= **(u8
**)p
;
1314 (*p
)++; /* clean flag */
1316 xorstate
= CEPH_OSD_UP
;
1317 if (xorstate
& CEPH_OSD_UP
)
1318 pr_info("osd%d down\n", osd
);
1319 if (osd
< map
->max_osd
)
1320 map
->osd_state
[osd
] ^= xorstate
;
1324 ceph_decode_32_safe(p
, end
, len
, e_inval
);
1327 ceph_decode_need(p
, end
, sizeof(u32
)*2, e_inval
);
1328 osd
= ceph_decode_32(p
);
1329 off
= ceph_decode_32(p
);
1330 pr_info("osd%d weight 0x%x %s\n", osd
, off
,
1331 off
== CEPH_OSD_IN
? "(in)" :
1332 (off
== CEPH_OSD_OUT
? "(out)" : ""));
1333 if (osd
< map
->max_osd
)
1334 map
->osd_weight
[osd
] = off
;
1338 err
= decode_new_pg_temp(p
, end
, map
);
1342 /* new_primary_temp */
1343 if (struct_v
>= 1) {
1344 err
= decode_new_primary_temp(p
, end
, map
);
1349 /* new_primary_affinity */
1350 if (struct_v
>= 2) {
1351 err
= decode_new_primary_affinity(p
, end
, map
);
1356 /* ignore the rest */
1359 dout("inc osdmap epoch %d max_osd %d\n", map
->epoch
, map
->max_osd
);
1365 pr_err("corrupt inc osdmap (%d) epoch %d off %d (%p of %p-%p)\n",
1366 err
, epoch
, (int)(*p
- start
), *p
, start
, end
);
1367 print_hex_dump(KERN_DEBUG
, "osdmap: ",
1368 DUMP_PREFIX_OFFSET
, 16, 1,
1369 start
, end
- start
, true);
1371 crush_destroy(newcrush
);
1372 return ERR_PTR(err
);
1379 * calculate file layout from given offset, length.
1380 * fill in correct oid, logical length, and object extent
1383 * for now, we write only a single su, until we can
1384 * pass a stride back to the caller.
1386 int ceph_calc_file_object_mapping(struct ceph_file_layout
*layout
,
1389 u64
*oxoff
, u64
*oxlen
)
1391 u32 osize
= le32_to_cpu(layout
->fl_object_size
);
1392 u32 su
= le32_to_cpu(layout
->fl_stripe_unit
);
1393 u32 sc
= le32_to_cpu(layout
->fl_stripe_count
);
1394 u32 bl
, stripeno
, stripepos
, objsetno
;
1398 dout("mapping %llu~%llu osize %u fl_su %u\n", off
, len
,
1400 if (su
== 0 || sc
== 0)
1402 su_per_object
= osize
/ su
;
1403 if (su_per_object
== 0)
1405 dout("osize %u / su %u = su_per_object %u\n", osize
, su
,
1408 if ((su
& ~PAGE_MASK
) != 0)
1411 /* bl = *off / su; */
1415 dout("off %llu / su %u = bl %u\n", off
, su
, bl
);
1418 stripepos
= bl
% sc
;
1419 objsetno
= stripeno
/ su_per_object
;
1421 *ono
= objsetno
* sc
+ stripepos
;
1422 dout("objset %u * sc %u = ono %u\n", objsetno
, sc
, (unsigned int)*ono
);
1424 /* *oxoff = *off % layout->fl_stripe_unit; # offset in su */
1426 su_offset
= do_div(t
, su
);
1427 *oxoff
= su_offset
+ (stripeno
% su_per_object
) * su
;
1430 * Calculate the length of the extent being written to the selected
1431 * object. This is the minimum of the full length requested (len) or
1432 * the remainder of the current stripe being written to.
1434 *oxlen
= min_t(u64
, len
, su
- su_offset
);
1436 dout(" obj extent %llu~%llu\n", *oxoff
, *oxlen
);
1440 dout(" invalid layout\n");
1446 EXPORT_SYMBOL(ceph_calc_file_object_mapping
);
1449 * Calculate mapping of a (oloc, oid) pair to a PG. Should only be
1450 * called with target's (oloc, oid), since tiering isn't taken into
1453 int ceph_oloc_oid_to_pg(struct ceph_osdmap
*osdmap
,
1454 struct ceph_object_locator
*oloc
,
1455 struct ceph_object_id
*oid
,
1456 struct ceph_pg
*pg_out
)
1458 struct ceph_pg_pool_info
*pi
;
1460 pi
= __lookup_pg_pool(&osdmap
->pg_pools
, oloc
->pool
);
1464 pg_out
->pool
= oloc
->pool
;
1465 pg_out
->seed
= ceph_str_hash(pi
->object_hash
, oid
->name
,
1468 dout("%s '%.*s' pgid %llu.%x\n", __func__
, oid
->name_len
, oid
->name
,
1469 pg_out
->pool
, pg_out
->seed
);
1472 EXPORT_SYMBOL(ceph_oloc_oid_to_pg
);
1474 static int do_crush(struct ceph_osdmap
*map
, int ruleno
, int x
,
1475 int *result
, int result_max
,
1476 const __u32
*weight
, int weight_max
)
1480 BUG_ON(result_max
> CEPH_PG_MAX_SIZE
);
1482 mutex_lock(&map
->crush_scratch_mutex
);
1483 r
= crush_do_rule(map
->crush
, ruleno
, x
, result
, result_max
,
1484 weight
, weight_max
, map
->crush_scratch_ary
);
1485 mutex_unlock(&map
->crush_scratch_mutex
);
1491 * Calculate raw (crush) set for given pgid.
1493 * Return raw set length, or error.
1495 static int pg_to_raw_osds(struct ceph_osdmap
*osdmap
,
1496 struct ceph_pg_pool_info
*pool
,
1497 struct ceph_pg pgid
, u32 pps
, int *osds
)
1503 ruleno
= crush_find_rule(osdmap
->crush
, pool
->crush_ruleset
,
1504 pool
->type
, pool
->size
);
1506 pr_err("no crush rule: pool %lld ruleset %d type %d size %d\n",
1507 pgid
.pool
, pool
->crush_ruleset
, pool
->type
,
1512 len
= do_crush(osdmap
, ruleno
, pps
, osds
,
1513 min_t(int, pool
->size
, CEPH_PG_MAX_SIZE
),
1514 osdmap
->osd_weight
, osdmap
->max_osd
);
1516 pr_err("error %d from crush rule %d: pool %lld ruleset %d type %d size %d\n",
1517 len
, ruleno
, pgid
.pool
, pool
->crush_ruleset
,
1518 pool
->type
, pool
->size
);
1526 * Given raw set, calculate up set and up primary.
1528 * Return up set length. *primary is set to up primary osd id, or -1
1529 * if up set is empty.
1531 static int raw_to_up_osds(struct ceph_osdmap
*osdmap
,
1532 struct ceph_pg_pool_info
*pool
,
1533 int *osds
, int len
, int *primary
)
1535 int up_primary
= -1;
1538 if (ceph_can_shift_osds(pool
)) {
1541 for (i
= 0; i
< len
; i
++) {
1542 if (ceph_osd_is_down(osdmap
, osds
[i
])) {
1547 osds
[i
- removed
] = osds
[i
];
1552 up_primary
= osds
[0];
1554 for (i
= len
- 1; i
>= 0; i
--) {
1555 if (ceph_osd_is_down(osdmap
, osds
[i
]))
1556 osds
[i
] = CRUSH_ITEM_NONE
;
1558 up_primary
= osds
[i
];
1562 *primary
= up_primary
;
1566 static void apply_primary_affinity(struct ceph_osdmap
*osdmap
, u32 pps
,
1567 struct ceph_pg_pool_info
*pool
,
1568 int *osds
, int len
, int *primary
)
1574 * Do we have any non-default primary_affinity values for these
1577 if (!osdmap
->osd_primary_affinity
)
1580 for (i
= 0; i
< len
; i
++) {
1583 if (osd
!= CRUSH_ITEM_NONE
&&
1584 osdmap
->osd_primary_affinity
[osd
] !=
1585 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY
) {
1593 * Pick the primary. Feed both the seed (for the pg) and the
1594 * osd into the hash/rng so that a proportional fraction of an
1595 * osd's pgs get rejected as primary.
1597 for (i
= 0; i
< len
; i
++) {
1601 if (osd
== CRUSH_ITEM_NONE
)
1604 aff
= osdmap
->osd_primary_affinity
[osd
];
1605 if (aff
< CEPH_OSD_MAX_PRIMARY_AFFINITY
&&
1606 (crush_hash32_2(CRUSH_HASH_RJENKINS1
,
1607 pps
, osd
) >> 16) >= aff
) {
1609 * We chose not to use this primary. Note it
1610 * anyway as a fallback in case we don't pick
1611 * anyone else, but keep looking.
1623 *primary
= osds
[pos
];
1625 if (ceph_can_shift_osds(pool
) && pos
> 0) {
1626 /* move the new primary to the front */
1627 for (i
= pos
; i
> 0; i
--)
1628 osds
[i
] = osds
[i
- 1];
1634 * Given up set, apply pg_temp and primary_temp mappings.
1636 * Return acting set length. *primary is set to acting primary osd id,
1637 * or -1 if acting set is empty.
1639 static int apply_temps(struct ceph_osdmap
*osdmap
,
1640 struct ceph_pg_pool_info
*pool
, struct ceph_pg pgid
,
1641 int *osds
, int len
, int *primary
)
1643 struct ceph_pg_mapping
*pg
;
1649 pgid
.seed
= ceph_stable_mod(pgid
.seed
, pool
->pg_num
,
1653 pg
= __lookup_pg_mapping(&osdmap
->pg_temp
, pgid
);
1658 for (i
= 0; i
< pg
->pg_temp
.len
; i
++) {
1659 if (ceph_osd_is_down(osdmap
, pg
->pg_temp
.osds
[i
])) {
1660 if (ceph_can_shift_osds(pool
))
1663 osds
[temp_len
++] = CRUSH_ITEM_NONE
;
1665 osds
[temp_len
++] = pg
->pg_temp
.osds
[i
];
1669 /* apply pg_temp's primary */
1670 for (i
= 0; i
< temp_len
; i
++) {
1671 if (osds
[i
] != CRUSH_ITEM_NONE
) {
1672 temp_primary
= osds
[i
];
1678 temp_primary
= *primary
;
1682 pg
= __lookup_pg_mapping(&osdmap
->primary_temp
, pgid
);
1684 temp_primary
= pg
->primary_temp
.osd
;
1686 *primary
= temp_primary
;
1691 * Calculate acting set for given pgid.
1693 * Return acting set length, or error. *primary is set to acting
1694 * primary osd id, or -1 if acting set is empty or on error.
1696 int ceph_calc_pg_acting(struct ceph_osdmap
*osdmap
, struct ceph_pg pgid
,
1697 int *osds
, int *primary
)
1699 struct ceph_pg_pool_info
*pool
;
1703 pool
= __lookup_pg_pool(&osdmap
->pg_pools
, pgid
.pool
);
1709 if (pool
->flags
& CEPH_POOL_FLAG_HASHPSPOOL
) {
1710 /* hash pool id and seed so that pool PGs do not overlap */
1711 pps
= crush_hash32_2(CRUSH_HASH_RJENKINS1
,
1712 ceph_stable_mod(pgid
.seed
, pool
->pgp_num
,
1713 pool
->pgp_num_mask
),
1717 * legacy behavior: add ps and pool together. this is
1718 * not a great approach because the PGs from each pool
1719 * will overlap on top of each other: 0.5 == 1.4 ==
1722 pps
= ceph_stable_mod(pgid
.seed
, pool
->pgp_num
,
1723 pool
->pgp_num_mask
) +
1724 (unsigned)pgid
.pool
;
1727 len
= pg_to_raw_osds(osdmap
, pool
, pgid
, pps
, osds
);
1733 len
= raw_to_up_osds(osdmap
, pool
, osds
, len
, primary
);
1735 apply_primary_affinity(osdmap
, pps
, pool
, osds
, len
, primary
);
1737 len
= apply_temps(osdmap
, pool
, pgid
, osds
, len
, primary
);
1743 * Return primary osd for given pgid, or -1 if none.
1745 int ceph_calc_pg_primary(struct ceph_osdmap
*osdmap
, struct ceph_pg pgid
)
1747 int osds
[CEPH_PG_MAX_SIZE
];
1750 ceph_calc_pg_acting(osdmap
, pgid
, osds
, &primary
);
1754 EXPORT_SYMBOL(ceph_calc_pg_primary
);