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