Linux Audio

Check our new training course

Loading...
v3.5.6
   1
   2#include <linux/ceph/ceph_debug.h>
   3
   4#include <linux/module.h>
   5#include <linux/slab.h>
   6#include <asm/div64.h>
   7
   8#include <linux/ceph/libceph.h>
   9#include <linux/ceph/osdmap.h>
  10#include <linux/ceph/decode.h>
  11#include <linux/crush/hash.h>
  12#include <linux/crush/mapper.h>
  13
  14char *ceph_osdmap_state_str(char *str, int len, int state)
  15{
  16	int flag = 0;
  17
  18	if (!len)
  19		goto done;
  20
  21	*str = '\0';
  22	if (state) {
  23		if (state & CEPH_OSD_EXISTS) {
  24			snprintf(str, len, "exists");
  25			flag = 1;
  26		}
  27		if (state & CEPH_OSD_UP) {
  28			snprintf(str, len, "%s%s%s", str, (flag ? ", " : ""),
  29				 "up");
  30			flag = 1;
  31		}
  32	} else {
  33		snprintf(str, len, "doesn't exist");
  34	}
  35done:
  36	return str;
  37}
  38
  39/* maps */
  40
  41static int calc_bits_of(unsigned int t)
  42{
  43	int b = 0;
  44	while (t) {
  45		t = t >> 1;
  46		b++;
  47	}
  48	return b;
  49}
  50
  51/*
  52 * the foo_mask is the smallest value 2^n-1 that is >= foo.
  53 */
  54static void calc_pg_masks(struct ceph_pg_pool_info *pi)
  55{
  56	pi->pg_num_mask = (1 << calc_bits_of(le32_to_cpu(pi->v.pg_num)-1)) - 1;
  57	pi->pgp_num_mask =
  58		(1 << calc_bits_of(le32_to_cpu(pi->v.pgp_num)-1)) - 1;
  59	pi->lpg_num_mask =
  60		(1 << calc_bits_of(le32_to_cpu(pi->v.lpg_num)-1)) - 1;
  61	pi->lpgp_num_mask =
  62		(1 << calc_bits_of(le32_to_cpu(pi->v.lpgp_num)-1)) - 1;
  63}
  64
  65/*
  66 * decode crush map
  67 */
  68static int crush_decode_uniform_bucket(void **p, void *end,
  69				       struct crush_bucket_uniform *b)
  70{
  71	dout("crush_decode_uniform_bucket %p to %p\n", *p, end);
  72	ceph_decode_need(p, end, (1+b->h.size) * sizeof(u32), bad);
  73	b->item_weight = ceph_decode_32(p);
  74	return 0;
  75bad:
  76	return -EINVAL;
  77}
  78
  79static int crush_decode_list_bucket(void **p, void *end,
  80				    struct crush_bucket_list *b)
  81{
  82	int j;
  83	dout("crush_decode_list_bucket %p to %p\n", *p, end);
  84	b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
  85	if (b->item_weights == NULL)
  86		return -ENOMEM;
  87	b->sum_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
  88	if (b->sum_weights == NULL)
  89		return -ENOMEM;
  90	ceph_decode_need(p, end, 2 * b->h.size * sizeof(u32), bad);
  91	for (j = 0; j < b->h.size; j++) {
  92		b->item_weights[j] = ceph_decode_32(p);
  93		b->sum_weights[j] = ceph_decode_32(p);
  94	}
  95	return 0;
  96bad:
  97	return -EINVAL;
  98}
  99
 100static int crush_decode_tree_bucket(void **p, void *end,
 101				    struct crush_bucket_tree *b)
 102{
 103	int j;
 104	dout("crush_decode_tree_bucket %p to %p\n", *p, end);
 105	ceph_decode_32_safe(p, end, b->num_nodes, bad);
 106	b->node_weights = kcalloc(b->num_nodes, sizeof(u32), GFP_NOFS);
 107	if (b->node_weights == NULL)
 108		return -ENOMEM;
 109	ceph_decode_need(p, end, b->num_nodes * sizeof(u32), bad);
 110	for (j = 0; j < b->num_nodes; j++)
 111		b->node_weights[j] = ceph_decode_32(p);
 112	return 0;
 113bad:
 114	return -EINVAL;
 115}
 116
 117static int crush_decode_straw_bucket(void **p, void *end,
 118				     struct crush_bucket_straw *b)
 119{
 120	int j;
 121	dout("crush_decode_straw_bucket %p to %p\n", *p, end);
 122	b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
 123	if (b->item_weights == NULL)
 124		return -ENOMEM;
 125	b->straws = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
 126	if (b->straws == NULL)
 127		return -ENOMEM;
 128	ceph_decode_need(p, end, 2 * b->h.size * sizeof(u32), bad);
 129	for (j = 0; j < b->h.size; j++) {
 130		b->item_weights[j] = ceph_decode_32(p);
 131		b->straws[j] = ceph_decode_32(p);
 132	}
 133	return 0;
 134bad:
 135	return -EINVAL;
 136}
 137
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 138static struct crush_map *crush_decode(void *pbyval, void *end)
 139{
 140	struct crush_map *c;
 141	int err = -EINVAL;
 142	int i, j;
 143	void **p = &pbyval;
 144	void *start = pbyval;
 145	u32 magic;
 
 146
 147	dout("crush_decode %p to %p len %d\n", *p, end, (int)(end - *p));
 148
 149	c = kzalloc(sizeof(*c), GFP_NOFS);
 150	if (c == NULL)
 151		return ERR_PTR(-ENOMEM);
 152
 
 
 
 
 
 
 153	ceph_decode_need(p, end, 4*sizeof(u32), bad);
 154	magic = ceph_decode_32(p);
 155	if (magic != CRUSH_MAGIC) {
 156		pr_err("crush_decode magic %x != current %x\n",
 157		       (unsigned int)magic, (unsigned int)CRUSH_MAGIC);
 158		goto bad;
 159	}
 160	c->max_buckets = ceph_decode_32(p);
 161	c->max_rules = ceph_decode_32(p);
 162	c->max_devices = ceph_decode_32(p);
 163
 164	c->buckets = kcalloc(c->max_buckets, sizeof(*c->buckets), GFP_NOFS);
 165	if (c->buckets == NULL)
 166		goto badmem;
 167	c->rules = kcalloc(c->max_rules, sizeof(*c->rules), GFP_NOFS);
 168	if (c->rules == NULL)
 169		goto badmem;
 170
 171	/* buckets */
 172	for (i = 0; i < c->max_buckets; i++) {
 173		int size = 0;
 174		u32 alg;
 175		struct crush_bucket *b;
 176
 177		ceph_decode_32_safe(p, end, alg, bad);
 178		if (alg == 0) {
 179			c->buckets[i] = NULL;
 180			continue;
 181		}
 182		dout("crush_decode bucket %d off %x %p to %p\n",
 183		     i, (int)(*p-start), *p, end);
 184
 185		switch (alg) {
 186		case CRUSH_BUCKET_UNIFORM:
 187			size = sizeof(struct crush_bucket_uniform);
 188			break;
 189		case CRUSH_BUCKET_LIST:
 190			size = sizeof(struct crush_bucket_list);
 191			break;
 192		case CRUSH_BUCKET_TREE:
 193			size = sizeof(struct crush_bucket_tree);
 194			break;
 195		case CRUSH_BUCKET_STRAW:
 196			size = sizeof(struct crush_bucket_straw);
 197			break;
 
 
 
 198		default:
 199			err = -EINVAL;
 200			goto bad;
 201		}
 202		BUG_ON(size == 0);
 203		b = c->buckets[i] = kzalloc(size, GFP_NOFS);
 204		if (b == NULL)
 205			goto badmem;
 206
 207		ceph_decode_need(p, end, 4*sizeof(u32), bad);
 208		b->id = ceph_decode_32(p);
 209		b->type = ceph_decode_16(p);
 210		b->alg = ceph_decode_8(p);
 211		b->hash = ceph_decode_8(p);
 212		b->weight = ceph_decode_32(p);
 213		b->size = ceph_decode_32(p);
 214
 215		dout("crush_decode bucket size %d off %x %p to %p\n",
 216		     b->size, (int)(*p-start), *p, end);
 217
 218		b->items = kcalloc(b->size, sizeof(__s32), GFP_NOFS);
 219		if (b->items == NULL)
 220			goto badmem;
 221		b->perm = kcalloc(b->size, sizeof(u32), GFP_NOFS);
 222		if (b->perm == NULL)
 223			goto badmem;
 224		b->perm_n = 0;
 225
 226		ceph_decode_need(p, end, b->size*sizeof(u32), bad);
 227		for (j = 0; j < b->size; j++)
 228			b->items[j] = ceph_decode_32(p);
 229
 230		switch (b->alg) {
 231		case CRUSH_BUCKET_UNIFORM:
 232			err = crush_decode_uniform_bucket(p, end,
 233				  (struct crush_bucket_uniform *)b);
 234			if (err < 0)
 235				goto bad;
 236			break;
 237		case CRUSH_BUCKET_LIST:
 238			err = crush_decode_list_bucket(p, end,
 239			       (struct crush_bucket_list *)b);
 240			if (err < 0)
 241				goto bad;
 242			break;
 243		case CRUSH_BUCKET_TREE:
 244			err = crush_decode_tree_bucket(p, end,
 245				(struct crush_bucket_tree *)b);
 246			if (err < 0)
 247				goto bad;
 248			break;
 249		case CRUSH_BUCKET_STRAW:
 250			err = crush_decode_straw_bucket(p, end,
 251				(struct crush_bucket_straw *)b);
 252			if (err < 0)
 253				goto bad;
 254			break;
 
 
 
 
 
 
 255		}
 256	}
 257
 258	/* rules */
 259	dout("rule vec is %p\n", c->rules);
 260	for (i = 0; i < c->max_rules; i++) {
 261		u32 yes;
 262		struct crush_rule *r;
 263
 264		ceph_decode_32_safe(p, end, yes, bad);
 265		if (!yes) {
 266			dout("crush_decode NO rule %d off %x %p to %p\n",
 267			     i, (int)(*p-start), *p, end);
 268			c->rules[i] = NULL;
 269			continue;
 270		}
 271
 272		dout("crush_decode rule %d off %x %p to %p\n",
 273		     i, (int)(*p-start), *p, end);
 274
 275		/* len */
 276		ceph_decode_32_safe(p, end, yes, bad);
 277#if BITS_PER_LONG == 32
 278		err = -EINVAL;
 279		if (yes > (ULONG_MAX - sizeof(*r))
 280			  / sizeof(struct crush_rule_step))
 281			goto bad;
 282#endif
 283		r = c->rules[i] = kmalloc(sizeof(*r) +
 284					  yes*sizeof(struct crush_rule_step),
 285					  GFP_NOFS);
 286		if (r == NULL)
 287			goto badmem;
 288		dout(" rule %d is at %p\n", i, r);
 289		r->len = yes;
 290		ceph_decode_copy_safe(p, end, &r->mask, 4, bad); /* 4 u8's */
 291		ceph_decode_need(p, end, r->len*3*sizeof(u32), bad);
 292		for (j = 0; j < r->len; j++) {
 293			r->steps[j].op = ceph_decode_32(p);
 294			r->steps[j].arg1 = ceph_decode_32(p);
 295			r->steps[j].arg2 = ceph_decode_32(p);
 296		}
 297	}
 298
 299	/* ignore trailing name maps. */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 300
 
 301	dout("crush_decode success\n");
 302	return c;
 303
 304badmem:
 305	err = -ENOMEM;
 306bad:
 307	dout("crush_decode fail %d\n", err);
 308	crush_destroy(c);
 309	return ERR_PTR(err);
 310}
 311
 312/*
 313 * rbtree of pg_mapping for handling pg_temp (explicit mapping of pgid
 314 * to a set of osds)
 315 */
 316static int pgid_cmp(struct ceph_pg l, struct ceph_pg r)
 317{
 318	u64 a = *(u64 *)&l;
 319	u64 b = *(u64 *)&r;
 320
 321	if (a < b)
 322		return -1;
 323	if (a > b)
 
 
 
 
 324		return 1;
 325	return 0;
 326}
 327
 328static int __insert_pg_mapping(struct ceph_pg_mapping *new,
 329			       struct rb_root *root)
 330{
 331	struct rb_node **p = &root->rb_node;
 332	struct rb_node *parent = NULL;
 333	struct ceph_pg_mapping *pg = NULL;
 334	int c;
 335
 336	dout("__insert_pg_mapping %llx %p\n", *(u64 *)&new->pgid, new);
 337	while (*p) {
 338		parent = *p;
 339		pg = rb_entry(parent, struct ceph_pg_mapping, node);
 340		c = pgid_cmp(new->pgid, pg->pgid);
 341		if (c < 0)
 342			p = &(*p)->rb_left;
 343		else if (c > 0)
 344			p = &(*p)->rb_right;
 345		else
 346			return -EEXIST;
 347	}
 348
 349	rb_link_node(&new->node, parent, p);
 350	rb_insert_color(&new->node, root);
 351	return 0;
 352}
 353
 354static struct ceph_pg_mapping *__lookup_pg_mapping(struct rb_root *root,
 355						   struct ceph_pg pgid)
 356{
 357	struct rb_node *n = root->rb_node;
 358	struct ceph_pg_mapping *pg;
 359	int c;
 360
 361	while (n) {
 362		pg = rb_entry(n, struct ceph_pg_mapping, node);
 363		c = pgid_cmp(pgid, pg->pgid);
 364		if (c < 0) {
 365			n = n->rb_left;
 366		} else if (c > 0) {
 367			n = n->rb_right;
 368		} else {
 369			dout("__lookup_pg_mapping %llx got %p\n",
 370			     *(u64 *)&pgid, pg);
 371			return pg;
 372		}
 373	}
 374	return NULL;
 375}
 376
 377static int __remove_pg_mapping(struct rb_root *root, struct ceph_pg pgid)
 378{
 379	struct ceph_pg_mapping *pg = __lookup_pg_mapping(root, pgid);
 380
 381	if (pg) {
 382		dout("__remove_pg_mapping %llx %p\n", *(u64 *)&pgid, pg);
 
 383		rb_erase(&pg->node, root);
 384		kfree(pg);
 385		return 0;
 386	}
 387	dout("__remove_pg_mapping %llx dne\n", *(u64 *)&pgid);
 388	return -ENOENT;
 389}
 390
 391/*
 392 * rbtree of pg pool info
 393 */
 394static int __insert_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *new)
 395{
 396	struct rb_node **p = &root->rb_node;
 397	struct rb_node *parent = NULL;
 398	struct ceph_pg_pool_info *pi = NULL;
 399
 400	while (*p) {
 401		parent = *p;
 402		pi = rb_entry(parent, struct ceph_pg_pool_info, node);
 403		if (new->id < pi->id)
 404			p = &(*p)->rb_left;
 405		else if (new->id > pi->id)
 406			p = &(*p)->rb_right;
 407		else
 408			return -EEXIST;
 409	}
 410
 411	rb_link_node(&new->node, parent, p);
 412	rb_insert_color(&new->node, root);
 413	return 0;
 414}
 415
 416static struct ceph_pg_pool_info *__lookup_pg_pool(struct rb_root *root, int id)
 417{
 418	struct ceph_pg_pool_info *pi;
 419	struct rb_node *n = root->rb_node;
 420
 421	while (n) {
 422		pi = rb_entry(n, struct ceph_pg_pool_info, node);
 423		if (id < pi->id)
 424			n = n->rb_left;
 425		else if (id > pi->id)
 426			n = n->rb_right;
 427		else
 428			return pi;
 429	}
 430	return NULL;
 431}
 432
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 433int ceph_pg_poolid_by_name(struct ceph_osdmap *map, const char *name)
 434{
 435	struct rb_node *rbp;
 436
 437	for (rbp = rb_first(&map->pg_pools); rbp; rbp = rb_next(rbp)) {
 438		struct ceph_pg_pool_info *pi =
 439			rb_entry(rbp, struct ceph_pg_pool_info, node);
 440		if (pi->name && strcmp(pi->name, name) == 0)
 441			return pi->id;
 442	}
 443	return -ENOENT;
 444}
 445EXPORT_SYMBOL(ceph_pg_poolid_by_name);
 446
 447static void __remove_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *pi)
 448{
 449	rb_erase(&pi->node, root);
 450	kfree(pi->name);
 451	kfree(pi);
 452}
 453
 454static int __decode_pool(void **p, void *end, struct ceph_pg_pool_info *pi)
 455{
 456	unsigned int n, m;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 457
 458	ceph_decode_copy(p, &pi->v, sizeof(pi->v));
 459	calc_pg_masks(pi);
 
 
 
 
 
 
 
 
 
 460
 461	/* num_snaps * snap_info_t */
 462	n = le32_to_cpu(pi->v.num_snaps);
 463	while (n--) {
 464		ceph_decode_need(p, end, sizeof(u64) + 1 + sizeof(u64) +
 465				 sizeof(struct ceph_timespec), bad);
 466		*p += sizeof(u64) +       /* key */
 467			1 + sizeof(u64) + /* u8, snapid */
 468			sizeof(struct ceph_timespec);
 469		m = ceph_decode_32(p);    /* snap name */
 470		*p += m;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 471	}
 472
 473	*p += le32_to_cpu(pi->v.num_removed_snap_intervals) * sizeof(u64) * 2;
 
 
 
 474	return 0;
 475
 476bad:
 477	return -EINVAL;
 478}
 479
 480static int __decode_pool_names(void **p, void *end, struct ceph_osdmap *map)
 481{
 482	struct ceph_pg_pool_info *pi;
 483	u32 num, len, pool;
 
 484
 485	ceph_decode_32_safe(p, end, num, bad);
 486	dout(" %d pool names\n", num);
 487	while (num--) {
 488		ceph_decode_32_safe(p, end, pool, bad);
 489		ceph_decode_32_safe(p, end, len, bad);
 490		dout("  pool %d len %d\n", pool, len);
 
 491		pi = __lookup_pg_pool(&map->pg_pools, pool);
 492		if (pi) {
 
 
 
 
 493			kfree(pi->name);
 494			pi->name = kmalloc(len + 1, GFP_NOFS);
 495			if (pi->name) {
 496				memcpy(pi->name, *p, len);
 497				pi->name[len] = '\0';
 498				dout("  name is %s\n", pi->name);
 499			}
 500		}
 501		*p += len;
 502	}
 503	return 0;
 504
 505bad:
 506	return -EINVAL;
 507}
 508
 509/*
 510 * osd map
 511 */
 512void ceph_osdmap_destroy(struct ceph_osdmap *map)
 513{
 514	dout("osdmap_destroy %p\n", map);
 515	if (map->crush)
 516		crush_destroy(map->crush);
 517	while (!RB_EMPTY_ROOT(&map->pg_temp)) {
 518		struct ceph_pg_mapping *pg =
 519			rb_entry(rb_first(&map->pg_temp),
 520				 struct ceph_pg_mapping, node);
 521		rb_erase(&pg->node, &map->pg_temp);
 522		kfree(pg);
 523	}
 
 
 
 
 
 
 
 524	while (!RB_EMPTY_ROOT(&map->pg_pools)) {
 525		struct ceph_pg_pool_info *pi =
 526			rb_entry(rb_first(&map->pg_pools),
 527				 struct ceph_pg_pool_info, node);
 528		__remove_pg_pool(&map->pg_pools, pi);
 529	}
 530	kfree(map->osd_state);
 531	kfree(map->osd_weight);
 532	kfree(map->osd_addr);
 
 533	kfree(map);
 534}
 535
 536/*
 537 * adjust max osd value.  reallocate arrays.
 
 
 538 */
 539static int osdmap_set_max_osd(struct ceph_osdmap *map, int max)
 540{
 541	u8 *state;
 542	struct ceph_entity_addr *addr;
 543	u32 *weight;
 
 
 544
 545	state = kcalloc(max, sizeof(*state), GFP_NOFS);
 546	addr = kcalloc(max, sizeof(*addr), GFP_NOFS);
 547	weight = kcalloc(max, sizeof(*weight), GFP_NOFS);
 548	if (state == NULL || addr == NULL || weight == NULL) {
 549		kfree(state);
 550		kfree(addr);
 551		kfree(weight);
 552		return -ENOMEM;
 553	}
 554
 555	/* copy old? */
 556	if (map->osd_state) {
 557		memcpy(state, map->osd_state, map->max_osd*sizeof(*state));
 558		memcpy(addr, map->osd_addr, map->max_osd*sizeof(*addr));
 559		memcpy(weight, map->osd_weight, map->max_osd*sizeof(*weight));
 560		kfree(map->osd_state);
 561		kfree(map->osd_addr);
 562		kfree(map->osd_weight);
 563	}
 564
 565	map->osd_state = state;
 
 
 
 
 566	map->osd_weight = weight;
 
 
 
 
 567	map->osd_addr = addr;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 568	map->max_osd = max;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 569	return 0;
 
 
 
 570}
 571
 572/*
 573 * decode a full map.
 574 */
 575struct ceph_osdmap *osdmap_decode(void **p, void *end)
 576{
 577	struct ceph_osdmap *map;
 578	u16 version;
 579	u32 len, max, i;
 580	u8 ev;
 581	int err = -EINVAL;
 582	void *start = *p;
 583	struct ceph_pg_pool_info *pi;
 584
 585	dout("osdmap_decode %p to %p len %d\n", *p, end, (int)(end - *p));
 586
 587	map = kzalloc(sizeof(*map), GFP_NOFS);
 588	if (map == NULL)
 589		return ERR_PTR(-ENOMEM);
 590	map->pg_temp = RB_ROOT;
 591
 592	ceph_decode_16_safe(p, end, version, bad);
 593	if (version > CEPH_OSDMAP_VERSION) {
 594		pr_warning("got unknown v %d > %d of osdmap\n", version,
 595			   CEPH_OSDMAP_VERSION);
 596		goto bad;
 597	}
 598
 599	ceph_decode_need(p, end, 2*sizeof(u64)+6*sizeof(u32), bad);
 
 
 600	ceph_decode_copy(p, &map->fsid, sizeof(map->fsid));
 601	map->epoch = ceph_decode_32(p);
 602	ceph_decode_copy(p, &map->created, sizeof(map->created));
 603	ceph_decode_copy(p, &map->modified, sizeof(map->modified));
 604
 605	ceph_decode_32_safe(p, end, max, bad);
 606	while (max--) {
 607		ceph_decode_need(p, end, 4 + 1 + sizeof(pi->v), bad);
 608		pi = kzalloc(sizeof(*pi), GFP_NOFS);
 609		if (!pi)
 610			goto bad;
 611		pi->id = ceph_decode_32(p);
 612		ev = ceph_decode_8(p); /* encoding version */
 613		if (ev > CEPH_PG_POOL_VERSION) {
 614			pr_warning("got unknown v %d > %d of ceph_pg_pool\n",
 615				   ev, CEPH_PG_POOL_VERSION);
 616			kfree(pi);
 617			goto bad;
 618		}
 619		err = __decode_pool(p, end, pi);
 620		if (err < 0) {
 621			kfree(pi);
 622			goto bad;
 623		}
 624		__insert_pg_pool(&map->pg_pools, pi);
 625	}
 626
 627	if (version >= 5 && __decode_pool_names(p, end, map) < 0)
 
 
 628		goto bad;
 629
 630	ceph_decode_32_safe(p, end, map->pool_max, bad);
 631
 632	ceph_decode_32_safe(p, end, map->flags, bad);
 633
 634	max = ceph_decode_32(p);
 
 635
 636	/* (re)alloc osd arrays */
 637	err = osdmap_set_max_osd(map, max);
 638	if (err < 0)
 639		goto bad;
 640	dout("osdmap_decode max_osd = %d\n", map->max_osd);
 641
 642	/* osds */
 643	err = -EINVAL;
 644	ceph_decode_need(p, end, 3*sizeof(u32) +
 645			 map->max_osd*(1 + sizeof(*map->osd_weight) +
 646				       sizeof(*map->osd_addr)), bad);
 647	*p += 4; /* skip length field (should match max) */
 
 
 
 648	ceph_decode_copy(p, map->osd_state, map->max_osd);
 649
 650	*p += 4; /* skip length field (should match max) */
 
 
 651	for (i = 0; i < map->max_osd; i++)
 652		map->osd_weight[i] = ceph_decode_32(p);
 653
 654	*p += 4; /* skip length field (should match max) */
 
 
 655	ceph_decode_copy(p, map->osd_addr, map->max_osd*sizeof(*map->osd_addr));
 656	for (i = 0; i < map->max_osd; i++)
 657		ceph_decode_addr(&map->osd_addr[i]);
 658
 659	/* pg_temp */
 660	ceph_decode_32_safe(p, end, len, bad);
 661	for (i = 0; i < len; i++) {
 662		int n, j;
 663		struct ceph_pg pgid;
 664		struct ceph_pg_mapping *pg;
 665
 666		ceph_decode_need(p, end, sizeof(u32) + sizeof(u64), bad);
 667		ceph_decode_copy(p, &pgid, sizeof(pgid));
 668		n = ceph_decode_32(p);
 669		ceph_decode_need(p, end, n * sizeof(u32), bad);
 670		err = -ENOMEM;
 671		pg = kmalloc(sizeof(*pg) + n*sizeof(u32), GFP_NOFS);
 672		if (!pg)
 673			goto bad;
 674		pg->pgid = pgid;
 675		pg->len = n;
 676		for (j = 0; j < n; j++)
 677			pg->osds[j] = ceph_decode_32(p);
 678
 679		err = __insert_pg_mapping(pg, &map->pg_temp);
 
 
 680		if (err)
 681			goto bad;
 682		dout(" added pg_temp %llx len %d\n", *(u64 *)&pgid, len);
 
 
 
 683	}
 684
 685	/* crush */
 686	ceph_decode_32_safe(p, end, len, bad);
 687	dout("osdmap_decode crush len %d from off 0x%x\n", len,
 688	     (int)(*p - start));
 689	ceph_decode_need(p, end, len, bad);
 690	map->crush = crush_decode(*p, end);
 691	*p += len;
 692	if (IS_ERR(map->crush)) {
 693		err = PTR_ERR(map->crush);
 694		map->crush = NULL;
 695		goto bad;
 696	}
 
 697
 698	/* ignore the rest of the map */
 699	*p = end;
 700
 701	dout("osdmap_decode done %p %p\n", *p, end);
 702	return map;
 703
 
 
 704bad:
 705	dout("osdmap_decode fail\n");
 706	ceph_osdmap_destroy(map);
 707	return ERR_PTR(err);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 708}
 709
 710/*
 711 * decode and apply an incremental map update.
 712 */
 713struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end,
 714					     struct ceph_osdmap *map,
 715					     struct ceph_messenger *msgr)
 716{
 717	struct crush_map *newcrush = NULL;
 718	struct ceph_fsid fsid;
 719	u32 epoch = 0;
 720	struct ceph_timespec modified;
 721	u32 len, pool;
 722	__s32 new_pool_max, new_flags, max;
 
 
 723	void *start = *p;
 724	int err = -EINVAL;
 725	u16 version;
 
 
 726
 727	ceph_decode_16_safe(p, end, version, bad);
 728	if (version > CEPH_OSDMAP_INC_VERSION) {
 729		pr_warning("got unknown v %d > %d of inc osdmap\n", version,
 730			   CEPH_OSDMAP_INC_VERSION);
 731		goto bad;
 732	}
 733
 734	ceph_decode_need(p, end, sizeof(fsid)+sizeof(modified)+2*sizeof(u32),
 735			 bad);
 
 736	ceph_decode_copy(p, &fsid, sizeof(fsid));
 737	epoch = ceph_decode_32(p);
 738	BUG_ON(epoch != map->epoch+1);
 739	ceph_decode_copy(p, &modified, sizeof(modified));
 740	new_pool_max = ceph_decode_32(p);
 741	new_flags = ceph_decode_32(p);
 742
 743	/* full map? */
 744	ceph_decode_32_safe(p, end, len, bad);
 745	if (len > 0) {
 746		dout("apply_incremental full map len %d, %p to %p\n",
 747		     len, *p, end);
 748		return osdmap_decode(p, min(*p+len, end));
 749	}
 750
 751	/* new crush? */
 752	ceph_decode_32_safe(p, end, len, bad);
 753	if (len > 0) {
 754		dout("apply_incremental new crush map len %d, %p to %p\n",
 755		     len, *p, end);
 756		newcrush = crush_decode(*p, min(*p+len, end));
 757		if (IS_ERR(newcrush))
 758			return ERR_CAST(newcrush);
 
 
 
 759		*p += len;
 760	}
 761
 762	/* new flags? */
 763	if (new_flags >= 0)
 764		map->flags = new_flags;
 765	if (new_pool_max >= 0)
 766		map->pool_max = new_pool_max;
 767
 768	ceph_decode_need(p, end, 5*sizeof(u32), bad);
 769
 770	/* new max? */
 771	max = ceph_decode_32(p);
 772	if (max >= 0) {
 773		err = osdmap_set_max_osd(map, max);
 774		if (err < 0)
 775			goto bad;
 776	}
 777
 778	map->epoch++;
 779	map->modified = modified;
 780	if (newcrush) {
 781		if (map->crush)
 782			crush_destroy(map->crush);
 783		map->crush = newcrush;
 784		newcrush = NULL;
 785	}
 786
 787	/* new_pool */
 788	ceph_decode_32_safe(p, end, len, bad);
 789	while (len--) {
 790		__u8 ev;
 791		struct ceph_pg_pool_info *pi;
 792
 793		ceph_decode_32_safe(p, end, pool, bad);
 794		ceph_decode_need(p, end, 1 + sizeof(pi->v), bad);
 795		ev = ceph_decode_8(p);  /* encoding version */
 796		if (ev > CEPH_PG_POOL_VERSION) {
 797			pr_warning("got unknown v %d > %d of ceph_pg_pool\n",
 798				   ev, CEPH_PG_POOL_VERSION);
 799			goto bad;
 800		}
 801		pi = __lookup_pg_pool(&map->pg_pools, pool);
 802		if (!pi) {
 803			pi = kzalloc(sizeof(*pi), GFP_NOFS);
 804			if (!pi) {
 805				err = -ENOMEM;
 806				goto bad;
 807			}
 808			pi->id = pool;
 809			__insert_pg_pool(&map->pg_pools, pi);
 810		}
 811		err = __decode_pool(p, end, pi);
 812		if (err < 0)
 813			goto bad;
 814	}
 815	if (version >= 5 && __decode_pool_names(p, end, map) < 0)
 816		goto bad;
 817
 818	/* old_pool */
 819	ceph_decode_32_safe(p, end, len, bad);
 820	while (len--) {
 821		struct ceph_pg_pool_info *pi;
 822
 823		ceph_decode_32_safe(p, end, pool, bad);
 824		pi = __lookup_pg_pool(&map->pg_pools, pool);
 825		if (pi)
 826			__remove_pg_pool(&map->pg_pools, pi);
 827	}
 828
 829	/* new_up */
 830	err = -EINVAL;
 831	ceph_decode_32_safe(p, end, len, bad);
 832	while (len--) {
 833		u32 osd;
 834		struct ceph_entity_addr addr;
 835		ceph_decode_32_safe(p, end, osd, bad);
 836		ceph_decode_copy_safe(p, end, &addr, sizeof(addr), bad);
 837		ceph_decode_addr(&addr);
 838		pr_info("osd%d up\n", osd);
 839		BUG_ON(osd >= map->max_osd);
 840		map->osd_state[osd] |= CEPH_OSD_UP;
 841		map->osd_addr[osd] = addr;
 842	}
 843
 844	/* new_state */
 845	ceph_decode_32_safe(p, end, len, bad);
 846	while (len--) {
 847		u32 osd;
 848		u8 xorstate;
 849		ceph_decode_32_safe(p, end, osd, bad);
 850		xorstate = **(u8 **)p;
 851		(*p)++;  /* clean flag */
 852		if (xorstate == 0)
 853			xorstate = CEPH_OSD_UP;
 854		if (xorstate & CEPH_OSD_UP)
 855			pr_info("osd%d down\n", osd);
 856		if (osd < map->max_osd)
 857			map->osd_state[osd] ^= xorstate;
 858	}
 859
 860	/* new_weight */
 861	ceph_decode_32_safe(p, end, len, bad);
 862	while (len--) {
 863		u32 osd, off;
 864		ceph_decode_need(p, end, sizeof(u32)*2, bad);
 865		osd = ceph_decode_32(p);
 866		off = ceph_decode_32(p);
 867		pr_info("osd%d weight 0x%x %s\n", osd, off,
 868		     off == CEPH_OSD_IN ? "(in)" :
 869		     (off == CEPH_OSD_OUT ? "(out)" : ""));
 870		if (osd < map->max_osd)
 871			map->osd_weight[osd] = off;
 872	}
 873
 874	/* new_pg_temp */
 875	ceph_decode_32_safe(p, end, len, bad);
 876	while (len--) {
 877		struct ceph_pg_mapping *pg;
 878		int j;
 879		struct ceph_pg pgid;
 880		u32 pglen;
 881		ceph_decode_need(p, end, sizeof(u64) + sizeof(u32), bad);
 882		ceph_decode_copy(p, &pgid, sizeof(pgid));
 883		pglen = ceph_decode_32(p);
 884
 885		if (pglen) {
 886			ceph_decode_need(p, end, pglen*sizeof(u32), bad);
 887
 888			/* removing existing (if any) */
 889			(void) __remove_pg_mapping(&map->pg_temp, pgid);
 890
 891			/* insert */
 892			pg = kmalloc(sizeof(*pg) + sizeof(u32)*pglen, GFP_NOFS);
 893			if (!pg) {
 894				err = -ENOMEM;
 895				goto bad;
 896			}
 897			pg->pgid = pgid;
 898			pg->len = pglen;
 899			for (j = 0; j < pglen; j++)
 900				pg->osds[j] = ceph_decode_32(p);
 901			err = __insert_pg_mapping(pg, &map->pg_temp);
 902			if (err) {
 903				kfree(pg);
 904				goto bad;
 905			}
 906			dout(" added pg_temp %llx len %d\n", *(u64 *)&pgid,
 907			     pglen);
 908		} else {
 909			/* remove */
 910			__remove_pg_mapping(&map->pg_temp, pgid);
 911		}
 912	}
 913
 914	/* ignore the rest */
 915	*p = end;
 
 
 916	return map;
 917
 
 
 918bad:
 919	pr_err("corrupt inc osdmap epoch %d off %d (%p of %p-%p)\n",
 920	       epoch, (int)(*p - start), *p, start, end);
 921	print_hex_dump(KERN_DEBUG, "osdmap: ",
 922		       DUMP_PREFIX_OFFSET, 16, 1,
 923		       start, end - start, true);
 924	if (newcrush)
 925		crush_destroy(newcrush);
 926	return ERR_PTR(err);
 927}
 928
 929
 930
 931
 932/*
 933 * calculate file layout from given offset, length.
 934 * fill in correct oid, logical length, and object extent
 935 * offset, length.
 936 *
 937 * for now, we write only a single su, until we can
 938 * pass a stride back to the caller.
 939 */
 940void ceph_calc_file_object_mapping(struct ceph_file_layout *layout,
 941				   u64 off, u64 *plen,
 942				   u64 *ono,
 943				   u64 *oxoff, u64 *oxlen)
 944{
 945	u32 osize = le32_to_cpu(layout->fl_object_size);
 946	u32 su = le32_to_cpu(layout->fl_stripe_unit);
 947	u32 sc = le32_to_cpu(layout->fl_stripe_count);
 948	u32 bl, stripeno, stripepos, objsetno;
 949	u32 su_per_object;
 950	u64 t, su_offset;
 951
 952	dout("mapping %llu~%llu  osize %u fl_su %u\n", off, *plen,
 953	     osize, su);
 
 
 954	su_per_object = osize / su;
 
 
 955	dout("osize %u / su %u = su_per_object %u\n", osize, su,
 956	     su_per_object);
 957
 958	BUG_ON((su & ~PAGE_MASK) != 0);
 
 
 959	/* bl = *off / su; */
 960	t = off;
 961	do_div(t, su);
 962	bl = t;
 963	dout("off %llu / su %u = bl %u\n", off, su, bl);
 964
 965	stripeno = bl / sc;
 966	stripepos = bl % sc;
 967	objsetno = stripeno / su_per_object;
 968
 969	*ono = objsetno * sc + stripepos;
 970	dout("objset %u * sc %u = ono %u\n", objsetno, sc, (unsigned int)*ono);
 971
 972	/* *oxoff = *off % layout->fl_stripe_unit;  # offset in su */
 973	t = off;
 974	su_offset = do_div(t, su);
 975	*oxoff = su_offset + (stripeno % su_per_object) * su;
 976
 977	/*
 978	 * Calculate the length of the extent being written to the selected
 979	 * object. This is the minimum of the full length requested (plen) or
 980	 * the remainder of the current stripe being written to.
 981	 */
 982	*oxlen = min_t(u64, *plen, su - su_offset);
 983	*plen = *oxlen;
 984
 985	dout(" obj extent %llu~%llu\n", *oxoff, *oxlen);
 
 
 
 
 
 
 
 
 986}
 987EXPORT_SYMBOL(ceph_calc_file_object_mapping);
 988
 989/*
 990 * calculate an object layout (i.e. pgid) from an oid,
 991 * file_layout, and osdmap
 
 992 */
 993int ceph_calc_object_layout(struct ceph_object_layout *ol,
 994			    const char *oid,
 995			    struct ceph_file_layout *fl,
 996			    struct ceph_osdmap *osdmap)
 997{
 998	unsigned int num, num_mask;
 999	struct ceph_pg pgid;
1000	int poolid = le32_to_cpu(fl->fl_pg_pool);
1001	struct ceph_pg_pool_info *pool;
1002	unsigned int ps;
1003
1004	BUG_ON(!osdmap);
1005
1006	pool = __lookup_pg_pool(&osdmap->pg_pools, poolid);
1007	if (!pool)
1008		return -EIO;
1009	ps = ceph_str_hash(pool->v.object_hash, oid, strlen(oid));
1010	num = le32_to_cpu(pool->v.pg_num);
1011	num_mask = pool->pg_num_mask;
1012
1013	pgid.ps = cpu_to_le16(ps);
1014	pgid.preferred = cpu_to_le16(-1);
1015	pgid.pool = fl->fl_pg_pool;
1016	dout("calc_object_layout '%s' pgid %d.%x\n", oid, poolid, ps);
1017
1018	ol->ol_pgid = pgid;
1019	ol->ol_stripe_unit = fl->fl_object_stripe_unit;
1020	return 0;
1021}
1022EXPORT_SYMBOL(ceph_calc_object_layout);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1023
1024/*
1025 * Calculate raw osd vector for the given pgid.  Return pointer to osd
1026 * array, or NULL on failure.
 
1027 */
1028static int *calc_pg_raw(struct ceph_osdmap *osdmap, struct ceph_pg pgid,
1029			int *osds, int *num)
 
1030{
1031	struct ceph_pg_mapping *pg;
1032	struct ceph_pg_pool_info *pool;
1033	int ruleno;
1034	unsigned int poolid, ps, pps, t, r;
1035
1036	poolid = le32_to_cpu(pgid.pool);
1037	ps = le16_to_cpu(pgid.ps);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1038
1039	pool = __lookup_pg_pool(&osdmap->pg_pools, poolid);
1040	if (!pool)
1041		return NULL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1042
1043	/* pg_temp? */
1044	t = ceph_stable_mod(ps, le32_to_cpu(pool->v.pg_num),
1045			    pool->pgp_num_mask);
1046	pgid.ps = cpu_to_le16(t);
1047	pg = __lookup_pg_mapping(&osdmap->pg_temp, pgid);
1048	if (pg) {
1049		*num = pg->len;
1050		return pg->osds;
1051	}
1052
1053	/* crush */
1054	ruleno = crush_find_rule(osdmap->crush, pool->v.crush_ruleset,
1055				 pool->v.type, pool->v.size);
1056	if (ruleno < 0) {
1057		pr_err("no crush rule pool %d ruleset %d type %d size %d\n",
1058		       poolid, pool->v.crush_ruleset, pool->v.type,
1059		       pool->v.size);
1060		return NULL;
1061	}
 
1062
1063	pps = ceph_stable_mod(ps,
1064			      le32_to_cpu(pool->v.pgp_num),
1065			      pool->pgp_num_mask);
1066	pps += poolid;
1067	r = crush_do_rule(osdmap->crush, ruleno, pps, osds,
1068			  min_t(int, pool->v.size, *num),
1069			  osdmap->osd_weight);
1070	if (r < 0) {
1071		pr_err("error %d from crush rule: pool %d ruleset %d type %d"
1072		       " size %d\n", r, poolid, pool->v.crush_ruleset,
1073		       pool->v.type, pool->v.size);
1074		return NULL;
1075	}
1076	*num = r;
1077	return osds;
 
 
 
 
 
 
1078}
1079
1080/*
1081 * Return acting set for given pgid.
 
 
 
1082 */
1083int ceph_calc_pg_acting(struct ceph_osdmap *osdmap, struct ceph_pg pgid,
1084			int *acting)
1085{
1086	int rawosds[CEPH_PG_MAX_SIZE], *osds;
1087	int i, o, num = CEPH_PG_MAX_SIZE;
 
1088
1089	osds = calc_pg_raw(osdmap, pgid, rawosds, &num);
1090	if (!osds)
1091		return -1;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1092
1093	/* primary is first up osd */
1094	o = 0;
1095	for (i = 0; i < num; i++)
1096		if (ceph_osd_is_up(osdmap, osds[i]))
1097			acting[o++] = osds[i];
1098	return o;
 
 
 
 
 
 
 
1099}
1100
1101/*
1102 * Return primary osd for given pgid, or -1 if none.
1103 */
1104int ceph_calc_pg_primary(struct ceph_osdmap *osdmap, struct ceph_pg pgid)
1105{
1106	int rawosds[CEPH_PG_MAX_SIZE], *osds;
1107	int i, num = CEPH_PG_MAX_SIZE;
1108
1109	osds = calc_pg_raw(osdmap, pgid, rawosds, &num);
1110	if (!osds)
1111		return -1;
1112
1113	/* primary is first up osd */
1114	for (i = 0; i < num; i++)
1115		if (ceph_osd_is_up(osdmap, osds[i]))
1116			return osds[i];
1117	return -1;
1118}
1119EXPORT_SYMBOL(ceph_calc_pg_primary);
v4.6
   1
   2#include <linux/ceph/ceph_debug.h>
   3
   4#include <linux/module.h>
   5#include <linux/slab.h>
   6#include <asm/div64.h>
   7
   8#include <linux/ceph/libceph.h>
   9#include <linux/ceph/osdmap.h>
  10#include <linux/ceph/decode.h>
  11#include <linux/crush/hash.h>
  12#include <linux/crush/mapper.h>
  13
  14char *ceph_osdmap_state_str(char *str, int len, int state)
  15{
 
 
  16	if (!len)
  17		return str;
  18
  19	if ((state & CEPH_OSD_EXISTS) && (state & CEPH_OSD_UP))
  20		snprintf(str, len, "exists, up");
  21	else if (state & CEPH_OSD_EXISTS)
  22		snprintf(str, len, "exists");
  23	else if (state & CEPH_OSD_UP)
  24		snprintf(str, len, "up");
  25	else
 
 
 
 
 
  26		snprintf(str, len, "doesn't exist");
  27
 
  28	return str;
  29}
  30
  31/* maps */
  32
  33static int calc_bits_of(unsigned int t)
  34{
  35	int b = 0;
  36	while (t) {
  37		t = t >> 1;
  38		b++;
  39	}
  40	return b;
  41}
  42
  43/*
  44 * the foo_mask is the smallest value 2^n-1 that is >= foo.
  45 */
  46static void calc_pg_masks(struct ceph_pg_pool_info *pi)
  47{
  48	pi->pg_num_mask = (1 << calc_bits_of(pi->pg_num-1)) - 1;
  49	pi->pgp_num_mask = (1 << calc_bits_of(pi->pgp_num-1)) - 1;
 
 
 
 
 
  50}
  51
  52/*
  53 * decode crush map
  54 */
  55static int crush_decode_uniform_bucket(void **p, void *end,
  56				       struct crush_bucket_uniform *b)
  57{
  58	dout("crush_decode_uniform_bucket %p to %p\n", *p, end);
  59	ceph_decode_need(p, end, (1+b->h.size) * sizeof(u32), bad);
  60	b->item_weight = ceph_decode_32(p);
  61	return 0;
  62bad:
  63	return -EINVAL;
  64}
  65
  66static int crush_decode_list_bucket(void **p, void *end,
  67				    struct crush_bucket_list *b)
  68{
  69	int j;
  70	dout("crush_decode_list_bucket %p to %p\n", *p, end);
  71	b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
  72	if (b->item_weights == NULL)
  73		return -ENOMEM;
  74	b->sum_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
  75	if (b->sum_weights == NULL)
  76		return -ENOMEM;
  77	ceph_decode_need(p, end, 2 * b->h.size * sizeof(u32), bad);
  78	for (j = 0; j < b->h.size; j++) {
  79		b->item_weights[j] = ceph_decode_32(p);
  80		b->sum_weights[j] = ceph_decode_32(p);
  81	}
  82	return 0;
  83bad:
  84	return -EINVAL;
  85}
  86
  87static int crush_decode_tree_bucket(void **p, void *end,
  88				    struct crush_bucket_tree *b)
  89{
  90	int j;
  91	dout("crush_decode_tree_bucket %p to %p\n", *p, end);
  92	ceph_decode_8_safe(p, end, b->num_nodes, bad);
  93	b->node_weights = kcalloc(b->num_nodes, sizeof(u32), GFP_NOFS);
  94	if (b->node_weights == NULL)
  95		return -ENOMEM;
  96	ceph_decode_need(p, end, b->num_nodes * sizeof(u32), bad);
  97	for (j = 0; j < b->num_nodes; j++)
  98		b->node_weights[j] = ceph_decode_32(p);
  99	return 0;
 100bad:
 101	return -EINVAL;
 102}
 103
 104static int crush_decode_straw_bucket(void **p, void *end,
 105				     struct crush_bucket_straw *b)
 106{
 107	int j;
 108	dout("crush_decode_straw_bucket %p to %p\n", *p, end);
 109	b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
 110	if (b->item_weights == NULL)
 111		return -ENOMEM;
 112	b->straws = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
 113	if (b->straws == NULL)
 114		return -ENOMEM;
 115	ceph_decode_need(p, end, 2 * b->h.size * sizeof(u32), bad);
 116	for (j = 0; j < b->h.size; j++) {
 117		b->item_weights[j] = ceph_decode_32(p);
 118		b->straws[j] = ceph_decode_32(p);
 119	}
 120	return 0;
 121bad:
 122	return -EINVAL;
 123}
 124
 125static int crush_decode_straw2_bucket(void **p, void *end,
 126				      struct crush_bucket_straw2 *b)
 127{
 128	int j;
 129	dout("crush_decode_straw2_bucket %p to %p\n", *p, end);
 130	b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
 131	if (b->item_weights == NULL)
 132		return -ENOMEM;
 133	ceph_decode_need(p, end, b->h.size * sizeof(u32), bad);
 134	for (j = 0; j < b->h.size; j++)
 135		b->item_weights[j] = ceph_decode_32(p);
 136	return 0;
 137bad:
 138	return -EINVAL;
 139}
 140
 141static int skip_name_map(void **p, void *end)
 142{
 143        int len;
 144        ceph_decode_32_safe(p, end, len ,bad);
 145        while (len--) {
 146                int strlen;
 147                *p += sizeof(u32);
 148                ceph_decode_32_safe(p, end, strlen, bad);
 149                *p += strlen;
 150}
 151        return 0;
 152bad:
 153        return -EINVAL;
 154}
 155
 156static struct crush_map *crush_decode(void *pbyval, void *end)
 157{
 158	struct crush_map *c;
 159	int err = -EINVAL;
 160	int i, j;
 161	void **p = &pbyval;
 162	void *start = pbyval;
 163	u32 magic;
 164	u32 num_name_maps;
 165
 166	dout("crush_decode %p to %p len %d\n", *p, end, (int)(end - *p));
 167
 168	c = kzalloc(sizeof(*c), GFP_NOFS);
 169	if (c == NULL)
 170		return ERR_PTR(-ENOMEM);
 171
 172        /* set tunables to default values */
 173        c->choose_local_tries = 2;
 174        c->choose_local_fallback_tries = 5;
 175        c->choose_total_tries = 19;
 176	c->chooseleaf_descend_once = 0;
 177
 178	ceph_decode_need(p, end, 4*sizeof(u32), bad);
 179	magic = ceph_decode_32(p);
 180	if (magic != CRUSH_MAGIC) {
 181		pr_err("crush_decode magic %x != current %x\n",
 182		       (unsigned int)magic, (unsigned int)CRUSH_MAGIC);
 183		goto bad;
 184	}
 185	c->max_buckets = ceph_decode_32(p);
 186	c->max_rules = ceph_decode_32(p);
 187	c->max_devices = ceph_decode_32(p);
 188
 189	c->buckets = kcalloc(c->max_buckets, sizeof(*c->buckets), GFP_NOFS);
 190	if (c->buckets == NULL)
 191		goto badmem;
 192	c->rules = kcalloc(c->max_rules, sizeof(*c->rules), GFP_NOFS);
 193	if (c->rules == NULL)
 194		goto badmem;
 195
 196	/* buckets */
 197	for (i = 0; i < c->max_buckets; i++) {
 198		int size = 0;
 199		u32 alg;
 200		struct crush_bucket *b;
 201
 202		ceph_decode_32_safe(p, end, alg, bad);
 203		if (alg == 0) {
 204			c->buckets[i] = NULL;
 205			continue;
 206		}
 207		dout("crush_decode bucket %d off %x %p to %p\n",
 208		     i, (int)(*p-start), *p, end);
 209
 210		switch (alg) {
 211		case CRUSH_BUCKET_UNIFORM:
 212			size = sizeof(struct crush_bucket_uniform);
 213			break;
 214		case CRUSH_BUCKET_LIST:
 215			size = sizeof(struct crush_bucket_list);
 216			break;
 217		case CRUSH_BUCKET_TREE:
 218			size = sizeof(struct crush_bucket_tree);
 219			break;
 220		case CRUSH_BUCKET_STRAW:
 221			size = sizeof(struct crush_bucket_straw);
 222			break;
 223		case CRUSH_BUCKET_STRAW2:
 224			size = sizeof(struct crush_bucket_straw2);
 225			break;
 226		default:
 227			err = -EINVAL;
 228			goto bad;
 229		}
 230		BUG_ON(size == 0);
 231		b = c->buckets[i] = kzalloc(size, GFP_NOFS);
 232		if (b == NULL)
 233			goto badmem;
 234
 235		ceph_decode_need(p, end, 4*sizeof(u32), bad);
 236		b->id = ceph_decode_32(p);
 237		b->type = ceph_decode_16(p);
 238		b->alg = ceph_decode_8(p);
 239		b->hash = ceph_decode_8(p);
 240		b->weight = ceph_decode_32(p);
 241		b->size = ceph_decode_32(p);
 242
 243		dout("crush_decode bucket size %d off %x %p to %p\n",
 244		     b->size, (int)(*p-start), *p, end);
 245
 246		b->items = kcalloc(b->size, sizeof(__s32), GFP_NOFS);
 247		if (b->items == NULL)
 248			goto badmem;
 249		b->perm = kcalloc(b->size, sizeof(u32), GFP_NOFS);
 250		if (b->perm == NULL)
 251			goto badmem;
 252		b->perm_n = 0;
 253
 254		ceph_decode_need(p, end, b->size*sizeof(u32), bad);
 255		for (j = 0; j < b->size; j++)
 256			b->items[j] = ceph_decode_32(p);
 257
 258		switch (b->alg) {
 259		case CRUSH_BUCKET_UNIFORM:
 260			err = crush_decode_uniform_bucket(p, end,
 261				  (struct crush_bucket_uniform *)b);
 262			if (err < 0)
 263				goto bad;
 264			break;
 265		case CRUSH_BUCKET_LIST:
 266			err = crush_decode_list_bucket(p, end,
 267			       (struct crush_bucket_list *)b);
 268			if (err < 0)
 269				goto bad;
 270			break;
 271		case CRUSH_BUCKET_TREE:
 272			err = crush_decode_tree_bucket(p, end,
 273				(struct crush_bucket_tree *)b);
 274			if (err < 0)
 275				goto bad;
 276			break;
 277		case CRUSH_BUCKET_STRAW:
 278			err = crush_decode_straw_bucket(p, end,
 279				(struct crush_bucket_straw *)b);
 280			if (err < 0)
 281				goto bad;
 282			break;
 283		case CRUSH_BUCKET_STRAW2:
 284			err = crush_decode_straw2_bucket(p, end,
 285				(struct crush_bucket_straw2 *)b);
 286			if (err < 0)
 287				goto bad;
 288			break;
 289		}
 290	}
 291
 292	/* rules */
 293	dout("rule vec is %p\n", c->rules);
 294	for (i = 0; i < c->max_rules; i++) {
 295		u32 yes;
 296		struct crush_rule *r;
 297
 298		ceph_decode_32_safe(p, end, yes, bad);
 299		if (!yes) {
 300			dout("crush_decode NO rule %d off %x %p to %p\n",
 301			     i, (int)(*p-start), *p, end);
 302			c->rules[i] = NULL;
 303			continue;
 304		}
 305
 306		dout("crush_decode rule %d off %x %p to %p\n",
 307		     i, (int)(*p-start), *p, end);
 308
 309		/* len */
 310		ceph_decode_32_safe(p, end, yes, bad);
 311#if BITS_PER_LONG == 32
 312		err = -EINVAL;
 313		if (yes > (ULONG_MAX - sizeof(*r))
 314			  / sizeof(struct crush_rule_step))
 315			goto bad;
 316#endif
 317		r = c->rules[i] = kmalloc(sizeof(*r) +
 318					  yes*sizeof(struct crush_rule_step),
 319					  GFP_NOFS);
 320		if (r == NULL)
 321			goto badmem;
 322		dout(" rule %d is at %p\n", i, r);
 323		r->len = yes;
 324		ceph_decode_copy_safe(p, end, &r->mask, 4, bad); /* 4 u8's */
 325		ceph_decode_need(p, end, r->len*3*sizeof(u32), bad);
 326		for (j = 0; j < r->len; j++) {
 327			r->steps[j].op = ceph_decode_32(p);
 328			r->steps[j].arg1 = ceph_decode_32(p);
 329			r->steps[j].arg2 = ceph_decode_32(p);
 330		}
 331	}
 332
 333	/* ignore trailing name maps. */
 334        for (num_name_maps = 0; num_name_maps < 3; num_name_maps++) {
 335                err = skip_name_map(p, end);
 336                if (err < 0)
 337                        goto done;
 338        }
 339
 340        /* tunables */
 341        ceph_decode_need(p, end, 3*sizeof(u32), done);
 342        c->choose_local_tries = ceph_decode_32(p);
 343        c->choose_local_fallback_tries =  ceph_decode_32(p);
 344        c->choose_total_tries = ceph_decode_32(p);
 345        dout("crush decode tunable choose_local_tries = %d\n",
 346             c->choose_local_tries);
 347        dout("crush decode tunable choose_local_fallback_tries = %d\n",
 348             c->choose_local_fallback_tries);
 349        dout("crush decode tunable choose_total_tries = %d\n",
 350             c->choose_total_tries);
 351
 352	ceph_decode_need(p, end, sizeof(u32), done);
 353	c->chooseleaf_descend_once = ceph_decode_32(p);
 354	dout("crush decode tunable chooseleaf_descend_once = %d\n",
 355	     c->chooseleaf_descend_once);
 356
 357	ceph_decode_need(p, end, sizeof(u8), done);
 358	c->chooseleaf_vary_r = ceph_decode_8(p);
 359	dout("crush decode tunable chooseleaf_vary_r = %d\n",
 360	     c->chooseleaf_vary_r);
 361
 362	/* skip straw_calc_version, allowed_bucket_algs */
 363	ceph_decode_need(p, end, sizeof(u8) + sizeof(u32), done);
 364	*p += sizeof(u8) + sizeof(u32);
 365
 366	ceph_decode_need(p, end, sizeof(u8), done);
 367	c->chooseleaf_stable = ceph_decode_8(p);
 368	dout("crush decode tunable chooseleaf_stable = %d\n",
 369	     c->chooseleaf_stable);
 370
 371done:
 372	dout("crush_decode success\n");
 373	return c;
 374
 375badmem:
 376	err = -ENOMEM;
 377bad:
 378	dout("crush_decode fail %d\n", err);
 379	crush_destroy(c);
 380	return ERR_PTR(err);
 381}
 382
 383/*
 384 * rbtree of pg_mapping for handling pg_temp (explicit mapping of pgid
 385 * to a set of osds) and primary_temp (explicit primary setting)
 386 */
 387static int pgid_cmp(struct ceph_pg l, struct ceph_pg r)
 388{
 389	if (l.pool < r.pool)
 
 
 
 390		return -1;
 391	if (l.pool > r.pool)
 392		return 1;
 393	if (l.seed < r.seed)
 394		return -1;
 395	if (l.seed > r.seed)
 396		return 1;
 397	return 0;
 398}
 399
 400static int __insert_pg_mapping(struct ceph_pg_mapping *new,
 401			       struct rb_root *root)
 402{
 403	struct rb_node **p = &root->rb_node;
 404	struct rb_node *parent = NULL;
 405	struct ceph_pg_mapping *pg = NULL;
 406	int c;
 407
 408	dout("__insert_pg_mapping %llx %p\n", *(u64 *)&new->pgid, new);
 409	while (*p) {
 410		parent = *p;
 411		pg = rb_entry(parent, struct ceph_pg_mapping, node);
 412		c = pgid_cmp(new->pgid, pg->pgid);
 413		if (c < 0)
 414			p = &(*p)->rb_left;
 415		else if (c > 0)
 416			p = &(*p)->rb_right;
 417		else
 418			return -EEXIST;
 419	}
 420
 421	rb_link_node(&new->node, parent, p);
 422	rb_insert_color(&new->node, root);
 423	return 0;
 424}
 425
 426static struct ceph_pg_mapping *__lookup_pg_mapping(struct rb_root *root,
 427						   struct ceph_pg pgid)
 428{
 429	struct rb_node *n = root->rb_node;
 430	struct ceph_pg_mapping *pg;
 431	int c;
 432
 433	while (n) {
 434		pg = rb_entry(n, struct ceph_pg_mapping, node);
 435		c = pgid_cmp(pgid, pg->pgid);
 436		if (c < 0) {
 437			n = n->rb_left;
 438		} else if (c > 0) {
 439			n = n->rb_right;
 440		} else {
 441			dout("__lookup_pg_mapping %lld.%x got %p\n",
 442			     pgid.pool, pgid.seed, pg);
 443			return pg;
 444		}
 445	}
 446	return NULL;
 447}
 448
 449static int __remove_pg_mapping(struct rb_root *root, struct ceph_pg pgid)
 450{
 451	struct ceph_pg_mapping *pg = __lookup_pg_mapping(root, pgid);
 452
 453	if (pg) {
 454		dout("__remove_pg_mapping %lld.%x %p\n", pgid.pool, pgid.seed,
 455		     pg);
 456		rb_erase(&pg->node, root);
 457		kfree(pg);
 458		return 0;
 459	}
 460	dout("__remove_pg_mapping %lld.%x dne\n", pgid.pool, pgid.seed);
 461	return -ENOENT;
 462}
 463
 464/*
 465 * rbtree of pg pool info
 466 */
 467static int __insert_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *new)
 468{
 469	struct rb_node **p = &root->rb_node;
 470	struct rb_node *parent = NULL;
 471	struct ceph_pg_pool_info *pi = NULL;
 472
 473	while (*p) {
 474		parent = *p;
 475		pi = rb_entry(parent, struct ceph_pg_pool_info, node);
 476		if (new->id < pi->id)
 477			p = &(*p)->rb_left;
 478		else if (new->id > pi->id)
 479			p = &(*p)->rb_right;
 480		else
 481			return -EEXIST;
 482	}
 483
 484	rb_link_node(&new->node, parent, p);
 485	rb_insert_color(&new->node, root);
 486	return 0;
 487}
 488
 489static struct ceph_pg_pool_info *__lookup_pg_pool(struct rb_root *root, u64 id)
 490{
 491	struct ceph_pg_pool_info *pi;
 492	struct rb_node *n = root->rb_node;
 493
 494	while (n) {
 495		pi = rb_entry(n, struct ceph_pg_pool_info, node);
 496		if (id < pi->id)
 497			n = n->rb_left;
 498		else if (id > pi->id)
 499			n = n->rb_right;
 500		else
 501			return pi;
 502	}
 503	return NULL;
 504}
 505
 506struct ceph_pg_pool_info *ceph_pg_pool_by_id(struct ceph_osdmap *map, u64 id)
 507{
 508	return __lookup_pg_pool(&map->pg_pools, id);
 509}
 510
 511const char *ceph_pg_pool_name_by_id(struct ceph_osdmap *map, u64 id)
 512{
 513	struct ceph_pg_pool_info *pi;
 514
 515	if (id == CEPH_NOPOOL)
 516		return NULL;
 517
 518	if (WARN_ON_ONCE(id > (u64) INT_MAX))
 519		return NULL;
 520
 521	pi = __lookup_pg_pool(&map->pg_pools, (int) id);
 522
 523	return pi ? pi->name : NULL;
 524}
 525EXPORT_SYMBOL(ceph_pg_pool_name_by_id);
 526
 527int ceph_pg_poolid_by_name(struct ceph_osdmap *map, const char *name)
 528{
 529	struct rb_node *rbp;
 530
 531	for (rbp = rb_first(&map->pg_pools); rbp; rbp = rb_next(rbp)) {
 532		struct ceph_pg_pool_info *pi =
 533			rb_entry(rbp, struct ceph_pg_pool_info, node);
 534		if (pi->name && strcmp(pi->name, name) == 0)
 535			return pi->id;
 536	}
 537	return -ENOENT;
 538}
 539EXPORT_SYMBOL(ceph_pg_poolid_by_name);
 540
 541static void __remove_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *pi)
 542{
 543	rb_erase(&pi->node, root);
 544	kfree(pi->name);
 545	kfree(pi);
 546}
 547
 548static int decode_pool(void **p, void *end, struct ceph_pg_pool_info *pi)
 549{
 550	u8 ev, cv;
 551	unsigned len, num;
 552	void *pool_end;
 553
 554	ceph_decode_need(p, end, 2 + 4, bad);
 555	ev = ceph_decode_8(p);  /* encoding version */
 556	cv = ceph_decode_8(p); /* compat version */
 557	if (ev < 5) {
 558		pr_warn("got v %d < 5 cv %d of ceph_pg_pool\n", ev, cv);
 559		return -EINVAL;
 560	}
 561	if (cv > 9) {
 562		pr_warn("got v %d cv %d > 9 of ceph_pg_pool\n", ev, cv);
 563		return -EINVAL;
 564	}
 565	len = ceph_decode_32(p);
 566	ceph_decode_need(p, end, len, bad);
 567	pool_end = *p + len;
 568
 569	pi->type = ceph_decode_8(p);
 570	pi->size = ceph_decode_8(p);
 571	pi->crush_ruleset = ceph_decode_8(p);
 572	pi->object_hash = ceph_decode_8(p);
 573
 574	pi->pg_num = ceph_decode_32(p);
 575	pi->pgp_num = ceph_decode_32(p);
 576
 577	*p += 4 + 4;  /* skip lpg* */
 578	*p += 4;      /* skip last_change */
 579	*p += 8 + 4;  /* skip snap_seq, snap_epoch */
 580
 581	/* skip snaps */
 582	num = ceph_decode_32(p);
 583	while (num--) {
 584		*p += 8;  /* snapid key */
 585		*p += 1 + 1; /* versions */
 586		len = ceph_decode_32(p);
 587		*p += len;
 588	}
 589
 590	/* skip removed_snaps */
 591	num = ceph_decode_32(p);
 592	*p += num * (8 + 8);
 593
 594	*p += 8;  /* skip auid */
 595	pi->flags = ceph_decode_64(p);
 596	*p += 4;  /* skip crash_replay_interval */
 597
 598	if (ev >= 7)
 599		*p += 1;  /* skip min_size */
 600
 601	if (ev >= 8)
 602		*p += 8 + 8;  /* skip quota_max_* */
 603
 604	if (ev >= 9) {
 605		/* skip tiers */
 606		num = ceph_decode_32(p);
 607		*p += num * 8;
 608
 609		*p += 8;  /* skip tier_of */
 610		*p += 1;  /* skip cache_mode */
 611
 612		pi->read_tier = ceph_decode_64(p);
 613		pi->write_tier = ceph_decode_64(p);
 614	} else {
 615		pi->read_tier = -1;
 616		pi->write_tier = -1;
 617	}
 618
 619	/* ignore the rest */
 620
 621	*p = pool_end;
 622	calc_pg_masks(pi);
 623	return 0;
 624
 625bad:
 626	return -EINVAL;
 627}
 628
 629static int decode_pool_names(void **p, void *end, struct ceph_osdmap *map)
 630{
 631	struct ceph_pg_pool_info *pi;
 632	u32 num, len;
 633	u64 pool;
 634
 635	ceph_decode_32_safe(p, end, num, bad);
 636	dout(" %d pool names\n", num);
 637	while (num--) {
 638		ceph_decode_64_safe(p, end, pool, bad);
 639		ceph_decode_32_safe(p, end, len, bad);
 640		dout("  pool %llu len %d\n", pool, len);
 641		ceph_decode_need(p, end, len, bad);
 642		pi = __lookup_pg_pool(&map->pg_pools, pool);
 643		if (pi) {
 644			char *name = kstrndup(*p, len, GFP_NOFS);
 645
 646			if (!name)
 647				return -ENOMEM;
 648			kfree(pi->name);
 649			pi->name = name;
 650			dout("  name is %s\n", pi->name);
 
 
 
 
 651		}
 652		*p += len;
 653	}
 654	return 0;
 655
 656bad:
 657	return -EINVAL;
 658}
 659
 660/*
 661 * osd map
 662 */
 663void ceph_osdmap_destroy(struct ceph_osdmap *map)
 664{
 665	dout("osdmap_destroy %p\n", map);
 666	if (map->crush)
 667		crush_destroy(map->crush);
 668	while (!RB_EMPTY_ROOT(&map->pg_temp)) {
 669		struct ceph_pg_mapping *pg =
 670			rb_entry(rb_first(&map->pg_temp),
 671				 struct ceph_pg_mapping, node);
 672		rb_erase(&pg->node, &map->pg_temp);
 673		kfree(pg);
 674	}
 675	while (!RB_EMPTY_ROOT(&map->primary_temp)) {
 676		struct ceph_pg_mapping *pg =
 677			rb_entry(rb_first(&map->primary_temp),
 678				 struct ceph_pg_mapping, node);
 679		rb_erase(&pg->node, &map->primary_temp);
 680		kfree(pg);
 681	}
 682	while (!RB_EMPTY_ROOT(&map->pg_pools)) {
 683		struct ceph_pg_pool_info *pi =
 684			rb_entry(rb_first(&map->pg_pools),
 685				 struct ceph_pg_pool_info, node);
 686		__remove_pg_pool(&map->pg_pools, pi);
 687	}
 688	kfree(map->osd_state);
 689	kfree(map->osd_weight);
 690	kfree(map->osd_addr);
 691	kfree(map->osd_primary_affinity);
 692	kfree(map);
 693}
 694
 695/*
 696 * Adjust max_osd value, (re)allocate arrays.
 697 *
 698 * The new elements are properly initialized.
 699 */
 700static int osdmap_set_max_osd(struct ceph_osdmap *map, int max)
 701{
 702	u8 *state;
 
 703	u32 *weight;
 704	struct ceph_entity_addr *addr;
 705	int i;
 706
 707	state = krealloc(map->osd_state, max*sizeof(*state), GFP_NOFS);
 708	if (!state)
 
 
 
 
 
 709		return -ENOMEM;
 
 
 
 
 
 
 
 
 
 
 
 
 710	map->osd_state = state;
 711
 712	weight = krealloc(map->osd_weight, max*sizeof(*weight), GFP_NOFS);
 713	if (!weight)
 714		return -ENOMEM;
 715	map->osd_weight = weight;
 716
 717	addr = krealloc(map->osd_addr, max*sizeof(*addr), GFP_NOFS);
 718	if (!addr)
 719		return -ENOMEM;
 720	map->osd_addr = addr;
 721
 722	for (i = map->max_osd; i < max; i++) {
 723		map->osd_state[i] = 0;
 724		map->osd_weight[i] = CEPH_OSD_OUT;
 725		memset(map->osd_addr + i, 0, sizeof(*map->osd_addr));
 726	}
 727
 728	if (map->osd_primary_affinity) {
 729		u32 *affinity;
 730
 731		affinity = krealloc(map->osd_primary_affinity,
 732				    max*sizeof(*affinity), GFP_NOFS);
 733		if (!affinity)
 734			return -ENOMEM;
 735		map->osd_primary_affinity = affinity;
 736
 737		for (i = map->max_osd; i < max; i++)
 738			map->osd_primary_affinity[i] =
 739			    CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
 740	}
 741
 742	map->max_osd = max;
 743
 744	return 0;
 745}
 746
 747#define OSDMAP_WRAPPER_COMPAT_VER	7
 748#define OSDMAP_CLIENT_DATA_COMPAT_VER	1
 749
 750/*
 751 * Return 0 or error.  On success, *v is set to 0 for old (v6) osdmaps,
 752 * to struct_v of the client_data section for new (v7 and above)
 753 * osdmaps.
 754 */
 755static int get_osdmap_client_data_v(void **p, void *end,
 756				    const char *prefix, u8 *v)
 757{
 758	u8 struct_v;
 759
 760	ceph_decode_8_safe(p, end, struct_v, e_inval);
 761	if (struct_v >= 7) {
 762		u8 struct_compat;
 763
 764		ceph_decode_8_safe(p, end, struct_compat, e_inval);
 765		if (struct_compat > OSDMAP_WRAPPER_COMPAT_VER) {
 766			pr_warn("got v %d cv %d > %d of %s ceph_osdmap\n",
 767				struct_v, struct_compat,
 768				OSDMAP_WRAPPER_COMPAT_VER, prefix);
 769			return -EINVAL;
 770		}
 771		*p += 4; /* ignore wrapper struct_len */
 772
 773		ceph_decode_8_safe(p, end, struct_v, e_inval);
 774		ceph_decode_8_safe(p, end, struct_compat, e_inval);
 775		if (struct_compat > OSDMAP_CLIENT_DATA_COMPAT_VER) {
 776			pr_warn("got v %d cv %d > %d of %s ceph_osdmap client data\n",
 777				struct_v, struct_compat,
 778				OSDMAP_CLIENT_DATA_COMPAT_VER, prefix);
 779			return -EINVAL;
 780		}
 781		*p += 4; /* ignore client data struct_len */
 782	} else {
 783		u16 version;
 784
 785		*p -= 1;
 786		ceph_decode_16_safe(p, end, version, e_inval);
 787		if (version < 6) {
 788			pr_warn("got v %d < 6 of %s ceph_osdmap\n",
 789				version, prefix);
 790			return -EINVAL;
 791		}
 792
 793		/* old osdmap enconding */
 794		struct_v = 0;
 795	}
 796
 797	*v = struct_v;
 798	return 0;
 799
 800e_inval:
 801	return -EINVAL;
 802}
 803
 804static int __decode_pools(void **p, void *end, struct ceph_osdmap *map,
 805			  bool incremental)
 806{
 807	u32 n;
 808
 809	ceph_decode_32_safe(p, end, n, e_inval);
 810	while (n--) {
 811		struct ceph_pg_pool_info *pi;
 812		u64 pool;
 813		int ret;
 814
 815		ceph_decode_64_safe(p, end, pool, e_inval);
 816
 817		pi = __lookup_pg_pool(&map->pg_pools, pool);
 818		if (!incremental || !pi) {
 819			pi = kzalloc(sizeof(*pi), GFP_NOFS);
 820			if (!pi)
 821				return -ENOMEM;
 822
 823			pi->id = pool;
 824
 825			ret = __insert_pg_pool(&map->pg_pools, pi);
 826			if (ret) {
 827				kfree(pi);
 828				return ret;
 829			}
 830		}
 831
 832		ret = decode_pool(p, end, pi);
 833		if (ret)
 834			return ret;
 835	}
 836
 837	return 0;
 838
 839e_inval:
 840	return -EINVAL;
 841}
 842
 843static int decode_pools(void **p, void *end, struct ceph_osdmap *map)
 844{
 845	return __decode_pools(p, end, map, false);
 846}
 847
 848static int decode_new_pools(void **p, void *end, struct ceph_osdmap *map)
 849{
 850	return __decode_pools(p, end, map, true);
 851}
 852
 853static int __decode_pg_temp(void **p, void *end, struct ceph_osdmap *map,
 854			    bool incremental)
 855{
 856	u32 n;
 857
 858	ceph_decode_32_safe(p, end, n, e_inval);
 859	while (n--) {
 860		struct ceph_pg pgid;
 861		u32 len, i;
 862		int ret;
 863
 864		ret = ceph_decode_pgid(p, end, &pgid);
 865		if (ret)
 866			return ret;
 867
 868		ceph_decode_32_safe(p, end, len, e_inval);
 869
 870		ret = __remove_pg_mapping(&map->pg_temp, pgid);
 871		BUG_ON(!incremental && ret != -ENOENT);
 872
 873		if (!incremental || len > 0) {
 874			struct ceph_pg_mapping *pg;
 875
 876			ceph_decode_need(p, end, len*sizeof(u32), e_inval);
 877
 878			if (len > (UINT_MAX - sizeof(*pg)) / sizeof(u32))
 879				return -EINVAL;
 880
 881			pg = kzalloc(sizeof(*pg) + len*sizeof(u32), GFP_NOFS);
 882			if (!pg)
 883				return -ENOMEM;
 884
 885			pg->pgid = pgid;
 886			pg->pg_temp.len = len;
 887			for (i = 0; i < len; i++)
 888				pg->pg_temp.osds[i] = ceph_decode_32(p);
 889
 890			ret = __insert_pg_mapping(pg, &map->pg_temp);
 891			if (ret) {
 892				kfree(pg);
 893				return ret;
 894			}
 895		}
 896	}
 897
 898	return 0;
 899
 900e_inval:
 901	return -EINVAL;
 902}
 903
 904static int decode_pg_temp(void **p, void *end, struct ceph_osdmap *map)
 905{
 906	return __decode_pg_temp(p, end, map, false);
 907}
 908
 909static int decode_new_pg_temp(void **p, void *end, struct ceph_osdmap *map)
 910{
 911	return __decode_pg_temp(p, end, map, true);
 912}
 913
 914static int __decode_primary_temp(void **p, void *end, struct ceph_osdmap *map,
 915				 bool incremental)
 916{
 917	u32 n;
 918
 919	ceph_decode_32_safe(p, end, n, e_inval);
 920	while (n--) {
 921		struct ceph_pg pgid;
 922		u32 osd;
 923		int ret;
 924
 925		ret = ceph_decode_pgid(p, end, &pgid);
 926		if (ret)
 927			return ret;
 928
 929		ceph_decode_32_safe(p, end, osd, e_inval);
 930
 931		ret = __remove_pg_mapping(&map->primary_temp, pgid);
 932		BUG_ON(!incremental && ret != -ENOENT);
 933
 934		if (!incremental || osd != (u32)-1) {
 935			struct ceph_pg_mapping *pg;
 936
 937			pg = kzalloc(sizeof(*pg), GFP_NOFS);
 938			if (!pg)
 939				return -ENOMEM;
 940
 941			pg->pgid = pgid;
 942			pg->primary_temp.osd = osd;
 943
 944			ret = __insert_pg_mapping(pg, &map->primary_temp);
 945			if (ret) {
 946				kfree(pg);
 947				return ret;
 948			}
 949		}
 950	}
 951
 952	return 0;
 953
 954e_inval:
 955	return -EINVAL;
 956}
 957
 958static int decode_primary_temp(void **p, void *end, struct ceph_osdmap *map)
 959{
 960	return __decode_primary_temp(p, end, map, false);
 961}
 962
 963static int decode_new_primary_temp(void **p, void *end,
 964				   struct ceph_osdmap *map)
 965{
 966	return __decode_primary_temp(p, end, map, true);
 967}
 968
 969u32 ceph_get_primary_affinity(struct ceph_osdmap *map, int osd)
 970{
 971	BUG_ON(osd >= map->max_osd);
 972
 973	if (!map->osd_primary_affinity)
 974		return CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
 975
 976	return map->osd_primary_affinity[osd];
 977}
 978
 979static int set_primary_affinity(struct ceph_osdmap *map, int osd, u32 aff)
 980{
 981	BUG_ON(osd >= map->max_osd);
 982
 983	if (!map->osd_primary_affinity) {
 984		int i;
 985
 986		map->osd_primary_affinity = kmalloc(map->max_osd*sizeof(u32),
 987						    GFP_NOFS);
 988		if (!map->osd_primary_affinity)
 989			return -ENOMEM;
 990
 991		for (i = 0; i < map->max_osd; i++)
 992			map->osd_primary_affinity[i] =
 993			    CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
 994	}
 995
 996	map->osd_primary_affinity[osd] = aff;
 997
 998	return 0;
 999}
1000
1001static int decode_primary_affinity(void **p, void *end,
1002				   struct ceph_osdmap *map)
1003{
1004	u32 len, i;
1005
1006	ceph_decode_32_safe(p, end, len, e_inval);
1007	if (len == 0) {
1008		kfree(map->osd_primary_affinity);
1009		map->osd_primary_affinity = NULL;
1010		return 0;
1011	}
1012	if (len != map->max_osd)
1013		goto e_inval;
1014
1015	ceph_decode_need(p, end, map->max_osd*sizeof(u32), e_inval);
1016
1017	for (i = 0; i < map->max_osd; i++) {
1018		int ret;
1019
1020		ret = set_primary_affinity(map, i, ceph_decode_32(p));
1021		if (ret)
1022			return ret;
1023	}
1024
1025	return 0;
1026
1027e_inval:
1028	return -EINVAL;
1029}
1030
1031static int decode_new_primary_affinity(void **p, void *end,
1032				       struct ceph_osdmap *map)
1033{
1034	u32 n;
1035
1036	ceph_decode_32_safe(p, end, n, e_inval);
1037	while (n--) {
1038		u32 osd, aff;
1039		int ret;
1040
1041		ceph_decode_32_safe(p, end, osd, e_inval);
1042		ceph_decode_32_safe(p, end, aff, e_inval);
1043
1044		ret = set_primary_affinity(map, osd, aff);
1045		if (ret)
1046			return ret;
1047
1048		pr_info("osd%d primary-affinity 0x%x\n", osd, aff);
1049	}
1050
1051	return 0;
1052
1053e_inval:
1054	return -EINVAL;
1055}
1056
1057/*
1058 * decode a full map.
1059 */
1060static int osdmap_decode(void **p, void *end, struct ceph_osdmap *map)
1061{
1062	u8 struct_v;
1063	u32 epoch = 0;
 
 
 
1064	void *start = *p;
1065	u32 max;
1066	u32 len, i;
1067	int err;
1068
1069	dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p));
 
 
 
1070
1071	err = get_osdmap_client_data_v(p, end, "full", &struct_v);
1072	if (err)
 
 
1073		goto bad;
 
1074
1075	/* fsid, epoch, created, modified */
1076	ceph_decode_need(p, end, sizeof(map->fsid) + sizeof(u32) +
1077			 sizeof(map->created) + sizeof(map->modified), e_inval);
1078	ceph_decode_copy(p, &map->fsid, sizeof(map->fsid));
1079	epoch = map->epoch = ceph_decode_32(p);
1080	ceph_decode_copy(p, &map->created, sizeof(map->created));
1081	ceph_decode_copy(p, &map->modified, sizeof(map->modified));
1082
1083	/* pools */
1084	err = decode_pools(p, end, map);
1085	if (err)
1086		goto bad;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1087
1088	/* pool_name */
1089	err = decode_pool_names(p, end, map);
1090	if (err)
1091		goto bad;
1092
1093	ceph_decode_32_safe(p, end, map->pool_max, e_inval);
1094
1095	ceph_decode_32_safe(p, end, map->flags, e_inval);
1096
1097	/* max_osd */
1098	ceph_decode_32_safe(p, end, max, e_inval);
1099
1100	/* (re)alloc osd arrays */
1101	err = osdmap_set_max_osd(map, max);
1102	if (err)
1103		goto bad;
 
1104
1105	/* osd_state, osd_weight, osd_addrs->client_addr */
 
1106	ceph_decode_need(p, end, 3*sizeof(u32) +
1107			 map->max_osd*(1 + sizeof(*map->osd_weight) +
1108				       sizeof(*map->osd_addr)), e_inval);
1109
1110	if (ceph_decode_32(p) != map->max_osd)
1111		goto e_inval;
1112
1113	ceph_decode_copy(p, map->osd_state, map->max_osd);
1114
1115	if (ceph_decode_32(p) != map->max_osd)
1116		goto e_inval;
1117
1118	for (i = 0; i < map->max_osd; i++)
1119		map->osd_weight[i] = ceph_decode_32(p);
1120
1121	if (ceph_decode_32(p) != map->max_osd)
1122		goto e_inval;
1123
1124	ceph_decode_copy(p, map->osd_addr, map->max_osd*sizeof(*map->osd_addr));
1125	for (i = 0; i < map->max_osd; i++)
1126		ceph_decode_addr(&map->osd_addr[i]);
1127
1128	/* pg_temp */
1129	err = decode_pg_temp(p, end, map);
1130	if (err)
1131		goto bad;
 
 
1132
1133	/* primary_temp */
1134	if (struct_v >= 1) {
1135		err = decode_primary_temp(p, end, map);
1136		if (err)
 
 
 
1137			goto bad;
1138	}
 
 
 
1139
1140	/* primary_affinity */
1141	if (struct_v >= 2) {
1142		err = decode_primary_affinity(p, end, map);
1143		if (err)
1144			goto bad;
1145	} else {
1146		/* XXX can this happen? */
1147		kfree(map->osd_primary_affinity);
1148		map->osd_primary_affinity = NULL;
1149	}
1150
1151	/* crush */
1152	ceph_decode_32_safe(p, end, len, e_inval);
1153	map->crush = crush_decode(*p, min(*p + len, end));
 
 
 
 
1154	if (IS_ERR(map->crush)) {
1155		err = PTR_ERR(map->crush);
1156		map->crush = NULL;
1157		goto bad;
1158	}
1159	*p += len;
1160
1161	/* ignore the rest */
1162	*p = end;
1163
1164	dout("full osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd);
1165	return 0;
1166
1167e_inval:
1168	err = -EINVAL;
1169bad:
1170	pr_err("corrupt full osdmap (%d) epoch %d off %d (%p of %p-%p)\n",
1171	       err, epoch, (int)(*p - start), *p, start, end);
1172	print_hex_dump(KERN_DEBUG, "osdmap: ",
1173		       DUMP_PREFIX_OFFSET, 16, 1,
1174		       start, end - start, true);
1175	return err;
1176}
1177
1178/*
1179 * Allocate and decode a full map.
1180 */
1181struct ceph_osdmap *ceph_osdmap_decode(void **p, void *end)
1182{
1183	struct ceph_osdmap *map;
1184	int ret;
1185
1186	map = kzalloc(sizeof(*map), GFP_NOFS);
1187	if (!map)
1188		return ERR_PTR(-ENOMEM);
1189
1190	map->pg_temp = RB_ROOT;
1191	map->primary_temp = RB_ROOT;
1192	mutex_init(&map->crush_scratch_mutex);
1193
1194	ret = osdmap_decode(p, end, map);
1195	if (ret) {
1196		ceph_osdmap_destroy(map);
1197		return ERR_PTR(ret);
1198	}
1199
1200	return map;
1201}
1202
1203/*
1204 * decode and apply an incremental map update.
1205 */
1206struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end,
1207					     struct ceph_osdmap *map,
1208					     struct ceph_messenger *msgr)
1209{
1210	struct crush_map *newcrush = NULL;
1211	struct ceph_fsid fsid;
1212	u32 epoch = 0;
1213	struct ceph_timespec modified;
1214	s32 len;
1215	u64 pool;
1216	__s64 new_pool_max;
1217	__s32 new_flags, max;
1218	void *start = *p;
1219	int err;
1220	u8 struct_v;
1221
1222	dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p));
1223
1224	err = get_osdmap_client_data_v(p, end, "inc", &struct_v);
1225	if (err)
 
 
1226		goto bad;
 
1227
1228	/* fsid, epoch, modified, new_pool_max, new_flags */
1229	ceph_decode_need(p, end, sizeof(fsid) + sizeof(u32) + sizeof(modified) +
1230			 sizeof(u64) + sizeof(u32), e_inval);
1231	ceph_decode_copy(p, &fsid, sizeof(fsid));
1232	epoch = ceph_decode_32(p);
1233	BUG_ON(epoch != map->epoch+1);
1234	ceph_decode_copy(p, &modified, sizeof(modified));
1235	new_pool_max = ceph_decode_64(p);
1236	new_flags = ceph_decode_32(p);
1237
1238	/* full map? */
1239	ceph_decode_32_safe(p, end, len, e_inval);
1240	if (len > 0) {
1241		dout("apply_incremental full map len %d, %p to %p\n",
1242		     len, *p, end);
1243		return ceph_osdmap_decode(p, min(*p+len, end));
1244	}
1245
1246	/* new crush? */
1247	ceph_decode_32_safe(p, end, len, e_inval);
1248	if (len > 0) {
 
 
1249		newcrush = crush_decode(*p, min(*p+len, end));
1250		if (IS_ERR(newcrush)) {
1251			err = PTR_ERR(newcrush);
1252			newcrush = NULL;
1253			goto bad;
1254		}
1255		*p += len;
1256	}
1257
1258	/* new flags? */
1259	if (new_flags >= 0)
1260		map->flags = new_flags;
1261	if (new_pool_max >= 0)
1262		map->pool_max = new_pool_max;
1263
 
 
1264	/* new max? */
1265	ceph_decode_32_safe(p, end, max, e_inval);
1266	if (max >= 0) {
1267		err = osdmap_set_max_osd(map, max);
1268		if (err)
1269			goto bad;
1270	}
1271
1272	map->epoch++;
1273	map->modified = modified;
1274	if (newcrush) {
1275		if (map->crush)
1276			crush_destroy(map->crush);
1277		map->crush = newcrush;
1278		newcrush = NULL;
1279	}
1280
1281	/* new_pools */
1282	err = decode_new_pools(p, end, map);
1283	if (err)
1284		goto bad;
 
1285
1286	/* new_pool_names */
1287	err = decode_pool_names(p, end, map);
1288	if (err)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1289		goto bad;
1290
1291	/* old_pool */
1292	ceph_decode_32_safe(p, end, len, e_inval);
1293	while (len--) {
1294		struct ceph_pg_pool_info *pi;
1295
1296		ceph_decode_64_safe(p, end, pool, e_inval);
1297		pi = __lookup_pg_pool(&map->pg_pools, pool);
1298		if (pi)
1299			__remove_pg_pool(&map->pg_pools, pi);
1300	}
1301
1302	/* new_up */
1303	ceph_decode_32_safe(p, end, len, e_inval);
 
1304	while (len--) {
1305		u32 osd;
1306		struct ceph_entity_addr addr;
1307		ceph_decode_32_safe(p, end, osd, e_inval);
1308		ceph_decode_copy_safe(p, end, &addr, sizeof(addr), e_inval);
1309		ceph_decode_addr(&addr);
1310		pr_info("osd%d up\n", osd);
1311		BUG_ON(osd >= map->max_osd);
1312		map->osd_state[osd] |= CEPH_OSD_UP | CEPH_OSD_EXISTS;
1313		map->osd_addr[osd] = addr;
1314	}
1315
1316	/* new_state */
1317	ceph_decode_32_safe(p, end, len, e_inval);
1318	while (len--) {
1319		u32 osd;
1320		u8 xorstate;
1321		ceph_decode_32_safe(p, end, osd, e_inval);
1322		xorstate = **(u8 **)p;
1323		(*p)++;  /* clean flag */
1324		if (xorstate == 0)
1325			xorstate = CEPH_OSD_UP;
1326		if (xorstate & CEPH_OSD_UP)
1327			pr_info("osd%d down\n", osd);
1328		if (osd < map->max_osd)
1329			map->osd_state[osd] ^= xorstate;
1330	}
1331
1332	/* new_weight */
1333	ceph_decode_32_safe(p, end, len, e_inval);
1334	while (len--) {
1335		u32 osd, off;
1336		ceph_decode_need(p, end, sizeof(u32)*2, e_inval);
1337		osd = ceph_decode_32(p);
1338		off = ceph_decode_32(p);
1339		pr_info("osd%d weight 0x%x %s\n", osd, off,
1340		     off == CEPH_OSD_IN ? "(in)" :
1341		     (off == CEPH_OSD_OUT ? "(out)" : ""));
1342		if (osd < map->max_osd)
1343			map->osd_weight[osd] = off;
1344	}
1345
1346	/* new_pg_temp */
1347	err = decode_new_pg_temp(p, end, map);
1348	if (err)
1349		goto bad;
1350
1351	/* new_primary_temp */
1352	if (struct_v >= 1) {
1353		err = decode_new_primary_temp(p, end, map);
1354		if (err)
1355			goto bad;
1356	}
1357
1358	/* new_primary_affinity */
1359	if (struct_v >= 2) {
1360		err = decode_new_primary_affinity(p, end, map);
1361		if (err)
1362			goto bad;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1363	}
1364
1365	/* ignore the rest */
1366	*p = end;
1367
1368	dout("inc osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd);
1369	return map;
1370
1371e_inval:
1372	err = -EINVAL;
1373bad:
1374	pr_err("corrupt inc osdmap (%d) epoch %d off %d (%p of %p-%p)\n",
1375	       err, epoch, (int)(*p - start), *p, start, end);
1376	print_hex_dump(KERN_DEBUG, "osdmap: ",
1377		       DUMP_PREFIX_OFFSET, 16, 1,
1378		       start, end - start, true);
1379	if (newcrush)
1380		crush_destroy(newcrush);
1381	return ERR_PTR(err);
1382}
1383
1384
1385
1386
1387/*
1388 * calculate file layout from given offset, length.
1389 * fill in correct oid, logical length, and object extent
1390 * offset, length.
1391 *
1392 * for now, we write only a single su, until we can
1393 * pass a stride back to the caller.
1394 */
1395int ceph_calc_file_object_mapping(struct ceph_file_layout *layout,
1396				   u64 off, u64 len,
1397				   u64 *ono,
1398				   u64 *oxoff, u64 *oxlen)
1399{
1400	u32 osize = le32_to_cpu(layout->fl_object_size);
1401	u32 su = le32_to_cpu(layout->fl_stripe_unit);
1402	u32 sc = le32_to_cpu(layout->fl_stripe_count);
1403	u32 bl, stripeno, stripepos, objsetno;
1404	u32 su_per_object;
1405	u64 t, su_offset;
1406
1407	dout("mapping %llu~%llu  osize %u fl_su %u\n", off, len,
1408	     osize, su);
1409	if (su == 0 || sc == 0)
1410		goto invalid;
1411	su_per_object = osize / su;
1412	if (su_per_object == 0)
1413		goto invalid;
1414	dout("osize %u / su %u = su_per_object %u\n", osize, su,
1415	     su_per_object);
1416
1417	if ((su & ~PAGE_MASK) != 0)
1418		goto invalid;
1419
1420	/* bl = *off / su; */
1421	t = off;
1422	do_div(t, su);
1423	bl = t;
1424	dout("off %llu / su %u = bl %u\n", off, su, bl);
1425
1426	stripeno = bl / sc;
1427	stripepos = bl % sc;
1428	objsetno = stripeno / su_per_object;
1429
1430	*ono = objsetno * sc + stripepos;
1431	dout("objset %u * sc %u = ono %u\n", objsetno, sc, (unsigned int)*ono);
1432
1433	/* *oxoff = *off % layout->fl_stripe_unit;  # offset in su */
1434	t = off;
1435	su_offset = do_div(t, su);
1436	*oxoff = su_offset + (stripeno % su_per_object) * su;
1437
1438	/*
1439	 * Calculate the length of the extent being written to the selected
1440	 * object. This is the minimum of the full length requested (len) or
1441	 * the remainder of the current stripe being written to.
1442	 */
1443	*oxlen = min_t(u64, len, su - su_offset);
 
1444
1445	dout(" obj extent %llu~%llu\n", *oxoff, *oxlen);
1446	return 0;
1447
1448invalid:
1449	dout(" invalid layout\n");
1450	*ono = 0;
1451	*oxoff = 0;
1452	*oxlen = 0;
1453	return -EINVAL;
1454}
1455EXPORT_SYMBOL(ceph_calc_file_object_mapping);
1456
1457/*
1458 * Calculate mapping of a (oloc, oid) pair to a PG.  Should only be
1459 * called with target's (oloc, oid), since tiering isn't taken into
1460 * account.
1461 */
1462int ceph_oloc_oid_to_pg(struct ceph_osdmap *osdmap,
1463			struct ceph_object_locator *oloc,
1464			struct ceph_object_id *oid,
1465			struct ceph_pg *pg_out)
1466{
1467	struct ceph_pg_pool_info *pi;
 
 
 
 
 
 
1468
1469	pi = __lookup_pg_pool(&osdmap->pg_pools, oloc->pool);
1470	if (!pi)
1471		return -EIO;
 
 
 
1472
1473	pg_out->pool = oloc->pool;
1474	pg_out->seed = ceph_str_hash(pi->object_hash, oid->name,
1475				     oid->name_len);
 
1476
1477	dout("%s '%.*s' pgid %llu.%x\n", __func__, oid->name_len, oid->name,
1478	     pg_out->pool, pg_out->seed);
1479	return 0;
1480}
1481EXPORT_SYMBOL(ceph_oloc_oid_to_pg);
1482
1483static int do_crush(struct ceph_osdmap *map, int ruleno, int x,
1484		    int *result, int result_max,
1485		    const __u32 *weight, int weight_max)
1486{
1487	int r;
1488
1489	BUG_ON(result_max > CEPH_PG_MAX_SIZE);
1490
1491	mutex_lock(&map->crush_scratch_mutex);
1492	r = crush_do_rule(map->crush, ruleno, x, result, result_max,
1493			  weight, weight_max, map->crush_scratch_ary);
1494	mutex_unlock(&map->crush_scratch_mutex);
1495
1496	return r;
1497}
1498
1499/*
1500 * Calculate raw (crush) set for given pgid.
1501 *
1502 * Return raw set length, or error.
1503 */
1504static int pg_to_raw_osds(struct ceph_osdmap *osdmap,
1505			  struct ceph_pg_pool_info *pool,
1506			  struct ceph_pg pgid, u32 pps, int *osds)
1507{
 
 
1508	int ruleno;
1509	int len;
1510
1511	/* crush */
1512	ruleno = crush_find_rule(osdmap->crush, pool->crush_ruleset,
1513				 pool->type, pool->size);
1514	if (ruleno < 0) {
1515		pr_err("no crush rule: pool %lld ruleset %d type %d size %d\n",
1516		       pgid.pool, pool->crush_ruleset, pool->type,
1517		       pool->size);
1518		return -ENOENT;
1519	}
1520
1521	len = do_crush(osdmap, ruleno, pps, osds,
1522		       min_t(int, pool->size, CEPH_PG_MAX_SIZE),
1523		       osdmap->osd_weight, osdmap->max_osd);
1524	if (len < 0) {
1525		pr_err("error %d from crush rule %d: pool %lld ruleset %d type %d size %d\n",
1526		       len, ruleno, pgid.pool, pool->crush_ruleset,
1527		       pool->type, pool->size);
1528		return len;
1529	}
1530
1531	return len;
1532}
1533
1534/*
1535 * Given raw set, calculate up set and up primary.
1536 *
1537 * Return up set length.  *primary is set to up primary osd id, or -1
1538 * if up set is empty.
1539 */
1540static int raw_to_up_osds(struct ceph_osdmap *osdmap,
1541			  struct ceph_pg_pool_info *pool,
1542			  int *osds, int len, int *primary)
1543{
1544	int up_primary = -1;
1545	int i;
1546
1547	if (ceph_can_shift_osds(pool)) {
1548		int removed = 0;
1549
1550		for (i = 0; i < len; i++) {
1551			if (ceph_osd_is_down(osdmap, osds[i])) {
1552				removed++;
1553				continue;
1554			}
1555			if (removed)
1556				osds[i - removed] = osds[i];
1557		}
1558
1559		len -= removed;
1560		if (len > 0)
1561			up_primary = osds[0];
1562	} else {
1563		for (i = len - 1; i >= 0; i--) {
1564			if (ceph_osd_is_down(osdmap, osds[i]))
1565				osds[i] = CRUSH_ITEM_NONE;
1566			else
1567				up_primary = osds[i];
1568		}
1569	}
1570
1571	*primary = up_primary;
1572	return len;
1573}
1574
1575static void apply_primary_affinity(struct ceph_osdmap *osdmap, u32 pps,
1576				   struct ceph_pg_pool_info *pool,
1577				   int *osds, int len, int *primary)
1578{
1579	int i;
1580	int pos = -1;
1581
1582	/*
1583	 * Do we have any non-default primary_affinity values for these
1584	 * osds?
1585	 */
1586	if (!osdmap->osd_primary_affinity)
1587		return;
1588
1589	for (i = 0; i < len; i++) {
1590		int osd = osds[i];
1591
1592		if (osd != CRUSH_ITEM_NONE &&
1593		    osdmap->osd_primary_affinity[osd] !=
1594					CEPH_OSD_DEFAULT_PRIMARY_AFFINITY) {
1595			break;
1596		}
1597	}
1598	if (i == len)
1599		return;
1600
1601	/*
1602	 * Pick the primary.  Feed both the seed (for the pg) and the
1603	 * osd into the hash/rng so that a proportional fraction of an
1604	 * osd's pgs get rejected as primary.
1605	 */
1606	for (i = 0; i < len; i++) {
1607		int osd = osds[i];
1608		u32 aff;
1609
1610		if (osd == CRUSH_ITEM_NONE)
1611			continue;
1612
1613		aff = osdmap->osd_primary_affinity[osd];
1614		if (aff < CEPH_OSD_MAX_PRIMARY_AFFINITY &&
1615		    (crush_hash32_2(CRUSH_HASH_RJENKINS1,
1616				    pps, osd) >> 16) >= aff) {
1617			/*
1618			 * We chose not to use this primary.  Note it
1619			 * anyway as a fallback in case we don't pick
1620			 * anyone else, but keep looking.
1621			 */
1622			if (pos < 0)
1623				pos = i;
1624		} else {
1625			pos = i;
1626			break;
1627		}
1628	}
1629	if (pos < 0)
1630		return;
1631
1632	*primary = osds[pos];
1633
1634	if (ceph_can_shift_osds(pool) && pos > 0) {
1635		/* move the new primary to the front */
1636		for (i = pos; i > 0; i--)
1637			osds[i] = osds[i - 1];
1638		osds[0] = *primary;
1639	}
1640}
1641
1642/*
1643 * Given up set, apply pg_temp and primary_temp mappings.
1644 *
1645 * Return acting set length.  *primary is set to acting primary osd id,
1646 * or -1 if acting set is empty.
1647 */
1648static int apply_temps(struct ceph_osdmap *osdmap,
1649		       struct ceph_pg_pool_info *pool, struct ceph_pg pgid,
1650		       int *osds, int len, int *primary)
1651{
1652	struct ceph_pg_mapping *pg;
1653	int temp_len;
1654	int temp_primary;
1655	int i;
1656
1657	/* raw_pg -> pg */
1658	pgid.seed = ceph_stable_mod(pgid.seed, pool->pg_num,
1659				    pool->pg_num_mask);
1660
1661	/* pg_temp? */
 
 
 
1662	pg = __lookup_pg_mapping(&osdmap->pg_temp, pgid);
1663	if (pg) {
1664		temp_len = 0;
1665		temp_primary = -1;
 
1666
1667		for (i = 0; i < pg->pg_temp.len; i++) {
1668			if (ceph_osd_is_down(osdmap, pg->pg_temp.osds[i])) {
1669				if (ceph_can_shift_osds(pool))
1670					continue;
1671				else
1672					osds[temp_len++] = CRUSH_ITEM_NONE;
1673			} else {
1674				osds[temp_len++] = pg->pg_temp.osds[i];
1675			}
1676		}
1677
1678		/* apply pg_temp's primary */
1679		for (i = 0; i < temp_len; i++) {
1680			if (osds[i] != CRUSH_ITEM_NONE) {
1681				temp_primary = osds[i];
1682				break;
1683			}
1684		}
1685	} else {
1686		temp_len = len;
1687		temp_primary = *primary;
 
 
1688	}
1689
1690	/* primary_temp? */
1691	pg = __lookup_pg_mapping(&osdmap->primary_temp, pgid);
1692	if (pg)
1693		temp_primary = pg->primary_temp.osd;
1694
1695	*primary = temp_primary;
1696	return temp_len;
1697}
1698
1699/*
1700 * Calculate acting set for given pgid.
1701 *
1702 * Return acting set length, or error.  *primary is set to acting
1703 * primary osd id, or -1 if acting set is empty or on error.
1704 */
1705int ceph_calc_pg_acting(struct ceph_osdmap *osdmap, struct ceph_pg pgid,
1706			int *osds, int *primary)
1707{
1708	struct ceph_pg_pool_info *pool;
1709	u32 pps;
1710	int len;
1711
1712	pool = __lookup_pg_pool(&osdmap->pg_pools, pgid.pool);
1713	if (!pool) {
1714		*primary = -1;
1715		return -ENOENT;
1716	}
1717
1718	if (pool->flags & CEPH_POOL_FLAG_HASHPSPOOL) {
1719		/* hash pool id and seed so that pool PGs do not overlap */
1720		pps = crush_hash32_2(CRUSH_HASH_RJENKINS1,
1721				     ceph_stable_mod(pgid.seed, pool->pgp_num,
1722						     pool->pgp_num_mask),
1723				     pgid.pool);
1724	} else {
1725		/*
1726		 * legacy behavior: add ps and pool together.  this is
1727		 * not a great approach because the PGs from each pool
1728		 * will overlap on top of each other: 0.5 == 1.4 ==
1729		 * 2.3 == ...
1730		 */
1731		pps = ceph_stable_mod(pgid.seed, pool->pgp_num,
1732				      pool->pgp_num_mask) +
1733			(unsigned)pgid.pool;
1734	}
1735
1736	len = pg_to_raw_osds(osdmap, pool, pgid, pps, osds);
1737	if (len < 0) {
1738		*primary = -1;
1739		return len;
1740	}
1741
1742	len = raw_to_up_osds(osdmap, pool, osds, len, primary);
1743
1744	apply_primary_affinity(osdmap, pps, pool, osds, len, primary);
1745
1746	len = apply_temps(osdmap, pool, pgid, osds, len, primary);
1747
1748	return len;
1749}
1750
1751/*
1752 * Return primary osd for given pgid, or -1 if none.
1753 */
1754int ceph_calc_pg_primary(struct ceph_osdmap *osdmap, struct ceph_pg pgid)
1755{
1756	int osds[CEPH_PG_MAX_SIZE];
1757	int primary;
1758
1759	ceph_calc_pg_acting(osdmap, pgid, osds, &primary);
 
 
1760
1761	return primary;
 
 
 
 
1762}
1763EXPORT_SYMBOL(ceph_calc_pg_primary);