ceph: fix msgpool reservation leak
[linux-2.6-block.git] / fs / ceph / osdmap.c
CommitLineData
f24e9980
SW
1
2#include <asm/div64.h>
3
4#include "super.h"
5#include "osdmap.h"
6#include "crush/hash.h"
7#include "crush/mapper.h"
8#include "decode.h"
9#include "ceph_debug.h"
10
11char *ceph_osdmap_state_str(char *str, int len, int state)
12{
13 int flag = 0;
14
15 if (!len)
16 goto done;
17
18 *str = '\0';
19 if (state) {
20 if (state & CEPH_OSD_EXISTS) {
21 snprintf(str, len, "exists");
22 flag = 1;
23 }
24 if (state & CEPH_OSD_UP) {
25 snprintf(str, len, "%s%s%s", str, (flag ? ", " : ""),
26 "up");
27 flag = 1;
28 }
29 } else {
30 snprintf(str, len, "doesn't exist");
31 }
32done:
33 return str;
34}
35
36/* maps */
37
38static int calc_bits_of(unsigned t)
39{
40 int b = 0;
41 while (t) {
42 t = t >> 1;
43 b++;
44 }
45 return b;
46}
47
48/*
49 * the foo_mask is the smallest value 2^n-1 that is >= foo.
50 */
51static void calc_pg_masks(struct ceph_pg_pool_info *pi)
52{
53 pi->pg_num_mask = (1 << calc_bits_of(le32_to_cpu(pi->v.pg_num)-1)) - 1;
54 pi->pgp_num_mask =
55 (1 << calc_bits_of(le32_to_cpu(pi->v.pgp_num)-1)) - 1;
56 pi->lpg_num_mask =
57 (1 << calc_bits_of(le32_to_cpu(pi->v.lpg_num)-1)) - 1;
58 pi->lpgp_num_mask =
59 (1 << calc_bits_of(le32_to_cpu(pi->v.lpgp_num)-1)) - 1;
60}
61
62/*
63 * decode crush map
64 */
65static int crush_decode_uniform_bucket(void **p, void *end,
66 struct crush_bucket_uniform *b)
67{
68 dout("crush_decode_uniform_bucket %p to %p\n", *p, end);
69 ceph_decode_need(p, end, (1+b->h.size) * sizeof(u32), bad);
c89136ea 70 b->item_weight = ceph_decode_32(p);
f24e9980
SW
71 return 0;
72bad:
73 return -EINVAL;
74}
75
76static int crush_decode_list_bucket(void **p, void *end,
77 struct crush_bucket_list *b)
78{
79 int j;
80 dout("crush_decode_list_bucket %p to %p\n", *p, end);
81 b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
82 if (b->item_weights == NULL)
83 return -ENOMEM;
84 b->sum_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
85 if (b->sum_weights == NULL)
86 return -ENOMEM;
87 ceph_decode_need(p, end, 2 * b->h.size * sizeof(u32), bad);
88 for (j = 0; j < b->h.size; j++) {
c89136ea
SW
89 b->item_weights[j] = ceph_decode_32(p);
90 b->sum_weights[j] = ceph_decode_32(p);
f24e9980
SW
91 }
92 return 0;
93bad:
94 return -EINVAL;
95}
96
97static int crush_decode_tree_bucket(void **p, void *end,
98 struct crush_bucket_tree *b)
99{
100 int j;
101 dout("crush_decode_tree_bucket %p to %p\n", *p, end);
102 ceph_decode_32_safe(p, end, b->num_nodes, bad);
103 b->node_weights = kcalloc(b->num_nodes, sizeof(u32), GFP_NOFS);
104 if (b->node_weights == NULL)
105 return -ENOMEM;
106 ceph_decode_need(p, end, b->num_nodes * sizeof(u32), bad);
107 for (j = 0; j < b->num_nodes; j++)
c89136ea 108 b->node_weights[j] = ceph_decode_32(p);
f24e9980
SW
109 return 0;
110bad:
111 return -EINVAL;
112}
113
114static int crush_decode_straw_bucket(void **p, void *end,
115 struct crush_bucket_straw *b)
116{
117 int j;
118 dout("crush_decode_straw_bucket %p to %p\n", *p, end);
119 b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
120 if (b->item_weights == NULL)
121 return -ENOMEM;
122 b->straws = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
123 if (b->straws == NULL)
124 return -ENOMEM;
125 ceph_decode_need(p, end, 2 * b->h.size * sizeof(u32), bad);
126 for (j = 0; j < b->h.size; j++) {
c89136ea
SW
127 b->item_weights[j] = ceph_decode_32(p);
128 b->straws[j] = ceph_decode_32(p);
f24e9980
SW
129 }
130 return 0;
131bad:
132 return -EINVAL;
133}
134
135static struct crush_map *crush_decode(void *pbyval, void *end)
136{
137 struct crush_map *c;
138 int err = -EINVAL;
139 int i, j;
140 void **p = &pbyval;
141 void *start = pbyval;
142 u32 magic;
143
144 dout("crush_decode %p to %p len %d\n", *p, end, (int)(end - *p));
145
146 c = kzalloc(sizeof(*c), GFP_NOFS);
147 if (c == NULL)
148 return ERR_PTR(-ENOMEM);
149
150 ceph_decode_need(p, end, 4*sizeof(u32), bad);
c89136ea 151 magic = ceph_decode_32(p);
f24e9980
SW
152 if (magic != CRUSH_MAGIC) {
153 pr_err("crush_decode magic %x != current %x\n",
154 (unsigned)magic, (unsigned)CRUSH_MAGIC);
155 goto bad;
156 }
c89136ea
SW
157 c->max_buckets = ceph_decode_32(p);
158 c->max_rules = ceph_decode_32(p);
159 c->max_devices = ceph_decode_32(p);
f24e9980
SW
160
161 c->device_parents = kcalloc(c->max_devices, sizeof(u32), GFP_NOFS);
162 if (c->device_parents == NULL)
163 goto badmem;
164 c->bucket_parents = kcalloc(c->max_buckets, sizeof(u32), GFP_NOFS);
165 if (c->bucket_parents == NULL)
166 goto badmem;
167
168 c->buckets = kcalloc(c->max_buckets, sizeof(*c->buckets), GFP_NOFS);
169 if (c->buckets == NULL)
170 goto badmem;
171 c->rules = kcalloc(c->max_rules, sizeof(*c->rules), GFP_NOFS);
172 if (c->rules == NULL)
173 goto badmem;
174
175 /* buckets */
176 for (i = 0; i < c->max_buckets; i++) {
177 int size = 0;
178 u32 alg;
179 struct crush_bucket *b;
180
181 ceph_decode_32_safe(p, end, alg, bad);
182 if (alg == 0) {
183 c->buckets[i] = NULL;
184 continue;
185 }
186 dout("crush_decode bucket %d off %x %p to %p\n",
187 i, (int)(*p-start), *p, end);
188
189 switch (alg) {
190 case CRUSH_BUCKET_UNIFORM:
191 size = sizeof(struct crush_bucket_uniform);
192 break;
193 case CRUSH_BUCKET_LIST:
194 size = sizeof(struct crush_bucket_list);
195 break;
196 case CRUSH_BUCKET_TREE:
197 size = sizeof(struct crush_bucket_tree);
198 break;
199 case CRUSH_BUCKET_STRAW:
200 size = sizeof(struct crush_bucket_straw);
201 break;
202 default:
203 goto bad;
204 }
205 BUG_ON(size == 0);
206 b = c->buckets[i] = kzalloc(size, GFP_NOFS);
207 if (b == NULL)
208 goto badmem;
209
210 ceph_decode_need(p, end, 4*sizeof(u32), bad);
c89136ea
SW
211 b->id = ceph_decode_32(p);
212 b->type = ceph_decode_16(p);
fb690390
SW
213 b->alg = ceph_decode_8(p);
214 b->hash = ceph_decode_8(p);
c89136ea
SW
215 b->weight = ceph_decode_32(p);
216 b->size = ceph_decode_32(p);
f24e9980
SW
217
218 dout("crush_decode bucket size %d off %x %p to %p\n",
219 b->size, (int)(*p-start), *p, end);
220
221 b->items = kcalloc(b->size, sizeof(__s32), GFP_NOFS);
222 if (b->items == NULL)
223 goto badmem;
224 b->perm = kcalloc(b->size, sizeof(u32), GFP_NOFS);
225 if (b->perm == NULL)
226 goto badmem;
227 b->perm_n = 0;
228
229 ceph_decode_need(p, end, b->size*sizeof(u32), bad);
230 for (j = 0; j < b->size; j++)
c89136ea 231 b->items[j] = ceph_decode_32(p);
f24e9980
SW
232
233 switch (b->alg) {
234 case CRUSH_BUCKET_UNIFORM:
235 err = crush_decode_uniform_bucket(p, end,
236 (struct crush_bucket_uniform *)b);
237 if (err < 0)
238 goto bad;
239 break;
240 case CRUSH_BUCKET_LIST:
241 err = crush_decode_list_bucket(p, end,
242 (struct crush_bucket_list *)b);
243 if (err < 0)
244 goto bad;
245 break;
246 case CRUSH_BUCKET_TREE:
247 err = crush_decode_tree_bucket(p, end,
248 (struct crush_bucket_tree *)b);
249 if (err < 0)
250 goto bad;
251 break;
252 case CRUSH_BUCKET_STRAW:
253 err = crush_decode_straw_bucket(p, end,
254 (struct crush_bucket_straw *)b);
255 if (err < 0)
256 goto bad;
257 break;
258 }
259 }
260
261 /* rules */
262 dout("rule vec is %p\n", c->rules);
263 for (i = 0; i < c->max_rules; i++) {
264 u32 yes;
265 struct crush_rule *r;
266
267 ceph_decode_32_safe(p, end, yes, bad);
268 if (!yes) {
269 dout("crush_decode NO rule %d off %x %p to %p\n",
270 i, (int)(*p-start), *p, end);
271 c->rules[i] = NULL;
272 continue;
273 }
274
275 dout("crush_decode rule %d off %x %p to %p\n",
276 i, (int)(*p-start), *p, end);
277
278 /* len */
279 ceph_decode_32_safe(p, end, yes, bad);
280#if BITS_PER_LONG == 32
281 if (yes > ULONG_MAX / sizeof(struct crush_rule_step))
282 goto bad;
283#endif
284 r = c->rules[i] = kmalloc(sizeof(*r) +
285 yes*sizeof(struct crush_rule_step),
286 GFP_NOFS);
287 if (r == NULL)
288 goto badmem;
289 dout(" rule %d is at %p\n", i, r);
290 r->len = yes;
291 ceph_decode_copy_safe(p, end, &r->mask, 4, bad); /* 4 u8's */
292 ceph_decode_need(p, end, r->len*3*sizeof(u32), bad);
293 for (j = 0; j < r->len; j++) {
c89136ea
SW
294 r->steps[j].op = ceph_decode_32(p);
295 r->steps[j].arg1 = ceph_decode_32(p);
296 r->steps[j].arg2 = ceph_decode_32(p);
f24e9980
SW
297 }
298 }
299
300 /* ignore trailing name maps. */
301
302 dout("crush_decode success\n");
303 return c;
304
305badmem:
306 err = -ENOMEM;
307bad:
308 dout("crush_decode fail %d\n", err);
309 crush_destroy(c);
310 return ERR_PTR(err);
311}
312
313
314/*
315 * osd map
316 */
317void ceph_osdmap_destroy(struct ceph_osdmap *map)
318{
319 dout("osdmap_destroy %p\n", map);
320 if (map->crush)
321 crush_destroy(map->crush);
322 while (!RB_EMPTY_ROOT(&map->pg_temp))
323 rb_erase(rb_first(&map->pg_temp), &map->pg_temp);
324 kfree(map->osd_state);
325 kfree(map->osd_weight);
326 kfree(map->pg_pool);
327 kfree(map->osd_addr);
328 kfree(map);
329}
330
331/*
332 * adjust max osd value. reallocate arrays.
333 */
334static int osdmap_set_max_osd(struct ceph_osdmap *map, int max)
335{
336 u8 *state;
337 struct ceph_entity_addr *addr;
338 u32 *weight;
339
340 state = kcalloc(max, sizeof(*state), GFP_NOFS);
341 addr = kcalloc(max, sizeof(*addr), GFP_NOFS);
342 weight = kcalloc(max, sizeof(*weight), GFP_NOFS);
343 if (state == NULL || addr == NULL || weight == NULL) {
344 kfree(state);
345 kfree(addr);
346 kfree(weight);
347 return -ENOMEM;
348 }
349
350 /* copy old? */
351 if (map->osd_state) {
352 memcpy(state, map->osd_state, map->max_osd*sizeof(*state));
353 memcpy(addr, map->osd_addr, map->max_osd*sizeof(*addr));
354 memcpy(weight, map->osd_weight, map->max_osd*sizeof(*weight));
355 kfree(map->osd_state);
356 kfree(map->osd_addr);
357 kfree(map->osd_weight);
358 }
359
360 map->osd_state = state;
361 map->osd_weight = weight;
362 map->osd_addr = addr;
363 map->max_osd = max;
364 return 0;
365}
366
367/*
368 * Insert a new pg_temp mapping
369 */
51042122
SW
370static int pgid_cmp(struct ceph_pg l, struct ceph_pg r)
371{
372 u64 a = *(u64 *)&l;
373 u64 b = *(u64 *)&r;
374
375 if (a < b)
376 return -1;
377 if (a > b)
378 return 1;
379 return 0;
380}
381
991abb6e
SW
382static int __insert_pg_mapping(struct ceph_pg_mapping *new,
383 struct rb_root *root)
f24e9980
SW
384{
385 struct rb_node **p = &root->rb_node;
386 struct rb_node *parent = NULL;
387 struct ceph_pg_mapping *pg = NULL;
51042122 388 int c;
f24e9980
SW
389
390 while (*p) {
391 parent = *p;
392 pg = rb_entry(parent, struct ceph_pg_mapping, node);
51042122
SW
393 c = pgid_cmp(new->pgid, pg->pgid);
394 if (c < 0)
f24e9980 395 p = &(*p)->rb_left;
51042122 396 else if (c > 0)
f24e9980
SW
397 p = &(*p)->rb_right;
398 else
991abb6e 399 return -EEXIST;
f24e9980
SW
400 }
401
402 rb_link_node(&new->node, parent, p);
403 rb_insert_color(&new->node, root);
991abb6e 404 return 0;
f24e9980
SW
405}
406
407/*
408 * decode a full map.
409 */
410struct ceph_osdmap *osdmap_decode(void **p, void *end)
411{
412 struct ceph_osdmap *map;
413 u16 version;
414 u32 len, max, i;
415 int err = -EINVAL;
416 void *start = *p;
417
418 dout("osdmap_decode %p to %p len %d\n", *p, end, (int)(end - *p));
419
420 map = kzalloc(sizeof(*map), GFP_NOFS);
421 if (map == NULL)
422 return ERR_PTR(-ENOMEM);
423 map->pg_temp = RB_ROOT;
424
425 ceph_decode_16_safe(p, end, version, bad);
426
427 ceph_decode_need(p, end, 2*sizeof(u64)+6*sizeof(u32), bad);
428 ceph_decode_copy(p, &map->fsid, sizeof(map->fsid));
c89136ea 429 map->epoch = ceph_decode_32(p);
f24e9980
SW
430 ceph_decode_copy(p, &map->created, sizeof(map->created));
431 ceph_decode_copy(p, &map->modified, sizeof(map->modified));
432
c89136ea 433 map->num_pools = ceph_decode_32(p);
f24e9980
SW
434 map->pg_pool = kcalloc(map->num_pools, sizeof(*map->pg_pool),
435 GFP_NOFS);
436 if (!map->pg_pool) {
437 err = -ENOMEM;
438 goto bad;
439 }
440 ceph_decode_32_safe(p, end, max, bad);
441 while (max--) {
442 ceph_decode_need(p, end, 4+sizeof(map->pg_pool->v), bad);
c89136ea 443 i = ceph_decode_32(p);
f24e9980
SW
444 if (i >= map->num_pools)
445 goto bad;
446 ceph_decode_copy(p, &map->pg_pool[i].v,
447 sizeof(map->pg_pool->v));
448 calc_pg_masks(&map->pg_pool[i]);
449 p += le32_to_cpu(map->pg_pool[i].v.num_snaps) * sizeof(u64);
450 p += le32_to_cpu(map->pg_pool[i].v.num_removed_snap_intervals)
451 * sizeof(u64) * 2;
452 }
453
454 ceph_decode_32_safe(p, end, map->flags, bad);
455
c89136ea 456 max = ceph_decode_32(p);
f24e9980
SW
457
458 /* (re)alloc osd arrays */
459 err = osdmap_set_max_osd(map, max);
460 if (err < 0)
461 goto bad;
462 dout("osdmap_decode max_osd = %d\n", map->max_osd);
463
464 /* osds */
465 err = -EINVAL;
466 ceph_decode_need(p, end, 3*sizeof(u32) +
467 map->max_osd*(1 + sizeof(*map->osd_weight) +
468 sizeof(*map->osd_addr)), bad);
469 *p += 4; /* skip length field (should match max) */
470 ceph_decode_copy(p, map->osd_state, map->max_osd);
471
472 *p += 4; /* skip length field (should match max) */
473 for (i = 0; i < map->max_osd; i++)
c89136ea 474 map->osd_weight[i] = ceph_decode_32(p);
f24e9980
SW
475
476 *p += 4; /* skip length field (should match max) */
477 ceph_decode_copy(p, map->osd_addr, map->max_osd*sizeof(*map->osd_addr));
63f2d211
SW
478 for (i = 0; i < map->max_osd; i++)
479 ceph_decode_addr(&map->osd_addr[i]);
f24e9980
SW
480
481 /* pg_temp */
482 ceph_decode_32_safe(p, end, len, bad);
483 for (i = 0; i < len; i++) {
484 int n, j;
51042122 485 struct ceph_pg pgid;
f24e9980
SW
486 struct ceph_pg_mapping *pg;
487
488 ceph_decode_need(p, end, sizeof(u32) + sizeof(u64), bad);
51042122 489 ceph_decode_copy(p, &pgid, sizeof(pgid));
c89136ea 490 n = ceph_decode_32(p);
f24e9980
SW
491 ceph_decode_need(p, end, n * sizeof(u32), bad);
492 pg = kmalloc(sizeof(*pg) + n*sizeof(u32), GFP_NOFS);
493 if (!pg) {
494 err = -ENOMEM;
495 goto bad;
496 }
497 pg->pgid = pgid;
498 pg->len = n;
499 for (j = 0; j < n; j++)
c89136ea 500 pg->osds[j] = ceph_decode_32(p);
f24e9980 501
991abb6e
SW
502 err = __insert_pg_mapping(pg, &map->pg_temp);
503 if (err)
504 goto bad;
51042122 505 dout(" added pg_temp %llx len %d\n", *(u64 *)&pgid, len);
f24e9980
SW
506 }
507
508 /* crush */
509 ceph_decode_32_safe(p, end, len, bad);
510 dout("osdmap_decode crush len %d from off 0x%x\n", len,
511 (int)(*p - start));
512 ceph_decode_need(p, end, len, bad);
513 map->crush = crush_decode(*p, end);
514 *p += len;
515 if (IS_ERR(map->crush)) {
516 err = PTR_ERR(map->crush);
517 map->crush = NULL;
518 goto bad;
519 }
520
521 /* ignore the rest of the map */
522 *p = end;
523
524 dout("osdmap_decode done %p %p\n", *p, end);
525 return map;
526
527bad:
528 dout("osdmap_decode fail\n");
529 ceph_osdmap_destroy(map);
530 return ERR_PTR(err);
531}
532
533/*
534 * decode and apply an incremental map update.
535 */
536struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end,
537 struct ceph_osdmap *map,
538 struct ceph_messenger *msgr)
539{
540 struct ceph_osdmap *newmap = map;
541 struct crush_map *newcrush = NULL;
542 struct ceph_fsid fsid;
543 u32 epoch = 0;
544 struct ceph_timespec modified;
545 u32 len, pool;
546 __s32 new_flags, max;
547 void *start = *p;
548 int err = -EINVAL;
549 u16 version;
550 struct rb_node *rbp;
551
552 ceph_decode_16_safe(p, end, version, bad);
553
554 ceph_decode_need(p, end, sizeof(fsid)+sizeof(modified)+2*sizeof(u32),
555 bad);
556 ceph_decode_copy(p, &fsid, sizeof(fsid));
c89136ea 557 epoch = ceph_decode_32(p);
f24e9980
SW
558 BUG_ON(epoch != map->epoch+1);
559 ceph_decode_copy(p, &modified, sizeof(modified));
c89136ea 560 new_flags = ceph_decode_32(p);
f24e9980
SW
561
562 /* full map? */
563 ceph_decode_32_safe(p, end, len, bad);
564 if (len > 0) {
565 dout("apply_incremental full map len %d, %p to %p\n",
566 len, *p, end);
567 newmap = osdmap_decode(p, min(*p+len, end));
568 return newmap; /* error or not */
569 }
570
571 /* new crush? */
572 ceph_decode_32_safe(p, end, len, bad);
573 if (len > 0) {
574 dout("apply_incremental new crush map len %d, %p to %p\n",
575 len, *p, end);
576 newcrush = crush_decode(*p, min(*p+len, end));
577 if (IS_ERR(newcrush))
578 return ERR_PTR(PTR_ERR(newcrush));
579 }
580
581 /* new flags? */
582 if (new_flags >= 0)
583 map->flags = new_flags;
584
585 ceph_decode_need(p, end, 5*sizeof(u32), bad);
586
587 /* new max? */
c89136ea 588 max = ceph_decode_32(p);
f24e9980
SW
589 if (max >= 0) {
590 err = osdmap_set_max_osd(map, max);
591 if (err < 0)
592 goto bad;
593 }
594
595 map->epoch++;
596 map->modified = map->modified;
597 if (newcrush) {
598 if (map->crush)
599 crush_destroy(map->crush);
600 map->crush = newcrush;
601 newcrush = NULL;
602 }
603
604 /* new_pool */
605 ceph_decode_32_safe(p, end, len, bad);
606 while (len--) {
607 ceph_decode_32_safe(p, end, pool, bad);
608 if (pool >= map->num_pools) {
609 void *pg_pool = kcalloc(pool + 1,
610 sizeof(*map->pg_pool),
611 GFP_NOFS);
612 if (!pg_pool) {
613 err = -ENOMEM;
614 goto bad;
615 }
616 memcpy(pg_pool, map->pg_pool,
617 map->num_pools * sizeof(*map->pg_pool));
618 kfree(map->pg_pool);
619 map->pg_pool = pg_pool;
620 map->num_pools = pool+1;
621 }
622 ceph_decode_copy(p, &map->pg_pool[pool].v,
623 sizeof(map->pg_pool->v));
624 calc_pg_masks(&map->pg_pool[pool]);
625 }
626
627 /* old_pool (ignore) */
628 ceph_decode_32_safe(p, end, len, bad);
629 *p += len * sizeof(u32);
630
631 /* new_up */
632 err = -EINVAL;
633 ceph_decode_32_safe(p, end, len, bad);
634 while (len--) {
635 u32 osd;
636 struct ceph_entity_addr addr;
637 ceph_decode_32_safe(p, end, osd, bad);
638 ceph_decode_copy_safe(p, end, &addr, sizeof(addr), bad);
63f2d211 639 ceph_decode_addr(&addr);
f24e9980
SW
640 pr_info("osd%d up\n", osd);
641 BUG_ON(osd >= map->max_osd);
642 map->osd_state[osd] |= CEPH_OSD_UP;
643 map->osd_addr[osd] = addr;
644 }
645
646 /* new_down */
647 ceph_decode_32_safe(p, end, len, bad);
648 while (len--) {
649 u32 osd;
650 ceph_decode_32_safe(p, end, osd, bad);
651 (*p)++; /* clean flag */
1bdb70e5 652 pr_info("osd%d down\n", osd);
f24e9980
SW
653 if (osd < map->max_osd)
654 map->osd_state[osd] &= ~CEPH_OSD_UP;
655 }
656
657 /* new_weight */
658 ceph_decode_32_safe(p, end, len, bad);
659 while (len--) {
660 u32 osd, off;
661 ceph_decode_need(p, end, sizeof(u32)*2, bad);
c89136ea
SW
662 osd = ceph_decode_32(p);
663 off = ceph_decode_32(p);
f24e9980
SW
664 pr_info("osd%d weight 0x%x %s\n", osd, off,
665 off == CEPH_OSD_IN ? "(in)" :
666 (off == CEPH_OSD_OUT ? "(out)" : ""));
667 if (osd < map->max_osd)
668 map->osd_weight[osd] = off;
669 }
670
671 /* new_pg_temp */
672 rbp = rb_first(&map->pg_temp);
673 ceph_decode_32_safe(p, end, len, bad);
674 while (len--) {
675 struct ceph_pg_mapping *pg;
676 int j;
51042122 677 struct ceph_pg pgid;
f24e9980
SW
678 u32 pglen;
679 ceph_decode_need(p, end, sizeof(u64) + sizeof(u32), bad);
51042122 680 ceph_decode_copy(p, &pgid, sizeof(pgid));
c89136ea 681 pglen = ceph_decode_32(p);
f24e9980
SW
682
683 /* remove any? */
51042122
SW
684 while (rbp && pgid_cmp(rb_entry(rbp, struct ceph_pg_mapping,
685 node)->pgid, pgid) <= 0) {
f24e9980
SW
686 struct rb_node *cur = rbp;
687 rbp = rb_next(rbp);
688 dout(" removed pg_temp %llx\n",
51042122
SW
689 *(u64 *)&rb_entry(cur, struct ceph_pg_mapping,
690 node)->pgid);
f24e9980
SW
691 rb_erase(cur, &map->pg_temp);
692 }
693
694 if (pglen) {
695 /* insert */
696 ceph_decode_need(p, end, pglen*sizeof(u32), bad);
697 pg = kmalloc(sizeof(*pg) + sizeof(u32)*pglen, GFP_NOFS);
698 if (!pg) {
699 err = -ENOMEM;
700 goto bad;
701 }
702 pg->pgid = pgid;
703 pg->len = pglen;
704 for (j = 0; j < len; j++)
c89136ea 705 pg->osds[j] = ceph_decode_32(p);
991abb6e
SW
706 err = __insert_pg_mapping(pg, &map->pg_temp);
707 if (err)
708 goto bad;
51042122
SW
709 dout(" added pg_temp %llx len %d\n", *(u64 *)&pgid,
710 pglen);
f24e9980
SW
711 }
712 }
713 while (rbp) {
714 struct rb_node *cur = rbp;
715 rbp = rb_next(rbp);
716 dout(" removed pg_temp %llx\n",
51042122
SW
717 *(u64 *)&rb_entry(cur, struct ceph_pg_mapping,
718 node)->pgid);
f24e9980
SW
719 rb_erase(cur, &map->pg_temp);
720 }
721
722 /* ignore the rest */
723 *p = end;
724 return map;
725
726bad:
727 pr_err("corrupt inc osdmap epoch %d off %d (%p of %p-%p)\n",
728 epoch, (int)(*p - start), *p, start, end);
729 if (newcrush)
730 crush_destroy(newcrush);
731 return ERR_PTR(err);
732}
733
734
735
736
737/*
738 * calculate file layout from given offset, length.
739 * fill in correct oid, logical length, and object extent
740 * offset, length.
741 *
742 * for now, we write only a single su, until we can
743 * pass a stride back to the caller.
744 */
745void ceph_calc_file_object_mapping(struct ceph_file_layout *layout,
746 u64 off, u64 *plen,
645a1025 747 u64 *ono,
f24e9980
SW
748 u64 *oxoff, u64 *oxlen)
749{
750 u32 osize = le32_to_cpu(layout->fl_object_size);
751 u32 su = le32_to_cpu(layout->fl_stripe_unit);
752 u32 sc = le32_to_cpu(layout->fl_stripe_count);
753 u32 bl, stripeno, stripepos, objsetno;
754 u32 su_per_object;
ff1d1f71 755 u64 t, su_offset;
f24e9980
SW
756
757 dout("mapping %llu~%llu osize %u fl_su %u\n", off, *plen,
758 osize, su);
35e054a6 759 su_per_object = osize / su;
f24e9980
SW
760 dout("osize %u / su %u = su_per_object %u\n", osize, su,
761 su_per_object);
762
763 BUG_ON((su & ~PAGE_MASK) != 0);
764 /* bl = *off / su; */
765 t = off;
766 do_div(t, su);
767 bl = t;
768 dout("off %llu / su %u = bl %u\n", off, su, bl);
769
770 stripeno = bl / sc;
771 stripepos = bl % sc;
772 objsetno = stripeno / su_per_object;
773
645a1025
SW
774 *ono = objsetno * sc + stripepos;
775 dout("objset %u * sc %u = ono %u\n", objsetno, sc, (unsigned)*ono);
776
777 /* *oxoff = *off % layout->fl_stripe_unit; # offset in su */
f24e9980 778 t = off;
ff1d1f71
NW
779 su_offset = do_div(t, su);
780 *oxoff = su_offset + (stripeno % su_per_object) * su;
781
782 /*
783 * Calculate the length of the extent being written to the selected
784 * object. This is the minimum of the full length requested (plen) or
785 * the remainder of the current stripe being written to.
786 */
787 *oxlen = min_t(u64, *plen, su - su_offset);
f24e9980
SW
788 *plen = *oxlen;
789
790 dout(" obj extent %llu~%llu\n", *oxoff, *oxlen);
791}
792
793/*
794 * calculate an object layout (i.e. pgid) from an oid,
795 * file_layout, and osdmap
796 */
797int ceph_calc_object_layout(struct ceph_object_layout *ol,
798 const char *oid,
799 struct ceph_file_layout *fl,
800 struct ceph_osdmap *osdmap)
801{
802 unsigned num, num_mask;
51042122 803 struct ceph_pg pgid;
f24e9980
SW
804 s32 preferred = (s32)le32_to_cpu(fl->fl_pg_preferred);
805 int poolid = le32_to_cpu(fl->fl_pg_pool);
806 struct ceph_pg_pool_info *pool;
51042122 807 unsigned ps;
f24e9980
SW
808
809 if (poolid >= osdmap->num_pools)
810 return -EIO;
f24e9980 811
51042122 812 pool = &osdmap->pg_pool[poolid];
1654dd0c 813 ps = ceph_str_hash(pool->v.object_hash, oid, strlen(oid));
f24e9980 814 if (preferred >= 0) {
51042122 815 ps += preferred;
f24e9980
SW
816 num = le32_to_cpu(pool->v.lpg_num);
817 num_mask = pool->lpg_num_mask;
818 } else {
819 num = le32_to_cpu(pool->v.pg_num);
820 num_mask = pool->pg_num_mask;
821 }
822
51042122
SW
823 pgid.ps = cpu_to_le16(ps);
824 pgid.preferred = cpu_to_le16(preferred);
825 pgid.pool = fl->fl_pg_pool;
f24e9980 826 if (preferred >= 0)
51042122
SW
827 dout("calc_object_layout '%s' pgid %d.%xp%d\n", oid, poolid, ps,
828 (int)preferred);
f24e9980 829 else
51042122 830 dout("calc_object_layout '%s' pgid %d.%x\n", oid, poolid, ps);
f24e9980 831
51042122 832 ol->ol_pgid = pgid;
f24e9980 833 ol->ol_stripe_unit = fl->fl_object_stripe_unit;
f24e9980
SW
834 return 0;
835}
836
837/*
838 * Calculate raw osd vector for the given pgid. Return pointer to osd
839 * array, or NULL on failure.
840 */
51042122 841static int *calc_pg_raw(struct ceph_osdmap *osdmap, struct ceph_pg pgid,
f24e9980
SW
842 int *osds, int *num)
843{
844 struct rb_node *n = osdmap->pg_temp.rb_node;
845 struct ceph_pg_mapping *pg;
846 struct ceph_pg_pool_info *pool;
847 int ruleno;
51042122
SW
848 unsigned poolid, ps, pps;
849 int preferred;
850 int c;
f24e9980
SW
851
852 /* pg_temp? */
853 while (n) {
854 pg = rb_entry(n, struct ceph_pg_mapping, node);
51042122
SW
855 c = pgid_cmp(pgid, pg->pgid);
856 if (c < 0)
f24e9980 857 n = n->rb_left;
51042122 858 else if (c > 0)
f24e9980
SW
859 n = n->rb_right;
860 else {
861 *num = pg->len;
862 return pg->osds;
863 }
864 }
865
866 /* crush */
51042122
SW
867 poolid = le32_to_cpu(pgid.pool);
868 ps = le16_to_cpu(pgid.ps);
869 preferred = (s16)le16_to_cpu(pgid.preferred);
870
767ea5c3
SW
871 /* don't forcefeed bad device ids to crush */
872 if (preferred >= osdmap->max_osd ||
873 preferred >= osdmap->crush->max_devices)
874 preferred = -1;
875
51042122 876 if (poolid >= osdmap->num_pools)
f24e9980 877 return NULL;
51042122 878 pool = &osdmap->pg_pool[poolid];
f24e9980
SW
879 ruleno = crush_find_rule(osdmap->crush, pool->v.crush_ruleset,
880 pool->v.type, pool->v.size);
881 if (ruleno < 0) {
882 pr_err("no crush rule pool %d type %d size %d\n",
51042122 883 poolid, pool->v.type, pool->v.size);
f24e9980
SW
884 return NULL;
885 }
886
51042122
SW
887 if (preferred >= 0)
888 pps = ceph_stable_mod(ps,
f24e9980
SW
889 le32_to_cpu(pool->v.lpgp_num),
890 pool->lpgp_num_mask);
891 else
51042122 892 pps = ceph_stable_mod(ps,
f24e9980
SW
893 le32_to_cpu(pool->v.pgp_num),
894 pool->pgp_num_mask);
51042122 895 pps += poolid;
f24e9980
SW
896 *num = crush_do_rule(osdmap->crush, ruleno, pps, osds,
897 min_t(int, pool->v.size, *num),
51042122 898 preferred, osdmap->osd_weight);
f24e9980
SW
899 return osds;
900}
901
902/*
903 * Return primary osd for given pgid, or -1 if none.
904 */
51042122 905int ceph_calc_pg_primary(struct ceph_osdmap *osdmap, struct ceph_pg pgid)
f24e9980
SW
906{
907 int rawosds[10], *osds;
908 int i, num = ARRAY_SIZE(rawosds);
909
910 osds = calc_pg_raw(osdmap, pgid, rawosds, &num);
911 if (!osds)
912 return -1;
913
914 /* primary is first up osd */
915 for (i = 0; i < num; i++)
916 if (ceph_osd_is_up(osdmap, osds[i])) {
917 return osds[i];
918 break;
919 }
920 return -1;
921}