blob: 41b380ac68acd3632f6fa2c4f2137b02024bb13d [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
Sage Weil546f04e2012-07-30 18:15:23 -0700141static int skip_name_map(void **p, void *end)
142{
143 int len;
144 ceph_decode_32_safe(p, end, len ,bad);
145 while (len--) {
146 int strlen;
147 *p += sizeof(u32);
148 ceph_decode_32_safe(p, end, strlen, bad);
149 *p += strlen;
150}
151 return 0;
152bad:
153 return -EINVAL;
154}
155
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100156static void crush_finalize(struct crush_map *c)
157{
158 __s32 b;
159
160 /* Space for the array of pointers to per-bucket workspace */
161 c->working_size = sizeof(struct crush_work) +
162 c->max_buckets * sizeof(struct crush_work_bucket *);
163
164 for (b = 0; b < c->max_buckets; b++) {
165 if (!c->buckets[b])
166 continue;
167
168 switch (c->buckets[b]->alg) {
169 default:
170 /*
171 * The base case, permutation variables and
172 * the pointer to the permutation array.
173 */
174 c->working_size += sizeof(struct crush_work_bucket);
175 break;
176 }
177 /* Every bucket has a permutation array. */
178 c->working_size += c->buckets[b]->size * sizeof(__u32);
179 }
180}
181
Sage Weilf24e9982009-10-06 11:31:10 -0700182static struct crush_map *crush_decode(void *pbyval, void *end)
183{
184 struct crush_map *c;
185 int err = -EINVAL;
186 int i, j;
187 void **p = &pbyval;
188 void *start = pbyval;
189 u32 magic;
Sage Weil546f04e2012-07-30 18:15:23 -0700190 u32 num_name_maps;
Sage Weilf24e9982009-10-06 11:31:10 -0700191
192 dout("crush_decode %p to %p len %d\n", *p, end, (int)(end - *p));
193
194 c = kzalloc(sizeof(*c), GFP_NOFS);
195 if (c == NULL)
196 return ERR_PTR(-ENOMEM);
197
Sage Weil546f04e2012-07-30 18:15:23 -0700198 /* set tunables to default values */
199 c->choose_local_tries = 2;
200 c->choose_local_fallback_tries = 5;
201 c->choose_total_tries = 19;
Jim Schutt1604f482012-11-30 09:15:25 -0700202 c->chooseleaf_descend_once = 0;
Sage Weil546f04e2012-07-30 18:15:23 -0700203
Sage Weilf24e9982009-10-06 11:31:10 -0700204 ceph_decode_need(p, end, 4*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700205 magic = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700206 if (magic != CRUSH_MAGIC) {
207 pr_err("crush_decode magic %x != current %x\n",
Eric Dumazet95c96172012-04-15 05:58:06 +0000208 (unsigned int)magic, (unsigned int)CRUSH_MAGIC);
Sage Weilf24e9982009-10-06 11:31:10 -0700209 goto bad;
210 }
Sage Weilc89136e2009-10-14 09:59:09 -0700211 c->max_buckets = ceph_decode_32(p);
212 c->max_rules = ceph_decode_32(p);
213 c->max_devices = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700214
Sage Weilf24e9982009-10-06 11:31:10 -0700215 c->buckets = kcalloc(c->max_buckets, sizeof(*c->buckets), GFP_NOFS);
216 if (c->buckets == NULL)
217 goto badmem;
218 c->rules = kcalloc(c->max_rules, sizeof(*c->rules), GFP_NOFS);
219 if (c->rules == NULL)
220 goto badmem;
221
222 /* buckets */
223 for (i = 0; i < c->max_buckets; i++) {
224 int size = 0;
225 u32 alg;
226 struct crush_bucket *b;
227
228 ceph_decode_32_safe(p, end, alg, bad);
229 if (alg == 0) {
230 c->buckets[i] = NULL;
231 continue;
232 }
233 dout("crush_decode bucket %d off %x %p to %p\n",
234 i, (int)(*p-start), *p, end);
235
236 switch (alg) {
237 case CRUSH_BUCKET_UNIFORM:
238 size = sizeof(struct crush_bucket_uniform);
239 break;
240 case CRUSH_BUCKET_LIST:
241 size = sizeof(struct crush_bucket_list);
242 break;
243 case CRUSH_BUCKET_TREE:
244 size = sizeof(struct crush_bucket_tree);
245 break;
246 case CRUSH_BUCKET_STRAW:
247 size = sizeof(struct crush_bucket_straw);
248 break;
Ilya Dryomov958a2762015-04-14 16:54:52 +0300249 case CRUSH_BUCKET_STRAW2:
250 size = sizeof(struct crush_bucket_straw2);
251 break;
Sage Weilf24e9982009-10-06 11:31:10 -0700252 default:
Sage Weil30dc6382009-12-21 14:49:37 -0800253 err = -EINVAL;
Sage Weilf24e9982009-10-06 11:31:10 -0700254 goto bad;
255 }
256 BUG_ON(size == 0);
257 b = c->buckets[i] = kzalloc(size, GFP_NOFS);
258 if (b == NULL)
259 goto badmem;
260
261 ceph_decode_need(p, end, 4*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700262 b->id = ceph_decode_32(p);
263 b->type = ceph_decode_16(p);
Sage Weilfb690392009-11-07 20:18:22 -0800264 b->alg = ceph_decode_8(p);
265 b->hash = ceph_decode_8(p);
Sage Weilc89136e2009-10-14 09:59:09 -0700266 b->weight = ceph_decode_32(p);
267 b->size = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700268
269 dout("crush_decode bucket size %d off %x %p to %p\n",
270 b->size, (int)(*p-start), *p, end);
271
272 b->items = kcalloc(b->size, sizeof(__s32), GFP_NOFS);
273 if (b->items == NULL)
274 goto badmem;
Sage Weilf24e9982009-10-06 11:31:10 -0700275
276 ceph_decode_need(p, end, b->size*sizeof(u32), bad);
277 for (j = 0; j < b->size; j++)
Sage Weilc89136e2009-10-14 09:59:09 -0700278 b->items[j] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700279
280 switch (b->alg) {
281 case CRUSH_BUCKET_UNIFORM:
282 err = crush_decode_uniform_bucket(p, end,
283 (struct crush_bucket_uniform *)b);
284 if (err < 0)
285 goto bad;
286 break;
287 case CRUSH_BUCKET_LIST:
288 err = crush_decode_list_bucket(p, end,
289 (struct crush_bucket_list *)b);
290 if (err < 0)
291 goto bad;
292 break;
293 case CRUSH_BUCKET_TREE:
294 err = crush_decode_tree_bucket(p, end,
295 (struct crush_bucket_tree *)b);
296 if (err < 0)
297 goto bad;
298 break;
299 case CRUSH_BUCKET_STRAW:
300 err = crush_decode_straw_bucket(p, end,
301 (struct crush_bucket_straw *)b);
302 if (err < 0)
303 goto bad;
304 break;
Ilya Dryomov958a2762015-04-14 16:54:52 +0300305 case CRUSH_BUCKET_STRAW2:
306 err = crush_decode_straw2_bucket(p, end,
307 (struct crush_bucket_straw2 *)b);
308 if (err < 0)
309 goto bad;
310 break;
Sage Weilf24e9982009-10-06 11:31:10 -0700311 }
312 }
313
314 /* rules */
315 dout("rule vec is %p\n", c->rules);
316 for (i = 0; i < c->max_rules; i++) {
317 u32 yes;
318 struct crush_rule *r;
319
Dan Carpenter293dffa2017-05-23 17:25:10 +0300320 err = -EINVAL;
Sage Weilf24e9982009-10-06 11:31:10 -0700321 ceph_decode_32_safe(p, end, yes, bad);
322 if (!yes) {
323 dout("crush_decode NO rule %d off %x %p to %p\n",
324 i, (int)(*p-start), *p, end);
325 c->rules[i] = NULL;
326 continue;
327 }
328
329 dout("crush_decode rule %d off %x %p to %p\n",
330 i, (int)(*p-start), *p, end);
331
332 /* len */
333 ceph_decode_32_safe(p, end, yes, bad);
334#if BITS_PER_LONG == 32
Sage Weil30dc6382009-12-21 14:49:37 -0800335 err = -EINVAL;
Xi Wang64486692012-02-16 11:55:48 -0500336 if (yes > (ULONG_MAX - sizeof(*r))
337 / sizeof(struct crush_rule_step))
Sage Weilf24e9982009-10-06 11:31:10 -0700338 goto bad;
339#endif
340 r = c->rules[i] = kmalloc(sizeof(*r) +
341 yes*sizeof(struct crush_rule_step),
342 GFP_NOFS);
343 if (r == NULL)
344 goto badmem;
345 dout(" rule %d is at %p\n", i, r);
346 r->len = yes;
347 ceph_decode_copy_safe(p, end, &r->mask, 4, bad); /* 4 u8's */
348 ceph_decode_need(p, end, r->len*3*sizeof(u32), bad);
349 for (j = 0; j < r->len; j++) {
Sage Weilc89136e2009-10-14 09:59:09 -0700350 r->steps[j].op = ceph_decode_32(p);
351 r->steps[j].arg1 = ceph_decode_32(p);
352 r->steps[j].arg2 = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700353 }
354 }
355
356 /* ignore trailing name maps. */
Sage Weil546f04e2012-07-30 18:15:23 -0700357 for (num_name_maps = 0; num_name_maps < 3; num_name_maps++) {
358 err = skip_name_map(p, end);
359 if (err < 0)
360 goto done;
361 }
Sage Weilf24e9982009-10-06 11:31:10 -0700362
Sage Weil546f04e2012-07-30 18:15:23 -0700363 /* tunables */
364 ceph_decode_need(p, end, 3*sizeof(u32), done);
365 c->choose_local_tries = ceph_decode_32(p);
366 c->choose_local_fallback_tries = ceph_decode_32(p);
367 c->choose_total_tries = ceph_decode_32(p);
Ilya Dryomovb9b519b2016-02-01 16:57:16 +0100368 dout("crush decode tunable choose_local_tries = %d\n",
Sage Weil546f04e2012-07-30 18:15:23 -0700369 c->choose_local_tries);
Ilya Dryomovb9b519b2016-02-01 16:57:16 +0100370 dout("crush decode tunable choose_local_fallback_tries = %d\n",
Sage Weil546f04e2012-07-30 18:15:23 -0700371 c->choose_local_fallback_tries);
Ilya Dryomovb9b519b2016-02-01 16:57:16 +0100372 dout("crush decode tunable choose_total_tries = %d\n",
Sage Weil546f04e2012-07-30 18:15:23 -0700373 c->choose_total_tries);
374
Jim Schutt1604f482012-11-30 09:15:25 -0700375 ceph_decode_need(p, end, sizeof(u32), done);
376 c->chooseleaf_descend_once = ceph_decode_32(p);
Ilya Dryomovb9b519b2016-02-01 16:57:16 +0100377 dout("crush decode tunable chooseleaf_descend_once = %d\n",
Jim Schutt1604f482012-11-30 09:15:25 -0700378 c->chooseleaf_descend_once);
379
Ilya Dryomovf1406622014-05-09 18:27:34 +0400380 ceph_decode_need(p, end, sizeof(u8), done);
381 c->chooseleaf_vary_r = ceph_decode_8(p);
Ilya Dryomovb9b519b2016-02-01 16:57:16 +0100382 dout("crush decode tunable chooseleaf_vary_r = %d\n",
Ilya Dryomovf1406622014-05-09 18:27:34 +0400383 c->chooseleaf_vary_r);
384
Ilya Dryomovb9b519b2016-02-01 16:57:16 +0100385 /* skip straw_calc_version, allowed_bucket_algs */
386 ceph_decode_need(p, end, sizeof(u8) + sizeof(u32), done);
387 *p += sizeof(u8) + sizeof(u32);
388
389 ceph_decode_need(p, end, sizeof(u8), done);
390 c->chooseleaf_stable = ceph_decode_8(p);
391 dout("crush decode tunable chooseleaf_stable = %d\n",
392 c->chooseleaf_stable);
393
Sage Weil546f04e2012-07-30 18:15:23 -0700394done:
Ilya Dryomov9afd30d2017-02-28 18:53:53 +0100395 crush_finalize(c);
Sage Weilf24e9982009-10-06 11:31:10 -0700396 dout("crush_decode success\n");
397 return c;
398
399badmem:
400 err = -ENOMEM;
401bad:
402 dout("crush_decode fail %d\n", err);
403 crush_destroy(c);
404 return ERR_PTR(err);
405}
406
Ilya Dryomovf984cb72016-04-28 16:07:23 +0200407int ceph_pg_compare(const struct ceph_pg *lhs, const struct ceph_pg *rhs)
408{
409 if (lhs->pool < rhs->pool)
410 return -1;
411 if (lhs->pool > rhs->pool)
412 return 1;
413 if (lhs->seed < rhs->seed)
414 return -1;
415 if (lhs->seed > rhs->seed)
416 return 1;
417
418 return 0;
419}
420
Ilya Dryomova02a9462017-06-19 12:18:05 +0200421int ceph_spg_compare(const struct ceph_spg *lhs, const struct ceph_spg *rhs)
422{
423 int ret;
424
425 ret = ceph_pg_compare(&lhs->pgid, &rhs->pgid);
426 if (ret)
427 return ret;
428
429 if (lhs->shard < rhs->shard)
430 return -1;
431 if (lhs->shard > rhs->shard)
432 return 1;
433
434 return 0;
435}
436
Ilya Dryomova303bb02017-06-21 17:27:17 +0200437static struct ceph_pg_mapping *alloc_pg_mapping(size_t payload_len)
438{
439 struct ceph_pg_mapping *pg;
440
441 pg = kmalloc(sizeof(*pg) + payload_len, GFP_NOIO);
442 if (!pg)
443 return NULL;
444
445 RB_CLEAR_NODE(&pg->node);
446 return pg;
447}
448
449static void free_pg_mapping(struct ceph_pg_mapping *pg)
450{
451 WARN_ON(!RB_EMPTY_NODE(&pg->node));
452
453 kfree(pg);
454}
455
Sage Weilf24e9982009-10-06 11:31:10 -0700456/*
Sage Weil9794b142010-02-16 15:53:32 -0800457 * rbtree of pg_mapping for handling pg_temp (explicit mapping of pgid
Ilya Dryomov9686f942014-03-21 19:05:29 +0200458 * to a set of osds) and primary_temp (explicit primary setting)
Sage Weilf24e9982009-10-06 11:31:10 -0700459 */
Sage Weil991abb62009-10-08 22:22:37 -0700460static int __insert_pg_mapping(struct ceph_pg_mapping *new,
461 struct rb_root *root)
Sage Weilf24e9982009-10-06 11:31:10 -0700462{
463 struct rb_node **p = &root->rb_node;
464 struct rb_node *parent = NULL;
465 struct ceph_pg_mapping *pg = NULL;
Sage Weil51042122009-11-04 11:39:12 -0800466 int c;
Sage Weilf24e9982009-10-06 11:31:10 -0700467
Sage Weil8adc8b32011-09-28 10:11:04 -0700468 dout("__insert_pg_mapping %llx %p\n", *(u64 *)&new->pgid, new);
Sage Weilf24e9982009-10-06 11:31:10 -0700469 while (*p) {
470 parent = *p;
471 pg = rb_entry(parent, struct ceph_pg_mapping, node);
Ilya Dryomovf984cb72016-04-28 16:07:23 +0200472 c = ceph_pg_compare(&new->pgid, &pg->pgid);
Sage Weil51042122009-11-04 11:39:12 -0800473 if (c < 0)
Sage Weilf24e9982009-10-06 11:31:10 -0700474 p = &(*p)->rb_left;
Sage Weil51042122009-11-04 11:39:12 -0800475 else if (c > 0)
Sage Weilf24e9982009-10-06 11:31:10 -0700476 p = &(*p)->rb_right;
477 else
Sage Weil991abb62009-10-08 22:22:37 -0700478 return -EEXIST;
Sage Weilf24e9982009-10-06 11:31:10 -0700479 }
480
481 rb_link_node(&new->node, parent, p);
482 rb_insert_color(&new->node, root);
Sage Weil991abb62009-10-08 22:22:37 -0700483 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700484}
485
Sage Weil9794b142010-02-16 15:53:32 -0800486static struct ceph_pg_mapping *__lookup_pg_mapping(struct rb_root *root,
Ilya Dryomov33333d12017-06-21 17:27:17 +0200487 const struct ceph_pg *pgid)
Sage Weil9794b142010-02-16 15:53:32 -0800488{
489 struct rb_node *n = root->rb_node;
490 struct ceph_pg_mapping *pg;
491 int c;
492
493 while (n) {
494 pg = rb_entry(n, struct ceph_pg_mapping, node);
Ilya Dryomov33333d12017-06-21 17:27:17 +0200495 c = ceph_pg_compare(pgid, &pg->pgid);
Sage Weil8adc8b32011-09-28 10:11:04 -0700496 if (c < 0) {
Sage Weil9794b142010-02-16 15:53:32 -0800497 n = n->rb_left;
Sage Weil8adc8b32011-09-28 10:11:04 -0700498 } else if (c > 0) {
Sage Weil9794b142010-02-16 15:53:32 -0800499 n = n->rb_right;
Sage Weil8adc8b32011-09-28 10:11:04 -0700500 } else {
Sage Weil5b191d92013-02-23 10:38:16 -0800501 dout("__lookup_pg_mapping %lld.%x got %p\n",
Ilya Dryomov33333d12017-06-21 17:27:17 +0200502 pgid->pool, pgid->seed, pg);
Sage Weil9794b142010-02-16 15:53:32 -0800503 return pg;
Sage Weil8adc8b32011-09-28 10:11:04 -0700504 }
Sage Weil9794b142010-02-16 15:53:32 -0800505 }
506 return NULL;
507}
508
Ilya Dryomov33333d12017-06-21 17:27:17 +0200509static int __remove_pg_mapping(struct rb_root *root, const struct ceph_pg *pgid)
Sage Weil8adc8b32011-09-28 10:11:04 -0700510{
511 struct ceph_pg_mapping *pg = __lookup_pg_mapping(root, pgid);
512
513 if (pg) {
Ilya Dryomov33333d12017-06-21 17:27:17 +0200514 dout("__remove_pg_mapping %lld.%x %p\n", pgid->pool, pgid->seed,
Sage Weil5b191d92013-02-23 10:38:16 -0800515 pg);
Sage Weil8adc8b32011-09-28 10:11:04 -0700516 rb_erase(&pg->node, root);
517 kfree(pg);
518 return 0;
519 }
Ilya Dryomov33333d12017-06-21 17:27:17 +0200520 dout("__remove_pg_mapping %lld.%x dne\n", pgid->pool, pgid->seed);
Sage Weil8adc8b32011-09-28 10:11:04 -0700521 return -ENOENT;
522}
523
Sage Weilf24e9982009-10-06 11:31:10 -0700524/*
Sage Weil4fc51be2010-02-16 15:55:03 -0800525 * rbtree of pg pool info
526 */
527static int __insert_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *new)
528{
529 struct rb_node **p = &root->rb_node;
530 struct rb_node *parent = NULL;
531 struct ceph_pg_pool_info *pi = NULL;
532
533 while (*p) {
534 parent = *p;
535 pi = rb_entry(parent, struct ceph_pg_pool_info, node);
536 if (new->id < pi->id)
537 p = &(*p)->rb_left;
538 else if (new->id > pi->id)
539 p = &(*p)->rb_right;
540 else
541 return -EEXIST;
542 }
543
544 rb_link_node(&new->node, parent, p);
545 rb_insert_color(&new->node, root);
546 return 0;
547}
548
Sage Weil4f6a7e52013-02-23 10:41:09 -0800549static struct ceph_pg_pool_info *__lookup_pg_pool(struct rb_root *root, u64 id)
Sage Weil4fc51be2010-02-16 15:55:03 -0800550{
551 struct ceph_pg_pool_info *pi;
552 struct rb_node *n = root->rb_node;
553
554 while (n) {
555 pi = rb_entry(n, struct ceph_pg_pool_info, node);
556 if (id < pi->id)
557 n = n->rb_left;
558 else if (id > pi->id)
559 n = n->rb_right;
560 else
561 return pi;
562 }
563 return NULL;
564}
565
Ilya Dryomovce7f6a22014-01-27 17:40:19 +0200566struct ceph_pg_pool_info *ceph_pg_pool_by_id(struct ceph_osdmap *map, u64 id)
567{
568 return __lookup_pg_pool(&map->pg_pools, id);
569}
570
Alex Elder72afc712012-10-30 19:40:33 -0500571const char *ceph_pg_pool_name_by_id(struct ceph_osdmap *map, u64 id)
572{
573 struct ceph_pg_pool_info *pi;
574
575 if (id == CEPH_NOPOOL)
576 return NULL;
577
578 if (WARN_ON_ONCE(id > (u64) INT_MAX))
579 return NULL;
580
581 pi = __lookup_pg_pool(&map->pg_pools, (int) id);
582
583 return pi ? pi->name : NULL;
584}
585EXPORT_SYMBOL(ceph_pg_pool_name_by_id);
586
Yehuda Sadeh7669a2c2010-05-17 12:31:35 -0700587int ceph_pg_poolid_by_name(struct ceph_osdmap *map, const char *name)
588{
589 struct rb_node *rbp;
590
591 for (rbp = rb_first(&map->pg_pools); rbp; rbp = rb_next(rbp)) {
592 struct ceph_pg_pool_info *pi =
593 rb_entry(rbp, struct ceph_pg_pool_info, node);
594 if (pi->name && strcmp(pi->name, name) == 0)
595 return pi->id;
596 }
597 return -ENOENT;
598}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700599EXPORT_SYMBOL(ceph_pg_poolid_by_name);
Yehuda Sadeh7669a2c2010-05-17 12:31:35 -0700600
Sage Weil2844a762010-04-09 15:46:42 -0700601static void __remove_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *pi)
602{
603 rb_erase(&pi->node, root);
604 kfree(pi->name);
605 kfree(pi);
606}
607
Ilya Dryomov0f70c7e2014-03-21 19:05:27 +0200608static int decode_pool(void **p, void *end, struct ceph_pg_pool_info *pi)
Sage Weilefd75762010-03-17 10:05:28 -0700609{
Sage Weil4f6a7e52013-02-23 10:41:09 -0800610 u8 ev, cv;
611 unsigned len, num;
612 void *pool_end;
Sage Weil73a7e692010-08-02 11:00:55 -0700613
Sage Weil4f6a7e52013-02-23 10:41:09 -0800614 ceph_decode_need(p, end, 2 + 4, bad);
615 ev = ceph_decode_8(p); /* encoding version */
616 cv = ceph_decode_8(p); /* compat version */
617 if (ev < 5) {
Joe Perchesb9a67892014-09-09 21:17:29 -0700618 pr_warn("got v %d < 5 cv %d of ceph_pg_pool\n", ev, cv);
Sage Weil4f6a7e52013-02-23 10:41:09 -0800619 return -EINVAL;
620 }
Ilya Dryomov17a13e402014-01-27 17:40:19 +0200621 if (cv > 9) {
Joe Perchesb9a67892014-09-09 21:17:29 -0700622 pr_warn("got v %d cv %d > 9 of ceph_pg_pool\n", ev, cv);
Sage Weil4f6a7e52013-02-23 10:41:09 -0800623 return -EINVAL;
624 }
625 len = ceph_decode_32(p);
626 ceph_decode_need(p, end, len, bad);
627 pool_end = *p + len;
Sage Weil73a7e692010-08-02 11:00:55 -0700628
Sage Weil4f6a7e52013-02-23 10:41:09 -0800629 pi->type = ceph_decode_8(p);
630 pi->size = ceph_decode_8(p);
631 pi->crush_ruleset = ceph_decode_8(p);
632 pi->object_hash = ceph_decode_8(p);
633
634 pi->pg_num = ceph_decode_32(p);
635 pi->pgp_num = ceph_decode_32(p);
636
637 *p += 4 + 4; /* skip lpg* */
638 *p += 4; /* skip last_change */
639 *p += 8 + 4; /* skip snap_seq, snap_epoch */
640
641 /* skip snaps */
642 num = ceph_decode_32(p);
643 while (num--) {
644 *p += 8; /* snapid key */
645 *p += 1 + 1; /* versions */
646 len = ceph_decode_32(p);
647 *p += len;
Sage Weil73a7e692010-08-02 11:00:55 -0700648 }
649
Ilya Dryomov17a13e402014-01-27 17:40:19 +0200650 /* skip removed_snaps */
Sage Weil4f6a7e52013-02-23 10:41:09 -0800651 num = ceph_decode_32(p);
652 *p += num * (8 + 8);
653
654 *p += 8; /* skip auid */
655 pi->flags = ceph_decode_64(p);
Ilya Dryomov17a13e402014-01-27 17:40:19 +0200656 *p += 4; /* skip crash_replay_interval */
657
658 if (ev >= 7)
Ilya Dryomov04812ac2016-04-28 16:07:23 +0200659 pi->min_size = ceph_decode_8(p);
660 else
661 pi->min_size = pi->size - pi->size / 2;
Ilya Dryomov17a13e402014-01-27 17:40:19 +0200662
663 if (ev >= 8)
664 *p += 8 + 8; /* skip quota_max_* */
665
666 if (ev >= 9) {
667 /* skip tiers */
668 num = ceph_decode_32(p);
669 *p += num * 8;
670
671 *p += 8; /* skip tier_of */
672 *p += 1; /* skip cache_mode */
673
674 pi->read_tier = ceph_decode_64(p);
675 pi->write_tier = ceph_decode_64(p);
676 } else {
677 pi->read_tier = -1;
678 pi->write_tier = -1;
679 }
Sage Weil4f6a7e52013-02-23 10:41:09 -0800680
Ilya Dryomov04812ac2016-04-28 16:07:23 +0200681 if (ev >= 10) {
682 /* skip properties */
683 num = ceph_decode_32(p);
684 while (num--) {
685 len = ceph_decode_32(p);
686 *p += len; /* key */
687 len = ceph_decode_32(p);
688 *p += len; /* val */
689 }
690 }
691
692 if (ev >= 11) {
693 /* skip hit_set_params */
694 *p += 1 + 1; /* versions */
695 len = ceph_decode_32(p);
696 *p += len;
697
698 *p += 4; /* skip hit_set_period */
699 *p += 4; /* skip hit_set_count */
700 }
701
702 if (ev >= 12)
703 *p += 4; /* skip stripe_width */
704
705 if (ev >= 13) {
706 *p += 8; /* skip target_max_bytes */
707 *p += 8; /* skip target_max_objects */
708 *p += 4; /* skip cache_target_dirty_ratio_micro */
709 *p += 4; /* skip cache_target_full_ratio_micro */
710 *p += 4; /* skip cache_min_flush_age */
711 *p += 4; /* skip cache_min_evict_age */
712 }
713
714 if (ev >= 14) {
715 /* skip erasure_code_profile */
716 len = ceph_decode_32(p);
717 *p += len;
718 }
719
Ilya Dryomov8e48cf02017-06-05 14:45:00 +0200720 /*
721 * last_force_op_resend_preluminous, will be overridden if the
722 * map was encoded with RESEND_ON_SPLIT
723 */
Ilya Dryomov04812ac2016-04-28 16:07:23 +0200724 if (ev >= 15)
725 pi->last_force_request_resend = ceph_decode_32(p);
726 else
727 pi->last_force_request_resend = 0;
728
Ilya Dryomov8e48cf02017-06-05 14:45:00 +0200729 if (ev >= 16)
730 *p += 4; /* skip min_read_recency_for_promote */
731
732 if (ev >= 17)
733 *p += 8; /* skip expected_num_objects */
734
735 if (ev >= 19)
736 *p += 4; /* skip cache_target_dirty_high_ratio_micro */
737
738 if (ev >= 20)
739 *p += 4; /* skip min_write_recency_for_promote */
740
741 if (ev >= 21)
742 *p += 1; /* skip use_gmt_hitset */
743
744 if (ev >= 22)
745 *p += 1; /* skip fast_read */
746
747 if (ev >= 23) {
748 *p += 4; /* skip hit_set_grade_decay_rate */
749 *p += 4; /* skip hit_set_search_last_n */
750 }
751
752 if (ev >= 24) {
753 /* skip opts */
754 *p += 1 + 1; /* versions */
755 len = ceph_decode_32(p);
756 *p += len;
757 }
758
759 if (ev >= 25)
760 pi->last_force_request_resend = ceph_decode_32(p);
761
Sage Weil4f6a7e52013-02-23 10:41:09 -0800762 /* ignore the rest */
763
764 *p = pool_end;
765 calc_pg_masks(pi);
Sage Weil73a7e692010-08-02 11:00:55 -0700766 return 0;
767
768bad:
769 return -EINVAL;
Sage Weilefd75762010-03-17 10:05:28 -0700770}
771
Ilya Dryomov0f70c7e2014-03-21 19:05:27 +0200772static int decode_pool_names(void **p, void *end, struct ceph_osdmap *map)
Sage Weil2844a762010-04-09 15:46:42 -0700773{
774 struct ceph_pg_pool_info *pi;
Sage Weil4f6a7e52013-02-23 10:41:09 -0800775 u32 num, len;
776 u64 pool;
Sage Weil2844a762010-04-09 15:46:42 -0700777
778 ceph_decode_32_safe(p, end, num, bad);
779 dout(" %d pool names\n", num);
780 while (num--) {
Sage Weil4f6a7e52013-02-23 10:41:09 -0800781 ceph_decode_64_safe(p, end, pool, bad);
Sage Weil2844a762010-04-09 15:46:42 -0700782 ceph_decode_32_safe(p, end, len, bad);
Sage Weil4f6a7e52013-02-23 10:41:09 -0800783 dout(" pool %llu len %d\n", pool, len);
Xi Wangad3b9042012-06-06 19:35:55 -0500784 ceph_decode_need(p, end, len, bad);
Sage Weil2844a762010-04-09 15:46:42 -0700785 pi = __lookup_pg_pool(&map->pg_pools, pool);
786 if (pi) {
Xi Wangad3b9042012-06-06 19:35:55 -0500787 char *name = kstrndup(*p, len, GFP_NOFS);
788
789 if (!name)
790 return -ENOMEM;
Sage Weil2844a762010-04-09 15:46:42 -0700791 kfree(pi->name);
Xi Wangad3b9042012-06-06 19:35:55 -0500792 pi->name = name;
793 dout(" name is %s\n", pi->name);
Sage Weil2844a762010-04-09 15:46:42 -0700794 }
795 *p += len;
796 }
797 return 0;
798
799bad:
800 return -EINVAL;
801}
802
803/*
804 * osd map
805 */
Ilya Dryomove5253a72016-04-28 16:07:25 +0200806struct ceph_osdmap *ceph_osdmap_alloc(void)
807{
808 struct ceph_osdmap *map;
809
810 map = kzalloc(sizeof(*map), GFP_NOIO);
811 if (!map)
812 return NULL;
813
814 map->pg_pools = RB_ROOT;
815 map->pool_max = -1;
816 map->pg_temp = RB_ROOT;
817 map->primary_temp = RB_ROOT;
Ilya Dryomov743efcf2017-01-31 15:55:06 +0100818 mutex_init(&map->crush_workspace_mutex);
Ilya Dryomove5253a72016-04-28 16:07:25 +0200819
820 return map;
821}
822
Sage Weil2844a762010-04-09 15:46:42 -0700823void ceph_osdmap_destroy(struct ceph_osdmap *map)
824{
825 dout("osdmap_destroy %p\n", map);
826 if (map->crush)
827 crush_destroy(map->crush);
828 while (!RB_EMPTY_ROOT(&map->pg_temp)) {
829 struct ceph_pg_mapping *pg =
830 rb_entry(rb_first(&map->pg_temp),
831 struct ceph_pg_mapping, node);
832 rb_erase(&pg->node, &map->pg_temp);
833 kfree(pg);
834 }
Ilya Dryomov9686f942014-03-21 19:05:29 +0200835 while (!RB_EMPTY_ROOT(&map->primary_temp)) {
836 struct ceph_pg_mapping *pg =
837 rb_entry(rb_first(&map->primary_temp),
838 struct ceph_pg_mapping, node);
839 rb_erase(&pg->node, &map->primary_temp);
840 kfree(pg);
841 }
Sage Weil2844a762010-04-09 15:46:42 -0700842 while (!RB_EMPTY_ROOT(&map->pg_pools)) {
843 struct ceph_pg_pool_info *pi =
844 rb_entry(rb_first(&map->pg_pools),
845 struct ceph_pg_pool_info, node);
846 __remove_pg_pool(&map->pg_pools, pi);
847 }
848 kfree(map->osd_state);
849 kfree(map->osd_weight);
850 kfree(map->osd_addr);
Ilya Dryomov2cfa34f2014-03-21 19:05:30 +0200851 kfree(map->osd_primary_affinity);
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100852 kfree(map->crush_workspace);
Sage Weil2844a762010-04-09 15:46:42 -0700853 kfree(map);
854}
855
856/*
Ilya Dryomov4d603512014-03-21 19:05:28 +0200857 * Adjust max_osd value, (re)allocate arrays.
858 *
859 * The new elements are properly initialized.
Sage Weil2844a762010-04-09 15:46:42 -0700860 */
861static int osdmap_set_max_osd(struct ceph_osdmap *map, int max)
862{
863 u8 *state;
Sage Weil2844a762010-04-09 15:46:42 -0700864 u32 *weight;
Ilya Dryomov4d603512014-03-21 19:05:28 +0200865 struct ceph_entity_addr *addr;
866 int i;
Sage Weil2844a762010-04-09 15:46:42 -0700867
Ilya Dryomov4d603512014-03-21 19:05:28 +0200868 state = krealloc(map->osd_state, max*sizeof(*state), GFP_NOFS);
Li RongQing589506f2014-09-07 18:10:51 +0800869 if (!state)
Sage Weil2844a762010-04-09 15:46:42 -0700870 return -ENOMEM;
Li RongQing589506f2014-09-07 18:10:51 +0800871 map->osd_state = state;
872
873 weight = krealloc(map->osd_weight, max*sizeof(*weight), GFP_NOFS);
874 if (!weight)
875 return -ENOMEM;
876 map->osd_weight = weight;
877
878 addr = krealloc(map->osd_addr, max*sizeof(*addr), GFP_NOFS);
879 if (!addr)
880 return -ENOMEM;
881 map->osd_addr = addr;
Sage Weil2844a762010-04-09 15:46:42 -0700882
Ilya Dryomov4d603512014-03-21 19:05:28 +0200883 for (i = map->max_osd; i < max; i++) {
Li RongQing589506f2014-09-07 18:10:51 +0800884 map->osd_state[i] = 0;
885 map->osd_weight[i] = CEPH_OSD_OUT;
886 memset(map->osd_addr + i, 0, sizeof(*map->osd_addr));
Sage Weil2844a762010-04-09 15:46:42 -0700887 }
888
Ilya Dryomov2cfa34f2014-03-21 19:05:30 +0200889 if (map->osd_primary_affinity) {
890 u32 *affinity;
891
892 affinity = krealloc(map->osd_primary_affinity,
893 max*sizeof(*affinity), GFP_NOFS);
894 if (!affinity)
895 return -ENOMEM;
Li RongQing589506f2014-09-07 18:10:51 +0800896 map->osd_primary_affinity = affinity;
Ilya Dryomov2cfa34f2014-03-21 19:05:30 +0200897
898 for (i = map->max_osd; i < max; i++)
Li RongQing589506f2014-09-07 18:10:51 +0800899 map->osd_primary_affinity[i] =
900 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
Ilya Dryomov2cfa34f2014-03-21 19:05:30 +0200901 }
902
Sage Weil2844a762010-04-09 15:46:42 -0700903 map->max_osd = max;
Ilya Dryomov4d603512014-03-21 19:05:28 +0200904
Sage Weil2844a762010-04-09 15:46:42 -0700905 return 0;
906}
907
Ilya Dryomov1b6a78b2017-01-31 15:55:06 +0100908static int osdmap_set_crush(struct ceph_osdmap *map, struct crush_map *crush)
909{
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100910 void *workspace;
Ilya Dryomov743efcf2017-01-31 15:55:06 +0100911 size_t work_size;
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100912
Ilya Dryomov1b6a78b2017-01-31 15:55:06 +0100913 if (IS_ERR(crush))
914 return PTR_ERR(crush);
915
Ilya Dryomov743efcf2017-01-31 15:55:06 +0100916 work_size = crush_work_size(crush, CEPH_PG_MAX_SIZE);
917 dout("%s work_size %zu bytes\n", __func__, work_size);
918 workspace = kmalloc(work_size, GFP_NOIO);
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100919 if (!workspace) {
920 crush_destroy(crush);
921 return -ENOMEM;
922 }
923 crush_init_workspace(crush, workspace);
924
Ilya Dryomov1b6a78b2017-01-31 15:55:06 +0100925 if (map->crush)
926 crush_destroy(map->crush);
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100927 kfree(map->crush_workspace);
Ilya Dryomov1b6a78b2017-01-31 15:55:06 +0100928 map->crush = crush;
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100929 map->crush_workspace = workspace;
Ilya Dryomov1b6a78b2017-01-31 15:55:06 +0100930 return 0;
931}
932
Ilya Dryomovec7af972014-03-21 19:05:29 +0200933#define OSDMAP_WRAPPER_COMPAT_VER 7
934#define OSDMAP_CLIENT_DATA_COMPAT_VER 1
935
936/*
937 * Return 0 or error. On success, *v is set to 0 for old (v6) osdmaps,
938 * to struct_v of the client_data section for new (v7 and above)
939 * osdmaps.
940 */
941static int get_osdmap_client_data_v(void **p, void *end,
942 const char *prefix, u8 *v)
943{
944 u8 struct_v;
945
946 ceph_decode_8_safe(p, end, struct_v, e_inval);
947 if (struct_v >= 7) {
948 u8 struct_compat;
949
950 ceph_decode_8_safe(p, end, struct_compat, e_inval);
951 if (struct_compat > OSDMAP_WRAPPER_COMPAT_VER) {
Joe Perchesb9a67892014-09-09 21:17:29 -0700952 pr_warn("got v %d cv %d > %d of %s ceph_osdmap\n",
953 struct_v, struct_compat,
954 OSDMAP_WRAPPER_COMPAT_VER, prefix);
Ilya Dryomovec7af972014-03-21 19:05:29 +0200955 return -EINVAL;
956 }
957 *p += 4; /* ignore wrapper struct_len */
958
959 ceph_decode_8_safe(p, end, struct_v, e_inval);
960 ceph_decode_8_safe(p, end, struct_compat, e_inval);
961 if (struct_compat > OSDMAP_CLIENT_DATA_COMPAT_VER) {
Joe Perchesb9a67892014-09-09 21:17:29 -0700962 pr_warn("got v %d cv %d > %d of %s ceph_osdmap client data\n",
963 struct_v, struct_compat,
964 OSDMAP_CLIENT_DATA_COMPAT_VER, prefix);
Ilya Dryomovec7af972014-03-21 19:05:29 +0200965 return -EINVAL;
966 }
967 *p += 4; /* ignore client data struct_len */
968 } else {
969 u16 version;
970
971 *p -= 1;
972 ceph_decode_16_safe(p, end, version, e_inval);
973 if (version < 6) {
Joe Perchesb9a67892014-09-09 21:17:29 -0700974 pr_warn("got v %d < 6 of %s ceph_osdmap\n",
975 version, prefix);
Ilya Dryomovec7af972014-03-21 19:05:29 +0200976 return -EINVAL;
977 }
978
979 /* old osdmap enconding */
980 struct_v = 0;
981 }
982
983 *v = struct_v;
984 return 0;
985
986e_inval:
987 return -EINVAL;
988}
989
Ilya Dryomov433fbdd2014-03-21 19:05:27 +0200990static int __decode_pools(void **p, void *end, struct ceph_osdmap *map,
991 bool incremental)
992{
993 u32 n;
994
995 ceph_decode_32_safe(p, end, n, e_inval);
996 while (n--) {
997 struct ceph_pg_pool_info *pi;
998 u64 pool;
999 int ret;
1000
1001 ceph_decode_64_safe(p, end, pool, e_inval);
1002
1003 pi = __lookup_pg_pool(&map->pg_pools, pool);
1004 if (!incremental || !pi) {
1005 pi = kzalloc(sizeof(*pi), GFP_NOFS);
1006 if (!pi)
1007 return -ENOMEM;
1008
1009 pi->id = pool;
1010
1011 ret = __insert_pg_pool(&map->pg_pools, pi);
1012 if (ret) {
1013 kfree(pi);
1014 return ret;
1015 }
1016 }
1017
1018 ret = decode_pool(p, end, pi);
1019 if (ret)
1020 return ret;
1021 }
1022
1023 return 0;
1024
1025e_inval:
1026 return -EINVAL;
1027}
1028
1029static int decode_pools(void **p, void *end, struct ceph_osdmap *map)
1030{
1031 return __decode_pools(p, end, map, false);
1032}
1033
1034static int decode_new_pools(void **p, void *end, struct ceph_osdmap *map)
1035{
1036 return __decode_pools(p, end, map, true);
1037}
1038
Ilya Dryomova303bb02017-06-21 17:27:17 +02001039typedef struct ceph_pg_mapping *(*decode_mapping_fn_t)(void **, void *, bool);
1040
1041static int decode_pg_mapping(void **p, void *end, struct rb_root *mapping_root,
1042 decode_mapping_fn_t fn, bool incremental)
Ilya Dryomov10db6342014-03-21 19:05:28 +02001043{
1044 u32 n;
1045
Ilya Dryomova303bb02017-06-21 17:27:17 +02001046 WARN_ON(!incremental && !fn);
1047
Ilya Dryomov10db6342014-03-21 19:05:28 +02001048 ceph_decode_32_safe(p, end, n, e_inval);
1049 while (n--) {
Ilya Dryomova303bb02017-06-21 17:27:17 +02001050 struct ceph_pg_mapping *pg;
Ilya Dryomov10db6342014-03-21 19:05:28 +02001051 struct ceph_pg pgid;
Ilya Dryomov10db6342014-03-21 19:05:28 +02001052 int ret;
1053
1054 ret = ceph_decode_pgid(p, end, &pgid);
1055 if (ret)
1056 return ret;
1057
Ilya Dryomova303bb02017-06-21 17:27:17 +02001058 ret = __remove_pg_mapping(mapping_root, &pgid);
1059 WARN_ON(!incremental && ret != -ENOENT);
Ilya Dryomov10db6342014-03-21 19:05:28 +02001060
Ilya Dryomova303bb02017-06-21 17:27:17 +02001061 if (fn) {
1062 pg = fn(p, end, incremental);
1063 if (IS_ERR(pg))
1064 return PTR_ERR(pg);
Ilya Dryomov10db6342014-03-21 19:05:28 +02001065
Ilya Dryomova303bb02017-06-21 17:27:17 +02001066 if (pg) {
1067 pg->pgid = pgid; /* struct */
1068 __insert_pg_mapping(pg, mapping_root);
Ilya Dryomov10db6342014-03-21 19:05:28 +02001069 }
1070 }
1071 }
1072
1073 return 0;
1074
1075e_inval:
1076 return -EINVAL;
1077}
1078
Ilya Dryomova303bb02017-06-21 17:27:17 +02001079static struct ceph_pg_mapping *__decode_pg_temp(void **p, void *end,
1080 bool incremental)
1081{
1082 struct ceph_pg_mapping *pg;
1083 u32 len, i;
1084
1085 ceph_decode_32_safe(p, end, len, e_inval);
1086 if (len == 0 && incremental)
1087 return NULL; /* new_pg_temp: [] to remove */
1088 if (len > (SIZE_MAX - sizeof(*pg)) / sizeof(u32))
1089 return ERR_PTR(-EINVAL);
1090
1091 ceph_decode_need(p, end, len * sizeof(u32), e_inval);
1092 pg = alloc_pg_mapping(len * sizeof(u32));
1093 if (!pg)
1094 return ERR_PTR(-ENOMEM);
1095
1096 pg->pg_temp.len = len;
1097 for (i = 0; i < len; i++)
1098 pg->pg_temp.osds[i] = ceph_decode_32(p);
1099
1100 return pg;
1101
1102e_inval:
1103 return ERR_PTR(-EINVAL);
1104}
1105
Ilya Dryomov10db6342014-03-21 19:05:28 +02001106static int decode_pg_temp(void **p, void *end, struct ceph_osdmap *map)
1107{
Ilya Dryomova303bb02017-06-21 17:27:17 +02001108 return decode_pg_mapping(p, end, &map->pg_temp, __decode_pg_temp,
1109 false);
Ilya Dryomov10db6342014-03-21 19:05:28 +02001110}
1111
1112static int decode_new_pg_temp(void **p, void *end, struct ceph_osdmap *map)
1113{
Ilya Dryomova303bb02017-06-21 17:27:17 +02001114 return decode_pg_mapping(p, end, &map->pg_temp, __decode_pg_temp,
1115 true);
Ilya Dryomov10db6342014-03-21 19:05:28 +02001116}
1117
Ilya Dryomova303bb02017-06-21 17:27:17 +02001118static struct ceph_pg_mapping *__decode_primary_temp(void **p, void *end,
1119 bool incremental)
Ilya Dryomovd286de72014-03-21 19:05:30 +02001120{
Ilya Dryomova303bb02017-06-21 17:27:17 +02001121 struct ceph_pg_mapping *pg;
1122 u32 osd;
Ilya Dryomovd286de72014-03-21 19:05:30 +02001123
Ilya Dryomova303bb02017-06-21 17:27:17 +02001124 ceph_decode_32_safe(p, end, osd, e_inval);
1125 if (osd == (u32)-1 && incremental)
1126 return NULL; /* new_primary_temp: -1 to remove */
Ilya Dryomovd286de72014-03-21 19:05:30 +02001127
Ilya Dryomova303bb02017-06-21 17:27:17 +02001128 pg = alloc_pg_mapping(0);
1129 if (!pg)
1130 return ERR_PTR(-ENOMEM);
Ilya Dryomovd286de72014-03-21 19:05:30 +02001131
Ilya Dryomova303bb02017-06-21 17:27:17 +02001132 pg->primary_temp.osd = osd;
1133 return pg;
Ilya Dryomovd286de72014-03-21 19:05:30 +02001134
1135e_inval:
Ilya Dryomova303bb02017-06-21 17:27:17 +02001136 return ERR_PTR(-EINVAL);
Ilya Dryomovd286de72014-03-21 19:05:30 +02001137}
1138
1139static int decode_primary_temp(void **p, void *end, struct ceph_osdmap *map)
1140{
Ilya Dryomova303bb02017-06-21 17:27:17 +02001141 return decode_pg_mapping(p, end, &map->primary_temp,
1142 __decode_primary_temp, false);
Ilya Dryomovd286de72014-03-21 19:05:30 +02001143}
1144
1145static int decode_new_primary_temp(void **p, void *end,
1146 struct ceph_osdmap *map)
1147{
Ilya Dryomova303bb02017-06-21 17:27:17 +02001148 return decode_pg_mapping(p, end, &map->primary_temp,
1149 __decode_primary_temp, true);
Ilya Dryomovd286de72014-03-21 19:05:30 +02001150}
1151
Ilya Dryomov2cfa34f2014-03-21 19:05:30 +02001152u32 ceph_get_primary_affinity(struct ceph_osdmap *map, int osd)
1153{
1154 BUG_ON(osd >= map->max_osd);
1155
1156 if (!map->osd_primary_affinity)
1157 return CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
1158
1159 return map->osd_primary_affinity[osd];
1160}
1161
1162static int set_primary_affinity(struct ceph_osdmap *map, int osd, u32 aff)
1163{
1164 BUG_ON(osd >= map->max_osd);
1165
1166 if (!map->osd_primary_affinity) {
1167 int i;
1168
1169 map->osd_primary_affinity = kmalloc(map->max_osd*sizeof(u32),
1170 GFP_NOFS);
1171 if (!map->osd_primary_affinity)
1172 return -ENOMEM;
1173
1174 for (i = 0; i < map->max_osd; i++)
1175 map->osd_primary_affinity[i] =
1176 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
1177 }
1178
1179 map->osd_primary_affinity[osd] = aff;
1180
1181 return 0;
1182}
1183
Ilya Dryomov63a69932014-03-21 19:05:30 +02001184static int decode_primary_affinity(void **p, void *end,
1185 struct ceph_osdmap *map)
1186{
1187 u32 len, i;
1188
1189 ceph_decode_32_safe(p, end, len, e_inval);
1190 if (len == 0) {
1191 kfree(map->osd_primary_affinity);
1192 map->osd_primary_affinity = NULL;
1193 return 0;
1194 }
1195 if (len != map->max_osd)
1196 goto e_inval;
1197
1198 ceph_decode_need(p, end, map->max_osd*sizeof(u32), e_inval);
1199
1200 for (i = 0; i < map->max_osd; i++) {
1201 int ret;
1202
1203 ret = set_primary_affinity(map, i, ceph_decode_32(p));
1204 if (ret)
1205 return ret;
1206 }
1207
1208 return 0;
1209
1210e_inval:
1211 return -EINVAL;
1212}
1213
1214static int decode_new_primary_affinity(void **p, void *end,
1215 struct ceph_osdmap *map)
1216{
1217 u32 n;
1218
1219 ceph_decode_32_safe(p, end, n, e_inval);
1220 while (n--) {
1221 u32 osd, aff;
1222 int ret;
1223
1224 ceph_decode_32_safe(p, end, osd, e_inval);
1225 ceph_decode_32_safe(p, end, aff, e_inval);
1226
1227 ret = set_primary_affinity(map, osd, aff);
1228 if (ret)
1229 return ret;
Ilya Dryomovf31da0f2014-04-02 20:34:04 +04001230
1231 pr_info("osd%d primary-affinity 0x%x\n", osd, aff);
Ilya Dryomov63a69932014-03-21 19:05:30 +02001232 }
1233
1234 return 0;
1235
1236e_inval:
1237 return -EINVAL;
1238}
1239
Sage Weil4fc51be2010-02-16 15:55:03 -08001240/*
Sage Weilf24e9982009-10-06 11:31:10 -07001241 * decode a full map.
1242 */
Ilya Dryomova2505d62014-03-13 16:36:13 +02001243static int osdmap_decode(void **p, void *end, struct ceph_osdmap *map)
Sage Weilf24e9982009-10-06 11:31:10 -07001244{
Ilya Dryomovec7af972014-03-21 19:05:29 +02001245 u8 struct_v;
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001246 u32 epoch = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001247 void *start = *p;
Ilya Dryomov39770582014-03-13 16:36:14 +02001248 u32 max;
1249 u32 len, i;
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001250 int err;
Sage Weilf24e9982009-10-06 11:31:10 -07001251
Ilya Dryomova2505d62014-03-13 16:36:13 +02001252 dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p));
Sage Weilf24e9982009-10-06 11:31:10 -07001253
Ilya Dryomovec7af972014-03-21 19:05:29 +02001254 err = get_osdmap_client_data_v(p, end, "full", &struct_v);
1255 if (err)
1256 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001257
Ilya Dryomov53bbaba2014-03-13 16:36:16 +02001258 /* fsid, epoch, created, modified */
1259 ceph_decode_need(p, end, sizeof(map->fsid) + sizeof(u32) +
1260 sizeof(map->created) + sizeof(map->modified), e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001261 ceph_decode_copy(p, &map->fsid, sizeof(map->fsid));
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001262 epoch = map->epoch = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -07001263 ceph_decode_copy(p, &map->created, sizeof(map->created));
1264 ceph_decode_copy(p, &map->modified, sizeof(map->modified));
1265
Ilya Dryomov433fbdd2014-03-21 19:05:27 +02001266 /* pools */
1267 err = decode_pools(p, end, map);
1268 if (err)
1269 goto bad;
Sage Weil2844a762010-04-09 15:46:42 -07001270
Ilya Dryomov0f70c7e2014-03-21 19:05:27 +02001271 /* pool_name */
1272 err = decode_pool_names(p, end, map);
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001273 if (err)
Sage Weil4f6a7e52013-02-23 10:41:09 -08001274 goto bad;
Sage Weil2844a762010-04-09 15:46:42 -07001275
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001276 ceph_decode_32_safe(p, end, map->pool_max, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001277
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001278 ceph_decode_32_safe(p, end, map->flags, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001279
Ilya Dryomov39770582014-03-13 16:36:14 +02001280 /* max_osd */
1281 ceph_decode_32_safe(p, end, max, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001282
1283 /* (re)alloc osd arrays */
1284 err = osdmap_set_max_osd(map, max);
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001285 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001286 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001287
Ilya Dryomov2d88b2e2014-03-13 16:36:14 +02001288 /* osd_state, osd_weight, osd_addrs->client_addr */
Sage Weilf24e9982009-10-06 11:31:10 -07001289 ceph_decode_need(p, end, 3*sizeof(u32) +
1290 map->max_osd*(1 + sizeof(*map->osd_weight) +
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001291 sizeof(*map->osd_addr)), e_inval);
1292
Ilya Dryomov2d88b2e2014-03-13 16:36:14 +02001293 if (ceph_decode_32(p) != map->max_osd)
1294 goto e_inval;
1295
Sage Weilf24e9982009-10-06 11:31:10 -07001296 ceph_decode_copy(p, map->osd_state, map->max_osd);
1297
Ilya Dryomov2d88b2e2014-03-13 16:36:14 +02001298 if (ceph_decode_32(p) != map->max_osd)
1299 goto e_inval;
1300
Sage Weilf24e9982009-10-06 11:31:10 -07001301 for (i = 0; i < map->max_osd; i++)
Sage Weilc89136e2009-10-14 09:59:09 -07001302 map->osd_weight[i] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -07001303
Ilya Dryomov2d88b2e2014-03-13 16:36:14 +02001304 if (ceph_decode_32(p) != map->max_osd)
1305 goto e_inval;
1306
Sage Weilf24e9982009-10-06 11:31:10 -07001307 ceph_decode_copy(p, map->osd_addr, map->max_osd*sizeof(*map->osd_addr));
Sage Weil63f2d212009-11-03 15:17:56 -08001308 for (i = 0; i < map->max_osd; i++)
1309 ceph_decode_addr(&map->osd_addr[i]);
Sage Weilf24e9982009-10-06 11:31:10 -07001310
1311 /* pg_temp */
Ilya Dryomov10db6342014-03-21 19:05:28 +02001312 err = decode_pg_temp(p, end, map);
1313 if (err)
1314 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001315
Ilya Dryomovd286de72014-03-21 19:05:30 +02001316 /* primary_temp */
1317 if (struct_v >= 1) {
1318 err = decode_primary_temp(p, end, map);
1319 if (err)
1320 goto bad;
1321 }
1322
Ilya Dryomov63a69932014-03-21 19:05:30 +02001323 /* primary_affinity */
1324 if (struct_v >= 2) {
1325 err = decode_primary_affinity(p, end, map);
1326 if (err)
1327 goto bad;
1328 } else {
1329 /* XXX can this happen? */
1330 kfree(map->osd_primary_affinity);
1331 map->osd_primary_affinity = NULL;
1332 }
1333
Sage Weilf24e9982009-10-06 11:31:10 -07001334 /* crush */
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001335 ceph_decode_32_safe(p, end, len, e_inval);
Ilya Dryomov1b6a78b2017-01-31 15:55:06 +01001336 err = osdmap_set_crush(map, crush_decode(*p, min(*p + len, end)));
1337 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001338 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001339
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001340 /* ignore the rest */
Sage Weilf24e9982009-10-06 11:31:10 -07001341 *p = end;
1342
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001343 dout("full osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd);
Ilya Dryomova2505d62014-03-13 16:36:13 +02001344 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001345
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001346e_inval:
1347 err = -EINVAL;
Sage Weilf24e9982009-10-06 11:31:10 -07001348bad:
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001349 pr_err("corrupt full osdmap (%d) epoch %d off %d (%p of %p-%p)\n",
1350 err, epoch, (int)(*p - start), *p, start, end);
1351 print_hex_dump(KERN_DEBUG, "osdmap: ",
1352 DUMP_PREFIX_OFFSET, 16, 1,
1353 start, end - start, true);
Ilya Dryomova2505d62014-03-13 16:36:13 +02001354 return err;
1355}
1356
1357/*
1358 * Allocate and decode a full map.
1359 */
1360struct ceph_osdmap *ceph_osdmap_decode(void **p, void *end)
1361{
1362 struct ceph_osdmap *map;
1363 int ret;
1364
Ilya Dryomove5253a72016-04-28 16:07:25 +02001365 map = ceph_osdmap_alloc();
Ilya Dryomova2505d62014-03-13 16:36:13 +02001366 if (!map)
1367 return ERR_PTR(-ENOMEM);
1368
Ilya Dryomova2505d62014-03-13 16:36:13 +02001369 ret = osdmap_decode(p, end, map);
1370 if (ret) {
1371 ceph_osdmap_destroy(map);
1372 return ERR_PTR(ret);
1373 }
1374
1375 return map;
Sage Weilf24e9982009-10-06 11:31:10 -07001376}
1377
1378/*
Ilya Dryomov930c5322016-07-19 03:50:28 +02001379 * Encoding order is (new_up_client, new_state, new_weight). Need to
1380 * apply in the (new_weight, new_state, new_up_client) order, because
1381 * an incremental map may look like e.g.
1382 *
1383 * new_up_client: { osd=6, addr=... } # set osd_state and addr
1384 * new_state: { osd=6, xorstate=EXISTS } # clear osd_state
1385 */
1386static int decode_new_up_state_weight(void **p, void *end,
1387 struct ceph_osdmap *map)
1388{
1389 void *new_up_client;
1390 void *new_state;
1391 void *new_weight_end;
1392 u32 len;
1393
1394 new_up_client = *p;
1395 ceph_decode_32_safe(p, end, len, e_inval);
1396 len *= sizeof(u32) + sizeof(struct ceph_entity_addr);
1397 ceph_decode_need(p, end, len, e_inval);
1398 *p += len;
1399
1400 new_state = *p;
1401 ceph_decode_32_safe(p, end, len, e_inval);
1402 len *= sizeof(u32) + sizeof(u8);
1403 ceph_decode_need(p, end, len, e_inval);
1404 *p += len;
1405
1406 /* new_weight */
1407 ceph_decode_32_safe(p, end, len, e_inval);
1408 while (len--) {
1409 s32 osd;
1410 u32 w;
1411
1412 ceph_decode_need(p, end, 2*sizeof(u32), e_inval);
1413 osd = ceph_decode_32(p);
1414 w = ceph_decode_32(p);
1415 BUG_ON(osd >= map->max_osd);
1416 pr_info("osd%d weight 0x%x %s\n", osd, w,
1417 w == CEPH_OSD_IN ? "(in)" :
1418 (w == CEPH_OSD_OUT ? "(out)" : ""));
1419 map->osd_weight[osd] = w;
1420
1421 /*
1422 * If we are marking in, set the EXISTS, and clear the
1423 * AUTOOUT and NEW bits.
1424 */
1425 if (w) {
1426 map->osd_state[osd] |= CEPH_OSD_EXISTS;
1427 map->osd_state[osd] &= ~(CEPH_OSD_AUTOOUT |
1428 CEPH_OSD_NEW);
1429 }
1430 }
1431 new_weight_end = *p;
1432
1433 /* new_state (up/down) */
1434 *p = new_state;
1435 len = ceph_decode_32(p);
1436 while (len--) {
1437 s32 osd;
1438 u8 xorstate;
1439 int ret;
1440
1441 osd = ceph_decode_32(p);
1442 xorstate = ceph_decode_8(p);
1443 if (xorstate == 0)
1444 xorstate = CEPH_OSD_UP;
1445 BUG_ON(osd >= map->max_osd);
1446 if ((map->osd_state[osd] & CEPH_OSD_UP) &&
1447 (xorstate & CEPH_OSD_UP))
1448 pr_info("osd%d down\n", osd);
1449 if ((map->osd_state[osd] & CEPH_OSD_EXISTS) &&
1450 (xorstate & CEPH_OSD_EXISTS)) {
1451 pr_info("osd%d does not exist\n", osd);
Ilya Dryomov930c5322016-07-19 03:50:28 +02001452 ret = set_primary_affinity(map, osd,
1453 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY);
1454 if (ret)
1455 return ret;
1456 memset(map->osd_addr + osd, 0, sizeof(*map->osd_addr));
1457 map->osd_state[osd] = 0;
1458 } else {
1459 map->osd_state[osd] ^= xorstate;
1460 }
1461 }
1462
1463 /* new_up_client */
1464 *p = new_up_client;
1465 len = ceph_decode_32(p);
1466 while (len--) {
1467 s32 osd;
1468 struct ceph_entity_addr addr;
1469
1470 osd = ceph_decode_32(p);
1471 ceph_decode_copy(p, &addr, sizeof(addr));
1472 ceph_decode_addr(&addr);
1473 BUG_ON(osd >= map->max_osd);
1474 pr_info("osd%d up\n", osd);
1475 map->osd_state[osd] |= CEPH_OSD_EXISTS | CEPH_OSD_UP;
1476 map->osd_addr[osd] = addr;
1477 }
1478
1479 *p = new_weight_end;
1480 return 0;
1481
1482e_inval:
1483 return -EINVAL;
1484}
1485
1486/*
Sage Weilf24e9982009-10-06 11:31:10 -07001487 * decode and apply an incremental map update.
1488 */
1489struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end,
Ilya Dryomov0c0a8de2016-04-28 16:07:21 +02001490 struct ceph_osdmap *map)
Sage Weilf24e9982009-10-06 11:31:10 -07001491{
Sage Weilf24e9982009-10-06 11:31:10 -07001492 struct ceph_fsid fsid;
1493 u32 epoch = 0;
1494 struct ceph_timespec modified;
Sage Weil4f6a7e52013-02-23 10:41:09 -08001495 s32 len;
1496 u64 pool;
1497 __s64 new_pool_max;
1498 __s32 new_flags, max;
Sage Weilf24e9982009-10-06 11:31:10 -07001499 void *start = *p;
Ilya Dryomov86f17422014-03-13 16:36:15 +02001500 int err;
Ilya Dryomovec7af972014-03-21 19:05:29 +02001501 u8 struct_v;
Sage Weilf24e9982009-10-06 11:31:10 -07001502
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001503 dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p));
1504
Ilya Dryomovec7af972014-03-21 19:05:29 +02001505 err = get_osdmap_client_data_v(p, end, "inc", &struct_v);
1506 if (err)
1507 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001508
Ilya Dryomov53bbaba2014-03-13 16:36:16 +02001509 /* fsid, epoch, modified, new_pool_max, new_flags */
1510 ceph_decode_need(p, end, sizeof(fsid) + sizeof(u32) + sizeof(modified) +
1511 sizeof(u64) + sizeof(u32), e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001512 ceph_decode_copy(p, &fsid, sizeof(fsid));
Sage Weilc89136e2009-10-14 09:59:09 -07001513 epoch = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -07001514 BUG_ON(epoch != map->epoch+1);
1515 ceph_decode_copy(p, &modified, sizeof(modified));
Sage Weil4f6a7e52013-02-23 10:41:09 -08001516 new_pool_max = ceph_decode_64(p);
Sage Weilc89136e2009-10-14 09:59:09 -07001517 new_flags = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -07001518
1519 /* full map? */
Ilya Dryomov86f17422014-03-13 16:36:15 +02001520 ceph_decode_32_safe(p, end, len, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001521 if (len > 0) {
1522 dout("apply_incremental full map len %d, %p to %p\n",
1523 len, *p, end);
Ilya Dryomova2505d62014-03-13 16:36:13 +02001524 return ceph_osdmap_decode(p, min(*p+len, end));
Sage Weilf24e9982009-10-06 11:31:10 -07001525 }
1526
1527 /* new crush? */
Ilya Dryomov86f17422014-03-13 16:36:15 +02001528 ceph_decode_32_safe(p, end, len, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001529 if (len > 0) {
Ilya Dryomov1b6a78b2017-01-31 15:55:06 +01001530 err = osdmap_set_crush(map,
1531 crush_decode(*p, min(*p + len, end)));
1532 if (err)
Ilya Dryomov86f17422014-03-13 16:36:15 +02001533 goto bad;
Sage Weilcebc5be2010-06-17 10:22:48 -07001534 *p += len;
Sage Weilf24e9982009-10-06 11:31:10 -07001535 }
1536
1537 /* new flags? */
1538 if (new_flags >= 0)
1539 map->flags = new_flags;
Sage Weil4fc51be2010-02-16 15:55:03 -08001540 if (new_pool_max >= 0)
1541 map->pool_max = new_pool_max;
Sage Weilf24e9982009-10-06 11:31:10 -07001542
Sage Weilf24e9982009-10-06 11:31:10 -07001543 /* new max? */
Ilya Dryomov53bbaba2014-03-13 16:36:16 +02001544 ceph_decode_32_safe(p, end, max, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001545 if (max >= 0) {
1546 err = osdmap_set_max_osd(map, max);
Ilya Dryomov86f17422014-03-13 16:36:15 +02001547 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001548 goto bad;
1549 }
1550
1551 map->epoch++;
Sage Weil31456662011-05-12 15:18:43 -07001552 map->modified = modified;
Sage Weilf24e9982009-10-06 11:31:10 -07001553
Ilya Dryomov433fbdd2014-03-21 19:05:27 +02001554 /* new_pools */
1555 err = decode_new_pools(p, end, map);
1556 if (err)
1557 goto bad;
Ilya Dryomov9464d002014-03-13 16:36:16 +02001558
Ilya Dryomov0f70c7e2014-03-21 19:05:27 +02001559 /* new_pool_names */
1560 err = decode_pool_names(p, end, map);
Ilya Dryomov9464d002014-03-13 16:36:16 +02001561 if (err)
1562 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001563
Sage Weil4fc51be2010-02-16 15:55:03 -08001564 /* old_pool */
Ilya Dryomov86f17422014-03-13 16:36:15 +02001565 ceph_decode_32_safe(p, end, len, e_inval);
Sage Weil4fc51be2010-02-16 15:55:03 -08001566 while (len--) {
1567 struct ceph_pg_pool_info *pi;
1568
Ilya Dryomov86f17422014-03-13 16:36:15 +02001569 ceph_decode_64_safe(p, end, pool, e_inval);
Sage Weil4fc51be2010-02-16 15:55:03 -08001570 pi = __lookup_pg_pool(&map->pg_pools, pool);
Sage Weil2844a762010-04-09 15:46:42 -07001571 if (pi)
1572 __remove_pg_pool(&map->pg_pools, pi);
Sage Weil4fc51be2010-02-16 15:55:03 -08001573 }
Sage Weilf24e9982009-10-06 11:31:10 -07001574
Ilya Dryomov930c5322016-07-19 03:50:28 +02001575 /* new_up_client, new_state, new_weight */
1576 err = decode_new_up_state_weight(p, end, map);
1577 if (err)
1578 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001579
1580 /* new_pg_temp */
Ilya Dryomov10db6342014-03-21 19:05:28 +02001581 err = decode_new_pg_temp(p, end, map);
1582 if (err)
1583 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001584
Ilya Dryomovd286de72014-03-21 19:05:30 +02001585 /* new_primary_temp */
1586 if (struct_v >= 1) {
1587 err = decode_new_primary_temp(p, end, map);
1588 if (err)
1589 goto bad;
1590 }
1591
Ilya Dryomov63a69932014-03-21 19:05:30 +02001592 /* new_primary_affinity */
1593 if (struct_v >= 2) {
1594 err = decode_new_primary_affinity(p, end, map);
1595 if (err)
1596 goto bad;
1597 }
1598
Sage Weilf24e9982009-10-06 11:31:10 -07001599 /* ignore the rest */
1600 *p = end;
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001601
1602 dout("inc osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001603 return map;
1604
Ilya Dryomov86f17422014-03-13 16:36:15 +02001605e_inval:
1606 err = -EINVAL;
Sage Weilf24e9982009-10-06 11:31:10 -07001607bad:
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001608 pr_err("corrupt inc osdmap (%d) epoch %d off %d (%p of %p-%p)\n",
1609 err, epoch, (int)(*p - start), *p, start, end);
Sage Weil9ec7cab2009-12-14 15:13:47 -08001610 print_hex_dump(KERN_DEBUG, "osdmap: ",
1611 DUMP_PREFIX_OFFSET, 16, 1,
1612 start, end - start, true);
Sage Weilf24e9982009-10-06 11:31:10 -07001613 return ERR_PTR(err);
1614}
1615
Yan, Zheng30c156d2016-02-14 11:24:31 +08001616void ceph_oloc_copy(struct ceph_object_locator *dest,
1617 const struct ceph_object_locator *src)
1618{
Ilya Dryomovca35ffe2017-06-05 14:44:59 +02001619 ceph_oloc_destroy(dest);
Yan, Zheng30c156d2016-02-14 11:24:31 +08001620
1621 dest->pool = src->pool;
1622 if (src->pool_ns)
1623 dest->pool_ns = ceph_get_string(src->pool_ns);
Ilya Dryomovca35ffe2017-06-05 14:44:59 +02001624 else
1625 dest->pool_ns = NULL;
Yan, Zheng30c156d2016-02-14 11:24:31 +08001626}
1627EXPORT_SYMBOL(ceph_oloc_copy);
1628
1629void ceph_oloc_destroy(struct ceph_object_locator *oloc)
1630{
1631 ceph_put_string(oloc->pool_ns);
1632}
1633EXPORT_SYMBOL(ceph_oloc_destroy);
1634
Ilya Dryomovd30291b2016-04-29 19:54:20 +02001635void ceph_oid_copy(struct ceph_object_id *dest,
1636 const struct ceph_object_id *src)
1637{
Ilya Dryomovca35ffe2017-06-05 14:44:59 +02001638 ceph_oid_destroy(dest);
Sage Weilf24e9982009-10-06 11:31:10 -07001639
Ilya Dryomovd30291b2016-04-29 19:54:20 +02001640 if (src->name != src->inline_name) {
1641 /* very rare, see ceph_object_id definition */
1642 dest->name = kmalloc(src->name_len + 1,
1643 GFP_NOIO | __GFP_NOFAIL);
Ilya Dryomovca35ffe2017-06-05 14:44:59 +02001644 } else {
1645 dest->name = dest->inline_name;
Ilya Dryomovd30291b2016-04-29 19:54:20 +02001646 }
Ilya Dryomovd30291b2016-04-29 19:54:20 +02001647 memcpy(dest->name, src->name, src->name_len + 1);
1648 dest->name_len = src->name_len;
1649}
1650EXPORT_SYMBOL(ceph_oid_copy);
1651
1652static __printf(2, 0)
1653int oid_printf_vargs(struct ceph_object_id *oid, const char *fmt, va_list ap)
1654{
1655 int len;
1656
1657 WARN_ON(!ceph_oid_empty(oid));
1658
1659 len = vsnprintf(oid->inline_name, sizeof(oid->inline_name), fmt, ap);
1660 if (len >= sizeof(oid->inline_name))
1661 return len;
1662
1663 oid->name_len = len;
1664 return 0;
1665}
1666
1667/*
1668 * If oid doesn't fit into inline buffer, BUG.
1669 */
1670void ceph_oid_printf(struct ceph_object_id *oid, const char *fmt, ...)
1671{
1672 va_list ap;
1673
1674 va_start(ap, fmt);
1675 BUG_ON(oid_printf_vargs(oid, fmt, ap));
1676 va_end(ap);
1677}
1678EXPORT_SYMBOL(ceph_oid_printf);
1679
1680static __printf(3, 0)
1681int oid_aprintf_vargs(struct ceph_object_id *oid, gfp_t gfp,
1682 const char *fmt, va_list ap)
1683{
1684 va_list aq;
1685 int len;
1686
1687 va_copy(aq, ap);
1688 len = oid_printf_vargs(oid, fmt, aq);
1689 va_end(aq);
1690
1691 if (len) {
1692 char *external_name;
1693
1694 external_name = kmalloc(len + 1, gfp);
1695 if (!external_name)
1696 return -ENOMEM;
1697
1698 oid->name = external_name;
1699 WARN_ON(vsnprintf(oid->name, len + 1, fmt, ap) != len);
1700 oid->name_len = len;
1701 }
1702
1703 return 0;
1704}
1705
1706/*
1707 * If oid doesn't fit into inline buffer, allocate.
1708 */
1709int ceph_oid_aprintf(struct ceph_object_id *oid, gfp_t gfp,
1710 const char *fmt, ...)
1711{
1712 va_list ap;
1713 int ret;
1714
1715 va_start(ap, fmt);
1716 ret = oid_aprintf_vargs(oid, gfp, fmt, ap);
1717 va_end(ap);
1718
1719 return ret;
1720}
1721EXPORT_SYMBOL(ceph_oid_aprintf);
1722
1723void ceph_oid_destroy(struct ceph_object_id *oid)
1724{
1725 if (oid->name != oid->inline_name)
1726 kfree(oid->name);
1727}
1728EXPORT_SYMBOL(ceph_oid_destroy);
Sage Weilf24e9982009-10-06 11:31:10 -07001729
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001730/*
1731 * osds only
1732 */
1733static bool __osds_equal(const struct ceph_osds *lhs,
1734 const struct ceph_osds *rhs)
1735{
1736 if (lhs->size == rhs->size &&
1737 !memcmp(lhs->osds, rhs->osds, rhs->size * sizeof(rhs->osds[0])))
1738 return true;
1739
1740 return false;
1741}
1742
1743/*
1744 * osds + primary
1745 */
1746static bool osds_equal(const struct ceph_osds *lhs,
1747 const struct ceph_osds *rhs)
1748{
1749 if (__osds_equal(lhs, rhs) &&
1750 lhs->primary == rhs->primary)
1751 return true;
1752
1753 return false;
1754}
1755
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02001756static bool osds_valid(const struct ceph_osds *set)
1757{
1758 /* non-empty set */
1759 if (set->size > 0 && set->primary >= 0)
1760 return true;
1761
1762 /* empty can_shift_osds set */
1763 if (!set->size && set->primary == -1)
1764 return true;
1765
1766 /* empty !can_shift_osds set - all NONE */
1767 if (set->size > 0 && set->primary == -1) {
1768 int i;
1769
1770 for (i = 0; i < set->size; i++) {
1771 if (set->osds[i] != CRUSH_ITEM_NONE)
1772 break;
1773 }
1774 if (i == set->size)
1775 return true;
1776 }
1777
1778 return false;
1779}
1780
1781void ceph_osds_copy(struct ceph_osds *dest, const struct ceph_osds *src)
1782{
1783 memcpy(dest->osds, src->osds, src->size * sizeof(src->osds[0]));
1784 dest->size = src->size;
1785 dest->primary = src->primary;
1786}
1787
Ilya Dryomov7de030d2017-06-15 16:30:54 +02001788bool ceph_pg_is_split(const struct ceph_pg *pgid, u32 old_pg_num,
1789 u32 new_pg_num)
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001790{
1791 int old_bits = calc_bits_of(old_pg_num);
1792 int old_mask = (1 << old_bits) - 1;
1793 int n;
1794
1795 WARN_ON(pgid->seed >= old_pg_num);
1796 if (new_pg_num <= old_pg_num)
1797 return false;
1798
1799 for (n = 1; ; n++) {
1800 int next_bit = n << (old_bits - 1);
1801 u32 s = next_bit | pgid->seed;
1802
1803 if (s < old_pg_num || s == pgid->seed)
1804 continue;
1805 if (s >= new_pg_num)
1806 break;
1807
1808 s = ceph_stable_mod(s, old_pg_num, old_mask);
1809 if (s == pgid->seed)
1810 return true;
1811 }
1812
1813 return false;
1814}
1815
1816bool ceph_is_new_interval(const struct ceph_osds *old_acting,
1817 const struct ceph_osds *new_acting,
1818 const struct ceph_osds *old_up,
1819 const struct ceph_osds *new_up,
1820 int old_size,
1821 int new_size,
1822 int old_min_size,
1823 int new_min_size,
1824 u32 old_pg_num,
1825 u32 new_pg_num,
1826 bool old_sort_bitwise,
1827 bool new_sort_bitwise,
1828 const struct ceph_pg *pgid)
1829{
1830 return !osds_equal(old_acting, new_acting) ||
1831 !osds_equal(old_up, new_up) ||
1832 old_size != new_size ||
1833 old_min_size != new_min_size ||
Ilya Dryomov7de030d2017-06-15 16:30:54 +02001834 ceph_pg_is_split(pgid, old_pg_num, new_pg_num) ||
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001835 old_sort_bitwise != new_sort_bitwise;
1836}
1837
1838static int calc_pg_rank(int osd, const struct ceph_osds *acting)
1839{
1840 int i;
1841
1842 for (i = 0; i < acting->size; i++) {
1843 if (acting->osds[i] == osd)
1844 return i;
1845 }
1846
1847 return -1;
1848}
1849
1850static bool primary_changed(const struct ceph_osds *old_acting,
1851 const struct ceph_osds *new_acting)
1852{
1853 if (!old_acting->size && !new_acting->size)
1854 return false; /* both still empty */
1855
1856 if (!old_acting->size ^ !new_acting->size)
1857 return true; /* was empty, now not, or vice versa */
1858
1859 if (old_acting->primary != new_acting->primary)
1860 return true; /* primary changed */
1861
1862 if (calc_pg_rank(old_acting->primary, old_acting) !=
1863 calc_pg_rank(new_acting->primary, new_acting))
1864 return true;
1865
1866 return false; /* same primary (tho replicas may have changed) */
1867}
1868
1869bool ceph_osds_changed(const struct ceph_osds *old_acting,
1870 const struct ceph_osds *new_acting,
1871 bool any_change)
1872{
1873 if (primary_changed(old_acting, new_acting))
1874 return true;
1875
1876 if (any_change && !__osds_equal(old_acting, new_acting))
1877 return true;
1878
1879 return false;
1880}
1881
Sage Weilf24e9982009-10-06 11:31:10 -07001882/*
1883 * calculate file layout from given offset, length.
1884 * fill in correct oid, logical length, and object extent
1885 * offset, length.
1886 *
1887 * for now, we write only a single su, until we can
1888 * pass a stride back to the caller.
1889 */
Sage Weild63b77f2012-09-24 20:59:48 -07001890int ceph_calc_file_object_mapping(struct ceph_file_layout *layout,
Alex Eldere8afad62012-11-14 09:38:19 -06001891 u64 off, u64 len,
Sage Weil645a1022009-10-28 15:15:05 -07001892 u64 *ono,
Sage Weilf24e9982009-10-06 11:31:10 -07001893 u64 *oxoff, u64 *oxlen)
1894{
Yan, Zheng76271512016-02-03 21:24:49 +08001895 u32 osize = layout->object_size;
1896 u32 su = layout->stripe_unit;
1897 u32 sc = layout->stripe_count;
Sage Weilf24e9982009-10-06 11:31:10 -07001898 u32 bl, stripeno, stripepos, objsetno;
1899 u32 su_per_object;
Noah Watkinsff1d1f72009-10-30 12:57:30 -07001900 u64 t, su_offset;
Sage Weilf24e9982009-10-06 11:31:10 -07001901
Alex Eldere8afad62012-11-14 09:38:19 -06001902 dout("mapping %llu~%llu osize %u fl_su %u\n", off, len,
Sage Weilf24e9982009-10-06 11:31:10 -07001903 osize, su);
Sage Weild63b77f2012-09-24 20:59:48 -07001904 if (su == 0 || sc == 0)
1905 goto invalid;
Noah Watkins35e054a2009-10-28 14:04:48 -07001906 su_per_object = osize / su;
Sage Weild63b77f2012-09-24 20:59:48 -07001907 if (su_per_object == 0)
1908 goto invalid;
Sage Weilf24e9982009-10-06 11:31:10 -07001909 dout("osize %u / su %u = su_per_object %u\n", osize, su,
1910 su_per_object);
1911
Sage Weild63b77f2012-09-24 20:59:48 -07001912 if ((su & ~PAGE_MASK) != 0)
1913 goto invalid;
1914
Sage Weilf24e9982009-10-06 11:31:10 -07001915 /* bl = *off / su; */
1916 t = off;
1917 do_div(t, su);
1918 bl = t;
1919 dout("off %llu / su %u = bl %u\n", off, su, bl);
1920
1921 stripeno = bl / sc;
1922 stripepos = bl % sc;
1923 objsetno = stripeno / su_per_object;
1924
Sage Weil645a1022009-10-28 15:15:05 -07001925 *ono = objsetno * sc + stripepos;
Eric Dumazet95c96172012-04-15 05:58:06 +00001926 dout("objset %u * sc %u = ono %u\n", objsetno, sc, (unsigned int)*ono);
Sage Weil645a1022009-10-28 15:15:05 -07001927
1928 /* *oxoff = *off % layout->fl_stripe_unit; # offset in su */
Sage Weilf24e9982009-10-06 11:31:10 -07001929 t = off;
Noah Watkinsff1d1f72009-10-30 12:57:30 -07001930 su_offset = do_div(t, su);
1931 *oxoff = su_offset + (stripeno % su_per_object) * su;
Sage Weil645a1022009-10-28 15:15:05 -07001932
Noah Watkinsff1d1f72009-10-30 12:57:30 -07001933 /*
1934 * Calculate the length of the extent being written to the selected
Alex Eldere8afad62012-11-14 09:38:19 -06001935 * object. This is the minimum of the full length requested (len) or
Noah Watkinsff1d1f72009-10-30 12:57:30 -07001936 * the remainder of the current stripe being written to.
1937 */
Alex Eldere8afad62012-11-14 09:38:19 -06001938 *oxlen = min_t(u64, len, su - su_offset);
Sage Weilf24e9982009-10-06 11:31:10 -07001939
1940 dout(" obj extent %llu~%llu\n", *oxoff, *oxlen);
Sage Weild63b77f2012-09-24 20:59:48 -07001941 return 0;
1942
1943invalid:
1944 dout(" invalid layout\n");
1945 *ono = 0;
1946 *oxoff = 0;
1947 *oxlen = 0;
1948 return -EINVAL;
Sage Weilf24e9982009-10-06 11:31:10 -07001949}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001950EXPORT_SYMBOL(ceph_calc_file_object_mapping);
Sage Weilf24e9982009-10-06 11:31:10 -07001951
1952/*
Ilya Dryomovd9591f52016-04-28 16:07:22 +02001953 * Map an object into a PG.
1954 *
1955 * Should only be called with target_oid and target_oloc (as opposed to
1956 * base_oid and base_oloc), since tiering isn't taken into account.
Sage Weilf24e9982009-10-06 11:31:10 -07001957 */
Ilya Dryomovdf281522017-06-15 16:30:56 +02001958int __ceph_object_locator_to_pg(struct ceph_pg_pool_info *pi,
1959 const struct ceph_object_id *oid,
1960 const struct ceph_object_locator *oloc,
1961 struct ceph_pg *raw_pgid)
Sage Weilf24e9982009-10-06 11:31:10 -07001962{
Ilya Dryomovdf281522017-06-15 16:30:56 +02001963 WARN_ON(pi->id != oloc->pool);
Sage Weilf24e9982009-10-06 11:31:10 -07001964
Yan, Zheng30c156d2016-02-14 11:24:31 +08001965 if (!oloc->pool_ns) {
1966 raw_pgid->pool = oloc->pool;
1967 raw_pgid->seed = ceph_str_hash(pi->object_hash, oid->name,
1968 oid->name_len);
1969 dout("%s %s -> raw_pgid %llu.%x\n", __func__, oid->name,
1970 raw_pgid->pool, raw_pgid->seed);
1971 } else {
1972 char stack_buf[256];
1973 char *buf = stack_buf;
1974 int nsl = oloc->pool_ns->len;
1975 size_t total = nsl + 1 + oid->name_len;
Ilya Dryomov7c13cb62014-01-27 17:40:19 +02001976
Yan, Zheng30c156d2016-02-14 11:24:31 +08001977 if (total > sizeof(stack_buf)) {
1978 buf = kmalloc(total, GFP_NOIO);
1979 if (!buf)
1980 return -ENOMEM;
1981 }
1982 memcpy(buf, oloc->pool_ns->str, nsl);
1983 buf[nsl] = '\037';
1984 memcpy(buf + nsl + 1, oid->name, oid->name_len);
1985 raw_pgid->pool = oloc->pool;
1986 raw_pgid->seed = ceph_str_hash(pi->object_hash, buf, total);
1987 if (buf != stack_buf)
1988 kfree(buf);
1989 dout("%s %s ns %.*s -> raw_pgid %llu.%x\n", __func__,
1990 oid->name, nsl, oloc->pool_ns->str,
1991 raw_pgid->pool, raw_pgid->seed);
1992 }
Sage Weilf24e9982009-10-06 11:31:10 -07001993 return 0;
1994}
Ilya Dryomovdf281522017-06-15 16:30:56 +02001995
1996int ceph_object_locator_to_pg(struct ceph_osdmap *osdmap,
1997 const struct ceph_object_id *oid,
1998 const struct ceph_object_locator *oloc,
1999 struct ceph_pg *raw_pgid)
2000{
2001 struct ceph_pg_pool_info *pi;
2002
2003 pi = ceph_pg_pool_by_id(osdmap, oloc->pool);
2004 if (!pi)
2005 return -ENOENT;
2006
2007 return __ceph_object_locator_to_pg(pi, oid, oloc, raw_pgid);
2008}
Ilya Dryomovd9591f52016-04-28 16:07:22 +02002009EXPORT_SYMBOL(ceph_object_locator_to_pg);
Sage Weilf24e9982009-10-06 11:31:10 -07002010
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002011/*
2012 * Map a raw PG (full precision ps) into an actual PG.
2013 */
2014static void raw_pg_to_pg(struct ceph_pg_pool_info *pi,
2015 const struct ceph_pg *raw_pgid,
2016 struct ceph_pg *pgid)
2017{
2018 pgid->pool = raw_pgid->pool;
2019 pgid->seed = ceph_stable_mod(raw_pgid->seed, pi->pg_num,
2020 pi->pg_num_mask);
2021}
2022
2023/*
2024 * Map a raw PG (full precision ps) into a placement ps (placement
2025 * seed). Include pool id in that value so that different pools don't
2026 * use the same seeds.
2027 */
2028static u32 raw_pg_to_pps(struct ceph_pg_pool_info *pi,
2029 const struct ceph_pg *raw_pgid)
2030{
2031 if (pi->flags & CEPH_POOL_FLAG_HASHPSPOOL) {
2032 /* hash pool id and seed so that pool PGs do not overlap */
2033 return crush_hash32_2(CRUSH_HASH_RJENKINS1,
2034 ceph_stable_mod(raw_pgid->seed,
2035 pi->pgp_num,
2036 pi->pgp_num_mask),
2037 raw_pgid->pool);
2038 } else {
2039 /*
2040 * legacy behavior: add ps and pool together. this is
2041 * not a great approach because the PGs from each pool
2042 * will overlap on top of each other: 0.5 == 1.4 ==
2043 * 2.3 == ...
2044 */
2045 return ceph_stable_mod(raw_pgid->seed, pi->pgp_num,
2046 pi->pgp_num_mask) +
2047 (unsigned)raw_pgid->pool;
2048 }
2049}
2050
Ilya Dryomov9d521472014-01-31 17:54:26 +02002051static int do_crush(struct ceph_osdmap *map, int ruleno, int x,
2052 int *result, int result_max,
2053 const __u32 *weight, int weight_max)
Ilya Dryomove8ef19c2013-12-24 21:19:24 +02002054{
Ilya Dryomov9d521472014-01-31 17:54:26 +02002055 int r;
Ilya Dryomove8ef19c2013-12-24 21:19:24 +02002056
Ilya Dryomov9d521472014-01-31 17:54:26 +02002057 BUG_ON(result_max > CEPH_PG_MAX_SIZE);
2058
Ilya Dryomov743efcf2017-01-31 15:55:06 +01002059 mutex_lock(&map->crush_workspace_mutex);
Ilya Dryomov9d521472014-01-31 17:54:26 +02002060 r = crush_do_rule(map->crush, ruleno, x, result, result_max,
Ilya Dryomov743efcf2017-01-31 15:55:06 +01002061 weight, weight_max, map->crush_workspace);
2062 mutex_unlock(&map->crush_workspace_mutex);
Ilya Dryomov9d521472014-01-31 17:54:26 +02002063
2064 return r;
Ilya Dryomove8ef19c2013-12-24 21:19:24 +02002065}
2066
Sage Weilf24e9982009-10-06 11:31:10 -07002067/*
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002068 * Calculate raw set (CRUSH output) for given PG. The result may
2069 * contain nonexistent OSDs. ->primary is undefined for a raw set.
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002070 *
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002071 * Placement seed (CRUSH input) is returned through @ppps.
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002072 */
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002073static void pg_to_raw_osds(struct ceph_osdmap *osdmap,
2074 struct ceph_pg_pool_info *pi,
2075 const struct ceph_pg *raw_pgid,
2076 struct ceph_osds *raw,
2077 u32 *ppps)
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002078{
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002079 u32 pps = raw_pg_to_pps(pi, raw_pgid);
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002080 int ruleno;
2081 int len;
2082
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002083 ceph_osds_init(raw);
2084 if (ppps)
2085 *ppps = pps;
2086
2087 ruleno = crush_find_rule(osdmap->crush, pi->crush_ruleset, pi->type,
2088 pi->size);
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002089 if (ruleno < 0) {
2090 pr_err("no crush rule: pool %lld ruleset %d type %d size %d\n",
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002091 pi->id, pi->crush_ruleset, pi->type, pi->size);
2092 return;
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002093 }
2094
Ilya Dryomovef9324bb2017-02-08 18:57:48 +01002095 if (pi->size > ARRAY_SIZE(raw->osds)) {
2096 pr_err_ratelimited("pool %lld ruleset %d type %d too wide: size %d > %zu\n",
2097 pi->id, pi->crush_ruleset, pi->type, pi->size,
2098 ARRAY_SIZE(raw->osds));
2099 return;
2100 }
2101
2102 len = do_crush(osdmap, ruleno, pps, raw->osds, pi->size,
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002103 osdmap->osd_weight, osdmap->max_osd);
2104 if (len < 0) {
2105 pr_err("error %d from crush rule %d: pool %lld ruleset %d type %d size %d\n",
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002106 len, ruleno, pi->id, pi->crush_ruleset, pi->type,
2107 pi->size);
2108 return;
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002109 }
2110
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002111 raw->size = len;
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002112}
2113
2114/*
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002115 * Given raw set, calculate up set and up primary. By definition of an
2116 * up set, the result won't contain nonexistent or down OSDs.
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002117 *
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002118 * This is done in-place - on return @set is the up set. If it's
2119 * empty, ->primary will remain undefined.
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002120 */
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002121static void raw_to_up_osds(struct ceph_osdmap *osdmap,
2122 struct ceph_pg_pool_info *pi,
2123 struct ceph_osds *set)
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002124{
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002125 int i;
2126
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002127 /* ->primary is undefined for a raw set */
2128 BUG_ON(set->primary != -1);
2129
2130 if (ceph_can_shift_osds(pi)) {
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002131 int removed = 0;
2132
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002133 /* shift left */
2134 for (i = 0; i < set->size; i++) {
2135 if (ceph_osd_is_down(osdmap, set->osds[i])) {
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002136 removed++;
2137 continue;
2138 }
2139 if (removed)
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002140 set->osds[i - removed] = set->osds[i];
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002141 }
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002142 set->size -= removed;
2143 if (set->size > 0)
2144 set->primary = set->osds[0];
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002145 } else {
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002146 /* set down/dne devices to NONE */
2147 for (i = set->size - 1; i >= 0; i--) {
2148 if (ceph_osd_is_down(osdmap, set->osds[i]))
2149 set->osds[i] = CRUSH_ITEM_NONE;
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002150 else
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002151 set->primary = set->osds[i];
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002152 }
2153 }
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002154}
2155
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002156static void apply_primary_affinity(struct ceph_osdmap *osdmap,
2157 struct ceph_pg_pool_info *pi,
2158 u32 pps,
2159 struct ceph_osds *up)
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002160{
2161 int i;
2162 int pos = -1;
2163
2164 /*
2165 * Do we have any non-default primary_affinity values for these
2166 * osds?
2167 */
2168 if (!osdmap->osd_primary_affinity)
2169 return;
2170
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002171 for (i = 0; i < up->size; i++) {
2172 int osd = up->osds[i];
Ilya Dryomov92b2e752014-04-10 18:09:41 +04002173
2174 if (osd != CRUSH_ITEM_NONE &&
2175 osdmap->osd_primary_affinity[osd] !=
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002176 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY) {
2177 break;
2178 }
2179 }
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002180 if (i == up->size)
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002181 return;
2182
2183 /*
2184 * Pick the primary. Feed both the seed (for the pg) and the
2185 * osd into the hash/rng so that a proportional fraction of an
2186 * osd's pgs get rejected as primary.
2187 */
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002188 for (i = 0; i < up->size; i++) {
2189 int osd = up->osds[i];
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002190 u32 aff;
2191
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002192 if (osd == CRUSH_ITEM_NONE)
2193 continue;
2194
2195 aff = osdmap->osd_primary_affinity[osd];
2196 if (aff < CEPH_OSD_MAX_PRIMARY_AFFINITY &&
2197 (crush_hash32_2(CRUSH_HASH_RJENKINS1,
2198 pps, osd) >> 16) >= aff) {
2199 /*
2200 * We chose not to use this primary. Note it
2201 * anyway as a fallback in case we don't pick
2202 * anyone else, but keep looking.
2203 */
2204 if (pos < 0)
2205 pos = i;
2206 } else {
2207 pos = i;
2208 break;
2209 }
2210 }
2211 if (pos < 0)
2212 return;
2213
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002214 up->primary = up->osds[pos];
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002215
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002216 if (ceph_can_shift_osds(pi) && pos > 0) {
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002217 /* move the new primary to the front */
2218 for (i = pos; i > 0; i--)
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002219 up->osds[i] = up->osds[i - 1];
2220 up->osds[0] = up->primary;
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002221 }
2222}
2223
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002224/*
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002225 * Get pg_temp and primary_temp mappings for given PG.
Ilya Dryomov45966c32014-03-24 17:12:47 +02002226 *
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002227 * Note that a PG may have none, only pg_temp, only primary_temp or
2228 * both pg_temp and primary_temp mappings. This means @temp isn't
2229 * always a valid OSD set on return: in the "only primary_temp" case,
2230 * @temp will have its ->primary >= 0 but ->size == 0.
Ilya Dryomov45966c32014-03-24 17:12:47 +02002231 */
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002232static void get_temp_osds(struct ceph_osdmap *osdmap,
2233 struct ceph_pg_pool_info *pi,
2234 const struct ceph_pg *raw_pgid,
2235 struct ceph_osds *temp)
Ilya Dryomov45966c32014-03-24 17:12:47 +02002236{
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002237 struct ceph_pg pgid;
Ilya Dryomov45966c32014-03-24 17:12:47 +02002238 struct ceph_pg_mapping *pg;
Ilya Dryomov45966c32014-03-24 17:12:47 +02002239 int i;
2240
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002241 raw_pg_to_pg(pi, raw_pgid, &pgid);
2242 ceph_osds_init(temp);
Ilya Dryomov45966c32014-03-24 17:12:47 +02002243
2244 /* pg_temp? */
Ilya Dryomov33333d12017-06-21 17:27:17 +02002245 pg = __lookup_pg_mapping(&osdmap->pg_temp, &pgid);
Ilya Dryomov45966c32014-03-24 17:12:47 +02002246 if (pg) {
Ilya Dryomov45966c32014-03-24 17:12:47 +02002247 for (i = 0; i < pg->pg_temp.len; i++) {
2248 if (ceph_osd_is_down(osdmap, pg->pg_temp.osds[i])) {
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002249 if (ceph_can_shift_osds(pi))
Ilya Dryomov45966c32014-03-24 17:12:47 +02002250 continue;
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002251
2252 temp->osds[temp->size++] = CRUSH_ITEM_NONE;
Ilya Dryomov45966c32014-03-24 17:12:47 +02002253 } else {
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002254 temp->osds[temp->size++] = pg->pg_temp.osds[i];
Ilya Dryomov45966c32014-03-24 17:12:47 +02002255 }
2256 }
2257
2258 /* apply pg_temp's primary */
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002259 for (i = 0; i < temp->size; i++) {
2260 if (temp->osds[i] != CRUSH_ITEM_NONE) {
2261 temp->primary = temp->osds[i];
Ilya Dryomov45966c32014-03-24 17:12:47 +02002262 break;
2263 }
2264 }
Ilya Dryomov45966c32014-03-24 17:12:47 +02002265 }
2266
Ilya Dryomov5e8d4d32014-03-24 17:12:48 +02002267 /* primary_temp? */
Ilya Dryomov33333d12017-06-21 17:27:17 +02002268 pg = __lookup_pg_mapping(&osdmap->primary_temp, &pgid);
Ilya Dryomov5e8d4d32014-03-24 17:12:48 +02002269 if (pg)
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002270 temp->primary = pg->primary_temp.osd;
Ilya Dryomov45966c32014-03-24 17:12:47 +02002271}
2272
2273/*
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002274 * Map a PG to its acting set as well as its up set.
Ilya Dryomovac972232014-03-24 17:12:48 +02002275 *
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002276 * Acting set is used for data mapping purposes, while up set can be
2277 * recorded for detecting interval changes and deciding whether to
2278 * resend a request.
Sage Weild85b7052010-05-10 10:24:48 -07002279 */
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002280void ceph_pg_to_up_acting_osds(struct ceph_osdmap *osdmap,
Ilya Dryomovdf281522017-06-15 16:30:56 +02002281 struct ceph_pg_pool_info *pi,
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002282 const struct ceph_pg *raw_pgid,
2283 struct ceph_osds *up,
2284 struct ceph_osds *acting)
Sage Weild85b7052010-05-10 10:24:48 -07002285{
Ilya Dryomovac972232014-03-24 17:12:48 +02002286 u32 pps;
Sage Weild85b7052010-05-10 10:24:48 -07002287
Ilya Dryomovdf281522017-06-15 16:30:56 +02002288 WARN_ON(pi->id != raw_pgid->pool);
Sage Weild85b7052010-05-10 10:24:48 -07002289
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002290 pg_to_raw_osds(osdmap, pi, raw_pgid, up, &pps);
2291 raw_to_up_osds(osdmap, pi, up);
2292 apply_primary_affinity(osdmap, pi, pps, up);
2293 get_temp_osds(osdmap, pi, raw_pgid, acting);
2294 if (!acting->size) {
2295 memcpy(acting->osds, up->osds, up->size * sizeof(up->osds[0]));
2296 acting->size = up->size;
2297 if (acting->primary == -1)
2298 acting->primary = up->primary;
Ilya Dryomovac972232014-03-24 17:12:48 +02002299 }
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002300 WARN_ON(!osds_valid(up) || !osds_valid(acting));
Sage Weild85b7052010-05-10 10:24:48 -07002301}
2302
Ilya Dryomovdc98ff72017-06-15 16:30:53 +02002303bool ceph_pg_to_primary_shard(struct ceph_osdmap *osdmap,
Ilya Dryomovdf281522017-06-15 16:30:56 +02002304 struct ceph_pg_pool_info *pi,
Ilya Dryomovdc98ff72017-06-15 16:30:53 +02002305 const struct ceph_pg *raw_pgid,
2306 struct ceph_spg *spgid)
2307{
Ilya Dryomovdc98ff72017-06-15 16:30:53 +02002308 struct ceph_pg pgid;
2309 struct ceph_osds up, acting;
2310 int i;
2311
Ilya Dryomovdf281522017-06-15 16:30:56 +02002312 WARN_ON(pi->id != raw_pgid->pool);
Ilya Dryomovdc98ff72017-06-15 16:30:53 +02002313 raw_pg_to_pg(pi, raw_pgid, &pgid);
2314
2315 if (ceph_can_shift_osds(pi)) {
2316 spgid->pgid = pgid; /* struct */
2317 spgid->shard = CEPH_SPG_NOSHARD;
2318 return true;
2319 }
2320
Ilya Dryomovdf281522017-06-15 16:30:56 +02002321 ceph_pg_to_up_acting_osds(osdmap, pi, &pgid, &up, &acting);
Ilya Dryomovdc98ff72017-06-15 16:30:53 +02002322 for (i = 0; i < acting.size; i++) {
2323 if (acting.osds[i] == acting.primary) {
2324 spgid->pgid = pgid; /* struct */
2325 spgid->shard = i;
2326 return true;
2327 }
2328 }
2329
2330 return false;
2331}
2332
Sage Weild85b7052010-05-10 10:24:48 -07002333/*
Ilya Dryomovf81f1632016-04-28 16:07:23 +02002334 * Return acting primary for given PG, or -1 if none.
Sage Weilf24e9982009-10-06 11:31:10 -07002335 */
Ilya Dryomovf81f1632016-04-28 16:07:23 +02002336int ceph_pg_to_acting_primary(struct ceph_osdmap *osdmap,
2337 const struct ceph_pg *raw_pgid)
Sage Weilf24e9982009-10-06 11:31:10 -07002338{
Ilya Dryomovdf281522017-06-15 16:30:56 +02002339 struct ceph_pg_pool_info *pi;
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002340 struct ceph_osds up, acting;
Sage Weilf24e9982009-10-06 11:31:10 -07002341
Ilya Dryomovdf281522017-06-15 16:30:56 +02002342 pi = ceph_pg_pool_by_id(osdmap, raw_pgid->pool);
2343 if (!pi)
2344 return -1;
2345
2346 ceph_pg_to_up_acting_osds(osdmap, pi, raw_pgid, &up, &acting);
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002347 return acting.primary;
Sage Weilf24e9982009-10-06 11:31:10 -07002348}
Ilya Dryomovf81f1632016-04-28 16:07:23 +02002349EXPORT_SYMBOL(ceph_pg_to_acting_primary);