Commit | Line | Data |
---|---|---|
f24e9980 | 1 | |
3d14c5d2 | 2 | #include <linux/ceph/ceph_debug.h> |
5a0e3ad6 | 3 | |
3d14c5d2 | 4 | #include <linux/module.h> |
5a0e3ad6 | 5 | #include <linux/slab.h> |
f24e9980 SW |
6 | #include <asm/div64.h> |
7 | ||
3d14c5d2 YS |
8 | #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> | |
f24e9980 SW |
13 | |
14 | char *ceph_osdmap_state_str(char *str, int len, int state) | |
15 | { | |
f24e9980 | 16 | if (!len) |
1ec3911d | 17 | return str; |
f24e9980 | 18 | |
1ec3911d CD |
19 | 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 | |
f24e9980 | 26 | snprintf(str, len, "doesn't exist"); |
1ec3911d | 27 | |
f24e9980 SW |
28 | return str; |
29 | } | |
30 | ||
31 | /* maps */ | |
32 | ||
95c96174 | 33 | static int calc_bits_of(unsigned int t) |
f24e9980 SW |
34 | { |
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 | */ | |
46 | static void calc_pg_masks(struct ceph_pg_pool_info *pi) | |
47 | { | |
4f6a7e5e SW |
48 | 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; | |
f24e9980 SW |
50 | } |
51 | ||
52 | /* | |
53 | * decode crush map | |
54 | */ | |
55 | static 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); | |
c89136ea | 60 | b->item_weight = ceph_decode_32(p); |
f24e9980 SW |
61 | return 0; |
62 | bad: | |
63 | return -EINVAL; | |
64 | } | |
65 | ||
66 | static 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++) { | |
c89136ea SW |
79 | b->item_weights[j] = ceph_decode_32(p); |
80 | b->sum_weights[j] = ceph_decode_32(p); | |
f24e9980 SW |
81 | } |
82 | return 0; | |
83 | bad: | |
84 | return -EINVAL; | |
85 | } | |
86 | ||
87 | static 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++) | |
c89136ea | 98 | b->node_weights[j] = ceph_decode_32(p); |
f24e9980 SW |
99 | return 0; |
100 | bad: | |
101 | return -EINVAL; | |
102 | } | |
103 | ||
104 | static 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++) { | |
c89136ea SW |
117 | b->item_weights[j] = ceph_decode_32(p); |
118 | b->straws[j] = ceph_decode_32(p); | |
f24e9980 SW |
119 | } |
120 | return 0; | |
121 | bad: | |
122 | return -EINVAL; | |
123 | } | |
124 | ||
546f04ef SW |
125 | static 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; | |
136 | bad: | |
137 | return -EINVAL; | |
138 | } | |
139 | ||
f24e9980 SW |
140 | static 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; | |
546f04ef | 148 | u32 num_name_maps; |
f24e9980 SW |
149 | |
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 | ||
546f04ef SW |
156 | /* set tunables to default values */ |
157 | c->choose_local_tries = 2; | |
158 | c->choose_local_fallback_tries = 5; | |
159 | c->choose_total_tries = 19; | |
1604f488 | 160 | c->chooseleaf_descend_once = 0; |
546f04ef | 161 | |
f24e9980 | 162 | ceph_decode_need(p, end, 4*sizeof(u32), bad); |
c89136ea | 163 | magic = ceph_decode_32(p); |
f24e9980 SW |
164 | if (magic != CRUSH_MAGIC) { |
165 | pr_err("crush_decode magic %x != current %x\n", | |
95c96174 | 166 | (unsigned int)magic, (unsigned int)CRUSH_MAGIC); |
f24e9980 SW |
167 | goto bad; |
168 | } | |
c89136ea SW |
169 | c->max_buckets = ceph_decode_32(p); |
170 | c->max_rules = ceph_decode_32(p); | |
171 | c->max_devices = ceph_decode_32(p); | |
f24e9980 | 172 | |
f24e9980 SW |
173 | 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: | |
30dc6381 | 208 | err = -EINVAL; |
f24e9980 SW |
209 | 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); | |
c89136ea SW |
217 | b->id = ceph_decode_32(p); |
218 | b->type = ceph_decode_16(p); | |
fb690390 SW |
219 | b->alg = ceph_decode_8(p); |
220 | b->hash = ceph_decode_8(p); | |
c89136ea SW |
221 | b->weight = ceph_decode_32(p); |
222 | b->size = ceph_decode_32(p); | |
f24e9980 SW |
223 | |
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++) | |
c89136ea | 237 | b->items[j] = ceph_decode_32(p); |
f24e9980 SW |
238 | |
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 | |
30dc6381 | 287 | err = -EINVAL; |
64486697 XW |
288 | if (yes > (ULONG_MAX - sizeof(*r)) |
289 | / sizeof(struct crush_rule_step)) | |
f24e9980 SW |
290 | 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++) { | |
c89136ea SW |
302 | 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); | |
f24e9980 SW |
305 | } |
306 | } | |
307 | ||
308 | /* ignore trailing name maps. */ | |
546f04ef SW |
309 | 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 | } | |
314 | ||
315 | /* 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); | |
f24e9980 | 326 | |
1604f488 JS |
327 | 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 | ||
546f04ef | 332 | done: |
f24e9980 SW |
333 | dout("crush_decode success\n"); |
334 | return c; | |
335 | ||
336 | badmem: | |
337 | err = -ENOMEM; | |
338 | bad: | |
339 | dout("crush_decode fail %d\n", err); | |
340 | crush_destroy(c); | |
341 | return ERR_PTR(err); | |
342 | } | |
343 | ||
f24e9980 | 344 | /* |
9794b146 SW |
345 | * rbtree of pg_mapping for handling pg_temp (explicit mapping of pgid |
346 | * to a set of osds) | |
f24e9980 | 347 | */ |
5b191d99 | 348 | static int pgid_cmp(struct ceph_pg l, struct ceph_pg r) |
51042122 | 349 | { |
5b191d99 SW |
350 | if (l.pool < r.pool) |
351 | return -1; | |
352 | if (l.pool > r.pool) | |
353 | return 1; | |
354 | if (l.seed < r.seed) | |
51042122 | 355 | return -1; |
5b191d99 | 356 | if (l.seed > r.seed) |
51042122 SW |
357 | return 1; |
358 | return 0; | |
359 | } | |
360 | ||
991abb6e SW |
361 | static int __insert_pg_mapping(struct ceph_pg_mapping *new, |
362 | struct rb_root *root) | |
f24e9980 SW |
363 | { |
364 | struct rb_node **p = &root->rb_node; | |
365 | struct rb_node *parent = NULL; | |
366 | struct ceph_pg_mapping *pg = NULL; | |
51042122 | 367 | int c; |
f24e9980 | 368 | |
8adc8b3d | 369 | dout("__insert_pg_mapping %llx %p\n", *(u64 *)&new->pgid, new); |
f24e9980 SW |
370 | while (*p) { |
371 | parent = *p; | |
372 | pg = rb_entry(parent, struct ceph_pg_mapping, node); | |
51042122 SW |
373 | c = pgid_cmp(new->pgid, pg->pgid); |
374 | if (c < 0) | |
f24e9980 | 375 | p = &(*p)->rb_left; |
51042122 | 376 | else if (c > 0) |
f24e9980 SW |
377 | p = &(*p)->rb_right; |
378 | else | |
991abb6e | 379 | return -EEXIST; |
f24e9980 SW |
380 | } |
381 | ||
382 | rb_link_node(&new->node, parent, p); | |
383 | rb_insert_color(&new->node, root); | |
991abb6e | 384 | return 0; |
f24e9980 SW |
385 | } |
386 | ||
9794b146 | 387 | static struct ceph_pg_mapping *__lookup_pg_mapping(struct rb_root *root, |
5b191d99 | 388 | struct ceph_pg pgid) |
9794b146 SW |
389 | { |
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); | |
8adc8b3d | 397 | if (c < 0) { |
9794b146 | 398 | n = n->rb_left; |
8adc8b3d | 399 | } else if (c > 0) { |
9794b146 | 400 | n = n->rb_right; |
8adc8b3d | 401 | } else { |
5b191d99 SW |
402 | dout("__lookup_pg_mapping %lld.%x got %p\n", |
403 | pgid.pool, pgid.seed, pg); | |
9794b146 | 404 | return pg; |
8adc8b3d | 405 | } |
9794b146 SW |
406 | } |
407 | return NULL; | |
408 | } | |
409 | ||
5b191d99 | 410 | static int __remove_pg_mapping(struct rb_root *root, struct ceph_pg pgid) |
8adc8b3d SW |
411 | { |
412 | struct ceph_pg_mapping *pg = __lookup_pg_mapping(root, pgid); | |
413 | ||
414 | if (pg) { | |
5b191d99 SW |
415 | dout("__remove_pg_mapping %lld.%x %p\n", pgid.pool, pgid.seed, |
416 | pg); | |
8adc8b3d SW |
417 | rb_erase(&pg->node, root); |
418 | kfree(pg); | |
419 | return 0; | |
420 | } | |
5b191d99 | 421 | dout("__remove_pg_mapping %lld.%x dne\n", pgid.pool, pgid.seed); |
8adc8b3d SW |
422 | return -ENOENT; |
423 | } | |
424 | ||
4fc51be8 SW |
425 | /* |
426 | * rbtree of pg pool info | |
427 | */ | |
428 | static 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 | ||
4f6a7e5e | 450 | static struct ceph_pg_pool_info *__lookup_pg_pool(struct rb_root *root, u64 id) |
4fc51be8 SW |
451 | { |
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 | ||
ce7f6a27 ID |
467 | struct ceph_pg_pool_info *ceph_pg_pool_by_id(struct ceph_osdmap *map, u64 id) |
468 | { | |
469 | return __lookup_pg_pool(&map->pg_pools, id); | |
470 | } | |
471 | ||
72afc71f AE |
472 | const char *ceph_pg_pool_name_by_id(struct ceph_osdmap *map, u64 id) |
473 | { | |
474 | struct ceph_pg_pool_info *pi; | |
475 | ||
476 | if (id == CEPH_NOPOOL) | |
477 | return NULL; | |
478 | ||
479 | if (WARN_ON_ONCE(id > (u64) INT_MAX)) | |
480 | return NULL; | |
481 | ||
482 | pi = __lookup_pg_pool(&map->pg_pools, (int) id); | |
483 | ||
484 | return pi ? pi->name : NULL; | |
485 | } | |
486 | EXPORT_SYMBOL(ceph_pg_pool_name_by_id); | |
487 | ||
7669a2c9 YS |
488 | int ceph_pg_poolid_by_name(struct ceph_osdmap *map, const char *name) |
489 | { | |
490 | struct rb_node *rbp; | |
491 | ||
492 | for (rbp = rb_first(&map->pg_pools); rbp; rbp = rb_next(rbp)) { | |
493 | struct ceph_pg_pool_info *pi = | |
494 | rb_entry(rbp, struct ceph_pg_pool_info, node); | |
495 | if (pi->name && strcmp(pi->name, name) == 0) | |
496 | return pi->id; | |
497 | } | |
498 | return -ENOENT; | |
499 | } | |
3d14c5d2 | 500 | EXPORT_SYMBOL(ceph_pg_poolid_by_name); |
7669a2c9 | 501 | |
2844a76a SW |
502 | static void __remove_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *pi) |
503 | { | |
504 | rb_erase(&pi->node, root); | |
505 | kfree(pi->name); | |
506 | kfree(pi); | |
507 | } | |
508 | ||
0f70c7ee | 509 | static int decode_pool(void **p, void *end, struct ceph_pg_pool_info *pi) |
efd7576b | 510 | { |
4f6a7e5e SW |
511 | u8 ev, cv; |
512 | unsigned len, num; | |
513 | void *pool_end; | |
514 | ||
515 | ceph_decode_need(p, end, 2 + 4, bad); | |
516 | ev = ceph_decode_8(p); /* encoding version */ | |
517 | cv = ceph_decode_8(p); /* compat version */ | |
518 | if (ev < 5) { | |
519 | pr_warning("got v %d < 5 cv %d of ceph_pg_pool\n", ev, cv); | |
520 | return -EINVAL; | |
521 | } | |
17a13e40 ID |
522 | if (cv > 9) { |
523 | pr_warning("got v %d cv %d > 9 of ceph_pg_pool\n", ev, cv); | |
4f6a7e5e SW |
524 | return -EINVAL; |
525 | } | |
526 | len = ceph_decode_32(p); | |
527 | ceph_decode_need(p, end, len, bad); | |
528 | pool_end = *p + len; | |
73a7e693 | 529 | |
4f6a7e5e SW |
530 | pi->type = ceph_decode_8(p); |
531 | pi->size = ceph_decode_8(p); | |
532 | pi->crush_ruleset = ceph_decode_8(p); | |
533 | pi->object_hash = ceph_decode_8(p); | |
73a7e693 | 534 | |
4f6a7e5e SW |
535 | pi->pg_num = ceph_decode_32(p); |
536 | pi->pgp_num = ceph_decode_32(p); | |
537 | ||
538 | *p += 4 + 4; /* skip lpg* */ | |
539 | *p += 4; /* skip last_change */ | |
540 | *p += 8 + 4; /* skip snap_seq, snap_epoch */ | |
541 | ||
542 | /* skip snaps */ | |
543 | num = ceph_decode_32(p); | |
544 | while (num--) { | |
545 | *p += 8; /* snapid key */ | |
546 | *p += 1 + 1; /* versions */ | |
547 | len = ceph_decode_32(p); | |
548 | *p += len; | |
73a7e693 SW |
549 | } |
550 | ||
17a13e40 | 551 | /* skip removed_snaps */ |
4f6a7e5e SW |
552 | num = ceph_decode_32(p); |
553 | *p += num * (8 + 8); | |
554 | ||
555 | *p += 8; /* skip auid */ | |
556 | pi->flags = ceph_decode_64(p); | |
17a13e40 ID |
557 | *p += 4; /* skip crash_replay_interval */ |
558 | ||
559 | if (ev >= 7) | |
560 | *p += 1; /* skip min_size */ | |
561 | ||
562 | if (ev >= 8) | |
563 | *p += 8 + 8; /* skip quota_max_* */ | |
564 | ||
565 | if (ev >= 9) { | |
566 | /* skip tiers */ | |
567 | num = ceph_decode_32(p); | |
568 | *p += num * 8; | |
569 | ||
570 | *p += 8; /* skip tier_of */ | |
571 | *p += 1; /* skip cache_mode */ | |
572 | ||
573 | pi->read_tier = ceph_decode_64(p); | |
574 | pi->write_tier = ceph_decode_64(p); | |
575 | } else { | |
576 | pi->read_tier = -1; | |
577 | pi->write_tier = -1; | |
578 | } | |
4f6a7e5e SW |
579 | |
580 | /* ignore the rest */ | |
581 | ||
582 | *p = pool_end; | |
583 | calc_pg_masks(pi); | |
73a7e693 SW |
584 | return 0; |
585 | ||
586 | bad: | |
587 | return -EINVAL; | |
efd7576b SW |
588 | } |
589 | ||
0f70c7ee | 590 | static int decode_pool_names(void **p, void *end, struct ceph_osdmap *map) |
2844a76a SW |
591 | { |
592 | struct ceph_pg_pool_info *pi; | |
4f6a7e5e SW |
593 | u32 num, len; |
594 | u64 pool; | |
2844a76a SW |
595 | |
596 | ceph_decode_32_safe(p, end, num, bad); | |
597 | dout(" %d pool names\n", num); | |
598 | while (num--) { | |
4f6a7e5e | 599 | ceph_decode_64_safe(p, end, pool, bad); |
2844a76a | 600 | ceph_decode_32_safe(p, end, len, bad); |
4f6a7e5e | 601 | dout(" pool %llu len %d\n", pool, len); |
ad3b904c | 602 | ceph_decode_need(p, end, len, bad); |
2844a76a SW |
603 | pi = __lookup_pg_pool(&map->pg_pools, pool); |
604 | if (pi) { | |
ad3b904c XW |
605 | char *name = kstrndup(*p, len, GFP_NOFS); |
606 | ||
607 | if (!name) | |
608 | return -ENOMEM; | |
2844a76a | 609 | kfree(pi->name); |
ad3b904c XW |
610 | pi->name = name; |
611 | dout(" name is %s\n", pi->name); | |
2844a76a SW |
612 | } |
613 | *p += len; | |
614 | } | |
615 | return 0; | |
616 | ||
617 | bad: | |
618 | return -EINVAL; | |
619 | } | |
620 | ||
621 | /* | |
622 | * osd map | |
623 | */ | |
624 | void ceph_osdmap_destroy(struct ceph_osdmap *map) | |
625 | { | |
626 | dout("osdmap_destroy %p\n", map); | |
627 | if (map->crush) | |
628 | crush_destroy(map->crush); | |
629 | while (!RB_EMPTY_ROOT(&map->pg_temp)) { | |
630 | struct ceph_pg_mapping *pg = | |
631 | rb_entry(rb_first(&map->pg_temp), | |
632 | struct ceph_pg_mapping, node); | |
633 | rb_erase(&pg->node, &map->pg_temp); | |
634 | kfree(pg); | |
635 | } | |
636 | while (!RB_EMPTY_ROOT(&map->pg_pools)) { | |
637 | struct ceph_pg_pool_info *pi = | |
638 | rb_entry(rb_first(&map->pg_pools), | |
639 | struct ceph_pg_pool_info, node); | |
640 | __remove_pg_pool(&map->pg_pools, pi); | |
641 | } | |
642 | kfree(map->osd_state); | |
643 | kfree(map->osd_weight); | |
644 | kfree(map->osd_addr); | |
645 | kfree(map); | |
646 | } | |
647 | ||
648 | /* | |
4d60351f ID |
649 | * Adjust max_osd value, (re)allocate arrays. |
650 | * | |
651 | * The new elements are properly initialized. | |
2844a76a SW |
652 | */ |
653 | static int osdmap_set_max_osd(struct ceph_osdmap *map, int max) | |
654 | { | |
655 | u8 *state; | |
2844a76a | 656 | u32 *weight; |
4d60351f ID |
657 | struct ceph_entity_addr *addr; |
658 | int i; | |
2844a76a | 659 | |
4d60351f ID |
660 | state = krealloc(map->osd_state, max*sizeof(*state), GFP_NOFS); |
661 | weight = krealloc(map->osd_weight, max*sizeof(*weight), GFP_NOFS); | |
662 | addr = krealloc(map->osd_addr, max*sizeof(*addr), GFP_NOFS); | |
663 | if (!state || !weight || !addr) { | |
2844a76a | 664 | kfree(state); |
2844a76a | 665 | kfree(weight); |
4d60351f ID |
666 | kfree(addr); |
667 | ||
2844a76a SW |
668 | return -ENOMEM; |
669 | } | |
670 | ||
4d60351f ID |
671 | for (i = map->max_osd; i < max; i++) { |
672 | state[i] = 0; | |
673 | weight[i] = CEPH_OSD_OUT; | |
674 | memset(addr + i, 0, sizeof(*addr)); | |
2844a76a SW |
675 | } |
676 | ||
677 | map->osd_state = state; | |
678 | map->osd_weight = weight; | |
679 | map->osd_addr = addr; | |
4d60351f | 680 | |
2844a76a | 681 | map->max_osd = max; |
4d60351f | 682 | |
2844a76a SW |
683 | return 0; |
684 | } | |
685 | ||
ec7af972 ID |
686 | #define OSDMAP_WRAPPER_COMPAT_VER 7 |
687 | #define OSDMAP_CLIENT_DATA_COMPAT_VER 1 | |
688 | ||
689 | /* | |
690 | * Return 0 or error. On success, *v is set to 0 for old (v6) osdmaps, | |
691 | * to struct_v of the client_data section for new (v7 and above) | |
692 | * osdmaps. | |
693 | */ | |
694 | static int get_osdmap_client_data_v(void **p, void *end, | |
695 | const char *prefix, u8 *v) | |
696 | { | |
697 | u8 struct_v; | |
698 | ||
699 | ceph_decode_8_safe(p, end, struct_v, e_inval); | |
700 | if (struct_v >= 7) { | |
701 | u8 struct_compat; | |
702 | ||
703 | ceph_decode_8_safe(p, end, struct_compat, e_inval); | |
704 | if (struct_compat > OSDMAP_WRAPPER_COMPAT_VER) { | |
705 | pr_warning("got v %d cv %d > %d of %s ceph_osdmap\n", | |
706 | struct_v, struct_compat, | |
707 | OSDMAP_WRAPPER_COMPAT_VER, prefix); | |
708 | return -EINVAL; | |
709 | } | |
710 | *p += 4; /* ignore wrapper struct_len */ | |
711 | ||
712 | ceph_decode_8_safe(p, end, struct_v, e_inval); | |
713 | ceph_decode_8_safe(p, end, struct_compat, e_inval); | |
714 | if (struct_compat > OSDMAP_CLIENT_DATA_COMPAT_VER) { | |
715 | pr_warning("got v %d cv %d > %d of %s ceph_osdmap client data\n", | |
716 | struct_v, struct_compat, | |
717 | OSDMAP_CLIENT_DATA_COMPAT_VER, prefix); | |
718 | return -EINVAL; | |
719 | } | |
720 | *p += 4; /* ignore client data struct_len */ | |
721 | } else { | |
722 | u16 version; | |
723 | ||
724 | *p -= 1; | |
725 | ceph_decode_16_safe(p, end, version, e_inval); | |
726 | if (version < 6) { | |
727 | pr_warning("got v %d < 6 of %s ceph_osdmap\n", version, | |
728 | prefix); | |
729 | return -EINVAL; | |
730 | } | |
731 | ||
732 | /* old osdmap enconding */ | |
733 | struct_v = 0; | |
734 | } | |
735 | ||
736 | *v = struct_v; | |
737 | return 0; | |
738 | ||
739 | e_inval: | |
740 | return -EINVAL; | |
741 | } | |
742 | ||
433fbdd3 ID |
743 | static int __decode_pools(void **p, void *end, struct ceph_osdmap *map, |
744 | bool incremental) | |
745 | { | |
746 | u32 n; | |
747 | ||
748 | ceph_decode_32_safe(p, end, n, e_inval); | |
749 | while (n--) { | |
750 | struct ceph_pg_pool_info *pi; | |
751 | u64 pool; | |
752 | int ret; | |
753 | ||
754 | ceph_decode_64_safe(p, end, pool, e_inval); | |
755 | ||
756 | pi = __lookup_pg_pool(&map->pg_pools, pool); | |
757 | if (!incremental || !pi) { | |
758 | pi = kzalloc(sizeof(*pi), GFP_NOFS); | |
759 | if (!pi) | |
760 | return -ENOMEM; | |
761 | ||
762 | pi->id = pool; | |
763 | ||
764 | ret = __insert_pg_pool(&map->pg_pools, pi); | |
765 | if (ret) { | |
766 | kfree(pi); | |
767 | return ret; | |
768 | } | |
769 | } | |
770 | ||
771 | ret = decode_pool(p, end, pi); | |
772 | if (ret) | |
773 | return ret; | |
774 | } | |
775 | ||
776 | return 0; | |
777 | ||
778 | e_inval: | |
779 | return -EINVAL; | |
780 | } | |
781 | ||
782 | static int decode_pools(void **p, void *end, struct ceph_osdmap *map) | |
783 | { | |
784 | return __decode_pools(p, end, map, false); | |
785 | } | |
786 | ||
787 | static int decode_new_pools(void **p, void *end, struct ceph_osdmap *map) | |
788 | { | |
789 | return __decode_pools(p, end, map, true); | |
790 | } | |
791 | ||
10db634e ID |
792 | static int __decode_pg_temp(void **p, void *end, struct ceph_osdmap *map, |
793 | bool incremental) | |
794 | { | |
795 | u32 n; | |
796 | ||
797 | ceph_decode_32_safe(p, end, n, e_inval); | |
798 | while (n--) { | |
799 | struct ceph_pg pgid; | |
800 | u32 len, i; | |
801 | int ret; | |
802 | ||
803 | ret = ceph_decode_pgid(p, end, &pgid); | |
804 | if (ret) | |
805 | return ret; | |
806 | ||
807 | ceph_decode_32_safe(p, end, len, e_inval); | |
808 | ||
809 | ret = __remove_pg_mapping(&map->pg_temp, pgid); | |
810 | BUG_ON(!incremental && ret != -ENOENT); | |
811 | ||
812 | if (!incremental || len > 0) { | |
813 | struct ceph_pg_mapping *pg; | |
814 | ||
815 | ceph_decode_need(p, end, len*sizeof(u32), e_inval); | |
816 | ||
817 | if (len > (UINT_MAX - sizeof(*pg)) / sizeof(u32)) | |
818 | return -EINVAL; | |
819 | ||
820 | pg = kzalloc(sizeof(*pg) + len*sizeof(u32), GFP_NOFS); | |
821 | if (!pg) | |
822 | return -ENOMEM; | |
823 | ||
824 | pg->pgid = pgid; | |
35a935d7 | 825 | pg->pg_temp.len = len; |
10db634e | 826 | for (i = 0; i < len; i++) |
35a935d7 | 827 | pg->pg_temp.osds[i] = ceph_decode_32(p); |
10db634e ID |
828 | |
829 | ret = __insert_pg_mapping(pg, &map->pg_temp); | |
830 | if (ret) { | |
831 | kfree(pg); | |
832 | return ret; | |
833 | } | |
834 | } | |
835 | } | |
836 | ||
837 | return 0; | |
838 | ||
839 | e_inval: | |
840 | return -EINVAL; | |
841 | } | |
842 | ||
843 | static int decode_pg_temp(void **p, void *end, struct ceph_osdmap *map) | |
844 | { | |
845 | return __decode_pg_temp(p, end, map, false); | |
846 | } | |
847 | ||
848 | static int decode_new_pg_temp(void **p, void *end, struct ceph_osdmap *map) | |
849 | { | |
850 | return __decode_pg_temp(p, end, map, true); | |
851 | } | |
852 | ||
f24e9980 SW |
853 | /* |
854 | * decode a full map. | |
855 | */ | |
a2505d63 | 856 | static int osdmap_decode(void **p, void *end, struct ceph_osdmap *map) |
f24e9980 | 857 | { |
ec7af972 | 858 | u8 struct_v; |
38a8d560 | 859 | u32 epoch = 0; |
f24e9980 | 860 | void *start = *p; |
3977058c ID |
861 | u32 max; |
862 | u32 len, i; | |
597b52f6 | 863 | int err; |
f24e9980 | 864 | |
a2505d63 | 865 | dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p)); |
f24e9980 | 866 | |
ec7af972 ID |
867 | err = get_osdmap_client_data_v(p, end, "full", &struct_v); |
868 | if (err) | |
869 | goto bad; | |
f24e9980 | 870 | |
53bbaba9 ID |
871 | /* fsid, epoch, created, modified */ |
872 | ceph_decode_need(p, end, sizeof(map->fsid) + sizeof(u32) + | |
873 | sizeof(map->created) + sizeof(map->modified), e_inval); | |
f24e9980 | 874 | ceph_decode_copy(p, &map->fsid, sizeof(map->fsid)); |
38a8d560 | 875 | epoch = map->epoch = ceph_decode_32(p); |
f24e9980 SW |
876 | ceph_decode_copy(p, &map->created, sizeof(map->created)); |
877 | ceph_decode_copy(p, &map->modified, sizeof(map->modified)); | |
878 | ||
433fbdd3 ID |
879 | /* pools */ |
880 | err = decode_pools(p, end, map); | |
881 | if (err) | |
882 | goto bad; | |
2844a76a | 883 | |
0f70c7ee ID |
884 | /* pool_name */ |
885 | err = decode_pool_names(p, end, map); | |
597b52f6 | 886 | if (err) |
4f6a7e5e | 887 | goto bad; |
2844a76a | 888 | |
597b52f6 | 889 | ceph_decode_32_safe(p, end, map->pool_max, e_inval); |
f24e9980 | 890 | |
597b52f6 | 891 | ceph_decode_32_safe(p, end, map->flags, e_inval); |
f24e9980 | 892 | |
3977058c ID |
893 | /* max_osd */ |
894 | ceph_decode_32_safe(p, end, max, e_inval); | |
f24e9980 SW |
895 | |
896 | /* (re)alloc osd arrays */ | |
897 | err = osdmap_set_max_osd(map, max); | |
597b52f6 | 898 | if (err) |
f24e9980 | 899 | goto bad; |
f24e9980 | 900 | |
2d88b2e0 | 901 | /* osd_state, osd_weight, osd_addrs->client_addr */ |
f24e9980 SW |
902 | ceph_decode_need(p, end, 3*sizeof(u32) + |
903 | map->max_osd*(1 + sizeof(*map->osd_weight) + | |
597b52f6 ID |
904 | sizeof(*map->osd_addr)), e_inval); |
905 | ||
2d88b2e0 ID |
906 | if (ceph_decode_32(p) != map->max_osd) |
907 | goto e_inval; | |
908 | ||
f24e9980 SW |
909 | ceph_decode_copy(p, map->osd_state, map->max_osd); |
910 | ||
2d88b2e0 ID |
911 | if (ceph_decode_32(p) != map->max_osd) |
912 | goto e_inval; | |
913 | ||
f24e9980 | 914 | for (i = 0; i < map->max_osd; i++) |
c89136ea | 915 | map->osd_weight[i] = ceph_decode_32(p); |
f24e9980 | 916 | |
2d88b2e0 ID |
917 | if (ceph_decode_32(p) != map->max_osd) |
918 | goto e_inval; | |
919 | ||
f24e9980 | 920 | ceph_decode_copy(p, map->osd_addr, map->max_osd*sizeof(*map->osd_addr)); |
63f2d211 SW |
921 | for (i = 0; i < map->max_osd; i++) |
922 | ceph_decode_addr(&map->osd_addr[i]); | |
f24e9980 SW |
923 | |
924 | /* pg_temp */ | |
10db634e ID |
925 | err = decode_pg_temp(p, end, map); |
926 | if (err) | |
927 | goto bad; | |
f24e9980 SW |
928 | |
929 | /* crush */ | |
597b52f6 | 930 | ceph_decode_32_safe(p, end, len, e_inval); |
9902e682 | 931 | map->crush = crush_decode(*p, min(*p + len, end)); |
f24e9980 SW |
932 | if (IS_ERR(map->crush)) { |
933 | err = PTR_ERR(map->crush); | |
934 | map->crush = NULL; | |
935 | goto bad; | |
936 | } | |
9902e682 | 937 | *p += len; |
f24e9980 | 938 | |
38a8d560 | 939 | /* ignore the rest */ |
f24e9980 SW |
940 | *p = end; |
941 | ||
38a8d560 | 942 | dout("full osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd); |
a2505d63 | 943 | return 0; |
f24e9980 | 944 | |
597b52f6 ID |
945 | e_inval: |
946 | err = -EINVAL; | |
f24e9980 | 947 | bad: |
38a8d560 ID |
948 | pr_err("corrupt full osdmap (%d) epoch %d off %d (%p of %p-%p)\n", |
949 | err, epoch, (int)(*p - start), *p, start, end); | |
950 | print_hex_dump(KERN_DEBUG, "osdmap: ", | |
951 | DUMP_PREFIX_OFFSET, 16, 1, | |
952 | start, end - start, true); | |
a2505d63 ID |
953 | return err; |
954 | } | |
955 | ||
956 | /* | |
957 | * Allocate and decode a full map. | |
958 | */ | |
959 | struct ceph_osdmap *ceph_osdmap_decode(void **p, void *end) | |
960 | { | |
961 | struct ceph_osdmap *map; | |
962 | int ret; | |
963 | ||
964 | map = kzalloc(sizeof(*map), GFP_NOFS); | |
965 | if (!map) | |
966 | return ERR_PTR(-ENOMEM); | |
967 | ||
968 | map->pg_temp = RB_ROOT; | |
969 | mutex_init(&map->crush_scratch_mutex); | |
970 | ||
971 | ret = osdmap_decode(p, end, map); | |
972 | if (ret) { | |
973 | ceph_osdmap_destroy(map); | |
974 | return ERR_PTR(ret); | |
975 | } | |
976 | ||
977 | return map; | |
f24e9980 SW |
978 | } |
979 | ||
980 | /* | |
981 | * decode and apply an incremental map update. | |
982 | */ | |
983 | struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end, | |
984 | struct ceph_osdmap *map, | |
985 | struct ceph_messenger *msgr) | |
986 | { | |
f24e9980 SW |
987 | struct crush_map *newcrush = NULL; |
988 | struct ceph_fsid fsid; | |
989 | u32 epoch = 0; | |
990 | struct ceph_timespec modified; | |
4f6a7e5e SW |
991 | s32 len; |
992 | u64 pool; | |
993 | __s64 new_pool_max; | |
994 | __s32 new_flags, max; | |
f24e9980 | 995 | void *start = *p; |
86f1742b | 996 | int err; |
ec7af972 | 997 | u8 struct_v; |
f24e9980 | 998 | |
38a8d560 ID |
999 | dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p)); |
1000 | ||
ec7af972 ID |
1001 | err = get_osdmap_client_data_v(p, end, "inc", &struct_v); |
1002 | if (err) | |
1003 | goto bad; | |
f24e9980 | 1004 | |
53bbaba9 ID |
1005 | /* fsid, epoch, modified, new_pool_max, new_flags */ |
1006 | ceph_decode_need(p, end, sizeof(fsid) + sizeof(u32) + sizeof(modified) + | |
1007 | sizeof(u64) + sizeof(u32), e_inval); | |
f24e9980 | 1008 | ceph_decode_copy(p, &fsid, sizeof(fsid)); |
c89136ea | 1009 | epoch = ceph_decode_32(p); |
f24e9980 SW |
1010 | BUG_ON(epoch != map->epoch+1); |
1011 | ceph_decode_copy(p, &modified, sizeof(modified)); | |
4f6a7e5e | 1012 | new_pool_max = ceph_decode_64(p); |
c89136ea | 1013 | new_flags = ceph_decode_32(p); |
f24e9980 SW |
1014 | |
1015 | /* full map? */ | |
86f1742b | 1016 | ceph_decode_32_safe(p, end, len, e_inval); |
f24e9980 SW |
1017 | if (len > 0) { |
1018 | dout("apply_incremental full map len %d, %p to %p\n", | |
1019 | len, *p, end); | |
a2505d63 | 1020 | return ceph_osdmap_decode(p, min(*p+len, end)); |
f24e9980 SW |
1021 | } |
1022 | ||
1023 | /* new crush? */ | |
86f1742b | 1024 | ceph_decode_32_safe(p, end, len, e_inval); |
f24e9980 | 1025 | if (len > 0) { |
f24e9980 | 1026 | newcrush = crush_decode(*p, min(*p+len, end)); |
86f1742b ID |
1027 | if (IS_ERR(newcrush)) { |
1028 | err = PTR_ERR(newcrush); | |
1029 | newcrush = NULL; | |
1030 | goto bad; | |
1031 | } | |
cebc5be6 | 1032 | *p += len; |
f24e9980 SW |
1033 | } |
1034 | ||
1035 | /* new flags? */ | |
1036 | if (new_flags >= 0) | |
1037 | map->flags = new_flags; | |
4fc51be8 SW |
1038 | if (new_pool_max >= 0) |
1039 | map->pool_max = new_pool_max; | |
f24e9980 | 1040 | |
f24e9980 | 1041 | /* new max? */ |
53bbaba9 | 1042 | ceph_decode_32_safe(p, end, max, e_inval); |
f24e9980 SW |
1043 | if (max >= 0) { |
1044 | err = osdmap_set_max_osd(map, max); | |
86f1742b | 1045 | if (err) |
f24e9980 SW |
1046 | goto bad; |
1047 | } | |
1048 | ||
1049 | map->epoch++; | |
31456665 | 1050 | map->modified = modified; |
f24e9980 SW |
1051 | if (newcrush) { |
1052 | if (map->crush) | |
1053 | crush_destroy(map->crush); | |
1054 | map->crush = newcrush; | |
1055 | newcrush = NULL; | |
1056 | } | |
1057 | ||
433fbdd3 ID |
1058 | /* new_pools */ |
1059 | err = decode_new_pools(p, end, map); | |
1060 | if (err) | |
1061 | goto bad; | |
9464d008 | 1062 | |
0f70c7ee ID |
1063 | /* new_pool_names */ |
1064 | err = decode_pool_names(p, end, map); | |
9464d008 ID |
1065 | if (err) |
1066 | goto bad; | |
f24e9980 | 1067 | |
4fc51be8 | 1068 | /* old_pool */ |
86f1742b | 1069 | ceph_decode_32_safe(p, end, len, e_inval); |
4fc51be8 SW |
1070 | while (len--) { |
1071 | struct ceph_pg_pool_info *pi; | |
1072 | ||
86f1742b | 1073 | ceph_decode_64_safe(p, end, pool, e_inval); |
4fc51be8 | 1074 | pi = __lookup_pg_pool(&map->pg_pools, pool); |
2844a76a SW |
1075 | if (pi) |
1076 | __remove_pg_pool(&map->pg_pools, pi); | |
4fc51be8 | 1077 | } |
f24e9980 SW |
1078 | |
1079 | /* new_up */ | |
86f1742b | 1080 | ceph_decode_32_safe(p, end, len, e_inval); |
f24e9980 SW |
1081 | while (len--) { |
1082 | u32 osd; | |
1083 | struct ceph_entity_addr addr; | |
86f1742b ID |
1084 | ceph_decode_32_safe(p, end, osd, e_inval); |
1085 | ceph_decode_copy_safe(p, end, &addr, sizeof(addr), e_inval); | |
63f2d211 | 1086 | ceph_decode_addr(&addr); |
f24e9980 SW |
1087 | pr_info("osd%d up\n", osd); |
1088 | BUG_ON(osd >= map->max_osd); | |
1089 | map->osd_state[osd] |= CEPH_OSD_UP; | |
1090 | map->osd_addr[osd] = addr; | |
1091 | } | |
1092 | ||
7662d8ff | 1093 | /* new_state */ |
86f1742b | 1094 | ceph_decode_32_safe(p, end, len, e_inval); |
f24e9980 SW |
1095 | while (len--) { |
1096 | u32 osd; | |
7662d8ff | 1097 | u8 xorstate; |
86f1742b | 1098 | ceph_decode_32_safe(p, end, osd, e_inval); |
7662d8ff | 1099 | xorstate = **(u8 **)p; |
f24e9980 | 1100 | (*p)++; /* clean flag */ |
7662d8ff SW |
1101 | if (xorstate == 0) |
1102 | xorstate = CEPH_OSD_UP; | |
1103 | if (xorstate & CEPH_OSD_UP) | |
1104 | pr_info("osd%d down\n", osd); | |
f24e9980 | 1105 | if (osd < map->max_osd) |
7662d8ff | 1106 | map->osd_state[osd] ^= xorstate; |
f24e9980 SW |
1107 | } |
1108 | ||
1109 | /* new_weight */ | |
86f1742b | 1110 | ceph_decode_32_safe(p, end, len, e_inval); |
f24e9980 SW |
1111 | while (len--) { |
1112 | u32 osd, off; | |
86f1742b | 1113 | ceph_decode_need(p, end, sizeof(u32)*2, e_inval); |
c89136ea SW |
1114 | osd = ceph_decode_32(p); |
1115 | off = ceph_decode_32(p); | |
f24e9980 SW |
1116 | pr_info("osd%d weight 0x%x %s\n", osd, off, |
1117 | off == CEPH_OSD_IN ? "(in)" : | |
1118 | (off == CEPH_OSD_OUT ? "(out)" : "")); | |
1119 | if (osd < map->max_osd) | |
1120 | map->osd_weight[osd] = off; | |
1121 | } | |
1122 | ||
1123 | /* new_pg_temp */ | |
10db634e ID |
1124 | err = decode_new_pg_temp(p, end, map); |
1125 | if (err) | |
1126 | goto bad; | |
f24e9980 SW |
1127 | |
1128 | /* ignore the rest */ | |
1129 | *p = end; | |
38a8d560 ID |
1130 | |
1131 | dout("inc osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd); | |
f24e9980 SW |
1132 | return map; |
1133 | ||
86f1742b ID |
1134 | e_inval: |
1135 | err = -EINVAL; | |
f24e9980 | 1136 | bad: |
38a8d560 ID |
1137 | pr_err("corrupt inc osdmap (%d) epoch %d off %d (%p of %p-%p)\n", |
1138 | err, epoch, (int)(*p - start), *p, start, end); | |
9ec7cab1 SW |
1139 | print_hex_dump(KERN_DEBUG, "osdmap: ", |
1140 | DUMP_PREFIX_OFFSET, 16, 1, | |
1141 | start, end - start, true); | |
f24e9980 SW |
1142 | if (newcrush) |
1143 | crush_destroy(newcrush); | |
1144 | return ERR_PTR(err); | |
1145 | } | |
1146 | ||
1147 | ||
1148 | ||
1149 | ||
1150 | /* | |
1151 | * calculate file layout from given offset, length. | |
1152 | * fill in correct oid, logical length, and object extent | |
1153 | * offset, length. | |
1154 | * | |
1155 | * for now, we write only a single su, until we can | |
1156 | * pass a stride back to the caller. | |
1157 | */ | |
d63b77f4 | 1158 | int ceph_calc_file_object_mapping(struct ceph_file_layout *layout, |
e8afad65 | 1159 | u64 off, u64 len, |
645a1025 | 1160 | u64 *ono, |
f24e9980 SW |
1161 | u64 *oxoff, u64 *oxlen) |
1162 | { | |
1163 | u32 osize = le32_to_cpu(layout->fl_object_size); | |
1164 | u32 su = le32_to_cpu(layout->fl_stripe_unit); | |
1165 | u32 sc = le32_to_cpu(layout->fl_stripe_count); | |
1166 | u32 bl, stripeno, stripepos, objsetno; | |
1167 | u32 su_per_object; | |
ff1d1f71 | 1168 | u64 t, su_offset; |
f24e9980 | 1169 | |
e8afad65 | 1170 | dout("mapping %llu~%llu osize %u fl_su %u\n", off, len, |
f24e9980 | 1171 | osize, su); |
d63b77f4 SW |
1172 | if (su == 0 || sc == 0) |
1173 | goto invalid; | |
35e054a6 | 1174 | su_per_object = osize / su; |
d63b77f4 SW |
1175 | if (su_per_object == 0) |
1176 | goto invalid; | |
f24e9980 SW |
1177 | dout("osize %u / su %u = su_per_object %u\n", osize, su, |
1178 | su_per_object); | |
1179 | ||
d63b77f4 SW |
1180 | if ((su & ~PAGE_MASK) != 0) |
1181 | goto invalid; | |
1182 | ||
f24e9980 SW |
1183 | /* bl = *off / su; */ |
1184 | t = off; | |
1185 | do_div(t, su); | |
1186 | bl = t; | |
1187 | dout("off %llu / su %u = bl %u\n", off, su, bl); | |
1188 | ||
1189 | stripeno = bl / sc; | |
1190 | stripepos = bl % sc; | |
1191 | objsetno = stripeno / su_per_object; | |
1192 | ||
645a1025 | 1193 | *ono = objsetno * sc + stripepos; |
95c96174 | 1194 | dout("objset %u * sc %u = ono %u\n", objsetno, sc, (unsigned int)*ono); |
645a1025 SW |
1195 | |
1196 | /* *oxoff = *off % layout->fl_stripe_unit; # offset in su */ | |
f24e9980 | 1197 | t = off; |
ff1d1f71 NW |
1198 | su_offset = do_div(t, su); |
1199 | *oxoff = su_offset + (stripeno % su_per_object) * su; | |
1200 | ||
1201 | /* | |
1202 | * Calculate the length of the extent being written to the selected | |
e8afad65 | 1203 | * object. This is the minimum of the full length requested (len) or |
ff1d1f71 NW |
1204 | * the remainder of the current stripe being written to. |
1205 | */ | |
e8afad65 | 1206 | *oxlen = min_t(u64, len, su - su_offset); |
f24e9980 SW |
1207 | |
1208 | dout(" obj extent %llu~%llu\n", *oxoff, *oxlen); | |
d63b77f4 SW |
1209 | return 0; |
1210 | ||
1211 | invalid: | |
1212 | dout(" invalid layout\n"); | |
1213 | *ono = 0; | |
1214 | *oxoff = 0; | |
1215 | *oxlen = 0; | |
1216 | return -EINVAL; | |
f24e9980 | 1217 | } |
3d14c5d2 | 1218 | EXPORT_SYMBOL(ceph_calc_file_object_mapping); |
f24e9980 SW |
1219 | |
1220 | /* | |
7c13cb64 ID |
1221 | * Calculate mapping of a (oloc, oid) pair to a PG. Should only be |
1222 | * called with target's (oloc, oid), since tiering isn't taken into | |
1223 | * account. | |
f24e9980 | 1224 | */ |
7c13cb64 ID |
1225 | int ceph_oloc_oid_to_pg(struct ceph_osdmap *osdmap, |
1226 | struct ceph_object_locator *oloc, | |
1227 | struct ceph_object_id *oid, | |
1228 | struct ceph_pg *pg_out) | |
f24e9980 | 1229 | { |
7c13cb64 | 1230 | struct ceph_pg_pool_info *pi; |
f24e9980 | 1231 | |
7c13cb64 ID |
1232 | pi = __lookup_pg_pool(&osdmap->pg_pools, oloc->pool); |
1233 | if (!pi) | |
4fc51be8 | 1234 | return -EIO; |
f24e9980 | 1235 | |
7c13cb64 ID |
1236 | pg_out->pool = oloc->pool; |
1237 | pg_out->seed = ceph_str_hash(pi->object_hash, oid->name, | |
1238 | oid->name_len); | |
1239 | ||
1240 | dout("%s '%.*s' pgid %llu.%x\n", __func__, oid->name_len, oid->name, | |
1241 | pg_out->pool, pg_out->seed); | |
f24e9980 SW |
1242 | return 0; |
1243 | } | |
7c13cb64 | 1244 | EXPORT_SYMBOL(ceph_oloc_oid_to_pg); |
f24e9980 | 1245 | |
9d521470 ID |
1246 | static int do_crush(struct ceph_osdmap *map, int ruleno, int x, |
1247 | int *result, int result_max, | |
1248 | const __u32 *weight, int weight_max) | |
e8ef19c4 | 1249 | { |
9d521470 ID |
1250 | int r; |
1251 | ||
1252 | BUG_ON(result_max > CEPH_PG_MAX_SIZE); | |
1253 | ||
1254 | mutex_lock(&map->crush_scratch_mutex); | |
1255 | r = crush_do_rule(map->crush, ruleno, x, result, result_max, | |
1256 | weight, weight_max, map->crush_scratch_ary); | |
1257 | mutex_unlock(&map->crush_scratch_mutex); | |
e8ef19c4 | 1258 | |
9d521470 | 1259 | return r; |
e8ef19c4 ID |
1260 | } |
1261 | ||
f24e9980 SW |
1262 | /* |
1263 | * Calculate raw osd vector for the given pgid. Return pointer to osd | |
1264 | * array, or NULL on failure. | |
1265 | */ | |
5b191d99 | 1266 | static int *calc_pg_raw(struct ceph_osdmap *osdmap, struct ceph_pg pgid, |
f24e9980 SW |
1267 | int *osds, int *num) |
1268 | { | |
f24e9980 SW |
1269 | struct ceph_pg_mapping *pg; |
1270 | struct ceph_pg_pool_info *pool; | |
1271 | int ruleno; | |
83ca14fd SW |
1272 | int r; |
1273 | u32 pps; | |
f24e9980 | 1274 | |
83ca14fd | 1275 | pool = __lookup_pg_pool(&osdmap->pg_pools, pgid.pool); |
782e182e SW |
1276 | if (!pool) |
1277 | return NULL; | |
1278 | ||
f24e9980 | 1279 | /* pg_temp? */ |
83ca14fd | 1280 | pgid.seed = ceph_stable_mod(pgid.seed, pool->pg_num, |
9542cf0b | 1281 | pool->pg_num_mask); |
9794b146 SW |
1282 | pg = __lookup_pg_mapping(&osdmap->pg_temp, pgid); |
1283 | if (pg) { | |
35a935d7 ID |
1284 | *num = pg->pg_temp.len; |
1285 | return pg->pg_temp.osds; | |
f24e9980 SW |
1286 | } |
1287 | ||
1288 | /* crush */ | |
4f6a7e5e SW |
1289 | ruleno = crush_find_rule(osdmap->crush, pool->crush_ruleset, |
1290 | pool->type, pool->size); | |
f24e9980 | 1291 | if (ruleno < 0) { |
83ca14fd SW |
1292 | pr_err("no crush rule pool %lld ruleset %d type %d size %d\n", |
1293 | pgid.pool, pool->crush_ruleset, pool->type, | |
4f6a7e5e | 1294 | pool->size); |
f24e9980 SW |
1295 | return NULL; |
1296 | } | |
1297 | ||
83ca14fd SW |
1298 | if (pool->flags & CEPH_POOL_FLAG_HASHPSPOOL) { |
1299 | /* hash pool id and seed sothat pool PGs do not overlap */ | |
1300 | pps = crush_hash32_2(CRUSH_HASH_RJENKINS1, | |
1301 | ceph_stable_mod(pgid.seed, pool->pgp_num, | |
1302 | pool->pgp_num_mask), | |
1303 | pgid.pool); | |
1304 | } else { | |
1305 | /* | |
1306 | * legacy ehavior: add ps and pool together. this is | |
1307 | * not a great approach because the PGs from each pool | |
1308 | * will overlap on top of each other: 0.5 == 1.4 == | |
1309 | * 2.3 == ... | |
1310 | */ | |
1311 | pps = ceph_stable_mod(pgid.seed, pool->pgp_num, | |
1312 | pool->pgp_num_mask) + | |
1313 | (unsigned)pgid.pool; | |
1314 | } | |
9d521470 ID |
1315 | r = do_crush(osdmap, ruleno, pps, osds, min_t(int, pool->size, *num), |
1316 | osdmap->osd_weight, osdmap->max_osd); | |
8b393269 | 1317 | if (r < 0) { |
83ca14fd SW |
1318 | pr_err("error %d from crush rule: pool %lld ruleset %d type %d" |
1319 | " size %d\n", r, pgid.pool, pool->crush_ruleset, | |
4f6a7e5e | 1320 | pool->type, pool->size); |
8b393269 SW |
1321 | return NULL; |
1322 | } | |
1323 | *num = r; | |
f24e9980 SW |
1324 | return osds; |
1325 | } | |
1326 | ||
d85b7056 SW |
1327 | /* |
1328 | * Return acting set for given pgid. | |
1329 | */ | |
5b191d99 | 1330 | int ceph_calc_pg_acting(struct ceph_osdmap *osdmap, struct ceph_pg pgid, |
d85b7056 SW |
1331 | int *acting) |
1332 | { | |
1333 | int rawosds[CEPH_PG_MAX_SIZE], *osds; | |
1334 | int i, o, num = CEPH_PG_MAX_SIZE; | |
1335 | ||
1336 | osds = calc_pg_raw(osdmap, pgid, rawosds, &num); | |
1337 | if (!osds) | |
1338 | return -1; | |
1339 | ||
1340 | /* primary is first up osd */ | |
1341 | o = 0; | |
1342 | for (i = 0; i < num; i++) | |
1343 | if (ceph_osd_is_up(osdmap, osds[i])) | |
1344 | acting[o++] = osds[i]; | |
1345 | return o; | |
1346 | } | |
1347 | ||
f24e9980 SW |
1348 | /* |
1349 | * Return primary osd for given pgid, or -1 if none. | |
1350 | */ | |
5b191d99 | 1351 | int ceph_calc_pg_primary(struct ceph_osdmap *osdmap, struct ceph_pg pgid) |
f24e9980 | 1352 | { |
d85b7056 SW |
1353 | int rawosds[CEPH_PG_MAX_SIZE], *osds; |
1354 | int i, num = CEPH_PG_MAX_SIZE; | |
f24e9980 SW |
1355 | |
1356 | osds = calc_pg_raw(osdmap, pgid, rawosds, &num); | |
1357 | if (!osds) | |
1358 | return -1; | |
1359 | ||
1360 | /* primary is first up osd */ | |
1361 | for (i = 0; i < num; i++) | |
d85b7056 | 1362 | if (ceph_osd_is_up(osdmap, osds[i])) |
f24e9980 | 1363 | return osds[i]; |
f24e9980 SW |
1364 | return -1; |
1365 | } | |
3d14c5d2 | 1366 | EXPORT_SYMBOL(ceph_calc_pg_primary); |