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 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 int)magic, (unsigned int)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->buckets = kcalloc(c->max_buckets, sizeof(*c->buckets), GFP_NOFS);
165 if (c->buckets == NULL)
167 c->rules = kcalloc(c->max_rules, sizeof(*c->rules), GFP_NOFS);
168 if (c->rules == NULL)
172 for (i = 0; i < c->max_buckets; i++) {
175 struct crush_bucket *b;
177 ceph_decode_32_safe(p, end, alg, bad);
179 c->buckets[i] = NULL;
182 dout("crush_decode bucket %d off %x %p to %p\n",
183 i, (int)(*p-start), *p, end);
186 case CRUSH_BUCKET_UNIFORM:
187 size = sizeof(struct crush_bucket_uniform);
189 case CRUSH_BUCKET_LIST:
190 size = sizeof(struct crush_bucket_list);
192 case CRUSH_BUCKET_TREE:
193 size = sizeof(struct crush_bucket_tree);
195 case CRUSH_BUCKET_STRAW:
196 size = sizeof(struct crush_bucket_straw);
203 b = c->buckets[i] = kzalloc(size, GFP_NOFS);
207 ceph_decode_need(p, end, 4*sizeof(u32), bad);
208 b->id = ceph_decode_32(p);
209 b->type = ceph_decode_16(p);
210 b->alg = ceph_decode_8(p);
211 b->hash = ceph_decode_8(p);
212 b->weight = ceph_decode_32(p);
213 b->size = ceph_decode_32(p);
215 dout("crush_decode bucket size %d off %x %p to %p\n",
216 b->size, (int)(*p-start), *p, end);
218 b->items = kcalloc(b->size, sizeof(__s32), GFP_NOFS);
219 if (b->items == NULL)
221 b->perm = kcalloc(b->size, sizeof(u32), GFP_NOFS);
226 ceph_decode_need(p, end, b->size*sizeof(u32), bad);
227 for (j = 0; j < b->size; j++)
228 b->items[j] = ceph_decode_32(p);
231 case CRUSH_BUCKET_UNIFORM:
232 err = crush_decode_uniform_bucket(p, end,
233 (struct crush_bucket_uniform *)b);
237 case CRUSH_BUCKET_LIST:
238 err = crush_decode_list_bucket(p, end,
239 (struct crush_bucket_list *)b);
243 case CRUSH_BUCKET_TREE:
244 err = crush_decode_tree_bucket(p, end,
245 (struct crush_bucket_tree *)b);
249 case CRUSH_BUCKET_STRAW:
250 err = crush_decode_straw_bucket(p, end,
251 (struct crush_bucket_straw *)b);
259 dout("rule vec is %p\n", c->rules);
260 for (i = 0; i < c->max_rules; i++) {
262 struct crush_rule *r;
264 ceph_decode_32_safe(p, end, yes, bad);
266 dout("crush_decode NO rule %d off %x %p to %p\n",
267 i, (int)(*p-start), *p, end);
272 dout("crush_decode rule %d off %x %p to %p\n",
273 i, (int)(*p-start), *p, end);
276 ceph_decode_32_safe(p, end, yes, bad);
277 #if BITS_PER_LONG == 32
279 if (yes > (ULONG_MAX - sizeof(*r))
280 / sizeof(struct crush_rule_step))
283 r = c->rules[i] = kmalloc(sizeof(*r) +
284 yes*sizeof(struct crush_rule_step),
288 dout(" rule %d is at %p\n", i, r);
290 ceph_decode_copy_safe(p, end, &r->mask, 4, bad); /* 4 u8's */
291 ceph_decode_need(p, end, r->len*3*sizeof(u32), bad);
292 for (j = 0; j < r->len; j++) {
293 r->steps[j].op = ceph_decode_32(p);
294 r->steps[j].arg1 = ceph_decode_32(p);
295 r->steps[j].arg2 = ceph_decode_32(p);
299 /* ignore trailing name maps. */
301 dout("crush_decode success\n");
307 dout("crush_decode fail %d\n", err);
313 * rbtree of pg_mapping for handling pg_temp (explicit mapping of pgid
316 static int pgid_cmp(struct ceph_pg l, struct ceph_pg r)
328 static int __insert_pg_mapping(struct ceph_pg_mapping *new,
329 struct rb_root *root)
331 struct rb_node **p = &root->rb_node;
332 struct rb_node *parent = NULL;
333 struct ceph_pg_mapping *pg = NULL;
336 dout("__insert_pg_mapping %llx %p\n", *(u64 *)&new->pgid, new);
339 pg = rb_entry(parent, struct ceph_pg_mapping, node);
340 c = pgid_cmp(new->pgid, pg->pgid);
349 rb_link_node(&new->node, parent, p);
350 rb_insert_color(&new->node, root);
354 static struct ceph_pg_mapping *__lookup_pg_mapping(struct rb_root *root,
357 struct rb_node *n = root->rb_node;
358 struct ceph_pg_mapping *pg;
362 pg = rb_entry(n, struct ceph_pg_mapping, node);
363 c = pgid_cmp(pgid, pg->pgid);
369 dout("__lookup_pg_mapping %llx got %p\n",
377 static int __remove_pg_mapping(struct rb_root *root, struct ceph_pg pgid)
379 struct ceph_pg_mapping *pg = __lookup_pg_mapping(root, pgid);
382 dout("__remove_pg_mapping %llx %p\n", *(u64 *)&pgid, pg);
383 rb_erase(&pg->node, root);
387 dout("__remove_pg_mapping %llx dne\n", *(u64 *)&pgid);
392 * rbtree of pg pool info
394 static int __insert_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *new)
396 struct rb_node **p = &root->rb_node;
397 struct rb_node *parent = NULL;
398 struct ceph_pg_pool_info *pi = NULL;
402 pi = rb_entry(parent, struct ceph_pg_pool_info, node);
403 if (new->id < pi->id)
405 else if (new->id > pi->id)
411 rb_link_node(&new->node, parent, p);
412 rb_insert_color(&new->node, root);
416 static struct ceph_pg_pool_info *__lookup_pg_pool(struct rb_root *root, int id)
418 struct ceph_pg_pool_info *pi;
419 struct rb_node *n = root->rb_node;
422 pi = rb_entry(n, struct ceph_pg_pool_info, node);
425 else if (id > pi->id)
433 int ceph_pg_poolid_by_name(struct ceph_osdmap *map, const char *name)
437 for (rbp = rb_first(&map->pg_pools); rbp; rbp = rb_next(rbp)) {
438 struct ceph_pg_pool_info *pi =
439 rb_entry(rbp, struct ceph_pg_pool_info, node);
440 if (pi->name && strcmp(pi->name, name) == 0)
445 EXPORT_SYMBOL(ceph_pg_poolid_by_name);
447 static void __remove_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *pi)
449 rb_erase(&pi->node, root);
454 static int __decode_pool(void **p, void *end, struct ceph_pg_pool_info *pi)
458 ceph_decode_copy(p, &pi->v, sizeof(pi->v));
461 /* num_snaps * snap_info_t */
462 n = le32_to_cpu(pi->v.num_snaps);
464 ceph_decode_need(p, end, sizeof(u64) + 1 + sizeof(u64) +
465 sizeof(struct ceph_timespec), bad);
466 *p += sizeof(u64) + /* key */
467 1 + sizeof(u64) + /* u8, snapid */
468 sizeof(struct ceph_timespec);
469 m = ceph_decode_32(p); /* snap name */
473 *p += le32_to_cpu(pi->v.num_removed_snap_intervals) * sizeof(u64) * 2;
480 static int __decode_pool_names(void **p, void *end, struct ceph_osdmap *map)
482 struct ceph_pg_pool_info *pi;
485 ceph_decode_32_safe(p, end, num, bad);
486 dout(" %d pool names\n", num);
488 ceph_decode_32_safe(p, end, pool, bad);
489 ceph_decode_32_safe(p, end, len, bad);
490 dout(" pool %d len %d\n", pool, len);
491 pi = __lookup_pg_pool(&map->pg_pools, pool);
494 pi->name = kmalloc(len + 1, GFP_NOFS);
496 memcpy(pi->name, *p, len);
497 pi->name[len] = '\0';
498 dout(" name is %s\n", pi->name);
512 void ceph_osdmap_destroy(struct ceph_osdmap *map)
514 dout("osdmap_destroy %p\n", map);
516 crush_destroy(map->crush);
517 while (!RB_EMPTY_ROOT(&map->pg_temp)) {
518 struct ceph_pg_mapping *pg =
519 rb_entry(rb_first(&map->pg_temp),
520 struct ceph_pg_mapping, node);
521 rb_erase(&pg->node, &map->pg_temp);
524 while (!RB_EMPTY_ROOT(&map->pg_pools)) {
525 struct ceph_pg_pool_info *pi =
526 rb_entry(rb_first(&map->pg_pools),
527 struct ceph_pg_pool_info, node);
528 __remove_pg_pool(&map->pg_pools, pi);
530 kfree(map->osd_state);
531 kfree(map->osd_weight);
532 kfree(map->osd_addr);
537 * adjust max osd value. reallocate arrays.
539 static int osdmap_set_max_osd(struct ceph_osdmap *map, int max)
542 struct ceph_entity_addr *addr;
545 state = kcalloc(max, sizeof(*state), GFP_NOFS);
546 addr = kcalloc(max, sizeof(*addr), GFP_NOFS);
547 weight = kcalloc(max, sizeof(*weight), GFP_NOFS);
548 if (state == NULL || addr == NULL || weight == NULL) {
556 if (map->osd_state) {
557 memcpy(state, map->osd_state, map->max_osd*sizeof(*state));
558 memcpy(addr, map->osd_addr, map->max_osd*sizeof(*addr));
559 memcpy(weight, map->osd_weight, map->max_osd*sizeof(*weight));
560 kfree(map->osd_state);
561 kfree(map->osd_addr);
562 kfree(map->osd_weight);
565 map->osd_state = state;
566 map->osd_weight = weight;
567 map->osd_addr = addr;
575 struct ceph_osdmap *osdmap_decode(void **p, void *end)
577 struct ceph_osdmap *map;
583 struct ceph_pg_pool_info *pi;
585 dout("osdmap_decode %p to %p len %d\n", *p, end, (int)(end - *p));
587 map = kzalloc(sizeof(*map), GFP_NOFS);
589 return ERR_PTR(-ENOMEM);
590 map->pg_temp = RB_ROOT;
592 ceph_decode_16_safe(p, end, version, bad);
593 if (version > CEPH_OSDMAP_VERSION) {
594 pr_warning("got unknown v %d > %d of osdmap\n", version,
595 CEPH_OSDMAP_VERSION);
599 ceph_decode_need(p, end, 2*sizeof(u64)+6*sizeof(u32), bad);
600 ceph_decode_copy(p, &map->fsid, sizeof(map->fsid));
601 map->epoch = ceph_decode_32(p);
602 ceph_decode_copy(p, &map->created, sizeof(map->created));
603 ceph_decode_copy(p, &map->modified, sizeof(map->modified));
605 ceph_decode_32_safe(p, end, max, bad);
607 ceph_decode_need(p, end, 4 + 1 + sizeof(pi->v), bad);
608 pi = kzalloc(sizeof(*pi), GFP_NOFS);
611 pi->id = ceph_decode_32(p);
612 ev = ceph_decode_8(p); /* encoding version */
613 if (ev > CEPH_PG_POOL_VERSION) {
614 pr_warning("got unknown v %d > %d of ceph_pg_pool\n",
615 ev, CEPH_PG_POOL_VERSION);
619 err = __decode_pool(p, end, pi);
624 __insert_pg_pool(&map->pg_pools, pi);
627 if (version >= 5 && __decode_pool_names(p, end, map) < 0)
630 ceph_decode_32_safe(p, end, map->pool_max, bad);
632 ceph_decode_32_safe(p, end, map->flags, bad);
634 max = ceph_decode_32(p);
636 /* (re)alloc osd arrays */
637 err = osdmap_set_max_osd(map, max);
640 dout("osdmap_decode max_osd = %d\n", map->max_osd);
644 ceph_decode_need(p, end, 3*sizeof(u32) +
645 map->max_osd*(1 + sizeof(*map->osd_weight) +
646 sizeof(*map->osd_addr)), bad);
647 *p += 4; /* skip length field (should match max) */
648 ceph_decode_copy(p, map->osd_state, map->max_osd);
650 *p += 4; /* skip length field (should match max) */
651 for (i = 0; i < map->max_osd; i++)
652 map->osd_weight[i] = ceph_decode_32(p);
654 *p += 4; /* skip length field (should match max) */
655 ceph_decode_copy(p, map->osd_addr, map->max_osd*sizeof(*map->osd_addr));
656 for (i = 0; i < map->max_osd; i++)
657 ceph_decode_addr(&map->osd_addr[i]);
660 ceph_decode_32_safe(p, end, len, bad);
661 for (i = 0; i < len; i++) {
664 struct ceph_pg_mapping *pg;
666 ceph_decode_need(p, end, sizeof(u32) + sizeof(u64), bad);
667 ceph_decode_copy(p, &pgid, sizeof(pgid));
668 n = ceph_decode_32(p);
669 ceph_decode_need(p, end, n * sizeof(u32), bad);
671 pg = kmalloc(sizeof(*pg) + n*sizeof(u32), GFP_NOFS);
676 for (j = 0; j < n; j++)
677 pg->osds[j] = ceph_decode_32(p);
679 err = __insert_pg_mapping(pg, &map->pg_temp);
682 dout(" added pg_temp %llx len %d\n", *(u64 *)&pgid, len);
686 ceph_decode_32_safe(p, end, len, bad);
687 dout("osdmap_decode crush len %d from off 0x%x\n", len,
689 ceph_decode_need(p, end, len, bad);
690 map->crush = crush_decode(*p, end);
692 if (IS_ERR(map->crush)) {
693 err = PTR_ERR(map->crush);
698 /* ignore the rest of the map */
701 dout("osdmap_decode done %p %p\n", *p, end);
705 dout("osdmap_decode fail\n");
706 ceph_osdmap_destroy(map);
711 * decode and apply an incremental map update.
713 struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end,
714 struct ceph_osdmap *map,
715 struct ceph_messenger *msgr)
717 struct crush_map *newcrush = NULL;
718 struct ceph_fsid fsid;
720 struct ceph_timespec modified;
722 __s32 new_pool_max, new_flags, max;
727 ceph_decode_16_safe(p, end, version, bad);
728 if (version > CEPH_OSDMAP_INC_VERSION) {
729 pr_warning("got unknown v %d > %d of inc osdmap\n", version,
730 CEPH_OSDMAP_INC_VERSION);
734 ceph_decode_need(p, end, sizeof(fsid)+sizeof(modified)+2*sizeof(u32),
736 ceph_decode_copy(p, &fsid, sizeof(fsid));
737 epoch = ceph_decode_32(p);
738 BUG_ON(epoch != map->epoch+1);
739 ceph_decode_copy(p, &modified, sizeof(modified));
740 new_pool_max = ceph_decode_32(p);
741 new_flags = ceph_decode_32(p);
744 ceph_decode_32_safe(p, end, len, bad);
746 dout("apply_incremental full map len %d, %p to %p\n",
748 return osdmap_decode(p, min(*p+len, end));
752 ceph_decode_32_safe(p, end, len, bad);
754 dout("apply_incremental new crush map len %d, %p to %p\n",
756 newcrush = crush_decode(*p, min(*p+len, end));
757 if (IS_ERR(newcrush))
758 return ERR_CAST(newcrush);
764 map->flags = new_flags;
765 if (new_pool_max >= 0)
766 map->pool_max = new_pool_max;
768 ceph_decode_need(p, end, 5*sizeof(u32), bad);
771 max = ceph_decode_32(p);
773 err = osdmap_set_max_osd(map, max);
779 map->modified = modified;
782 crush_destroy(map->crush);
783 map->crush = newcrush;
788 ceph_decode_32_safe(p, end, len, bad);
791 struct ceph_pg_pool_info *pi;
793 ceph_decode_32_safe(p, end, pool, bad);
794 ceph_decode_need(p, end, 1 + sizeof(pi->v), bad);
795 ev = ceph_decode_8(p); /* encoding version */
796 if (ev > CEPH_PG_POOL_VERSION) {
797 pr_warning("got unknown v %d > %d of ceph_pg_pool\n",
798 ev, CEPH_PG_POOL_VERSION);
801 pi = __lookup_pg_pool(&map->pg_pools, pool);
803 pi = kzalloc(sizeof(*pi), GFP_NOFS);
809 __insert_pg_pool(&map->pg_pools, pi);
811 err = __decode_pool(p, end, pi);
815 if (version >= 5 && __decode_pool_names(p, end, map) < 0)
819 ceph_decode_32_safe(p, end, len, bad);
821 struct ceph_pg_pool_info *pi;
823 ceph_decode_32_safe(p, end, pool, bad);
824 pi = __lookup_pg_pool(&map->pg_pools, pool);
826 __remove_pg_pool(&map->pg_pools, pi);
831 ceph_decode_32_safe(p, end, len, bad);
834 struct ceph_entity_addr addr;
835 ceph_decode_32_safe(p, end, osd, bad);
836 ceph_decode_copy_safe(p, end, &addr, sizeof(addr), bad);
837 ceph_decode_addr(&addr);
838 pr_info("osd%d up\n", osd);
839 BUG_ON(osd >= map->max_osd);
840 map->osd_state[osd] |= CEPH_OSD_UP;
841 map->osd_addr[osd] = addr;
845 ceph_decode_32_safe(p, end, len, bad);
849 ceph_decode_32_safe(p, end, osd, bad);
850 xorstate = **(u8 **)p;
851 (*p)++; /* clean flag */
853 xorstate = CEPH_OSD_UP;
854 if (xorstate & CEPH_OSD_UP)
855 pr_info("osd%d down\n", osd);
856 if (osd < map->max_osd)
857 map->osd_state[osd] ^= xorstate;
861 ceph_decode_32_safe(p, end, len, bad);
864 ceph_decode_need(p, end, sizeof(u32)*2, bad);
865 osd = ceph_decode_32(p);
866 off = ceph_decode_32(p);
867 pr_info("osd%d weight 0x%x %s\n", osd, off,
868 off == CEPH_OSD_IN ? "(in)" :
869 (off == CEPH_OSD_OUT ? "(out)" : ""));
870 if (osd < map->max_osd)
871 map->osd_weight[osd] = off;
875 ceph_decode_32_safe(p, end, len, bad);
877 struct ceph_pg_mapping *pg;
881 ceph_decode_need(p, end, sizeof(u64) + sizeof(u32), bad);
882 ceph_decode_copy(p, &pgid, sizeof(pgid));
883 pglen = ceph_decode_32(p);
886 ceph_decode_need(p, end, pglen*sizeof(u32), bad);
888 /* removing existing (if any) */
889 (void) __remove_pg_mapping(&map->pg_temp, pgid);
892 pg = kmalloc(sizeof(*pg) + sizeof(u32)*pglen, GFP_NOFS);
899 for (j = 0; j < pglen; j++)
900 pg->osds[j] = ceph_decode_32(p);
901 err = __insert_pg_mapping(pg, &map->pg_temp);
906 dout(" added pg_temp %llx len %d\n", *(u64 *)&pgid,
910 __remove_pg_mapping(&map->pg_temp, pgid);
914 /* ignore the rest */
919 pr_err("corrupt inc osdmap epoch %d off %d (%p of %p-%p)\n",
920 epoch, (int)(*p - start), *p, start, end);
921 print_hex_dump(KERN_DEBUG, "osdmap: ",
922 DUMP_PREFIX_OFFSET, 16, 1,
923 start, end - start, true);
925 crush_destroy(newcrush);
933 * calculate file layout from given offset, length.
934 * fill in correct oid, logical length, and object extent
937 * for now, we write only a single su, until we can
938 * pass a stride back to the caller.
940 void ceph_calc_file_object_mapping(struct ceph_file_layout *layout,
943 u64 *oxoff, u64 *oxlen)
945 u32 osize = le32_to_cpu(layout->fl_object_size);
946 u32 su = le32_to_cpu(layout->fl_stripe_unit);
947 u32 sc = le32_to_cpu(layout->fl_stripe_count);
948 u32 bl, stripeno, stripepos, objsetno;
952 dout("mapping %llu~%llu osize %u fl_su %u\n", off, *plen,
954 su_per_object = osize / su;
955 dout("osize %u / su %u = su_per_object %u\n", osize, su,
958 BUG_ON((su & ~PAGE_MASK) != 0);
959 /* bl = *off / su; */
963 dout("off %llu / su %u = bl %u\n", off, su, bl);
967 objsetno = stripeno / su_per_object;
969 *ono = objsetno * sc + stripepos;
970 dout("objset %u * sc %u = ono %u\n", objsetno, sc, (unsigned int)*ono);
972 /* *oxoff = *off % layout->fl_stripe_unit; # offset in su */
974 su_offset = do_div(t, su);
975 *oxoff = su_offset + (stripeno % su_per_object) * su;
978 * Calculate the length of the extent being written to the selected
979 * object. This is the minimum of the full length requested (plen) or
980 * the remainder of the current stripe being written to.
982 *oxlen = min_t(u64, *plen, su - su_offset);
985 dout(" obj extent %llu~%llu\n", *oxoff, *oxlen);
987 EXPORT_SYMBOL(ceph_calc_file_object_mapping);
990 * calculate an object layout (i.e. pgid) from an oid,
991 * file_layout, and osdmap
993 int ceph_calc_object_layout(struct ceph_object_layout *ol,
995 struct ceph_file_layout *fl,
996 struct ceph_osdmap *osdmap)
998 unsigned int num, num_mask;
1000 int poolid = le32_to_cpu(fl->fl_pg_pool);
1001 struct ceph_pg_pool_info *pool;
1006 pool = __lookup_pg_pool(&osdmap->pg_pools, poolid);
1009 ps = ceph_str_hash(pool->v.object_hash, oid, strlen(oid));
1010 num = le32_to_cpu(pool->v.pg_num);
1011 num_mask = pool->pg_num_mask;
1013 pgid.ps = cpu_to_le16(ps);
1014 pgid.preferred = cpu_to_le16(-1);
1015 pgid.pool = fl->fl_pg_pool;
1016 dout("calc_object_layout '%s' pgid %d.%x\n", oid, poolid, ps);
1019 ol->ol_stripe_unit = fl->fl_object_stripe_unit;
1022 EXPORT_SYMBOL(ceph_calc_object_layout);
1025 * Calculate raw osd vector for the given pgid. Return pointer to osd
1026 * array, or NULL on failure.
1028 static int *calc_pg_raw(struct ceph_osdmap *osdmap, struct ceph_pg pgid,
1029 int *osds, int *num)
1031 struct ceph_pg_mapping *pg;
1032 struct ceph_pg_pool_info *pool;
1034 unsigned int poolid, ps, pps, t, r;
1036 poolid = le32_to_cpu(pgid.pool);
1037 ps = le16_to_cpu(pgid.ps);
1039 pool = __lookup_pg_pool(&osdmap->pg_pools, poolid);
1044 t = ceph_stable_mod(ps, le32_to_cpu(pool->v.pg_num),
1045 pool->pgp_num_mask);
1046 pgid.ps = cpu_to_le16(t);
1047 pg = __lookup_pg_mapping(&osdmap->pg_temp, pgid);
1054 ruleno = crush_find_rule(osdmap->crush, pool->v.crush_ruleset,
1055 pool->v.type, pool->v.size);
1057 pr_err("no crush rule pool %d ruleset %d type %d size %d\n",
1058 poolid, pool->v.crush_ruleset, pool->v.type,
1063 pps = ceph_stable_mod(ps,
1064 le32_to_cpu(pool->v.pgp_num),
1065 pool->pgp_num_mask);
1067 r = crush_do_rule(osdmap->crush, ruleno, pps, osds,
1068 min_t(int, pool->v.size, *num),
1069 osdmap->osd_weight);
1071 pr_err("error %d from crush rule: pool %d ruleset %d type %d"
1072 " size %d\n", r, poolid, pool->v.crush_ruleset,
1073 pool->v.type, pool->v.size);
1081 * Return acting set for given pgid.
1083 int ceph_calc_pg_acting(struct ceph_osdmap *osdmap, struct ceph_pg pgid,
1086 int rawosds[CEPH_PG_MAX_SIZE], *osds;
1087 int i, o, num = CEPH_PG_MAX_SIZE;
1089 osds = calc_pg_raw(osdmap, pgid, rawosds, &num);
1093 /* primary is first up osd */
1095 for (i = 0; i < num; i++)
1096 if (ceph_osd_is_up(osdmap, osds[i]))
1097 acting[o++] = osds[i];
1102 * Return primary osd for given pgid, or -1 if none.
1104 int ceph_calc_pg_primary(struct ceph_osdmap *osdmap, struct ceph_pg pgid)
1106 int rawosds[CEPH_PG_MAX_SIZE], *osds;
1107 int i, num = CEPH_PG_MAX_SIZE;
1109 osds = calc_pg_raw(osdmap, pgid, rawosds, &num);
1113 /* primary is first up osd */
1114 for (i = 0; i < num; i++)
1115 if (ceph_osd_is_up(osdmap, osds[i]))
1119 EXPORT_SYMBOL(ceph_calc_pg_primary);