ceph: OSD client
[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);
70 ceph_decode_32(p, b->item_weight);
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++) {
89 ceph_decode_32(p, b->item_weights[j]);
90 ceph_decode_32(p, b->sum_weights[j]);
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++)
108 ceph_decode_32(p, b->node_weights[j]);
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++) {
127 ceph_decode_32(p, b->item_weights[j]);
128 ceph_decode_32(p, b->straws[j]);
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);
151 ceph_decode_32(p, magic);
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 }
157 ceph_decode_32(p, c->max_buckets);
158 ceph_decode_32(p, c->max_rules);
159 ceph_decode_32(p, c->max_devices);
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);
211 ceph_decode_32(p, b->id);
212 ceph_decode_16(p, b->type);
213 ceph_decode_16(p, b->alg);
214 ceph_decode_32(p, b->weight);
215 ceph_decode_32(p, b->size);
216
217 dout("crush_decode bucket size %d off %x %p to %p\n",
218 b->size, (int)(*p-start), *p, end);
219
220 b->items = kcalloc(b->size, sizeof(__s32), GFP_NOFS);
221 if (b->items == NULL)
222 goto badmem;
223 b->perm = kcalloc(b->size, sizeof(u32), GFP_NOFS);
224 if (b->perm == NULL)
225 goto badmem;
226 b->perm_n = 0;
227
228 ceph_decode_need(p, end, b->size*sizeof(u32), bad);
229 for (j = 0; j < b->size; j++)
230 ceph_decode_32(p, b->items[j]);
231
232 switch (b->alg) {
233 case CRUSH_BUCKET_UNIFORM:
234 err = crush_decode_uniform_bucket(p, end,
235 (struct crush_bucket_uniform *)b);
236 if (err < 0)
237 goto bad;
238 break;
239 case CRUSH_BUCKET_LIST:
240 err = crush_decode_list_bucket(p, end,
241 (struct crush_bucket_list *)b);
242 if (err < 0)
243 goto bad;
244 break;
245 case CRUSH_BUCKET_TREE:
246 err = crush_decode_tree_bucket(p, end,
247 (struct crush_bucket_tree *)b);
248 if (err < 0)
249 goto bad;
250 break;
251 case CRUSH_BUCKET_STRAW:
252 err = crush_decode_straw_bucket(p, end,
253 (struct crush_bucket_straw *)b);
254 if (err < 0)
255 goto bad;
256 break;
257 }
258 }
259
260 /* rules */
261 dout("rule vec is %p\n", c->rules);
262 for (i = 0; i < c->max_rules; i++) {
263 u32 yes;
264 struct crush_rule *r;
265
266 ceph_decode_32_safe(p, end, yes, bad);
267 if (!yes) {
268 dout("crush_decode NO rule %d off %x %p to %p\n",
269 i, (int)(*p-start), *p, end);
270 c->rules[i] = NULL;
271 continue;
272 }
273
274 dout("crush_decode rule %d off %x %p to %p\n",
275 i, (int)(*p-start), *p, end);
276
277 /* len */
278 ceph_decode_32_safe(p, end, yes, bad);
279#if BITS_PER_LONG == 32
280 if (yes > ULONG_MAX / 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 ceph_decode_32(p, r->steps[j].op);
294 ceph_decode_32(p, r->steps[j].arg1);
295 ceph_decode_32(p, r->steps[j].arg2);
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/*
314 * osd map
315 */
316void ceph_osdmap_destroy(struct ceph_osdmap *map)
317{
318 dout("osdmap_destroy %p\n", map);
319 if (map->crush)
320 crush_destroy(map->crush);
321 while (!RB_EMPTY_ROOT(&map->pg_temp))
322 rb_erase(rb_first(&map->pg_temp), &map->pg_temp);
323 kfree(map->osd_state);
324 kfree(map->osd_weight);
325 kfree(map->pg_pool);
326 kfree(map->osd_addr);
327 kfree(map);
328}
329
330/*
331 * adjust max osd value. reallocate arrays.
332 */
333static int osdmap_set_max_osd(struct ceph_osdmap *map, int max)
334{
335 u8 *state;
336 struct ceph_entity_addr *addr;
337 u32 *weight;
338
339 state = kcalloc(max, sizeof(*state), GFP_NOFS);
340 addr = kcalloc(max, sizeof(*addr), GFP_NOFS);
341 weight = kcalloc(max, sizeof(*weight), GFP_NOFS);
342 if (state == NULL || addr == NULL || weight == NULL) {
343 kfree(state);
344 kfree(addr);
345 kfree(weight);
346 return -ENOMEM;
347 }
348
349 /* copy old? */
350 if (map->osd_state) {
351 memcpy(state, map->osd_state, map->max_osd*sizeof(*state));
352 memcpy(addr, map->osd_addr, map->max_osd*sizeof(*addr));
353 memcpy(weight, map->osd_weight, map->max_osd*sizeof(*weight));
354 kfree(map->osd_state);
355 kfree(map->osd_addr);
356 kfree(map->osd_weight);
357 }
358
359 map->osd_state = state;
360 map->osd_weight = weight;
361 map->osd_addr = addr;
362 map->max_osd = max;
363 return 0;
364}
365
366/*
367 * Insert a new pg_temp mapping
368 */
369static void __insert_pg_mapping(struct ceph_pg_mapping *new,
370 struct rb_root *root)
371{
372 struct rb_node **p = &root->rb_node;
373 struct rb_node *parent = NULL;
374 struct ceph_pg_mapping *pg = NULL;
375
376 while (*p) {
377 parent = *p;
378 pg = rb_entry(parent, struct ceph_pg_mapping, node);
379 if (new->pgid < pg->pgid)
380 p = &(*p)->rb_left;
381 else if (new->pgid > pg->pgid)
382 p = &(*p)->rb_right;
383 else
384 BUG();
385 }
386
387 rb_link_node(&new->node, parent, p);
388 rb_insert_color(&new->node, root);
389}
390
391/*
392 * decode a full map.
393 */
394struct ceph_osdmap *osdmap_decode(void **p, void *end)
395{
396 struct ceph_osdmap *map;
397 u16 version;
398 u32 len, max, i;
399 int err = -EINVAL;
400 void *start = *p;
401
402 dout("osdmap_decode %p to %p len %d\n", *p, end, (int)(end - *p));
403
404 map = kzalloc(sizeof(*map), GFP_NOFS);
405 if (map == NULL)
406 return ERR_PTR(-ENOMEM);
407 map->pg_temp = RB_ROOT;
408
409 ceph_decode_16_safe(p, end, version, bad);
410
411 ceph_decode_need(p, end, 2*sizeof(u64)+6*sizeof(u32), bad);
412 ceph_decode_copy(p, &map->fsid, sizeof(map->fsid));
413 ceph_decode_32(p, map->epoch);
414 ceph_decode_copy(p, &map->created, sizeof(map->created));
415 ceph_decode_copy(p, &map->modified, sizeof(map->modified));
416
417 ceph_decode_32(p, map->num_pools);
418 map->pg_pool = kcalloc(map->num_pools, sizeof(*map->pg_pool),
419 GFP_NOFS);
420 if (!map->pg_pool) {
421 err = -ENOMEM;
422 goto bad;
423 }
424 ceph_decode_32_safe(p, end, max, bad);
425 while (max--) {
426 ceph_decode_need(p, end, 4+sizeof(map->pg_pool->v), bad);
427 ceph_decode_32(p, i);
428 if (i >= map->num_pools)
429 goto bad;
430 ceph_decode_copy(p, &map->pg_pool[i].v,
431 sizeof(map->pg_pool->v));
432 calc_pg_masks(&map->pg_pool[i]);
433 p += le32_to_cpu(map->pg_pool[i].v.num_snaps) * sizeof(u64);
434 p += le32_to_cpu(map->pg_pool[i].v.num_removed_snap_intervals)
435 * sizeof(u64) * 2;
436 }
437
438 ceph_decode_32_safe(p, end, map->flags, bad);
439
440 ceph_decode_32(p, max);
441
442 /* (re)alloc osd arrays */
443 err = osdmap_set_max_osd(map, max);
444 if (err < 0)
445 goto bad;
446 dout("osdmap_decode max_osd = %d\n", map->max_osd);
447
448 /* osds */
449 err = -EINVAL;
450 ceph_decode_need(p, end, 3*sizeof(u32) +
451 map->max_osd*(1 + sizeof(*map->osd_weight) +
452 sizeof(*map->osd_addr)), bad);
453 *p += 4; /* skip length field (should match max) */
454 ceph_decode_copy(p, map->osd_state, map->max_osd);
455
456 *p += 4; /* skip length field (should match max) */
457 for (i = 0; i < map->max_osd; i++)
458 ceph_decode_32(p, map->osd_weight[i]);
459
460 *p += 4; /* skip length field (should match max) */
461 ceph_decode_copy(p, map->osd_addr, map->max_osd*sizeof(*map->osd_addr));
462
463 /* pg_temp */
464 ceph_decode_32_safe(p, end, len, bad);
465 for (i = 0; i < len; i++) {
466 int n, j;
467 u64 pgid;
468 struct ceph_pg_mapping *pg;
469
470 ceph_decode_need(p, end, sizeof(u32) + sizeof(u64), bad);
471 ceph_decode_64(p, pgid);
472 ceph_decode_32(p, n);
473 ceph_decode_need(p, end, n * sizeof(u32), bad);
474 pg = kmalloc(sizeof(*pg) + n*sizeof(u32), GFP_NOFS);
475 if (!pg) {
476 err = -ENOMEM;
477 goto bad;
478 }
479 pg->pgid = pgid;
480 pg->len = n;
481 for (j = 0; j < n; j++)
482 ceph_decode_32(p, pg->osds[j]);
483
484 __insert_pg_mapping(pg, &map->pg_temp);
485 dout(" added pg_temp %llx len %d\n", pgid, len);
486 }
487
488 /* crush */
489 ceph_decode_32_safe(p, end, len, bad);
490 dout("osdmap_decode crush len %d from off 0x%x\n", len,
491 (int)(*p - start));
492 ceph_decode_need(p, end, len, bad);
493 map->crush = crush_decode(*p, end);
494 *p += len;
495 if (IS_ERR(map->crush)) {
496 err = PTR_ERR(map->crush);
497 map->crush = NULL;
498 goto bad;
499 }
500
501 /* ignore the rest of the map */
502 *p = end;
503
504 dout("osdmap_decode done %p %p\n", *p, end);
505 return map;
506
507bad:
508 dout("osdmap_decode fail\n");
509 ceph_osdmap_destroy(map);
510 return ERR_PTR(err);
511}
512
513/*
514 * decode and apply an incremental map update.
515 */
516struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end,
517 struct ceph_osdmap *map,
518 struct ceph_messenger *msgr)
519{
520 struct ceph_osdmap *newmap = map;
521 struct crush_map *newcrush = NULL;
522 struct ceph_fsid fsid;
523 u32 epoch = 0;
524 struct ceph_timespec modified;
525 u32 len, pool;
526 __s32 new_flags, max;
527 void *start = *p;
528 int err = -EINVAL;
529 u16 version;
530 struct rb_node *rbp;
531
532 ceph_decode_16_safe(p, end, version, bad);
533
534 ceph_decode_need(p, end, sizeof(fsid)+sizeof(modified)+2*sizeof(u32),
535 bad);
536 ceph_decode_copy(p, &fsid, sizeof(fsid));
537 ceph_decode_32(p, epoch);
538 BUG_ON(epoch != map->epoch+1);
539 ceph_decode_copy(p, &modified, sizeof(modified));
540 ceph_decode_32(p, new_flags);
541
542 /* full map? */
543 ceph_decode_32_safe(p, end, len, bad);
544 if (len > 0) {
545 dout("apply_incremental full map len %d, %p to %p\n",
546 len, *p, end);
547 newmap = osdmap_decode(p, min(*p+len, end));
548 return newmap; /* error or not */
549 }
550
551 /* new crush? */
552 ceph_decode_32_safe(p, end, len, bad);
553 if (len > 0) {
554 dout("apply_incremental new crush map len %d, %p to %p\n",
555 len, *p, end);
556 newcrush = crush_decode(*p, min(*p+len, end));
557 if (IS_ERR(newcrush))
558 return ERR_PTR(PTR_ERR(newcrush));
559 }
560
561 /* new flags? */
562 if (new_flags >= 0)
563 map->flags = new_flags;
564
565 ceph_decode_need(p, end, 5*sizeof(u32), bad);
566
567 /* new max? */
568 ceph_decode_32(p, max);
569 if (max >= 0) {
570 err = osdmap_set_max_osd(map, max);
571 if (err < 0)
572 goto bad;
573 }
574
575 map->epoch++;
576 map->modified = map->modified;
577 if (newcrush) {
578 if (map->crush)
579 crush_destroy(map->crush);
580 map->crush = newcrush;
581 newcrush = NULL;
582 }
583
584 /* new_pool */
585 ceph_decode_32_safe(p, end, len, bad);
586 while (len--) {
587 ceph_decode_32_safe(p, end, pool, bad);
588 if (pool >= map->num_pools) {
589 void *pg_pool = kcalloc(pool + 1,
590 sizeof(*map->pg_pool),
591 GFP_NOFS);
592 if (!pg_pool) {
593 err = -ENOMEM;
594 goto bad;
595 }
596 memcpy(pg_pool, map->pg_pool,
597 map->num_pools * sizeof(*map->pg_pool));
598 kfree(map->pg_pool);
599 map->pg_pool = pg_pool;
600 map->num_pools = pool+1;
601 }
602 ceph_decode_copy(p, &map->pg_pool[pool].v,
603 sizeof(map->pg_pool->v));
604 calc_pg_masks(&map->pg_pool[pool]);
605 }
606
607 /* old_pool (ignore) */
608 ceph_decode_32_safe(p, end, len, bad);
609 *p += len * sizeof(u32);
610
611 /* new_up */
612 err = -EINVAL;
613 ceph_decode_32_safe(p, end, len, bad);
614 while (len--) {
615 u32 osd;
616 struct ceph_entity_addr addr;
617 ceph_decode_32_safe(p, end, osd, bad);
618 ceph_decode_copy_safe(p, end, &addr, sizeof(addr), bad);
619 pr_info("osd%d up\n", osd);
620 BUG_ON(osd >= map->max_osd);
621 map->osd_state[osd] |= CEPH_OSD_UP;
622 map->osd_addr[osd] = addr;
623 }
624
625 /* new_down */
626 ceph_decode_32_safe(p, end, len, bad);
627 while (len--) {
628 u32 osd;
629 ceph_decode_32_safe(p, end, osd, bad);
630 (*p)++; /* clean flag */
631 pr_info("ceph osd%d down\n", osd);
632 if (osd < map->max_osd)
633 map->osd_state[osd] &= ~CEPH_OSD_UP;
634 }
635
636 /* new_weight */
637 ceph_decode_32_safe(p, end, len, bad);
638 while (len--) {
639 u32 osd, off;
640 ceph_decode_need(p, end, sizeof(u32)*2, bad);
641 ceph_decode_32(p, osd);
642 ceph_decode_32(p, off);
643 pr_info("osd%d weight 0x%x %s\n", osd, off,
644 off == CEPH_OSD_IN ? "(in)" :
645 (off == CEPH_OSD_OUT ? "(out)" : ""));
646 if (osd < map->max_osd)
647 map->osd_weight[osd] = off;
648 }
649
650 /* new_pg_temp */
651 rbp = rb_first(&map->pg_temp);
652 ceph_decode_32_safe(p, end, len, bad);
653 while (len--) {
654 struct ceph_pg_mapping *pg;
655 int j;
656 u64 pgid;
657 u32 pglen;
658 ceph_decode_need(p, end, sizeof(u64) + sizeof(u32), bad);
659 ceph_decode_64(p, pgid);
660 ceph_decode_32(p, pglen);
661
662 /* remove any? */
663 while (rbp && rb_entry(rbp, struct ceph_pg_mapping,
664 node)->pgid <= pgid) {
665 struct rb_node *cur = rbp;
666 rbp = rb_next(rbp);
667 dout(" removed pg_temp %llx\n",
668 rb_entry(cur, struct ceph_pg_mapping, node)->pgid);
669 rb_erase(cur, &map->pg_temp);
670 }
671
672 if (pglen) {
673 /* insert */
674 ceph_decode_need(p, end, pglen*sizeof(u32), bad);
675 pg = kmalloc(sizeof(*pg) + sizeof(u32)*pglen, GFP_NOFS);
676 if (!pg) {
677 err = -ENOMEM;
678 goto bad;
679 }
680 pg->pgid = pgid;
681 pg->len = pglen;
682 for (j = 0; j < len; j++)
683 ceph_decode_32(p, pg->osds[j]);
684 __insert_pg_mapping(pg, &map->pg_temp);
685 dout(" added pg_temp %llx len %d\n", pgid, pglen);
686 }
687 }
688 while (rbp) {
689 struct rb_node *cur = rbp;
690 rbp = rb_next(rbp);
691 dout(" removed pg_temp %llx\n",
692 rb_entry(cur, struct ceph_pg_mapping, node)->pgid);
693 rb_erase(cur, &map->pg_temp);
694 }
695
696 /* ignore the rest */
697 *p = end;
698 return map;
699
700bad:
701 pr_err("corrupt inc osdmap epoch %d off %d (%p of %p-%p)\n",
702 epoch, (int)(*p - start), *p, start, end);
703 if (newcrush)
704 crush_destroy(newcrush);
705 return ERR_PTR(err);
706}
707
708
709
710
711/*
712 * calculate file layout from given offset, length.
713 * fill in correct oid, logical length, and object extent
714 * offset, length.
715 *
716 * for now, we write only a single su, until we can
717 * pass a stride back to the caller.
718 */
719void ceph_calc_file_object_mapping(struct ceph_file_layout *layout,
720 u64 off, u64 *plen,
721 u64 *bno,
722 u64 *oxoff, u64 *oxlen)
723{
724 u32 osize = le32_to_cpu(layout->fl_object_size);
725 u32 su = le32_to_cpu(layout->fl_stripe_unit);
726 u32 sc = le32_to_cpu(layout->fl_stripe_count);
727 u32 bl, stripeno, stripepos, objsetno;
728 u32 su_per_object;
729 u64 t;
730
731 dout("mapping %llu~%llu osize %u fl_su %u\n", off, *plen,
732 osize, su);
733 su_per_object = osize / le32_to_cpu(layout->fl_stripe_unit);
734 dout("osize %u / su %u = su_per_object %u\n", osize, su,
735 su_per_object);
736
737 BUG_ON((su & ~PAGE_MASK) != 0);
738 /* bl = *off / su; */
739 t = off;
740 do_div(t, su);
741 bl = t;
742 dout("off %llu / su %u = bl %u\n", off, su, bl);
743
744 stripeno = bl / sc;
745 stripepos = bl % sc;
746 objsetno = stripeno / su_per_object;
747
748 *bno = objsetno * sc + stripepos;
749 dout("objset %u * sc %u = bno %u\n", objsetno, sc, (unsigned)*bno);
750 /* *oxoff = *off / layout->fl_stripe_unit; */
751 t = off;
752 *oxoff = do_div(t, su);
753 *oxlen = min_t(u64, *plen, su - *oxoff);
754 *plen = *oxlen;
755
756 dout(" obj extent %llu~%llu\n", *oxoff, *oxlen);
757}
758
759/*
760 * calculate an object layout (i.e. pgid) from an oid,
761 * file_layout, and osdmap
762 */
763int ceph_calc_object_layout(struct ceph_object_layout *ol,
764 const char *oid,
765 struct ceph_file_layout *fl,
766 struct ceph_osdmap *osdmap)
767{
768 unsigned num, num_mask;
769 union ceph_pg pgid;
770 s32 preferred = (s32)le32_to_cpu(fl->fl_pg_preferred);
771 int poolid = le32_to_cpu(fl->fl_pg_pool);
772 struct ceph_pg_pool_info *pool;
773
774 if (poolid >= osdmap->num_pools)
775 return -EIO;
776 pool = &osdmap->pg_pool[poolid];
777
778 if (preferred >= 0) {
779 num = le32_to_cpu(pool->v.lpg_num);
780 num_mask = pool->lpg_num_mask;
781 } else {
782 num = le32_to_cpu(pool->v.pg_num);
783 num_mask = pool->pg_num_mask;
784 }
785
786 pgid.pg64 = 0; /* start with it zeroed out */
787 pgid.pg.ps = ceph_full_name_hash(oid, strlen(oid));
788 pgid.pg.preferred = preferred;
789 pgid.pg.pool = le32_to_cpu(fl->fl_pg_pool);
790 if (preferred >= 0)
791 dout("calc_object_layout '%s' pgid %d.%xp%d (%llx)\n", oid,
792 pgid.pg.pool, pgid.pg.ps, (int)preferred, pgid.pg64);
793 else
794 dout("calc_object_layout '%s' pgid %d.%x (%llx)\n", oid,
795 pgid.pg.pool, pgid.pg.ps, pgid.pg64);
796
797 ol->ol_pgid = cpu_to_le64(pgid.pg64);
798 ol->ol_stripe_unit = fl->fl_object_stripe_unit;
799
800 return 0;
801}
802
803/*
804 * Calculate raw osd vector for the given pgid. Return pointer to osd
805 * array, or NULL on failure.
806 */
807static int *calc_pg_raw(struct ceph_osdmap *osdmap, union ceph_pg pgid,
808 int *osds, int *num)
809{
810 struct rb_node *n = osdmap->pg_temp.rb_node;
811 struct ceph_pg_mapping *pg;
812 struct ceph_pg_pool_info *pool;
813 int ruleno;
814 unsigned pps; /* placement ps */
815
816 /* pg_temp? */
817 while (n) {
818 pg = rb_entry(n, struct ceph_pg_mapping, node);
819 if (pgid.pg64 < pg->pgid)
820 n = n->rb_left;
821 else if (pgid.pg64 > pg->pgid)
822 n = n->rb_right;
823 else {
824 *num = pg->len;
825 return pg->osds;
826 }
827 }
828
829 /* crush */
830 if (pgid.pg.pool >= osdmap->num_pools)
831 return NULL;
832 pool = &osdmap->pg_pool[pgid.pg.pool];
833 ruleno = crush_find_rule(osdmap->crush, pool->v.crush_ruleset,
834 pool->v.type, pool->v.size);
835 if (ruleno < 0) {
836 pr_err("no crush rule pool %d type %d size %d\n",
837 pgid.pg.pool, pool->v.type, pool->v.size);
838 return NULL;
839 }
840
841 if (pgid.pg.preferred >= 0)
842 pps = ceph_stable_mod(pgid.pg.ps,
843 le32_to_cpu(pool->v.lpgp_num),
844 pool->lpgp_num_mask);
845 else
846 pps = ceph_stable_mod(pgid.pg.ps,
847 le32_to_cpu(pool->v.pgp_num),
848 pool->pgp_num_mask);
849 pps += pgid.pg.pool;
850 *num = crush_do_rule(osdmap->crush, ruleno, pps, osds,
851 min_t(int, pool->v.size, *num),
852 pgid.pg.preferred, osdmap->osd_weight);
853 return osds;
854}
855
856/*
857 * Return primary osd for given pgid, or -1 if none.
858 */
859int ceph_calc_pg_primary(struct ceph_osdmap *osdmap, union ceph_pg pgid)
860{
861 int rawosds[10], *osds;
862 int i, num = ARRAY_SIZE(rawosds);
863
864 osds = calc_pg_raw(osdmap, pgid, rawosds, &num);
865 if (!osds)
866 return -1;
867
868 /* primary is first up osd */
869 for (i = 0; i < num; i++)
870 if (ceph_osd_is_up(osdmap, osds[i])) {
871 return osds[i];
872 break;
873 }
874 return -1;
875}