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
)
23 if (state
& CEPH_OSD_EXISTS
) {
24 snprintf(str
, len
, "exists");
27 if (state
& CEPH_OSD_UP
) {
28 snprintf(str
, len
, "%s%s%s", str
, (flag
? ", " : ""),
33 snprintf(str
, len
, "doesn't exist");
41 static int calc_bits_of(unsigned int t
)
52 * the foo_mask is the smallest value 2^n-1 that is >= foo.
54 static void calc_pg_masks(struct ceph_pg_pool_info
*pi
)
56 pi
->pg_num_mask
= (1 << calc_bits_of(le32_to_cpu(pi
->v
.pg_num
)-1)) - 1;
58 (1 << calc_bits_of(le32_to_cpu(pi
->v
.pgp_num
)-1)) - 1;
60 (1 << calc_bits_of(le32_to_cpu(pi
->v
.lpg_num
)-1)) - 1;
62 (1 << calc_bits_of(le32_to_cpu(pi
->v
.lpgp_num
)-1)) - 1;
68 static int crush_decode_uniform_bucket(void **p
, void *end
,
69 struct crush_bucket_uniform
*b
)
71 dout("crush_decode_uniform_bucket %p to %p\n", *p
, end
);
72 ceph_decode_need(p
, end
, (1+b
->h
.size
) * sizeof(u32
), bad
);
73 b
->item_weight
= ceph_decode_32(p
);
79 static int crush_decode_list_bucket(void **p
, void *end
,
80 struct crush_bucket_list
*b
)
83 dout("crush_decode_list_bucket %p to %p\n", *p
, end
);
84 b
->item_weights
= kcalloc(b
->h
.size
, sizeof(u32
), GFP_NOFS
);
85 if (b
->item_weights
== NULL
)
87 b
->sum_weights
= kcalloc(b
->h
.size
, sizeof(u32
), GFP_NOFS
);
88 if (b
->sum_weights
== NULL
)
90 ceph_decode_need(p
, end
, 2 * b
->h
.size
* sizeof(u32
), bad
);
91 for (j
= 0; j
< b
->h
.size
; j
++) {
92 b
->item_weights
[j
] = ceph_decode_32(p
);
93 b
->sum_weights
[j
] = ceph_decode_32(p
);
100 static int crush_decode_tree_bucket(void **p
, void *end
,
101 struct crush_bucket_tree
*b
)
104 dout("crush_decode_tree_bucket %p to %p\n", *p
, end
);
105 ceph_decode_32_safe(p
, end
, b
->num_nodes
, bad
);
106 b
->node_weights
= kcalloc(b
->num_nodes
, sizeof(u32
), GFP_NOFS
);
107 if (b
->node_weights
== NULL
)
109 ceph_decode_need(p
, end
, b
->num_nodes
* sizeof(u32
), bad
);
110 for (j
= 0; j
< b
->num_nodes
; j
++)
111 b
->node_weights
[j
] = ceph_decode_32(p
);
117 static int crush_decode_straw_bucket(void **p
, void *end
,
118 struct crush_bucket_straw
*b
)
121 dout("crush_decode_straw_bucket %p to %p\n", *p
, end
);
122 b
->item_weights
= kcalloc(b
->h
.size
, sizeof(u32
), GFP_NOFS
);
123 if (b
->item_weights
== NULL
)
125 b
->straws
= kcalloc(b
->h
.size
, sizeof(u32
), GFP_NOFS
);
126 if (b
->straws
== NULL
)
128 ceph_decode_need(p
, end
, 2 * b
->h
.size
* sizeof(u32
), bad
);
129 for (j
= 0; j
< b
->h
.size
; j
++) {
130 b
->item_weights
[j
] = ceph_decode_32(p
);
131 b
->straws
[j
] = ceph_decode_32(p
);
138 static int skip_name_map(void **p
, void *end
)
141 ceph_decode_32_safe(p
, end
, len
,bad
);
145 ceph_decode_32_safe(p
, end
, strlen
, bad
);
153 static struct crush_map
*crush_decode(void *pbyval
, void *end
)
159 void *start
= pbyval
;
163 dout("crush_decode %p to %p len %d\n", *p
, end
, (int)(end
- *p
));
165 c
= kzalloc(sizeof(*c
), GFP_NOFS
);
167 return ERR_PTR(-ENOMEM
);
169 /* set tunables to default values */
170 c
->choose_local_tries
= 2;
171 c
->choose_local_fallback_tries
= 5;
172 c
->choose_total_tries
= 19;
174 ceph_decode_need(p
, end
, 4*sizeof(u32
), bad
);
175 magic
= ceph_decode_32(p
);
176 if (magic
!= CRUSH_MAGIC
) {
177 pr_err("crush_decode magic %x != current %x\n",
178 (unsigned int)magic
, (unsigned int)CRUSH_MAGIC
);
181 c
->max_buckets
= ceph_decode_32(p
);
182 c
->max_rules
= ceph_decode_32(p
);
183 c
->max_devices
= ceph_decode_32(p
);
185 c
->buckets
= kcalloc(c
->max_buckets
, sizeof(*c
->buckets
), GFP_NOFS
);
186 if (c
->buckets
== NULL
)
188 c
->rules
= kcalloc(c
->max_rules
, sizeof(*c
->rules
), GFP_NOFS
);
189 if (c
->rules
== NULL
)
193 for (i
= 0; i
< c
->max_buckets
; i
++) {
196 struct crush_bucket
*b
;
198 ceph_decode_32_safe(p
, end
, alg
, bad
);
200 c
->buckets
[i
] = NULL
;
203 dout("crush_decode bucket %d off %x %p to %p\n",
204 i
, (int)(*p
-start
), *p
, end
);
207 case CRUSH_BUCKET_UNIFORM
:
208 size
= sizeof(struct crush_bucket_uniform
);
210 case CRUSH_BUCKET_LIST
:
211 size
= sizeof(struct crush_bucket_list
);
213 case CRUSH_BUCKET_TREE
:
214 size
= sizeof(struct crush_bucket_tree
);
216 case CRUSH_BUCKET_STRAW
:
217 size
= sizeof(struct crush_bucket_straw
);
224 b
= c
->buckets
[i
] = kzalloc(size
, GFP_NOFS
);
228 ceph_decode_need(p
, end
, 4*sizeof(u32
), bad
);
229 b
->id
= ceph_decode_32(p
);
230 b
->type
= ceph_decode_16(p
);
231 b
->alg
= ceph_decode_8(p
);
232 b
->hash
= ceph_decode_8(p
);
233 b
->weight
= ceph_decode_32(p
);
234 b
->size
= ceph_decode_32(p
);
236 dout("crush_decode bucket size %d off %x %p to %p\n",
237 b
->size
, (int)(*p
-start
), *p
, end
);
239 b
->items
= kcalloc(b
->size
, sizeof(__s32
), GFP_NOFS
);
240 if (b
->items
== NULL
)
242 b
->perm
= kcalloc(b
->size
, sizeof(u32
), GFP_NOFS
);
247 ceph_decode_need(p
, end
, b
->size
*sizeof(u32
), bad
);
248 for (j
= 0; j
< b
->size
; j
++)
249 b
->items
[j
] = ceph_decode_32(p
);
252 case CRUSH_BUCKET_UNIFORM
:
253 err
= crush_decode_uniform_bucket(p
, end
,
254 (struct crush_bucket_uniform
*)b
);
258 case CRUSH_BUCKET_LIST
:
259 err
= crush_decode_list_bucket(p
, end
,
260 (struct crush_bucket_list
*)b
);
264 case CRUSH_BUCKET_TREE
:
265 err
= crush_decode_tree_bucket(p
, end
,
266 (struct crush_bucket_tree
*)b
);
270 case CRUSH_BUCKET_STRAW
:
271 err
= crush_decode_straw_bucket(p
, end
,
272 (struct crush_bucket_straw
*)b
);
280 dout("rule vec is %p\n", c
->rules
);
281 for (i
= 0; i
< c
->max_rules
; i
++) {
283 struct crush_rule
*r
;
285 ceph_decode_32_safe(p
, end
, yes
, bad
);
287 dout("crush_decode NO rule %d off %x %p to %p\n",
288 i
, (int)(*p
-start
), *p
, end
);
293 dout("crush_decode rule %d off %x %p to %p\n",
294 i
, (int)(*p
-start
), *p
, end
);
297 ceph_decode_32_safe(p
, end
, yes
, bad
);
298 #if BITS_PER_LONG == 32
300 if (yes
> (ULONG_MAX
- sizeof(*r
))
301 / sizeof(struct crush_rule_step
))
304 r
= c
->rules
[i
] = kmalloc(sizeof(*r
) +
305 yes
*sizeof(struct crush_rule_step
),
309 dout(" rule %d is at %p\n", i
, r
);
311 ceph_decode_copy_safe(p
, end
, &r
->mask
, 4, bad
); /* 4 u8's */
312 ceph_decode_need(p
, end
, r
->len
*3*sizeof(u32
), bad
);
313 for (j
= 0; j
< r
->len
; j
++) {
314 r
->steps
[j
].op
= ceph_decode_32(p
);
315 r
->steps
[j
].arg1
= ceph_decode_32(p
);
316 r
->steps
[j
].arg2
= ceph_decode_32(p
);
320 /* ignore trailing name maps. */
321 for (num_name_maps
= 0; num_name_maps
< 3; num_name_maps
++) {
322 err
= skip_name_map(p
, end
);
328 ceph_decode_need(p
, end
, 3*sizeof(u32
), done
);
329 c
->choose_local_tries
= ceph_decode_32(p
);
330 c
->choose_local_fallback_tries
= ceph_decode_32(p
);
331 c
->choose_total_tries
= ceph_decode_32(p
);
332 dout("crush decode tunable choose_local_tries = %d",
333 c
->choose_local_tries
);
334 dout("crush decode tunable choose_local_fallback_tries = %d",
335 c
->choose_local_fallback_tries
);
336 dout("crush decode tunable choose_total_tries = %d",
337 c
->choose_total_tries
);
340 dout("crush_decode success\n");
346 dout("crush_decode fail %d\n", err
);
352 * rbtree of pg_mapping for handling pg_temp (explicit mapping of pgid
355 static int pgid_cmp(struct ceph_pg l
, struct ceph_pg r
)
367 static int __insert_pg_mapping(struct ceph_pg_mapping
*new,
368 struct rb_root
*root
)
370 struct rb_node
**p
= &root
->rb_node
;
371 struct rb_node
*parent
= NULL
;
372 struct ceph_pg_mapping
*pg
= NULL
;
375 dout("__insert_pg_mapping %llx %p\n", *(u64
*)&new->pgid
, new);
378 pg
= rb_entry(parent
, struct ceph_pg_mapping
, node
);
379 c
= pgid_cmp(new->pgid
, pg
->pgid
);
388 rb_link_node(&new->node
, parent
, p
);
389 rb_insert_color(&new->node
, root
);
393 static struct ceph_pg_mapping
*__lookup_pg_mapping(struct rb_root
*root
,
396 struct rb_node
*n
= root
->rb_node
;
397 struct ceph_pg_mapping
*pg
;
401 pg
= rb_entry(n
, struct ceph_pg_mapping
, node
);
402 c
= pgid_cmp(pgid
, pg
->pgid
);
408 dout("__lookup_pg_mapping %llx got %p\n",
416 static int __remove_pg_mapping(struct rb_root
*root
, struct ceph_pg pgid
)
418 struct ceph_pg_mapping
*pg
= __lookup_pg_mapping(root
, pgid
);
421 dout("__remove_pg_mapping %llx %p\n", *(u64
*)&pgid
, pg
);
422 rb_erase(&pg
->node
, root
);
426 dout("__remove_pg_mapping %llx dne\n", *(u64
*)&pgid
);
431 * rbtree of pg pool info
433 static int __insert_pg_pool(struct rb_root
*root
, struct ceph_pg_pool_info
*new)
435 struct rb_node
**p
= &root
->rb_node
;
436 struct rb_node
*parent
= NULL
;
437 struct ceph_pg_pool_info
*pi
= NULL
;
441 pi
= rb_entry(parent
, struct ceph_pg_pool_info
, node
);
442 if (new->id
< pi
->id
)
444 else if (new->id
> pi
->id
)
450 rb_link_node(&new->node
, parent
, p
);
451 rb_insert_color(&new->node
, root
);
455 static struct ceph_pg_pool_info
*__lookup_pg_pool(struct rb_root
*root
, int id
)
457 struct ceph_pg_pool_info
*pi
;
458 struct rb_node
*n
= root
->rb_node
;
461 pi
= rb_entry(n
, struct ceph_pg_pool_info
, node
);
464 else if (id
> pi
->id
)
472 const char *ceph_pg_pool_name_by_id(struct ceph_osdmap
*map
, u64 id
)
474 struct ceph_pg_pool_info
*pi
;
476 if (id
== CEPH_NOPOOL
)
479 if (WARN_ON_ONCE(id
> (u64
) INT_MAX
))
482 pi
= __lookup_pg_pool(&map
->pg_pools
, (int) id
);
484 return pi
? pi
->name
: NULL
;
486 EXPORT_SYMBOL(ceph_pg_pool_name_by_id
);
488 int ceph_pg_poolid_by_name(struct ceph_osdmap
*map
, const char *name
)
492 for (rbp
= rb_first(&map
->pg_pools
); rbp
; rbp
= rb_next(rbp
)) {
493 struct ceph_pg_pool_info
*pi
=
494 rb_entry(rbp
, struct ceph_pg_pool_info
, node
);
495 if (pi
->name
&& strcmp(pi
->name
, name
) == 0)
500 EXPORT_SYMBOL(ceph_pg_poolid_by_name
);
502 static void __remove_pg_pool(struct rb_root
*root
, struct ceph_pg_pool_info
*pi
)
504 rb_erase(&pi
->node
, root
);
509 static int __decode_pool(void **p
, void *end
, struct ceph_pg_pool_info
*pi
)
513 ceph_decode_copy(p
, &pi
->v
, sizeof(pi
->v
));
516 /* num_snaps * snap_info_t */
517 n
= le32_to_cpu(pi
->v
.num_snaps
);
519 ceph_decode_need(p
, end
, sizeof(u64
) + 1 + sizeof(u64
) +
520 sizeof(struct ceph_timespec
), bad
);
521 *p
+= sizeof(u64
) + /* key */
522 1 + sizeof(u64
) + /* u8, snapid */
523 sizeof(struct ceph_timespec
);
524 m
= ceph_decode_32(p
); /* snap name */
528 *p
+= le32_to_cpu(pi
->v
.num_removed_snap_intervals
) * sizeof(u64
) * 2;
535 static int __decode_pool_names(void **p
, void *end
, struct ceph_osdmap
*map
)
537 struct ceph_pg_pool_info
*pi
;
540 ceph_decode_32_safe(p
, end
, num
, bad
);
541 dout(" %d pool names\n", num
);
543 ceph_decode_32_safe(p
, end
, pool
, bad
);
544 ceph_decode_32_safe(p
, end
, len
, bad
);
545 dout(" pool %d len %d\n", pool
, len
);
546 ceph_decode_need(p
, end
, len
, bad
);
547 pi
= __lookup_pg_pool(&map
->pg_pools
, pool
);
549 char *name
= kstrndup(*p
, len
, GFP_NOFS
);
555 dout(" name is %s\n", pi
->name
);
568 void ceph_osdmap_destroy(struct ceph_osdmap
*map
)
570 dout("osdmap_destroy %p\n", map
);
572 crush_destroy(map
->crush
);
573 while (!RB_EMPTY_ROOT(&map
->pg_temp
)) {
574 struct ceph_pg_mapping
*pg
=
575 rb_entry(rb_first(&map
->pg_temp
),
576 struct ceph_pg_mapping
, node
);
577 rb_erase(&pg
->node
, &map
->pg_temp
);
580 while (!RB_EMPTY_ROOT(&map
->pg_pools
)) {
581 struct ceph_pg_pool_info
*pi
=
582 rb_entry(rb_first(&map
->pg_pools
),
583 struct ceph_pg_pool_info
, node
);
584 __remove_pg_pool(&map
->pg_pools
, pi
);
586 kfree(map
->osd_state
);
587 kfree(map
->osd_weight
);
588 kfree(map
->osd_addr
);
593 * adjust max osd value. reallocate arrays.
595 static int osdmap_set_max_osd(struct ceph_osdmap
*map
, int max
)
598 struct ceph_entity_addr
*addr
;
601 state
= kcalloc(max
, sizeof(*state
), GFP_NOFS
);
602 addr
= kcalloc(max
, sizeof(*addr
), GFP_NOFS
);
603 weight
= kcalloc(max
, sizeof(*weight
), GFP_NOFS
);
604 if (state
== NULL
|| addr
== NULL
|| weight
== NULL
) {
612 if (map
->osd_state
) {
613 memcpy(state
, map
->osd_state
, map
->max_osd
*sizeof(*state
));
614 memcpy(addr
, map
->osd_addr
, map
->max_osd
*sizeof(*addr
));
615 memcpy(weight
, map
->osd_weight
, map
->max_osd
*sizeof(*weight
));
616 kfree(map
->osd_state
);
617 kfree(map
->osd_addr
);
618 kfree(map
->osd_weight
);
621 map
->osd_state
= state
;
622 map
->osd_weight
= weight
;
623 map
->osd_addr
= addr
;
631 struct ceph_osdmap
*osdmap_decode(void **p
, void *end
)
633 struct ceph_osdmap
*map
;
639 struct ceph_pg_pool_info
*pi
;
641 dout("osdmap_decode %p to %p len %d\n", *p
, end
, (int)(end
- *p
));
643 map
= kzalloc(sizeof(*map
), GFP_NOFS
);
645 return ERR_PTR(-ENOMEM
);
646 map
->pg_temp
= RB_ROOT
;
648 ceph_decode_16_safe(p
, end
, version
, bad
);
649 if (version
> CEPH_OSDMAP_VERSION
) {
650 pr_warning("got unknown v %d > %d of osdmap\n", version
,
651 CEPH_OSDMAP_VERSION
);
655 ceph_decode_need(p
, end
, 2*sizeof(u64
)+6*sizeof(u32
), bad
);
656 ceph_decode_copy(p
, &map
->fsid
, sizeof(map
->fsid
));
657 map
->epoch
= ceph_decode_32(p
);
658 ceph_decode_copy(p
, &map
->created
, sizeof(map
->created
));
659 ceph_decode_copy(p
, &map
->modified
, sizeof(map
->modified
));
661 ceph_decode_32_safe(p
, end
, max
, bad
);
663 ceph_decode_need(p
, end
, 4 + 1 + sizeof(pi
->v
), bad
);
665 pi
= kzalloc(sizeof(*pi
), GFP_NOFS
);
668 pi
->id
= ceph_decode_32(p
);
670 ev
= ceph_decode_8(p
); /* encoding version */
671 if (ev
> CEPH_PG_POOL_VERSION
) {
672 pr_warning("got unknown v %d > %d of ceph_pg_pool\n",
673 ev
, CEPH_PG_POOL_VERSION
);
677 err
= __decode_pool(p
, end
, pi
);
682 __insert_pg_pool(&map
->pg_pools
, pi
);
686 err
= __decode_pool_names(p
, end
, map
);
688 dout("fail to decode pool names");
693 ceph_decode_32_safe(p
, end
, map
->pool_max
, bad
);
695 ceph_decode_32_safe(p
, end
, map
->flags
, bad
);
697 max
= ceph_decode_32(p
);
699 /* (re)alloc osd arrays */
700 err
= osdmap_set_max_osd(map
, max
);
703 dout("osdmap_decode max_osd = %d\n", map
->max_osd
);
707 ceph_decode_need(p
, end
, 3*sizeof(u32
) +
708 map
->max_osd
*(1 + sizeof(*map
->osd_weight
) +
709 sizeof(*map
->osd_addr
)), bad
);
710 *p
+= 4; /* skip length field (should match max) */
711 ceph_decode_copy(p
, map
->osd_state
, map
->max_osd
);
713 *p
+= 4; /* skip length field (should match max) */
714 for (i
= 0; i
< map
->max_osd
; i
++)
715 map
->osd_weight
[i
] = ceph_decode_32(p
);
717 *p
+= 4; /* skip length field (should match max) */
718 ceph_decode_copy(p
, map
->osd_addr
, map
->max_osd
*sizeof(*map
->osd_addr
));
719 for (i
= 0; i
< map
->max_osd
; i
++)
720 ceph_decode_addr(&map
->osd_addr
[i
]);
723 ceph_decode_32_safe(p
, end
, len
, bad
);
724 for (i
= 0; i
< len
; i
++) {
727 struct ceph_pg_mapping
*pg
;
729 ceph_decode_need(p
, end
, sizeof(u32
) + sizeof(u64
), bad
);
730 ceph_decode_copy(p
, &pgid
, sizeof(pgid
));
731 n
= ceph_decode_32(p
);
733 if (n
> (UINT_MAX
- sizeof(*pg
)) / sizeof(u32
))
735 ceph_decode_need(p
, end
, n
* sizeof(u32
), bad
);
737 pg
= kmalloc(sizeof(*pg
) + n
*sizeof(u32
), GFP_NOFS
);
742 for (j
= 0; j
< n
; j
++)
743 pg
->osds
[j
] = ceph_decode_32(p
);
745 err
= __insert_pg_mapping(pg
, &map
->pg_temp
);
748 dout(" added pg_temp %llx len %d\n", *(u64
*)&pgid
, len
);
752 ceph_decode_32_safe(p
, end
, len
, bad
);
753 dout("osdmap_decode crush len %d from off 0x%x\n", len
,
755 ceph_decode_need(p
, end
, len
, bad
);
756 map
->crush
= crush_decode(*p
, end
);
758 if (IS_ERR(map
->crush
)) {
759 err
= PTR_ERR(map
->crush
);
764 /* ignore the rest of the map */
767 dout("osdmap_decode done %p %p\n", *p
, end
);
771 dout("osdmap_decode fail err %d\n", err
);
772 ceph_osdmap_destroy(map
);
777 * decode and apply an incremental map update.
779 struct ceph_osdmap
*osdmap_apply_incremental(void **p
, void *end
,
780 struct ceph_osdmap
*map
,
781 struct ceph_messenger
*msgr
)
783 struct crush_map
*newcrush
= NULL
;
784 struct ceph_fsid fsid
;
786 struct ceph_timespec modified
;
788 __s32 new_pool_max
, new_flags
, max
;
793 ceph_decode_16_safe(p
, end
, version
, bad
);
794 if (version
> CEPH_OSDMAP_INC_VERSION
) {
795 pr_warning("got unknown v %d > %d of inc osdmap\n", version
,
796 CEPH_OSDMAP_INC_VERSION
);
800 ceph_decode_need(p
, end
, sizeof(fsid
)+sizeof(modified
)+2*sizeof(u32
),
802 ceph_decode_copy(p
, &fsid
, sizeof(fsid
));
803 epoch
= ceph_decode_32(p
);
804 BUG_ON(epoch
!= map
->epoch
+1);
805 ceph_decode_copy(p
, &modified
, sizeof(modified
));
806 new_pool_max
= ceph_decode_32(p
);
807 new_flags
= ceph_decode_32(p
);
810 ceph_decode_32_safe(p
, end
, len
, bad
);
812 dout("apply_incremental full map len %d, %p to %p\n",
814 return osdmap_decode(p
, min(*p
+len
, end
));
818 ceph_decode_32_safe(p
, end
, len
, bad
);
820 dout("apply_incremental new crush map len %d, %p to %p\n",
822 newcrush
= crush_decode(*p
, min(*p
+len
, end
));
823 if (IS_ERR(newcrush
))
824 return ERR_CAST(newcrush
);
830 map
->flags
= new_flags
;
831 if (new_pool_max
>= 0)
832 map
->pool_max
= new_pool_max
;
834 ceph_decode_need(p
, end
, 5*sizeof(u32
), bad
);
837 max
= ceph_decode_32(p
);
839 err
= osdmap_set_max_osd(map
, max
);
845 map
->modified
= modified
;
848 crush_destroy(map
->crush
);
849 map
->crush
= newcrush
;
854 ceph_decode_32_safe(p
, end
, len
, bad
);
857 struct ceph_pg_pool_info
*pi
;
859 ceph_decode_32_safe(p
, end
, pool
, bad
);
860 ceph_decode_need(p
, end
, 1 + sizeof(pi
->v
), bad
);
861 ev
= ceph_decode_8(p
); /* encoding version */
862 if (ev
> CEPH_PG_POOL_VERSION
) {
863 pr_warning("got unknown v %d > %d of ceph_pg_pool\n",
864 ev
, CEPH_PG_POOL_VERSION
);
868 pi
= __lookup_pg_pool(&map
->pg_pools
, pool
);
870 pi
= kzalloc(sizeof(*pi
), GFP_NOFS
);
876 __insert_pg_pool(&map
->pg_pools
, pi
);
878 err
= __decode_pool(p
, end
, pi
);
883 err
= __decode_pool_names(p
, end
, map
);
889 ceph_decode_32_safe(p
, end
, len
, bad
);
891 struct ceph_pg_pool_info
*pi
;
893 ceph_decode_32_safe(p
, end
, pool
, bad
);
894 pi
= __lookup_pg_pool(&map
->pg_pools
, pool
);
896 __remove_pg_pool(&map
->pg_pools
, pi
);
901 ceph_decode_32_safe(p
, end
, len
, bad
);
904 struct ceph_entity_addr addr
;
905 ceph_decode_32_safe(p
, end
, osd
, bad
);
906 ceph_decode_copy_safe(p
, end
, &addr
, sizeof(addr
), bad
);
907 ceph_decode_addr(&addr
);
908 pr_info("osd%d up\n", osd
);
909 BUG_ON(osd
>= map
->max_osd
);
910 map
->osd_state
[osd
] |= CEPH_OSD_UP
;
911 map
->osd_addr
[osd
] = addr
;
915 ceph_decode_32_safe(p
, end
, len
, bad
);
919 ceph_decode_32_safe(p
, end
, osd
, bad
);
920 xorstate
= **(u8
**)p
;
921 (*p
)++; /* clean flag */
923 xorstate
= CEPH_OSD_UP
;
924 if (xorstate
& CEPH_OSD_UP
)
925 pr_info("osd%d down\n", osd
);
926 if (osd
< map
->max_osd
)
927 map
->osd_state
[osd
] ^= xorstate
;
931 ceph_decode_32_safe(p
, end
, len
, bad
);
934 ceph_decode_need(p
, end
, sizeof(u32
)*2, bad
);
935 osd
= ceph_decode_32(p
);
936 off
= ceph_decode_32(p
);
937 pr_info("osd%d weight 0x%x %s\n", osd
, off
,
938 off
== CEPH_OSD_IN
? "(in)" :
939 (off
== CEPH_OSD_OUT
? "(out)" : ""));
940 if (osd
< map
->max_osd
)
941 map
->osd_weight
[osd
] = off
;
945 ceph_decode_32_safe(p
, end
, len
, bad
);
947 struct ceph_pg_mapping
*pg
;
951 ceph_decode_need(p
, end
, sizeof(u64
) + sizeof(u32
), bad
);
952 ceph_decode_copy(p
, &pgid
, sizeof(pgid
));
953 pglen
= ceph_decode_32(p
);
956 ceph_decode_need(p
, end
, pglen
*sizeof(u32
), bad
);
958 /* removing existing (if any) */
959 (void) __remove_pg_mapping(&map
->pg_temp
, pgid
);
963 if (pglen
> (UINT_MAX
- sizeof(*pg
)) / sizeof(u32
))
966 pg
= kmalloc(sizeof(*pg
) + sizeof(u32
)*pglen
, GFP_NOFS
);
971 for (j
= 0; j
< pglen
; j
++)
972 pg
->osds
[j
] = ceph_decode_32(p
);
973 err
= __insert_pg_mapping(pg
, &map
->pg_temp
);
978 dout(" added pg_temp %llx len %d\n", *(u64
*)&pgid
,
982 __remove_pg_mapping(&map
->pg_temp
, pgid
);
986 /* ignore the rest */
991 pr_err("corrupt inc osdmap epoch %d off %d (%p of %p-%p)\n",
992 epoch
, (int)(*p
- start
), *p
, start
, end
);
993 print_hex_dump(KERN_DEBUG
, "osdmap: ",
994 DUMP_PREFIX_OFFSET
, 16, 1,
995 start
, end
- start
, true);
997 crush_destroy(newcrush
);
1005 * calculate file layout from given offset, length.
1006 * fill in correct oid, logical length, and object extent
1009 * for now, we write only a single su, until we can
1010 * pass a stride back to the caller.
1012 int ceph_calc_file_object_mapping(struct ceph_file_layout
*layout
,
1015 u64
*oxoff
, u64
*oxlen
)
1017 u32 osize
= le32_to_cpu(layout
->fl_object_size
);
1018 u32 su
= le32_to_cpu(layout
->fl_stripe_unit
);
1019 u32 sc
= le32_to_cpu(layout
->fl_stripe_count
);
1020 u32 bl
, stripeno
, stripepos
, objsetno
;
1024 dout("mapping %llu~%llu osize %u fl_su %u\n", off
, *plen
,
1026 if (su
== 0 || sc
== 0)
1028 su_per_object
= osize
/ su
;
1029 if (su_per_object
== 0)
1031 dout("osize %u / su %u = su_per_object %u\n", osize
, su
,
1034 if ((su
& ~PAGE_MASK
) != 0)
1037 /* bl = *off / su; */
1041 dout("off %llu / su %u = bl %u\n", off
, su
, bl
);
1044 stripepos
= bl
% sc
;
1045 objsetno
= stripeno
/ su_per_object
;
1047 *ono
= objsetno
* sc
+ stripepos
;
1048 dout("objset %u * sc %u = ono %u\n", objsetno
, sc
, (unsigned int)*ono
);
1050 /* *oxoff = *off % layout->fl_stripe_unit; # offset in su */
1052 su_offset
= do_div(t
, su
);
1053 *oxoff
= su_offset
+ (stripeno
% su_per_object
) * su
;
1056 * Calculate the length of the extent being written to the selected
1057 * object. This is the minimum of the full length requested (plen) or
1058 * the remainder of the current stripe being written to.
1060 *oxlen
= min_t(u64
, *plen
, su
- su_offset
);
1063 dout(" obj extent %llu~%llu\n", *oxoff
, *oxlen
);
1067 dout(" invalid layout\n");
1073 EXPORT_SYMBOL(ceph_calc_file_object_mapping
);
1076 * calculate an object layout (i.e. pgid) from an oid,
1077 * file_layout, and osdmap
1079 int ceph_calc_object_layout(struct ceph_object_layout
*ol
,
1081 struct ceph_file_layout
*fl
,
1082 struct ceph_osdmap
*osdmap
)
1084 unsigned int num
, num_mask
;
1085 struct ceph_pg pgid
;
1086 int poolid
= le32_to_cpu(fl
->fl_pg_pool
);
1087 struct ceph_pg_pool_info
*pool
;
1092 pool
= __lookup_pg_pool(&osdmap
->pg_pools
, poolid
);
1095 ps
= ceph_str_hash(pool
->v
.object_hash
, oid
, strlen(oid
));
1096 num
= le32_to_cpu(pool
->v
.pg_num
);
1097 num_mask
= pool
->pg_num_mask
;
1099 pgid
.ps
= cpu_to_le16(ps
);
1100 pgid
.preferred
= cpu_to_le16(-1);
1101 pgid
.pool
= fl
->fl_pg_pool
;
1102 dout("calc_object_layout '%s' pgid %d.%x\n", oid
, poolid
, ps
);
1105 ol
->ol_stripe_unit
= fl
->fl_object_stripe_unit
;
1108 EXPORT_SYMBOL(ceph_calc_object_layout
);
1111 * Calculate raw osd vector for the given pgid. Return pointer to osd
1112 * array, or NULL on failure.
1114 static int *calc_pg_raw(struct ceph_osdmap
*osdmap
, struct ceph_pg pgid
,
1115 int *osds
, int *num
)
1117 struct ceph_pg_mapping
*pg
;
1118 struct ceph_pg_pool_info
*pool
;
1120 unsigned int poolid
, ps
, pps
, t
, r
;
1122 poolid
= le32_to_cpu(pgid
.pool
);
1123 ps
= le16_to_cpu(pgid
.ps
);
1125 pool
= __lookup_pg_pool(&osdmap
->pg_pools
, poolid
);
1130 t
= ceph_stable_mod(ps
, le32_to_cpu(pool
->v
.pg_num
),
1131 pool
->pgp_num_mask
);
1132 pgid
.ps
= cpu_to_le16(t
);
1133 pg
= __lookup_pg_mapping(&osdmap
->pg_temp
, pgid
);
1140 ruleno
= crush_find_rule(osdmap
->crush
, pool
->v
.crush_ruleset
,
1141 pool
->v
.type
, pool
->v
.size
);
1143 pr_err("no crush rule pool %d ruleset %d type %d size %d\n",
1144 poolid
, pool
->v
.crush_ruleset
, pool
->v
.type
,
1149 pps
= ceph_stable_mod(ps
,
1150 le32_to_cpu(pool
->v
.pgp_num
),
1151 pool
->pgp_num_mask
);
1153 r
= crush_do_rule(osdmap
->crush
, ruleno
, pps
, osds
,
1154 min_t(int, pool
->v
.size
, *num
),
1155 osdmap
->osd_weight
);
1157 pr_err("error %d from crush rule: pool %d ruleset %d type %d"
1158 " size %d\n", r
, poolid
, pool
->v
.crush_ruleset
,
1159 pool
->v
.type
, pool
->v
.size
);
1167 * Return acting set for given pgid.
1169 int ceph_calc_pg_acting(struct ceph_osdmap
*osdmap
, struct ceph_pg pgid
,
1172 int rawosds
[CEPH_PG_MAX_SIZE
], *osds
;
1173 int i
, o
, num
= CEPH_PG_MAX_SIZE
;
1175 osds
= calc_pg_raw(osdmap
, pgid
, rawosds
, &num
);
1179 /* primary is first up osd */
1181 for (i
= 0; i
< num
; i
++)
1182 if (ceph_osd_is_up(osdmap
, osds
[i
]))
1183 acting
[o
++] = osds
[i
];
1188 * Return primary osd for given pgid, or -1 if none.
1190 int ceph_calc_pg_primary(struct ceph_osdmap
*osdmap
, struct ceph_pg pgid
)
1192 int rawosds
[CEPH_PG_MAX_SIZE
], *osds
;
1193 int i
, num
= CEPH_PG_MAX_SIZE
;
1195 osds
= calc_pg_raw(osdmap
, pgid
, rawosds
, &num
);
1199 /* primary is first up osd */
1200 for (i
= 0; i
< num
; i
++)
1201 if (ceph_osd_is_up(osdmap
, osds
[i
]))
1205 EXPORT_SYMBOL(ceph_calc_pg_primary
);