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 332f140662fSIlya Dryomov ceph_decode_need(p, end, sizeof(u8), done); 333f140662fSIlya Dryomov c->chooseleaf_vary_r = ceph_decode_8(p); 334f140662fSIlya Dryomov dout("crush decode tunable chooseleaf_vary_r = %d", 335f140662fSIlya Dryomov c->chooseleaf_vary_r); 336f140662fSIlya Dryomov 337546f04efSSage Weil done: 3383d14c5d2SYehuda Sadeh dout("crush_decode success\n"); 3393d14c5d2SYehuda Sadeh return c; 3403d14c5d2SYehuda Sadeh 3413d14c5d2SYehuda Sadeh badmem: 3423d14c5d2SYehuda Sadeh err = -ENOMEM; 3433d14c5d2SYehuda Sadeh bad: 3443d14c5d2SYehuda Sadeh dout("crush_decode fail %d\n", err); 3453d14c5d2SYehuda Sadeh crush_destroy(c); 3463d14c5d2SYehuda Sadeh return ERR_PTR(err); 3473d14c5d2SYehuda Sadeh } 3483d14c5d2SYehuda Sadeh 3493d14c5d2SYehuda Sadeh /* 3503d14c5d2SYehuda Sadeh * rbtree of pg_mapping for handling pg_temp (explicit mapping of pgid 3519686f94cSIlya Dryomov * to a set of osds) and primary_temp (explicit primary setting) 3523d14c5d2SYehuda Sadeh */ 3535b191d99SSage Weil static int pgid_cmp(struct ceph_pg l, struct ceph_pg r) 3543d14c5d2SYehuda Sadeh { 3555b191d99SSage Weil if (l.pool < r.pool) 3563d14c5d2SYehuda Sadeh return -1; 3575b191d99SSage Weil if (l.pool > r.pool) 3585b191d99SSage Weil return 1; 3595b191d99SSage Weil if (l.seed < r.seed) 3605b191d99SSage Weil return -1; 3615b191d99SSage Weil if (l.seed > r.seed) 3623d14c5d2SYehuda Sadeh return 1; 3633d14c5d2SYehuda Sadeh return 0; 3643d14c5d2SYehuda Sadeh } 3653d14c5d2SYehuda Sadeh 3663d14c5d2SYehuda Sadeh static int __insert_pg_mapping(struct ceph_pg_mapping *new, 3673d14c5d2SYehuda Sadeh struct rb_root *root) 3683d14c5d2SYehuda Sadeh { 3693d14c5d2SYehuda Sadeh struct rb_node **p = &root->rb_node; 3703d14c5d2SYehuda Sadeh struct rb_node *parent = NULL; 3713d14c5d2SYehuda Sadeh struct ceph_pg_mapping *pg = NULL; 3723d14c5d2SYehuda Sadeh int c; 3733d14c5d2SYehuda Sadeh 3748adc8b3dSSage Weil dout("__insert_pg_mapping %llx %p\n", *(u64 *)&new->pgid, new); 3753d14c5d2SYehuda Sadeh while (*p) { 3763d14c5d2SYehuda Sadeh parent = *p; 3773d14c5d2SYehuda Sadeh pg = rb_entry(parent, struct ceph_pg_mapping, node); 3783d14c5d2SYehuda Sadeh c = pgid_cmp(new->pgid, pg->pgid); 3793d14c5d2SYehuda Sadeh if (c < 0) 3803d14c5d2SYehuda Sadeh p = &(*p)->rb_left; 3813d14c5d2SYehuda Sadeh else if (c > 0) 3823d14c5d2SYehuda Sadeh p = &(*p)->rb_right; 3833d14c5d2SYehuda Sadeh else 3843d14c5d2SYehuda Sadeh return -EEXIST; 3853d14c5d2SYehuda Sadeh } 3863d14c5d2SYehuda Sadeh 3873d14c5d2SYehuda Sadeh rb_link_node(&new->node, parent, p); 3883d14c5d2SYehuda Sadeh rb_insert_color(&new->node, root); 3893d14c5d2SYehuda Sadeh return 0; 3903d14c5d2SYehuda Sadeh } 3913d14c5d2SYehuda Sadeh 3923d14c5d2SYehuda Sadeh static struct ceph_pg_mapping *__lookup_pg_mapping(struct rb_root *root, 3935b191d99SSage Weil struct ceph_pg pgid) 3943d14c5d2SYehuda Sadeh { 3953d14c5d2SYehuda Sadeh struct rb_node *n = root->rb_node; 3963d14c5d2SYehuda Sadeh struct ceph_pg_mapping *pg; 3973d14c5d2SYehuda Sadeh int c; 3983d14c5d2SYehuda Sadeh 3993d14c5d2SYehuda Sadeh while (n) { 4003d14c5d2SYehuda Sadeh pg = rb_entry(n, struct ceph_pg_mapping, node); 4013d14c5d2SYehuda Sadeh c = pgid_cmp(pgid, pg->pgid); 4028adc8b3dSSage Weil if (c < 0) { 4033d14c5d2SYehuda Sadeh n = n->rb_left; 4048adc8b3dSSage Weil } else if (c > 0) { 4053d14c5d2SYehuda Sadeh n = n->rb_right; 4068adc8b3dSSage Weil } else { 4075b191d99SSage Weil dout("__lookup_pg_mapping %lld.%x got %p\n", 4085b191d99SSage Weil pgid.pool, pgid.seed, pg); 4093d14c5d2SYehuda Sadeh return pg; 4103d14c5d2SYehuda Sadeh } 4118adc8b3dSSage Weil } 4123d14c5d2SYehuda Sadeh return NULL; 4133d14c5d2SYehuda Sadeh } 4143d14c5d2SYehuda Sadeh 4155b191d99SSage Weil static int __remove_pg_mapping(struct rb_root *root, struct ceph_pg pgid) 4168adc8b3dSSage Weil { 4178adc8b3dSSage Weil struct ceph_pg_mapping *pg = __lookup_pg_mapping(root, pgid); 4188adc8b3dSSage Weil 4198adc8b3dSSage Weil if (pg) { 4205b191d99SSage Weil dout("__remove_pg_mapping %lld.%x %p\n", pgid.pool, pgid.seed, 4215b191d99SSage Weil pg); 4228adc8b3dSSage Weil rb_erase(&pg->node, root); 4238adc8b3dSSage Weil kfree(pg); 4248adc8b3dSSage Weil return 0; 4258adc8b3dSSage Weil } 4265b191d99SSage Weil dout("__remove_pg_mapping %lld.%x dne\n", pgid.pool, pgid.seed); 4278adc8b3dSSage Weil return -ENOENT; 4288adc8b3dSSage Weil } 4298adc8b3dSSage Weil 4303d14c5d2SYehuda Sadeh /* 4313d14c5d2SYehuda Sadeh * rbtree of pg pool info 4323d14c5d2SYehuda Sadeh */ 4333d14c5d2SYehuda Sadeh static int __insert_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *new) 4343d14c5d2SYehuda Sadeh { 4353d14c5d2SYehuda Sadeh struct rb_node **p = &root->rb_node; 4363d14c5d2SYehuda Sadeh struct rb_node *parent = NULL; 4373d14c5d2SYehuda Sadeh struct ceph_pg_pool_info *pi = NULL; 4383d14c5d2SYehuda Sadeh 4393d14c5d2SYehuda Sadeh while (*p) { 4403d14c5d2SYehuda Sadeh parent = *p; 4413d14c5d2SYehuda Sadeh pi = rb_entry(parent, struct ceph_pg_pool_info, node); 4423d14c5d2SYehuda Sadeh if (new->id < pi->id) 4433d14c5d2SYehuda Sadeh p = &(*p)->rb_left; 4443d14c5d2SYehuda Sadeh else if (new->id > pi->id) 4453d14c5d2SYehuda Sadeh p = &(*p)->rb_right; 4463d14c5d2SYehuda Sadeh else 4473d14c5d2SYehuda Sadeh return -EEXIST; 4483d14c5d2SYehuda Sadeh } 4493d14c5d2SYehuda Sadeh 4503d14c5d2SYehuda Sadeh rb_link_node(&new->node, parent, p); 4513d14c5d2SYehuda Sadeh rb_insert_color(&new->node, root); 4523d14c5d2SYehuda Sadeh return 0; 4533d14c5d2SYehuda Sadeh } 4543d14c5d2SYehuda Sadeh 4554f6a7e5eSSage Weil static struct ceph_pg_pool_info *__lookup_pg_pool(struct rb_root *root, u64 id) 4563d14c5d2SYehuda Sadeh { 4573d14c5d2SYehuda Sadeh struct ceph_pg_pool_info *pi; 4583d14c5d2SYehuda Sadeh struct rb_node *n = root->rb_node; 4593d14c5d2SYehuda Sadeh 4603d14c5d2SYehuda Sadeh while (n) { 4613d14c5d2SYehuda Sadeh pi = rb_entry(n, struct ceph_pg_pool_info, node); 4623d14c5d2SYehuda Sadeh if (id < pi->id) 4633d14c5d2SYehuda Sadeh n = n->rb_left; 4643d14c5d2SYehuda Sadeh else if (id > pi->id) 4653d14c5d2SYehuda Sadeh n = n->rb_right; 4663d14c5d2SYehuda Sadeh else 4673d14c5d2SYehuda Sadeh return pi; 4683d14c5d2SYehuda Sadeh } 4693d14c5d2SYehuda Sadeh return NULL; 4703d14c5d2SYehuda Sadeh } 4713d14c5d2SYehuda Sadeh 472ce7f6a27SIlya Dryomov struct ceph_pg_pool_info *ceph_pg_pool_by_id(struct ceph_osdmap *map, u64 id) 473ce7f6a27SIlya Dryomov { 474ce7f6a27SIlya Dryomov return __lookup_pg_pool(&map->pg_pools, id); 475ce7f6a27SIlya Dryomov } 476ce7f6a27SIlya Dryomov 47772afc71fSAlex Elder const char *ceph_pg_pool_name_by_id(struct ceph_osdmap *map, u64 id) 47872afc71fSAlex Elder { 47972afc71fSAlex Elder struct ceph_pg_pool_info *pi; 48072afc71fSAlex Elder 48172afc71fSAlex Elder if (id == CEPH_NOPOOL) 48272afc71fSAlex Elder return NULL; 48372afc71fSAlex Elder 48472afc71fSAlex Elder if (WARN_ON_ONCE(id > (u64) INT_MAX)) 48572afc71fSAlex Elder return NULL; 48672afc71fSAlex Elder 48772afc71fSAlex Elder pi = __lookup_pg_pool(&map->pg_pools, (int) id); 48872afc71fSAlex Elder 48972afc71fSAlex Elder return pi ? pi->name : NULL; 49072afc71fSAlex Elder } 49172afc71fSAlex Elder EXPORT_SYMBOL(ceph_pg_pool_name_by_id); 49272afc71fSAlex Elder 4933d14c5d2SYehuda Sadeh int ceph_pg_poolid_by_name(struct ceph_osdmap *map, const char *name) 4943d14c5d2SYehuda Sadeh { 4953d14c5d2SYehuda Sadeh struct rb_node *rbp; 4963d14c5d2SYehuda Sadeh 4973d14c5d2SYehuda Sadeh for (rbp = rb_first(&map->pg_pools); rbp; rbp = rb_next(rbp)) { 4983d14c5d2SYehuda Sadeh struct ceph_pg_pool_info *pi = 4993d14c5d2SYehuda Sadeh rb_entry(rbp, struct ceph_pg_pool_info, node); 5003d14c5d2SYehuda Sadeh if (pi->name && strcmp(pi->name, name) == 0) 5013d14c5d2SYehuda Sadeh return pi->id; 5023d14c5d2SYehuda Sadeh } 5033d14c5d2SYehuda Sadeh return -ENOENT; 5043d14c5d2SYehuda Sadeh } 5053d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_pg_poolid_by_name); 5063d14c5d2SYehuda Sadeh 5073d14c5d2SYehuda Sadeh static void __remove_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *pi) 5083d14c5d2SYehuda Sadeh { 5093d14c5d2SYehuda Sadeh rb_erase(&pi->node, root); 5103d14c5d2SYehuda Sadeh kfree(pi->name); 5113d14c5d2SYehuda Sadeh kfree(pi); 5123d14c5d2SYehuda Sadeh } 5133d14c5d2SYehuda Sadeh 5140f70c7eeSIlya Dryomov static int decode_pool(void **p, void *end, struct ceph_pg_pool_info *pi) 5153d14c5d2SYehuda Sadeh { 5164f6a7e5eSSage Weil u8 ev, cv; 5174f6a7e5eSSage Weil unsigned len, num; 5184f6a7e5eSSage Weil void *pool_end; 5193d14c5d2SYehuda Sadeh 5204f6a7e5eSSage Weil ceph_decode_need(p, end, 2 + 4, bad); 5214f6a7e5eSSage Weil ev = ceph_decode_8(p); /* encoding version */ 5224f6a7e5eSSage Weil cv = ceph_decode_8(p); /* compat version */ 5234f6a7e5eSSage Weil if (ev < 5) { 524b9a67899SJoe Perches pr_warn("got v %d < 5 cv %d of ceph_pg_pool\n", ev, cv); 5254f6a7e5eSSage Weil return -EINVAL; 5264f6a7e5eSSage Weil } 52717a13e40SIlya Dryomov if (cv > 9) { 528b9a67899SJoe Perches pr_warn("got v %d cv %d > 9 of ceph_pg_pool\n", ev, cv); 5294f6a7e5eSSage Weil return -EINVAL; 5304f6a7e5eSSage Weil } 5314f6a7e5eSSage Weil len = ceph_decode_32(p); 5324f6a7e5eSSage Weil ceph_decode_need(p, end, len, bad); 5334f6a7e5eSSage Weil pool_end = *p + len; 5343d14c5d2SYehuda Sadeh 5354f6a7e5eSSage Weil pi->type = ceph_decode_8(p); 5364f6a7e5eSSage Weil pi->size = ceph_decode_8(p); 5374f6a7e5eSSage Weil pi->crush_ruleset = ceph_decode_8(p); 5384f6a7e5eSSage Weil pi->object_hash = ceph_decode_8(p); 5394f6a7e5eSSage Weil 5404f6a7e5eSSage Weil pi->pg_num = ceph_decode_32(p); 5414f6a7e5eSSage Weil pi->pgp_num = ceph_decode_32(p); 5424f6a7e5eSSage Weil 5434f6a7e5eSSage Weil *p += 4 + 4; /* skip lpg* */ 5444f6a7e5eSSage Weil *p += 4; /* skip last_change */ 5454f6a7e5eSSage Weil *p += 8 + 4; /* skip snap_seq, snap_epoch */ 5464f6a7e5eSSage Weil 5474f6a7e5eSSage Weil /* skip snaps */ 5484f6a7e5eSSage Weil num = ceph_decode_32(p); 5494f6a7e5eSSage Weil while (num--) { 5504f6a7e5eSSage Weil *p += 8; /* snapid key */ 5514f6a7e5eSSage Weil *p += 1 + 1; /* versions */ 5524f6a7e5eSSage Weil len = ceph_decode_32(p); 5534f6a7e5eSSage Weil *p += len; 5543d14c5d2SYehuda Sadeh } 5553d14c5d2SYehuda Sadeh 55617a13e40SIlya Dryomov /* skip removed_snaps */ 5574f6a7e5eSSage Weil num = ceph_decode_32(p); 5584f6a7e5eSSage Weil *p += num * (8 + 8); 5594f6a7e5eSSage Weil 5604f6a7e5eSSage Weil *p += 8; /* skip auid */ 5614f6a7e5eSSage Weil pi->flags = ceph_decode_64(p); 56217a13e40SIlya Dryomov *p += 4; /* skip crash_replay_interval */ 56317a13e40SIlya Dryomov 56417a13e40SIlya Dryomov if (ev >= 7) 56517a13e40SIlya Dryomov *p += 1; /* skip min_size */ 56617a13e40SIlya Dryomov 56717a13e40SIlya Dryomov if (ev >= 8) 56817a13e40SIlya Dryomov *p += 8 + 8; /* skip quota_max_* */ 56917a13e40SIlya Dryomov 57017a13e40SIlya Dryomov if (ev >= 9) { 57117a13e40SIlya Dryomov /* skip tiers */ 57217a13e40SIlya Dryomov num = ceph_decode_32(p); 57317a13e40SIlya Dryomov *p += num * 8; 57417a13e40SIlya Dryomov 57517a13e40SIlya Dryomov *p += 8; /* skip tier_of */ 57617a13e40SIlya Dryomov *p += 1; /* skip cache_mode */ 57717a13e40SIlya Dryomov 57817a13e40SIlya Dryomov pi->read_tier = ceph_decode_64(p); 57917a13e40SIlya Dryomov pi->write_tier = ceph_decode_64(p); 58017a13e40SIlya Dryomov } else { 58117a13e40SIlya Dryomov pi->read_tier = -1; 58217a13e40SIlya Dryomov pi->write_tier = -1; 58317a13e40SIlya Dryomov } 5844f6a7e5eSSage Weil 5854f6a7e5eSSage Weil /* ignore the rest */ 5864f6a7e5eSSage Weil 5874f6a7e5eSSage Weil *p = pool_end; 5884f6a7e5eSSage Weil calc_pg_masks(pi); 5893d14c5d2SYehuda Sadeh return 0; 5903d14c5d2SYehuda Sadeh 5913d14c5d2SYehuda Sadeh bad: 5923d14c5d2SYehuda Sadeh return -EINVAL; 5933d14c5d2SYehuda Sadeh } 5943d14c5d2SYehuda Sadeh 5950f70c7eeSIlya Dryomov static int decode_pool_names(void **p, void *end, struct ceph_osdmap *map) 5963d14c5d2SYehuda Sadeh { 5973d14c5d2SYehuda Sadeh struct ceph_pg_pool_info *pi; 5984f6a7e5eSSage Weil u32 num, len; 5994f6a7e5eSSage Weil u64 pool; 6003d14c5d2SYehuda Sadeh 6013d14c5d2SYehuda Sadeh ceph_decode_32_safe(p, end, num, bad); 6023d14c5d2SYehuda Sadeh dout(" %d pool names\n", num); 6033d14c5d2SYehuda Sadeh while (num--) { 6044f6a7e5eSSage Weil ceph_decode_64_safe(p, end, pool, bad); 6053d14c5d2SYehuda Sadeh ceph_decode_32_safe(p, end, len, bad); 6064f6a7e5eSSage Weil dout(" pool %llu len %d\n", pool, len); 607ad3b904cSXi Wang ceph_decode_need(p, end, len, bad); 6083d14c5d2SYehuda Sadeh pi = __lookup_pg_pool(&map->pg_pools, pool); 6093d14c5d2SYehuda Sadeh if (pi) { 610ad3b904cSXi Wang char *name = kstrndup(*p, len, GFP_NOFS); 611ad3b904cSXi Wang 612ad3b904cSXi Wang if (!name) 613ad3b904cSXi Wang return -ENOMEM; 6143d14c5d2SYehuda Sadeh kfree(pi->name); 615ad3b904cSXi Wang pi->name = name; 6163d14c5d2SYehuda Sadeh dout(" name is %s\n", pi->name); 6173d14c5d2SYehuda Sadeh } 6183d14c5d2SYehuda Sadeh *p += len; 6193d14c5d2SYehuda Sadeh } 6203d14c5d2SYehuda Sadeh return 0; 6213d14c5d2SYehuda Sadeh 6223d14c5d2SYehuda Sadeh bad: 6233d14c5d2SYehuda Sadeh return -EINVAL; 6243d14c5d2SYehuda Sadeh } 6253d14c5d2SYehuda Sadeh 6263d14c5d2SYehuda Sadeh /* 6273d14c5d2SYehuda Sadeh * osd map 6283d14c5d2SYehuda Sadeh */ 6293d14c5d2SYehuda Sadeh void ceph_osdmap_destroy(struct ceph_osdmap *map) 6303d14c5d2SYehuda Sadeh { 6313d14c5d2SYehuda Sadeh dout("osdmap_destroy %p\n", map); 6323d14c5d2SYehuda Sadeh if (map->crush) 6333d14c5d2SYehuda Sadeh crush_destroy(map->crush); 6343d14c5d2SYehuda Sadeh while (!RB_EMPTY_ROOT(&map->pg_temp)) { 6353d14c5d2SYehuda Sadeh struct ceph_pg_mapping *pg = 6363d14c5d2SYehuda Sadeh rb_entry(rb_first(&map->pg_temp), 6373d14c5d2SYehuda Sadeh struct ceph_pg_mapping, node); 6383d14c5d2SYehuda Sadeh rb_erase(&pg->node, &map->pg_temp); 6393d14c5d2SYehuda Sadeh kfree(pg); 6403d14c5d2SYehuda Sadeh } 6419686f94cSIlya Dryomov while (!RB_EMPTY_ROOT(&map->primary_temp)) { 6429686f94cSIlya Dryomov struct ceph_pg_mapping *pg = 6439686f94cSIlya Dryomov rb_entry(rb_first(&map->primary_temp), 6449686f94cSIlya Dryomov struct ceph_pg_mapping, node); 6459686f94cSIlya Dryomov rb_erase(&pg->node, &map->primary_temp); 6469686f94cSIlya Dryomov kfree(pg); 6479686f94cSIlya Dryomov } 6483d14c5d2SYehuda Sadeh while (!RB_EMPTY_ROOT(&map->pg_pools)) { 6493d14c5d2SYehuda Sadeh struct ceph_pg_pool_info *pi = 6503d14c5d2SYehuda Sadeh rb_entry(rb_first(&map->pg_pools), 6513d14c5d2SYehuda Sadeh struct ceph_pg_pool_info, node); 6523d14c5d2SYehuda Sadeh __remove_pg_pool(&map->pg_pools, pi); 6533d14c5d2SYehuda Sadeh } 6543d14c5d2SYehuda Sadeh kfree(map->osd_state); 6553d14c5d2SYehuda Sadeh kfree(map->osd_weight); 6563d14c5d2SYehuda Sadeh kfree(map->osd_addr); 6572cfa34f2SIlya Dryomov kfree(map->osd_primary_affinity); 6583d14c5d2SYehuda Sadeh kfree(map); 6593d14c5d2SYehuda Sadeh } 6603d14c5d2SYehuda Sadeh 6613d14c5d2SYehuda Sadeh /* 6624d60351fSIlya Dryomov * Adjust max_osd value, (re)allocate arrays. 6634d60351fSIlya Dryomov * 6644d60351fSIlya Dryomov * The new elements are properly initialized. 6653d14c5d2SYehuda Sadeh */ 6663d14c5d2SYehuda Sadeh static int osdmap_set_max_osd(struct ceph_osdmap *map, int max) 6673d14c5d2SYehuda Sadeh { 6683d14c5d2SYehuda Sadeh u8 *state; 6693d14c5d2SYehuda Sadeh u32 *weight; 6704d60351fSIlya Dryomov struct ceph_entity_addr *addr; 6714d60351fSIlya Dryomov int i; 6723d14c5d2SYehuda Sadeh 6734d60351fSIlya Dryomov state = krealloc(map->osd_state, max*sizeof(*state), GFP_NOFS); 674589506f1SLi RongQing if (!state) 6753d14c5d2SYehuda Sadeh return -ENOMEM; 676589506f1SLi RongQing map->osd_state = state; 677589506f1SLi RongQing 678589506f1SLi RongQing weight = krealloc(map->osd_weight, max*sizeof(*weight), GFP_NOFS); 679589506f1SLi RongQing if (!weight) 680589506f1SLi RongQing return -ENOMEM; 681589506f1SLi RongQing map->osd_weight = weight; 682589506f1SLi RongQing 683589506f1SLi RongQing addr = krealloc(map->osd_addr, max*sizeof(*addr), GFP_NOFS); 684589506f1SLi RongQing if (!addr) 685589506f1SLi RongQing return -ENOMEM; 686589506f1SLi RongQing map->osd_addr = addr; 6873d14c5d2SYehuda Sadeh 6884d60351fSIlya Dryomov for (i = map->max_osd; i < max; i++) { 689589506f1SLi RongQing map->osd_state[i] = 0; 690589506f1SLi RongQing map->osd_weight[i] = CEPH_OSD_OUT; 691589506f1SLi RongQing memset(map->osd_addr + i, 0, sizeof(*map->osd_addr)); 6923d14c5d2SYehuda Sadeh } 6933d14c5d2SYehuda Sadeh 6942cfa34f2SIlya Dryomov if (map->osd_primary_affinity) { 6952cfa34f2SIlya Dryomov u32 *affinity; 6962cfa34f2SIlya Dryomov 6972cfa34f2SIlya Dryomov affinity = krealloc(map->osd_primary_affinity, 6982cfa34f2SIlya Dryomov max*sizeof(*affinity), GFP_NOFS); 6992cfa34f2SIlya Dryomov if (!affinity) 7002cfa34f2SIlya Dryomov return -ENOMEM; 701589506f1SLi RongQing map->osd_primary_affinity = affinity; 7022cfa34f2SIlya Dryomov 7032cfa34f2SIlya Dryomov for (i = map->max_osd; i < max; i++) 704589506f1SLi RongQing map->osd_primary_affinity[i] = 705589506f1SLi RongQing CEPH_OSD_DEFAULT_PRIMARY_AFFINITY; 7062cfa34f2SIlya Dryomov } 7072cfa34f2SIlya Dryomov 7083d14c5d2SYehuda Sadeh map->max_osd = max; 7094d60351fSIlya Dryomov 7103d14c5d2SYehuda Sadeh return 0; 7113d14c5d2SYehuda Sadeh } 7123d14c5d2SYehuda Sadeh 713ec7af972SIlya Dryomov #define OSDMAP_WRAPPER_COMPAT_VER 7 714ec7af972SIlya Dryomov #define OSDMAP_CLIENT_DATA_COMPAT_VER 1 715ec7af972SIlya Dryomov 716ec7af972SIlya Dryomov /* 717ec7af972SIlya Dryomov * Return 0 or error. On success, *v is set to 0 for old (v6) osdmaps, 718ec7af972SIlya Dryomov * to struct_v of the client_data section for new (v7 and above) 719ec7af972SIlya Dryomov * osdmaps. 720ec7af972SIlya Dryomov */ 721ec7af972SIlya Dryomov static int get_osdmap_client_data_v(void **p, void *end, 722ec7af972SIlya Dryomov const char *prefix, u8 *v) 723ec7af972SIlya Dryomov { 724ec7af972SIlya Dryomov u8 struct_v; 725ec7af972SIlya Dryomov 726ec7af972SIlya Dryomov ceph_decode_8_safe(p, end, struct_v, e_inval); 727ec7af972SIlya Dryomov if (struct_v >= 7) { 728ec7af972SIlya Dryomov u8 struct_compat; 729ec7af972SIlya Dryomov 730ec7af972SIlya Dryomov ceph_decode_8_safe(p, end, struct_compat, e_inval); 731ec7af972SIlya Dryomov if (struct_compat > OSDMAP_WRAPPER_COMPAT_VER) { 732b9a67899SJoe Perches pr_warn("got v %d cv %d > %d of %s ceph_osdmap\n", 733ec7af972SIlya Dryomov struct_v, struct_compat, 734ec7af972SIlya Dryomov OSDMAP_WRAPPER_COMPAT_VER, prefix); 735ec7af972SIlya Dryomov return -EINVAL; 736ec7af972SIlya Dryomov } 737ec7af972SIlya Dryomov *p += 4; /* ignore wrapper struct_len */ 738ec7af972SIlya Dryomov 739ec7af972SIlya Dryomov ceph_decode_8_safe(p, end, struct_v, e_inval); 740ec7af972SIlya Dryomov ceph_decode_8_safe(p, end, struct_compat, e_inval); 741ec7af972SIlya Dryomov if (struct_compat > OSDMAP_CLIENT_DATA_COMPAT_VER) { 742b9a67899SJoe Perches pr_warn("got v %d cv %d > %d of %s ceph_osdmap client data\n", 743ec7af972SIlya Dryomov struct_v, struct_compat, 744ec7af972SIlya Dryomov OSDMAP_CLIENT_DATA_COMPAT_VER, prefix); 745ec7af972SIlya Dryomov return -EINVAL; 746ec7af972SIlya Dryomov } 747ec7af972SIlya Dryomov *p += 4; /* ignore client data struct_len */ 748ec7af972SIlya Dryomov } else { 749ec7af972SIlya Dryomov u16 version; 750ec7af972SIlya Dryomov 751ec7af972SIlya Dryomov *p -= 1; 752ec7af972SIlya Dryomov ceph_decode_16_safe(p, end, version, e_inval); 753ec7af972SIlya Dryomov if (version < 6) { 754b9a67899SJoe Perches pr_warn("got v %d < 6 of %s ceph_osdmap\n", 755b9a67899SJoe Perches version, prefix); 756ec7af972SIlya Dryomov return -EINVAL; 757ec7af972SIlya Dryomov } 758ec7af972SIlya Dryomov 759ec7af972SIlya Dryomov /* old osdmap enconding */ 760ec7af972SIlya Dryomov struct_v = 0; 761ec7af972SIlya Dryomov } 762ec7af972SIlya Dryomov 763ec7af972SIlya Dryomov *v = struct_v; 764ec7af972SIlya Dryomov return 0; 765ec7af972SIlya Dryomov 766ec7af972SIlya Dryomov e_inval: 767ec7af972SIlya Dryomov return -EINVAL; 768ec7af972SIlya Dryomov } 769ec7af972SIlya Dryomov 770433fbdd3SIlya Dryomov static int __decode_pools(void **p, void *end, struct ceph_osdmap *map, 771433fbdd3SIlya Dryomov bool incremental) 772433fbdd3SIlya Dryomov { 773433fbdd3SIlya Dryomov u32 n; 774433fbdd3SIlya Dryomov 775433fbdd3SIlya Dryomov ceph_decode_32_safe(p, end, n, e_inval); 776433fbdd3SIlya Dryomov while (n--) { 777433fbdd3SIlya Dryomov struct ceph_pg_pool_info *pi; 778433fbdd3SIlya Dryomov u64 pool; 779433fbdd3SIlya Dryomov int ret; 780433fbdd3SIlya Dryomov 781433fbdd3SIlya Dryomov ceph_decode_64_safe(p, end, pool, e_inval); 782433fbdd3SIlya Dryomov 783433fbdd3SIlya Dryomov pi = __lookup_pg_pool(&map->pg_pools, pool); 784433fbdd3SIlya Dryomov if (!incremental || !pi) { 785433fbdd3SIlya Dryomov pi = kzalloc(sizeof(*pi), GFP_NOFS); 786433fbdd3SIlya Dryomov if (!pi) 787433fbdd3SIlya Dryomov return -ENOMEM; 788433fbdd3SIlya Dryomov 789433fbdd3SIlya Dryomov pi->id = pool; 790433fbdd3SIlya Dryomov 791433fbdd3SIlya Dryomov ret = __insert_pg_pool(&map->pg_pools, pi); 792433fbdd3SIlya Dryomov if (ret) { 793433fbdd3SIlya Dryomov kfree(pi); 794433fbdd3SIlya Dryomov return ret; 795433fbdd3SIlya Dryomov } 796433fbdd3SIlya Dryomov } 797433fbdd3SIlya Dryomov 798433fbdd3SIlya Dryomov ret = decode_pool(p, end, pi); 799433fbdd3SIlya Dryomov if (ret) 800433fbdd3SIlya Dryomov return ret; 801433fbdd3SIlya Dryomov } 802433fbdd3SIlya Dryomov 803433fbdd3SIlya Dryomov return 0; 804433fbdd3SIlya Dryomov 805433fbdd3SIlya Dryomov e_inval: 806433fbdd3SIlya Dryomov return -EINVAL; 807433fbdd3SIlya Dryomov } 808433fbdd3SIlya Dryomov 809433fbdd3SIlya Dryomov static int decode_pools(void **p, void *end, struct ceph_osdmap *map) 810433fbdd3SIlya Dryomov { 811433fbdd3SIlya Dryomov return __decode_pools(p, end, map, false); 812433fbdd3SIlya Dryomov } 813433fbdd3SIlya Dryomov 814433fbdd3SIlya Dryomov static int decode_new_pools(void **p, void *end, struct ceph_osdmap *map) 815433fbdd3SIlya Dryomov { 816433fbdd3SIlya Dryomov return __decode_pools(p, end, map, true); 817433fbdd3SIlya Dryomov } 818433fbdd3SIlya Dryomov 81910db634eSIlya Dryomov static int __decode_pg_temp(void **p, void *end, struct ceph_osdmap *map, 82010db634eSIlya Dryomov bool incremental) 82110db634eSIlya Dryomov { 82210db634eSIlya Dryomov u32 n; 82310db634eSIlya Dryomov 82410db634eSIlya Dryomov ceph_decode_32_safe(p, end, n, e_inval); 82510db634eSIlya Dryomov while (n--) { 82610db634eSIlya Dryomov struct ceph_pg pgid; 82710db634eSIlya Dryomov u32 len, i; 82810db634eSIlya Dryomov int ret; 82910db634eSIlya Dryomov 83010db634eSIlya Dryomov ret = ceph_decode_pgid(p, end, &pgid); 83110db634eSIlya Dryomov if (ret) 83210db634eSIlya Dryomov return ret; 83310db634eSIlya Dryomov 83410db634eSIlya Dryomov ceph_decode_32_safe(p, end, len, e_inval); 83510db634eSIlya Dryomov 83610db634eSIlya Dryomov ret = __remove_pg_mapping(&map->pg_temp, pgid); 83710db634eSIlya Dryomov BUG_ON(!incremental && ret != -ENOENT); 83810db634eSIlya Dryomov 83910db634eSIlya Dryomov if (!incremental || len > 0) { 84010db634eSIlya Dryomov struct ceph_pg_mapping *pg; 84110db634eSIlya Dryomov 84210db634eSIlya Dryomov ceph_decode_need(p, end, len*sizeof(u32), e_inval); 84310db634eSIlya Dryomov 84410db634eSIlya Dryomov if (len > (UINT_MAX - sizeof(*pg)) / sizeof(u32)) 84510db634eSIlya Dryomov return -EINVAL; 84610db634eSIlya Dryomov 84710db634eSIlya Dryomov pg = kzalloc(sizeof(*pg) + len*sizeof(u32), GFP_NOFS); 84810db634eSIlya Dryomov if (!pg) 84910db634eSIlya Dryomov return -ENOMEM; 85010db634eSIlya Dryomov 85110db634eSIlya Dryomov pg->pgid = pgid; 85235a935d7SIlya Dryomov pg->pg_temp.len = len; 85310db634eSIlya Dryomov for (i = 0; i < len; i++) 85435a935d7SIlya Dryomov pg->pg_temp.osds[i] = ceph_decode_32(p); 85510db634eSIlya Dryomov 85610db634eSIlya Dryomov ret = __insert_pg_mapping(pg, &map->pg_temp); 85710db634eSIlya Dryomov if (ret) { 85810db634eSIlya Dryomov kfree(pg); 85910db634eSIlya Dryomov return ret; 86010db634eSIlya Dryomov } 86110db634eSIlya Dryomov } 86210db634eSIlya Dryomov } 86310db634eSIlya Dryomov 86410db634eSIlya Dryomov return 0; 86510db634eSIlya Dryomov 86610db634eSIlya Dryomov e_inval: 86710db634eSIlya Dryomov return -EINVAL; 86810db634eSIlya Dryomov } 86910db634eSIlya Dryomov 87010db634eSIlya Dryomov static int decode_pg_temp(void **p, void *end, struct ceph_osdmap *map) 87110db634eSIlya Dryomov { 87210db634eSIlya Dryomov return __decode_pg_temp(p, end, map, false); 87310db634eSIlya Dryomov } 87410db634eSIlya Dryomov 87510db634eSIlya Dryomov static int decode_new_pg_temp(void **p, void *end, struct ceph_osdmap *map) 87610db634eSIlya Dryomov { 87710db634eSIlya Dryomov return __decode_pg_temp(p, end, map, true); 87810db634eSIlya Dryomov } 87910db634eSIlya Dryomov 880d286de79SIlya Dryomov static int __decode_primary_temp(void **p, void *end, struct ceph_osdmap *map, 881d286de79SIlya Dryomov bool incremental) 882d286de79SIlya Dryomov { 883d286de79SIlya Dryomov u32 n; 884d286de79SIlya Dryomov 885d286de79SIlya Dryomov ceph_decode_32_safe(p, end, n, e_inval); 886d286de79SIlya Dryomov while (n--) { 887d286de79SIlya Dryomov struct ceph_pg pgid; 888d286de79SIlya Dryomov u32 osd; 889d286de79SIlya Dryomov int ret; 890d286de79SIlya Dryomov 891d286de79SIlya Dryomov ret = ceph_decode_pgid(p, end, &pgid); 892d286de79SIlya Dryomov if (ret) 893d286de79SIlya Dryomov return ret; 894d286de79SIlya Dryomov 895d286de79SIlya Dryomov ceph_decode_32_safe(p, end, osd, e_inval); 896d286de79SIlya Dryomov 897d286de79SIlya Dryomov ret = __remove_pg_mapping(&map->primary_temp, pgid); 898d286de79SIlya Dryomov BUG_ON(!incremental && ret != -ENOENT); 899d286de79SIlya Dryomov 900d286de79SIlya Dryomov if (!incremental || osd != (u32)-1) { 901d286de79SIlya Dryomov struct ceph_pg_mapping *pg; 902d286de79SIlya Dryomov 903d286de79SIlya Dryomov pg = kzalloc(sizeof(*pg), GFP_NOFS); 904d286de79SIlya Dryomov if (!pg) 905d286de79SIlya Dryomov return -ENOMEM; 906d286de79SIlya Dryomov 907d286de79SIlya Dryomov pg->pgid = pgid; 908d286de79SIlya Dryomov pg->primary_temp.osd = osd; 909d286de79SIlya Dryomov 910d286de79SIlya Dryomov ret = __insert_pg_mapping(pg, &map->primary_temp); 911d286de79SIlya Dryomov if (ret) { 912d286de79SIlya Dryomov kfree(pg); 913d286de79SIlya Dryomov return ret; 914d286de79SIlya Dryomov } 915d286de79SIlya Dryomov } 916d286de79SIlya Dryomov } 917d286de79SIlya Dryomov 918d286de79SIlya Dryomov return 0; 919d286de79SIlya Dryomov 920d286de79SIlya Dryomov e_inval: 921d286de79SIlya Dryomov return -EINVAL; 922d286de79SIlya Dryomov } 923d286de79SIlya Dryomov 924d286de79SIlya Dryomov static int decode_primary_temp(void **p, void *end, struct ceph_osdmap *map) 925d286de79SIlya Dryomov { 926d286de79SIlya Dryomov return __decode_primary_temp(p, end, map, false); 927d286de79SIlya Dryomov } 928d286de79SIlya Dryomov 929d286de79SIlya Dryomov static int decode_new_primary_temp(void **p, void *end, 930d286de79SIlya Dryomov struct ceph_osdmap *map) 931d286de79SIlya Dryomov { 932d286de79SIlya Dryomov return __decode_primary_temp(p, end, map, true); 933d286de79SIlya Dryomov } 934d286de79SIlya Dryomov 9352cfa34f2SIlya Dryomov u32 ceph_get_primary_affinity(struct ceph_osdmap *map, int osd) 9362cfa34f2SIlya Dryomov { 9372cfa34f2SIlya Dryomov BUG_ON(osd >= map->max_osd); 9382cfa34f2SIlya Dryomov 9392cfa34f2SIlya Dryomov if (!map->osd_primary_affinity) 9402cfa34f2SIlya Dryomov return CEPH_OSD_DEFAULT_PRIMARY_AFFINITY; 9412cfa34f2SIlya Dryomov 9422cfa34f2SIlya Dryomov return map->osd_primary_affinity[osd]; 9432cfa34f2SIlya Dryomov } 9442cfa34f2SIlya Dryomov 9452cfa34f2SIlya Dryomov static int set_primary_affinity(struct ceph_osdmap *map, int osd, u32 aff) 9462cfa34f2SIlya Dryomov { 9472cfa34f2SIlya Dryomov BUG_ON(osd >= map->max_osd); 9482cfa34f2SIlya Dryomov 9492cfa34f2SIlya Dryomov if (!map->osd_primary_affinity) { 9502cfa34f2SIlya Dryomov int i; 9512cfa34f2SIlya Dryomov 9522cfa34f2SIlya Dryomov map->osd_primary_affinity = kmalloc(map->max_osd*sizeof(u32), 9532cfa34f2SIlya Dryomov GFP_NOFS); 9542cfa34f2SIlya Dryomov if (!map->osd_primary_affinity) 9552cfa34f2SIlya Dryomov return -ENOMEM; 9562cfa34f2SIlya Dryomov 9572cfa34f2SIlya Dryomov for (i = 0; i < map->max_osd; i++) 9582cfa34f2SIlya Dryomov map->osd_primary_affinity[i] = 9592cfa34f2SIlya Dryomov CEPH_OSD_DEFAULT_PRIMARY_AFFINITY; 9602cfa34f2SIlya Dryomov } 9612cfa34f2SIlya Dryomov 9622cfa34f2SIlya Dryomov map->osd_primary_affinity[osd] = aff; 9632cfa34f2SIlya Dryomov 9642cfa34f2SIlya Dryomov return 0; 9652cfa34f2SIlya Dryomov } 9662cfa34f2SIlya Dryomov 96763a6993fSIlya Dryomov static int decode_primary_affinity(void **p, void *end, 96863a6993fSIlya Dryomov struct ceph_osdmap *map) 96963a6993fSIlya Dryomov { 97063a6993fSIlya Dryomov u32 len, i; 97163a6993fSIlya Dryomov 97263a6993fSIlya Dryomov ceph_decode_32_safe(p, end, len, e_inval); 97363a6993fSIlya Dryomov if (len == 0) { 97463a6993fSIlya Dryomov kfree(map->osd_primary_affinity); 97563a6993fSIlya Dryomov map->osd_primary_affinity = NULL; 97663a6993fSIlya Dryomov return 0; 97763a6993fSIlya Dryomov } 97863a6993fSIlya Dryomov if (len != map->max_osd) 97963a6993fSIlya Dryomov goto e_inval; 98063a6993fSIlya Dryomov 98163a6993fSIlya Dryomov ceph_decode_need(p, end, map->max_osd*sizeof(u32), e_inval); 98263a6993fSIlya Dryomov 98363a6993fSIlya Dryomov for (i = 0; i < map->max_osd; i++) { 98463a6993fSIlya Dryomov int ret; 98563a6993fSIlya Dryomov 98663a6993fSIlya Dryomov ret = set_primary_affinity(map, i, ceph_decode_32(p)); 98763a6993fSIlya Dryomov if (ret) 98863a6993fSIlya Dryomov return ret; 98963a6993fSIlya Dryomov } 99063a6993fSIlya Dryomov 99163a6993fSIlya Dryomov return 0; 99263a6993fSIlya Dryomov 99363a6993fSIlya Dryomov e_inval: 99463a6993fSIlya Dryomov return -EINVAL; 99563a6993fSIlya Dryomov } 99663a6993fSIlya Dryomov 99763a6993fSIlya Dryomov static int decode_new_primary_affinity(void **p, void *end, 99863a6993fSIlya Dryomov struct ceph_osdmap *map) 99963a6993fSIlya Dryomov { 100063a6993fSIlya Dryomov u32 n; 100163a6993fSIlya Dryomov 100263a6993fSIlya Dryomov ceph_decode_32_safe(p, end, n, e_inval); 100363a6993fSIlya Dryomov while (n--) { 100463a6993fSIlya Dryomov u32 osd, aff; 100563a6993fSIlya Dryomov int ret; 100663a6993fSIlya Dryomov 100763a6993fSIlya Dryomov ceph_decode_32_safe(p, end, osd, e_inval); 100863a6993fSIlya Dryomov ceph_decode_32_safe(p, end, aff, e_inval); 100963a6993fSIlya Dryomov 101063a6993fSIlya Dryomov ret = set_primary_affinity(map, osd, aff); 101163a6993fSIlya Dryomov if (ret) 101263a6993fSIlya Dryomov return ret; 1013f31da0f3SIlya Dryomov 1014f31da0f3SIlya Dryomov pr_info("osd%d primary-affinity 0x%x\n", osd, aff); 101563a6993fSIlya Dryomov } 101663a6993fSIlya Dryomov 101763a6993fSIlya Dryomov return 0; 101863a6993fSIlya Dryomov 101963a6993fSIlya Dryomov e_inval: 102063a6993fSIlya Dryomov return -EINVAL; 102163a6993fSIlya Dryomov } 102263a6993fSIlya Dryomov 10233d14c5d2SYehuda Sadeh /* 10243d14c5d2SYehuda Sadeh * decode a full map. 10253d14c5d2SYehuda Sadeh */ 1026a2505d63SIlya Dryomov static int osdmap_decode(void **p, void *end, struct ceph_osdmap *map) 10273d14c5d2SYehuda Sadeh { 1028ec7af972SIlya Dryomov u8 struct_v; 102938a8d560SIlya Dryomov u32 epoch = 0; 10303d14c5d2SYehuda Sadeh void *start = *p; 10313977058cSIlya Dryomov u32 max; 10323977058cSIlya Dryomov u32 len, i; 1033597b52f6SIlya Dryomov int err; 10343d14c5d2SYehuda Sadeh 1035a2505d63SIlya Dryomov dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p)); 10363d14c5d2SYehuda Sadeh 1037ec7af972SIlya Dryomov err = get_osdmap_client_data_v(p, end, "full", &struct_v); 1038ec7af972SIlya Dryomov if (err) 1039ec7af972SIlya Dryomov goto bad; 10403d14c5d2SYehuda Sadeh 104153bbaba9SIlya Dryomov /* fsid, epoch, created, modified */ 104253bbaba9SIlya Dryomov ceph_decode_need(p, end, sizeof(map->fsid) + sizeof(u32) + 104353bbaba9SIlya Dryomov sizeof(map->created) + sizeof(map->modified), e_inval); 10443d14c5d2SYehuda Sadeh ceph_decode_copy(p, &map->fsid, sizeof(map->fsid)); 104538a8d560SIlya Dryomov epoch = map->epoch = ceph_decode_32(p); 10463d14c5d2SYehuda Sadeh ceph_decode_copy(p, &map->created, sizeof(map->created)); 10473d14c5d2SYehuda Sadeh ceph_decode_copy(p, &map->modified, sizeof(map->modified)); 10483d14c5d2SYehuda Sadeh 1049433fbdd3SIlya Dryomov /* pools */ 1050433fbdd3SIlya Dryomov err = decode_pools(p, end, map); 1051433fbdd3SIlya Dryomov if (err) 10523d14c5d2SYehuda Sadeh goto bad; 10533d14c5d2SYehuda Sadeh 10540f70c7eeSIlya Dryomov /* pool_name */ 10550f70c7eeSIlya Dryomov err = decode_pool_names(p, end, map); 1056597b52f6SIlya Dryomov if (err) 10573d14c5d2SYehuda Sadeh goto bad; 10583d14c5d2SYehuda Sadeh 1059597b52f6SIlya Dryomov ceph_decode_32_safe(p, end, map->pool_max, e_inval); 10603d14c5d2SYehuda Sadeh 1061597b52f6SIlya Dryomov ceph_decode_32_safe(p, end, map->flags, e_inval); 10623d14c5d2SYehuda Sadeh 10633977058cSIlya Dryomov /* max_osd */ 10643977058cSIlya Dryomov ceph_decode_32_safe(p, end, max, e_inval); 10653d14c5d2SYehuda Sadeh 10663d14c5d2SYehuda Sadeh /* (re)alloc osd arrays */ 10673d14c5d2SYehuda Sadeh err = osdmap_set_max_osd(map, max); 1068597b52f6SIlya Dryomov if (err) 10693d14c5d2SYehuda Sadeh goto bad; 10703d14c5d2SYehuda Sadeh 10712d88b2e0SIlya Dryomov /* osd_state, osd_weight, osd_addrs->client_addr */ 10723d14c5d2SYehuda Sadeh ceph_decode_need(p, end, 3*sizeof(u32) + 10733d14c5d2SYehuda Sadeh map->max_osd*(1 + sizeof(*map->osd_weight) + 1074597b52f6SIlya Dryomov sizeof(*map->osd_addr)), e_inval); 1075597b52f6SIlya Dryomov 10762d88b2e0SIlya Dryomov if (ceph_decode_32(p) != map->max_osd) 10772d88b2e0SIlya Dryomov goto e_inval; 10782d88b2e0SIlya Dryomov 10793d14c5d2SYehuda Sadeh ceph_decode_copy(p, map->osd_state, map->max_osd); 10803d14c5d2SYehuda Sadeh 10812d88b2e0SIlya Dryomov if (ceph_decode_32(p) != map->max_osd) 10822d88b2e0SIlya Dryomov goto e_inval; 10832d88b2e0SIlya Dryomov 10843d14c5d2SYehuda Sadeh for (i = 0; i < map->max_osd; i++) 10853d14c5d2SYehuda Sadeh map->osd_weight[i] = ceph_decode_32(p); 10863d14c5d2SYehuda Sadeh 10872d88b2e0SIlya Dryomov if (ceph_decode_32(p) != map->max_osd) 10882d88b2e0SIlya Dryomov goto e_inval; 10892d88b2e0SIlya Dryomov 10903d14c5d2SYehuda Sadeh ceph_decode_copy(p, map->osd_addr, map->max_osd*sizeof(*map->osd_addr)); 10913d14c5d2SYehuda Sadeh for (i = 0; i < map->max_osd; i++) 10923d14c5d2SYehuda Sadeh ceph_decode_addr(&map->osd_addr[i]); 10933d14c5d2SYehuda Sadeh 10943d14c5d2SYehuda Sadeh /* pg_temp */ 109510db634eSIlya Dryomov err = decode_pg_temp(p, end, map); 1096d6c0dd6bSSage Weil if (err) 1097d6c0dd6bSSage Weil goto bad; 10983d14c5d2SYehuda Sadeh 1099d286de79SIlya Dryomov /* primary_temp */ 1100d286de79SIlya Dryomov if (struct_v >= 1) { 1101d286de79SIlya Dryomov err = decode_primary_temp(p, end, map); 1102d286de79SIlya Dryomov if (err) 1103d286de79SIlya Dryomov goto bad; 1104d286de79SIlya Dryomov } 1105d286de79SIlya Dryomov 110663a6993fSIlya Dryomov /* primary_affinity */ 110763a6993fSIlya Dryomov if (struct_v >= 2) { 110863a6993fSIlya Dryomov err = decode_primary_affinity(p, end, map); 110963a6993fSIlya Dryomov if (err) 111063a6993fSIlya Dryomov goto bad; 111163a6993fSIlya Dryomov } else { 111263a6993fSIlya Dryomov /* XXX can this happen? */ 111363a6993fSIlya Dryomov kfree(map->osd_primary_affinity); 111463a6993fSIlya Dryomov map->osd_primary_affinity = NULL; 111563a6993fSIlya Dryomov } 111663a6993fSIlya Dryomov 11173d14c5d2SYehuda Sadeh /* crush */ 1118597b52f6SIlya Dryomov ceph_decode_32_safe(p, end, len, e_inval); 11199902e682SIlya Dryomov map->crush = crush_decode(*p, min(*p + len, end)); 11203d14c5d2SYehuda Sadeh if (IS_ERR(map->crush)) { 11213d14c5d2SYehuda Sadeh err = PTR_ERR(map->crush); 11223d14c5d2SYehuda Sadeh map->crush = NULL; 11233d14c5d2SYehuda Sadeh goto bad; 11243d14c5d2SYehuda Sadeh } 11259902e682SIlya Dryomov *p += len; 11263d14c5d2SYehuda Sadeh 112738a8d560SIlya Dryomov /* ignore the rest */ 11283d14c5d2SYehuda Sadeh *p = end; 11293d14c5d2SYehuda Sadeh 113038a8d560SIlya Dryomov dout("full osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd); 1131a2505d63SIlya Dryomov return 0; 11323d14c5d2SYehuda Sadeh 1133597b52f6SIlya Dryomov e_inval: 1134597b52f6SIlya Dryomov err = -EINVAL; 11353d14c5d2SYehuda Sadeh bad: 113638a8d560SIlya Dryomov pr_err("corrupt full osdmap (%d) epoch %d off %d (%p of %p-%p)\n", 113738a8d560SIlya Dryomov err, epoch, (int)(*p - start), *p, start, end); 113838a8d560SIlya Dryomov print_hex_dump(KERN_DEBUG, "osdmap: ", 113938a8d560SIlya Dryomov DUMP_PREFIX_OFFSET, 16, 1, 114038a8d560SIlya Dryomov start, end - start, true); 1141a2505d63SIlya Dryomov return err; 1142a2505d63SIlya Dryomov } 1143a2505d63SIlya Dryomov 1144a2505d63SIlya Dryomov /* 1145a2505d63SIlya Dryomov * Allocate and decode a full map. 1146a2505d63SIlya Dryomov */ 1147a2505d63SIlya Dryomov struct ceph_osdmap *ceph_osdmap_decode(void **p, void *end) 1148a2505d63SIlya Dryomov { 1149a2505d63SIlya Dryomov struct ceph_osdmap *map; 1150a2505d63SIlya Dryomov int ret; 1151a2505d63SIlya Dryomov 1152a2505d63SIlya Dryomov map = kzalloc(sizeof(*map), GFP_NOFS); 1153a2505d63SIlya Dryomov if (!map) 1154a2505d63SIlya Dryomov return ERR_PTR(-ENOMEM); 1155a2505d63SIlya Dryomov 1156a2505d63SIlya Dryomov map->pg_temp = RB_ROOT; 11579686f94cSIlya Dryomov map->primary_temp = RB_ROOT; 1158a2505d63SIlya Dryomov mutex_init(&map->crush_scratch_mutex); 1159a2505d63SIlya Dryomov 1160a2505d63SIlya Dryomov ret = osdmap_decode(p, end, map); 1161a2505d63SIlya Dryomov if (ret) { 11623d14c5d2SYehuda Sadeh ceph_osdmap_destroy(map); 1163a2505d63SIlya Dryomov return ERR_PTR(ret); 1164a2505d63SIlya Dryomov } 1165a2505d63SIlya Dryomov 1166a2505d63SIlya Dryomov return map; 11673d14c5d2SYehuda Sadeh } 11683d14c5d2SYehuda Sadeh 11693d14c5d2SYehuda Sadeh /* 11703d14c5d2SYehuda Sadeh * decode and apply an incremental map update. 11713d14c5d2SYehuda Sadeh */ 11723d14c5d2SYehuda Sadeh struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end, 11733d14c5d2SYehuda Sadeh struct ceph_osdmap *map, 11743d14c5d2SYehuda Sadeh struct ceph_messenger *msgr) 11753d14c5d2SYehuda Sadeh { 11763d14c5d2SYehuda Sadeh struct crush_map *newcrush = NULL; 11773d14c5d2SYehuda Sadeh struct ceph_fsid fsid; 11783d14c5d2SYehuda Sadeh u32 epoch = 0; 11793d14c5d2SYehuda Sadeh struct ceph_timespec modified; 11804f6a7e5eSSage Weil s32 len; 11814f6a7e5eSSage Weil u64 pool; 11824f6a7e5eSSage Weil __s64 new_pool_max; 11834f6a7e5eSSage Weil __s32 new_flags, max; 11843d14c5d2SYehuda Sadeh void *start = *p; 118586f1742bSIlya Dryomov int err; 1186ec7af972SIlya Dryomov u8 struct_v; 11873d14c5d2SYehuda Sadeh 118838a8d560SIlya Dryomov dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p)); 118938a8d560SIlya Dryomov 1190ec7af972SIlya Dryomov err = get_osdmap_client_data_v(p, end, "inc", &struct_v); 1191ec7af972SIlya Dryomov if (err) 1192ec7af972SIlya Dryomov goto bad; 11933d14c5d2SYehuda Sadeh 119453bbaba9SIlya Dryomov /* fsid, epoch, modified, new_pool_max, new_flags */ 119553bbaba9SIlya Dryomov ceph_decode_need(p, end, sizeof(fsid) + sizeof(u32) + sizeof(modified) + 119653bbaba9SIlya Dryomov sizeof(u64) + sizeof(u32), e_inval); 11973d14c5d2SYehuda Sadeh ceph_decode_copy(p, &fsid, sizeof(fsid)); 11983d14c5d2SYehuda Sadeh epoch = ceph_decode_32(p); 11993d14c5d2SYehuda Sadeh BUG_ON(epoch != map->epoch+1); 12003d14c5d2SYehuda Sadeh ceph_decode_copy(p, &modified, sizeof(modified)); 12014f6a7e5eSSage Weil new_pool_max = ceph_decode_64(p); 12023d14c5d2SYehuda Sadeh new_flags = ceph_decode_32(p); 12033d14c5d2SYehuda Sadeh 12043d14c5d2SYehuda Sadeh /* full map? */ 120586f1742bSIlya Dryomov ceph_decode_32_safe(p, end, len, e_inval); 12063d14c5d2SYehuda Sadeh if (len > 0) { 12073d14c5d2SYehuda Sadeh dout("apply_incremental full map len %d, %p to %p\n", 12083d14c5d2SYehuda Sadeh len, *p, end); 1209a2505d63SIlya Dryomov return ceph_osdmap_decode(p, min(*p+len, end)); 12103d14c5d2SYehuda Sadeh } 12113d14c5d2SYehuda Sadeh 12123d14c5d2SYehuda Sadeh /* new crush? */ 121386f1742bSIlya Dryomov ceph_decode_32_safe(p, end, len, e_inval); 12143d14c5d2SYehuda Sadeh if (len > 0) { 12153d14c5d2SYehuda Sadeh newcrush = crush_decode(*p, min(*p+len, end)); 121686f1742bSIlya Dryomov if (IS_ERR(newcrush)) { 121786f1742bSIlya Dryomov err = PTR_ERR(newcrush); 121886f1742bSIlya Dryomov newcrush = NULL; 121986f1742bSIlya Dryomov goto bad; 122086f1742bSIlya Dryomov } 12213d14c5d2SYehuda Sadeh *p += len; 12223d14c5d2SYehuda Sadeh } 12233d14c5d2SYehuda Sadeh 12243d14c5d2SYehuda Sadeh /* new flags? */ 12253d14c5d2SYehuda Sadeh if (new_flags >= 0) 12263d14c5d2SYehuda Sadeh map->flags = new_flags; 12273d14c5d2SYehuda Sadeh if (new_pool_max >= 0) 12283d14c5d2SYehuda Sadeh map->pool_max = new_pool_max; 12293d14c5d2SYehuda Sadeh 12303d14c5d2SYehuda Sadeh /* new max? */ 123153bbaba9SIlya Dryomov ceph_decode_32_safe(p, end, max, e_inval); 12323d14c5d2SYehuda Sadeh if (max >= 0) { 12333d14c5d2SYehuda Sadeh err = osdmap_set_max_osd(map, max); 123486f1742bSIlya Dryomov if (err) 12353d14c5d2SYehuda Sadeh goto bad; 12363d14c5d2SYehuda Sadeh } 12373d14c5d2SYehuda Sadeh 12383d14c5d2SYehuda Sadeh map->epoch++; 123931456665SSage Weil map->modified = modified; 12403d14c5d2SYehuda Sadeh if (newcrush) { 12413d14c5d2SYehuda Sadeh if (map->crush) 12423d14c5d2SYehuda Sadeh crush_destroy(map->crush); 12433d14c5d2SYehuda Sadeh map->crush = newcrush; 12443d14c5d2SYehuda Sadeh newcrush = NULL; 12453d14c5d2SYehuda Sadeh } 12463d14c5d2SYehuda Sadeh 1247433fbdd3SIlya Dryomov /* new_pools */ 1248433fbdd3SIlya Dryomov err = decode_new_pools(p, end, map); 1249433fbdd3SIlya Dryomov if (err) 12503d14c5d2SYehuda Sadeh goto bad; 12519464d008SIlya Dryomov 12520f70c7eeSIlya Dryomov /* new_pool_names */ 12530f70c7eeSIlya Dryomov err = decode_pool_names(p, end, map); 125486f1742bSIlya Dryomov if (err) 12553d14c5d2SYehuda Sadeh goto bad; 12563d14c5d2SYehuda Sadeh 12573d14c5d2SYehuda Sadeh /* old_pool */ 125886f1742bSIlya Dryomov ceph_decode_32_safe(p, end, len, e_inval); 12593d14c5d2SYehuda Sadeh while (len--) { 12603d14c5d2SYehuda Sadeh struct ceph_pg_pool_info *pi; 12613d14c5d2SYehuda Sadeh 126286f1742bSIlya Dryomov ceph_decode_64_safe(p, end, pool, e_inval); 12633d14c5d2SYehuda Sadeh pi = __lookup_pg_pool(&map->pg_pools, pool); 12643d14c5d2SYehuda Sadeh if (pi) 12653d14c5d2SYehuda Sadeh __remove_pg_pool(&map->pg_pools, pi); 12663d14c5d2SYehuda Sadeh } 12673d14c5d2SYehuda Sadeh 12683d14c5d2SYehuda Sadeh /* new_up */ 126986f1742bSIlya Dryomov ceph_decode_32_safe(p, end, len, e_inval); 12703d14c5d2SYehuda Sadeh while (len--) { 12713d14c5d2SYehuda Sadeh u32 osd; 12723d14c5d2SYehuda Sadeh struct ceph_entity_addr addr; 127386f1742bSIlya Dryomov ceph_decode_32_safe(p, end, osd, e_inval); 127486f1742bSIlya Dryomov ceph_decode_copy_safe(p, end, &addr, sizeof(addr), e_inval); 12753d14c5d2SYehuda Sadeh ceph_decode_addr(&addr); 12763d14c5d2SYehuda Sadeh pr_info("osd%d up\n", osd); 12773d14c5d2SYehuda Sadeh BUG_ON(osd >= map->max_osd); 12783d14c5d2SYehuda Sadeh map->osd_state[osd] |= CEPH_OSD_UP; 12793d14c5d2SYehuda Sadeh map->osd_addr[osd] = addr; 12803d14c5d2SYehuda Sadeh } 12813d14c5d2SYehuda Sadeh 12827662d8ffSSage Weil /* new_state */ 128386f1742bSIlya Dryomov ceph_decode_32_safe(p, end, len, e_inval); 12843d14c5d2SYehuda Sadeh while (len--) { 12853d14c5d2SYehuda Sadeh u32 osd; 12867662d8ffSSage Weil u8 xorstate; 128786f1742bSIlya Dryomov ceph_decode_32_safe(p, end, osd, e_inval); 12887662d8ffSSage Weil xorstate = **(u8 **)p; 12893d14c5d2SYehuda Sadeh (*p)++; /* clean flag */ 12907662d8ffSSage Weil if (xorstate == 0) 12917662d8ffSSage Weil xorstate = CEPH_OSD_UP; 12927662d8ffSSage Weil if (xorstate & CEPH_OSD_UP) 12933d14c5d2SYehuda Sadeh pr_info("osd%d down\n", osd); 12943d14c5d2SYehuda Sadeh if (osd < map->max_osd) 12957662d8ffSSage Weil map->osd_state[osd] ^= xorstate; 12963d14c5d2SYehuda Sadeh } 12973d14c5d2SYehuda Sadeh 12983d14c5d2SYehuda Sadeh /* new_weight */ 129986f1742bSIlya Dryomov ceph_decode_32_safe(p, end, len, e_inval); 13003d14c5d2SYehuda Sadeh while (len--) { 13013d14c5d2SYehuda Sadeh u32 osd, off; 130286f1742bSIlya Dryomov ceph_decode_need(p, end, sizeof(u32)*2, e_inval); 13033d14c5d2SYehuda Sadeh osd = ceph_decode_32(p); 13043d14c5d2SYehuda Sadeh off = ceph_decode_32(p); 13053d14c5d2SYehuda Sadeh pr_info("osd%d weight 0x%x %s\n", osd, off, 13063d14c5d2SYehuda Sadeh off == CEPH_OSD_IN ? "(in)" : 13073d14c5d2SYehuda Sadeh (off == CEPH_OSD_OUT ? "(out)" : "")); 13083d14c5d2SYehuda Sadeh if (osd < map->max_osd) 13093d14c5d2SYehuda Sadeh map->osd_weight[osd] = off; 13103d14c5d2SYehuda Sadeh } 13113d14c5d2SYehuda Sadeh 13123d14c5d2SYehuda Sadeh /* new_pg_temp */ 131310db634eSIlya Dryomov err = decode_new_pg_temp(p, end, map); 1314d6c0dd6bSSage Weil if (err) 1315d6c0dd6bSSage Weil goto bad; 13163d14c5d2SYehuda Sadeh 1317d286de79SIlya Dryomov /* new_primary_temp */ 1318d286de79SIlya Dryomov if (struct_v >= 1) { 1319d286de79SIlya Dryomov err = decode_new_primary_temp(p, end, map); 1320d286de79SIlya Dryomov if (err) 1321d286de79SIlya Dryomov goto bad; 1322d286de79SIlya Dryomov } 1323d286de79SIlya Dryomov 132463a6993fSIlya Dryomov /* new_primary_affinity */ 132563a6993fSIlya Dryomov if (struct_v >= 2) { 132663a6993fSIlya Dryomov err = decode_new_primary_affinity(p, end, map); 132763a6993fSIlya Dryomov if (err) 132863a6993fSIlya Dryomov goto bad; 132963a6993fSIlya Dryomov } 133063a6993fSIlya Dryomov 13313d14c5d2SYehuda Sadeh /* ignore the rest */ 13323d14c5d2SYehuda Sadeh *p = end; 133338a8d560SIlya Dryomov 133438a8d560SIlya Dryomov dout("inc osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd); 13353d14c5d2SYehuda Sadeh return map; 13363d14c5d2SYehuda Sadeh 133786f1742bSIlya Dryomov e_inval: 133886f1742bSIlya Dryomov err = -EINVAL; 13393d14c5d2SYehuda Sadeh bad: 134038a8d560SIlya Dryomov pr_err("corrupt inc osdmap (%d) epoch %d off %d (%p of %p-%p)\n", 134138a8d560SIlya Dryomov err, epoch, (int)(*p - start), *p, start, end); 13423d14c5d2SYehuda Sadeh print_hex_dump(KERN_DEBUG, "osdmap: ", 13433d14c5d2SYehuda Sadeh DUMP_PREFIX_OFFSET, 16, 1, 13443d14c5d2SYehuda Sadeh start, end - start, true); 13453d14c5d2SYehuda Sadeh if (newcrush) 13463d14c5d2SYehuda Sadeh crush_destroy(newcrush); 13473d14c5d2SYehuda Sadeh return ERR_PTR(err); 13483d14c5d2SYehuda Sadeh } 13493d14c5d2SYehuda Sadeh 13503d14c5d2SYehuda Sadeh 13513d14c5d2SYehuda Sadeh 13523d14c5d2SYehuda Sadeh 13533d14c5d2SYehuda Sadeh /* 13543d14c5d2SYehuda Sadeh * calculate file layout from given offset, length. 13553d14c5d2SYehuda Sadeh * fill in correct oid, logical length, and object extent 13563d14c5d2SYehuda Sadeh * offset, length. 13573d14c5d2SYehuda Sadeh * 13583d14c5d2SYehuda Sadeh * for now, we write only a single su, until we can 13593d14c5d2SYehuda Sadeh * pass a stride back to the caller. 13603d14c5d2SYehuda Sadeh */ 1361d63b77f4SSage Weil int ceph_calc_file_object_mapping(struct ceph_file_layout *layout, 1362e8afad65SAlex Elder u64 off, u64 len, 13633d14c5d2SYehuda Sadeh u64 *ono, 13643d14c5d2SYehuda Sadeh u64 *oxoff, u64 *oxlen) 13653d14c5d2SYehuda Sadeh { 13663d14c5d2SYehuda Sadeh u32 osize = le32_to_cpu(layout->fl_object_size); 13673d14c5d2SYehuda Sadeh u32 su = le32_to_cpu(layout->fl_stripe_unit); 13683d14c5d2SYehuda Sadeh u32 sc = le32_to_cpu(layout->fl_stripe_count); 13693d14c5d2SYehuda Sadeh u32 bl, stripeno, stripepos, objsetno; 13703d14c5d2SYehuda Sadeh u32 su_per_object; 13713d14c5d2SYehuda Sadeh u64 t, su_offset; 13723d14c5d2SYehuda Sadeh 1373e8afad65SAlex Elder dout("mapping %llu~%llu osize %u fl_su %u\n", off, len, 13743d14c5d2SYehuda Sadeh osize, su); 1375d63b77f4SSage Weil if (su == 0 || sc == 0) 1376d63b77f4SSage Weil goto invalid; 13773d14c5d2SYehuda Sadeh su_per_object = osize / su; 1378d63b77f4SSage Weil if (su_per_object == 0) 1379d63b77f4SSage Weil goto invalid; 13803d14c5d2SYehuda Sadeh dout("osize %u / su %u = su_per_object %u\n", osize, su, 13813d14c5d2SYehuda Sadeh su_per_object); 13823d14c5d2SYehuda Sadeh 1383d63b77f4SSage Weil if ((su & ~PAGE_MASK) != 0) 1384d63b77f4SSage Weil goto invalid; 1385d63b77f4SSage Weil 13863d14c5d2SYehuda Sadeh /* bl = *off / su; */ 13873d14c5d2SYehuda Sadeh t = off; 13883d14c5d2SYehuda Sadeh do_div(t, su); 13893d14c5d2SYehuda Sadeh bl = t; 13903d14c5d2SYehuda Sadeh dout("off %llu / su %u = bl %u\n", off, su, bl); 13913d14c5d2SYehuda Sadeh 13923d14c5d2SYehuda Sadeh stripeno = bl / sc; 13933d14c5d2SYehuda Sadeh stripepos = bl % sc; 13943d14c5d2SYehuda Sadeh objsetno = stripeno / su_per_object; 13953d14c5d2SYehuda Sadeh 13963d14c5d2SYehuda Sadeh *ono = objsetno * sc + stripepos; 139795c96174SEric Dumazet dout("objset %u * sc %u = ono %u\n", objsetno, sc, (unsigned int)*ono); 13983d14c5d2SYehuda Sadeh 13993d14c5d2SYehuda Sadeh /* *oxoff = *off % layout->fl_stripe_unit; # offset in su */ 14003d14c5d2SYehuda Sadeh t = off; 14013d14c5d2SYehuda Sadeh su_offset = do_div(t, su); 14023d14c5d2SYehuda Sadeh *oxoff = su_offset + (stripeno % su_per_object) * su; 14033d14c5d2SYehuda Sadeh 14043d14c5d2SYehuda Sadeh /* 14053d14c5d2SYehuda Sadeh * Calculate the length of the extent being written to the selected 1406e8afad65SAlex Elder * object. This is the minimum of the full length requested (len) or 14073d14c5d2SYehuda Sadeh * the remainder of the current stripe being written to. 14083d14c5d2SYehuda Sadeh */ 1409e8afad65SAlex Elder *oxlen = min_t(u64, len, su - su_offset); 14103d14c5d2SYehuda Sadeh 14113d14c5d2SYehuda Sadeh dout(" obj extent %llu~%llu\n", *oxoff, *oxlen); 1412d63b77f4SSage Weil return 0; 1413d63b77f4SSage Weil 1414d63b77f4SSage Weil invalid: 1415d63b77f4SSage Weil dout(" invalid layout\n"); 1416d63b77f4SSage Weil *ono = 0; 1417d63b77f4SSage Weil *oxoff = 0; 1418d63b77f4SSage Weil *oxlen = 0; 1419d63b77f4SSage Weil return -EINVAL; 14203d14c5d2SYehuda Sadeh } 14213d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_calc_file_object_mapping); 14223d14c5d2SYehuda Sadeh 14233d14c5d2SYehuda Sadeh /* 14247c13cb64SIlya Dryomov * Calculate mapping of a (oloc, oid) pair to a PG. Should only be 14257c13cb64SIlya Dryomov * called with target's (oloc, oid), since tiering isn't taken into 14267c13cb64SIlya Dryomov * account. 14273d14c5d2SYehuda Sadeh */ 14287c13cb64SIlya Dryomov int ceph_oloc_oid_to_pg(struct ceph_osdmap *osdmap, 14297c13cb64SIlya Dryomov struct ceph_object_locator *oloc, 14307c13cb64SIlya Dryomov struct ceph_object_id *oid, 14317c13cb64SIlya Dryomov struct ceph_pg *pg_out) 14323d14c5d2SYehuda Sadeh { 14337c13cb64SIlya Dryomov struct ceph_pg_pool_info *pi; 14343d14c5d2SYehuda Sadeh 14357c13cb64SIlya Dryomov pi = __lookup_pg_pool(&osdmap->pg_pools, oloc->pool); 14367c13cb64SIlya Dryomov if (!pi) 14373d14c5d2SYehuda Sadeh return -EIO; 14383d14c5d2SYehuda Sadeh 14397c13cb64SIlya Dryomov pg_out->pool = oloc->pool; 14407c13cb64SIlya Dryomov pg_out->seed = ceph_str_hash(pi->object_hash, oid->name, 14417c13cb64SIlya Dryomov oid->name_len); 14427c13cb64SIlya Dryomov 14437c13cb64SIlya Dryomov dout("%s '%.*s' pgid %llu.%x\n", __func__, oid->name_len, oid->name, 14447c13cb64SIlya Dryomov pg_out->pool, pg_out->seed); 14453d14c5d2SYehuda Sadeh return 0; 14463d14c5d2SYehuda Sadeh } 14477c13cb64SIlya Dryomov EXPORT_SYMBOL(ceph_oloc_oid_to_pg); 14483d14c5d2SYehuda Sadeh 14499d521470SIlya Dryomov static int do_crush(struct ceph_osdmap *map, int ruleno, int x, 1450e8ef19c4SIlya Dryomov int *result, int result_max, 1451e8ef19c4SIlya Dryomov const __u32 *weight, int weight_max) 1452e8ef19c4SIlya Dryomov { 14539d521470SIlya Dryomov int r; 1454e8ef19c4SIlya Dryomov 14559d521470SIlya Dryomov BUG_ON(result_max > CEPH_PG_MAX_SIZE); 14569d521470SIlya Dryomov 14579d521470SIlya Dryomov mutex_lock(&map->crush_scratch_mutex); 14589d521470SIlya Dryomov r = crush_do_rule(map->crush, ruleno, x, result, result_max, 14599d521470SIlya Dryomov weight, weight_max, map->crush_scratch_ary); 14609d521470SIlya Dryomov mutex_unlock(&map->crush_scratch_mutex); 14619d521470SIlya Dryomov 14629d521470SIlya Dryomov return r; 1463e8ef19c4SIlya Dryomov } 1464e8ef19c4SIlya Dryomov 14653d14c5d2SYehuda Sadeh /* 14662bd93d4dSIlya Dryomov * Calculate raw (crush) set for given pgid. 14672bd93d4dSIlya Dryomov * 14682bd93d4dSIlya Dryomov * Return raw set length, or error. 14692bd93d4dSIlya Dryomov */ 14702bd93d4dSIlya Dryomov static int pg_to_raw_osds(struct ceph_osdmap *osdmap, 14712bd93d4dSIlya Dryomov struct ceph_pg_pool_info *pool, 14722bd93d4dSIlya Dryomov struct ceph_pg pgid, u32 pps, int *osds) 14732bd93d4dSIlya Dryomov { 14742bd93d4dSIlya Dryomov int ruleno; 14752bd93d4dSIlya Dryomov int len; 14762bd93d4dSIlya Dryomov 14772bd93d4dSIlya Dryomov /* crush */ 14782bd93d4dSIlya Dryomov ruleno = crush_find_rule(osdmap->crush, pool->crush_ruleset, 14792bd93d4dSIlya Dryomov pool->type, pool->size); 14802bd93d4dSIlya Dryomov if (ruleno < 0) { 14812bd93d4dSIlya Dryomov pr_err("no crush rule: pool %lld ruleset %d type %d size %d\n", 14822bd93d4dSIlya Dryomov pgid.pool, pool->crush_ruleset, pool->type, 14832bd93d4dSIlya Dryomov pool->size); 14842bd93d4dSIlya Dryomov return -ENOENT; 14852bd93d4dSIlya Dryomov } 14862bd93d4dSIlya Dryomov 14872bd93d4dSIlya Dryomov len = do_crush(osdmap, ruleno, pps, osds, 14882bd93d4dSIlya Dryomov min_t(int, pool->size, CEPH_PG_MAX_SIZE), 14892bd93d4dSIlya Dryomov osdmap->osd_weight, osdmap->max_osd); 14902bd93d4dSIlya Dryomov if (len < 0) { 14912bd93d4dSIlya Dryomov pr_err("error %d from crush rule %d: pool %lld ruleset %d type %d size %d\n", 14922bd93d4dSIlya Dryomov len, ruleno, pgid.pool, pool->crush_ruleset, 14932bd93d4dSIlya Dryomov pool->type, pool->size); 14942bd93d4dSIlya Dryomov return len; 14952bd93d4dSIlya Dryomov } 14962bd93d4dSIlya Dryomov 14972bd93d4dSIlya Dryomov return len; 14982bd93d4dSIlya Dryomov } 14992bd93d4dSIlya Dryomov 15002bd93d4dSIlya Dryomov /* 15012bd93d4dSIlya Dryomov * Given raw set, calculate up set and up primary. 15022bd93d4dSIlya Dryomov * 15032bd93d4dSIlya Dryomov * Return up set length. *primary is set to up primary osd id, or -1 15042bd93d4dSIlya Dryomov * if up set is empty. 15052bd93d4dSIlya Dryomov */ 15062bd93d4dSIlya Dryomov static int raw_to_up_osds(struct ceph_osdmap *osdmap, 15072bd93d4dSIlya Dryomov struct ceph_pg_pool_info *pool, 15082bd93d4dSIlya Dryomov int *osds, int len, int *primary) 15092bd93d4dSIlya Dryomov { 15102bd93d4dSIlya Dryomov int up_primary = -1; 15112bd93d4dSIlya Dryomov int i; 15122bd93d4dSIlya Dryomov 15132bd93d4dSIlya Dryomov if (ceph_can_shift_osds(pool)) { 15142bd93d4dSIlya Dryomov int removed = 0; 15152bd93d4dSIlya Dryomov 15162bd93d4dSIlya Dryomov for (i = 0; i < len; i++) { 15172bd93d4dSIlya Dryomov if (ceph_osd_is_down(osdmap, osds[i])) { 15182bd93d4dSIlya Dryomov removed++; 15192bd93d4dSIlya Dryomov continue; 15202bd93d4dSIlya Dryomov } 15212bd93d4dSIlya Dryomov if (removed) 15222bd93d4dSIlya Dryomov osds[i - removed] = osds[i]; 15232bd93d4dSIlya Dryomov } 15242bd93d4dSIlya Dryomov 15252bd93d4dSIlya Dryomov len -= removed; 15262bd93d4dSIlya Dryomov if (len > 0) 15272bd93d4dSIlya Dryomov up_primary = osds[0]; 15282bd93d4dSIlya Dryomov } else { 15292bd93d4dSIlya Dryomov for (i = len - 1; i >= 0; i--) { 15302bd93d4dSIlya Dryomov if (ceph_osd_is_down(osdmap, osds[i])) 15312bd93d4dSIlya Dryomov osds[i] = CRUSH_ITEM_NONE; 15322bd93d4dSIlya Dryomov else 15332bd93d4dSIlya Dryomov up_primary = osds[i]; 15342bd93d4dSIlya Dryomov } 15352bd93d4dSIlya Dryomov } 15362bd93d4dSIlya Dryomov 15372bd93d4dSIlya Dryomov *primary = up_primary; 15382bd93d4dSIlya Dryomov return len; 15392bd93d4dSIlya Dryomov } 15402bd93d4dSIlya Dryomov 154147ec1f3cSIlya Dryomov static void apply_primary_affinity(struct ceph_osdmap *osdmap, u32 pps, 154247ec1f3cSIlya Dryomov struct ceph_pg_pool_info *pool, 154347ec1f3cSIlya Dryomov int *osds, int len, int *primary) 154447ec1f3cSIlya Dryomov { 154547ec1f3cSIlya Dryomov int i; 154647ec1f3cSIlya Dryomov int pos = -1; 154747ec1f3cSIlya Dryomov 154847ec1f3cSIlya Dryomov /* 154947ec1f3cSIlya Dryomov * Do we have any non-default primary_affinity values for these 155047ec1f3cSIlya Dryomov * osds? 155147ec1f3cSIlya Dryomov */ 155247ec1f3cSIlya Dryomov if (!osdmap->osd_primary_affinity) 155347ec1f3cSIlya Dryomov return; 155447ec1f3cSIlya Dryomov 155547ec1f3cSIlya Dryomov for (i = 0; i < len; i++) { 155692b2e751SIlya Dryomov int osd = osds[i]; 155792b2e751SIlya Dryomov 155892b2e751SIlya Dryomov if (osd != CRUSH_ITEM_NONE && 155992b2e751SIlya Dryomov osdmap->osd_primary_affinity[osd] != 156047ec1f3cSIlya Dryomov CEPH_OSD_DEFAULT_PRIMARY_AFFINITY) { 156147ec1f3cSIlya Dryomov break; 156247ec1f3cSIlya Dryomov } 156347ec1f3cSIlya Dryomov } 156447ec1f3cSIlya Dryomov if (i == len) 156547ec1f3cSIlya Dryomov return; 156647ec1f3cSIlya Dryomov 156747ec1f3cSIlya Dryomov /* 156847ec1f3cSIlya Dryomov * Pick the primary. Feed both the seed (for the pg) and the 156947ec1f3cSIlya Dryomov * osd into the hash/rng so that a proportional fraction of an 157047ec1f3cSIlya Dryomov * osd's pgs get rejected as primary. 157147ec1f3cSIlya Dryomov */ 157247ec1f3cSIlya Dryomov for (i = 0; i < len; i++) { 157392b2e751SIlya Dryomov int osd = osds[i]; 157447ec1f3cSIlya Dryomov u32 aff; 157547ec1f3cSIlya Dryomov 157647ec1f3cSIlya Dryomov if (osd == CRUSH_ITEM_NONE) 157747ec1f3cSIlya Dryomov continue; 157847ec1f3cSIlya Dryomov 157947ec1f3cSIlya Dryomov aff = osdmap->osd_primary_affinity[osd]; 158047ec1f3cSIlya Dryomov if (aff < CEPH_OSD_MAX_PRIMARY_AFFINITY && 158147ec1f3cSIlya Dryomov (crush_hash32_2(CRUSH_HASH_RJENKINS1, 158247ec1f3cSIlya Dryomov pps, osd) >> 16) >= aff) { 158347ec1f3cSIlya Dryomov /* 158447ec1f3cSIlya Dryomov * We chose not to use this primary. Note it 158547ec1f3cSIlya Dryomov * anyway as a fallback in case we don't pick 158647ec1f3cSIlya Dryomov * anyone else, but keep looking. 158747ec1f3cSIlya Dryomov */ 158847ec1f3cSIlya Dryomov if (pos < 0) 158947ec1f3cSIlya Dryomov pos = i; 159047ec1f3cSIlya Dryomov } else { 159147ec1f3cSIlya Dryomov pos = i; 159247ec1f3cSIlya Dryomov break; 159347ec1f3cSIlya Dryomov } 159447ec1f3cSIlya Dryomov } 159547ec1f3cSIlya Dryomov if (pos < 0) 159647ec1f3cSIlya Dryomov return; 159747ec1f3cSIlya Dryomov 159847ec1f3cSIlya Dryomov *primary = osds[pos]; 159947ec1f3cSIlya Dryomov 160047ec1f3cSIlya Dryomov if (ceph_can_shift_osds(pool) && pos > 0) { 160147ec1f3cSIlya Dryomov /* move the new primary to the front */ 160247ec1f3cSIlya Dryomov for (i = pos; i > 0; i--) 160347ec1f3cSIlya Dryomov osds[i] = osds[i - 1]; 160447ec1f3cSIlya Dryomov osds[0] = *primary; 160547ec1f3cSIlya Dryomov } 160647ec1f3cSIlya Dryomov } 160747ec1f3cSIlya Dryomov 16082bd93d4dSIlya Dryomov /* 16095e8d4d36SIlya Dryomov * Given up set, apply pg_temp and primary_temp mappings. 161045966c34SIlya Dryomov * 161145966c34SIlya Dryomov * Return acting set length. *primary is set to acting primary osd id, 161245966c34SIlya Dryomov * or -1 if acting set is empty. 161345966c34SIlya Dryomov */ 161445966c34SIlya Dryomov static int apply_temps(struct ceph_osdmap *osdmap, 161545966c34SIlya Dryomov struct ceph_pg_pool_info *pool, struct ceph_pg pgid, 161645966c34SIlya Dryomov int *osds, int len, int *primary) 161745966c34SIlya Dryomov { 161845966c34SIlya Dryomov struct ceph_pg_mapping *pg; 161945966c34SIlya Dryomov int temp_len; 162045966c34SIlya Dryomov int temp_primary; 162145966c34SIlya Dryomov int i; 162245966c34SIlya Dryomov 162345966c34SIlya Dryomov /* raw_pg -> pg */ 162445966c34SIlya Dryomov pgid.seed = ceph_stable_mod(pgid.seed, pool->pg_num, 162545966c34SIlya Dryomov pool->pg_num_mask); 162645966c34SIlya Dryomov 162745966c34SIlya Dryomov /* pg_temp? */ 162845966c34SIlya Dryomov pg = __lookup_pg_mapping(&osdmap->pg_temp, pgid); 162945966c34SIlya Dryomov if (pg) { 163045966c34SIlya Dryomov temp_len = 0; 163145966c34SIlya Dryomov temp_primary = -1; 163245966c34SIlya Dryomov 163345966c34SIlya Dryomov for (i = 0; i < pg->pg_temp.len; i++) { 163445966c34SIlya Dryomov if (ceph_osd_is_down(osdmap, pg->pg_temp.osds[i])) { 163545966c34SIlya Dryomov if (ceph_can_shift_osds(pool)) 163645966c34SIlya Dryomov continue; 163745966c34SIlya Dryomov else 163845966c34SIlya Dryomov osds[temp_len++] = CRUSH_ITEM_NONE; 163945966c34SIlya Dryomov } else { 164045966c34SIlya Dryomov osds[temp_len++] = pg->pg_temp.osds[i]; 164145966c34SIlya Dryomov } 164245966c34SIlya Dryomov } 164345966c34SIlya Dryomov 164445966c34SIlya Dryomov /* apply pg_temp's primary */ 164545966c34SIlya Dryomov for (i = 0; i < temp_len; i++) { 164645966c34SIlya Dryomov if (osds[i] != CRUSH_ITEM_NONE) { 164745966c34SIlya Dryomov temp_primary = osds[i]; 164845966c34SIlya Dryomov break; 164945966c34SIlya Dryomov } 165045966c34SIlya Dryomov } 165145966c34SIlya Dryomov } else { 165245966c34SIlya Dryomov temp_len = len; 165345966c34SIlya Dryomov temp_primary = *primary; 165445966c34SIlya Dryomov } 165545966c34SIlya Dryomov 16565e8d4d36SIlya Dryomov /* primary_temp? */ 16575e8d4d36SIlya Dryomov pg = __lookup_pg_mapping(&osdmap->primary_temp, pgid); 16585e8d4d36SIlya Dryomov if (pg) 16595e8d4d36SIlya Dryomov temp_primary = pg->primary_temp.osd; 16605e8d4d36SIlya Dryomov 166145966c34SIlya Dryomov *primary = temp_primary; 166245966c34SIlya Dryomov return temp_len; 166345966c34SIlya Dryomov } 166445966c34SIlya Dryomov 166545966c34SIlya Dryomov /* 1666ac972230SIlya Dryomov * Calculate acting set for given pgid. 1667ac972230SIlya Dryomov * 16688008ab10SIlya Dryomov * Return acting set length, or error. *primary is set to acting 16698008ab10SIlya Dryomov * primary osd id, or -1 if acting set is empty or on error. 16703d14c5d2SYehuda Sadeh */ 16715b191d99SSage Weil int ceph_calc_pg_acting(struct ceph_osdmap *osdmap, struct ceph_pg pgid, 16728008ab10SIlya Dryomov int *osds, int *primary) 16733d14c5d2SYehuda Sadeh { 1674ac972230SIlya Dryomov struct ceph_pg_pool_info *pool; 1675ac972230SIlya Dryomov u32 pps; 1676ac972230SIlya Dryomov int len; 16773d14c5d2SYehuda Sadeh 1678ac972230SIlya Dryomov pool = __lookup_pg_pool(&osdmap->pg_pools, pgid.pool); 16798008ab10SIlya Dryomov if (!pool) { 16808008ab10SIlya Dryomov *primary = -1; 16818008ab10SIlya Dryomov return -ENOENT; 16828008ab10SIlya Dryomov } 16833d14c5d2SYehuda Sadeh 1684ac972230SIlya Dryomov if (pool->flags & CEPH_POOL_FLAG_HASHPSPOOL) { 1685ac972230SIlya Dryomov /* hash pool id and seed so that pool PGs do not overlap */ 1686ac972230SIlya Dryomov pps = crush_hash32_2(CRUSH_HASH_RJENKINS1, 1687ac972230SIlya Dryomov ceph_stable_mod(pgid.seed, pool->pgp_num, 1688ac972230SIlya Dryomov pool->pgp_num_mask), 1689ac972230SIlya Dryomov pgid.pool); 1690ac972230SIlya Dryomov } else { 1691ac972230SIlya Dryomov /* 1692ac972230SIlya Dryomov * legacy behavior: add ps and pool together. this is 1693ac972230SIlya Dryomov * not a great approach because the PGs from each pool 1694ac972230SIlya Dryomov * will overlap on top of each other: 0.5 == 1.4 == 1695ac972230SIlya Dryomov * 2.3 == ... 1696ac972230SIlya Dryomov */ 1697ac972230SIlya Dryomov pps = ceph_stable_mod(pgid.seed, pool->pgp_num, 1698ac972230SIlya Dryomov pool->pgp_num_mask) + 1699ac972230SIlya Dryomov (unsigned)pgid.pool; 1700ac972230SIlya Dryomov } 1701ac972230SIlya Dryomov 1702ac972230SIlya Dryomov len = pg_to_raw_osds(osdmap, pool, pgid, pps, osds); 17038008ab10SIlya Dryomov if (len < 0) { 17048008ab10SIlya Dryomov *primary = -1; 1705ac972230SIlya Dryomov return len; 17068008ab10SIlya Dryomov } 1707ac972230SIlya Dryomov 17088008ab10SIlya Dryomov len = raw_to_up_osds(osdmap, pool, osds, len, primary); 1709ac972230SIlya Dryomov 171047ec1f3cSIlya Dryomov apply_primary_affinity(osdmap, pps, pool, osds, len, primary); 171147ec1f3cSIlya Dryomov 17128008ab10SIlya Dryomov len = apply_temps(osdmap, pool, pgid, osds, len, primary); 1713ac972230SIlya Dryomov 1714ac972230SIlya Dryomov return len; 17153d14c5d2SYehuda Sadeh } 17163d14c5d2SYehuda Sadeh 17173d14c5d2SYehuda Sadeh /* 17183d14c5d2SYehuda Sadeh * Return primary osd for given pgid, or -1 if none. 17193d14c5d2SYehuda Sadeh */ 17205b191d99SSage Weil int ceph_calc_pg_primary(struct ceph_osdmap *osdmap, struct ceph_pg pgid) 17213d14c5d2SYehuda Sadeh { 1722c4c12285SIlya Dryomov int osds[CEPH_PG_MAX_SIZE]; 1723c4c12285SIlya Dryomov int primary; 17243d14c5d2SYehuda Sadeh 1725c4c12285SIlya Dryomov ceph_calc_pg_acting(osdmap, pgid, osds, &primary); 17263d14c5d2SYehuda Sadeh 1727c4c12285SIlya Dryomov return primary; 17283d14c5d2SYehuda Sadeh } 17293d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_calc_pg_primary); 1730