blob: ca05871635bc16ff78203da0665d32369f12999e [file] [log] [blame]
Sage Weilf24e9982009-10-06 11:31:10 -07001
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002#include <linux/ceph/ceph_debug.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09003
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09005#include <linux/slab.h>
Sage Weilf24e9982009-10-06 11:31:10 -07006#include <asm/div64.h>
7
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07008#include <linux/ceph/libceph.h>
9#include <linux/ceph/osdmap.h>
10#include <linux/ceph/decode.h>
11#include <linux/crush/hash.h>
12#include <linux/crush/mapper.h>
Sage Weilf24e9982009-10-06 11:31:10 -070013
14char *ceph_osdmap_state_str(char *str, int len, int state)
15{
16 int flag = 0;
17
18 if (!len)
19 goto done;
20
21 *str = '\0';
22 if (state) {
23 if (state & CEPH_OSD_EXISTS) {
24 snprintf(str, len, "exists");
25 flag = 1;
26 }
27 if (state & CEPH_OSD_UP) {
28 snprintf(str, len, "%s%s%s", str, (flag ? ", " : ""),
29 "up");
30 flag = 1;
31 }
32 } else {
33 snprintf(str, len, "doesn't exist");
34 }
35done:
36 return str;
37}
38
39/* maps */
40
Eric Dumazet95c96172012-04-15 05:58:06 +000041static int calc_bits_of(unsigned int t)
Sage Weilf24e9982009-10-06 11:31:10 -070042{
43 int b = 0;
44 while (t) {
45 t = t >> 1;
46 b++;
47 }
48 return b;
49}
50
51/*
52 * the foo_mask is the smallest value 2^n-1 that is >= foo.
53 */
54static void calc_pg_masks(struct ceph_pg_pool_info *pi)
55{
56 pi->pg_num_mask = (1 << calc_bits_of(le32_to_cpu(pi->v.pg_num)-1)) - 1;
57 pi->pgp_num_mask =
58 (1 << calc_bits_of(le32_to_cpu(pi->v.pgp_num)-1)) - 1;
59 pi->lpg_num_mask =
60 (1 << calc_bits_of(le32_to_cpu(pi->v.lpg_num)-1)) - 1;
61 pi->lpgp_num_mask =
62 (1 << calc_bits_of(le32_to_cpu(pi->v.lpgp_num)-1)) - 1;
63}
64
65/*
66 * decode crush map
67 */
68static int crush_decode_uniform_bucket(void **p, void *end,
69 struct crush_bucket_uniform *b)
70{
71 dout("crush_decode_uniform_bucket %p to %p\n", *p, end);
72 ceph_decode_need(p, end, (1+b->h.size) * sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -070073 b->item_weight = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -070074 return 0;
75bad:
76 return -EINVAL;
77}
78
79static int crush_decode_list_bucket(void **p, void *end,
80 struct crush_bucket_list *b)
81{
82 int j;
83 dout("crush_decode_list_bucket %p to %p\n", *p, end);
84 b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
85 if (b->item_weights == NULL)
86 return -ENOMEM;
87 b->sum_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
88 if (b->sum_weights == NULL)
89 return -ENOMEM;
90 ceph_decode_need(p, end, 2 * b->h.size * sizeof(u32), bad);
91 for (j = 0; j < b->h.size; j++) {
Sage Weilc89136e2009-10-14 09:59:09 -070092 b->item_weights[j] = ceph_decode_32(p);
93 b->sum_weights[j] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -070094 }
95 return 0;
96bad:
97 return -EINVAL;
98}
99
100static int crush_decode_tree_bucket(void **p, void *end,
101 struct crush_bucket_tree *b)
102{
103 int j;
104 dout("crush_decode_tree_bucket %p to %p\n", *p, end);
105 ceph_decode_32_safe(p, end, b->num_nodes, bad);
106 b->node_weights = kcalloc(b->num_nodes, sizeof(u32), GFP_NOFS);
107 if (b->node_weights == NULL)
108 return -ENOMEM;
109 ceph_decode_need(p, end, b->num_nodes * sizeof(u32), bad);
110 for (j = 0; j < b->num_nodes; j++)
Sage Weilc89136e2009-10-14 09:59:09 -0700111 b->node_weights[j] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700112 return 0;
113bad:
114 return -EINVAL;
115}
116
117static int crush_decode_straw_bucket(void **p, void *end,
118 struct crush_bucket_straw *b)
119{
120 int j;
121 dout("crush_decode_straw_bucket %p to %p\n", *p, end);
122 b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
123 if (b->item_weights == NULL)
124 return -ENOMEM;
125 b->straws = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
126 if (b->straws == NULL)
127 return -ENOMEM;
128 ceph_decode_need(p, end, 2 * b->h.size * sizeof(u32), bad);
129 for (j = 0; j < b->h.size; j++) {
Sage Weilc89136e2009-10-14 09:59:09 -0700130 b->item_weights[j] = ceph_decode_32(p);
131 b->straws[j] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700132 }
133 return 0;
134bad:
135 return -EINVAL;
136}
137
Sage Weil546f04e2012-07-30 18:15:23 -0700138static int skip_name_map(void **p, void *end)
139{
140 int len;
141 ceph_decode_32_safe(p, end, len ,bad);
142 while (len--) {
143 int strlen;
144 *p += sizeof(u32);
145 ceph_decode_32_safe(p, end, strlen, bad);
146 *p += strlen;
147}
148 return 0;
149bad:
150 return -EINVAL;
151}
152
Sage Weilf24e9982009-10-06 11:31:10 -0700153static struct crush_map *crush_decode(void *pbyval, void *end)
154{
155 struct crush_map *c;
156 int err = -EINVAL;
157 int i, j;
158 void **p = &pbyval;
159 void *start = pbyval;
160 u32 magic;
Sage Weil546f04e2012-07-30 18:15:23 -0700161 u32 num_name_maps;
Sage Weilf24e9982009-10-06 11:31:10 -0700162
163 dout("crush_decode %p to %p len %d\n", *p, end, (int)(end - *p));
164
165 c = kzalloc(sizeof(*c), GFP_NOFS);
166 if (c == NULL)
167 return ERR_PTR(-ENOMEM);
168
Sage Weil546f04e2012-07-30 18:15:23 -0700169 /* set tunables to default values */
170 c->choose_local_tries = 2;
171 c->choose_local_fallback_tries = 5;
172 c->choose_total_tries = 19;
Jim Schutt1604f482012-11-30 09:15:25 -0700173 c->chooseleaf_descend_once = 0;
Sage Weil546f04e2012-07-30 18:15:23 -0700174
Sage Weilf24e9982009-10-06 11:31:10 -0700175 ceph_decode_need(p, end, 4*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700176 magic = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700177 if (magic != CRUSH_MAGIC) {
178 pr_err("crush_decode magic %x != current %x\n",
Eric Dumazet95c96172012-04-15 05:58:06 +0000179 (unsigned int)magic, (unsigned int)CRUSH_MAGIC);
Sage Weilf24e9982009-10-06 11:31:10 -0700180 goto bad;
181 }
Sage Weilc89136e2009-10-14 09:59:09 -0700182 c->max_buckets = ceph_decode_32(p);
183 c->max_rules = ceph_decode_32(p);
184 c->max_devices = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700185
Sage Weilf24e9982009-10-06 11:31:10 -0700186 c->buckets = kcalloc(c->max_buckets, sizeof(*c->buckets), GFP_NOFS);
187 if (c->buckets == NULL)
188 goto badmem;
189 c->rules = kcalloc(c->max_rules, sizeof(*c->rules), GFP_NOFS);
190 if (c->rules == NULL)
191 goto badmem;
192
193 /* buckets */
194 for (i = 0; i < c->max_buckets; i++) {
195 int size = 0;
196 u32 alg;
197 struct crush_bucket *b;
198
199 ceph_decode_32_safe(p, end, alg, bad);
200 if (alg == 0) {
201 c->buckets[i] = NULL;
202 continue;
203 }
204 dout("crush_decode bucket %d off %x %p to %p\n",
205 i, (int)(*p-start), *p, end);
206
207 switch (alg) {
208 case CRUSH_BUCKET_UNIFORM:
209 size = sizeof(struct crush_bucket_uniform);
210 break;
211 case CRUSH_BUCKET_LIST:
212 size = sizeof(struct crush_bucket_list);
213 break;
214 case CRUSH_BUCKET_TREE:
215 size = sizeof(struct crush_bucket_tree);
216 break;
217 case CRUSH_BUCKET_STRAW:
218 size = sizeof(struct crush_bucket_straw);
219 break;
220 default:
Sage Weil30dc6382009-12-21 14:49:37 -0800221 err = -EINVAL;
Sage Weilf24e9982009-10-06 11:31:10 -0700222 goto bad;
223 }
224 BUG_ON(size == 0);
225 b = c->buckets[i] = kzalloc(size, GFP_NOFS);
226 if (b == NULL)
227 goto badmem;
228
229 ceph_decode_need(p, end, 4*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700230 b->id = ceph_decode_32(p);
231 b->type = ceph_decode_16(p);
Sage Weilfb690392009-11-07 20:18:22 -0800232 b->alg = ceph_decode_8(p);
233 b->hash = ceph_decode_8(p);
Sage Weilc89136e2009-10-14 09:59:09 -0700234 b->weight = ceph_decode_32(p);
235 b->size = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700236
237 dout("crush_decode bucket size %d off %x %p to %p\n",
238 b->size, (int)(*p-start), *p, end);
239
240 b->items = kcalloc(b->size, sizeof(__s32), GFP_NOFS);
241 if (b->items == NULL)
242 goto badmem;
243 b->perm = kcalloc(b->size, sizeof(u32), GFP_NOFS);
244 if (b->perm == NULL)
245 goto badmem;
246 b->perm_n = 0;
247
248 ceph_decode_need(p, end, b->size*sizeof(u32), bad);
249 for (j = 0; j < b->size; j++)
Sage Weilc89136e2009-10-14 09:59:09 -0700250 b->items[j] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700251
252 switch (b->alg) {
253 case CRUSH_BUCKET_UNIFORM:
254 err = crush_decode_uniform_bucket(p, end,
255 (struct crush_bucket_uniform *)b);
256 if (err < 0)
257 goto bad;
258 break;
259 case CRUSH_BUCKET_LIST:
260 err = crush_decode_list_bucket(p, end,
261 (struct crush_bucket_list *)b);
262 if (err < 0)
263 goto bad;
264 break;
265 case CRUSH_BUCKET_TREE:
266 err = crush_decode_tree_bucket(p, end,
267 (struct crush_bucket_tree *)b);
268 if (err < 0)
269 goto bad;
270 break;
271 case CRUSH_BUCKET_STRAW:
272 err = crush_decode_straw_bucket(p, end,
273 (struct crush_bucket_straw *)b);
274 if (err < 0)
275 goto bad;
276 break;
277 }
278 }
279
280 /* rules */
281 dout("rule vec is %p\n", c->rules);
282 for (i = 0; i < c->max_rules; i++) {
283 u32 yes;
284 struct crush_rule *r;
285
286 ceph_decode_32_safe(p, end, yes, bad);
287 if (!yes) {
288 dout("crush_decode NO rule %d off %x %p to %p\n",
289 i, (int)(*p-start), *p, end);
290 c->rules[i] = NULL;
291 continue;
292 }
293
294 dout("crush_decode rule %d off %x %p to %p\n",
295 i, (int)(*p-start), *p, end);
296
297 /* len */
298 ceph_decode_32_safe(p, end, yes, bad);
299#if BITS_PER_LONG == 32
Sage Weil30dc6382009-12-21 14:49:37 -0800300 err = -EINVAL;
Xi Wang64486692012-02-16 11:55:48 -0500301 if (yes > (ULONG_MAX - sizeof(*r))
302 / sizeof(struct crush_rule_step))
Sage Weilf24e9982009-10-06 11:31:10 -0700303 goto bad;
304#endif
305 r = c->rules[i] = kmalloc(sizeof(*r) +
306 yes*sizeof(struct crush_rule_step),
307 GFP_NOFS);
308 if (r == NULL)
309 goto badmem;
310 dout(" rule %d is at %p\n", i, r);
311 r->len = yes;
312 ceph_decode_copy_safe(p, end, &r->mask, 4, bad); /* 4 u8's */
313 ceph_decode_need(p, end, r->len*3*sizeof(u32), bad);
314 for (j = 0; j < r->len; j++) {
Sage Weilc89136e2009-10-14 09:59:09 -0700315 r->steps[j].op = ceph_decode_32(p);
316 r->steps[j].arg1 = ceph_decode_32(p);
317 r->steps[j].arg2 = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700318 }
319 }
320
321 /* ignore trailing name maps. */
Sage Weil546f04e2012-07-30 18:15:23 -0700322 for (num_name_maps = 0; num_name_maps < 3; num_name_maps++) {
323 err = skip_name_map(p, end);
324 if (err < 0)
325 goto done;
326 }
Sage Weilf24e9982009-10-06 11:31:10 -0700327
Sage Weil546f04e2012-07-30 18:15:23 -0700328 /* tunables */
329 ceph_decode_need(p, end, 3*sizeof(u32), done);
330 c->choose_local_tries = ceph_decode_32(p);
331 c->choose_local_fallback_tries = ceph_decode_32(p);
332 c->choose_total_tries = ceph_decode_32(p);
333 dout("crush decode tunable choose_local_tries = %d",
334 c->choose_local_tries);
335 dout("crush decode tunable choose_local_fallback_tries = %d",
336 c->choose_local_fallback_tries);
337 dout("crush decode tunable choose_total_tries = %d",
338 c->choose_total_tries);
339
Jim Schutt1604f482012-11-30 09:15:25 -0700340 ceph_decode_need(p, end, sizeof(u32), done);
341 c->chooseleaf_descend_once = ceph_decode_32(p);
342 dout("crush decode tunable chooseleaf_descend_once = %d",
343 c->chooseleaf_descend_once);
344
Sage Weil546f04e2012-07-30 18:15:23 -0700345done:
Sage Weilf24e9982009-10-06 11:31:10 -0700346 dout("crush_decode success\n");
347 return c;
348
349badmem:
350 err = -ENOMEM;
351bad:
352 dout("crush_decode fail %d\n", err);
353 crush_destroy(c);
354 return ERR_PTR(err);
355}
356
Sage Weilf24e9982009-10-06 11:31:10 -0700357/*
Sage Weil9794b142010-02-16 15:53:32 -0800358 * rbtree of pg_mapping for handling pg_temp (explicit mapping of pgid
359 * to a set of osds)
Sage Weilf24e9982009-10-06 11:31:10 -0700360 */
Sage Weil51042122009-11-04 11:39:12 -0800361static int pgid_cmp(struct ceph_pg l, struct ceph_pg r)
362{
363 u64 a = *(u64 *)&l;
364 u64 b = *(u64 *)&r;
365
366 if (a < b)
367 return -1;
368 if (a > b)
369 return 1;
370 return 0;
371}
372
Sage Weil991abb62009-10-08 22:22:37 -0700373static int __insert_pg_mapping(struct ceph_pg_mapping *new,
374 struct rb_root *root)
Sage Weilf24e9982009-10-06 11:31:10 -0700375{
376 struct rb_node **p = &root->rb_node;
377 struct rb_node *parent = NULL;
378 struct ceph_pg_mapping *pg = NULL;
Sage Weil51042122009-11-04 11:39:12 -0800379 int c;
Sage Weilf24e9982009-10-06 11:31:10 -0700380
Sage Weil8adc8b32011-09-28 10:11:04 -0700381 dout("__insert_pg_mapping %llx %p\n", *(u64 *)&new->pgid, new);
Sage Weilf24e9982009-10-06 11:31:10 -0700382 while (*p) {
383 parent = *p;
384 pg = rb_entry(parent, struct ceph_pg_mapping, node);
Sage Weil51042122009-11-04 11:39:12 -0800385 c = pgid_cmp(new->pgid, pg->pgid);
386 if (c < 0)
Sage Weilf24e9982009-10-06 11:31:10 -0700387 p = &(*p)->rb_left;
Sage Weil51042122009-11-04 11:39:12 -0800388 else if (c > 0)
Sage Weilf24e9982009-10-06 11:31:10 -0700389 p = &(*p)->rb_right;
390 else
Sage Weil991abb62009-10-08 22:22:37 -0700391 return -EEXIST;
Sage Weilf24e9982009-10-06 11:31:10 -0700392 }
393
394 rb_link_node(&new->node, parent, p);
395 rb_insert_color(&new->node, root);
Sage Weil991abb62009-10-08 22:22:37 -0700396 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700397}
398
Sage Weil9794b142010-02-16 15:53:32 -0800399static struct ceph_pg_mapping *__lookup_pg_mapping(struct rb_root *root,
400 struct ceph_pg pgid)
401{
402 struct rb_node *n = root->rb_node;
403 struct ceph_pg_mapping *pg;
404 int c;
405
406 while (n) {
407 pg = rb_entry(n, struct ceph_pg_mapping, node);
408 c = pgid_cmp(pgid, pg->pgid);
Sage Weil8adc8b32011-09-28 10:11:04 -0700409 if (c < 0) {
Sage Weil9794b142010-02-16 15:53:32 -0800410 n = n->rb_left;
Sage Weil8adc8b32011-09-28 10:11:04 -0700411 } else if (c > 0) {
Sage Weil9794b142010-02-16 15:53:32 -0800412 n = n->rb_right;
Sage Weil8adc8b32011-09-28 10:11:04 -0700413 } else {
414 dout("__lookup_pg_mapping %llx got %p\n",
415 *(u64 *)&pgid, pg);
Sage Weil9794b142010-02-16 15:53:32 -0800416 return pg;
Sage Weil8adc8b32011-09-28 10:11:04 -0700417 }
Sage Weil9794b142010-02-16 15:53:32 -0800418 }
419 return NULL;
420}
421
Sage Weil8adc8b32011-09-28 10:11:04 -0700422static int __remove_pg_mapping(struct rb_root *root, struct ceph_pg pgid)
423{
424 struct ceph_pg_mapping *pg = __lookup_pg_mapping(root, pgid);
425
426 if (pg) {
427 dout("__remove_pg_mapping %llx %p\n", *(u64 *)&pgid, pg);
428 rb_erase(&pg->node, root);
429 kfree(pg);
430 return 0;
431 }
432 dout("__remove_pg_mapping %llx dne\n", *(u64 *)&pgid);
433 return -ENOENT;
434}
435
Sage Weilf24e9982009-10-06 11:31:10 -0700436/*
Sage Weil4fc51be2010-02-16 15:55:03 -0800437 * rbtree of pg pool info
438 */
439static int __insert_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *new)
440{
441 struct rb_node **p = &root->rb_node;
442 struct rb_node *parent = NULL;
443 struct ceph_pg_pool_info *pi = NULL;
444
445 while (*p) {
446 parent = *p;
447 pi = rb_entry(parent, struct ceph_pg_pool_info, node);
448 if (new->id < pi->id)
449 p = &(*p)->rb_left;
450 else if (new->id > pi->id)
451 p = &(*p)->rb_right;
452 else
453 return -EEXIST;
454 }
455
456 rb_link_node(&new->node, parent, p);
457 rb_insert_color(&new->node, root);
458 return 0;
459}
460
461static struct ceph_pg_pool_info *__lookup_pg_pool(struct rb_root *root, int id)
462{
463 struct ceph_pg_pool_info *pi;
464 struct rb_node *n = root->rb_node;
465
466 while (n) {
467 pi = rb_entry(n, struct ceph_pg_pool_info, node);
468 if (id < pi->id)
469 n = n->rb_left;
470 else if (id > pi->id)
471 n = n->rb_right;
472 else
473 return pi;
474 }
475 return NULL;
476}
477
Alex Elder72afc712012-10-30 19:40:33 -0500478const char *ceph_pg_pool_name_by_id(struct ceph_osdmap *map, u64 id)
479{
480 struct ceph_pg_pool_info *pi;
481
482 if (id == CEPH_NOPOOL)
483 return NULL;
484
485 if (WARN_ON_ONCE(id > (u64) INT_MAX))
486 return NULL;
487
488 pi = __lookup_pg_pool(&map->pg_pools, (int) id);
489
490 return pi ? pi->name : NULL;
491}
492EXPORT_SYMBOL(ceph_pg_pool_name_by_id);
493
Yehuda Sadeh7669a2c2010-05-17 12:31:35 -0700494int ceph_pg_poolid_by_name(struct ceph_osdmap *map, const char *name)
495{
496 struct rb_node *rbp;
497
498 for (rbp = rb_first(&map->pg_pools); rbp; rbp = rb_next(rbp)) {
499 struct ceph_pg_pool_info *pi =
500 rb_entry(rbp, struct ceph_pg_pool_info, node);
501 if (pi->name && strcmp(pi->name, name) == 0)
502 return pi->id;
503 }
504 return -ENOENT;
505}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700506EXPORT_SYMBOL(ceph_pg_poolid_by_name);
Yehuda Sadeh7669a2c2010-05-17 12:31:35 -0700507
Sage Weil2844a762010-04-09 15:46:42 -0700508static void __remove_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *pi)
509{
510 rb_erase(&pi->node, root);
511 kfree(pi->name);
512 kfree(pi);
513}
514
Sage Weil73a7e692010-08-02 11:00:55 -0700515static int __decode_pool(void **p, void *end, struct ceph_pg_pool_info *pi)
Sage Weilefd75762010-03-17 10:05:28 -0700516{
Eric Dumazet95c96172012-04-15 05:58:06 +0000517 unsigned int n, m;
Sage Weil73a7e692010-08-02 11:00:55 -0700518
Sage Weilefd75762010-03-17 10:05:28 -0700519 ceph_decode_copy(p, &pi->v, sizeof(pi->v));
520 calc_pg_masks(pi);
Sage Weil73a7e692010-08-02 11:00:55 -0700521
522 /* num_snaps * snap_info_t */
523 n = le32_to_cpu(pi->v.num_snaps);
524 while (n--) {
525 ceph_decode_need(p, end, sizeof(u64) + 1 + sizeof(u64) +
526 sizeof(struct ceph_timespec), bad);
527 *p += sizeof(u64) + /* key */
528 1 + sizeof(u64) + /* u8, snapid */
529 sizeof(struct ceph_timespec);
530 m = ceph_decode_32(p); /* snap name */
531 *p += m;
532 }
533
Sage Weilefd75762010-03-17 10:05:28 -0700534 *p += le32_to_cpu(pi->v.num_removed_snap_intervals) * sizeof(u64) * 2;
Sage Weil73a7e692010-08-02 11:00:55 -0700535 return 0;
536
537bad:
538 return -EINVAL;
Sage Weilefd75762010-03-17 10:05:28 -0700539}
540
Sage Weil2844a762010-04-09 15:46:42 -0700541static int __decode_pool_names(void **p, void *end, struct ceph_osdmap *map)
542{
543 struct ceph_pg_pool_info *pi;
544 u32 num, len, pool;
545
546 ceph_decode_32_safe(p, end, num, bad);
547 dout(" %d pool names\n", num);
548 while (num--) {
549 ceph_decode_32_safe(p, end, pool, bad);
550 ceph_decode_32_safe(p, end, len, bad);
551 dout(" pool %d len %d\n", pool, len);
Xi Wangad3b9042012-06-06 19:35:55 -0500552 ceph_decode_need(p, end, len, bad);
Sage Weil2844a762010-04-09 15:46:42 -0700553 pi = __lookup_pg_pool(&map->pg_pools, pool);
554 if (pi) {
Xi Wangad3b9042012-06-06 19:35:55 -0500555 char *name = kstrndup(*p, len, GFP_NOFS);
556
557 if (!name)
558 return -ENOMEM;
Sage Weil2844a762010-04-09 15:46:42 -0700559 kfree(pi->name);
Xi Wangad3b9042012-06-06 19:35:55 -0500560 pi->name = name;
561 dout(" name is %s\n", pi->name);
Sage Weil2844a762010-04-09 15:46:42 -0700562 }
563 *p += len;
564 }
565 return 0;
566
567bad:
568 return -EINVAL;
569}
570
571/*
572 * osd map
573 */
574void ceph_osdmap_destroy(struct ceph_osdmap *map)
575{
576 dout("osdmap_destroy %p\n", map);
577 if (map->crush)
578 crush_destroy(map->crush);
579 while (!RB_EMPTY_ROOT(&map->pg_temp)) {
580 struct ceph_pg_mapping *pg =
581 rb_entry(rb_first(&map->pg_temp),
582 struct ceph_pg_mapping, node);
583 rb_erase(&pg->node, &map->pg_temp);
584 kfree(pg);
585 }
586 while (!RB_EMPTY_ROOT(&map->pg_pools)) {
587 struct ceph_pg_pool_info *pi =
588 rb_entry(rb_first(&map->pg_pools),
589 struct ceph_pg_pool_info, node);
590 __remove_pg_pool(&map->pg_pools, pi);
591 }
592 kfree(map->osd_state);
593 kfree(map->osd_weight);
594 kfree(map->osd_addr);
595 kfree(map);
596}
597
598/*
599 * adjust max osd value. reallocate arrays.
600 */
601static int osdmap_set_max_osd(struct ceph_osdmap *map, int max)
602{
603 u8 *state;
604 struct ceph_entity_addr *addr;
605 u32 *weight;
606
607 state = kcalloc(max, sizeof(*state), GFP_NOFS);
608 addr = kcalloc(max, sizeof(*addr), GFP_NOFS);
609 weight = kcalloc(max, sizeof(*weight), GFP_NOFS);
610 if (state == NULL || addr == NULL || weight == NULL) {
611 kfree(state);
612 kfree(addr);
613 kfree(weight);
614 return -ENOMEM;
615 }
616
617 /* copy old? */
618 if (map->osd_state) {
619 memcpy(state, map->osd_state, map->max_osd*sizeof(*state));
620 memcpy(addr, map->osd_addr, map->max_osd*sizeof(*addr));
621 memcpy(weight, map->osd_weight, map->max_osd*sizeof(*weight));
622 kfree(map->osd_state);
623 kfree(map->osd_addr);
624 kfree(map->osd_weight);
625 }
626
627 map->osd_state = state;
628 map->osd_weight = weight;
629 map->osd_addr = addr;
630 map->max_osd = max;
631 return 0;
632}
633
Sage Weil4fc51be2010-02-16 15:55:03 -0800634/*
Sage Weilf24e9982009-10-06 11:31:10 -0700635 * decode a full map.
636 */
637struct ceph_osdmap *osdmap_decode(void **p, void *end)
638{
639 struct ceph_osdmap *map;
640 u16 version;
641 u32 len, max, i;
Sage Weil361be862010-01-25 16:03:02 -0800642 u8 ev;
Sage Weilf24e9982009-10-06 11:31:10 -0700643 int err = -EINVAL;
644 void *start = *p;
Sage Weil4fc51be2010-02-16 15:55:03 -0800645 struct ceph_pg_pool_info *pi;
Sage Weilf24e9982009-10-06 11:31:10 -0700646
647 dout("osdmap_decode %p to %p len %d\n", *p, end, (int)(end - *p));
648
649 map = kzalloc(sizeof(*map), GFP_NOFS);
650 if (map == NULL)
651 return ERR_PTR(-ENOMEM);
652 map->pg_temp = RB_ROOT;
653
654 ceph_decode_16_safe(p, end, version, bad);
Sage Weil02f90c62010-02-04 16:18:10 -0800655 if (version > CEPH_OSDMAP_VERSION) {
656 pr_warning("got unknown v %d > %d of osdmap\n", version,
657 CEPH_OSDMAP_VERSION);
658 goto bad;
659 }
Sage Weilf24e9982009-10-06 11:31:10 -0700660
661 ceph_decode_need(p, end, 2*sizeof(u64)+6*sizeof(u32), bad);
662 ceph_decode_copy(p, &map->fsid, sizeof(map->fsid));
Sage Weilc89136e2009-10-14 09:59:09 -0700663 map->epoch = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700664 ceph_decode_copy(p, &map->created, sizeof(map->created));
665 ceph_decode_copy(p, &map->modified, sizeof(map->modified));
666
Sage Weilf24e9982009-10-06 11:31:10 -0700667 ceph_decode_32_safe(p, end, max, bad);
668 while (max--) {
Sage Weil4fc51be2010-02-16 15:55:03 -0800669 ceph_decode_need(p, end, 4 + 1 + sizeof(pi->v), bad);
Sage Weil0ed72852012-10-29 11:01:42 -0700670 err = -ENOMEM;
Sage Weil2844a762010-04-09 15:46:42 -0700671 pi = kzalloc(sizeof(*pi), GFP_NOFS);
Sage Weil4fc51be2010-02-16 15:55:03 -0800672 if (!pi)
Sage Weilf24e9982009-10-06 11:31:10 -0700673 goto bad;
Sage Weil4fc51be2010-02-16 15:55:03 -0800674 pi->id = ceph_decode_32(p);
Sage Weil0ed72852012-10-29 11:01:42 -0700675 err = -EINVAL;
Sage Weil361be862010-01-25 16:03:02 -0800676 ev = ceph_decode_8(p); /* encoding version */
Sage Weil02f90c62010-02-04 16:18:10 -0800677 if (ev > CEPH_PG_POOL_VERSION) {
678 pr_warning("got unknown v %d > %d of ceph_pg_pool\n",
679 ev, CEPH_PG_POOL_VERSION);
Dan Carpenterb0bbb0b2010-07-08 14:49:38 +0200680 kfree(pi);
Sage Weil02f90c62010-02-04 16:18:10 -0800681 goto bad;
682 }
Sage Weil73a7e692010-08-02 11:00:55 -0700683 err = __decode_pool(p, end, pi);
Jesper Juhlb0aee352010-12-24 23:01:12 +0100684 if (err < 0) {
685 kfree(pi);
Sage Weil73a7e692010-08-02 11:00:55 -0700686 goto bad;
Jesper Juhlb0aee352010-12-24 23:01:12 +0100687 }
Sage Weil4fc51be2010-02-16 15:55:03 -0800688 __insert_pg_pool(&map->pg_pools, pi);
Sage Weilf24e9982009-10-06 11:31:10 -0700689 }
Sage Weil2844a762010-04-09 15:46:42 -0700690
Sage Weil0ed72852012-10-29 11:01:42 -0700691 if (version >= 5) {
692 err = __decode_pool_names(p, end, map);
693 if (err < 0) {
694 dout("fail to decode pool names");
695 goto bad;
696 }
697 }
Sage Weil2844a762010-04-09 15:46:42 -0700698
Sage Weil4fc51be2010-02-16 15:55:03 -0800699 ceph_decode_32_safe(p, end, map->pool_max, bad);
Sage Weilf24e9982009-10-06 11:31:10 -0700700
701 ceph_decode_32_safe(p, end, map->flags, bad);
702
Sage Weilc89136e2009-10-14 09:59:09 -0700703 max = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700704
705 /* (re)alloc osd arrays */
706 err = osdmap_set_max_osd(map, max);
707 if (err < 0)
708 goto bad;
709 dout("osdmap_decode max_osd = %d\n", map->max_osd);
710
711 /* osds */
712 err = -EINVAL;
713 ceph_decode_need(p, end, 3*sizeof(u32) +
714 map->max_osd*(1 + sizeof(*map->osd_weight) +
715 sizeof(*map->osd_addr)), bad);
716 *p += 4; /* skip length field (should match max) */
717 ceph_decode_copy(p, map->osd_state, map->max_osd);
718
719 *p += 4; /* skip length field (should match max) */
720 for (i = 0; i < map->max_osd; i++)
Sage Weilc89136e2009-10-14 09:59:09 -0700721 map->osd_weight[i] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700722
723 *p += 4; /* skip length field (should match max) */
724 ceph_decode_copy(p, map->osd_addr, map->max_osd*sizeof(*map->osd_addr));
Sage Weil63f2d212009-11-03 15:17:56 -0800725 for (i = 0; i < map->max_osd; i++)
726 ceph_decode_addr(&map->osd_addr[i]);
Sage Weilf24e9982009-10-06 11:31:10 -0700727
728 /* pg_temp */
729 ceph_decode_32_safe(p, end, len, bad);
730 for (i = 0; i < len; i++) {
731 int n, j;
Sage Weil51042122009-11-04 11:39:12 -0800732 struct ceph_pg pgid;
Sage Weilf24e9982009-10-06 11:31:10 -0700733 struct ceph_pg_mapping *pg;
734
735 ceph_decode_need(p, end, sizeof(u32) + sizeof(u64), bad);
Sage Weil51042122009-11-04 11:39:12 -0800736 ceph_decode_copy(p, &pgid, sizeof(pgid));
Sage Weilc89136e2009-10-14 09:59:09 -0700737 n = ceph_decode_32(p);
Xi Wange91a9b62012-06-06 19:35:55 -0500738 err = -EINVAL;
739 if (n > (UINT_MAX - sizeof(*pg)) / sizeof(u32))
740 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -0700741 ceph_decode_need(p, end, n * sizeof(u32), bad);
Sage Weil30dc6382009-12-21 14:49:37 -0800742 err = -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -0700743 pg = kmalloc(sizeof(*pg) + n*sizeof(u32), GFP_NOFS);
Sage Weil30dc6382009-12-21 14:49:37 -0800744 if (!pg)
Sage Weilf24e9982009-10-06 11:31:10 -0700745 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -0700746 pg->pgid = pgid;
747 pg->len = n;
748 for (j = 0; j < n; j++)
Sage Weilc89136e2009-10-14 09:59:09 -0700749 pg->osds[j] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700750
Sage Weil991abb62009-10-08 22:22:37 -0700751 err = __insert_pg_mapping(pg, &map->pg_temp);
752 if (err)
753 goto bad;
Sage Weil51042122009-11-04 11:39:12 -0800754 dout(" added pg_temp %llx len %d\n", *(u64 *)&pgid, len);
Sage Weilf24e9982009-10-06 11:31:10 -0700755 }
756
757 /* crush */
758 ceph_decode_32_safe(p, end, len, bad);
759 dout("osdmap_decode crush len %d from off 0x%x\n", len,
760 (int)(*p - start));
761 ceph_decode_need(p, end, len, bad);
762 map->crush = crush_decode(*p, end);
763 *p += len;
764 if (IS_ERR(map->crush)) {
765 err = PTR_ERR(map->crush);
766 map->crush = NULL;
767 goto bad;
768 }
769
770 /* ignore the rest of the map */
771 *p = end;
772
773 dout("osdmap_decode done %p %p\n", *p, end);
774 return map;
775
776bad:
Sage Weil0ed72852012-10-29 11:01:42 -0700777 dout("osdmap_decode fail err %d\n", err);
Sage Weilf24e9982009-10-06 11:31:10 -0700778 ceph_osdmap_destroy(map);
779 return ERR_PTR(err);
780}
781
782/*
783 * decode and apply an incremental map update.
784 */
785struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end,
786 struct ceph_osdmap *map,
787 struct ceph_messenger *msgr)
788{
Sage Weilf24e9982009-10-06 11:31:10 -0700789 struct crush_map *newcrush = NULL;
790 struct ceph_fsid fsid;
791 u32 epoch = 0;
792 struct ceph_timespec modified;
793 u32 len, pool;
Sage Weil4fc51be2010-02-16 15:55:03 -0800794 __s32 new_pool_max, new_flags, max;
Sage Weilf24e9982009-10-06 11:31:10 -0700795 void *start = *p;
796 int err = -EINVAL;
797 u16 version;
Sage Weilf24e9982009-10-06 11:31:10 -0700798
799 ceph_decode_16_safe(p, end, version, bad);
Sage Weil02f90c62010-02-04 16:18:10 -0800800 if (version > CEPH_OSDMAP_INC_VERSION) {
801 pr_warning("got unknown v %d > %d of inc osdmap\n", version,
802 CEPH_OSDMAP_INC_VERSION);
803 goto bad;
804 }
Sage Weilf24e9982009-10-06 11:31:10 -0700805
806 ceph_decode_need(p, end, sizeof(fsid)+sizeof(modified)+2*sizeof(u32),
807 bad);
808 ceph_decode_copy(p, &fsid, sizeof(fsid));
Sage Weilc89136e2009-10-14 09:59:09 -0700809 epoch = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700810 BUG_ON(epoch != map->epoch+1);
811 ceph_decode_copy(p, &modified, sizeof(modified));
Sage Weil4fc51be2010-02-16 15:55:03 -0800812 new_pool_max = ceph_decode_32(p);
Sage Weilc89136e2009-10-14 09:59:09 -0700813 new_flags = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700814
815 /* full map? */
816 ceph_decode_32_safe(p, end, len, bad);
817 if (len > 0) {
818 dout("apply_incremental full map len %d, %p to %p\n",
819 len, *p, end);
Sage Weil30dc6382009-12-21 14:49:37 -0800820 return osdmap_decode(p, min(*p+len, end));
Sage Weilf24e9982009-10-06 11:31:10 -0700821 }
822
823 /* new crush? */
824 ceph_decode_32_safe(p, end, len, bad);
825 if (len > 0) {
826 dout("apply_incremental new crush map len %d, %p to %p\n",
827 len, *p, end);
828 newcrush = crush_decode(*p, min(*p+len, end));
829 if (IS_ERR(newcrush))
Julia Lawall7e34bc52010-05-22 12:01:14 +0200830 return ERR_CAST(newcrush);
Sage Weilcebc5be2010-06-17 10:22:48 -0700831 *p += len;
Sage Weilf24e9982009-10-06 11:31:10 -0700832 }
833
834 /* new flags? */
835 if (new_flags >= 0)
836 map->flags = new_flags;
Sage Weil4fc51be2010-02-16 15:55:03 -0800837 if (new_pool_max >= 0)
838 map->pool_max = new_pool_max;
Sage Weilf24e9982009-10-06 11:31:10 -0700839
840 ceph_decode_need(p, end, 5*sizeof(u32), bad);
841
842 /* new max? */
Sage Weilc89136e2009-10-14 09:59:09 -0700843 max = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700844 if (max >= 0) {
845 err = osdmap_set_max_osd(map, max);
846 if (err < 0)
847 goto bad;
848 }
849
850 map->epoch++;
Sage Weil31456662011-05-12 15:18:43 -0700851 map->modified = modified;
Sage Weilf24e9982009-10-06 11:31:10 -0700852 if (newcrush) {
853 if (map->crush)
854 crush_destroy(map->crush);
855 map->crush = newcrush;
856 newcrush = NULL;
857 }
858
859 /* new_pool */
860 ceph_decode_32_safe(p, end, len, bad);
861 while (len--) {
Sage Weil361be862010-01-25 16:03:02 -0800862 __u8 ev;
Sage Weil4fc51be2010-02-16 15:55:03 -0800863 struct ceph_pg_pool_info *pi;
Sage Weil361be862010-01-25 16:03:02 -0800864
Sage Weilf24e9982009-10-06 11:31:10 -0700865 ceph_decode_32_safe(p, end, pool, bad);
Sage Weil4fc51be2010-02-16 15:55:03 -0800866 ceph_decode_need(p, end, 1 + sizeof(pi->v), bad);
Sage Weil361be862010-01-25 16:03:02 -0800867 ev = ceph_decode_8(p); /* encoding version */
Sage Weil02f90c62010-02-04 16:18:10 -0800868 if (ev > CEPH_PG_POOL_VERSION) {
869 pr_warning("got unknown v %d > %d of ceph_pg_pool\n",
870 ev, CEPH_PG_POOL_VERSION);
Sage Weil0ed72852012-10-29 11:01:42 -0700871 err = -EINVAL;
Sage Weil02f90c62010-02-04 16:18:10 -0800872 goto bad;
873 }
Sage Weil4fc51be2010-02-16 15:55:03 -0800874 pi = __lookup_pg_pool(&map->pg_pools, pool);
875 if (!pi) {
Sage Weil2844a762010-04-09 15:46:42 -0700876 pi = kzalloc(sizeof(*pi), GFP_NOFS);
Sage Weil4fc51be2010-02-16 15:55:03 -0800877 if (!pi) {
878 err = -ENOMEM;
879 goto bad;
880 }
881 pi->id = pool;
882 __insert_pg_pool(&map->pg_pools, pi);
883 }
Sage Weil73a7e692010-08-02 11:00:55 -0700884 err = __decode_pool(p, end, pi);
885 if (err < 0)
886 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -0700887 }
Sage Weil0ed72852012-10-29 11:01:42 -0700888 if (version >= 5) {
889 err = __decode_pool_names(p, end, map);
890 if (err < 0)
891 goto bad;
892 }
Sage Weilf24e9982009-10-06 11:31:10 -0700893
Sage Weil4fc51be2010-02-16 15:55:03 -0800894 /* old_pool */
Sage Weilf24e9982009-10-06 11:31:10 -0700895 ceph_decode_32_safe(p, end, len, bad);
Sage Weil4fc51be2010-02-16 15:55:03 -0800896 while (len--) {
897 struct ceph_pg_pool_info *pi;
898
899 ceph_decode_32_safe(p, end, pool, bad);
900 pi = __lookup_pg_pool(&map->pg_pools, pool);
Sage Weil2844a762010-04-09 15:46:42 -0700901 if (pi)
902 __remove_pg_pool(&map->pg_pools, pi);
Sage Weil4fc51be2010-02-16 15:55:03 -0800903 }
Sage Weilf24e9982009-10-06 11:31:10 -0700904
905 /* new_up */
906 err = -EINVAL;
907 ceph_decode_32_safe(p, end, len, bad);
908 while (len--) {
909 u32 osd;
910 struct ceph_entity_addr addr;
911 ceph_decode_32_safe(p, end, osd, bad);
912 ceph_decode_copy_safe(p, end, &addr, sizeof(addr), bad);
Sage Weil63f2d212009-11-03 15:17:56 -0800913 ceph_decode_addr(&addr);
Sage Weilf24e9982009-10-06 11:31:10 -0700914 pr_info("osd%d up\n", osd);
915 BUG_ON(osd >= map->max_osd);
916 map->osd_state[osd] |= CEPH_OSD_UP;
917 map->osd_addr[osd] = addr;
918 }
919
Sage Weil7662d8f2011-05-03 12:52:05 -0700920 /* new_state */
Sage Weilf24e9982009-10-06 11:31:10 -0700921 ceph_decode_32_safe(p, end, len, bad);
922 while (len--) {
923 u32 osd;
Sage Weil7662d8f2011-05-03 12:52:05 -0700924 u8 xorstate;
Sage Weilf24e9982009-10-06 11:31:10 -0700925 ceph_decode_32_safe(p, end, osd, bad);
Sage Weil7662d8f2011-05-03 12:52:05 -0700926 xorstate = **(u8 **)p;
Sage Weilf24e9982009-10-06 11:31:10 -0700927 (*p)++; /* clean flag */
Sage Weil7662d8f2011-05-03 12:52:05 -0700928 if (xorstate == 0)
929 xorstate = CEPH_OSD_UP;
930 if (xorstate & CEPH_OSD_UP)
931 pr_info("osd%d down\n", osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700932 if (osd < map->max_osd)
Sage Weil7662d8f2011-05-03 12:52:05 -0700933 map->osd_state[osd] ^= xorstate;
Sage Weilf24e9982009-10-06 11:31:10 -0700934 }
935
936 /* new_weight */
937 ceph_decode_32_safe(p, end, len, bad);
938 while (len--) {
939 u32 osd, off;
940 ceph_decode_need(p, end, sizeof(u32)*2, bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700941 osd = ceph_decode_32(p);
942 off = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700943 pr_info("osd%d weight 0x%x %s\n", osd, off,
944 off == CEPH_OSD_IN ? "(in)" :
945 (off == CEPH_OSD_OUT ? "(out)" : ""));
946 if (osd < map->max_osd)
947 map->osd_weight[osd] = off;
948 }
949
950 /* new_pg_temp */
Sage Weilf24e9982009-10-06 11:31:10 -0700951 ceph_decode_32_safe(p, end, len, bad);
952 while (len--) {
953 struct ceph_pg_mapping *pg;
954 int j;
Sage Weil51042122009-11-04 11:39:12 -0800955 struct ceph_pg pgid;
Sage Weilf24e9982009-10-06 11:31:10 -0700956 u32 pglen;
957 ceph_decode_need(p, end, sizeof(u64) + sizeof(u32), bad);
Sage Weil51042122009-11-04 11:39:12 -0800958 ceph_decode_copy(p, &pgid, sizeof(pgid));
Sage Weilc89136e2009-10-14 09:59:09 -0700959 pglen = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700960
Sage Weilf24e9982009-10-06 11:31:10 -0700961 if (pglen) {
Sage Weilf24e9982009-10-06 11:31:10 -0700962 ceph_decode_need(p, end, pglen*sizeof(u32), bad);
Sage Weil6bd9adb2012-05-21 09:45:23 -0700963
964 /* removing existing (if any) */
965 (void) __remove_pg_mapping(&map->pg_temp, pgid);
966
967 /* insert */
Sage Weil0ed72852012-10-29 11:01:42 -0700968 err = -EINVAL;
969 if (pglen > (UINT_MAX - sizeof(*pg)) / sizeof(u32))
Xi Wanga55060492012-06-06 19:35:55 -0500970 goto bad;
Sage Weil0ed72852012-10-29 11:01:42 -0700971 err = -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -0700972 pg = kmalloc(sizeof(*pg) + sizeof(u32)*pglen, GFP_NOFS);
Sage Weil0ed72852012-10-29 11:01:42 -0700973 if (!pg)
Sage Weilf24e9982009-10-06 11:31:10 -0700974 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -0700975 pg->pgid = pgid;
976 pg->len = pglen;
Sage Weil7067f792009-12-21 16:02:37 -0800977 for (j = 0; j < pglen; j++)
Sage Weilc89136e2009-10-14 09:59:09 -0700978 pg->osds[j] = ceph_decode_32(p);
Sage Weil991abb62009-10-08 22:22:37 -0700979 err = __insert_pg_mapping(pg, &map->pg_temp);
Sage Weilbc4fdca2010-07-20 16:19:56 -0700980 if (err) {
981 kfree(pg);
Sage Weil991abb62009-10-08 22:22:37 -0700982 goto bad;
Sage Weilbc4fdca2010-07-20 16:19:56 -0700983 }
Sage Weil51042122009-11-04 11:39:12 -0800984 dout(" added pg_temp %llx len %d\n", *(u64 *)&pgid,
985 pglen);
Sage Weil8adc8b32011-09-28 10:11:04 -0700986 } else {
987 /* remove */
988 __remove_pg_mapping(&map->pg_temp, pgid);
Sage Weilf24e9982009-10-06 11:31:10 -0700989 }
990 }
Sage Weilf24e9982009-10-06 11:31:10 -0700991
992 /* ignore the rest */
993 *p = end;
994 return map;
995
996bad:
997 pr_err("corrupt inc osdmap epoch %d off %d (%p of %p-%p)\n",
998 epoch, (int)(*p - start), *p, start, end);
Sage Weil9ec7cab2009-12-14 15:13:47 -0800999 print_hex_dump(KERN_DEBUG, "osdmap: ",
1000 DUMP_PREFIX_OFFSET, 16, 1,
1001 start, end - start, true);
Sage Weilf24e9982009-10-06 11:31:10 -07001002 if (newcrush)
1003 crush_destroy(newcrush);
1004 return ERR_PTR(err);
1005}
1006
1007
1008
1009
1010/*
1011 * calculate file layout from given offset, length.
1012 * fill in correct oid, logical length, and object extent
1013 * offset, length.
1014 *
1015 * for now, we write only a single su, until we can
1016 * pass a stride back to the caller.
1017 */
Sage Weild63b77f2012-09-24 20:59:48 -07001018int ceph_calc_file_object_mapping(struct ceph_file_layout *layout,
Sage Weilf24e9982009-10-06 11:31:10 -07001019 u64 off, u64 *plen,
Sage Weil645a1022009-10-28 15:15:05 -07001020 u64 *ono,
Sage Weilf24e9982009-10-06 11:31:10 -07001021 u64 *oxoff, u64 *oxlen)
1022{
1023 u32 osize = le32_to_cpu(layout->fl_object_size);
1024 u32 su = le32_to_cpu(layout->fl_stripe_unit);
1025 u32 sc = le32_to_cpu(layout->fl_stripe_count);
1026 u32 bl, stripeno, stripepos, objsetno;
1027 u32 su_per_object;
Noah Watkinsff1d1f72009-10-30 12:57:30 -07001028 u64 t, su_offset;
Sage Weilf24e9982009-10-06 11:31:10 -07001029
1030 dout("mapping %llu~%llu osize %u fl_su %u\n", off, *plen,
1031 osize, su);
Sage Weild63b77f2012-09-24 20:59:48 -07001032 if (su == 0 || sc == 0)
1033 goto invalid;
Noah Watkins35e054a2009-10-28 14:04:48 -07001034 su_per_object = osize / su;
Sage Weild63b77f2012-09-24 20:59:48 -07001035 if (su_per_object == 0)
1036 goto invalid;
Sage Weilf24e9982009-10-06 11:31:10 -07001037 dout("osize %u / su %u = su_per_object %u\n", osize, su,
1038 su_per_object);
1039
Sage Weild63b77f2012-09-24 20:59:48 -07001040 if ((su & ~PAGE_MASK) != 0)
1041 goto invalid;
1042
Sage Weilf24e9982009-10-06 11:31:10 -07001043 /* bl = *off / su; */
1044 t = off;
1045 do_div(t, su);
1046 bl = t;
1047 dout("off %llu / su %u = bl %u\n", off, su, bl);
1048
1049 stripeno = bl / sc;
1050 stripepos = bl % sc;
1051 objsetno = stripeno / su_per_object;
1052
Sage Weil645a1022009-10-28 15:15:05 -07001053 *ono = objsetno * sc + stripepos;
Eric Dumazet95c96172012-04-15 05:58:06 +00001054 dout("objset %u * sc %u = ono %u\n", objsetno, sc, (unsigned int)*ono);
Sage Weil645a1022009-10-28 15:15:05 -07001055
1056 /* *oxoff = *off % layout->fl_stripe_unit; # offset in su */
Sage Weilf24e9982009-10-06 11:31:10 -07001057 t = off;
Noah Watkinsff1d1f72009-10-30 12:57:30 -07001058 su_offset = do_div(t, su);
1059 *oxoff = su_offset + (stripeno % su_per_object) * su;
Sage Weil645a1022009-10-28 15:15:05 -07001060
Noah Watkinsff1d1f72009-10-30 12:57:30 -07001061 /*
1062 * Calculate the length of the extent being written to the selected
1063 * object. This is the minimum of the full length requested (plen) or
1064 * the remainder of the current stripe being written to.
1065 */
1066 *oxlen = min_t(u64, *plen, su - su_offset);
Sage Weilf24e9982009-10-06 11:31:10 -07001067 *plen = *oxlen;
1068
1069 dout(" obj extent %llu~%llu\n", *oxoff, *oxlen);
Sage Weild63b77f2012-09-24 20:59:48 -07001070 return 0;
1071
1072invalid:
1073 dout(" invalid layout\n");
1074 *ono = 0;
1075 *oxoff = 0;
1076 *oxlen = 0;
1077 return -EINVAL;
Sage Weilf24e9982009-10-06 11:31:10 -07001078}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001079EXPORT_SYMBOL(ceph_calc_file_object_mapping);
Sage Weilf24e9982009-10-06 11:31:10 -07001080
1081/*
1082 * calculate an object layout (i.e. pgid) from an oid,
1083 * file_layout, and osdmap
1084 */
1085int ceph_calc_object_layout(struct ceph_object_layout *ol,
1086 const char *oid,
1087 struct ceph_file_layout *fl,
1088 struct ceph_osdmap *osdmap)
1089{
Eric Dumazet95c96172012-04-15 05:58:06 +00001090 unsigned int num, num_mask;
Sage Weil51042122009-11-04 11:39:12 -08001091 struct ceph_pg pgid;
Sage Weilf24e9982009-10-06 11:31:10 -07001092 int poolid = le32_to_cpu(fl->fl_pg_pool);
1093 struct ceph_pg_pool_info *pool;
Eric Dumazet95c96172012-04-15 05:58:06 +00001094 unsigned int ps;
Sage Weilf24e9982009-10-06 11:31:10 -07001095
Sage Weil30dc6382009-12-21 14:49:37 -08001096 BUG_ON(!osdmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001097
Sage Weil4fc51be2010-02-16 15:55:03 -08001098 pool = __lookup_pg_pool(&osdmap->pg_pools, poolid);
1099 if (!pool)
1100 return -EIO;
Sage Weil1654dd02009-11-06 21:55:25 -08001101 ps = ceph_str_hash(pool->v.object_hash, oid, strlen(oid));
Sage Weil3469ac1a2012-05-07 15:33:36 -07001102 num = le32_to_cpu(pool->v.pg_num);
1103 num_mask = pool->pg_num_mask;
Sage Weilf24e9982009-10-06 11:31:10 -07001104
Sage Weil51042122009-11-04 11:39:12 -08001105 pgid.ps = cpu_to_le16(ps);
Sage Weil3469ac1a2012-05-07 15:33:36 -07001106 pgid.preferred = cpu_to_le16(-1);
Sage Weil51042122009-11-04 11:39:12 -08001107 pgid.pool = fl->fl_pg_pool;
Sage Weil3469ac1a2012-05-07 15:33:36 -07001108 dout("calc_object_layout '%s' pgid %d.%x\n", oid, poolid, ps);
Sage Weilf24e9982009-10-06 11:31:10 -07001109
Sage Weil51042122009-11-04 11:39:12 -08001110 ol->ol_pgid = pgid;
Sage Weilf24e9982009-10-06 11:31:10 -07001111 ol->ol_stripe_unit = fl->fl_object_stripe_unit;
Sage Weilf24e9982009-10-06 11:31:10 -07001112 return 0;
1113}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001114EXPORT_SYMBOL(ceph_calc_object_layout);
Sage Weilf24e9982009-10-06 11:31:10 -07001115
1116/*
1117 * Calculate raw osd vector for the given pgid. Return pointer to osd
1118 * array, or NULL on failure.
1119 */
Sage Weil51042122009-11-04 11:39:12 -08001120static int *calc_pg_raw(struct ceph_osdmap *osdmap, struct ceph_pg pgid,
Sage Weilf24e9982009-10-06 11:31:10 -07001121 int *osds, int *num)
1122{
Sage Weilf24e9982009-10-06 11:31:10 -07001123 struct ceph_pg_mapping *pg;
1124 struct ceph_pg_pool_info *pool;
1125 int ruleno;
Linus Torvaldsaf56e0a2012-05-30 11:17:19 -07001126 unsigned int poolid, ps, pps, t, r;
Sage Weilf24e9982009-10-06 11:31:10 -07001127
Sage Weil782e1822011-09-28 10:08:27 -07001128 poolid = le32_to_cpu(pgid.pool);
1129 ps = le16_to_cpu(pgid.ps);
Sage Weil782e1822011-09-28 10:08:27 -07001130
1131 pool = __lookup_pg_pool(&osdmap->pg_pools, poolid);
1132 if (!pool)
1133 return NULL;
1134
Sage Weilf24e9982009-10-06 11:31:10 -07001135 /* pg_temp? */
Sage Weil3469ac1a2012-05-07 15:33:36 -07001136 t = ceph_stable_mod(ps, le32_to_cpu(pool->v.pg_num),
1137 pool->pgp_num_mask);
Sage Weil782e1822011-09-28 10:08:27 -07001138 pgid.ps = cpu_to_le16(t);
Sage Weil9794b142010-02-16 15:53:32 -08001139 pg = __lookup_pg_mapping(&osdmap->pg_temp, pgid);
1140 if (pg) {
1141 *num = pg->len;
1142 return pg->osds;
Sage Weilf24e9982009-10-06 11:31:10 -07001143 }
1144
1145 /* crush */
Sage Weilf24e9982009-10-06 11:31:10 -07001146 ruleno = crush_find_rule(osdmap->crush, pool->v.crush_ruleset,
1147 pool->v.type, pool->v.size);
1148 if (ruleno < 0) {
Sage Weileffcb9e2010-07-09 11:00:08 -07001149 pr_err("no crush rule pool %d ruleset %d type %d size %d\n",
1150 poolid, pool->v.crush_ruleset, pool->v.type,
1151 pool->v.size);
Sage Weilf24e9982009-10-06 11:31:10 -07001152 return NULL;
1153 }
1154
Sage Weil3469ac1a2012-05-07 15:33:36 -07001155 pps = ceph_stable_mod(ps,
1156 le32_to_cpu(pool->v.pgp_num),
1157 pool->pgp_num_mask);
Sage Weil51042122009-11-04 11:39:12 -08001158 pps += poolid;
Sage Weil8b393262012-05-07 15:37:23 -07001159 r = crush_do_rule(osdmap->crush, ruleno, pps, osds,
1160 min_t(int, pool->v.size, *num),
1161 osdmap->osd_weight);
1162 if (r < 0) {
1163 pr_err("error %d from crush rule: pool %d ruleset %d type %d"
1164 " size %d\n", r, poolid, pool->v.crush_ruleset,
1165 pool->v.type, pool->v.size);
1166 return NULL;
1167 }
1168 *num = r;
Sage Weilf24e9982009-10-06 11:31:10 -07001169 return osds;
1170}
1171
1172/*
Sage Weild85b7052010-05-10 10:24:48 -07001173 * Return acting set for given pgid.
1174 */
1175int ceph_calc_pg_acting(struct ceph_osdmap *osdmap, struct ceph_pg pgid,
1176 int *acting)
1177{
1178 int rawosds[CEPH_PG_MAX_SIZE], *osds;
1179 int i, o, num = CEPH_PG_MAX_SIZE;
1180
1181 osds = calc_pg_raw(osdmap, pgid, rawosds, &num);
1182 if (!osds)
1183 return -1;
1184
1185 /* primary is first up osd */
1186 o = 0;
1187 for (i = 0; i < num; i++)
1188 if (ceph_osd_is_up(osdmap, osds[i]))
1189 acting[o++] = osds[i];
1190 return o;
1191}
1192
1193/*
Sage Weilf24e9982009-10-06 11:31:10 -07001194 * Return primary osd for given pgid, or -1 if none.
1195 */
Sage Weil51042122009-11-04 11:39:12 -08001196int ceph_calc_pg_primary(struct ceph_osdmap *osdmap, struct ceph_pg pgid)
Sage Weilf24e9982009-10-06 11:31:10 -07001197{
Sage Weild85b7052010-05-10 10:24:48 -07001198 int rawosds[CEPH_PG_MAX_SIZE], *osds;
1199 int i, num = CEPH_PG_MAX_SIZE;
Sage Weilf24e9982009-10-06 11:31:10 -07001200
1201 osds = calc_pg_raw(osdmap, pgid, rawosds, &num);
1202 if (!osds)
1203 return -1;
1204
1205 /* primary is first up osd */
1206 for (i = 0; i < num; i++)
Sage Weild85b7052010-05-10 10:24:48 -07001207 if (ceph_osd_is_up(osdmap, osds[i]))
Sage Weilf24e9982009-10-06 11:31:10 -07001208 return osds[i];
Sage Weilf24e9982009-10-06 11:31:10 -07001209 return -1;
1210}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001211EXPORT_SYMBOL(ceph_calc_pg_primary);