blob: f630d10722997d6d76292c21286040e9f0c0bcca [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);
Ilya Dryomov82cd0032015-06-29 19:30:23 +030092 ceph_decode_8_safe(p, end, b->num_nodes, bad);
Sage Weilf24e9982009-10-06 11:31:10 -070093 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
Ilya Dryomov958a2762015-04-14 16:54:52 +0300125static int crush_decode_straw2_bucket(void **p, void *end,
126 struct crush_bucket_straw2 *b)
127{
128 int j;
129 dout("crush_decode_straw2_bucket %p to %p\n", *p, end);
130 b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS);
131 if (b->item_weights == NULL)
132 return -ENOMEM;
133 ceph_decode_need(p, end, b->h.size * sizeof(u32), bad);
134 for (j = 0; j < b->h.size; j++)
135 b->item_weights[j] = ceph_decode_32(p);
136 return 0;
137bad:
138 return -EINVAL;
139}
140
Ilya Dryomov5cf9c4a2017-06-22 19:44:05 +0200141static struct crush_choose_arg_map *alloc_choose_arg_map(void)
142{
143 struct crush_choose_arg_map *arg_map;
144
145 arg_map = kzalloc(sizeof(*arg_map), GFP_NOIO);
146 if (!arg_map)
147 return NULL;
148
149 RB_CLEAR_NODE(&arg_map->node);
150 return arg_map;
151}
152
153static void free_choose_arg_map(struct crush_choose_arg_map *arg_map)
154{
155 if (arg_map) {
156 int i, j;
157
158 WARN_ON(!RB_EMPTY_NODE(&arg_map->node));
159
160 for (i = 0; i < arg_map->size; i++) {
161 struct crush_choose_arg *arg = &arg_map->args[i];
162
163 for (j = 0; j < arg->weight_set_size; j++)
164 kfree(arg->weight_set[j].weights);
165 kfree(arg->weight_set);
166 kfree(arg->ids);
167 }
168 kfree(arg_map->args);
169 kfree(arg_map);
170 }
171}
172
173DEFINE_RB_FUNCS(choose_arg_map, struct crush_choose_arg_map, choose_args_index,
174 node);
175
176void clear_choose_args(struct crush_map *c)
177{
178 while (!RB_EMPTY_ROOT(&c->choose_args)) {
179 struct crush_choose_arg_map *arg_map =
180 rb_entry(rb_first(&c->choose_args),
181 struct crush_choose_arg_map, node);
182
183 erase_choose_arg_map(&c->choose_args, arg_map);
184 free_choose_arg_map(arg_map);
185 }
186}
187
188static u32 *decode_array_32_alloc(void **p, void *end, u32 *plen)
189{
190 u32 *a = NULL;
191 u32 len;
192 int ret;
193
194 ceph_decode_32_safe(p, end, len, e_inval);
195 if (len) {
196 u32 i;
197
198 a = kmalloc_array(len, sizeof(u32), GFP_NOIO);
199 if (!a) {
200 ret = -ENOMEM;
201 goto fail;
202 }
203
204 ceph_decode_need(p, end, len * sizeof(u32), e_inval);
205 for (i = 0; i < len; i++)
206 a[i] = ceph_decode_32(p);
207 }
208
209 *plen = len;
210 return a;
211
212e_inval:
213 ret = -EINVAL;
214fail:
215 kfree(a);
216 return ERR_PTR(ret);
217}
218
219/*
220 * Assumes @arg is zero-initialized.
221 */
222static int decode_choose_arg(void **p, void *end, struct crush_choose_arg *arg)
223{
224 int ret;
225
226 ceph_decode_32_safe(p, end, arg->weight_set_size, e_inval);
227 if (arg->weight_set_size) {
228 u32 i;
229
230 arg->weight_set = kmalloc_array(arg->weight_set_size,
231 sizeof(*arg->weight_set),
232 GFP_NOIO);
233 if (!arg->weight_set)
234 return -ENOMEM;
235
236 for (i = 0; i < arg->weight_set_size; i++) {
237 struct crush_weight_set *w = &arg->weight_set[i];
238
239 w->weights = decode_array_32_alloc(p, end, &w->size);
240 if (IS_ERR(w->weights)) {
241 ret = PTR_ERR(w->weights);
242 w->weights = NULL;
243 return ret;
244 }
245 }
246 }
247
248 arg->ids = decode_array_32_alloc(p, end, &arg->ids_size);
249 if (IS_ERR(arg->ids)) {
250 ret = PTR_ERR(arg->ids);
251 arg->ids = NULL;
252 return ret;
253 }
254
255 return 0;
256
257e_inval:
258 return -EINVAL;
259}
260
261static int decode_choose_args(void **p, void *end, struct crush_map *c)
262{
263 struct crush_choose_arg_map *arg_map = NULL;
264 u32 num_choose_arg_maps, num_buckets;
265 int ret;
266
267 ceph_decode_32_safe(p, end, num_choose_arg_maps, e_inval);
268 while (num_choose_arg_maps--) {
269 arg_map = alloc_choose_arg_map();
270 if (!arg_map) {
271 ret = -ENOMEM;
272 goto fail;
273 }
274
275 ceph_decode_64_safe(p, end, arg_map->choose_args_index,
276 e_inval);
277 arg_map->size = c->max_buckets;
278 arg_map->args = kcalloc(arg_map->size, sizeof(*arg_map->args),
279 GFP_NOIO);
280 if (!arg_map->args) {
281 ret = -ENOMEM;
282 goto fail;
283 }
284
285 ceph_decode_32_safe(p, end, num_buckets, e_inval);
286 while (num_buckets--) {
287 struct crush_choose_arg *arg;
288 u32 bucket_index;
289
290 ceph_decode_32_safe(p, end, bucket_index, e_inval);
291 if (bucket_index >= arg_map->size)
292 goto e_inval;
293
294 arg = &arg_map->args[bucket_index];
295 ret = decode_choose_arg(p, end, arg);
296 if (ret)
297 goto fail;
298 }
299
300 insert_choose_arg_map(&c->choose_args, arg_map);
301 }
302
303 return 0;
304
305e_inval:
306 ret = -EINVAL;
307fail:
308 free_choose_arg_map(arg_map);
309 return ret;
310}
311
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100312static void crush_finalize(struct crush_map *c)
313{
314 __s32 b;
315
316 /* Space for the array of pointers to per-bucket workspace */
317 c->working_size = sizeof(struct crush_work) +
318 c->max_buckets * sizeof(struct crush_work_bucket *);
319
320 for (b = 0; b < c->max_buckets; b++) {
321 if (!c->buckets[b])
322 continue;
323
324 switch (c->buckets[b]->alg) {
325 default:
326 /*
327 * The base case, permutation variables and
328 * the pointer to the permutation array.
329 */
330 c->working_size += sizeof(struct crush_work_bucket);
331 break;
332 }
333 /* Every bucket has a permutation array. */
334 c->working_size += c->buckets[b]->size * sizeof(__u32);
335 }
336}
337
Sage Weilf24e9982009-10-06 11:31:10 -0700338static struct crush_map *crush_decode(void *pbyval, void *end)
339{
340 struct crush_map *c;
341 int err = -EINVAL;
342 int i, j;
343 void **p = &pbyval;
344 void *start = pbyval;
345 u32 magic;
346
347 dout("crush_decode %p to %p len %d\n", *p, end, (int)(end - *p));
348
349 c = kzalloc(sizeof(*c), GFP_NOFS);
350 if (c == NULL)
351 return ERR_PTR(-ENOMEM);
352
Ilya Dryomov5cf9c4a2017-06-22 19:44:05 +0200353 c->choose_args = RB_ROOT;
354
Sage Weil546f04e2012-07-30 18:15:23 -0700355 /* set tunables to default values */
356 c->choose_local_tries = 2;
357 c->choose_local_fallback_tries = 5;
358 c->choose_total_tries = 19;
Jim Schutt1604f482012-11-30 09:15:25 -0700359 c->chooseleaf_descend_once = 0;
Sage Weil546f04e2012-07-30 18:15:23 -0700360
Sage Weilf24e9982009-10-06 11:31:10 -0700361 ceph_decode_need(p, end, 4*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700362 magic = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700363 if (magic != CRUSH_MAGIC) {
364 pr_err("crush_decode magic %x != current %x\n",
Eric Dumazet95c96172012-04-15 05:58:06 +0000365 (unsigned int)magic, (unsigned int)CRUSH_MAGIC);
Sage Weilf24e9982009-10-06 11:31:10 -0700366 goto bad;
367 }
Sage Weilc89136e2009-10-14 09:59:09 -0700368 c->max_buckets = ceph_decode_32(p);
369 c->max_rules = ceph_decode_32(p);
370 c->max_devices = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700371
Sage Weilf24e9982009-10-06 11:31:10 -0700372 c->buckets = kcalloc(c->max_buckets, sizeof(*c->buckets), GFP_NOFS);
373 if (c->buckets == NULL)
374 goto badmem;
375 c->rules = kcalloc(c->max_rules, sizeof(*c->rules), GFP_NOFS);
376 if (c->rules == NULL)
377 goto badmem;
378
379 /* buckets */
380 for (i = 0; i < c->max_buckets; i++) {
381 int size = 0;
382 u32 alg;
383 struct crush_bucket *b;
384
385 ceph_decode_32_safe(p, end, alg, bad);
386 if (alg == 0) {
387 c->buckets[i] = NULL;
388 continue;
389 }
390 dout("crush_decode bucket %d off %x %p to %p\n",
391 i, (int)(*p-start), *p, end);
392
393 switch (alg) {
394 case CRUSH_BUCKET_UNIFORM:
395 size = sizeof(struct crush_bucket_uniform);
396 break;
397 case CRUSH_BUCKET_LIST:
398 size = sizeof(struct crush_bucket_list);
399 break;
400 case CRUSH_BUCKET_TREE:
401 size = sizeof(struct crush_bucket_tree);
402 break;
403 case CRUSH_BUCKET_STRAW:
404 size = sizeof(struct crush_bucket_straw);
405 break;
Ilya Dryomov958a2762015-04-14 16:54:52 +0300406 case CRUSH_BUCKET_STRAW2:
407 size = sizeof(struct crush_bucket_straw2);
408 break;
Sage Weilf24e9982009-10-06 11:31:10 -0700409 default:
Sage Weil30dc6382009-12-21 14:49:37 -0800410 err = -EINVAL;
Sage Weilf24e9982009-10-06 11:31:10 -0700411 goto bad;
412 }
413 BUG_ON(size == 0);
414 b = c->buckets[i] = kzalloc(size, GFP_NOFS);
415 if (b == NULL)
416 goto badmem;
417
418 ceph_decode_need(p, end, 4*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700419 b->id = ceph_decode_32(p);
420 b->type = ceph_decode_16(p);
Sage Weilfb690392009-11-07 20:18:22 -0800421 b->alg = ceph_decode_8(p);
422 b->hash = ceph_decode_8(p);
Sage Weilc89136e2009-10-14 09:59:09 -0700423 b->weight = ceph_decode_32(p);
424 b->size = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700425
426 dout("crush_decode bucket size %d off %x %p to %p\n",
427 b->size, (int)(*p-start), *p, end);
428
429 b->items = kcalloc(b->size, sizeof(__s32), GFP_NOFS);
430 if (b->items == NULL)
431 goto badmem;
Sage Weilf24e9982009-10-06 11:31:10 -0700432
433 ceph_decode_need(p, end, b->size*sizeof(u32), bad);
434 for (j = 0; j < b->size; j++)
Sage Weilc89136e2009-10-14 09:59:09 -0700435 b->items[j] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700436
437 switch (b->alg) {
438 case CRUSH_BUCKET_UNIFORM:
439 err = crush_decode_uniform_bucket(p, end,
440 (struct crush_bucket_uniform *)b);
441 if (err < 0)
442 goto bad;
443 break;
444 case CRUSH_BUCKET_LIST:
445 err = crush_decode_list_bucket(p, end,
446 (struct crush_bucket_list *)b);
447 if (err < 0)
448 goto bad;
449 break;
450 case CRUSH_BUCKET_TREE:
451 err = crush_decode_tree_bucket(p, end,
452 (struct crush_bucket_tree *)b);
453 if (err < 0)
454 goto bad;
455 break;
456 case CRUSH_BUCKET_STRAW:
457 err = crush_decode_straw_bucket(p, end,
458 (struct crush_bucket_straw *)b);
459 if (err < 0)
460 goto bad;
461 break;
Ilya Dryomov958a2762015-04-14 16:54:52 +0300462 case CRUSH_BUCKET_STRAW2:
463 err = crush_decode_straw2_bucket(p, end,
464 (struct crush_bucket_straw2 *)b);
465 if (err < 0)
466 goto bad;
467 break;
Sage Weilf24e9982009-10-06 11:31:10 -0700468 }
469 }
470
471 /* rules */
472 dout("rule vec is %p\n", c->rules);
473 for (i = 0; i < c->max_rules; i++) {
474 u32 yes;
475 struct crush_rule *r;
476
Dan Carpenter293dffa2017-05-23 17:25:10 +0300477 err = -EINVAL;
Sage Weilf24e9982009-10-06 11:31:10 -0700478 ceph_decode_32_safe(p, end, yes, bad);
479 if (!yes) {
480 dout("crush_decode NO rule %d off %x %p to %p\n",
481 i, (int)(*p-start), *p, end);
482 c->rules[i] = NULL;
483 continue;
484 }
485
486 dout("crush_decode rule %d off %x %p to %p\n",
487 i, (int)(*p-start), *p, end);
488
489 /* len */
490 ceph_decode_32_safe(p, end, yes, bad);
491#if BITS_PER_LONG == 32
Sage Weil30dc6382009-12-21 14:49:37 -0800492 err = -EINVAL;
Xi Wang64486692012-02-16 11:55:48 -0500493 if (yes > (ULONG_MAX - sizeof(*r))
494 / sizeof(struct crush_rule_step))
Sage Weilf24e9982009-10-06 11:31:10 -0700495 goto bad;
496#endif
497 r = c->rules[i] = kmalloc(sizeof(*r) +
498 yes*sizeof(struct crush_rule_step),
499 GFP_NOFS);
500 if (r == NULL)
501 goto badmem;
502 dout(" rule %d is at %p\n", i, r);
503 r->len = yes;
504 ceph_decode_copy_safe(p, end, &r->mask, 4, bad); /* 4 u8's */
505 ceph_decode_need(p, end, r->len*3*sizeof(u32), bad);
506 for (j = 0; j < r->len; j++) {
Sage Weilc89136e2009-10-14 09:59:09 -0700507 r->steps[j].op = ceph_decode_32(p);
508 r->steps[j].arg1 = ceph_decode_32(p);
509 r->steps[j].arg2 = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700510 }
511 }
512
Ilya Dryomov278b1d702017-06-21 17:27:17 +0200513 ceph_decode_skip_map(p, end, 32, string, bad); /* type_map */
514 ceph_decode_skip_map(p, end, 32, string, bad); /* name_map */
515 ceph_decode_skip_map(p, end, 32, string, bad); /* rule_name_map */
Sage Weilf24e9982009-10-06 11:31:10 -0700516
Sage Weil546f04e2012-07-30 18:15:23 -0700517 /* tunables */
518 ceph_decode_need(p, end, 3*sizeof(u32), done);
519 c->choose_local_tries = ceph_decode_32(p);
520 c->choose_local_fallback_tries = ceph_decode_32(p);
521 c->choose_total_tries = ceph_decode_32(p);
Ilya Dryomovb9b519b2016-02-01 16:57:16 +0100522 dout("crush decode tunable choose_local_tries = %d\n",
Sage Weil546f04e2012-07-30 18:15:23 -0700523 c->choose_local_tries);
Ilya Dryomovb9b519b2016-02-01 16:57:16 +0100524 dout("crush decode tunable choose_local_fallback_tries = %d\n",
Sage Weil546f04e2012-07-30 18:15:23 -0700525 c->choose_local_fallback_tries);
Ilya Dryomovb9b519b2016-02-01 16:57:16 +0100526 dout("crush decode tunable choose_total_tries = %d\n",
Sage Weil546f04e2012-07-30 18:15:23 -0700527 c->choose_total_tries);
528
Jim Schutt1604f482012-11-30 09:15:25 -0700529 ceph_decode_need(p, end, sizeof(u32), done);
530 c->chooseleaf_descend_once = ceph_decode_32(p);
Ilya Dryomovb9b519b2016-02-01 16:57:16 +0100531 dout("crush decode tunable chooseleaf_descend_once = %d\n",
Jim Schutt1604f482012-11-30 09:15:25 -0700532 c->chooseleaf_descend_once);
533
Ilya Dryomovf1406622014-05-09 18:27:34 +0400534 ceph_decode_need(p, end, sizeof(u8), done);
535 c->chooseleaf_vary_r = ceph_decode_8(p);
Ilya Dryomovb9b519b2016-02-01 16:57:16 +0100536 dout("crush decode tunable chooseleaf_vary_r = %d\n",
Ilya Dryomovf1406622014-05-09 18:27:34 +0400537 c->chooseleaf_vary_r);
538
Ilya Dryomovb9b519b2016-02-01 16:57:16 +0100539 /* skip straw_calc_version, allowed_bucket_algs */
540 ceph_decode_need(p, end, sizeof(u8) + sizeof(u32), done);
541 *p += sizeof(u8) + sizeof(u32);
542
543 ceph_decode_need(p, end, sizeof(u8), done);
544 c->chooseleaf_stable = ceph_decode_8(p);
545 dout("crush decode tunable chooseleaf_stable = %d\n",
546 c->chooseleaf_stable);
547
Ilya Dryomov5cf9c4a2017-06-22 19:44:05 +0200548 if (*p != end) {
549 /* class_map */
550 ceph_decode_skip_map(p, end, 32, 32, bad);
551 /* class_name */
552 ceph_decode_skip_map(p, end, 32, string, bad);
553 /* class_bucket */
554 ceph_decode_skip_map_of_map(p, end, 32, 32, 32, bad);
555 }
556
557 if (*p != end) {
558 err = decode_choose_args(p, end, c);
559 if (err)
560 goto bad;
561 }
562
Sage Weil546f04e2012-07-30 18:15:23 -0700563done:
Ilya Dryomov9afd30d2017-02-28 18:53:53 +0100564 crush_finalize(c);
Sage Weilf24e9982009-10-06 11:31:10 -0700565 dout("crush_decode success\n");
566 return c;
567
568badmem:
569 err = -ENOMEM;
570bad:
571 dout("crush_decode fail %d\n", err);
572 crush_destroy(c);
573 return ERR_PTR(err);
574}
575
Ilya Dryomovf984cb72016-04-28 16:07:23 +0200576int ceph_pg_compare(const struct ceph_pg *lhs, const struct ceph_pg *rhs)
577{
578 if (lhs->pool < rhs->pool)
579 return -1;
580 if (lhs->pool > rhs->pool)
581 return 1;
582 if (lhs->seed < rhs->seed)
583 return -1;
584 if (lhs->seed > rhs->seed)
585 return 1;
586
587 return 0;
588}
589
Ilya Dryomova02a9462017-06-19 12:18:05 +0200590int ceph_spg_compare(const struct ceph_spg *lhs, const struct ceph_spg *rhs)
591{
592 int ret;
593
594 ret = ceph_pg_compare(&lhs->pgid, &rhs->pgid);
595 if (ret)
596 return ret;
597
598 if (lhs->shard < rhs->shard)
599 return -1;
600 if (lhs->shard > rhs->shard)
601 return 1;
602
603 return 0;
604}
605
Ilya Dryomova303bb02017-06-21 17:27:17 +0200606static struct ceph_pg_mapping *alloc_pg_mapping(size_t payload_len)
607{
608 struct ceph_pg_mapping *pg;
609
610 pg = kmalloc(sizeof(*pg) + payload_len, GFP_NOIO);
611 if (!pg)
612 return NULL;
613
614 RB_CLEAR_NODE(&pg->node);
615 return pg;
616}
617
618static void free_pg_mapping(struct ceph_pg_mapping *pg)
619{
620 WARN_ON(!RB_EMPTY_NODE(&pg->node));
621
622 kfree(pg);
623}
624
Sage Weilf24e9982009-10-06 11:31:10 -0700625/*
Sage Weil9794b142010-02-16 15:53:32 -0800626 * rbtree of pg_mapping for handling pg_temp (explicit mapping of pgid
Ilya Dryomov9686f942014-03-21 19:05:29 +0200627 * to a set of osds) and primary_temp (explicit primary setting)
Sage Weilf24e9982009-10-06 11:31:10 -0700628 */
Ilya Dryomovab751442017-06-21 17:27:17 +0200629DEFINE_RB_FUNCS2(pg_mapping, struct ceph_pg_mapping, pgid, ceph_pg_compare,
630 RB_BYPTR, const struct ceph_pg *, node)
Sage Weil8adc8b32011-09-28 10:11:04 -0700631
Sage Weilf24e9982009-10-06 11:31:10 -0700632/*
Sage Weil4fc51be2010-02-16 15:55:03 -0800633 * rbtree of pg pool info
634 */
635static int __insert_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *new)
636{
637 struct rb_node **p = &root->rb_node;
638 struct rb_node *parent = NULL;
639 struct ceph_pg_pool_info *pi = NULL;
640
641 while (*p) {
642 parent = *p;
643 pi = rb_entry(parent, struct ceph_pg_pool_info, node);
644 if (new->id < pi->id)
645 p = &(*p)->rb_left;
646 else if (new->id > pi->id)
647 p = &(*p)->rb_right;
648 else
649 return -EEXIST;
650 }
651
652 rb_link_node(&new->node, parent, p);
653 rb_insert_color(&new->node, root);
654 return 0;
655}
656
Sage Weil4f6a7e52013-02-23 10:41:09 -0800657static struct ceph_pg_pool_info *__lookup_pg_pool(struct rb_root *root, u64 id)
Sage Weil4fc51be2010-02-16 15:55:03 -0800658{
659 struct ceph_pg_pool_info *pi;
660 struct rb_node *n = root->rb_node;
661
662 while (n) {
663 pi = rb_entry(n, struct ceph_pg_pool_info, node);
664 if (id < pi->id)
665 n = n->rb_left;
666 else if (id > pi->id)
667 n = n->rb_right;
668 else
669 return pi;
670 }
671 return NULL;
672}
673
Ilya Dryomovce7f6a22014-01-27 17:40:19 +0200674struct ceph_pg_pool_info *ceph_pg_pool_by_id(struct ceph_osdmap *map, u64 id)
675{
676 return __lookup_pg_pool(&map->pg_pools, id);
677}
678
Alex Elder72afc712012-10-30 19:40:33 -0500679const char *ceph_pg_pool_name_by_id(struct ceph_osdmap *map, u64 id)
680{
681 struct ceph_pg_pool_info *pi;
682
683 if (id == CEPH_NOPOOL)
684 return NULL;
685
686 if (WARN_ON_ONCE(id > (u64) INT_MAX))
687 return NULL;
688
689 pi = __lookup_pg_pool(&map->pg_pools, (int) id);
690
691 return pi ? pi->name : NULL;
692}
693EXPORT_SYMBOL(ceph_pg_pool_name_by_id);
694
Yehuda Sadeh7669a2c2010-05-17 12:31:35 -0700695int ceph_pg_poolid_by_name(struct ceph_osdmap *map, const char *name)
696{
697 struct rb_node *rbp;
698
699 for (rbp = rb_first(&map->pg_pools); rbp; rbp = rb_next(rbp)) {
700 struct ceph_pg_pool_info *pi =
701 rb_entry(rbp, struct ceph_pg_pool_info, node);
702 if (pi->name && strcmp(pi->name, name) == 0)
703 return pi->id;
704 }
705 return -ENOENT;
706}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700707EXPORT_SYMBOL(ceph_pg_poolid_by_name);
Yehuda Sadeh7669a2c2010-05-17 12:31:35 -0700708
Sage Weil2844a762010-04-09 15:46:42 -0700709static void __remove_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *pi)
710{
711 rb_erase(&pi->node, root);
712 kfree(pi->name);
713 kfree(pi);
714}
715
Ilya Dryomov0f70c7e2014-03-21 19:05:27 +0200716static int decode_pool(void **p, void *end, struct ceph_pg_pool_info *pi)
Sage Weilefd75762010-03-17 10:05:28 -0700717{
Sage Weil4f6a7e52013-02-23 10:41:09 -0800718 u8 ev, cv;
719 unsigned len, num;
720 void *pool_end;
Sage Weil73a7e692010-08-02 11:00:55 -0700721
Sage Weil4f6a7e52013-02-23 10:41:09 -0800722 ceph_decode_need(p, end, 2 + 4, bad);
723 ev = ceph_decode_8(p); /* encoding version */
724 cv = ceph_decode_8(p); /* compat version */
725 if (ev < 5) {
Joe Perchesb9a67892014-09-09 21:17:29 -0700726 pr_warn("got v %d < 5 cv %d of ceph_pg_pool\n", ev, cv);
Sage Weil4f6a7e52013-02-23 10:41:09 -0800727 return -EINVAL;
728 }
Ilya Dryomov17a13e402014-01-27 17:40:19 +0200729 if (cv > 9) {
Joe Perchesb9a67892014-09-09 21:17:29 -0700730 pr_warn("got v %d cv %d > 9 of ceph_pg_pool\n", ev, cv);
Sage Weil4f6a7e52013-02-23 10:41:09 -0800731 return -EINVAL;
732 }
733 len = ceph_decode_32(p);
734 ceph_decode_need(p, end, len, bad);
735 pool_end = *p + len;
Sage Weil73a7e692010-08-02 11:00:55 -0700736
Sage Weil4f6a7e52013-02-23 10:41:09 -0800737 pi->type = ceph_decode_8(p);
738 pi->size = ceph_decode_8(p);
739 pi->crush_ruleset = ceph_decode_8(p);
740 pi->object_hash = ceph_decode_8(p);
741
742 pi->pg_num = ceph_decode_32(p);
743 pi->pgp_num = ceph_decode_32(p);
744
745 *p += 4 + 4; /* skip lpg* */
746 *p += 4; /* skip last_change */
747 *p += 8 + 4; /* skip snap_seq, snap_epoch */
748
749 /* skip snaps */
750 num = ceph_decode_32(p);
751 while (num--) {
752 *p += 8; /* snapid key */
753 *p += 1 + 1; /* versions */
754 len = ceph_decode_32(p);
755 *p += len;
Sage Weil73a7e692010-08-02 11:00:55 -0700756 }
757
Ilya Dryomov17a13e402014-01-27 17:40:19 +0200758 /* skip removed_snaps */
Sage Weil4f6a7e52013-02-23 10:41:09 -0800759 num = ceph_decode_32(p);
760 *p += num * (8 + 8);
761
762 *p += 8; /* skip auid */
763 pi->flags = ceph_decode_64(p);
Ilya Dryomov17a13e402014-01-27 17:40:19 +0200764 *p += 4; /* skip crash_replay_interval */
765
766 if (ev >= 7)
Ilya Dryomov04812ac2016-04-28 16:07:23 +0200767 pi->min_size = ceph_decode_8(p);
768 else
769 pi->min_size = pi->size - pi->size / 2;
Ilya Dryomov17a13e402014-01-27 17:40:19 +0200770
771 if (ev >= 8)
772 *p += 8 + 8; /* skip quota_max_* */
773
774 if (ev >= 9) {
775 /* skip tiers */
776 num = ceph_decode_32(p);
777 *p += num * 8;
778
779 *p += 8; /* skip tier_of */
780 *p += 1; /* skip cache_mode */
781
782 pi->read_tier = ceph_decode_64(p);
783 pi->write_tier = ceph_decode_64(p);
784 } else {
785 pi->read_tier = -1;
786 pi->write_tier = -1;
787 }
Sage Weil4f6a7e52013-02-23 10:41:09 -0800788
Ilya Dryomov04812ac2016-04-28 16:07:23 +0200789 if (ev >= 10) {
790 /* skip properties */
791 num = ceph_decode_32(p);
792 while (num--) {
793 len = ceph_decode_32(p);
794 *p += len; /* key */
795 len = ceph_decode_32(p);
796 *p += len; /* val */
797 }
798 }
799
800 if (ev >= 11) {
801 /* skip hit_set_params */
802 *p += 1 + 1; /* versions */
803 len = ceph_decode_32(p);
804 *p += len;
805
806 *p += 4; /* skip hit_set_period */
807 *p += 4; /* skip hit_set_count */
808 }
809
810 if (ev >= 12)
811 *p += 4; /* skip stripe_width */
812
813 if (ev >= 13) {
814 *p += 8; /* skip target_max_bytes */
815 *p += 8; /* skip target_max_objects */
816 *p += 4; /* skip cache_target_dirty_ratio_micro */
817 *p += 4; /* skip cache_target_full_ratio_micro */
818 *p += 4; /* skip cache_min_flush_age */
819 *p += 4; /* skip cache_min_evict_age */
820 }
821
822 if (ev >= 14) {
823 /* skip erasure_code_profile */
824 len = ceph_decode_32(p);
825 *p += len;
826 }
827
Ilya Dryomov8e48cf02017-06-05 14:45:00 +0200828 /*
829 * last_force_op_resend_preluminous, will be overridden if the
830 * map was encoded with RESEND_ON_SPLIT
831 */
Ilya Dryomov04812ac2016-04-28 16:07:23 +0200832 if (ev >= 15)
833 pi->last_force_request_resend = ceph_decode_32(p);
834 else
835 pi->last_force_request_resend = 0;
836
Ilya Dryomov8e48cf02017-06-05 14:45:00 +0200837 if (ev >= 16)
838 *p += 4; /* skip min_read_recency_for_promote */
839
840 if (ev >= 17)
841 *p += 8; /* skip expected_num_objects */
842
843 if (ev >= 19)
844 *p += 4; /* skip cache_target_dirty_high_ratio_micro */
845
846 if (ev >= 20)
847 *p += 4; /* skip min_write_recency_for_promote */
848
849 if (ev >= 21)
850 *p += 1; /* skip use_gmt_hitset */
851
852 if (ev >= 22)
853 *p += 1; /* skip fast_read */
854
855 if (ev >= 23) {
856 *p += 4; /* skip hit_set_grade_decay_rate */
857 *p += 4; /* skip hit_set_search_last_n */
858 }
859
860 if (ev >= 24) {
861 /* skip opts */
862 *p += 1 + 1; /* versions */
863 len = ceph_decode_32(p);
864 *p += len;
865 }
866
867 if (ev >= 25)
868 pi->last_force_request_resend = ceph_decode_32(p);
869
Sage Weil4f6a7e52013-02-23 10:41:09 -0800870 /* ignore the rest */
871
872 *p = pool_end;
873 calc_pg_masks(pi);
Sage Weil73a7e692010-08-02 11:00:55 -0700874 return 0;
875
876bad:
877 return -EINVAL;
Sage Weilefd75762010-03-17 10:05:28 -0700878}
879
Ilya Dryomov0f70c7e2014-03-21 19:05:27 +0200880static int decode_pool_names(void **p, void *end, struct ceph_osdmap *map)
Sage Weil2844a762010-04-09 15:46:42 -0700881{
882 struct ceph_pg_pool_info *pi;
Sage Weil4f6a7e52013-02-23 10:41:09 -0800883 u32 num, len;
884 u64 pool;
Sage Weil2844a762010-04-09 15:46:42 -0700885
886 ceph_decode_32_safe(p, end, num, bad);
887 dout(" %d pool names\n", num);
888 while (num--) {
Sage Weil4f6a7e52013-02-23 10:41:09 -0800889 ceph_decode_64_safe(p, end, pool, bad);
Sage Weil2844a762010-04-09 15:46:42 -0700890 ceph_decode_32_safe(p, end, len, bad);
Sage Weil4f6a7e52013-02-23 10:41:09 -0800891 dout(" pool %llu len %d\n", pool, len);
Xi Wangad3b9042012-06-06 19:35:55 -0500892 ceph_decode_need(p, end, len, bad);
Sage Weil2844a762010-04-09 15:46:42 -0700893 pi = __lookup_pg_pool(&map->pg_pools, pool);
894 if (pi) {
Xi Wangad3b9042012-06-06 19:35:55 -0500895 char *name = kstrndup(*p, len, GFP_NOFS);
896
897 if (!name)
898 return -ENOMEM;
Sage Weil2844a762010-04-09 15:46:42 -0700899 kfree(pi->name);
Xi Wangad3b9042012-06-06 19:35:55 -0500900 pi->name = name;
901 dout(" name is %s\n", pi->name);
Sage Weil2844a762010-04-09 15:46:42 -0700902 }
903 *p += len;
904 }
905 return 0;
906
907bad:
908 return -EINVAL;
909}
910
911/*
912 * osd map
913 */
Ilya Dryomove5253a72016-04-28 16:07:25 +0200914struct ceph_osdmap *ceph_osdmap_alloc(void)
915{
916 struct ceph_osdmap *map;
917
918 map = kzalloc(sizeof(*map), GFP_NOIO);
919 if (!map)
920 return NULL;
921
922 map->pg_pools = RB_ROOT;
923 map->pool_max = -1;
924 map->pg_temp = RB_ROOT;
925 map->primary_temp = RB_ROOT;
Ilya Dryomov6f428df2017-06-21 17:27:18 +0200926 map->pg_upmap = RB_ROOT;
927 map->pg_upmap_items = RB_ROOT;
Ilya Dryomov743efcf2017-01-31 15:55:06 +0100928 mutex_init(&map->crush_workspace_mutex);
Ilya Dryomove5253a72016-04-28 16:07:25 +0200929
930 return map;
931}
932
Sage Weil2844a762010-04-09 15:46:42 -0700933void ceph_osdmap_destroy(struct ceph_osdmap *map)
934{
935 dout("osdmap_destroy %p\n", map);
936 if (map->crush)
937 crush_destroy(map->crush);
938 while (!RB_EMPTY_ROOT(&map->pg_temp)) {
939 struct ceph_pg_mapping *pg =
940 rb_entry(rb_first(&map->pg_temp),
941 struct ceph_pg_mapping, node);
Ilya Dryomovab751442017-06-21 17:27:17 +0200942 erase_pg_mapping(&map->pg_temp, pg);
943 free_pg_mapping(pg);
Sage Weil2844a762010-04-09 15:46:42 -0700944 }
Ilya Dryomov9686f942014-03-21 19:05:29 +0200945 while (!RB_EMPTY_ROOT(&map->primary_temp)) {
946 struct ceph_pg_mapping *pg =
947 rb_entry(rb_first(&map->primary_temp),
948 struct ceph_pg_mapping, node);
Ilya Dryomovab751442017-06-21 17:27:17 +0200949 erase_pg_mapping(&map->primary_temp, pg);
950 free_pg_mapping(pg);
Ilya Dryomov9686f942014-03-21 19:05:29 +0200951 }
Ilya Dryomov6f428df2017-06-21 17:27:18 +0200952 while (!RB_EMPTY_ROOT(&map->pg_upmap)) {
953 struct ceph_pg_mapping *pg =
954 rb_entry(rb_first(&map->pg_upmap),
955 struct ceph_pg_mapping, node);
956 rb_erase(&pg->node, &map->pg_upmap);
957 kfree(pg);
958 }
959 while (!RB_EMPTY_ROOT(&map->pg_upmap_items)) {
960 struct ceph_pg_mapping *pg =
961 rb_entry(rb_first(&map->pg_upmap_items),
962 struct ceph_pg_mapping, node);
963 rb_erase(&pg->node, &map->pg_upmap_items);
964 kfree(pg);
965 }
Sage Weil2844a762010-04-09 15:46:42 -0700966 while (!RB_EMPTY_ROOT(&map->pg_pools)) {
967 struct ceph_pg_pool_info *pi =
968 rb_entry(rb_first(&map->pg_pools),
969 struct ceph_pg_pool_info, node);
970 __remove_pg_pool(&map->pg_pools, pi);
971 }
972 kfree(map->osd_state);
973 kfree(map->osd_weight);
974 kfree(map->osd_addr);
Ilya Dryomov2cfa34f2014-03-21 19:05:30 +0200975 kfree(map->osd_primary_affinity);
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100976 kfree(map->crush_workspace);
Sage Weil2844a762010-04-09 15:46:42 -0700977 kfree(map);
978}
979
980/*
Ilya Dryomov4d603512014-03-21 19:05:28 +0200981 * Adjust max_osd value, (re)allocate arrays.
982 *
983 * The new elements are properly initialized.
Sage Weil2844a762010-04-09 15:46:42 -0700984 */
985static int osdmap_set_max_osd(struct ceph_osdmap *map, int max)
986{
987 u8 *state;
Sage Weil2844a762010-04-09 15:46:42 -0700988 u32 *weight;
Ilya Dryomov4d603512014-03-21 19:05:28 +0200989 struct ceph_entity_addr *addr;
990 int i;
Sage Weil2844a762010-04-09 15:46:42 -0700991
Ilya Dryomov4d603512014-03-21 19:05:28 +0200992 state = krealloc(map->osd_state, max*sizeof(*state), GFP_NOFS);
Li RongQing589506f2014-09-07 18:10:51 +0800993 if (!state)
Sage Weil2844a762010-04-09 15:46:42 -0700994 return -ENOMEM;
Li RongQing589506f2014-09-07 18:10:51 +0800995 map->osd_state = state;
996
997 weight = krealloc(map->osd_weight, max*sizeof(*weight), GFP_NOFS);
998 if (!weight)
999 return -ENOMEM;
1000 map->osd_weight = weight;
1001
1002 addr = krealloc(map->osd_addr, max*sizeof(*addr), GFP_NOFS);
1003 if (!addr)
1004 return -ENOMEM;
1005 map->osd_addr = addr;
Sage Weil2844a762010-04-09 15:46:42 -07001006
Ilya Dryomov4d603512014-03-21 19:05:28 +02001007 for (i = map->max_osd; i < max; i++) {
Li RongQing589506f2014-09-07 18:10:51 +08001008 map->osd_state[i] = 0;
1009 map->osd_weight[i] = CEPH_OSD_OUT;
1010 memset(map->osd_addr + i, 0, sizeof(*map->osd_addr));
Sage Weil2844a762010-04-09 15:46:42 -07001011 }
1012
Ilya Dryomov2cfa34f2014-03-21 19:05:30 +02001013 if (map->osd_primary_affinity) {
1014 u32 *affinity;
1015
1016 affinity = krealloc(map->osd_primary_affinity,
1017 max*sizeof(*affinity), GFP_NOFS);
1018 if (!affinity)
1019 return -ENOMEM;
Li RongQing589506f2014-09-07 18:10:51 +08001020 map->osd_primary_affinity = affinity;
Ilya Dryomov2cfa34f2014-03-21 19:05:30 +02001021
1022 for (i = map->max_osd; i < max; i++)
Li RongQing589506f2014-09-07 18:10:51 +08001023 map->osd_primary_affinity[i] =
1024 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
Ilya Dryomov2cfa34f2014-03-21 19:05:30 +02001025 }
1026
Sage Weil2844a762010-04-09 15:46:42 -07001027 map->max_osd = max;
Ilya Dryomov4d603512014-03-21 19:05:28 +02001028
Sage Weil2844a762010-04-09 15:46:42 -07001029 return 0;
1030}
1031
Ilya Dryomov1b6a78b2017-01-31 15:55:06 +01001032static int osdmap_set_crush(struct ceph_osdmap *map, struct crush_map *crush)
1033{
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +01001034 void *workspace;
Ilya Dryomov743efcf2017-01-31 15:55:06 +01001035 size_t work_size;
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +01001036
Ilya Dryomov1b6a78b2017-01-31 15:55:06 +01001037 if (IS_ERR(crush))
1038 return PTR_ERR(crush);
1039
Ilya Dryomov743efcf2017-01-31 15:55:06 +01001040 work_size = crush_work_size(crush, CEPH_PG_MAX_SIZE);
1041 dout("%s work_size %zu bytes\n", __func__, work_size);
1042 workspace = kmalloc(work_size, GFP_NOIO);
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +01001043 if (!workspace) {
1044 crush_destroy(crush);
1045 return -ENOMEM;
1046 }
1047 crush_init_workspace(crush, workspace);
1048
Ilya Dryomov1b6a78b2017-01-31 15:55:06 +01001049 if (map->crush)
1050 crush_destroy(map->crush);
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +01001051 kfree(map->crush_workspace);
Ilya Dryomov1b6a78b2017-01-31 15:55:06 +01001052 map->crush = crush;
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +01001053 map->crush_workspace = workspace;
Ilya Dryomov1b6a78b2017-01-31 15:55:06 +01001054 return 0;
1055}
1056
Ilya Dryomovec7af972014-03-21 19:05:29 +02001057#define OSDMAP_WRAPPER_COMPAT_VER 7
1058#define OSDMAP_CLIENT_DATA_COMPAT_VER 1
1059
1060/*
1061 * Return 0 or error. On success, *v is set to 0 for old (v6) osdmaps,
1062 * to struct_v of the client_data section for new (v7 and above)
1063 * osdmaps.
1064 */
1065static int get_osdmap_client_data_v(void **p, void *end,
1066 const char *prefix, u8 *v)
1067{
1068 u8 struct_v;
1069
1070 ceph_decode_8_safe(p, end, struct_v, e_inval);
1071 if (struct_v >= 7) {
1072 u8 struct_compat;
1073
1074 ceph_decode_8_safe(p, end, struct_compat, e_inval);
1075 if (struct_compat > OSDMAP_WRAPPER_COMPAT_VER) {
Joe Perchesb9a67892014-09-09 21:17:29 -07001076 pr_warn("got v %d cv %d > %d of %s ceph_osdmap\n",
1077 struct_v, struct_compat,
1078 OSDMAP_WRAPPER_COMPAT_VER, prefix);
Ilya Dryomovec7af972014-03-21 19:05:29 +02001079 return -EINVAL;
1080 }
1081 *p += 4; /* ignore wrapper struct_len */
1082
1083 ceph_decode_8_safe(p, end, struct_v, e_inval);
1084 ceph_decode_8_safe(p, end, struct_compat, e_inval);
1085 if (struct_compat > OSDMAP_CLIENT_DATA_COMPAT_VER) {
Joe Perchesb9a67892014-09-09 21:17:29 -07001086 pr_warn("got v %d cv %d > %d of %s ceph_osdmap client data\n",
1087 struct_v, struct_compat,
1088 OSDMAP_CLIENT_DATA_COMPAT_VER, prefix);
Ilya Dryomovec7af972014-03-21 19:05:29 +02001089 return -EINVAL;
1090 }
1091 *p += 4; /* ignore client data struct_len */
1092 } else {
1093 u16 version;
1094
1095 *p -= 1;
1096 ceph_decode_16_safe(p, end, version, e_inval);
1097 if (version < 6) {
Joe Perchesb9a67892014-09-09 21:17:29 -07001098 pr_warn("got v %d < 6 of %s ceph_osdmap\n",
1099 version, prefix);
Ilya Dryomovec7af972014-03-21 19:05:29 +02001100 return -EINVAL;
1101 }
1102
1103 /* old osdmap enconding */
1104 struct_v = 0;
1105 }
1106
1107 *v = struct_v;
1108 return 0;
1109
1110e_inval:
1111 return -EINVAL;
1112}
1113
Ilya Dryomov433fbdd2014-03-21 19:05:27 +02001114static int __decode_pools(void **p, void *end, struct ceph_osdmap *map,
1115 bool incremental)
1116{
1117 u32 n;
1118
1119 ceph_decode_32_safe(p, end, n, e_inval);
1120 while (n--) {
1121 struct ceph_pg_pool_info *pi;
1122 u64 pool;
1123 int ret;
1124
1125 ceph_decode_64_safe(p, end, pool, e_inval);
1126
1127 pi = __lookup_pg_pool(&map->pg_pools, pool);
1128 if (!incremental || !pi) {
1129 pi = kzalloc(sizeof(*pi), GFP_NOFS);
1130 if (!pi)
1131 return -ENOMEM;
1132
1133 pi->id = pool;
1134
1135 ret = __insert_pg_pool(&map->pg_pools, pi);
1136 if (ret) {
1137 kfree(pi);
1138 return ret;
1139 }
1140 }
1141
1142 ret = decode_pool(p, end, pi);
1143 if (ret)
1144 return ret;
1145 }
1146
1147 return 0;
1148
1149e_inval:
1150 return -EINVAL;
1151}
1152
1153static int decode_pools(void **p, void *end, struct ceph_osdmap *map)
1154{
1155 return __decode_pools(p, end, map, false);
1156}
1157
1158static int decode_new_pools(void **p, void *end, struct ceph_osdmap *map)
1159{
1160 return __decode_pools(p, end, map, true);
1161}
1162
Ilya Dryomova303bb02017-06-21 17:27:17 +02001163typedef struct ceph_pg_mapping *(*decode_mapping_fn_t)(void **, void *, bool);
1164
1165static int decode_pg_mapping(void **p, void *end, struct rb_root *mapping_root,
1166 decode_mapping_fn_t fn, bool incremental)
Ilya Dryomov10db6342014-03-21 19:05:28 +02001167{
1168 u32 n;
1169
Ilya Dryomova303bb02017-06-21 17:27:17 +02001170 WARN_ON(!incremental && !fn);
1171
Ilya Dryomov10db6342014-03-21 19:05:28 +02001172 ceph_decode_32_safe(p, end, n, e_inval);
1173 while (n--) {
Ilya Dryomova303bb02017-06-21 17:27:17 +02001174 struct ceph_pg_mapping *pg;
Ilya Dryomov10db6342014-03-21 19:05:28 +02001175 struct ceph_pg pgid;
Ilya Dryomov10db6342014-03-21 19:05:28 +02001176 int ret;
1177
1178 ret = ceph_decode_pgid(p, end, &pgid);
1179 if (ret)
1180 return ret;
1181
Ilya Dryomovab751442017-06-21 17:27:17 +02001182 pg = lookup_pg_mapping(mapping_root, &pgid);
1183 if (pg) {
1184 WARN_ON(!incremental);
1185 erase_pg_mapping(mapping_root, pg);
1186 free_pg_mapping(pg);
1187 }
Ilya Dryomov10db6342014-03-21 19:05:28 +02001188
Ilya Dryomova303bb02017-06-21 17:27:17 +02001189 if (fn) {
1190 pg = fn(p, end, incremental);
1191 if (IS_ERR(pg))
1192 return PTR_ERR(pg);
Ilya Dryomov10db6342014-03-21 19:05:28 +02001193
Ilya Dryomova303bb02017-06-21 17:27:17 +02001194 if (pg) {
1195 pg->pgid = pgid; /* struct */
Ilya Dryomovab751442017-06-21 17:27:17 +02001196 insert_pg_mapping(mapping_root, pg);
Ilya Dryomov10db6342014-03-21 19:05:28 +02001197 }
1198 }
1199 }
1200
1201 return 0;
1202
1203e_inval:
1204 return -EINVAL;
1205}
1206
Ilya Dryomova303bb02017-06-21 17:27:17 +02001207static struct ceph_pg_mapping *__decode_pg_temp(void **p, void *end,
1208 bool incremental)
1209{
1210 struct ceph_pg_mapping *pg;
1211 u32 len, i;
1212
1213 ceph_decode_32_safe(p, end, len, e_inval);
1214 if (len == 0 && incremental)
1215 return NULL; /* new_pg_temp: [] to remove */
1216 if (len > (SIZE_MAX - sizeof(*pg)) / sizeof(u32))
1217 return ERR_PTR(-EINVAL);
1218
1219 ceph_decode_need(p, end, len * sizeof(u32), e_inval);
1220 pg = alloc_pg_mapping(len * sizeof(u32));
1221 if (!pg)
1222 return ERR_PTR(-ENOMEM);
1223
1224 pg->pg_temp.len = len;
1225 for (i = 0; i < len; i++)
1226 pg->pg_temp.osds[i] = ceph_decode_32(p);
1227
1228 return pg;
1229
1230e_inval:
1231 return ERR_PTR(-EINVAL);
1232}
1233
Ilya Dryomov10db6342014-03-21 19:05:28 +02001234static int decode_pg_temp(void **p, void *end, struct ceph_osdmap *map)
1235{
Ilya Dryomova303bb02017-06-21 17:27:17 +02001236 return decode_pg_mapping(p, end, &map->pg_temp, __decode_pg_temp,
1237 false);
Ilya Dryomov10db6342014-03-21 19:05:28 +02001238}
1239
1240static int decode_new_pg_temp(void **p, void *end, struct ceph_osdmap *map)
1241{
Ilya Dryomova303bb02017-06-21 17:27:17 +02001242 return decode_pg_mapping(p, end, &map->pg_temp, __decode_pg_temp,
1243 true);
Ilya Dryomov10db6342014-03-21 19:05:28 +02001244}
1245
Ilya Dryomova303bb02017-06-21 17:27:17 +02001246static struct ceph_pg_mapping *__decode_primary_temp(void **p, void *end,
1247 bool incremental)
Ilya Dryomovd286de72014-03-21 19:05:30 +02001248{
Ilya Dryomova303bb02017-06-21 17:27:17 +02001249 struct ceph_pg_mapping *pg;
1250 u32 osd;
Ilya Dryomovd286de72014-03-21 19:05:30 +02001251
Ilya Dryomova303bb02017-06-21 17:27:17 +02001252 ceph_decode_32_safe(p, end, osd, e_inval);
1253 if (osd == (u32)-1 && incremental)
1254 return NULL; /* new_primary_temp: -1 to remove */
Ilya Dryomovd286de72014-03-21 19:05:30 +02001255
Ilya Dryomova303bb02017-06-21 17:27:17 +02001256 pg = alloc_pg_mapping(0);
1257 if (!pg)
1258 return ERR_PTR(-ENOMEM);
Ilya Dryomovd286de72014-03-21 19:05:30 +02001259
Ilya Dryomova303bb02017-06-21 17:27:17 +02001260 pg->primary_temp.osd = osd;
1261 return pg;
Ilya Dryomovd286de72014-03-21 19:05:30 +02001262
1263e_inval:
Ilya Dryomova303bb02017-06-21 17:27:17 +02001264 return ERR_PTR(-EINVAL);
Ilya Dryomovd286de72014-03-21 19:05:30 +02001265}
1266
1267static int decode_primary_temp(void **p, void *end, struct ceph_osdmap *map)
1268{
Ilya Dryomova303bb02017-06-21 17:27:17 +02001269 return decode_pg_mapping(p, end, &map->primary_temp,
1270 __decode_primary_temp, false);
Ilya Dryomovd286de72014-03-21 19:05:30 +02001271}
1272
1273static int decode_new_primary_temp(void **p, void *end,
1274 struct ceph_osdmap *map)
1275{
Ilya Dryomova303bb02017-06-21 17:27:17 +02001276 return decode_pg_mapping(p, end, &map->primary_temp,
1277 __decode_primary_temp, true);
Ilya Dryomovd286de72014-03-21 19:05:30 +02001278}
1279
Ilya Dryomov2cfa34f2014-03-21 19:05:30 +02001280u32 ceph_get_primary_affinity(struct ceph_osdmap *map, int osd)
1281{
1282 BUG_ON(osd >= map->max_osd);
1283
1284 if (!map->osd_primary_affinity)
1285 return CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
1286
1287 return map->osd_primary_affinity[osd];
1288}
1289
1290static int set_primary_affinity(struct ceph_osdmap *map, int osd, u32 aff)
1291{
1292 BUG_ON(osd >= map->max_osd);
1293
1294 if (!map->osd_primary_affinity) {
1295 int i;
1296
1297 map->osd_primary_affinity = kmalloc(map->max_osd*sizeof(u32),
1298 GFP_NOFS);
1299 if (!map->osd_primary_affinity)
1300 return -ENOMEM;
1301
1302 for (i = 0; i < map->max_osd; i++)
1303 map->osd_primary_affinity[i] =
1304 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
1305 }
1306
1307 map->osd_primary_affinity[osd] = aff;
1308
1309 return 0;
1310}
1311
Ilya Dryomov63a69932014-03-21 19:05:30 +02001312static int decode_primary_affinity(void **p, void *end,
1313 struct ceph_osdmap *map)
1314{
1315 u32 len, i;
1316
1317 ceph_decode_32_safe(p, end, len, e_inval);
1318 if (len == 0) {
1319 kfree(map->osd_primary_affinity);
1320 map->osd_primary_affinity = NULL;
1321 return 0;
1322 }
1323 if (len != map->max_osd)
1324 goto e_inval;
1325
1326 ceph_decode_need(p, end, map->max_osd*sizeof(u32), e_inval);
1327
1328 for (i = 0; i < map->max_osd; i++) {
1329 int ret;
1330
1331 ret = set_primary_affinity(map, i, ceph_decode_32(p));
1332 if (ret)
1333 return ret;
1334 }
1335
1336 return 0;
1337
1338e_inval:
1339 return -EINVAL;
1340}
1341
1342static int decode_new_primary_affinity(void **p, void *end,
1343 struct ceph_osdmap *map)
1344{
1345 u32 n;
1346
1347 ceph_decode_32_safe(p, end, n, e_inval);
1348 while (n--) {
1349 u32 osd, aff;
1350 int ret;
1351
1352 ceph_decode_32_safe(p, end, osd, e_inval);
1353 ceph_decode_32_safe(p, end, aff, e_inval);
1354
1355 ret = set_primary_affinity(map, osd, aff);
1356 if (ret)
1357 return ret;
Ilya Dryomovf31da0f2014-04-02 20:34:04 +04001358
1359 pr_info("osd%d primary-affinity 0x%x\n", osd, aff);
Ilya Dryomov63a69932014-03-21 19:05:30 +02001360 }
1361
1362 return 0;
1363
1364e_inval:
1365 return -EINVAL;
1366}
1367
Ilya Dryomov6f428df2017-06-21 17:27:18 +02001368static struct ceph_pg_mapping *__decode_pg_upmap(void **p, void *end,
1369 bool __unused)
1370{
1371 return __decode_pg_temp(p, end, false);
1372}
1373
1374static int decode_pg_upmap(void **p, void *end, struct ceph_osdmap *map)
1375{
1376 return decode_pg_mapping(p, end, &map->pg_upmap, __decode_pg_upmap,
1377 false);
1378}
1379
1380static int decode_new_pg_upmap(void **p, void *end, struct ceph_osdmap *map)
1381{
1382 return decode_pg_mapping(p, end, &map->pg_upmap, __decode_pg_upmap,
1383 true);
1384}
1385
1386static int decode_old_pg_upmap(void **p, void *end, struct ceph_osdmap *map)
1387{
1388 return decode_pg_mapping(p, end, &map->pg_upmap, NULL, true);
1389}
1390
1391static struct ceph_pg_mapping *__decode_pg_upmap_items(void **p, void *end,
1392 bool __unused)
1393{
1394 struct ceph_pg_mapping *pg;
1395 u32 len, i;
1396
1397 ceph_decode_32_safe(p, end, len, e_inval);
1398 if (len > (SIZE_MAX - sizeof(*pg)) / (2 * sizeof(u32)))
1399 return ERR_PTR(-EINVAL);
1400
1401 ceph_decode_need(p, end, 2 * len * sizeof(u32), e_inval);
1402 pg = kzalloc(sizeof(*pg) + 2 * len * sizeof(u32), GFP_NOIO);
1403 if (!pg)
1404 return ERR_PTR(-ENOMEM);
1405
1406 pg->pg_upmap_items.len = len;
1407 for (i = 0; i < len; i++) {
1408 pg->pg_upmap_items.from_to[i][0] = ceph_decode_32(p);
1409 pg->pg_upmap_items.from_to[i][1] = ceph_decode_32(p);
1410 }
1411
1412 return pg;
1413
1414e_inval:
1415 return ERR_PTR(-EINVAL);
1416}
1417
1418static int decode_pg_upmap_items(void **p, void *end, struct ceph_osdmap *map)
1419{
1420 return decode_pg_mapping(p, end, &map->pg_upmap_items,
1421 __decode_pg_upmap_items, false);
1422}
1423
1424static int decode_new_pg_upmap_items(void **p, void *end,
1425 struct ceph_osdmap *map)
1426{
1427 return decode_pg_mapping(p, end, &map->pg_upmap_items,
1428 __decode_pg_upmap_items, true);
1429}
1430
1431static int decode_old_pg_upmap_items(void **p, void *end,
1432 struct ceph_osdmap *map)
1433{
1434 return decode_pg_mapping(p, end, &map->pg_upmap_items, NULL, true);
1435}
1436
Sage Weil4fc51be2010-02-16 15:55:03 -08001437/*
Sage Weilf24e9982009-10-06 11:31:10 -07001438 * decode a full map.
1439 */
Ilya Dryomova2505d62014-03-13 16:36:13 +02001440static int osdmap_decode(void **p, void *end, struct ceph_osdmap *map)
Sage Weilf24e9982009-10-06 11:31:10 -07001441{
Ilya Dryomovec7af972014-03-21 19:05:29 +02001442 u8 struct_v;
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001443 u32 epoch = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001444 void *start = *p;
Ilya Dryomov39770582014-03-13 16:36:14 +02001445 u32 max;
1446 u32 len, i;
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001447 int err;
Sage Weilf24e9982009-10-06 11:31:10 -07001448
Ilya Dryomova2505d62014-03-13 16:36:13 +02001449 dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p));
Sage Weilf24e9982009-10-06 11:31:10 -07001450
Ilya Dryomovec7af972014-03-21 19:05:29 +02001451 err = get_osdmap_client_data_v(p, end, "full", &struct_v);
1452 if (err)
1453 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001454
Ilya Dryomov53bbaba2014-03-13 16:36:16 +02001455 /* fsid, epoch, created, modified */
1456 ceph_decode_need(p, end, sizeof(map->fsid) + sizeof(u32) +
1457 sizeof(map->created) + sizeof(map->modified), e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001458 ceph_decode_copy(p, &map->fsid, sizeof(map->fsid));
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001459 epoch = map->epoch = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -07001460 ceph_decode_copy(p, &map->created, sizeof(map->created));
1461 ceph_decode_copy(p, &map->modified, sizeof(map->modified));
1462
Ilya Dryomov433fbdd2014-03-21 19:05:27 +02001463 /* pools */
1464 err = decode_pools(p, end, map);
1465 if (err)
1466 goto bad;
Sage Weil2844a762010-04-09 15:46:42 -07001467
Ilya Dryomov0f70c7e2014-03-21 19:05:27 +02001468 /* pool_name */
1469 err = decode_pool_names(p, end, map);
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001470 if (err)
Sage Weil4f6a7e52013-02-23 10:41:09 -08001471 goto bad;
Sage Weil2844a762010-04-09 15:46:42 -07001472
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001473 ceph_decode_32_safe(p, end, map->pool_max, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001474
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001475 ceph_decode_32_safe(p, end, map->flags, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001476
Ilya Dryomov39770582014-03-13 16:36:14 +02001477 /* max_osd */
1478 ceph_decode_32_safe(p, end, max, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001479
1480 /* (re)alloc osd arrays */
1481 err = osdmap_set_max_osd(map, max);
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001482 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001483 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001484
Ilya Dryomov2d88b2e2014-03-13 16:36:14 +02001485 /* osd_state, osd_weight, osd_addrs->client_addr */
Sage Weilf24e9982009-10-06 11:31:10 -07001486 ceph_decode_need(p, end, 3*sizeof(u32) +
1487 map->max_osd*(1 + sizeof(*map->osd_weight) +
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001488 sizeof(*map->osd_addr)), e_inval);
1489
Ilya Dryomov2d88b2e2014-03-13 16:36:14 +02001490 if (ceph_decode_32(p) != map->max_osd)
1491 goto e_inval;
1492
Sage Weilf24e9982009-10-06 11:31:10 -07001493 ceph_decode_copy(p, map->osd_state, map->max_osd);
1494
Ilya Dryomov2d88b2e2014-03-13 16:36:14 +02001495 if (ceph_decode_32(p) != map->max_osd)
1496 goto e_inval;
1497
Sage Weilf24e9982009-10-06 11:31:10 -07001498 for (i = 0; i < map->max_osd; i++)
Sage Weilc89136e2009-10-14 09:59:09 -07001499 map->osd_weight[i] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -07001500
Ilya Dryomov2d88b2e2014-03-13 16:36:14 +02001501 if (ceph_decode_32(p) != map->max_osd)
1502 goto e_inval;
1503
Sage Weilf24e9982009-10-06 11:31:10 -07001504 ceph_decode_copy(p, map->osd_addr, map->max_osd*sizeof(*map->osd_addr));
Sage Weil63f2d212009-11-03 15:17:56 -08001505 for (i = 0; i < map->max_osd; i++)
1506 ceph_decode_addr(&map->osd_addr[i]);
Sage Weilf24e9982009-10-06 11:31:10 -07001507
1508 /* pg_temp */
Ilya Dryomov10db6342014-03-21 19:05:28 +02001509 err = decode_pg_temp(p, end, map);
1510 if (err)
1511 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001512
Ilya Dryomovd286de72014-03-21 19:05:30 +02001513 /* primary_temp */
1514 if (struct_v >= 1) {
1515 err = decode_primary_temp(p, end, map);
1516 if (err)
1517 goto bad;
1518 }
1519
Ilya Dryomov63a69932014-03-21 19:05:30 +02001520 /* primary_affinity */
1521 if (struct_v >= 2) {
1522 err = decode_primary_affinity(p, end, map);
1523 if (err)
1524 goto bad;
1525 } else {
Ilya Dryomov6f428df2017-06-21 17:27:18 +02001526 WARN_ON(map->osd_primary_affinity);
Ilya Dryomov63a69932014-03-21 19:05:30 +02001527 }
1528
Sage Weilf24e9982009-10-06 11:31:10 -07001529 /* crush */
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001530 ceph_decode_32_safe(p, end, len, e_inval);
Ilya Dryomov1b6a78b2017-01-31 15:55:06 +01001531 err = osdmap_set_crush(map, crush_decode(*p, min(*p + len, end)));
1532 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001533 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001534
Ilya Dryomov6f428df2017-06-21 17:27:18 +02001535 *p += len;
1536 if (struct_v >= 3) {
1537 /* erasure_code_profiles */
1538 ceph_decode_skip_map_of_map(p, end, string, string, string,
1539 bad);
1540 }
1541
1542 if (struct_v >= 4) {
1543 err = decode_pg_upmap(p, end, map);
1544 if (err)
1545 goto bad;
1546
1547 err = decode_pg_upmap_items(p, end, map);
1548 if (err)
1549 goto bad;
1550 } else {
1551 WARN_ON(!RB_EMPTY_ROOT(&map->pg_upmap));
1552 WARN_ON(!RB_EMPTY_ROOT(&map->pg_upmap_items));
1553 }
1554
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001555 /* ignore the rest */
Sage Weilf24e9982009-10-06 11:31:10 -07001556 *p = end;
1557
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001558 dout("full osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd);
Ilya Dryomova2505d62014-03-13 16:36:13 +02001559 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001560
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001561e_inval:
1562 err = -EINVAL;
Sage Weilf24e9982009-10-06 11:31:10 -07001563bad:
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001564 pr_err("corrupt full osdmap (%d) epoch %d off %d (%p of %p-%p)\n",
1565 err, epoch, (int)(*p - start), *p, start, end);
1566 print_hex_dump(KERN_DEBUG, "osdmap: ",
1567 DUMP_PREFIX_OFFSET, 16, 1,
1568 start, end - start, true);
Ilya Dryomova2505d62014-03-13 16:36:13 +02001569 return err;
1570}
1571
1572/*
1573 * Allocate and decode a full map.
1574 */
1575struct ceph_osdmap *ceph_osdmap_decode(void **p, void *end)
1576{
1577 struct ceph_osdmap *map;
1578 int ret;
1579
Ilya Dryomove5253a72016-04-28 16:07:25 +02001580 map = ceph_osdmap_alloc();
Ilya Dryomova2505d62014-03-13 16:36:13 +02001581 if (!map)
1582 return ERR_PTR(-ENOMEM);
1583
Ilya Dryomova2505d62014-03-13 16:36:13 +02001584 ret = osdmap_decode(p, end, map);
1585 if (ret) {
1586 ceph_osdmap_destroy(map);
1587 return ERR_PTR(ret);
1588 }
1589
1590 return map;
Sage Weilf24e9982009-10-06 11:31:10 -07001591}
1592
1593/*
Ilya Dryomov930c5322016-07-19 03:50:28 +02001594 * Encoding order is (new_up_client, new_state, new_weight). Need to
1595 * apply in the (new_weight, new_state, new_up_client) order, because
1596 * an incremental map may look like e.g.
1597 *
1598 * new_up_client: { osd=6, addr=... } # set osd_state and addr
1599 * new_state: { osd=6, xorstate=EXISTS } # clear osd_state
1600 */
1601static int decode_new_up_state_weight(void **p, void *end,
1602 struct ceph_osdmap *map)
1603{
1604 void *new_up_client;
1605 void *new_state;
1606 void *new_weight_end;
1607 u32 len;
1608
1609 new_up_client = *p;
1610 ceph_decode_32_safe(p, end, len, e_inval);
1611 len *= sizeof(u32) + sizeof(struct ceph_entity_addr);
1612 ceph_decode_need(p, end, len, e_inval);
1613 *p += len;
1614
1615 new_state = *p;
1616 ceph_decode_32_safe(p, end, len, e_inval);
1617 len *= sizeof(u32) + sizeof(u8);
1618 ceph_decode_need(p, end, len, e_inval);
1619 *p += len;
1620
1621 /* new_weight */
1622 ceph_decode_32_safe(p, end, len, e_inval);
1623 while (len--) {
1624 s32 osd;
1625 u32 w;
1626
1627 ceph_decode_need(p, end, 2*sizeof(u32), e_inval);
1628 osd = ceph_decode_32(p);
1629 w = ceph_decode_32(p);
1630 BUG_ON(osd >= map->max_osd);
1631 pr_info("osd%d weight 0x%x %s\n", osd, w,
1632 w == CEPH_OSD_IN ? "(in)" :
1633 (w == CEPH_OSD_OUT ? "(out)" : ""));
1634 map->osd_weight[osd] = w;
1635
1636 /*
1637 * If we are marking in, set the EXISTS, and clear the
1638 * AUTOOUT and NEW bits.
1639 */
1640 if (w) {
1641 map->osd_state[osd] |= CEPH_OSD_EXISTS;
1642 map->osd_state[osd] &= ~(CEPH_OSD_AUTOOUT |
1643 CEPH_OSD_NEW);
1644 }
1645 }
1646 new_weight_end = *p;
1647
1648 /* new_state (up/down) */
1649 *p = new_state;
1650 len = ceph_decode_32(p);
1651 while (len--) {
1652 s32 osd;
1653 u8 xorstate;
1654 int ret;
1655
1656 osd = ceph_decode_32(p);
1657 xorstate = ceph_decode_8(p);
1658 if (xorstate == 0)
1659 xorstate = CEPH_OSD_UP;
1660 BUG_ON(osd >= map->max_osd);
1661 if ((map->osd_state[osd] & CEPH_OSD_UP) &&
1662 (xorstate & CEPH_OSD_UP))
1663 pr_info("osd%d down\n", osd);
1664 if ((map->osd_state[osd] & CEPH_OSD_EXISTS) &&
1665 (xorstate & CEPH_OSD_EXISTS)) {
1666 pr_info("osd%d does not exist\n", osd);
Ilya Dryomov930c5322016-07-19 03:50:28 +02001667 ret = set_primary_affinity(map, osd,
1668 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY);
1669 if (ret)
1670 return ret;
1671 memset(map->osd_addr + osd, 0, sizeof(*map->osd_addr));
1672 map->osd_state[osd] = 0;
1673 } else {
1674 map->osd_state[osd] ^= xorstate;
1675 }
1676 }
1677
1678 /* new_up_client */
1679 *p = new_up_client;
1680 len = ceph_decode_32(p);
1681 while (len--) {
1682 s32 osd;
1683 struct ceph_entity_addr addr;
1684
1685 osd = ceph_decode_32(p);
1686 ceph_decode_copy(p, &addr, sizeof(addr));
1687 ceph_decode_addr(&addr);
1688 BUG_ON(osd >= map->max_osd);
1689 pr_info("osd%d up\n", osd);
1690 map->osd_state[osd] |= CEPH_OSD_EXISTS | CEPH_OSD_UP;
1691 map->osd_addr[osd] = addr;
1692 }
1693
1694 *p = new_weight_end;
1695 return 0;
1696
1697e_inval:
1698 return -EINVAL;
1699}
1700
1701/*
Sage Weilf24e9982009-10-06 11:31:10 -07001702 * decode and apply an incremental map update.
1703 */
1704struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end,
Ilya Dryomov0c0a8de2016-04-28 16:07:21 +02001705 struct ceph_osdmap *map)
Sage Weilf24e9982009-10-06 11:31:10 -07001706{
Sage Weilf24e9982009-10-06 11:31:10 -07001707 struct ceph_fsid fsid;
1708 u32 epoch = 0;
1709 struct ceph_timespec modified;
Sage Weil4f6a7e52013-02-23 10:41:09 -08001710 s32 len;
1711 u64 pool;
1712 __s64 new_pool_max;
1713 __s32 new_flags, max;
Sage Weilf24e9982009-10-06 11:31:10 -07001714 void *start = *p;
Ilya Dryomov86f17422014-03-13 16:36:15 +02001715 int err;
Ilya Dryomovec7af972014-03-21 19:05:29 +02001716 u8 struct_v;
Sage Weilf24e9982009-10-06 11:31:10 -07001717
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001718 dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p));
1719
Ilya Dryomovec7af972014-03-21 19:05:29 +02001720 err = get_osdmap_client_data_v(p, end, "inc", &struct_v);
1721 if (err)
1722 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001723
Ilya Dryomov53bbaba2014-03-13 16:36:16 +02001724 /* fsid, epoch, modified, new_pool_max, new_flags */
1725 ceph_decode_need(p, end, sizeof(fsid) + sizeof(u32) + sizeof(modified) +
1726 sizeof(u64) + sizeof(u32), e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001727 ceph_decode_copy(p, &fsid, sizeof(fsid));
Sage Weilc89136e2009-10-14 09:59:09 -07001728 epoch = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -07001729 BUG_ON(epoch != map->epoch+1);
1730 ceph_decode_copy(p, &modified, sizeof(modified));
Sage Weil4f6a7e52013-02-23 10:41:09 -08001731 new_pool_max = ceph_decode_64(p);
Sage Weilc89136e2009-10-14 09:59:09 -07001732 new_flags = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -07001733
1734 /* full map? */
Ilya Dryomov86f17422014-03-13 16:36:15 +02001735 ceph_decode_32_safe(p, end, len, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001736 if (len > 0) {
1737 dout("apply_incremental full map len %d, %p to %p\n",
1738 len, *p, end);
Ilya Dryomova2505d62014-03-13 16:36:13 +02001739 return ceph_osdmap_decode(p, min(*p+len, end));
Sage Weilf24e9982009-10-06 11:31:10 -07001740 }
1741
1742 /* new crush? */
Ilya Dryomov86f17422014-03-13 16:36:15 +02001743 ceph_decode_32_safe(p, end, len, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001744 if (len > 0) {
Ilya Dryomov1b6a78b2017-01-31 15:55:06 +01001745 err = osdmap_set_crush(map,
1746 crush_decode(*p, min(*p + len, end)));
1747 if (err)
Ilya Dryomov86f17422014-03-13 16:36:15 +02001748 goto bad;
Sage Weilcebc5be2010-06-17 10:22:48 -07001749 *p += len;
Sage Weilf24e9982009-10-06 11:31:10 -07001750 }
1751
1752 /* new flags? */
1753 if (new_flags >= 0)
1754 map->flags = new_flags;
Sage Weil4fc51be2010-02-16 15:55:03 -08001755 if (new_pool_max >= 0)
1756 map->pool_max = new_pool_max;
Sage Weilf24e9982009-10-06 11:31:10 -07001757
Sage Weilf24e9982009-10-06 11:31:10 -07001758 /* new max? */
Ilya Dryomov53bbaba2014-03-13 16:36:16 +02001759 ceph_decode_32_safe(p, end, max, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001760 if (max >= 0) {
1761 err = osdmap_set_max_osd(map, max);
Ilya Dryomov86f17422014-03-13 16:36:15 +02001762 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001763 goto bad;
1764 }
1765
1766 map->epoch++;
Sage Weil31456662011-05-12 15:18:43 -07001767 map->modified = modified;
Sage Weilf24e9982009-10-06 11:31:10 -07001768
Ilya Dryomov433fbdd2014-03-21 19:05:27 +02001769 /* new_pools */
1770 err = decode_new_pools(p, end, map);
1771 if (err)
1772 goto bad;
Ilya Dryomov9464d002014-03-13 16:36:16 +02001773
Ilya Dryomov0f70c7e2014-03-21 19:05:27 +02001774 /* new_pool_names */
1775 err = decode_pool_names(p, end, map);
Ilya Dryomov9464d002014-03-13 16:36:16 +02001776 if (err)
1777 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001778
Sage Weil4fc51be2010-02-16 15:55:03 -08001779 /* old_pool */
Ilya Dryomov86f17422014-03-13 16:36:15 +02001780 ceph_decode_32_safe(p, end, len, e_inval);
Sage Weil4fc51be2010-02-16 15:55:03 -08001781 while (len--) {
1782 struct ceph_pg_pool_info *pi;
1783
Ilya Dryomov86f17422014-03-13 16:36:15 +02001784 ceph_decode_64_safe(p, end, pool, e_inval);
Sage Weil4fc51be2010-02-16 15:55:03 -08001785 pi = __lookup_pg_pool(&map->pg_pools, pool);
Sage Weil2844a762010-04-09 15:46:42 -07001786 if (pi)
1787 __remove_pg_pool(&map->pg_pools, pi);
Sage Weil4fc51be2010-02-16 15:55:03 -08001788 }
Sage Weilf24e9982009-10-06 11:31:10 -07001789
Ilya Dryomov930c5322016-07-19 03:50:28 +02001790 /* new_up_client, new_state, new_weight */
1791 err = decode_new_up_state_weight(p, end, map);
1792 if (err)
1793 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001794
1795 /* new_pg_temp */
Ilya Dryomov10db6342014-03-21 19:05:28 +02001796 err = decode_new_pg_temp(p, end, map);
1797 if (err)
1798 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001799
Ilya Dryomovd286de72014-03-21 19:05:30 +02001800 /* new_primary_temp */
1801 if (struct_v >= 1) {
1802 err = decode_new_primary_temp(p, end, map);
1803 if (err)
1804 goto bad;
1805 }
1806
Ilya Dryomov63a69932014-03-21 19:05:30 +02001807 /* new_primary_affinity */
1808 if (struct_v >= 2) {
1809 err = decode_new_primary_affinity(p, end, map);
1810 if (err)
1811 goto bad;
1812 }
1813
Ilya Dryomov6f428df2017-06-21 17:27:18 +02001814 if (struct_v >= 3) {
1815 /* new_erasure_code_profiles */
1816 ceph_decode_skip_map_of_map(p, end, string, string, string,
1817 bad);
1818 /* old_erasure_code_profiles */
1819 ceph_decode_skip_set(p, end, string, bad);
1820 }
1821
1822 if (struct_v >= 4) {
1823 err = decode_new_pg_upmap(p, end, map);
1824 if (err)
1825 goto bad;
1826
1827 err = decode_old_pg_upmap(p, end, map);
1828 if (err)
1829 goto bad;
1830
1831 err = decode_new_pg_upmap_items(p, end, map);
1832 if (err)
1833 goto bad;
1834
1835 err = decode_old_pg_upmap_items(p, end, map);
1836 if (err)
1837 goto bad;
1838 }
1839
Sage Weilf24e9982009-10-06 11:31:10 -07001840 /* ignore the rest */
1841 *p = end;
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001842
1843 dout("inc osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001844 return map;
1845
Ilya Dryomov86f17422014-03-13 16:36:15 +02001846e_inval:
1847 err = -EINVAL;
Sage Weilf24e9982009-10-06 11:31:10 -07001848bad:
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001849 pr_err("corrupt inc osdmap (%d) epoch %d off %d (%p of %p-%p)\n",
1850 err, epoch, (int)(*p - start), *p, start, end);
Sage Weil9ec7cab2009-12-14 15:13:47 -08001851 print_hex_dump(KERN_DEBUG, "osdmap: ",
1852 DUMP_PREFIX_OFFSET, 16, 1,
1853 start, end - start, true);
Sage Weilf24e9982009-10-06 11:31:10 -07001854 return ERR_PTR(err);
1855}
1856
Yan, Zheng30c156d2016-02-14 11:24:31 +08001857void ceph_oloc_copy(struct ceph_object_locator *dest,
1858 const struct ceph_object_locator *src)
1859{
Ilya Dryomovca35ffe2017-06-05 14:44:59 +02001860 ceph_oloc_destroy(dest);
Yan, Zheng30c156d2016-02-14 11:24:31 +08001861
1862 dest->pool = src->pool;
1863 if (src->pool_ns)
1864 dest->pool_ns = ceph_get_string(src->pool_ns);
Ilya Dryomovca35ffe2017-06-05 14:44:59 +02001865 else
1866 dest->pool_ns = NULL;
Yan, Zheng30c156d2016-02-14 11:24:31 +08001867}
1868EXPORT_SYMBOL(ceph_oloc_copy);
1869
1870void ceph_oloc_destroy(struct ceph_object_locator *oloc)
1871{
1872 ceph_put_string(oloc->pool_ns);
1873}
1874EXPORT_SYMBOL(ceph_oloc_destroy);
1875
Ilya Dryomovd30291b2016-04-29 19:54:20 +02001876void ceph_oid_copy(struct ceph_object_id *dest,
1877 const struct ceph_object_id *src)
1878{
Ilya Dryomovca35ffe2017-06-05 14:44:59 +02001879 ceph_oid_destroy(dest);
Sage Weilf24e9982009-10-06 11:31:10 -07001880
Ilya Dryomovd30291b2016-04-29 19:54:20 +02001881 if (src->name != src->inline_name) {
1882 /* very rare, see ceph_object_id definition */
1883 dest->name = kmalloc(src->name_len + 1,
1884 GFP_NOIO | __GFP_NOFAIL);
Ilya Dryomovca35ffe2017-06-05 14:44:59 +02001885 } else {
1886 dest->name = dest->inline_name;
Ilya Dryomovd30291b2016-04-29 19:54:20 +02001887 }
Ilya Dryomovd30291b2016-04-29 19:54:20 +02001888 memcpy(dest->name, src->name, src->name_len + 1);
1889 dest->name_len = src->name_len;
1890}
1891EXPORT_SYMBOL(ceph_oid_copy);
1892
1893static __printf(2, 0)
1894int oid_printf_vargs(struct ceph_object_id *oid, const char *fmt, va_list ap)
1895{
1896 int len;
1897
1898 WARN_ON(!ceph_oid_empty(oid));
1899
1900 len = vsnprintf(oid->inline_name, sizeof(oid->inline_name), fmt, ap);
1901 if (len >= sizeof(oid->inline_name))
1902 return len;
1903
1904 oid->name_len = len;
1905 return 0;
1906}
1907
1908/*
1909 * If oid doesn't fit into inline buffer, BUG.
1910 */
1911void ceph_oid_printf(struct ceph_object_id *oid, const char *fmt, ...)
1912{
1913 va_list ap;
1914
1915 va_start(ap, fmt);
1916 BUG_ON(oid_printf_vargs(oid, fmt, ap));
1917 va_end(ap);
1918}
1919EXPORT_SYMBOL(ceph_oid_printf);
1920
1921static __printf(3, 0)
1922int oid_aprintf_vargs(struct ceph_object_id *oid, gfp_t gfp,
1923 const char *fmt, va_list ap)
1924{
1925 va_list aq;
1926 int len;
1927
1928 va_copy(aq, ap);
1929 len = oid_printf_vargs(oid, fmt, aq);
1930 va_end(aq);
1931
1932 if (len) {
1933 char *external_name;
1934
1935 external_name = kmalloc(len + 1, gfp);
1936 if (!external_name)
1937 return -ENOMEM;
1938
1939 oid->name = external_name;
1940 WARN_ON(vsnprintf(oid->name, len + 1, fmt, ap) != len);
1941 oid->name_len = len;
1942 }
1943
1944 return 0;
1945}
1946
1947/*
1948 * If oid doesn't fit into inline buffer, allocate.
1949 */
1950int ceph_oid_aprintf(struct ceph_object_id *oid, gfp_t gfp,
1951 const char *fmt, ...)
1952{
1953 va_list ap;
1954 int ret;
1955
1956 va_start(ap, fmt);
1957 ret = oid_aprintf_vargs(oid, gfp, fmt, ap);
1958 va_end(ap);
1959
1960 return ret;
1961}
1962EXPORT_SYMBOL(ceph_oid_aprintf);
1963
1964void ceph_oid_destroy(struct ceph_object_id *oid)
1965{
1966 if (oid->name != oid->inline_name)
1967 kfree(oid->name);
1968}
1969EXPORT_SYMBOL(ceph_oid_destroy);
Sage Weilf24e9982009-10-06 11:31:10 -07001970
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001971/*
1972 * osds only
1973 */
1974static bool __osds_equal(const struct ceph_osds *lhs,
1975 const struct ceph_osds *rhs)
1976{
1977 if (lhs->size == rhs->size &&
1978 !memcmp(lhs->osds, rhs->osds, rhs->size * sizeof(rhs->osds[0])))
1979 return true;
1980
1981 return false;
1982}
1983
1984/*
1985 * osds + primary
1986 */
1987static bool osds_equal(const struct ceph_osds *lhs,
1988 const struct ceph_osds *rhs)
1989{
1990 if (__osds_equal(lhs, rhs) &&
1991 lhs->primary == rhs->primary)
1992 return true;
1993
1994 return false;
1995}
1996
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02001997static bool osds_valid(const struct ceph_osds *set)
1998{
1999 /* non-empty set */
2000 if (set->size > 0 && set->primary >= 0)
2001 return true;
2002
2003 /* empty can_shift_osds set */
2004 if (!set->size && set->primary == -1)
2005 return true;
2006
2007 /* empty !can_shift_osds set - all NONE */
2008 if (set->size > 0 && set->primary == -1) {
2009 int i;
2010
2011 for (i = 0; i < set->size; i++) {
2012 if (set->osds[i] != CRUSH_ITEM_NONE)
2013 break;
2014 }
2015 if (i == set->size)
2016 return true;
2017 }
2018
2019 return false;
2020}
2021
2022void ceph_osds_copy(struct ceph_osds *dest, const struct ceph_osds *src)
2023{
2024 memcpy(dest->osds, src->osds, src->size * sizeof(src->osds[0]));
2025 dest->size = src->size;
2026 dest->primary = src->primary;
2027}
2028
Ilya Dryomov7de030d2017-06-15 16:30:54 +02002029bool ceph_pg_is_split(const struct ceph_pg *pgid, u32 old_pg_num,
2030 u32 new_pg_num)
Ilya Dryomov63244fa2016-04-28 16:07:23 +02002031{
2032 int old_bits = calc_bits_of(old_pg_num);
2033 int old_mask = (1 << old_bits) - 1;
2034 int n;
2035
2036 WARN_ON(pgid->seed >= old_pg_num);
2037 if (new_pg_num <= old_pg_num)
2038 return false;
2039
2040 for (n = 1; ; n++) {
2041 int next_bit = n << (old_bits - 1);
2042 u32 s = next_bit | pgid->seed;
2043
2044 if (s < old_pg_num || s == pgid->seed)
2045 continue;
2046 if (s >= new_pg_num)
2047 break;
2048
2049 s = ceph_stable_mod(s, old_pg_num, old_mask);
2050 if (s == pgid->seed)
2051 return true;
2052 }
2053
2054 return false;
2055}
2056
2057bool ceph_is_new_interval(const struct ceph_osds *old_acting,
2058 const struct ceph_osds *new_acting,
2059 const struct ceph_osds *old_up,
2060 const struct ceph_osds *new_up,
2061 int old_size,
2062 int new_size,
2063 int old_min_size,
2064 int new_min_size,
2065 u32 old_pg_num,
2066 u32 new_pg_num,
2067 bool old_sort_bitwise,
2068 bool new_sort_bitwise,
2069 const struct ceph_pg *pgid)
2070{
2071 return !osds_equal(old_acting, new_acting) ||
2072 !osds_equal(old_up, new_up) ||
2073 old_size != new_size ||
2074 old_min_size != new_min_size ||
Ilya Dryomov7de030d2017-06-15 16:30:54 +02002075 ceph_pg_is_split(pgid, old_pg_num, new_pg_num) ||
Ilya Dryomov63244fa2016-04-28 16:07:23 +02002076 old_sort_bitwise != new_sort_bitwise;
2077}
2078
2079static int calc_pg_rank(int osd, const struct ceph_osds *acting)
2080{
2081 int i;
2082
2083 for (i = 0; i < acting->size; i++) {
2084 if (acting->osds[i] == osd)
2085 return i;
2086 }
2087
2088 return -1;
2089}
2090
2091static bool primary_changed(const struct ceph_osds *old_acting,
2092 const struct ceph_osds *new_acting)
2093{
2094 if (!old_acting->size && !new_acting->size)
2095 return false; /* both still empty */
2096
2097 if (!old_acting->size ^ !new_acting->size)
2098 return true; /* was empty, now not, or vice versa */
2099
2100 if (old_acting->primary != new_acting->primary)
2101 return true; /* primary changed */
2102
2103 if (calc_pg_rank(old_acting->primary, old_acting) !=
2104 calc_pg_rank(new_acting->primary, new_acting))
2105 return true;
2106
2107 return false; /* same primary (tho replicas may have changed) */
2108}
2109
2110bool ceph_osds_changed(const struct ceph_osds *old_acting,
2111 const struct ceph_osds *new_acting,
2112 bool any_change)
2113{
2114 if (primary_changed(old_acting, new_acting))
2115 return true;
2116
2117 if (any_change && !__osds_equal(old_acting, new_acting))
2118 return true;
2119
2120 return false;
2121}
2122
Sage Weilf24e9982009-10-06 11:31:10 -07002123/*
2124 * calculate file layout from given offset, length.
2125 * fill in correct oid, logical length, and object extent
2126 * offset, length.
2127 *
2128 * for now, we write only a single su, until we can
2129 * pass a stride back to the caller.
2130 */
Sage Weild63b77f2012-09-24 20:59:48 -07002131int ceph_calc_file_object_mapping(struct ceph_file_layout *layout,
Alex Eldere8afad62012-11-14 09:38:19 -06002132 u64 off, u64 len,
Sage Weil645a1022009-10-28 15:15:05 -07002133 u64 *ono,
Sage Weilf24e9982009-10-06 11:31:10 -07002134 u64 *oxoff, u64 *oxlen)
2135{
Yan, Zheng76271512016-02-03 21:24:49 +08002136 u32 osize = layout->object_size;
2137 u32 su = layout->stripe_unit;
2138 u32 sc = layout->stripe_count;
Sage Weilf24e9982009-10-06 11:31:10 -07002139 u32 bl, stripeno, stripepos, objsetno;
2140 u32 su_per_object;
Noah Watkinsff1d1f72009-10-30 12:57:30 -07002141 u64 t, su_offset;
Sage Weilf24e9982009-10-06 11:31:10 -07002142
Alex Eldere8afad62012-11-14 09:38:19 -06002143 dout("mapping %llu~%llu osize %u fl_su %u\n", off, len,
Sage Weilf24e9982009-10-06 11:31:10 -07002144 osize, su);
Sage Weild63b77f2012-09-24 20:59:48 -07002145 if (su == 0 || sc == 0)
2146 goto invalid;
Noah Watkins35e054a2009-10-28 14:04:48 -07002147 su_per_object = osize / su;
Sage Weild63b77f2012-09-24 20:59:48 -07002148 if (su_per_object == 0)
2149 goto invalid;
Sage Weilf24e9982009-10-06 11:31:10 -07002150 dout("osize %u / su %u = su_per_object %u\n", osize, su,
2151 su_per_object);
2152
Sage Weild63b77f2012-09-24 20:59:48 -07002153 if ((su & ~PAGE_MASK) != 0)
2154 goto invalid;
2155
Sage Weilf24e9982009-10-06 11:31:10 -07002156 /* bl = *off / su; */
2157 t = off;
2158 do_div(t, su);
2159 bl = t;
2160 dout("off %llu / su %u = bl %u\n", off, su, bl);
2161
2162 stripeno = bl / sc;
2163 stripepos = bl % sc;
2164 objsetno = stripeno / su_per_object;
2165
Sage Weil645a1022009-10-28 15:15:05 -07002166 *ono = objsetno * sc + stripepos;
Eric Dumazet95c96172012-04-15 05:58:06 +00002167 dout("objset %u * sc %u = ono %u\n", objsetno, sc, (unsigned int)*ono);
Sage Weil645a1022009-10-28 15:15:05 -07002168
2169 /* *oxoff = *off % layout->fl_stripe_unit; # offset in su */
Sage Weilf24e9982009-10-06 11:31:10 -07002170 t = off;
Noah Watkinsff1d1f72009-10-30 12:57:30 -07002171 su_offset = do_div(t, su);
2172 *oxoff = su_offset + (stripeno % su_per_object) * su;
Sage Weil645a1022009-10-28 15:15:05 -07002173
Noah Watkinsff1d1f72009-10-30 12:57:30 -07002174 /*
2175 * Calculate the length of the extent being written to the selected
Alex Eldere8afad62012-11-14 09:38:19 -06002176 * object. This is the minimum of the full length requested (len) or
Noah Watkinsff1d1f72009-10-30 12:57:30 -07002177 * the remainder of the current stripe being written to.
2178 */
Alex Eldere8afad62012-11-14 09:38:19 -06002179 *oxlen = min_t(u64, len, su - su_offset);
Sage Weilf24e9982009-10-06 11:31:10 -07002180
2181 dout(" obj extent %llu~%llu\n", *oxoff, *oxlen);
Sage Weild63b77f2012-09-24 20:59:48 -07002182 return 0;
2183
2184invalid:
2185 dout(" invalid layout\n");
2186 *ono = 0;
2187 *oxoff = 0;
2188 *oxlen = 0;
2189 return -EINVAL;
Sage Weilf24e9982009-10-06 11:31:10 -07002190}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002191EXPORT_SYMBOL(ceph_calc_file_object_mapping);
Sage Weilf24e9982009-10-06 11:31:10 -07002192
2193/*
Ilya Dryomovd9591f52016-04-28 16:07:22 +02002194 * Map an object into a PG.
2195 *
2196 * Should only be called with target_oid and target_oloc (as opposed to
2197 * base_oid and base_oloc), since tiering isn't taken into account.
Sage Weilf24e9982009-10-06 11:31:10 -07002198 */
Ilya Dryomovdf281522017-06-15 16:30:56 +02002199int __ceph_object_locator_to_pg(struct ceph_pg_pool_info *pi,
2200 const struct ceph_object_id *oid,
2201 const struct ceph_object_locator *oloc,
2202 struct ceph_pg *raw_pgid)
Sage Weilf24e9982009-10-06 11:31:10 -07002203{
Ilya Dryomovdf281522017-06-15 16:30:56 +02002204 WARN_ON(pi->id != oloc->pool);
Sage Weilf24e9982009-10-06 11:31:10 -07002205
Yan, Zheng30c156d2016-02-14 11:24:31 +08002206 if (!oloc->pool_ns) {
2207 raw_pgid->pool = oloc->pool;
2208 raw_pgid->seed = ceph_str_hash(pi->object_hash, oid->name,
2209 oid->name_len);
2210 dout("%s %s -> raw_pgid %llu.%x\n", __func__, oid->name,
2211 raw_pgid->pool, raw_pgid->seed);
2212 } else {
2213 char stack_buf[256];
2214 char *buf = stack_buf;
2215 int nsl = oloc->pool_ns->len;
2216 size_t total = nsl + 1 + oid->name_len;
Ilya Dryomov7c13cb62014-01-27 17:40:19 +02002217
Yan, Zheng30c156d2016-02-14 11:24:31 +08002218 if (total > sizeof(stack_buf)) {
2219 buf = kmalloc(total, GFP_NOIO);
2220 if (!buf)
2221 return -ENOMEM;
2222 }
2223 memcpy(buf, oloc->pool_ns->str, nsl);
2224 buf[nsl] = '\037';
2225 memcpy(buf + nsl + 1, oid->name, oid->name_len);
2226 raw_pgid->pool = oloc->pool;
2227 raw_pgid->seed = ceph_str_hash(pi->object_hash, buf, total);
2228 if (buf != stack_buf)
2229 kfree(buf);
2230 dout("%s %s ns %.*s -> raw_pgid %llu.%x\n", __func__,
2231 oid->name, nsl, oloc->pool_ns->str,
2232 raw_pgid->pool, raw_pgid->seed);
2233 }
Sage Weilf24e9982009-10-06 11:31:10 -07002234 return 0;
2235}
Ilya Dryomovdf281522017-06-15 16:30:56 +02002236
2237int ceph_object_locator_to_pg(struct ceph_osdmap *osdmap,
2238 const struct ceph_object_id *oid,
2239 const struct ceph_object_locator *oloc,
2240 struct ceph_pg *raw_pgid)
2241{
2242 struct ceph_pg_pool_info *pi;
2243
2244 pi = ceph_pg_pool_by_id(osdmap, oloc->pool);
2245 if (!pi)
2246 return -ENOENT;
2247
2248 return __ceph_object_locator_to_pg(pi, oid, oloc, raw_pgid);
2249}
Ilya Dryomovd9591f52016-04-28 16:07:22 +02002250EXPORT_SYMBOL(ceph_object_locator_to_pg);
Sage Weilf24e9982009-10-06 11:31:10 -07002251
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002252/*
2253 * Map a raw PG (full precision ps) into an actual PG.
2254 */
2255static void raw_pg_to_pg(struct ceph_pg_pool_info *pi,
2256 const struct ceph_pg *raw_pgid,
2257 struct ceph_pg *pgid)
2258{
2259 pgid->pool = raw_pgid->pool;
2260 pgid->seed = ceph_stable_mod(raw_pgid->seed, pi->pg_num,
2261 pi->pg_num_mask);
2262}
2263
2264/*
2265 * Map a raw PG (full precision ps) into a placement ps (placement
2266 * seed). Include pool id in that value so that different pools don't
2267 * use the same seeds.
2268 */
2269static u32 raw_pg_to_pps(struct ceph_pg_pool_info *pi,
2270 const struct ceph_pg *raw_pgid)
2271{
2272 if (pi->flags & CEPH_POOL_FLAG_HASHPSPOOL) {
2273 /* hash pool id and seed so that pool PGs do not overlap */
2274 return crush_hash32_2(CRUSH_HASH_RJENKINS1,
2275 ceph_stable_mod(raw_pgid->seed,
2276 pi->pgp_num,
2277 pi->pgp_num_mask),
2278 raw_pgid->pool);
2279 } else {
2280 /*
2281 * legacy behavior: add ps and pool together. this is
2282 * not a great approach because the PGs from each pool
2283 * will overlap on top of each other: 0.5 == 1.4 ==
2284 * 2.3 == ...
2285 */
2286 return ceph_stable_mod(raw_pgid->seed, pi->pgp_num,
2287 pi->pgp_num_mask) +
2288 (unsigned)raw_pgid->pool;
2289 }
2290}
2291
Ilya Dryomov9d521472014-01-31 17:54:26 +02002292static int do_crush(struct ceph_osdmap *map, int ruleno, int x,
2293 int *result, int result_max,
Ilya Dryomov5cf9c4a2017-06-22 19:44:05 +02002294 const __u32 *weight, int weight_max,
2295 u64 choose_args_index)
Ilya Dryomove8ef19c2013-12-24 21:19:24 +02002296{
Ilya Dryomov5cf9c4a2017-06-22 19:44:05 +02002297 struct crush_choose_arg_map *arg_map;
Ilya Dryomov9d521472014-01-31 17:54:26 +02002298 int r;
Ilya Dryomove8ef19c2013-12-24 21:19:24 +02002299
Ilya Dryomov9d521472014-01-31 17:54:26 +02002300 BUG_ON(result_max > CEPH_PG_MAX_SIZE);
2301
Ilya Dryomov5cf9c4a2017-06-22 19:44:05 +02002302 arg_map = lookup_choose_arg_map(&map->crush->choose_args,
2303 choose_args_index);
2304
Ilya Dryomov743efcf2017-01-31 15:55:06 +01002305 mutex_lock(&map->crush_workspace_mutex);
Ilya Dryomov9d521472014-01-31 17:54:26 +02002306 r = crush_do_rule(map->crush, ruleno, x, result, result_max,
Ilya Dryomov5cf9c4a2017-06-22 19:44:05 +02002307 weight, weight_max, map->crush_workspace,
2308 arg_map ? arg_map->args : NULL);
Ilya Dryomov743efcf2017-01-31 15:55:06 +01002309 mutex_unlock(&map->crush_workspace_mutex);
Ilya Dryomov9d521472014-01-31 17:54:26 +02002310
2311 return r;
Ilya Dryomove8ef19c2013-12-24 21:19:24 +02002312}
2313
Ilya Dryomov1c2e7b42017-06-21 17:27:18 +02002314static void remove_nonexistent_osds(struct ceph_osdmap *osdmap,
2315 struct ceph_pg_pool_info *pi,
2316 struct ceph_osds *set)
2317{
2318 int i;
2319
2320 if (ceph_can_shift_osds(pi)) {
2321 int removed = 0;
2322
2323 /* shift left */
2324 for (i = 0; i < set->size; i++) {
2325 if (!ceph_osd_exists(osdmap, set->osds[i])) {
2326 removed++;
2327 continue;
2328 }
2329 if (removed)
2330 set->osds[i - removed] = set->osds[i];
2331 }
2332 set->size -= removed;
2333 } else {
2334 /* set dne devices to NONE */
2335 for (i = 0; i < set->size; i++) {
2336 if (!ceph_osd_exists(osdmap, set->osds[i]))
2337 set->osds[i] = CRUSH_ITEM_NONE;
2338 }
2339 }
2340}
2341
Sage Weilf24e9982009-10-06 11:31:10 -07002342/*
Ilya Dryomov1c2e7b42017-06-21 17:27:18 +02002343 * Calculate raw set (CRUSH output) for given PG and filter out
2344 * nonexistent OSDs. ->primary is undefined for a raw set.
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002345 *
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002346 * Placement seed (CRUSH input) is returned through @ppps.
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002347 */
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002348static void pg_to_raw_osds(struct ceph_osdmap *osdmap,
2349 struct ceph_pg_pool_info *pi,
2350 const struct ceph_pg *raw_pgid,
2351 struct ceph_osds *raw,
2352 u32 *ppps)
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002353{
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002354 u32 pps = raw_pg_to_pps(pi, raw_pgid);
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002355 int ruleno;
2356 int len;
2357
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002358 ceph_osds_init(raw);
2359 if (ppps)
2360 *ppps = pps;
2361
2362 ruleno = crush_find_rule(osdmap->crush, pi->crush_ruleset, pi->type,
2363 pi->size);
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002364 if (ruleno < 0) {
2365 pr_err("no crush rule: pool %lld ruleset %d type %d size %d\n",
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002366 pi->id, pi->crush_ruleset, pi->type, pi->size);
2367 return;
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002368 }
2369
Ilya Dryomovef9324bb2017-02-08 18:57:48 +01002370 if (pi->size > ARRAY_SIZE(raw->osds)) {
2371 pr_err_ratelimited("pool %lld ruleset %d type %d too wide: size %d > %zu\n",
2372 pi->id, pi->crush_ruleset, pi->type, pi->size,
2373 ARRAY_SIZE(raw->osds));
2374 return;
2375 }
2376
2377 len = do_crush(osdmap, ruleno, pps, raw->osds, pi->size,
Ilya Dryomov5cf9c4a2017-06-22 19:44:05 +02002378 osdmap->osd_weight, osdmap->max_osd, pi->id);
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002379 if (len < 0) {
2380 pr_err("error %d from crush rule %d: pool %lld ruleset %d type %d size %d\n",
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002381 len, ruleno, pi->id, pi->crush_ruleset, pi->type,
2382 pi->size);
2383 return;
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002384 }
2385
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002386 raw->size = len;
Ilya Dryomov1c2e7b42017-06-21 17:27:18 +02002387 remove_nonexistent_osds(osdmap, pi, raw);
2388}
2389
2390/* apply pg_upmap[_items] mappings */
2391static void apply_upmap(struct ceph_osdmap *osdmap,
2392 const struct ceph_pg *pgid,
2393 struct ceph_osds *raw)
2394{
2395 struct ceph_pg_mapping *pg;
2396 int i, j;
2397
2398 pg = lookup_pg_mapping(&osdmap->pg_upmap, pgid);
2399 if (pg) {
2400 /* make sure targets aren't marked out */
2401 for (i = 0; i < pg->pg_upmap.len; i++) {
2402 int osd = pg->pg_upmap.osds[i];
2403
2404 if (osd != CRUSH_ITEM_NONE &&
2405 osd < osdmap->max_osd &&
2406 osdmap->osd_weight[osd] == 0) {
2407 /* reject/ignore explicit mapping */
2408 return;
2409 }
2410 }
2411 for (i = 0; i < pg->pg_upmap.len; i++)
2412 raw->osds[i] = pg->pg_upmap.osds[i];
2413 raw->size = pg->pg_upmap.len;
2414 return;
2415 }
2416
2417 pg = lookup_pg_mapping(&osdmap->pg_upmap_items, pgid);
2418 if (pg) {
2419 /*
2420 * Note: this approach does not allow a bidirectional swap,
2421 * e.g., [[1,2],[2,1]] applied to [0,1,2] -> [0,2,1].
2422 */
2423 for (i = 0; i < pg->pg_upmap_items.len; i++) {
2424 int from = pg->pg_upmap_items.from_to[i][0];
2425 int to = pg->pg_upmap_items.from_to[i][1];
2426 int pos = -1;
2427 bool exists = false;
2428
2429 /* make sure replacement doesn't already appear */
2430 for (j = 0; j < raw->size; j++) {
2431 int osd = raw->osds[j];
2432
2433 if (osd == to) {
2434 exists = true;
2435 break;
2436 }
2437 /* ignore mapping if target is marked out */
2438 if (osd == from && pos < 0 &&
2439 !(to != CRUSH_ITEM_NONE &&
2440 to < osdmap->max_osd &&
2441 osdmap->osd_weight[to] == 0)) {
2442 pos = j;
2443 }
2444 }
2445 if (!exists && pos >= 0) {
2446 raw->osds[pos] = to;
2447 return;
2448 }
2449 }
2450 }
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002451}
2452
2453/*
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002454 * Given raw set, calculate up set and up primary. By definition of an
2455 * up set, the result won't contain nonexistent or down OSDs.
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002456 *
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002457 * This is done in-place - on return @set is the up set. If it's
2458 * empty, ->primary will remain undefined.
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002459 */
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002460static void raw_to_up_osds(struct ceph_osdmap *osdmap,
2461 struct ceph_pg_pool_info *pi,
2462 struct ceph_osds *set)
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002463{
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002464 int i;
2465
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002466 /* ->primary is undefined for a raw set */
2467 BUG_ON(set->primary != -1);
2468
2469 if (ceph_can_shift_osds(pi)) {
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002470 int removed = 0;
2471
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002472 /* shift left */
2473 for (i = 0; i < set->size; i++) {
2474 if (ceph_osd_is_down(osdmap, set->osds[i])) {
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002475 removed++;
2476 continue;
2477 }
2478 if (removed)
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002479 set->osds[i - removed] = set->osds[i];
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002480 }
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002481 set->size -= removed;
2482 if (set->size > 0)
2483 set->primary = set->osds[0];
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002484 } else {
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002485 /* set down/dne devices to NONE */
2486 for (i = set->size - 1; i >= 0; i--) {
2487 if (ceph_osd_is_down(osdmap, set->osds[i]))
2488 set->osds[i] = CRUSH_ITEM_NONE;
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002489 else
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002490 set->primary = set->osds[i];
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002491 }
2492 }
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002493}
2494
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002495static void apply_primary_affinity(struct ceph_osdmap *osdmap,
2496 struct ceph_pg_pool_info *pi,
2497 u32 pps,
2498 struct ceph_osds *up)
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002499{
2500 int i;
2501 int pos = -1;
2502
2503 /*
2504 * Do we have any non-default primary_affinity values for these
2505 * osds?
2506 */
2507 if (!osdmap->osd_primary_affinity)
2508 return;
2509
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002510 for (i = 0; i < up->size; i++) {
2511 int osd = up->osds[i];
Ilya Dryomov92b2e752014-04-10 18:09:41 +04002512
2513 if (osd != CRUSH_ITEM_NONE &&
2514 osdmap->osd_primary_affinity[osd] !=
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002515 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY) {
2516 break;
2517 }
2518 }
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002519 if (i == up->size)
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002520 return;
2521
2522 /*
2523 * Pick the primary. Feed both the seed (for the pg) and the
2524 * osd into the hash/rng so that a proportional fraction of an
2525 * osd's pgs get rejected as primary.
2526 */
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002527 for (i = 0; i < up->size; i++) {
2528 int osd = up->osds[i];
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002529 u32 aff;
2530
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002531 if (osd == CRUSH_ITEM_NONE)
2532 continue;
2533
2534 aff = osdmap->osd_primary_affinity[osd];
2535 if (aff < CEPH_OSD_MAX_PRIMARY_AFFINITY &&
2536 (crush_hash32_2(CRUSH_HASH_RJENKINS1,
2537 pps, osd) >> 16) >= aff) {
2538 /*
2539 * We chose not to use this primary. Note it
2540 * anyway as a fallback in case we don't pick
2541 * anyone else, but keep looking.
2542 */
2543 if (pos < 0)
2544 pos = i;
2545 } else {
2546 pos = i;
2547 break;
2548 }
2549 }
2550 if (pos < 0)
2551 return;
2552
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002553 up->primary = up->osds[pos];
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002554
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002555 if (ceph_can_shift_osds(pi) && pos > 0) {
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002556 /* move the new primary to the front */
2557 for (i = pos; i > 0; i--)
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002558 up->osds[i] = up->osds[i - 1];
2559 up->osds[0] = up->primary;
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002560 }
2561}
2562
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002563/*
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002564 * Get pg_temp and primary_temp mappings for given PG.
Ilya Dryomov45966c32014-03-24 17:12:47 +02002565 *
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002566 * Note that a PG may have none, only pg_temp, only primary_temp or
2567 * both pg_temp and primary_temp mappings. This means @temp isn't
2568 * always a valid OSD set on return: in the "only primary_temp" case,
2569 * @temp will have its ->primary >= 0 but ->size == 0.
Ilya Dryomov45966c32014-03-24 17:12:47 +02002570 */
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002571static void get_temp_osds(struct ceph_osdmap *osdmap,
2572 struct ceph_pg_pool_info *pi,
Ilya Dryomov463bb8d2017-06-21 17:27:18 +02002573 const struct ceph_pg *pgid,
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002574 struct ceph_osds *temp)
Ilya Dryomov45966c32014-03-24 17:12:47 +02002575{
2576 struct ceph_pg_mapping *pg;
Ilya Dryomov45966c32014-03-24 17:12:47 +02002577 int i;
2578
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002579 ceph_osds_init(temp);
Ilya Dryomov45966c32014-03-24 17:12:47 +02002580
2581 /* pg_temp? */
Ilya Dryomov463bb8d2017-06-21 17:27:18 +02002582 pg = lookup_pg_mapping(&osdmap->pg_temp, pgid);
Ilya Dryomov45966c32014-03-24 17:12:47 +02002583 if (pg) {
Ilya Dryomov45966c32014-03-24 17:12:47 +02002584 for (i = 0; i < pg->pg_temp.len; i++) {
2585 if (ceph_osd_is_down(osdmap, pg->pg_temp.osds[i])) {
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002586 if (ceph_can_shift_osds(pi))
Ilya Dryomov45966c32014-03-24 17:12:47 +02002587 continue;
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002588
2589 temp->osds[temp->size++] = CRUSH_ITEM_NONE;
Ilya Dryomov45966c32014-03-24 17:12:47 +02002590 } else {
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002591 temp->osds[temp->size++] = pg->pg_temp.osds[i];
Ilya Dryomov45966c32014-03-24 17:12:47 +02002592 }
2593 }
2594
2595 /* apply pg_temp's primary */
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002596 for (i = 0; i < temp->size; i++) {
2597 if (temp->osds[i] != CRUSH_ITEM_NONE) {
2598 temp->primary = temp->osds[i];
Ilya Dryomov45966c32014-03-24 17:12:47 +02002599 break;
2600 }
2601 }
Ilya Dryomov45966c32014-03-24 17:12:47 +02002602 }
2603
Ilya Dryomov5e8d4d32014-03-24 17:12:48 +02002604 /* primary_temp? */
Ilya Dryomov463bb8d2017-06-21 17:27:18 +02002605 pg = lookup_pg_mapping(&osdmap->primary_temp, pgid);
Ilya Dryomov5e8d4d32014-03-24 17:12:48 +02002606 if (pg)
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002607 temp->primary = pg->primary_temp.osd;
Ilya Dryomov45966c32014-03-24 17:12:47 +02002608}
2609
2610/*
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002611 * Map a PG to its acting set as well as its up set.
Ilya Dryomovac972232014-03-24 17:12:48 +02002612 *
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002613 * Acting set is used for data mapping purposes, while up set can be
2614 * recorded for detecting interval changes and deciding whether to
2615 * resend a request.
Sage Weild85b7052010-05-10 10:24:48 -07002616 */
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002617void ceph_pg_to_up_acting_osds(struct ceph_osdmap *osdmap,
Ilya Dryomovdf281522017-06-15 16:30:56 +02002618 struct ceph_pg_pool_info *pi,
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002619 const struct ceph_pg *raw_pgid,
2620 struct ceph_osds *up,
2621 struct ceph_osds *acting)
Sage Weild85b7052010-05-10 10:24:48 -07002622{
Ilya Dryomov463bb8d2017-06-21 17:27:18 +02002623 struct ceph_pg pgid;
Ilya Dryomovac972232014-03-24 17:12:48 +02002624 u32 pps;
Sage Weild85b7052010-05-10 10:24:48 -07002625
Ilya Dryomovdf281522017-06-15 16:30:56 +02002626 WARN_ON(pi->id != raw_pgid->pool);
Ilya Dryomov463bb8d2017-06-21 17:27:18 +02002627 raw_pg_to_pg(pi, raw_pgid, &pgid);
Sage Weild85b7052010-05-10 10:24:48 -07002628
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002629 pg_to_raw_osds(osdmap, pi, raw_pgid, up, &pps);
Ilya Dryomov1c2e7b42017-06-21 17:27:18 +02002630 apply_upmap(osdmap, &pgid, up);
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002631 raw_to_up_osds(osdmap, pi, up);
2632 apply_primary_affinity(osdmap, pi, pps, up);
Ilya Dryomov463bb8d2017-06-21 17:27:18 +02002633 get_temp_osds(osdmap, pi, &pgid, acting);
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002634 if (!acting->size) {
2635 memcpy(acting->osds, up->osds, up->size * sizeof(up->osds[0]));
2636 acting->size = up->size;
2637 if (acting->primary == -1)
2638 acting->primary = up->primary;
Ilya Dryomovac972232014-03-24 17:12:48 +02002639 }
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002640 WARN_ON(!osds_valid(up) || !osds_valid(acting));
Sage Weild85b7052010-05-10 10:24:48 -07002641}
2642
Ilya Dryomovdc98ff72017-06-15 16:30:53 +02002643bool ceph_pg_to_primary_shard(struct ceph_osdmap *osdmap,
Ilya Dryomovdf281522017-06-15 16:30:56 +02002644 struct ceph_pg_pool_info *pi,
Ilya Dryomovdc98ff72017-06-15 16:30:53 +02002645 const struct ceph_pg *raw_pgid,
2646 struct ceph_spg *spgid)
2647{
Ilya Dryomovdc98ff72017-06-15 16:30:53 +02002648 struct ceph_pg pgid;
2649 struct ceph_osds up, acting;
2650 int i;
2651
Ilya Dryomovdf281522017-06-15 16:30:56 +02002652 WARN_ON(pi->id != raw_pgid->pool);
Ilya Dryomovdc98ff72017-06-15 16:30:53 +02002653 raw_pg_to_pg(pi, raw_pgid, &pgid);
2654
2655 if (ceph_can_shift_osds(pi)) {
2656 spgid->pgid = pgid; /* struct */
2657 spgid->shard = CEPH_SPG_NOSHARD;
2658 return true;
2659 }
2660
Ilya Dryomovdf281522017-06-15 16:30:56 +02002661 ceph_pg_to_up_acting_osds(osdmap, pi, &pgid, &up, &acting);
Ilya Dryomovdc98ff72017-06-15 16:30:53 +02002662 for (i = 0; i < acting.size; i++) {
2663 if (acting.osds[i] == acting.primary) {
2664 spgid->pgid = pgid; /* struct */
2665 spgid->shard = i;
2666 return true;
2667 }
2668 }
2669
2670 return false;
2671}
2672
Sage Weild85b7052010-05-10 10:24:48 -07002673/*
Ilya Dryomovf81f1632016-04-28 16:07:23 +02002674 * Return acting primary for given PG, or -1 if none.
Sage Weilf24e9982009-10-06 11:31:10 -07002675 */
Ilya Dryomovf81f1632016-04-28 16:07:23 +02002676int ceph_pg_to_acting_primary(struct ceph_osdmap *osdmap,
2677 const struct ceph_pg *raw_pgid)
Sage Weilf24e9982009-10-06 11:31:10 -07002678{
Ilya Dryomovdf281522017-06-15 16:30:56 +02002679 struct ceph_pg_pool_info *pi;
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002680 struct ceph_osds up, acting;
Sage Weilf24e9982009-10-06 11:31:10 -07002681
Ilya Dryomovdf281522017-06-15 16:30:56 +02002682 pi = ceph_pg_pool_by_id(osdmap, raw_pgid->pool);
2683 if (!pi)
2684 return -1;
2685
2686 ceph_pg_to_up_acting_osds(osdmap, pi, raw_pgid, &up, &acting);
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002687 return acting.primary;
Sage Weilf24e9982009-10-06 11:31:10 -07002688}
Ilya Dryomovf81f1632016-04-28 16:07:23 +02002689EXPORT_SYMBOL(ceph_pg_to_acting_primary);