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 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 struct crush_map
*crush_decode(void *pbyval
, void *end
)
144 void *start
= pbyval
;
147 dout("crush_decode %p to %p len %d\n", *p
, end
, (int)(end
- *p
));
149 c
= kzalloc(sizeof(*c
), GFP_NOFS
);
151 return ERR_PTR(-ENOMEM
);
153 ceph_decode_need(p
, end
, 4*sizeof(u32
), bad
);
154 magic
= ceph_decode_32(p
);
155 if (magic
!= CRUSH_MAGIC
) {
156 pr_err("crush_decode magic %x != current %x\n",
157 (unsigned)magic
, (unsigned)CRUSH_MAGIC
);
160 c
->max_buckets
= ceph_decode_32(p
);
161 c
->max_rules
= ceph_decode_32(p
);
162 c
->max_devices
= ceph_decode_32(p
);
164 c
->device_parents
= kcalloc(c
->max_devices
, sizeof(u32
), GFP_NOFS
);
165 if (c
->device_parents
== NULL
)
167 c
->bucket_parents
= kcalloc(c
->max_buckets
, sizeof(u32
), GFP_NOFS
);
168 if (c
->bucket_parents
== NULL
)
171 c
->buckets
= kcalloc(c
->max_buckets
, sizeof(*c
->buckets
), GFP_NOFS
);
172 if (c
->buckets
== NULL
)
174 c
->rules
= kcalloc(c
->max_rules
, sizeof(*c
->rules
), GFP_NOFS
);
175 if (c
->rules
== NULL
)
179 for (i
= 0; i
< c
->max_buckets
; i
++) {
182 struct crush_bucket
*b
;
184 ceph_decode_32_safe(p
, end
, alg
, bad
);
186 c
->buckets
[i
] = NULL
;
189 dout("crush_decode bucket %d off %x %p to %p\n",
190 i
, (int)(*p
-start
), *p
, end
);
193 case CRUSH_BUCKET_UNIFORM
:
194 size
= sizeof(struct crush_bucket_uniform
);
196 case CRUSH_BUCKET_LIST
:
197 size
= sizeof(struct crush_bucket_list
);
199 case CRUSH_BUCKET_TREE
:
200 size
= sizeof(struct crush_bucket_tree
);
202 case CRUSH_BUCKET_STRAW
:
203 size
= sizeof(struct crush_bucket_straw
);
210 b
= c
->buckets
[i
] = kzalloc(size
, GFP_NOFS
);
214 ceph_decode_need(p
, end
, 4*sizeof(u32
), bad
);
215 b
->id
= ceph_decode_32(p
);
216 b
->type
= ceph_decode_16(p
);
217 b
->alg
= ceph_decode_8(p
);
218 b
->hash
= ceph_decode_8(p
);
219 b
->weight
= ceph_decode_32(p
);
220 b
->size
= ceph_decode_32(p
);
222 dout("crush_decode bucket size %d off %x %p to %p\n",
223 b
->size
, (int)(*p
-start
), *p
, end
);
225 b
->items
= kcalloc(b
->size
, sizeof(__s32
), GFP_NOFS
);
226 if (b
->items
== NULL
)
228 b
->perm
= kcalloc(b
->size
, sizeof(u32
), GFP_NOFS
);
233 ceph_decode_need(p
, end
, b
->size
*sizeof(u32
), bad
);
234 for (j
= 0; j
< b
->size
; j
++)
235 b
->items
[j
] = ceph_decode_32(p
);
238 case CRUSH_BUCKET_UNIFORM
:
239 err
= crush_decode_uniform_bucket(p
, end
,
240 (struct crush_bucket_uniform
*)b
);
244 case CRUSH_BUCKET_LIST
:
245 err
= crush_decode_list_bucket(p
, end
,
246 (struct crush_bucket_list
*)b
);
250 case CRUSH_BUCKET_TREE
:
251 err
= crush_decode_tree_bucket(p
, end
,
252 (struct crush_bucket_tree
*)b
);
256 case CRUSH_BUCKET_STRAW
:
257 err
= crush_decode_straw_bucket(p
, end
,
258 (struct crush_bucket_straw
*)b
);
266 dout("rule vec is %p\n", c
->rules
);
267 for (i
= 0; i
< c
->max_rules
; i
++) {
269 struct crush_rule
*r
;
271 ceph_decode_32_safe(p
, end
, yes
, bad
);
273 dout("crush_decode NO rule %d off %x %p to %p\n",
274 i
, (int)(*p
-start
), *p
, end
);
279 dout("crush_decode rule %d off %x %p to %p\n",
280 i
, (int)(*p
-start
), *p
, end
);
283 ceph_decode_32_safe(p
, end
, yes
, bad
);
284 #if BITS_PER_LONG == 32
286 if (yes
> ULONG_MAX
/ sizeof(struct crush_rule_step
))
289 r
= c
->rules
[i
] = kmalloc(sizeof(*r
) +
290 yes
*sizeof(struct crush_rule_step
),
294 dout(" rule %d is at %p\n", i
, r
);
296 ceph_decode_copy_safe(p
, end
, &r
->mask
, 4, bad
); /* 4 u8's */
297 ceph_decode_need(p
, end
, r
->len
*3*sizeof(u32
), bad
);
298 for (j
= 0; j
< r
->len
; j
++) {
299 r
->steps
[j
].op
= ceph_decode_32(p
);
300 r
->steps
[j
].arg1
= ceph_decode_32(p
);
301 r
->steps
[j
].arg2
= ceph_decode_32(p
);
305 /* ignore trailing name maps. */
307 dout("crush_decode success\n");
313 dout("crush_decode fail %d\n", err
);
319 * rbtree of pg_mapping for handling pg_temp (explicit mapping of pgid
322 static int pgid_cmp(struct ceph_pg l
, struct ceph_pg r
)
334 static int __insert_pg_mapping(struct ceph_pg_mapping
*new,
335 struct rb_root
*root
)
337 struct rb_node
**p
= &root
->rb_node
;
338 struct rb_node
*parent
= NULL
;
339 struct ceph_pg_mapping
*pg
= NULL
;
344 pg
= rb_entry(parent
, struct ceph_pg_mapping
, node
);
345 c
= pgid_cmp(new->pgid
, pg
->pgid
);
354 rb_link_node(&new->node
, parent
, p
);
355 rb_insert_color(&new->node
, root
);
359 static struct ceph_pg_mapping
*__lookup_pg_mapping(struct rb_root
*root
,
362 struct rb_node
*n
= root
->rb_node
;
363 struct ceph_pg_mapping
*pg
;
367 pg
= rb_entry(n
, struct ceph_pg_mapping
, node
);
368 c
= pgid_cmp(pgid
, pg
->pgid
);
380 * rbtree of pg pool info
382 static int __insert_pg_pool(struct rb_root
*root
, struct ceph_pg_pool_info
*new)
384 struct rb_node
**p
= &root
->rb_node
;
385 struct rb_node
*parent
= NULL
;
386 struct ceph_pg_pool_info
*pi
= NULL
;
390 pi
= rb_entry(parent
, struct ceph_pg_pool_info
, node
);
391 if (new->id
< pi
->id
)
393 else if (new->id
> pi
->id
)
399 rb_link_node(&new->node
, parent
, p
);
400 rb_insert_color(&new->node
, root
);
404 static struct ceph_pg_pool_info
*__lookup_pg_pool(struct rb_root
*root
, int id
)
406 struct ceph_pg_pool_info
*pi
;
407 struct rb_node
*n
= root
->rb_node
;
410 pi
= rb_entry(n
, struct ceph_pg_pool_info
, node
);
413 else if (id
> pi
->id
)
421 int ceph_pg_poolid_by_name(struct ceph_osdmap
*map
, const char *name
)
425 for (rbp
= rb_first(&map
->pg_pools
); rbp
; rbp
= rb_next(rbp
)) {
426 struct ceph_pg_pool_info
*pi
=
427 rb_entry(rbp
, struct ceph_pg_pool_info
, node
);
428 if (pi
->name
&& strcmp(pi
->name
, name
) == 0)
433 EXPORT_SYMBOL(ceph_pg_poolid_by_name
);
435 static void __remove_pg_pool(struct rb_root
*root
, struct ceph_pg_pool_info
*pi
)
437 rb_erase(&pi
->node
, root
);
442 static int __decode_pool(void **p
, void *end
, struct ceph_pg_pool_info
*pi
)
446 ceph_decode_copy(p
, &pi
->v
, sizeof(pi
->v
));
449 /* num_snaps * snap_info_t */
450 n
= le32_to_cpu(pi
->v
.num_snaps
);
452 ceph_decode_need(p
, end
, sizeof(u64
) + 1 + sizeof(u64
) +
453 sizeof(struct ceph_timespec
), bad
);
454 *p
+= sizeof(u64
) + /* key */
455 1 + sizeof(u64
) + /* u8, snapid */
456 sizeof(struct ceph_timespec
);
457 m
= ceph_decode_32(p
); /* snap name */
461 *p
+= le32_to_cpu(pi
->v
.num_removed_snap_intervals
) * sizeof(u64
) * 2;
468 static int __decode_pool_names(void **p
, void *end
, struct ceph_osdmap
*map
)
470 struct ceph_pg_pool_info
*pi
;
473 ceph_decode_32_safe(p
, end
, num
, bad
);
474 dout(" %d pool names\n", num
);
476 ceph_decode_32_safe(p
, end
, pool
, bad
);
477 ceph_decode_32_safe(p
, end
, len
, bad
);
478 dout(" pool %d len %d\n", pool
, len
);
479 pi
= __lookup_pg_pool(&map
->pg_pools
, pool
);
482 pi
->name
= kmalloc(len
+ 1, GFP_NOFS
);
484 memcpy(pi
->name
, *p
, len
);
485 pi
->name
[len
] = '\0';
486 dout(" name is %s\n", pi
->name
);
500 void ceph_osdmap_destroy(struct ceph_osdmap
*map
)
502 dout("osdmap_destroy %p\n", map
);
504 crush_destroy(map
->crush
);
505 while (!RB_EMPTY_ROOT(&map
->pg_temp
)) {
506 struct ceph_pg_mapping
*pg
=
507 rb_entry(rb_first(&map
->pg_temp
),
508 struct ceph_pg_mapping
, node
);
509 rb_erase(&pg
->node
, &map
->pg_temp
);
512 while (!RB_EMPTY_ROOT(&map
->pg_pools
)) {
513 struct ceph_pg_pool_info
*pi
=
514 rb_entry(rb_first(&map
->pg_pools
),
515 struct ceph_pg_pool_info
, node
);
516 __remove_pg_pool(&map
->pg_pools
, pi
);
518 kfree(map
->osd_state
);
519 kfree(map
->osd_weight
);
520 kfree(map
->osd_addr
);
525 * adjust max osd value. reallocate arrays.
527 static int osdmap_set_max_osd(struct ceph_osdmap
*map
, int max
)
530 struct ceph_entity_addr
*addr
;
533 state
= kcalloc(max
, sizeof(*state
), GFP_NOFS
);
534 addr
= kcalloc(max
, sizeof(*addr
), GFP_NOFS
);
535 weight
= kcalloc(max
, sizeof(*weight
), GFP_NOFS
);
536 if (state
== NULL
|| addr
== NULL
|| weight
== NULL
) {
544 if (map
->osd_state
) {
545 memcpy(state
, map
->osd_state
, map
->max_osd
*sizeof(*state
));
546 memcpy(addr
, map
->osd_addr
, map
->max_osd
*sizeof(*addr
));
547 memcpy(weight
, map
->osd_weight
, map
->max_osd
*sizeof(*weight
));
548 kfree(map
->osd_state
);
549 kfree(map
->osd_addr
);
550 kfree(map
->osd_weight
);
553 map
->osd_state
= state
;
554 map
->osd_weight
= weight
;
555 map
->osd_addr
= addr
;
563 struct ceph_osdmap
*osdmap_decode(void **p
, void *end
)
565 struct ceph_osdmap
*map
;
571 struct ceph_pg_pool_info
*pi
;
573 dout("osdmap_decode %p to %p len %d\n", *p
, end
, (int)(end
- *p
));
575 map
= kzalloc(sizeof(*map
), GFP_NOFS
);
577 return ERR_PTR(-ENOMEM
);
578 map
->pg_temp
= RB_ROOT
;
580 ceph_decode_16_safe(p
, end
, version
, bad
);
581 if (version
> CEPH_OSDMAP_VERSION
) {
582 pr_warning("got unknown v %d > %d of osdmap\n", version
,
583 CEPH_OSDMAP_VERSION
);
587 ceph_decode_need(p
, end
, 2*sizeof(u64
)+6*sizeof(u32
), bad
);
588 ceph_decode_copy(p
, &map
->fsid
, sizeof(map
->fsid
));
589 map
->epoch
= ceph_decode_32(p
);
590 ceph_decode_copy(p
, &map
->created
, sizeof(map
->created
));
591 ceph_decode_copy(p
, &map
->modified
, sizeof(map
->modified
));
593 ceph_decode_32_safe(p
, end
, max
, bad
);
595 ceph_decode_need(p
, end
, 4 + 1 + sizeof(pi
->v
), bad
);
596 pi
= kzalloc(sizeof(*pi
), GFP_NOFS
);
599 pi
->id
= ceph_decode_32(p
);
600 ev
= ceph_decode_8(p
); /* encoding version */
601 if (ev
> CEPH_PG_POOL_VERSION
) {
602 pr_warning("got unknown v %d > %d of ceph_pg_pool\n",
603 ev
, CEPH_PG_POOL_VERSION
);
607 err
= __decode_pool(p
, end
, pi
);
612 __insert_pg_pool(&map
->pg_pools
, pi
);
615 if (version
>= 5 && __decode_pool_names(p
, end
, map
) < 0)
618 ceph_decode_32_safe(p
, end
, map
->pool_max
, bad
);
620 ceph_decode_32_safe(p
, end
, map
->flags
, bad
);
622 max
= ceph_decode_32(p
);
624 /* (re)alloc osd arrays */
625 err
= osdmap_set_max_osd(map
, max
);
628 dout("osdmap_decode max_osd = %d\n", map
->max_osd
);
632 ceph_decode_need(p
, end
, 3*sizeof(u32
) +
633 map
->max_osd
*(1 + sizeof(*map
->osd_weight
) +
634 sizeof(*map
->osd_addr
)), bad
);
635 *p
+= 4; /* skip length field (should match max) */
636 ceph_decode_copy(p
, map
->osd_state
, map
->max_osd
);
638 *p
+= 4; /* skip length field (should match max) */
639 for (i
= 0; i
< map
->max_osd
; i
++)
640 map
->osd_weight
[i
] = ceph_decode_32(p
);
642 *p
+= 4; /* skip length field (should match max) */
643 ceph_decode_copy(p
, map
->osd_addr
, map
->max_osd
*sizeof(*map
->osd_addr
));
644 for (i
= 0; i
< map
->max_osd
; i
++)
645 ceph_decode_addr(&map
->osd_addr
[i
]);
648 ceph_decode_32_safe(p
, end
, len
, bad
);
649 for (i
= 0; i
< len
; i
++) {
652 struct ceph_pg_mapping
*pg
;
654 ceph_decode_need(p
, end
, sizeof(u32
) + sizeof(u64
), bad
);
655 ceph_decode_copy(p
, &pgid
, sizeof(pgid
));
656 n
= ceph_decode_32(p
);
657 ceph_decode_need(p
, end
, n
* sizeof(u32
), bad
);
659 pg
= kmalloc(sizeof(*pg
) + n
*sizeof(u32
), GFP_NOFS
);
664 for (j
= 0; j
< n
; j
++)
665 pg
->osds
[j
] = ceph_decode_32(p
);
667 err
= __insert_pg_mapping(pg
, &map
->pg_temp
);
670 dout(" added pg_temp %llx len %d\n", *(u64
*)&pgid
, len
);
674 ceph_decode_32_safe(p
, end
, len
, bad
);
675 dout("osdmap_decode crush len %d from off 0x%x\n", len
,
677 ceph_decode_need(p
, end
, len
, bad
);
678 map
->crush
= crush_decode(*p
, end
);
680 if (IS_ERR(map
->crush
)) {
681 err
= PTR_ERR(map
->crush
);
686 /* ignore the rest of the map */
689 dout("osdmap_decode done %p %p\n", *p
, end
);
693 dout("osdmap_decode fail\n");
694 ceph_osdmap_destroy(map
);
699 * decode and apply an incremental map update.
701 struct ceph_osdmap
*osdmap_apply_incremental(void **p
, void *end
,
702 struct ceph_osdmap
*map
,
703 struct ceph_messenger
*msgr
)
705 struct crush_map
*newcrush
= NULL
;
706 struct ceph_fsid fsid
;
708 struct ceph_timespec modified
;
710 __s32 new_pool_max
, new_flags
, max
;
716 ceph_decode_16_safe(p
, end
, version
, bad
);
717 if (version
> CEPH_OSDMAP_INC_VERSION
) {
718 pr_warning("got unknown v %d > %d of inc osdmap\n", version
,
719 CEPH_OSDMAP_INC_VERSION
);
723 ceph_decode_need(p
, end
, sizeof(fsid
)+sizeof(modified
)+2*sizeof(u32
),
725 ceph_decode_copy(p
, &fsid
, sizeof(fsid
));
726 epoch
= ceph_decode_32(p
);
727 BUG_ON(epoch
!= map
->epoch
+1);
728 ceph_decode_copy(p
, &modified
, sizeof(modified
));
729 new_pool_max
= ceph_decode_32(p
);
730 new_flags
= ceph_decode_32(p
);
733 ceph_decode_32_safe(p
, end
, len
, bad
);
735 dout("apply_incremental full map len %d, %p to %p\n",
737 return osdmap_decode(p
, min(*p
+len
, end
));
741 ceph_decode_32_safe(p
, end
, len
, bad
);
743 dout("apply_incremental new crush map len %d, %p to %p\n",
745 newcrush
= crush_decode(*p
, min(*p
+len
, end
));
746 if (IS_ERR(newcrush
))
747 return ERR_CAST(newcrush
);
753 map
->flags
= new_flags
;
754 if (new_pool_max
>= 0)
755 map
->pool_max
= new_pool_max
;
757 ceph_decode_need(p
, end
, 5*sizeof(u32
), bad
);
760 max
= ceph_decode_32(p
);
762 err
= osdmap_set_max_osd(map
, max
);
768 map
->modified
= modified
;
771 crush_destroy(map
->crush
);
772 map
->crush
= newcrush
;
777 ceph_decode_32_safe(p
, end
, len
, bad
);
780 struct ceph_pg_pool_info
*pi
;
782 ceph_decode_32_safe(p
, end
, pool
, bad
);
783 ceph_decode_need(p
, end
, 1 + sizeof(pi
->v
), bad
);
784 ev
= ceph_decode_8(p
); /* encoding version */
785 if (ev
> CEPH_PG_POOL_VERSION
) {
786 pr_warning("got unknown v %d > %d of ceph_pg_pool\n",
787 ev
, CEPH_PG_POOL_VERSION
);
790 pi
= __lookup_pg_pool(&map
->pg_pools
, pool
);
792 pi
= kzalloc(sizeof(*pi
), GFP_NOFS
);
798 __insert_pg_pool(&map
->pg_pools
, pi
);
800 err
= __decode_pool(p
, end
, pi
);
804 if (version
>= 5 && __decode_pool_names(p
, end
, map
) < 0)
808 ceph_decode_32_safe(p
, end
, len
, bad
);
810 struct ceph_pg_pool_info
*pi
;
812 ceph_decode_32_safe(p
, end
, pool
, bad
);
813 pi
= __lookup_pg_pool(&map
->pg_pools
, pool
);
815 __remove_pg_pool(&map
->pg_pools
, pi
);
820 ceph_decode_32_safe(p
, end
, len
, bad
);
823 struct ceph_entity_addr addr
;
824 ceph_decode_32_safe(p
, end
, osd
, bad
);
825 ceph_decode_copy_safe(p
, end
, &addr
, sizeof(addr
), bad
);
826 ceph_decode_addr(&addr
);
827 pr_info("osd%d up\n", osd
);
828 BUG_ON(osd
>= map
->max_osd
);
829 map
->osd_state
[osd
] |= CEPH_OSD_UP
;
830 map
->osd_addr
[osd
] = addr
;
834 ceph_decode_32_safe(p
, end
, len
, bad
);
838 ceph_decode_32_safe(p
, end
, osd
, bad
);
839 xorstate
= **(u8
**)p
;
840 (*p
)++; /* clean flag */
842 xorstate
= CEPH_OSD_UP
;
843 if (xorstate
& CEPH_OSD_UP
)
844 pr_info("osd%d down\n", osd
);
845 if (osd
< map
->max_osd
)
846 map
->osd_state
[osd
] ^= xorstate
;
850 ceph_decode_32_safe(p
, end
, len
, bad
);
853 ceph_decode_need(p
, end
, sizeof(u32
)*2, bad
);
854 osd
= ceph_decode_32(p
);
855 off
= ceph_decode_32(p
);
856 pr_info("osd%d weight 0x%x %s\n", osd
, off
,
857 off
== CEPH_OSD_IN
? "(in)" :
858 (off
== CEPH_OSD_OUT
? "(out)" : ""));
859 if (osd
< map
->max_osd
)
860 map
->osd_weight
[osd
] = off
;
864 rbp
= rb_first(&map
->pg_temp
);
865 ceph_decode_32_safe(p
, end
, len
, bad
);
867 struct ceph_pg_mapping
*pg
;
871 ceph_decode_need(p
, end
, sizeof(u64
) + sizeof(u32
), bad
);
872 ceph_decode_copy(p
, &pgid
, sizeof(pgid
));
873 pglen
= ceph_decode_32(p
);
876 while (rbp
&& pgid_cmp(rb_entry(rbp
, struct ceph_pg_mapping
,
877 node
)->pgid
, pgid
) <= 0) {
878 struct ceph_pg_mapping
*cur
=
879 rb_entry(rbp
, struct ceph_pg_mapping
, node
);
882 dout(" removed pg_temp %llx\n", *(u64
*)&cur
->pgid
);
883 rb_erase(&cur
->node
, &map
->pg_temp
);
889 ceph_decode_need(p
, end
, pglen
*sizeof(u32
), bad
);
890 pg
= kmalloc(sizeof(*pg
) + sizeof(u32
)*pglen
, GFP_NOFS
);
897 for (j
= 0; j
< pglen
; j
++)
898 pg
->osds
[j
] = ceph_decode_32(p
);
899 err
= __insert_pg_mapping(pg
, &map
->pg_temp
);
904 dout(" added pg_temp %llx len %d\n", *(u64
*)&pgid
,
909 struct ceph_pg_mapping
*cur
=
910 rb_entry(rbp
, struct ceph_pg_mapping
, node
);
913 dout(" removed pg_temp %llx\n", *(u64
*)&cur
->pgid
);
914 rb_erase(&cur
->node
, &map
->pg_temp
);
918 /* ignore the rest */
923 pr_err("corrupt inc osdmap epoch %d off %d (%p of %p-%p)\n",
924 epoch
, (int)(*p
- start
), *p
, start
, end
);
925 print_hex_dump(KERN_DEBUG
, "osdmap: ",
926 DUMP_PREFIX_OFFSET
, 16, 1,
927 start
, end
- start
, true);
929 crush_destroy(newcrush
);
937 * calculate file layout from given offset, length.
938 * fill in correct oid, logical length, and object extent
941 * for now, we write only a single su, until we can
942 * pass a stride back to the caller.
944 void ceph_calc_file_object_mapping(struct ceph_file_layout
*layout
,
947 u64
*oxoff
, u64
*oxlen
)
949 u32 osize
= le32_to_cpu(layout
->fl_object_size
);
950 u32 su
= le32_to_cpu(layout
->fl_stripe_unit
);
951 u32 sc
= le32_to_cpu(layout
->fl_stripe_count
);
952 u32 bl
, stripeno
, stripepos
, objsetno
;
956 dout("mapping %llu~%llu osize %u fl_su %u\n", off
, *plen
,
958 su_per_object
= osize
/ su
;
959 dout("osize %u / su %u = su_per_object %u\n", osize
, su
,
962 BUG_ON((su
& ~PAGE_MASK
) != 0);
963 /* bl = *off / su; */
967 dout("off %llu / su %u = bl %u\n", off
, su
, bl
);
971 objsetno
= stripeno
/ su_per_object
;
973 *ono
= objsetno
* sc
+ stripepos
;
974 dout("objset %u * sc %u = ono %u\n", objsetno
, sc
, (unsigned)*ono
);
976 /* *oxoff = *off % layout->fl_stripe_unit; # offset in su */
978 su_offset
= do_div(t
, su
);
979 *oxoff
= su_offset
+ (stripeno
% su_per_object
) * su
;
982 * Calculate the length of the extent being written to the selected
983 * object. This is the minimum of the full length requested (plen) or
984 * the remainder of the current stripe being written to.
986 *oxlen
= min_t(u64
, *plen
, su
- su_offset
);
989 dout(" obj extent %llu~%llu\n", *oxoff
, *oxlen
);
991 EXPORT_SYMBOL(ceph_calc_file_object_mapping
);
994 * calculate an object layout (i.e. pgid) from an oid,
995 * file_layout, and osdmap
997 int ceph_calc_object_layout(struct ceph_object_layout
*ol
,
999 struct ceph_file_layout
*fl
,
1000 struct ceph_osdmap
*osdmap
)
1002 unsigned num
, num_mask
;
1003 struct ceph_pg pgid
;
1004 s32 preferred
= (s32
)le32_to_cpu(fl
->fl_pg_preferred
);
1005 int poolid
= le32_to_cpu(fl
->fl_pg_pool
);
1006 struct ceph_pg_pool_info
*pool
;
1011 pool
= __lookup_pg_pool(&osdmap
->pg_pools
, poolid
);
1014 ps
= ceph_str_hash(pool
->v
.object_hash
, oid
, strlen(oid
));
1015 if (preferred
>= 0) {
1017 num
= le32_to_cpu(pool
->v
.lpg_num
);
1018 num_mask
= pool
->lpg_num_mask
;
1020 num
= le32_to_cpu(pool
->v
.pg_num
);
1021 num_mask
= pool
->pg_num_mask
;
1024 pgid
.ps
= cpu_to_le16(ps
);
1025 pgid
.preferred
= cpu_to_le16(preferred
);
1026 pgid
.pool
= fl
->fl_pg_pool
;
1028 dout("calc_object_layout '%s' pgid %d.%xp%d\n", oid
, poolid
, ps
,
1031 dout("calc_object_layout '%s' pgid %d.%x\n", oid
, poolid
, ps
);
1034 ol
->ol_stripe_unit
= fl
->fl_object_stripe_unit
;
1037 EXPORT_SYMBOL(ceph_calc_object_layout
);
1040 * Calculate raw osd vector for the given pgid. Return pointer to osd
1041 * array, or NULL on failure.
1043 static int *calc_pg_raw(struct ceph_osdmap
*osdmap
, struct ceph_pg pgid
,
1044 int *osds
, int *num
)
1046 struct ceph_pg_mapping
*pg
;
1047 struct ceph_pg_pool_info
*pool
;
1049 unsigned poolid
, ps
, pps
;
1053 pg
= __lookup_pg_mapping(&osdmap
->pg_temp
, pgid
);
1060 poolid
= le32_to_cpu(pgid
.pool
);
1061 ps
= le16_to_cpu(pgid
.ps
);
1062 preferred
= (s16
)le16_to_cpu(pgid
.preferred
);
1064 /* don't forcefeed bad device ids to crush */
1065 if (preferred
>= osdmap
->max_osd
||
1066 preferred
>= osdmap
->crush
->max_devices
)
1069 pool
= __lookup_pg_pool(&osdmap
->pg_pools
, poolid
);
1072 ruleno
= crush_find_rule(osdmap
->crush
, pool
->v
.crush_ruleset
,
1073 pool
->v
.type
, pool
->v
.size
);
1075 pr_err("no crush rule pool %d ruleset %d type %d size %d\n",
1076 poolid
, pool
->v
.crush_ruleset
, pool
->v
.type
,
1082 pps
= ceph_stable_mod(ps
,
1083 le32_to_cpu(pool
->v
.lpgp_num
),
1084 pool
->lpgp_num_mask
);
1086 pps
= ceph_stable_mod(ps
,
1087 le32_to_cpu(pool
->v
.pgp_num
),
1088 pool
->pgp_num_mask
);
1090 *num
= crush_do_rule(osdmap
->crush
, ruleno
, pps
, osds
,
1091 min_t(int, pool
->v
.size
, *num
),
1092 preferred
, osdmap
->osd_weight
);
1097 * Return acting set for given pgid.
1099 int ceph_calc_pg_acting(struct ceph_osdmap
*osdmap
, struct ceph_pg pgid
,
1102 int rawosds
[CEPH_PG_MAX_SIZE
], *osds
;
1103 int i
, o
, num
= CEPH_PG_MAX_SIZE
;
1105 osds
= calc_pg_raw(osdmap
, pgid
, rawosds
, &num
);
1109 /* primary is first up osd */
1111 for (i
= 0; i
< num
; i
++)
1112 if (ceph_osd_is_up(osdmap
, osds
[i
]))
1113 acting
[o
++] = osds
[i
];
1118 * Return primary osd for given pgid, or -1 if none.
1120 int ceph_calc_pg_primary(struct ceph_osdmap
*osdmap
, struct ceph_pg pgid
)
1122 int rawosds
[CEPH_PG_MAX_SIZE
], *osds
;
1123 int i
, num
= CEPH_PG_MAX_SIZE
;
1125 osds
= calc_pg_raw(osdmap
, pgid
, rawosds
, &num
);
1129 /* primary is first up osd */
1130 for (i
= 0; i
< num
; i
++)
1131 if (ceph_osd_is_up(osdmap
, osds
[i
]))
1135 EXPORT_SYMBOL(ceph_calc_pg_primary
);