blob: cd7bb265d7892c9b5d6a868b34fae72ae5a886ab [file] [log] [blame]
Sage Weilf24e9982009-10-06 11:31:10 -07001
2#include <asm/div64.h>
3
4#include "super.h"
5#include "osdmap.h"
6#include "crush/hash.h"
7#include "crush/mapper.h"
8#include "decode.h"
9#include "ceph_debug.h"
10
11char *ceph_osdmap_state_str(char *str, int len, int state)
12{
13 int flag = 0;
14
15 if (!len)
16 goto done;
17
18 *str = '\0';
19 if (state) {
20 if (state & CEPH_OSD_EXISTS) {
21 snprintf(str, len, "exists");
22 flag = 1;
23 }
24 if (state & CEPH_OSD_UP) {
25 snprintf(str, len, "%s%s%s", str, (flag ? ", " : ""),
26 "up");
27 flag = 1;
28 }
29 } else {
30 snprintf(str, len, "doesn't exist");
31 }
32done:
33 return str;
34}
35
36/* maps */
37
38static int calc_bits_of(unsigned t)
39{
40 int b = 0;
41 while (t) {
42 t = t >> 1;
43 b++;
44 }
45 return b;
46}
47
48/*
49 * the foo_mask is the smallest value 2^n-1 that is >= foo.
50 */
51static void calc_pg_masks(struct ceph_pg_pool_info *pi)
52{
53 pi->pg_num_mask = (1 << calc_bits_of(le32_to_cpu(pi->v.pg_num)-1)) - 1;
54 pi->pgp_num_mask =
55 (1 << calc_bits_of(le32_to_cpu(pi->v.pgp_num)-1)) - 1;
56 pi->lpg_num_mask =
57 (1 << calc_bits_of(le32_to_cpu(pi->v.lpg_num)-1)) - 1;
58 pi->lpgp_num_mask =
59 (1 << calc_bits_of(le32_to_cpu(pi->v.lpgp_num)-1)) - 1;
60}
61
62/*
63 * decode crush map
64 */
65static int crush_decode_uniform_bucket(void **p, void *end,
66 struct crush_bucket_uniform *b)
67{
68 dout("crush_decode_uniform_bucket %p to %p\n", *p, end);
69 ceph_decode_need(p, end, (1+b->h.size) * sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -070070 b->item_weight = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -070071 return 0;
72bad:
73 return -EINVAL;
74}
75
76static int crush_decode_list_bucket(void **p, void *end,
77 struct crush_bucket_list *b)
78{
79 int j;
80 dout("crush_decode_list_bucket %p to %p\n", *p, end);
81 b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
82 if (b->item_weights == NULL)
83 return -ENOMEM;
84 b->sum_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
85 if (b->sum_weights == NULL)
86 return -ENOMEM;
87 ceph_decode_need(p, end, 2 * b->h.size * sizeof(u32), bad);
88 for (j = 0; j < b->h.size; j++) {
Sage Weilc89136e2009-10-14 09:59:09 -070089 b->item_weights[j] = ceph_decode_32(p);
90 b->sum_weights[j] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -070091 }
92 return 0;
93bad:
94 return -EINVAL;
95}
96
97static int crush_decode_tree_bucket(void **p, void *end,
98 struct crush_bucket_tree *b)
99{
100 int j;
101 dout("crush_decode_tree_bucket %p to %p\n", *p, end);
102 ceph_decode_32_safe(p, end, b->num_nodes, bad);
103 b->node_weights = kcalloc(b->num_nodes, sizeof(u32), GFP_NOFS);
104 if (b->node_weights == NULL)
105 return -ENOMEM;
106 ceph_decode_need(p, end, b->num_nodes * sizeof(u32), bad);
107 for (j = 0; j < b->num_nodes; j++)
Sage Weilc89136e2009-10-14 09:59:09 -0700108 b->node_weights[j] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700109 return 0;
110bad:
111 return -EINVAL;
112}
113
114static int crush_decode_straw_bucket(void **p, void *end,
115 struct crush_bucket_straw *b)
116{
117 int j;
118 dout("crush_decode_straw_bucket %p to %p\n", *p, end);
119 b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
120 if (b->item_weights == NULL)
121 return -ENOMEM;
122 b->straws = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
123 if (b->straws == NULL)
124 return -ENOMEM;
125 ceph_decode_need(p, end, 2 * b->h.size * sizeof(u32), bad);
126 for (j = 0; j < b->h.size; j++) {
Sage Weilc89136e2009-10-14 09:59:09 -0700127 b->item_weights[j] = ceph_decode_32(p);
128 b->straws[j] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700129 }
130 return 0;
131bad:
132 return -EINVAL;
133}
134
135static struct crush_map *crush_decode(void *pbyval, void *end)
136{
137 struct crush_map *c;
138 int err = -EINVAL;
139 int i, j;
140 void **p = &pbyval;
141 void *start = pbyval;
142 u32 magic;
143
144 dout("crush_decode %p to %p len %d\n", *p, end, (int)(end - *p));
145
146 c = kzalloc(sizeof(*c), GFP_NOFS);
147 if (c == NULL)
148 return ERR_PTR(-ENOMEM);
149
150 ceph_decode_need(p, end, 4*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700151 magic = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700152 if (magic != CRUSH_MAGIC) {
153 pr_err("crush_decode magic %x != current %x\n",
154 (unsigned)magic, (unsigned)CRUSH_MAGIC);
155 goto bad;
156 }
Sage Weilc89136e2009-10-14 09:59:09 -0700157 c->max_buckets = ceph_decode_32(p);
158 c->max_rules = ceph_decode_32(p);
159 c->max_devices = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700160
161 c->device_parents = kcalloc(c->max_devices, sizeof(u32), GFP_NOFS);
162 if (c->device_parents == NULL)
163 goto badmem;
164 c->bucket_parents = kcalloc(c->max_buckets, sizeof(u32), GFP_NOFS);
165 if (c->bucket_parents == NULL)
166 goto badmem;
167
168 c->buckets = kcalloc(c->max_buckets, sizeof(*c->buckets), GFP_NOFS);
169 if (c->buckets == NULL)
170 goto badmem;
171 c->rules = kcalloc(c->max_rules, sizeof(*c->rules), GFP_NOFS);
172 if (c->rules == NULL)
173 goto badmem;
174
175 /* buckets */
176 for (i = 0; i < c->max_buckets; i++) {
177 int size = 0;
178 u32 alg;
179 struct crush_bucket *b;
180
181 ceph_decode_32_safe(p, end, alg, bad);
182 if (alg == 0) {
183 c->buckets[i] = NULL;
184 continue;
185 }
186 dout("crush_decode bucket %d off %x %p to %p\n",
187 i, (int)(*p-start), *p, end);
188
189 switch (alg) {
190 case CRUSH_BUCKET_UNIFORM:
191 size = sizeof(struct crush_bucket_uniform);
192 break;
193 case CRUSH_BUCKET_LIST:
194 size = sizeof(struct crush_bucket_list);
195 break;
196 case CRUSH_BUCKET_TREE:
197 size = sizeof(struct crush_bucket_tree);
198 break;
199 case CRUSH_BUCKET_STRAW:
200 size = sizeof(struct crush_bucket_straw);
201 break;
202 default:
203 goto bad;
204 }
205 BUG_ON(size == 0);
206 b = c->buckets[i] = kzalloc(size, GFP_NOFS);
207 if (b == NULL)
208 goto badmem;
209
210 ceph_decode_need(p, end, 4*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700211 b->id = ceph_decode_32(p);
212 b->type = ceph_decode_16(p);
213 b->alg = ceph_decode_16(p);
214 b->weight = ceph_decode_32(p);
215 b->size = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700216
217 dout("crush_decode bucket size %d off %x %p to %p\n",
218 b->size, (int)(*p-start), *p, end);
219
220 b->items = kcalloc(b->size, sizeof(__s32), GFP_NOFS);
221 if (b->items == NULL)
222 goto badmem;
223 b->perm = kcalloc(b->size, sizeof(u32), GFP_NOFS);
224 if (b->perm == NULL)
225 goto badmem;
226 b->perm_n = 0;
227
228 ceph_decode_need(p, end, b->size*sizeof(u32), bad);
229 for (j = 0; j < b->size; j++)
Sage Weilc89136e2009-10-14 09:59:09 -0700230 b->items[j] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700231
232 switch (b->alg) {
233 case CRUSH_BUCKET_UNIFORM:
234 err = crush_decode_uniform_bucket(p, end,
235 (struct crush_bucket_uniform *)b);
236 if (err < 0)
237 goto bad;
238 break;
239 case CRUSH_BUCKET_LIST:
240 err = crush_decode_list_bucket(p, end,
241 (struct crush_bucket_list *)b);
242 if (err < 0)
243 goto bad;
244 break;
245 case CRUSH_BUCKET_TREE:
246 err = crush_decode_tree_bucket(p, end,
247 (struct crush_bucket_tree *)b);
248 if (err < 0)
249 goto bad;
250 break;
251 case CRUSH_BUCKET_STRAW:
252 err = crush_decode_straw_bucket(p, end,
253 (struct crush_bucket_straw *)b);
254 if (err < 0)
255 goto bad;
256 break;
257 }
258 }
259
260 /* rules */
261 dout("rule vec is %p\n", c->rules);
262 for (i = 0; i < c->max_rules; i++) {
263 u32 yes;
264 struct crush_rule *r;
265
266 ceph_decode_32_safe(p, end, yes, bad);
267 if (!yes) {
268 dout("crush_decode NO rule %d off %x %p to %p\n",
269 i, (int)(*p-start), *p, end);
270 c->rules[i] = NULL;
271 continue;
272 }
273
274 dout("crush_decode rule %d off %x %p to %p\n",
275 i, (int)(*p-start), *p, end);
276
277 /* len */
278 ceph_decode_32_safe(p, end, yes, bad);
279#if BITS_PER_LONG == 32
280 if (yes > ULONG_MAX / sizeof(struct crush_rule_step))
281 goto bad;
282#endif
283 r = c->rules[i] = kmalloc(sizeof(*r) +
284 yes*sizeof(struct crush_rule_step),
285 GFP_NOFS);
286 if (r == NULL)
287 goto badmem;
288 dout(" rule %d is at %p\n", i, r);
289 r->len = yes;
290 ceph_decode_copy_safe(p, end, &r->mask, 4, bad); /* 4 u8's */
291 ceph_decode_need(p, end, r->len*3*sizeof(u32), bad);
292 for (j = 0; j < r->len; j++) {
Sage Weilc89136e2009-10-14 09:59:09 -0700293 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);
Sage Weilf24e9982009-10-06 11:31:10 -0700296 }
297 }
298
299 /* ignore trailing name maps. */
300
301 dout("crush_decode success\n");
302 return c;
303
304badmem:
305 err = -ENOMEM;
306bad:
307 dout("crush_decode fail %d\n", err);
308 crush_destroy(c);
309 return ERR_PTR(err);
310}
311
312
313/*
314 * osd map
315 */
316void ceph_osdmap_destroy(struct ceph_osdmap *map)
317{
318 dout("osdmap_destroy %p\n", map);
319 if (map->crush)
320 crush_destroy(map->crush);
321 while (!RB_EMPTY_ROOT(&map->pg_temp))
322 rb_erase(rb_first(&map->pg_temp), &map->pg_temp);
323 kfree(map->osd_state);
324 kfree(map->osd_weight);
325 kfree(map->pg_pool);
326 kfree(map->osd_addr);
327 kfree(map);
328}
329
330/*
331 * adjust max osd value. reallocate arrays.
332 */
333static int osdmap_set_max_osd(struct ceph_osdmap *map, int max)
334{
335 u8 *state;
336 struct ceph_entity_addr *addr;
337 u32 *weight;
338
339 state = kcalloc(max, sizeof(*state), GFP_NOFS);
340 addr = kcalloc(max, sizeof(*addr), GFP_NOFS);
341 weight = kcalloc(max, sizeof(*weight), GFP_NOFS);
342 if (state == NULL || addr == NULL || weight == NULL) {
343 kfree(state);
344 kfree(addr);
345 kfree(weight);
346 return -ENOMEM;
347 }
348
349 /* copy old? */
350 if (map->osd_state) {
351 memcpy(state, map->osd_state, map->max_osd*sizeof(*state));
352 memcpy(addr, map->osd_addr, map->max_osd*sizeof(*addr));
353 memcpy(weight, map->osd_weight, map->max_osd*sizeof(*weight));
354 kfree(map->osd_state);
355 kfree(map->osd_addr);
356 kfree(map->osd_weight);
357 }
358
359 map->osd_state = state;
360 map->osd_weight = weight;
361 map->osd_addr = addr;
362 map->max_osd = max;
363 return 0;
364}
365
366/*
367 * Insert a new pg_temp mapping
368 */
Sage Weil991abb62009-10-08 22:22:37 -0700369static int __insert_pg_mapping(struct ceph_pg_mapping *new,
370 struct rb_root *root)
Sage Weilf24e9982009-10-06 11:31:10 -0700371{
372 struct rb_node **p = &root->rb_node;
373 struct rb_node *parent = NULL;
374 struct ceph_pg_mapping *pg = NULL;
375
376 while (*p) {
377 parent = *p;
378 pg = rb_entry(parent, struct ceph_pg_mapping, node);
379 if (new->pgid < pg->pgid)
380 p = &(*p)->rb_left;
381 else if (new->pgid > pg->pgid)
382 p = &(*p)->rb_right;
383 else
Sage Weil991abb62009-10-08 22:22:37 -0700384 return -EEXIST;
Sage Weilf24e9982009-10-06 11:31:10 -0700385 }
386
387 rb_link_node(&new->node, parent, p);
388 rb_insert_color(&new->node, root);
Sage Weil991abb62009-10-08 22:22:37 -0700389 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700390}
391
392/*
393 * decode a full map.
394 */
395struct ceph_osdmap *osdmap_decode(void **p, void *end)
396{
397 struct ceph_osdmap *map;
398 u16 version;
399 u32 len, max, i;
400 int err = -EINVAL;
401 void *start = *p;
402
403 dout("osdmap_decode %p to %p len %d\n", *p, end, (int)(end - *p));
404
405 map = kzalloc(sizeof(*map), GFP_NOFS);
406 if (map == NULL)
407 return ERR_PTR(-ENOMEM);
408 map->pg_temp = RB_ROOT;
409
410 ceph_decode_16_safe(p, end, version, bad);
411
412 ceph_decode_need(p, end, 2*sizeof(u64)+6*sizeof(u32), bad);
413 ceph_decode_copy(p, &map->fsid, sizeof(map->fsid));
Sage Weilc89136e2009-10-14 09:59:09 -0700414 map->epoch = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700415 ceph_decode_copy(p, &map->created, sizeof(map->created));
416 ceph_decode_copy(p, &map->modified, sizeof(map->modified));
417
Sage Weilc89136e2009-10-14 09:59:09 -0700418 map->num_pools = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700419 map->pg_pool = kcalloc(map->num_pools, sizeof(*map->pg_pool),
420 GFP_NOFS);
421 if (!map->pg_pool) {
422 err = -ENOMEM;
423 goto bad;
424 }
425 ceph_decode_32_safe(p, end, max, bad);
426 while (max--) {
427 ceph_decode_need(p, end, 4+sizeof(map->pg_pool->v), bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700428 i = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700429 if (i >= map->num_pools)
430 goto bad;
431 ceph_decode_copy(p, &map->pg_pool[i].v,
432 sizeof(map->pg_pool->v));
433 calc_pg_masks(&map->pg_pool[i]);
434 p += le32_to_cpu(map->pg_pool[i].v.num_snaps) * sizeof(u64);
435 p += le32_to_cpu(map->pg_pool[i].v.num_removed_snap_intervals)
436 * sizeof(u64) * 2;
437 }
438
439 ceph_decode_32_safe(p, end, map->flags, bad);
440
Sage Weilc89136e2009-10-14 09:59:09 -0700441 max = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700442
443 /* (re)alloc osd arrays */
444 err = osdmap_set_max_osd(map, max);
445 if (err < 0)
446 goto bad;
447 dout("osdmap_decode max_osd = %d\n", map->max_osd);
448
449 /* osds */
450 err = -EINVAL;
451 ceph_decode_need(p, end, 3*sizeof(u32) +
452 map->max_osd*(1 + sizeof(*map->osd_weight) +
453 sizeof(*map->osd_addr)), bad);
454 *p += 4; /* skip length field (should match max) */
455 ceph_decode_copy(p, map->osd_state, map->max_osd);
456
457 *p += 4; /* skip length field (should match max) */
458 for (i = 0; i < map->max_osd; i++)
Sage Weilc89136e2009-10-14 09:59:09 -0700459 map->osd_weight[i] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700460
461 *p += 4; /* skip length field (should match max) */
462 ceph_decode_copy(p, map->osd_addr, map->max_osd*sizeof(*map->osd_addr));
Sage Weil63f2d212009-11-03 15:17:56 -0800463 for (i = 0; i < map->max_osd; i++)
464 ceph_decode_addr(&map->osd_addr[i]);
Sage Weilf24e9982009-10-06 11:31:10 -0700465
466 /* pg_temp */
467 ceph_decode_32_safe(p, end, len, bad);
468 for (i = 0; i < len; i++) {
469 int n, j;
470 u64 pgid;
471 struct ceph_pg_mapping *pg;
472
473 ceph_decode_need(p, end, sizeof(u32) + sizeof(u64), bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700474 pgid = ceph_decode_64(p);
475 n = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700476 ceph_decode_need(p, end, n * sizeof(u32), bad);
477 pg = kmalloc(sizeof(*pg) + n*sizeof(u32), GFP_NOFS);
478 if (!pg) {
479 err = -ENOMEM;
480 goto bad;
481 }
482 pg->pgid = pgid;
483 pg->len = n;
484 for (j = 0; j < n; j++)
Sage Weilc89136e2009-10-14 09:59:09 -0700485 pg->osds[j] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700486
Sage Weil991abb62009-10-08 22:22:37 -0700487 err = __insert_pg_mapping(pg, &map->pg_temp);
488 if (err)
489 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -0700490 dout(" added pg_temp %llx len %d\n", pgid, len);
491 }
492
493 /* crush */
494 ceph_decode_32_safe(p, end, len, bad);
495 dout("osdmap_decode crush len %d from off 0x%x\n", len,
496 (int)(*p - start));
497 ceph_decode_need(p, end, len, bad);
498 map->crush = crush_decode(*p, end);
499 *p += len;
500 if (IS_ERR(map->crush)) {
501 err = PTR_ERR(map->crush);
502 map->crush = NULL;
503 goto bad;
504 }
505
506 /* ignore the rest of the map */
507 *p = end;
508
509 dout("osdmap_decode done %p %p\n", *p, end);
510 return map;
511
512bad:
513 dout("osdmap_decode fail\n");
514 ceph_osdmap_destroy(map);
515 return ERR_PTR(err);
516}
517
518/*
519 * decode and apply an incremental map update.
520 */
521struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end,
522 struct ceph_osdmap *map,
523 struct ceph_messenger *msgr)
524{
525 struct ceph_osdmap *newmap = map;
526 struct crush_map *newcrush = NULL;
527 struct ceph_fsid fsid;
528 u32 epoch = 0;
529 struct ceph_timespec modified;
530 u32 len, pool;
531 __s32 new_flags, max;
532 void *start = *p;
533 int err = -EINVAL;
534 u16 version;
535 struct rb_node *rbp;
536
537 ceph_decode_16_safe(p, end, version, bad);
538
539 ceph_decode_need(p, end, sizeof(fsid)+sizeof(modified)+2*sizeof(u32),
540 bad);
541 ceph_decode_copy(p, &fsid, sizeof(fsid));
Sage Weilc89136e2009-10-14 09:59:09 -0700542 epoch = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700543 BUG_ON(epoch != map->epoch+1);
544 ceph_decode_copy(p, &modified, sizeof(modified));
Sage Weilc89136e2009-10-14 09:59:09 -0700545 new_flags = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700546
547 /* full map? */
548 ceph_decode_32_safe(p, end, len, bad);
549 if (len > 0) {
550 dout("apply_incremental full map len %d, %p to %p\n",
551 len, *p, end);
552 newmap = osdmap_decode(p, min(*p+len, end));
553 return newmap; /* error or not */
554 }
555
556 /* new crush? */
557 ceph_decode_32_safe(p, end, len, bad);
558 if (len > 0) {
559 dout("apply_incremental new crush map len %d, %p to %p\n",
560 len, *p, end);
561 newcrush = crush_decode(*p, min(*p+len, end));
562 if (IS_ERR(newcrush))
563 return ERR_PTR(PTR_ERR(newcrush));
564 }
565
566 /* new flags? */
567 if (new_flags >= 0)
568 map->flags = new_flags;
569
570 ceph_decode_need(p, end, 5*sizeof(u32), bad);
571
572 /* new max? */
Sage Weilc89136e2009-10-14 09:59:09 -0700573 max = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700574 if (max >= 0) {
575 err = osdmap_set_max_osd(map, max);
576 if (err < 0)
577 goto bad;
578 }
579
580 map->epoch++;
581 map->modified = map->modified;
582 if (newcrush) {
583 if (map->crush)
584 crush_destroy(map->crush);
585 map->crush = newcrush;
586 newcrush = NULL;
587 }
588
589 /* new_pool */
590 ceph_decode_32_safe(p, end, len, bad);
591 while (len--) {
592 ceph_decode_32_safe(p, end, pool, bad);
593 if (pool >= map->num_pools) {
594 void *pg_pool = kcalloc(pool + 1,
595 sizeof(*map->pg_pool),
596 GFP_NOFS);
597 if (!pg_pool) {
598 err = -ENOMEM;
599 goto bad;
600 }
601 memcpy(pg_pool, map->pg_pool,
602 map->num_pools * sizeof(*map->pg_pool));
603 kfree(map->pg_pool);
604 map->pg_pool = pg_pool;
605 map->num_pools = pool+1;
606 }
607 ceph_decode_copy(p, &map->pg_pool[pool].v,
608 sizeof(map->pg_pool->v));
609 calc_pg_masks(&map->pg_pool[pool]);
610 }
611
612 /* old_pool (ignore) */
613 ceph_decode_32_safe(p, end, len, bad);
614 *p += len * sizeof(u32);
615
616 /* new_up */
617 err = -EINVAL;
618 ceph_decode_32_safe(p, end, len, bad);
619 while (len--) {
620 u32 osd;
621 struct ceph_entity_addr addr;
622 ceph_decode_32_safe(p, end, osd, bad);
623 ceph_decode_copy_safe(p, end, &addr, sizeof(addr), bad);
Sage Weil63f2d212009-11-03 15:17:56 -0800624 ceph_decode_addr(&addr);
Sage Weilf24e9982009-10-06 11:31:10 -0700625 pr_info("osd%d up\n", osd);
626 BUG_ON(osd >= map->max_osd);
627 map->osd_state[osd] |= CEPH_OSD_UP;
628 map->osd_addr[osd] = addr;
629 }
630
631 /* new_down */
632 ceph_decode_32_safe(p, end, len, bad);
633 while (len--) {
634 u32 osd;
635 ceph_decode_32_safe(p, end, osd, bad);
636 (*p)++; /* clean flag */
637 pr_info("ceph osd%d down\n", osd);
638 if (osd < map->max_osd)
639 map->osd_state[osd] &= ~CEPH_OSD_UP;
640 }
641
642 /* new_weight */
643 ceph_decode_32_safe(p, end, len, bad);
644 while (len--) {
645 u32 osd, off;
646 ceph_decode_need(p, end, sizeof(u32)*2, bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700647 osd = ceph_decode_32(p);
648 off = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700649 pr_info("osd%d weight 0x%x %s\n", osd, off,
650 off == CEPH_OSD_IN ? "(in)" :
651 (off == CEPH_OSD_OUT ? "(out)" : ""));
652 if (osd < map->max_osd)
653 map->osd_weight[osd] = off;
654 }
655
656 /* new_pg_temp */
657 rbp = rb_first(&map->pg_temp);
658 ceph_decode_32_safe(p, end, len, bad);
659 while (len--) {
660 struct ceph_pg_mapping *pg;
661 int j;
662 u64 pgid;
663 u32 pglen;
664 ceph_decode_need(p, end, sizeof(u64) + sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700665 pgid = ceph_decode_64(p);
666 pglen = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700667
668 /* remove any? */
669 while (rbp && rb_entry(rbp, struct ceph_pg_mapping,
670 node)->pgid <= pgid) {
671 struct rb_node *cur = rbp;
672 rbp = rb_next(rbp);
673 dout(" removed pg_temp %llx\n",
674 rb_entry(cur, struct ceph_pg_mapping, node)->pgid);
675 rb_erase(cur, &map->pg_temp);
676 }
677
678 if (pglen) {
679 /* insert */
680 ceph_decode_need(p, end, pglen*sizeof(u32), bad);
681 pg = kmalloc(sizeof(*pg) + sizeof(u32)*pglen, GFP_NOFS);
682 if (!pg) {
683 err = -ENOMEM;
684 goto bad;
685 }
686 pg->pgid = pgid;
687 pg->len = pglen;
688 for (j = 0; j < len; j++)
Sage Weilc89136e2009-10-14 09:59:09 -0700689 pg->osds[j] = ceph_decode_32(p);
Sage Weil991abb62009-10-08 22:22:37 -0700690 err = __insert_pg_mapping(pg, &map->pg_temp);
691 if (err)
692 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -0700693 dout(" added pg_temp %llx len %d\n", pgid, pglen);
694 }
695 }
696 while (rbp) {
697 struct rb_node *cur = rbp;
698 rbp = rb_next(rbp);
699 dout(" removed pg_temp %llx\n",
700 rb_entry(cur, struct ceph_pg_mapping, node)->pgid);
701 rb_erase(cur, &map->pg_temp);
702 }
703
704 /* ignore the rest */
705 *p = end;
706 return map;
707
708bad:
709 pr_err("corrupt inc osdmap epoch %d off %d (%p of %p-%p)\n",
710 epoch, (int)(*p - start), *p, start, end);
711 if (newcrush)
712 crush_destroy(newcrush);
713 return ERR_PTR(err);
714}
715
716
717
718
719/*
720 * calculate file layout from given offset, length.
721 * fill in correct oid, logical length, and object extent
722 * offset, length.
723 *
724 * for now, we write only a single su, until we can
725 * pass a stride back to the caller.
726 */
727void ceph_calc_file_object_mapping(struct ceph_file_layout *layout,
728 u64 off, u64 *plen,
Sage Weil645a1022009-10-28 15:15:05 -0700729 u64 *ono,
Sage Weilf24e9982009-10-06 11:31:10 -0700730 u64 *oxoff, u64 *oxlen)
731{
732 u32 osize = le32_to_cpu(layout->fl_object_size);
733 u32 su = le32_to_cpu(layout->fl_stripe_unit);
734 u32 sc = le32_to_cpu(layout->fl_stripe_count);
735 u32 bl, stripeno, stripepos, objsetno;
736 u32 su_per_object;
Noah Watkinsff1d1f72009-10-30 12:57:30 -0700737 u64 t, su_offset;
Sage Weilf24e9982009-10-06 11:31:10 -0700738
739 dout("mapping %llu~%llu osize %u fl_su %u\n", off, *plen,
740 osize, su);
Noah Watkins35e054a2009-10-28 14:04:48 -0700741 su_per_object = osize / su;
Sage Weilf24e9982009-10-06 11:31:10 -0700742 dout("osize %u / su %u = su_per_object %u\n", osize, su,
743 su_per_object);
744
745 BUG_ON((su & ~PAGE_MASK) != 0);
746 /* bl = *off / su; */
747 t = off;
748 do_div(t, su);
749 bl = t;
750 dout("off %llu / su %u = bl %u\n", off, su, bl);
751
752 stripeno = bl / sc;
753 stripepos = bl % sc;
754 objsetno = stripeno / su_per_object;
755
Sage Weil645a1022009-10-28 15:15:05 -0700756 *ono = objsetno * sc + stripepos;
757 dout("objset %u * sc %u = ono %u\n", objsetno, sc, (unsigned)*ono);
758
759 /* *oxoff = *off % layout->fl_stripe_unit; # offset in su */
Sage Weilf24e9982009-10-06 11:31:10 -0700760 t = off;
Noah Watkinsff1d1f72009-10-30 12:57:30 -0700761 su_offset = do_div(t, su);
762 *oxoff = su_offset + (stripeno % su_per_object) * su;
Sage Weil645a1022009-10-28 15:15:05 -0700763
Noah Watkinsff1d1f72009-10-30 12:57:30 -0700764 /*
765 * Calculate the length of the extent being written to the selected
766 * object. This is the minimum of the full length requested (plen) or
767 * the remainder of the current stripe being written to.
768 */
769 *oxlen = min_t(u64, *plen, su - su_offset);
Sage Weilf24e9982009-10-06 11:31:10 -0700770 *plen = *oxlen;
771
772 dout(" obj extent %llu~%llu\n", *oxoff, *oxlen);
773}
774
775/*
776 * calculate an object layout (i.e. pgid) from an oid,
777 * file_layout, and osdmap
778 */
779int ceph_calc_object_layout(struct ceph_object_layout *ol,
780 const char *oid,
781 struct ceph_file_layout *fl,
782 struct ceph_osdmap *osdmap)
783{
784 unsigned num, num_mask;
785 union ceph_pg pgid;
786 s32 preferred = (s32)le32_to_cpu(fl->fl_pg_preferred);
787 int poolid = le32_to_cpu(fl->fl_pg_pool);
788 struct ceph_pg_pool_info *pool;
789
790 if (poolid >= osdmap->num_pools)
791 return -EIO;
792 pool = &osdmap->pg_pool[poolid];
793
794 if (preferred >= 0) {
795 num = le32_to_cpu(pool->v.lpg_num);
796 num_mask = pool->lpg_num_mask;
797 } else {
798 num = le32_to_cpu(pool->v.pg_num);
799 num_mask = pool->pg_num_mask;
800 }
801
802 pgid.pg64 = 0; /* start with it zeroed out */
803 pgid.pg.ps = ceph_full_name_hash(oid, strlen(oid));
804 pgid.pg.preferred = preferred;
Sage Weilee7fdfa2009-10-19 11:41:51 -0700805 if (preferred >= 0)
806 pgid.pg.ps += preferred;
Sage Weilf24e9982009-10-06 11:31:10 -0700807 pgid.pg.pool = le32_to_cpu(fl->fl_pg_pool);
808 if (preferred >= 0)
809 dout("calc_object_layout '%s' pgid %d.%xp%d (%llx)\n", oid,
810 pgid.pg.pool, pgid.pg.ps, (int)preferred, pgid.pg64);
811 else
812 dout("calc_object_layout '%s' pgid %d.%x (%llx)\n", oid,
813 pgid.pg.pool, pgid.pg.ps, pgid.pg64);
814
815 ol->ol_pgid = cpu_to_le64(pgid.pg64);
816 ol->ol_stripe_unit = fl->fl_object_stripe_unit;
817
818 return 0;
819}
820
821/*
822 * Calculate raw osd vector for the given pgid. Return pointer to osd
823 * array, or NULL on failure.
824 */
825static int *calc_pg_raw(struct ceph_osdmap *osdmap, union ceph_pg pgid,
826 int *osds, int *num)
827{
828 struct rb_node *n = osdmap->pg_temp.rb_node;
829 struct ceph_pg_mapping *pg;
830 struct ceph_pg_pool_info *pool;
831 int ruleno;
832 unsigned pps; /* placement ps */
833
834 /* pg_temp? */
835 while (n) {
836 pg = rb_entry(n, struct ceph_pg_mapping, node);
837 if (pgid.pg64 < pg->pgid)
838 n = n->rb_left;
839 else if (pgid.pg64 > pg->pgid)
840 n = n->rb_right;
841 else {
842 *num = pg->len;
843 return pg->osds;
844 }
845 }
846
847 /* crush */
848 if (pgid.pg.pool >= osdmap->num_pools)
849 return NULL;
850 pool = &osdmap->pg_pool[pgid.pg.pool];
851 ruleno = crush_find_rule(osdmap->crush, pool->v.crush_ruleset,
852 pool->v.type, pool->v.size);
853 if (ruleno < 0) {
854 pr_err("no crush rule pool %d type %d size %d\n",
855 pgid.pg.pool, pool->v.type, pool->v.size);
856 return NULL;
857 }
858
859 if (pgid.pg.preferred >= 0)
860 pps = ceph_stable_mod(pgid.pg.ps,
861 le32_to_cpu(pool->v.lpgp_num),
862 pool->lpgp_num_mask);
863 else
864 pps = ceph_stable_mod(pgid.pg.ps,
865 le32_to_cpu(pool->v.pgp_num),
866 pool->pgp_num_mask);
867 pps += pgid.pg.pool;
868 *num = crush_do_rule(osdmap->crush, ruleno, pps, osds,
869 min_t(int, pool->v.size, *num),
870 pgid.pg.preferred, osdmap->osd_weight);
871 return osds;
872}
873
874/*
875 * Return primary osd for given pgid, or -1 if none.
876 */
877int ceph_calc_pg_primary(struct ceph_osdmap *osdmap, union ceph_pg pgid)
878{
879 int rawosds[10], *osds;
880 int i, num = ARRAY_SIZE(rawosds);
881
882 osds = calc_pg_raw(osdmap, pgid, rawosds, &num);
883 if (!osds)
884 return -1;
885
886 /* primary is first up osd */
887 for (i = 0; i < num; i++)
888 if (ceph_osd_is_up(osdmap, osds[i])) {
889 return osds[i];
890 break;
891 }
892 return -1;
893}