Commit | Line | Data |
---|---|---|
f24e9980 SW |
1 | |
2 | #include <asm/div64.h> | |
3 | ||
4 | #include "super.h" | |
5 | #include "osdmap.h" | |
6 | #include "crush/hash.h" | |
7 | #include "crush/mapper.h" | |
8 | #include "decode.h" | |
9 | #include "ceph_debug.h" | |
10 | ||
11 | char *ceph_osdmap_state_str(char *str, int len, int state) | |
12 | { | |
13 | int flag = 0; | |
14 | ||
15 | if (!len) | |
16 | goto done; | |
17 | ||
18 | *str = '\0'; | |
19 | if (state) { | |
20 | if (state & CEPH_OSD_EXISTS) { | |
21 | snprintf(str, len, "exists"); | |
22 | flag = 1; | |
23 | } | |
24 | if (state & CEPH_OSD_UP) { | |
25 | snprintf(str, len, "%s%s%s", str, (flag ? ", " : ""), | |
26 | "up"); | |
27 | flag = 1; | |
28 | } | |
29 | } else { | |
30 | snprintf(str, len, "doesn't exist"); | |
31 | } | |
32 | done: | |
33 | return str; | |
34 | } | |
35 | ||
36 | /* maps */ | |
37 | ||
38 | static int calc_bits_of(unsigned t) | |
39 | { | |
40 | int b = 0; | |
41 | while (t) { | |
42 | t = t >> 1; | |
43 | b++; | |
44 | } | |
45 | return b; | |
46 | } | |
47 | ||
48 | /* | |
49 | * the foo_mask is the smallest value 2^n-1 that is >= foo. | |
50 | */ | |
51 | static void calc_pg_masks(struct ceph_pg_pool_info *pi) | |
52 | { | |
53 | pi->pg_num_mask = (1 << calc_bits_of(le32_to_cpu(pi->v.pg_num)-1)) - 1; | |
54 | pi->pgp_num_mask = | |
55 | (1 << calc_bits_of(le32_to_cpu(pi->v.pgp_num)-1)) - 1; | |
56 | pi->lpg_num_mask = | |
57 | (1 << calc_bits_of(le32_to_cpu(pi->v.lpg_num)-1)) - 1; | |
58 | pi->lpgp_num_mask = | |
59 | (1 << calc_bits_of(le32_to_cpu(pi->v.lpgp_num)-1)) - 1; | |
60 | } | |
61 | ||
62 | /* | |
63 | * decode crush map | |
64 | */ | |
65 | static int crush_decode_uniform_bucket(void **p, void *end, | |
66 | struct crush_bucket_uniform *b) | |
67 | { | |
68 | dout("crush_decode_uniform_bucket %p to %p\n", *p, end); | |
69 | ceph_decode_need(p, end, (1+b->h.size) * sizeof(u32), bad); | |
c89136ea | 70 | b->item_weight = ceph_decode_32(p); |
f24e9980 SW |
71 | return 0; |
72 | bad: | |
73 | return -EINVAL; | |
74 | } | |
75 | ||
76 | static int crush_decode_list_bucket(void **p, void *end, | |
77 | struct crush_bucket_list *b) | |
78 | { | |
79 | int j; | |
80 | dout("crush_decode_list_bucket %p to %p\n", *p, end); | |
81 | b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS); | |
82 | if (b->item_weights == NULL) | |
83 | return -ENOMEM; | |
84 | b->sum_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS); | |
85 | if (b->sum_weights == NULL) | |
86 | return -ENOMEM; | |
87 | ceph_decode_need(p, end, 2 * b->h.size * sizeof(u32), bad); | |
88 | for (j = 0; j < b->h.size; j++) { | |
c89136ea SW |
89 | b->item_weights[j] = ceph_decode_32(p); |
90 | b->sum_weights[j] = ceph_decode_32(p); | |
f24e9980 SW |
91 | } |
92 | return 0; | |
93 | bad: | |
94 | return -EINVAL; | |
95 | } | |
96 | ||
97 | static int crush_decode_tree_bucket(void **p, void *end, | |
98 | struct crush_bucket_tree *b) | |
99 | { | |
100 | int j; | |
101 | dout("crush_decode_tree_bucket %p to %p\n", *p, end); | |
102 | ceph_decode_32_safe(p, end, b->num_nodes, bad); | |
103 | b->node_weights = kcalloc(b->num_nodes, sizeof(u32), GFP_NOFS); | |
104 | if (b->node_weights == NULL) | |
105 | return -ENOMEM; | |
106 | ceph_decode_need(p, end, b->num_nodes * sizeof(u32), bad); | |
107 | for (j = 0; j < b->num_nodes; j++) | |
c89136ea | 108 | b->node_weights[j] = ceph_decode_32(p); |
f24e9980 SW |
109 | return 0; |
110 | bad: | |
111 | return -EINVAL; | |
112 | } | |
113 | ||
114 | static int crush_decode_straw_bucket(void **p, void *end, | |
115 | struct crush_bucket_straw *b) | |
116 | { | |
117 | int j; | |
118 | dout("crush_decode_straw_bucket %p to %p\n", *p, end); | |
119 | b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS); | |
120 | if (b->item_weights == NULL) | |
121 | return -ENOMEM; | |
122 | b->straws = kcalloc(b->h.size, sizeof(u32), GFP_NOFS); | |
123 | if (b->straws == NULL) | |
124 | return -ENOMEM; | |
125 | ceph_decode_need(p, end, 2 * b->h.size * sizeof(u32), bad); | |
126 | for (j = 0; j < b->h.size; j++) { | |
c89136ea SW |
127 | b->item_weights[j] = ceph_decode_32(p); |
128 | b->straws[j] = ceph_decode_32(p); | |
f24e9980 SW |
129 | } |
130 | return 0; | |
131 | bad: | |
132 | return -EINVAL; | |
133 | } | |
134 | ||
135 | static struct crush_map *crush_decode(void *pbyval, void *end) | |
136 | { | |
137 | struct crush_map *c; | |
138 | int err = -EINVAL; | |
139 | int i, j; | |
140 | void **p = &pbyval; | |
141 | void *start = pbyval; | |
142 | u32 magic; | |
143 | ||
144 | dout("crush_decode %p to %p len %d\n", *p, end, (int)(end - *p)); | |
145 | ||
146 | c = kzalloc(sizeof(*c), GFP_NOFS); | |
147 | if (c == NULL) | |
148 | return ERR_PTR(-ENOMEM); | |
149 | ||
150 | ceph_decode_need(p, end, 4*sizeof(u32), bad); | |
c89136ea | 151 | magic = ceph_decode_32(p); |
f24e9980 SW |
152 | if (magic != CRUSH_MAGIC) { |
153 | pr_err("crush_decode magic %x != current %x\n", | |
154 | (unsigned)magic, (unsigned)CRUSH_MAGIC); | |
155 | goto bad; | |
156 | } | |
c89136ea SW |
157 | c->max_buckets = ceph_decode_32(p); |
158 | c->max_rules = ceph_decode_32(p); | |
159 | c->max_devices = ceph_decode_32(p); | |
f24e9980 SW |
160 | |
161 | c->device_parents = kcalloc(c->max_devices, sizeof(u32), GFP_NOFS); | |
162 | if (c->device_parents == NULL) | |
163 | goto badmem; | |
164 | c->bucket_parents = kcalloc(c->max_buckets, sizeof(u32), GFP_NOFS); | |
165 | if (c->bucket_parents == NULL) | |
166 | goto badmem; | |
167 | ||
168 | c->buckets = kcalloc(c->max_buckets, sizeof(*c->buckets), GFP_NOFS); | |
169 | if (c->buckets == NULL) | |
170 | goto badmem; | |
171 | c->rules = kcalloc(c->max_rules, sizeof(*c->rules), GFP_NOFS); | |
172 | if (c->rules == NULL) | |
173 | goto badmem; | |
174 | ||
175 | /* buckets */ | |
176 | for (i = 0; i < c->max_buckets; i++) { | |
177 | int size = 0; | |
178 | u32 alg; | |
179 | struct crush_bucket *b; | |
180 | ||
181 | ceph_decode_32_safe(p, end, alg, bad); | |
182 | if (alg == 0) { | |
183 | c->buckets[i] = NULL; | |
184 | continue; | |
185 | } | |
186 | dout("crush_decode bucket %d off %x %p to %p\n", | |
187 | i, (int)(*p-start), *p, end); | |
188 | ||
189 | switch (alg) { | |
190 | case CRUSH_BUCKET_UNIFORM: | |
191 | size = sizeof(struct crush_bucket_uniform); | |
192 | break; | |
193 | case CRUSH_BUCKET_LIST: | |
194 | size = sizeof(struct crush_bucket_list); | |
195 | break; | |
196 | case CRUSH_BUCKET_TREE: | |
197 | size = sizeof(struct crush_bucket_tree); | |
198 | break; | |
199 | case CRUSH_BUCKET_STRAW: | |
200 | size = sizeof(struct crush_bucket_straw); | |
201 | break; | |
202 | default: | |
30dc6381 | 203 | err = -EINVAL; |
f24e9980 SW |
204 | goto bad; |
205 | } | |
206 | BUG_ON(size == 0); | |
207 | b = c->buckets[i] = kzalloc(size, GFP_NOFS); | |
208 | if (b == NULL) | |
209 | goto badmem; | |
210 | ||
211 | ceph_decode_need(p, end, 4*sizeof(u32), bad); | |
c89136ea SW |
212 | b->id = ceph_decode_32(p); |
213 | b->type = ceph_decode_16(p); | |
fb690390 SW |
214 | b->alg = ceph_decode_8(p); |
215 | b->hash = ceph_decode_8(p); | |
c89136ea SW |
216 | b->weight = ceph_decode_32(p); |
217 | b->size = ceph_decode_32(p); | |
f24e9980 SW |
218 | |
219 | dout("crush_decode bucket size %d off %x %p to %p\n", | |
220 | b->size, (int)(*p-start), *p, end); | |
221 | ||
222 | b->items = kcalloc(b->size, sizeof(__s32), GFP_NOFS); | |
223 | if (b->items == NULL) | |
224 | goto badmem; | |
225 | b->perm = kcalloc(b->size, sizeof(u32), GFP_NOFS); | |
226 | if (b->perm == NULL) | |
227 | goto badmem; | |
228 | b->perm_n = 0; | |
229 | ||
230 | ceph_decode_need(p, end, b->size*sizeof(u32), bad); | |
231 | for (j = 0; j < b->size; j++) | |
c89136ea | 232 | b->items[j] = ceph_decode_32(p); |
f24e9980 SW |
233 | |
234 | switch (b->alg) { | |
235 | case CRUSH_BUCKET_UNIFORM: | |
236 | err = crush_decode_uniform_bucket(p, end, | |
237 | (struct crush_bucket_uniform *)b); | |
238 | if (err < 0) | |
239 | goto bad; | |
240 | break; | |
241 | case CRUSH_BUCKET_LIST: | |
242 | err = crush_decode_list_bucket(p, end, | |
243 | (struct crush_bucket_list *)b); | |
244 | if (err < 0) | |
245 | goto bad; | |
246 | break; | |
247 | case CRUSH_BUCKET_TREE: | |
248 | err = crush_decode_tree_bucket(p, end, | |
249 | (struct crush_bucket_tree *)b); | |
250 | if (err < 0) | |
251 | goto bad; | |
252 | break; | |
253 | case CRUSH_BUCKET_STRAW: | |
254 | err = crush_decode_straw_bucket(p, end, | |
255 | (struct crush_bucket_straw *)b); | |
256 | if (err < 0) | |
257 | goto bad; | |
258 | break; | |
259 | } | |
260 | } | |
261 | ||
262 | /* rules */ | |
263 | dout("rule vec is %p\n", c->rules); | |
264 | for (i = 0; i < c->max_rules; i++) { | |
265 | u32 yes; | |
266 | struct crush_rule *r; | |
267 | ||
268 | ceph_decode_32_safe(p, end, yes, bad); | |
269 | if (!yes) { | |
270 | dout("crush_decode NO rule %d off %x %p to %p\n", | |
271 | i, (int)(*p-start), *p, end); | |
272 | c->rules[i] = NULL; | |
273 | continue; | |
274 | } | |
275 | ||
276 | dout("crush_decode rule %d off %x %p to %p\n", | |
277 | i, (int)(*p-start), *p, end); | |
278 | ||
279 | /* len */ | |
280 | ceph_decode_32_safe(p, end, yes, bad); | |
281 | #if BITS_PER_LONG == 32 | |
30dc6381 | 282 | err = -EINVAL; |
f24e9980 SW |
283 | if (yes > ULONG_MAX / sizeof(struct crush_rule_step)) |
284 | goto bad; | |
285 | #endif | |
286 | r = c->rules[i] = kmalloc(sizeof(*r) + | |
287 | yes*sizeof(struct crush_rule_step), | |
288 | GFP_NOFS); | |
289 | if (r == NULL) | |
290 | goto badmem; | |
291 | dout(" rule %d is at %p\n", i, r); | |
292 | r->len = yes; | |
293 | ceph_decode_copy_safe(p, end, &r->mask, 4, bad); /* 4 u8's */ | |
294 | ceph_decode_need(p, end, r->len*3*sizeof(u32), bad); | |
295 | for (j = 0; j < r->len; j++) { | |
c89136ea SW |
296 | r->steps[j].op = ceph_decode_32(p); |
297 | r->steps[j].arg1 = ceph_decode_32(p); | |
298 | r->steps[j].arg2 = ceph_decode_32(p); | |
f24e9980 SW |
299 | } |
300 | } | |
301 | ||
302 | /* ignore trailing name maps. */ | |
303 | ||
304 | dout("crush_decode success\n"); | |
305 | return c; | |
306 | ||
307 | badmem: | |
308 | err = -ENOMEM; | |
309 | bad: | |
310 | dout("crush_decode fail %d\n", err); | |
311 | crush_destroy(c); | |
312 | return ERR_PTR(err); | |
313 | } | |
314 | ||
315 | ||
316 | /* | |
317 | * osd map | |
318 | */ | |
319 | void ceph_osdmap_destroy(struct ceph_osdmap *map) | |
320 | { | |
321 | dout("osdmap_destroy %p\n", map); | |
322 | if (map->crush) | |
323 | crush_destroy(map->crush); | |
9794b146 SW |
324 | while (!RB_EMPTY_ROOT(&map->pg_temp)) { |
325 | struct ceph_pg_mapping *pg = | |
326 | rb_entry(rb_first(&map->pg_temp), | |
327 | struct ceph_pg_mapping, node); | |
328 | rb_erase(&pg->node, &map->pg_temp); | |
329 | kfree(pg); | |
330 | } | |
f24e9980 SW |
331 | kfree(map->osd_state); |
332 | kfree(map->osd_weight); | |
333 | kfree(map->pg_pool); | |
334 | kfree(map->osd_addr); | |
335 | kfree(map); | |
336 | } | |
337 | ||
338 | /* | |
339 | * adjust max osd value. reallocate arrays. | |
340 | */ | |
341 | static int osdmap_set_max_osd(struct ceph_osdmap *map, int max) | |
342 | { | |
343 | u8 *state; | |
344 | struct ceph_entity_addr *addr; | |
345 | u32 *weight; | |
346 | ||
347 | state = kcalloc(max, sizeof(*state), GFP_NOFS); | |
348 | addr = kcalloc(max, sizeof(*addr), GFP_NOFS); | |
349 | weight = kcalloc(max, sizeof(*weight), GFP_NOFS); | |
350 | if (state == NULL || addr == NULL || weight == NULL) { | |
351 | kfree(state); | |
352 | kfree(addr); | |
353 | kfree(weight); | |
354 | return -ENOMEM; | |
355 | } | |
356 | ||
357 | /* copy old? */ | |
358 | if (map->osd_state) { | |
359 | memcpy(state, map->osd_state, map->max_osd*sizeof(*state)); | |
360 | memcpy(addr, map->osd_addr, map->max_osd*sizeof(*addr)); | |
361 | memcpy(weight, map->osd_weight, map->max_osd*sizeof(*weight)); | |
362 | kfree(map->osd_state); | |
363 | kfree(map->osd_addr); | |
364 | kfree(map->osd_weight); | |
365 | } | |
366 | ||
367 | map->osd_state = state; | |
368 | map->osd_weight = weight; | |
369 | map->osd_addr = addr; | |
370 | map->max_osd = max; | |
371 | return 0; | |
372 | } | |
373 | ||
374 | /* | |
9794b146 SW |
375 | * rbtree of pg_mapping for handling pg_temp (explicit mapping of pgid |
376 | * to a set of osds) | |
f24e9980 | 377 | */ |
51042122 SW |
378 | static int pgid_cmp(struct ceph_pg l, struct ceph_pg r) |
379 | { | |
380 | u64 a = *(u64 *)&l; | |
381 | u64 b = *(u64 *)&r; | |
382 | ||
383 | if (a < b) | |
384 | return -1; | |
385 | if (a > b) | |
386 | return 1; | |
387 | return 0; | |
388 | } | |
389 | ||
991abb6e SW |
390 | static int __insert_pg_mapping(struct ceph_pg_mapping *new, |
391 | struct rb_root *root) | |
f24e9980 SW |
392 | { |
393 | struct rb_node **p = &root->rb_node; | |
394 | struct rb_node *parent = NULL; | |
395 | struct ceph_pg_mapping *pg = NULL; | |
51042122 | 396 | int c; |
f24e9980 SW |
397 | |
398 | while (*p) { | |
399 | parent = *p; | |
400 | pg = rb_entry(parent, struct ceph_pg_mapping, node); | |
51042122 SW |
401 | c = pgid_cmp(new->pgid, pg->pgid); |
402 | if (c < 0) | |
f24e9980 | 403 | p = &(*p)->rb_left; |
51042122 | 404 | else if (c > 0) |
f24e9980 SW |
405 | p = &(*p)->rb_right; |
406 | else | |
991abb6e | 407 | return -EEXIST; |
f24e9980 SW |
408 | } |
409 | ||
410 | rb_link_node(&new->node, parent, p); | |
411 | rb_insert_color(&new->node, root); | |
991abb6e | 412 | return 0; |
f24e9980 SW |
413 | } |
414 | ||
9794b146 SW |
415 | static struct ceph_pg_mapping *__lookup_pg_mapping(struct rb_root *root, |
416 | struct ceph_pg pgid) | |
417 | { | |
418 | struct rb_node *n = root->rb_node; | |
419 | struct ceph_pg_mapping *pg; | |
420 | int c; | |
421 | ||
422 | while (n) { | |
423 | pg = rb_entry(n, struct ceph_pg_mapping, node); | |
424 | c = pgid_cmp(pgid, pg->pgid); | |
425 | if (c < 0) | |
426 | n = n->rb_left; | |
427 | else if (c > 0) | |
428 | n = n->rb_right; | |
429 | else | |
430 | return pg; | |
431 | } | |
432 | return NULL; | |
433 | } | |
434 | ||
f24e9980 SW |
435 | /* |
436 | * decode a full map. | |
437 | */ | |
438 | struct ceph_osdmap *osdmap_decode(void **p, void *end) | |
439 | { | |
440 | struct ceph_osdmap *map; | |
441 | u16 version; | |
442 | u32 len, max, i; | |
361be860 | 443 | u8 ev; |
f24e9980 SW |
444 | int err = -EINVAL; |
445 | void *start = *p; | |
446 | ||
447 | dout("osdmap_decode %p to %p len %d\n", *p, end, (int)(end - *p)); | |
448 | ||
449 | map = kzalloc(sizeof(*map), GFP_NOFS); | |
450 | if (map == NULL) | |
451 | return ERR_PTR(-ENOMEM); | |
452 | map->pg_temp = RB_ROOT; | |
453 | ||
454 | ceph_decode_16_safe(p, end, version, bad); | |
02f90c61 SW |
455 | if (version > CEPH_OSDMAP_VERSION) { |
456 | pr_warning("got unknown v %d > %d of osdmap\n", version, | |
457 | CEPH_OSDMAP_VERSION); | |
458 | goto bad; | |
459 | } | |
f24e9980 SW |
460 | |
461 | ceph_decode_need(p, end, 2*sizeof(u64)+6*sizeof(u32), bad); | |
462 | ceph_decode_copy(p, &map->fsid, sizeof(map->fsid)); | |
c89136ea | 463 | map->epoch = ceph_decode_32(p); |
f24e9980 SW |
464 | ceph_decode_copy(p, &map->created, sizeof(map->created)); |
465 | ceph_decode_copy(p, &map->modified, sizeof(map->modified)); | |
466 | ||
c89136ea | 467 | map->num_pools = ceph_decode_32(p); |
f24e9980 SW |
468 | map->pg_pool = kcalloc(map->num_pools, sizeof(*map->pg_pool), |
469 | GFP_NOFS); | |
470 | if (!map->pg_pool) { | |
471 | err = -ENOMEM; | |
472 | goto bad; | |
473 | } | |
474 | ceph_decode_32_safe(p, end, max, bad); | |
475 | while (max--) { | |
361be860 | 476 | ceph_decode_need(p, end, 4+1+sizeof(map->pg_pool->v), bad); |
c89136ea | 477 | i = ceph_decode_32(p); |
f24e9980 SW |
478 | if (i >= map->num_pools) |
479 | goto bad; | |
361be860 | 480 | ev = ceph_decode_8(p); /* encoding version */ |
02f90c61 SW |
481 | if (ev > CEPH_PG_POOL_VERSION) { |
482 | pr_warning("got unknown v %d > %d of ceph_pg_pool\n", | |
483 | ev, CEPH_PG_POOL_VERSION); | |
484 | goto bad; | |
485 | } | |
f24e9980 SW |
486 | ceph_decode_copy(p, &map->pg_pool[i].v, |
487 | sizeof(map->pg_pool->v)); | |
488 | calc_pg_masks(&map->pg_pool[i]); | |
489 | p += le32_to_cpu(map->pg_pool[i].v.num_snaps) * sizeof(u64); | |
490 | p += le32_to_cpu(map->pg_pool[i].v.num_removed_snap_intervals) | |
491 | * sizeof(u64) * 2; | |
492 | } | |
493 | ||
494 | ceph_decode_32_safe(p, end, map->flags, bad); | |
495 | ||
c89136ea | 496 | max = ceph_decode_32(p); |
f24e9980 SW |
497 | |
498 | /* (re)alloc osd arrays */ | |
499 | err = osdmap_set_max_osd(map, max); | |
500 | if (err < 0) | |
501 | goto bad; | |
502 | dout("osdmap_decode max_osd = %d\n", map->max_osd); | |
503 | ||
504 | /* osds */ | |
505 | err = -EINVAL; | |
506 | ceph_decode_need(p, end, 3*sizeof(u32) + | |
507 | map->max_osd*(1 + sizeof(*map->osd_weight) + | |
508 | sizeof(*map->osd_addr)), bad); | |
509 | *p += 4; /* skip length field (should match max) */ | |
510 | ceph_decode_copy(p, map->osd_state, map->max_osd); | |
511 | ||
512 | *p += 4; /* skip length field (should match max) */ | |
513 | for (i = 0; i < map->max_osd; i++) | |
c89136ea | 514 | map->osd_weight[i] = ceph_decode_32(p); |
f24e9980 SW |
515 | |
516 | *p += 4; /* skip length field (should match max) */ | |
517 | ceph_decode_copy(p, map->osd_addr, map->max_osd*sizeof(*map->osd_addr)); | |
63f2d211 SW |
518 | for (i = 0; i < map->max_osd; i++) |
519 | ceph_decode_addr(&map->osd_addr[i]); | |
f24e9980 SW |
520 | |
521 | /* pg_temp */ | |
522 | ceph_decode_32_safe(p, end, len, bad); | |
523 | for (i = 0; i < len; i++) { | |
524 | int n, j; | |
51042122 | 525 | struct ceph_pg pgid; |
f24e9980 SW |
526 | struct ceph_pg_mapping *pg; |
527 | ||
528 | ceph_decode_need(p, end, sizeof(u32) + sizeof(u64), bad); | |
51042122 | 529 | ceph_decode_copy(p, &pgid, sizeof(pgid)); |
c89136ea | 530 | n = ceph_decode_32(p); |
f24e9980 | 531 | ceph_decode_need(p, end, n * sizeof(u32), bad); |
30dc6381 | 532 | err = -ENOMEM; |
f24e9980 | 533 | pg = kmalloc(sizeof(*pg) + n*sizeof(u32), GFP_NOFS); |
30dc6381 | 534 | if (!pg) |
f24e9980 | 535 | goto bad; |
f24e9980 SW |
536 | pg->pgid = pgid; |
537 | pg->len = n; | |
538 | for (j = 0; j < n; j++) | |
c89136ea | 539 | pg->osds[j] = ceph_decode_32(p); |
f24e9980 | 540 | |
991abb6e SW |
541 | err = __insert_pg_mapping(pg, &map->pg_temp); |
542 | if (err) | |
543 | goto bad; | |
51042122 | 544 | dout(" added pg_temp %llx len %d\n", *(u64 *)&pgid, len); |
f24e9980 SW |
545 | } |
546 | ||
547 | /* crush */ | |
548 | ceph_decode_32_safe(p, end, len, bad); | |
549 | dout("osdmap_decode crush len %d from off 0x%x\n", len, | |
550 | (int)(*p - start)); | |
551 | ceph_decode_need(p, end, len, bad); | |
552 | map->crush = crush_decode(*p, end); | |
553 | *p += len; | |
554 | if (IS_ERR(map->crush)) { | |
555 | err = PTR_ERR(map->crush); | |
556 | map->crush = NULL; | |
557 | goto bad; | |
558 | } | |
559 | ||
560 | /* ignore the rest of the map */ | |
561 | *p = end; | |
562 | ||
563 | dout("osdmap_decode done %p %p\n", *p, end); | |
564 | return map; | |
565 | ||
566 | bad: | |
567 | dout("osdmap_decode fail\n"); | |
568 | ceph_osdmap_destroy(map); | |
569 | return ERR_PTR(err); | |
570 | } | |
571 | ||
572 | /* | |
573 | * decode and apply an incremental map update. | |
574 | */ | |
575 | struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end, | |
576 | struct ceph_osdmap *map, | |
577 | struct ceph_messenger *msgr) | |
578 | { | |
f24e9980 SW |
579 | struct crush_map *newcrush = NULL; |
580 | struct ceph_fsid fsid; | |
581 | u32 epoch = 0; | |
582 | struct ceph_timespec modified; | |
583 | u32 len, pool; | |
584 | __s32 new_flags, max; | |
585 | void *start = *p; | |
586 | int err = -EINVAL; | |
587 | u16 version; | |
588 | struct rb_node *rbp; | |
589 | ||
590 | ceph_decode_16_safe(p, end, version, bad); | |
02f90c61 SW |
591 | if (version > CEPH_OSDMAP_INC_VERSION) { |
592 | pr_warning("got unknown v %d > %d of inc osdmap\n", version, | |
593 | CEPH_OSDMAP_INC_VERSION); | |
594 | goto bad; | |
595 | } | |
f24e9980 SW |
596 | |
597 | ceph_decode_need(p, end, sizeof(fsid)+sizeof(modified)+2*sizeof(u32), | |
598 | bad); | |
599 | ceph_decode_copy(p, &fsid, sizeof(fsid)); | |
c89136ea | 600 | epoch = ceph_decode_32(p); |
f24e9980 SW |
601 | BUG_ON(epoch != map->epoch+1); |
602 | ceph_decode_copy(p, &modified, sizeof(modified)); | |
c89136ea | 603 | new_flags = ceph_decode_32(p); |
f24e9980 SW |
604 | |
605 | /* full map? */ | |
606 | ceph_decode_32_safe(p, end, len, bad); | |
607 | if (len > 0) { | |
608 | dout("apply_incremental full map len %d, %p to %p\n", | |
609 | len, *p, end); | |
30dc6381 | 610 | return osdmap_decode(p, min(*p+len, end)); |
f24e9980 SW |
611 | } |
612 | ||
613 | /* new crush? */ | |
614 | ceph_decode_32_safe(p, end, len, bad); | |
615 | if (len > 0) { | |
616 | dout("apply_incremental new crush map len %d, %p to %p\n", | |
617 | len, *p, end); | |
618 | newcrush = crush_decode(*p, min(*p+len, end)); | |
619 | if (IS_ERR(newcrush)) | |
620 | return ERR_PTR(PTR_ERR(newcrush)); | |
621 | } | |
622 | ||
623 | /* new flags? */ | |
624 | if (new_flags >= 0) | |
625 | map->flags = new_flags; | |
626 | ||
627 | ceph_decode_need(p, end, 5*sizeof(u32), bad); | |
628 | ||
629 | /* new max? */ | |
c89136ea | 630 | max = ceph_decode_32(p); |
f24e9980 SW |
631 | if (max >= 0) { |
632 | err = osdmap_set_max_osd(map, max); | |
633 | if (err < 0) | |
634 | goto bad; | |
635 | } | |
636 | ||
637 | map->epoch++; | |
638 | map->modified = map->modified; | |
639 | if (newcrush) { | |
640 | if (map->crush) | |
641 | crush_destroy(map->crush); | |
642 | map->crush = newcrush; | |
643 | newcrush = NULL; | |
644 | } | |
645 | ||
646 | /* new_pool */ | |
647 | ceph_decode_32_safe(p, end, len, bad); | |
648 | while (len--) { | |
361be860 SW |
649 | __u8 ev; |
650 | ||
f24e9980 SW |
651 | ceph_decode_32_safe(p, end, pool, bad); |
652 | if (pool >= map->num_pools) { | |
653 | void *pg_pool = kcalloc(pool + 1, | |
654 | sizeof(*map->pg_pool), | |
655 | GFP_NOFS); | |
656 | if (!pg_pool) { | |
657 | err = -ENOMEM; | |
658 | goto bad; | |
659 | } | |
660 | memcpy(pg_pool, map->pg_pool, | |
661 | map->num_pools * sizeof(*map->pg_pool)); | |
662 | kfree(map->pg_pool); | |
663 | map->pg_pool = pg_pool; | |
664 | map->num_pools = pool+1; | |
665 | } | |
361be860 SW |
666 | ceph_decode_need(p, end, 1 + sizeof(map->pg_pool->v), bad); |
667 | ev = ceph_decode_8(p); /* encoding version */ | |
02f90c61 SW |
668 | if (ev > CEPH_PG_POOL_VERSION) { |
669 | pr_warning("got unknown v %d > %d of ceph_pg_pool\n", | |
670 | ev, CEPH_PG_POOL_VERSION); | |
671 | goto bad; | |
672 | } | |
f24e9980 SW |
673 | ceph_decode_copy(p, &map->pg_pool[pool].v, |
674 | sizeof(map->pg_pool->v)); | |
675 | calc_pg_masks(&map->pg_pool[pool]); | |
676 | } | |
677 | ||
678 | /* old_pool (ignore) */ | |
679 | ceph_decode_32_safe(p, end, len, bad); | |
680 | *p += len * sizeof(u32); | |
681 | ||
682 | /* new_up */ | |
683 | err = -EINVAL; | |
684 | ceph_decode_32_safe(p, end, len, bad); | |
685 | while (len--) { | |
686 | u32 osd; | |
687 | struct ceph_entity_addr addr; | |
688 | ceph_decode_32_safe(p, end, osd, bad); | |
689 | ceph_decode_copy_safe(p, end, &addr, sizeof(addr), bad); | |
63f2d211 | 690 | ceph_decode_addr(&addr); |
f24e9980 SW |
691 | pr_info("osd%d up\n", osd); |
692 | BUG_ON(osd >= map->max_osd); | |
693 | map->osd_state[osd] |= CEPH_OSD_UP; | |
694 | map->osd_addr[osd] = addr; | |
695 | } | |
696 | ||
697 | /* new_down */ | |
698 | ceph_decode_32_safe(p, end, len, bad); | |
699 | while (len--) { | |
700 | u32 osd; | |
701 | ceph_decode_32_safe(p, end, osd, bad); | |
702 | (*p)++; /* clean flag */ | |
1bdb70e5 | 703 | pr_info("osd%d down\n", osd); |
f24e9980 SW |
704 | if (osd < map->max_osd) |
705 | map->osd_state[osd] &= ~CEPH_OSD_UP; | |
706 | } | |
707 | ||
708 | /* new_weight */ | |
709 | ceph_decode_32_safe(p, end, len, bad); | |
710 | while (len--) { | |
711 | u32 osd, off; | |
712 | ceph_decode_need(p, end, sizeof(u32)*2, bad); | |
c89136ea SW |
713 | osd = ceph_decode_32(p); |
714 | off = ceph_decode_32(p); | |
f24e9980 SW |
715 | pr_info("osd%d weight 0x%x %s\n", osd, off, |
716 | off == CEPH_OSD_IN ? "(in)" : | |
717 | (off == CEPH_OSD_OUT ? "(out)" : "")); | |
718 | if (osd < map->max_osd) | |
719 | map->osd_weight[osd] = off; | |
720 | } | |
721 | ||
722 | /* new_pg_temp */ | |
723 | rbp = rb_first(&map->pg_temp); | |
724 | ceph_decode_32_safe(p, end, len, bad); | |
725 | while (len--) { | |
726 | struct ceph_pg_mapping *pg; | |
727 | int j; | |
51042122 | 728 | struct ceph_pg pgid; |
f24e9980 SW |
729 | u32 pglen; |
730 | ceph_decode_need(p, end, sizeof(u64) + sizeof(u32), bad); | |
51042122 | 731 | ceph_decode_copy(p, &pgid, sizeof(pgid)); |
c89136ea | 732 | pglen = ceph_decode_32(p); |
f24e9980 SW |
733 | |
734 | /* remove any? */ | |
51042122 SW |
735 | while (rbp && pgid_cmp(rb_entry(rbp, struct ceph_pg_mapping, |
736 | node)->pgid, pgid) <= 0) { | |
f24e9980 SW |
737 | struct rb_node *cur = rbp; |
738 | rbp = rb_next(rbp); | |
739 | dout(" removed pg_temp %llx\n", | |
51042122 SW |
740 | *(u64 *)&rb_entry(cur, struct ceph_pg_mapping, |
741 | node)->pgid); | |
f24e9980 SW |
742 | rb_erase(cur, &map->pg_temp); |
743 | } | |
744 | ||
745 | if (pglen) { | |
746 | /* insert */ | |
747 | ceph_decode_need(p, end, pglen*sizeof(u32), bad); | |
748 | pg = kmalloc(sizeof(*pg) + sizeof(u32)*pglen, GFP_NOFS); | |
749 | if (!pg) { | |
750 | err = -ENOMEM; | |
751 | goto bad; | |
752 | } | |
753 | pg->pgid = pgid; | |
754 | pg->len = pglen; | |
7067f797 | 755 | for (j = 0; j < pglen; j++) |
c89136ea | 756 | pg->osds[j] = ceph_decode_32(p); |
991abb6e SW |
757 | err = __insert_pg_mapping(pg, &map->pg_temp); |
758 | if (err) | |
759 | goto bad; | |
51042122 SW |
760 | dout(" added pg_temp %llx len %d\n", *(u64 *)&pgid, |
761 | pglen); | |
f24e9980 SW |
762 | } |
763 | } | |
764 | while (rbp) { | |
765 | struct rb_node *cur = rbp; | |
766 | rbp = rb_next(rbp); | |
767 | dout(" removed pg_temp %llx\n", | |
51042122 SW |
768 | *(u64 *)&rb_entry(cur, struct ceph_pg_mapping, |
769 | node)->pgid); | |
f24e9980 SW |
770 | rb_erase(cur, &map->pg_temp); |
771 | } | |
772 | ||
773 | /* ignore the rest */ | |
774 | *p = end; | |
775 | return map; | |
776 | ||
777 | bad: | |
778 | pr_err("corrupt inc osdmap epoch %d off %d (%p of %p-%p)\n", | |
779 | epoch, (int)(*p - start), *p, start, end); | |
9ec7cab1 SW |
780 | print_hex_dump(KERN_DEBUG, "osdmap: ", |
781 | DUMP_PREFIX_OFFSET, 16, 1, | |
782 | start, end - start, true); | |
f24e9980 SW |
783 | if (newcrush) |
784 | crush_destroy(newcrush); | |
785 | return ERR_PTR(err); | |
786 | } | |
787 | ||
788 | ||
789 | ||
790 | ||
791 | /* | |
792 | * calculate file layout from given offset, length. | |
793 | * fill in correct oid, logical length, and object extent | |
794 | * offset, length. | |
795 | * | |
796 | * for now, we write only a single su, until we can | |
797 | * pass a stride back to the caller. | |
798 | */ | |
799 | void ceph_calc_file_object_mapping(struct ceph_file_layout *layout, | |
800 | u64 off, u64 *plen, | |
645a1025 | 801 | u64 *ono, |
f24e9980 SW |
802 | u64 *oxoff, u64 *oxlen) |
803 | { | |
804 | u32 osize = le32_to_cpu(layout->fl_object_size); | |
805 | u32 su = le32_to_cpu(layout->fl_stripe_unit); | |
806 | u32 sc = le32_to_cpu(layout->fl_stripe_count); | |
807 | u32 bl, stripeno, stripepos, objsetno; | |
808 | u32 su_per_object; | |
ff1d1f71 | 809 | u64 t, su_offset; |
f24e9980 SW |
810 | |
811 | dout("mapping %llu~%llu osize %u fl_su %u\n", off, *plen, | |
812 | osize, su); | |
35e054a6 | 813 | su_per_object = osize / su; |
f24e9980 SW |
814 | dout("osize %u / su %u = su_per_object %u\n", osize, su, |
815 | su_per_object); | |
816 | ||
817 | BUG_ON((su & ~PAGE_MASK) != 0); | |
818 | /* bl = *off / su; */ | |
819 | t = off; | |
820 | do_div(t, su); | |
821 | bl = t; | |
822 | dout("off %llu / su %u = bl %u\n", off, su, bl); | |
823 | ||
824 | stripeno = bl / sc; | |
825 | stripepos = bl % sc; | |
826 | objsetno = stripeno / su_per_object; | |
827 | ||
645a1025 SW |
828 | *ono = objsetno * sc + stripepos; |
829 | dout("objset %u * sc %u = ono %u\n", objsetno, sc, (unsigned)*ono); | |
830 | ||
831 | /* *oxoff = *off % layout->fl_stripe_unit; # offset in su */ | |
f24e9980 | 832 | t = off; |
ff1d1f71 NW |
833 | su_offset = do_div(t, su); |
834 | *oxoff = su_offset + (stripeno % su_per_object) * su; | |
835 | ||
836 | /* | |
837 | * Calculate the length of the extent being written to the selected | |
838 | * object. This is the minimum of the full length requested (plen) or | |
839 | * the remainder of the current stripe being written to. | |
840 | */ | |
841 | *oxlen = min_t(u64, *plen, su - su_offset); | |
f24e9980 SW |
842 | *plen = *oxlen; |
843 | ||
844 | dout(" obj extent %llu~%llu\n", *oxoff, *oxlen); | |
845 | } | |
846 | ||
847 | /* | |
848 | * calculate an object layout (i.e. pgid) from an oid, | |
849 | * file_layout, and osdmap | |
850 | */ | |
851 | int ceph_calc_object_layout(struct ceph_object_layout *ol, | |
852 | const char *oid, | |
853 | struct ceph_file_layout *fl, | |
854 | struct ceph_osdmap *osdmap) | |
855 | { | |
856 | unsigned num, num_mask; | |
51042122 | 857 | struct ceph_pg pgid; |
f24e9980 SW |
858 | s32 preferred = (s32)le32_to_cpu(fl->fl_pg_preferred); |
859 | int poolid = le32_to_cpu(fl->fl_pg_pool); | |
860 | struct ceph_pg_pool_info *pool; | |
51042122 | 861 | unsigned ps; |
f24e9980 | 862 | |
30dc6381 | 863 | BUG_ON(!osdmap); |
f24e9980 SW |
864 | if (poolid >= osdmap->num_pools) |
865 | return -EIO; | |
f24e9980 | 866 | |
51042122 | 867 | pool = &osdmap->pg_pool[poolid]; |
1654dd0c | 868 | ps = ceph_str_hash(pool->v.object_hash, oid, strlen(oid)); |
f24e9980 | 869 | if (preferred >= 0) { |
51042122 | 870 | ps += preferred; |
f24e9980 SW |
871 | num = le32_to_cpu(pool->v.lpg_num); |
872 | num_mask = pool->lpg_num_mask; | |
873 | } else { | |
874 | num = le32_to_cpu(pool->v.pg_num); | |
875 | num_mask = pool->pg_num_mask; | |
876 | } | |
877 | ||
51042122 SW |
878 | pgid.ps = cpu_to_le16(ps); |
879 | pgid.preferred = cpu_to_le16(preferred); | |
880 | pgid.pool = fl->fl_pg_pool; | |
f24e9980 | 881 | if (preferred >= 0) |
51042122 SW |
882 | dout("calc_object_layout '%s' pgid %d.%xp%d\n", oid, poolid, ps, |
883 | (int)preferred); | |
f24e9980 | 884 | else |
51042122 | 885 | dout("calc_object_layout '%s' pgid %d.%x\n", oid, poolid, ps); |
f24e9980 | 886 | |
51042122 | 887 | ol->ol_pgid = pgid; |
f24e9980 | 888 | ol->ol_stripe_unit = fl->fl_object_stripe_unit; |
f24e9980 SW |
889 | return 0; |
890 | } | |
891 | ||
892 | /* | |
893 | * Calculate raw osd vector for the given pgid. Return pointer to osd | |
894 | * array, or NULL on failure. | |
895 | */ | |
51042122 | 896 | static int *calc_pg_raw(struct ceph_osdmap *osdmap, struct ceph_pg pgid, |
f24e9980 SW |
897 | int *osds, int *num) |
898 | { | |
f24e9980 SW |
899 | struct ceph_pg_mapping *pg; |
900 | struct ceph_pg_pool_info *pool; | |
901 | int ruleno; | |
51042122 SW |
902 | unsigned poolid, ps, pps; |
903 | int preferred; | |
f24e9980 SW |
904 | |
905 | /* pg_temp? */ | |
9794b146 SW |
906 | pg = __lookup_pg_mapping(&osdmap->pg_temp, pgid); |
907 | if (pg) { | |
908 | *num = pg->len; | |
909 | return pg->osds; | |
f24e9980 SW |
910 | } |
911 | ||
912 | /* crush */ | |
51042122 SW |
913 | poolid = le32_to_cpu(pgid.pool); |
914 | ps = le16_to_cpu(pgid.ps); | |
915 | preferred = (s16)le16_to_cpu(pgid.preferred); | |
916 | ||
767ea5c3 SW |
917 | /* don't forcefeed bad device ids to crush */ |
918 | if (preferred >= osdmap->max_osd || | |
919 | preferred >= osdmap->crush->max_devices) | |
920 | preferred = -1; | |
921 | ||
51042122 | 922 | if (poolid >= osdmap->num_pools) |
f24e9980 | 923 | return NULL; |
51042122 | 924 | pool = &osdmap->pg_pool[poolid]; |
f24e9980 SW |
925 | ruleno = crush_find_rule(osdmap->crush, pool->v.crush_ruleset, |
926 | pool->v.type, pool->v.size); | |
927 | if (ruleno < 0) { | |
928 | pr_err("no crush rule pool %d type %d size %d\n", | |
51042122 | 929 | poolid, pool->v.type, pool->v.size); |
f24e9980 SW |
930 | return NULL; |
931 | } | |
932 | ||
51042122 SW |
933 | if (preferred >= 0) |
934 | pps = ceph_stable_mod(ps, | |
f24e9980 SW |
935 | le32_to_cpu(pool->v.lpgp_num), |
936 | pool->lpgp_num_mask); | |
937 | else | |
51042122 | 938 | pps = ceph_stable_mod(ps, |
f24e9980 SW |
939 | le32_to_cpu(pool->v.pgp_num), |
940 | pool->pgp_num_mask); | |
51042122 | 941 | pps += poolid; |
f24e9980 SW |
942 | *num = crush_do_rule(osdmap->crush, ruleno, pps, osds, |
943 | min_t(int, pool->v.size, *num), | |
51042122 | 944 | preferred, osdmap->osd_weight); |
f24e9980 SW |
945 | return osds; |
946 | } | |
947 | ||
948 | /* | |
949 | * Return primary osd for given pgid, or -1 if none. | |
950 | */ | |
51042122 | 951 | int ceph_calc_pg_primary(struct ceph_osdmap *osdmap, struct ceph_pg pgid) |
f24e9980 SW |
952 | { |
953 | int rawosds[10], *osds; | |
954 | int i, num = ARRAY_SIZE(rawosds); | |
955 | ||
956 | osds = calc_pg_raw(osdmap, pgid, rawosds, &num); | |
957 | if (!osds) | |
958 | return -1; | |
959 | ||
960 | /* primary is first up osd */ | |
961 | for (i = 0; i < num; i++) | |
962 | if (ceph_osd_is_up(osdmap, osds[i])) { | |
963 | return osds[i]; | |
964 | break; | |
965 | } | |
966 | return -1; | |
967 | } |