blob: 4543b9aba40ce1a9e4565824624bcecba6c298cb [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{
Sage Weilf24e9982009-10-06 11:31:10 -070016 if (!len)
Cong Ding1ec39112013-01-25 17:48:59 -060017 return str;
Sage Weilf24e9982009-10-06 11:31:10 -070018
Cong Ding1ec39112013-01-25 17:48:59 -060019 if ((state & CEPH_OSD_EXISTS) && (state & CEPH_OSD_UP))
20 snprintf(str, len, "exists, up");
21 else if (state & CEPH_OSD_EXISTS)
22 snprintf(str, len, "exists");
23 else if (state & CEPH_OSD_UP)
24 snprintf(str, len, "up");
25 else
Sage Weilf24e9982009-10-06 11:31:10 -070026 snprintf(str, len, "doesn't exist");
Cong Ding1ec39112013-01-25 17:48:59 -060027
Sage Weilf24e9982009-10-06 11:31:10 -070028 return str;
29}
30
31/* maps */
32
Eric Dumazet95c96172012-04-15 05:58:06 +000033static int calc_bits_of(unsigned int t)
Sage Weilf24e9982009-10-06 11:31:10 -070034{
35 int b = 0;
36 while (t) {
37 t = t >> 1;
38 b++;
39 }
40 return b;
41}
42
43/*
44 * the foo_mask is the smallest value 2^n-1 that is >= foo.
45 */
46static void calc_pg_masks(struct ceph_pg_pool_info *pi)
47{
Sage Weil4f6a7e52013-02-23 10:41:09 -080048 pi->pg_num_mask = (1 << calc_bits_of(pi->pg_num-1)) - 1;
49 pi->pgp_num_mask = (1 << calc_bits_of(pi->pgp_num-1)) - 1;
Sage Weilf24e9982009-10-06 11:31:10 -070050}
51
52/*
53 * decode crush map
54 */
55static int crush_decode_uniform_bucket(void **p, void *end,
56 struct crush_bucket_uniform *b)
57{
58 dout("crush_decode_uniform_bucket %p to %p\n", *p, end);
59 ceph_decode_need(p, end, (1+b->h.size) * sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -070060 b->item_weight = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -070061 return 0;
62bad:
63 return -EINVAL;
64}
65
66static int crush_decode_list_bucket(void **p, void *end,
67 struct crush_bucket_list *b)
68{
69 int j;
70 dout("crush_decode_list_bucket %p to %p\n", *p, end);
71 b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
72 if (b->item_weights == NULL)
73 return -ENOMEM;
74 b->sum_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
75 if (b->sum_weights == NULL)
76 return -ENOMEM;
77 ceph_decode_need(p, end, 2 * b->h.size * sizeof(u32), bad);
78 for (j = 0; j < b->h.size; j++) {
Sage Weilc89136e2009-10-14 09:59:09 -070079 b->item_weights[j] = ceph_decode_32(p);
80 b->sum_weights[j] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -070081 }
82 return 0;
83bad:
84 return -EINVAL;
85}
86
87static int crush_decode_tree_bucket(void **p, void *end,
88 struct crush_bucket_tree *b)
89{
90 int j;
91 dout("crush_decode_tree_bucket %p to %p\n", *p, end);
92 ceph_decode_32_safe(p, end, b->num_nodes, bad);
93 b->node_weights = kcalloc(b->num_nodes, sizeof(u32), GFP_NOFS);
94 if (b->node_weights == NULL)
95 return -ENOMEM;
96 ceph_decode_need(p, end, b->num_nodes * sizeof(u32), bad);
97 for (j = 0; j < b->num_nodes; j++)
Sage Weilc89136e2009-10-14 09:59:09 -070098 b->node_weights[j] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -070099 return 0;
100bad:
101 return -EINVAL;
102}
103
104static int crush_decode_straw_bucket(void **p, void *end,
105 struct crush_bucket_straw *b)
106{
107 int j;
108 dout("crush_decode_straw_bucket %p to %p\n", *p, end);
109 b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
110 if (b->item_weights == NULL)
111 return -ENOMEM;
112 b->straws = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
113 if (b->straws == NULL)
114 return -ENOMEM;
115 ceph_decode_need(p, end, 2 * b->h.size * sizeof(u32), bad);
116 for (j = 0; j < b->h.size; j++) {
Sage Weilc89136e2009-10-14 09:59:09 -0700117 b->item_weights[j] = ceph_decode_32(p);
118 b->straws[j] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700119 }
120 return 0;
121bad:
122 return -EINVAL;
123}
124
Sage Weil546f04e2012-07-30 18:15:23 -0700125static int skip_name_map(void **p, void *end)
126{
127 int len;
128 ceph_decode_32_safe(p, end, len ,bad);
129 while (len--) {
130 int strlen;
131 *p += sizeof(u32);
132 ceph_decode_32_safe(p, end, strlen, bad);
133 *p += strlen;
134}
135 return 0;
136bad:
137 return -EINVAL;
138}
139
Sage Weilf24e9982009-10-06 11:31:10 -0700140static struct crush_map *crush_decode(void *pbyval, void *end)
141{
142 struct crush_map *c;
143 int err = -EINVAL;
144 int i, j;
145 void **p = &pbyval;
146 void *start = pbyval;
147 u32 magic;
Sage Weil546f04e2012-07-30 18:15:23 -0700148 u32 num_name_maps;
Sage Weilf24e9982009-10-06 11:31:10 -0700149
150 dout("crush_decode %p to %p len %d\n", *p, end, (int)(end - *p));
151
152 c = kzalloc(sizeof(*c), GFP_NOFS);
153 if (c == NULL)
154 return ERR_PTR(-ENOMEM);
155
Sage Weil546f04e2012-07-30 18:15:23 -0700156 /* set tunables to default values */
157 c->choose_local_tries = 2;
158 c->choose_local_fallback_tries = 5;
159 c->choose_total_tries = 19;
Jim Schutt1604f482012-11-30 09:15:25 -0700160 c->chooseleaf_descend_once = 0;
Sage Weil546f04e2012-07-30 18:15:23 -0700161
Sage Weilf24e9982009-10-06 11:31:10 -0700162 ceph_decode_need(p, end, 4*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700163 magic = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700164 if (magic != CRUSH_MAGIC) {
165 pr_err("crush_decode magic %x != current %x\n",
Eric Dumazet95c96172012-04-15 05:58:06 +0000166 (unsigned int)magic, (unsigned int)CRUSH_MAGIC);
Sage Weilf24e9982009-10-06 11:31:10 -0700167 goto bad;
168 }
Sage Weilc89136e2009-10-14 09:59:09 -0700169 c->max_buckets = ceph_decode_32(p);
170 c->max_rules = ceph_decode_32(p);
171 c->max_devices = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700172
Sage Weilf24e9982009-10-06 11:31:10 -0700173 c->buckets = kcalloc(c->max_buckets, sizeof(*c->buckets), GFP_NOFS);
174 if (c->buckets == NULL)
175 goto badmem;
176 c->rules = kcalloc(c->max_rules, sizeof(*c->rules), GFP_NOFS);
177 if (c->rules == NULL)
178 goto badmem;
179
180 /* buckets */
181 for (i = 0; i < c->max_buckets; i++) {
182 int size = 0;
183 u32 alg;
184 struct crush_bucket *b;
185
186 ceph_decode_32_safe(p, end, alg, bad);
187 if (alg == 0) {
188 c->buckets[i] = NULL;
189 continue;
190 }
191 dout("crush_decode bucket %d off %x %p to %p\n",
192 i, (int)(*p-start), *p, end);
193
194 switch (alg) {
195 case CRUSH_BUCKET_UNIFORM:
196 size = sizeof(struct crush_bucket_uniform);
197 break;
198 case CRUSH_BUCKET_LIST:
199 size = sizeof(struct crush_bucket_list);
200 break;
201 case CRUSH_BUCKET_TREE:
202 size = sizeof(struct crush_bucket_tree);
203 break;
204 case CRUSH_BUCKET_STRAW:
205 size = sizeof(struct crush_bucket_straw);
206 break;
207 default:
Sage Weil30dc6382009-12-21 14:49:37 -0800208 err = -EINVAL;
Sage Weilf24e9982009-10-06 11:31:10 -0700209 goto bad;
210 }
211 BUG_ON(size == 0);
212 b = c->buckets[i] = kzalloc(size, GFP_NOFS);
213 if (b == NULL)
214 goto badmem;
215
216 ceph_decode_need(p, end, 4*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700217 b->id = ceph_decode_32(p);
218 b->type = ceph_decode_16(p);
Sage Weilfb690392009-11-07 20:18:22 -0800219 b->alg = ceph_decode_8(p);
220 b->hash = ceph_decode_8(p);
Sage Weilc89136e2009-10-14 09:59:09 -0700221 b->weight = ceph_decode_32(p);
222 b->size = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700223
224 dout("crush_decode bucket size %d off %x %p to %p\n",
225 b->size, (int)(*p-start), *p, end);
226
227 b->items = kcalloc(b->size, sizeof(__s32), GFP_NOFS);
228 if (b->items == NULL)
229 goto badmem;
230 b->perm = kcalloc(b->size, sizeof(u32), GFP_NOFS);
231 if (b->perm == NULL)
232 goto badmem;
233 b->perm_n = 0;
234
235 ceph_decode_need(p, end, b->size*sizeof(u32), bad);
236 for (j = 0; j < b->size; j++)
Sage Weilc89136e2009-10-14 09:59:09 -0700237 b->items[j] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700238
239 switch (b->alg) {
240 case CRUSH_BUCKET_UNIFORM:
241 err = crush_decode_uniform_bucket(p, end,
242 (struct crush_bucket_uniform *)b);
243 if (err < 0)
244 goto bad;
245 break;
246 case CRUSH_BUCKET_LIST:
247 err = crush_decode_list_bucket(p, end,
248 (struct crush_bucket_list *)b);
249 if (err < 0)
250 goto bad;
251 break;
252 case CRUSH_BUCKET_TREE:
253 err = crush_decode_tree_bucket(p, end,
254 (struct crush_bucket_tree *)b);
255 if (err < 0)
256 goto bad;
257 break;
258 case CRUSH_BUCKET_STRAW:
259 err = crush_decode_straw_bucket(p, end,
260 (struct crush_bucket_straw *)b);
261 if (err < 0)
262 goto bad;
263 break;
264 }
265 }
266
267 /* rules */
268 dout("rule vec is %p\n", c->rules);
269 for (i = 0; i < c->max_rules; i++) {
270 u32 yes;
271 struct crush_rule *r;
272
273 ceph_decode_32_safe(p, end, yes, bad);
274 if (!yes) {
275 dout("crush_decode NO rule %d off %x %p to %p\n",
276 i, (int)(*p-start), *p, end);
277 c->rules[i] = NULL;
278 continue;
279 }
280
281 dout("crush_decode rule %d off %x %p to %p\n",
282 i, (int)(*p-start), *p, end);
283
284 /* len */
285 ceph_decode_32_safe(p, end, yes, bad);
286#if BITS_PER_LONG == 32
Sage Weil30dc6382009-12-21 14:49:37 -0800287 err = -EINVAL;
Xi Wang64486692012-02-16 11:55:48 -0500288 if (yes > (ULONG_MAX - sizeof(*r))
289 / sizeof(struct crush_rule_step))
Sage Weilf24e9982009-10-06 11:31:10 -0700290 goto bad;
291#endif
292 r = c->rules[i] = kmalloc(sizeof(*r) +
293 yes*sizeof(struct crush_rule_step),
294 GFP_NOFS);
295 if (r == NULL)
296 goto badmem;
297 dout(" rule %d is at %p\n", i, r);
298 r->len = yes;
299 ceph_decode_copy_safe(p, end, &r->mask, 4, bad); /* 4 u8's */
300 ceph_decode_need(p, end, r->len*3*sizeof(u32), bad);
301 for (j = 0; j < r->len; j++) {
Sage Weilc89136e2009-10-14 09:59:09 -0700302 r->steps[j].op = ceph_decode_32(p);
303 r->steps[j].arg1 = ceph_decode_32(p);
304 r->steps[j].arg2 = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700305 }
306 }
307
308 /* ignore trailing name maps. */
Sage Weil546f04e2012-07-30 18:15:23 -0700309 for (num_name_maps = 0; num_name_maps < 3; num_name_maps++) {
310 err = skip_name_map(p, end);
311 if (err < 0)
312 goto done;
313 }
Sage Weilf24e9982009-10-06 11:31:10 -0700314
Sage Weil546f04e2012-07-30 18:15:23 -0700315 /* tunables */
316 ceph_decode_need(p, end, 3*sizeof(u32), done);
317 c->choose_local_tries = ceph_decode_32(p);
318 c->choose_local_fallback_tries = ceph_decode_32(p);
319 c->choose_total_tries = ceph_decode_32(p);
320 dout("crush decode tunable choose_local_tries = %d",
321 c->choose_local_tries);
322 dout("crush decode tunable choose_local_fallback_tries = %d",
323 c->choose_local_fallback_tries);
324 dout("crush decode tunable choose_total_tries = %d",
325 c->choose_total_tries);
326
Jim Schutt1604f482012-11-30 09:15:25 -0700327 ceph_decode_need(p, end, sizeof(u32), done);
328 c->chooseleaf_descend_once = ceph_decode_32(p);
329 dout("crush decode tunable chooseleaf_descend_once = %d",
330 c->chooseleaf_descend_once);
331
Sage Weil546f04e2012-07-30 18:15:23 -0700332done:
Sage Weilf24e9982009-10-06 11:31:10 -0700333 dout("crush_decode success\n");
334 return c;
335
336badmem:
337 err = -ENOMEM;
338bad:
339 dout("crush_decode fail %d\n", err);
340 crush_destroy(c);
341 return ERR_PTR(err);
342}
343
Sage Weilf24e9982009-10-06 11:31:10 -0700344/*
Sage Weil9794b142010-02-16 15:53:32 -0800345 * rbtree of pg_mapping for handling pg_temp (explicit mapping of pgid
346 * to a set of osds)
Sage Weilf24e9982009-10-06 11:31:10 -0700347 */
Sage Weil5b191d92013-02-23 10:38:16 -0800348static int pgid_cmp(struct ceph_pg l, struct ceph_pg r)
Sage Weil51042122009-11-04 11:39:12 -0800349{
Sage Weil5b191d92013-02-23 10:38:16 -0800350 if (l.pool < r.pool)
Sage Weil51042122009-11-04 11:39:12 -0800351 return -1;
Sage Weil5b191d92013-02-23 10:38:16 -0800352 if (l.pool > r.pool)
353 return 1;
354 if (l.seed < r.seed)
355 return -1;
356 if (l.seed > r.seed)
Sage Weil51042122009-11-04 11:39:12 -0800357 return 1;
358 return 0;
359}
360
Sage Weil991abb62009-10-08 22:22:37 -0700361static int __insert_pg_mapping(struct ceph_pg_mapping *new,
362 struct rb_root *root)
Sage Weilf24e9982009-10-06 11:31:10 -0700363{
364 struct rb_node **p = &root->rb_node;
365 struct rb_node *parent = NULL;
366 struct ceph_pg_mapping *pg = NULL;
Sage Weil51042122009-11-04 11:39:12 -0800367 int c;
Sage Weilf24e9982009-10-06 11:31:10 -0700368
Sage Weil8adc8b32011-09-28 10:11:04 -0700369 dout("__insert_pg_mapping %llx %p\n", *(u64 *)&new->pgid, new);
Sage Weilf24e9982009-10-06 11:31:10 -0700370 while (*p) {
371 parent = *p;
372 pg = rb_entry(parent, struct ceph_pg_mapping, node);
Sage Weil51042122009-11-04 11:39:12 -0800373 c = pgid_cmp(new->pgid, pg->pgid);
374 if (c < 0)
Sage Weilf24e9982009-10-06 11:31:10 -0700375 p = &(*p)->rb_left;
Sage Weil51042122009-11-04 11:39:12 -0800376 else if (c > 0)
Sage Weilf24e9982009-10-06 11:31:10 -0700377 p = &(*p)->rb_right;
378 else
Sage Weil991abb62009-10-08 22:22:37 -0700379 return -EEXIST;
Sage Weilf24e9982009-10-06 11:31:10 -0700380 }
381
382 rb_link_node(&new->node, parent, p);
383 rb_insert_color(&new->node, root);
Sage Weil991abb62009-10-08 22:22:37 -0700384 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700385}
386
Sage Weil9794b142010-02-16 15:53:32 -0800387static struct ceph_pg_mapping *__lookup_pg_mapping(struct rb_root *root,
Sage Weil5b191d92013-02-23 10:38:16 -0800388 struct ceph_pg pgid)
Sage Weil9794b142010-02-16 15:53:32 -0800389{
390 struct rb_node *n = root->rb_node;
391 struct ceph_pg_mapping *pg;
392 int c;
393
394 while (n) {
395 pg = rb_entry(n, struct ceph_pg_mapping, node);
396 c = pgid_cmp(pgid, pg->pgid);
Sage Weil8adc8b32011-09-28 10:11:04 -0700397 if (c < 0) {
Sage Weil9794b142010-02-16 15:53:32 -0800398 n = n->rb_left;
Sage Weil8adc8b32011-09-28 10:11:04 -0700399 } else if (c > 0) {
Sage Weil9794b142010-02-16 15:53:32 -0800400 n = n->rb_right;
Sage Weil8adc8b32011-09-28 10:11:04 -0700401 } else {
Sage Weil5b191d92013-02-23 10:38:16 -0800402 dout("__lookup_pg_mapping %lld.%x got %p\n",
403 pgid.pool, pgid.seed, pg);
Sage Weil9794b142010-02-16 15:53:32 -0800404 return pg;
Sage Weil8adc8b32011-09-28 10:11:04 -0700405 }
Sage Weil9794b142010-02-16 15:53:32 -0800406 }
407 return NULL;
408}
409
Sage Weil5b191d92013-02-23 10:38:16 -0800410static int __remove_pg_mapping(struct rb_root *root, struct ceph_pg pgid)
Sage Weil8adc8b32011-09-28 10:11:04 -0700411{
412 struct ceph_pg_mapping *pg = __lookup_pg_mapping(root, pgid);
413
414 if (pg) {
Sage Weil5b191d92013-02-23 10:38:16 -0800415 dout("__remove_pg_mapping %lld.%x %p\n", pgid.pool, pgid.seed,
416 pg);
Sage Weil8adc8b32011-09-28 10:11:04 -0700417 rb_erase(&pg->node, root);
418 kfree(pg);
419 return 0;
420 }
Sage Weil5b191d92013-02-23 10:38:16 -0800421 dout("__remove_pg_mapping %lld.%x dne\n", pgid.pool, pgid.seed);
Sage Weil8adc8b32011-09-28 10:11:04 -0700422 return -ENOENT;
423}
424
Sage Weilf24e9982009-10-06 11:31:10 -0700425/*
Sage Weil4fc51be2010-02-16 15:55:03 -0800426 * rbtree of pg pool info
427 */
428static int __insert_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *new)
429{
430 struct rb_node **p = &root->rb_node;
431 struct rb_node *parent = NULL;
432 struct ceph_pg_pool_info *pi = NULL;
433
434 while (*p) {
435 parent = *p;
436 pi = rb_entry(parent, struct ceph_pg_pool_info, node);
437 if (new->id < pi->id)
438 p = &(*p)->rb_left;
439 else if (new->id > pi->id)
440 p = &(*p)->rb_right;
441 else
442 return -EEXIST;
443 }
444
445 rb_link_node(&new->node, parent, p);
446 rb_insert_color(&new->node, root);
447 return 0;
448}
449
Sage Weil4f6a7e52013-02-23 10:41:09 -0800450static struct ceph_pg_pool_info *__lookup_pg_pool(struct rb_root *root, u64 id)
Sage Weil4fc51be2010-02-16 15:55:03 -0800451{
452 struct ceph_pg_pool_info *pi;
453 struct rb_node *n = root->rb_node;
454
455 while (n) {
456 pi = rb_entry(n, struct ceph_pg_pool_info, node);
457 if (id < pi->id)
458 n = n->rb_left;
459 else if (id > pi->id)
460 n = n->rb_right;
461 else
462 return pi;
463 }
464 return NULL;
465}
466
Alex Elder72afc712012-10-30 19:40:33 -0500467const char *ceph_pg_pool_name_by_id(struct ceph_osdmap *map, u64 id)
468{
469 struct ceph_pg_pool_info *pi;
470
471 if (id == CEPH_NOPOOL)
472 return NULL;
473
474 if (WARN_ON_ONCE(id > (u64) INT_MAX))
475 return NULL;
476
477 pi = __lookup_pg_pool(&map->pg_pools, (int) id);
478
479 return pi ? pi->name : NULL;
480}
481EXPORT_SYMBOL(ceph_pg_pool_name_by_id);
482
Yehuda Sadeh7669a2c2010-05-17 12:31:35 -0700483int ceph_pg_poolid_by_name(struct ceph_osdmap *map, const char *name)
484{
485 struct rb_node *rbp;
486
487 for (rbp = rb_first(&map->pg_pools); rbp; rbp = rb_next(rbp)) {
488 struct ceph_pg_pool_info *pi =
489 rb_entry(rbp, struct ceph_pg_pool_info, node);
490 if (pi->name && strcmp(pi->name, name) == 0)
491 return pi->id;
492 }
493 return -ENOENT;
494}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700495EXPORT_SYMBOL(ceph_pg_poolid_by_name);
Yehuda Sadeh7669a2c2010-05-17 12:31:35 -0700496
Sage Weil2844a762010-04-09 15:46:42 -0700497static void __remove_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *pi)
498{
499 rb_erase(&pi->node, root);
500 kfree(pi->name);
501 kfree(pi);
502}
503
Sage Weil73a7e692010-08-02 11:00:55 -0700504static int __decode_pool(void **p, void *end, struct ceph_pg_pool_info *pi)
Sage Weilefd75762010-03-17 10:05:28 -0700505{
Sage Weil4f6a7e52013-02-23 10:41:09 -0800506 u8 ev, cv;
507 unsigned len, num;
508 void *pool_end;
Sage Weil73a7e692010-08-02 11:00:55 -0700509
Sage Weil4f6a7e52013-02-23 10:41:09 -0800510 ceph_decode_need(p, end, 2 + 4, bad);
511 ev = ceph_decode_8(p); /* encoding version */
512 cv = ceph_decode_8(p); /* compat version */
513 if (ev < 5) {
514 pr_warning("got v %d < 5 cv %d of ceph_pg_pool\n", ev, cv);
515 return -EINVAL;
516 }
517 if (cv > 7) {
518 pr_warning("got v %d cv %d > 7 of ceph_pg_pool\n", ev, cv);
519 return -EINVAL;
520 }
521 len = ceph_decode_32(p);
522 ceph_decode_need(p, end, len, bad);
523 pool_end = *p + len;
Sage Weil73a7e692010-08-02 11:00:55 -0700524
Sage Weil4f6a7e52013-02-23 10:41:09 -0800525 pi->type = ceph_decode_8(p);
526 pi->size = ceph_decode_8(p);
527 pi->crush_ruleset = ceph_decode_8(p);
528 pi->object_hash = ceph_decode_8(p);
529
530 pi->pg_num = ceph_decode_32(p);
531 pi->pgp_num = ceph_decode_32(p);
532
533 *p += 4 + 4; /* skip lpg* */
534 *p += 4; /* skip last_change */
535 *p += 8 + 4; /* skip snap_seq, snap_epoch */
536
537 /* skip snaps */
538 num = ceph_decode_32(p);
539 while (num--) {
540 *p += 8; /* snapid key */
541 *p += 1 + 1; /* versions */
542 len = ceph_decode_32(p);
543 *p += len;
Sage Weil73a7e692010-08-02 11:00:55 -0700544 }
545
Sage Weil4f6a7e52013-02-23 10:41:09 -0800546 /* skip removed snaps */
547 num = ceph_decode_32(p);
548 *p += num * (8 + 8);
549
550 *p += 8; /* skip auid */
551 pi->flags = ceph_decode_64(p);
552
553 /* ignore the rest */
554
555 *p = pool_end;
556 calc_pg_masks(pi);
Sage Weil73a7e692010-08-02 11:00:55 -0700557 return 0;
558
559bad:
560 return -EINVAL;
Sage Weilefd75762010-03-17 10:05:28 -0700561}
562
Sage Weil2844a762010-04-09 15:46:42 -0700563static int __decode_pool_names(void **p, void *end, struct ceph_osdmap *map)
564{
565 struct ceph_pg_pool_info *pi;
Sage Weil4f6a7e52013-02-23 10:41:09 -0800566 u32 num, len;
567 u64 pool;
Sage Weil2844a762010-04-09 15:46:42 -0700568
569 ceph_decode_32_safe(p, end, num, bad);
570 dout(" %d pool names\n", num);
571 while (num--) {
Sage Weil4f6a7e52013-02-23 10:41:09 -0800572 ceph_decode_64_safe(p, end, pool, bad);
Sage Weil2844a762010-04-09 15:46:42 -0700573 ceph_decode_32_safe(p, end, len, bad);
Sage Weil4f6a7e52013-02-23 10:41:09 -0800574 dout(" pool %llu len %d\n", pool, len);
Xi Wangad3b9042012-06-06 19:35:55 -0500575 ceph_decode_need(p, end, len, bad);
Sage Weil2844a762010-04-09 15:46:42 -0700576 pi = __lookup_pg_pool(&map->pg_pools, pool);
577 if (pi) {
Xi Wangad3b9042012-06-06 19:35:55 -0500578 char *name = kstrndup(*p, len, GFP_NOFS);
579
580 if (!name)
581 return -ENOMEM;
Sage Weil2844a762010-04-09 15:46:42 -0700582 kfree(pi->name);
Xi Wangad3b9042012-06-06 19:35:55 -0500583 pi->name = name;
584 dout(" name is %s\n", pi->name);
Sage Weil2844a762010-04-09 15:46:42 -0700585 }
586 *p += len;
587 }
588 return 0;
589
590bad:
591 return -EINVAL;
592}
593
594/*
595 * osd map
596 */
597void ceph_osdmap_destroy(struct ceph_osdmap *map)
598{
599 dout("osdmap_destroy %p\n", map);
600 if (map->crush)
601 crush_destroy(map->crush);
602 while (!RB_EMPTY_ROOT(&map->pg_temp)) {
603 struct ceph_pg_mapping *pg =
604 rb_entry(rb_first(&map->pg_temp),
605 struct ceph_pg_mapping, node);
606 rb_erase(&pg->node, &map->pg_temp);
607 kfree(pg);
608 }
609 while (!RB_EMPTY_ROOT(&map->pg_pools)) {
610 struct ceph_pg_pool_info *pi =
611 rb_entry(rb_first(&map->pg_pools),
612 struct ceph_pg_pool_info, node);
613 __remove_pg_pool(&map->pg_pools, pi);
614 }
615 kfree(map->osd_state);
616 kfree(map->osd_weight);
617 kfree(map->osd_addr);
618 kfree(map);
619}
620
621/*
622 * adjust max osd value. reallocate arrays.
623 */
624static int osdmap_set_max_osd(struct ceph_osdmap *map, int max)
625{
626 u8 *state;
627 struct ceph_entity_addr *addr;
628 u32 *weight;
629
630 state = kcalloc(max, sizeof(*state), GFP_NOFS);
631 addr = kcalloc(max, sizeof(*addr), GFP_NOFS);
632 weight = kcalloc(max, sizeof(*weight), GFP_NOFS);
633 if (state == NULL || addr == NULL || weight == NULL) {
634 kfree(state);
635 kfree(addr);
636 kfree(weight);
637 return -ENOMEM;
638 }
639
640 /* copy old? */
641 if (map->osd_state) {
642 memcpy(state, map->osd_state, map->max_osd*sizeof(*state));
643 memcpy(addr, map->osd_addr, map->max_osd*sizeof(*addr));
644 memcpy(weight, map->osd_weight, map->max_osd*sizeof(*weight));
645 kfree(map->osd_state);
646 kfree(map->osd_addr);
647 kfree(map->osd_weight);
648 }
649
650 map->osd_state = state;
651 map->osd_weight = weight;
652 map->osd_addr = addr;
653 map->max_osd = max;
654 return 0;
655}
656
Sage Weild6c0dd62013-03-06 14:57:03 -0800657static int __decode_pgid(void **p, void *end, struct ceph_pg *pg)
658{
659 u8 v;
660
661 ceph_decode_need(p, end, 1+8+4+4, bad);
662 v = ceph_decode_8(p);
663 if (v != 1)
664 goto bad;
665 pg->pool = ceph_decode_64(p);
666 pg->seed = ceph_decode_32(p);
667 *p += 4; /* skip preferred */
668 return 0;
669
670bad:
671 dout("error decoding pgid\n");
672 return -EINVAL;
673}
674
Sage Weil4fc51be2010-02-16 15:55:03 -0800675/*
Sage Weilf24e9982009-10-06 11:31:10 -0700676 * decode a full map.
677 */
678struct ceph_osdmap *osdmap_decode(void **p, void *end)
679{
680 struct ceph_osdmap *map;
681 u16 version;
682 u32 len, max, i;
683 int err = -EINVAL;
684 void *start = *p;
Sage Weil4fc51be2010-02-16 15:55:03 -0800685 struct ceph_pg_pool_info *pi;
Sage Weilf24e9982009-10-06 11:31:10 -0700686
687 dout("osdmap_decode %p to %p len %d\n", *p, end, (int)(end - *p));
688
689 map = kzalloc(sizeof(*map), GFP_NOFS);
690 if (map == NULL)
691 return ERR_PTR(-ENOMEM);
692 map->pg_temp = RB_ROOT;
693
694 ceph_decode_16_safe(p, end, version, bad);
Sage Weil4f6a7e52013-02-23 10:41:09 -0800695 if (version > 6) {
696 pr_warning("got unknown v %d > 6 of osdmap\n", version);
697 goto bad;
698 }
699 if (version < 6) {
700 pr_warning("got old v %d < 6 of osdmap\n", version);
Sage Weil02f90c62010-02-04 16:18:10 -0800701 goto bad;
702 }
Sage Weilf24e9982009-10-06 11:31:10 -0700703
704 ceph_decode_need(p, end, 2*sizeof(u64)+6*sizeof(u32), bad);
705 ceph_decode_copy(p, &map->fsid, sizeof(map->fsid));
Sage Weilc89136e2009-10-14 09:59:09 -0700706 map->epoch = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700707 ceph_decode_copy(p, &map->created, sizeof(map->created));
708 ceph_decode_copy(p, &map->modified, sizeof(map->modified));
709
Sage Weilf24e9982009-10-06 11:31:10 -0700710 ceph_decode_32_safe(p, end, max, bad);
711 while (max--) {
Sage Weil4f6a7e52013-02-23 10:41:09 -0800712 ceph_decode_need(p, end, 8 + 2, bad);
Sage Weil0ed72852012-10-29 11:01:42 -0700713 err = -ENOMEM;
Sage Weil2844a762010-04-09 15:46:42 -0700714 pi = kzalloc(sizeof(*pi), GFP_NOFS);
Sage Weil4fc51be2010-02-16 15:55:03 -0800715 if (!pi)
Sage Weilf24e9982009-10-06 11:31:10 -0700716 goto bad;
Sage Weil4f6a7e52013-02-23 10:41:09 -0800717 pi->id = ceph_decode_64(p);
Sage Weil73a7e692010-08-02 11:00:55 -0700718 err = __decode_pool(p, end, pi);
Jesper Juhlb0aee352010-12-24 23:01:12 +0100719 if (err < 0) {
720 kfree(pi);
Sage Weil73a7e692010-08-02 11:00:55 -0700721 goto bad;
Jesper Juhlb0aee352010-12-24 23:01:12 +0100722 }
Sage Weil4fc51be2010-02-16 15:55:03 -0800723 __insert_pg_pool(&map->pg_pools, pi);
Sage Weilf24e9982009-10-06 11:31:10 -0700724 }
Sage Weil2844a762010-04-09 15:46:42 -0700725
Sage Weil4f6a7e52013-02-23 10:41:09 -0800726 err = __decode_pool_names(p, end, map);
727 if (err < 0) {
728 dout("fail to decode pool names");
729 goto bad;
Sage Weil0ed72852012-10-29 11:01:42 -0700730 }
Sage Weil2844a762010-04-09 15:46:42 -0700731
Sage Weil4fc51be2010-02-16 15:55:03 -0800732 ceph_decode_32_safe(p, end, map->pool_max, bad);
Sage Weilf24e9982009-10-06 11:31:10 -0700733
734 ceph_decode_32_safe(p, end, map->flags, bad);
735
Sage Weilc89136e2009-10-14 09:59:09 -0700736 max = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700737
738 /* (re)alloc osd arrays */
739 err = osdmap_set_max_osd(map, max);
740 if (err < 0)
741 goto bad;
742 dout("osdmap_decode max_osd = %d\n", map->max_osd);
743
744 /* osds */
745 err = -EINVAL;
746 ceph_decode_need(p, end, 3*sizeof(u32) +
747 map->max_osd*(1 + sizeof(*map->osd_weight) +
748 sizeof(*map->osd_addr)), bad);
749 *p += 4; /* skip length field (should match max) */
750 ceph_decode_copy(p, map->osd_state, map->max_osd);
751
752 *p += 4; /* skip length field (should match max) */
753 for (i = 0; i < map->max_osd; i++)
Sage Weilc89136e2009-10-14 09:59:09 -0700754 map->osd_weight[i] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700755
756 *p += 4; /* skip length field (should match max) */
757 ceph_decode_copy(p, map->osd_addr, map->max_osd*sizeof(*map->osd_addr));
Sage Weil63f2d212009-11-03 15:17:56 -0800758 for (i = 0; i < map->max_osd; i++)
759 ceph_decode_addr(&map->osd_addr[i]);
Sage Weilf24e9982009-10-06 11:31:10 -0700760
761 /* pg_temp */
762 ceph_decode_32_safe(p, end, len, bad);
763 for (i = 0; i < len; i++) {
764 int n, j;
Sage Weil5b191d92013-02-23 10:38:16 -0800765 struct ceph_pg pgid;
Sage Weilf24e9982009-10-06 11:31:10 -0700766 struct ceph_pg_mapping *pg;
767
Sage Weild6c0dd62013-03-06 14:57:03 -0800768 err = __decode_pgid(p, end, &pgid);
769 if (err)
770 goto bad;
771 ceph_decode_need(p, end, sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700772 n = ceph_decode_32(p);
Xi Wange91a9b62012-06-06 19:35:55 -0500773 err = -EINVAL;
774 if (n > (UINT_MAX - sizeof(*pg)) / sizeof(u32))
775 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -0700776 ceph_decode_need(p, end, n * sizeof(u32), bad);
Sage Weil30dc6382009-12-21 14:49:37 -0800777 err = -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -0700778 pg = kmalloc(sizeof(*pg) + n*sizeof(u32), GFP_NOFS);
Sage Weil30dc6382009-12-21 14:49:37 -0800779 if (!pg)
Sage Weilf24e9982009-10-06 11:31:10 -0700780 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -0700781 pg->pgid = pgid;
782 pg->len = n;
783 for (j = 0; j < n; j++)
Sage Weilc89136e2009-10-14 09:59:09 -0700784 pg->osds[j] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700785
Sage Weil991abb62009-10-08 22:22:37 -0700786 err = __insert_pg_mapping(pg, &map->pg_temp);
787 if (err)
788 goto bad;
Sage Weil5b191d92013-02-23 10:38:16 -0800789 dout(" added pg_temp %lld.%x len %d\n", pgid.pool, pgid.seed,
790 len);
Sage Weilf24e9982009-10-06 11:31:10 -0700791 }
792
793 /* crush */
794 ceph_decode_32_safe(p, end, len, bad);
795 dout("osdmap_decode crush len %d from off 0x%x\n", len,
796 (int)(*p - start));
797 ceph_decode_need(p, end, len, bad);
798 map->crush = crush_decode(*p, end);
799 *p += len;
800 if (IS_ERR(map->crush)) {
801 err = PTR_ERR(map->crush);
802 map->crush = NULL;
803 goto bad;
804 }
805
806 /* ignore the rest of the map */
807 *p = end;
808
809 dout("osdmap_decode done %p %p\n", *p, end);
810 return map;
811
812bad:
Sage Weil0ed72852012-10-29 11:01:42 -0700813 dout("osdmap_decode fail err %d\n", err);
Sage Weilf24e9982009-10-06 11:31:10 -0700814 ceph_osdmap_destroy(map);
815 return ERR_PTR(err);
816}
817
818/*
819 * decode and apply an incremental map update.
820 */
821struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end,
822 struct ceph_osdmap *map,
823 struct ceph_messenger *msgr)
824{
Sage Weilf24e9982009-10-06 11:31:10 -0700825 struct crush_map *newcrush = NULL;
826 struct ceph_fsid fsid;
827 u32 epoch = 0;
828 struct ceph_timespec modified;
Sage Weil4f6a7e52013-02-23 10:41:09 -0800829 s32 len;
830 u64 pool;
831 __s64 new_pool_max;
832 __s32 new_flags, max;
Sage Weilf24e9982009-10-06 11:31:10 -0700833 void *start = *p;
834 int err = -EINVAL;
835 u16 version;
Sage Weilf24e9982009-10-06 11:31:10 -0700836
837 ceph_decode_16_safe(p, end, version, bad);
Sage Weild6c0dd62013-03-06 14:57:03 -0800838 if (version != 6) {
839 pr_warning("got unknown v %d != 6 of inc osdmap\n", version);
Sage Weil02f90c62010-02-04 16:18:10 -0800840 goto bad;
841 }
Sage Weilf24e9982009-10-06 11:31:10 -0700842
843 ceph_decode_need(p, end, sizeof(fsid)+sizeof(modified)+2*sizeof(u32),
844 bad);
845 ceph_decode_copy(p, &fsid, sizeof(fsid));
Sage Weilc89136e2009-10-14 09:59:09 -0700846 epoch = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700847 BUG_ON(epoch != map->epoch+1);
848 ceph_decode_copy(p, &modified, sizeof(modified));
Sage Weil4f6a7e52013-02-23 10:41:09 -0800849 new_pool_max = ceph_decode_64(p);
Sage Weilc89136e2009-10-14 09:59:09 -0700850 new_flags = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700851
852 /* full map? */
853 ceph_decode_32_safe(p, end, len, bad);
854 if (len > 0) {
855 dout("apply_incremental full map len %d, %p to %p\n",
856 len, *p, end);
Sage Weil30dc6382009-12-21 14:49:37 -0800857 return osdmap_decode(p, min(*p+len, end));
Sage Weilf24e9982009-10-06 11:31:10 -0700858 }
859
860 /* new crush? */
861 ceph_decode_32_safe(p, end, len, bad);
862 if (len > 0) {
863 dout("apply_incremental new crush map len %d, %p to %p\n",
864 len, *p, end);
865 newcrush = crush_decode(*p, min(*p+len, end));
866 if (IS_ERR(newcrush))
Julia Lawall7e34bc52010-05-22 12:01:14 +0200867 return ERR_CAST(newcrush);
Sage Weilcebc5be2010-06-17 10:22:48 -0700868 *p += len;
Sage Weilf24e9982009-10-06 11:31:10 -0700869 }
870
871 /* new flags? */
872 if (new_flags >= 0)
873 map->flags = new_flags;
Sage Weil4fc51be2010-02-16 15:55:03 -0800874 if (new_pool_max >= 0)
875 map->pool_max = new_pool_max;
Sage Weilf24e9982009-10-06 11:31:10 -0700876
877 ceph_decode_need(p, end, 5*sizeof(u32), bad);
878
879 /* new max? */
Sage Weilc89136e2009-10-14 09:59:09 -0700880 max = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700881 if (max >= 0) {
882 err = osdmap_set_max_osd(map, max);
883 if (err < 0)
884 goto bad;
885 }
886
887 map->epoch++;
Sage Weil31456662011-05-12 15:18:43 -0700888 map->modified = modified;
Sage Weilf24e9982009-10-06 11:31:10 -0700889 if (newcrush) {
890 if (map->crush)
891 crush_destroy(map->crush);
892 map->crush = newcrush;
893 newcrush = NULL;
894 }
895
896 /* new_pool */
897 ceph_decode_32_safe(p, end, len, bad);
898 while (len--) {
Sage Weil4fc51be2010-02-16 15:55:03 -0800899 struct ceph_pg_pool_info *pi;
Sage Weil361be862010-01-25 16:03:02 -0800900
Sage Weil4f6a7e52013-02-23 10:41:09 -0800901 ceph_decode_64_safe(p, end, pool, bad);
Sage Weil4fc51be2010-02-16 15:55:03 -0800902 pi = __lookup_pg_pool(&map->pg_pools, pool);
903 if (!pi) {
Sage Weil2844a762010-04-09 15:46:42 -0700904 pi = kzalloc(sizeof(*pi), GFP_NOFS);
Sage Weil4fc51be2010-02-16 15:55:03 -0800905 if (!pi) {
906 err = -ENOMEM;
907 goto bad;
908 }
909 pi->id = pool;
910 __insert_pg_pool(&map->pg_pools, pi);
911 }
Sage Weil73a7e692010-08-02 11:00:55 -0700912 err = __decode_pool(p, end, pi);
913 if (err < 0)
914 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -0700915 }
Sage Weil0ed72852012-10-29 11:01:42 -0700916 if (version >= 5) {
917 err = __decode_pool_names(p, end, map);
918 if (err < 0)
919 goto bad;
920 }
Sage Weilf24e9982009-10-06 11:31:10 -0700921
Sage Weil4fc51be2010-02-16 15:55:03 -0800922 /* old_pool */
Sage Weilf24e9982009-10-06 11:31:10 -0700923 ceph_decode_32_safe(p, end, len, bad);
Sage Weil4fc51be2010-02-16 15:55:03 -0800924 while (len--) {
925 struct ceph_pg_pool_info *pi;
926
Sage Weil4f6a7e52013-02-23 10:41:09 -0800927 ceph_decode_64_safe(p, end, pool, bad);
Sage Weil4fc51be2010-02-16 15:55:03 -0800928 pi = __lookup_pg_pool(&map->pg_pools, pool);
Sage Weil2844a762010-04-09 15:46:42 -0700929 if (pi)
930 __remove_pg_pool(&map->pg_pools, pi);
Sage Weil4fc51be2010-02-16 15:55:03 -0800931 }
Sage Weilf24e9982009-10-06 11:31:10 -0700932
933 /* new_up */
934 err = -EINVAL;
935 ceph_decode_32_safe(p, end, len, bad);
936 while (len--) {
937 u32 osd;
938 struct ceph_entity_addr addr;
939 ceph_decode_32_safe(p, end, osd, bad);
940 ceph_decode_copy_safe(p, end, &addr, sizeof(addr), bad);
Sage Weil63f2d212009-11-03 15:17:56 -0800941 ceph_decode_addr(&addr);
Sage Weilf24e9982009-10-06 11:31:10 -0700942 pr_info("osd%d up\n", osd);
943 BUG_ON(osd >= map->max_osd);
944 map->osd_state[osd] |= CEPH_OSD_UP;
945 map->osd_addr[osd] = addr;
946 }
947
Sage Weil7662d8f2011-05-03 12:52:05 -0700948 /* new_state */
Sage Weilf24e9982009-10-06 11:31:10 -0700949 ceph_decode_32_safe(p, end, len, bad);
950 while (len--) {
951 u32 osd;
Sage Weil7662d8f2011-05-03 12:52:05 -0700952 u8 xorstate;
Sage Weilf24e9982009-10-06 11:31:10 -0700953 ceph_decode_32_safe(p, end, osd, bad);
Sage Weil7662d8f2011-05-03 12:52:05 -0700954 xorstate = **(u8 **)p;
Sage Weilf24e9982009-10-06 11:31:10 -0700955 (*p)++; /* clean flag */
Sage Weil7662d8f2011-05-03 12:52:05 -0700956 if (xorstate == 0)
957 xorstate = CEPH_OSD_UP;
958 if (xorstate & CEPH_OSD_UP)
959 pr_info("osd%d down\n", osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700960 if (osd < map->max_osd)
Sage Weil7662d8f2011-05-03 12:52:05 -0700961 map->osd_state[osd] ^= xorstate;
Sage Weilf24e9982009-10-06 11:31:10 -0700962 }
963
964 /* new_weight */
965 ceph_decode_32_safe(p, end, len, bad);
966 while (len--) {
967 u32 osd, off;
968 ceph_decode_need(p, end, sizeof(u32)*2, bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700969 osd = ceph_decode_32(p);
970 off = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700971 pr_info("osd%d weight 0x%x %s\n", osd, off,
972 off == CEPH_OSD_IN ? "(in)" :
973 (off == CEPH_OSD_OUT ? "(out)" : ""));
974 if (osd < map->max_osd)
975 map->osd_weight[osd] = off;
976 }
977
978 /* new_pg_temp */
Sage Weilf24e9982009-10-06 11:31:10 -0700979 ceph_decode_32_safe(p, end, len, bad);
980 while (len--) {
981 struct ceph_pg_mapping *pg;
982 int j;
Sage Weil5b191d92013-02-23 10:38:16 -0800983 struct ceph_pg pgid;
Sage Weilf24e9982009-10-06 11:31:10 -0700984 u32 pglen;
Sage Weilf24e9982009-10-06 11:31:10 -0700985
Sage Weild6c0dd62013-03-06 14:57:03 -0800986 err = __decode_pgid(p, end, &pgid);
987 if (err)
988 goto bad;
989 ceph_decode_need(p, end, sizeof(u32), bad);
990 pglen = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700991 if (pglen) {
Sage Weilf24e9982009-10-06 11:31:10 -0700992 ceph_decode_need(p, end, pglen*sizeof(u32), bad);
Sage Weil6bd9adb2012-05-21 09:45:23 -0700993
994 /* removing existing (if any) */
995 (void) __remove_pg_mapping(&map->pg_temp, pgid);
996
997 /* insert */
Sage Weil0ed72852012-10-29 11:01:42 -0700998 err = -EINVAL;
999 if (pglen > (UINT_MAX - sizeof(*pg)) / sizeof(u32))
Xi Wanga5506042012-06-06 19:35:55 -05001000 goto bad;
Sage Weil0ed72852012-10-29 11:01:42 -07001001 err = -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07001002 pg = kmalloc(sizeof(*pg) + sizeof(u32)*pglen, GFP_NOFS);
Sage Weil0ed72852012-10-29 11:01:42 -07001003 if (!pg)
Sage Weilf24e9982009-10-06 11:31:10 -07001004 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001005 pg->pgid = pgid;
1006 pg->len = pglen;
Sage Weil7067f792009-12-21 16:02:37 -08001007 for (j = 0; j < pglen; j++)
Sage Weilc89136e2009-10-14 09:59:09 -07001008 pg->osds[j] = ceph_decode_32(p);
Sage Weil991abb62009-10-08 22:22:37 -07001009 err = __insert_pg_mapping(pg, &map->pg_temp);
Sage Weilbc4fdca2010-07-20 16:19:56 -07001010 if (err) {
1011 kfree(pg);
Sage Weil991abb62009-10-08 22:22:37 -07001012 goto bad;
Sage Weilbc4fdca2010-07-20 16:19:56 -07001013 }
Sage Weil5b191d92013-02-23 10:38:16 -08001014 dout(" added pg_temp %lld.%x len %d\n", pgid.pool,
1015 pgid.seed, pglen);
Sage Weil8adc8b32011-09-28 10:11:04 -07001016 } else {
1017 /* remove */
1018 __remove_pg_mapping(&map->pg_temp, pgid);
Sage Weilf24e9982009-10-06 11:31:10 -07001019 }
1020 }
Sage Weilf24e9982009-10-06 11:31:10 -07001021
1022 /* ignore the rest */
1023 *p = end;
1024 return map;
1025
1026bad:
1027 pr_err("corrupt inc osdmap epoch %d off %d (%p of %p-%p)\n",
1028 epoch, (int)(*p - start), *p, start, end);
Sage Weil9ec7cab2009-12-14 15:13:47 -08001029 print_hex_dump(KERN_DEBUG, "osdmap: ",
1030 DUMP_PREFIX_OFFSET, 16, 1,
1031 start, end - start, true);
Sage Weilf24e9982009-10-06 11:31:10 -07001032 if (newcrush)
1033 crush_destroy(newcrush);
1034 return ERR_PTR(err);
1035}
1036
1037
1038
1039
1040/*
1041 * calculate file layout from given offset, length.
1042 * fill in correct oid, logical length, and object extent
1043 * offset, length.
1044 *
1045 * for now, we write only a single su, until we can
1046 * pass a stride back to the caller.
1047 */
Sage Weild63b77f2012-09-24 20:59:48 -07001048int ceph_calc_file_object_mapping(struct ceph_file_layout *layout,
Alex Eldere8afad62012-11-14 09:38:19 -06001049 u64 off, u64 len,
Sage Weil645a1022009-10-28 15:15:05 -07001050 u64 *ono,
Sage Weilf24e9982009-10-06 11:31:10 -07001051 u64 *oxoff, u64 *oxlen)
1052{
1053 u32 osize = le32_to_cpu(layout->fl_object_size);
1054 u32 su = le32_to_cpu(layout->fl_stripe_unit);
1055 u32 sc = le32_to_cpu(layout->fl_stripe_count);
1056 u32 bl, stripeno, stripepos, objsetno;
1057 u32 su_per_object;
Noah Watkinsff1d1f72009-10-30 12:57:30 -07001058 u64 t, su_offset;
Sage Weilf24e9982009-10-06 11:31:10 -07001059
Alex Eldere8afad62012-11-14 09:38:19 -06001060 dout("mapping %llu~%llu osize %u fl_su %u\n", off, len,
Sage Weilf24e9982009-10-06 11:31:10 -07001061 osize, su);
Sage Weild63b77f2012-09-24 20:59:48 -07001062 if (su == 0 || sc == 0)
1063 goto invalid;
Noah Watkins35e054a2009-10-28 14:04:48 -07001064 su_per_object = osize / su;
Sage Weild63b77f2012-09-24 20:59:48 -07001065 if (su_per_object == 0)
1066 goto invalid;
Sage Weilf24e9982009-10-06 11:31:10 -07001067 dout("osize %u / su %u = su_per_object %u\n", osize, su,
1068 su_per_object);
1069
Sage Weild63b77f2012-09-24 20:59:48 -07001070 if ((su & ~PAGE_MASK) != 0)
1071 goto invalid;
1072
Sage Weilf24e9982009-10-06 11:31:10 -07001073 /* bl = *off / su; */
1074 t = off;
1075 do_div(t, su);
1076 bl = t;
1077 dout("off %llu / su %u = bl %u\n", off, su, bl);
1078
1079 stripeno = bl / sc;
1080 stripepos = bl % sc;
1081 objsetno = stripeno / su_per_object;
1082
Sage Weil645a1022009-10-28 15:15:05 -07001083 *ono = objsetno * sc + stripepos;
Eric Dumazet95c96172012-04-15 05:58:06 +00001084 dout("objset %u * sc %u = ono %u\n", objsetno, sc, (unsigned int)*ono);
Sage Weil645a1022009-10-28 15:15:05 -07001085
1086 /* *oxoff = *off % layout->fl_stripe_unit; # offset in su */
Sage Weilf24e9982009-10-06 11:31:10 -07001087 t = off;
Noah Watkinsff1d1f72009-10-30 12:57:30 -07001088 su_offset = do_div(t, su);
1089 *oxoff = su_offset + (stripeno % su_per_object) * su;
Sage Weil645a1022009-10-28 15:15:05 -07001090
Noah Watkinsff1d1f72009-10-30 12:57:30 -07001091 /*
1092 * Calculate the length of the extent being written to the selected
Alex Eldere8afad62012-11-14 09:38:19 -06001093 * object. This is the minimum of the full length requested (len) or
Noah Watkinsff1d1f72009-10-30 12:57:30 -07001094 * the remainder of the current stripe being written to.
1095 */
Alex Eldere8afad62012-11-14 09:38:19 -06001096 *oxlen = min_t(u64, len, su - su_offset);
Sage Weilf24e9982009-10-06 11:31:10 -07001097
1098 dout(" obj extent %llu~%llu\n", *oxoff, *oxlen);
Sage Weild63b77f2012-09-24 20:59:48 -07001099 return 0;
1100
1101invalid:
1102 dout(" invalid layout\n");
1103 *ono = 0;
1104 *oxoff = 0;
1105 *oxlen = 0;
1106 return -EINVAL;
Sage Weilf24e9982009-10-06 11:31:10 -07001107}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001108EXPORT_SYMBOL(ceph_calc_file_object_mapping);
Sage Weilf24e9982009-10-06 11:31:10 -07001109
1110/*
1111 * calculate an object layout (i.e. pgid) from an oid,
1112 * file_layout, and osdmap
1113 */
Sage Weil2169aea2013-02-25 16:13:08 -08001114int ceph_calc_object_layout(struct ceph_pg *pg,
Sage Weilf24e9982009-10-06 11:31:10 -07001115 const char *oid,
1116 struct ceph_file_layout *fl,
1117 struct ceph_osdmap *osdmap)
1118{
Eric Dumazet95c96172012-04-15 05:58:06 +00001119 unsigned int num, num_mask;
Sage Weilf24e9982009-10-06 11:31:10 -07001120 struct ceph_pg_pool_info *pool;
1121
Sage Weil30dc6382009-12-21 14:49:37 -08001122 BUG_ON(!osdmap);
Sage Weil2169aea2013-02-25 16:13:08 -08001123 pg->pool = le32_to_cpu(fl->fl_pg_pool);
1124 pool = __lookup_pg_pool(&osdmap->pg_pools, pg->pool);
Sage Weil4fc51be2010-02-16 15:55:03 -08001125 if (!pool)
1126 return -EIO;
Sage Weil2169aea2013-02-25 16:13:08 -08001127 pg->seed = ceph_str_hash(pool->object_hash, oid, strlen(oid));
Sage Weil4f6a7e52013-02-23 10:41:09 -08001128 num = pool->pg_num;
Sage Weil3469ac1a2012-05-07 15:33:36 -07001129 num_mask = pool->pg_num_mask;
Sage Weilf24e9982009-10-06 11:31:10 -07001130
Sage Weil2169aea2013-02-25 16:13:08 -08001131 dout("calc_object_layout '%s' pgid %lld.%x\n", oid, pg->pool, pg->seed);
Sage Weilf24e9982009-10-06 11:31:10 -07001132 return 0;
1133}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001134EXPORT_SYMBOL(ceph_calc_object_layout);
Sage Weilf24e9982009-10-06 11:31:10 -07001135
1136/*
1137 * Calculate raw osd vector for the given pgid. Return pointer to osd
1138 * array, or NULL on failure.
1139 */
Sage Weil5b191d92013-02-23 10:38:16 -08001140static int *calc_pg_raw(struct ceph_osdmap *osdmap, struct ceph_pg pgid,
Sage Weilf24e9982009-10-06 11:31:10 -07001141 int *osds, int *num)
1142{
Sage Weilf24e9982009-10-06 11:31:10 -07001143 struct ceph_pg_mapping *pg;
1144 struct ceph_pg_pool_info *pool;
1145 int ruleno;
Sage Weil83ca14f2013-02-26 10:39:09 -08001146 int r;
1147 u32 pps;
Sage Weilf24e9982009-10-06 11:31:10 -07001148
Sage Weil83ca14f2013-02-26 10:39:09 -08001149 pool = __lookup_pg_pool(&osdmap->pg_pools, pgid.pool);
Sage Weil782e1822011-09-28 10:08:27 -07001150 if (!pool)
1151 return NULL;
1152
Sage Weilf24e9982009-10-06 11:31:10 -07001153 /* pg_temp? */
Sage Weil83ca14f2013-02-26 10:39:09 -08001154 pgid.seed = ceph_stable_mod(pgid.seed, pool->pg_num,
1155 pool->pgp_num_mask);
Sage Weil9794b142010-02-16 15:53:32 -08001156 pg = __lookup_pg_mapping(&osdmap->pg_temp, pgid);
1157 if (pg) {
1158 *num = pg->len;
1159 return pg->osds;
Sage Weilf24e9982009-10-06 11:31:10 -07001160 }
1161
1162 /* crush */
Sage Weil4f6a7e52013-02-23 10:41:09 -08001163 ruleno = crush_find_rule(osdmap->crush, pool->crush_ruleset,
1164 pool->type, pool->size);
Sage Weilf24e9982009-10-06 11:31:10 -07001165 if (ruleno < 0) {
Sage Weil83ca14f2013-02-26 10:39:09 -08001166 pr_err("no crush rule pool %lld ruleset %d type %d size %d\n",
1167 pgid.pool, pool->crush_ruleset, pool->type,
Sage Weil4f6a7e52013-02-23 10:41:09 -08001168 pool->size);
Sage Weilf24e9982009-10-06 11:31:10 -07001169 return NULL;
1170 }
1171
Sage Weil83ca14f2013-02-26 10:39:09 -08001172 if (pool->flags & CEPH_POOL_FLAG_HASHPSPOOL) {
1173 /* hash pool id and seed sothat pool PGs do not overlap */
1174 pps = crush_hash32_2(CRUSH_HASH_RJENKINS1,
1175 ceph_stable_mod(pgid.seed, pool->pgp_num,
1176 pool->pgp_num_mask),
1177 pgid.pool);
1178 } else {
1179 /*
1180 * legacy ehavior: add ps and pool together. this is
1181 * not a great approach because the PGs from each pool
1182 * will overlap on top of each other: 0.5 == 1.4 ==
1183 * 2.3 == ...
1184 */
1185 pps = ceph_stable_mod(pgid.seed, pool->pgp_num,
1186 pool->pgp_num_mask) +
1187 (unsigned)pgid.pool;
1188 }
Sage Weil8b393262012-05-07 15:37:23 -07001189 r = crush_do_rule(osdmap->crush, ruleno, pps, osds,
Sage Weil4f6a7e52013-02-23 10:41:09 -08001190 min_t(int, pool->size, *num),
Sage Weil8b393262012-05-07 15:37:23 -07001191 osdmap->osd_weight);
1192 if (r < 0) {
Sage Weil83ca14f2013-02-26 10:39:09 -08001193 pr_err("error %d from crush rule: pool %lld ruleset %d type %d"
1194 " size %d\n", r, pgid.pool, pool->crush_ruleset,
Sage Weil4f6a7e52013-02-23 10:41:09 -08001195 pool->type, pool->size);
Sage Weil8b393262012-05-07 15:37:23 -07001196 return NULL;
1197 }
1198 *num = r;
Sage Weilf24e9982009-10-06 11:31:10 -07001199 return osds;
1200}
1201
1202/*
Sage Weild85b7052010-05-10 10:24:48 -07001203 * Return acting set for given pgid.
1204 */
Sage Weil5b191d92013-02-23 10:38:16 -08001205int ceph_calc_pg_acting(struct ceph_osdmap *osdmap, struct ceph_pg pgid,
Sage Weild85b7052010-05-10 10:24:48 -07001206 int *acting)
1207{
1208 int rawosds[CEPH_PG_MAX_SIZE], *osds;
1209 int i, o, num = CEPH_PG_MAX_SIZE;
1210
1211 osds = calc_pg_raw(osdmap, pgid, rawosds, &num);
1212 if (!osds)
1213 return -1;
1214
1215 /* primary is first up osd */
1216 o = 0;
1217 for (i = 0; i < num; i++)
1218 if (ceph_osd_is_up(osdmap, osds[i]))
1219 acting[o++] = osds[i];
1220 return o;
1221}
1222
1223/*
Sage Weilf24e9982009-10-06 11:31:10 -07001224 * Return primary osd for given pgid, or -1 if none.
1225 */
Sage Weil5b191d92013-02-23 10:38:16 -08001226int ceph_calc_pg_primary(struct ceph_osdmap *osdmap, struct ceph_pg pgid)
Sage Weilf24e9982009-10-06 11:31:10 -07001227{
Sage Weild85b7052010-05-10 10:24:48 -07001228 int rawosds[CEPH_PG_MAX_SIZE], *osds;
1229 int i, num = CEPH_PG_MAX_SIZE;
Sage Weilf24e9982009-10-06 11:31:10 -07001230
1231 osds = calc_pg_raw(osdmap, pgid, rawosds, &num);
1232 if (!osds)
1233 return -1;
1234
1235 /* primary is first up osd */
1236 for (i = 0; i < num; i++)
Sage Weild85b7052010-05-10 10:24:48 -07001237 if (ceph_osd_is_up(osdmap, osds[i]))
Sage Weilf24e9982009-10-06 11:31:10 -07001238 return osds[i];
Sage Weilf24e9982009-10-06 11:31:10 -07001239 return -1;
1240}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001241EXPORT_SYMBOL(ceph_calc_pg_primary);