blob: d82fe87c2a6e62f2a825ebb859158306e73a1448 [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);
Sage Weil9794b142010-02-16 15:53:32 -0800324 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 }
Sage Weil4fc51be2010-02-16 15:55:03 -0800331 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 }
Sage Weilf24e9982009-10-06 11:31:10 -0700338 kfree(map->osd_state);
339 kfree(map->osd_weight);
Sage Weilf24e9982009-10-06 11:31:10 -0700340 kfree(map->osd_addr);
341 kfree(map);
342}
343
344/*
345 * adjust max osd value. reallocate arrays.
346 */
347static 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/*
Sage Weil9794b142010-02-16 15:53:32 -0800381 * rbtree of pg_mapping for handling pg_temp (explicit mapping of pgid
382 * to a set of osds)
Sage Weilf24e9982009-10-06 11:31:10 -0700383 */
Sage Weil51042122009-11-04 11:39:12 -0800384static 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
Sage Weil991abb62009-10-08 22:22:37 -0700396static int __insert_pg_mapping(struct ceph_pg_mapping *new,
397 struct rb_root *root)
Sage Weilf24e9982009-10-06 11:31:10 -0700398{
399 struct rb_node **p = &root->rb_node;
400 struct rb_node *parent = NULL;
401 struct ceph_pg_mapping *pg = NULL;
Sage Weil51042122009-11-04 11:39:12 -0800402 int c;
Sage Weilf24e9982009-10-06 11:31:10 -0700403
404 while (*p) {
405 parent = *p;
406 pg = rb_entry(parent, struct ceph_pg_mapping, node);
Sage Weil51042122009-11-04 11:39:12 -0800407 c = pgid_cmp(new->pgid, pg->pgid);
408 if (c < 0)
Sage Weilf24e9982009-10-06 11:31:10 -0700409 p = &(*p)->rb_left;
Sage Weil51042122009-11-04 11:39:12 -0800410 else if (c > 0)
Sage Weilf24e9982009-10-06 11:31:10 -0700411 p = &(*p)->rb_right;
412 else
Sage Weil991abb62009-10-08 22:22:37 -0700413 return -EEXIST;
Sage Weilf24e9982009-10-06 11:31:10 -0700414 }
415
416 rb_link_node(&new->node, parent, p);
417 rb_insert_color(&new->node, root);
Sage Weil991abb62009-10-08 22:22:37 -0700418 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700419}
420
Sage Weil9794b142010-02-16 15:53:32 -0800421static 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
Sage Weilf24e9982009-10-06 11:31:10 -0700441/*
Sage Weil4fc51be2010-02-16 15:55:03 -0800442 * rbtree of pg pool info
443 */
444static 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
466static 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
Sage Weilefd75762010-03-17 10:05:28 -0700483void __decode_pool(void **p, struct ceph_pg_pool_info *pi)
484{
485 ceph_decode_copy(p, &pi->v, sizeof(pi->v));
486 calc_pg_masks(pi);
487 *p += le32_to_cpu(pi->v.num_snaps) * sizeof(u64);
488 *p += le32_to_cpu(pi->v.num_removed_snap_intervals) * sizeof(u64) * 2;
489}
490
Sage Weil4fc51be2010-02-16 15:55:03 -0800491/*
Sage Weilf24e9982009-10-06 11:31:10 -0700492 * decode a full map.
493 */
494struct ceph_osdmap *osdmap_decode(void **p, void *end)
495{
496 struct ceph_osdmap *map;
497 u16 version;
498 u32 len, max, i;
Sage Weil361be862010-01-25 16:03:02 -0800499 u8 ev;
Sage Weilf24e9982009-10-06 11:31:10 -0700500 int err = -EINVAL;
501 void *start = *p;
Sage Weil4fc51be2010-02-16 15:55:03 -0800502 struct ceph_pg_pool_info *pi;
Sage Weilf24e9982009-10-06 11:31:10 -0700503
504 dout("osdmap_decode %p to %p len %d\n", *p, end, (int)(end - *p));
505
506 map = kzalloc(sizeof(*map), GFP_NOFS);
507 if (map == NULL)
508 return ERR_PTR(-ENOMEM);
509 map->pg_temp = RB_ROOT;
510
511 ceph_decode_16_safe(p, end, version, bad);
Sage Weil02f90c62010-02-04 16:18:10 -0800512 if (version > CEPH_OSDMAP_VERSION) {
513 pr_warning("got unknown v %d > %d of osdmap\n", version,
514 CEPH_OSDMAP_VERSION);
515 goto bad;
516 }
Sage Weilf24e9982009-10-06 11:31:10 -0700517
518 ceph_decode_need(p, end, 2*sizeof(u64)+6*sizeof(u32), bad);
519 ceph_decode_copy(p, &map->fsid, sizeof(map->fsid));
Sage Weilc89136e2009-10-14 09:59:09 -0700520 map->epoch = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700521 ceph_decode_copy(p, &map->created, sizeof(map->created));
522 ceph_decode_copy(p, &map->modified, sizeof(map->modified));
523
Sage Weilf24e9982009-10-06 11:31:10 -0700524 ceph_decode_32_safe(p, end, max, bad);
525 while (max--) {
Sage Weil4fc51be2010-02-16 15:55:03 -0800526 ceph_decode_need(p, end, 4 + 1 + sizeof(pi->v), bad);
527 pi = kmalloc(sizeof(*pi), GFP_NOFS);
528 if (!pi)
Sage Weilf24e9982009-10-06 11:31:10 -0700529 goto bad;
Sage Weil4fc51be2010-02-16 15:55:03 -0800530 pi->id = ceph_decode_32(p);
Sage Weil361be862010-01-25 16:03:02 -0800531 ev = ceph_decode_8(p); /* encoding version */
Sage Weil02f90c62010-02-04 16:18:10 -0800532 if (ev > CEPH_PG_POOL_VERSION) {
533 pr_warning("got unknown v %d > %d of ceph_pg_pool\n",
534 ev, CEPH_PG_POOL_VERSION);
535 goto bad;
536 }
Sage Weilefd75762010-03-17 10:05:28 -0700537 __decode_pool(p, pi);
Sage Weil4fc51be2010-02-16 15:55:03 -0800538 __insert_pg_pool(&map->pg_pools, pi);
Sage Weilf24e9982009-10-06 11:31:10 -0700539 }
Sage Weil4fc51be2010-02-16 15:55:03 -0800540 ceph_decode_32_safe(p, end, map->pool_max, bad);
Sage Weilf24e9982009-10-06 11:31:10 -0700541
542 ceph_decode_32_safe(p, end, map->flags, bad);
543
Sage Weilc89136e2009-10-14 09:59:09 -0700544 max = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700545
546 /* (re)alloc osd arrays */
547 err = osdmap_set_max_osd(map, max);
548 if (err < 0)
549 goto bad;
550 dout("osdmap_decode max_osd = %d\n", map->max_osd);
551
552 /* osds */
553 err = -EINVAL;
554 ceph_decode_need(p, end, 3*sizeof(u32) +
555 map->max_osd*(1 + sizeof(*map->osd_weight) +
556 sizeof(*map->osd_addr)), bad);
557 *p += 4; /* skip length field (should match max) */
558 ceph_decode_copy(p, map->osd_state, map->max_osd);
559
560 *p += 4; /* skip length field (should match max) */
561 for (i = 0; i < map->max_osd; i++)
Sage Weilc89136e2009-10-14 09:59:09 -0700562 map->osd_weight[i] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700563
564 *p += 4; /* skip length field (should match max) */
565 ceph_decode_copy(p, map->osd_addr, map->max_osd*sizeof(*map->osd_addr));
Sage Weil63f2d212009-11-03 15:17:56 -0800566 for (i = 0; i < map->max_osd; i++)
567 ceph_decode_addr(&map->osd_addr[i]);
Sage Weilf24e9982009-10-06 11:31:10 -0700568
569 /* pg_temp */
570 ceph_decode_32_safe(p, end, len, bad);
571 for (i = 0; i < len; i++) {
572 int n, j;
Sage Weil51042122009-11-04 11:39:12 -0800573 struct ceph_pg pgid;
Sage Weilf24e9982009-10-06 11:31:10 -0700574 struct ceph_pg_mapping *pg;
575
576 ceph_decode_need(p, end, sizeof(u32) + sizeof(u64), bad);
Sage Weil51042122009-11-04 11:39:12 -0800577 ceph_decode_copy(p, &pgid, sizeof(pgid));
Sage Weilc89136e2009-10-14 09:59:09 -0700578 n = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700579 ceph_decode_need(p, end, n * sizeof(u32), bad);
Sage Weil30dc6382009-12-21 14:49:37 -0800580 err = -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -0700581 pg = kmalloc(sizeof(*pg) + n*sizeof(u32), GFP_NOFS);
Sage Weil30dc6382009-12-21 14:49:37 -0800582 if (!pg)
Sage Weilf24e9982009-10-06 11:31:10 -0700583 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -0700584 pg->pgid = pgid;
585 pg->len = n;
586 for (j = 0; j < n; j++)
Sage Weilc89136e2009-10-14 09:59:09 -0700587 pg->osds[j] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700588
Sage Weil991abb62009-10-08 22:22:37 -0700589 err = __insert_pg_mapping(pg, &map->pg_temp);
590 if (err)
591 goto bad;
Sage Weil51042122009-11-04 11:39:12 -0800592 dout(" added pg_temp %llx len %d\n", *(u64 *)&pgid, len);
Sage Weilf24e9982009-10-06 11:31:10 -0700593 }
594
595 /* crush */
596 ceph_decode_32_safe(p, end, len, bad);
597 dout("osdmap_decode crush len %d from off 0x%x\n", len,
598 (int)(*p - start));
599 ceph_decode_need(p, end, len, bad);
600 map->crush = crush_decode(*p, end);
601 *p += len;
602 if (IS_ERR(map->crush)) {
603 err = PTR_ERR(map->crush);
604 map->crush = NULL;
605 goto bad;
606 }
607
608 /* ignore the rest of the map */
609 *p = end;
610
611 dout("osdmap_decode done %p %p\n", *p, end);
612 return map;
613
614bad:
615 dout("osdmap_decode fail\n");
616 ceph_osdmap_destroy(map);
617 return ERR_PTR(err);
618}
619
620/*
621 * decode and apply an incremental map update.
622 */
623struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end,
624 struct ceph_osdmap *map,
625 struct ceph_messenger *msgr)
626{
Sage Weilf24e9982009-10-06 11:31:10 -0700627 struct crush_map *newcrush = NULL;
628 struct ceph_fsid fsid;
629 u32 epoch = 0;
630 struct ceph_timespec modified;
631 u32 len, pool;
Sage Weil4fc51be2010-02-16 15:55:03 -0800632 __s32 new_pool_max, new_flags, max;
Sage Weilf24e9982009-10-06 11:31:10 -0700633 void *start = *p;
634 int err = -EINVAL;
635 u16 version;
636 struct rb_node *rbp;
637
638 ceph_decode_16_safe(p, end, version, bad);
Sage Weil02f90c62010-02-04 16:18:10 -0800639 if (version > CEPH_OSDMAP_INC_VERSION) {
640 pr_warning("got unknown v %d > %d of inc osdmap\n", version,
641 CEPH_OSDMAP_INC_VERSION);
642 goto bad;
643 }
Sage Weilf24e9982009-10-06 11:31:10 -0700644
645 ceph_decode_need(p, end, sizeof(fsid)+sizeof(modified)+2*sizeof(u32),
646 bad);
647 ceph_decode_copy(p, &fsid, sizeof(fsid));
Sage Weilc89136e2009-10-14 09:59:09 -0700648 epoch = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700649 BUG_ON(epoch != map->epoch+1);
650 ceph_decode_copy(p, &modified, sizeof(modified));
Sage Weil4fc51be2010-02-16 15:55:03 -0800651 new_pool_max = ceph_decode_32(p);
Sage Weilc89136e2009-10-14 09:59:09 -0700652 new_flags = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700653
654 /* full map? */
655 ceph_decode_32_safe(p, end, len, bad);
656 if (len > 0) {
657 dout("apply_incremental full map len %d, %p to %p\n",
658 len, *p, end);
Sage Weil30dc6382009-12-21 14:49:37 -0800659 return osdmap_decode(p, min(*p+len, end));
Sage Weilf24e9982009-10-06 11:31:10 -0700660 }
661
662 /* new crush? */
663 ceph_decode_32_safe(p, end, len, bad);
664 if (len > 0) {
665 dout("apply_incremental new crush map len %d, %p to %p\n",
666 len, *p, end);
667 newcrush = crush_decode(*p, min(*p+len, end));
668 if (IS_ERR(newcrush))
669 return ERR_PTR(PTR_ERR(newcrush));
670 }
671
672 /* new flags? */
673 if (new_flags >= 0)
674 map->flags = new_flags;
Sage Weil4fc51be2010-02-16 15:55:03 -0800675 if (new_pool_max >= 0)
676 map->pool_max = new_pool_max;
Sage Weilf24e9982009-10-06 11:31:10 -0700677
678 ceph_decode_need(p, end, 5*sizeof(u32), bad);
679
680 /* new max? */
Sage Weilc89136e2009-10-14 09:59:09 -0700681 max = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700682 if (max >= 0) {
683 err = osdmap_set_max_osd(map, max);
684 if (err < 0)
685 goto bad;
686 }
687
688 map->epoch++;
689 map->modified = map->modified;
690 if (newcrush) {
691 if (map->crush)
692 crush_destroy(map->crush);
693 map->crush = newcrush;
694 newcrush = NULL;
695 }
696
697 /* new_pool */
698 ceph_decode_32_safe(p, end, len, bad);
699 while (len--) {
Sage Weil361be862010-01-25 16:03:02 -0800700 __u8 ev;
Sage Weil4fc51be2010-02-16 15:55:03 -0800701 struct ceph_pg_pool_info *pi;
Sage Weil361be862010-01-25 16:03:02 -0800702
Sage Weilf24e9982009-10-06 11:31:10 -0700703 ceph_decode_32_safe(p, end, pool, bad);
Sage Weil4fc51be2010-02-16 15:55:03 -0800704 ceph_decode_need(p, end, 1 + sizeof(pi->v), bad);
Sage Weil361be862010-01-25 16:03:02 -0800705 ev = ceph_decode_8(p); /* encoding version */
Sage Weil02f90c62010-02-04 16:18:10 -0800706 if (ev > CEPH_PG_POOL_VERSION) {
707 pr_warning("got unknown v %d > %d of ceph_pg_pool\n",
708 ev, CEPH_PG_POOL_VERSION);
709 goto bad;
710 }
Sage Weil4fc51be2010-02-16 15:55:03 -0800711 pi = __lookup_pg_pool(&map->pg_pools, pool);
712 if (!pi) {
713 pi = kmalloc(sizeof(*pi), GFP_NOFS);
714 if (!pi) {
715 err = -ENOMEM;
716 goto bad;
717 }
718 pi->id = pool;
719 __insert_pg_pool(&map->pg_pools, pi);
720 }
Sage Weilefd75762010-03-17 10:05:28 -0700721 __decode_pool(p, pi);
Sage Weilf24e9982009-10-06 11:31:10 -0700722 }
723
Sage Weil4fc51be2010-02-16 15:55:03 -0800724 /* old_pool */
Sage Weilf24e9982009-10-06 11:31:10 -0700725 ceph_decode_32_safe(p, end, len, bad);
Sage Weil4fc51be2010-02-16 15:55:03 -0800726 while (len--) {
727 struct ceph_pg_pool_info *pi;
728
729 ceph_decode_32_safe(p, end, pool, bad);
730 pi = __lookup_pg_pool(&map->pg_pools, pool);
731 if (pi) {
732 rb_erase(&pi->node, &map->pg_pools);
733 kfree(pi);
734 }
735 }
Sage Weilf24e9982009-10-06 11:31:10 -0700736
737 /* new_up */
738 err = -EINVAL;
739 ceph_decode_32_safe(p, end, len, bad);
740 while (len--) {
741 u32 osd;
742 struct ceph_entity_addr addr;
743 ceph_decode_32_safe(p, end, osd, bad);
744 ceph_decode_copy_safe(p, end, &addr, sizeof(addr), bad);
Sage Weil63f2d212009-11-03 15:17:56 -0800745 ceph_decode_addr(&addr);
Sage Weilf24e9982009-10-06 11:31:10 -0700746 pr_info("osd%d up\n", osd);
747 BUG_ON(osd >= map->max_osd);
748 map->osd_state[osd] |= CEPH_OSD_UP;
749 map->osd_addr[osd] = addr;
750 }
751
752 /* new_down */
753 ceph_decode_32_safe(p, end, len, bad);
754 while (len--) {
755 u32 osd;
756 ceph_decode_32_safe(p, end, osd, bad);
757 (*p)++; /* clean flag */
Sage Weil1bdb70e2009-11-06 13:57:49 -0800758 pr_info("osd%d down\n", osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700759 if (osd < map->max_osd)
760 map->osd_state[osd] &= ~CEPH_OSD_UP;
761 }
762
763 /* new_weight */
764 ceph_decode_32_safe(p, end, len, bad);
765 while (len--) {
766 u32 osd, off;
767 ceph_decode_need(p, end, sizeof(u32)*2, bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700768 osd = ceph_decode_32(p);
769 off = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700770 pr_info("osd%d weight 0x%x %s\n", osd, off,
771 off == CEPH_OSD_IN ? "(in)" :
772 (off == CEPH_OSD_OUT ? "(out)" : ""));
773 if (osd < map->max_osd)
774 map->osd_weight[osd] = off;
775 }
776
777 /* new_pg_temp */
778 rbp = rb_first(&map->pg_temp);
779 ceph_decode_32_safe(p, end, len, bad);
780 while (len--) {
781 struct ceph_pg_mapping *pg;
782 int j;
Sage Weil51042122009-11-04 11:39:12 -0800783 struct ceph_pg pgid;
Sage Weilf24e9982009-10-06 11:31:10 -0700784 u32 pglen;
785 ceph_decode_need(p, end, sizeof(u64) + sizeof(u32), bad);
Sage Weil51042122009-11-04 11:39:12 -0800786 ceph_decode_copy(p, &pgid, sizeof(pgid));
Sage Weilc89136e2009-10-14 09:59:09 -0700787 pglen = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700788
789 /* remove any? */
Sage Weil51042122009-11-04 11:39:12 -0800790 while (rbp && pgid_cmp(rb_entry(rbp, struct ceph_pg_mapping,
791 node)->pgid, pgid) <= 0) {
Sage Weilf24e9982009-10-06 11:31:10 -0700792 struct rb_node *cur = rbp;
793 rbp = rb_next(rbp);
794 dout(" removed pg_temp %llx\n",
Sage Weil51042122009-11-04 11:39:12 -0800795 *(u64 *)&rb_entry(cur, struct ceph_pg_mapping,
796 node)->pgid);
Sage Weilf24e9982009-10-06 11:31:10 -0700797 rb_erase(cur, &map->pg_temp);
798 }
799
800 if (pglen) {
801 /* insert */
802 ceph_decode_need(p, end, pglen*sizeof(u32), bad);
803 pg = kmalloc(sizeof(*pg) + sizeof(u32)*pglen, GFP_NOFS);
804 if (!pg) {
805 err = -ENOMEM;
806 goto bad;
807 }
808 pg->pgid = pgid;
809 pg->len = pglen;
Sage Weil7067f792009-12-21 16:02:37 -0800810 for (j = 0; j < pglen; j++)
Sage Weilc89136e2009-10-14 09:59:09 -0700811 pg->osds[j] = ceph_decode_32(p);
Sage Weil991abb62009-10-08 22:22:37 -0700812 err = __insert_pg_mapping(pg, &map->pg_temp);
813 if (err)
814 goto bad;
Sage Weil51042122009-11-04 11:39:12 -0800815 dout(" added pg_temp %llx len %d\n", *(u64 *)&pgid,
816 pglen);
Sage Weilf24e9982009-10-06 11:31:10 -0700817 }
818 }
819 while (rbp) {
820 struct rb_node *cur = rbp;
821 rbp = rb_next(rbp);
822 dout(" removed pg_temp %llx\n",
Sage Weil51042122009-11-04 11:39:12 -0800823 *(u64 *)&rb_entry(cur, struct ceph_pg_mapping,
824 node)->pgid);
Sage Weilf24e9982009-10-06 11:31:10 -0700825 rb_erase(cur, &map->pg_temp);
826 }
827
828 /* ignore the rest */
829 *p = end;
830 return map;
831
832bad:
833 pr_err("corrupt inc osdmap epoch %d off %d (%p of %p-%p)\n",
834 epoch, (int)(*p - start), *p, start, end);
Sage Weil9ec7cab2009-12-14 15:13:47 -0800835 print_hex_dump(KERN_DEBUG, "osdmap: ",
836 DUMP_PREFIX_OFFSET, 16, 1,
837 start, end - start, true);
Sage Weilf24e9982009-10-06 11:31:10 -0700838 if (newcrush)
839 crush_destroy(newcrush);
840 return ERR_PTR(err);
841}
842
843
844
845
846/*
847 * calculate file layout from given offset, length.
848 * fill in correct oid, logical length, and object extent
849 * offset, length.
850 *
851 * for now, we write only a single su, until we can
852 * pass a stride back to the caller.
853 */
854void ceph_calc_file_object_mapping(struct ceph_file_layout *layout,
855 u64 off, u64 *plen,
Sage Weil645a1022009-10-28 15:15:05 -0700856 u64 *ono,
Sage Weilf24e9982009-10-06 11:31:10 -0700857 u64 *oxoff, u64 *oxlen)
858{
859 u32 osize = le32_to_cpu(layout->fl_object_size);
860 u32 su = le32_to_cpu(layout->fl_stripe_unit);
861 u32 sc = le32_to_cpu(layout->fl_stripe_count);
862 u32 bl, stripeno, stripepos, objsetno;
863 u32 su_per_object;
Noah Watkinsff1d1f72009-10-30 12:57:30 -0700864 u64 t, su_offset;
Sage Weilf24e9982009-10-06 11:31:10 -0700865
866 dout("mapping %llu~%llu osize %u fl_su %u\n", off, *plen,
867 osize, su);
Noah Watkins35e054a2009-10-28 14:04:48 -0700868 su_per_object = osize / su;
Sage Weilf24e9982009-10-06 11:31:10 -0700869 dout("osize %u / su %u = su_per_object %u\n", osize, su,
870 su_per_object);
871
872 BUG_ON((su & ~PAGE_MASK) != 0);
873 /* bl = *off / su; */
874 t = off;
875 do_div(t, su);
876 bl = t;
877 dout("off %llu / su %u = bl %u\n", off, su, bl);
878
879 stripeno = bl / sc;
880 stripepos = bl % sc;
881 objsetno = stripeno / su_per_object;
882
Sage Weil645a1022009-10-28 15:15:05 -0700883 *ono = objsetno * sc + stripepos;
884 dout("objset %u * sc %u = ono %u\n", objsetno, sc, (unsigned)*ono);
885
886 /* *oxoff = *off % layout->fl_stripe_unit; # offset in su */
Sage Weilf24e9982009-10-06 11:31:10 -0700887 t = off;
Noah Watkinsff1d1f72009-10-30 12:57:30 -0700888 su_offset = do_div(t, su);
889 *oxoff = su_offset + (stripeno % su_per_object) * su;
Sage Weil645a1022009-10-28 15:15:05 -0700890
Noah Watkinsff1d1f72009-10-30 12:57:30 -0700891 /*
892 * Calculate the length of the extent being written to the selected
893 * object. This is the minimum of the full length requested (plen) or
894 * the remainder of the current stripe being written to.
895 */
896 *oxlen = min_t(u64, *plen, su - su_offset);
Sage Weilf24e9982009-10-06 11:31:10 -0700897 *plen = *oxlen;
898
899 dout(" obj extent %llu~%llu\n", *oxoff, *oxlen);
900}
901
902/*
903 * calculate an object layout (i.e. pgid) from an oid,
904 * file_layout, and osdmap
905 */
906int ceph_calc_object_layout(struct ceph_object_layout *ol,
907 const char *oid,
908 struct ceph_file_layout *fl,
909 struct ceph_osdmap *osdmap)
910{
911 unsigned num, num_mask;
Sage Weil51042122009-11-04 11:39:12 -0800912 struct ceph_pg pgid;
Sage Weilf24e9982009-10-06 11:31:10 -0700913 s32 preferred = (s32)le32_to_cpu(fl->fl_pg_preferred);
914 int poolid = le32_to_cpu(fl->fl_pg_pool);
915 struct ceph_pg_pool_info *pool;
Sage Weil51042122009-11-04 11:39:12 -0800916 unsigned ps;
Sage Weilf24e9982009-10-06 11:31:10 -0700917
Sage Weil30dc6382009-12-21 14:49:37 -0800918 BUG_ON(!osdmap);
Sage Weilf24e9982009-10-06 11:31:10 -0700919
Sage Weil4fc51be2010-02-16 15:55:03 -0800920 pool = __lookup_pg_pool(&osdmap->pg_pools, poolid);
921 if (!pool)
922 return -EIO;
Sage Weil1654dd02009-11-06 21:55:25 -0800923 ps = ceph_str_hash(pool->v.object_hash, oid, strlen(oid));
Sage Weilf24e9982009-10-06 11:31:10 -0700924 if (preferred >= 0) {
Sage Weil51042122009-11-04 11:39:12 -0800925 ps += preferred;
Sage Weilf24e9982009-10-06 11:31:10 -0700926 num = le32_to_cpu(pool->v.lpg_num);
927 num_mask = pool->lpg_num_mask;
928 } else {
929 num = le32_to_cpu(pool->v.pg_num);
930 num_mask = pool->pg_num_mask;
931 }
932
Sage Weil51042122009-11-04 11:39:12 -0800933 pgid.ps = cpu_to_le16(ps);
934 pgid.preferred = cpu_to_le16(preferred);
935 pgid.pool = fl->fl_pg_pool;
Sage Weilee7fdfa2009-10-19 11:41:51 -0700936 if (preferred >= 0)
Sage Weil51042122009-11-04 11:39:12 -0800937 dout("calc_object_layout '%s' pgid %d.%xp%d\n", oid, poolid, ps,
938 (int)preferred);
Sage Weilf24e9982009-10-06 11:31:10 -0700939 else
Sage Weil51042122009-11-04 11:39:12 -0800940 dout("calc_object_layout '%s' pgid %d.%x\n", oid, poolid, ps);
Sage Weilf24e9982009-10-06 11:31:10 -0700941
Sage Weil51042122009-11-04 11:39:12 -0800942 ol->ol_pgid = pgid;
Sage Weilf24e9982009-10-06 11:31:10 -0700943 ol->ol_stripe_unit = fl->fl_object_stripe_unit;
Sage Weilf24e9982009-10-06 11:31:10 -0700944 return 0;
945}
946
947/*
948 * Calculate raw osd vector for the given pgid. Return pointer to osd
949 * array, or NULL on failure.
950 */
Sage Weil51042122009-11-04 11:39:12 -0800951static int *calc_pg_raw(struct ceph_osdmap *osdmap, struct ceph_pg pgid,
Sage Weilf24e9982009-10-06 11:31:10 -0700952 int *osds, int *num)
953{
Sage Weilf24e9982009-10-06 11:31:10 -0700954 struct ceph_pg_mapping *pg;
955 struct ceph_pg_pool_info *pool;
956 int ruleno;
Sage Weil51042122009-11-04 11:39:12 -0800957 unsigned poolid, ps, pps;
958 int preferred;
Sage Weilf24e9982009-10-06 11:31:10 -0700959
960 /* pg_temp? */
Sage Weil9794b142010-02-16 15:53:32 -0800961 pg = __lookup_pg_mapping(&osdmap->pg_temp, pgid);
962 if (pg) {
963 *num = pg->len;
964 return pg->osds;
Sage Weilf24e9982009-10-06 11:31:10 -0700965 }
966
967 /* crush */
Sage Weil51042122009-11-04 11:39:12 -0800968 poolid = le32_to_cpu(pgid.pool);
969 ps = le16_to_cpu(pgid.ps);
970 preferred = (s16)le16_to_cpu(pgid.preferred);
971
Sage Weil767ea5c2009-12-09 12:34:01 -0800972 /* don't forcefeed bad device ids to crush */
973 if (preferred >= osdmap->max_osd ||
974 preferred >= osdmap->crush->max_devices)
975 preferred = -1;
976
Sage Weil4fc51be2010-02-16 15:55:03 -0800977 pool = __lookup_pg_pool(&osdmap->pg_pools, poolid);
978 if (!pool)
Sage Weilf24e9982009-10-06 11:31:10 -0700979 return NULL;
Sage Weilf24e9982009-10-06 11:31:10 -0700980 ruleno = crush_find_rule(osdmap->crush, pool->v.crush_ruleset,
981 pool->v.type, pool->v.size);
982 if (ruleno < 0) {
983 pr_err("no crush rule pool %d type %d size %d\n",
Sage Weil51042122009-11-04 11:39:12 -0800984 poolid, pool->v.type, pool->v.size);
Sage Weilf24e9982009-10-06 11:31:10 -0700985 return NULL;
986 }
987
Sage Weil51042122009-11-04 11:39:12 -0800988 if (preferred >= 0)
989 pps = ceph_stable_mod(ps,
Sage Weilf24e9982009-10-06 11:31:10 -0700990 le32_to_cpu(pool->v.lpgp_num),
991 pool->lpgp_num_mask);
992 else
Sage Weil51042122009-11-04 11:39:12 -0800993 pps = ceph_stable_mod(ps,
Sage Weilf24e9982009-10-06 11:31:10 -0700994 le32_to_cpu(pool->v.pgp_num),
995 pool->pgp_num_mask);
Sage Weil51042122009-11-04 11:39:12 -0800996 pps += poolid;
Sage Weilf24e9982009-10-06 11:31:10 -0700997 *num = crush_do_rule(osdmap->crush, ruleno, pps, osds,
998 min_t(int, pool->v.size, *num),
Sage Weil51042122009-11-04 11:39:12 -0800999 preferred, osdmap->osd_weight);
Sage Weilf24e9982009-10-06 11:31:10 -07001000 return osds;
1001}
1002
1003/*
1004 * Return primary osd for given pgid, or -1 if none.
1005 */
Sage Weil51042122009-11-04 11:39:12 -08001006int ceph_calc_pg_primary(struct ceph_osdmap *osdmap, struct ceph_pg pgid)
Sage Weilf24e9982009-10-06 11:31:10 -07001007{
1008 int rawosds[10], *osds;
1009 int i, num = ARRAY_SIZE(rawosds);
1010
1011 osds = calc_pg_raw(osdmap, pgid, rawosds, &num);
1012 if (!osds)
1013 return -1;
1014
1015 /* primary is first up osd */
1016 for (i = 0; i < num; i++)
1017 if (ceph_osd_is_up(osdmap, osds[i])) {
1018 return osds[i];
1019 break;
1020 }
1021 return -1;
1022}