blob: 3ccd11761cb603912404891866c16fa9ac0b0677 [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
Sage Weilf24e9982009-10-06 11:31:10 -0700317/*
Sage Weil9794b142010-02-16 15:53:32 -0800318 * rbtree of pg_mapping for handling pg_temp (explicit mapping of pgid
319 * to a set of osds)
Sage Weilf24e9982009-10-06 11:31:10 -0700320 */
Sage Weil51042122009-11-04 11:39:12 -0800321static int pgid_cmp(struct ceph_pg l, struct ceph_pg r)
322{
323 u64 a = *(u64 *)&l;
324 u64 b = *(u64 *)&r;
325
326 if (a < b)
327 return -1;
328 if (a > b)
329 return 1;
330 return 0;
331}
332
Sage Weil991abb62009-10-08 22:22:37 -0700333static int __insert_pg_mapping(struct ceph_pg_mapping *new,
334 struct rb_root *root)
Sage Weilf24e9982009-10-06 11:31:10 -0700335{
336 struct rb_node **p = &root->rb_node;
337 struct rb_node *parent = NULL;
338 struct ceph_pg_mapping *pg = NULL;
Sage Weil51042122009-11-04 11:39:12 -0800339 int c;
Sage Weilf24e9982009-10-06 11:31:10 -0700340
341 while (*p) {
342 parent = *p;
343 pg = rb_entry(parent, struct ceph_pg_mapping, node);
Sage Weil51042122009-11-04 11:39:12 -0800344 c = pgid_cmp(new->pgid, pg->pgid);
345 if (c < 0)
Sage Weilf24e9982009-10-06 11:31:10 -0700346 p = &(*p)->rb_left;
Sage Weil51042122009-11-04 11:39:12 -0800347 else if (c > 0)
Sage Weilf24e9982009-10-06 11:31:10 -0700348 p = &(*p)->rb_right;
349 else
Sage Weil991abb62009-10-08 22:22:37 -0700350 return -EEXIST;
Sage Weilf24e9982009-10-06 11:31:10 -0700351 }
352
353 rb_link_node(&new->node, parent, p);
354 rb_insert_color(&new->node, root);
Sage Weil991abb62009-10-08 22:22:37 -0700355 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700356}
357
Sage Weil9794b142010-02-16 15:53:32 -0800358static struct ceph_pg_mapping *__lookup_pg_mapping(struct rb_root *root,
359 struct ceph_pg pgid)
360{
361 struct rb_node *n = root->rb_node;
362 struct ceph_pg_mapping *pg;
363 int c;
364
365 while (n) {
366 pg = rb_entry(n, struct ceph_pg_mapping, node);
367 c = pgid_cmp(pgid, pg->pgid);
368 if (c < 0)
369 n = n->rb_left;
370 else if (c > 0)
371 n = n->rb_right;
372 else
373 return pg;
374 }
375 return NULL;
376}
377
Sage Weilf24e9982009-10-06 11:31:10 -0700378/*
Sage Weil4fc51be2010-02-16 15:55:03 -0800379 * rbtree of pg pool info
380 */
381static int __insert_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *new)
382{
383 struct rb_node **p = &root->rb_node;
384 struct rb_node *parent = NULL;
385 struct ceph_pg_pool_info *pi = NULL;
386
387 while (*p) {
388 parent = *p;
389 pi = rb_entry(parent, struct ceph_pg_pool_info, node);
390 if (new->id < pi->id)
391 p = &(*p)->rb_left;
392 else if (new->id > pi->id)
393 p = &(*p)->rb_right;
394 else
395 return -EEXIST;
396 }
397
398 rb_link_node(&new->node, parent, p);
399 rb_insert_color(&new->node, root);
400 return 0;
401}
402
403static struct ceph_pg_pool_info *__lookup_pg_pool(struct rb_root *root, int id)
404{
405 struct ceph_pg_pool_info *pi;
406 struct rb_node *n = root->rb_node;
407
408 while (n) {
409 pi = rb_entry(n, struct ceph_pg_pool_info, node);
410 if (id < pi->id)
411 n = n->rb_left;
412 else if (id > pi->id)
413 n = n->rb_right;
414 else
415 return pi;
416 }
417 return NULL;
418}
419
Yehuda Sadeh7669a2c2010-05-17 12:31:35 -0700420int ceph_pg_poolid_by_name(struct ceph_osdmap *map, const char *name)
421{
422 struct rb_node *rbp;
423
424 for (rbp = rb_first(&map->pg_pools); rbp; rbp = rb_next(rbp)) {
425 struct ceph_pg_pool_info *pi =
426 rb_entry(rbp, struct ceph_pg_pool_info, node);
427 if (pi->name && strcmp(pi->name, name) == 0)
428 return pi->id;
429 }
430 return -ENOENT;
431}
432
Sage Weil2844a762010-04-09 15:46:42 -0700433static void __remove_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *pi)
434{
435 rb_erase(&pi->node, root);
436 kfree(pi->name);
437 kfree(pi);
438}
439
Sage Weil73a7e692010-08-02 11:00:55 -0700440static int __decode_pool(void **p, void *end, struct ceph_pg_pool_info *pi)
Sage Weilefd75762010-03-17 10:05:28 -0700441{
Sage Weil73a7e692010-08-02 11:00:55 -0700442 unsigned n, m;
443
Sage Weilefd75762010-03-17 10:05:28 -0700444 ceph_decode_copy(p, &pi->v, sizeof(pi->v));
445 calc_pg_masks(pi);
Sage Weil73a7e692010-08-02 11:00:55 -0700446
447 /* num_snaps * snap_info_t */
448 n = le32_to_cpu(pi->v.num_snaps);
449 while (n--) {
450 ceph_decode_need(p, end, sizeof(u64) + 1 + sizeof(u64) +
451 sizeof(struct ceph_timespec), bad);
452 *p += sizeof(u64) + /* key */
453 1 + sizeof(u64) + /* u8, snapid */
454 sizeof(struct ceph_timespec);
455 m = ceph_decode_32(p); /* snap name */
456 *p += m;
457 }
458
Sage Weilefd75762010-03-17 10:05:28 -0700459 *p += le32_to_cpu(pi->v.num_removed_snap_intervals) * sizeof(u64) * 2;
Sage Weil73a7e692010-08-02 11:00:55 -0700460 return 0;
461
462bad:
463 return -EINVAL;
Sage Weilefd75762010-03-17 10:05:28 -0700464}
465
Sage Weil2844a762010-04-09 15:46:42 -0700466static int __decode_pool_names(void **p, void *end, struct ceph_osdmap *map)
467{
468 struct ceph_pg_pool_info *pi;
469 u32 num, len, pool;
470
471 ceph_decode_32_safe(p, end, num, bad);
472 dout(" %d pool names\n", num);
473 while (num--) {
474 ceph_decode_32_safe(p, end, pool, bad);
475 ceph_decode_32_safe(p, end, len, bad);
476 dout(" pool %d len %d\n", pool, len);
477 pi = __lookup_pg_pool(&map->pg_pools, pool);
478 if (pi) {
479 kfree(pi->name);
480 pi->name = kmalloc(len + 1, GFP_NOFS);
481 if (pi->name) {
482 memcpy(pi->name, *p, len);
483 pi->name[len] = '\0';
484 dout(" name is %s\n", pi->name);
485 }
486 }
487 *p += len;
488 }
489 return 0;
490
491bad:
492 return -EINVAL;
493}
494
495/*
496 * osd map
497 */
498void ceph_osdmap_destroy(struct ceph_osdmap *map)
499{
500 dout("osdmap_destroy %p\n", map);
501 if (map->crush)
502 crush_destroy(map->crush);
503 while (!RB_EMPTY_ROOT(&map->pg_temp)) {
504 struct ceph_pg_mapping *pg =
505 rb_entry(rb_first(&map->pg_temp),
506 struct ceph_pg_mapping, node);
507 rb_erase(&pg->node, &map->pg_temp);
508 kfree(pg);
509 }
510 while (!RB_EMPTY_ROOT(&map->pg_pools)) {
511 struct ceph_pg_pool_info *pi =
512 rb_entry(rb_first(&map->pg_pools),
513 struct ceph_pg_pool_info, node);
514 __remove_pg_pool(&map->pg_pools, pi);
515 }
516 kfree(map->osd_state);
517 kfree(map->osd_weight);
518 kfree(map->osd_addr);
519 kfree(map);
520}
521
522/*
523 * adjust max osd value. reallocate arrays.
524 */
525static int osdmap_set_max_osd(struct ceph_osdmap *map, int max)
526{
527 u8 *state;
528 struct ceph_entity_addr *addr;
529 u32 *weight;
530
531 state = kcalloc(max, sizeof(*state), GFP_NOFS);
532 addr = kcalloc(max, sizeof(*addr), GFP_NOFS);
533 weight = kcalloc(max, sizeof(*weight), GFP_NOFS);
534 if (state == NULL || addr == NULL || weight == NULL) {
535 kfree(state);
536 kfree(addr);
537 kfree(weight);
538 return -ENOMEM;
539 }
540
541 /* copy old? */
542 if (map->osd_state) {
543 memcpy(state, map->osd_state, map->max_osd*sizeof(*state));
544 memcpy(addr, map->osd_addr, map->max_osd*sizeof(*addr));
545 memcpy(weight, map->osd_weight, map->max_osd*sizeof(*weight));
546 kfree(map->osd_state);
547 kfree(map->osd_addr);
548 kfree(map->osd_weight);
549 }
550
551 map->osd_state = state;
552 map->osd_weight = weight;
553 map->osd_addr = addr;
554 map->max_osd = max;
555 return 0;
556}
557
Sage Weil4fc51be2010-02-16 15:55:03 -0800558/*
Sage Weilf24e9982009-10-06 11:31:10 -0700559 * decode a full map.
560 */
561struct ceph_osdmap *osdmap_decode(void **p, void *end)
562{
563 struct ceph_osdmap *map;
564 u16 version;
565 u32 len, max, i;
Sage Weil361be862010-01-25 16:03:02 -0800566 u8 ev;
Sage Weilf24e9982009-10-06 11:31:10 -0700567 int err = -EINVAL;
568 void *start = *p;
Sage Weil4fc51be2010-02-16 15:55:03 -0800569 struct ceph_pg_pool_info *pi;
Sage Weilf24e9982009-10-06 11:31:10 -0700570
571 dout("osdmap_decode %p to %p len %d\n", *p, end, (int)(end - *p));
572
573 map = kzalloc(sizeof(*map), GFP_NOFS);
574 if (map == NULL)
575 return ERR_PTR(-ENOMEM);
576 map->pg_temp = RB_ROOT;
577
578 ceph_decode_16_safe(p, end, version, bad);
Sage Weil02f90c62010-02-04 16:18:10 -0800579 if (version > CEPH_OSDMAP_VERSION) {
580 pr_warning("got unknown v %d > %d of osdmap\n", version,
581 CEPH_OSDMAP_VERSION);
582 goto bad;
583 }
Sage Weilf24e9982009-10-06 11:31:10 -0700584
585 ceph_decode_need(p, end, 2*sizeof(u64)+6*sizeof(u32), bad);
586 ceph_decode_copy(p, &map->fsid, sizeof(map->fsid));
Sage Weilc89136e2009-10-14 09:59:09 -0700587 map->epoch = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700588 ceph_decode_copy(p, &map->created, sizeof(map->created));
589 ceph_decode_copy(p, &map->modified, sizeof(map->modified));
590
Sage Weilf24e9982009-10-06 11:31:10 -0700591 ceph_decode_32_safe(p, end, max, bad);
592 while (max--) {
Sage Weil4fc51be2010-02-16 15:55:03 -0800593 ceph_decode_need(p, end, 4 + 1 + sizeof(pi->v), bad);
Sage Weil2844a762010-04-09 15:46:42 -0700594 pi = kzalloc(sizeof(*pi), GFP_NOFS);
Sage Weil4fc51be2010-02-16 15:55:03 -0800595 if (!pi)
Sage Weilf24e9982009-10-06 11:31:10 -0700596 goto bad;
Sage Weil4fc51be2010-02-16 15:55:03 -0800597 pi->id = ceph_decode_32(p);
Sage Weil361be862010-01-25 16:03:02 -0800598 ev = ceph_decode_8(p); /* encoding version */
Sage Weil02f90c62010-02-04 16:18:10 -0800599 if (ev > CEPH_PG_POOL_VERSION) {
600 pr_warning("got unknown v %d > %d of ceph_pg_pool\n",
601 ev, CEPH_PG_POOL_VERSION);
Dan Carpenterb0bbb0b2010-07-08 14:49:38 +0200602 kfree(pi);
Sage Weil02f90c62010-02-04 16:18:10 -0800603 goto bad;
604 }
Sage Weil73a7e692010-08-02 11:00:55 -0700605 err = __decode_pool(p, end, pi);
606 if (err < 0)
607 goto bad;
Sage Weil4fc51be2010-02-16 15:55:03 -0800608 __insert_pg_pool(&map->pg_pools, pi);
Sage Weilf24e9982009-10-06 11:31:10 -0700609 }
Sage Weil2844a762010-04-09 15:46:42 -0700610
611 if (version >= 5 && __decode_pool_names(p, end, map) < 0)
612 goto bad;
613
Sage Weil4fc51be2010-02-16 15:55:03 -0800614 ceph_decode_32_safe(p, end, map->pool_max, bad);
Sage Weilf24e9982009-10-06 11:31:10 -0700615
616 ceph_decode_32_safe(p, end, map->flags, bad);
617
Sage Weilc89136e2009-10-14 09:59:09 -0700618 max = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700619
620 /* (re)alloc osd arrays */
621 err = osdmap_set_max_osd(map, max);
622 if (err < 0)
623 goto bad;
624 dout("osdmap_decode max_osd = %d\n", map->max_osd);
625
626 /* osds */
627 err = -EINVAL;
628 ceph_decode_need(p, end, 3*sizeof(u32) +
629 map->max_osd*(1 + sizeof(*map->osd_weight) +
630 sizeof(*map->osd_addr)), bad);
631 *p += 4; /* skip length field (should match max) */
632 ceph_decode_copy(p, map->osd_state, map->max_osd);
633
634 *p += 4; /* skip length field (should match max) */
635 for (i = 0; i < map->max_osd; i++)
Sage Weilc89136e2009-10-14 09:59:09 -0700636 map->osd_weight[i] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700637
638 *p += 4; /* skip length field (should match max) */
639 ceph_decode_copy(p, map->osd_addr, map->max_osd*sizeof(*map->osd_addr));
Sage Weil63f2d212009-11-03 15:17:56 -0800640 for (i = 0; i < map->max_osd; i++)
641 ceph_decode_addr(&map->osd_addr[i]);
Sage Weilf24e9982009-10-06 11:31:10 -0700642
643 /* pg_temp */
644 ceph_decode_32_safe(p, end, len, bad);
645 for (i = 0; i < len; i++) {
646 int n, j;
Sage Weil51042122009-11-04 11:39:12 -0800647 struct ceph_pg pgid;
Sage Weilf24e9982009-10-06 11:31:10 -0700648 struct ceph_pg_mapping *pg;
649
650 ceph_decode_need(p, end, sizeof(u32) + sizeof(u64), bad);
Sage Weil51042122009-11-04 11:39:12 -0800651 ceph_decode_copy(p, &pgid, sizeof(pgid));
Sage Weilc89136e2009-10-14 09:59:09 -0700652 n = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700653 ceph_decode_need(p, end, n * sizeof(u32), bad);
Sage Weil30dc6382009-12-21 14:49:37 -0800654 err = -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -0700655 pg = kmalloc(sizeof(*pg) + n*sizeof(u32), GFP_NOFS);
Sage Weil30dc6382009-12-21 14:49:37 -0800656 if (!pg)
Sage Weilf24e9982009-10-06 11:31:10 -0700657 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -0700658 pg->pgid = pgid;
659 pg->len = n;
660 for (j = 0; j < n; j++)
Sage Weilc89136e2009-10-14 09:59:09 -0700661 pg->osds[j] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700662
Sage Weil991abb62009-10-08 22:22:37 -0700663 err = __insert_pg_mapping(pg, &map->pg_temp);
664 if (err)
665 goto bad;
Sage Weil51042122009-11-04 11:39:12 -0800666 dout(" added pg_temp %llx len %d\n", *(u64 *)&pgid, len);
Sage Weilf24e9982009-10-06 11:31:10 -0700667 }
668
669 /* crush */
670 ceph_decode_32_safe(p, end, len, bad);
671 dout("osdmap_decode crush len %d from off 0x%x\n", len,
672 (int)(*p - start));
673 ceph_decode_need(p, end, len, bad);
674 map->crush = crush_decode(*p, end);
675 *p += len;
676 if (IS_ERR(map->crush)) {
677 err = PTR_ERR(map->crush);
678 map->crush = NULL;
679 goto bad;
680 }
681
682 /* ignore the rest of the map */
683 *p = end;
684
685 dout("osdmap_decode done %p %p\n", *p, end);
686 return map;
687
688bad:
689 dout("osdmap_decode fail\n");
690 ceph_osdmap_destroy(map);
691 return ERR_PTR(err);
692}
693
694/*
695 * decode and apply an incremental map update.
696 */
697struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end,
698 struct ceph_osdmap *map,
699 struct ceph_messenger *msgr)
700{
Sage Weilf24e9982009-10-06 11:31:10 -0700701 struct crush_map *newcrush = NULL;
702 struct ceph_fsid fsid;
703 u32 epoch = 0;
704 struct ceph_timespec modified;
705 u32 len, pool;
Sage Weil4fc51be2010-02-16 15:55:03 -0800706 __s32 new_pool_max, new_flags, max;
Sage Weilf24e9982009-10-06 11:31:10 -0700707 void *start = *p;
708 int err = -EINVAL;
709 u16 version;
710 struct rb_node *rbp;
711
712 ceph_decode_16_safe(p, end, version, bad);
Sage Weil02f90c62010-02-04 16:18:10 -0800713 if (version > CEPH_OSDMAP_INC_VERSION) {
714 pr_warning("got unknown v %d > %d of inc osdmap\n", version,
715 CEPH_OSDMAP_INC_VERSION);
716 goto bad;
717 }
Sage Weilf24e9982009-10-06 11:31:10 -0700718
719 ceph_decode_need(p, end, sizeof(fsid)+sizeof(modified)+2*sizeof(u32),
720 bad);
721 ceph_decode_copy(p, &fsid, sizeof(fsid));
Sage Weilc89136e2009-10-14 09:59:09 -0700722 epoch = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700723 BUG_ON(epoch != map->epoch+1);
724 ceph_decode_copy(p, &modified, sizeof(modified));
Sage Weil4fc51be2010-02-16 15:55:03 -0800725 new_pool_max = ceph_decode_32(p);
Sage Weilc89136e2009-10-14 09:59:09 -0700726 new_flags = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700727
728 /* full map? */
729 ceph_decode_32_safe(p, end, len, bad);
730 if (len > 0) {
731 dout("apply_incremental full map len %d, %p to %p\n",
732 len, *p, end);
Sage Weil30dc6382009-12-21 14:49:37 -0800733 return osdmap_decode(p, min(*p+len, end));
Sage Weilf24e9982009-10-06 11:31:10 -0700734 }
735
736 /* new crush? */
737 ceph_decode_32_safe(p, end, len, bad);
738 if (len > 0) {
739 dout("apply_incremental new crush map len %d, %p to %p\n",
740 len, *p, end);
741 newcrush = crush_decode(*p, min(*p+len, end));
742 if (IS_ERR(newcrush))
Julia Lawall7e34bc52010-05-22 12:01:14 +0200743 return ERR_CAST(newcrush);
Sage Weilcebc5be2010-06-17 10:22:48 -0700744 *p += len;
Sage Weilf24e9982009-10-06 11:31:10 -0700745 }
746
747 /* new flags? */
748 if (new_flags >= 0)
749 map->flags = new_flags;
Sage Weil4fc51be2010-02-16 15:55:03 -0800750 if (new_pool_max >= 0)
751 map->pool_max = new_pool_max;
Sage Weilf24e9982009-10-06 11:31:10 -0700752
753 ceph_decode_need(p, end, 5*sizeof(u32), bad);
754
755 /* new max? */
Sage Weilc89136e2009-10-14 09:59:09 -0700756 max = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700757 if (max >= 0) {
758 err = osdmap_set_max_osd(map, max);
759 if (err < 0)
760 goto bad;
761 }
762
763 map->epoch++;
764 map->modified = map->modified;
765 if (newcrush) {
766 if (map->crush)
767 crush_destroy(map->crush);
768 map->crush = newcrush;
769 newcrush = NULL;
770 }
771
772 /* new_pool */
773 ceph_decode_32_safe(p, end, len, bad);
774 while (len--) {
Sage Weil361be862010-01-25 16:03:02 -0800775 __u8 ev;
Sage Weil4fc51be2010-02-16 15:55:03 -0800776 struct ceph_pg_pool_info *pi;
Sage Weil361be862010-01-25 16:03:02 -0800777
Sage Weilf24e9982009-10-06 11:31:10 -0700778 ceph_decode_32_safe(p, end, pool, bad);
Sage Weil4fc51be2010-02-16 15:55:03 -0800779 ceph_decode_need(p, end, 1 + sizeof(pi->v), bad);
Sage Weil361be862010-01-25 16:03:02 -0800780 ev = ceph_decode_8(p); /* encoding version */
Sage Weil02f90c62010-02-04 16:18:10 -0800781 if (ev > CEPH_PG_POOL_VERSION) {
782 pr_warning("got unknown v %d > %d of ceph_pg_pool\n",
783 ev, CEPH_PG_POOL_VERSION);
784 goto bad;
785 }
Sage Weil4fc51be2010-02-16 15:55:03 -0800786 pi = __lookup_pg_pool(&map->pg_pools, pool);
787 if (!pi) {
Sage Weil2844a762010-04-09 15:46:42 -0700788 pi = kzalloc(sizeof(*pi), GFP_NOFS);
Sage Weil4fc51be2010-02-16 15:55:03 -0800789 if (!pi) {
790 err = -ENOMEM;
791 goto bad;
792 }
793 pi->id = pool;
794 __insert_pg_pool(&map->pg_pools, pi);
795 }
Sage Weil73a7e692010-08-02 11:00:55 -0700796 err = __decode_pool(p, end, pi);
797 if (err < 0)
798 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -0700799 }
Sage Weil2844a762010-04-09 15:46:42 -0700800 if (version >= 5 && __decode_pool_names(p, end, map) < 0)
801 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -0700802
Sage Weil4fc51be2010-02-16 15:55:03 -0800803 /* old_pool */
Sage Weilf24e9982009-10-06 11:31:10 -0700804 ceph_decode_32_safe(p, end, len, bad);
Sage Weil4fc51be2010-02-16 15:55:03 -0800805 while (len--) {
806 struct ceph_pg_pool_info *pi;
807
808 ceph_decode_32_safe(p, end, pool, bad);
809 pi = __lookup_pg_pool(&map->pg_pools, pool);
Sage Weil2844a762010-04-09 15:46:42 -0700810 if (pi)
811 __remove_pg_pool(&map->pg_pools, pi);
Sage Weil4fc51be2010-02-16 15:55:03 -0800812 }
Sage Weilf24e9982009-10-06 11:31:10 -0700813
814 /* new_up */
815 err = -EINVAL;
816 ceph_decode_32_safe(p, end, len, bad);
817 while (len--) {
818 u32 osd;
819 struct ceph_entity_addr addr;
820 ceph_decode_32_safe(p, end, osd, bad);
821 ceph_decode_copy_safe(p, end, &addr, sizeof(addr), bad);
Sage Weil63f2d212009-11-03 15:17:56 -0800822 ceph_decode_addr(&addr);
Sage Weilf24e9982009-10-06 11:31:10 -0700823 pr_info("osd%d up\n", osd);
824 BUG_ON(osd >= map->max_osd);
825 map->osd_state[osd] |= CEPH_OSD_UP;
826 map->osd_addr[osd] = addr;
827 }
828
829 /* new_down */
830 ceph_decode_32_safe(p, end, len, bad);
831 while (len--) {
832 u32 osd;
833 ceph_decode_32_safe(p, end, osd, bad);
834 (*p)++; /* clean flag */
Sage Weil1bdb70e2009-11-06 13:57:49 -0800835 pr_info("osd%d down\n", osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700836 if (osd < map->max_osd)
837 map->osd_state[osd] &= ~CEPH_OSD_UP;
838 }
839
840 /* new_weight */
841 ceph_decode_32_safe(p, end, len, bad);
842 while (len--) {
843 u32 osd, off;
844 ceph_decode_need(p, end, sizeof(u32)*2, bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700845 osd = ceph_decode_32(p);
846 off = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700847 pr_info("osd%d weight 0x%x %s\n", osd, off,
848 off == CEPH_OSD_IN ? "(in)" :
849 (off == CEPH_OSD_OUT ? "(out)" : ""));
850 if (osd < map->max_osd)
851 map->osd_weight[osd] = off;
852 }
853
854 /* new_pg_temp */
855 rbp = rb_first(&map->pg_temp);
856 ceph_decode_32_safe(p, end, len, bad);
857 while (len--) {
858 struct ceph_pg_mapping *pg;
859 int j;
Sage Weil51042122009-11-04 11:39:12 -0800860 struct ceph_pg pgid;
Sage Weilf24e9982009-10-06 11:31:10 -0700861 u32 pglen;
862 ceph_decode_need(p, end, sizeof(u64) + sizeof(u32), bad);
Sage Weil51042122009-11-04 11:39:12 -0800863 ceph_decode_copy(p, &pgid, sizeof(pgid));
Sage Weilc89136e2009-10-14 09:59:09 -0700864 pglen = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700865
866 /* remove any? */
Sage Weil51042122009-11-04 11:39:12 -0800867 while (rbp && pgid_cmp(rb_entry(rbp, struct ceph_pg_mapping,
868 node)->pgid, pgid) <= 0) {
Sage Weilbc4fdca2010-07-20 16:19:56 -0700869 struct ceph_pg_mapping *cur =
870 rb_entry(rbp, struct ceph_pg_mapping, node);
Sage Weil213c99e2010-08-03 10:25:11 -0700871
Sage Weilf24e9982009-10-06 11:31:10 -0700872 rbp = rb_next(rbp);
Sage Weilbc4fdca2010-07-20 16:19:56 -0700873 dout(" removed pg_temp %llx\n", *(u64 *)&cur->pgid);
874 rb_erase(&cur->node, &map->pg_temp);
875 kfree(cur);
Sage Weilf24e9982009-10-06 11:31:10 -0700876 }
877
878 if (pglen) {
879 /* insert */
880 ceph_decode_need(p, end, pglen*sizeof(u32), bad);
881 pg = kmalloc(sizeof(*pg) + sizeof(u32)*pglen, GFP_NOFS);
882 if (!pg) {
883 err = -ENOMEM;
884 goto bad;
885 }
886 pg->pgid = pgid;
887 pg->len = pglen;
Sage Weil7067f792009-12-21 16:02:37 -0800888 for (j = 0; j < pglen; j++)
Sage Weilc89136e2009-10-14 09:59:09 -0700889 pg->osds[j] = ceph_decode_32(p);
Sage Weil991abb62009-10-08 22:22:37 -0700890 err = __insert_pg_mapping(pg, &map->pg_temp);
Sage Weilbc4fdca2010-07-20 16:19:56 -0700891 if (err) {
892 kfree(pg);
Sage Weil991abb62009-10-08 22:22:37 -0700893 goto bad;
Sage Weilbc4fdca2010-07-20 16:19:56 -0700894 }
Sage Weil51042122009-11-04 11:39:12 -0800895 dout(" added pg_temp %llx len %d\n", *(u64 *)&pgid,
896 pglen);
Sage Weilf24e9982009-10-06 11:31:10 -0700897 }
898 }
899 while (rbp) {
Sage Weilbc4fdca2010-07-20 16:19:56 -0700900 struct ceph_pg_mapping *cur =
901 rb_entry(rbp, struct ceph_pg_mapping, node);
902
Sage Weilf24e9982009-10-06 11:31:10 -0700903 rbp = rb_next(rbp);
Sage Weilbc4fdca2010-07-20 16:19:56 -0700904 dout(" removed pg_temp %llx\n", *(u64 *)&cur->pgid);
905 rb_erase(&cur->node, &map->pg_temp);
906 kfree(cur);
Sage Weilf24e9982009-10-06 11:31:10 -0700907 }
908
909 /* ignore the rest */
910 *p = end;
911 return map;
912
913bad:
914 pr_err("corrupt inc osdmap epoch %d off %d (%p of %p-%p)\n",
915 epoch, (int)(*p - start), *p, start, end);
Sage Weil9ec7cab2009-12-14 15:13:47 -0800916 print_hex_dump(KERN_DEBUG, "osdmap: ",
917 DUMP_PREFIX_OFFSET, 16, 1,
918 start, end - start, true);
Sage Weilf24e9982009-10-06 11:31:10 -0700919 if (newcrush)
920 crush_destroy(newcrush);
921 return ERR_PTR(err);
922}
923
924
925
926
927/*
928 * calculate file layout from given offset, length.
929 * fill in correct oid, logical length, and object extent
930 * offset, length.
931 *
932 * for now, we write only a single su, until we can
933 * pass a stride back to the caller.
934 */
935void ceph_calc_file_object_mapping(struct ceph_file_layout *layout,
936 u64 off, u64 *plen,
Sage Weil645a1022009-10-28 15:15:05 -0700937 u64 *ono,
Sage Weilf24e9982009-10-06 11:31:10 -0700938 u64 *oxoff, u64 *oxlen)
939{
940 u32 osize = le32_to_cpu(layout->fl_object_size);
941 u32 su = le32_to_cpu(layout->fl_stripe_unit);
942 u32 sc = le32_to_cpu(layout->fl_stripe_count);
943 u32 bl, stripeno, stripepos, objsetno;
944 u32 su_per_object;
Noah Watkinsff1d1f72009-10-30 12:57:30 -0700945 u64 t, su_offset;
Sage Weilf24e9982009-10-06 11:31:10 -0700946
947 dout("mapping %llu~%llu osize %u fl_su %u\n", off, *plen,
948 osize, su);
Noah Watkins35e054a2009-10-28 14:04:48 -0700949 su_per_object = osize / su;
Sage Weilf24e9982009-10-06 11:31:10 -0700950 dout("osize %u / su %u = su_per_object %u\n", osize, su,
951 su_per_object);
952
953 BUG_ON((su & ~PAGE_MASK) != 0);
954 /* bl = *off / su; */
955 t = off;
956 do_div(t, su);
957 bl = t;
958 dout("off %llu / su %u = bl %u\n", off, su, bl);
959
960 stripeno = bl / sc;
961 stripepos = bl % sc;
962 objsetno = stripeno / su_per_object;
963
Sage Weil645a1022009-10-28 15:15:05 -0700964 *ono = objsetno * sc + stripepos;
965 dout("objset %u * sc %u = ono %u\n", objsetno, sc, (unsigned)*ono);
966
967 /* *oxoff = *off % layout->fl_stripe_unit; # offset in su */
Sage Weilf24e9982009-10-06 11:31:10 -0700968 t = off;
Noah Watkinsff1d1f72009-10-30 12:57:30 -0700969 su_offset = do_div(t, su);
970 *oxoff = su_offset + (stripeno % su_per_object) * su;
Sage Weil645a1022009-10-28 15:15:05 -0700971
Noah Watkinsff1d1f72009-10-30 12:57:30 -0700972 /*
973 * Calculate the length of the extent being written to the selected
974 * object. This is the minimum of the full length requested (plen) or
975 * the remainder of the current stripe being written to.
976 */
977 *oxlen = min_t(u64, *plen, su - su_offset);
Sage Weilf24e9982009-10-06 11:31:10 -0700978 *plen = *oxlen;
979
980 dout(" obj extent %llu~%llu\n", *oxoff, *oxlen);
981}
982
983/*
984 * calculate an object layout (i.e. pgid) from an oid,
985 * file_layout, and osdmap
986 */
987int ceph_calc_object_layout(struct ceph_object_layout *ol,
988 const char *oid,
989 struct ceph_file_layout *fl,
990 struct ceph_osdmap *osdmap)
991{
992 unsigned num, num_mask;
Sage Weil51042122009-11-04 11:39:12 -0800993 struct ceph_pg pgid;
Sage Weilf24e9982009-10-06 11:31:10 -0700994 s32 preferred = (s32)le32_to_cpu(fl->fl_pg_preferred);
995 int poolid = le32_to_cpu(fl->fl_pg_pool);
996 struct ceph_pg_pool_info *pool;
Sage Weil51042122009-11-04 11:39:12 -0800997 unsigned ps;
Sage Weilf24e9982009-10-06 11:31:10 -0700998
Sage Weil30dc6382009-12-21 14:49:37 -0800999 BUG_ON(!osdmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001000
Sage Weil4fc51be2010-02-16 15:55:03 -08001001 pool = __lookup_pg_pool(&osdmap->pg_pools, poolid);
1002 if (!pool)
1003 return -EIO;
Sage Weil1654dd02009-11-06 21:55:25 -08001004 ps = ceph_str_hash(pool->v.object_hash, oid, strlen(oid));
Sage Weilf24e9982009-10-06 11:31:10 -07001005 if (preferred >= 0) {
Sage Weil51042122009-11-04 11:39:12 -08001006 ps += preferred;
Sage Weilf24e9982009-10-06 11:31:10 -07001007 num = le32_to_cpu(pool->v.lpg_num);
1008 num_mask = pool->lpg_num_mask;
1009 } else {
1010 num = le32_to_cpu(pool->v.pg_num);
1011 num_mask = pool->pg_num_mask;
1012 }
1013
Sage Weil51042122009-11-04 11:39:12 -08001014 pgid.ps = cpu_to_le16(ps);
1015 pgid.preferred = cpu_to_le16(preferred);
1016 pgid.pool = fl->fl_pg_pool;
Sage Weilee7fdfa2009-10-19 11:41:51 -07001017 if (preferred >= 0)
Sage Weil51042122009-11-04 11:39:12 -08001018 dout("calc_object_layout '%s' pgid %d.%xp%d\n", oid, poolid, ps,
1019 (int)preferred);
Sage Weilf24e9982009-10-06 11:31:10 -07001020 else
Sage Weil51042122009-11-04 11:39:12 -08001021 dout("calc_object_layout '%s' pgid %d.%x\n", oid, poolid, ps);
Sage Weilf24e9982009-10-06 11:31:10 -07001022
Sage Weil51042122009-11-04 11:39:12 -08001023 ol->ol_pgid = pgid;
Sage Weilf24e9982009-10-06 11:31:10 -07001024 ol->ol_stripe_unit = fl->fl_object_stripe_unit;
Sage Weilf24e9982009-10-06 11:31:10 -07001025 return 0;
1026}
1027
1028/*
1029 * Calculate raw osd vector for the given pgid. Return pointer to osd
1030 * array, or NULL on failure.
1031 */
Sage Weil51042122009-11-04 11:39:12 -08001032static int *calc_pg_raw(struct ceph_osdmap *osdmap, struct ceph_pg pgid,
Sage Weilf24e9982009-10-06 11:31:10 -07001033 int *osds, int *num)
1034{
Sage Weilf24e9982009-10-06 11:31:10 -07001035 struct ceph_pg_mapping *pg;
1036 struct ceph_pg_pool_info *pool;
1037 int ruleno;
Sage Weil51042122009-11-04 11:39:12 -08001038 unsigned poolid, ps, pps;
1039 int preferred;
Sage Weilf24e9982009-10-06 11:31:10 -07001040
1041 /* pg_temp? */
Sage Weil9794b142010-02-16 15:53:32 -08001042 pg = __lookup_pg_mapping(&osdmap->pg_temp, pgid);
1043 if (pg) {
1044 *num = pg->len;
1045 return pg->osds;
Sage Weilf24e9982009-10-06 11:31:10 -07001046 }
1047
1048 /* crush */
Sage Weil51042122009-11-04 11:39:12 -08001049 poolid = le32_to_cpu(pgid.pool);
1050 ps = le16_to_cpu(pgid.ps);
1051 preferred = (s16)le16_to_cpu(pgid.preferred);
1052
Sage Weil767ea5c2009-12-09 12:34:01 -08001053 /* don't forcefeed bad device ids to crush */
1054 if (preferred >= osdmap->max_osd ||
1055 preferred >= osdmap->crush->max_devices)
1056 preferred = -1;
1057
Sage Weil4fc51be2010-02-16 15:55:03 -08001058 pool = __lookup_pg_pool(&osdmap->pg_pools, poolid);
1059 if (!pool)
Sage Weilf24e9982009-10-06 11:31:10 -07001060 return NULL;
Sage Weilf24e9982009-10-06 11:31:10 -07001061 ruleno = crush_find_rule(osdmap->crush, pool->v.crush_ruleset,
1062 pool->v.type, pool->v.size);
1063 if (ruleno < 0) {
Sage Weileffcb9e2010-07-09 11:00:08 -07001064 pr_err("no crush rule pool %d ruleset %d type %d size %d\n",
1065 poolid, pool->v.crush_ruleset, pool->v.type,
1066 pool->v.size);
Sage Weilf24e9982009-10-06 11:31:10 -07001067 return NULL;
1068 }
1069
Sage Weil51042122009-11-04 11:39:12 -08001070 if (preferred >= 0)
1071 pps = ceph_stable_mod(ps,
Sage Weilf24e9982009-10-06 11:31:10 -07001072 le32_to_cpu(pool->v.lpgp_num),
1073 pool->lpgp_num_mask);
1074 else
Sage Weil51042122009-11-04 11:39:12 -08001075 pps = ceph_stable_mod(ps,
Sage Weilf24e9982009-10-06 11:31:10 -07001076 le32_to_cpu(pool->v.pgp_num),
1077 pool->pgp_num_mask);
Sage Weil51042122009-11-04 11:39:12 -08001078 pps += poolid;
Sage Weilf24e9982009-10-06 11:31:10 -07001079 *num = crush_do_rule(osdmap->crush, ruleno, pps, osds,
1080 min_t(int, pool->v.size, *num),
Sage Weil51042122009-11-04 11:39:12 -08001081 preferred, osdmap->osd_weight);
Sage Weilf24e9982009-10-06 11:31:10 -07001082 return osds;
1083}
1084
1085/*
Sage Weild85b7052010-05-10 10:24:48 -07001086 * Return acting set for given pgid.
1087 */
1088int ceph_calc_pg_acting(struct ceph_osdmap *osdmap, struct ceph_pg pgid,
1089 int *acting)
1090{
1091 int rawosds[CEPH_PG_MAX_SIZE], *osds;
1092 int i, o, num = CEPH_PG_MAX_SIZE;
1093
1094 osds = calc_pg_raw(osdmap, pgid, rawosds, &num);
1095 if (!osds)
1096 return -1;
1097
1098 /* primary is first up osd */
1099 o = 0;
1100 for (i = 0; i < num; i++)
1101 if (ceph_osd_is_up(osdmap, osds[i]))
1102 acting[o++] = osds[i];
1103 return o;
1104}
1105
1106/*
Sage Weilf24e9982009-10-06 11:31:10 -07001107 * Return primary osd for given pgid, or -1 if none.
1108 */
Sage Weil51042122009-11-04 11:39:12 -08001109int ceph_calc_pg_primary(struct ceph_osdmap *osdmap, struct ceph_pg pgid)
Sage Weilf24e9982009-10-06 11:31:10 -07001110{
Sage Weild85b7052010-05-10 10:24:48 -07001111 int rawosds[CEPH_PG_MAX_SIZE], *osds;
1112 int i, num = CEPH_PG_MAX_SIZE;
Sage Weilf24e9982009-10-06 11:31:10 -07001113
1114 osds = calc_pg_raw(osdmap, pgid, rawosds, &num);
1115 if (!osds)
1116 return -1;
1117
1118 /* primary is first up osd */
1119 for (i = 0; i < num; i++)
Sage Weild85b7052010-05-10 10:24:48 -07001120 if (ceph_osd_is_up(osdmap, osds[i]))
Sage Weilf24e9982009-10-06 11:31:10 -07001121 return osds[i];
Sage Weilf24e9982009-10-06 11:31:10 -07001122 return -1;
1123}