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 
ceph_osdmap_state_str(char * str,int len,int state)14 char *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 	}
35 done:
36 	return str;
37 }
38 
39 /* maps */
40 
calc_bits_of(unsigned t)41 static int calc_bits_of(unsigned 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  */
calc_pg_masks(struct ceph_pg_pool_info * pi)54 static 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  */
crush_decode_uniform_bucket(void ** p,void * end,struct crush_bucket_uniform * b)68 static 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;
75 bad:
76 	return -EINVAL;
77 }
78 
crush_decode_list_bucket(void ** p,void * end,struct crush_bucket_list * b)79 static 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;
96 bad:
97 	return -EINVAL;
98 }
99 
crush_decode_tree_bucket(void ** p,void * end,struct crush_bucket_tree * b)100 static 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;
113 bad:
114 	return -EINVAL;
115 }
116 
crush_decode_straw_bucket(void ** p,void * end,struct crush_bucket_straw * b)117 static 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;
134 bad:
135 	return -EINVAL;
136 }
137 
crush_decode(void * pbyval,void * end)138 static 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)magic, (unsigned)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->device_parents = kcalloc(c->max_devices, sizeof(u32), GFP_NOFS);
165 	if (c->device_parents == NULL)
166 		goto badmem;
167 	c->bucket_parents = kcalloc(c->max_buckets, sizeof(u32), GFP_NOFS);
168 	if (c->bucket_parents == NULL)
169 		goto badmem;
170 
171 	c->buckets = kcalloc(c->max_buckets, sizeof(*c->buckets), GFP_NOFS);
172 	if (c->buckets == NULL)
173 		goto badmem;
174 	c->rules = kcalloc(c->max_rules, sizeof(*c->rules), GFP_NOFS);
175 	if (c->rules == NULL)
176 		goto badmem;
177 
178 	/* buckets */
179 	for (i = 0; i < c->max_buckets; i++) {
180 		int size = 0;
181 		u32 alg;
182 		struct crush_bucket *b;
183 
184 		ceph_decode_32_safe(p, end, alg, bad);
185 		if (alg == 0) {
186 			c->buckets[i] = NULL;
187 			continue;
188 		}
189 		dout("crush_decode bucket %d off %x %p to %p\n",
190 		     i, (int)(*p-start), *p, end);
191 
192 		switch (alg) {
193 		case CRUSH_BUCKET_UNIFORM:
194 			size = sizeof(struct crush_bucket_uniform);
195 			break;
196 		case CRUSH_BUCKET_LIST:
197 			size = sizeof(struct crush_bucket_list);
198 			break;
199 		case CRUSH_BUCKET_TREE:
200 			size = sizeof(struct crush_bucket_tree);
201 			break;
202 		case CRUSH_BUCKET_STRAW:
203 			size = sizeof(struct crush_bucket_straw);
204 			break;
205 		default:
206 			err = -EINVAL;
207 			goto bad;
208 		}
209 		BUG_ON(size == 0);
210 		b = c->buckets[i] = kzalloc(size, GFP_NOFS);
211 		if (b == NULL)
212 			goto badmem;
213 
214 		ceph_decode_need(p, end, 4*sizeof(u32), bad);
215 		b->id = ceph_decode_32(p);
216 		b->type = ceph_decode_16(p);
217 		b->alg = ceph_decode_8(p);
218 		b->hash = ceph_decode_8(p);
219 		b->weight = ceph_decode_32(p);
220 		b->size = ceph_decode_32(p);
221 
222 		dout("crush_decode bucket size %d off %x %p to %p\n",
223 		     b->size, (int)(*p-start), *p, end);
224 
225 		b->items = kcalloc(b->size, sizeof(__s32), GFP_NOFS);
226 		if (b->items == NULL)
227 			goto badmem;
228 		b->perm = kcalloc(b->size, sizeof(u32), GFP_NOFS);
229 		if (b->perm == NULL)
230 			goto badmem;
231 		b->perm_n = 0;
232 
233 		ceph_decode_need(p, end, b->size*sizeof(u32), bad);
234 		for (j = 0; j < b->size; j++)
235 			b->items[j] = ceph_decode_32(p);
236 
237 		switch (b->alg) {
238 		case CRUSH_BUCKET_UNIFORM:
239 			err = crush_decode_uniform_bucket(p, end,
240 				  (struct crush_bucket_uniform *)b);
241 			if (err < 0)
242 				goto bad;
243 			break;
244 		case CRUSH_BUCKET_LIST:
245 			err = crush_decode_list_bucket(p, end,
246 			       (struct crush_bucket_list *)b);
247 			if (err < 0)
248 				goto bad;
249 			break;
250 		case CRUSH_BUCKET_TREE:
251 			err = crush_decode_tree_bucket(p, end,
252 				(struct crush_bucket_tree *)b);
253 			if (err < 0)
254 				goto bad;
255 			break;
256 		case CRUSH_BUCKET_STRAW:
257 			err = crush_decode_straw_bucket(p, end,
258 				(struct crush_bucket_straw *)b);
259 			if (err < 0)
260 				goto bad;
261 			break;
262 		}
263 	}
264 
265 	/* rules */
266 	dout("rule vec is %p\n", c->rules);
267 	for (i = 0; i < c->max_rules; i++) {
268 		u32 yes;
269 		struct crush_rule *r;
270 
271 		ceph_decode_32_safe(p, end, yes, bad);
272 		if (!yes) {
273 			dout("crush_decode NO rule %d off %x %p to %p\n",
274 			     i, (int)(*p-start), *p, end);
275 			c->rules[i] = NULL;
276 			continue;
277 		}
278 
279 		dout("crush_decode rule %d off %x %p to %p\n",
280 		     i, (int)(*p-start), *p, end);
281 
282 		/* len */
283 		ceph_decode_32_safe(p, end, yes, bad);
284 #if BITS_PER_LONG == 32
285 		err = -EINVAL;
286 		if (yes > (ULONG_MAX - sizeof(*r))
287 			  / sizeof(struct crush_rule_step))
288 			goto bad;
289 #endif
290 		r = c->rules[i] = kmalloc(sizeof(*r) +
291 					  yes*sizeof(struct crush_rule_step),
292 					  GFP_NOFS);
293 		if (r == NULL)
294 			goto badmem;
295 		dout(" rule %d is at %p\n", i, r);
296 		r->len = yes;
297 		ceph_decode_copy_safe(p, end, &r->mask, 4, bad); /* 4 u8's */
298 		ceph_decode_need(p, end, r->len*3*sizeof(u32), bad);
299 		for (j = 0; j < r->len; j++) {
300 			r->steps[j].op = ceph_decode_32(p);
301 			r->steps[j].arg1 = ceph_decode_32(p);
302 			r->steps[j].arg2 = ceph_decode_32(p);
303 		}
304 	}
305 
306 	/* ignore trailing name maps. */
307 
308 	dout("crush_decode success\n");
309 	return c;
310 
311 badmem:
312 	err = -ENOMEM;
313 bad:
314 	dout("crush_decode fail %d\n", err);
315 	crush_destroy(c);
316 	return ERR_PTR(err);
317 }
318 
319 /*
320  * rbtree of pg_mapping for handling pg_temp (explicit mapping of pgid
321  * to a set of osds)
322  */
pgid_cmp(struct ceph_pg l,struct ceph_pg r)323 static int pgid_cmp(struct ceph_pg l, struct ceph_pg r)
324 {
325 	u64 a = *(u64 *)&l;
326 	u64 b = *(u64 *)&r;
327 
328 	if (a < b)
329 		return -1;
330 	if (a > b)
331 		return 1;
332 	return 0;
333 }
334 
__insert_pg_mapping(struct ceph_pg_mapping * new,struct rb_root * root)335 static int __insert_pg_mapping(struct ceph_pg_mapping *new,
336 			       struct rb_root *root)
337 {
338 	struct rb_node **p = &root->rb_node;
339 	struct rb_node *parent = NULL;
340 	struct ceph_pg_mapping *pg = NULL;
341 	int c;
342 
343 	dout("__insert_pg_mapping %llx %p\n", *(u64 *)&new->pgid, new);
344 	while (*p) {
345 		parent = *p;
346 		pg = rb_entry(parent, struct ceph_pg_mapping, node);
347 		c = pgid_cmp(new->pgid, pg->pgid);
348 		if (c < 0)
349 			p = &(*p)->rb_left;
350 		else if (c > 0)
351 			p = &(*p)->rb_right;
352 		else
353 			return -EEXIST;
354 	}
355 
356 	rb_link_node(&new->node, parent, p);
357 	rb_insert_color(&new->node, root);
358 	return 0;
359 }
360 
__lookup_pg_mapping(struct rb_root * root,struct ceph_pg pgid)361 static struct ceph_pg_mapping *__lookup_pg_mapping(struct rb_root *root,
362 						   struct ceph_pg pgid)
363 {
364 	struct rb_node *n = root->rb_node;
365 	struct ceph_pg_mapping *pg;
366 	int c;
367 
368 	while (n) {
369 		pg = rb_entry(n, struct ceph_pg_mapping, node);
370 		c = pgid_cmp(pgid, pg->pgid);
371 		if (c < 0) {
372 			n = n->rb_left;
373 		} else if (c > 0) {
374 			n = n->rb_right;
375 		} else {
376 			dout("__lookup_pg_mapping %llx got %p\n",
377 			     *(u64 *)&pgid, pg);
378 			return pg;
379 		}
380 	}
381 	return NULL;
382 }
383 
__remove_pg_mapping(struct rb_root * root,struct ceph_pg pgid)384 static int __remove_pg_mapping(struct rb_root *root, struct ceph_pg pgid)
385 {
386 	struct ceph_pg_mapping *pg = __lookup_pg_mapping(root, pgid);
387 
388 	if (pg) {
389 		dout("__remove_pg_mapping %llx %p\n", *(u64 *)&pgid, pg);
390 		rb_erase(&pg->node, root);
391 		kfree(pg);
392 		return 0;
393 	}
394 	dout("__remove_pg_mapping %llx dne\n", *(u64 *)&pgid);
395 	return -ENOENT;
396 }
397 
398 /*
399  * rbtree of pg pool info
400  */
__insert_pg_pool(struct rb_root * root,struct ceph_pg_pool_info * new)401 static int __insert_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *new)
402 {
403 	struct rb_node **p = &root->rb_node;
404 	struct rb_node *parent = NULL;
405 	struct ceph_pg_pool_info *pi = NULL;
406 
407 	while (*p) {
408 		parent = *p;
409 		pi = rb_entry(parent, struct ceph_pg_pool_info, node);
410 		if (new->id < pi->id)
411 			p = &(*p)->rb_left;
412 		else if (new->id > pi->id)
413 			p = &(*p)->rb_right;
414 		else
415 			return -EEXIST;
416 	}
417 
418 	rb_link_node(&new->node, parent, p);
419 	rb_insert_color(&new->node, root);
420 	return 0;
421 }
422 
__lookup_pg_pool(struct rb_root * root,int id)423 static struct ceph_pg_pool_info *__lookup_pg_pool(struct rb_root *root, int id)
424 {
425 	struct ceph_pg_pool_info *pi;
426 	struct rb_node *n = root->rb_node;
427 
428 	while (n) {
429 		pi = rb_entry(n, struct ceph_pg_pool_info, node);
430 		if (id < pi->id)
431 			n = n->rb_left;
432 		else if (id > pi->id)
433 			n = n->rb_right;
434 		else
435 			return pi;
436 	}
437 	return NULL;
438 }
439 
ceph_pg_poolid_by_name(struct ceph_osdmap * map,const char * name)440 int ceph_pg_poolid_by_name(struct ceph_osdmap *map, const char *name)
441 {
442 	struct rb_node *rbp;
443 
444 	for (rbp = rb_first(&map->pg_pools); rbp; rbp = rb_next(rbp)) {
445 		struct ceph_pg_pool_info *pi =
446 			rb_entry(rbp, struct ceph_pg_pool_info, node);
447 		if (pi->name && strcmp(pi->name, name) == 0)
448 			return pi->id;
449 	}
450 	return -ENOENT;
451 }
452 EXPORT_SYMBOL(ceph_pg_poolid_by_name);
453 
__remove_pg_pool(struct rb_root * root,struct ceph_pg_pool_info * pi)454 static void __remove_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *pi)
455 {
456 	rb_erase(&pi->node, root);
457 	kfree(pi->name);
458 	kfree(pi);
459 }
460 
__decode_pool(void ** p,void * end,struct ceph_pg_pool_info * pi)461 static int __decode_pool(void **p, void *end, struct ceph_pg_pool_info *pi)
462 {
463 	unsigned n, m;
464 
465 	ceph_decode_copy(p, &pi->v, sizeof(pi->v));
466 	calc_pg_masks(pi);
467 
468 	/* num_snaps * snap_info_t */
469 	n = le32_to_cpu(pi->v.num_snaps);
470 	while (n--) {
471 		ceph_decode_need(p, end, sizeof(u64) + 1 + sizeof(u64) +
472 				 sizeof(struct ceph_timespec), bad);
473 		*p += sizeof(u64) +       /* key */
474 			1 + sizeof(u64) + /* u8, snapid */
475 			sizeof(struct ceph_timespec);
476 		m = ceph_decode_32(p);    /* snap name */
477 		*p += m;
478 	}
479 
480 	*p += le32_to_cpu(pi->v.num_removed_snap_intervals) * sizeof(u64) * 2;
481 	return 0;
482 
483 bad:
484 	return -EINVAL;
485 }
486 
__decode_pool_names(void ** p,void * end,struct ceph_osdmap * map)487 static int __decode_pool_names(void **p, void *end, struct ceph_osdmap *map)
488 {
489 	struct ceph_pg_pool_info *pi;
490 	u32 num, len, pool;
491 
492 	ceph_decode_32_safe(p, end, num, bad);
493 	dout(" %d pool names\n", num);
494 	while (num--) {
495 		ceph_decode_32_safe(p, end, pool, bad);
496 		ceph_decode_32_safe(p, end, len, bad);
497 		dout("  pool %d len %d\n", pool, len);
498 		ceph_decode_need(p, end, len, bad);
499 		pi = __lookup_pg_pool(&map->pg_pools, pool);
500 		if (pi) {
501 			char *name = kstrndup(*p, len, GFP_NOFS);
502 
503 			if (!name)
504 				return -ENOMEM;
505 			kfree(pi->name);
506 			pi->name = name;
507 			dout("  name is %s\n", pi->name);
508 		}
509 		*p += len;
510 	}
511 	return 0;
512 
513 bad:
514 	return -EINVAL;
515 }
516 
517 /*
518  * osd map
519  */
ceph_osdmap_destroy(struct ceph_osdmap * map)520 void ceph_osdmap_destroy(struct ceph_osdmap *map)
521 {
522 	dout("osdmap_destroy %p\n", map);
523 	if (map->crush)
524 		crush_destroy(map->crush);
525 	while (!RB_EMPTY_ROOT(&map->pg_temp)) {
526 		struct ceph_pg_mapping *pg =
527 			rb_entry(rb_first(&map->pg_temp),
528 				 struct ceph_pg_mapping, node);
529 		rb_erase(&pg->node, &map->pg_temp);
530 		kfree(pg);
531 	}
532 	while (!RB_EMPTY_ROOT(&map->pg_pools)) {
533 		struct ceph_pg_pool_info *pi =
534 			rb_entry(rb_first(&map->pg_pools),
535 				 struct ceph_pg_pool_info, node);
536 		__remove_pg_pool(&map->pg_pools, pi);
537 	}
538 	kfree(map->osd_state);
539 	kfree(map->osd_weight);
540 	kfree(map->osd_addr);
541 	kfree(map);
542 }
543 
544 /*
545  * adjust max osd value.  reallocate arrays.
546  */
osdmap_set_max_osd(struct ceph_osdmap * map,int max)547 static int osdmap_set_max_osd(struct ceph_osdmap *map, int max)
548 {
549 	u8 *state;
550 	struct ceph_entity_addr *addr;
551 	u32 *weight;
552 
553 	state = kcalloc(max, sizeof(*state), GFP_NOFS);
554 	addr = kcalloc(max, sizeof(*addr), GFP_NOFS);
555 	weight = kcalloc(max, sizeof(*weight), GFP_NOFS);
556 	if (state == NULL || addr == NULL || weight == NULL) {
557 		kfree(state);
558 		kfree(addr);
559 		kfree(weight);
560 		return -ENOMEM;
561 	}
562 
563 	/* copy old? */
564 	if (map->osd_state) {
565 		memcpy(state, map->osd_state, map->max_osd*sizeof(*state));
566 		memcpy(addr, map->osd_addr, map->max_osd*sizeof(*addr));
567 		memcpy(weight, map->osd_weight, map->max_osd*sizeof(*weight));
568 		kfree(map->osd_state);
569 		kfree(map->osd_addr);
570 		kfree(map->osd_weight);
571 	}
572 
573 	map->osd_state = state;
574 	map->osd_weight = weight;
575 	map->osd_addr = addr;
576 	map->max_osd = max;
577 	return 0;
578 }
579 
580 /*
581  * decode a full map.
582  */
osdmap_decode(void ** p,void * end)583 struct ceph_osdmap *osdmap_decode(void **p, void *end)
584 {
585 	struct ceph_osdmap *map;
586 	u16 version;
587 	u32 len, max, i;
588 	u8 ev;
589 	int err = -EINVAL;
590 	void *start = *p;
591 	struct ceph_pg_pool_info *pi;
592 
593 	dout("osdmap_decode %p to %p len %d\n", *p, end, (int)(end - *p));
594 
595 	map = kzalloc(sizeof(*map), GFP_NOFS);
596 	if (map == NULL)
597 		return ERR_PTR(-ENOMEM);
598 	map->pg_temp = RB_ROOT;
599 
600 	ceph_decode_16_safe(p, end, version, bad);
601 	if (version > CEPH_OSDMAP_VERSION) {
602 		pr_warning("got unknown v %d > %d of osdmap\n", version,
603 			   CEPH_OSDMAP_VERSION);
604 		goto bad;
605 	}
606 
607 	ceph_decode_need(p, end, 2*sizeof(u64)+6*sizeof(u32), bad);
608 	ceph_decode_copy(p, &map->fsid, sizeof(map->fsid));
609 	map->epoch = ceph_decode_32(p);
610 	ceph_decode_copy(p, &map->created, sizeof(map->created));
611 	ceph_decode_copy(p, &map->modified, sizeof(map->modified));
612 
613 	ceph_decode_32_safe(p, end, max, bad);
614 	while (max--) {
615 		ceph_decode_need(p, end, 4 + 1 + sizeof(pi->v), bad);
616 		err = -ENOMEM;
617 		pi = kzalloc(sizeof(*pi), GFP_NOFS);
618 		if (!pi)
619 			goto bad;
620 		pi->id = ceph_decode_32(p);
621 		err = -EINVAL;
622 		ev = ceph_decode_8(p); /* encoding version */
623 		if (ev > CEPH_PG_POOL_VERSION) {
624 			pr_warning("got unknown v %d > %d of ceph_pg_pool\n",
625 				   ev, CEPH_PG_POOL_VERSION);
626 			kfree(pi);
627 			goto bad;
628 		}
629 		err = __decode_pool(p, end, pi);
630 		if (err < 0) {
631 			kfree(pi);
632 			goto bad;
633 		}
634 		__insert_pg_pool(&map->pg_pools, pi);
635 	}
636 
637 	if (version >= 5) {
638 		err = __decode_pool_names(p, end, map);
639 		if (err < 0) {
640 			dout("fail to decode pool names");
641 			goto bad;
642 		}
643 	}
644 
645 	ceph_decode_32_safe(p, end, map->pool_max, bad);
646 
647 	ceph_decode_32_safe(p, end, map->flags, bad);
648 
649 	max = ceph_decode_32(p);
650 
651 	/* (re)alloc osd arrays */
652 	err = osdmap_set_max_osd(map, max);
653 	if (err < 0)
654 		goto bad;
655 	dout("osdmap_decode max_osd = %d\n", map->max_osd);
656 
657 	/* osds */
658 	err = -EINVAL;
659 	ceph_decode_need(p, end, 3*sizeof(u32) +
660 			 map->max_osd*(1 + sizeof(*map->osd_weight) +
661 				       sizeof(*map->osd_addr)), bad);
662 	*p += 4; /* skip length field (should match max) */
663 	ceph_decode_copy(p, map->osd_state, map->max_osd);
664 
665 	*p += 4; /* skip length field (should match max) */
666 	for (i = 0; i < map->max_osd; i++)
667 		map->osd_weight[i] = ceph_decode_32(p);
668 
669 	*p += 4; /* skip length field (should match max) */
670 	ceph_decode_copy(p, map->osd_addr, map->max_osd*sizeof(*map->osd_addr));
671 	for (i = 0; i < map->max_osd; i++)
672 		ceph_decode_addr(&map->osd_addr[i]);
673 
674 	/* pg_temp */
675 	ceph_decode_32_safe(p, end, len, bad);
676 	for (i = 0; i < len; i++) {
677 		int n, j;
678 		struct ceph_pg pgid;
679 		struct ceph_pg_mapping *pg;
680 
681 		ceph_decode_need(p, end, sizeof(u32) + sizeof(u64), bad);
682 		ceph_decode_copy(p, &pgid, sizeof(pgid));
683 		n = ceph_decode_32(p);
684 		err = -EINVAL;
685 		if (n > (UINT_MAX - sizeof(*pg)) / sizeof(u32))
686 			goto bad;
687 		ceph_decode_need(p, end, n * sizeof(u32), bad);
688 		err = -ENOMEM;
689 		pg = kmalloc(sizeof(*pg) + n*sizeof(u32), GFP_NOFS);
690 		if (!pg)
691 			goto bad;
692 		pg->pgid = pgid;
693 		pg->len = n;
694 		for (j = 0; j < n; j++)
695 			pg->osds[j] = ceph_decode_32(p);
696 
697 		err = __insert_pg_mapping(pg, &map->pg_temp);
698 		if (err)
699 			goto bad;
700 		dout(" added pg_temp %llx len %d\n", *(u64 *)&pgid, len);
701 	}
702 
703 	/* crush */
704 	ceph_decode_32_safe(p, end, len, bad);
705 	dout("osdmap_decode crush len %d from off 0x%x\n", len,
706 	     (int)(*p - start));
707 	ceph_decode_need(p, end, len, bad);
708 	map->crush = crush_decode(*p, end);
709 	*p += len;
710 	if (IS_ERR(map->crush)) {
711 		err = PTR_ERR(map->crush);
712 		map->crush = NULL;
713 		goto bad;
714 	}
715 
716 	/* ignore the rest of the map */
717 	*p = end;
718 
719 	dout("osdmap_decode done %p %p\n", *p, end);
720 	return map;
721 
722 bad:
723 	dout("osdmap_decode fail err %d\n", err);
724 	ceph_osdmap_destroy(map);
725 	return ERR_PTR(err);
726 }
727 
728 /*
729  * decode and apply an incremental map update.
730  */
osdmap_apply_incremental(void ** p,void * end,struct ceph_osdmap * map,struct ceph_messenger * msgr)731 struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end,
732 					     struct ceph_osdmap *map,
733 					     struct ceph_messenger *msgr)
734 {
735 	struct crush_map *newcrush = NULL;
736 	struct ceph_fsid fsid;
737 	u32 epoch = 0;
738 	struct ceph_timespec modified;
739 	u32 len, pool;
740 	__s32 new_pool_max, new_flags, max;
741 	void *start = *p;
742 	int err = -EINVAL;
743 	u16 version;
744 
745 	ceph_decode_16_safe(p, end, version, bad);
746 	if (version > CEPH_OSDMAP_INC_VERSION) {
747 		pr_warning("got unknown v %d > %d of inc osdmap\n", version,
748 			   CEPH_OSDMAP_INC_VERSION);
749 		goto bad;
750 	}
751 
752 	ceph_decode_need(p, end, sizeof(fsid)+sizeof(modified)+2*sizeof(u32),
753 			 bad);
754 	ceph_decode_copy(p, &fsid, sizeof(fsid));
755 	epoch = ceph_decode_32(p);
756 	BUG_ON(epoch != map->epoch+1);
757 	ceph_decode_copy(p, &modified, sizeof(modified));
758 	new_pool_max = ceph_decode_32(p);
759 	new_flags = ceph_decode_32(p);
760 
761 	/* full map? */
762 	ceph_decode_32_safe(p, end, len, bad);
763 	if (len > 0) {
764 		dout("apply_incremental full map len %d, %p to %p\n",
765 		     len, *p, end);
766 		return osdmap_decode(p, min(*p+len, end));
767 	}
768 
769 	/* new crush? */
770 	ceph_decode_32_safe(p, end, len, bad);
771 	if (len > 0) {
772 		dout("apply_incremental new crush map len %d, %p to %p\n",
773 		     len, *p, end);
774 		newcrush = crush_decode(*p, min(*p+len, end));
775 		if (IS_ERR(newcrush))
776 			return ERR_CAST(newcrush);
777 		*p += len;
778 	}
779 
780 	/* new flags? */
781 	if (new_flags >= 0)
782 		map->flags = new_flags;
783 	if (new_pool_max >= 0)
784 		map->pool_max = new_pool_max;
785 
786 	ceph_decode_need(p, end, 5*sizeof(u32), bad);
787 
788 	/* new max? */
789 	max = ceph_decode_32(p);
790 	if (max >= 0) {
791 		err = osdmap_set_max_osd(map, max);
792 		if (err < 0)
793 			goto bad;
794 	}
795 
796 	map->epoch++;
797 	map->modified = modified;
798 	if (newcrush) {
799 		if (map->crush)
800 			crush_destroy(map->crush);
801 		map->crush = newcrush;
802 		newcrush = NULL;
803 	}
804 
805 	/* new_pool */
806 	ceph_decode_32_safe(p, end, len, bad);
807 	while (len--) {
808 		__u8 ev;
809 		struct ceph_pg_pool_info *pi;
810 
811 		ceph_decode_32_safe(p, end, pool, bad);
812 		ceph_decode_need(p, end, 1 + sizeof(pi->v), bad);
813 		ev = ceph_decode_8(p);  /* encoding version */
814 		if (ev > CEPH_PG_POOL_VERSION) {
815 			pr_warning("got unknown v %d > %d of ceph_pg_pool\n",
816 				   ev, CEPH_PG_POOL_VERSION);
817 			err = -EINVAL;
818 			goto bad;
819 		}
820 		pi = __lookup_pg_pool(&map->pg_pools, pool);
821 		if (!pi) {
822 			pi = kzalloc(sizeof(*pi), GFP_NOFS);
823 			if (!pi) {
824 				err = -ENOMEM;
825 				goto bad;
826 			}
827 			pi->id = pool;
828 			__insert_pg_pool(&map->pg_pools, pi);
829 		}
830 		err = __decode_pool(p, end, pi);
831 		if (err < 0)
832 			goto bad;
833 	}
834 	if (version >= 5) {
835 		err = __decode_pool_names(p, end, map);
836 		if (err < 0)
837 			goto bad;
838 	}
839 
840 	/* old_pool */
841 	ceph_decode_32_safe(p, end, len, bad);
842 	while (len--) {
843 		struct ceph_pg_pool_info *pi;
844 
845 		ceph_decode_32_safe(p, end, pool, bad);
846 		pi = __lookup_pg_pool(&map->pg_pools, pool);
847 		if (pi)
848 			__remove_pg_pool(&map->pg_pools, pi);
849 	}
850 
851 	/* new_up */
852 	err = -EINVAL;
853 	ceph_decode_32_safe(p, end, len, bad);
854 	while (len--) {
855 		u32 osd;
856 		struct ceph_entity_addr addr;
857 		ceph_decode_32_safe(p, end, osd, bad);
858 		ceph_decode_copy_safe(p, end, &addr, sizeof(addr), bad);
859 		ceph_decode_addr(&addr);
860 		pr_info("osd%d up\n", osd);
861 		BUG_ON(osd >= map->max_osd);
862 		map->osd_state[osd] |= CEPH_OSD_UP;
863 		map->osd_addr[osd] = addr;
864 	}
865 
866 	/* new_state */
867 	ceph_decode_32_safe(p, end, len, bad);
868 	while (len--) {
869 		u32 osd;
870 		u8 xorstate;
871 		ceph_decode_32_safe(p, end, osd, bad);
872 		xorstate = **(u8 **)p;
873 		(*p)++;  /* clean flag */
874 		if (xorstate == 0)
875 			xorstate = CEPH_OSD_UP;
876 		if (xorstate & CEPH_OSD_UP)
877 			pr_info("osd%d down\n", osd);
878 		if (osd < map->max_osd)
879 			map->osd_state[osd] ^= xorstate;
880 	}
881 
882 	/* new_weight */
883 	ceph_decode_32_safe(p, end, len, bad);
884 	while (len--) {
885 		u32 osd, off;
886 		ceph_decode_need(p, end, sizeof(u32)*2, bad);
887 		osd = ceph_decode_32(p);
888 		off = ceph_decode_32(p);
889 		pr_info("osd%d weight 0x%x %s\n", osd, off,
890 		     off == CEPH_OSD_IN ? "(in)" :
891 		     (off == CEPH_OSD_OUT ? "(out)" : ""));
892 		if (osd < map->max_osd)
893 			map->osd_weight[osd] = off;
894 	}
895 
896 	/* new_pg_temp */
897 	ceph_decode_32_safe(p, end, len, bad);
898 	while (len--) {
899 		struct ceph_pg_mapping *pg;
900 		int j;
901 		struct ceph_pg pgid;
902 		u32 pglen;
903 		ceph_decode_need(p, end, sizeof(u64) + sizeof(u32), bad);
904 		ceph_decode_copy(p, &pgid, sizeof(pgid));
905 		pglen = ceph_decode_32(p);
906 
907 		if (pglen) {
908 			ceph_decode_need(p, end, pglen*sizeof(u32), bad);
909 
910 			/* removing existing (if any) */
911 			(void) __remove_pg_mapping(&map->pg_temp, pgid);
912 
913 			/* insert */
914 			err = -EINVAL;
915 			if (pglen > (UINT_MAX - sizeof(*pg)) / sizeof(u32))
916 				goto bad;
917 			err = -ENOMEM;
918 			pg = kmalloc(sizeof(*pg) + sizeof(u32)*pglen, GFP_NOFS);
919 			if (!pg)
920 				goto bad;
921 			pg->pgid = pgid;
922 			pg->len = pglen;
923 			for (j = 0; j < pglen; j++)
924 				pg->osds[j] = ceph_decode_32(p);
925 			err = __insert_pg_mapping(pg, &map->pg_temp);
926 			if (err) {
927 				kfree(pg);
928 				goto bad;
929 			}
930 			dout(" added pg_temp %llx len %d\n", *(u64 *)&pgid,
931 			     pglen);
932 		} else {
933 			/* remove */
934 			__remove_pg_mapping(&map->pg_temp, pgid);
935 		}
936 	}
937 
938 	/* ignore the rest */
939 	*p = end;
940 	return map;
941 
942 bad:
943 	pr_err("corrupt inc osdmap epoch %d off %d (%p of %p-%p)\n",
944 	       epoch, (int)(*p - start), *p, start, end);
945 	print_hex_dump(KERN_DEBUG, "osdmap: ",
946 		       DUMP_PREFIX_OFFSET, 16, 1,
947 		       start, end - start, true);
948 	if (newcrush)
949 		crush_destroy(newcrush);
950 	return ERR_PTR(err);
951 }
952 
953 
954 
955 
956 /*
957  * calculate file layout from given offset, length.
958  * fill in correct oid, logical length, and object extent
959  * offset, length.
960  *
961  * for now, we write only a single su, until we can
962  * pass a stride back to the caller.
963  */
ceph_calc_file_object_mapping(struct ceph_file_layout * layout,u64 off,u64 * plen,u64 * ono,u64 * oxoff,u64 * oxlen)964 int ceph_calc_file_object_mapping(struct ceph_file_layout *layout,
965 				   u64 off, u64 *plen,
966 				   u64 *ono,
967 				   u64 *oxoff, u64 *oxlen)
968 {
969 	u32 osize = le32_to_cpu(layout->fl_object_size);
970 	u32 su = le32_to_cpu(layout->fl_stripe_unit);
971 	u32 sc = le32_to_cpu(layout->fl_stripe_count);
972 	u32 bl, stripeno, stripepos, objsetno;
973 	u32 su_per_object;
974 	u64 t, su_offset;
975 
976 	dout("mapping %llu~%llu  osize %u fl_su %u\n", off, *plen,
977 	     osize, su);
978 	if (su == 0 || sc == 0)
979 		goto invalid;
980 	su_per_object = osize / su;
981 	if (su_per_object == 0)
982 		goto invalid;
983 	dout("osize %u / su %u = su_per_object %u\n", osize, su,
984 	     su_per_object);
985 
986 	if ((su & ~PAGE_MASK) != 0)
987 		goto invalid;
988 
989 	/* bl = *off / su; */
990 	t = off;
991 	do_div(t, su);
992 	bl = t;
993 	dout("off %llu / su %u = bl %u\n", off, su, bl);
994 
995 	stripeno = bl / sc;
996 	stripepos = bl % sc;
997 	objsetno = stripeno / su_per_object;
998 
999 	*ono = objsetno * sc + stripepos;
1000 	dout("objset %u * sc %u = ono %u\n", objsetno, sc, (unsigned)*ono);
1001 
1002 	/* *oxoff = *off % layout->fl_stripe_unit;  # offset in su */
1003 	t = off;
1004 	su_offset = do_div(t, su);
1005 	*oxoff = su_offset + (stripeno % su_per_object) * su;
1006 
1007 	/*
1008 	 * Calculate the length of the extent being written to the selected
1009 	 * object. This is the minimum of the full length requested (plen) or
1010 	 * the remainder of the current stripe being written to.
1011 	 */
1012 	*oxlen = min_t(u64, *plen, su - su_offset);
1013 	*plen = *oxlen;
1014 
1015 	dout(" obj extent %llu~%llu\n", *oxoff, *oxlen);
1016 	return 0;
1017 
1018 invalid:
1019 	dout(" invalid layout\n");
1020 	*ono = 0;
1021 	*oxoff = 0;
1022 	*oxlen = 0;
1023 	return -EINVAL;
1024 }
1025 EXPORT_SYMBOL(ceph_calc_file_object_mapping);
1026 
1027 /*
1028  * calculate an object layout (i.e. pgid) from an oid,
1029  * file_layout, and osdmap
1030  */
ceph_calc_object_layout(struct ceph_object_layout * ol,const char * oid,struct ceph_file_layout * fl,struct ceph_osdmap * osdmap)1031 int ceph_calc_object_layout(struct ceph_object_layout *ol,
1032 			    const char *oid,
1033 			    struct ceph_file_layout *fl,
1034 			    struct ceph_osdmap *osdmap)
1035 {
1036 	unsigned num, num_mask;
1037 	struct ceph_pg pgid;
1038 	s32 preferred = (s32)le32_to_cpu(fl->fl_pg_preferred);
1039 	int poolid = le32_to_cpu(fl->fl_pg_pool);
1040 	struct ceph_pg_pool_info *pool;
1041 	unsigned ps;
1042 
1043 	BUG_ON(!osdmap);
1044 
1045 	pool = __lookup_pg_pool(&osdmap->pg_pools, poolid);
1046 	if (!pool)
1047 		return -EIO;
1048 	ps = ceph_str_hash(pool->v.object_hash, oid, strlen(oid));
1049 	if (preferred >= 0) {
1050 		ps += preferred;
1051 		num = le32_to_cpu(pool->v.lpg_num);
1052 		num_mask = pool->lpg_num_mask;
1053 	} else {
1054 		num = le32_to_cpu(pool->v.pg_num);
1055 		num_mask = pool->pg_num_mask;
1056 	}
1057 
1058 	pgid.ps = cpu_to_le16(ps);
1059 	pgid.preferred = cpu_to_le16(preferred);
1060 	pgid.pool = fl->fl_pg_pool;
1061 	if (preferred >= 0)
1062 		dout("calc_object_layout '%s' pgid %d.%xp%d\n", oid, poolid, ps,
1063 		     (int)preferred);
1064 	else
1065 		dout("calc_object_layout '%s' pgid %d.%x\n", oid, poolid, ps);
1066 
1067 	ol->ol_pgid = pgid;
1068 	ol->ol_stripe_unit = fl->fl_object_stripe_unit;
1069 	return 0;
1070 }
1071 EXPORT_SYMBOL(ceph_calc_object_layout);
1072 
1073 /*
1074  * Calculate raw osd vector for the given pgid.  Return pointer to osd
1075  * array, or NULL on failure.
1076  */
calc_pg_raw(struct ceph_osdmap * osdmap,struct ceph_pg pgid,int * osds,int * num)1077 static int *calc_pg_raw(struct ceph_osdmap *osdmap, struct ceph_pg pgid,
1078 			int *osds, int *num)
1079 {
1080 	struct ceph_pg_mapping *pg;
1081 	struct ceph_pg_pool_info *pool;
1082 	int ruleno;
1083 	unsigned poolid, ps, pps, t;
1084 	int preferred;
1085 
1086 	poolid = le32_to_cpu(pgid.pool);
1087 	ps = le16_to_cpu(pgid.ps);
1088 	preferred = (s16)le16_to_cpu(pgid.preferred);
1089 
1090 	pool = __lookup_pg_pool(&osdmap->pg_pools, poolid);
1091 	if (!pool)
1092 		return NULL;
1093 
1094 	/* pg_temp? */
1095 	if (preferred >= 0)
1096 		t = ceph_stable_mod(ps, le32_to_cpu(pool->v.lpg_num),
1097 				    pool->lpgp_num_mask);
1098 	else
1099 		t = ceph_stable_mod(ps, le32_to_cpu(pool->v.pg_num),
1100 				    pool->pgp_num_mask);
1101 	pgid.ps = cpu_to_le16(t);
1102 	pg = __lookup_pg_mapping(&osdmap->pg_temp, pgid);
1103 	if (pg) {
1104 		*num = pg->len;
1105 		return pg->osds;
1106 	}
1107 
1108 	/* crush */
1109 	ruleno = crush_find_rule(osdmap->crush, pool->v.crush_ruleset,
1110 				 pool->v.type, pool->v.size);
1111 	if (ruleno < 0) {
1112 		pr_err("no crush rule pool %d ruleset %d type %d size %d\n",
1113 		       poolid, pool->v.crush_ruleset, pool->v.type,
1114 		       pool->v.size);
1115 		return NULL;
1116 	}
1117 
1118 	/* don't forcefeed bad device ids to crush */
1119 	if (preferred >= osdmap->max_osd ||
1120 	    preferred >= osdmap->crush->max_devices)
1121 		preferred = -1;
1122 
1123 	if (preferred >= 0)
1124 		pps = ceph_stable_mod(ps,
1125 				      le32_to_cpu(pool->v.lpgp_num),
1126 				      pool->lpgp_num_mask);
1127 	else
1128 		pps = ceph_stable_mod(ps,
1129 				      le32_to_cpu(pool->v.pgp_num),
1130 				      pool->pgp_num_mask);
1131 	pps += poolid;
1132 	*num = crush_do_rule(osdmap->crush, ruleno, pps, osds,
1133 			     min_t(int, pool->v.size, *num),
1134 			     preferred, osdmap->osd_weight);
1135 	return osds;
1136 }
1137 
1138 /*
1139  * Return acting set for given pgid.
1140  */
ceph_calc_pg_acting(struct ceph_osdmap * osdmap,struct ceph_pg pgid,int * acting)1141 int ceph_calc_pg_acting(struct ceph_osdmap *osdmap, struct ceph_pg pgid,
1142 			int *acting)
1143 {
1144 	int rawosds[CEPH_PG_MAX_SIZE], *osds;
1145 	int i, o, num = CEPH_PG_MAX_SIZE;
1146 
1147 	osds = calc_pg_raw(osdmap, pgid, rawosds, &num);
1148 	if (!osds)
1149 		return -1;
1150 
1151 	/* primary is first up osd */
1152 	o = 0;
1153 	for (i = 0; i < num; i++)
1154 		if (ceph_osd_is_up(osdmap, osds[i]))
1155 			acting[o++] = osds[i];
1156 	return o;
1157 }
1158 
1159 /*
1160  * Return primary osd for given pgid, or -1 if none.
1161  */
ceph_calc_pg_primary(struct ceph_osdmap * osdmap,struct ceph_pg pgid)1162 int ceph_calc_pg_primary(struct ceph_osdmap *osdmap, struct ceph_pg pgid)
1163 {
1164 	int rawosds[CEPH_PG_MAX_SIZE], *osds;
1165 	int i, num = CEPH_PG_MAX_SIZE;
1166 
1167 	osds = calc_pg_raw(osdmap, pgid, rawosds, &num);
1168 	if (!osds)
1169 		return -1;
1170 
1171 	/* primary is first up osd */
1172 	for (i = 0; i < num; i++)
1173 		if (ceph_osd_is_up(osdmap, osds[i]))
1174 			return osds[i];
1175 	return -1;
1176 }
1177 EXPORT_SYMBOL(ceph_calc_pg_primary);
1178