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 | ||
72afc71f AE |
467 | const 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 | } | |
481 | EXPORT_SYMBOL(ceph_pg_pool_name_by_id); | |
482 | ||
7669a2c9 YS |
483 | int 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 | } | |
3d14c5d2 | 495 | EXPORT_SYMBOL(ceph_pg_poolid_by_name); |
7669a2c9 | 496 | |
2844a76a SW |
497 | static 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 | ||
73a7e693 | 504 | static int __decode_pool(void **p, void *end, struct ceph_pg_pool_info *pi) |
efd7576b | 505 | { |
4f6a7e5e SW |
506 | u8 ev, cv; |
507 | unsigned len, num; | |
508 | void *pool_end; | |
509 | ||
510 | 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; | |
73a7e693 | 524 | |
4f6a7e5e SW |
525 | 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); | |
73a7e693 | 529 | |
4f6a7e5e SW |
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; | |
73a7e693 SW |
544 | } |
545 | ||
4f6a7e5e SW |
546 | /* 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); | |
73a7e693 SW |
557 | return 0; |
558 | ||
559 | bad: | |
560 | return -EINVAL; | |
efd7576b SW |
561 | } |
562 | ||
2844a76a SW |
563 | static int __decode_pool_names(void **p, void *end, struct ceph_osdmap *map) |
564 | { | |
565 | struct ceph_pg_pool_info *pi; | |
4f6a7e5e SW |
566 | u32 num, len; |
567 | u64 pool; | |
2844a76a SW |
568 | |
569 | ceph_decode_32_safe(p, end, num, bad); | |
570 | dout(" %d pool names\n", num); | |
571 | while (num--) { | |
4f6a7e5e | 572 | ceph_decode_64_safe(p, end, pool, bad); |
2844a76a | 573 | ceph_decode_32_safe(p, end, len, bad); |
4f6a7e5e | 574 | dout(" pool %llu len %d\n", pool, len); |
ad3b904c | 575 | ceph_decode_need(p, end, len, bad); |
2844a76a SW |
576 | pi = __lookup_pg_pool(&map->pg_pools, pool); |
577 | if (pi) { | |
ad3b904c XW |
578 | char *name = kstrndup(*p, len, GFP_NOFS); |
579 | ||
580 | if (!name) | |
581 | return -ENOMEM; | |
2844a76a | 582 | kfree(pi->name); |
ad3b904c XW |
583 | pi->name = name; |
584 | dout(" name is %s\n", pi->name); | |
2844a76a SW |
585 | } |
586 | *p += len; | |
587 | } | |
588 | return 0; | |
589 | ||
590 | bad: | |
591 | return -EINVAL; | |
592 | } | |
593 | ||
594 | /* | |
595 | * osd map | |
596 | */ | |
597 | void 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 | */ | |
624 | static 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 | ||
f24e9980 SW |
657 | /* |
658 | * decode a full map. | |
659 | */ | |
660 | struct ceph_osdmap *osdmap_decode(void **p, void *end) | |
661 | { | |
662 | struct ceph_osdmap *map; | |
663 | u16 version; | |
664 | u32 len, max, i; | |
665 | int err = -EINVAL; | |
666 | void *start = *p; | |
4fc51be8 | 667 | struct ceph_pg_pool_info *pi; |
f24e9980 SW |
668 | |
669 | dout("osdmap_decode %p to %p len %d\n", *p, end, (int)(end - *p)); | |
670 | ||
671 | map = kzalloc(sizeof(*map), GFP_NOFS); | |
672 | if (map == NULL) | |
673 | return ERR_PTR(-ENOMEM); | |
674 | map->pg_temp = RB_ROOT; | |
675 | ||
676 | ceph_decode_16_safe(p, end, version, bad); | |
4f6a7e5e SW |
677 | if (version > 6) { |
678 | pr_warning("got unknown v %d > 6 of osdmap\n", version); | |
679 | goto bad; | |
680 | } | |
681 | if (version < 6) { | |
682 | pr_warning("got old v %d < 6 of osdmap\n", version); | |
02f90c61 SW |
683 | goto bad; |
684 | } | |
f24e9980 SW |
685 | |
686 | ceph_decode_need(p, end, 2*sizeof(u64)+6*sizeof(u32), bad); | |
687 | ceph_decode_copy(p, &map->fsid, sizeof(map->fsid)); | |
c89136ea | 688 | map->epoch = ceph_decode_32(p); |
f24e9980 SW |
689 | ceph_decode_copy(p, &map->created, sizeof(map->created)); |
690 | ceph_decode_copy(p, &map->modified, sizeof(map->modified)); | |
691 | ||
f24e9980 SW |
692 | ceph_decode_32_safe(p, end, max, bad); |
693 | while (max--) { | |
4f6a7e5e | 694 | ceph_decode_need(p, end, 8 + 2, bad); |
0ed7285e | 695 | err = -ENOMEM; |
2844a76a | 696 | pi = kzalloc(sizeof(*pi), GFP_NOFS); |
4fc51be8 | 697 | if (!pi) |
f24e9980 | 698 | goto bad; |
4f6a7e5e | 699 | pi->id = ceph_decode_64(p); |
73a7e693 | 700 | err = __decode_pool(p, end, pi); |
b0aee351 JJ |
701 | if (err < 0) { |
702 | kfree(pi); | |
73a7e693 | 703 | goto bad; |
b0aee351 | 704 | } |
4fc51be8 | 705 | __insert_pg_pool(&map->pg_pools, pi); |
f24e9980 | 706 | } |
2844a76a | 707 | |
4f6a7e5e SW |
708 | err = __decode_pool_names(p, end, map); |
709 | if (err < 0) { | |
710 | dout("fail to decode pool names"); | |
711 | goto bad; | |
0ed7285e | 712 | } |
2844a76a | 713 | |
4fc51be8 | 714 | ceph_decode_32_safe(p, end, map->pool_max, bad); |
f24e9980 SW |
715 | |
716 | ceph_decode_32_safe(p, end, map->flags, bad); | |
717 | ||
c89136ea | 718 | max = ceph_decode_32(p); |
f24e9980 SW |
719 | |
720 | /* (re)alloc osd arrays */ | |
721 | err = osdmap_set_max_osd(map, max); | |
722 | if (err < 0) | |
723 | goto bad; | |
724 | dout("osdmap_decode max_osd = %d\n", map->max_osd); | |
725 | ||
726 | /* osds */ | |
727 | err = -EINVAL; | |
728 | ceph_decode_need(p, end, 3*sizeof(u32) + | |
729 | map->max_osd*(1 + sizeof(*map->osd_weight) + | |
730 | sizeof(*map->osd_addr)), bad); | |
731 | *p += 4; /* skip length field (should match max) */ | |
732 | ceph_decode_copy(p, map->osd_state, map->max_osd); | |
733 | ||
734 | *p += 4; /* skip length field (should match max) */ | |
735 | for (i = 0; i < map->max_osd; i++) | |
c89136ea | 736 | map->osd_weight[i] = ceph_decode_32(p); |
f24e9980 SW |
737 | |
738 | *p += 4; /* skip length field (should match max) */ | |
739 | ceph_decode_copy(p, map->osd_addr, map->max_osd*sizeof(*map->osd_addr)); | |
63f2d211 SW |
740 | for (i = 0; i < map->max_osd; i++) |
741 | ceph_decode_addr(&map->osd_addr[i]); | |
f24e9980 SW |
742 | |
743 | /* pg_temp */ | |
744 | ceph_decode_32_safe(p, end, len, bad); | |
745 | for (i = 0; i < len; i++) { | |
746 | int n, j; | |
5b191d99 | 747 | struct ceph_pg pgid; |
f24e9980 SW |
748 | struct ceph_pg_mapping *pg; |
749 | ||
ef4859d6 | 750 | err = ceph_decode_pgid(p, end, &pgid); |
d6c0dd6b SW |
751 | if (err) |
752 | goto bad; | |
753 | ceph_decode_need(p, end, sizeof(u32), bad); | |
c89136ea | 754 | n = ceph_decode_32(p); |
e91a9b63 XW |
755 | err = -EINVAL; |
756 | if (n > (UINT_MAX - sizeof(*pg)) / sizeof(u32)) | |
757 | goto bad; | |
f24e9980 | 758 | ceph_decode_need(p, end, n * sizeof(u32), bad); |
30dc6381 | 759 | err = -ENOMEM; |
f24e9980 | 760 | pg = kmalloc(sizeof(*pg) + n*sizeof(u32), GFP_NOFS); |
30dc6381 | 761 | if (!pg) |
f24e9980 | 762 | goto bad; |
f24e9980 SW |
763 | pg->pgid = pgid; |
764 | pg->len = n; | |
765 | for (j = 0; j < n; j++) | |
c89136ea | 766 | pg->osds[j] = ceph_decode_32(p); |
f24e9980 | 767 | |
991abb6e SW |
768 | err = __insert_pg_mapping(pg, &map->pg_temp); |
769 | if (err) | |
770 | goto bad; | |
5b191d99 SW |
771 | dout(" added pg_temp %lld.%x len %d\n", pgid.pool, pgid.seed, |
772 | len); | |
f24e9980 SW |
773 | } |
774 | ||
775 | /* crush */ | |
776 | ceph_decode_32_safe(p, end, len, bad); | |
777 | dout("osdmap_decode crush len %d from off 0x%x\n", len, | |
778 | (int)(*p - start)); | |
779 | ceph_decode_need(p, end, len, bad); | |
780 | map->crush = crush_decode(*p, end); | |
781 | *p += len; | |
782 | if (IS_ERR(map->crush)) { | |
783 | err = PTR_ERR(map->crush); | |
784 | map->crush = NULL; | |
785 | goto bad; | |
786 | } | |
787 | ||
788 | /* ignore the rest of the map */ | |
789 | *p = end; | |
790 | ||
791 | dout("osdmap_decode done %p %p\n", *p, end); | |
792 | return map; | |
793 | ||
794 | bad: | |
0ed7285e | 795 | dout("osdmap_decode fail err %d\n", err); |
f24e9980 SW |
796 | ceph_osdmap_destroy(map); |
797 | return ERR_PTR(err); | |
798 | } | |
799 | ||
800 | /* | |
801 | * decode and apply an incremental map update. | |
802 | */ | |
803 | struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end, | |
804 | struct ceph_osdmap *map, | |
805 | struct ceph_messenger *msgr) | |
806 | { | |
f24e9980 SW |
807 | struct crush_map *newcrush = NULL; |
808 | struct ceph_fsid fsid; | |
809 | u32 epoch = 0; | |
810 | struct ceph_timespec modified; | |
4f6a7e5e SW |
811 | s32 len; |
812 | u64 pool; | |
813 | __s64 new_pool_max; | |
814 | __s32 new_flags, max; | |
f24e9980 SW |
815 | void *start = *p; |
816 | int err = -EINVAL; | |
817 | u16 version; | |
f24e9980 SW |
818 | |
819 | ceph_decode_16_safe(p, end, version, bad); | |
d6c0dd6b SW |
820 | if (version != 6) { |
821 | pr_warning("got unknown v %d != 6 of inc osdmap\n", version); | |
02f90c61 SW |
822 | goto bad; |
823 | } | |
f24e9980 SW |
824 | |
825 | ceph_decode_need(p, end, sizeof(fsid)+sizeof(modified)+2*sizeof(u32), | |
826 | bad); | |
827 | ceph_decode_copy(p, &fsid, sizeof(fsid)); | |
c89136ea | 828 | epoch = ceph_decode_32(p); |
f24e9980 SW |
829 | BUG_ON(epoch != map->epoch+1); |
830 | ceph_decode_copy(p, &modified, sizeof(modified)); | |
4f6a7e5e | 831 | new_pool_max = ceph_decode_64(p); |
c89136ea | 832 | new_flags = ceph_decode_32(p); |
f24e9980 SW |
833 | |
834 | /* full map? */ | |
835 | ceph_decode_32_safe(p, end, len, bad); | |
836 | if (len > 0) { | |
837 | dout("apply_incremental full map len %d, %p to %p\n", | |
838 | len, *p, end); | |
30dc6381 | 839 | return osdmap_decode(p, min(*p+len, end)); |
f24e9980 SW |
840 | } |
841 | ||
842 | /* new crush? */ | |
843 | ceph_decode_32_safe(p, end, len, bad); | |
844 | if (len > 0) { | |
845 | dout("apply_incremental new crush map len %d, %p to %p\n", | |
846 | len, *p, end); | |
847 | newcrush = crush_decode(*p, min(*p+len, end)); | |
848 | if (IS_ERR(newcrush)) | |
7e34bc52 | 849 | return ERR_CAST(newcrush); |
cebc5be6 | 850 | *p += len; |
f24e9980 SW |
851 | } |
852 | ||
853 | /* new flags? */ | |
854 | if (new_flags >= 0) | |
855 | map->flags = new_flags; | |
4fc51be8 SW |
856 | if (new_pool_max >= 0) |
857 | map->pool_max = new_pool_max; | |
f24e9980 SW |
858 | |
859 | ceph_decode_need(p, end, 5*sizeof(u32), bad); | |
860 | ||
861 | /* new max? */ | |
c89136ea | 862 | max = ceph_decode_32(p); |
f24e9980 SW |
863 | if (max >= 0) { |
864 | err = osdmap_set_max_osd(map, max); | |
865 | if (err < 0) | |
866 | goto bad; | |
867 | } | |
868 | ||
869 | map->epoch++; | |
31456665 | 870 | map->modified = modified; |
f24e9980 SW |
871 | if (newcrush) { |
872 | if (map->crush) | |
873 | crush_destroy(map->crush); | |
874 | map->crush = newcrush; | |
875 | newcrush = NULL; | |
876 | } | |
877 | ||
878 | /* new_pool */ | |
879 | ceph_decode_32_safe(p, end, len, bad); | |
880 | while (len--) { | |
4fc51be8 | 881 | struct ceph_pg_pool_info *pi; |
361be860 | 882 | |
4f6a7e5e | 883 | ceph_decode_64_safe(p, end, pool, bad); |
4fc51be8 SW |
884 | pi = __lookup_pg_pool(&map->pg_pools, pool); |
885 | if (!pi) { | |
2844a76a | 886 | pi = kzalloc(sizeof(*pi), GFP_NOFS); |
4fc51be8 SW |
887 | if (!pi) { |
888 | err = -ENOMEM; | |
889 | goto bad; | |
890 | } | |
891 | pi->id = pool; | |
892 | __insert_pg_pool(&map->pg_pools, pi); | |
893 | } | |
73a7e693 SW |
894 | err = __decode_pool(p, end, pi); |
895 | if (err < 0) | |
896 | goto bad; | |
f24e9980 | 897 | } |
0ed7285e SW |
898 | if (version >= 5) { |
899 | err = __decode_pool_names(p, end, map); | |
900 | if (err < 0) | |
901 | goto bad; | |
902 | } | |
f24e9980 | 903 | |
4fc51be8 | 904 | /* old_pool */ |
f24e9980 | 905 | ceph_decode_32_safe(p, end, len, bad); |
4fc51be8 SW |
906 | while (len--) { |
907 | struct ceph_pg_pool_info *pi; | |
908 | ||
4f6a7e5e | 909 | ceph_decode_64_safe(p, end, pool, bad); |
4fc51be8 | 910 | pi = __lookup_pg_pool(&map->pg_pools, pool); |
2844a76a SW |
911 | if (pi) |
912 | __remove_pg_pool(&map->pg_pools, pi); | |
4fc51be8 | 913 | } |
f24e9980 SW |
914 | |
915 | /* new_up */ | |
916 | err = -EINVAL; | |
917 | ceph_decode_32_safe(p, end, len, bad); | |
918 | while (len--) { | |
919 | u32 osd; | |
920 | struct ceph_entity_addr addr; | |
921 | ceph_decode_32_safe(p, end, osd, bad); | |
922 | ceph_decode_copy_safe(p, end, &addr, sizeof(addr), bad); | |
63f2d211 | 923 | ceph_decode_addr(&addr); |
f24e9980 SW |
924 | pr_info("osd%d up\n", osd); |
925 | BUG_ON(osd >= map->max_osd); | |
926 | map->osd_state[osd] |= CEPH_OSD_UP; | |
927 | map->osd_addr[osd] = addr; | |
928 | } | |
929 | ||
7662d8ff | 930 | /* new_state */ |
f24e9980 SW |
931 | ceph_decode_32_safe(p, end, len, bad); |
932 | while (len--) { | |
933 | u32 osd; | |
7662d8ff | 934 | u8 xorstate; |
f24e9980 | 935 | ceph_decode_32_safe(p, end, osd, bad); |
7662d8ff | 936 | xorstate = **(u8 **)p; |
f24e9980 | 937 | (*p)++; /* clean flag */ |
7662d8ff SW |
938 | if (xorstate == 0) |
939 | xorstate = CEPH_OSD_UP; | |
940 | if (xorstate & CEPH_OSD_UP) | |
941 | pr_info("osd%d down\n", osd); | |
f24e9980 | 942 | if (osd < map->max_osd) |
7662d8ff | 943 | map->osd_state[osd] ^= xorstate; |
f24e9980 SW |
944 | } |
945 | ||
946 | /* new_weight */ | |
947 | ceph_decode_32_safe(p, end, len, bad); | |
948 | while (len--) { | |
949 | u32 osd, off; | |
950 | ceph_decode_need(p, end, sizeof(u32)*2, bad); | |
c89136ea SW |
951 | osd = ceph_decode_32(p); |
952 | off = ceph_decode_32(p); | |
f24e9980 SW |
953 | pr_info("osd%d weight 0x%x %s\n", osd, off, |
954 | off == CEPH_OSD_IN ? "(in)" : | |
955 | (off == CEPH_OSD_OUT ? "(out)" : "")); | |
956 | if (osd < map->max_osd) | |
957 | map->osd_weight[osd] = off; | |
958 | } | |
959 | ||
960 | /* new_pg_temp */ | |
f24e9980 SW |
961 | ceph_decode_32_safe(p, end, len, bad); |
962 | while (len--) { | |
963 | struct ceph_pg_mapping *pg; | |
964 | int j; | |
5b191d99 | 965 | struct ceph_pg pgid; |
f24e9980 | 966 | u32 pglen; |
f24e9980 | 967 | |
ef4859d6 | 968 | err = ceph_decode_pgid(p, end, &pgid); |
d6c0dd6b SW |
969 | if (err) |
970 | goto bad; | |
971 | ceph_decode_need(p, end, sizeof(u32), bad); | |
972 | pglen = ceph_decode_32(p); | |
f24e9980 | 973 | if (pglen) { |
f24e9980 | 974 | ceph_decode_need(p, end, pglen*sizeof(u32), bad); |
6bd9adbd SW |
975 | |
976 | /* removing existing (if any) */ | |
977 | (void) __remove_pg_mapping(&map->pg_temp, pgid); | |
978 | ||
979 | /* insert */ | |
0ed7285e SW |
980 | err = -EINVAL; |
981 | if (pglen > (UINT_MAX - sizeof(*pg)) / sizeof(u32)) | |
a5506049 | 982 | goto bad; |
0ed7285e | 983 | err = -ENOMEM; |
f24e9980 | 984 | pg = kmalloc(sizeof(*pg) + sizeof(u32)*pglen, GFP_NOFS); |
0ed7285e | 985 | if (!pg) |
f24e9980 | 986 | goto bad; |
f24e9980 SW |
987 | pg->pgid = pgid; |
988 | pg->len = pglen; | |
7067f797 | 989 | for (j = 0; j < pglen; j++) |
c89136ea | 990 | pg->osds[j] = ceph_decode_32(p); |
991abb6e | 991 | err = __insert_pg_mapping(pg, &map->pg_temp); |
bc4fdca8 SW |
992 | if (err) { |
993 | kfree(pg); | |
991abb6e | 994 | goto bad; |
bc4fdca8 | 995 | } |
5b191d99 SW |
996 | dout(" added pg_temp %lld.%x len %d\n", pgid.pool, |
997 | pgid.seed, pglen); | |
8adc8b3d SW |
998 | } else { |
999 | /* remove */ | |
1000 | __remove_pg_mapping(&map->pg_temp, pgid); | |
f24e9980 SW |
1001 | } |
1002 | } | |
f24e9980 SW |
1003 | |
1004 | /* ignore the rest */ | |
1005 | *p = end; | |
1006 | return map; | |
1007 | ||
1008 | bad: | |
1009 | pr_err("corrupt inc osdmap epoch %d off %d (%p of %p-%p)\n", | |
1010 | epoch, (int)(*p - start), *p, start, end); | |
9ec7cab1 SW |
1011 | print_hex_dump(KERN_DEBUG, "osdmap: ", |
1012 | DUMP_PREFIX_OFFSET, 16, 1, | |
1013 | start, end - start, true); | |
f24e9980 SW |
1014 | if (newcrush) |
1015 | crush_destroy(newcrush); | |
1016 | return ERR_PTR(err); | |
1017 | } | |
1018 | ||
1019 | ||
1020 | ||
1021 | ||
1022 | /* | |
1023 | * calculate file layout from given offset, length. | |
1024 | * fill in correct oid, logical length, and object extent | |
1025 | * offset, length. | |
1026 | * | |
1027 | * for now, we write only a single su, until we can | |
1028 | * pass a stride back to the caller. | |
1029 | */ | |
d63b77f4 | 1030 | int ceph_calc_file_object_mapping(struct ceph_file_layout *layout, |
e8afad65 | 1031 | u64 off, u64 len, |
645a1025 | 1032 | u64 *ono, |
f24e9980 SW |
1033 | u64 *oxoff, u64 *oxlen) |
1034 | { | |
1035 | u32 osize = le32_to_cpu(layout->fl_object_size); | |
1036 | u32 su = le32_to_cpu(layout->fl_stripe_unit); | |
1037 | u32 sc = le32_to_cpu(layout->fl_stripe_count); | |
1038 | u32 bl, stripeno, stripepos, objsetno; | |
1039 | u32 su_per_object; | |
ff1d1f71 | 1040 | u64 t, su_offset; |
f24e9980 | 1041 | |
e8afad65 | 1042 | dout("mapping %llu~%llu osize %u fl_su %u\n", off, len, |
f24e9980 | 1043 | osize, su); |
d63b77f4 SW |
1044 | if (su == 0 || sc == 0) |
1045 | goto invalid; | |
35e054a6 | 1046 | su_per_object = osize / su; |
d63b77f4 SW |
1047 | if (su_per_object == 0) |
1048 | goto invalid; | |
f24e9980 SW |
1049 | dout("osize %u / su %u = su_per_object %u\n", osize, su, |
1050 | su_per_object); | |
1051 | ||
d63b77f4 SW |
1052 | if ((su & ~PAGE_MASK) != 0) |
1053 | goto invalid; | |
1054 | ||
f24e9980 SW |
1055 | /* bl = *off / su; */ |
1056 | t = off; | |
1057 | do_div(t, su); | |
1058 | bl = t; | |
1059 | dout("off %llu / su %u = bl %u\n", off, su, bl); | |
1060 | ||
1061 | stripeno = bl / sc; | |
1062 | stripepos = bl % sc; | |
1063 | objsetno = stripeno / su_per_object; | |
1064 | ||
645a1025 | 1065 | *ono = objsetno * sc + stripepos; |
95c96174 | 1066 | dout("objset %u * sc %u = ono %u\n", objsetno, sc, (unsigned int)*ono); |
645a1025 SW |
1067 | |
1068 | /* *oxoff = *off % layout->fl_stripe_unit; # offset in su */ | |
f24e9980 | 1069 | t = off; |
ff1d1f71 NW |
1070 | su_offset = do_div(t, su); |
1071 | *oxoff = su_offset + (stripeno % su_per_object) * su; | |
1072 | ||
1073 | /* | |
1074 | * Calculate the length of the extent being written to the selected | |
e8afad65 | 1075 | * object. This is the minimum of the full length requested (len) or |
ff1d1f71 NW |
1076 | * the remainder of the current stripe being written to. |
1077 | */ | |
e8afad65 | 1078 | *oxlen = min_t(u64, len, su - su_offset); |
f24e9980 SW |
1079 | |
1080 | dout(" obj extent %llu~%llu\n", *oxoff, *oxlen); | |
d63b77f4 SW |
1081 | return 0; |
1082 | ||
1083 | invalid: | |
1084 | dout(" invalid layout\n"); | |
1085 | *ono = 0; | |
1086 | *oxoff = 0; | |
1087 | *oxlen = 0; | |
1088 | return -EINVAL; | |
f24e9980 | 1089 | } |
3d14c5d2 | 1090 | EXPORT_SYMBOL(ceph_calc_file_object_mapping); |
f24e9980 SW |
1091 | |
1092 | /* | |
1093 | * calculate an object layout (i.e. pgid) from an oid, | |
1094 | * file_layout, and osdmap | |
1095 | */ | |
41766f87 AE |
1096 | int ceph_calc_ceph_pg(struct ceph_pg *pg, const char *oid, |
1097 | struct ceph_osdmap *osdmap, uint64_t pool) | |
f24e9980 | 1098 | { |
41766f87 | 1099 | struct ceph_pg_pool_info *pool_info; |
f24e9980 | 1100 | |
30dc6381 | 1101 | BUG_ON(!osdmap); |
41766f87 AE |
1102 | pool_info = __lookup_pg_pool(&osdmap->pg_pools, pool); |
1103 | if (!pool_info) | |
4fc51be8 | 1104 | return -EIO; |
41766f87 AE |
1105 | pg->pool = pool; |
1106 | pg->seed = ceph_str_hash(pool_info->object_hash, oid, strlen(oid)); | |
f24e9980 | 1107 | |
41766f87 | 1108 | dout("%s '%s' pgid %lld.%x\n", __func__, oid, pg->pool, pg->seed); |
f24e9980 SW |
1109 | return 0; |
1110 | } | |
41766f87 | 1111 | EXPORT_SYMBOL(ceph_calc_ceph_pg); |
f24e9980 SW |
1112 | |
1113 | /* | |
1114 | * Calculate raw osd vector for the given pgid. Return pointer to osd | |
1115 | * array, or NULL on failure. | |
1116 | */ | |
5b191d99 | 1117 | static int *calc_pg_raw(struct ceph_osdmap *osdmap, struct ceph_pg pgid, |
f24e9980 SW |
1118 | int *osds, int *num) |
1119 | { | |
f24e9980 SW |
1120 | struct ceph_pg_mapping *pg; |
1121 | struct ceph_pg_pool_info *pool; | |
1122 | int ruleno; | |
83ca14fd SW |
1123 | int r; |
1124 | u32 pps; | |
f24e9980 | 1125 | |
83ca14fd | 1126 | pool = __lookup_pg_pool(&osdmap->pg_pools, pgid.pool); |
782e182e SW |
1127 | if (!pool) |
1128 | return NULL; | |
1129 | ||
f24e9980 | 1130 | /* pg_temp? */ |
83ca14fd | 1131 | pgid.seed = ceph_stable_mod(pgid.seed, pool->pg_num, |
9542cf0b | 1132 | pool->pg_num_mask); |
9794b146 SW |
1133 | pg = __lookup_pg_mapping(&osdmap->pg_temp, pgid); |
1134 | if (pg) { | |
1135 | *num = pg->len; | |
1136 | return pg->osds; | |
f24e9980 SW |
1137 | } |
1138 | ||
1139 | /* crush */ | |
4f6a7e5e SW |
1140 | ruleno = crush_find_rule(osdmap->crush, pool->crush_ruleset, |
1141 | pool->type, pool->size); | |
f24e9980 | 1142 | if (ruleno < 0) { |
83ca14fd SW |
1143 | pr_err("no crush rule pool %lld ruleset %d type %d size %d\n", |
1144 | pgid.pool, pool->crush_ruleset, pool->type, | |
4f6a7e5e | 1145 | pool->size); |
f24e9980 SW |
1146 | return NULL; |
1147 | } | |
1148 | ||
83ca14fd SW |
1149 | if (pool->flags & CEPH_POOL_FLAG_HASHPSPOOL) { |
1150 | /* hash pool id and seed sothat pool PGs do not overlap */ | |
1151 | pps = crush_hash32_2(CRUSH_HASH_RJENKINS1, | |
1152 | ceph_stable_mod(pgid.seed, pool->pgp_num, | |
1153 | pool->pgp_num_mask), | |
1154 | pgid.pool); | |
1155 | } else { | |
1156 | /* | |
1157 | * legacy ehavior: add ps and pool together. this is | |
1158 | * not a great approach because the PGs from each pool | |
1159 | * will overlap on top of each other: 0.5 == 1.4 == | |
1160 | * 2.3 == ... | |
1161 | */ | |
1162 | pps = ceph_stable_mod(pgid.seed, pool->pgp_num, | |
1163 | pool->pgp_num_mask) + | |
1164 | (unsigned)pgid.pool; | |
1165 | } | |
8b393269 | 1166 | r = crush_do_rule(osdmap->crush, ruleno, pps, osds, |
4f6a7e5e | 1167 | min_t(int, pool->size, *num), |
8b393269 SW |
1168 | osdmap->osd_weight); |
1169 | if (r < 0) { | |
83ca14fd SW |
1170 | pr_err("error %d from crush rule: pool %lld ruleset %d type %d" |
1171 | " size %d\n", r, pgid.pool, pool->crush_ruleset, | |
4f6a7e5e | 1172 | pool->type, pool->size); |
8b393269 SW |
1173 | return NULL; |
1174 | } | |
1175 | *num = r; | |
f24e9980 SW |
1176 | return osds; |
1177 | } | |
1178 | ||
d85b7056 SW |
1179 | /* |
1180 | * Return acting set for given pgid. | |
1181 | */ | |
5b191d99 | 1182 | int ceph_calc_pg_acting(struct ceph_osdmap *osdmap, struct ceph_pg pgid, |
d85b7056 SW |
1183 | int *acting) |
1184 | { | |
1185 | int rawosds[CEPH_PG_MAX_SIZE], *osds; | |
1186 | int i, o, num = CEPH_PG_MAX_SIZE; | |
1187 | ||
1188 | osds = calc_pg_raw(osdmap, pgid, rawosds, &num); | |
1189 | if (!osds) | |
1190 | return -1; | |
1191 | ||
1192 | /* primary is first up osd */ | |
1193 | o = 0; | |
1194 | for (i = 0; i < num; i++) | |
1195 | if (ceph_osd_is_up(osdmap, osds[i])) | |
1196 | acting[o++] = osds[i]; | |
1197 | return o; | |
1198 | } | |
1199 | ||
f24e9980 SW |
1200 | /* |
1201 | * Return primary osd for given pgid, or -1 if none. | |
1202 | */ | |
5b191d99 | 1203 | int ceph_calc_pg_primary(struct ceph_osdmap *osdmap, struct ceph_pg pgid) |
f24e9980 | 1204 | { |
d85b7056 SW |
1205 | int rawosds[CEPH_PG_MAX_SIZE], *osds; |
1206 | int i, num = CEPH_PG_MAX_SIZE; | |
f24e9980 SW |
1207 | |
1208 | osds = calc_pg_raw(osdmap, pgid, rawosds, &num); | |
1209 | if (!osds) | |
1210 | return -1; | |
1211 | ||
1212 | /* primary is first up osd */ | |
1213 | for (i = 0; i < num; i++) | |
d85b7056 | 1214 | if (ceph_osd_is_up(osdmap, osds[i])) |
f24e9980 | 1215 | return osds[i]; |
f24e9980 SW |
1216 | return -1; |
1217 | } | |
3d14c5d2 | 1218 | EXPORT_SYMBOL(ceph_calc_pg_primary); |