13d14c5d2SYehuda Sadeh 23d14c5d2SYehuda Sadeh #include <linux/ceph/ceph_debug.h> 33d14c5d2SYehuda Sadeh 43d14c5d2SYehuda Sadeh #include <linux/module.h> 53d14c5d2SYehuda Sadeh #include <linux/slab.h> 63d14c5d2SYehuda Sadeh #include <asm/div64.h> 73d14c5d2SYehuda Sadeh 83d14c5d2SYehuda Sadeh #include <linux/ceph/libceph.h> 93d14c5d2SYehuda Sadeh #include <linux/ceph/osdmap.h> 103d14c5d2SYehuda Sadeh #include <linux/ceph/decode.h> 113d14c5d2SYehuda Sadeh #include <linux/crush/hash.h> 123d14c5d2SYehuda Sadeh #include <linux/crush/mapper.h> 133d14c5d2SYehuda Sadeh 143d14c5d2SYehuda Sadeh char *ceph_osdmap_state_str(char *str, int len, int state) 153d14c5d2SYehuda Sadeh { 163d14c5d2SYehuda Sadeh if (!len) 171ec3911dSCong Ding return str; 183d14c5d2SYehuda Sadeh 191ec3911dSCong Ding if ((state & CEPH_OSD_EXISTS) && (state & CEPH_OSD_UP)) 201ec3911dSCong Ding snprintf(str, len, "exists, up"); 211ec3911dSCong Ding else if (state & CEPH_OSD_EXISTS) 223d14c5d2SYehuda Sadeh snprintf(str, len, "exists"); 231ec3911dSCong Ding else if (state & CEPH_OSD_UP) 241ec3911dSCong Ding snprintf(str, len, "up"); 251ec3911dSCong Ding else 263d14c5d2SYehuda Sadeh snprintf(str, len, "doesn't exist"); 271ec3911dSCong Ding 283d14c5d2SYehuda Sadeh return str; 293d14c5d2SYehuda Sadeh } 303d14c5d2SYehuda Sadeh 313d14c5d2SYehuda Sadeh /* maps */ 323d14c5d2SYehuda Sadeh 3395c96174SEric Dumazet static int calc_bits_of(unsigned int t) 343d14c5d2SYehuda Sadeh { 353d14c5d2SYehuda Sadeh int b = 0; 363d14c5d2SYehuda Sadeh while (t) { 373d14c5d2SYehuda Sadeh t = t >> 1; 383d14c5d2SYehuda Sadeh b++; 393d14c5d2SYehuda Sadeh } 403d14c5d2SYehuda Sadeh return b; 413d14c5d2SYehuda Sadeh } 423d14c5d2SYehuda Sadeh 433d14c5d2SYehuda Sadeh /* 443d14c5d2SYehuda Sadeh * the foo_mask is the smallest value 2^n-1 that is >= foo. 453d14c5d2SYehuda Sadeh */ 463d14c5d2SYehuda Sadeh static void calc_pg_masks(struct ceph_pg_pool_info *pi) 473d14c5d2SYehuda Sadeh { 484f6a7e5eSSage Weil pi->pg_num_mask = (1 << calc_bits_of(pi->pg_num-1)) - 1; 494f6a7e5eSSage Weil pi->pgp_num_mask = (1 << calc_bits_of(pi->pgp_num-1)) - 1; 503d14c5d2SYehuda Sadeh } 513d14c5d2SYehuda Sadeh 523d14c5d2SYehuda Sadeh /* 533d14c5d2SYehuda Sadeh * decode crush map 543d14c5d2SYehuda Sadeh */ 553d14c5d2SYehuda Sadeh static int crush_decode_uniform_bucket(void **p, void *end, 563d14c5d2SYehuda Sadeh struct crush_bucket_uniform *b) 573d14c5d2SYehuda Sadeh { 583d14c5d2SYehuda Sadeh dout("crush_decode_uniform_bucket %p to %p\n", *p, end); 593d14c5d2SYehuda Sadeh ceph_decode_need(p, end, (1+b->h.size) * sizeof(u32), bad); 603d14c5d2SYehuda Sadeh b->item_weight = ceph_decode_32(p); 613d14c5d2SYehuda Sadeh return 0; 623d14c5d2SYehuda Sadeh bad: 633d14c5d2SYehuda Sadeh return -EINVAL; 643d14c5d2SYehuda Sadeh } 653d14c5d2SYehuda Sadeh 663d14c5d2SYehuda Sadeh static int crush_decode_list_bucket(void **p, void *end, 673d14c5d2SYehuda Sadeh struct crush_bucket_list *b) 683d14c5d2SYehuda Sadeh { 693d14c5d2SYehuda Sadeh int j; 703d14c5d2SYehuda Sadeh dout("crush_decode_list_bucket %p to %p\n", *p, end); 713d14c5d2SYehuda Sadeh b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS); 723d14c5d2SYehuda Sadeh if (b->item_weights == NULL) 733d14c5d2SYehuda Sadeh return -ENOMEM; 743d14c5d2SYehuda Sadeh b->sum_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS); 753d14c5d2SYehuda Sadeh if (b->sum_weights == NULL) 763d14c5d2SYehuda Sadeh return -ENOMEM; 773d14c5d2SYehuda Sadeh ceph_decode_need(p, end, 2 * b->h.size * sizeof(u32), bad); 783d14c5d2SYehuda Sadeh for (j = 0; j < b->h.size; j++) { 793d14c5d2SYehuda Sadeh b->item_weights[j] = ceph_decode_32(p); 803d14c5d2SYehuda Sadeh b->sum_weights[j] = ceph_decode_32(p); 813d14c5d2SYehuda Sadeh } 823d14c5d2SYehuda Sadeh return 0; 833d14c5d2SYehuda Sadeh bad: 843d14c5d2SYehuda Sadeh return -EINVAL; 853d14c5d2SYehuda Sadeh } 863d14c5d2SYehuda Sadeh 873d14c5d2SYehuda Sadeh static int crush_decode_tree_bucket(void **p, void *end, 883d14c5d2SYehuda Sadeh struct crush_bucket_tree *b) 893d14c5d2SYehuda Sadeh { 903d14c5d2SYehuda Sadeh int j; 913d14c5d2SYehuda Sadeh dout("crush_decode_tree_bucket %p to %p\n", *p, end); 923d14c5d2SYehuda Sadeh ceph_decode_32_safe(p, end, b->num_nodes, bad); 933d14c5d2SYehuda Sadeh b->node_weights = kcalloc(b->num_nodes, sizeof(u32), GFP_NOFS); 943d14c5d2SYehuda Sadeh if (b->node_weights == NULL) 953d14c5d2SYehuda Sadeh return -ENOMEM; 963d14c5d2SYehuda Sadeh ceph_decode_need(p, end, b->num_nodes * sizeof(u32), bad); 973d14c5d2SYehuda Sadeh for (j = 0; j < b->num_nodes; j++) 983d14c5d2SYehuda Sadeh b->node_weights[j] = ceph_decode_32(p); 993d14c5d2SYehuda Sadeh return 0; 1003d14c5d2SYehuda Sadeh bad: 1013d14c5d2SYehuda Sadeh return -EINVAL; 1023d14c5d2SYehuda Sadeh } 1033d14c5d2SYehuda Sadeh 1043d14c5d2SYehuda Sadeh static int crush_decode_straw_bucket(void **p, void *end, 1053d14c5d2SYehuda Sadeh struct crush_bucket_straw *b) 1063d14c5d2SYehuda Sadeh { 1073d14c5d2SYehuda Sadeh int j; 1083d14c5d2SYehuda Sadeh dout("crush_decode_straw_bucket %p to %p\n", *p, end); 1093d14c5d2SYehuda Sadeh b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS); 1103d14c5d2SYehuda Sadeh if (b->item_weights == NULL) 1113d14c5d2SYehuda Sadeh return -ENOMEM; 1123d14c5d2SYehuda Sadeh b->straws = kcalloc(b->h.size, sizeof(u32), GFP_NOFS); 1133d14c5d2SYehuda Sadeh if (b->straws == NULL) 1143d14c5d2SYehuda Sadeh return -ENOMEM; 1153d14c5d2SYehuda Sadeh ceph_decode_need(p, end, 2 * b->h.size * sizeof(u32), bad); 1163d14c5d2SYehuda Sadeh for (j = 0; j < b->h.size; j++) { 1173d14c5d2SYehuda Sadeh b->item_weights[j] = ceph_decode_32(p); 1183d14c5d2SYehuda Sadeh b->straws[j] = ceph_decode_32(p); 1193d14c5d2SYehuda Sadeh } 1203d14c5d2SYehuda Sadeh return 0; 1213d14c5d2SYehuda Sadeh bad: 1223d14c5d2SYehuda Sadeh return -EINVAL; 1233d14c5d2SYehuda Sadeh } 1243d14c5d2SYehuda Sadeh 125546f04efSSage Weil static int skip_name_map(void **p, void *end) 126546f04efSSage Weil { 127546f04efSSage Weil int len; 128546f04efSSage Weil ceph_decode_32_safe(p, end, len ,bad); 129546f04efSSage Weil while (len--) { 130546f04efSSage Weil int strlen; 131546f04efSSage Weil *p += sizeof(u32); 132546f04efSSage Weil ceph_decode_32_safe(p, end, strlen, bad); 133546f04efSSage Weil *p += strlen; 134546f04efSSage Weil } 135546f04efSSage Weil return 0; 136546f04efSSage Weil bad: 137546f04efSSage Weil return -EINVAL; 138546f04efSSage Weil } 139546f04efSSage Weil 1403d14c5d2SYehuda Sadeh static struct crush_map *crush_decode(void *pbyval, void *end) 1413d14c5d2SYehuda Sadeh { 1423d14c5d2SYehuda Sadeh struct crush_map *c; 1433d14c5d2SYehuda Sadeh int err = -EINVAL; 1443d14c5d2SYehuda Sadeh int i, j; 1453d14c5d2SYehuda Sadeh void **p = &pbyval; 1463d14c5d2SYehuda Sadeh void *start = pbyval; 1473d14c5d2SYehuda Sadeh u32 magic; 148546f04efSSage Weil u32 num_name_maps; 1493d14c5d2SYehuda Sadeh 1503d14c5d2SYehuda Sadeh dout("crush_decode %p to %p len %d\n", *p, end, (int)(end - *p)); 1513d14c5d2SYehuda Sadeh 1523d14c5d2SYehuda Sadeh c = kzalloc(sizeof(*c), GFP_NOFS); 1533d14c5d2SYehuda Sadeh if (c == NULL) 1543d14c5d2SYehuda Sadeh return ERR_PTR(-ENOMEM); 1553d14c5d2SYehuda Sadeh 156546f04efSSage Weil /* set tunables to default values */ 157546f04efSSage Weil c->choose_local_tries = 2; 158546f04efSSage Weil c->choose_local_fallback_tries = 5; 159546f04efSSage Weil c->choose_total_tries = 19; 1601604f488SJim Schutt c->chooseleaf_descend_once = 0; 161546f04efSSage Weil 1623d14c5d2SYehuda Sadeh ceph_decode_need(p, end, 4*sizeof(u32), bad); 1633d14c5d2SYehuda Sadeh magic = ceph_decode_32(p); 1643d14c5d2SYehuda Sadeh if (magic != CRUSH_MAGIC) { 1653d14c5d2SYehuda Sadeh pr_err("crush_decode magic %x != current %x\n", 16695c96174SEric Dumazet (unsigned int)magic, (unsigned int)CRUSH_MAGIC); 1673d14c5d2SYehuda Sadeh goto bad; 1683d14c5d2SYehuda Sadeh } 1693d14c5d2SYehuda Sadeh c->max_buckets = ceph_decode_32(p); 1703d14c5d2SYehuda Sadeh c->max_rules = ceph_decode_32(p); 1713d14c5d2SYehuda Sadeh c->max_devices = ceph_decode_32(p); 1723d14c5d2SYehuda Sadeh 1733d14c5d2SYehuda Sadeh c->buckets = kcalloc(c->max_buckets, sizeof(*c->buckets), GFP_NOFS); 1743d14c5d2SYehuda Sadeh if (c->buckets == NULL) 1753d14c5d2SYehuda Sadeh goto badmem; 1763d14c5d2SYehuda Sadeh c->rules = kcalloc(c->max_rules, sizeof(*c->rules), GFP_NOFS); 1773d14c5d2SYehuda Sadeh if (c->rules == NULL) 1783d14c5d2SYehuda Sadeh goto badmem; 1793d14c5d2SYehuda Sadeh 1803d14c5d2SYehuda Sadeh /* buckets */ 1813d14c5d2SYehuda Sadeh for (i = 0; i < c->max_buckets; i++) { 1823d14c5d2SYehuda Sadeh int size = 0; 1833d14c5d2SYehuda Sadeh u32 alg; 1843d14c5d2SYehuda Sadeh struct crush_bucket *b; 1853d14c5d2SYehuda Sadeh 1863d14c5d2SYehuda Sadeh ceph_decode_32_safe(p, end, alg, bad); 1873d14c5d2SYehuda Sadeh if (alg == 0) { 1883d14c5d2SYehuda Sadeh c->buckets[i] = NULL; 1893d14c5d2SYehuda Sadeh continue; 1903d14c5d2SYehuda Sadeh } 1913d14c5d2SYehuda Sadeh dout("crush_decode bucket %d off %x %p to %p\n", 1923d14c5d2SYehuda Sadeh i, (int)(*p-start), *p, end); 1933d14c5d2SYehuda Sadeh 1943d14c5d2SYehuda Sadeh switch (alg) { 1953d14c5d2SYehuda Sadeh case CRUSH_BUCKET_UNIFORM: 1963d14c5d2SYehuda Sadeh size = sizeof(struct crush_bucket_uniform); 1973d14c5d2SYehuda Sadeh break; 1983d14c5d2SYehuda Sadeh case CRUSH_BUCKET_LIST: 1993d14c5d2SYehuda Sadeh size = sizeof(struct crush_bucket_list); 2003d14c5d2SYehuda Sadeh break; 2013d14c5d2SYehuda Sadeh case CRUSH_BUCKET_TREE: 2023d14c5d2SYehuda Sadeh size = sizeof(struct crush_bucket_tree); 2033d14c5d2SYehuda Sadeh break; 2043d14c5d2SYehuda Sadeh case CRUSH_BUCKET_STRAW: 2053d14c5d2SYehuda Sadeh size = sizeof(struct crush_bucket_straw); 2063d14c5d2SYehuda Sadeh break; 2073d14c5d2SYehuda Sadeh default: 2083d14c5d2SYehuda Sadeh err = -EINVAL; 2093d14c5d2SYehuda Sadeh goto bad; 2103d14c5d2SYehuda Sadeh } 2113d14c5d2SYehuda Sadeh BUG_ON(size == 0); 2123d14c5d2SYehuda Sadeh b = c->buckets[i] = kzalloc(size, GFP_NOFS); 2133d14c5d2SYehuda Sadeh if (b == NULL) 2143d14c5d2SYehuda Sadeh goto badmem; 2153d14c5d2SYehuda Sadeh 2163d14c5d2SYehuda Sadeh ceph_decode_need(p, end, 4*sizeof(u32), bad); 2173d14c5d2SYehuda Sadeh b->id = ceph_decode_32(p); 2183d14c5d2SYehuda Sadeh b->type = ceph_decode_16(p); 2193d14c5d2SYehuda Sadeh b->alg = ceph_decode_8(p); 2203d14c5d2SYehuda Sadeh b->hash = ceph_decode_8(p); 2213d14c5d2SYehuda Sadeh b->weight = ceph_decode_32(p); 2223d14c5d2SYehuda Sadeh b->size = ceph_decode_32(p); 2233d14c5d2SYehuda Sadeh 2243d14c5d2SYehuda Sadeh dout("crush_decode bucket size %d off %x %p to %p\n", 2253d14c5d2SYehuda Sadeh b->size, (int)(*p-start), *p, end); 2263d14c5d2SYehuda Sadeh 2273d14c5d2SYehuda Sadeh b->items = kcalloc(b->size, sizeof(__s32), GFP_NOFS); 2283d14c5d2SYehuda Sadeh if (b->items == NULL) 2293d14c5d2SYehuda Sadeh goto badmem; 2303d14c5d2SYehuda Sadeh b->perm = kcalloc(b->size, sizeof(u32), GFP_NOFS); 2313d14c5d2SYehuda Sadeh if (b->perm == NULL) 2323d14c5d2SYehuda Sadeh goto badmem; 2333d14c5d2SYehuda Sadeh b->perm_n = 0; 2343d14c5d2SYehuda Sadeh 2353d14c5d2SYehuda Sadeh ceph_decode_need(p, end, b->size*sizeof(u32), bad); 2363d14c5d2SYehuda Sadeh for (j = 0; j < b->size; j++) 2373d14c5d2SYehuda Sadeh b->items[j] = ceph_decode_32(p); 2383d14c5d2SYehuda Sadeh 2393d14c5d2SYehuda Sadeh switch (b->alg) { 2403d14c5d2SYehuda Sadeh case CRUSH_BUCKET_UNIFORM: 2413d14c5d2SYehuda Sadeh err = crush_decode_uniform_bucket(p, end, 2423d14c5d2SYehuda Sadeh (struct crush_bucket_uniform *)b); 2433d14c5d2SYehuda Sadeh if (err < 0) 2443d14c5d2SYehuda Sadeh goto bad; 2453d14c5d2SYehuda Sadeh break; 2463d14c5d2SYehuda Sadeh case CRUSH_BUCKET_LIST: 2473d14c5d2SYehuda Sadeh err = crush_decode_list_bucket(p, end, 2483d14c5d2SYehuda Sadeh (struct crush_bucket_list *)b); 2493d14c5d2SYehuda Sadeh if (err < 0) 2503d14c5d2SYehuda Sadeh goto bad; 2513d14c5d2SYehuda Sadeh break; 2523d14c5d2SYehuda Sadeh case CRUSH_BUCKET_TREE: 2533d14c5d2SYehuda Sadeh err = crush_decode_tree_bucket(p, end, 2543d14c5d2SYehuda Sadeh (struct crush_bucket_tree *)b); 2553d14c5d2SYehuda Sadeh if (err < 0) 2563d14c5d2SYehuda Sadeh goto bad; 2573d14c5d2SYehuda Sadeh break; 2583d14c5d2SYehuda Sadeh case CRUSH_BUCKET_STRAW: 2593d14c5d2SYehuda Sadeh err = crush_decode_straw_bucket(p, end, 2603d14c5d2SYehuda Sadeh (struct crush_bucket_straw *)b); 2613d14c5d2SYehuda Sadeh if (err < 0) 2623d14c5d2SYehuda Sadeh goto bad; 2633d14c5d2SYehuda Sadeh break; 2643d14c5d2SYehuda Sadeh } 2653d14c5d2SYehuda Sadeh } 2663d14c5d2SYehuda Sadeh 2673d14c5d2SYehuda Sadeh /* rules */ 2683d14c5d2SYehuda Sadeh dout("rule vec is %p\n", c->rules); 2693d14c5d2SYehuda Sadeh for (i = 0; i < c->max_rules; i++) { 2703d14c5d2SYehuda Sadeh u32 yes; 2713d14c5d2SYehuda Sadeh struct crush_rule *r; 2723d14c5d2SYehuda Sadeh 2733d14c5d2SYehuda Sadeh ceph_decode_32_safe(p, end, yes, bad); 2743d14c5d2SYehuda Sadeh if (!yes) { 2753d14c5d2SYehuda Sadeh dout("crush_decode NO rule %d off %x %p to %p\n", 2763d14c5d2SYehuda Sadeh i, (int)(*p-start), *p, end); 2773d14c5d2SYehuda Sadeh c->rules[i] = NULL; 2783d14c5d2SYehuda Sadeh continue; 2793d14c5d2SYehuda Sadeh } 2803d14c5d2SYehuda Sadeh 2813d14c5d2SYehuda Sadeh dout("crush_decode rule %d off %x %p to %p\n", 2823d14c5d2SYehuda Sadeh i, (int)(*p-start), *p, end); 2833d14c5d2SYehuda Sadeh 2843d14c5d2SYehuda Sadeh /* len */ 2853d14c5d2SYehuda Sadeh ceph_decode_32_safe(p, end, yes, bad); 2863d14c5d2SYehuda Sadeh #if BITS_PER_LONG == 32 2873d14c5d2SYehuda Sadeh err = -EINVAL; 28864486697SXi Wang if (yes > (ULONG_MAX - sizeof(*r)) 28964486697SXi Wang / sizeof(struct crush_rule_step)) 2903d14c5d2SYehuda Sadeh goto bad; 2913d14c5d2SYehuda Sadeh #endif 2923d14c5d2SYehuda Sadeh r = c->rules[i] = kmalloc(sizeof(*r) + 2933d14c5d2SYehuda Sadeh yes*sizeof(struct crush_rule_step), 2943d14c5d2SYehuda Sadeh GFP_NOFS); 2953d14c5d2SYehuda Sadeh if (r == NULL) 2963d14c5d2SYehuda Sadeh goto badmem; 2973d14c5d2SYehuda Sadeh dout(" rule %d is at %p\n", i, r); 2983d14c5d2SYehuda Sadeh r->len = yes; 2993d14c5d2SYehuda Sadeh ceph_decode_copy_safe(p, end, &r->mask, 4, bad); /* 4 u8's */ 3003d14c5d2SYehuda Sadeh ceph_decode_need(p, end, r->len*3*sizeof(u32), bad); 3013d14c5d2SYehuda Sadeh for (j = 0; j < r->len; j++) { 3023d14c5d2SYehuda Sadeh r->steps[j].op = ceph_decode_32(p); 3033d14c5d2SYehuda Sadeh r->steps[j].arg1 = ceph_decode_32(p); 3043d14c5d2SYehuda Sadeh r->steps[j].arg2 = ceph_decode_32(p); 3053d14c5d2SYehuda Sadeh } 3063d14c5d2SYehuda Sadeh } 3073d14c5d2SYehuda Sadeh 3083d14c5d2SYehuda Sadeh /* ignore trailing name maps. */ 309546f04efSSage Weil for (num_name_maps = 0; num_name_maps < 3; num_name_maps++) { 310546f04efSSage Weil err = skip_name_map(p, end); 311546f04efSSage Weil if (err < 0) 312546f04efSSage Weil goto done; 313546f04efSSage Weil } 3143d14c5d2SYehuda Sadeh 315546f04efSSage Weil /* tunables */ 316546f04efSSage Weil ceph_decode_need(p, end, 3*sizeof(u32), done); 317546f04efSSage Weil c->choose_local_tries = ceph_decode_32(p); 318546f04efSSage Weil c->choose_local_fallback_tries = ceph_decode_32(p); 319546f04efSSage Weil c->choose_total_tries = ceph_decode_32(p); 320546f04efSSage Weil dout("crush decode tunable choose_local_tries = %d", 321546f04efSSage Weil c->choose_local_tries); 322546f04efSSage Weil dout("crush decode tunable choose_local_fallback_tries = %d", 323546f04efSSage Weil c->choose_local_fallback_tries); 324546f04efSSage Weil dout("crush decode tunable choose_total_tries = %d", 325546f04efSSage Weil c->choose_total_tries); 326546f04efSSage Weil 3271604f488SJim Schutt ceph_decode_need(p, end, sizeof(u32), done); 3281604f488SJim Schutt c->chooseleaf_descend_once = ceph_decode_32(p); 3291604f488SJim Schutt dout("crush decode tunable chooseleaf_descend_once = %d", 3301604f488SJim Schutt c->chooseleaf_descend_once); 3311604f488SJim Schutt 332546f04efSSage Weil done: 3333d14c5d2SYehuda Sadeh dout("crush_decode success\n"); 3343d14c5d2SYehuda Sadeh return c; 3353d14c5d2SYehuda Sadeh 3363d14c5d2SYehuda Sadeh badmem: 3373d14c5d2SYehuda Sadeh err = -ENOMEM; 3383d14c5d2SYehuda Sadeh bad: 3393d14c5d2SYehuda Sadeh dout("crush_decode fail %d\n", err); 3403d14c5d2SYehuda Sadeh crush_destroy(c); 3413d14c5d2SYehuda Sadeh return ERR_PTR(err); 3423d14c5d2SYehuda Sadeh } 3433d14c5d2SYehuda Sadeh 3443d14c5d2SYehuda Sadeh /* 3453d14c5d2SYehuda Sadeh * rbtree of pg_mapping for handling pg_temp (explicit mapping of pgid 3469686f94cSIlya Dryomov * to a set of osds) and primary_temp (explicit primary setting) 3473d14c5d2SYehuda Sadeh */ 3485b191d99SSage Weil static int pgid_cmp(struct ceph_pg l, struct ceph_pg r) 3493d14c5d2SYehuda Sadeh { 3505b191d99SSage Weil if (l.pool < r.pool) 3513d14c5d2SYehuda Sadeh return -1; 3525b191d99SSage Weil if (l.pool > r.pool) 3535b191d99SSage Weil return 1; 3545b191d99SSage Weil if (l.seed < r.seed) 3555b191d99SSage Weil return -1; 3565b191d99SSage Weil if (l.seed > r.seed) 3573d14c5d2SYehuda Sadeh return 1; 3583d14c5d2SYehuda Sadeh return 0; 3593d14c5d2SYehuda Sadeh } 3603d14c5d2SYehuda Sadeh 3613d14c5d2SYehuda Sadeh static int __insert_pg_mapping(struct ceph_pg_mapping *new, 3623d14c5d2SYehuda Sadeh struct rb_root *root) 3633d14c5d2SYehuda Sadeh { 3643d14c5d2SYehuda Sadeh struct rb_node **p = &root->rb_node; 3653d14c5d2SYehuda Sadeh struct rb_node *parent = NULL; 3663d14c5d2SYehuda Sadeh struct ceph_pg_mapping *pg = NULL; 3673d14c5d2SYehuda Sadeh int c; 3683d14c5d2SYehuda Sadeh 3698adc8b3dSSage Weil dout("__insert_pg_mapping %llx %p\n", *(u64 *)&new->pgid, new); 3703d14c5d2SYehuda Sadeh while (*p) { 3713d14c5d2SYehuda Sadeh parent = *p; 3723d14c5d2SYehuda Sadeh pg = rb_entry(parent, struct ceph_pg_mapping, node); 3733d14c5d2SYehuda Sadeh c = pgid_cmp(new->pgid, pg->pgid); 3743d14c5d2SYehuda Sadeh if (c < 0) 3753d14c5d2SYehuda Sadeh p = &(*p)->rb_left; 3763d14c5d2SYehuda Sadeh else if (c > 0) 3773d14c5d2SYehuda Sadeh p = &(*p)->rb_right; 3783d14c5d2SYehuda Sadeh else 3793d14c5d2SYehuda Sadeh return -EEXIST; 3803d14c5d2SYehuda Sadeh } 3813d14c5d2SYehuda Sadeh 3823d14c5d2SYehuda Sadeh rb_link_node(&new->node, parent, p); 3833d14c5d2SYehuda Sadeh rb_insert_color(&new->node, root); 3843d14c5d2SYehuda Sadeh return 0; 3853d14c5d2SYehuda Sadeh } 3863d14c5d2SYehuda Sadeh 3873d14c5d2SYehuda Sadeh static struct ceph_pg_mapping *__lookup_pg_mapping(struct rb_root *root, 3885b191d99SSage Weil struct ceph_pg pgid) 3893d14c5d2SYehuda Sadeh { 3903d14c5d2SYehuda Sadeh struct rb_node *n = root->rb_node; 3913d14c5d2SYehuda Sadeh struct ceph_pg_mapping *pg; 3923d14c5d2SYehuda Sadeh int c; 3933d14c5d2SYehuda Sadeh 3943d14c5d2SYehuda Sadeh while (n) { 3953d14c5d2SYehuda Sadeh pg = rb_entry(n, struct ceph_pg_mapping, node); 3963d14c5d2SYehuda Sadeh c = pgid_cmp(pgid, pg->pgid); 3978adc8b3dSSage Weil if (c < 0) { 3983d14c5d2SYehuda Sadeh n = n->rb_left; 3998adc8b3dSSage Weil } else if (c > 0) { 4003d14c5d2SYehuda Sadeh n = n->rb_right; 4018adc8b3dSSage Weil } else { 4025b191d99SSage Weil dout("__lookup_pg_mapping %lld.%x got %p\n", 4035b191d99SSage Weil pgid.pool, pgid.seed, pg); 4043d14c5d2SYehuda Sadeh return pg; 4053d14c5d2SYehuda Sadeh } 4068adc8b3dSSage Weil } 4073d14c5d2SYehuda Sadeh return NULL; 4083d14c5d2SYehuda Sadeh } 4093d14c5d2SYehuda Sadeh 4105b191d99SSage Weil static int __remove_pg_mapping(struct rb_root *root, struct ceph_pg pgid) 4118adc8b3dSSage Weil { 4128adc8b3dSSage Weil struct ceph_pg_mapping *pg = __lookup_pg_mapping(root, pgid); 4138adc8b3dSSage Weil 4148adc8b3dSSage Weil if (pg) { 4155b191d99SSage Weil dout("__remove_pg_mapping %lld.%x %p\n", pgid.pool, pgid.seed, 4165b191d99SSage Weil pg); 4178adc8b3dSSage Weil rb_erase(&pg->node, root); 4188adc8b3dSSage Weil kfree(pg); 4198adc8b3dSSage Weil return 0; 4208adc8b3dSSage Weil } 4215b191d99SSage Weil dout("__remove_pg_mapping %lld.%x dne\n", pgid.pool, pgid.seed); 4228adc8b3dSSage Weil return -ENOENT; 4238adc8b3dSSage Weil } 4248adc8b3dSSage Weil 4253d14c5d2SYehuda Sadeh /* 4263d14c5d2SYehuda Sadeh * rbtree of pg pool info 4273d14c5d2SYehuda Sadeh */ 4283d14c5d2SYehuda Sadeh static int __insert_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *new) 4293d14c5d2SYehuda Sadeh { 4303d14c5d2SYehuda Sadeh struct rb_node **p = &root->rb_node; 4313d14c5d2SYehuda Sadeh struct rb_node *parent = NULL; 4323d14c5d2SYehuda Sadeh struct ceph_pg_pool_info *pi = NULL; 4333d14c5d2SYehuda Sadeh 4343d14c5d2SYehuda Sadeh while (*p) { 4353d14c5d2SYehuda Sadeh parent = *p; 4363d14c5d2SYehuda Sadeh pi = rb_entry(parent, struct ceph_pg_pool_info, node); 4373d14c5d2SYehuda Sadeh if (new->id < pi->id) 4383d14c5d2SYehuda Sadeh p = &(*p)->rb_left; 4393d14c5d2SYehuda Sadeh else if (new->id > pi->id) 4403d14c5d2SYehuda Sadeh p = &(*p)->rb_right; 4413d14c5d2SYehuda Sadeh else 4423d14c5d2SYehuda Sadeh return -EEXIST; 4433d14c5d2SYehuda Sadeh } 4443d14c5d2SYehuda Sadeh 4453d14c5d2SYehuda Sadeh rb_link_node(&new->node, parent, p); 4463d14c5d2SYehuda Sadeh rb_insert_color(&new->node, root); 4473d14c5d2SYehuda Sadeh return 0; 4483d14c5d2SYehuda Sadeh } 4493d14c5d2SYehuda Sadeh 4504f6a7e5eSSage Weil static struct ceph_pg_pool_info *__lookup_pg_pool(struct rb_root *root, u64 id) 4513d14c5d2SYehuda Sadeh { 4523d14c5d2SYehuda Sadeh struct ceph_pg_pool_info *pi; 4533d14c5d2SYehuda Sadeh struct rb_node *n = root->rb_node; 4543d14c5d2SYehuda Sadeh 4553d14c5d2SYehuda Sadeh while (n) { 4563d14c5d2SYehuda Sadeh pi = rb_entry(n, struct ceph_pg_pool_info, node); 4573d14c5d2SYehuda Sadeh if (id < pi->id) 4583d14c5d2SYehuda Sadeh n = n->rb_left; 4593d14c5d2SYehuda Sadeh else if (id > pi->id) 4603d14c5d2SYehuda Sadeh n = n->rb_right; 4613d14c5d2SYehuda Sadeh else 4623d14c5d2SYehuda Sadeh return pi; 4633d14c5d2SYehuda Sadeh } 4643d14c5d2SYehuda Sadeh return NULL; 4653d14c5d2SYehuda Sadeh } 4663d14c5d2SYehuda Sadeh 467ce7f6a27SIlya Dryomov struct ceph_pg_pool_info *ceph_pg_pool_by_id(struct ceph_osdmap *map, u64 id) 468ce7f6a27SIlya Dryomov { 469ce7f6a27SIlya Dryomov return __lookup_pg_pool(&map->pg_pools, id); 470ce7f6a27SIlya Dryomov } 471ce7f6a27SIlya Dryomov 47272afc71fSAlex Elder const char *ceph_pg_pool_name_by_id(struct ceph_osdmap *map, u64 id) 47372afc71fSAlex Elder { 47472afc71fSAlex Elder struct ceph_pg_pool_info *pi; 47572afc71fSAlex Elder 47672afc71fSAlex Elder if (id == CEPH_NOPOOL) 47772afc71fSAlex Elder return NULL; 47872afc71fSAlex Elder 47972afc71fSAlex Elder if (WARN_ON_ONCE(id > (u64) INT_MAX)) 48072afc71fSAlex Elder return NULL; 48172afc71fSAlex Elder 48272afc71fSAlex Elder pi = __lookup_pg_pool(&map->pg_pools, (int) id); 48372afc71fSAlex Elder 48472afc71fSAlex Elder return pi ? pi->name : NULL; 48572afc71fSAlex Elder } 48672afc71fSAlex Elder EXPORT_SYMBOL(ceph_pg_pool_name_by_id); 48772afc71fSAlex Elder 4883d14c5d2SYehuda Sadeh int ceph_pg_poolid_by_name(struct ceph_osdmap *map, const char *name) 4893d14c5d2SYehuda Sadeh { 4903d14c5d2SYehuda Sadeh struct rb_node *rbp; 4913d14c5d2SYehuda Sadeh 4923d14c5d2SYehuda Sadeh for (rbp = rb_first(&map->pg_pools); rbp; rbp = rb_next(rbp)) { 4933d14c5d2SYehuda Sadeh struct ceph_pg_pool_info *pi = 4943d14c5d2SYehuda Sadeh rb_entry(rbp, struct ceph_pg_pool_info, node); 4953d14c5d2SYehuda Sadeh if (pi->name && strcmp(pi->name, name) == 0) 4963d14c5d2SYehuda Sadeh return pi->id; 4973d14c5d2SYehuda Sadeh } 4983d14c5d2SYehuda Sadeh return -ENOENT; 4993d14c5d2SYehuda Sadeh } 5003d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_pg_poolid_by_name); 5013d14c5d2SYehuda Sadeh 5023d14c5d2SYehuda Sadeh static void __remove_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *pi) 5033d14c5d2SYehuda Sadeh { 5043d14c5d2SYehuda Sadeh rb_erase(&pi->node, root); 5053d14c5d2SYehuda Sadeh kfree(pi->name); 5063d14c5d2SYehuda Sadeh kfree(pi); 5073d14c5d2SYehuda Sadeh } 5083d14c5d2SYehuda Sadeh 5090f70c7eeSIlya Dryomov static int decode_pool(void **p, void *end, struct ceph_pg_pool_info *pi) 5103d14c5d2SYehuda Sadeh { 5114f6a7e5eSSage Weil u8 ev, cv; 5124f6a7e5eSSage Weil unsigned len, num; 5134f6a7e5eSSage Weil void *pool_end; 5143d14c5d2SYehuda Sadeh 5154f6a7e5eSSage Weil ceph_decode_need(p, end, 2 + 4, bad); 5164f6a7e5eSSage Weil ev = ceph_decode_8(p); /* encoding version */ 5174f6a7e5eSSage Weil cv = ceph_decode_8(p); /* compat version */ 5184f6a7e5eSSage Weil if (ev < 5) { 5194f6a7e5eSSage Weil pr_warning("got v %d < 5 cv %d of ceph_pg_pool\n", ev, cv); 5204f6a7e5eSSage Weil return -EINVAL; 5214f6a7e5eSSage Weil } 52217a13e40SIlya Dryomov if (cv > 9) { 52317a13e40SIlya Dryomov pr_warning("got v %d cv %d > 9 of ceph_pg_pool\n", ev, cv); 5244f6a7e5eSSage Weil return -EINVAL; 5254f6a7e5eSSage Weil } 5264f6a7e5eSSage Weil len = ceph_decode_32(p); 5274f6a7e5eSSage Weil ceph_decode_need(p, end, len, bad); 5284f6a7e5eSSage Weil pool_end = *p + len; 5293d14c5d2SYehuda Sadeh 5304f6a7e5eSSage Weil pi->type = ceph_decode_8(p); 5314f6a7e5eSSage Weil pi->size = ceph_decode_8(p); 5324f6a7e5eSSage Weil pi->crush_ruleset = ceph_decode_8(p); 5334f6a7e5eSSage Weil pi->object_hash = ceph_decode_8(p); 5344f6a7e5eSSage Weil 5354f6a7e5eSSage Weil pi->pg_num = ceph_decode_32(p); 5364f6a7e5eSSage Weil pi->pgp_num = ceph_decode_32(p); 5374f6a7e5eSSage Weil 5384f6a7e5eSSage Weil *p += 4 + 4; /* skip lpg* */ 5394f6a7e5eSSage Weil *p += 4; /* skip last_change */ 5404f6a7e5eSSage Weil *p += 8 + 4; /* skip snap_seq, snap_epoch */ 5414f6a7e5eSSage Weil 5424f6a7e5eSSage Weil /* skip snaps */ 5434f6a7e5eSSage Weil num = ceph_decode_32(p); 5444f6a7e5eSSage Weil while (num--) { 5454f6a7e5eSSage Weil *p += 8; /* snapid key */ 5464f6a7e5eSSage Weil *p += 1 + 1; /* versions */ 5474f6a7e5eSSage Weil len = ceph_decode_32(p); 5484f6a7e5eSSage Weil *p += len; 5493d14c5d2SYehuda Sadeh } 5503d14c5d2SYehuda Sadeh 55117a13e40SIlya Dryomov /* skip removed_snaps */ 5524f6a7e5eSSage Weil num = ceph_decode_32(p); 5534f6a7e5eSSage Weil *p += num * (8 + 8); 5544f6a7e5eSSage Weil 5554f6a7e5eSSage Weil *p += 8; /* skip auid */ 5564f6a7e5eSSage Weil pi->flags = ceph_decode_64(p); 55717a13e40SIlya Dryomov *p += 4; /* skip crash_replay_interval */ 55817a13e40SIlya Dryomov 55917a13e40SIlya Dryomov if (ev >= 7) 56017a13e40SIlya Dryomov *p += 1; /* skip min_size */ 56117a13e40SIlya Dryomov 56217a13e40SIlya Dryomov if (ev >= 8) 56317a13e40SIlya Dryomov *p += 8 + 8; /* skip quota_max_* */ 56417a13e40SIlya Dryomov 56517a13e40SIlya Dryomov if (ev >= 9) { 56617a13e40SIlya Dryomov /* skip tiers */ 56717a13e40SIlya Dryomov num = ceph_decode_32(p); 56817a13e40SIlya Dryomov *p += num * 8; 56917a13e40SIlya Dryomov 57017a13e40SIlya Dryomov *p += 8; /* skip tier_of */ 57117a13e40SIlya Dryomov *p += 1; /* skip cache_mode */ 57217a13e40SIlya Dryomov 57317a13e40SIlya Dryomov pi->read_tier = ceph_decode_64(p); 57417a13e40SIlya Dryomov pi->write_tier = ceph_decode_64(p); 57517a13e40SIlya Dryomov } else { 57617a13e40SIlya Dryomov pi->read_tier = -1; 57717a13e40SIlya Dryomov pi->write_tier = -1; 57817a13e40SIlya Dryomov } 5794f6a7e5eSSage Weil 5804f6a7e5eSSage Weil /* ignore the rest */ 5814f6a7e5eSSage Weil 5824f6a7e5eSSage Weil *p = pool_end; 5834f6a7e5eSSage Weil calc_pg_masks(pi); 5843d14c5d2SYehuda Sadeh return 0; 5853d14c5d2SYehuda Sadeh 5863d14c5d2SYehuda Sadeh bad: 5873d14c5d2SYehuda Sadeh return -EINVAL; 5883d14c5d2SYehuda Sadeh } 5893d14c5d2SYehuda Sadeh 5900f70c7eeSIlya Dryomov static int decode_pool_names(void **p, void *end, struct ceph_osdmap *map) 5913d14c5d2SYehuda Sadeh { 5923d14c5d2SYehuda Sadeh struct ceph_pg_pool_info *pi; 5934f6a7e5eSSage Weil u32 num, len; 5944f6a7e5eSSage Weil u64 pool; 5953d14c5d2SYehuda Sadeh 5963d14c5d2SYehuda Sadeh ceph_decode_32_safe(p, end, num, bad); 5973d14c5d2SYehuda Sadeh dout(" %d pool names\n", num); 5983d14c5d2SYehuda Sadeh while (num--) { 5994f6a7e5eSSage Weil ceph_decode_64_safe(p, end, pool, bad); 6003d14c5d2SYehuda Sadeh ceph_decode_32_safe(p, end, len, bad); 6014f6a7e5eSSage Weil dout(" pool %llu len %d\n", pool, len); 602ad3b904cSXi Wang ceph_decode_need(p, end, len, bad); 6033d14c5d2SYehuda Sadeh pi = __lookup_pg_pool(&map->pg_pools, pool); 6043d14c5d2SYehuda Sadeh if (pi) { 605ad3b904cSXi Wang char *name = kstrndup(*p, len, GFP_NOFS); 606ad3b904cSXi Wang 607ad3b904cSXi Wang if (!name) 608ad3b904cSXi Wang return -ENOMEM; 6093d14c5d2SYehuda Sadeh kfree(pi->name); 610ad3b904cSXi Wang pi->name = name; 6113d14c5d2SYehuda Sadeh dout(" name is %s\n", pi->name); 6123d14c5d2SYehuda Sadeh } 6133d14c5d2SYehuda Sadeh *p += len; 6143d14c5d2SYehuda Sadeh } 6153d14c5d2SYehuda Sadeh return 0; 6163d14c5d2SYehuda Sadeh 6173d14c5d2SYehuda Sadeh bad: 6183d14c5d2SYehuda Sadeh return -EINVAL; 6193d14c5d2SYehuda Sadeh } 6203d14c5d2SYehuda Sadeh 6213d14c5d2SYehuda Sadeh /* 6223d14c5d2SYehuda Sadeh * osd map 6233d14c5d2SYehuda Sadeh */ 6243d14c5d2SYehuda Sadeh void ceph_osdmap_destroy(struct ceph_osdmap *map) 6253d14c5d2SYehuda Sadeh { 6263d14c5d2SYehuda Sadeh dout("osdmap_destroy %p\n", map); 6273d14c5d2SYehuda Sadeh if (map->crush) 6283d14c5d2SYehuda Sadeh crush_destroy(map->crush); 6293d14c5d2SYehuda Sadeh while (!RB_EMPTY_ROOT(&map->pg_temp)) { 6303d14c5d2SYehuda Sadeh struct ceph_pg_mapping *pg = 6313d14c5d2SYehuda Sadeh rb_entry(rb_first(&map->pg_temp), 6323d14c5d2SYehuda Sadeh struct ceph_pg_mapping, node); 6333d14c5d2SYehuda Sadeh rb_erase(&pg->node, &map->pg_temp); 6343d14c5d2SYehuda Sadeh kfree(pg); 6353d14c5d2SYehuda Sadeh } 6369686f94cSIlya Dryomov while (!RB_EMPTY_ROOT(&map->primary_temp)) { 6379686f94cSIlya Dryomov struct ceph_pg_mapping *pg = 6389686f94cSIlya Dryomov rb_entry(rb_first(&map->primary_temp), 6399686f94cSIlya Dryomov struct ceph_pg_mapping, node); 6409686f94cSIlya Dryomov rb_erase(&pg->node, &map->primary_temp); 6419686f94cSIlya Dryomov kfree(pg); 6429686f94cSIlya Dryomov } 6433d14c5d2SYehuda Sadeh while (!RB_EMPTY_ROOT(&map->pg_pools)) { 6443d14c5d2SYehuda Sadeh struct ceph_pg_pool_info *pi = 6453d14c5d2SYehuda Sadeh rb_entry(rb_first(&map->pg_pools), 6463d14c5d2SYehuda Sadeh struct ceph_pg_pool_info, node); 6473d14c5d2SYehuda Sadeh __remove_pg_pool(&map->pg_pools, pi); 6483d14c5d2SYehuda Sadeh } 6493d14c5d2SYehuda Sadeh kfree(map->osd_state); 6503d14c5d2SYehuda Sadeh kfree(map->osd_weight); 6513d14c5d2SYehuda Sadeh kfree(map->osd_addr); 6522cfa34f2SIlya Dryomov kfree(map->osd_primary_affinity); 6533d14c5d2SYehuda Sadeh kfree(map); 6543d14c5d2SYehuda Sadeh } 6553d14c5d2SYehuda Sadeh 6563d14c5d2SYehuda Sadeh /* 6574d60351fSIlya Dryomov * Adjust max_osd value, (re)allocate arrays. 6584d60351fSIlya Dryomov * 6594d60351fSIlya Dryomov * The new elements are properly initialized. 6603d14c5d2SYehuda Sadeh */ 6613d14c5d2SYehuda Sadeh static int osdmap_set_max_osd(struct ceph_osdmap *map, int max) 6623d14c5d2SYehuda Sadeh { 6633d14c5d2SYehuda Sadeh u8 *state; 6643d14c5d2SYehuda Sadeh u32 *weight; 6654d60351fSIlya Dryomov struct ceph_entity_addr *addr; 6664d60351fSIlya Dryomov int i; 6673d14c5d2SYehuda Sadeh 6684d60351fSIlya Dryomov state = krealloc(map->osd_state, max*sizeof(*state), GFP_NOFS); 6694d60351fSIlya Dryomov weight = krealloc(map->osd_weight, max*sizeof(*weight), GFP_NOFS); 6704d60351fSIlya Dryomov addr = krealloc(map->osd_addr, max*sizeof(*addr), GFP_NOFS); 6714d60351fSIlya Dryomov if (!state || !weight || !addr) { 6723d14c5d2SYehuda Sadeh kfree(state); 6733d14c5d2SYehuda Sadeh kfree(weight); 6744d60351fSIlya Dryomov kfree(addr); 6754d60351fSIlya Dryomov 6763d14c5d2SYehuda Sadeh return -ENOMEM; 6773d14c5d2SYehuda Sadeh } 6783d14c5d2SYehuda Sadeh 6794d60351fSIlya Dryomov for (i = map->max_osd; i < max; i++) { 6804d60351fSIlya Dryomov state[i] = 0; 6814d60351fSIlya Dryomov weight[i] = CEPH_OSD_OUT; 6824d60351fSIlya Dryomov memset(addr + i, 0, sizeof(*addr)); 6833d14c5d2SYehuda Sadeh } 6843d14c5d2SYehuda Sadeh 6853d14c5d2SYehuda Sadeh map->osd_state = state; 6863d14c5d2SYehuda Sadeh map->osd_weight = weight; 6873d14c5d2SYehuda Sadeh map->osd_addr = addr; 6884d60351fSIlya Dryomov 6892cfa34f2SIlya Dryomov if (map->osd_primary_affinity) { 6902cfa34f2SIlya Dryomov u32 *affinity; 6912cfa34f2SIlya Dryomov 6922cfa34f2SIlya Dryomov affinity = krealloc(map->osd_primary_affinity, 6932cfa34f2SIlya Dryomov max*sizeof(*affinity), GFP_NOFS); 6942cfa34f2SIlya Dryomov if (!affinity) 6952cfa34f2SIlya Dryomov return -ENOMEM; 6962cfa34f2SIlya Dryomov 6972cfa34f2SIlya Dryomov for (i = map->max_osd; i < max; i++) 6982cfa34f2SIlya Dryomov affinity[i] = CEPH_OSD_DEFAULT_PRIMARY_AFFINITY; 6992cfa34f2SIlya Dryomov 7002cfa34f2SIlya Dryomov map->osd_primary_affinity = affinity; 7012cfa34f2SIlya Dryomov } 7022cfa34f2SIlya Dryomov 7033d14c5d2SYehuda Sadeh map->max_osd = max; 7044d60351fSIlya Dryomov 7053d14c5d2SYehuda Sadeh return 0; 7063d14c5d2SYehuda Sadeh } 7073d14c5d2SYehuda Sadeh 708ec7af972SIlya Dryomov #define OSDMAP_WRAPPER_COMPAT_VER 7 709ec7af972SIlya Dryomov #define OSDMAP_CLIENT_DATA_COMPAT_VER 1 710ec7af972SIlya Dryomov 711ec7af972SIlya Dryomov /* 712ec7af972SIlya Dryomov * Return 0 or error. On success, *v is set to 0 for old (v6) osdmaps, 713ec7af972SIlya Dryomov * to struct_v of the client_data section for new (v7 and above) 714ec7af972SIlya Dryomov * osdmaps. 715ec7af972SIlya Dryomov */ 716ec7af972SIlya Dryomov static int get_osdmap_client_data_v(void **p, void *end, 717ec7af972SIlya Dryomov const char *prefix, u8 *v) 718ec7af972SIlya Dryomov { 719ec7af972SIlya Dryomov u8 struct_v; 720ec7af972SIlya Dryomov 721ec7af972SIlya Dryomov ceph_decode_8_safe(p, end, struct_v, e_inval); 722ec7af972SIlya Dryomov if (struct_v >= 7) { 723ec7af972SIlya Dryomov u8 struct_compat; 724ec7af972SIlya Dryomov 725ec7af972SIlya Dryomov ceph_decode_8_safe(p, end, struct_compat, e_inval); 726ec7af972SIlya Dryomov if (struct_compat > OSDMAP_WRAPPER_COMPAT_VER) { 727ec7af972SIlya Dryomov pr_warning("got v %d cv %d > %d of %s ceph_osdmap\n", 728ec7af972SIlya Dryomov struct_v, struct_compat, 729ec7af972SIlya Dryomov OSDMAP_WRAPPER_COMPAT_VER, prefix); 730ec7af972SIlya Dryomov return -EINVAL; 731ec7af972SIlya Dryomov } 732ec7af972SIlya Dryomov *p += 4; /* ignore wrapper struct_len */ 733ec7af972SIlya Dryomov 734ec7af972SIlya Dryomov ceph_decode_8_safe(p, end, struct_v, e_inval); 735ec7af972SIlya Dryomov ceph_decode_8_safe(p, end, struct_compat, e_inval); 736ec7af972SIlya Dryomov if (struct_compat > OSDMAP_CLIENT_DATA_COMPAT_VER) { 737ec7af972SIlya Dryomov pr_warning("got v %d cv %d > %d of %s ceph_osdmap client data\n", 738ec7af972SIlya Dryomov struct_v, struct_compat, 739ec7af972SIlya Dryomov OSDMAP_CLIENT_DATA_COMPAT_VER, prefix); 740ec7af972SIlya Dryomov return -EINVAL; 741ec7af972SIlya Dryomov } 742ec7af972SIlya Dryomov *p += 4; /* ignore client data struct_len */ 743ec7af972SIlya Dryomov } else { 744ec7af972SIlya Dryomov u16 version; 745ec7af972SIlya Dryomov 746ec7af972SIlya Dryomov *p -= 1; 747ec7af972SIlya Dryomov ceph_decode_16_safe(p, end, version, e_inval); 748ec7af972SIlya Dryomov if (version < 6) { 749ec7af972SIlya Dryomov pr_warning("got v %d < 6 of %s ceph_osdmap\n", version, 750ec7af972SIlya Dryomov prefix); 751ec7af972SIlya Dryomov return -EINVAL; 752ec7af972SIlya Dryomov } 753ec7af972SIlya Dryomov 754ec7af972SIlya Dryomov /* old osdmap enconding */ 755ec7af972SIlya Dryomov struct_v = 0; 756ec7af972SIlya Dryomov } 757ec7af972SIlya Dryomov 758ec7af972SIlya Dryomov *v = struct_v; 759ec7af972SIlya Dryomov return 0; 760ec7af972SIlya Dryomov 761ec7af972SIlya Dryomov e_inval: 762ec7af972SIlya Dryomov return -EINVAL; 763ec7af972SIlya Dryomov } 764ec7af972SIlya Dryomov 765433fbdd3SIlya Dryomov static int __decode_pools(void **p, void *end, struct ceph_osdmap *map, 766433fbdd3SIlya Dryomov bool incremental) 767433fbdd3SIlya Dryomov { 768433fbdd3SIlya Dryomov u32 n; 769433fbdd3SIlya Dryomov 770433fbdd3SIlya Dryomov ceph_decode_32_safe(p, end, n, e_inval); 771433fbdd3SIlya Dryomov while (n--) { 772433fbdd3SIlya Dryomov struct ceph_pg_pool_info *pi; 773433fbdd3SIlya Dryomov u64 pool; 774433fbdd3SIlya Dryomov int ret; 775433fbdd3SIlya Dryomov 776433fbdd3SIlya Dryomov ceph_decode_64_safe(p, end, pool, e_inval); 777433fbdd3SIlya Dryomov 778433fbdd3SIlya Dryomov pi = __lookup_pg_pool(&map->pg_pools, pool); 779433fbdd3SIlya Dryomov if (!incremental || !pi) { 780433fbdd3SIlya Dryomov pi = kzalloc(sizeof(*pi), GFP_NOFS); 781433fbdd3SIlya Dryomov if (!pi) 782433fbdd3SIlya Dryomov return -ENOMEM; 783433fbdd3SIlya Dryomov 784433fbdd3SIlya Dryomov pi->id = pool; 785433fbdd3SIlya Dryomov 786433fbdd3SIlya Dryomov ret = __insert_pg_pool(&map->pg_pools, pi); 787433fbdd3SIlya Dryomov if (ret) { 788433fbdd3SIlya Dryomov kfree(pi); 789433fbdd3SIlya Dryomov return ret; 790433fbdd3SIlya Dryomov } 791433fbdd3SIlya Dryomov } 792433fbdd3SIlya Dryomov 793433fbdd3SIlya Dryomov ret = decode_pool(p, end, pi); 794433fbdd3SIlya Dryomov if (ret) 795433fbdd3SIlya Dryomov return ret; 796433fbdd3SIlya Dryomov } 797433fbdd3SIlya Dryomov 798433fbdd3SIlya Dryomov return 0; 799433fbdd3SIlya Dryomov 800433fbdd3SIlya Dryomov e_inval: 801433fbdd3SIlya Dryomov return -EINVAL; 802433fbdd3SIlya Dryomov } 803433fbdd3SIlya Dryomov 804433fbdd3SIlya Dryomov static int decode_pools(void **p, void *end, struct ceph_osdmap *map) 805433fbdd3SIlya Dryomov { 806433fbdd3SIlya Dryomov return __decode_pools(p, end, map, false); 807433fbdd3SIlya Dryomov } 808433fbdd3SIlya Dryomov 809433fbdd3SIlya Dryomov static int decode_new_pools(void **p, void *end, struct ceph_osdmap *map) 810433fbdd3SIlya Dryomov { 811433fbdd3SIlya Dryomov return __decode_pools(p, end, map, true); 812433fbdd3SIlya Dryomov } 813433fbdd3SIlya Dryomov 81410db634eSIlya Dryomov static int __decode_pg_temp(void **p, void *end, struct ceph_osdmap *map, 81510db634eSIlya Dryomov bool incremental) 81610db634eSIlya Dryomov { 81710db634eSIlya Dryomov u32 n; 81810db634eSIlya Dryomov 81910db634eSIlya Dryomov ceph_decode_32_safe(p, end, n, e_inval); 82010db634eSIlya Dryomov while (n--) { 82110db634eSIlya Dryomov struct ceph_pg pgid; 82210db634eSIlya Dryomov u32 len, i; 82310db634eSIlya Dryomov int ret; 82410db634eSIlya Dryomov 82510db634eSIlya Dryomov ret = ceph_decode_pgid(p, end, &pgid); 82610db634eSIlya Dryomov if (ret) 82710db634eSIlya Dryomov return ret; 82810db634eSIlya Dryomov 82910db634eSIlya Dryomov ceph_decode_32_safe(p, end, len, e_inval); 83010db634eSIlya Dryomov 83110db634eSIlya Dryomov ret = __remove_pg_mapping(&map->pg_temp, pgid); 83210db634eSIlya Dryomov BUG_ON(!incremental && ret != -ENOENT); 83310db634eSIlya Dryomov 83410db634eSIlya Dryomov if (!incremental || len > 0) { 83510db634eSIlya Dryomov struct ceph_pg_mapping *pg; 83610db634eSIlya Dryomov 83710db634eSIlya Dryomov ceph_decode_need(p, end, len*sizeof(u32), e_inval); 83810db634eSIlya Dryomov 83910db634eSIlya Dryomov if (len > (UINT_MAX - sizeof(*pg)) / sizeof(u32)) 84010db634eSIlya Dryomov return -EINVAL; 84110db634eSIlya Dryomov 84210db634eSIlya Dryomov pg = kzalloc(sizeof(*pg) + len*sizeof(u32), GFP_NOFS); 84310db634eSIlya Dryomov if (!pg) 84410db634eSIlya Dryomov return -ENOMEM; 84510db634eSIlya Dryomov 84610db634eSIlya Dryomov pg->pgid = pgid; 84735a935d7SIlya Dryomov pg->pg_temp.len = len; 84810db634eSIlya Dryomov for (i = 0; i < len; i++) 84935a935d7SIlya Dryomov pg->pg_temp.osds[i] = ceph_decode_32(p); 85010db634eSIlya Dryomov 85110db634eSIlya Dryomov ret = __insert_pg_mapping(pg, &map->pg_temp); 85210db634eSIlya Dryomov if (ret) { 85310db634eSIlya Dryomov kfree(pg); 85410db634eSIlya Dryomov return ret; 85510db634eSIlya Dryomov } 85610db634eSIlya Dryomov } 85710db634eSIlya Dryomov } 85810db634eSIlya Dryomov 85910db634eSIlya Dryomov return 0; 86010db634eSIlya Dryomov 86110db634eSIlya Dryomov e_inval: 86210db634eSIlya Dryomov return -EINVAL; 86310db634eSIlya Dryomov } 86410db634eSIlya Dryomov 86510db634eSIlya Dryomov static int decode_pg_temp(void **p, void *end, struct ceph_osdmap *map) 86610db634eSIlya Dryomov { 86710db634eSIlya Dryomov return __decode_pg_temp(p, end, map, false); 86810db634eSIlya Dryomov } 86910db634eSIlya Dryomov 87010db634eSIlya Dryomov static int decode_new_pg_temp(void **p, void *end, struct ceph_osdmap *map) 87110db634eSIlya Dryomov { 87210db634eSIlya Dryomov return __decode_pg_temp(p, end, map, true); 87310db634eSIlya Dryomov } 87410db634eSIlya Dryomov 875d286de79SIlya Dryomov static int __decode_primary_temp(void **p, void *end, struct ceph_osdmap *map, 876d286de79SIlya Dryomov bool incremental) 877d286de79SIlya Dryomov { 878d286de79SIlya Dryomov u32 n; 879d286de79SIlya Dryomov 880d286de79SIlya Dryomov ceph_decode_32_safe(p, end, n, e_inval); 881d286de79SIlya Dryomov while (n--) { 882d286de79SIlya Dryomov struct ceph_pg pgid; 883d286de79SIlya Dryomov u32 osd; 884d286de79SIlya Dryomov int ret; 885d286de79SIlya Dryomov 886d286de79SIlya Dryomov ret = ceph_decode_pgid(p, end, &pgid); 887d286de79SIlya Dryomov if (ret) 888d286de79SIlya Dryomov return ret; 889d286de79SIlya Dryomov 890d286de79SIlya Dryomov ceph_decode_32_safe(p, end, osd, e_inval); 891d286de79SIlya Dryomov 892d286de79SIlya Dryomov ret = __remove_pg_mapping(&map->primary_temp, pgid); 893d286de79SIlya Dryomov BUG_ON(!incremental && ret != -ENOENT); 894d286de79SIlya Dryomov 895d286de79SIlya Dryomov if (!incremental || osd != (u32)-1) { 896d286de79SIlya Dryomov struct ceph_pg_mapping *pg; 897d286de79SIlya Dryomov 898d286de79SIlya Dryomov pg = kzalloc(sizeof(*pg), GFP_NOFS); 899d286de79SIlya Dryomov if (!pg) 900d286de79SIlya Dryomov return -ENOMEM; 901d286de79SIlya Dryomov 902d286de79SIlya Dryomov pg->pgid = pgid; 903d286de79SIlya Dryomov pg->primary_temp.osd = osd; 904d286de79SIlya Dryomov 905d286de79SIlya Dryomov ret = __insert_pg_mapping(pg, &map->primary_temp); 906d286de79SIlya Dryomov if (ret) { 907d286de79SIlya Dryomov kfree(pg); 908d286de79SIlya Dryomov return ret; 909d286de79SIlya Dryomov } 910d286de79SIlya Dryomov } 911d286de79SIlya Dryomov } 912d286de79SIlya Dryomov 913d286de79SIlya Dryomov return 0; 914d286de79SIlya Dryomov 915d286de79SIlya Dryomov e_inval: 916d286de79SIlya Dryomov return -EINVAL; 917d286de79SIlya Dryomov } 918d286de79SIlya Dryomov 919d286de79SIlya Dryomov static int decode_primary_temp(void **p, void *end, struct ceph_osdmap *map) 920d286de79SIlya Dryomov { 921d286de79SIlya Dryomov return __decode_primary_temp(p, end, map, false); 922d286de79SIlya Dryomov } 923d286de79SIlya Dryomov 924d286de79SIlya Dryomov static int decode_new_primary_temp(void **p, void *end, 925d286de79SIlya Dryomov struct ceph_osdmap *map) 926d286de79SIlya Dryomov { 927d286de79SIlya Dryomov return __decode_primary_temp(p, end, map, true); 928d286de79SIlya Dryomov } 929d286de79SIlya Dryomov 9302cfa34f2SIlya Dryomov u32 ceph_get_primary_affinity(struct ceph_osdmap *map, int osd) 9312cfa34f2SIlya Dryomov { 9322cfa34f2SIlya Dryomov BUG_ON(osd >= map->max_osd); 9332cfa34f2SIlya Dryomov 9342cfa34f2SIlya Dryomov if (!map->osd_primary_affinity) 9352cfa34f2SIlya Dryomov return CEPH_OSD_DEFAULT_PRIMARY_AFFINITY; 9362cfa34f2SIlya Dryomov 9372cfa34f2SIlya Dryomov return map->osd_primary_affinity[osd]; 9382cfa34f2SIlya Dryomov } 9392cfa34f2SIlya Dryomov 9402cfa34f2SIlya Dryomov static int set_primary_affinity(struct ceph_osdmap *map, int osd, u32 aff) 9412cfa34f2SIlya Dryomov { 9422cfa34f2SIlya Dryomov BUG_ON(osd >= map->max_osd); 9432cfa34f2SIlya Dryomov 9442cfa34f2SIlya Dryomov if (!map->osd_primary_affinity) { 9452cfa34f2SIlya Dryomov int i; 9462cfa34f2SIlya Dryomov 9472cfa34f2SIlya Dryomov map->osd_primary_affinity = kmalloc(map->max_osd*sizeof(u32), 9482cfa34f2SIlya Dryomov GFP_NOFS); 9492cfa34f2SIlya Dryomov if (!map->osd_primary_affinity) 9502cfa34f2SIlya Dryomov return -ENOMEM; 9512cfa34f2SIlya Dryomov 9522cfa34f2SIlya Dryomov for (i = 0; i < map->max_osd; i++) 9532cfa34f2SIlya Dryomov map->osd_primary_affinity[i] = 9542cfa34f2SIlya Dryomov CEPH_OSD_DEFAULT_PRIMARY_AFFINITY; 9552cfa34f2SIlya Dryomov } 9562cfa34f2SIlya Dryomov 9572cfa34f2SIlya Dryomov map->osd_primary_affinity[osd] = aff; 9582cfa34f2SIlya Dryomov 9592cfa34f2SIlya Dryomov return 0; 9602cfa34f2SIlya Dryomov } 9612cfa34f2SIlya Dryomov 9623d14c5d2SYehuda Sadeh /* 9633d14c5d2SYehuda Sadeh * decode a full map. 9643d14c5d2SYehuda Sadeh */ 965a2505d63SIlya Dryomov static int osdmap_decode(void **p, void *end, struct ceph_osdmap *map) 9663d14c5d2SYehuda Sadeh { 967ec7af972SIlya Dryomov u8 struct_v; 96838a8d560SIlya Dryomov u32 epoch = 0; 9693d14c5d2SYehuda Sadeh void *start = *p; 9703977058cSIlya Dryomov u32 max; 9713977058cSIlya Dryomov u32 len, i; 972597b52f6SIlya Dryomov int err; 9733d14c5d2SYehuda Sadeh 974a2505d63SIlya Dryomov dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p)); 9753d14c5d2SYehuda Sadeh 976ec7af972SIlya Dryomov err = get_osdmap_client_data_v(p, end, "full", &struct_v); 977ec7af972SIlya Dryomov if (err) 978ec7af972SIlya Dryomov goto bad; 9793d14c5d2SYehuda Sadeh 98053bbaba9SIlya Dryomov /* fsid, epoch, created, modified */ 98153bbaba9SIlya Dryomov ceph_decode_need(p, end, sizeof(map->fsid) + sizeof(u32) + 98253bbaba9SIlya Dryomov sizeof(map->created) + sizeof(map->modified), e_inval); 9833d14c5d2SYehuda Sadeh ceph_decode_copy(p, &map->fsid, sizeof(map->fsid)); 98438a8d560SIlya Dryomov epoch = map->epoch = ceph_decode_32(p); 9853d14c5d2SYehuda Sadeh ceph_decode_copy(p, &map->created, sizeof(map->created)); 9863d14c5d2SYehuda Sadeh ceph_decode_copy(p, &map->modified, sizeof(map->modified)); 9873d14c5d2SYehuda Sadeh 988433fbdd3SIlya Dryomov /* pools */ 989433fbdd3SIlya Dryomov err = decode_pools(p, end, map); 990433fbdd3SIlya Dryomov if (err) 9913d14c5d2SYehuda Sadeh goto bad; 9923d14c5d2SYehuda Sadeh 9930f70c7eeSIlya Dryomov /* pool_name */ 9940f70c7eeSIlya Dryomov err = decode_pool_names(p, end, map); 995597b52f6SIlya Dryomov if (err) 9963d14c5d2SYehuda Sadeh goto bad; 9973d14c5d2SYehuda Sadeh 998597b52f6SIlya Dryomov ceph_decode_32_safe(p, end, map->pool_max, e_inval); 9993d14c5d2SYehuda Sadeh 1000597b52f6SIlya Dryomov ceph_decode_32_safe(p, end, map->flags, e_inval); 10013d14c5d2SYehuda Sadeh 10023977058cSIlya Dryomov /* max_osd */ 10033977058cSIlya Dryomov ceph_decode_32_safe(p, end, max, e_inval); 10043d14c5d2SYehuda Sadeh 10053d14c5d2SYehuda Sadeh /* (re)alloc osd arrays */ 10063d14c5d2SYehuda Sadeh err = osdmap_set_max_osd(map, max); 1007597b52f6SIlya Dryomov if (err) 10083d14c5d2SYehuda Sadeh goto bad; 10093d14c5d2SYehuda Sadeh 10102d88b2e0SIlya Dryomov /* osd_state, osd_weight, osd_addrs->client_addr */ 10113d14c5d2SYehuda Sadeh ceph_decode_need(p, end, 3*sizeof(u32) + 10123d14c5d2SYehuda Sadeh map->max_osd*(1 + sizeof(*map->osd_weight) + 1013597b52f6SIlya Dryomov sizeof(*map->osd_addr)), e_inval); 1014597b52f6SIlya Dryomov 10152d88b2e0SIlya Dryomov if (ceph_decode_32(p) != map->max_osd) 10162d88b2e0SIlya Dryomov goto e_inval; 10172d88b2e0SIlya Dryomov 10183d14c5d2SYehuda Sadeh ceph_decode_copy(p, map->osd_state, map->max_osd); 10193d14c5d2SYehuda Sadeh 10202d88b2e0SIlya Dryomov if (ceph_decode_32(p) != map->max_osd) 10212d88b2e0SIlya Dryomov goto e_inval; 10222d88b2e0SIlya Dryomov 10233d14c5d2SYehuda Sadeh for (i = 0; i < map->max_osd; i++) 10243d14c5d2SYehuda Sadeh map->osd_weight[i] = ceph_decode_32(p); 10253d14c5d2SYehuda Sadeh 10262d88b2e0SIlya Dryomov if (ceph_decode_32(p) != map->max_osd) 10272d88b2e0SIlya Dryomov goto e_inval; 10282d88b2e0SIlya Dryomov 10293d14c5d2SYehuda Sadeh ceph_decode_copy(p, map->osd_addr, map->max_osd*sizeof(*map->osd_addr)); 10303d14c5d2SYehuda Sadeh for (i = 0; i < map->max_osd; i++) 10313d14c5d2SYehuda Sadeh ceph_decode_addr(&map->osd_addr[i]); 10323d14c5d2SYehuda Sadeh 10333d14c5d2SYehuda Sadeh /* pg_temp */ 103410db634eSIlya Dryomov err = decode_pg_temp(p, end, map); 1035d6c0dd6bSSage Weil if (err) 1036d6c0dd6bSSage Weil goto bad; 10373d14c5d2SYehuda Sadeh 1038d286de79SIlya Dryomov /* primary_temp */ 1039d286de79SIlya Dryomov if (struct_v >= 1) { 1040d286de79SIlya Dryomov err = decode_primary_temp(p, end, map); 1041d286de79SIlya Dryomov if (err) 1042d286de79SIlya Dryomov goto bad; 1043d286de79SIlya Dryomov } 1044d286de79SIlya Dryomov 10453d14c5d2SYehuda Sadeh /* crush */ 1046597b52f6SIlya Dryomov ceph_decode_32_safe(p, end, len, e_inval); 10479902e682SIlya Dryomov map->crush = crush_decode(*p, min(*p + len, end)); 10483d14c5d2SYehuda Sadeh if (IS_ERR(map->crush)) { 10493d14c5d2SYehuda Sadeh err = PTR_ERR(map->crush); 10503d14c5d2SYehuda Sadeh map->crush = NULL; 10513d14c5d2SYehuda Sadeh goto bad; 10523d14c5d2SYehuda Sadeh } 10539902e682SIlya Dryomov *p += len; 10543d14c5d2SYehuda Sadeh 105538a8d560SIlya Dryomov /* ignore the rest */ 10563d14c5d2SYehuda Sadeh *p = end; 10573d14c5d2SYehuda Sadeh 105838a8d560SIlya Dryomov dout("full osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd); 1059a2505d63SIlya Dryomov return 0; 10603d14c5d2SYehuda Sadeh 1061597b52f6SIlya Dryomov e_inval: 1062597b52f6SIlya Dryomov err = -EINVAL; 10633d14c5d2SYehuda Sadeh bad: 106438a8d560SIlya Dryomov pr_err("corrupt full osdmap (%d) epoch %d off %d (%p of %p-%p)\n", 106538a8d560SIlya Dryomov err, epoch, (int)(*p - start), *p, start, end); 106638a8d560SIlya Dryomov print_hex_dump(KERN_DEBUG, "osdmap: ", 106738a8d560SIlya Dryomov DUMP_PREFIX_OFFSET, 16, 1, 106838a8d560SIlya Dryomov start, end - start, true); 1069a2505d63SIlya Dryomov return err; 1070a2505d63SIlya Dryomov } 1071a2505d63SIlya Dryomov 1072a2505d63SIlya Dryomov /* 1073a2505d63SIlya Dryomov * Allocate and decode a full map. 1074a2505d63SIlya Dryomov */ 1075a2505d63SIlya Dryomov struct ceph_osdmap *ceph_osdmap_decode(void **p, void *end) 1076a2505d63SIlya Dryomov { 1077a2505d63SIlya Dryomov struct ceph_osdmap *map; 1078a2505d63SIlya Dryomov int ret; 1079a2505d63SIlya Dryomov 1080a2505d63SIlya Dryomov map = kzalloc(sizeof(*map), GFP_NOFS); 1081a2505d63SIlya Dryomov if (!map) 1082a2505d63SIlya Dryomov return ERR_PTR(-ENOMEM); 1083a2505d63SIlya Dryomov 1084a2505d63SIlya Dryomov map->pg_temp = RB_ROOT; 10859686f94cSIlya Dryomov map->primary_temp = RB_ROOT; 1086a2505d63SIlya Dryomov mutex_init(&map->crush_scratch_mutex); 1087a2505d63SIlya Dryomov 1088a2505d63SIlya Dryomov ret = osdmap_decode(p, end, map); 1089a2505d63SIlya Dryomov if (ret) { 10903d14c5d2SYehuda Sadeh ceph_osdmap_destroy(map); 1091a2505d63SIlya Dryomov return ERR_PTR(ret); 1092a2505d63SIlya Dryomov } 1093a2505d63SIlya Dryomov 1094a2505d63SIlya Dryomov return map; 10953d14c5d2SYehuda Sadeh } 10963d14c5d2SYehuda Sadeh 10973d14c5d2SYehuda Sadeh /* 10983d14c5d2SYehuda Sadeh * decode and apply an incremental map update. 10993d14c5d2SYehuda Sadeh */ 11003d14c5d2SYehuda Sadeh struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end, 11013d14c5d2SYehuda Sadeh struct ceph_osdmap *map, 11023d14c5d2SYehuda Sadeh struct ceph_messenger *msgr) 11033d14c5d2SYehuda Sadeh { 11043d14c5d2SYehuda Sadeh struct crush_map *newcrush = NULL; 11053d14c5d2SYehuda Sadeh struct ceph_fsid fsid; 11063d14c5d2SYehuda Sadeh u32 epoch = 0; 11073d14c5d2SYehuda Sadeh struct ceph_timespec modified; 11084f6a7e5eSSage Weil s32 len; 11094f6a7e5eSSage Weil u64 pool; 11104f6a7e5eSSage Weil __s64 new_pool_max; 11114f6a7e5eSSage Weil __s32 new_flags, max; 11123d14c5d2SYehuda Sadeh void *start = *p; 111386f1742bSIlya Dryomov int err; 1114ec7af972SIlya Dryomov u8 struct_v; 11153d14c5d2SYehuda Sadeh 111638a8d560SIlya Dryomov dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p)); 111738a8d560SIlya Dryomov 1118ec7af972SIlya Dryomov err = get_osdmap_client_data_v(p, end, "inc", &struct_v); 1119ec7af972SIlya Dryomov if (err) 1120ec7af972SIlya Dryomov goto bad; 11213d14c5d2SYehuda Sadeh 112253bbaba9SIlya Dryomov /* fsid, epoch, modified, new_pool_max, new_flags */ 112353bbaba9SIlya Dryomov ceph_decode_need(p, end, sizeof(fsid) + sizeof(u32) + sizeof(modified) + 112453bbaba9SIlya Dryomov sizeof(u64) + sizeof(u32), e_inval); 11253d14c5d2SYehuda Sadeh ceph_decode_copy(p, &fsid, sizeof(fsid)); 11263d14c5d2SYehuda Sadeh epoch = ceph_decode_32(p); 11273d14c5d2SYehuda Sadeh BUG_ON(epoch != map->epoch+1); 11283d14c5d2SYehuda Sadeh ceph_decode_copy(p, &modified, sizeof(modified)); 11294f6a7e5eSSage Weil new_pool_max = ceph_decode_64(p); 11303d14c5d2SYehuda Sadeh new_flags = ceph_decode_32(p); 11313d14c5d2SYehuda Sadeh 11323d14c5d2SYehuda Sadeh /* full map? */ 113386f1742bSIlya Dryomov ceph_decode_32_safe(p, end, len, e_inval); 11343d14c5d2SYehuda Sadeh if (len > 0) { 11353d14c5d2SYehuda Sadeh dout("apply_incremental full map len %d, %p to %p\n", 11363d14c5d2SYehuda Sadeh len, *p, end); 1137a2505d63SIlya Dryomov return ceph_osdmap_decode(p, min(*p+len, end)); 11383d14c5d2SYehuda Sadeh } 11393d14c5d2SYehuda Sadeh 11403d14c5d2SYehuda Sadeh /* new crush? */ 114186f1742bSIlya Dryomov ceph_decode_32_safe(p, end, len, e_inval); 11423d14c5d2SYehuda Sadeh if (len > 0) { 11433d14c5d2SYehuda Sadeh newcrush = crush_decode(*p, min(*p+len, end)); 114486f1742bSIlya Dryomov if (IS_ERR(newcrush)) { 114586f1742bSIlya Dryomov err = PTR_ERR(newcrush); 114686f1742bSIlya Dryomov newcrush = NULL; 114786f1742bSIlya Dryomov goto bad; 114886f1742bSIlya Dryomov } 11493d14c5d2SYehuda Sadeh *p += len; 11503d14c5d2SYehuda Sadeh } 11513d14c5d2SYehuda Sadeh 11523d14c5d2SYehuda Sadeh /* new flags? */ 11533d14c5d2SYehuda Sadeh if (new_flags >= 0) 11543d14c5d2SYehuda Sadeh map->flags = new_flags; 11553d14c5d2SYehuda Sadeh if (new_pool_max >= 0) 11563d14c5d2SYehuda Sadeh map->pool_max = new_pool_max; 11573d14c5d2SYehuda Sadeh 11583d14c5d2SYehuda Sadeh /* new max? */ 115953bbaba9SIlya Dryomov ceph_decode_32_safe(p, end, max, e_inval); 11603d14c5d2SYehuda Sadeh if (max >= 0) { 11613d14c5d2SYehuda Sadeh err = osdmap_set_max_osd(map, max); 116286f1742bSIlya Dryomov if (err) 11633d14c5d2SYehuda Sadeh goto bad; 11643d14c5d2SYehuda Sadeh } 11653d14c5d2SYehuda Sadeh 11663d14c5d2SYehuda Sadeh map->epoch++; 116731456665SSage Weil map->modified = modified; 11683d14c5d2SYehuda Sadeh if (newcrush) { 11693d14c5d2SYehuda Sadeh if (map->crush) 11703d14c5d2SYehuda Sadeh crush_destroy(map->crush); 11713d14c5d2SYehuda Sadeh map->crush = newcrush; 11723d14c5d2SYehuda Sadeh newcrush = NULL; 11733d14c5d2SYehuda Sadeh } 11743d14c5d2SYehuda Sadeh 1175433fbdd3SIlya Dryomov /* new_pools */ 1176433fbdd3SIlya Dryomov err = decode_new_pools(p, end, map); 1177433fbdd3SIlya Dryomov if (err) 11783d14c5d2SYehuda Sadeh goto bad; 11799464d008SIlya Dryomov 11800f70c7eeSIlya Dryomov /* new_pool_names */ 11810f70c7eeSIlya Dryomov err = decode_pool_names(p, end, map); 118286f1742bSIlya Dryomov if (err) 11833d14c5d2SYehuda Sadeh goto bad; 11843d14c5d2SYehuda Sadeh 11853d14c5d2SYehuda Sadeh /* old_pool */ 118686f1742bSIlya Dryomov ceph_decode_32_safe(p, end, len, e_inval); 11873d14c5d2SYehuda Sadeh while (len--) { 11883d14c5d2SYehuda Sadeh struct ceph_pg_pool_info *pi; 11893d14c5d2SYehuda Sadeh 119086f1742bSIlya Dryomov ceph_decode_64_safe(p, end, pool, e_inval); 11913d14c5d2SYehuda Sadeh pi = __lookup_pg_pool(&map->pg_pools, pool); 11923d14c5d2SYehuda Sadeh if (pi) 11933d14c5d2SYehuda Sadeh __remove_pg_pool(&map->pg_pools, pi); 11943d14c5d2SYehuda Sadeh } 11953d14c5d2SYehuda Sadeh 11963d14c5d2SYehuda Sadeh /* new_up */ 119786f1742bSIlya Dryomov ceph_decode_32_safe(p, end, len, e_inval); 11983d14c5d2SYehuda Sadeh while (len--) { 11993d14c5d2SYehuda Sadeh u32 osd; 12003d14c5d2SYehuda Sadeh struct ceph_entity_addr addr; 120186f1742bSIlya Dryomov ceph_decode_32_safe(p, end, osd, e_inval); 120286f1742bSIlya Dryomov ceph_decode_copy_safe(p, end, &addr, sizeof(addr), e_inval); 12033d14c5d2SYehuda Sadeh ceph_decode_addr(&addr); 12043d14c5d2SYehuda Sadeh pr_info("osd%d up\n", osd); 12053d14c5d2SYehuda Sadeh BUG_ON(osd >= map->max_osd); 12063d14c5d2SYehuda Sadeh map->osd_state[osd] |= CEPH_OSD_UP; 12073d14c5d2SYehuda Sadeh map->osd_addr[osd] = addr; 12083d14c5d2SYehuda Sadeh } 12093d14c5d2SYehuda Sadeh 12107662d8ffSSage Weil /* new_state */ 121186f1742bSIlya Dryomov ceph_decode_32_safe(p, end, len, e_inval); 12123d14c5d2SYehuda Sadeh while (len--) { 12133d14c5d2SYehuda Sadeh u32 osd; 12147662d8ffSSage Weil u8 xorstate; 121586f1742bSIlya Dryomov ceph_decode_32_safe(p, end, osd, e_inval); 12167662d8ffSSage Weil xorstate = **(u8 **)p; 12173d14c5d2SYehuda Sadeh (*p)++; /* clean flag */ 12187662d8ffSSage Weil if (xorstate == 0) 12197662d8ffSSage Weil xorstate = CEPH_OSD_UP; 12207662d8ffSSage Weil if (xorstate & CEPH_OSD_UP) 12213d14c5d2SYehuda Sadeh pr_info("osd%d down\n", osd); 12223d14c5d2SYehuda Sadeh if (osd < map->max_osd) 12237662d8ffSSage Weil map->osd_state[osd] ^= xorstate; 12243d14c5d2SYehuda Sadeh } 12253d14c5d2SYehuda Sadeh 12263d14c5d2SYehuda Sadeh /* new_weight */ 122786f1742bSIlya Dryomov ceph_decode_32_safe(p, end, len, e_inval); 12283d14c5d2SYehuda Sadeh while (len--) { 12293d14c5d2SYehuda Sadeh u32 osd, off; 123086f1742bSIlya Dryomov ceph_decode_need(p, end, sizeof(u32)*2, e_inval); 12313d14c5d2SYehuda Sadeh osd = ceph_decode_32(p); 12323d14c5d2SYehuda Sadeh off = ceph_decode_32(p); 12333d14c5d2SYehuda Sadeh pr_info("osd%d weight 0x%x %s\n", osd, off, 12343d14c5d2SYehuda Sadeh off == CEPH_OSD_IN ? "(in)" : 12353d14c5d2SYehuda Sadeh (off == CEPH_OSD_OUT ? "(out)" : "")); 12363d14c5d2SYehuda Sadeh if (osd < map->max_osd) 12373d14c5d2SYehuda Sadeh map->osd_weight[osd] = off; 12383d14c5d2SYehuda Sadeh } 12393d14c5d2SYehuda Sadeh 12403d14c5d2SYehuda Sadeh /* new_pg_temp */ 124110db634eSIlya Dryomov err = decode_new_pg_temp(p, end, map); 1242d6c0dd6bSSage Weil if (err) 1243d6c0dd6bSSage Weil goto bad; 12443d14c5d2SYehuda Sadeh 1245d286de79SIlya Dryomov /* new_primary_temp */ 1246d286de79SIlya Dryomov if (struct_v >= 1) { 1247d286de79SIlya Dryomov err = decode_new_primary_temp(p, end, map); 1248d286de79SIlya Dryomov if (err) 1249d286de79SIlya Dryomov goto bad; 1250d286de79SIlya Dryomov } 1251d286de79SIlya Dryomov 12523d14c5d2SYehuda Sadeh /* ignore the rest */ 12533d14c5d2SYehuda Sadeh *p = end; 125438a8d560SIlya Dryomov 125538a8d560SIlya Dryomov dout("inc osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd); 12563d14c5d2SYehuda Sadeh return map; 12573d14c5d2SYehuda Sadeh 125886f1742bSIlya Dryomov e_inval: 125986f1742bSIlya Dryomov err = -EINVAL; 12603d14c5d2SYehuda Sadeh bad: 126138a8d560SIlya Dryomov pr_err("corrupt inc osdmap (%d) epoch %d off %d (%p of %p-%p)\n", 126238a8d560SIlya Dryomov err, epoch, (int)(*p - start), *p, start, end); 12633d14c5d2SYehuda Sadeh print_hex_dump(KERN_DEBUG, "osdmap: ", 12643d14c5d2SYehuda Sadeh DUMP_PREFIX_OFFSET, 16, 1, 12653d14c5d2SYehuda Sadeh start, end - start, true); 12663d14c5d2SYehuda Sadeh if (newcrush) 12673d14c5d2SYehuda Sadeh crush_destroy(newcrush); 12683d14c5d2SYehuda Sadeh return ERR_PTR(err); 12693d14c5d2SYehuda Sadeh } 12703d14c5d2SYehuda Sadeh 12713d14c5d2SYehuda Sadeh 12723d14c5d2SYehuda Sadeh 12733d14c5d2SYehuda Sadeh 12743d14c5d2SYehuda Sadeh /* 12753d14c5d2SYehuda Sadeh * calculate file layout from given offset, length. 12763d14c5d2SYehuda Sadeh * fill in correct oid, logical length, and object extent 12773d14c5d2SYehuda Sadeh * offset, length. 12783d14c5d2SYehuda Sadeh * 12793d14c5d2SYehuda Sadeh * for now, we write only a single su, until we can 12803d14c5d2SYehuda Sadeh * pass a stride back to the caller. 12813d14c5d2SYehuda Sadeh */ 1282d63b77f4SSage Weil int ceph_calc_file_object_mapping(struct ceph_file_layout *layout, 1283e8afad65SAlex Elder u64 off, u64 len, 12843d14c5d2SYehuda Sadeh u64 *ono, 12853d14c5d2SYehuda Sadeh u64 *oxoff, u64 *oxlen) 12863d14c5d2SYehuda Sadeh { 12873d14c5d2SYehuda Sadeh u32 osize = le32_to_cpu(layout->fl_object_size); 12883d14c5d2SYehuda Sadeh u32 su = le32_to_cpu(layout->fl_stripe_unit); 12893d14c5d2SYehuda Sadeh u32 sc = le32_to_cpu(layout->fl_stripe_count); 12903d14c5d2SYehuda Sadeh u32 bl, stripeno, stripepos, objsetno; 12913d14c5d2SYehuda Sadeh u32 su_per_object; 12923d14c5d2SYehuda Sadeh u64 t, su_offset; 12933d14c5d2SYehuda Sadeh 1294e8afad65SAlex Elder dout("mapping %llu~%llu osize %u fl_su %u\n", off, len, 12953d14c5d2SYehuda Sadeh osize, su); 1296d63b77f4SSage Weil if (su == 0 || sc == 0) 1297d63b77f4SSage Weil goto invalid; 12983d14c5d2SYehuda Sadeh su_per_object = osize / su; 1299d63b77f4SSage Weil if (su_per_object == 0) 1300d63b77f4SSage Weil goto invalid; 13013d14c5d2SYehuda Sadeh dout("osize %u / su %u = su_per_object %u\n", osize, su, 13023d14c5d2SYehuda Sadeh su_per_object); 13033d14c5d2SYehuda Sadeh 1304d63b77f4SSage Weil if ((su & ~PAGE_MASK) != 0) 1305d63b77f4SSage Weil goto invalid; 1306d63b77f4SSage Weil 13073d14c5d2SYehuda Sadeh /* bl = *off / su; */ 13083d14c5d2SYehuda Sadeh t = off; 13093d14c5d2SYehuda Sadeh do_div(t, su); 13103d14c5d2SYehuda Sadeh bl = t; 13113d14c5d2SYehuda Sadeh dout("off %llu / su %u = bl %u\n", off, su, bl); 13123d14c5d2SYehuda Sadeh 13133d14c5d2SYehuda Sadeh stripeno = bl / sc; 13143d14c5d2SYehuda Sadeh stripepos = bl % sc; 13153d14c5d2SYehuda Sadeh objsetno = stripeno / su_per_object; 13163d14c5d2SYehuda Sadeh 13173d14c5d2SYehuda Sadeh *ono = objsetno * sc + stripepos; 131895c96174SEric Dumazet dout("objset %u * sc %u = ono %u\n", objsetno, sc, (unsigned int)*ono); 13193d14c5d2SYehuda Sadeh 13203d14c5d2SYehuda Sadeh /* *oxoff = *off % layout->fl_stripe_unit; # offset in su */ 13213d14c5d2SYehuda Sadeh t = off; 13223d14c5d2SYehuda Sadeh su_offset = do_div(t, su); 13233d14c5d2SYehuda Sadeh *oxoff = su_offset + (stripeno % su_per_object) * su; 13243d14c5d2SYehuda Sadeh 13253d14c5d2SYehuda Sadeh /* 13263d14c5d2SYehuda Sadeh * Calculate the length of the extent being written to the selected 1327e8afad65SAlex Elder * object. This is the minimum of the full length requested (len) or 13283d14c5d2SYehuda Sadeh * the remainder of the current stripe being written to. 13293d14c5d2SYehuda Sadeh */ 1330e8afad65SAlex Elder *oxlen = min_t(u64, len, su - su_offset); 13313d14c5d2SYehuda Sadeh 13323d14c5d2SYehuda Sadeh dout(" obj extent %llu~%llu\n", *oxoff, *oxlen); 1333d63b77f4SSage Weil return 0; 1334d63b77f4SSage Weil 1335d63b77f4SSage Weil invalid: 1336d63b77f4SSage Weil dout(" invalid layout\n"); 1337d63b77f4SSage Weil *ono = 0; 1338d63b77f4SSage Weil *oxoff = 0; 1339d63b77f4SSage Weil *oxlen = 0; 1340d63b77f4SSage Weil return -EINVAL; 13413d14c5d2SYehuda Sadeh } 13423d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_calc_file_object_mapping); 13433d14c5d2SYehuda Sadeh 13443d14c5d2SYehuda Sadeh /* 13457c13cb64SIlya Dryomov * Calculate mapping of a (oloc, oid) pair to a PG. Should only be 13467c13cb64SIlya Dryomov * called with target's (oloc, oid), since tiering isn't taken into 13477c13cb64SIlya Dryomov * account. 13483d14c5d2SYehuda Sadeh */ 13497c13cb64SIlya Dryomov int ceph_oloc_oid_to_pg(struct ceph_osdmap *osdmap, 13507c13cb64SIlya Dryomov struct ceph_object_locator *oloc, 13517c13cb64SIlya Dryomov struct ceph_object_id *oid, 13527c13cb64SIlya Dryomov struct ceph_pg *pg_out) 13533d14c5d2SYehuda Sadeh { 13547c13cb64SIlya Dryomov struct ceph_pg_pool_info *pi; 13553d14c5d2SYehuda Sadeh 13567c13cb64SIlya Dryomov pi = __lookup_pg_pool(&osdmap->pg_pools, oloc->pool); 13577c13cb64SIlya Dryomov if (!pi) 13583d14c5d2SYehuda Sadeh return -EIO; 13593d14c5d2SYehuda Sadeh 13607c13cb64SIlya Dryomov pg_out->pool = oloc->pool; 13617c13cb64SIlya Dryomov pg_out->seed = ceph_str_hash(pi->object_hash, oid->name, 13627c13cb64SIlya Dryomov oid->name_len); 13637c13cb64SIlya Dryomov 13647c13cb64SIlya Dryomov dout("%s '%.*s' pgid %llu.%x\n", __func__, oid->name_len, oid->name, 13657c13cb64SIlya Dryomov pg_out->pool, pg_out->seed); 13663d14c5d2SYehuda Sadeh return 0; 13673d14c5d2SYehuda Sadeh } 13687c13cb64SIlya Dryomov EXPORT_SYMBOL(ceph_oloc_oid_to_pg); 13693d14c5d2SYehuda Sadeh 13709d521470SIlya Dryomov static int do_crush(struct ceph_osdmap *map, int ruleno, int x, 1371e8ef19c4SIlya Dryomov int *result, int result_max, 1372e8ef19c4SIlya Dryomov const __u32 *weight, int weight_max) 1373e8ef19c4SIlya Dryomov { 13749d521470SIlya Dryomov int r; 1375e8ef19c4SIlya Dryomov 13769d521470SIlya Dryomov BUG_ON(result_max > CEPH_PG_MAX_SIZE); 13779d521470SIlya Dryomov 13789d521470SIlya Dryomov mutex_lock(&map->crush_scratch_mutex); 13799d521470SIlya Dryomov r = crush_do_rule(map->crush, ruleno, x, result, result_max, 13809d521470SIlya Dryomov weight, weight_max, map->crush_scratch_ary); 13819d521470SIlya Dryomov mutex_unlock(&map->crush_scratch_mutex); 13829d521470SIlya Dryomov 13839d521470SIlya Dryomov return r; 1384e8ef19c4SIlya Dryomov } 1385e8ef19c4SIlya Dryomov 13863d14c5d2SYehuda Sadeh /* 13873d14c5d2SYehuda Sadeh * Calculate raw osd vector for the given pgid. Return pointer to osd 13883d14c5d2SYehuda Sadeh * array, or NULL on failure. 13893d14c5d2SYehuda Sadeh */ 13905b191d99SSage Weil static int *calc_pg_raw(struct ceph_osdmap *osdmap, struct ceph_pg pgid, 13913d14c5d2SYehuda Sadeh int *osds, int *num) 13923d14c5d2SYehuda Sadeh { 13933d14c5d2SYehuda Sadeh struct ceph_pg_mapping *pg; 13943d14c5d2SYehuda Sadeh struct ceph_pg_pool_info *pool; 13953d14c5d2SYehuda Sadeh int ruleno; 139683ca14fdSSage Weil int r; 139783ca14fdSSage Weil u32 pps; 13983d14c5d2SYehuda Sadeh 139983ca14fdSSage Weil pool = __lookup_pg_pool(&osdmap->pg_pools, pgid.pool); 1400782e182eSSage Weil if (!pool) 1401782e182eSSage Weil return NULL; 1402782e182eSSage Weil 14033d14c5d2SYehuda Sadeh /* pg_temp? */ 140483ca14fdSSage Weil pgid.seed = ceph_stable_mod(pgid.seed, pool->pg_num, 14059542cf0bSSage Weil pool->pg_num_mask); 14063d14c5d2SYehuda Sadeh pg = __lookup_pg_mapping(&osdmap->pg_temp, pgid); 14073d14c5d2SYehuda Sadeh if (pg) { 140835a935d7SIlya Dryomov *num = pg->pg_temp.len; 140935a935d7SIlya Dryomov return pg->pg_temp.osds; 14103d14c5d2SYehuda Sadeh } 14113d14c5d2SYehuda Sadeh 14123d14c5d2SYehuda Sadeh /* crush */ 14134f6a7e5eSSage Weil ruleno = crush_find_rule(osdmap->crush, pool->crush_ruleset, 14144f6a7e5eSSage Weil pool->type, pool->size); 14153d14c5d2SYehuda Sadeh if (ruleno < 0) { 141683ca14fdSSage Weil pr_err("no crush rule pool %lld ruleset %d type %d size %d\n", 141783ca14fdSSage Weil pgid.pool, pool->crush_ruleset, pool->type, 14184f6a7e5eSSage Weil pool->size); 14193d14c5d2SYehuda Sadeh return NULL; 14203d14c5d2SYehuda Sadeh } 14213d14c5d2SYehuda Sadeh 142283ca14fdSSage Weil if (pool->flags & CEPH_POOL_FLAG_HASHPSPOOL) { 142383ca14fdSSage Weil /* hash pool id and seed sothat pool PGs do not overlap */ 142483ca14fdSSage Weil pps = crush_hash32_2(CRUSH_HASH_RJENKINS1, 142583ca14fdSSage Weil ceph_stable_mod(pgid.seed, pool->pgp_num, 142683ca14fdSSage Weil pool->pgp_num_mask), 142783ca14fdSSage Weil pgid.pool); 142883ca14fdSSage Weil } else { 142983ca14fdSSage Weil /* 143083ca14fdSSage Weil * legacy ehavior: add ps and pool together. this is 143183ca14fdSSage Weil * not a great approach because the PGs from each pool 143283ca14fdSSage Weil * will overlap on top of each other: 0.5 == 1.4 == 143383ca14fdSSage Weil * 2.3 == ... 143483ca14fdSSage Weil */ 143583ca14fdSSage Weil pps = ceph_stable_mod(pgid.seed, pool->pgp_num, 143683ca14fdSSage Weil pool->pgp_num_mask) + 143783ca14fdSSage Weil (unsigned)pgid.pool; 143883ca14fdSSage Weil } 14399d521470SIlya Dryomov r = do_crush(osdmap, ruleno, pps, osds, min_t(int, pool->size, *num), 1440b3b33b0eSIlya Dryomov osdmap->osd_weight, osdmap->max_osd); 14418b393269SSage Weil if (r < 0) { 144283ca14fdSSage Weil pr_err("error %d from crush rule: pool %lld ruleset %d type %d" 144383ca14fdSSage Weil " size %d\n", r, pgid.pool, pool->crush_ruleset, 14444f6a7e5eSSage Weil pool->type, pool->size); 14458b393269SSage Weil return NULL; 14468b393269SSage Weil } 14478b393269SSage Weil *num = r; 14483d14c5d2SYehuda Sadeh return osds; 14493d14c5d2SYehuda Sadeh } 14503d14c5d2SYehuda Sadeh 14513d14c5d2SYehuda Sadeh /* 14523d14c5d2SYehuda Sadeh * Return acting set for given pgid. 14533d14c5d2SYehuda Sadeh */ 14545b191d99SSage Weil int ceph_calc_pg_acting(struct ceph_osdmap *osdmap, struct ceph_pg pgid, 14553d14c5d2SYehuda Sadeh int *acting) 14563d14c5d2SYehuda Sadeh { 14573d14c5d2SYehuda Sadeh int rawosds[CEPH_PG_MAX_SIZE], *osds; 14583d14c5d2SYehuda Sadeh int i, o, num = CEPH_PG_MAX_SIZE; 14593d14c5d2SYehuda Sadeh 14603d14c5d2SYehuda Sadeh osds = calc_pg_raw(osdmap, pgid, rawosds, &num); 14613d14c5d2SYehuda Sadeh if (!osds) 14623d14c5d2SYehuda Sadeh return -1; 14633d14c5d2SYehuda Sadeh 14643d14c5d2SYehuda Sadeh /* primary is first up osd */ 14653d14c5d2SYehuda Sadeh o = 0; 14663d14c5d2SYehuda Sadeh for (i = 0; i < num; i++) 14673d14c5d2SYehuda Sadeh if (ceph_osd_is_up(osdmap, osds[i])) 14683d14c5d2SYehuda Sadeh acting[o++] = osds[i]; 14693d14c5d2SYehuda Sadeh return o; 14703d14c5d2SYehuda Sadeh } 14713d14c5d2SYehuda Sadeh 14723d14c5d2SYehuda Sadeh /* 14733d14c5d2SYehuda Sadeh * Return primary osd for given pgid, or -1 if none. 14743d14c5d2SYehuda Sadeh */ 14755b191d99SSage Weil int ceph_calc_pg_primary(struct ceph_osdmap *osdmap, struct ceph_pg pgid) 14763d14c5d2SYehuda Sadeh { 14773d14c5d2SYehuda Sadeh int rawosds[CEPH_PG_MAX_SIZE], *osds; 14783d14c5d2SYehuda Sadeh int i, num = CEPH_PG_MAX_SIZE; 14793d14c5d2SYehuda Sadeh 14803d14c5d2SYehuda Sadeh osds = calc_pg_raw(osdmap, pgid, rawosds, &num); 14813d14c5d2SYehuda Sadeh if (!osds) 14823d14c5d2SYehuda Sadeh return -1; 14833d14c5d2SYehuda Sadeh 14843d14c5d2SYehuda Sadeh /* primary is first up osd */ 14853d14c5d2SYehuda Sadeh for (i = 0; i < num; i++) 14863d14c5d2SYehuda Sadeh if (ceph_osd_is_up(osdmap, osds[i])) 14873d14c5d2SYehuda Sadeh return osds[i]; 14883d14c5d2SYehuda Sadeh return -1; 14893d14c5d2SYehuda Sadeh } 14903d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_calc_pg_primary); 1491