blob: 21c6623c4b07370b73ed73504a031d8a7b01c1d1 [file] [log] [blame]
Sage Weilf24e9982009-10-06 11:31:10 -07001
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09002#include "ceph_debug.h"
3
4#include <linux/slab.h>
Sage Weilf24e9982009-10-06 11:31:10 -07005#include <asm/div64.h>
6
7#include "super.h"
8#include "osdmap.h"
9#include "crush/hash.h"
10#include "crush/mapper.h"
11#include "decode.h"
Sage Weilf24e9982009-10-06 11:31:10 -070012
13char *ceph_osdmap_state_str(char *str, int len, int state)
14{
15 int flag = 0;
16
17 if (!len)
18 goto done;
19
20 *str = '\0';
21 if (state) {
22 if (state & CEPH_OSD_EXISTS) {
23 snprintf(str, len, "exists");
24 flag = 1;
25 }
26 if (state & CEPH_OSD_UP) {
27 snprintf(str, len, "%s%s%s", str, (flag ? ", " : ""),
28 "up");
29 flag = 1;
30 }
31 } else {
32 snprintf(str, len, "doesn't exist");
33 }
34done:
35 return str;
36}
37
38/* maps */
39
40static int calc_bits_of(unsigned t)
41{
42 int b = 0;
43 while (t) {
44 t = t >> 1;
45 b++;
46 }
47 return b;
48}
49
50/*
51 * the foo_mask is the smallest value 2^n-1 that is >= foo.
52 */
53static void calc_pg_masks(struct ceph_pg_pool_info *pi)
54{
55 pi->pg_num_mask = (1 << calc_bits_of(le32_to_cpu(pi->v.pg_num)-1)) - 1;
56 pi->pgp_num_mask =
57 (1 << calc_bits_of(le32_to_cpu(pi->v.pgp_num)-1)) - 1;
58 pi->lpg_num_mask =
59 (1 << calc_bits_of(le32_to_cpu(pi->v.lpg_num)-1)) - 1;
60 pi->lpgp_num_mask =
61 (1 << calc_bits_of(le32_to_cpu(pi->v.lpgp_num)-1)) - 1;
62}
63
64/*
65 * decode crush map
66 */
67static int crush_decode_uniform_bucket(void **p, void *end,
68 struct crush_bucket_uniform *b)
69{
70 dout("crush_decode_uniform_bucket %p to %p\n", *p, end);
71 ceph_decode_need(p, end, (1+b->h.size) * sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -070072 b->item_weight = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -070073 return 0;
74bad:
75 return -EINVAL;
76}
77
78static int crush_decode_list_bucket(void **p, void *end,
79 struct crush_bucket_list *b)
80{
81 int j;
82 dout("crush_decode_list_bucket %p to %p\n", *p, end);
83 b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
84 if (b->item_weights == NULL)
85 return -ENOMEM;
86 b->sum_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
87 if (b->sum_weights == NULL)
88 return -ENOMEM;
89 ceph_decode_need(p, end, 2 * b->h.size * sizeof(u32), bad);
90 for (j = 0; j < b->h.size; j++) {
Sage Weilc89136e2009-10-14 09:59:09 -070091 b->item_weights[j] = ceph_decode_32(p);
92 b->sum_weights[j] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -070093 }
94 return 0;
95bad:
96 return -EINVAL;
97}
98
99static int crush_decode_tree_bucket(void **p, void *end,
100 struct crush_bucket_tree *b)
101{
102 int j;
103 dout("crush_decode_tree_bucket %p to %p\n", *p, end);
104 ceph_decode_32_safe(p, end, b->num_nodes, bad);
105 b->node_weights = kcalloc(b->num_nodes, sizeof(u32), GFP_NOFS);
106 if (b->node_weights == NULL)
107 return -ENOMEM;
108 ceph_decode_need(p, end, b->num_nodes * sizeof(u32), bad);
109 for (j = 0; j < b->num_nodes; j++)
Sage Weilc89136e2009-10-14 09:59:09 -0700110 b->node_weights[j] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700111 return 0;
112bad:
113 return -EINVAL;
114}
115
116static int crush_decode_straw_bucket(void **p, void *end,
117 struct crush_bucket_straw *b)
118{
119 int j;
120 dout("crush_decode_straw_bucket %p to %p\n", *p, end);
121 b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
122 if (b->item_weights == NULL)
123 return -ENOMEM;
124 b->straws = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
125 if (b->straws == NULL)
126 return -ENOMEM;
127 ceph_decode_need(p, end, 2 * b->h.size * sizeof(u32), bad);
128 for (j = 0; j < b->h.size; j++) {
Sage Weilc89136e2009-10-14 09:59:09 -0700129 b->item_weights[j] = ceph_decode_32(p);
130 b->straws[j] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700131 }
132 return 0;
133bad:
134 return -EINVAL;
135}
136
137static struct crush_map *crush_decode(void *pbyval, void *end)
138{
139 struct crush_map *c;
140 int err = -EINVAL;
141 int i, j;
142 void **p = &pbyval;
143 void *start = pbyval;
144 u32 magic;
145
146 dout("crush_decode %p to %p len %d\n", *p, end, (int)(end - *p));
147
148 c = kzalloc(sizeof(*c), GFP_NOFS);
149 if (c == NULL)
150 return ERR_PTR(-ENOMEM);
151
152 ceph_decode_need(p, end, 4*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700153 magic = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700154 if (magic != CRUSH_MAGIC) {
155 pr_err("crush_decode magic %x != current %x\n",
156 (unsigned)magic, (unsigned)CRUSH_MAGIC);
157 goto bad;
158 }
Sage Weilc89136e2009-10-14 09:59:09 -0700159 c->max_buckets = ceph_decode_32(p);
160 c->max_rules = ceph_decode_32(p);
161 c->max_devices = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700162
163 c->device_parents = kcalloc(c->max_devices, sizeof(u32), GFP_NOFS);
164 if (c->device_parents == NULL)
165 goto badmem;
166 c->bucket_parents = kcalloc(c->max_buckets, sizeof(u32), GFP_NOFS);
167 if (c->bucket_parents == NULL)
168 goto badmem;
169
170 c->buckets = kcalloc(c->max_buckets, sizeof(*c->buckets), GFP_NOFS);
171 if (c->buckets == NULL)
172 goto badmem;
173 c->rules = kcalloc(c->max_rules, sizeof(*c->rules), GFP_NOFS);
174 if (c->rules == NULL)
175 goto badmem;
176
177 /* buckets */
178 for (i = 0; i < c->max_buckets; i++) {
179 int size = 0;
180 u32 alg;
181 struct crush_bucket *b;
182
183 ceph_decode_32_safe(p, end, alg, bad);
184 if (alg == 0) {
185 c->buckets[i] = NULL;
186 continue;
187 }
188 dout("crush_decode bucket %d off %x %p to %p\n",
189 i, (int)(*p-start), *p, end);
190
191 switch (alg) {
192 case CRUSH_BUCKET_UNIFORM:
193 size = sizeof(struct crush_bucket_uniform);
194 break;
195 case CRUSH_BUCKET_LIST:
196 size = sizeof(struct crush_bucket_list);
197 break;
198 case CRUSH_BUCKET_TREE:
199 size = sizeof(struct crush_bucket_tree);
200 break;
201 case CRUSH_BUCKET_STRAW:
202 size = sizeof(struct crush_bucket_straw);
203 break;
204 default:
Sage Weil30dc6382009-12-21 14:49:37 -0800205 err = -EINVAL;
Sage Weilf24e9982009-10-06 11:31:10 -0700206 goto bad;
207 }
208 BUG_ON(size == 0);
209 b = c->buckets[i] = kzalloc(size, GFP_NOFS);
210 if (b == NULL)
211 goto badmem;
212
213 ceph_decode_need(p, end, 4*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700214 b->id = ceph_decode_32(p);
215 b->type = ceph_decode_16(p);
Sage Weilfb690392009-11-07 20:18:22 -0800216 b->alg = ceph_decode_8(p);
217 b->hash = ceph_decode_8(p);
Sage Weilc89136e2009-10-14 09:59:09 -0700218 b->weight = ceph_decode_32(p);
219 b->size = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700220
221 dout("crush_decode bucket size %d off %x %p to %p\n",
222 b->size, (int)(*p-start), *p, end);
223
224 b->items = kcalloc(b->size, sizeof(__s32), GFP_NOFS);
225 if (b->items == NULL)
226 goto badmem;
227 b->perm = kcalloc(b->size, sizeof(u32), GFP_NOFS);
228 if (b->perm == NULL)
229 goto badmem;
230 b->perm_n = 0;
231
232 ceph_decode_need(p, end, b->size*sizeof(u32), bad);
233 for (j = 0; j < b->size; j++)
Sage Weilc89136e2009-10-14 09:59:09 -0700234 b->items[j] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700235
236 switch (b->alg) {
237 case CRUSH_BUCKET_UNIFORM:
238 err = crush_decode_uniform_bucket(p, end,
239 (struct crush_bucket_uniform *)b);
240 if (err < 0)
241 goto bad;
242 break;
243 case CRUSH_BUCKET_LIST:
244 err = crush_decode_list_bucket(p, end,
245 (struct crush_bucket_list *)b);
246 if (err < 0)
247 goto bad;
248 break;
249 case CRUSH_BUCKET_TREE:
250 err = crush_decode_tree_bucket(p, end,
251 (struct crush_bucket_tree *)b);
252 if (err < 0)
253 goto bad;
254 break;
255 case CRUSH_BUCKET_STRAW:
256 err = crush_decode_straw_bucket(p, end,
257 (struct crush_bucket_straw *)b);
258 if (err < 0)
259 goto bad;
260 break;
261 }
262 }
263
264 /* rules */
265 dout("rule vec is %p\n", c->rules);
266 for (i = 0; i < c->max_rules; i++) {
267 u32 yes;
268 struct crush_rule *r;
269
270 ceph_decode_32_safe(p, end, yes, bad);
271 if (!yes) {
272 dout("crush_decode NO rule %d off %x %p to %p\n",
273 i, (int)(*p-start), *p, end);
274 c->rules[i] = NULL;
275 continue;
276 }
277
278 dout("crush_decode rule %d off %x %p to %p\n",
279 i, (int)(*p-start), *p, end);
280
281 /* len */
282 ceph_decode_32_safe(p, end, yes, bad);
283#if BITS_PER_LONG == 32
Sage Weil30dc6382009-12-21 14:49:37 -0800284 err = -EINVAL;
Sage Weilf24e9982009-10-06 11:31:10 -0700285 if (yes > ULONG_MAX / sizeof(struct crush_rule_step))
286 goto bad;
287#endif
288 r = c->rules[i] = kmalloc(sizeof(*r) +
289 yes*sizeof(struct crush_rule_step),
290 GFP_NOFS);
291 if (r == NULL)
292 goto badmem;
293 dout(" rule %d is at %p\n", i, r);
294 r->len = yes;
295 ceph_decode_copy_safe(p, end, &r->mask, 4, bad); /* 4 u8's */
296 ceph_decode_need(p, end, r->len*3*sizeof(u32), bad);
297 for (j = 0; j < r->len; j++) {
Sage Weilc89136e2009-10-14 09:59:09 -0700298 r->steps[j].op = ceph_decode_32(p);
299 r->steps[j].arg1 = ceph_decode_32(p);
300 r->steps[j].arg2 = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700301 }
302 }
303
304 /* ignore trailing name maps. */
305
306 dout("crush_decode success\n");
307 return c;
308
309badmem:
310 err = -ENOMEM;
311bad:
312 dout("crush_decode fail %d\n", err);
313 crush_destroy(c);
314 return ERR_PTR(err);
315}
316
317
318/*
319 * osd map
320 */
321void ceph_osdmap_destroy(struct ceph_osdmap *map)
322{
323 dout("osdmap_destroy %p\n", map);
324 if (map->crush)
325 crush_destroy(map->crush);
Sage Weil9794b142010-02-16 15:53:32 -0800326 while (!RB_EMPTY_ROOT(&map->pg_temp)) {
327 struct ceph_pg_mapping *pg =
328 rb_entry(rb_first(&map->pg_temp),
329 struct ceph_pg_mapping, node);
330 rb_erase(&pg->node, &map->pg_temp);
331 kfree(pg);
332 }
Sage Weil4fc51be2010-02-16 15:55:03 -0800333 while (!RB_EMPTY_ROOT(&map->pg_pools)) {
334 struct ceph_pg_pool_info *pi =
335 rb_entry(rb_first(&map->pg_pools),
336 struct ceph_pg_pool_info, node);
337 rb_erase(&pi->node, &map->pg_pools);
338 kfree(pi);
339 }
Sage Weilf24e9982009-10-06 11:31:10 -0700340 kfree(map->osd_state);
341 kfree(map->osd_weight);
Sage Weilf24e9982009-10-06 11:31:10 -0700342 kfree(map->osd_addr);
343 kfree(map);
344}
345
346/*
347 * adjust max osd value. reallocate arrays.
348 */
349static int osdmap_set_max_osd(struct ceph_osdmap *map, int max)
350{
351 u8 *state;
352 struct ceph_entity_addr *addr;
353 u32 *weight;
354
355 state = kcalloc(max, sizeof(*state), GFP_NOFS);
356 addr = kcalloc(max, sizeof(*addr), GFP_NOFS);
357 weight = kcalloc(max, sizeof(*weight), GFP_NOFS);
358 if (state == NULL || addr == NULL || weight == NULL) {
359 kfree(state);
360 kfree(addr);
361 kfree(weight);
362 return -ENOMEM;
363 }
364
365 /* copy old? */
366 if (map->osd_state) {
367 memcpy(state, map->osd_state, map->max_osd*sizeof(*state));
368 memcpy(addr, map->osd_addr, map->max_osd*sizeof(*addr));
369 memcpy(weight, map->osd_weight, map->max_osd*sizeof(*weight));
370 kfree(map->osd_state);
371 kfree(map->osd_addr);
372 kfree(map->osd_weight);
373 }
374
375 map->osd_state = state;
376 map->osd_weight = weight;
377 map->osd_addr = addr;
378 map->max_osd = max;
379 return 0;
380}
381
382/*
Sage Weil9794b142010-02-16 15:53:32 -0800383 * rbtree of pg_mapping for handling pg_temp (explicit mapping of pgid
384 * to a set of osds)
Sage Weilf24e9982009-10-06 11:31:10 -0700385 */
Sage Weil51042122009-11-04 11:39:12 -0800386static int pgid_cmp(struct ceph_pg l, struct ceph_pg r)
387{
388 u64 a = *(u64 *)&l;
389 u64 b = *(u64 *)&r;
390
391 if (a < b)
392 return -1;
393 if (a > b)
394 return 1;
395 return 0;
396}
397
Sage Weil991abb62009-10-08 22:22:37 -0700398static int __insert_pg_mapping(struct ceph_pg_mapping *new,
399 struct rb_root *root)
Sage Weilf24e9982009-10-06 11:31:10 -0700400{
401 struct rb_node **p = &root->rb_node;
402 struct rb_node *parent = NULL;
403 struct ceph_pg_mapping *pg = NULL;
Sage Weil51042122009-11-04 11:39:12 -0800404 int c;
Sage Weilf24e9982009-10-06 11:31:10 -0700405
406 while (*p) {
407 parent = *p;
408 pg = rb_entry(parent, struct ceph_pg_mapping, node);
Sage Weil51042122009-11-04 11:39:12 -0800409 c = pgid_cmp(new->pgid, pg->pgid);
410 if (c < 0)
Sage Weilf24e9982009-10-06 11:31:10 -0700411 p = &(*p)->rb_left;
Sage Weil51042122009-11-04 11:39:12 -0800412 else if (c > 0)
Sage Weilf24e9982009-10-06 11:31:10 -0700413 p = &(*p)->rb_right;
414 else
Sage Weil991abb62009-10-08 22:22:37 -0700415 return -EEXIST;
Sage Weilf24e9982009-10-06 11:31:10 -0700416 }
417
418 rb_link_node(&new->node, parent, p);
419 rb_insert_color(&new->node, root);
Sage Weil991abb62009-10-08 22:22:37 -0700420 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700421}
422
Sage Weil9794b142010-02-16 15:53:32 -0800423static struct ceph_pg_mapping *__lookup_pg_mapping(struct rb_root *root,
424 struct ceph_pg pgid)
425{
426 struct rb_node *n = root->rb_node;
427 struct ceph_pg_mapping *pg;
428 int c;
429
430 while (n) {
431 pg = rb_entry(n, struct ceph_pg_mapping, node);
432 c = pgid_cmp(pgid, pg->pgid);
433 if (c < 0)
434 n = n->rb_left;
435 else if (c > 0)
436 n = n->rb_right;
437 else
438 return pg;
439 }
440 return NULL;
441}
442
Sage Weilf24e9982009-10-06 11:31:10 -0700443/*
Sage Weil4fc51be2010-02-16 15:55:03 -0800444 * rbtree of pg pool info
445 */
446static int __insert_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *new)
447{
448 struct rb_node **p = &root->rb_node;
449 struct rb_node *parent = NULL;
450 struct ceph_pg_pool_info *pi = NULL;
451
452 while (*p) {
453 parent = *p;
454 pi = rb_entry(parent, struct ceph_pg_pool_info, node);
455 if (new->id < pi->id)
456 p = &(*p)->rb_left;
457 else if (new->id > pi->id)
458 p = &(*p)->rb_right;
459 else
460 return -EEXIST;
461 }
462
463 rb_link_node(&new->node, parent, p);
464 rb_insert_color(&new->node, root);
465 return 0;
466}
467
468static struct ceph_pg_pool_info *__lookup_pg_pool(struct rb_root *root, int id)
469{
470 struct ceph_pg_pool_info *pi;
471 struct rb_node *n = root->rb_node;
472
473 while (n) {
474 pi = rb_entry(n, struct ceph_pg_pool_info, node);
475 if (id < pi->id)
476 n = n->rb_left;
477 else if (id > pi->id)
478 n = n->rb_right;
479 else
480 return pi;
481 }
482 return NULL;
483}
484
Sage Weilefd75762010-03-17 10:05:28 -0700485void __decode_pool(void **p, struct ceph_pg_pool_info *pi)
486{
487 ceph_decode_copy(p, &pi->v, sizeof(pi->v));
488 calc_pg_masks(pi);
489 *p += le32_to_cpu(pi->v.num_snaps) * sizeof(u64);
490 *p += le32_to_cpu(pi->v.num_removed_snap_intervals) * sizeof(u64) * 2;
491}
492
Sage Weil4fc51be2010-02-16 15:55:03 -0800493/*
Sage Weilf24e9982009-10-06 11:31:10 -0700494 * decode a full map.
495 */
496struct ceph_osdmap *osdmap_decode(void **p, void *end)
497{
498 struct ceph_osdmap *map;
499 u16 version;
500 u32 len, max, i;
Sage Weil361be862010-01-25 16:03:02 -0800501 u8 ev;
Sage Weilf24e9982009-10-06 11:31:10 -0700502 int err = -EINVAL;
503 void *start = *p;
Sage Weil4fc51be2010-02-16 15:55:03 -0800504 struct ceph_pg_pool_info *pi;
Sage Weilf24e9982009-10-06 11:31:10 -0700505
506 dout("osdmap_decode %p to %p len %d\n", *p, end, (int)(end - *p));
507
508 map = kzalloc(sizeof(*map), GFP_NOFS);
509 if (map == NULL)
510 return ERR_PTR(-ENOMEM);
511 map->pg_temp = RB_ROOT;
512
513 ceph_decode_16_safe(p, end, version, bad);
Sage Weil02f90c62010-02-04 16:18:10 -0800514 if (version > CEPH_OSDMAP_VERSION) {
515 pr_warning("got unknown v %d > %d of osdmap\n", version,
516 CEPH_OSDMAP_VERSION);
517 goto bad;
518 }
Sage Weilf24e9982009-10-06 11:31:10 -0700519
520 ceph_decode_need(p, end, 2*sizeof(u64)+6*sizeof(u32), bad);
521 ceph_decode_copy(p, &map->fsid, sizeof(map->fsid));
Sage Weilc89136e2009-10-14 09:59:09 -0700522 map->epoch = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700523 ceph_decode_copy(p, &map->created, sizeof(map->created));
524 ceph_decode_copy(p, &map->modified, sizeof(map->modified));
525
Sage Weilf24e9982009-10-06 11:31:10 -0700526 ceph_decode_32_safe(p, end, max, bad);
527 while (max--) {
Sage Weil4fc51be2010-02-16 15:55:03 -0800528 ceph_decode_need(p, end, 4 + 1 + sizeof(pi->v), bad);
529 pi = kmalloc(sizeof(*pi), GFP_NOFS);
530 if (!pi)
Sage Weilf24e9982009-10-06 11:31:10 -0700531 goto bad;
Sage Weil4fc51be2010-02-16 15:55:03 -0800532 pi->id = ceph_decode_32(p);
Sage Weil361be862010-01-25 16:03:02 -0800533 ev = ceph_decode_8(p); /* encoding version */
Sage Weil02f90c62010-02-04 16:18:10 -0800534 if (ev > CEPH_PG_POOL_VERSION) {
535 pr_warning("got unknown v %d > %d of ceph_pg_pool\n",
536 ev, CEPH_PG_POOL_VERSION);
537 goto bad;
538 }
Sage Weilefd75762010-03-17 10:05:28 -0700539 __decode_pool(p, pi);
Sage Weil4fc51be2010-02-16 15:55:03 -0800540 __insert_pg_pool(&map->pg_pools, pi);
Sage Weilf24e9982009-10-06 11:31:10 -0700541 }
Sage Weil4fc51be2010-02-16 15:55:03 -0800542 ceph_decode_32_safe(p, end, map->pool_max, bad);
Sage Weilf24e9982009-10-06 11:31:10 -0700543
544 ceph_decode_32_safe(p, end, map->flags, bad);
545
Sage Weilc89136e2009-10-14 09:59:09 -0700546 max = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700547
548 /* (re)alloc osd arrays */
549 err = osdmap_set_max_osd(map, max);
550 if (err < 0)
551 goto bad;
552 dout("osdmap_decode max_osd = %d\n", map->max_osd);
553
554 /* osds */
555 err = -EINVAL;
556 ceph_decode_need(p, end, 3*sizeof(u32) +
557 map->max_osd*(1 + sizeof(*map->osd_weight) +
558 sizeof(*map->osd_addr)), bad);
559 *p += 4; /* skip length field (should match max) */
560 ceph_decode_copy(p, map->osd_state, map->max_osd);
561
562 *p += 4; /* skip length field (should match max) */
563 for (i = 0; i < map->max_osd; i++)
Sage Weilc89136e2009-10-14 09:59:09 -0700564 map->osd_weight[i] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700565
566 *p += 4; /* skip length field (should match max) */
567 ceph_decode_copy(p, map->osd_addr, map->max_osd*sizeof(*map->osd_addr));
Sage Weil63f2d212009-11-03 15:17:56 -0800568 for (i = 0; i < map->max_osd; i++)
569 ceph_decode_addr(&map->osd_addr[i]);
Sage Weilf24e9982009-10-06 11:31:10 -0700570
571 /* pg_temp */
572 ceph_decode_32_safe(p, end, len, bad);
573 for (i = 0; i < len; i++) {
574 int n, j;
Sage Weil51042122009-11-04 11:39:12 -0800575 struct ceph_pg pgid;
Sage Weilf24e9982009-10-06 11:31:10 -0700576 struct ceph_pg_mapping *pg;
577
578 ceph_decode_need(p, end, sizeof(u32) + sizeof(u64), bad);
Sage Weil51042122009-11-04 11:39:12 -0800579 ceph_decode_copy(p, &pgid, sizeof(pgid));
Sage Weilc89136e2009-10-14 09:59:09 -0700580 n = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700581 ceph_decode_need(p, end, n * sizeof(u32), bad);
Sage Weil30dc6382009-12-21 14:49:37 -0800582 err = -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -0700583 pg = kmalloc(sizeof(*pg) + n*sizeof(u32), GFP_NOFS);
Sage Weil30dc6382009-12-21 14:49:37 -0800584 if (!pg)
Sage Weilf24e9982009-10-06 11:31:10 -0700585 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -0700586 pg->pgid = pgid;
587 pg->len = n;
588 for (j = 0; j < n; j++)
Sage Weilc89136e2009-10-14 09:59:09 -0700589 pg->osds[j] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700590
Sage Weil991abb62009-10-08 22:22:37 -0700591 err = __insert_pg_mapping(pg, &map->pg_temp);
592 if (err)
593 goto bad;
Sage Weil51042122009-11-04 11:39:12 -0800594 dout(" added pg_temp %llx len %d\n", *(u64 *)&pgid, len);
Sage Weilf24e9982009-10-06 11:31:10 -0700595 }
596
597 /* crush */
598 ceph_decode_32_safe(p, end, len, bad);
599 dout("osdmap_decode crush len %d from off 0x%x\n", len,
600 (int)(*p - start));
601 ceph_decode_need(p, end, len, bad);
602 map->crush = crush_decode(*p, end);
603 *p += len;
604 if (IS_ERR(map->crush)) {
605 err = PTR_ERR(map->crush);
606 map->crush = NULL;
607 goto bad;
608 }
609
610 /* ignore the rest of the map */
611 *p = end;
612
613 dout("osdmap_decode done %p %p\n", *p, end);
614 return map;
615
616bad:
617 dout("osdmap_decode fail\n");
618 ceph_osdmap_destroy(map);
619 return ERR_PTR(err);
620}
621
622/*
623 * decode and apply an incremental map update.
624 */
625struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end,
626 struct ceph_osdmap *map,
627 struct ceph_messenger *msgr)
628{
Sage Weilf24e9982009-10-06 11:31:10 -0700629 struct crush_map *newcrush = NULL;
630 struct ceph_fsid fsid;
631 u32 epoch = 0;
632 struct ceph_timespec modified;
633 u32 len, pool;
Sage Weil4fc51be2010-02-16 15:55:03 -0800634 __s32 new_pool_max, new_flags, max;
Sage Weilf24e9982009-10-06 11:31:10 -0700635 void *start = *p;
636 int err = -EINVAL;
637 u16 version;
638 struct rb_node *rbp;
639
640 ceph_decode_16_safe(p, end, version, bad);
Sage Weil02f90c62010-02-04 16:18:10 -0800641 if (version > CEPH_OSDMAP_INC_VERSION) {
642 pr_warning("got unknown v %d > %d of inc osdmap\n", version,
643 CEPH_OSDMAP_INC_VERSION);
644 goto bad;
645 }
Sage Weilf24e9982009-10-06 11:31:10 -0700646
647 ceph_decode_need(p, end, sizeof(fsid)+sizeof(modified)+2*sizeof(u32),
648 bad);
649 ceph_decode_copy(p, &fsid, sizeof(fsid));
Sage Weilc89136e2009-10-14 09:59:09 -0700650 epoch = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700651 BUG_ON(epoch != map->epoch+1);
652 ceph_decode_copy(p, &modified, sizeof(modified));
Sage Weil4fc51be2010-02-16 15:55:03 -0800653 new_pool_max = ceph_decode_32(p);
Sage Weilc89136e2009-10-14 09:59:09 -0700654 new_flags = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700655
656 /* full map? */
657 ceph_decode_32_safe(p, end, len, bad);
658 if (len > 0) {
659 dout("apply_incremental full map len %d, %p to %p\n",
660 len, *p, end);
Sage Weil30dc6382009-12-21 14:49:37 -0800661 return osdmap_decode(p, min(*p+len, end));
Sage Weilf24e9982009-10-06 11:31:10 -0700662 }
663
664 /* new crush? */
665 ceph_decode_32_safe(p, end, len, bad);
666 if (len > 0) {
667 dout("apply_incremental new crush map len %d, %p to %p\n",
668 len, *p, end);
669 newcrush = crush_decode(*p, min(*p+len, end));
670 if (IS_ERR(newcrush))
671 return ERR_PTR(PTR_ERR(newcrush));
672 }
673
674 /* new flags? */
675 if (new_flags >= 0)
676 map->flags = new_flags;
Sage Weil4fc51be2010-02-16 15:55:03 -0800677 if (new_pool_max >= 0)
678 map->pool_max = new_pool_max;
Sage Weilf24e9982009-10-06 11:31:10 -0700679
680 ceph_decode_need(p, end, 5*sizeof(u32), bad);
681
682 /* new max? */
Sage Weilc89136e2009-10-14 09:59:09 -0700683 max = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700684 if (max >= 0) {
685 err = osdmap_set_max_osd(map, max);
686 if (err < 0)
687 goto bad;
688 }
689
690 map->epoch++;
691 map->modified = map->modified;
692 if (newcrush) {
693 if (map->crush)
694 crush_destroy(map->crush);
695 map->crush = newcrush;
696 newcrush = NULL;
697 }
698
699 /* new_pool */
700 ceph_decode_32_safe(p, end, len, bad);
701 while (len--) {
Sage Weil361be862010-01-25 16:03:02 -0800702 __u8 ev;
Sage Weil4fc51be2010-02-16 15:55:03 -0800703 struct ceph_pg_pool_info *pi;
Sage Weil361be862010-01-25 16:03:02 -0800704
Sage Weilf24e9982009-10-06 11:31:10 -0700705 ceph_decode_32_safe(p, end, pool, bad);
Sage Weil4fc51be2010-02-16 15:55:03 -0800706 ceph_decode_need(p, end, 1 + sizeof(pi->v), bad);
Sage Weil361be862010-01-25 16:03:02 -0800707 ev = ceph_decode_8(p); /* encoding version */
Sage Weil02f90c62010-02-04 16:18:10 -0800708 if (ev > CEPH_PG_POOL_VERSION) {
709 pr_warning("got unknown v %d > %d of ceph_pg_pool\n",
710 ev, CEPH_PG_POOL_VERSION);
711 goto bad;
712 }
Sage Weil4fc51be2010-02-16 15:55:03 -0800713 pi = __lookup_pg_pool(&map->pg_pools, pool);
714 if (!pi) {
715 pi = kmalloc(sizeof(*pi), GFP_NOFS);
716 if (!pi) {
717 err = -ENOMEM;
718 goto bad;
719 }
720 pi->id = pool;
721 __insert_pg_pool(&map->pg_pools, pi);
722 }
Sage Weilefd75762010-03-17 10:05:28 -0700723 __decode_pool(p, pi);
Sage Weilf24e9982009-10-06 11:31:10 -0700724 }
725
Sage Weil4fc51be2010-02-16 15:55:03 -0800726 /* old_pool */
Sage Weilf24e9982009-10-06 11:31:10 -0700727 ceph_decode_32_safe(p, end, len, bad);
Sage Weil4fc51be2010-02-16 15:55:03 -0800728 while (len--) {
729 struct ceph_pg_pool_info *pi;
730
731 ceph_decode_32_safe(p, end, pool, bad);
732 pi = __lookup_pg_pool(&map->pg_pools, pool);
733 if (pi) {
734 rb_erase(&pi->node, &map->pg_pools);
735 kfree(pi);
736 }
737 }
Sage Weilf24e9982009-10-06 11:31:10 -0700738
739 /* new_up */
740 err = -EINVAL;
741 ceph_decode_32_safe(p, end, len, bad);
742 while (len--) {
743 u32 osd;
744 struct ceph_entity_addr addr;
745 ceph_decode_32_safe(p, end, osd, bad);
746 ceph_decode_copy_safe(p, end, &addr, sizeof(addr), bad);
Sage Weil63f2d212009-11-03 15:17:56 -0800747 ceph_decode_addr(&addr);
Sage Weilf24e9982009-10-06 11:31:10 -0700748 pr_info("osd%d up\n", osd);
749 BUG_ON(osd >= map->max_osd);
750 map->osd_state[osd] |= CEPH_OSD_UP;
751 map->osd_addr[osd] = addr;
752 }
753
754 /* new_down */
755 ceph_decode_32_safe(p, end, len, bad);
756 while (len--) {
757 u32 osd;
758 ceph_decode_32_safe(p, end, osd, bad);
759 (*p)++; /* clean flag */
Sage Weil1bdb70e2009-11-06 13:57:49 -0800760 pr_info("osd%d down\n", osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700761 if (osd < map->max_osd)
762 map->osd_state[osd] &= ~CEPH_OSD_UP;
763 }
764
765 /* new_weight */
766 ceph_decode_32_safe(p, end, len, bad);
767 while (len--) {
768 u32 osd, off;
769 ceph_decode_need(p, end, sizeof(u32)*2, bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700770 osd = ceph_decode_32(p);
771 off = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700772 pr_info("osd%d weight 0x%x %s\n", osd, off,
773 off == CEPH_OSD_IN ? "(in)" :
774 (off == CEPH_OSD_OUT ? "(out)" : ""));
775 if (osd < map->max_osd)
776 map->osd_weight[osd] = off;
777 }
778
779 /* new_pg_temp */
780 rbp = rb_first(&map->pg_temp);
781 ceph_decode_32_safe(p, end, len, bad);
782 while (len--) {
783 struct ceph_pg_mapping *pg;
784 int j;
Sage Weil51042122009-11-04 11:39:12 -0800785 struct ceph_pg pgid;
Sage Weilf24e9982009-10-06 11:31:10 -0700786 u32 pglen;
787 ceph_decode_need(p, end, sizeof(u64) + sizeof(u32), bad);
Sage Weil51042122009-11-04 11:39:12 -0800788 ceph_decode_copy(p, &pgid, sizeof(pgid));
Sage Weilc89136e2009-10-14 09:59:09 -0700789 pglen = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700790
791 /* remove any? */
Sage Weil51042122009-11-04 11:39:12 -0800792 while (rbp && pgid_cmp(rb_entry(rbp, struct ceph_pg_mapping,
793 node)->pgid, pgid) <= 0) {
Sage Weilf24e9982009-10-06 11:31:10 -0700794 struct rb_node *cur = rbp;
795 rbp = rb_next(rbp);
796 dout(" removed pg_temp %llx\n",
Sage Weil51042122009-11-04 11:39:12 -0800797 *(u64 *)&rb_entry(cur, struct ceph_pg_mapping,
798 node)->pgid);
Sage Weilf24e9982009-10-06 11:31:10 -0700799 rb_erase(cur, &map->pg_temp);
800 }
801
802 if (pglen) {
803 /* insert */
804 ceph_decode_need(p, end, pglen*sizeof(u32), bad);
805 pg = kmalloc(sizeof(*pg) + sizeof(u32)*pglen, GFP_NOFS);
806 if (!pg) {
807 err = -ENOMEM;
808 goto bad;
809 }
810 pg->pgid = pgid;
811 pg->len = pglen;
Sage Weil7067f792009-12-21 16:02:37 -0800812 for (j = 0; j < pglen; j++)
Sage Weilc89136e2009-10-14 09:59:09 -0700813 pg->osds[j] = ceph_decode_32(p);
Sage Weil991abb62009-10-08 22:22:37 -0700814 err = __insert_pg_mapping(pg, &map->pg_temp);
815 if (err)
816 goto bad;
Sage Weil51042122009-11-04 11:39:12 -0800817 dout(" added pg_temp %llx len %d\n", *(u64 *)&pgid,
818 pglen);
Sage Weilf24e9982009-10-06 11:31:10 -0700819 }
820 }
821 while (rbp) {
822 struct rb_node *cur = rbp;
823 rbp = rb_next(rbp);
824 dout(" removed pg_temp %llx\n",
Sage Weil51042122009-11-04 11:39:12 -0800825 *(u64 *)&rb_entry(cur, struct ceph_pg_mapping,
826 node)->pgid);
Sage Weilf24e9982009-10-06 11:31:10 -0700827 rb_erase(cur, &map->pg_temp);
828 }
829
830 /* ignore the rest */
831 *p = end;
832 return map;
833
834bad:
835 pr_err("corrupt inc osdmap epoch %d off %d (%p of %p-%p)\n",
836 epoch, (int)(*p - start), *p, start, end);
Sage Weil9ec7cab2009-12-14 15:13:47 -0800837 print_hex_dump(KERN_DEBUG, "osdmap: ",
838 DUMP_PREFIX_OFFSET, 16, 1,
839 start, end - start, true);
Sage Weilf24e9982009-10-06 11:31:10 -0700840 if (newcrush)
841 crush_destroy(newcrush);
842 return ERR_PTR(err);
843}
844
845
846
847
848/*
849 * calculate file layout from given offset, length.
850 * fill in correct oid, logical length, and object extent
851 * offset, length.
852 *
853 * for now, we write only a single su, until we can
854 * pass a stride back to the caller.
855 */
856void ceph_calc_file_object_mapping(struct ceph_file_layout *layout,
857 u64 off, u64 *plen,
Sage Weil645a1022009-10-28 15:15:05 -0700858 u64 *ono,
Sage Weilf24e9982009-10-06 11:31:10 -0700859 u64 *oxoff, u64 *oxlen)
860{
861 u32 osize = le32_to_cpu(layout->fl_object_size);
862 u32 su = le32_to_cpu(layout->fl_stripe_unit);
863 u32 sc = le32_to_cpu(layout->fl_stripe_count);
864 u32 bl, stripeno, stripepos, objsetno;
865 u32 su_per_object;
Noah Watkinsff1d1f72009-10-30 12:57:30 -0700866 u64 t, su_offset;
Sage Weilf24e9982009-10-06 11:31:10 -0700867
868 dout("mapping %llu~%llu osize %u fl_su %u\n", off, *plen,
869 osize, su);
Noah Watkins35e054a2009-10-28 14:04:48 -0700870 su_per_object = osize / su;
Sage Weilf24e9982009-10-06 11:31:10 -0700871 dout("osize %u / su %u = su_per_object %u\n", osize, su,
872 su_per_object);
873
874 BUG_ON((su & ~PAGE_MASK) != 0);
875 /* bl = *off / su; */
876 t = off;
877 do_div(t, su);
878 bl = t;
879 dout("off %llu / su %u = bl %u\n", off, su, bl);
880
881 stripeno = bl / sc;
882 stripepos = bl % sc;
883 objsetno = stripeno / su_per_object;
884
Sage Weil645a1022009-10-28 15:15:05 -0700885 *ono = objsetno * sc + stripepos;
886 dout("objset %u * sc %u = ono %u\n", objsetno, sc, (unsigned)*ono);
887
888 /* *oxoff = *off % layout->fl_stripe_unit; # offset in su */
Sage Weilf24e9982009-10-06 11:31:10 -0700889 t = off;
Noah Watkinsff1d1f72009-10-30 12:57:30 -0700890 su_offset = do_div(t, su);
891 *oxoff = su_offset + (stripeno % su_per_object) * su;
Sage Weil645a1022009-10-28 15:15:05 -0700892
Noah Watkinsff1d1f72009-10-30 12:57:30 -0700893 /*
894 * Calculate the length of the extent being written to the selected
895 * object. This is the minimum of the full length requested (plen) or
896 * the remainder of the current stripe being written to.
897 */
898 *oxlen = min_t(u64, *plen, su - su_offset);
Sage Weilf24e9982009-10-06 11:31:10 -0700899 *plen = *oxlen;
900
901 dout(" obj extent %llu~%llu\n", *oxoff, *oxlen);
902}
903
904/*
905 * calculate an object layout (i.e. pgid) from an oid,
906 * file_layout, and osdmap
907 */
908int ceph_calc_object_layout(struct ceph_object_layout *ol,
909 const char *oid,
910 struct ceph_file_layout *fl,
911 struct ceph_osdmap *osdmap)
912{
913 unsigned num, num_mask;
Sage Weil51042122009-11-04 11:39:12 -0800914 struct ceph_pg pgid;
Sage Weilf24e9982009-10-06 11:31:10 -0700915 s32 preferred = (s32)le32_to_cpu(fl->fl_pg_preferred);
916 int poolid = le32_to_cpu(fl->fl_pg_pool);
917 struct ceph_pg_pool_info *pool;
Sage Weil51042122009-11-04 11:39:12 -0800918 unsigned ps;
Sage Weilf24e9982009-10-06 11:31:10 -0700919
Sage Weil30dc6382009-12-21 14:49:37 -0800920 BUG_ON(!osdmap);
Sage Weilf24e9982009-10-06 11:31:10 -0700921
Sage Weil4fc51be2010-02-16 15:55:03 -0800922 pool = __lookup_pg_pool(&osdmap->pg_pools, poolid);
923 if (!pool)
924 return -EIO;
Sage Weil1654dd02009-11-06 21:55:25 -0800925 ps = ceph_str_hash(pool->v.object_hash, oid, strlen(oid));
Sage Weilf24e9982009-10-06 11:31:10 -0700926 if (preferred >= 0) {
Sage Weil51042122009-11-04 11:39:12 -0800927 ps += preferred;
Sage Weilf24e9982009-10-06 11:31:10 -0700928 num = le32_to_cpu(pool->v.lpg_num);
929 num_mask = pool->lpg_num_mask;
930 } else {
931 num = le32_to_cpu(pool->v.pg_num);
932 num_mask = pool->pg_num_mask;
933 }
934
Sage Weil51042122009-11-04 11:39:12 -0800935 pgid.ps = cpu_to_le16(ps);
936 pgid.preferred = cpu_to_le16(preferred);
937 pgid.pool = fl->fl_pg_pool;
Sage Weilee7fdfa2009-10-19 11:41:51 -0700938 if (preferred >= 0)
Sage Weil51042122009-11-04 11:39:12 -0800939 dout("calc_object_layout '%s' pgid %d.%xp%d\n", oid, poolid, ps,
940 (int)preferred);
Sage Weilf24e9982009-10-06 11:31:10 -0700941 else
Sage Weil51042122009-11-04 11:39:12 -0800942 dout("calc_object_layout '%s' pgid %d.%x\n", oid, poolid, ps);
Sage Weilf24e9982009-10-06 11:31:10 -0700943
Sage Weil51042122009-11-04 11:39:12 -0800944 ol->ol_pgid = pgid;
Sage Weilf24e9982009-10-06 11:31:10 -0700945 ol->ol_stripe_unit = fl->fl_object_stripe_unit;
Sage Weilf24e9982009-10-06 11:31:10 -0700946 return 0;
947}
948
949/*
950 * Calculate raw osd vector for the given pgid. Return pointer to osd
951 * array, or NULL on failure.
952 */
Sage Weil51042122009-11-04 11:39:12 -0800953static int *calc_pg_raw(struct ceph_osdmap *osdmap, struct ceph_pg pgid,
Sage Weilf24e9982009-10-06 11:31:10 -0700954 int *osds, int *num)
955{
Sage Weilf24e9982009-10-06 11:31:10 -0700956 struct ceph_pg_mapping *pg;
957 struct ceph_pg_pool_info *pool;
958 int ruleno;
Sage Weil51042122009-11-04 11:39:12 -0800959 unsigned poolid, ps, pps;
960 int preferred;
Sage Weilf24e9982009-10-06 11:31:10 -0700961
962 /* pg_temp? */
Sage Weil9794b142010-02-16 15:53:32 -0800963 pg = __lookup_pg_mapping(&osdmap->pg_temp, pgid);
964 if (pg) {
965 *num = pg->len;
966 return pg->osds;
Sage Weilf24e9982009-10-06 11:31:10 -0700967 }
968
969 /* crush */
Sage Weil51042122009-11-04 11:39:12 -0800970 poolid = le32_to_cpu(pgid.pool);
971 ps = le16_to_cpu(pgid.ps);
972 preferred = (s16)le16_to_cpu(pgid.preferred);
973
Sage Weil767ea5c2009-12-09 12:34:01 -0800974 /* don't forcefeed bad device ids to crush */
975 if (preferred >= osdmap->max_osd ||
976 preferred >= osdmap->crush->max_devices)
977 preferred = -1;
978
Sage Weil4fc51be2010-02-16 15:55:03 -0800979 pool = __lookup_pg_pool(&osdmap->pg_pools, poolid);
980 if (!pool)
Sage Weilf24e9982009-10-06 11:31:10 -0700981 return NULL;
Sage Weilf24e9982009-10-06 11:31:10 -0700982 ruleno = crush_find_rule(osdmap->crush, pool->v.crush_ruleset,
983 pool->v.type, pool->v.size);
984 if (ruleno < 0) {
985 pr_err("no crush rule pool %d type %d size %d\n",
Sage Weil51042122009-11-04 11:39:12 -0800986 poolid, pool->v.type, pool->v.size);
Sage Weilf24e9982009-10-06 11:31:10 -0700987 return NULL;
988 }
989
Sage Weil51042122009-11-04 11:39:12 -0800990 if (preferred >= 0)
991 pps = ceph_stable_mod(ps,
Sage Weilf24e9982009-10-06 11:31:10 -0700992 le32_to_cpu(pool->v.lpgp_num),
993 pool->lpgp_num_mask);
994 else
Sage Weil51042122009-11-04 11:39:12 -0800995 pps = ceph_stable_mod(ps,
Sage Weilf24e9982009-10-06 11:31:10 -0700996 le32_to_cpu(pool->v.pgp_num),
997 pool->pgp_num_mask);
Sage Weil51042122009-11-04 11:39:12 -0800998 pps += poolid;
Sage Weilf24e9982009-10-06 11:31:10 -0700999 *num = crush_do_rule(osdmap->crush, ruleno, pps, osds,
1000 min_t(int, pool->v.size, *num),
Sage Weil51042122009-11-04 11:39:12 -08001001 preferred, osdmap->osd_weight);
Sage Weilf24e9982009-10-06 11:31:10 -07001002 return osds;
1003}
1004
1005/*
1006 * Return primary osd for given pgid, or -1 if none.
1007 */
Sage Weil51042122009-11-04 11:39:12 -08001008int ceph_calc_pg_primary(struct ceph_osdmap *osdmap, struct ceph_pg pgid)
Sage Weilf24e9982009-10-06 11:31:10 -07001009{
1010 int rawosds[10], *osds;
1011 int i, num = ARRAY_SIZE(rawosds);
1012
1013 osds = calc_pg_raw(osdmap, pgid, rawosds, &num);
1014 if (!osds)
1015 return -1;
1016
1017 /* primary is first up osd */
1018 for (i = 0; i < num; i++)
1019 if (ceph_osd_is_up(osdmap, osds[i])) {
1020 return osds[i];
1021 break;
1022 }
1023 return -1;
1024}