blob: a9416308de6f32d7c9a8c25fc72f10daeaa964ab [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:
Sage Weil30dc6382009-12-21 14:49:37 -0800203 err = -EINVAL;
Sage Weilf24e9982009-10-06 11:31:10 -0700204 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);
Sage Weilc89136e2009-10-14 09:59:09 -0700212 b->id = ceph_decode_32(p);
213 b->type = ceph_decode_16(p);
Sage Weilfb690392009-11-07 20:18:22 -0800214 b->alg = ceph_decode_8(p);
215 b->hash = ceph_decode_8(p);
Sage Weilc89136e2009-10-14 09:59:09 -0700216 b->weight = ceph_decode_32(p);
217 b->size = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700218
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++)
Sage Weilc89136e2009-10-14 09:59:09 -0700232 b->items[j] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700233
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
Sage Weil30dc6382009-12-21 14:49:37 -0800282 err = -EINVAL;
Sage Weilf24e9982009-10-06 11:31:10 -0700283 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++) {
Sage Weilc89136e2009-10-14 09:59:09 -0700296 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);
Sage Weilf24e9982009-10-06 11:31:10 -0700299 }
300 }
301
302 /* ignore trailing name maps. */
303
304 dout("crush_decode success\n");
305 return c;
306
307badmem:
308 err = -ENOMEM;
309bad:
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 */
319void ceph_osdmap_destroy(struct ceph_osdmap *map)
320{
321 dout("osdmap_destroy %p\n", map);
322 if (map->crush)
323 crush_destroy(map->crush);
324 while (!RB_EMPTY_ROOT(&map->pg_temp))
325 rb_erase(rb_first(&map->pg_temp), &map->pg_temp);
326 kfree(map->osd_state);
327 kfree(map->osd_weight);
328 kfree(map->pg_pool);
329 kfree(map->osd_addr);
330 kfree(map);
331}
332
333/*
334 * adjust max osd value. reallocate arrays.
335 */
336static int osdmap_set_max_osd(struct ceph_osdmap *map, int max)
337{
338 u8 *state;
339 struct ceph_entity_addr *addr;
340 u32 *weight;
341
342 state = kcalloc(max, sizeof(*state), GFP_NOFS);
343 addr = kcalloc(max, sizeof(*addr), GFP_NOFS);
344 weight = kcalloc(max, sizeof(*weight), GFP_NOFS);
345 if (state == NULL || addr == NULL || weight == NULL) {
346 kfree(state);
347 kfree(addr);
348 kfree(weight);
349 return -ENOMEM;
350 }
351
352 /* copy old? */
353 if (map->osd_state) {
354 memcpy(state, map->osd_state, map->max_osd*sizeof(*state));
355 memcpy(addr, map->osd_addr, map->max_osd*sizeof(*addr));
356 memcpy(weight, map->osd_weight, map->max_osd*sizeof(*weight));
357 kfree(map->osd_state);
358 kfree(map->osd_addr);
359 kfree(map->osd_weight);
360 }
361
362 map->osd_state = state;
363 map->osd_weight = weight;
364 map->osd_addr = addr;
365 map->max_osd = max;
366 return 0;
367}
368
369/*
370 * Insert a new pg_temp mapping
371 */
Sage Weil51042122009-11-04 11:39:12 -0800372static int pgid_cmp(struct ceph_pg l, struct ceph_pg r)
373{
374 u64 a = *(u64 *)&l;
375 u64 b = *(u64 *)&r;
376
377 if (a < b)
378 return -1;
379 if (a > b)
380 return 1;
381 return 0;
382}
383
Sage Weil991abb62009-10-08 22:22:37 -0700384static int __insert_pg_mapping(struct ceph_pg_mapping *new,
385 struct rb_root *root)
Sage Weilf24e9982009-10-06 11:31:10 -0700386{
387 struct rb_node **p = &root->rb_node;
388 struct rb_node *parent = NULL;
389 struct ceph_pg_mapping *pg = NULL;
Sage Weil51042122009-11-04 11:39:12 -0800390 int c;
Sage Weilf24e9982009-10-06 11:31:10 -0700391
392 while (*p) {
393 parent = *p;
394 pg = rb_entry(parent, struct ceph_pg_mapping, node);
Sage Weil51042122009-11-04 11:39:12 -0800395 c = pgid_cmp(new->pgid, pg->pgid);
396 if (c < 0)
Sage Weilf24e9982009-10-06 11:31:10 -0700397 p = &(*p)->rb_left;
Sage Weil51042122009-11-04 11:39:12 -0800398 else if (c > 0)
Sage Weilf24e9982009-10-06 11:31:10 -0700399 p = &(*p)->rb_right;
400 else
Sage Weil991abb62009-10-08 22:22:37 -0700401 return -EEXIST;
Sage Weilf24e9982009-10-06 11:31:10 -0700402 }
403
404 rb_link_node(&new->node, parent, p);
405 rb_insert_color(&new->node, root);
Sage Weil991abb62009-10-08 22:22:37 -0700406 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700407}
408
409/*
410 * decode a full map.
411 */
412struct ceph_osdmap *osdmap_decode(void **p, void *end)
413{
414 struct ceph_osdmap *map;
415 u16 version;
416 u32 len, max, i;
417 int err = -EINVAL;
418 void *start = *p;
419
420 dout("osdmap_decode %p to %p len %d\n", *p, end, (int)(end - *p));
421
422 map = kzalloc(sizeof(*map), GFP_NOFS);
423 if (map == NULL)
424 return ERR_PTR(-ENOMEM);
425 map->pg_temp = RB_ROOT;
426
427 ceph_decode_16_safe(p, end, version, bad);
428
429 ceph_decode_need(p, end, 2*sizeof(u64)+6*sizeof(u32), bad);
430 ceph_decode_copy(p, &map->fsid, sizeof(map->fsid));
Sage Weilc89136e2009-10-14 09:59:09 -0700431 map->epoch = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700432 ceph_decode_copy(p, &map->created, sizeof(map->created));
433 ceph_decode_copy(p, &map->modified, sizeof(map->modified));
434
Sage Weilc89136e2009-10-14 09:59:09 -0700435 map->num_pools = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700436 map->pg_pool = kcalloc(map->num_pools, sizeof(*map->pg_pool),
437 GFP_NOFS);
438 if (!map->pg_pool) {
439 err = -ENOMEM;
440 goto bad;
441 }
442 ceph_decode_32_safe(p, end, max, bad);
443 while (max--) {
444 ceph_decode_need(p, end, 4+sizeof(map->pg_pool->v), bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700445 i = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700446 if (i >= map->num_pools)
447 goto bad;
448 ceph_decode_copy(p, &map->pg_pool[i].v,
449 sizeof(map->pg_pool->v));
450 calc_pg_masks(&map->pg_pool[i]);
451 p += le32_to_cpu(map->pg_pool[i].v.num_snaps) * sizeof(u64);
452 p += le32_to_cpu(map->pg_pool[i].v.num_removed_snap_intervals)
453 * sizeof(u64) * 2;
454 }
455
456 ceph_decode_32_safe(p, end, map->flags, bad);
457
Sage Weilc89136e2009-10-14 09:59:09 -0700458 max = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700459
460 /* (re)alloc osd arrays */
461 err = osdmap_set_max_osd(map, max);
462 if (err < 0)
463 goto bad;
464 dout("osdmap_decode max_osd = %d\n", map->max_osd);
465
466 /* osds */
467 err = -EINVAL;
468 ceph_decode_need(p, end, 3*sizeof(u32) +
469 map->max_osd*(1 + sizeof(*map->osd_weight) +
470 sizeof(*map->osd_addr)), bad);
471 *p += 4; /* skip length field (should match max) */
472 ceph_decode_copy(p, map->osd_state, map->max_osd);
473
474 *p += 4; /* skip length field (should match max) */
475 for (i = 0; i < map->max_osd; i++)
Sage Weilc89136e2009-10-14 09:59:09 -0700476 map->osd_weight[i] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700477
478 *p += 4; /* skip length field (should match max) */
479 ceph_decode_copy(p, map->osd_addr, map->max_osd*sizeof(*map->osd_addr));
Sage Weil63f2d212009-11-03 15:17:56 -0800480 for (i = 0; i < map->max_osd; i++)
481 ceph_decode_addr(&map->osd_addr[i]);
Sage Weilf24e9982009-10-06 11:31:10 -0700482
483 /* pg_temp */
484 ceph_decode_32_safe(p, end, len, bad);
485 for (i = 0; i < len; i++) {
486 int n, j;
Sage Weil51042122009-11-04 11:39:12 -0800487 struct ceph_pg pgid;
Sage Weilf24e9982009-10-06 11:31:10 -0700488 struct ceph_pg_mapping *pg;
489
490 ceph_decode_need(p, end, sizeof(u32) + sizeof(u64), bad);
Sage Weil51042122009-11-04 11:39:12 -0800491 ceph_decode_copy(p, &pgid, sizeof(pgid));
Sage Weilc89136e2009-10-14 09:59:09 -0700492 n = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700493 ceph_decode_need(p, end, n * sizeof(u32), bad);
Sage Weil30dc6382009-12-21 14:49:37 -0800494 err = -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -0700495 pg = kmalloc(sizeof(*pg) + n*sizeof(u32), GFP_NOFS);
Sage Weil30dc6382009-12-21 14:49:37 -0800496 if (!pg)
Sage Weilf24e9982009-10-06 11:31:10 -0700497 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -0700498 pg->pgid = pgid;
499 pg->len = n;
500 for (j = 0; j < n; j++)
Sage Weilc89136e2009-10-14 09:59:09 -0700501 pg->osds[j] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700502
Sage Weil991abb62009-10-08 22:22:37 -0700503 err = __insert_pg_mapping(pg, &map->pg_temp);
504 if (err)
505 goto bad;
Sage Weil51042122009-11-04 11:39:12 -0800506 dout(" added pg_temp %llx len %d\n", *(u64 *)&pgid, len);
Sage Weilf24e9982009-10-06 11:31:10 -0700507 }
508
509 /* crush */
510 ceph_decode_32_safe(p, end, len, bad);
511 dout("osdmap_decode crush len %d from off 0x%x\n", len,
512 (int)(*p - start));
513 ceph_decode_need(p, end, len, bad);
514 map->crush = crush_decode(*p, end);
515 *p += len;
516 if (IS_ERR(map->crush)) {
517 err = PTR_ERR(map->crush);
518 map->crush = NULL;
519 goto bad;
520 }
521
522 /* ignore the rest of the map */
523 *p = end;
524
525 dout("osdmap_decode done %p %p\n", *p, end);
526 return map;
527
528bad:
529 dout("osdmap_decode fail\n");
530 ceph_osdmap_destroy(map);
531 return ERR_PTR(err);
532}
533
534/*
535 * decode and apply an incremental map update.
536 */
537struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end,
538 struct ceph_osdmap *map,
539 struct ceph_messenger *msgr)
540{
541 struct ceph_osdmap *newmap = map;
542 struct crush_map *newcrush = NULL;
543 struct ceph_fsid fsid;
544 u32 epoch = 0;
545 struct ceph_timespec modified;
546 u32 len, pool;
547 __s32 new_flags, max;
548 void *start = *p;
549 int err = -EINVAL;
550 u16 version;
551 struct rb_node *rbp;
552
553 ceph_decode_16_safe(p, end, version, bad);
554
555 ceph_decode_need(p, end, sizeof(fsid)+sizeof(modified)+2*sizeof(u32),
556 bad);
557 ceph_decode_copy(p, &fsid, sizeof(fsid));
Sage Weilc89136e2009-10-14 09:59:09 -0700558 epoch = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700559 BUG_ON(epoch != map->epoch+1);
560 ceph_decode_copy(p, &modified, sizeof(modified));
Sage Weilc89136e2009-10-14 09:59:09 -0700561 new_flags = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700562
563 /* full map? */
564 ceph_decode_32_safe(p, end, len, bad);
565 if (len > 0) {
566 dout("apply_incremental full map len %d, %p to %p\n",
567 len, *p, end);
Sage Weil30dc6382009-12-21 14:49:37 -0800568 return osdmap_decode(p, min(*p+len, end));
Sage Weilf24e9982009-10-06 11:31:10 -0700569 }
570
571 /* new crush? */
572 ceph_decode_32_safe(p, end, len, bad);
573 if (len > 0) {
574 dout("apply_incremental new crush map len %d, %p to %p\n",
575 len, *p, end);
576 newcrush = crush_decode(*p, min(*p+len, end));
577 if (IS_ERR(newcrush))
578 return ERR_PTR(PTR_ERR(newcrush));
579 }
580
581 /* new flags? */
582 if (new_flags >= 0)
583 map->flags = new_flags;
584
585 ceph_decode_need(p, end, 5*sizeof(u32), bad);
586
587 /* new max? */
Sage Weilc89136e2009-10-14 09:59:09 -0700588 max = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700589 if (max >= 0) {
590 err = osdmap_set_max_osd(map, max);
591 if (err < 0)
592 goto bad;
593 }
594
595 map->epoch++;
596 map->modified = map->modified;
597 if (newcrush) {
598 if (map->crush)
599 crush_destroy(map->crush);
600 map->crush = newcrush;
601 newcrush = NULL;
602 }
603
604 /* new_pool */
605 ceph_decode_32_safe(p, end, len, bad);
606 while (len--) {
607 ceph_decode_32_safe(p, end, pool, bad);
608 if (pool >= map->num_pools) {
609 void *pg_pool = kcalloc(pool + 1,
610 sizeof(*map->pg_pool),
611 GFP_NOFS);
612 if (!pg_pool) {
613 err = -ENOMEM;
614 goto bad;
615 }
616 memcpy(pg_pool, map->pg_pool,
617 map->num_pools * sizeof(*map->pg_pool));
618 kfree(map->pg_pool);
619 map->pg_pool = pg_pool;
620 map->num_pools = pool+1;
621 }
622 ceph_decode_copy(p, &map->pg_pool[pool].v,
623 sizeof(map->pg_pool->v));
624 calc_pg_masks(&map->pg_pool[pool]);
625 }
626
627 /* old_pool (ignore) */
628 ceph_decode_32_safe(p, end, len, bad);
629 *p += len * sizeof(u32);
630
631 /* new_up */
632 err = -EINVAL;
633 ceph_decode_32_safe(p, end, len, bad);
634 while (len--) {
635 u32 osd;
636 struct ceph_entity_addr addr;
637 ceph_decode_32_safe(p, end, osd, bad);
638 ceph_decode_copy_safe(p, end, &addr, sizeof(addr), bad);
Sage Weil63f2d212009-11-03 15:17:56 -0800639 ceph_decode_addr(&addr);
Sage Weilf24e9982009-10-06 11:31:10 -0700640 pr_info("osd%d up\n", osd);
641 BUG_ON(osd >= map->max_osd);
642 map->osd_state[osd] |= CEPH_OSD_UP;
643 map->osd_addr[osd] = addr;
644 }
645
646 /* new_down */
647 ceph_decode_32_safe(p, end, len, bad);
648 while (len--) {
649 u32 osd;
650 ceph_decode_32_safe(p, end, osd, bad);
651 (*p)++; /* clean flag */
Sage Weil1bdb70e2009-11-06 13:57:49 -0800652 pr_info("osd%d down\n", osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700653 if (osd < map->max_osd)
654 map->osd_state[osd] &= ~CEPH_OSD_UP;
655 }
656
657 /* new_weight */
658 ceph_decode_32_safe(p, end, len, bad);
659 while (len--) {
660 u32 osd, off;
661 ceph_decode_need(p, end, sizeof(u32)*2, bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700662 osd = ceph_decode_32(p);
663 off = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700664 pr_info("osd%d weight 0x%x %s\n", osd, off,
665 off == CEPH_OSD_IN ? "(in)" :
666 (off == CEPH_OSD_OUT ? "(out)" : ""));
667 if (osd < map->max_osd)
668 map->osd_weight[osd] = off;
669 }
670
671 /* new_pg_temp */
672 rbp = rb_first(&map->pg_temp);
673 ceph_decode_32_safe(p, end, len, bad);
674 while (len--) {
675 struct ceph_pg_mapping *pg;
676 int j;
Sage Weil51042122009-11-04 11:39:12 -0800677 struct ceph_pg pgid;
Sage Weilf24e9982009-10-06 11:31:10 -0700678 u32 pglen;
679 ceph_decode_need(p, end, sizeof(u64) + sizeof(u32), bad);
Sage Weil51042122009-11-04 11:39:12 -0800680 ceph_decode_copy(p, &pgid, sizeof(pgid));
Sage Weilc89136e2009-10-14 09:59:09 -0700681 pglen = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700682
683 /* remove any? */
Sage Weil51042122009-11-04 11:39:12 -0800684 while (rbp && pgid_cmp(rb_entry(rbp, struct ceph_pg_mapping,
685 node)->pgid, pgid) <= 0) {
Sage Weilf24e9982009-10-06 11:31:10 -0700686 struct rb_node *cur = rbp;
687 rbp = rb_next(rbp);
688 dout(" removed pg_temp %llx\n",
Sage Weil51042122009-11-04 11:39:12 -0800689 *(u64 *)&rb_entry(cur, struct ceph_pg_mapping,
690 node)->pgid);
Sage Weilf24e9982009-10-06 11:31:10 -0700691 rb_erase(cur, &map->pg_temp);
692 }
693
694 if (pglen) {
695 /* insert */
696 ceph_decode_need(p, end, pglen*sizeof(u32), bad);
697 pg = kmalloc(sizeof(*pg) + sizeof(u32)*pglen, GFP_NOFS);
698 if (!pg) {
699 err = -ENOMEM;
700 goto bad;
701 }
702 pg->pgid = pgid;
703 pg->len = pglen;
704 for (j = 0; j < len; j++)
Sage Weilc89136e2009-10-14 09:59:09 -0700705 pg->osds[j] = ceph_decode_32(p);
Sage Weil991abb62009-10-08 22:22:37 -0700706 err = __insert_pg_mapping(pg, &map->pg_temp);
707 if (err)
708 goto bad;
Sage Weil51042122009-11-04 11:39:12 -0800709 dout(" added pg_temp %llx len %d\n", *(u64 *)&pgid,
710 pglen);
Sage Weilf24e9982009-10-06 11:31:10 -0700711 }
712 }
713 while (rbp) {
714 struct rb_node *cur = rbp;
715 rbp = rb_next(rbp);
716 dout(" removed pg_temp %llx\n",
Sage Weil51042122009-11-04 11:39:12 -0800717 *(u64 *)&rb_entry(cur, struct ceph_pg_mapping,
718 node)->pgid);
Sage Weilf24e9982009-10-06 11:31:10 -0700719 rb_erase(cur, &map->pg_temp);
720 }
721
722 /* ignore the rest */
723 *p = end;
724 return map;
725
726bad:
727 pr_err("corrupt inc osdmap epoch %d off %d (%p of %p-%p)\n",
728 epoch, (int)(*p - start), *p, start, end);
Sage Weil9ec7cab2009-12-14 15:13:47 -0800729 print_hex_dump(KERN_DEBUG, "osdmap: ",
730 DUMP_PREFIX_OFFSET, 16, 1,
731 start, end - start, true);
Sage Weilf24e9982009-10-06 11:31:10 -0700732 if (newcrush)
733 crush_destroy(newcrush);
734 return ERR_PTR(err);
735}
736
737
738
739
740/*
741 * calculate file layout from given offset, length.
742 * fill in correct oid, logical length, and object extent
743 * offset, length.
744 *
745 * for now, we write only a single su, until we can
746 * pass a stride back to the caller.
747 */
748void ceph_calc_file_object_mapping(struct ceph_file_layout *layout,
749 u64 off, u64 *plen,
Sage Weil645a1022009-10-28 15:15:05 -0700750 u64 *ono,
Sage Weilf24e9982009-10-06 11:31:10 -0700751 u64 *oxoff, u64 *oxlen)
752{
753 u32 osize = le32_to_cpu(layout->fl_object_size);
754 u32 su = le32_to_cpu(layout->fl_stripe_unit);
755 u32 sc = le32_to_cpu(layout->fl_stripe_count);
756 u32 bl, stripeno, stripepos, objsetno;
757 u32 su_per_object;
Noah Watkinsff1d1f72009-10-30 12:57:30 -0700758 u64 t, su_offset;
Sage Weilf24e9982009-10-06 11:31:10 -0700759
760 dout("mapping %llu~%llu osize %u fl_su %u\n", off, *plen,
761 osize, su);
Noah Watkins35e054a2009-10-28 14:04:48 -0700762 su_per_object = osize / su;
Sage Weilf24e9982009-10-06 11:31:10 -0700763 dout("osize %u / su %u = su_per_object %u\n", osize, su,
764 su_per_object);
765
766 BUG_ON((su & ~PAGE_MASK) != 0);
767 /* bl = *off / su; */
768 t = off;
769 do_div(t, su);
770 bl = t;
771 dout("off %llu / su %u = bl %u\n", off, su, bl);
772
773 stripeno = bl / sc;
774 stripepos = bl % sc;
775 objsetno = stripeno / su_per_object;
776
Sage Weil645a1022009-10-28 15:15:05 -0700777 *ono = objsetno * sc + stripepos;
778 dout("objset %u * sc %u = ono %u\n", objsetno, sc, (unsigned)*ono);
779
780 /* *oxoff = *off % layout->fl_stripe_unit; # offset in su */
Sage Weilf24e9982009-10-06 11:31:10 -0700781 t = off;
Noah Watkinsff1d1f72009-10-30 12:57:30 -0700782 su_offset = do_div(t, su);
783 *oxoff = su_offset + (stripeno % su_per_object) * su;
Sage Weil645a1022009-10-28 15:15:05 -0700784
Noah Watkinsff1d1f72009-10-30 12:57:30 -0700785 /*
786 * Calculate the length of the extent being written to the selected
787 * object. This is the minimum of the full length requested (plen) or
788 * the remainder of the current stripe being written to.
789 */
790 *oxlen = min_t(u64, *plen, su - su_offset);
Sage Weilf24e9982009-10-06 11:31:10 -0700791 *plen = *oxlen;
792
793 dout(" obj extent %llu~%llu\n", *oxoff, *oxlen);
794}
795
796/*
797 * calculate an object layout (i.e. pgid) from an oid,
798 * file_layout, and osdmap
799 */
800int ceph_calc_object_layout(struct ceph_object_layout *ol,
801 const char *oid,
802 struct ceph_file_layout *fl,
803 struct ceph_osdmap *osdmap)
804{
805 unsigned num, num_mask;
Sage Weil51042122009-11-04 11:39:12 -0800806 struct ceph_pg pgid;
Sage Weilf24e9982009-10-06 11:31:10 -0700807 s32 preferred = (s32)le32_to_cpu(fl->fl_pg_preferred);
808 int poolid = le32_to_cpu(fl->fl_pg_pool);
809 struct ceph_pg_pool_info *pool;
Sage Weil51042122009-11-04 11:39:12 -0800810 unsigned ps;
Sage Weilf24e9982009-10-06 11:31:10 -0700811
Sage Weil30dc6382009-12-21 14:49:37 -0800812 BUG_ON(!osdmap);
Sage Weilf24e9982009-10-06 11:31:10 -0700813 if (poolid >= osdmap->num_pools)
814 return -EIO;
Sage Weilf24e9982009-10-06 11:31:10 -0700815
Sage Weil51042122009-11-04 11:39:12 -0800816 pool = &osdmap->pg_pool[poolid];
Sage Weil1654dd02009-11-06 21:55:25 -0800817 ps = ceph_str_hash(pool->v.object_hash, oid, strlen(oid));
Sage Weilf24e9982009-10-06 11:31:10 -0700818 if (preferred >= 0) {
Sage Weil51042122009-11-04 11:39:12 -0800819 ps += preferred;
Sage Weilf24e9982009-10-06 11:31:10 -0700820 num = le32_to_cpu(pool->v.lpg_num);
821 num_mask = pool->lpg_num_mask;
822 } else {
823 num = le32_to_cpu(pool->v.pg_num);
824 num_mask = pool->pg_num_mask;
825 }
826
Sage Weil51042122009-11-04 11:39:12 -0800827 pgid.ps = cpu_to_le16(ps);
828 pgid.preferred = cpu_to_le16(preferred);
829 pgid.pool = fl->fl_pg_pool;
Sage Weilee7fdfa2009-10-19 11:41:51 -0700830 if (preferred >= 0)
Sage Weil51042122009-11-04 11:39:12 -0800831 dout("calc_object_layout '%s' pgid %d.%xp%d\n", oid, poolid, ps,
832 (int)preferred);
Sage Weilf24e9982009-10-06 11:31:10 -0700833 else
Sage Weil51042122009-11-04 11:39:12 -0800834 dout("calc_object_layout '%s' pgid %d.%x\n", oid, poolid, ps);
Sage Weilf24e9982009-10-06 11:31:10 -0700835
Sage Weil51042122009-11-04 11:39:12 -0800836 ol->ol_pgid = pgid;
Sage Weilf24e9982009-10-06 11:31:10 -0700837 ol->ol_stripe_unit = fl->fl_object_stripe_unit;
Sage Weilf24e9982009-10-06 11:31:10 -0700838 return 0;
839}
840
841/*
842 * Calculate raw osd vector for the given pgid. Return pointer to osd
843 * array, or NULL on failure.
844 */
Sage Weil51042122009-11-04 11:39:12 -0800845static int *calc_pg_raw(struct ceph_osdmap *osdmap, struct ceph_pg pgid,
Sage Weilf24e9982009-10-06 11:31:10 -0700846 int *osds, int *num)
847{
848 struct rb_node *n = osdmap->pg_temp.rb_node;
849 struct ceph_pg_mapping *pg;
850 struct ceph_pg_pool_info *pool;
851 int ruleno;
Sage Weil51042122009-11-04 11:39:12 -0800852 unsigned poolid, ps, pps;
853 int preferred;
854 int c;
Sage Weilf24e9982009-10-06 11:31:10 -0700855
856 /* pg_temp? */
857 while (n) {
858 pg = rb_entry(n, struct ceph_pg_mapping, node);
Sage Weil51042122009-11-04 11:39:12 -0800859 c = pgid_cmp(pgid, pg->pgid);
860 if (c < 0)
Sage Weilf24e9982009-10-06 11:31:10 -0700861 n = n->rb_left;
Sage Weil51042122009-11-04 11:39:12 -0800862 else if (c > 0)
Sage Weilf24e9982009-10-06 11:31:10 -0700863 n = n->rb_right;
864 else {
865 *num = pg->len;
866 return pg->osds;
867 }
868 }
869
870 /* crush */
Sage Weil51042122009-11-04 11:39:12 -0800871 poolid = le32_to_cpu(pgid.pool);
872 ps = le16_to_cpu(pgid.ps);
873 preferred = (s16)le16_to_cpu(pgid.preferred);
874
Sage Weil767ea5c2009-12-09 12:34:01 -0800875 /* don't forcefeed bad device ids to crush */
876 if (preferred >= osdmap->max_osd ||
877 preferred >= osdmap->crush->max_devices)
878 preferred = -1;
879
Sage Weil51042122009-11-04 11:39:12 -0800880 if (poolid >= osdmap->num_pools)
Sage Weilf24e9982009-10-06 11:31:10 -0700881 return NULL;
Sage Weil51042122009-11-04 11:39:12 -0800882 pool = &osdmap->pg_pool[poolid];
Sage Weilf24e9982009-10-06 11:31:10 -0700883 ruleno = crush_find_rule(osdmap->crush, pool->v.crush_ruleset,
884 pool->v.type, pool->v.size);
885 if (ruleno < 0) {
886 pr_err("no crush rule pool %d type %d size %d\n",
Sage Weil51042122009-11-04 11:39:12 -0800887 poolid, pool->v.type, pool->v.size);
Sage Weilf24e9982009-10-06 11:31:10 -0700888 return NULL;
889 }
890
Sage Weil51042122009-11-04 11:39:12 -0800891 if (preferred >= 0)
892 pps = ceph_stable_mod(ps,
Sage Weilf24e9982009-10-06 11:31:10 -0700893 le32_to_cpu(pool->v.lpgp_num),
894 pool->lpgp_num_mask);
895 else
Sage Weil51042122009-11-04 11:39:12 -0800896 pps = ceph_stable_mod(ps,
Sage Weilf24e9982009-10-06 11:31:10 -0700897 le32_to_cpu(pool->v.pgp_num),
898 pool->pgp_num_mask);
Sage Weil51042122009-11-04 11:39:12 -0800899 pps += poolid;
Sage Weilf24e9982009-10-06 11:31:10 -0700900 *num = crush_do_rule(osdmap->crush, ruleno, pps, osds,
901 min_t(int, pool->v.size, *num),
Sage Weil51042122009-11-04 11:39:12 -0800902 preferred, osdmap->osd_weight);
Sage Weilf24e9982009-10-06 11:31:10 -0700903 return osds;
904}
905
906/*
907 * Return primary osd for given pgid, or -1 if none.
908 */
Sage Weil51042122009-11-04 11:39:12 -0800909int ceph_calc_pg_primary(struct ceph_osdmap *osdmap, struct ceph_pg pgid)
Sage Weilf24e9982009-10-06 11:31:10 -0700910{
911 int rawosds[10], *osds;
912 int i, num = ARRAY_SIZE(rawosds);
913
914 osds = calc_pg_raw(osdmap, pgid, rawosds, &num);
915 if (!osds)
916 return -1;
917
918 /* primary is first up osd */
919 for (i = 0; i < num; i++)
920 if (ceph_osd_is_up(osdmap, osds[i])) {
921 return osds[i];
922 break;
923 }
924 return -1;
925}