ceph: use rbtree for pg pools; decode new osdmap format
[safe/jmp/linux-2.6] / 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                 struct ceph_pg_mapping *pg =
326                         rb_entry(rb_first(&map->pg_temp),
327                                  struct ceph_pg_mapping, node);
328                 rb_erase(&pg->node, &map->pg_temp);
329                 kfree(pg);
330         }
331         while (!RB_EMPTY_ROOT(&map->pg_pools)) {
332                 struct ceph_pg_pool_info *pi =
333                         rb_entry(rb_first(&map->pg_pools),
334                                  struct ceph_pg_pool_info, node);
335                 rb_erase(&pi->node, &map->pg_pools);
336                 kfree(pi);
337         }
338         kfree(map->osd_state);
339         kfree(map->osd_weight);
340         kfree(map->osd_addr);
341         kfree(map);
342 }
343
344 /*
345  * adjust max osd value.  reallocate arrays.
346  */
347 static int osdmap_set_max_osd(struct ceph_osdmap *map, int max)
348 {
349         u8 *state;
350         struct ceph_entity_addr *addr;
351         u32 *weight;
352
353         state = kcalloc(max, sizeof(*state), GFP_NOFS);
354         addr = kcalloc(max, sizeof(*addr), GFP_NOFS);
355         weight = kcalloc(max, sizeof(*weight), GFP_NOFS);
356         if (state == NULL || addr == NULL || weight == NULL) {
357                 kfree(state);
358                 kfree(addr);
359                 kfree(weight);
360                 return -ENOMEM;
361         }
362
363         /* copy old? */
364         if (map->osd_state) {
365                 memcpy(state, map->osd_state, map->max_osd*sizeof(*state));
366                 memcpy(addr, map->osd_addr, map->max_osd*sizeof(*addr));
367                 memcpy(weight, map->osd_weight, map->max_osd*sizeof(*weight));
368                 kfree(map->osd_state);
369                 kfree(map->osd_addr);
370                 kfree(map->osd_weight);
371         }
372
373         map->osd_state = state;
374         map->osd_weight = weight;
375         map->osd_addr = addr;
376         map->max_osd = max;
377         return 0;
378 }
379
380 /*
381  * rbtree of pg_mapping for handling pg_temp (explicit mapping of pgid
382  * to a set of osds)
383  */
384 static int pgid_cmp(struct ceph_pg l, struct ceph_pg r)
385 {
386         u64 a = *(u64 *)&l;
387         u64 b = *(u64 *)&r;
388
389         if (a < b)
390                 return -1;
391         if (a > b)
392                 return 1;
393         return 0;
394 }
395
396 static int __insert_pg_mapping(struct ceph_pg_mapping *new,
397                                struct rb_root *root)
398 {
399         struct rb_node **p = &root->rb_node;
400         struct rb_node *parent = NULL;
401         struct ceph_pg_mapping *pg = NULL;
402         int c;
403
404         while (*p) {
405                 parent = *p;
406                 pg = rb_entry(parent, struct ceph_pg_mapping, node);
407                 c = pgid_cmp(new->pgid, pg->pgid);
408                 if (c < 0)
409                         p = &(*p)->rb_left;
410                 else if (c > 0)
411                         p = &(*p)->rb_right;
412                 else
413                         return -EEXIST;
414         }
415
416         rb_link_node(&new->node, parent, p);
417         rb_insert_color(&new->node, root);
418         return 0;
419 }
420
421 static struct ceph_pg_mapping *__lookup_pg_mapping(struct rb_root *root,
422                                                    struct ceph_pg pgid)
423 {
424         struct rb_node *n = root->rb_node;
425         struct ceph_pg_mapping *pg;
426         int c;
427
428         while (n) {
429                 pg = rb_entry(n, struct ceph_pg_mapping, node);
430                 c = pgid_cmp(pgid, pg->pgid);
431                 if (c < 0)
432                         n = n->rb_left;
433                 else if (c > 0)
434                         n = n->rb_right;
435                 else
436                         return pg;
437         }
438         return NULL;
439 }
440
441 /*
442  * rbtree of pg pool info
443  */
444 static int __insert_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *new)
445 {
446         struct rb_node **p = &root->rb_node;
447         struct rb_node *parent = NULL;
448         struct ceph_pg_pool_info *pi = NULL;
449
450         while (*p) {
451                 parent = *p;
452                 pi = rb_entry(parent, struct ceph_pg_pool_info, node);
453                 if (new->id < pi->id)
454                         p = &(*p)->rb_left;
455                 else if (new->id > pi->id)
456                         p = &(*p)->rb_right;
457                 else
458                         return -EEXIST;
459         }
460
461         rb_link_node(&new->node, parent, p);
462         rb_insert_color(&new->node, root);
463         return 0;
464 }
465
466 static struct ceph_pg_pool_info *__lookup_pg_pool(struct rb_root *root, int id)
467 {
468         struct ceph_pg_pool_info *pi;
469         struct rb_node *n = root->rb_node;
470
471         while (n) {
472                 pi = rb_entry(n, struct ceph_pg_pool_info, node);
473                 if (id < pi->id)
474                         n = n->rb_left;
475                 else if (id > pi->id)
476                         n = n->rb_right;
477                 else
478                         return pi;
479         }
480         return NULL;
481 }
482
483 /*
484  * decode a full map.
485  */
486 struct ceph_osdmap *osdmap_decode(void **p, void *end)
487 {
488         struct ceph_osdmap *map;
489         u16 version;
490         u32 len, max, i;
491         u8 ev;
492         int err = -EINVAL;
493         void *start = *p;
494         struct ceph_pg_pool_info *pi;
495
496         dout("osdmap_decode %p to %p len %d\n", *p, end, (int)(end - *p));
497
498         map = kzalloc(sizeof(*map), GFP_NOFS);
499         if (map == NULL)
500                 return ERR_PTR(-ENOMEM);
501         map->pg_temp = RB_ROOT;
502
503         ceph_decode_16_safe(p, end, version, bad);
504         if (version > CEPH_OSDMAP_VERSION) {
505                 pr_warning("got unknown v %d > %d of osdmap\n", version,
506                            CEPH_OSDMAP_VERSION);
507                 goto bad;
508         }
509
510         ceph_decode_need(p, end, 2*sizeof(u64)+6*sizeof(u32), bad);
511         ceph_decode_copy(p, &map->fsid, sizeof(map->fsid));
512         map->epoch = ceph_decode_32(p);
513         ceph_decode_copy(p, &map->created, sizeof(map->created));
514         ceph_decode_copy(p, &map->modified, sizeof(map->modified));
515
516         ceph_decode_32_safe(p, end, max, bad);
517         while (max--) {
518                 ceph_decode_need(p, end, 4 + 1 + sizeof(pi->v), bad);
519                 pi = kmalloc(sizeof(*pi), GFP_NOFS);
520                 if (!pi)
521                         goto bad;
522                 pi->id = ceph_decode_32(p);
523                 ev = ceph_decode_8(p); /* encoding version */
524                 if (ev > CEPH_PG_POOL_VERSION) {
525                         pr_warning("got unknown v %d > %d of ceph_pg_pool\n",
526                                    ev, CEPH_PG_POOL_VERSION);
527                         goto bad;
528                 }
529                 ceph_decode_copy(p, &pi->v, sizeof(pi->v));
530                 __insert_pg_pool(&map->pg_pools, pi);
531                 calc_pg_masks(pi);
532                 p += le32_to_cpu(pi->v.num_snaps) * sizeof(u64);
533                 p += le32_to_cpu(pi->v.num_removed_snap_intervals)
534                         * sizeof(u64) * 2;
535         }
536         ceph_decode_32_safe(p, end, map->pool_max, bad);
537
538         ceph_decode_32_safe(p, end, map->flags, bad);
539
540         max = ceph_decode_32(p);
541
542         /* (re)alloc osd arrays */
543         err = osdmap_set_max_osd(map, max);
544         if (err < 0)
545                 goto bad;
546         dout("osdmap_decode max_osd = %d\n", map->max_osd);
547
548         /* osds */
549         err = -EINVAL;
550         ceph_decode_need(p, end, 3*sizeof(u32) +
551                          map->max_osd*(1 + sizeof(*map->osd_weight) +
552                                        sizeof(*map->osd_addr)), bad);
553         *p += 4; /* skip length field (should match max) */
554         ceph_decode_copy(p, map->osd_state, map->max_osd);
555
556         *p += 4; /* skip length field (should match max) */
557         for (i = 0; i < map->max_osd; i++)
558                 map->osd_weight[i] = ceph_decode_32(p);
559
560         *p += 4; /* skip length field (should match max) */
561         ceph_decode_copy(p, map->osd_addr, map->max_osd*sizeof(*map->osd_addr));
562         for (i = 0; i < map->max_osd; i++)
563                 ceph_decode_addr(&map->osd_addr[i]);
564
565         /* pg_temp */
566         ceph_decode_32_safe(p, end, len, bad);
567         for (i = 0; i < len; i++) {
568                 int n, j;
569                 struct ceph_pg pgid;
570                 struct ceph_pg_mapping *pg;
571
572                 ceph_decode_need(p, end, sizeof(u32) + sizeof(u64), bad);
573                 ceph_decode_copy(p, &pgid, sizeof(pgid));
574                 n = ceph_decode_32(p);
575                 ceph_decode_need(p, end, n * sizeof(u32), bad);
576                 err = -ENOMEM;
577                 pg = kmalloc(sizeof(*pg) + n*sizeof(u32), GFP_NOFS);
578                 if (!pg)
579                         goto bad;
580                 pg->pgid = pgid;
581                 pg->len = n;
582                 for (j = 0; j < n; j++)
583                         pg->osds[j] = ceph_decode_32(p);
584
585                 err = __insert_pg_mapping(pg, &map->pg_temp);
586                 if (err)
587                         goto bad;
588                 dout(" added pg_temp %llx len %d\n", *(u64 *)&pgid, len);
589         }
590
591         /* crush */
592         ceph_decode_32_safe(p, end, len, bad);
593         dout("osdmap_decode crush len %d from off 0x%x\n", len,
594              (int)(*p - start));
595         ceph_decode_need(p, end, len, bad);
596         map->crush = crush_decode(*p, end);
597         *p += len;
598         if (IS_ERR(map->crush)) {
599                 err = PTR_ERR(map->crush);
600                 map->crush = NULL;
601                 goto bad;
602         }
603
604         /* ignore the rest of the map */
605         *p = end;
606
607         dout("osdmap_decode done %p %p\n", *p, end);
608         return map;
609
610 bad:
611         dout("osdmap_decode fail\n");
612         ceph_osdmap_destroy(map);
613         return ERR_PTR(err);
614 }
615
616 /*
617  * decode and apply an incremental map update.
618  */
619 struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end,
620                                              struct ceph_osdmap *map,
621                                              struct ceph_messenger *msgr)
622 {
623         struct crush_map *newcrush = NULL;
624         struct ceph_fsid fsid;
625         u32 epoch = 0;
626         struct ceph_timespec modified;
627         u32 len, pool;
628         __s32 new_pool_max, new_flags, max;
629         void *start = *p;
630         int err = -EINVAL;
631         u16 version;
632         struct rb_node *rbp;
633
634         ceph_decode_16_safe(p, end, version, bad);
635         if (version > CEPH_OSDMAP_INC_VERSION) {
636                 pr_warning("got unknown v %d > %d of inc osdmap\n", version,
637                            CEPH_OSDMAP_INC_VERSION);
638                 goto bad;
639         }
640
641         ceph_decode_need(p, end, sizeof(fsid)+sizeof(modified)+2*sizeof(u32),
642                          bad);
643         ceph_decode_copy(p, &fsid, sizeof(fsid));
644         epoch = ceph_decode_32(p);
645         BUG_ON(epoch != map->epoch+1);
646         ceph_decode_copy(p, &modified, sizeof(modified));
647         new_pool_max = ceph_decode_32(p);
648         new_flags = ceph_decode_32(p);
649
650         /* full map? */
651         ceph_decode_32_safe(p, end, len, bad);
652         if (len > 0) {
653                 dout("apply_incremental full map len %d, %p to %p\n",
654                      len, *p, end);
655                 return osdmap_decode(p, min(*p+len, end));
656         }
657
658         /* new crush? */
659         ceph_decode_32_safe(p, end, len, bad);
660         if (len > 0) {
661                 dout("apply_incremental new crush map len %d, %p to %p\n",
662                      len, *p, end);
663                 newcrush = crush_decode(*p, min(*p+len, end));
664                 if (IS_ERR(newcrush))
665                         return ERR_PTR(PTR_ERR(newcrush));
666         }
667
668         /* new flags? */
669         if (new_flags >= 0)
670                 map->flags = new_flags;
671         if (new_pool_max >= 0)
672                 map->pool_max = new_pool_max;
673
674         ceph_decode_need(p, end, 5*sizeof(u32), bad);
675
676         /* new max? */
677         max = ceph_decode_32(p);
678         if (max >= 0) {
679                 err = osdmap_set_max_osd(map, max);
680                 if (err < 0)
681                         goto bad;
682         }
683
684         map->epoch++;
685         map->modified = map->modified;
686         if (newcrush) {
687                 if (map->crush)
688                         crush_destroy(map->crush);
689                 map->crush = newcrush;
690                 newcrush = NULL;
691         }
692
693         /* new_pool */
694         ceph_decode_32_safe(p, end, len, bad);
695         while (len--) {
696                 __u8 ev;
697                 struct ceph_pg_pool_info *pi;
698
699                 ceph_decode_32_safe(p, end, pool, bad);
700                 ceph_decode_need(p, end, 1 + sizeof(pi->v), bad);
701                 ev = ceph_decode_8(p);  /* encoding version */
702                 if (ev > CEPH_PG_POOL_VERSION) {
703                         pr_warning("got unknown v %d > %d of ceph_pg_pool\n",
704                                    ev, CEPH_PG_POOL_VERSION);
705                         goto bad;
706                 }
707                 pi = __lookup_pg_pool(&map->pg_pools, pool);
708                 if (!pi) {
709                         pi = kmalloc(sizeof(*pi), GFP_NOFS);
710                         if (!pi) {
711                                 err = -ENOMEM;
712                                 goto bad;
713                         }
714                         pi->id = pool;
715                         __insert_pg_pool(&map->pg_pools, pi);
716                 }
717                 ceph_decode_copy(p, &pi->v, sizeof(pi->v));
718                 calc_pg_masks(pi);
719         }
720
721         /* old_pool */
722         ceph_decode_32_safe(p, end, len, bad);
723         while (len--) {
724                 struct ceph_pg_pool_info *pi;
725
726                 ceph_decode_32_safe(p, end, pool, bad);
727                 pi = __lookup_pg_pool(&map->pg_pools, pool);
728                 if (pi) {
729                         rb_erase(&pi->node, &map->pg_pools);
730                         kfree(pi);
731                 }
732         }
733
734         /* new_up */
735         err = -EINVAL;
736         ceph_decode_32_safe(p, end, len, bad);
737         while (len--) {
738                 u32 osd;
739                 struct ceph_entity_addr addr;
740                 ceph_decode_32_safe(p, end, osd, bad);
741                 ceph_decode_copy_safe(p, end, &addr, sizeof(addr), bad);
742                 ceph_decode_addr(&addr);
743                 pr_info("osd%d up\n", osd);
744                 BUG_ON(osd >= map->max_osd);
745                 map->osd_state[osd] |= CEPH_OSD_UP;
746                 map->osd_addr[osd] = addr;
747         }
748
749         /* new_down */
750         ceph_decode_32_safe(p, end, len, bad);
751         while (len--) {
752                 u32 osd;
753                 ceph_decode_32_safe(p, end, osd, bad);
754                 (*p)++;  /* clean flag */
755                 pr_info("osd%d down\n", osd);
756                 if (osd < map->max_osd)
757                         map->osd_state[osd] &= ~CEPH_OSD_UP;
758         }
759
760         /* new_weight */
761         ceph_decode_32_safe(p, end, len, bad);
762         while (len--) {
763                 u32 osd, off;
764                 ceph_decode_need(p, end, sizeof(u32)*2, bad);
765                 osd = ceph_decode_32(p);
766                 off = ceph_decode_32(p);
767                 pr_info("osd%d weight 0x%x %s\n", osd, off,
768                      off == CEPH_OSD_IN ? "(in)" :
769                      (off == CEPH_OSD_OUT ? "(out)" : ""));
770                 if (osd < map->max_osd)
771                         map->osd_weight[osd] = off;
772         }
773
774         /* new_pg_temp */
775         rbp = rb_first(&map->pg_temp);
776         ceph_decode_32_safe(p, end, len, bad);
777         while (len--) {
778                 struct ceph_pg_mapping *pg;
779                 int j;
780                 struct ceph_pg pgid;
781                 u32 pglen;
782                 ceph_decode_need(p, end, sizeof(u64) + sizeof(u32), bad);
783                 ceph_decode_copy(p, &pgid, sizeof(pgid));
784                 pglen = ceph_decode_32(p);
785
786                 /* remove any? */
787                 while (rbp && pgid_cmp(rb_entry(rbp, struct ceph_pg_mapping,
788                                                 node)->pgid, pgid) <= 0) {
789                         struct rb_node *cur = rbp;
790                         rbp = rb_next(rbp);
791                         dout(" removed pg_temp %llx\n",
792                              *(u64 *)&rb_entry(cur, struct ceph_pg_mapping,
793                                                node)->pgid);
794                         rb_erase(cur, &map->pg_temp);
795                 }
796
797                 if (pglen) {
798                         /* insert */
799                         ceph_decode_need(p, end, pglen*sizeof(u32), bad);
800                         pg = kmalloc(sizeof(*pg) + sizeof(u32)*pglen, GFP_NOFS);
801                         if (!pg) {
802                                 err = -ENOMEM;
803                                 goto bad;
804                         }
805                         pg->pgid = pgid;
806                         pg->len = pglen;
807                         for (j = 0; j < pglen; j++)
808                                 pg->osds[j] = ceph_decode_32(p);
809                         err = __insert_pg_mapping(pg, &map->pg_temp);
810                         if (err)
811                                 goto bad;
812                         dout(" added pg_temp %llx len %d\n", *(u64 *)&pgid,
813                              pglen);
814                 }
815         }
816         while (rbp) {
817                 struct rb_node *cur = rbp;
818                 rbp = rb_next(rbp);
819                 dout(" removed pg_temp %llx\n",
820                      *(u64 *)&rb_entry(cur, struct ceph_pg_mapping,
821                                        node)->pgid);
822                 rb_erase(cur, &map->pg_temp);
823         }
824
825         /* ignore the rest */
826         *p = end;
827         return map;
828
829 bad:
830         pr_err("corrupt inc osdmap epoch %d off %d (%p of %p-%p)\n",
831                epoch, (int)(*p - start), *p, start, end);
832         print_hex_dump(KERN_DEBUG, "osdmap: ",
833                        DUMP_PREFIX_OFFSET, 16, 1,
834                        start, end - start, true);
835         if (newcrush)
836                 crush_destroy(newcrush);
837         return ERR_PTR(err);
838 }
839
840
841
842
843 /*
844  * calculate file layout from given offset, length.
845  * fill in correct oid, logical length, and object extent
846  * offset, length.
847  *
848  * for now, we write only a single su, until we can
849  * pass a stride back to the caller.
850  */
851 void ceph_calc_file_object_mapping(struct ceph_file_layout *layout,
852                                    u64 off, u64 *plen,
853                                    u64 *ono,
854                                    u64 *oxoff, u64 *oxlen)
855 {
856         u32 osize = le32_to_cpu(layout->fl_object_size);
857         u32 su = le32_to_cpu(layout->fl_stripe_unit);
858         u32 sc = le32_to_cpu(layout->fl_stripe_count);
859         u32 bl, stripeno, stripepos, objsetno;
860         u32 su_per_object;
861         u64 t, su_offset;
862
863         dout("mapping %llu~%llu  osize %u fl_su %u\n", off, *plen,
864              osize, su);
865         su_per_object = osize / su;
866         dout("osize %u / su %u = su_per_object %u\n", osize, su,
867              su_per_object);
868
869         BUG_ON((su & ~PAGE_MASK) != 0);
870         /* bl = *off / su; */
871         t = off;
872         do_div(t, su);
873         bl = t;
874         dout("off %llu / su %u = bl %u\n", off, su, bl);
875
876         stripeno = bl / sc;
877         stripepos = bl % sc;
878         objsetno = stripeno / su_per_object;
879
880         *ono = objsetno * sc + stripepos;
881         dout("objset %u * sc %u = ono %u\n", objsetno, sc, (unsigned)*ono);
882
883         /* *oxoff = *off % layout->fl_stripe_unit;  # offset in su */
884         t = off;
885         su_offset = do_div(t, su);
886         *oxoff = su_offset + (stripeno % su_per_object) * su;
887
888         /*
889          * Calculate the length of the extent being written to the selected
890          * object. This is the minimum of the full length requested (plen) or
891          * the remainder of the current stripe being written to.
892          */
893         *oxlen = min_t(u64, *plen, su - su_offset);
894         *plen = *oxlen;
895
896         dout(" obj extent %llu~%llu\n", *oxoff, *oxlen);
897 }
898
899 /*
900  * calculate an object layout (i.e. pgid) from an oid,
901  * file_layout, and osdmap
902  */
903 int ceph_calc_object_layout(struct ceph_object_layout *ol,
904                             const char *oid,
905                             struct ceph_file_layout *fl,
906                             struct ceph_osdmap *osdmap)
907 {
908         unsigned num, num_mask;
909         struct ceph_pg pgid;
910         s32 preferred = (s32)le32_to_cpu(fl->fl_pg_preferred);
911         int poolid = le32_to_cpu(fl->fl_pg_pool);
912         struct ceph_pg_pool_info *pool;
913         unsigned ps;
914
915         BUG_ON(!osdmap);
916
917         pool = __lookup_pg_pool(&osdmap->pg_pools, poolid);
918         if (!pool)
919                 return -EIO;
920         ps = ceph_str_hash(pool->v.object_hash, oid, strlen(oid));
921         if (preferred >= 0) {
922                 ps += preferred;
923                 num = le32_to_cpu(pool->v.lpg_num);
924                 num_mask = pool->lpg_num_mask;
925         } else {
926                 num = le32_to_cpu(pool->v.pg_num);
927                 num_mask = pool->pg_num_mask;
928         }
929
930         pgid.ps = cpu_to_le16(ps);
931         pgid.preferred = cpu_to_le16(preferred);
932         pgid.pool = fl->fl_pg_pool;
933         if (preferred >= 0)
934                 dout("calc_object_layout '%s' pgid %d.%xp%d\n", oid, poolid, ps,
935                      (int)preferred);
936         else
937                 dout("calc_object_layout '%s' pgid %d.%x\n", oid, poolid, ps);
938
939         ol->ol_pgid = pgid;
940         ol->ol_stripe_unit = fl->fl_object_stripe_unit;
941         return 0;
942 }
943
944 /*
945  * Calculate raw osd vector for the given pgid.  Return pointer to osd
946  * array, or NULL on failure.
947  */
948 static int *calc_pg_raw(struct ceph_osdmap *osdmap, struct ceph_pg pgid,
949                         int *osds, int *num)
950 {
951         struct ceph_pg_mapping *pg;
952         struct ceph_pg_pool_info *pool;
953         int ruleno;
954         unsigned poolid, ps, pps;
955         int preferred;
956
957         /* pg_temp? */
958         pg = __lookup_pg_mapping(&osdmap->pg_temp, pgid);
959         if (pg) {
960                 *num = pg->len;
961                 return pg->osds;
962         }
963
964         /* crush */
965         poolid = le32_to_cpu(pgid.pool);
966         ps = le16_to_cpu(pgid.ps);
967         preferred = (s16)le16_to_cpu(pgid.preferred);
968
969         /* don't forcefeed bad device ids to crush */
970         if (preferred >= osdmap->max_osd ||
971             preferred >= osdmap->crush->max_devices)
972                 preferred = -1;
973
974         pool = __lookup_pg_pool(&osdmap->pg_pools, poolid);
975         if (!pool)
976                 return NULL;
977         ruleno = crush_find_rule(osdmap->crush, pool->v.crush_ruleset,
978                                  pool->v.type, pool->v.size);
979         if (ruleno < 0) {
980                 pr_err("no crush rule pool %d type %d size %d\n",
981                        poolid, pool->v.type, pool->v.size);
982                 return NULL;
983         }
984
985         if (preferred >= 0)
986                 pps = ceph_stable_mod(ps,
987                                       le32_to_cpu(pool->v.lpgp_num),
988                                       pool->lpgp_num_mask);
989         else
990                 pps = ceph_stable_mod(ps,
991                                       le32_to_cpu(pool->v.pgp_num),
992                                       pool->pgp_num_mask);
993         pps += poolid;
994         *num = crush_do_rule(osdmap->crush, ruleno, pps, osds,
995                              min_t(int, pool->v.size, *num),
996                              preferred, osdmap->osd_weight);
997         return osds;
998 }
999
1000 /*
1001  * Return primary osd for given pgid, or -1 if none.
1002  */
1003 int ceph_calc_pg_primary(struct ceph_osdmap *osdmap, struct ceph_pg pgid)
1004 {
1005         int rawosds[10], *osds;
1006         int i, num = ARRAY_SIZE(rawosds);
1007
1008         osds = calc_pg_raw(osdmap, pgid, rawosds, &num);
1009         if (!osds)
1010                 return -1;
1011
1012         /* primary is first up osd */
1013         for (i = 0; i < num; i++)
1014                 if (ceph_osd_is_up(osdmap, osds[i])) {
1015                         return osds[i];
1016                         break;
1017                 }
1018         return -1;
1019 }