blob: a4155620eace7804e9a21362aa6b04b74c915a1c [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
Sage Weilf24e9982009-10-06 11:31:10 -0700421/*
Sage Weil9794b142010-02-16 15:53:32 -0800422 * rbtree of pg_mapping for handling pg_temp (explicit mapping of pgid
Ilya Dryomov9686f942014-03-21 19:05:29 +0200423 * to a set of osds) and primary_temp (explicit primary setting)
Sage Weilf24e9982009-10-06 11:31:10 -0700424 */
Sage Weil991abb62009-10-08 22:22:37 -0700425static int __insert_pg_mapping(struct ceph_pg_mapping *new,
426 struct rb_root *root)
Sage Weilf24e9982009-10-06 11:31:10 -0700427{
428 struct rb_node **p = &root->rb_node;
429 struct rb_node *parent = NULL;
430 struct ceph_pg_mapping *pg = NULL;
Sage Weil51042122009-11-04 11:39:12 -0800431 int c;
Sage Weilf24e9982009-10-06 11:31:10 -0700432
Sage Weil8adc8b32011-09-28 10:11:04 -0700433 dout("__insert_pg_mapping %llx %p\n", *(u64 *)&new->pgid, new);
Sage Weilf24e9982009-10-06 11:31:10 -0700434 while (*p) {
435 parent = *p;
436 pg = rb_entry(parent, struct ceph_pg_mapping, node);
Ilya Dryomovf984cb72016-04-28 16:07:23 +0200437 c = ceph_pg_compare(&new->pgid, &pg->pgid);
Sage Weil51042122009-11-04 11:39:12 -0800438 if (c < 0)
Sage Weilf24e9982009-10-06 11:31:10 -0700439 p = &(*p)->rb_left;
Sage Weil51042122009-11-04 11:39:12 -0800440 else if (c > 0)
Sage Weilf24e9982009-10-06 11:31:10 -0700441 p = &(*p)->rb_right;
442 else
Sage Weil991abb62009-10-08 22:22:37 -0700443 return -EEXIST;
Sage Weilf24e9982009-10-06 11:31:10 -0700444 }
445
446 rb_link_node(&new->node, parent, p);
447 rb_insert_color(&new->node, root);
Sage Weil991abb62009-10-08 22:22:37 -0700448 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700449}
450
Sage Weil9794b142010-02-16 15:53:32 -0800451static struct ceph_pg_mapping *__lookup_pg_mapping(struct rb_root *root,
Sage Weil5b191d92013-02-23 10:38:16 -0800452 struct ceph_pg pgid)
Sage Weil9794b142010-02-16 15:53:32 -0800453{
454 struct rb_node *n = root->rb_node;
455 struct ceph_pg_mapping *pg;
456 int c;
457
458 while (n) {
459 pg = rb_entry(n, struct ceph_pg_mapping, node);
Ilya Dryomovf984cb72016-04-28 16:07:23 +0200460 c = ceph_pg_compare(&pgid, &pg->pgid);
Sage Weil8adc8b32011-09-28 10:11:04 -0700461 if (c < 0) {
Sage Weil9794b142010-02-16 15:53:32 -0800462 n = n->rb_left;
Sage Weil8adc8b32011-09-28 10:11:04 -0700463 } else if (c > 0) {
Sage Weil9794b142010-02-16 15:53:32 -0800464 n = n->rb_right;
Sage Weil8adc8b32011-09-28 10:11:04 -0700465 } else {
Sage Weil5b191d92013-02-23 10:38:16 -0800466 dout("__lookup_pg_mapping %lld.%x got %p\n",
467 pgid.pool, pgid.seed, pg);
Sage Weil9794b142010-02-16 15:53:32 -0800468 return pg;
Sage Weil8adc8b32011-09-28 10:11:04 -0700469 }
Sage Weil9794b142010-02-16 15:53:32 -0800470 }
471 return NULL;
472}
473
Sage Weil5b191d92013-02-23 10:38:16 -0800474static int __remove_pg_mapping(struct rb_root *root, struct ceph_pg pgid)
Sage Weil8adc8b32011-09-28 10:11:04 -0700475{
476 struct ceph_pg_mapping *pg = __lookup_pg_mapping(root, pgid);
477
478 if (pg) {
Sage Weil5b191d92013-02-23 10:38:16 -0800479 dout("__remove_pg_mapping %lld.%x %p\n", pgid.pool, pgid.seed,
480 pg);
Sage Weil8adc8b32011-09-28 10:11:04 -0700481 rb_erase(&pg->node, root);
482 kfree(pg);
483 return 0;
484 }
Sage Weil5b191d92013-02-23 10:38:16 -0800485 dout("__remove_pg_mapping %lld.%x dne\n", pgid.pool, pgid.seed);
Sage Weil8adc8b32011-09-28 10:11:04 -0700486 return -ENOENT;
487}
488
Sage Weilf24e9982009-10-06 11:31:10 -0700489/*
Sage Weil4fc51be2010-02-16 15:55:03 -0800490 * rbtree of pg pool info
491 */
492static int __insert_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *new)
493{
494 struct rb_node **p = &root->rb_node;
495 struct rb_node *parent = NULL;
496 struct ceph_pg_pool_info *pi = NULL;
497
498 while (*p) {
499 parent = *p;
500 pi = rb_entry(parent, struct ceph_pg_pool_info, node);
501 if (new->id < pi->id)
502 p = &(*p)->rb_left;
503 else if (new->id > pi->id)
504 p = &(*p)->rb_right;
505 else
506 return -EEXIST;
507 }
508
509 rb_link_node(&new->node, parent, p);
510 rb_insert_color(&new->node, root);
511 return 0;
512}
513
Sage Weil4f6a7e52013-02-23 10:41:09 -0800514static struct ceph_pg_pool_info *__lookup_pg_pool(struct rb_root *root, u64 id)
Sage Weil4fc51be2010-02-16 15:55:03 -0800515{
516 struct ceph_pg_pool_info *pi;
517 struct rb_node *n = root->rb_node;
518
519 while (n) {
520 pi = rb_entry(n, struct ceph_pg_pool_info, node);
521 if (id < pi->id)
522 n = n->rb_left;
523 else if (id > pi->id)
524 n = n->rb_right;
525 else
526 return pi;
527 }
528 return NULL;
529}
530
Ilya Dryomovce7f6a22014-01-27 17:40:19 +0200531struct ceph_pg_pool_info *ceph_pg_pool_by_id(struct ceph_osdmap *map, u64 id)
532{
533 return __lookup_pg_pool(&map->pg_pools, id);
534}
535
Alex Elder72afc712012-10-30 19:40:33 -0500536const char *ceph_pg_pool_name_by_id(struct ceph_osdmap *map, u64 id)
537{
538 struct ceph_pg_pool_info *pi;
539
540 if (id == CEPH_NOPOOL)
541 return NULL;
542
543 if (WARN_ON_ONCE(id > (u64) INT_MAX))
544 return NULL;
545
546 pi = __lookup_pg_pool(&map->pg_pools, (int) id);
547
548 return pi ? pi->name : NULL;
549}
550EXPORT_SYMBOL(ceph_pg_pool_name_by_id);
551
Yehuda Sadeh7669a2c2010-05-17 12:31:35 -0700552int ceph_pg_poolid_by_name(struct ceph_osdmap *map, const char *name)
553{
554 struct rb_node *rbp;
555
556 for (rbp = rb_first(&map->pg_pools); rbp; rbp = rb_next(rbp)) {
557 struct ceph_pg_pool_info *pi =
558 rb_entry(rbp, struct ceph_pg_pool_info, node);
559 if (pi->name && strcmp(pi->name, name) == 0)
560 return pi->id;
561 }
562 return -ENOENT;
563}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700564EXPORT_SYMBOL(ceph_pg_poolid_by_name);
Yehuda Sadeh7669a2c2010-05-17 12:31:35 -0700565
Sage Weil2844a762010-04-09 15:46:42 -0700566static void __remove_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *pi)
567{
568 rb_erase(&pi->node, root);
569 kfree(pi->name);
570 kfree(pi);
571}
572
Ilya Dryomov0f70c7e2014-03-21 19:05:27 +0200573static int decode_pool(void **p, void *end, struct ceph_pg_pool_info *pi)
Sage Weilefd75762010-03-17 10:05:28 -0700574{
Sage Weil4f6a7e52013-02-23 10:41:09 -0800575 u8 ev, cv;
576 unsigned len, num;
577 void *pool_end;
Sage Weil73a7e692010-08-02 11:00:55 -0700578
Sage Weil4f6a7e52013-02-23 10:41:09 -0800579 ceph_decode_need(p, end, 2 + 4, bad);
580 ev = ceph_decode_8(p); /* encoding version */
581 cv = ceph_decode_8(p); /* compat version */
582 if (ev < 5) {
Joe Perchesb9a67892014-09-09 21:17:29 -0700583 pr_warn("got v %d < 5 cv %d of ceph_pg_pool\n", ev, cv);
Sage Weil4f6a7e52013-02-23 10:41:09 -0800584 return -EINVAL;
585 }
Ilya Dryomov17a13e402014-01-27 17:40:19 +0200586 if (cv > 9) {
Joe Perchesb9a67892014-09-09 21:17:29 -0700587 pr_warn("got v %d cv %d > 9 of ceph_pg_pool\n", ev, cv);
Sage Weil4f6a7e52013-02-23 10:41:09 -0800588 return -EINVAL;
589 }
590 len = ceph_decode_32(p);
591 ceph_decode_need(p, end, len, bad);
592 pool_end = *p + len;
Sage Weil73a7e692010-08-02 11:00:55 -0700593
Sage Weil4f6a7e52013-02-23 10:41:09 -0800594 pi->type = ceph_decode_8(p);
595 pi->size = ceph_decode_8(p);
596 pi->crush_ruleset = ceph_decode_8(p);
597 pi->object_hash = ceph_decode_8(p);
598
599 pi->pg_num = ceph_decode_32(p);
600 pi->pgp_num = ceph_decode_32(p);
601
602 *p += 4 + 4; /* skip lpg* */
603 *p += 4; /* skip last_change */
604 *p += 8 + 4; /* skip snap_seq, snap_epoch */
605
606 /* skip snaps */
607 num = ceph_decode_32(p);
608 while (num--) {
609 *p += 8; /* snapid key */
610 *p += 1 + 1; /* versions */
611 len = ceph_decode_32(p);
612 *p += len;
Sage Weil73a7e692010-08-02 11:00:55 -0700613 }
614
Ilya Dryomov17a13e402014-01-27 17:40:19 +0200615 /* skip removed_snaps */
Sage Weil4f6a7e52013-02-23 10:41:09 -0800616 num = ceph_decode_32(p);
617 *p += num * (8 + 8);
618
619 *p += 8; /* skip auid */
620 pi->flags = ceph_decode_64(p);
Ilya Dryomov17a13e402014-01-27 17:40:19 +0200621 *p += 4; /* skip crash_replay_interval */
622
623 if (ev >= 7)
Ilya Dryomov04812ac2016-04-28 16:07:23 +0200624 pi->min_size = ceph_decode_8(p);
625 else
626 pi->min_size = pi->size - pi->size / 2;
Ilya Dryomov17a13e402014-01-27 17:40:19 +0200627
628 if (ev >= 8)
629 *p += 8 + 8; /* skip quota_max_* */
630
631 if (ev >= 9) {
632 /* skip tiers */
633 num = ceph_decode_32(p);
634 *p += num * 8;
635
636 *p += 8; /* skip tier_of */
637 *p += 1; /* skip cache_mode */
638
639 pi->read_tier = ceph_decode_64(p);
640 pi->write_tier = ceph_decode_64(p);
641 } else {
642 pi->read_tier = -1;
643 pi->write_tier = -1;
644 }
Sage Weil4f6a7e52013-02-23 10:41:09 -0800645
Ilya Dryomov04812ac2016-04-28 16:07:23 +0200646 if (ev >= 10) {
647 /* skip properties */
648 num = ceph_decode_32(p);
649 while (num--) {
650 len = ceph_decode_32(p);
651 *p += len; /* key */
652 len = ceph_decode_32(p);
653 *p += len; /* val */
654 }
655 }
656
657 if (ev >= 11) {
658 /* skip hit_set_params */
659 *p += 1 + 1; /* versions */
660 len = ceph_decode_32(p);
661 *p += len;
662
663 *p += 4; /* skip hit_set_period */
664 *p += 4; /* skip hit_set_count */
665 }
666
667 if (ev >= 12)
668 *p += 4; /* skip stripe_width */
669
670 if (ev >= 13) {
671 *p += 8; /* skip target_max_bytes */
672 *p += 8; /* skip target_max_objects */
673 *p += 4; /* skip cache_target_dirty_ratio_micro */
674 *p += 4; /* skip cache_target_full_ratio_micro */
675 *p += 4; /* skip cache_min_flush_age */
676 *p += 4; /* skip cache_min_evict_age */
677 }
678
679 if (ev >= 14) {
680 /* skip erasure_code_profile */
681 len = ceph_decode_32(p);
682 *p += len;
683 }
684
Ilya Dryomov8e48cf02017-06-05 14:45:00 +0200685 /*
686 * last_force_op_resend_preluminous, will be overridden if the
687 * map was encoded with RESEND_ON_SPLIT
688 */
Ilya Dryomov04812ac2016-04-28 16:07:23 +0200689 if (ev >= 15)
690 pi->last_force_request_resend = ceph_decode_32(p);
691 else
692 pi->last_force_request_resend = 0;
693
Ilya Dryomov8e48cf02017-06-05 14:45:00 +0200694 if (ev >= 16)
695 *p += 4; /* skip min_read_recency_for_promote */
696
697 if (ev >= 17)
698 *p += 8; /* skip expected_num_objects */
699
700 if (ev >= 19)
701 *p += 4; /* skip cache_target_dirty_high_ratio_micro */
702
703 if (ev >= 20)
704 *p += 4; /* skip min_write_recency_for_promote */
705
706 if (ev >= 21)
707 *p += 1; /* skip use_gmt_hitset */
708
709 if (ev >= 22)
710 *p += 1; /* skip fast_read */
711
712 if (ev >= 23) {
713 *p += 4; /* skip hit_set_grade_decay_rate */
714 *p += 4; /* skip hit_set_search_last_n */
715 }
716
717 if (ev >= 24) {
718 /* skip opts */
719 *p += 1 + 1; /* versions */
720 len = ceph_decode_32(p);
721 *p += len;
722 }
723
724 if (ev >= 25)
725 pi->last_force_request_resend = ceph_decode_32(p);
726
Sage Weil4f6a7e52013-02-23 10:41:09 -0800727 /* ignore the rest */
728
729 *p = pool_end;
730 calc_pg_masks(pi);
Sage Weil73a7e692010-08-02 11:00:55 -0700731 return 0;
732
733bad:
734 return -EINVAL;
Sage Weilefd75762010-03-17 10:05:28 -0700735}
736
Ilya Dryomov0f70c7e2014-03-21 19:05:27 +0200737static int decode_pool_names(void **p, void *end, struct ceph_osdmap *map)
Sage Weil2844a762010-04-09 15:46:42 -0700738{
739 struct ceph_pg_pool_info *pi;
Sage Weil4f6a7e52013-02-23 10:41:09 -0800740 u32 num, len;
741 u64 pool;
Sage Weil2844a762010-04-09 15:46:42 -0700742
743 ceph_decode_32_safe(p, end, num, bad);
744 dout(" %d pool names\n", num);
745 while (num--) {
Sage Weil4f6a7e52013-02-23 10:41:09 -0800746 ceph_decode_64_safe(p, end, pool, bad);
Sage Weil2844a762010-04-09 15:46:42 -0700747 ceph_decode_32_safe(p, end, len, bad);
Sage Weil4f6a7e52013-02-23 10:41:09 -0800748 dout(" pool %llu len %d\n", pool, len);
Xi Wangad3b9042012-06-06 19:35:55 -0500749 ceph_decode_need(p, end, len, bad);
Sage Weil2844a762010-04-09 15:46:42 -0700750 pi = __lookup_pg_pool(&map->pg_pools, pool);
751 if (pi) {
Xi Wangad3b9042012-06-06 19:35:55 -0500752 char *name = kstrndup(*p, len, GFP_NOFS);
753
754 if (!name)
755 return -ENOMEM;
Sage Weil2844a762010-04-09 15:46:42 -0700756 kfree(pi->name);
Xi Wangad3b9042012-06-06 19:35:55 -0500757 pi->name = name;
758 dout(" name is %s\n", pi->name);
Sage Weil2844a762010-04-09 15:46:42 -0700759 }
760 *p += len;
761 }
762 return 0;
763
764bad:
765 return -EINVAL;
766}
767
768/*
769 * osd map
770 */
Ilya Dryomove5253a72016-04-28 16:07:25 +0200771struct ceph_osdmap *ceph_osdmap_alloc(void)
772{
773 struct ceph_osdmap *map;
774
775 map = kzalloc(sizeof(*map), GFP_NOIO);
776 if (!map)
777 return NULL;
778
779 map->pg_pools = RB_ROOT;
780 map->pool_max = -1;
781 map->pg_temp = RB_ROOT;
782 map->primary_temp = RB_ROOT;
Ilya Dryomov743efcf2017-01-31 15:55:06 +0100783 mutex_init(&map->crush_workspace_mutex);
Ilya Dryomove5253a72016-04-28 16:07:25 +0200784
785 return map;
786}
787
Sage Weil2844a762010-04-09 15:46:42 -0700788void ceph_osdmap_destroy(struct ceph_osdmap *map)
789{
790 dout("osdmap_destroy %p\n", map);
791 if (map->crush)
792 crush_destroy(map->crush);
793 while (!RB_EMPTY_ROOT(&map->pg_temp)) {
794 struct ceph_pg_mapping *pg =
795 rb_entry(rb_first(&map->pg_temp),
796 struct ceph_pg_mapping, node);
797 rb_erase(&pg->node, &map->pg_temp);
798 kfree(pg);
799 }
Ilya Dryomov9686f942014-03-21 19:05:29 +0200800 while (!RB_EMPTY_ROOT(&map->primary_temp)) {
801 struct ceph_pg_mapping *pg =
802 rb_entry(rb_first(&map->primary_temp),
803 struct ceph_pg_mapping, node);
804 rb_erase(&pg->node, &map->primary_temp);
805 kfree(pg);
806 }
Sage Weil2844a762010-04-09 15:46:42 -0700807 while (!RB_EMPTY_ROOT(&map->pg_pools)) {
808 struct ceph_pg_pool_info *pi =
809 rb_entry(rb_first(&map->pg_pools),
810 struct ceph_pg_pool_info, node);
811 __remove_pg_pool(&map->pg_pools, pi);
812 }
813 kfree(map->osd_state);
814 kfree(map->osd_weight);
815 kfree(map->osd_addr);
Ilya Dryomov2cfa34f2014-03-21 19:05:30 +0200816 kfree(map->osd_primary_affinity);
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100817 kfree(map->crush_workspace);
Sage Weil2844a762010-04-09 15:46:42 -0700818 kfree(map);
819}
820
821/*
Ilya Dryomov4d603512014-03-21 19:05:28 +0200822 * Adjust max_osd value, (re)allocate arrays.
823 *
824 * The new elements are properly initialized.
Sage Weil2844a762010-04-09 15:46:42 -0700825 */
826static int osdmap_set_max_osd(struct ceph_osdmap *map, int max)
827{
828 u8 *state;
Sage Weil2844a762010-04-09 15:46:42 -0700829 u32 *weight;
Ilya Dryomov4d603512014-03-21 19:05:28 +0200830 struct ceph_entity_addr *addr;
831 int i;
Sage Weil2844a762010-04-09 15:46:42 -0700832
Ilya Dryomov4d603512014-03-21 19:05:28 +0200833 state = krealloc(map->osd_state, max*sizeof(*state), GFP_NOFS);
Li RongQing589506f2014-09-07 18:10:51 +0800834 if (!state)
Sage Weil2844a762010-04-09 15:46:42 -0700835 return -ENOMEM;
Li RongQing589506f2014-09-07 18:10:51 +0800836 map->osd_state = state;
837
838 weight = krealloc(map->osd_weight, max*sizeof(*weight), GFP_NOFS);
839 if (!weight)
840 return -ENOMEM;
841 map->osd_weight = weight;
842
843 addr = krealloc(map->osd_addr, max*sizeof(*addr), GFP_NOFS);
844 if (!addr)
845 return -ENOMEM;
846 map->osd_addr = addr;
Sage Weil2844a762010-04-09 15:46:42 -0700847
Ilya Dryomov4d603512014-03-21 19:05:28 +0200848 for (i = map->max_osd; i < max; i++) {
Li RongQing589506f2014-09-07 18:10:51 +0800849 map->osd_state[i] = 0;
850 map->osd_weight[i] = CEPH_OSD_OUT;
851 memset(map->osd_addr + i, 0, sizeof(*map->osd_addr));
Sage Weil2844a762010-04-09 15:46:42 -0700852 }
853
Ilya Dryomov2cfa34f2014-03-21 19:05:30 +0200854 if (map->osd_primary_affinity) {
855 u32 *affinity;
856
857 affinity = krealloc(map->osd_primary_affinity,
858 max*sizeof(*affinity), GFP_NOFS);
859 if (!affinity)
860 return -ENOMEM;
Li RongQing589506f2014-09-07 18:10:51 +0800861 map->osd_primary_affinity = affinity;
Ilya Dryomov2cfa34f2014-03-21 19:05:30 +0200862
863 for (i = map->max_osd; i < max; i++)
Li RongQing589506f2014-09-07 18:10:51 +0800864 map->osd_primary_affinity[i] =
865 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
Ilya Dryomov2cfa34f2014-03-21 19:05:30 +0200866 }
867
Sage Weil2844a762010-04-09 15:46:42 -0700868 map->max_osd = max;
Ilya Dryomov4d603512014-03-21 19:05:28 +0200869
Sage Weil2844a762010-04-09 15:46:42 -0700870 return 0;
871}
872
Ilya Dryomov1b6a78b2017-01-31 15:55:06 +0100873static int osdmap_set_crush(struct ceph_osdmap *map, struct crush_map *crush)
874{
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100875 void *workspace;
Ilya Dryomov743efcf2017-01-31 15:55:06 +0100876 size_t work_size;
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100877
Ilya Dryomov1b6a78b2017-01-31 15:55:06 +0100878 if (IS_ERR(crush))
879 return PTR_ERR(crush);
880
Ilya Dryomov743efcf2017-01-31 15:55:06 +0100881 work_size = crush_work_size(crush, CEPH_PG_MAX_SIZE);
882 dout("%s work_size %zu bytes\n", __func__, work_size);
883 workspace = kmalloc(work_size, GFP_NOIO);
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100884 if (!workspace) {
885 crush_destroy(crush);
886 return -ENOMEM;
887 }
888 crush_init_workspace(crush, workspace);
889
Ilya Dryomov1b6a78b2017-01-31 15:55:06 +0100890 if (map->crush)
891 crush_destroy(map->crush);
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100892 kfree(map->crush_workspace);
Ilya Dryomov1b6a78b2017-01-31 15:55:06 +0100893 map->crush = crush;
Ilya Dryomov66a0e2d2017-01-31 15:55:06 +0100894 map->crush_workspace = workspace;
Ilya Dryomov1b6a78b2017-01-31 15:55:06 +0100895 return 0;
896}
897
Ilya Dryomovec7af972014-03-21 19:05:29 +0200898#define OSDMAP_WRAPPER_COMPAT_VER 7
899#define OSDMAP_CLIENT_DATA_COMPAT_VER 1
900
901/*
902 * Return 0 or error. On success, *v is set to 0 for old (v6) osdmaps,
903 * to struct_v of the client_data section for new (v7 and above)
904 * osdmaps.
905 */
906static int get_osdmap_client_data_v(void **p, void *end,
907 const char *prefix, u8 *v)
908{
909 u8 struct_v;
910
911 ceph_decode_8_safe(p, end, struct_v, e_inval);
912 if (struct_v >= 7) {
913 u8 struct_compat;
914
915 ceph_decode_8_safe(p, end, struct_compat, e_inval);
916 if (struct_compat > OSDMAP_WRAPPER_COMPAT_VER) {
Joe Perchesb9a67892014-09-09 21:17:29 -0700917 pr_warn("got v %d cv %d > %d of %s ceph_osdmap\n",
918 struct_v, struct_compat,
919 OSDMAP_WRAPPER_COMPAT_VER, prefix);
Ilya Dryomovec7af972014-03-21 19:05:29 +0200920 return -EINVAL;
921 }
922 *p += 4; /* ignore wrapper struct_len */
923
924 ceph_decode_8_safe(p, end, struct_v, e_inval);
925 ceph_decode_8_safe(p, end, struct_compat, e_inval);
926 if (struct_compat > OSDMAP_CLIENT_DATA_COMPAT_VER) {
Joe Perchesb9a67892014-09-09 21:17:29 -0700927 pr_warn("got v %d cv %d > %d of %s ceph_osdmap client data\n",
928 struct_v, struct_compat,
929 OSDMAP_CLIENT_DATA_COMPAT_VER, prefix);
Ilya Dryomovec7af972014-03-21 19:05:29 +0200930 return -EINVAL;
931 }
932 *p += 4; /* ignore client data struct_len */
933 } else {
934 u16 version;
935
936 *p -= 1;
937 ceph_decode_16_safe(p, end, version, e_inval);
938 if (version < 6) {
Joe Perchesb9a67892014-09-09 21:17:29 -0700939 pr_warn("got v %d < 6 of %s ceph_osdmap\n",
940 version, prefix);
Ilya Dryomovec7af972014-03-21 19:05:29 +0200941 return -EINVAL;
942 }
943
944 /* old osdmap enconding */
945 struct_v = 0;
946 }
947
948 *v = struct_v;
949 return 0;
950
951e_inval:
952 return -EINVAL;
953}
954
Ilya Dryomov433fbdd2014-03-21 19:05:27 +0200955static int __decode_pools(void **p, void *end, struct ceph_osdmap *map,
956 bool incremental)
957{
958 u32 n;
959
960 ceph_decode_32_safe(p, end, n, e_inval);
961 while (n--) {
962 struct ceph_pg_pool_info *pi;
963 u64 pool;
964 int ret;
965
966 ceph_decode_64_safe(p, end, pool, e_inval);
967
968 pi = __lookup_pg_pool(&map->pg_pools, pool);
969 if (!incremental || !pi) {
970 pi = kzalloc(sizeof(*pi), GFP_NOFS);
971 if (!pi)
972 return -ENOMEM;
973
974 pi->id = pool;
975
976 ret = __insert_pg_pool(&map->pg_pools, pi);
977 if (ret) {
978 kfree(pi);
979 return ret;
980 }
981 }
982
983 ret = decode_pool(p, end, pi);
984 if (ret)
985 return ret;
986 }
987
988 return 0;
989
990e_inval:
991 return -EINVAL;
992}
993
994static int decode_pools(void **p, void *end, struct ceph_osdmap *map)
995{
996 return __decode_pools(p, end, map, false);
997}
998
999static int decode_new_pools(void **p, void *end, struct ceph_osdmap *map)
1000{
1001 return __decode_pools(p, end, map, true);
1002}
1003
Ilya Dryomov10db6342014-03-21 19:05:28 +02001004static int __decode_pg_temp(void **p, void *end, struct ceph_osdmap *map,
1005 bool incremental)
1006{
1007 u32 n;
1008
1009 ceph_decode_32_safe(p, end, n, e_inval);
1010 while (n--) {
1011 struct ceph_pg pgid;
1012 u32 len, i;
1013 int ret;
1014
1015 ret = ceph_decode_pgid(p, end, &pgid);
1016 if (ret)
1017 return ret;
1018
1019 ceph_decode_32_safe(p, end, len, e_inval);
1020
1021 ret = __remove_pg_mapping(&map->pg_temp, pgid);
1022 BUG_ON(!incremental && ret != -ENOENT);
1023
1024 if (!incremental || len > 0) {
1025 struct ceph_pg_mapping *pg;
1026
1027 ceph_decode_need(p, end, len*sizeof(u32), e_inval);
1028
1029 if (len > (UINT_MAX - sizeof(*pg)) / sizeof(u32))
1030 return -EINVAL;
1031
1032 pg = kzalloc(sizeof(*pg) + len*sizeof(u32), GFP_NOFS);
1033 if (!pg)
1034 return -ENOMEM;
1035
1036 pg->pgid = pgid;
Ilya Dryomov35a935d2014-03-21 19:05:29 +02001037 pg->pg_temp.len = len;
Ilya Dryomov10db6342014-03-21 19:05:28 +02001038 for (i = 0; i < len; i++)
Ilya Dryomov35a935d2014-03-21 19:05:29 +02001039 pg->pg_temp.osds[i] = ceph_decode_32(p);
Ilya Dryomov10db6342014-03-21 19:05:28 +02001040
1041 ret = __insert_pg_mapping(pg, &map->pg_temp);
1042 if (ret) {
1043 kfree(pg);
1044 return ret;
1045 }
1046 }
1047 }
1048
1049 return 0;
1050
1051e_inval:
1052 return -EINVAL;
1053}
1054
1055static int decode_pg_temp(void **p, void *end, struct ceph_osdmap *map)
1056{
1057 return __decode_pg_temp(p, end, map, false);
1058}
1059
1060static int decode_new_pg_temp(void **p, void *end, struct ceph_osdmap *map)
1061{
1062 return __decode_pg_temp(p, end, map, true);
1063}
1064
Ilya Dryomovd286de72014-03-21 19:05:30 +02001065static int __decode_primary_temp(void **p, void *end, struct ceph_osdmap *map,
1066 bool incremental)
1067{
1068 u32 n;
1069
1070 ceph_decode_32_safe(p, end, n, e_inval);
1071 while (n--) {
1072 struct ceph_pg pgid;
1073 u32 osd;
1074 int ret;
1075
1076 ret = ceph_decode_pgid(p, end, &pgid);
1077 if (ret)
1078 return ret;
1079
1080 ceph_decode_32_safe(p, end, osd, e_inval);
1081
1082 ret = __remove_pg_mapping(&map->primary_temp, pgid);
1083 BUG_ON(!incremental && ret != -ENOENT);
1084
1085 if (!incremental || osd != (u32)-1) {
1086 struct ceph_pg_mapping *pg;
1087
1088 pg = kzalloc(sizeof(*pg), GFP_NOFS);
1089 if (!pg)
1090 return -ENOMEM;
1091
1092 pg->pgid = pgid;
1093 pg->primary_temp.osd = osd;
1094
1095 ret = __insert_pg_mapping(pg, &map->primary_temp);
1096 if (ret) {
1097 kfree(pg);
1098 return ret;
1099 }
1100 }
1101 }
1102
1103 return 0;
1104
1105e_inval:
1106 return -EINVAL;
1107}
1108
1109static int decode_primary_temp(void **p, void *end, struct ceph_osdmap *map)
1110{
1111 return __decode_primary_temp(p, end, map, false);
1112}
1113
1114static int decode_new_primary_temp(void **p, void *end,
1115 struct ceph_osdmap *map)
1116{
1117 return __decode_primary_temp(p, end, map, true);
1118}
1119
Ilya Dryomov2cfa34f2014-03-21 19:05:30 +02001120u32 ceph_get_primary_affinity(struct ceph_osdmap *map, int osd)
1121{
1122 BUG_ON(osd >= map->max_osd);
1123
1124 if (!map->osd_primary_affinity)
1125 return CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
1126
1127 return map->osd_primary_affinity[osd];
1128}
1129
1130static int set_primary_affinity(struct ceph_osdmap *map, int osd, u32 aff)
1131{
1132 BUG_ON(osd >= map->max_osd);
1133
1134 if (!map->osd_primary_affinity) {
1135 int i;
1136
1137 map->osd_primary_affinity = kmalloc(map->max_osd*sizeof(u32),
1138 GFP_NOFS);
1139 if (!map->osd_primary_affinity)
1140 return -ENOMEM;
1141
1142 for (i = 0; i < map->max_osd; i++)
1143 map->osd_primary_affinity[i] =
1144 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
1145 }
1146
1147 map->osd_primary_affinity[osd] = aff;
1148
1149 return 0;
1150}
1151
Ilya Dryomov63a69932014-03-21 19:05:30 +02001152static int decode_primary_affinity(void **p, void *end,
1153 struct ceph_osdmap *map)
1154{
1155 u32 len, i;
1156
1157 ceph_decode_32_safe(p, end, len, e_inval);
1158 if (len == 0) {
1159 kfree(map->osd_primary_affinity);
1160 map->osd_primary_affinity = NULL;
1161 return 0;
1162 }
1163 if (len != map->max_osd)
1164 goto e_inval;
1165
1166 ceph_decode_need(p, end, map->max_osd*sizeof(u32), e_inval);
1167
1168 for (i = 0; i < map->max_osd; i++) {
1169 int ret;
1170
1171 ret = set_primary_affinity(map, i, ceph_decode_32(p));
1172 if (ret)
1173 return ret;
1174 }
1175
1176 return 0;
1177
1178e_inval:
1179 return -EINVAL;
1180}
1181
1182static int decode_new_primary_affinity(void **p, void *end,
1183 struct ceph_osdmap *map)
1184{
1185 u32 n;
1186
1187 ceph_decode_32_safe(p, end, n, e_inval);
1188 while (n--) {
1189 u32 osd, aff;
1190 int ret;
1191
1192 ceph_decode_32_safe(p, end, osd, e_inval);
1193 ceph_decode_32_safe(p, end, aff, e_inval);
1194
1195 ret = set_primary_affinity(map, osd, aff);
1196 if (ret)
1197 return ret;
Ilya Dryomovf31da0f2014-04-02 20:34:04 +04001198
1199 pr_info("osd%d primary-affinity 0x%x\n", osd, aff);
Ilya Dryomov63a69932014-03-21 19:05:30 +02001200 }
1201
1202 return 0;
1203
1204e_inval:
1205 return -EINVAL;
1206}
1207
Sage Weil4fc51be2010-02-16 15:55:03 -08001208/*
Sage Weilf24e9982009-10-06 11:31:10 -07001209 * decode a full map.
1210 */
Ilya Dryomova2505d62014-03-13 16:36:13 +02001211static int osdmap_decode(void **p, void *end, struct ceph_osdmap *map)
Sage Weilf24e9982009-10-06 11:31:10 -07001212{
Ilya Dryomovec7af972014-03-21 19:05:29 +02001213 u8 struct_v;
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001214 u32 epoch = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001215 void *start = *p;
Ilya Dryomov39770582014-03-13 16:36:14 +02001216 u32 max;
1217 u32 len, i;
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001218 int err;
Sage Weilf24e9982009-10-06 11:31:10 -07001219
Ilya Dryomova2505d62014-03-13 16:36:13 +02001220 dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p));
Sage Weilf24e9982009-10-06 11:31:10 -07001221
Ilya Dryomovec7af972014-03-21 19:05:29 +02001222 err = get_osdmap_client_data_v(p, end, "full", &struct_v);
1223 if (err)
1224 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001225
Ilya Dryomov53bbaba2014-03-13 16:36:16 +02001226 /* fsid, epoch, created, modified */
1227 ceph_decode_need(p, end, sizeof(map->fsid) + sizeof(u32) +
1228 sizeof(map->created) + sizeof(map->modified), e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001229 ceph_decode_copy(p, &map->fsid, sizeof(map->fsid));
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001230 epoch = map->epoch = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -07001231 ceph_decode_copy(p, &map->created, sizeof(map->created));
1232 ceph_decode_copy(p, &map->modified, sizeof(map->modified));
1233
Ilya Dryomov433fbdd2014-03-21 19:05:27 +02001234 /* pools */
1235 err = decode_pools(p, end, map);
1236 if (err)
1237 goto bad;
Sage Weil2844a762010-04-09 15:46:42 -07001238
Ilya Dryomov0f70c7e2014-03-21 19:05:27 +02001239 /* pool_name */
1240 err = decode_pool_names(p, end, map);
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001241 if (err)
Sage Weil4f6a7e52013-02-23 10:41:09 -08001242 goto bad;
Sage Weil2844a762010-04-09 15:46:42 -07001243
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001244 ceph_decode_32_safe(p, end, map->pool_max, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001245
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001246 ceph_decode_32_safe(p, end, map->flags, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001247
Ilya Dryomov39770582014-03-13 16:36:14 +02001248 /* max_osd */
1249 ceph_decode_32_safe(p, end, max, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001250
1251 /* (re)alloc osd arrays */
1252 err = osdmap_set_max_osd(map, max);
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001253 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001254 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001255
Ilya Dryomov2d88b2e2014-03-13 16:36:14 +02001256 /* osd_state, osd_weight, osd_addrs->client_addr */
Sage Weilf24e9982009-10-06 11:31:10 -07001257 ceph_decode_need(p, end, 3*sizeof(u32) +
1258 map->max_osd*(1 + sizeof(*map->osd_weight) +
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001259 sizeof(*map->osd_addr)), e_inval);
1260
Ilya Dryomov2d88b2e2014-03-13 16:36:14 +02001261 if (ceph_decode_32(p) != map->max_osd)
1262 goto e_inval;
1263
Sage Weilf24e9982009-10-06 11:31:10 -07001264 ceph_decode_copy(p, map->osd_state, map->max_osd);
1265
Ilya Dryomov2d88b2e2014-03-13 16:36:14 +02001266 if (ceph_decode_32(p) != map->max_osd)
1267 goto e_inval;
1268
Sage Weilf24e9982009-10-06 11:31:10 -07001269 for (i = 0; i < map->max_osd; i++)
Sage Weilc89136e2009-10-14 09:59:09 -07001270 map->osd_weight[i] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -07001271
Ilya Dryomov2d88b2e2014-03-13 16:36:14 +02001272 if (ceph_decode_32(p) != map->max_osd)
1273 goto e_inval;
1274
Sage Weilf24e9982009-10-06 11:31:10 -07001275 ceph_decode_copy(p, map->osd_addr, map->max_osd*sizeof(*map->osd_addr));
Sage Weil63f2d212009-11-03 15:17:56 -08001276 for (i = 0; i < map->max_osd; i++)
1277 ceph_decode_addr(&map->osd_addr[i]);
Sage Weilf24e9982009-10-06 11:31:10 -07001278
1279 /* pg_temp */
Ilya Dryomov10db6342014-03-21 19:05:28 +02001280 err = decode_pg_temp(p, end, map);
1281 if (err)
1282 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001283
Ilya Dryomovd286de72014-03-21 19:05:30 +02001284 /* primary_temp */
1285 if (struct_v >= 1) {
1286 err = decode_primary_temp(p, end, map);
1287 if (err)
1288 goto bad;
1289 }
1290
Ilya Dryomov63a69932014-03-21 19:05:30 +02001291 /* primary_affinity */
1292 if (struct_v >= 2) {
1293 err = decode_primary_affinity(p, end, map);
1294 if (err)
1295 goto bad;
1296 } else {
1297 /* XXX can this happen? */
1298 kfree(map->osd_primary_affinity);
1299 map->osd_primary_affinity = NULL;
1300 }
1301
Sage Weilf24e9982009-10-06 11:31:10 -07001302 /* crush */
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001303 ceph_decode_32_safe(p, end, len, e_inval);
Ilya Dryomov1b6a78b2017-01-31 15:55:06 +01001304 err = osdmap_set_crush(map, crush_decode(*p, min(*p + len, end)));
1305 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001306 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001307
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001308 /* ignore the rest */
Sage Weilf24e9982009-10-06 11:31:10 -07001309 *p = end;
1310
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001311 dout("full osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd);
Ilya Dryomova2505d62014-03-13 16:36:13 +02001312 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001313
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001314e_inval:
1315 err = -EINVAL;
Sage Weilf24e9982009-10-06 11:31:10 -07001316bad:
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001317 pr_err("corrupt full osdmap (%d) epoch %d off %d (%p of %p-%p)\n",
1318 err, epoch, (int)(*p - start), *p, start, end);
1319 print_hex_dump(KERN_DEBUG, "osdmap: ",
1320 DUMP_PREFIX_OFFSET, 16, 1,
1321 start, end - start, true);
Ilya Dryomova2505d62014-03-13 16:36:13 +02001322 return err;
1323}
1324
1325/*
1326 * Allocate and decode a full map.
1327 */
1328struct ceph_osdmap *ceph_osdmap_decode(void **p, void *end)
1329{
1330 struct ceph_osdmap *map;
1331 int ret;
1332
Ilya Dryomove5253a72016-04-28 16:07:25 +02001333 map = ceph_osdmap_alloc();
Ilya Dryomova2505d62014-03-13 16:36:13 +02001334 if (!map)
1335 return ERR_PTR(-ENOMEM);
1336
Ilya Dryomova2505d62014-03-13 16:36:13 +02001337 ret = osdmap_decode(p, end, map);
1338 if (ret) {
1339 ceph_osdmap_destroy(map);
1340 return ERR_PTR(ret);
1341 }
1342
1343 return map;
Sage Weilf24e9982009-10-06 11:31:10 -07001344}
1345
1346/*
Ilya Dryomov930c5322016-07-19 03:50:28 +02001347 * Encoding order is (new_up_client, new_state, new_weight). Need to
1348 * apply in the (new_weight, new_state, new_up_client) order, because
1349 * an incremental map may look like e.g.
1350 *
1351 * new_up_client: { osd=6, addr=... } # set osd_state and addr
1352 * new_state: { osd=6, xorstate=EXISTS } # clear osd_state
1353 */
1354static int decode_new_up_state_weight(void **p, void *end,
1355 struct ceph_osdmap *map)
1356{
1357 void *new_up_client;
1358 void *new_state;
1359 void *new_weight_end;
1360 u32 len;
1361
1362 new_up_client = *p;
1363 ceph_decode_32_safe(p, end, len, e_inval);
1364 len *= sizeof(u32) + sizeof(struct ceph_entity_addr);
1365 ceph_decode_need(p, end, len, e_inval);
1366 *p += len;
1367
1368 new_state = *p;
1369 ceph_decode_32_safe(p, end, len, e_inval);
1370 len *= sizeof(u32) + sizeof(u8);
1371 ceph_decode_need(p, end, len, e_inval);
1372 *p += len;
1373
1374 /* new_weight */
1375 ceph_decode_32_safe(p, end, len, e_inval);
1376 while (len--) {
1377 s32 osd;
1378 u32 w;
1379
1380 ceph_decode_need(p, end, 2*sizeof(u32), e_inval);
1381 osd = ceph_decode_32(p);
1382 w = ceph_decode_32(p);
1383 BUG_ON(osd >= map->max_osd);
1384 pr_info("osd%d weight 0x%x %s\n", osd, w,
1385 w == CEPH_OSD_IN ? "(in)" :
1386 (w == CEPH_OSD_OUT ? "(out)" : ""));
1387 map->osd_weight[osd] = w;
1388
1389 /*
1390 * If we are marking in, set the EXISTS, and clear the
1391 * AUTOOUT and NEW bits.
1392 */
1393 if (w) {
1394 map->osd_state[osd] |= CEPH_OSD_EXISTS;
1395 map->osd_state[osd] &= ~(CEPH_OSD_AUTOOUT |
1396 CEPH_OSD_NEW);
1397 }
1398 }
1399 new_weight_end = *p;
1400
1401 /* new_state (up/down) */
1402 *p = new_state;
1403 len = ceph_decode_32(p);
1404 while (len--) {
1405 s32 osd;
1406 u8 xorstate;
1407 int ret;
1408
1409 osd = ceph_decode_32(p);
1410 xorstate = ceph_decode_8(p);
1411 if (xorstate == 0)
1412 xorstate = CEPH_OSD_UP;
1413 BUG_ON(osd >= map->max_osd);
1414 if ((map->osd_state[osd] & CEPH_OSD_UP) &&
1415 (xorstate & CEPH_OSD_UP))
1416 pr_info("osd%d down\n", osd);
1417 if ((map->osd_state[osd] & CEPH_OSD_EXISTS) &&
1418 (xorstate & CEPH_OSD_EXISTS)) {
1419 pr_info("osd%d does not exist\n", osd);
Ilya Dryomov930c5322016-07-19 03:50:28 +02001420 ret = set_primary_affinity(map, osd,
1421 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY);
1422 if (ret)
1423 return ret;
1424 memset(map->osd_addr + osd, 0, sizeof(*map->osd_addr));
1425 map->osd_state[osd] = 0;
1426 } else {
1427 map->osd_state[osd] ^= xorstate;
1428 }
1429 }
1430
1431 /* new_up_client */
1432 *p = new_up_client;
1433 len = ceph_decode_32(p);
1434 while (len--) {
1435 s32 osd;
1436 struct ceph_entity_addr addr;
1437
1438 osd = ceph_decode_32(p);
1439 ceph_decode_copy(p, &addr, sizeof(addr));
1440 ceph_decode_addr(&addr);
1441 BUG_ON(osd >= map->max_osd);
1442 pr_info("osd%d up\n", osd);
1443 map->osd_state[osd] |= CEPH_OSD_EXISTS | CEPH_OSD_UP;
1444 map->osd_addr[osd] = addr;
1445 }
1446
1447 *p = new_weight_end;
1448 return 0;
1449
1450e_inval:
1451 return -EINVAL;
1452}
1453
1454/*
Sage Weilf24e9982009-10-06 11:31:10 -07001455 * decode and apply an incremental map update.
1456 */
1457struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end,
Ilya Dryomov0c0a8de2016-04-28 16:07:21 +02001458 struct ceph_osdmap *map)
Sage Weilf24e9982009-10-06 11:31:10 -07001459{
Sage Weilf24e9982009-10-06 11:31:10 -07001460 struct ceph_fsid fsid;
1461 u32 epoch = 0;
1462 struct ceph_timespec modified;
Sage Weil4f6a7e52013-02-23 10:41:09 -08001463 s32 len;
1464 u64 pool;
1465 __s64 new_pool_max;
1466 __s32 new_flags, max;
Sage Weilf24e9982009-10-06 11:31:10 -07001467 void *start = *p;
Ilya Dryomov86f17422014-03-13 16:36:15 +02001468 int err;
Ilya Dryomovec7af972014-03-21 19:05:29 +02001469 u8 struct_v;
Sage Weilf24e9982009-10-06 11:31:10 -07001470
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001471 dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p));
1472
Ilya Dryomovec7af972014-03-21 19:05:29 +02001473 err = get_osdmap_client_data_v(p, end, "inc", &struct_v);
1474 if (err)
1475 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001476
Ilya Dryomov53bbaba2014-03-13 16:36:16 +02001477 /* fsid, epoch, modified, new_pool_max, new_flags */
1478 ceph_decode_need(p, end, sizeof(fsid) + sizeof(u32) + sizeof(modified) +
1479 sizeof(u64) + sizeof(u32), e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001480 ceph_decode_copy(p, &fsid, sizeof(fsid));
Sage Weilc89136e2009-10-14 09:59:09 -07001481 epoch = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -07001482 BUG_ON(epoch != map->epoch+1);
1483 ceph_decode_copy(p, &modified, sizeof(modified));
Sage Weil4f6a7e52013-02-23 10:41:09 -08001484 new_pool_max = ceph_decode_64(p);
Sage Weilc89136e2009-10-14 09:59:09 -07001485 new_flags = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -07001486
1487 /* full map? */
Ilya Dryomov86f17422014-03-13 16:36:15 +02001488 ceph_decode_32_safe(p, end, len, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001489 if (len > 0) {
1490 dout("apply_incremental full map len %d, %p to %p\n",
1491 len, *p, end);
Ilya Dryomova2505d62014-03-13 16:36:13 +02001492 return ceph_osdmap_decode(p, min(*p+len, end));
Sage Weilf24e9982009-10-06 11:31:10 -07001493 }
1494
1495 /* new crush? */
Ilya Dryomov86f17422014-03-13 16:36:15 +02001496 ceph_decode_32_safe(p, end, len, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001497 if (len > 0) {
Ilya Dryomov1b6a78b2017-01-31 15:55:06 +01001498 err = osdmap_set_crush(map,
1499 crush_decode(*p, min(*p + len, end)));
1500 if (err)
Ilya Dryomov86f17422014-03-13 16:36:15 +02001501 goto bad;
Sage Weilcebc5be2010-06-17 10:22:48 -07001502 *p += len;
Sage Weilf24e9982009-10-06 11:31:10 -07001503 }
1504
1505 /* new flags? */
1506 if (new_flags >= 0)
1507 map->flags = new_flags;
Sage Weil4fc51be2010-02-16 15:55:03 -08001508 if (new_pool_max >= 0)
1509 map->pool_max = new_pool_max;
Sage Weilf24e9982009-10-06 11:31:10 -07001510
Sage Weilf24e9982009-10-06 11:31:10 -07001511 /* new max? */
Ilya Dryomov53bbaba2014-03-13 16:36:16 +02001512 ceph_decode_32_safe(p, end, max, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001513 if (max >= 0) {
1514 err = osdmap_set_max_osd(map, max);
Ilya Dryomov86f17422014-03-13 16:36:15 +02001515 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001516 goto bad;
1517 }
1518
1519 map->epoch++;
Sage Weil31456662011-05-12 15:18:43 -07001520 map->modified = modified;
Sage Weilf24e9982009-10-06 11:31:10 -07001521
Ilya Dryomov433fbdd2014-03-21 19:05:27 +02001522 /* new_pools */
1523 err = decode_new_pools(p, end, map);
1524 if (err)
1525 goto bad;
Ilya Dryomov9464d002014-03-13 16:36:16 +02001526
Ilya Dryomov0f70c7e2014-03-21 19:05:27 +02001527 /* new_pool_names */
1528 err = decode_pool_names(p, end, map);
Ilya Dryomov9464d002014-03-13 16:36:16 +02001529 if (err)
1530 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001531
Sage Weil4fc51be2010-02-16 15:55:03 -08001532 /* old_pool */
Ilya Dryomov86f17422014-03-13 16:36:15 +02001533 ceph_decode_32_safe(p, end, len, e_inval);
Sage Weil4fc51be2010-02-16 15:55:03 -08001534 while (len--) {
1535 struct ceph_pg_pool_info *pi;
1536
Ilya Dryomov86f17422014-03-13 16:36:15 +02001537 ceph_decode_64_safe(p, end, pool, e_inval);
Sage Weil4fc51be2010-02-16 15:55:03 -08001538 pi = __lookup_pg_pool(&map->pg_pools, pool);
Sage Weil2844a762010-04-09 15:46:42 -07001539 if (pi)
1540 __remove_pg_pool(&map->pg_pools, pi);
Sage Weil4fc51be2010-02-16 15:55:03 -08001541 }
Sage Weilf24e9982009-10-06 11:31:10 -07001542
Ilya Dryomov930c5322016-07-19 03:50:28 +02001543 /* new_up_client, new_state, new_weight */
1544 err = decode_new_up_state_weight(p, end, map);
1545 if (err)
1546 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001547
1548 /* new_pg_temp */
Ilya Dryomov10db6342014-03-21 19:05:28 +02001549 err = decode_new_pg_temp(p, end, map);
1550 if (err)
1551 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001552
Ilya Dryomovd286de72014-03-21 19:05:30 +02001553 /* new_primary_temp */
1554 if (struct_v >= 1) {
1555 err = decode_new_primary_temp(p, end, map);
1556 if (err)
1557 goto bad;
1558 }
1559
Ilya Dryomov63a69932014-03-21 19:05:30 +02001560 /* new_primary_affinity */
1561 if (struct_v >= 2) {
1562 err = decode_new_primary_affinity(p, end, map);
1563 if (err)
1564 goto bad;
1565 }
1566
Sage Weilf24e9982009-10-06 11:31:10 -07001567 /* ignore the rest */
1568 *p = end;
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001569
1570 dout("inc osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001571 return map;
1572
Ilya Dryomov86f17422014-03-13 16:36:15 +02001573e_inval:
1574 err = -EINVAL;
Sage Weilf24e9982009-10-06 11:31:10 -07001575bad:
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001576 pr_err("corrupt inc osdmap (%d) epoch %d off %d (%p of %p-%p)\n",
1577 err, epoch, (int)(*p - start), *p, start, end);
Sage Weil9ec7cab2009-12-14 15:13:47 -08001578 print_hex_dump(KERN_DEBUG, "osdmap: ",
1579 DUMP_PREFIX_OFFSET, 16, 1,
1580 start, end - start, true);
Sage Weilf24e9982009-10-06 11:31:10 -07001581 return ERR_PTR(err);
1582}
1583
Yan, Zheng30c156d2016-02-14 11:24:31 +08001584void ceph_oloc_copy(struct ceph_object_locator *dest,
1585 const struct ceph_object_locator *src)
1586{
Ilya Dryomovca35ffe2017-06-05 14:44:59 +02001587 ceph_oloc_destroy(dest);
Yan, Zheng30c156d2016-02-14 11:24:31 +08001588
1589 dest->pool = src->pool;
1590 if (src->pool_ns)
1591 dest->pool_ns = ceph_get_string(src->pool_ns);
Ilya Dryomovca35ffe2017-06-05 14:44:59 +02001592 else
1593 dest->pool_ns = NULL;
Yan, Zheng30c156d2016-02-14 11:24:31 +08001594}
1595EXPORT_SYMBOL(ceph_oloc_copy);
1596
1597void ceph_oloc_destroy(struct ceph_object_locator *oloc)
1598{
1599 ceph_put_string(oloc->pool_ns);
1600}
1601EXPORT_SYMBOL(ceph_oloc_destroy);
1602
Ilya Dryomovd30291b2016-04-29 19:54:20 +02001603void ceph_oid_copy(struct ceph_object_id *dest,
1604 const struct ceph_object_id *src)
1605{
Ilya Dryomovca35ffe2017-06-05 14:44:59 +02001606 ceph_oid_destroy(dest);
Sage Weilf24e9982009-10-06 11:31:10 -07001607
Ilya Dryomovd30291b2016-04-29 19:54:20 +02001608 if (src->name != src->inline_name) {
1609 /* very rare, see ceph_object_id definition */
1610 dest->name = kmalloc(src->name_len + 1,
1611 GFP_NOIO | __GFP_NOFAIL);
Ilya Dryomovca35ffe2017-06-05 14:44:59 +02001612 } else {
1613 dest->name = dest->inline_name;
Ilya Dryomovd30291b2016-04-29 19:54:20 +02001614 }
Ilya Dryomovd30291b2016-04-29 19:54:20 +02001615 memcpy(dest->name, src->name, src->name_len + 1);
1616 dest->name_len = src->name_len;
1617}
1618EXPORT_SYMBOL(ceph_oid_copy);
1619
1620static __printf(2, 0)
1621int oid_printf_vargs(struct ceph_object_id *oid, const char *fmt, va_list ap)
1622{
1623 int len;
1624
1625 WARN_ON(!ceph_oid_empty(oid));
1626
1627 len = vsnprintf(oid->inline_name, sizeof(oid->inline_name), fmt, ap);
1628 if (len >= sizeof(oid->inline_name))
1629 return len;
1630
1631 oid->name_len = len;
1632 return 0;
1633}
1634
1635/*
1636 * If oid doesn't fit into inline buffer, BUG.
1637 */
1638void ceph_oid_printf(struct ceph_object_id *oid, const char *fmt, ...)
1639{
1640 va_list ap;
1641
1642 va_start(ap, fmt);
1643 BUG_ON(oid_printf_vargs(oid, fmt, ap));
1644 va_end(ap);
1645}
1646EXPORT_SYMBOL(ceph_oid_printf);
1647
1648static __printf(3, 0)
1649int oid_aprintf_vargs(struct ceph_object_id *oid, gfp_t gfp,
1650 const char *fmt, va_list ap)
1651{
1652 va_list aq;
1653 int len;
1654
1655 va_copy(aq, ap);
1656 len = oid_printf_vargs(oid, fmt, aq);
1657 va_end(aq);
1658
1659 if (len) {
1660 char *external_name;
1661
1662 external_name = kmalloc(len + 1, gfp);
1663 if (!external_name)
1664 return -ENOMEM;
1665
1666 oid->name = external_name;
1667 WARN_ON(vsnprintf(oid->name, len + 1, fmt, ap) != len);
1668 oid->name_len = len;
1669 }
1670
1671 return 0;
1672}
1673
1674/*
1675 * If oid doesn't fit into inline buffer, allocate.
1676 */
1677int ceph_oid_aprintf(struct ceph_object_id *oid, gfp_t gfp,
1678 const char *fmt, ...)
1679{
1680 va_list ap;
1681 int ret;
1682
1683 va_start(ap, fmt);
1684 ret = oid_aprintf_vargs(oid, gfp, fmt, ap);
1685 va_end(ap);
1686
1687 return ret;
1688}
1689EXPORT_SYMBOL(ceph_oid_aprintf);
1690
1691void ceph_oid_destroy(struct ceph_object_id *oid)
1692{
1693 if (oid->name != oid->inline_name)
1694 kfree(oid->name);
1695}
1696EXPORT_SYMBOL(ceph_oid_destroy);
Sage Weilf24e9982009-10-06 11:31:10 -07001697
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001698/*
1699 * osds only
1700 */
1701static bool __osds_equal(const struct ceph_osds *lhs,
1702 const struct ceph_osds *rhs)
1703{
1704 if (lhs->size == rhs->size &&
1705 !memcmp(lhs->osds, rhs->osds, rhs->size * sizeof(rhs->osds[0])))
1706 return true;
1707
1708 return false;
1709}
1710
1711/*
1712 * osds + primary
1713 */
1714static bool osds_equal(const struct ceph_osds *lhs,
1715 const struct ceph_osds *rhs)
1716{
1717 if (__osds_equal(lhs, rhs) &&
1718 lhs->primary == rhs->primary)
1719 return true;
1720
1721 return false;
1722}
1723
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02001724static bool osds_valid(const struct ceph_osds *set)
1725{
1726 /* non-empty set */
1727 if (set->size > 0 && set->primary >= 0)
1728 return true;
1729
1730 /* empty can_shift_osds set */
1731 if (!set->size && set->primary == -1)
1732 return true;
1733
1734 /* empty !can_shift_osds set - all NONE */
1735 if (set->size > 0 && set->primary == -1) {
1736 int i;
1737
1738 for (i = 0; i < set->size; i++) {
1739 if (set->osds[i] != CRUSH_ITEM_NONE)
1740 break;
1741 }
1742 if (i == set->size)
1743 return true;
1744 }
1745
1746 return false;
1747}
1748
1749void ceph_osds_copy(struct ceph_osds *dest, const struct ceph_osds *src)
1750{
1751 memcpy(dest->osds, src->osds, src->size * sizeof(src->osds[0]));
1752 dest->size = src->size;
1753 dest->primary = src->primary;
1754}
1755
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001756static bool is_split(const struct ceph_pg *pgid,
1757 u32 old_pg_num,
1758 u32 new_pg_num)
1759{
1760 int old_bits = calc_bits_of(old_pg_num);
1761 int old_mask = (1 << old_bits) - 1;
1762 int n;
1763
1764 WARN_ON(pgid->seed >= old_pg_num);
1765 if (new_pg_num <= old_pg_num)
1766 return false;
1767
1768 for (n = 1; ; n++) {
1769 int next_bit = n << (old_bits - 1);
1770 u32 s = next_bit | pgid->seed;
1771
1772 if (s < old_pg_num || s == pgid->seed)
1773 continue;
1774 if (s >= new_pg_num)
1775 break;
1776
1777 s = ceph_stable_mod(s, old_pg_num, old_mask);
1778 if (s == pgid->seed)
1779 return true;
1780 }
1781
1782 return false;
1783}
1784
1785bool ceph_is_new_interval(const struct ceph_osds *old_acting,
1786 const struct ceph_osds *new_acting,
1787 const struct ceph_osds *old_up,
1788 const struct ceph_osds *new_up,
1789 int old_size,
1790 int new_size,
1791 int old_min_size,
1792 int new_min_size,
1793 u32 old_pg_num,
1794 u32 new_pg_num,
1795 bool old_sort_bitwise,
1796 bool new_sort_bitwise,
1797 const struct ceph_pg *pgid)
1798{
1799 return !osds_equal(old_acting, new_acting) ||
1800 !osds_equal(old_up, new_up) ||
1801 old_size != new_size ||
1802 old_min_size != new_min_size ||
1803 is_split(pgid, old_pg_num, new_pg_num) ||
1804 old_sort_bitwise != new_sort_bitwise;
1805}
1806
1807static int calc_pg_rank(int osd, const struct ceph_osds *acting)
1808{
1809 int i;
1810
1811 for (i = 0; i < acting->size; i++) {
1812 if (acting->osds[i] == osd)
1813 return i;
1814 }
1815
1816 return -1;
1817}
1818
1819static bool primary_changed(const struct ceph_osds *old_acting,
1820 const struct ceph_osds *new_acting)
1821{
1822 if (!old_acting->size && !new_acting->size)
1823 return false; /* both still empty */
1824
1825 if (!old_acting->size ^ !new_acting->size)
1826 return true; /* was empty, now not, or vice versa */
1827
1828 if (old_acting->primary != new_acting->primary)
1829 return true; /* primary changed */
1830
1831 if (calc_pg_rank(old_acting->primary, old_acting) !=
1832 calc_pg_rank(new_acting->primary, new_acting))
1833 return true;
1834
1835 return false; /* same primary (tho replicas may have changed) */
1836}
1837
1838bool ceph_osds_changed(const struct ceph_osds *old_acting,
1839 const struct ceph_osds *new_acting,
1840 bool any_change)
1841{
1842 if (primary_changed(old_acting, new_acting))
1843 return true;
1844
1845 if (any_change && !__osds_equal(old_acting, new_acting))
1846 return true;
1847
1848 return false;
1849}
1850
Sage Weilf24e9982009-10-06 11:31:10 -07001851/*
1852 * calculate file layout from given offset, length.
1853 * fill in correct oid, logical length, and object extent
1854 * offset, length.
1855 *
1856 * for now, we write only a single su, until we can
1857 * pass a stride back to the caller.
1858 */
Sage Weild63b77f2012-09-24 20:59:48 -07001859int ceph_calc_file_object_mapping(struct ceph_file_layout *layout,
Alex Eldere8afad62012-11-14 09:38:19 -06001860 u64 off, u64 len,
Sage Weil645a1022009-10-28 15:15:05 -07001861 u64 *ono,
Sage Weilf24e9982009-10-06 11:31:10 -07001862 u64 *oxoff, u64 *oxlen)
1863{
Yan, Zheng76271512016-02-03 21:24:49 +08001864 u32 osize = layout->object_size;
1865 u32 su = layout->stripe_unit;
1866 u32 sc = layout->stripe_count;
Sage Weilf24e9982009-10-06 11:31:10 -07001867 u32 bl, stripeno, stripepos, objsetno;
1868 u32 su_per_object;
Noah Watkinsff1d1f72009-10-30 12:57:30 -07001869 u64 t, su_offset;
Sage Weilf24e9982009-10-06 11:31:10 -07001870
Alex Eldere8afad62012-11-14 09:38:19 -06001871 dout("mapping %llu~%llu osize %u fl_su %u\n", off, len,
Sage Weilf24e9982009-10-06 11:31:10 -07001872 osize, su);
Sage Weild63b77f2012-09-24 20:59:48 -07001873 if (su == 0 || sc == 0)
1874 goto invalid;
Noah Watkins35e054a2009-10-28 14:04:48 -07001875 su_per_object = osize / su;
Sage Weild63b77f2012-09-24 20:59:48 -07001876 if (su_per_object == 0)
1877 goto invalid;
Sage Weilf24e9982009-10-06 11:31:10 -07001878 dout("osize %u / su %u = su_per_object %u\n", osize, su,
1879 su_per_object);
1880
Sage Weild63b77f2012-09-24 20:59:48 -07001881 if ((su & ~PAGE_MASK) != 0)
1882 goto invalid;
1883
Sage Weilf24e9982009-10-06 11:31:10 -07001884 /* bl = *off / su; */
1885 t = off;
1886 do_div(t, su);
1887 bl = t;
1888 dout("off %llu / su %u = bl %u\n", off, su, bl);
1889
1890 stripeno = bl / sc;
1891 stripepos = bl % sc;
1892 objsetno = stripeno / su_per_object;
1893
Sage Weil645a1022009-10-28 15:15:05 -07001894 *ono = objsetno * sc + stripepos;
Eric Dumazet95c96172012-04-15 05:58:06 +00001895 dout("objset %u * sc %u = ono %u\n", objsetno, sc, (unsigned int)*ono);
Sage Weil645a1022009-10-28 15:15:05 -07001896
1897 /* *oxoff = *off % layout->fl_stripe_unit; # offset in su */
Sage Weilf24e9982009-10-06 11:31:10 -07001898 t = off;
Noah Watkinsff1d1f72009-10-30 12:57:30 -07001899 su_offset = do_div(t, su);
1900 *oxoff = su_offset + (stripeno % su_per_object) * su;
Sage Weil645a1022009-10-28 15:15:05 -07001901
Noah Watkinsff1d1f72009-10-30 12:57:30 -07001902 /*
1903 * Calculate the length of the extent being written to the selected
Alex Eldere8afad62012-11-14 09:38:19 -06001904 * object. This is the minimum of the full length requested (len) or
Noah Watkinsff1d1f72009-10-30 12:57:30 -07001905 * the remainder of the current stripe being written to.
1906 */
Alex Eldere8afad62012-11-14 09:38:19 -06001907 *oxlen = min_t(u64, len, su - su_offset);
Sage Weilf24e9982009-10-06 11:31:10 -07001908
1909 dout(" obj extent %llu~%llu\n", *oxoff, *oxlen);
Sage Weild63b77f2012-09-24 20:59:48 -07001910 return 0;
1911
1912invalid:
1913 dout(" invalid layout\n");
1914 *ono = 0;
1915 *oxoff = 0;
1916 *oxlen = 0;
1917 return -EINVAL;
Sage Weilf24e9982009-10-06 11:31:10 -07001918}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001919EXPORT_SYMBOL(ceph_calc_file_object_mapping);
Sage Weilf24e9982009-10-06 11:31:10 -07001920
1921/*
Ilya Dryomovd9591f52016-04-28 16:07:22 +02001922 * Map an object into a PG.
1923 *
1924 * Should only be called with target_oid and target_oloc (as opposed to
1925 * base_oid and base_oloc), since tiering isn't taken into account.
Sage Weilf24e9982009-10-06 11:31:10 -07001926 */
Ilya Dryomovd9591f52016-04-28 16:07:22 +02001927int ceph_object_locator_to_pg(struct ceph_osdmap *osdmap,
1928 struct ceph_object_id *oid,
1929 struct ceph_object_locator *oloc,
1930 struct ceph_pg *raw_pgid)
Sage Weilf24e9982009-10-06 11:31:10 -07001931{
Ilya Dryomov7c13cb62014-01-27 17:40:19 +02001932 struct ceph_pg_pool_info *pi;
Sage Weilf24e9982009-10-06 11:31:10 -07001933
Ilya Dryomovd9591f52016-04-28 16:07:22 +02001934 pi = ceph_pg_pool_by_id(osdmap, oloc->pool);
Ilya Dryomov7c13cb62014-01-27 17:40:19 +02001935 if (!pi)
Ilya Dryomovd9591f52016-04-28 16:07:22 +02001936 return -ENOENT;
Sage Weilf24e9982009-10-06 11:31:10 -07001937
Yan, Zheng30c156d2016-02-14 11:24:31 +08001938 if (!oloc->pool_ns) {
1939 raw_pgid->pool = oloc->pool;
1940 raw_pgid->seed = ceph_str_hash(pi->object_hash, oid->name,
1941 oid->name_len);
1942 dout("%s %s -> raw_pgid %llu.%x\n", __func__, oid->name,
1943 raw_pgid->pool, raw_pgid->seed);
1944 } else {
1945 char stack_buf[256];
1946 char *buf = stack_buf;
1947 int nsl = oloc->pool_ns->len;
1948 size_t total = nsl + 1 + oid->name_len;
Ilya Dryomov7c13cb62014-01-27 17:40:19 +02001949
Yan, Zheng30c156d2016-02-14 11:24:31 +08001950 if (total > sizeof(stack_buf)) {
1951 buf = kmalloc(total, GFP_NOIO);
1952 if (!buf)
1953 return -ENOMEM;
1954 }
1955 memcpy(buf, oloc->pool_ns->str, nsl);
1956 buf[nsl] = '\037';
1957 memcpy(buf + nsl + 1, oid->name, oid->name_len);
1958 raw_pgid->pool = oloc->pool;
1959 raw_pgid->seed = ceph_str_hash(pi->object_hash, buf, total);
1960 if (buf != stack_buf)
1961 kfree(buf);
1962 dout("%s %s ns %.*s -> raw_pgid %llu.%x\n", __func__,
1963 oid->name, nsl, oloc->pool_ns->str,
1964 raw_pgid->pool, raw_pgid->seed);
1965 }
Sage Weilf24e9982009-10-06 11:31:10 -07001966 return 0;
1967}
Ilya Dryomovd9591f52016-04-28 16:07:22 +02001968EXPORT_SYMBOL(ceph_object_locator_to_pg);
Sage Weilf24e9982009-10-06 11:31:10 -07001969
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02001970/*
1971 * Map a raw PG (full precision ps) into an actual PG.
1972 */
1973static void raw_pg_to_pg(struct ceph_pg_pool_info *pi,
1974 const struct ceph_pg *raw_pgid,
1975 struct ceph_pg *pgid)
1976{
1977 pgid->pool = raw_pgid->pool;
1978 pgid->seed = ceph_stable_mod(raw_pgid->seed, pi->pg_num,
1979 pi->pg_num_mask);
1980}
1981
1982/*
1983 * Map a raw PG (full precision ps) into a placement ps (placement
1984 * seed). Include pool id in that value so that different pools don't
1985 * use the same seeds.
1986 */
1987static u32 raw_pg_to_pps(struct ceph_pg_pool_info *pi,
1988 const struct ceph_pg *raw_pgid)
1989{
1990 if (pi->flags & CEPH_POOL_FLAG_HASHPSPOOL) {
1991 /* hash pool id and seed so that pool PGs do not overlap */
1992 return crush_hash32_2(CRUSH_HASH_RJENKINS1,
1993 ceph_stable_mod(raw_pgid->seed,
1994 pi->pgp_num,
1995 pi->pgp_num_mask),
1996 raw_pgid->pool);
1997 } else {
1998 /*
1999 * legacy behavior: add ps and pool together. this is
2000 * not a great approach because the PGs from each pool
2001 * will overlap on top of each other: 0.5 == 1.4 ==
2002 * 2.3 == ...
2003 */
2004 return ceph_stable_mod(raw_pgid->seed, pi->pgp_num,
2005 pi->pgp_num_mask) +
2006 (unsigned)raw_pgid->pool;
2007 }
2008}
2009
Ilya Dryomov9d521472014-01-31 17:54:26 +02002010static int do_crush(struct ceph_osdmap *map, int ruleno, int x,
2011 int *result, int result_max,
2012 const __u32 *weight, int weight_max)
Ilya Dryomove8ef19c2013-12-24 21:19:24 +02002013{
Ilya Dryomov9d521472014-01-31 17:54:26 +02002014 int r;
Ilya Dryomove8ef19c2013-12-24 21:19:24 +02002015
Ilya Dryomov9d521472014-01-31 17:54:26 +02002016 BUG_ON(result_max > CEPH_PG_MAX_SIZE);
2017
Ilya Dryomov743efcf2017-01-31 15:55:06 +01002018 mutex_lock(&map->crush_workspace_mutex);
Ilya Dryomov9d521472014-01-31 17:54:26 +02002019 r = crush_do_rule(map->crush, ruleno, x, result, result_max,
Ilya Dryomov743efcf2017-01-31 15:55:06 +01002020 weight, weight_max, map->crush_workspace);
2021 mutex_unlock(&map->crush_workspace_mutex);
Ilya Dryomov9d521472014-01-31 17:54:26 +02002022
2023 return r;
Ilya Dryomove8ef19c2013-12-24 21:19:24 +02002024}
2025
Sage Weilf24e9982009-10-06 11:31:10 -07002026/*
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002027 * Calculate raw set (CRUSH output) for given PG. The result may
2028 * contain nonexistent OSDs. ->primary is undefined for a raw set.
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002029 *
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002030 * Placement seed (CRUSH input) is returned through @ppps.
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002031 */
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002032static void pg_to_raw_osds(struct ceph_osdmap *osdmap,
2033 struct ceph_pg_pool_info *pi,
2034 const struct ceph_pg *raw_pgid,
2035 struct ceph_osds *raw,
2036 u32 *ppps)
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002037{
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002038 u32 pps = raw_pg_to_pps(pi, raw_pgid);
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002039 int ruleno;
2040 int len;
2041
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002042 ceph_osds_init(raw);
2043 if (ppps)
2044 *ppps = pps;
2045
2046 ruleno = crush_find_rule(osdmap->crush, pi->crush_ruleset, pi->type,
2047 pi->size);
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002048 if (ruleno < 0) {
2049 pr_err("no crush rule: pool %lld ruleset %d type %d size %d\n",
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002050 pi->id, pi->crush_ruleset, pi->type, pi->size);
2051 return;
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002052 }
2053
Ilya Dryomovef9324bb2017-02-08 18:57:48 +01002054 if (pi->size > ARRAY_SIZE(raw->osds)) {
2055 pr_err_ratelimited("pool %lld ruleset %d type %d too wide: size %d > %zu\n",
2056 pi->id, pi->crush_ruleset, pi->type, pi->size,
2057 ARRAY_SIZE(raw->osds));
2058 return;
2059 }
2060
2061 len = do_crush(osdmap, ruleno, pps, raw->osds, pi->size,
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002062 osdmap->osd_weight, osdmap->max_osd);
2063 if (len < 0) {
2064 pr_err("error %d from crush rule %d: pool %lld ruleset %d type %d size %d\n",
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002065 len, ruleno, pi->id, pi->crush_ruleset, pi->type,
2066 pi->size);
2067 return;
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002068 }
2069
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002070 raw->size = len;
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002071}
2072
2073/*
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002074 * Given raw set, calculate up set and up primary. By definition of an
2075 * up set, the result won't contain nonexistent or down OSDs.
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002076 *
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002077 * This is done in-place - on return @set is the up set. If it's
2078 * empty, ->primary will remain undefined.
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002079 */
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002080static void raw_to_up_osds(struct ceph_osdmap *osdmap,
2081 struct ceph_pg_pool_info *pi,
2082 struct ceph_osds *set)
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002083{
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002084 int i;
2085
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002086 /* ->primary is undefined for a raw set */
2087 BUG_ON(set->primary != -1);
2088
2089 if (ceph_can_shift_osds(pi)) {
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002090 int removed = 0;
2091
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002092 /* shift left */
2093 for (i = 0; i < set->size; i++) {
2094 if (ceph_osd_is_down(osdmap, set->osds[i])) {
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002095 removed++;
2096 continue;
2097 }
2098 if (removed)
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002099 set->osds[i - removed] = set->osds[i];
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002100 }
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002101 set->size -= removed;
2102 if (set->size > 0)
2103 set->primary = set->osds[0];
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002104 } else {
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002105 /* set down/dne devices to NONE */
2106 for (i = set->size - 1; i >= 0; i--) {
2107 if (ceph_osd_is_down(osdmap, set->osds[i]))
2108 set->osds[i] = CRUSH_ITEM_NONE;
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002109 else
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002110 set->primary = set->osds[i];
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002111 }
2112 }
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002113}
2114
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002115static void apply_primary_affinity(struct ceph_osdmap *osdmap,
2116 struct ceph_pg_pool_info *pi,
2117 u32 pps,
2118 struct ceph_osds *up)
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002119{
2120 int i;
2121 int pos = -1;
2122
2123 /*
2124 * Do we have any non-default primary_affinity values for these
2125 * osds?
2126 */
2127 if (!osdmap->osd_primary_affinity)
2128 return;
2129
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002130 for (i = 0; i < up->size; i++) {
2131 int osd = up->osds[i];
Ilya Dryomov92b2e752014-04-10 18:09:41 +04002132
2133 if (osd != CRUSH_ITEM_NONE &&
2134 osdmap->osd_primary_affinity[osd] !=
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002135 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY) {
2136 break;
2137 }
2138 }
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002139 if (i == up->size)
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002140 return;
2141
2142 /*
2143 * Pick the primary. Feed both the seed (for the pg) and the
2144 * osd into the hash/rng so that a proportional fraction of an
2145 * osd's pgs get rejected as primary.
2146 */
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002147 for (i = 0; i < up->size; i++) {
2148 int osd = up->osds[i];
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002149 u32 aff;
2150
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002151 if (osd == CRUSH_ITEM_NONE)
2152 continue;
2153
2154 aff = osdmap->osd_primary_affinity[osd];
2155 if (aff < CEPH_OSD_MAX_PRIMARY_AFFINITY &&
2156 (crush_hash32_2(CRUSH_HASH_RJENKINS1,
2157 pps, osd) >> 16) >= aff) {
2158 /*
2159 * We chose not to use this primary. Note it
2160 * anyway as a fallback in case we don't pick
2161 * anyone else, but keep looking.
2162 */
2163 if (pos < 0)
2164 pos = i;
2165 } else {
2166 pos = i;
2167 break;
2168 }
2169 }
2170 if (pos < 0)
2171 return;
2172
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002173 up->primary = up->osds[pos];
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002174
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002175 if (ceph_can_shift_osds(pi) && pos > 0) {
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002176 /* move the new primary to the front */
2177 for (i = pos; i > 0; i--)
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002178 up->osds[i] = up->osds[i - 1];
2179 up->osds[0] = up->primary;
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002180 }
2181}
2182
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002183/*
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002184 * Get pg_temp and primary_temp mappings for given PG.
Ilya Dryomov45966c32014-03-24 17:12:47 +02002185 *
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002186 * Note that a PG may have none, only pg_temp, only primary_temp or
2187 * both pg_temp and primary_temp mappings. This means @temp isn't
2188 * always a valid OSD set on return: in the "only primary_temp" case,
2189 * @temp will have its ->primary >= 0 but ->size == 0.
Ilya Dryomov45966c32014-03-24 17:12:47 +02002190 */
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002191static void get_temp_osds(struct ceph_osdmap *osdmap,
2192 struct ceph_pg_pool_info *pi,
2193 const struct ceph_pg *raw_pgid,
2194 struct ceph_osds *temp)
Ilya Dryomov45966c32014-03-24 17:12:47 +02002195{
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002196 struct ceph_pg pgid;
Ilya Dryomov45966c32014-03-24 17:12:47 +02002197 struct ceph_pg_mapping *pg;
Ilya Dryomov45966c32014-03-24 17:12:47 +02002198 int i;
2199
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002200 raw_pg_to_pg(pi, raw_pgid, &pgid);
2201 ceph_osds_init(temp);
Ilya Dryomov45966c32014-03-24 17:12:47 +02002202
2203 /* pg_temp? */
2204 pg = __lookup_pg_mapping(&osdmap->pg_temp, pgid);
2205 if (pg) {
Ilya Dryomov45966c32014-03-24 17:12:47 +02002206 for (i = 0; i < pg->pg_temp.len; i++) {
2207 if (ceph_osd_is_down(osdmap, pg->pg_temp.osds[i])) {
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002208 if (ceph_can_shift_osds(pi))
Ilya Dryomov45966c32014-03-24 17:12:47 +02002209 continue;
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002210
2211 temp->osds[temp->size++] = CRUSH_ITEM_NONE;
Ilya Dryomov45966c32014-03-24 17:12:47 +02002212 } else {
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002213 temp->osds[temp->size++] = pg->pg_temp.osds[i];
Ilya Dryomov45966c32014-03-24 17:12:47 +02002214 }
2215 }
2216
2217 /* apply pg_temp's primary */
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002218 for (i = 0; i < temp->size; i++) {
2219 if (temp->osds[i] != CRUSH_ITEM_NONE) {
2220 temp->primary = temp->osds[i];
Ilya Dryomov45966c32014-03-24 17:12:47 +02002221 break;
2222 }
2223 }
Ilya Dryomov45966c32014-03-24 17:12:47 +02002224 }
2225
Ilya Dryomov5e8d4d32014-03-24 17:12:48 +02002226 /* primary_temp? */
2227 pg = __lookup_pg_mapping(&osdmap->primary_temp, pgid);
2228 if (pg)
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002229 temp->primary = pg->primary_temp.osd;
Ilya Dryomov45966c32014-03-24 17:12:47 +02002230}
2231
2232/*
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002233 * Map a PG to its acting set as well as its up set.
Ilya Dryomovac972232014-03-24 17:12:48 +02002234 *
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002235 * Acting set is used for data mapping purposes, while up set can be
2236 * recorded for detecting interval changes and deciding whether to
2237 * resend a request.
Sage Weild85b7052010-05-10 10:24:48 -07002238 */
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002239void ceph_pg_to_up_acting_osds(struct ceph_osdmap *osdmap,
2240 const struct ceph_pg *raw_pgid,
2241 struct ceph_osds *up,
2242 struct ceph_osds *acting)
Sage Weild85b7052010-05-10 10:24:48 -07002243{
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002244 struct ceph_pg_pool_info *pi;
Ilya Dryomovac972232014-03-24 17:12:48 +02002245 u32 pps;
Sage Weild85b7052010-05-10 10:24:48 -07002246
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002247 pi = ceph_pg_pool_by_id(osdmap, raw_pgid->pool);
2248 if (!pi) {
2249 ceph_osds_init(up);
2250 ceph_osds_init(acting);
2251 goto out;
Ilya Dryomov8008ab12014-03-24 17:12:48 +02002252 }
Sage Weild85b7052010-05-10 10:24:48 -07002253
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002254 pg_to_raw_osds(osdmap, pi, raw_pgid, up, &pps);
2255 raw_to_up_osds(osdmap, pi, up);
2256 apply_primary_affinity(osdmap, pi, pps, up);
2257 get_temp_osds(osdmap, pi, raw_pgid, acting);
2258 if (!acting->size) {
2259 memcpy(acting->osds, up->osds, up->size * sizeof(up->osds[0]));
2260 acting->size = up->size;
2261 if (acting->primary == -1)
2262 acting->primary = up->primary;
Ilya Dryomovac972232014-03-24 17:12:48 +02002263 }
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002264out:
2265 WARN_ON(!osds_valid(up) || !osds_valid(acting));
Sage Weild85b7052010-05-10 10:24:48 -07002266}
2267
Ilya Dryomovdc98ff72017-06-15 16:30:53 +02002268bool ceph_pg_to_primary_shard(struct ceph_osdmap *osdmap,
2269 const struct ceph_pg *raw_pgid,
2270 struct ceph_spg *spgid)
2271{
2272 struct ceph_pg_pool_info *pi;
2273 struct ceph_pg pgid;
2274 struct ceph_osds up, acting;
2275 int i;
2276
2277 pi = ceph_pg_pool_by_id(osdmap, raw_pgid->pool);
2278 if (!pi)
2279 return false;
2280
2281 raw_pg_to_pg(pi, raw_pgid, &pgid);
2282
2283 if (ceph_can_shift_osds(pi)) {
2284 spgid->pgid = pgid; /* struct */
2285 spgid->shard = CEPH_SPG_NOSHARD;
2286 return true;
2287 }
2288
2289 ceph_pg_to_up_acting_osds(osdmap, &pgid, &up, &acting);
2290 for (i = 0; i < acting.size; i++) {
2291 if (acting.osds[i] == acting.primary) {
2292 spgid->pgid = pgid; /* struct */
2293 spgid->shard = i;
2294 return true;
2295 }
2296 }
2297
2298 return false;
2299}
2300
Sage Weild85b7052010-05-10 10:24:48 -07002301/*
Ilya Dryomovf81f1632016-04-28 16:07:23 +02002302 * Return acting primary for given PG, or -1 if none.
Sage Weilf24e9982009-10-06 11:31:10 -07002303 */
Ilya Dryomovf81f1632016-04-28 16:07:23 +02002304int ceph_pg_to_acting_primary(struct ceph_osdmap *osdmap,
2305 const struct ceph_pg *raw_pgid)
Sage Weilf24e9982009-10-06 11:31:10 -07002306{
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002307 struct ceph_osds up, acting;
Sage Weilf24e9982009-10-06 11:31:10 -07002308
Ilya Dryomovf81f1632016-04-28 16:07:23 +02002309 ceph_pg_to_up_acting_osds(osdmap, raw_pgid, &up, &acting);
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002310 return acting.primary;
Sage Weilf24e9982009-10-06 11:31:10 -07002311}
Ilya Dryomovf81f1632016-04-28 16:07:23 +02002312EXPORT_SYMBOL(ceph_pg_to_acting_primary);