xref: /openbmc/linux/net/ceph/osdmap.c (revision 958a2765)
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 
125958a2765SIlya Dryomov static int crush_decode_straw2_bucket(void **p, void *end,
126958a2765SIlya Dryomov 				      struct crush_bucket_straw2 *b)
127958a2765SIlya Dryomov {
128958a2765SIlya Dryomov 	int j;
129958a2765SIlya Dryomov 	dout("crush_decode_straw2_bucket %p to %p\n", *p, end);
130958a2765SIlya Dryomov 	b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
131958a2765SIlya Dryomov 	if (b->item_weights == NULL)
132958a2765SIlya Dryomov 		return -ENOMEM;
133958a2765SIlya Dryomov 	ceph_decode_need(p, end, b->h.size * sizeof(u32), bad);
134958a2765SIlya Dryomov 	for (j = 0; j < b->h.size; j++)
135958a2765SIlya Dryomov 		b->item_weights[j] = ceph_decode_32(p);
136958a2765SIlya Dryomov 	return 0;
137958a2765SIlya Dryomov bad:
138958a2765SIlya Dryomov 	return -EINVAL;
139958a2765SIlya Dryomov }
140958a2765SIlya Dryomov 
141546f04efSSage Weil static int skip_name_map(void **p, void *end)
142546f04efSSage Weil {
143546f04efSSage Weil         int len;
144546f04efSSage Weil         ceph_decode_32_safe(p, end, len ,bad);
145546f04efSSage Weil         while (len--) {
146546f04efSSage Weil                 int strlen;
147546f04efSSage Weil                 *p += sizeof(u32);
148546f04efSSage Weil                 ceph_decode_32_safe(p, end, strlen, bad);
149546f04efSSage Weil                 *p += strlen;
150546f04efSSage Weil }
151546f04efSSage Weil         return 0;
152546f04efSSage Weil bad:
153546f04efSSage Weil         return -EINVAL;
154546f04efSSage Weil }
155546f04efSSage Weil 
1563d14c5d2SYehuda Sadeh static struct crush_map *crush_decode(void *pbyval, void *end)
1573d14c5d2SYehuda Sadeh {
1583d14c5d2SYehuda Sadeh 	struct crush_map *c;
1593d14c5d2SYehuda Sadeh 	int err = -EINVAL;
1603d14c5d2SYehuda Sadeh 	int i, j;
1613d14c5d2SYehuda Sadeh 	void **p = &pbyval;
1623d14c5d2SYehuda Sadeh 	void *start = pbyval;
1633d14c5d2SYehuda Sadeh 	u32 magic;
164546f04efSSage Weil 	u32 num_name_maps;
1653d14c5d2SYehuda Sadeh 
1663d14c5d2SYehuda Sadeh 	dout("crush_decode %p to %p len %d\n", *p, end, (int)(end - *p));
1673d14c5d2SYehuda Sadeh 
1683d14c5d2SYehuda Sadeh 	c = kzalloc(sizeof(*c), GFP_NOFS);
1693d14c5d2SYehuda Sadeh 	if (c == NULL)
1703d14c5d2SYehuda Sadeh 		return ERR_PTR(-ENOMEM);
1713d14c5d2SYehuda Sadeh 
172546f04efSSage Weil         /* set tunables to default values */
173546f04efSSage Weil         c->choose_local_tries = 2;
174546f04efSSage Weil         c->choose_local_fallback_tries = 5;
175546f04efSSage Weil         c->choose_total_tries = 19;
1761604f488SJim Schutt 	c->chooseleaf_descend_once = 0;
177546f04efSSage Weil 
1783d14c5d2SYehuda Sadeh 	ceph_decode_need(p, end, 4*sizeof(u32), bad);
1793d14c5d2SYehuda Sadeh 	magic = ceph_decode_32(p);
1803d14c5d2SYehuda Sadeh 	if (magic != CRUSH_MAGIC) {
1813d14c5d2SYehuda Sadeh 		pr_err("crush_decode magic %x != current %x\n",
18295c96174SEric Dumazet 		       (unsigned int)magic, (unsigned int)CRUSH_MAGIC);
1833d14c5d2SYehuda Sadeh 		goto bad;
1843d14c5d2SYehuda Sadeh 	}
1853d14c5d2SYehuda Sadeh 	c->max_buckets = ceph_decode_32(p);
1863d14c5d2SYehuda Sadeh 	c->max_rules = ceph_decode_32(p);
1873d14c5d2SYehuda Sadeh 	c->max_devices = ceph_decode_32(p);
1883d14c5d2SYehuda Sadeh 
1893d14c5d2SYehuda Sadeh 	c->buckets = kcalloc(c->max_buckets, sizeof(*c->buckets), GFP_NOFS);
1903d14c5d2SYehuda Sadeh 	if (c->buckets == NULL)
1913d14c5d2SYehuda Sadeh 		goto badmem;
1923d14c5d2SYehuda Sadeh 	c->rules = kcalloc(c->max_rules, sizeof(*c->rules), GFP_NOFS);
1933d14c5d2SYehuda Sadeh 	if (c->rules == NULL)
1943d14c5d2SYehuda Sadeh 		goto badmem;
1953d14c5d2SYehuda Sadeh 
1963d14c5d2SYehuda Sadeh 	/* buckets */
1973d14c5d2SYehuda Sadeh 	for (i = 0; i < c->max_buckets; i++) {
1983d14c5d2SYehuda Sadeh 		int size = 0;
1993d14c5d2SYehuda Sadeh 		u32 alg;
2003d14c5d2SYehuda Sadeh 		struct crush_bucket *b;
2013d14c5d2SYehuda Sadeh 
2023d14c5d2SYehuda Sadeh 		ceph_decode_32_safe(p, end, alg, bad);
2033d14c5d2SYehuda Sadeh 		if (alg == 0) {
2043d14c5d2SYehuda Sadeh 			c->buckets[i] = NULL;
2053d14c5d2SYehuda Sadeh 			continue;
2063d14c5d2SYehuda Sadeh 		}
2073d14c5d2SYehuda Sadeh 		dout("crush_decode bucket %d off %x %p to %p\n",
2083d14c5d2SYehuda Sadeh 		     i, (int)(*p-start), *p, end);
2093d14c5d2SYehuda Sadeh 
2103d14c5d2SYehuda Sadeh 		switch (alg) {
2113d14c5d2SYehuda Sadeh 		case CRUSH_BUCKET_UNIFORM:
2123d14c5d2SYehuda Sadeh 			size = sizeof(struct crush_bucket_uniform);
2133d14c5d2SYehuda Sadeh 			break;
2143d14c5d2SYehuda Sadeh 		case CRUSH_BUCKET_LIST:
2153d14c5d2SYehuda Sadeh 			size = sizeof(struct crush_bucket_list);
2163d14c5d2SYehuda Sadeh 			break;
2173d14c5d2SYehuda Sadeh 		case CRUSH_BUCKET_TREE:
2183d14c5d2SYehuda Sadeh 			size = sizeof(struct crush_bucket_tree);
2193d14c5d2SYehuda Sadeh 			break;
2203d14c5d2SYehuda Sadeh 		case CRUSH_BUCKET_STRAW:
2213d14c5d2SYehuda Sadeh 			size = sizeof(struct crush_bucket_straw);
2223d14c5d2SYehuda Sadeh 			break;
223958a2765SIlya Dryomov 		case CRUSH_BUCKET_STRAW2:
224958a2765SIlya Dryomov 			size = sizeof(struct crush_bucket_straw2);
225958a2765SIlya Dryomov 			break;
2263d14c5d2SYehuda Sadeh 		default:
2273d14c5d2SYehuda Sadeh 			err = -EINVAL;
2283d14c5d2SYehuda Sadeh 			goto bad;
2293d14c5d2SYehuda Sadeh 		}
2303d14c5d2SYehuda Sadeh 		BUG_ON(size == 0);
2313d14c5d2SYehuda Sadeh 		b = c->buckets[i] = kzalloc(size, GFP_NOFS);
2323d14c5d2SYehuda Sadeh 		if (b == NULL)
2333d14c5d2SYehuda Sadeh 			goto badmem;
2343d14c5d2SYehuda Sadeh 
2353d14c5d2SYehuda Sadeh 		ceph_decode_need(p, end, 4*sizeof(u32), bad);
2363d14c5d2SYehuda Sadeh 		b->id = ceph_decode_32(p);
2373d14c5d2SYehuda Sadeh 		b->type = ceph_decode_16(p);
2383d14c5d2SYehuda Sadeh 		b->alg = ceph_decode_8(p);
2393d14c5d2SYehuda Sadeh 		b->hash = ceph_decode_8(p);
2403d14c5d2SYehuda Sadeh 		b->weight = ceph_decode_32(p);
2413d14c5d2SYehuda Sadeh 		b->size = ceph_decode_32(p);
2423d14c5d2SYehuda Sadeh 
2433d14c5d2SYehuda Sadeh 		dout("crush_decode bucket size %d off %x %p to %p\n",
2443d14c5d2SYehuda Sadeh 		     b->size, (int)(*p-start), *p, end);
2453d14c5d2SYehuda Sadeh 
2463d14c5d2SYehuda Sadeh 		b->items = kcalloc(b->size, sizeof(__s32), GFP_NOFS);
2473d14c5d2SYehuda Sadeh 		if (b->items == NULL)
2483d14c5d2SYehuda Sadeh 			goto badmem;
2493d14c5d2SYehuda Sadeh 		b->perm = kcalloc(b->size, sizeof(u32), GFP_NOFS);
2503d14c5d2SYehuda Sadeh 		if (b->perm == NULL)
2513d14c5d2SYehuda Sadeh 			goto badmem;
2523d14c5d2SYehuda Sadeh 		b->perm_n = 0;
2533d14c5d2SYehuda Sadeh 
2543d14c5d2SYehuda Sadeh 		ceph_decode_need(p, end, b->size*sizeof(u32), bad);
2553d14c5d2SYehuda Sadeh 		for (j = 0; j < b->size; j++)
2563d14c5d2SYehuda Sadeh 			b->items[j] = ceph_decode_32(p);
2573d14c5d2SYehuda Sadeh 
2583d14c5d2SYehuda Sadeh 		switch (b->alg) {
2593d14c5d2SYehuda Sadeh 		case CRUSH_BUCKET_UNIFORM:
2603d14c5d2SYehuda Sadeh 			err = crush_decode_uniform_bucket(p, end,
2613d14c5d2SYehuda Sadeh 				  (struct crush_bucket_uniform *)b);
2623d14c5d2SYehuda Sadeh 			if (err < 0)
2633d14c5d2SYehuda Sadeh 				goto bad;
2643d14c5d2SYehuda Sadeh 			break;
2653d14c5d2SYehuda Sadeh 		case CRUSH_BUCKET_LIST:
2663d14c5d2SYehuda Sadeh 			err = crush_decode_list_bucket(p, end,
2673d14c5d2SYehuda Sadeh 			       (struct crush_bucket_list *)b);
2683d14c5d2SYehuda Sadeh 			if (err < 0)
2693d14c5d2SYehuda Sadeh 				goto bad;
2703d14c5d2SYehuda Sadeh 			break;
2713d14c5d2SYehuda Sadeh 		case CRUSH_BUCKET_TREE:
2723d14c5d2SYehuda Sadeh 			err = crush_decode_tree_bucket(p, end,
2733d14c5d2SYehuda Sadeh 				(struct crush_bucket_tree *)b);
2743d14c5d2SYehuda Sadeh 			if (err < 0)
2753d14c5d2SYehuda Sadeh 				goto bad;
2763d14c5d2SYehuda Sadeh 			break;
2773d14c5d2SYehuda Sadeh 		case CRUSH_BUCKET_STRAW:
2783d14c5d2SYehuda Sadeh 			err = crush_decode_straw_bucket(p, end,
2793d14c5d2SYehuda Sadeh 				(struct crush_bucket_straw *)b);
2803d14c5d2SYehuda Sadeh 			if (err < 0)
2813d14c5d2SYehuda Sadeh 				goto bad;
2823d14c5d2SYehuda Sadeh 			break;
283958a2765SIlya Dryomov 		case CRUSH_BUCKET_STRAW2:
284958a2765SIlya Dryomov 			err = crush_decode_straw2_bucket(p, end,
285958a2765SIlya Dryomov 				(struct crush_bucket_straw2 *)b);
286958a2765SIlya Dryomov 			if (err < 0)
287958a2765SIlya Dryomov 				goto bad;
288958a2765SIlya Dryomov 			break;
2893d14c5d2SYehuda Sadeh 		}
2903d14c5d2SYehuda Sadeh 	}
2913d14c5d2SYehuda Sadeh 
2923d14c5d2SYehuda Sadeh 	/* rules */
2933d14c5d2SYehuda Sadeh 	dout("rule vec is %p\n", c->rules);
2943d14c5d2SYehuda Sadeh 	for (i = 0; i < c->max_rules; i++) {
2953d14c5d2SYehuda Sadeh 		u32 yes;
2963d14c5d2SYehuda Sadeh 		struct crush_rule *r;
2973d14c5d2SYehuda Sadeh 
2983d14c5d2SYehuda Sadeh 		ceph_decode_32_safe(p, end, yes, bad);
2993d14c5d2SYehuda Sadeh 		if (!yes) {
3003d14c5d2SYehuda Sadeh 			dout("crush_decode NO rule %d off %x %p to %p\n",
3013d14c5d2SYehuda Sadeh 			     i, (int)(*p-start), *p, end);
3023d14c5d2SYehuda Sadeh 			c->rules[i] = NULL;
3033d14c5d2SYehuda Sadeh 			continue;
3043d14c5d2SYehuda Sadeh 		}
3053d14c5d2SYehuda Sadeh 
3063d14c5d2SYehuda Sadeh 		dout("crush_decode rule %d off %x %p to %p\n",
3073d14c5d2SYehuda Sadeh 		     i, (int)(*p-start), *p, end);
3083d14c5d2SYehuda Sadeh 
3093d14c5d2SYehuda Sadeh 		/* len */
3103d14c5d2SYehuda Sadeh 		ceph_decode_32_safe(p, end, yes, bad);
3113d14c5d2SYehuda Sadeh #if BITS_PER_LONG == 32
3123d14c5d2SYehuda Sadeh 		err = -EINVAL;
31364486697SXi Wang 		if (yes > (ULONG_MAX - sizeof(*r))
31464486697SXi Wang 			  / sizeof(struct crush_rule_step))
3153d14c5d2SYehuda Sadeh 			goto bad;
3163d14c5d2SYehuda Sadeh #endif
3173d14c5d2SYehuda Sadeh 		r = c->rules[i] = kmalloc(sizeof(*r) +
3183d14c5d2SYehuda Sadeh 					  yes*sizeof(struct crush_rule_step),
3193d14c5d2SYehuda Sadeh 					  GFP_NOFS);
3203d14c5d2SYehuda Sadeh 		if (r == NULL)
3213d14c5d2SYehuda Sadeh 			goto badmem;
3223d14c5d2SYehuda Sadeh 		dout(" rule %d is at %p\n", i, r);
3233d14c5d2SYehuda Sadeh 		r->len = yes;
3243d14c5d2SYehuda Sadeh 		ceph_decode_copy_safe(p, end, &r->mask, 4, bad); /* 4 u8's */
3253d14c5d2SYehuda Sadeh 		ceph_decode_need(p, end, r->len*3*sizeof(u32), bad);
3263d14c5d2SYehuda Sadeh 		for (j = 0; j < r->len; j++) {
3273d14c5d2SYehuda Sadeh 			r->steps[j].op = ceph_decode_32(p);
3283d14c5d2SYehuda Sadeh 			r->steps[j].arg1 = ceph_decode_32(p);
3293d14c5d2SYehuda Sadeh 			r->steps[j].arg2 = ceph_decode_32(p);
3303d14c5d2SYehuda Sadeh 		}
3313d14c5d2SYehuda Sadeh 	}
3323d14c5d2SYehuda Sadeh 
3333d14c5d2SYehuda Sadeh 	/* ignore trailing name maps. */
334546f04efSSage Weil         for (num_name_maps = 0; num_name_maps < 3; num_name_maps++) {
335546f04efSSage Weil                 err = skip_name_map(p, end);
336546f04efSSage Weil                 if (err < 0)
337546f04efSSage Weil                         goto done;
338546f04efSSage Weil         }
3393d14c5d2SYehuda Sadeh 
340546f04efSSage Weil         /* tunables */
341546f04efSSage Weil         ceph_decode_need(p, end, 3*sizeof(u32), done);
342546f04efSSage Weil         c->choose_local_tries = ceph_decode_32(p);
343546f04efSSage Weil         c->choose_local_fallback_tries =  ceph_decode_32(p);
344546f04efSSage Weil         c->choose_total_tries = ceph_decode_32(p);
345546f04efSSage Weil         dout("crush decode tunable choose_local_tries = %d",
346546f04efSSage Weil              c->choose_local_tries);
347546f04efSSage Weil         dout("crush decode tunable choose_local_fallback_tries = %d",
348546f04efSSage Weil              c->choose_local_fallback_tries);
349546f04efSSage Weil         dout("crush decode tunable choose_total_tries = %d",
350546f04efSSage Weil              c->choose_total_tries);
351546f04efSSage Weil 
3521604f488SJim Schutt 	ceph_decode_need(p, end, sizeof(u32), done);
3531604f488SJim Schutt 	c->chooseleaf_descend_once = ceph_decode_32(p);
3541604f488SJim Schutt 	dout("crush decode tunable chooseleaf_descend_once = %d",
3551604f488SJim Schutt 	     c->chooseleaf_descend_once);
3561604f488SJim Schutt 
357f140662fSIlya Dryomov 	ceph_decode_need(p, end, sizeof(u8), done);
358f140662fSIlya Dryomov 	c->chooseleaf_vary_r = ceph_decode_8(p);
359f140662fSIlya Dryomov 	dout("crush decode tunable chooseleaf_vary_r = %d",
360f140662fSIlya Dryomov 	     c->chooseleaf_vary_r);
361f140662fSIlya Dryomov 
362546f04efSSage Weil done:
3633d14c5d2SYehuda Sadeh 	dout("crush_decode success\n");
3643d14c5d2SYehuda Sadeh 	return c;
3653d14c5d2SYehuda Sadeh 
3663d14c5d2SYehuda Sadeh badmem:
3673d14c5d2SYehuda Sadeh 	err = -ENOMEM;
3683d14c5d2SYehuda Sadeh bad:
3693d14c5d2SYehuda Sadeh 	dout("crush_decode fail %d\n", err);
3703d14c5d2SYehuda Sadeh 	crush_destroy(c);
3713d14c5d2SYehuda Sadeh 	return ERR_PTR(err);
3723d14c5d2SYehuda Sadeh }
3733d14c5d2SYehuda Sadeh 
3743d14c5d2SYehuda Sadeh /*
3753d14c5d2SYehuda Sadeh  * rbtree of pg_mapping for handling pg_temp (explicit mapping of pgid
3769686f94cSIlya Dryomov  * to a set of osds) and primary_temp (explicit primary setting)
3773d14c5d2SYehuda Sadeh  */
3785b191d99SSage Weil static int pgid_cmp(struct ceph_pg l, struct ceph_pg r)
3793d14c5d2SYehuda Sadeh {
3805b191d99SSage Weil 	if (l.pool < r.pool)
3813d14c5d2SYehuda Sadeh 		return -1;
3825b191d99SSage Weil 	if (l.pool > r.pool)
3835b191d99SSage Weil 		return 1;
3845b191d99SSage Weil 	if (l.seed < r.seed)
3855b191d99SSage Weil 		return -1;
3865b191d99SSage Weil 	if (l.seed > r.seed)
3873d14c5d2SYehuda Sadeh 		return 1;
3883d14c5d2SYehuda Sadeh 	return 0;
3893d14c5d2SYehuda Sadeh }
3903d14c5d2SYehuda Sadeh 
3913d14c5d2SYehuda Sadeh static int __insert_pg_mapping(struct ceph_pg_mapping *new,
3923d14c5d2SYehuda Sadeh 			       struct rb_root *root)
3933d14c5d2SYehuda Sadeh {
3943d14c5d2SYehuda Sadeh 	struct rb_node **p = &root->rb_node;
3953d14c5d2SYehuda Sadeh 	struct rb_node *parent = NULL;
3963d14c5d2SYehuda Sadeh 	struct ceph_pg_mapping *pg = NULL;
3973d14c5d2SYehuda Sadeh 	int c;
3983d14c5d2SYehuda Sadeh 
3998adc8b3dSSage Weil 	dout("__insert_pg_mapping %llx %p\n", *(u64 *)&new->pgid, new);
4003d14c5d2SYehuda Sadeh 	while (*p) {
4013d14c5d2SYehuda Sadeh 		parent = *p;
4023d14c5d2SYehuda Sadeh 		pg = rb_entry(parent, struct ceph_pg_mapping, node);
4033d14c5d2SYehuda Sadeh 		c = pgid_cmp(new->pgid, pg->pgid);
4043d14c5d2SYehuda Sadeh 		if (c < 0)
4053d14c5d2SYehuda Sadeh 			p = &(*p)->rb_left;
4063d14c5d2SYehuda Sadeh 		else if (c > 0)
4073d14c5d2SYehuda Sadeh 			p = &(*p)->rb_right;
4083d14c5d2SYehuda Sadeh 		else
4093d14c5d2SYehuda Sadeh 			return -EEXIST;
4103d14c5d2SYehuda Sadeh 	}
4113d14c5d2SYehuda Sadeh 
4123d14c5d2SYehuda Sadeh 	rb_link_node(&new->node, parent, p);
4133d14c5d2SYehuda Sadeh 	rb_insert_color(&new->node, root);
4143d14c5d2SYehuda Sadeh 	return 0;
4153d14c5d2SYehuda Sadeh }
4163d14c5d2SYehuda Sadeh 
4173d14c5d2SYehuda Sadeh static struct ceph_pg_mapping *__lookup_pg_mapping(struct rb_root *root,
4185b191d99SSage Weil 						   struct ceph_pg pgid)
4193d14c5d2SYehuda Sadeh {
4203d14c5d2SYehuda Sadeh 	struct rb_node *n = root->rb_node;
4213d14c5d2SYehuda Sadeh 	struct ceph_pg_mapping *pg;
4223d14c5d2SYehuda Sadeh 	int c;
4233d14c5d2SYehuda Sadeh 
4243d14c5d2SYehuda Sadeh 	while (n) {
4253d14c5d2SYehuda Sadeh 		pg = rb_entry(n, struct ceph_pg_mapping, node);
4263d14c5d2SYehuda Sadeh 		c = pgid_cmp(pgid, pg->pgid);
4278adc8b3dSSage Weil 		if (c < 0) {
4283d14c5d2SYehuda Sadeh 			n = n->rb_left;
4298adc8b3dSSage Weil 		} else if (c > 0) {
4303d14c5d2SYehuda Sadeh 			n = n->rb_right;
4318adc8b3dSSage Weil 		} else {
4325b191d99SSage Weil 			dout("__lookup_pg_mapping %lld.%x got %p\n",
4335b191d99SSage Weil 			     pgid.pool, pgid.seed, pg);
4343d14c5d2SYehuda Sadeh 			return pg;
4353d14c5d2SYehuda Sadeh 		}
4368adc8b3dSSage Weil 	}
4373d14c5d2SYehuda Sadeh 	return NULL;
4383d14c5d2SYehuda Sadeh }
4393d14c5d2SYehuda Sadeh 
4405b191d99SSage Weil static int __remove_pg_mapping(struct rb_root *root, struct ceph_pg pgid)
4418adc8b3dSSage Weil {
4428adc8b3dSSage Weil 	struct ceph_pg_mapping *pg = __lookup_pg_mapping(root, pgid);
4438adc8b3dSSage Weil 
4448adc8b3dSSage Weil 	if (pg) {
4455b191d99SSage Weil 		dout("__remove_pg_mapping %lld.%x %p\n", pgid.pool, pgid.seed,
4465b191d99SSage Weil 		     pg);
4478adc8b3dSSage Weil 		rb_erase(&pg->node, root);
4488adc8b3dSSage Weil 		kfree(pg);
4498adc8b3dSSage Weil 		return 0;
4508adc8b3dSSage Weil 	}
4515b191d99SSage Weil 	dout("__remove_pg_mapping %lld.%x dne\n", pgid.pool, pgid.seed);
4528adc8b3dSSage Weil 	return -ENOENT;
4538adc8b3dSSage Weil }
4548adc8b3dSSage Weil 
4553d14c5d2SYehuda Sadeh /*
4563d14c5d2SYehuda Sadeh  * rbtree of pg pool info
4573d14c5d2SYehuda Sadeh  */
4583d14c5d2SYehuda Sadeh static int __insert_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *new)
4593d14c5d2SYehuda Sadeh {
4603d14c5d2SYehuda Sadeh 	struct rb_node **p = &root->rb_node;
4613d14c5d2SYehuda Sadeh 	struct rb_node *parent = NULL;
4623d14c5d2SYehuda Sadeh 	struct ceph_pg_pool_info *pi = NULL;
4633d14c5d2SYehuda Sadeh 
4643d14c5d2SYehuda Sadeh 	while (*p) {
4653d14c5d2SYehuda Sadeh 		parent = *p;
4663d14c5d2SYehuda Sadeh 		pi = rb_entry(parent, struct ceph_pg_pool_info, node);
4673d14c5d2SYehuda Sadeh 		if (new->id < pi->id)
4683d14c5d2SYehuda Sadeh 			p = &(*p)->rb_left;
4693d14c5d2SYehuda Sadeh 		else if (new->id > pi->id)
4703d14c5d2SYehuda Sadeh 			p = &(*p)->rb_right;
4713d14c5d2SYehuda Sadeh 		else
4723d14c5d2SYehuda Sadeh 			return -EEXIST;
4733d14c5d2SYehuda Sadeh 	}
4743d14c5d2SYehuda Sadeh 
4753d14c5d2SYehuda Sadeh 	rb_link_node(&new->node, parent, p);
4763d14c5d2SYehuda Sadeh 	rb_insert_color(&new->node, root);
4773d14c5d2SYehuda Sadeh 	return 0;
4783d14c5d2SYehuda Sadeh }
4793d14c5d2SYehuda Sadeh 
4804f6a7e5eSSage Weil static struct ceph_pg_pool_info *__lookup_pg_pool(struct rb_root *root, u64 id)
4813d14c5d2SYehuda Sadeh {
4823d14c5d2SYehuda Sadeh 	struct ceph_pg_pool_info *pi;
4833d14c5d2SYehuda Sadeh 	struct rb_node *n = root->rb_node;
4843d14c5d2SYehuda Sadeh 
4853d14c5d2SYehuda Sadeh 	while (n) {
4863d14c5d2SYehuda Sadeh 		pi = rb_entry(n, struct ceph_pg_pool_info, node);
4873d14c5d2SYehuda Sadeh 		if (id < pi->id)
4883d14c5d2SYehuda Sadeh 			n = n->rb_left;
4893d14c5d2SYehuda Sadeh 		else if (id > pi->id)
4903d14c5d2SYehuda Sadeh 			n = n->rb_right;
4913d14c5d2SYehuda Sadeh 		else
4923d14c5d2SYehuda Sadeh 			return pi;
4933d14c5d2SYehuda Sadeh 	}
4943d14c5d2SYehuda Sadeh 	return NULL;
4953d14c5d2SYehuda Sadeh }
4963d14c5d2SYehuda Sadeh 
497ce7f6a27SIlya Dryomov struct ceph_pg_pool_info *ceph_pg_pool_by_id(struct ceph_osdmap *map, u64 id)
498ce7f6a27SIlya Dryomov {
499ce7f6a27SIlya Dryomov 	return __lookup_pg_pool(&map->pg_pools, id);
500ce7f6a27SIlya Dryomov }
501ce7f6a27SIlya Dryomov 
50272afc71fSAlex Elder const char *ceph_pg_pool_name_by_id(struct ceph_osdmap *map, u64 id)
50372afc71fSAlex Elder {
50472afc71fSAlex Elder 	struct ceph_pg_pool_info *pi;
50572afc71fSAlex Elder 
50672afc71fSAlex Elder 	if (id == CEPH_NOPOOL)
50772afc71fSAlex Elder 		return NULL;
50872afc71fSAlex Elder 
50972afc71fSAlex Elder 	if (WARN_ON_ONCE(id > (u64) INT_MAX))
51072afc71fSAlex Elder 		return NULL;
51172afc71fSAlex Elder 
51272afc71fSAlex Elder 	pi = __lookup_pg_pool(&map->pg_pools, (int) id);
51372afc71fSAlex Elder 
51472afc71fSAlex Elder 	return pi ? pi->name : NULL;
51572afc71fSAlex Elder }
51672afc71fSAlex Elder EXPORT_SYMBOL(ceph_pg_pool_name_by_id);
51772afc71fSAlex Elder 
5183d14c5d2SYehuda Sadeh int ceph_pg_poolid_by_name(struct ceph_osdmap *map, const char *name)
5193d14c5d2SYehuda Sadeh {
5203d14c5d2SYehuda Sadeh 	struct rb_node *rbp;
5213d14c5d2SYehuda Sadeh 
5223d14c5d2SYehuda Sadeh 	for (rbp = rb_first(&map->pg_pools); rbp; rbp = rb_next(rbp)) {
5233d14c5d2SYehuda Sadeh 		struct ceph_pg_pool_info *pi =
5243d14c5d2SYehuda Sadeh 			rb_entry(rbp, struct ceph_pg_pool_info, node);
5253d14c5d2SYehuda Sadeh 		if (pi->name && strcmp(pi->name, name) == 0)
5263d14c5d2SYehuda Sadeh 			return pi->id;
5273d14c5d2SYehuda Sadeh 	}
5283d14c5d2SYehuda Sadeh 	return -ENOENT;
5293d14c5d2SYehuda Sadeh }
5303d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_pg_poolid_by_name);
5313d14c5d2SYehuda Sadeh 
5323d14c5d2SYehuda Sadeh static void __remove_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *pi)
5333d14c5d2SYehuda Sadeh {
5343d14c5d2SYehuda Sadeh 	rb_erase(&pi->node, root);
5353d14c5d2SYehuda Sadeh 	kfree(pi->name);
5363d14c5d2SYehuda Sadeh 	kfree(pi);
5373d14c5d2SYehuda Sadeh }
5383d14c5d2SYehuda Sadeh 
5390f70c7eeSIlya Dryomov static int decode_pool(void **p, void *end, struct ceph_pg_pool_info *pi)
5403d14c5d2SYehuda Sadeh {
5414f6a7e5eSSage Weil 	u8 ev, cv;
5424f6a7e5eSSage Weil 	unsigned len, num;
5434f6a7e5eSSage Weil 	void *pool_end;
5443d14c5d2SYehuda Sadeh 
5454f6a7e5eSSage Weil 	ceph_decode_need(p, end, 2 + 4, bad);
5464f6a7e5eSSage Weil 	ev = ceph_decode_8(p);  /* encoding version */
5474f6a7e5eSSage Weil 	cv = ceph_decode_8(p); /* compat version */
5484f6a7e5eSSage Weil 	if (ev < 5) {
549b9a67899SJoe Perches 		pr_warn("got v %d < 5 cv %d of ceph_pg_pool\n", ev, cv);
5504f6a7e5eSSage Weil 		return -EINVAL;
5514f6a7e5eSSage Weil 	}
55217a13e40SIlya Dryomov 	if (cv > 9) {
553b9a67899SJoe Perches 		pr_warn("got v %d cv %d > 9 of ceph_pg_pool\n", ev, cv);
5544f6a7e5eSSage Weil 		return -EINVAL;
5554f6a7e5eSSage Weil 	}
5564f6a7e5eSSage Weil 	len = ceph_decode_32(p);
5574f6a7e5eSSage Weil 	ceph_decode_need(p, end, len, bad);
5584f6a7e5eSSage Weil 	pool_end = *p + len;
5593d14c5d2SYehuda Sadeh 
5604f6a7e5eSSage Weil 	pi->type = ceph_decode_8(p);
5614f6a7e5eSSage Weil 	pi->size = ceph_decode_8(p);
5624f6a7e5eSSage Weil 	pi->crush_ruleset = ceph_decode_8(p);
5634f6a7e5eSSage Weil 	pi->object_hash = ceph_decode_8(p);
5644f6a7e5eSSage Weil 
5654f6a7e5eSSage Weil 	pi->pg_num = ceph_decode_32(p);
5664f6a7e5eSSage Weil 	pi->pgp_num = ceph_decode_32(p);
5674f6a7e5eSSage Weil 
5684f6a7e5eSSage Weil 	*p += 4 + 4;  /* skip lpg* */
5694f6a7e5eSSage Weil 	*p += 4;      /* skip last_change */
5704f6a7e5eSSage Weil 	*p += 8 + 4;  /* skip snap_seq, snap_epoch */
5714f6a7e5eSSage Weil 
5724f6a7e5eSSage Weil 	/* skip snaps */
5734f6a7e5eSSage Weil 	num = ceph_decode_32(p);
5744f6a7e5eSSage Weil 	while (num--) {
5754f6a7e5eSSage Weil 		*p += 8;  /* snapid key */
5764f6a7e5eSSage Weil 		*p += 1 + 1; /* versions */
5774f6a7e5eSSage Weil 		len = ceph_decode_32(p);
5784f6a7e5eSSage Weil 		*p += len;
5793d14c5d2SYehuda Sadeh 	}
5803d14c5d2SYehuda Sadeh 
58117a13e40SIlya Dryomov 	/* skip removed_snaps */
5824f6a7e5eSSage Weil 	num = ceph_decode_32(p);
5834f6a7e5eSSage Weil 	*p += num * (8 + 8);
5844f6a7e5eSSage Weil 
5854f6a7e5eSSage Weil 	*p += 8;  /* skip auid */
5864f6a7e5eSSage Weil 	pi->flags = ceph_decode_64(p);
58717a13e40SIlya Dryomov 	*p += 4;  /* skip crash_replay_interval */
58817a13e40SIlya Dryomov 
58917a13e40SIlya Dryomov 	if (ev >= 7)
59017a13e40SIlya Dryomov 		*p += 1;  /* skip min_size */
59117a13e40SIlya Dryomov 
59217a13e40SIlya Dryomov 	if (ev >= 8)
59317a13e40SIlya Dryomov 		*p += 8 + 8;  /* skip quota_max_* */
59417a13e40SIlya Dryomov 
59517a13e40SIlya Dryomov 	if (ev >= 9) {
59617a13e40SIlya Dryomov 		/* skip tiers */
59717a13e40SIlya Dryomov 		num = ceph_decode_32(p);
59817a13e40SIlya Dryomov 		*p += num * 8;
59917a13e40SIlya Dryomov 
60017a13e40SIlya Dryomov 		*p += 8;  /* skip tier_of */
60117a13e40SIlya Dryomov 		*p += 1;  /* skip cache_mode */
60217a13e40SIlya Dryomov 
60317a13e40SIlya Dryomov 		pi->read_tier = ceph_decode_64(p);
60417a13e40SIlya Dryomov 		pi->write_tier = ceph_decode_64(p);
60517a13e40SIlya Dryomov 	} else {
60617a13e40SIlya Dryomov 		pi->read_tier = -1;
60717a13e40SIlya Dryomov 		pi->write_tier = -1;
60817a13e40SIlya Dryomov 	}
6094f6a7e5eSSage Weil 
6104f6a7e5eSSage Weil 	/* ignore the rest */
6114f6a7e5eSSage Weil 
6124f6a7e5eSSage Weil 	*p = pool_end;
6134f6a7e5eSSage Weil 	calc_pg_masks(pi);
6143d14c5d2SYehuda Sadeh 	return 0;
6153d14c5d2SYehuda Sadeh 
6163d14c5d2SYehuda Sadeh bad:
6173d14c5d2SYehuda Sadeh 	return -EINVAL;
6183d14c5d2SYehuda Sadeh }
6193d14c5d2SYehuda Sadeh 
6200f70c7eeSIlya Dryomov static int decode_pool_names(void **p, void *end, struct ceph_osdmap *map)
6213d14c5d2SYehuda Sadeh {
6223d14c5d2SYehuda Sadeh 	struct ceph_pg_pool_info *pi;
6234f6a7e5eSSage Weil 	u32 num, len;
6244f6a7e5eSSage Weil 	u64 pool;
6253d14c5d2SYehuda Sadeh 
6263d14c5d2SYehuda Sadeh 	ceph_decode_32_safe(p, end, num, bad);
6273d14c5d2SYehuda Sadeh 	dout(" %d pool names\n", num);
6283d14c5d2SYehuda Sadeh 	while (num--) {
6294f6a7e5eSSage Weil 		ceph_decode_64_safe(p, end, pool, bad);
6303d14c5d2SYehuda Sadeh 		ceph_decode_32_safe(p, end, len, bad);
6314f6a7e5eSSage Weil 		dout("  pool %llu len %d\n", pool, len);
632ad3b904cSXi Wang 		ceph_decode_need(p, end, len, bad);
6333d14c5d2SYehuda Sadeh 		pi = __lookup_pg_pool(&map->pg_pools, pool);
6343d14c5d2SYehuda Sadeh 		if (pi) {
635ad3b904cSXi Wang 			char *name = kstrndup(*p, len, GFP_NOFS);
636ad3b904cSXi Wang 
637ad3b904cSXi Wang 			if (!name)
638ad3b904cSXi Wang 				return -ENOMEM;
6393d14c5d2SYehuda Sadeh 			kfree(pi->name);
640ad3b904cSXi Wang 			pi->name = name;
6413d14c5d2SYehuda Sadeh 			dout("  name is %s\n", pi->name);
6423d14c5d2SYehuda Sadeh 		}
6433d14c5d2SYehuda Sadeh 		*p += len;
6443d14c5d2SYehuda Sadeh 	}
6453d14c5d2SYehuda Sadeh 	return 0;
6463d14c5d2SYehuda Sadeh 
6473d14c5d2SYehuda Sadeh bad:
6483d14c5d2SYehuda Sadeh 	return -EINVAL;
6493d14c5d2SYehuda Sadeh }
6503d14c5d2SYehuda Sadeh 
6513d14c5d2SYehuda Sadeh /*
6523d14c5d2SYehuda Sadeh  * osd map
6533d14c5d2SYehuda Sadeh  */
6543d14c5d2SYehuda Sadeh void ceph_osdmap_destroy(struct ceph_osdmap *map)
6553d14c5d2SYehuda Sadeh {
6563d14c5d2SYehuda Sadeh 	dout("osdmap_destroy %p\n", map);
6573d14c5d2SYehuda Sadeh 	if (map->crush)
6583d14c5d2SYehuda Sadeh 		crush_destroy(map->crush);
6593d14c5d2SYehuda Sadeh 	while (!RB_EMPTY_ROOT(&map->pg_temp)) {
6603d14c5d2SYehuda Sadeh 		struct ceph_pg_mapping *pg =
6613d14c5d2SYehuda Sadeh 			rb_entry(rb_first(&map->pg_temp),
6623d14c5d2SYehuda Sadeh 				 struct ceph_pg_mapping, node);
6633d14c5d2SYehuda Sadeh 		rb_erase(&pg->node, &map->pg_temp);
6643d14c5d2SYehuda Sadeh 		kfree(pg);
6653d14c5d2SYehuda Sadeh 	}
6669686f94cSIlya Dryomov 	while (!RB_EMPTY_ROOT(&map->primary_temp)) {
6679686f94cSIlya Dryomov 		struct ceph_pg_mapping *pg =
6689686f94cSIlya Dryomov 			rb_entry(rb_first(&map->primary_temp),
6699686f94cSIlya Dryomov 				 struct ceph_pg_mapping, node);
6709686f94cSIlya Dryomov 		rb_erase(&pg->node, &map->primary_temp);
6719686f94cSIlya Dryomov 		kfree(pg);
6729686f94cSIlya Dryomov 	}
6733d14c5d2SYehuda Sadeh 	while (!RB_EMPTY_ROOT(&map->pg_pools)) {
6743d14c5d2SYehuda Sadeh 		struct ceph_pg_pool_info *pi =
6753d14c5d2SYehuda Sadeh 			rb_entry(rb_first(&map->pg_pools),
6763d14c5d2SYehuda Sadeh 				 struct ceph_pg_pool_info, node);
6773d14c5d2SYehuda Sadeh 		__remove_pg_pool(&map->pg_pools, pi);
6783d14c5d2SYehuda Sadeh 	}
6793d14c5d2SYehuda Sadeh 	kfree(map->osd_state);
6803d14c5d2SYehuda Sadeh 	kfree(map->osd_weight);
6813d14c5d2SYehuda Sadeh 	kfree(map->osd_addr);
6822cfa34f2SIlya Dryomov 	kfree(map->osd_primary_affinity);
6833d14c5d2SYehuda Sadeh 	kfree(map);
6843d14c5d2SYehuda Sadeh }
6853d14c5d2SYehuda Sadeh 
6863d14c5d2SYehuda Sadeh /*
6874d60351fSIlya Dryomov  * Adjust max_osd value, (re)allocate arrays.
6884d60351fSIlya Dryomov  *
6894d60351fSIlya Dryomov  * The new elements are properly initialized.
6903d14c5d2SYehuda Sadeh  */
6913d14c5d2SYehuda Sadeh static int osdmap_set_max_osd(struct ceph_osdmap *map, int max)
6923d14c5d2SYehuda Sadeh {
6933d14c5d2SYehuda Sadeh 	u8 *state;
6943d14c5d2SYehuda Sadeh 	u32 *weight;
6954d60351fSIlya Dryomov 	struct ceph_entity_addr *addr;
6964d60351fSIlya Dryomov 	int i;
6973d14c5d2SYehuda Sadeh 
6984d60351fSIlya Dryomov 	state = krealloc(map->osd_state, max*sizeof(*state), GFP_NOFS);
699589506f1SLi RongQing 	if (!state)
7003d14c5d2SYehuda Sadeh 		return -ENOMEM;
701589506f1SLi RongQing 	map->osd_state = state;
702589506f1SLi RongQing 
703589506f1SLi RongQing 	weight = krealloc(map->osd_weight, max*sizeof(*weight), GFP_NOFS);
704589506f1SLi RongQing 	if (!weight)
705589506f1SLi RongQing 		return -ENOMEM;
706589506f1SLi RongQing 	map->osd_weight = weight;
707589506f1SLi RongQing 
708589506f1SLi RongQing 	addr = krealloc(map->osd_addr, max*sizeof(*addr), GFP_NOFS);
709589506f1SLi RongQing 	if (!addr)
710589506f1SLi RongQing 		return -ENOMEM;
711589506f1SLi RongQing 	map->osd_addr = addr;
7123d14c5d2SYehuda Sadeh 
7134d60351fSIlya Dryomov 	for (i = map->max_osd; i < max; i++) {
714589506f1SLi RongQing 		map->osd_state[i] = 0;
715589506f1SLi RongQing 		map->osd_weight[i] = CEPH_OSD_OUT;
716589506f1SLi RongQing 		memset(map->osd_addr + i, 0, sizeof(*map->osd_addr));
7173d14c5d2SYehuda Sadeh 	}
7183d14c5d2SYehuda Sadeh 
7192cfa34f2SIlya Dryomov 	if (map->osd_primary_affinity) {
7202cfa34f2SIlya Dryomov 		u32 *affinity;
7212cfa34f2SIlya Dryomov 
7222cfa34f2SIlya Dryomov 		affinity = krealloc(map->osd_primary_affinity,
7232cfa34f2SIlya Dryomov 				    max*sizeof(*affinity), GFP_NOFS);
7242cfa34f2SIlya Dryomov 		if (!affinity)
7252cfa34f2SIlya Dryomov 			return -ENOMEM;
726589506f1SLi RongQing 		map->osd_primary_affinity = affinity;
7272cfa34f2SIlya Dryomov 
7282cfa34f2SIlya Dryomov 		for (i = map->max_osd; i < max; i++)
729589506f1SLi RongQing 			map->osd_primary_affinity[i] =
730589506f1SLi RongQing 			    CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
7312cfa34f2SIlya Dryomov 	}
7322cfa34f2SIlya Dryomov 
7333d14c5d2SYehuda Sadeh 	map->max_osd = max;
7344d60351fSIlya Dryomov 
7353d14c5d2SYehuda Sadeh 	return 0;
7363d14c5d2SYehuda Sadeh }
7373d14c5d2SYehuda Sadeh 
738ec7af972SIlya Dryomov #define OSDMAP_WRAPPER_COMPAT_VER	7
739ec7af972SIlya Dryomov #define OSDMAP_CLIENT_DATA_COMPAT_VER	1
740ec7af972SIlya Dryomov 
741ec7af972SIlya Dryomov /*
742ec7af972SIlya Dryomov  * Return 0 or error.  On success, *v is set to 0 for old (v6) osdmaps,
743ec7af972SIlya Dryomov  * to struct_v of the client_data section for new (v7 and above)
744ec7af972SIlya Dryomov  * osdmaps.
745ec7af972SIlya Dryomov  */
746ec7af972SIlya Dryomov static int get_osdmap_client_data_v(void **p, void *end,
747ec7af972SIlya Dryomov 				    const char *prefix, u8 *v)
748ec7af972SIlya Dryomov {
749ec7af972SIlya Dryomov 	u8 struct_v;
750ec7af972SIlya Dryomov 
751ec7af972SIlya Dryomov 	ceph_decode_8_safe(p, end, struct_v, e_inval);
752ec7af972SIlya Dryomov 	if (struct_v >= 7) {
753ec7af972SIlya Dryomov 		u8 struct_compat;
754ec7af972SIlya Dryomov 
755ec7af972SIlya Dryomov 		ceph_decode_8_safe(p, end, struct_compat, e_inval);
756ec7af972SIlya Dryomov 		if (struct_compat > OSDMAP_WRAPPER_COMPAT_VER) {
757b9a67899SJoe Perches 			pr_warn("got v %d cv %d > %d of %s ceph_osdmap\n",
758ec7af972SIlya Dryomov 				struct_v, struct_compat,
759ec7af972SIlya Dryomov 				OSDMAP_WRAPPER_COMPAT_VER, prefix);
760ec7af972SIlya Dryomov 			return -EINVAL;
761ec7af972SIlya Dryomov 		}
762ec7af972SIlya Dryomov 		*p += 4; /* ignore wrapper struct_len */
763ec7af972SIlya Dryomov 
764ec7af972SIlya Dryomov 		ceph_decode_8_safe(p, end, struct_v, e_inval);
765ec7af972SIlya Dryomov 		ceph_decode_8_safe(p, end, struct_compat, e_inval);
766ec7af972SIlya Dryomov 		if (struct_compat > OSDMAP_CLIENT_DATA_COMPAT_VER) {
767b9a67899SJoe Perches 			pr_warn("got v %d cv %d > %d of %s ceph_osdmap client data\n",
768ec7af972SIlya Dryomov 				struct_v, struct_compat,
769ec7af972SIlya Dryomov 				OSDMAP_CLIENT_DATA_COMPAT_VER, prefix);
770ec7af972SIlya Dryomov 			return -EINVAL;
771ec7af972SIlya Dryomov 		}
772ec7af972SIlya Dryomov 		*p += 4; /* ignore client data struct_len */
773ec7af972SIlya Dryomov 	} else {
774ec7af972SIlya Dryomov 		u16 version;
775ec7af972SIlya Dryomov 
776ec7af972SIlya Dryomov 		*p -= 1;
777ec7af972SIlya Dryomov 		ceph_decode_16_safe(p, end, version, e_inval);
778ec7af972SIlya Dryomov 		if (version < 6) {
779b9a67899SJoe Perches 			pr_warn("got v %d < 6 of %s ceph_osdmap\n",
780b9a67899SJoe Perches 				version, prefix);
781ec7af972SIlya Dryomov 			return -EINVAL;
782ec7af972SIlya Dryomov 		}
783ec7af972SIlya Dryomov 
784ec7af972SIlya Dryomov 		/* old osdmap enconding */
785ec7af972SIlya Dryomov 		struct_v = 0;
786ec7af972SIlya Dryomov 	}
787ec7af972SIlya Dryomov 
788ec7af972SIlya Dryomov 	*v = struct_v;
789ec7af972SIlya Dryomov 	return 0;
790ec7af972SIlya Dryomov 
791ec7af972SIlya Dryomov e_inval:
792ec7af972SIlya Dryomov 	return -EINVAL;
793ec7af972SIlya Dryomov }
794ec7af972SIlya Dryomov 
795433fbdd3SIlya Dryomov static int __decode_pools(void **p, void *end, struct ceph_osdmap *map,
796433fbdd3SIlya Dryomov 			  bool incremental)
797433fbdd3SIlya Dryomov {
798433fbdd3SIlya Dryomov 	u32 n;
799433fbdd3SIlya Dryomov 
800433fbdd3SIlya Dryomov 	ceph_decode_32_safe(p, end, n, e_inval);
801433fbdd3SIlya Dryomov 	while (n--) {
802433fbdd3SIlya Dryomov 		struct ceph_pg_pool_info *pi;
803433fbdd3SIlya Dryomov 		u64 pool;
804433fbdd3SIlya Dryomov 		int ret;
805433fbdd3SIlya Dryomov 
806433fbdd3SIlya Dryomov 		ceph_decode_64_safe(p, end, pool, e_inval);
807433fbdd3SIlya Dryomov 
808433fbdd3SIlya Dryomov 		pi = __lookup_pg_pool(&map->pg_pools, pool);
809433fbdd3SIlya Dryomov 		if (!incremental || !pi) {
810433fbdd3SIlya Dryomov 			pi = kzalloc(sizeof(*pi), GFP_NOFS);
811433fbdd3SIlya Dryomov 			if (!pi)
812433fbdd3SIlya Dryomov 				return -ENOMEM;
813433fbdd3SIlya Dryomov 
814433fbdd3SIlya Dryomov 			pi->id = pool;
815433fbdd3SIlya Dryomov 
816433fbdd3SIlya Dryomov 			ret = __insert_pg_pool(&map->pg_pools, pi);
817433fbdd3SIlya Dryomov 			if (ret) {
818433fbdd3SIlya Dryomov 				kfree(pi);
819433fbdd3SIlya Dryomov 				return ret;
820433fbdd3SIlya Dryomov 			}
821433fbdd3SIlya Dryomov 		}
822433fbdd3SIlya Dryomov 
823433fbdd3SIlya Dryomov 		ret = decode_pool(p, end, pi);
824433fbdd3SIlya Dryomov 		if (ret)
825433fbdd3SIlya Dryomov 			return ret;
826433fbdd3SIlya Dryomov 	}
827433fbdd3SIlya Dryomov 
828433fbdd3SIlya Dryomov 	return 0;
829433fbdd3SIlya Dryomov 
830433fbdd3SIlya Dryomov e_inval:
831433fbdd3SIlya Dryomov 	return -EINVAL;
832433fbdd3SIlya Dryomov }
833433fbdd3SIlya Dryomov 
834433fbdd3SIlya Dryomov static int decode_pools(void **p, void *end, struct ceph_osdmap *map)
835433fbdd3SIlya Dryomov {
836433fbdd3SIlya Dryomov 	return __decode_pools(p, end, map, false);
837433fbdd3SIlya Dryomov }
838433fbdd3SIlya Dryomov 
839433fbdd3SIlya Dryomov static int decode_new_pools(void **p, void *end, struct ceph_osdmap *map)
840433fbdd3SIlya Dryomov {
841433fbdd3SIlya Dryomov 	return __decode_pools(p, end, map, true);
842433fbdd3SIlya Dryomov }
843433fbdd3SIlya Dryomov 
84410db634eSIlya Dryomov static int __decode_pg_temp(void **p, void *end, struct ceph_osdmap *map,
84510db634eSIlya Dryomov 			    bool incremental)
84610db634eSIlya Dryomov {
84710db634eSIlya Dryomov 	u32 n;
84810db634eSIlya Dryomov 
84910db634eSIlya Dryomov 	ceph_decode_32_safe(p, end, n, e_inval);
85010db634eSIlya Dryomov 	while (n--) {
85110db634eSIlya Dryomov 		struct ceph_pg pgid;
85210db634eSIlya Dryomov 		u32 len, i;
85310db634eSIlya Dryomov 		int ret;
85410db634eSIlya Dryomov 
85510db634eSIlya Dryomov 		ret = ceph_decode_pgid(p, end, &pgid);
85610db634eSIlya Dryomov 		if (ret)
85710db634eSIlya Dryomov 			return ret;
85810db634eSIlya Dryomov 
85910db634eSIlya Dryomov 		ceph_decode_32_safe(p, end, len, e_inval);
86010db634eSIlya Dryomov 
86110db634eSIlya Dryomov 		ret = __remove_pg_mapping(&map->pg_temp, pgid);
86210db634eSIlya Dryomov 		BUG_ON(!incremental && ret != -ENOENT);
86310db634eSIlya Dryomov 
86410db634eSIlya Dryomov 		if (!incremental || len > 0) {
86510db634eSIlya Dryomov 			struct ceph_pg_mapping *pg;
86610db634eSIlya Dryomov 
86710db634eSIlya Dryomov 			ceph_decode_need(p, end, len*sizeof(u32), e_inval);
86810db634eSIlya Dryomov 
86910db634eSIlya Dryomov 			if (len > (UINT_MAX - sizeof(*pg)) / sizeof(u32))
87010db634eSIlya Dryomov 				return -EINVAL;
87110db634eSIlya Dryomov 
87210db634eSIlya Dryomov 			pg = kzalloc(sizeof(*pg) + len*sizeof(u32), GFP_NOFS);
87310db634eSIlya Dryomov 			if (!pg)
87410db634eSIlya Dryomov 				return -ENOMEM;
87510db634eSIlya Dryomov 
87610db634eSIlya Dryomov 			pg->pgid = pgid;
87735a935d7SIlya Dryomov 			pg->pg_temp.len = len;
87810db634eSIlya Dryomov 			for (i = 0; i < len; i++)
87935a935d7SIlya Dryomov 				pg->pg_temp.osds[i] = ceph_decode_32(p);
88010db634eSIlya Dryomov 
88110db634eSIlya Dryomov 			ret = __insert_pg_mapping(pg, &map->pg_temp);
88210db634eSIlya Dryomov 			if (ret) {
88310db634eSIlya Dryomov 				kfree(pg);
88410db634eSIlya Dryomov 				return ret;
88510db634eSIlya Dryomov 			}
88610db634eSIlya Dryomov 		}
88710db634eSIlya Dryomov 	}
88810db634eSIlya Dryomov 
88910db634eSIlya Dryomov 	return 0;
89010db634eSIlya Dryomov 
89110db634eSIlya Dryomov e_inval:
89210db634eSIlya Dryomov 	return -EINVAL;
89310db634eSIlya Dryomov }
89410db634eSIlya Dryomov 
89510db634eSIlya Dryomov static int decode_pg_temp(void **p, void *end, struct ceph_osdmap *map)
89610db634eSIlya Dryomov {
89710db634eSIlya Dryomov 	return __decode_pg_temp(p, end, map, false);
89810db634eSIlya Dryomov }
89910db634eSIlya Dryomov 
90010db634eSIlya Dryomov static int decode_new_pg_temp(void **p, void *end, struct ceph_osdmap *map)
90110db634eSIlya Dryomov {
90210db634eSIlya Dryomov 	return __decode_pg_temp(p, end, map, true);
90310db634eSIlya Dryomov }
90410db634eSIlya Dryomov 
905d286de79SIlya Dryomov static int __decode_primary_temp(void **p, void *end, struct ceph_osdmap *map,
906d286de79SIlya Dryomov 				 bool incremental)
907d286de79SIlya Dryomov {
908d286de79SIlya Dryomov 	u32 n;
909d286de79SIlya Dryomov 
910d286de79SIlya Dryomov 	ceph_decode_32_safe(p, end, n, e_inval);
911d286de79SIlya Dryomov 	while (n--) {
912d286de79SIlya Dryomov 		struct ceph_pg pgid;
913d286de79SIlya Dryomov 		u32 osd;
914d286de79SIlya Dryomov 		int ret;
915d286de79SIlya Dryomov 
916d286de79SIlya Dryomov 		ret = ceph_decode_pgid(p, end, &pgid);
917d286de79SIlya Dryomov 		if (ret)
918d286de79SIlya Dryomov 			return ret;
919d286de79SIlya Dryomov 
920d286de79SIlya Dryomov 		ceph_decode_32_safe(p, end, osd, e_inval);
921d286de79SIlya Dryomov 
922d286de79SIlya Dryomov 		ret = __remove_pg_mapping(&map->primary_temp, pgid);
923d286de79SIlya Dryomov 		BUG_ON(!incremental && ret != -ENOENT);
924d286de79SIlya Dryomov 
925d286de79SIlya Dryomov 		if (!incremental || osd != (u32)-1) {
926d286de79SIlya Dryomov 			struct ceph_pg_mapping *pg;
927d286de79SIlya Dryomov 
928d286de79SIlya Dryomov 			pg = kzalloc(sizeof(*pg), GFP_NOFS);
929d286de79SIlya Dryomov 			if (!pg)
930d286de79SIlya Dryomov 				return -ENOMEM;
931d286de79SIlya Dryomov 
932d286de79SIlya Dryomov 			pg->pgid = pgid;
933d286de79SIlya Dryomov 			pg->primary_temp.osd = osd;
934d286de79SIlya Dryomov 
935d286de79SIlya Dryomov 			ret = __insert_pg_mapping(pg, &map->primary_temp);
936d286de79SIlya Dryomov 			if (ret) {
937d286de79SIlya Dryomov 				kfree(pg);
938d286de79SIlya Dryomov 				return ret;
939d286de79SIlya Dryomov 			}
940d286de79SIlya Dryomov 		}
941d286de79SIlya Dryomov 	}
942d286de79SIlya Dryomov 
943d286de79SIlya Dryomov 	return 0;
944d286de79SIlya Dryomov 
945d286de79SIlya Dryomov e_inval:
946d286de79SIlya Dryomov 	return -EINVAL;
947d286de79SIlya Dryomov }
948d286de79SIlya Dryomov 
949d286de79SIlya Dryomov static int decode_primary_temp(void **p, void *end, struct ceph_osdmap *map)
950d286de79SIlya Dryomov {
951d286de79SIlya Dryomov 	return __decode_primary_temp(p, end, map, false);
952d286de79SIlya Dryomov }
953d286de79SIlya Dryomov 
954d286de79SIlya Dryomov static int decode_new_primary_temp(void **p, void *end,
955d286de79SIlya Dryomov 				   struct ceph_osdmap *map)
956d286de79SIlya Dryomov {
957d286de79SIlya Dryomov 	return __decode_primary_temp(p, end, map, true);
958d286de79SIlya Dryomov }
959d286de79SIlya Dryomov 
9602cfa34f2SIlya Dryomov u32 ceph_get_primary_affinity(struct ceph_osdmap *map, int osd)
9612cfa34f2SIlya Dryomov {
9622cfa34f2SIlya Dryomov 	BUG_ON(osd >= map->max_osd);
9632cfa34f2SIlya Dryomov 
9642cfa34f2SIlya Dryomov 	if (!map->osd_primary_affinity)
9652cfa34f2SIlya Dryomov 		return CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
9662cfa34f2SIlya Dryomov 
9672cfa34f2SIlya Dryomov 	return map->osd_primary_affinity[osd];
9682cfa34f2SIlya Dryomov }
9692cfa34f2SIlya Dryomov 
9702cfa34f2SIlya Dryomov static int set_primary_affinity(struct ceph_osdmap *map, int osd, u32 aff)
9712cfa34f2SIlya Dryomov {
9722cfa34f2SIlya Dryomov 	BUG_ON(osd >= map->max_osd);
9732cfa34f2SIlya Dryomov 
9742cfa34f2SIlya Dryomov 	if (!map->osd_primary_affinity) {
9752cfa34f2SIlya Dryomov 		int i;
9762cfa34f2SIlya Dryomov 
9772cfa34f2SIlya Dryomov 		map->osd_primary_affinity = kmalloc(map->max_osd*sizeof(u32),
9782cfa34f2SIlya Dryomov 						    GFP_NOFS);
9792cfa34f2SIlya Dryomov 		if (!map->osd_primary_affinity)
9802cfa34f2SIlya Dryomov 			return -ENOMEM;
9812cfa34f2SIlya Dryomov 
9822cfa34f2SIlya Dryomov 		for (i = 0; i < map->max_osd; i++)
9832cfa34f2SIlya Dryomov 			map->osd_primary_affinity[i] =
9842cfa34f2SIlya Dryomov 			    CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
9852cfa34f2SIlya Dryomov 	}
9862cfa34f2SIlya Dryomov 
9872cfa34f2SIlya Dryomov 	map->osd_primary_affinity[osd] = aff;
9882cfa34f2SIlya Dryomov 
9892cfa34f2SIlya Dryomov 	return 0;
9902cfa34f2SIlya Dryomov }
9912cfa34f2SIlya Dryomov 
99263a6993fSIlya Dryomov static int decode_primary_affinity(void **p, void *end,
99363a6993fSIlya Dryomov 				   struct ceph_osdmap *map)
99463a6993fSIlya Dryomov {
99563a6993fSIlya Dryomov 	u32 len, i;
99663a6993fSIlya Dryomov 
99763a6993fSIlya Dryomov 	ceph_decode_32_safe(p, end, len, e_inval);
99863a6993fSIlya Dryomov 	if (len == 0) {
99963a6993fSIlya Dryomov 		kfree(map->osd_primary_affinity);
100063a6993fSIlya Dryomov 		map->osd_primary_affinity = NULL;
100163a6993fSIlya Dryomov 		return 0;
100263a6993fSIlya Dryomov 	}
100363a6993fSIlya Dryomov 	if (len != map->max_osd)
100463a6993fSIlya Dryomov 		goto e_inval;
100563a6993fSIlya Dryomov 
100663a6993fSIlya Dryomov 	ceph_decode_need(p, end, map->max_osd*sizeof(u32), e_inval);
100763a6993fSIlya Dryomov 
100863a6993fSIlya Dryomov 	for (i = 0; i < map->max_osd; i++) {
100963a6993fSIlya Dryomov 		int ret;
101063a6993fSIlya Dryomov 
101163a6993fSIlya Dryomov 		ret = set_primary_affinity(map, i, ceph_decode_32(p));
101263a6993fSIlya Dryomov 		if (ret)
101363a6993fSIlya Dryomov 			return ret;
101463a6993fSIlya Dryomov 	}
101563a6993fSIlya Dryomov 
101663a6993fSIlya Dryomov 	return 0;
101763a6993fSIlya Dryomov 
101863a6993fSIlya Dryomov e_inval:
101963a6993fSIlya Dryomov 	return -EINVAL;
102063a6993fSIlya Dryomov }
102163a6993fSIlya Dryomov 
102263a6993fSIlya Dryomov static int decode_new_primary_affinity(void **p, void *end,
102363a6993fSIlya Dryomov 				       struct ceph_osdmap *map)
102463a6993fSIlya Dryomov {
102563a6993fSIlya Dryomov 	u32 n;
102663a6993fSIlya Dryomov 
102763a6993fSIlya Dryomov 	ceph_decode_32_safe(p, end, n, e_inval);
102863a6993fSIlya Dryomov 	while (n--) {
102963a6993fSIlya Dryomov 		u32 osd, aff;
103063a6993fSIlya Dryomov 		int ret;
103163a6993fSIlya Dryomov 
103263a6993fSIlya Dryomov 		ceph_decode_32_safe(p, end, osd, e_inval);
103363a6993fSIlya Dryomov 		ceph_decode_32_safe(p, end, aff, e_inval);
103463a6993fSIlya Dryomov 
103563a6993fSIlya Dryomov 		ret = set_primary_affinity(map, osd, aff);
103663a6993fSIlya Dryomov 		if (ret)
103763a6993fSIlya Dryomov 			return ret;
1038f31da0f3SIlya Dryomov 
1039f31da0f3SIlya Dryomov 		pr_info("osd%d primary-affinity 0x%x\n", osd, aff);
104063a6993fSIlya Dryomov 	}
104163a6993fSIlya Dryomov 
104263a6993fSIlya Dryomov 	return 0;
104363a6993fSIlya Dryomov 
104463a6993fSIlya Dryomov e_inval:
104563a6993fSIlya Dryomov 	return -EINVAL;
104663a6993fSIlya Dryomov }
104763a6993fSIlya Dryomov 
10483d14c5d2SYehuda Sadeh /*
10493d14c5d2SYehuda Sadeh  * decode a full map.
10503d14c5d2SYehuda Sadeh  */
1051a2505d63SIlya Dryomov static int osdmap_decode(void **p, void *end, struct ceph_osdmap *map)
10523d14c5d2SYehuda Sadeh {
1053ec7af972SIlya Dryomov 	u8 struct_v;
105438a8d560SIlya Dryomov 	u32 epoch = 0;
10553d14c5d2SYehuda Sadeh 	void *start = *p;
10563977058cSIlya Dryomov 	u32 max;
10573977058cSIlya Dryomov 	u32 len, i;
1058597b52f6SIlya Dryomov 	int err;
10593d14c5d2SYehuda Sadeh 
1060a2505d63SIlya Dryomov 	dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p));
10613d14c5d2SYehuda Sadeh 
1062ec7af972SIlya Dryomov 	err = get_osdmap_client_data_v(p, end, "full", &struct_v);
1063ec7af972SIlya Dryomov 	if (err)
1064ec7af972SIlya Dryomov 		goto bad;
10653d14c5d2SYehuda Sadeh 
106653bbaba9SIlya Dryomov 	/* fsid, epoch, created, modified */
106753bbaba9SIlya Dryomov 	ceph_decode_need(p, end, sizeof(map->fsid) + sizeof(u32) +
106853bbaba9SIlya Dryomov 			 sizeof(map->created) + sizeof(map->modified), e_inval);
10693d14c5d2SYehuda Sadeh 	ceph_decode_copy(p, &map->fsid, sizeof(map->fsid));
107038a8d560SIlya Dryomov 	epoch = map->epoch = ceph_decode_32(p);
10713d14c5d2SYehuda Sadeh 	ceph_decode_copy(p, &map->created, sizeof(map->created));
10723d14c5d2SYehuda Sadeh 	ceph_decode_copy(p, &map->modified, sizeof(map->modified));
10733d14c5d2SYehuda Sadeh 
1074433fbdd3SIlya Dryomov 	/* pools */
1075433fbdd3SIlya Dryomov 	err = decode_pools(p, end, map);
1076433fbdd3SIlya Dryomov 	if (err)
10773d14c5d2SYehuda Sadeh 		goto bad;
10783d14c5d2SYehuda Sadeh 
10790f70c7eeSIlya Dryomov 	/* pool_name */
10800f70c7eeSIlya Dryomov 	err = decode_pool_names(p, end, map);
1081597b52f6SIlya Dryomov 	if (err)
10823d14c5d2SYehuda Sadeh 		goto bad;
10833d14c5d2SYehuda Sadeh 
1084597b52f6SIlya Dryomov 	ceph_decode_32_safe(p, end, map->pool_max, e_inval);
10853d14c5d2SYehuda Sadeh 
1086597b52f6SIlya Dryomov 	ceph_decode_32_safe(p, end, map->flags, e_inval);
10873d14c5d2SYehuda Sadeh 
10883977058cSIlya Dryomov 	/* max_osd */
10893977058cSIlya Dryomov 	ceph_decode_32_safe(p, end, max, e_inval);
10903d14c5d2SYehuda Sadeh 
10913d14c5d2SYehuda Sadeh 	/* (re)alloc osd arrays */
10923d14c5d2SYehuda Sadeh 	err = osdmap_set_max_osd(map, max);
1093597b52f6SIlya Dryomov 	if (err)
10943d14c5d2SYehuda Sadeh 		goto bad;
10953d14c5d2SYehuda Sadeh 
10962d88b2e0SIlya Dryomov 	/* osd_state, osd_weight, osd_addrs->client_addr */
10973d14c5d2SYehuda Sadeh 	ceph_decode_need(p, end, 3*sizeof(u32) +
10983d14c5d2SYehuda Sadeh 			 map->max_osd*(1 + sizeof(*map->osd_weight) +
1099597b52f6SIlya Dryomov 				       sizeof(*map->osd_addr)), e_inval);
1100597b52f6SIlya Dryomov 
11012d88b2e0SIlya Dryomov 	if (ceph_decode_32(p) != map->max_osd)
11022d88b2e0SIlya Dryomov 		goto e_inval;
11032d88b2e0SIlya Dryomov 
11043d14c5d2SYehuda Sadeh 	ceph_decode_copy(p, map->osd_state, map->max_osd);
11053d14c5d2SYehuda Sadeh 
11062d88b2e0SIlya Dryomov 	if (ceph_decode_32(p) != map->max_osd)
11072d88b2e0SIlya Dryomov 		goto e_inval;
11082d88b2e0SIlya Dryomov 
11093d14c5d2SYehuda Sadeh 	for (i = 0; i < map->max_osd; i++)
11103d14c5d2SYehuda Sadeh 		map->osd_weight[i] = ceph_decode_32(p);
11113d14c5d2SYehuda Sadeh 
11122d88b2e0SIlya Dryomov 	if (ceph_decode_32(p) != map->max_osd)
11132d88b2e0SIlya Dryomov 		goto e_inval;
11142d88b2e0SIlya Dryomov 
11153d14c5d2SYehuda Sadeh 	ceph_decode_copy(p, map->osd_addr, map->max_osd*sizeof(*map->osd_addr));
11163d14c5d2SYehuda Sadeh 	for (i = 0; i < map->max_osd; i++)
11173d14c5d2SYehuda Sadeh 		ceph_decode_addr(&map->osd_addr[i]);
11183d14c5d2SYehuda Sadeh 
11193d14c5d2SYehuda Sadeh 	/* pg_temp */
112010db634eSIlya Dryomov 	err = decode_pg_temp(p, end, map);
1121d6c0dd6bSSage Weil 	if (err)
1122d6c0dd6bSSage Weil 		goto bad;
11233d14c5d2SYehuda Sadeh 
1124d286de79SIlya Dryomov 	/* primary_temp */
1125d286de79SIlya Dryomov 	if (struct_v >= 1) {
1126d286de79SIlya Dryomov 		err = decode_primary_temp(p, end, map);
1127d286de79SIlya Dryomov 		if (err)
1128d286de79SIlya Dryomov 			goto bad;
1129d286de79SIlya Dryomov 	}
1130d286de79SIlya Dryomov 
113163a6993fSIlya Dryomov 	/* primary_affinity */
113263a6993fSIlya Dryomov 	if (struct_v >= 2) {
113363a6993fSIlya Dryomov 		err = decode_primary_affinity(p, end, map);
113463a6993fSIlya Dryomov 		if (err)
113563a6993fSIlya Dryomov 			goto bad;
113663a6993fSIlya Dryomov 	} else {
113763a6993fSIlya Dryomov 		/* XXX can this happen? */
113863a6993fSIlya Dryomov 		kfree(map->osd_primary_affinity);
113963a6993fSIlya Dryomov 		map->osd_primary_affinity = NULL;
114063a6993fSIlya Dryomov 	}
114163a6993fSIlya Dryomov 
11423d14c5d2SYehuda Sadeh 	/* crush */
1143597b52f6SIlya Dryomov 	ceph_decode_32_safe(p, end, len, e_inval);
11449902e682SIlya Dryomov 	map->crush = crush_decode(*p, min(*p + len, end));
11453d14c5d2SYehuda Sadeh 	if (IS_ERR(map->crush)) {
11463d14c5d2SYehuda Sadeh 		err = PTR_ERR(map->crush);
11473d14c5d2SYehuda Sadeh 		map->crush = NULL;
11483d14c5d2SYehuda Sadeh 		goto bad;
11493d14c5d2SYehuda Sadeh 	}
11509902e682SIlya Dryomov 	*p += len;
11513d14c5d2SYehuda Sadeh 
115238a8d560SIlya Dryomov 	/* ignore the rest */
11533d14c5d2SYehuda Sadeh 	*p = end;
11543d14c5d2SYehuda Sadeh 
115538a8d560SIlya Dryomov 	dout("full osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd);
1156a2505d63SIlya Dryomov 	return 0;
11573d14c5d2SYehuda Sadeh 
1158597b52f6SIlya Dryomov e_inval:
1159597b52f6SIlya Dryomov 	err = -EINVAL;
11603d14c5d2SYehuda Sadeh bad:
116138a8d560SIlya Dryomov 	pr_err("corrupt full osdmap (%d) epoch %d off %d (%p of %p-%p)\n",
116238a8d560SIlya Dryomov 	       err, epoch, (int)(*p - start), *p, start, end);
116338a8d560SIlya Dryomov 	print_hex_dump(KERN_DEBUG, "osdmap: ",
116438a8d560SIlya Dryomov 		       DUMP_PREFIX_OFFSET, 16, 1,
116538a8d560SIlya Dryomov 		       start, end - start, true);
1166a2505d63SIlya Dryomov 	return err;
1167a2505d63SIlya Dryomov }
1168a2505d63SIlya Dryomov 
1169a2505d63SIlya Dryomov /*
1170a2505d63SIlya Dryomov  * Allocate and decode a full map.
1171a2505d63SIlya Dryomov  */
1172a2505d63SIlya Dryomov struct ceph_osdmap *ceph_osdmap_decode(void **p, void *end)
1173a2505d63SIlya Dryomov {
1174a2505d63SIlya Dryomov 	struct ceph_osdmap *map;
1175a2505d63SIlya Dryomov 	int ret;
1176a2505d63SIlya Dryomov 
1177a2505d63SIlya Dryomov 	map = kzalloc(sizeof(*map), GFP_NOFS);
1178a2505d63SIlya Dryomov 	if (!map)
1179a2505d63SIlya Dryomov 		return ERR_PTR(-ENOMEM);
1180a2505d63SIlya Dryomov 
1181a2505d63SIlya Dryomov 	map->pg_temp = RB_ROOT;
11829686f94cSIlya Dryomov 	map->primary_temp = RB_ROOT;
1183a2505d63SIlya Dryomov 	mutex_init(&map->crush_scratch_mutex);
1184a2505d63SIlya Dryomov 
1185a2505d63SIlya Dryomov 	ret = osdmap_decode(p, end, map);
1186a2505d63SIlya Dryomov 	if (ret) {
11873d14c5d2SYehuda Sadeh 		ceph_osdmap_destroy(map);
1188a2505d63SIlya Dryomov 		return ERR_PTR(ret);
1189a2505d63SIlya Dryomov 	}
1190a2505d63SIlya Dryomov 
1191a2505d63SIlya Dryomov 	return map;
11923d14c5d2SYehuda Sadeh }
11933d14c5d2SYehuda Sadeh 
11943d14c5d2SYehuda Sadeh /*
11953d14c5d2SYehuda Sadeh  * decode and apply an incremental map update.
11963d14c5d2SYehuda Sadeh  */
11973d14c5d2SYehuda Sadeh struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end,
11983d14c5d2SYehuda Sadeh 					     struct ceph_osdmap *map,
11993d14c5d2SYehuda Sadeh 					     struct ceph_messenger *msgr)
12003d14c5d2SYehuda Sadeh {
12013d14c5d2SYehuda Sadeh 	struct crush_map *newcrush = NULL;
12023d14c5d2SYehuda Sadeh 	struct ceph_fsid fsid;
12033d14c5d2SYehuda Sadeh 	u32 epoch = 0;
12043d14c5d2SYehuda Sadeh 	struct ceph_timespec modified;
12054f6a7e5eSSage Weil 	s32 len;
12064f6a7e5eSSage Weil 	u64 pool;
12074f6a7e5eSSage Weil 	__s64 new_pool_max;
12084f6a7e5eSSage Weil 	__s32 new_flags, max;
12093d14c5d2SYehuda Sadeh 	void *start = *p;
121086f1742bSIlya Dryomov 	int err;
1211ec7af972SIlya Dryomov 	u8 struct_v;
12123d14c5d2SYehuda Sadeh 
121338a8d560SIlya Dryomov 	dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p));
121438a8d560SIlya Dryomov 
1215ec7af972SIlya Dryomov 	err = get_osdmap_client_data_v(p, end, "inc", &struct_v);
1216ec7af972SIlya Dryomov 	if (err)
1217ec7af972SIlya Dryomov 		goto bad;
12183d14c5d2SYehuda Sadeh 
121953bbaba9SIlya Dryomov 	/* fsid, epoch, modified, new_pool_max, new_flags */
122053bbaba9SIlya Dryomov 	ceph_decode_need(p, end, sizeof(fsid) + sizeof(u32) + sizeof(modified) +
122153bbaba9SIlya Dryomov 			 sizeof(u64) + sizeof(u32), e_inval);
12223d14c5d2SYehuda Sadeh 	ceph_decode_copy(p, &fsid, sizeof(fsid));
12233d14c5d2SYehuda Sadeh 	epoch = ceph_decode_32(p);
12243d14c5d2SYehuda Sadeh 	BUG_ON(epoch != map->epoch+1);
12253d14c5d2SYehuda Sadeh 	ceph_decode_copy(p, &modified, sizeof(modified));
12264f6a7e5eSSage Weil 	new_pool_max = ceph_decode_64(p);
12273d14c5d2SYehuda Sadeh 	new_flags = ceph_decode_32(p);
12283d14c5d2SYehuda Sadeh 
12293d14c5d2SYehuda Sadeh 	/* full map? */
123086f1742bSIlya Dryomov 	ceph_decode_32_safe(p, end, len, e_inval);
12313d14c5d2SYehuda Sadeh 	if (len > 0) {
12323d14c5d2SYehuda Sadeh 		dout("apply_incremental full map len %d, %p to %p\n",
12333d14c5d2SYehuda Sadeh 		     len, *p, end);
1234a2505d63SIlya Dryomov 		return ceph_osdmap_decode(p, min(*p+len, end));
12353d14c5d2SYehuda Sadeh 	}
12363d14c5d2SYehuda Sadeh 
12373d14c5d2SYehuda Sadeh 	/* new crush? */
123886f1742bSIlya Dryomov 	ceph_decode_32_safe(p, end, len, e_inval);
12393d14c5d2SYehuda Sadeh 	if (len > 0) {
12403d14c5d2SYehuda Sadeh 		newcrush = crush_decode(*p, min(*p+len, end));
124186f1742bSIlya Dryomov 		if (IS_ERR(newcrush)) {
124286f1742bSIlya Dryomov 			err = PTR_ERR(newcrush);
124386f1742bSIlya Dryomov 			newcrush = NULL;
124486f1742bSIlya Dryomov 			goto bad;
124586f1742bSIlya Dryomov 		}
12463d14c5d2SYehuda Sadeh 		*p += len;
12473d14c5d2SYehuda Sadeh 	}
12483d14c5d2SYehuda Sadeh 
12493d14c5d2SYehuda Sadeh 	/* new flags? */
12503d14c5d2SYehuda Sadeh 	if (new_flags >= 0)
12513d14c5d2SYehuda Sadeh 		map->flags = new_flags;
12523d14c5d2SYehuda Sadeh 	if (new_pool_max >= 0)
12533d14c5d2SYehuda Sadeh 		map->pool_max = new_pool_max;
12543d14c5d2SYehuda Sadeh 
12553d14c5d2SYehuda Sadeh 	/* new max? */
125653bbaba9SIlya Dryomov 	ceph_decode_32_safe(p, end, max, e_inval);
12573d14c5d2SYehuda Sadeh 	if (max >= 0) {
12583d14c5d2SYehuda Sadeh 		err = osdmap_set_max_osd(map, max);
125986f1742bSIlya Dryomov 		if (err)
12603d14c5d2SYehuda Sadeh 			goto bad;
12613d14c5d2SYehuda Sadeh 	}
12623d14c5d2SYehuda Sadeh 
12633d14c5d2SYehuda Sadeh 	map->epoch++;
126431456665SSage Weil 	map->modified = modified;
12653d14c5d2SYehuda Sadeh 	if (newcrush) {
12663d14c5d2SYehuda Sadeh 		if (map->crush)
12673d14c5d2SYehuda Sadeh 			crush_destroy(map->crush);
12683d14c5d2SYehuda Sadeh 		map->crush = newcrush;
12693d14c5d2SYehuda Sadeh 		newcrush = NULL;
12703d14c5d2SYehuda Sadeh 	}
12713d14c5d2SYehuda Sadeh 
1272433fbdd3SIlya Dryomov 	/* new_pools */
1273433fbdd3SIlya Dryomov 	err = decode_new_pools(p, end, map);
1274433fbdd3SIlya Dryomov 	if (err)
12753d14c5d2SYehuda Sadeh 		goto bad;
12769464d008SIlya Dryomov 
12770f70c7eeSIlya Dryomov 	/* new_pool_names */
12780f70c7eeSIlya Dryomov 	err = decode_pool_names(p, end, map);
127986f1742bSIlya Dryomov 	if (err)
12803d14c5d2SYehuda Sadeh 		goto bad;
12813d14c5d2SYehuda Sadeh 
12823d14c5d2SYehuda Sadeh 	/* old_pool */
128386f1742bSIlya Dryomov 	ceph_decode_32_safe(p, end, len, e_inval);
12843d14c5d2SYehuda Sadeh 	while (len--) {
12853d14c5d2SYehuda Sadeh 		struct ceph_pg_pool_info *pi;
12863d14c5d2SYehuda Sadeh 
128786f1742bSIlya Dryomov 		ceph_decode_64_safe(p, end, pool, e_inval);
12883d14c5d2SYehuda Sadeh 		pi = __lookup_pg_pool(&map->pg_pools, pool);
12893d14c5d2SYehuda Sadeh 		if (pi)
12903d14c5d2SYehuda Sadeh 			__remove_pg_pool(&map->pg_pools, pi);
12913d14c5d2SYehuda Sadeh 	}
12923d14c5d2SYehuda Sadeh 
12933d14c5d2SYehuda Sadeh 	/* new_up */
129486f1742bSIlya Dryomov 	ceph_decode_32_safe(p, end, len, e_inval);
12953d14c5d2SYehuda Sadeh 	while (len--) {
12963d14c5d2SYehuda Sadeh 		u32 osd;
12973d14c5d2SYehuda Sadeh 		struct ceph_entity_addr addr;
129886f1742bSIlya Dryomov 		ceph_decode_32_safe(p, end, osd, e_inval);
129986f1742bSIlya Dryomov 		ceph_decode_copy_safe(p, end, &addr, sizeof(addr), e_inval);
13003d14c5d2SYehuda Sadeh 		ceph_decode_addr(&addr);
13013d14c5d2SYehuda Sadeh 		pr_info("osd%d up\n", osd);
13023d14c5d2SYehuda Sadeh 		BUG_ON(osd >= map->max_osd);
13033d14c5d2SYehuda Sadeh 		map->osd_state[osd] |= CEPH_OSD_UP;
13043d14c5d2SYehuda Sadeh 		map->osd_addr[osd] = addr;
13053d14c5d2SYehuda Sadeh 	}
13063d14c5d2SYehuda Sadeh 
13077662d8ffSSage Weil 	/* new_state */
130886f1742bSIlya Dryomov 	ceph_decode_32_safe(p, end, len, e_inval);
13093d14c5d2SYehuda Sadeh 	while (len--) {
13103d14c5d2SYehuda Sadeh 		u32 osd;
13117662d8ffSSage Weil 		u8 xorstate;
131286f1742bSIlya Dryomov 		ceph_decode_32_safe(p, end, osd, e_inval);
13137662d8ffSSage Weil 		xorstate = **(u8 **)p;
13143d14c5d2SYehuda Sadeh 		(*p)++;  /* clean flag */
13157662d8ffSSage Weil 		if (xorstate == 0)
13167662d8ffSSage Weil 			xorstate = CEPH_OSD_UP;
13177662d8ffSSage Weil 		if (xorstate & CEPH_OSD_UP)
13183d14c5d2SYehuda Sadeh 			pr_info("osd%d down\n", osd);
13193d14c5d2SYehuda Sadeh 		if (osd < map->max_osd)
13207662d8ffSSage Weil 			map->osd_state[osd] ^= xorstate;
13213d14c5d2SYehuda Sadeh 	}
13223d14c5d2SYehuda Sadeh 
13233d14c5d2SYehuda Sadeh 	/* new_weight */
132486f1742bSIlya Dryomov 	ceph_decode_32_safe(p, end, len, e_inval);
13253d14c5d2SYehuda Sadeh 	while (len--) {
13263d14c5d2SYehuda Sadeh 		u32 osd, off;
132786f1742bSIlya Dryomov 		ceph_decode_need(p, end, sizeof(u32)*2, e_inval);
13283d14c5d2SYehuda Sadeh 		osd = ceph_decode_32(p);
13293d14c5d2SYehuda Sadeh 		off = ceph_decode_32(p);
13303d14c5d2SYehuda Sadeh 		pr_info("osd%d weight 0x%x %s\n", osd, off,
13313d14c5d2SYehuda Sadeh 		     off == CEPH_OSD_IN ? "(in)" :
13323d14c5d2SYehuda Sadeh 		     (off == CEPH_OSD_OUT ? "(out)" : ""));
13333d14c5d2SYehuda Sadeh 		if (osd < map->max_osd)
13343d14c5d2SYehuda Sadeh 			map->osd_weight[osd] = off;
13353d14c5d2SYehuda Sadeh 	}
13363d14c5d2SYehuda Sadeh 
13373d14c5d2SYehuda Sadeh 	/* new_pg_temp */
133810db634eSIlya Dryomov 	err = decode_new_pg_temp(p, end, map);
1339d6c0dd6bSSage Weil 	if (err)
1340d6c0dd6bSSage Weil 		goto bad;
13413d14c5d2SYehuda Sadeh 
1342d286de79SIlya Dryomov 	/* new_primary_temp */
1343d286de79SIlya Dryomov 	if (struct_v >= 1) {
1344d286de79SIlya Dryomov 		err = decode_new_primary_temp(p, end, map);
1345d286de79SIlya Dryomov 		if (err)
1346d286de79SIlya Dryomov 			goto bad;
1347d286de79SIlya Dryomov 	}
1348d286de79SIlya Dryomov 
134963a6993fSIlya Dryomov 	/* new_primary_affinity */
135063a6993fSIlya Dryomov 	if (struct_v >= 2) {
135163a6993fSIlya Dryomov 		err = decode_new_primary_affinity(p, end, map);
135263a6993fSIlya Dryomov 		if (err)
135363a6993fSIlya Dryomov 			goto bad;
135463a6993fSIlya Dryomov 	}
135563a6993fSIlya Dryomov 
13563d14c5d2SYehuda Sadeh 	/* ignore the rest */
13573d14c5d2SYehuda Sadeh 	*p = end;
135838a8d560SIlya Dryomov 
135938a8d560SIlya Dryomov 	dout("inc osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd);
13603d14c5d2SYehuda Sadeh 	return map;
13613d14c5d2SYehuda Sadeh 
136286f1742bSIlya Dryomov e_inval:
136386f1742bSIlya Dryomov 	err = -EINVAL;
13643d14c5d2SYehuda Sadeh bad:
136538a8d560SIlya Dryomov 	pr_err("corrupt inc osdmap (%d) epoch %d off %d (%p of %p-%p)\n",
136638a8d560SIlya Dryomov 	       err, epoch, (int)(*p - start), *p, start, end);
13673d14c5d2SYehuda Sadeh 	print_hex_dump(KERN_DEBUG, "osdmap: ",
13683d14c5d2SYehuda Sadeh 		       DUMP_PREFIX_OFFSET, 16, 1,
13693d14c5d2SYehuda Sadeh 		       start, end - start, true);
13703d14c5d2SYehuda Sadeh 	if (newcrush)
13713d14c5d2SYehuda Sadeh 		crush_destroy(newcrush);
13723d14c5d2SYehuda Sadeh 	return ERR_PTR(err);
13733d14c5d2SYehuda Sadeh }
13743d14c5d2SYehuda Sadeh 
13753d14c5d2SYehuda Sadeh 
13763d14c5d2SYehuda Sadeh 
13773d14c5d2SYehuda Sadeh 
13783d14c5d2SYehuda Sadeh /*
13793d14c5d2SYehuda Sadeh  * calculate file layout from given offset, length.
13803d14c5d2SYehuda Sadeh  * fill in correct oid, logical length, and object extent
13813d14c5d2SYehuda Sadeh  * offset, length.
13823d14c5d2SYehuda Sadeh  *
13833d14c5d2SYehuda Sadeh  * for now, we write only a single su, until we can
13843d14c5d2SYehuda Sadeh  * pass a stride back to the caller.
13853d14c5d2SYehuda Sadeh  */
1386d63b77f4SSage Weil int ceph_calc_file_object_mapping(struct ceph_file_layout *layout,
1387e8afad65SAlex Elder 				   u64 off, u64 len,
13883d14c5d2SYehuda Sadeh 				   u64 *ono,
13893d14c5d2SYehuda Sadeh 				   u64 *oxoff, u64 *oxlen)
13903d14c5d2SYehuda Sadeh {
13913d14c5d2SYehuda Sadeh 	u32 osize = le32_to_cpu(layout->fl_object_size);
13923d14c5d2SYehuda Sadeh 	u32 su = le32_to_cpu(layout->fl_stripe_unit);
13933d14c5d2SYehuda Sadeh 	u32 sc = le32_to_cpu(layout->fl_stripe_count);
13943d14c5d2SYehuda Sadeh 	u32 bl, stripeno, stripepos, objsetno;
13953d14c5d2SYehuda Sadeh 	u32 su_per_object;
13963d14c5d2SYehuda Sadeh 	u64 t, su_offset;
13973d14c5d2SYehuda Sadeh 
1398e8afad65SAlex Elder 	dout("mapping %llu~%llu  osize %u fl_su %u\n", off, len,
13993d14c5d2SYehuda Sadeh 	     osize, su);
1400d63b77f4SSage Weil 	if (su == 0 || sc == 0)
1401d63b77f4SSage Weil 		goto invalid;
14023d14c5d2SYehuda Sadeh 	su_per_object = osize / su;
1403d63b77f4SSage Weil 	if (su_per_object == 0)
1404d63b77f4SSage Weil 		goto invalid;
14053d14c5d2SYehuda Sadeh 	dout("osize %u / su %u = su_per_object %u\n", osize, su,
14063d14c5d2SYehuda Sadeh 	     su_per_object);
14073d14c5d2SYehuda Sadeh 
1408d63b77f4SSage Weil 	if ((su & ~PAGE_MASK) != 0)
1409d63b77f4SSage Weil 		goto invalid;
1410d63b77f4SSage Weil 
14113d14c5d2SYehuda Sadeh 	/* bl = *off / su; */
14123d14c5d2SYehuda Sadeh 	t = off;
14133d14c5d2SYehuda Sadeh 	do_div(t, su);
14143d14c5d2SYehuda Sadeh 	bl = t;
14153d14c5d2SYehuda Sadeh 	dout("off %llu / su %u = bl %u\n", off, su, bl);
14163d14c5d2SYehuda Sadeh 
14173d14c5d2SYehuda Sadeh 	stripeno = bl / sc;
14183d14c5d2SYehuda Sadeh 	stripepos = bl % sc;
14193d14c5d2SYehuda Sadeh 	objsetno = stripeno / su_per_object;
14203d14c5d2SYehuda Sadeh 
14213d14c5d2SYehuda Sadeh 	*ono = objsetno * sc + stripepos;
142295c96174SEric Dumazet 	dout("objset %u * sc %u = ono %u\n", objsetno, sc, (unsigned int)*ono);
14233d14c5d2SYehuda Sadeh 
14243d14c5d2SYehuda Sadeh 	/* *oxoff = *off % layout->fl_stripe_unit;  # offset in su */
14253d14c5d2SYehuda Sadeh 	t = off;
14263d14c5d2SYehuda Sadeh 	su_offset = do_div(t, su);
14273d14c5d2SYehuda Sadeh 	*oxoff = su_offset + (stripeno % su_per_object) * su;
14283d14c5d2SYehuda Sadeh 
14293d14c5d2SYehuda Sadeh 	/*
14303d14c5d2SYehuda Sadeh 	 * Calculate the length of the extent being written to the selected
1431e8afad65SAlex Elder 	 * object. This is the minimum of the full length requested (len) or
14323d14c5d2SYehuda Sadeh 	 * the remainder of the current stripe being written to.
14333d14c5d2SYehuda Sadeh 	 */
1434e8afad65SAlex Elder 	*oxlen = min_t(u64, len, su - su_offset);
14353d14c5d2SYehuda Sadeh 
14363d14c5d2SYehuda Sadeh 	dout(" obj extent %llu~%llu\n", *oxoff, *oxlen);
1437d63b77f4SSage Weil 	return 0;
1438d63b77f4SSage Weil 
1439d63b77f4SSage Weil invalid:
1440d63b77f4SSage Weil 	dout(" invalid layout\n");
1441d63b77f4SSage Weil 	*ono = 0;
1442d63b77f4SSage Weil 	*oxoff = 0;
1443d63b77f4SSage Weil 	*oxlen = 0;
1444d63b77f4SSage Weil 	return -EINVAL;
14453d14c5d2SYehuda Sadeh }
14463d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_calc_file_object_mapping);
14473d14c5d2SYehuda Sadeh 
14483d14c5d2SYehuda Sadeh /*
14497c13cb64SIlya Dryomov  * Calculate mapping of a (oloc, oid) pair to a PG.  Should only be
14507c13cb64SIlya Dryomov  * called with target's (oloc, oid), since tiering isn't taken into
14517c13cb64SIlya Dryomov  * account.
14523d14c5d2SYehuda Sadeh  */
14537c13cb64SIlya Dryomov int ceph_oloc_oid_to_pg(struct ceph_osdmap *osdmap,
14547c13cb64SIlya Dryomov 			struct ceph_object_locator *oloc,
14557c13cb64SIlya Dryomov 			struct ceph_object_id *oid,
14567c13cb64SIlya Dryomov 			struct ceph_pg *pg_out)
14573d14c5d2SYehuda Sadeh {
14587c13cb64SIlya Dryomov 	struct ceph_pg_pool_info *pi;
14593d14c5d2SYehuda Sadeh 
14607c13cb64SIlya Dryomov 	pi = __lookup_pg_pool(&osdmap->pg_pools, oloc->pool);
14617c13cb64SIlya Dryomov 	if (!pi)
14623d14c5d2SYehuda Sadeh 		return -EIO;
14633d14c5d2SYehuda Sadeh 
14647c13cb64SIlya Dryomov 	pg_out->pool = oloc->pool;
14657c13cb64SIlya Dryomov 	pg_out->seed = ceph_str_hash(pi->object_hash, oid->name,
14667c13cb64SIlya Dryomov 				     oid->name_len);
14677c13cb64SIlya Dryomov 
14687c13cb64SIlya Dryomov 	dout("%s '%.*s' pgid %llu.%x\n", __func__, oid->name_len, oid->name,
14697c13cb64SIlya Dryomov 	     pg_out->pool, pg_out->seed);
14703d14c5d2SYehuda Sadeh 	return 0;
14713d14c5d2SYehuda Sadeh }
14727c13cb64SIlya Dryomov EXPORT_SYMBOL(ceph_oloc_oid_to_pg);
14733d14c5d2SYehuda Sadeh 
14749d521470SIlya Dryomov static int do_crush(struct ceph_osdmap *map, int ruleno, int x,
1475e8ef19c4SIlya Dryomov 		    int *result, int result_max,
1476e8ef19c4SIlya Dryomov 		    const __u32 *weight, int weight_max)
1477e8ef19c4SIlya Dryomov {
14789d521470SIlya Dryomov 	int r;
1479e8ef19c4SIlya Dryomov 
14809d521470SIlya Dryomov 	BUG_ON(result_max > CEPH_PG_MAX_SIZE);
14819d521470SIlya Dryomov 
14829d521470SIlya Dryomov 	mutex_lock(&map->crush_scratch_mutex);
14839d521470SIlya Dryomov 	r = crush_do_rule(map->crush, ruleno, x, result, result_max,
14849d521470SIlya Dryomov 			  weight, weight_max, map->crush_scratch_ary);
14859d521470SIlya Dryomov 	mutex_unlock(&map->crush_scratch_mutex);
14869d521470SIlya Dryomov 
14879d521470SIlya Dryomov 	return r;
1488e8ef19c4SIlya Dryomov }
1489e8ef19c4SIlya Dryomov 
14903d14c5d2SYehuda Sadeh /*
14912bd93d4dSIlya Dryomov  * Calculate raw (crush) set for given pgid.
14922bd93d4dSIlya Dryomov  *
14932bd93d4dSIlya Dryomov  * Return raw set length, or error.
14942bd93d4dSIlya Dryomov  */
14952bd93d4dSIlya Dryomov static int pg_to_raw_osds(struct ceph_osdmap *osdmap,
14962bd93d4dSIlya Dryomov 			  struct ceph_pg_pool_info *pool,
14972bd93d4dSIlya Dryomov 			  struct ceph_pg pgid, u32 pps, int *osds)
14982bd93d4dSIlya Dryomov {
14992bd93d4dSIlya Dryomov 	int ruleno;
15002bd93d4dSIlya Dryomov 	int len;
15012bd93d4dSIlya Dryomov 
15022bd93d4dSIlya Dryomov 	/* crush */
15032bd93d4dSIlya Dryomov 	ruleno = crush_find_rule(osdmap->crush, pool->crush_ruleset,
15042bd93d4dSIlya Dryomov 				 pool->type, pool->size);
15052bd93d4dSIlya Dryomov 	if (ruleno < 0) {
15062bd93d4dSIlya Dryomov 		pr_err("no crush rule: pool %lld ruleset %d type %d size %d\n",
15072bd93d4dSIlya Dryomov 		       pgid.pool, pool->crush_ruleset, pool->type,
15082bd93d4dSIlya Dryomov 		       pool->size);
15092bd93d4dSIlya Dryomov 		return -ENOENT;
15102bd93d4dSIlya Dryomov 	}
15112bd93d4dSIlya Dryomov 
15122bd93d4dSIlya Dryomov 	len = do_crush(osdmap, ruleno, pps, osds,
15132bd93d4dSIlya Dryomov 		       min_t(int, pool->size, CEPH_PG_MAX_SIZE),
15142bd93d4dSIlya Dryomov 		       osdmap->osd_weight, osdmap->max_osd);
15152bd93d4dSIlya Dryomov 	if (len < 0) {
15162bd93d4dSIlya Dryomov 		pr_err("error %d from crush rule %d: pool %lld ruleset %d type %d size %d\n",
15172bd93d4dSIlya Dryomov 		       len, ruleno, pgid.pool, pool->crush_ruleset,
15182bd93d4dSIlya Dryomov 		       pool->type, pool->size);
15192bd93d4dSIlya Dryomov 		return len;
15202bd93d4dSIlya Dryomov 	}
15212bd93d4dSIlya Dryomov 
15222bd93d4dSIlya Dryomov 	return len;
15232bd93d4dSIlya Dryomov }
15242bd93d4dSIlya Dryomov 
15252bd93d4dSIlya Dryomov /*
15262bd93d4dSIlya Dryomov  * Given raw set, calculate up set and up primary.
15272bd93d4dSIlya Dryomov  *
15282bd93d4dSIlya Dryomov  * Return up set length.  *primary is set to up primary osd id, or -1
15292bd93d4dSIlya Dryomov  * if up set is empty.
15302bd93d4dSIlya Dryomov  */
15312bd93d4dSIlya Dryomov static int raw_to_up_osds(struct ceph_osdmap *osdmap,
15322bd93d4dSIlya Dryomov 			  struct ceph_pg_pool_info *pool,
15332bd93d4dSIlya Dryomov 			  int *osds, int len, int *primary)
15342bd93d4dSIlya Dryomov {
15352bd93d4dSIlya Dryomov 	int up_primary = -1;
15362bd93d4dSIlya Dryomov 	int i;
15372bd93d4dSIlya Dryomov 
15382bd93d4dSIlya Dryomov 	if (ceph_can_shift_osds(pool)) {
15392bd93d4dSIlya Dryomov 		int removed = 0;
15402bd93d4dSIlya Dryomov 
15412bd93d4dSIlya Dryomov 		for (i = 0; i < len; i++) {
15422bd93d4dSIlya Dryomov 			if (ceph_osd_is_down(osdmap, osds[i])) {
15432bd93d4dSIlya Dryomov 				removed++;
15442bd93d4dSIlya Dryomov 				continue;
15452bd93d4dSIlya Dryomov 			}
15462bd93d4dSIlya Dryomov 			if (removed)
15472bd93d4dSIlya Dryomov 				osds[i - removed] = osds[i];
15482bd93d4dSIlya Dryomov 		}
15492bd93d4dSIlya Dryomov 
15502bd93d4dSIlya Dryomov 		len -= removed;
15512bd93d4dSIlya Dryomov 		if (len > 0)
15522bd93d4dSIlya Dryomov 			up_primary = osds[0];
15532bd93d4dSIlya Dryomov 	} else {
15542bd93d4dSIlya Dryomov 		for (i = len - 1; i >= 0; i--) {
15552bd93d4dSIlya Dryomov 			if (ceph_osd_is_down(osdmap, osds[i]))
15562bd93d4dSIlya Dryomov 				osds[i] = CRUSH_ITEM_NONE;
15572bd93d4dSIlya Dryomov 			else
15582bd93d4dSIlya Dryomov 				up_primary = osds[i];
15592bd93d4dSIlya Dryomov 		}
15602bd93d4dSIlya Dryomov 	}
15612bd93d4dSIlya Dryomov 
15622bd93d4dSIlya Dryomov 	*primary = up_primary;
15632bd93d4dSIlya Dryomov 	return len;
15642bd93d4dSIlya Dryomov }
15652bd93d4dSIlya Dryomov 
156647ec1f3cSIlya Dryomov static void apply_primary_affinity(struct ceph_osdmap *osdmap, u32 pps,
156747ec1f3cSIlya Dryomov 				   struct ceph_pg_pool_info *pool,
156847ec1f3cSIlya Dryomov 				   int *osds, int len, int *primary)
156947ec1f3cSIlya Dryomov {
157047ec1f3cSIlya Dryomov 	int i;
157147ec1f3cSIlya Dryomov 	int pos = -1;
157247ec1f3cSIlya Dryomov 
157347ec1f3cSIlya Dryomov 	/*
157447ec1f3cSIlya Dryomov 	 * Do we have any non-default primary_affinity values for these
157547ec1f3cSIlya Dryomov 	 * osds?
157647ec1f3cSIlya Dryomov 	 */
157747ec1f3cSIlya Dryomov 	if (!osdmap->osd_primary_affinity)
157847ec1f3cSIlya Dryomov 		return;
157947ec1f3cSIlya Dryomov 
158047ec1f3cSIlya Dryomov 	for (i = 0; i < len; i++) {
158192b2e751SIlya Dryomov 		int osd = osds[i];
158292b2e751SIlya Dryomov 
158392b2e751SIlya Dryomov 		if (osd != CRUSH_ITEM_NONE &&
158492b2e751SIlya Dryomov 		    osdmap->osd_primary_affinity[osd] !=
158547ec1f3cSIlya Dryomov 					CEPH_OSD_DEFAULT_PRIMARY_AFFINITY) {
158647ec1f3cSIlya Dryomov 			break;
158747ec1f3cSIlya Dryomov 		}
158847ec1f3cSIlya Dryomov 	}
158947ec1f3cSIlya Dryomov 	if (i == len)
159047ec1f3cSIlya Dryomov 		return;
159147ec1f3cSIlya Dryomov 
159247ec1f3cSIlya Dryomov 	/*
159347ec1f3cSIlya Dryomov 	 * Pick the primary.  Feed both the seed (for the pg) and the
159447ec1f3cSIlya Dryomov 	 * osd into the hash/rng so that a proportional fraction of an
159547ec1f3cSIlya Dryomov 	 * osd's pgs get rejected as primary.
159647ec1f3cSIlya Dryomov 	 */
159747ec1f3cSIlya Dryomov 	for (i = 0; i < len; i++) {
159892b2e751SIlya Dryomov 		int osd = osds[i];
159947ec1f3cSIlya Dryomov 		u32 aff;
160047ec1f3cSIlya Dryomov 
160147ec1f3cSIlya Dryomov 		if (osd == CRUSH_ITEM_NONE)
160247ec1f3cSIlya Dryomov 			continue;
160347ec1f3cSIlya Dryomov 
160447ec1f3cSIlya Dryomov 		aff = osdmap->osd_primary_affinity[osd];
160547ec1f3cSIlya Dryomov 		if (aff < CEPH_OSD_MAX_PRIMARY_AFFINITY &&
160647ec1f3cSIlya Dryomov 		    (crush_hash32_2(CRUSH_HASH_RJENKINS1,
160747ec1f3cSIlya Dryomov 				    pps, osd) >> 16) >= aff) {
160847ec1f3cSIlya Dryomov 			/*
160947ec1f3cSIlya Dryomov 			 * We chose not to use this primary.  Note it
161047ec1f3cSIlya Dryomov 			 * anyway as a fallback in case we don't pick
161147ec1f3cSIlya Dryomov 			 * anyone else, but keep looking.
161247ec1f3cSIlya Dryomov 			 */
161347ec1f3cSIlya Dryomov 			if (pos < 0)
161447ec1f3cSIlya Dryomov 				pos = i;
161547ec1f3cSIlya Dryomov 		} else {
161647ec1f3cSIlya Dryomov 			pos = i;
161747ec1f3cSIlya Dryomov 			break;
161847ec1f3cSIlya Dryomov 		}
161947ec1f3cSIlya Dryomov 	}
162047ec1f3cSIlya Dryomov 	if (pos < 0)
162147ec1f3cSIlya Dryomov 		return;
162247ec1f3cSIlya Dryomov 
162347ec1f3cSIlya Dryomov 	*primary = osds[pos];
162447ec1f3cSIlya Dryomov 
162547ec1f3cSIlya Dryomov 	if (ceph_can_shift_osds(pool) && pos > 0) {
162647ec1f3cSIlya Dryomov 		/* move the new primary to the front */
162747ec1f3cSIlya Dryomov 		for (i = pos; i > 0; i--)
162847ec1f3cSIlya Dryomov 			osds[i] = osds[i - 1];
162947ec1f3cSIlya Dryomov 		osds[0] = *primary;
163047ec1f3cSIlya Dryomov 	}
163147ec1f3cSIlya Dryomov }
163247ec1f3cSIlya Dryomov 
16332bd93d4dSIlya Dryomov /*
16345e8d4d36SIlya Dryomov  * Given up set, apply pg_temp and primary_temp mappings.
163545966c34SIlya Dryomov  *
163645966c34SIlya Dryomov  * Return acting set length.  *primary is set to acting primary osd id,
163745966c34SIlya Dryomov  * or -1 if acting set is empty.
163845966c34SIlya Dryomov  */
163945966c34SIlya Dryomov static int apply_temps(struct ceph_osdmap *osdmap,
164045966c34SIlya Dryomov 		       struct ceph_pg_pool_info *pool, struct ceph_pg pgid,
164145966c34SIlya Dryomov 		       int *osds, int len, int *primary)
164245966c34SIlya Dryomov {
164345966c34SIlya Dryomov 	struct ceph_pg_mapping *pg;
164445966c34SIlya Dryomov 	int temp_len;
164545966c34SIlya Dryomov 	int temp_primary;
164645966c34SIlya Dryomov 	int i;
164745966c34SIlya Dryomov 
164845966c34SIlya Dryomov 	/* raw_pg -> pg */
164945966c34SIlya Dryomov 	pgid.seed = ceph_stable_mod(pgid.seed, pool->pg_num,
165045966c34SIlya Dryomov 				    pool->pg_num_mask);
165145966c34SIlya Dryomov 
165245966c34SIlya Dryomov 	/* pg_temp? */
165345966c34SIlya Dryomov 	pg = __lookup_pg_mapping(&osdmap->pg_temp, pgid);
165445966c34SIlya Dryomov 	if (pg) {
165545966c34SIlya Dryomov 		temp_len = 0;
165645966c34SIlya Dryomov 		temp_primary = -1;
165745966c34SIlya Dryomov 
165845966c34SIlya Dryomov 		for (i = 0; i < pg->pg_temp.len; i++) {
165945966c34SIlya Dryomov 			if (ceph_osd_is_down(osdmap, pg->pg_temp.osds[i])) {
166045966c34SIlya Dryomov 				if (ceph_can_shift_osds(pool))
166145966c34SIlya Dryomov 					continue;
166245966c34SIlya Dryomov 				else
166345966c34SIlya Dryomov 					osds[temp_len++] = CRUSH_ITEM_NONE;
166445966c34SIlya Dryomov 			} else {
166545966c34SIlya Dryomov 				osds[temp_len++] = pg->pg_temp.osds[i];
166645966c34SIlya Dryomov 			}
166745966c34SIlya Dryomov 		}
166845966c34SIlya Dryomov 
166945966c34SIlya Dryomov 		/* apply pg_temp's primary */
167045966c34SIlya Dryomov 		for (i = 0; i < temp_len; i++) {
167145966c34SIlya Dryomov 			if (osds[i] != CRUSH_ITEM_NONE) {
167245966c34SIlya Dryomov 				temp_primary = osds[i];
167345966c34SIlya Dryomov 				break;
167445966c34SIlya Dryomov 			}
167545966c34SIlya Dryomov 		}
167645966c34SIlya Dryomov 	} else {
167745966c34SIlya Dryomov 		temp_len = len;
167845966c34SIlya Dryomov 		temp_primary = *primary;
167945966c34SIlya Dryomov 	}
168045966c34SIlya Dryomov 
16815e8d4d36SIlya Dryomov 	/* primary_temp? */
16825e8d4d36SIlya Dryomov 	pg = __lookup_pg_mapping(&osdmap->primary_temp, pgid);
16835e8d4d36SIlya Dryomov 	if (pg)
16845e8d4d36SIlya Dryomov 		temp_primary = pg->primary_temp.osd;
16855e8d4d36SIlya Dryomov 
168645966c34SIlya Dryomov 	*primary = temp_primary;
168745966c34SIlya Dryomov 	return temp_len;
168845966c34SIlya Dryomov }
168945966c34SIlya Dryomov 
169045966c34SIlya Dryomov /*
1691ac972230SIlya Dryomov  * Calculate acting set for given pgid.
1692ac972230SIlya Dryomov  *
16938008ab10SIlya Dryomov  * Return acting set length, or error.  *primary is set to acting
16948008ab10SIlya Dryomov  * primary osd id, or -1 if acting set is empty or on error.
16953d14c5d2SYehuda Sadeh  */
16965b191d99SSage Weil int ceph_calc_pg_acting(struct ceph_osdmap *osdmap, struct ceph_pg pgid,
16978008ab10SIlya Dryomov 			int *osds, int *primary)
16983d14c5d2SYehuda Sadeh {
1699ac972230SIlya Dryomov 	struct ceph_pg_pool_info *pool;
1700ac972230SIlya Dryomov 	u32 pps;
1701ac972230SIlya Dryomov 	int len;
17023d14c5d2SYehuda Sadeh 
1703ac972230SIlya Dryomov 	pool = __lookup_pg_pool(&osdmap->pg_pools, pgid.pool);
17048008ab10SIlya Dryomov 	if (!pool) {
17058008ab10SIlya Dryomov 		*primary = -1;
17068008ab10SIlya Dryomov 		return -ENOENT;
17078008ab10SIlya Dryomov 	}
17083d14c5d2SYehuda Sadeh 
1709ac972230SIlya Dryomov 	if (pool->flags & CEPH_POOL_FLAG_HASHPSPOOL) {
1710ac972230SIlya Dryomov 		/* hash pool id and seed so that pool PGs do not overlap */
1711ac972230SIlya Dryomov 		pps = crush_hash32_2(CRUSH_HASH_RJENKINS1,
1712ac972230SIlya Dryomov 				     ceph_stable_mod(pgid.seed, pool->pgp_num,
1713ac972230SIlya Dryomov 						     pool->pgp_num_mask),
1714ac972230SIlya Dryomov 				     pgid.pool);
1715ac972230SIlya Dryomov 	} else {
1716ac972230SIlya Dryomov 		/*
1717ac972230SIlya Dryomov 		 * legacy behavior: add ps and pool together.  this is
1718ac972230SIlya Dryomov 		 * not a great approach because the PGs from each pool
1719ac972230SIlya Dryomov 		 * will overlap on top of each other: 0.5 == 1.4 ==
1720ac972230SIlya Dryomov 		 * 2.3 == ...
1721ac972230SIlya Dryomov 		 */
1722ac972230SIlya Dryomov 		pps = ceph_stable_mod(pgid.seed, pool->pgp_num,
1723ac972230SIlya Dryomov 				      pool->pgp_num_mask) +
1724ac972230SIlya Dryomov 			(unsigned)pgid.pool;
1725ac972230SIlya Dryomov 	}
1726ac972230SIlya Dryomov 
1727ac972230SIlya Dryomov 	len = pg_to_raw_osds(osdmap, pool, pgid, pps, osds);
17288008ab10SIlya Dryomov 	if (len < 0) {
17298008ab10SIlya Dryomov 		*primary = -1;
1730ac972230SIlya Dryomov 		return len;
17318008ab10SIlya Dryomov 	}
1732ac972230SIlya Dryomov 
17338008ab10SIlya Dryomov 	len = raw_to_up_osds(osdmap, pool, osds, len, primary);
1734ac972230SIlya Dryomov 
173547ec1f3cSIlya Dryomov 	apply_primary_affinity(osdmap, pps, pool, osds, len, primary);
173647ec1f3cSIlya Dryomov 
17378008ab10SIlya Dryomov 	len = apply_temps(osdmap, pool, pgid, osds, len, primary);
1738ac972230SIlya Dryomov 
1739ac972230SIlya Dryomov 	return len;
17403d14c5d2SYehuda Sadeh }
17413d14c5d2SYehuda Sadeh 
17423d14c5d2SYehuda Sadeh /*
17433d14c5d2SYehuda Sadeh  * Return primary osd for given pgid, or -1 if none.
17443d14c5d2SYehuda Sadeh  */
17455b191d99SSage Weil int ceph_calc_pg_primary(struct ceph_osdmap *osdmap, struct ceph_pg pgid)
17463d14c5d2SYehuda Sadeh {
1747c4c12285SIlya Dryomov 	int osds[CEPH_PG_MAX_SIZE];
1748c4c12285SIlya Dryomov 	int primary;
17493d14c5d2SYehuda Sadeh 
1750c4c12285SIlya Dryomov 	ceph_calc_pg_acting(osdmap, pgid, osds, &primary);
17513d14c5d2SYehuda Sadeh 
1752c4c12285SIlya Dryomov 	return primary;
17533d14c5d2SYehuda Sadeh }
17543d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_calc_pg_primary);
1755