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