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