blob: 7e480bf75bcf5bfd69bd89cdccf29fc082a15e16 [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
Sage Weilf24e9982009-10-06 11:31:10 -0700156static struct crush_map *crush_decode(void *pbyval, void *end)
157{
158 struct crush_map *c;
159 int err = -EINVAL;
160 int i, j;
161 void **p = &pbyval;
162 void *start = pbyval;
163 u32 magic;
Sage Weil546f04e2012-07-30 18:15:23 -0700164 u32 num_name_maps;
Sage Weilf24e9982009-10-06 11:31:10 -0700165
166 dout("crush_decode %p to %p len %d\n", *p, end, (int)(end - *p));
167
168 c = kzalloc(sizeof(*c), GFP_NOFS);
169 if (c == NULL)
170 return ERR_PTR(-ENOMEM);
171
Sage Weil546f04e2012-07-30 18:15:23 -0700172 /* set tunables to default values */
173 c->choose_local_tries = 2;
174 c->choose_local_fallback_tries = 5;
175 c->choose_total_tries = 19;
Jim Schutt1604f482012-11-30 09:15:25 -0700176 c->chooseleaf_descend_once = 0;
Sage Weil546f04e2012-07-30 18:15:23 -0700177
Sage Weilf24e9982009-10-06 11:31:10 -0700178 ceph_decode_need(p, end, 4*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700179 magic = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700180 if (magic != CRUSH_MAGIC) {
181 pr_err("crush_decode magic %x != current %x\n",
Eric Dumazet95c96172012-04-15 05:58:06 +0000182 (unsigned int)magic, (unsigned int)CRUSH_MAGIC);
Sage Weilf24e9982009-10-06 11:31:10 -0700183 goto bad;
184 }
Sage Weilc89136e2009-10-14 09:59:09 -0700185 c->max_buckets = ceph_decode_32(p);
186 c->max_rules = ceph_decode_32(p);
187 c->max_devices = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700188
Sage Weilf24e9982009-10-06 11:31:10 -0700189 c->buckets = kcalloc(c->max_buckets, sizeof(*c->buckets), GFP_NOFS);
190 if (c->buckets == NULL)
191 goto badmem;
192 c->rules = kcalloc(c->max_rules, sizeof(*c->rules), GFP_NOFS);
193 if (c->rules == NULL)
194 goto badmem;
195
196 /* buckets */
197 for (i = 0; i < c->max_buckets; i++) {
198 int size = 0;
199 u32 alg;
200 struct crush_bucket *b;
201
202 ceph_decode_32_safe(p, end, alg, bad);
203 if (alg == 0) {
204 c->buckets[i] = NULL;
205 continue;
206 }
207 dout("crush_decode bucket %d off %x %p to %p\n",
208 i, (int)(*p-start), *p, end);
209
210 switch (alg) {
211 case CRUSH_BUCKET_UNIFORM:
212 size = sizeof(struct crush_bucket_uniform);
213 break;
214 case CRUSH_BUCKET_LIST:
215 size = sizeof(struct crush_bucket_list);
216 break;
217 case CRUSH_BUCKET_TREE:
218 size = sizeof(struct crush_bucket_tree);
219 break;
220 case CRUSH_BUCKET_STRAW:
221 size = sizeof(struct crush_bucket_straw);
222 break;
Ilya Dryomov958a2762015-04-14 16:54:52 +0300223 case CRUSH_BUCKET_STRAW2:
224 size = sizeof(struct crush_bucket_straw2);
225 break;
Sage Weilf24e9982009-10-06 11:31:10 -0700226 default:
Sage Weil30dc6382009-12-21 14:49:37 -0800227 err = -EINVAL;
Sage Weilf24e9982009-10-06 11:31:10 -0700228 goto bad;
229 }
230 BUG_ON(size == 0);
231 b = c->buckets[i] = kzalloc(size, GFP_NOFS);
232 if (b == NULL)
233 goto badmem;
234
235 ceph_decode_need(p, end, 4*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700236 b->id = ceph_decode_32(p);
237 b->type = ceph_decode_16(p);
Sage Weilfb690392009-11-07 20:18:22 -0800238 b->alg = ceph_decode_8(p);
239 b->hash = ceph_decode_8(p);
Sage Weilc89136e2009-10-14 09:59:09 -0700240 b->weight = ceph_decode_32(p);
241 b->size = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700242
243 dout("crush_decode bucket size %d off %x %p to %p\n",
244 b->size, (int)(*p-start), *p, end);
245
246 b->items = kcalloc(b->size, sizeof(__s32), GFP_NOFS);
247 if (b->items == NULL)
248 goto badmem;
249 b->perm = kcalloc(b->size, sizeof(u32), GFP_NOFS);
250 if (b->perm == NULL)
251 goto badmem;
252 b->perm_n = 0;
253
254 ceph_decode_need(p, end, b->size*sizeof(u32), bad);
255 for (j = 0; j < b->size; j++)
Sage Weilc89136e2009-10-14 09:59:09 -0700256 b->items[j] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700257
258 switch (b->alg) {
259 case CRUSH_BUCKET_UNIFORM:
260 err = crush_decode_uniform_bucket(p, end,
261 (struct crush_bucket_uniform *)b);
262 if (err < 0)
263 goto bad;
264 break;
265 case CRUSH_BUCKET_LIST:
266 err = crush_decode_list_bucket(p, end,
267 (struct crush_bucket_list *)b);
268 if (err < 0)
269 goto bad;
270 break;
271 case CRUSH_BUCKET_TREE:
272 err = crush_decode_tree_bucket(p, end,
273 (struct crush_bucket_tree *)b);
274 if (err < 0)
275 goto bad;
276 break;
277 case CRUSH_BUCKET_STRAW:
278 err = crush_decode_straw_bucket(p, end,
279 (struct crush_bucket_straw *)b);
280 if (err < 0)
281 goto bad;
282 break;
Ilya Dryomov958a2762015-04-14 16:54:52 +0300283 case CRUSH_BUCKET_STRAW2:
284 err = crush_decode_straw2_bucket(p, end,
285 (struct crush_bucket_straw2 *)b);
286 if (err < 0)
287 goto bad;
288 break;
Sage Weilf24e9982009-10-06 11:31:10 -0700289 }
290 }
291
292 /* rules */
293 dout("rule vec is %p\n", c->rules);
294 for (i = 0; i < c->max_rules; i++) {
295 u32 yes;
296 struct crush_rule *r;
297
298 ceph_decode_32_safe(p, end, yes, bad);
299 if (!yes) {
300 dout("crush_decode NO rule %d off %x %p to %p\n",
301 i, (int)(*p-start), *p, end);
302 c->rules[i] = NULL;
303 continue;
304 }
305
306 dout("crush_decode rule %d off %x %p to %p\n",
307 i, (int)(*p-start), *p, end);
308
309 /* len */
310 ceph_decode_32_safe(p, end, yes, bad);
311#if BITS_PER_LONG == 32
Sage Weil30dc6382009-12-21 14:49:37 -0800312 err = -EINVAL;
Xi Wang64486692012-02-16 11:55:48 -0500313 if (yes > (ULONG_MAX - sizeof(*r))
314 / sizeof(struct crush_rule_step))
Sage Weilf24e9982009-10-06 11:31:10 -0700315 goto bad;
316#endif
317 r = c->rules[i] = kmalloc(sizeof(*r) +
318 yes*sizeof(struct crush_rule_step),
319 GFP_NOFS);
320 if (r == NULL)
321 goto badmem;
322 dout(" rule %d is at %p\n", i, r);
323 r->len = yes;
324 ceph_decode_copy_safe(p, end, &r->mask, 4, bad); /* 4 u8's */
325 ceph_decode_need(p, end, r->len*3*sizeof(u32), bad);
326 for (j = 0; j < r->len; j++) {
Sage Weilc89136e2009-10-14 09:59:09 -0700327 r->steps[j].op = ceph_decode_32(p);
328 r->steps[j].arg1 = ceph_decode_32(p);
329 r->steps[j].arg2 = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700330 }
331 }
332
333 /* ignore trailing name maps. */
Sage Weil546f04e2012-07-30 18:15:23 -0700334 for (num_name_maps = 0; num_name_maps < 3; num_name_maps++) {
335 err = skip_name_map(p, end);
336 if (err < 0)
337 goto done;
338 }
Sage Weilf24e9982009-10-06 11:31:10 -0700339
Sage Weil546f04e2012-07-30 18:15:23 -0700340 /* tunables */
341 ceph_decode_need(p, end, 3*sizeof(u32), done);
342 c->choose_local_tries = ceph_decode_32(p);
343 c->choose_local_fallback_tries = ceph_decode_32(p);
344 c->choose_total_tries = ceph_decode_32(p);
Ilya Dryomovb9b519b2016-02-01 16:57:16 +0100345 dout("crush decode tunable choose_local_tries = %d\n",
Sage Weil546f04e2012-07-30 18:15:23 -0700346 c->choose_local_tries);
Ilya Dryomovb9b519b2016-02-01 16:57:16 +0100347 dout("crush decode tunable choose_local_fallback_tries = %d\n",
Sage Weil546f04e2012-07-30 18:15:23 -0700348 c->choose_local_fallback_tries);
Ilya Dryomovb9b519b2016-02-01 16:57:16 +0100349 dout("crush decode tunable choose_total_tries = %d\n",
Sage Weil546f04e2012-07-30 18:15:23 -0700350 c->choose_total_tries);
351
Jim Schutt1604f482012-11-30 09:15:25 -0700352 ceph_decode_need(p, end, sizeof(u32), done);
353 c->chooseleaf_descend_once = ceph_decode_32(p);
Ilya Dryomovb9b519b2016-02-01 16:57:16 +0100354 dout("crush decode tunable chooseleaf_descend_once = %d\n",
Jim Schutt1604f482012-11-30 09:15:25 -0700355 c->chooseleaf_descend_once);
356
Ilya Dryomovf1406622014-05-09 18:27:34 +0400357 ceph_decode_need(p, end, sizeof(u8), done);
358 c->chooseleaf_vary_r = ceph_decode_8(p);
Ilya Dryomovb9b519b2016-02-01 16:57:16 +0100359 dout("crush decode tunable chooseleaf_vary_r = %d\n",
Ilya Dryomovf1406622014-05-09 18:27:34 +0400360 c->chooseleaf_vary_r);
361
Ilya Dryomovb9b519b2016-02-01 16:57:16 +0100362 /* skip straw_calc_version, allowed_bucket_algs */
363 ceph_decode_need(p, end, sizeof(u8) + sizeof(u32), done);
364 *p += sizeof(u8) + sizeof(u32);
365
366 ceph_decode_need(p, end, sizeof(u8), done);
367 c->chooseleaf_stable = ceph_decode_8(p);
368 dout("crush decode tunable chooseleaf_stable = %d\n",
369 c->chooseleaf_stable);
370
Sage Weil546f04e2012-07-30 18:15:23 -0700371done:
Sage Weilf24e9982009-10-06 11:31:10 -0700372 dout("crush_decode success\n");
373 return c;
374
375badmem:
376 err = -ENOMEM;
377bad:
378 dout("crush_decode fail %d\n", err);
379 crush_destroy(c);
380 return ERR_PTR(err);
381}
382
Ilya Dryomovf984cb72016-04-28 16:07:23 +0200383int ceph_pg_compare(const struct ceph_pg *lhs, const struct ceph_pg *rhs)
384{
385 if (lhs->pool < rhs->pool)
386 return -1;
387 if (lhs->pool > rhs->pool)
388 return 1;
389 if (lhs->seed < rhs->seed)
390 return -1;
391 if (lhs->seed > rhs->seed)
392 return 1;
393
394 return 0;
395}
396
Sage Weilf24e9982009-10-06 11:31:10 -0700397/*
Sage Weil9794b142010-02-16 15:53:32 -0800398 * rbtree of pg_mapping for handling pg_temp (explicit mapping of pgid
Ilya Dryomov9686f942014-03-21 19:05:29 +0200399 * to a set of osds) and primary_temp (explicit primary setting)
Sage Weilf24e9982009-10-06 11:31:10 -0700400 */
Sage Weil991abb62009-10-08 22:22:37 -0700401static int __insert_pg_mapping(struct ceph_pg_mapping *new,
402 struct rb_root *root)
Sage Weilf24e9982009-10-06 11:31:10 -0700403{
404 struct rb_node **p = &root->rb_node;
405 struct rb_node *parent = NULL;
406 struct ceph_pg_mapping *pg = NULL;
Sage Weil51042122009-11-04 11:39:12 -0800407 int c;
Sage Weilf24e9982009-10-06 11:31:10 -0700408
Sage Weil8adc8b32011-09-28 10:11:04 -0700409 dout("__insert_pg_mapping %llx %p\n", *(u64 *)&new->pgid, new);
Sage Weilf24e9982009-10-06 11:31:10 -0700410 while (*p) {
411 parent = *p;
412 pg = rb_entry(parent, struct ceph_pg_mapping, node);
Ilya Dryomovf984cb72016-04-28 16:07:23 +0200413 c = ceph_pg_compare(&new->pgid, &pg->pgid);
Sage Weil51042122009-11-04 11:39:12 -0800414 if (c < 0)
Sage Weilf24e9982009-10-06 11:31:10 -0700415 p = &(*p)->rb_left;
Sage Weil51042122009-11-04 11:39:12 -0800416 else if (c > 0)
Sage Weilf24e9982009-10-06 11:31:10 -0700417 p = &(*p)->rb_right;
418 else
Sage Weil991abb62009-10-08 22:22:37 -0700419 return -EEXIST;
Sage Weilf24e9982009-10-06 11:31:10 -0700420 }
421
422 rb_link_node(&new->node, parent, p);
423 rb_insert_color(&new->node, root);
Sage Weil991abb62009-10-08 22:22:37 -0700424 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700425}
426
Sage Weil9794b142010-02-16 15:53:32 -0800427static struct ceph_pg_mapping *__lookup_pg_mapping(struct rb_root *root,
Sage Weil5b191d92013-02-23 10:38:16 -0800428 struct ceph_pg pgid)
Sage Weil9794b142010-02-16 15:53:32 -0800429{
430 struct rb_node *n = root->rb_node;
431 struct ceph_pg_mapping *pg;
432 int c;
433
434 while (n) {
435 pg = rb_entry(n, struct ceph_pg_mapping, node);
Ilya Dryomovf984cb72016-04-28 16:07:23 +0200436 c = ceph_pg_compare(&pgid, &pg->pgid);
Sage Weil8adc8b32011-09-28 10:11:04 -0700437 if (c < 0) {
Sage Weil9794b142010-02-16 15:53:32 -0800438 n = n->rb_left;
Sage Weil8adc8b32011-09-28 10:11:04 -0700439 } else if (c > 0) {
Sage Weil9794b142010-02-16 15:53:32 -0800440 n = n->rb_right;
Sage Weil8adc8b32011-09-28 10:11:04 -0700441 } else {
Sage Weil5b191d92013-02-23 10:38:16 -0800442 dout("__lookup_pg_mapping %lld.%x got %p\n",
443 pgid.pool, pgid.seed, pg);
Sage Weil9794b142010-02-16 15:53:32 -0800444 return pg;
Sage Weil8adc8b32011-09-28 10:11:04 -0700445 }
Sage Weil9794b142010-02-16 15:53:32 -0800446 }
447 return NULL;
448}
449
Sage Weil5b191d92013-02-23 10:38:16 -0800450static int __remove_pg_mapping(struct rb_root *root, struct ceph_pg pgid)
Sage Weil8adc8b32011-09-28 10:11:04 -0700451{
452 struct ceph_pg_mapping *pg = __lookup_pg_mapping(root, pgid);
453
454 if (pg) {
Sage Weil5b191d92013-02-23 10:38:16 -0800455 dout("__remove_pg_mapping %lld.%x %p\n", pgid.pool, pgid.seed,
456 pg);
Sage Weil8adc8b32011-09-28 10:11:04 -0700457 rb_erase(&pg->node, root);
458 kfree(pg);
459 return 0;
460 }
Sage Weil5b191d92013-02-23 10:38:16 -0800461 dout("__remove_pg_mapping %lld.%x dne\n", pgid.pool, pgid.seed);
Sage Weil8adc8b32011-09-28 10:11:04 -0700462 return -ENOENT;
463}
464
Sage Weilf24e9982009-10-06 11:31:10 -0700465/*
Sage Weil4fc51be2010-02-16 15:55:03 -0800466 * rbtree of pg pool info
467 */
468static int __insert_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *new)
469{
470 struct rb_node **p = &root->rb_node;
471 struct rb_node *parent = NULL;
472 struct ceph_pg_pool_info *pi = NULL;
473
474 while (*p) {
475 parent = *p;
476 pi = rb_entry(parent, struct ceph_pg_pool_info, node);
477 if (new->id < pi->id)
478 p = &(*p)->rb_left;
479 else if (new->id > pi->id)
480 p = &(*p)->rb_right;
481 else
482 return -EEXIST;
483 }
484
485 rb_link_node(&new->node, parent, p);
486 rb_insert_color(&new->node, root);
487 return 0;
488}
489
Sage Weil4f6a7e52013-02-23 10:41:09 -0800490static struct ceph_pg_pool_info *__lookup_pg_pool(struct rb_root *root, u64 id)
Sage Weil4fc51be2010-02-16 15:55:03 -0800491{
492 struct ceph_pg_pool_info *pi;
493 struct rb_node *n = root->rb_node;
494
495 while (n) {
496 pi = rb_entry(n, struct ceph_pg_pool_info, node);
497 if (id < pi->id)
498 n = n->rb_left;
499 else if (id > pi->id)
500 n = n->rb_right;
501 else
502 return pi;
503 }
504 return NULL;
505}
506
Ilya Dryomovce7f6a22014-01-27 17:40:19 +0200507struct ceph_pg_pool_info *ceph_pg_pool_by_id(struct ceph_osdmap *map, u64 id)
508{
509 return __lookup_pg_pool(&map->pg_pools, id);
510}
511
Alex Elder72afc712012-10-30 19:40:33 -0500512const char *ceph_pg_pool_name_by_id(struct ceph_osdmap *map, u64 id)
513{
514 struct ceph_pg_pool_info *pi;
515
516 if (id == CEPH_NOPOOL)
517 return NULL;
518
519 if (WARN_ON_ONCE(id > (u64) INT_MAX))
520 return NULL;
521
522 pi = __lookup_pg_pool(&map->pg_pools, (int) id);
523
524 return pi ? pi->name : NULL;
525}
526EXPORT_SYMBOL(ceph_pg_pool_name_by_id);
527
Yehuda Sadeh7669a2c2010-05-17 12:31:35 -0700528int ceph_pg_poolid_by_name(struct ceph_osdmap *map, const char *name)
529{
530 struct rb_node *rbp;
531
532 for (rbp = rb_first(&map->pg_pools); rbp; rbp = rb_next(rbp)) {
533 struct ceph_pg_pool_info *pi =
534 rb_entry(rbp, struct ceph_pg_pool_info, node);
535 if (pi->name && strcmp(pi->name, name) == 0)
536 return pi->id;
537 }
538 return -ENOENT;
539}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700540EXPORT_SYMBOL(ceph_pg_poolid_by_name);
Yehuda Sadeh7669a2c2010-05-17 12:31:35 -0700541
Sage Weil2844a762010-04-09 15:46:42 -0700542static void __remove_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *pi)
543{
544 rb_erase(&pi->node, root);
545 kfree(pi->name);
546 kfree(pi);
547}
548
Ilya Dryomov0f70c7e2014-03-21 19:05:27 +0200549static int decode_pool(void **p, void *end, struct ceph_pg_pool_info *pi)
Sage Weilefd75762010-03-17 10:05:28 -0700550{
Sage Weil4f6a7e52013-02-23 10:41:09 -0800551 u8 ev, cv;
552 unsigned len, num;
553 void *pool_end;
Sage Weil73a7e692010-08-02 11:00:55 -0700554
Sage Weil4f6a7e52013-02-23 10:41:09 -0800555 ceph_decode_need(p, end, 2 + 4, bad);
556 ev = ceph_decode_8(p); /* encoding version */
557 cv = ceph_decode_8(p); /* compat version */
558 if (ev < 5) {
Joe Perchesb9a67892014-09-09 21:17:29 -0700559 pr_warn("got v %d < 5 cv %d of ceph_pg_pool\n", ev, cv);
Sage Weil4f6a7e52013-02-23 10:41:09 -0800560 return -EINVAL;
561 }
Ilya Dryomov17a13e402014-01-27 17:40:19 +0200562 if (cv > 9) {
Joe Perchesb9a67892014-09-09 21:17:29 -0700563 pr_warn("got v %d cv %d > 9 of ceph_pg_pool\n", ev, cv);
Sage Weil4f6a7e52013-02-23 10:41:09 -0800564 return -EINVAL;
565 }
566 len = ceph_decode_32(p);
567 ceph_decode_need(p, end, len, bad);
568 pool_end = *p + len;
Sage Weil73a7e692010-08-02 11:00:55 -0700569
Sage Weil4f6a7e52013-02-23 10:41:09 -0800570 pi->type = ceph_decode_8(p);
571 pi->size = ceph_decode_8(p);
572 pi->crush_ruleset = ceph_decode_8(p);
573 pi->object_hash = ceph_decode_8(p);
574
575 pi->pg_num = ceph_decode_32(p);
576 pi->pgp_num = ceph_decode_32(p);
577
578 *p += 4 + 4; /* skip lpg* */
579 *p += 4; /* skip last_change */
580 *p += 8 + 4; /* skip snap_seq, snap_epoch */
581
582 /* skip snaps */
583 num = ceph_decode_32(p);
584 while (num--) {
585 *p += 8; /* snapid key */
586 *p += 1 + 1; /* versions */
587 len = ceph_decode_32(p);
588 *p += len;
Sage Weil73a7e692010-08-02 11:00:55 -0700589 }
590
Ilya Dryomov17a13e402014-01-27 17:40:19 +0200591 /* skip removed_snaps */
Sage Weil4f6a7e52013-02-23 10:41:09 -0800592 num = ceph_decode_32(p);
593 *p += num * (8 + 8);
594
595 *p += 8; /* skip auid */
596 pi->flags = ceph_decode_64(p);
Ilya Dryomov17a13e402014-01-27 17:40:19 +0200597 *p += 4; /* skip crash_replay_interval */
598
599 if (ev >= 7)
Ilya Dryomov04812ac2016-04-28 16:07:23 +0200600 pi->min_size = ceph_decode_8(p);
601 else
602 pi->min_size = pi->size - pi->size / 2;
Ilya Dryomov17a13e402014-01-27 17:40:19 +0200603
604 if (ev >= 8)
605 *p += 8 + 8; /* skip quota_max_* */
606
607 if (ev >= 9) {
608 /* skip tiers */
609 num = ceph_decode_32(p);
610 *p += num * 8;
611
612 *p += 8; /* skip tier_of */
613 *p += 1; /* skip cache_mode */
614
615 pi->read_tier = ceph_decode_64(p);
616 pi->write_tier = ceph_decode_64(p);
617 } else {
618 pi->read_tier = -1;
619 pi->write_tier = -1;
620 }
Sage Weil4f6a7e52013-02-23 10:41:09 -0800621
Ilya Dryomov04812ac2016-04-28 16:07:23 +0200622 if (ev >= 10) {
623 /* skip properties */
624 num = ceph_decode_32(p);
625 while (num--) {
626 len = ceph_decode_32(p);
627 *p += len; /* key */
628 len = ceph_decode_32(p);
629 *p += len; /* val */
630 }
631 }
632
633 if (ev >= 11) {
634 /* skip hit_set_params */
635 *p += 1 + 1; /* versions */
636 len = ceph_decode_32(p);
637 *p += len;
638
639 *p += 4; /* skip hit_set_period */
640 *p += 4; /* skip hit_set_count */
641 }
642
643 if (ev >= 12)
644 *p += 4; /* skip stripe_width */
645
646 if (ev >= 13) {
647 *p += 8; /* skip target_max_bytes */
648 *p += 8; /* skip target_max_objects */
649 *p += 4; /* skip cache_target_dirty_ratio_micro */
650 *p += 4; /* skip cache_target_full_ratio_micro */
651 *p += 4; /* skip cache_min_flush_age */
652 *p += 4; /* skip cache_min_evict_age */
653 }
654
655 if (ev >= 14) {
656 /* skip erasure_code_profile */
657 len = ceph_decode_32(p);
658 *p += len;
659 }
660
661 if (ev >= 15)
662 pi->last_force_request_resend = ceph_decode_32(p);
663 else
664 pi->last_force_request_resend = 0;
665
Sage Weil4f6a7e52013-02-23 10:41:09 -0800666 /* ignore the rest */
667
668 *p = pool_end;
669 calc_pg_masks(pi);
Sage Weil73a7e692010-08-02 11:00:55 -0700670 return 0;
671
672bad:
673 return -EINVAL;
Sage Weilefd75762010-03-17 10:05:28 -0700674}
675
Ilya Dryomov0f70c7e2014-03-21 19:05:27 +0200676static int decode_pool_names(void **p, void *end, struct ceph_osdmap *map)
Sage Weil2844a762010-04-09 15:46:42 -0700677{
678 struct ceph_pg_pool_info *pi;
Sage Weil4f6a7e52013-02-23 10:41:09 -0800679 u32 num, len;
680 u64 pool;
Sage Weil2844a762010-04-09 15:46:42 -0700681
682 ceph_decode_32_safe(p, end, num, bad);
683 dout(" %d pool names\n", num);
684 while (num--) {
Sage Weil4f6a7e52013-02-23 10:41:09 -0800685 ceph_decode_64_safe(p, end, pool, bad);
Sage Weil2844a762010-04-09 15:46:42 -0700686 ceph_decode_32_safe(p, end, len, bad);
Sage Weil4f6a7e52013-02-23 10:41:09 -0800687 dout(" pool %llu len %d\n", pool, len);
Xi Wangad3b9042012-06-06 19:35:55 -0500688 ceph_decode_need(p, end, len, bad);
Sage Weil2844a762010-04-09 15:46:42 -0700689 pi = __lookup_pg_pool(&map->pg_pools, pool);
690 if (pi) {
Xi Wangad3b9042012-06-06 19:35:55 -0500691 char *name = kstrndup(*p, len, GFP_NOFS);
692
693 if (!name)
694 return -ENOMEM;
Sage Weil2844a762010-04-09 15:46:42 -0700695 kfree(pi->name);
Xi Wangad3b9042012-06-06 19:35:55 -0500696 pi->name = name;
697 dout(" name is %s\n", pi->name);
Sage Weil2844a762010-04-09 15:46:42 -0700698 }
699 *p += len;
700 }
701 return 0;
702
703bad:
704 return -EINVAL;
705}
706
707/*
708 * osd map
709 */
Ilya Dryomove5253a72016-04-28 16:07:25 +0200710struct ceph_osdmap *ceph_osdmap_alloc(void)
711{
712 struct ceph_osdmap *map;
713
714 map = kzalloc(sizeof(*map), GFP_NOIO);
715 if (!map)
716 return NULL;
717
718 map->pg_pools = RB_ROOT;
719 map->pool_max = -1;
720 map->pg_temp = RB_ROOT;
721 map->primary_temp = RB_ROOT;
722 mutex_init(&map->crush_scratch_mutex);
723
724 return map;
725}
726
Sage Weil2844a762010-04-09 15:46:42 -0700727void ceph_osdmap_destroy(struct ceph_osdmap *map)
728{
729 dout("osdmap_destroy %p\n", map);
730 if (map->crush)
731 crush_destroy(map->crush);
732 while (!RB_EMPTY_ROOT(&map->pg_temp)) {
733 struct ceph_pg_mapping *pg =
734 rb_entry(rb_first(&map->pg_temp),
735 struct ceph_pg_mapping, node);
736 rb_erase(&pg->node, &map->pg_temp);
737 kfree(pg);
738 }
Ilya Dryomov9686f942014-03-21 19:05:29 +0200739 while (!RB_EMPTY_ROOT(&map->primary_temp)) {
740 struct ceph_pg_mapping *pg =
741 rb_entry(rb_first(&map->primary_temp),
742 struct ceph_pg_mapping, node);
743 rb_erase(&pg->node, &map->primary_temp);
744 kfree(pg);
745 }
Sage Weil2844a762010-04-09 15:46:42 -0700746 while (!RB_EMPTY_ROOT(&map->pg_pools)) {
747 struct ceph_pg_pool_info *pi =
748 rb_entry(rb_first(&map->pg_pools),
749 struct ceph_pg_pool_info, node);
750 __remove_pg_pool(&map->pg_pools, pi);
751 }
752 kfree(map->osd_state);
753 kfree(map->osd_weight);
754 kfree(map->osd_addr);
Ilya Dryomov2cfa34f2014-03-21 19:05:30 +0200755 kfree(map->osd_primary_affinity);
Sage Weil2844a762010-04-09 15:46:42 -0700756 kfree(map);
757}
758
759/*
Ilya Dryomov4d603512014-03-21 19:05:28 +0200760 * Adjust max_osd value, (re)allocate arrays.
761 *
762 * The new elements are properly initialized.
Sage Weil2844a762010-04-09 15:46:42 -0700763 */
764static int osdmap_set_max_osd(struct ceph_osdmap *map, int max)
765{
766 u8 *state;
Sage Weil2844a762010-04-09 15:46:42 -0700767 u32 *weight;
Ilya Dryomov4d603512014-03-21 19:05:28 +0200768 struct ceph_entity_addr *addr;
769 int i;
Sage Weil2844a762010-04-09 15:46:42 -0700770
Ilya Dryomov4d603512014-03-21 19:05:28 +0200771 state = krealloc(map->osd_state, max*sizeof(*state), GFP_NOFS);
Li RongQing589506f2014-09-07 18:10:51 +0800772 if (!state)
Sage Weil2844a762010-04-09 15:46:42 -0700773 return -ENOMEM;
Li RongQing589506f2014-09-07 18:10:51 +0800774 map->osd_state = state;
775
776 weight = krealloc(map->osd_weight, max*sizeof(*weight), GFP_NOFS);
777 if (!weight)
778 return -ENOMEM;
779 map->osd_weight = weight;
780
781 addr = krealloc(map->osd_addr, max*sizeof(*addr), GFP_NOFS);
782 if (!addr)
783 return -ENOMEM;
784 map->osd_addr = addr;
Sage Weil2844a762010-04-09 15:46:42 -0700785
Ilya Dryomov4d603512014-03-21 19:05:28 +0200786 for (i = map->max_osd; i < max; i++) {
Li RongQing589506f2014-09-07 18:10:51 +0800787 map->osd_state[i] = 0;
788 map->osd_weight[i] = CEPH_OSD_OUT;
789 memset(map->osd_addr + i, 0, sizeof(*map->osd_addr));
Sage Weil2844a762010-04-09 15:46:42 -0700790 }
791
Ilya Dryomov2cfa34f2014-03-21 19:05:30 +0200792 if (map->osd_primary_affinity) {
793 u32 *affinity;
794
795 affinity = krealloc(map->osd_primary_affinity,
796 max*sizeof(*affinity), GFP_NOFS);
797 if (!affinity)
798 return -ENOMEM;
Li RongQing589506f2014-09-07 18:10:51 +0800799 map->osd_primary_affinity = affinity;
Ilya Dryomov2cfa34f2014-03-21 19:05:30 +0200800
801 for (i = map->max_osd; i < max; i++)
Li RongQing589506f2014-09-07 18:10:51 +0800802 map->osd_primary_affinity[i] =
803 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
Ilya Dryomov2cfa34f2014-03-21 19:05:30 +0200804 }
805
Sage Weil2844a762010-04-09 15:46:42 -0700806 map->max_osd = max;
Ilya Dryomov4d603512014-03-21 19:05:28 +0200807
Sage Weil2844a762010-04-09 15:46:42 -0700808 return 0;
809}
810
Ilya Dryomovec7af972014-03-21 19:05:29 +0200811#define OSDMAP_WRAPPER_COMPAT_VER 7
812#define OSDMAP_CLIENT_DATA_COMPAT_VER 1
813
814/*
815 * Return 0 or error. On success, *v is set to 0 for old (v6) osdmaps,
816 * to struct_v of the client_data section for new (v7 and above)
817 * osdmaps.
818 */
819static int get_osdmap_client_data_v(void **p, void *end,
820 const char *prefix, u8 *v)
821{
822 u8 struct_v;
823
824 ceph_decode_8_safe(p, end, struct_v, e_inval);
825 if (struct_v >= 7) {
826 u8 struct_compat;
827
828 ceph_decode_8_safe(p, end, struct_compat, e_inval);
829 if (struct_compat > OSDMAP_WRAPPER_COMPAT_VER) {
Joe Perchesb9a67892014-09-09 21:17:29 -0700830 pr_warn("got v %d cv %d > %d of %s ceph_osdmap\n",
831 struct_v, struct_compat,
832 OSDMAP_WRAPPER_COMPAT_VER, prefix);
Ilya Dryomovec7af972014-03-21 19:05:29 +0200833 return -EINVAL;
834 }
835 *p += 4; /* ignore wrapper struct_len */
836
837 ceph_decode_8_safe(p, end, struct_v, e_inval);
838 ceph_decode_8_safe(p, end, struct_compat, e_inval);
839 if (struct_compat > OSDMAP_CLIENT_DATA_COMPAT_VER) {
Joe Perchesb9a67892014-09-09 21:17:29 -0700840 pr_warn("got v %d cv %d > %d of %s ceph_osdmap client data\n",
841 struct_v, struct_compat,
842 OSDMAP_CLIENT_DATA_COMPAT_VER, prefix);
Ilya Dryomovec7af972014-03-21 19:05:29 +0200843 return -EINVAL;
844 }
845 *p += 4; /* ignore client data struct_len */
846 } else {
847 u16 version;
848
849 *p -= 1;
850 ceph_decode_16_safe(p, end, version, e_inval);
851 if (version < 6) {
Joe Perchesb9a67892014-09-09 21:17:29 -0700852 pr_warn("got v %d < 6 of %s ceph_osdmap\n",
853 version, prefix);
Ilya Dryomovec7af972014-03-21 19:05:29 +0200854 return -EINVAL;
855 }
856
857 /* old osdmap enconding */
858 struct_v = 0;
859 }
860
861 *v = struct_v;
862 return 0;
863
864e_inval:
865 return -EINVAL;
866}
867
Ilya Dryomov433fbdd2014-03-21 19:05:27 +0200868static int __decode_pools(void **p, void *end, struct ceph_osdmap *map,
869 bool incremental)
870{
871 u32 n;
872
873 ceph_decode_32_safe(p, end, n, e_inval);
874 while (n--) {
875 struct ceph_pg_pool_info *pi;
876 u64 pool;
877 int ret;
878
879 ceph_decode_64_safe(p, end, pool, e_inval);
880
881 pi = __lookup_pg_pool(&map->pg_pools, pool);
882 if (!incremental || !pi) {
883 pi = kzalloc(sizeof(*pi), GFP_NOFS);
884 if (!pi)
885 return -ENOMEM;
886
887 pi->id = pool;
888
889 ret = __insert_pg_pool(&map->pg_pools, pi);
890 if (ret) {
891 kfree(pi);
892 return ret;
893 }
894 }
895
896 ret = decode_pool(p, end, pi);
897 if (ret)
898 return ret;
899 }
900
901 return 0;
902
903e_inval:
904 return -EINVAL;
905}
906
907static int decode_pools(void **p, void *end, struct ceph_osdmap *map)
908{
909 return __decode_pools(p, end, map, false);
910}
911
912static int decode_new_pools(void **p, void *end, struct ceph_osdmap *map)
913{
914 return __decode_pools(p, end, map, true);
915}
916
Ilya Dryomov10db6342014-03-21 19:05:28 +0200917static int __decode_pg_temp(void **p, void *end, struct ceph_osdmap *map,
918 bool incremental)
919{
920 u32 n;
921
922 ceph_decode_32_safe(p, end, n, e_inval);
923 while (n--) {
924 struct ceph_pg pgid;
925 u32 len, i;
926 int ret;
927
928 ret = ceph_decode_pgid(p, end, &pgid);
929 if (ret)
930 return ret;
931
932 ceph_decode_32_safe(p, end, len, e_inval);
933
934 ret = __remove_pg_mapping(&map->pg_temp, pgid);
935 BUG_ON(!incremental && ret != -ENOENT);
936
937 if (!incremental || len > 0) {
938 struct ceph_pg_mapping *pg;
939
940 ceph_decode_need(p, end, len*sizeof(u32), e_inval);
941
942 if (len > (UINT_MAX - sizeof(*pg)) / sizeof(u32))
943 return -EINVAL;
944
945 pg = kzalloc(sizeof(*pg) + len*sizeof(u32), GFP_NOFS);
946 if (!pg)
947 return -ENOMEM;
948
949 pg->pgid = pgid;
Ilya Dryomov35a935d2014-03-21 19:05:29 +0200950 pg->pg_temp.len = len;
Ilya Dryomov10db6342014-03-21 19:05:28 +0200951 for (i = 0; i < len; i++)
Ilya Dryomov35a935d2014-03-21 19:05:29 +0200952 pg->pg_temp.osds[i] = ceph_decode_32(p);
Ilya Dryomov10db6342014-03-21 19:05:28 +0200953
954 ret = __insert_pg_mapping(pg, &map->pg_temp);
955 if (ret) {
956 kfree(pg);
957 return ret;
958 }
959 }
960 }
961
962 return 0;
963
964e_inval:
965 return -EINVAL;
966}
967
968static int decode_pg_temp(void **p, void *end, struct ceph_osdmap *map)
969{
970 return __decode_pg_temp(p, end, map, false);
971}
972
973static int decode_new_pg_temp(void **p, void *end, struct ceph_osdmap *map)
974{
975 return __decode_pg_temp(p, end, map, true);
976}
977
Ilya Dryomovd286de72014-03-21 19:05:30 +0200978static int __decode_primary_temp(void **p, void *end, struct ceph_osdmap *map,
979 bool incremental)
980{
981 u32 n;
982
983 ceph_decode_32_safe(p, end, n, e_inval);
984 while (n--) {
985 struct ceph_pg pgid;
986 u32 osd;
987 int ret;
988
989 ret = ceph_decode_pgid(p, end, &pgid);
990 if (ret)
991 return ret;
992
993 ceph_decode_32_safe(p, end, osd, e_inval);
994
995 ret = __remove_pg_mapping(&map->primary_temp, pgid);
996 BUG_ON(!incremental && ret != -ENOENT);
997
998 if (!incremental || osd != (u32)-1) {
999 struct ceph_pg_mapping *pg;
1000
1001 pg = kzalloc(sizeof(*pg), GFP_NOFS);
1002 if (!pg)
1003 return -ENOMEM;
1004
1005 pg->pgid = pgid;
1006 pg->primary_temp.osd = osd;
1007
1008 ret = __insert_pg_mapping(pg, &map->primary_temp);
1009 if (ret) {
1010 kfree(pg);
1011 return ret;
1012 }
1013 }
1014 }
1015
1016 return 0;
1017
1018e_inval:
1019 return -EINVAL;
1020}
1021
1022static int decode_primary_temp(void **p, void *end, struct ceph_osdmap *map)
1023{
1024 return __decode_primary_temp(p, end, map, false);
1025}
1026
1027static int decode_new_primary_temp(void **p, void *end,
1028 struct ceph_osdmap *map)
1029{
1030 return __decode_primary_temp(p, end, map, true);
1031}
1032
Ilya Dryomov2cfa34f2014-03-21 19:05:30 +02001033u32 ceph_get_primary_affinity(struct ceph_osdmap *map, int osd)
1034{
1035 BUG_ON(osd >= map->max_osd);
1036
1037 if (!map->osd_primary_affinity)
1038 return CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
1039
1040 return map->osd_primary_affinity[osd];
1041}
1042
1043static int set_primary_affinity(struct ceph_osdmap *map, int osd, u32 aff)
1044{
1045 BUG_ON(osd >= map->max_osd);
1046
1047 if (!map->osd_primary_affinity) {
1048 int i;
1049
1050 map->osd_primary_affinity = kmalloc(map->max_osd*sizeof(u32),
1051 GFP_NOFS);
1052 if (!map->osd_primary_affinity)
1053 return -ENOMEM;
1054
1055 for (i = 0; i < map->max_osd; i++)
1056 map->osd_primary_affinity[i] =
1057 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
1058 }
1059
1060 map->osd_primary_affinity[osd] = aff;
1061
1062 return 0;
1063}
1064
Ilya Dryomov63a69932014-03-21 19:05:30 +02001065static int decode_primary_affinity(void **p, void *end,
1066 struct ceph_osdmap *map)
1067{
1068 u32 len, i;
1069
1070 ceph_decode_32_safe(p, end, len, e_inval);
1071 if (len == 0) {
1072 kfree(map->osd_primary_affinity);
1073 map->osd_primary_affinity = NULL;
1074 return 0;
1075 }
1076 if (len != map->max_osd)
1077 goto e_inval;
1078
1079 ceph_decode_need(p, end, map->max_osd*sizeof(u32), e_inval);
1080
1081 for (i = 0; i < map->max_osd; i++) {
1082 int ret;
1083
1084 ret = set_primary_affinity(map, i, ceph_decode_32(p));
1085 if (ret)
1086 return ret;
1087 }
1088
1089 return 0;
1090
1091e_inval:
1092 return -EINVAL;
1093}
1094
1095static int decode_new_primary_affinity(void **p, void *end,
1096 struct ceph_osdmap *map)
1097{
1098 u32 n;
1099
1100 ceph_decode_32_safe(p, end, n, e_inval);
1101 while (n--) {
1102 u32 osd, aff;
1103 int ret;
1104
1105 ceph_decode_32_safe(p, end, osd, e_inval);
1106 ceph_decode_32_safe(p, end, aff, e_inval);
1107
1108 ret = set_primary_affinity(map, osd, aff);
1109 if (ret)
1110 return ret;
Ilya Dryomovf31da0f2014-04-02 20:34:04 +04001111
1112 pr_info("osd%d primary-affinity 0x%x\n", osd, aff);
Ilya Dryomov63a69932014-03-21 19:05:30 +02001113 }
1114
1115 return 0;
1116
1117e_inval:
1118 return -EINVAL;
1119}
1120
Sage Weil4fc51be2010-02-16 15:55:03 -08001121/*
Sage Weilf24e9982009-10-06 11:31:10 -07001122 * decode a full map.
1123 */
Ilya Dryomova2505d62014-03-13 16:36:13 +02001124static int osdmap_decode(void **p, void *end, struct ceph_osdmap *map)
Sage Weilf24e9982009-10-06 11:31:10 -07001125{
Ilya Dryomovec7af972014-03-21 19:05:29 +02001126 u8 struct_v;
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001127 u32 epoch = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001128 void *start = *p;
Ilya Dryomov39770582014-03-13 16:36:14 +02001129 u32 max;
1130 u32 len, i;
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001131 int err;
Sage Weilf24e9982009-10-06 11:31:10 -07001132
Ilya Dryomova2505d62014-03-13 16:36:13 +02001133 dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p));
Sage Weilf24e9982009-10-06 11:31:10 -07001134
Ilya Dryomovec7af972014-03-21 19:05:29 +02001135 err = get_osdmap_client_data_v(p, end, "full", &struct_v);
1136 if (err)
1137 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001138
Ilya Dryomov53bbaba2014-03-13 16:36:16 +02001139 /* fsid, epoch, created, modified */
1140 ceph_decode_need(p, end, sizeof(map->fsid) + sizeof(u32) +
1141 sizeof(map->created) + sizeof(map->modified), e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001142 ceph_decode_copy(p, &map->fsid, sizeof(map->fsid));
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001143 epoch = map->epoch = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -07001144 ceph_decode_copy(p, &map->created, sizeof(map->created));
1145 ceph_decode_copy(p, &map->modified, sizeof(map->modified));
1146
Ilya Dryomov433fbdd2014-03-21 19:05:27 +02001147 /* pools */
1148 err = decode_pools(p, end, map);
1149 if (err)
1150 goto bad;
Sage Weil2844a762010-04-09 15:46:42 -07001151
Ilya Dryomov0f70c7e2014-03-21 19:05:27 +02001152 /* pool_name */
1153 err = decode_pool_names(p, end, map);
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001154 if (err)
Sage Weil4f6a7e52013-02-23 10:41:09 -08001155 goto bad;
Sage Weil2844a762010-04-09 15:46:42 -07001156
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001157 ceph_decode_32_safe(p, end, map->pool_max, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001158
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001159 ceph_decode_32_safe(p, end, map->flags, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001160
Ilya Dryomov39770582014-03-13 16:36:14 +02001161 /* max_osd */
1162 ceph_decode_32_safe(p, end, max, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001163
1164 /* (re)alloc osd arrays */
1165 err = osdmap_set_max_osd(map, max);
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001166 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001167 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001168
Ilya Dryomov2d88b2e2014-03-13 16:36:14 +02001169 /* osd_state, osd_weight, osd_addrs->client_addr */
Sage Weilf24e9982009-10-06 11:31:10 -07001170 ceph_decode_need(p, end, 3*sizeof(u32) +
1171 map->max_osd*(1 + sizeof(*map->osd_weight) +
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001172 sizeof(*map->osd_addr)), e_inval);
1173
Ilya Dryomov2d88b2e2014-03-13 16:36:14 +02001174 if (ceph_decode_32(p) != map->max_osd)
1175 goto e_inval;
1176
Sage Weilf24e9982009-10-06 11:31:10 -07001177 ceph_decode_copy(p, map->osd_state, map->max_osd);
1178
Ilya Dryomov2d88b2e2014-03-13 16:36:14 +02001179 if (ceph_decode_32(p) != map->max_osd)
1180 goto e_inval;
1181
Sage Weilf24e9982009-10-06 11:31:10 -07001182 for (i = 0; i < map->max_osd; i++)
Sage Weilc89136e2009-10-14 09:59:09 -07001183 map->osd_weight[i] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -07001184
Ilya Dryomov2d88b2e2014-03-13 16:36:14 +02001185 if (ceph_decode_32(p) != map->max_osd)
1186 goto e_inval;
1187
Sage Weilf24e9982009-10-06 11:31:10 -07001188 ceph_decode_copy(p, map->osd_addr, map->max_osd*sizeof(*map->osd_addr));
Sage Weil63f2d212009-11-03 15:17:56 -08001189 for (i = 0; i < map->max_osd; i++)
1190 ceph_decode_addr(&map->osd_addr[i]);
Sage Weilf24e9982009-10-06 11:31:10 -07001191
1192 /* pg_temp */
Ilya Dryomov10db6342014-03-21 19:05:28 +02001193 err = decode_pg_temp(p, end, map);
1194 if (err)
1195 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001196
Ilya Dryomovd286de72014-03-21 19:05:30 +02001197 /* primary_temp */
1198 if (struct_v >= 1) {
1199 err = decode_primary_temp(p, end, map);
1200 if (err)
1201 goto bad;
1202 }
1203
Ilya Dryomov63a69932014-03-21 19:05:30 +02001204 /* primary_affinity */
1205 if (struct_v >= 2) {
1206 err = decode_primary_affinity(p, end, map);
1207 if (err)
1208 goto bad;
1209 } else {
1210 /* XXX can this happen? */
1211 kfree(map->osd_primary_affinity);
1212 map->osd_primary_affinity = NULL;
1213 }
1214
Sage Weilf24e9982009-10-06 11:31:10 -07001215 /* crush */
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001216 ceph_decode_32_safe(p, end, len, e_inval);
Ilya Dryomov9902e682014-03-13 16:36:15 +02001217 map->crush = crush_decode(*p, min(*p + len, end));
Sage Weilf24e9982009-10-06 11:31:10 -07001218 if (IS_ERR(map->crush)) {
1219 err = PTR_ERR(map->crush);
1220 map->crush = NULL;
1221 goto bad;
1222 }
Ilya Dryomov9902e682014-03-13 16:36:15 +02001223 *p += len;
Sage Weilf24e9982009-10-06 11:31:10 -07001224
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001225 /* ignore the rest */
Sage Weilf24e9982009-10-06 11:31:10 -07001226 *p = end;
1227
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001228 dout("full osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd);
Ilya Dryomova2505d62014-03-13 16:36:13 +02001229 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001230
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001231e_inval:
1232 err = -EINVAL;
Sage Weilf24e9982009-10-06 11:31:10 -07001233bad:
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001234 pr_err("corrupt full osdmap (%d) epoch %d off %d (%p of %p-%p)\n",
1235 err, epoch, (int)(*p - start), *p, start, end);
1236 print_hex_dump(KERN_DEBUG, "osdmap: ",
1237 DUMP_PREFIX_OFFSET, 16, 1,
1238 start, end - start, true);
Ilya Dryomova2505d62014-03-13 16:36:13 +02001239 return err;
1240}
1241
1242/*
1243 * Allocate and decode a full map.
1244 */
1245struct ceph_osdmap *ceph_osdmap_decode(void **p, void *end)
1246{
1247 struct ceph_osdmap *map;
1248 int ret;
1249
Ilya Dryomove5253a72016-04-28 16:07:25 +02001250 map = ceph_osdmap_alloc();
Ilya Dryomova2505d62014-03-13 16:36:13 +02001251 if (!map)
1252 return ERR_PTR(-ENOMEM);
1253
Ilya Dryomova2505d62014-03-13 16:36:13 +02001254 ret = osdmap_decode(p, end, map);
1255 if (ret) {
1256 ceph_osdmap_destroy(map);
1257 return ERR_PTR(ret);
1258 }
1259
1260 return map;
Sage Weilf24e9982009-10-06 11:31:10 -07001261}
1262
1263/*
Ilya Dryomov930c5322016-07-19 03:50:28 +02001264 * Encoding order is (new_up_client, new_state, new_weight). Need to
1265 * apply in the (new_weight, new_state, new_up_client) order, because
1266 * an incremental map may look like e.g.
1267 *
1268 * new_up_client: { osd=6, addr=... } # set osd_state and addr
1269 * new_state: { osd=6, xorstate=EXISTS } # clear osd_state
1270 */
1271static int decode_new_up_state_weight(void **p, void *end,
1272 struct ceph_osdmap *map)
1273{
1274 void *new_up_client;
1275 void *new_state;
1276 void *new_weight_end;
1277 u32 len;
1278
1279 new_up_client = *p;
1280 ceph_decode_32_safe(p, end, len, e_inval);
1281 len *= sizeof(u32) + sizeof(struct ceph_entity_addr);
1282 ceph_decode_need(p, end, len, e_inval);
1283 *p += len;
1284
1285 new_state = *p;
1286 ceph_decode_32_safe(p, end, len, e_inval);
1287 len *= sizeof(u32) + sizeof(u8);
1288 ceph_decode_need(p, end, len, e_inval);
1289 *p += len;
1290
1291 /* new_weight */
1292 ceph_decode_32_safe(p, end, len, e_inval);
1293 while (len--) {
1294 s32 osd;
1295 u32 w;
1296
1297 ceph_decode_need(p, end, 2*sizeof(u32), e_inval);
1298 osd = ceph_decode_32(p);
1299 w = ceph_decode_32(p);
1300 BUG_ON(osd >= map->max_osd);
1301 pr_info("osd%d weight 0x%x %s\n", osd, w,
1302 w == CEPH_OSD_IN ? "(in)" :
1303 (w == CEPH_OSD_OUT ? "(out)" : ""));
1304 map->osd_weight[osd] = w;
1305
1306 /*
1307 * If we are marking in, set the EXISTS, and clear the
1308 * AUTOOUT and NEW bits.
1309 */
1310 if (w) {
1311 map->osd_state[osd] |= CEPH_OSD_EXISTS;
1312 map->osd_state[osd] &= ~(CEPH_OSD_AUTOOUT |
1313 CEPH_OSD_NEW);
1314 }
1315 }
1316 new_weight_end = *p;
1317
1318 /* new_state (up/down) */
1319 *p = new_state;
1320 len = ceph_decode_32(p);
1321 while (len--) {
1322 s32 osd;
1323 u8 xorstate;
1324 int ret;
1325
1326 osd = ceph_decode_32(p);
1327 xorstate = ceph_decode_8(p);
1328 if (xorstate == 0)
1329 xorstate = CEPH_OSD_UP;
1330 BUG_ON(osd >= map->max_osd);
1331 if ((map->osd_state[osd] & CEPH_OSD_UP) &&
1332 (xorstate & CEPH_OSD_UP))
1333 pr_info("osd%d down\n", osd);
1334 if ((map->osd_state[osd] & CEPH_OSD_EXISTS) &&
1335 (xorstate & CEPH_OSD_EXISTS)) {
1336 pr_info("osd%d does not exist\n", osd);
1337 map->osd_weight[osd] = CEPH_OSD_IN;
1338 ret = set_primary_affinity(map, osd,
1339 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY);
1340 if (ret)
1341 return ret;
1342 memset(map->osd_addr + osd, 0, sizeof(*map->osd_addr));
1343 map->osd_state[osd] = 0;
1344 } else {
1345 map->osd_state[osd] ^= xorstate;
1346 }
1347 }
1348
1349 /* new_up_client */
1350 *p = new_up_client;
1351 len = ceph_decode_32(p);
1352 while (len--) {
1353 s32 osd;
1354 struct ceph_entity_addr addr;
1355
1356 osd = ceph_decode_32(p);
1357 ceph_decode_copy(p, &addr, sizeof(addr));
1358 ceph_decode_addr(&addr);
1359 BUG_ON(osd >= map->max_osd);
1360 pr_info("osd%d up\n", osd);
1361 map->osd_state[osd] |= CEPH_OSD_EXISTS | CEPH_OSD_UP;
1362 map->osd_addr[osd] = addr;
1363 }
1364
1365 *p = new_weight_end;
1366 return 0;
1367
1368e_inval:
1369 return -EINVAL;
1370}
1371
1372/*
Sage Weilf24e9982009-10-06 11:31:10 -07001373 * decode and apply an incremental map update.
1374 */
1375struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end,
Ilya Dryomov0c0a8de2016-04-28 16:07:21 +02001376 struct ceph_osdmap *map)
Sage Weilf24e9982009-10-06 11:31:10 -07001377{
Sage Weilf24e9982009-10-06 11:31:10 -07001378 struct crush_map *newcrush = NULL;
1379 struct ceph_fsid fsid;
1380 u32 epoch = 0;
1381 struct ceph_timespec modified;
Sage Weil4f6a7e52013-02-23 10:41:09 -08001382 s32 len;
1383 u64 pool;
1384 __s64 new_pool_max;
1385 __s32 new_flags, max;
Sage Weilf24e9982009-10-06 11:31:10 -07001386 void *start = *p;
Ilya Dryomov86f17422014-03-13 16:36:15 +02001387 int err;
Ilya Dryomovec7af972014-03-21 19:05:29 +02001388 u8 struct_v;
Sage Weilf24e9982009-10-06 11:31:10 -07001389
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001390 dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p));
1391
Ilya Dryomovec7af972014-03-21 19:05:29 +02001392 err = get_osdmap_client_data_v(p, end, "inc", &struct_v);
1393 if (err)
1394 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001395
Ilya Dryomov53bbaba2014-03-13 16:36:16 +02001396 /* fsid, epoch, modified, new_pool_max, new_flags */
1397 ceph_decode_need(p, end, sizeof(fsid) + sizeof(u32) + sizeof(modified) +
1398 sizeof(u64) + sizeof(u32), e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001399 ceph_decode_copy(p, &fsid, sizeof(fsid));
Sage Weilc89136e2009-10-14 09:59:09 -07001400 epoch = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -07001401 BUG_ON(epoch != map->epoch+1);
1402 ceph_decode_copy(p, &modified, sizeof(modified));
Sage Weil4f6a7e52013-02-23 10:41:09 -08001403 new_pool_max = ceph_decode_64(p);
Sage Weilc89136e2009-10-14 09:59:09 -07001404 new_flags = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -07001405
1406 /* full map? */
Ilya Dryomov86f17422014-03-13 16:36:15 +02001407 ceph_decode_32_safe(p, end, len, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001408 if (len > 0) {
1409 dout("apply_incremental full map len %d, %p to %p\n",
1410 len, *p, end);
Ilya Dryomova2505d62014-03-13 16:36:13 +02001411 return ceph_osdmap_decode(p, min(*p+len, end));
Sage Weilf24e9982009-10-06 11:31:10 -07001412 }
1413
1414 /* new crush? */
Ilya Dryomov86f17422014-03-13 16:36:15 +02001415 ceph_decode_32_safe(p, end, len, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001416 if (len > 0) {
Sage Weilf24e9982009-10-06 11:31:10 -07001417 newcrush = crush_decode(*p, min(*p+len, end));
Ilya Dryomov86f17422014-03-13 16:36:15 +02001418 if (IS_ERR(newcrush)) {
1419 err = PTR_ERR(newcrush);
1420 newcrush = NULL;
1421 goto bad;
1422 }
Sage Weilcebc5be2010-06-17 10:22:48 -07001423 *p += len;
Sage Weilf24e9982009-10-06 11:31:10 -07001424 }
1425
1426 /* new flags? */
1427 if (new_flags >= 0)
1428 map->flags = new_flags;
Sage Weil4fc51be2010-02-16 15:55:03 -08001429 if (new_pool_max >= 0)
1430 map->pool_max = new_pool_max;
Sage Weilf24e9982009-10-06 11:31:10 -07001431
Sage Weilf24e9982009-10-06 11:31:10 -07001432 /* new max? */
Ilya Dryomov53bbaba2014-03-13 16:36:16 +02001433 ceph_decode_32_safe(p, end, max, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001434 if (max >= 0) {
1435 err = osdmap_set_max_osd(map, max);
Ilya Dryomov86f17422014-03-13 16:36:15 +02001436 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001437 goto bad;
1438 }
1439
1440 map->epoch++;
Sage Weil31456662011-05-12 15:18:43 -07001441 map->modified = modified;
Sage Weilf24e9982009-10-06 11:31:10 -07001442 if (newcrush) {
1443 if (map->crush)
1444 crush_destroy(map->crush);
1445 map->crush = newcrush;
1446 newcrush = NULL;
1447 }
1448
Ilya Dryomov433fbdd2014-03-21 19:05:27 +02001449 /* new_pools */
1450 err = decode_new_pools(p, end, map);
1451 if (err)
1452 goto bad;
Ilya Dryomov9464d002014-03-13 16:36:16 +02001453
Ilya Dryomov0f70c7e2014-03-21 19:05:27 +02001454 /* new_pool_names */
1455 err = decode_pool_names(p, end, map);
Ilya Dryomov9464d002014-03-13 16:36:16 +02001456 if (err)
1457 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001458
Sage Weil4fc51be2010-02-16 15:55:03 -08001459 /* old_pool */
Ilya Dryomov86f17422014-03-13 16:36:15 +02001460 ceph_decode_32_safe(p, end, len, e_inval);
Sage Weil4fc51be2010-02-16 15:55:03 -08001461 while (len--) {
1462 struct ceph_pg_pool_info *pi;
1463
Ilya Dryomov86f17422014-03-13 16:36:15 +02001464 ceph_decode_64_safe(p, end, pool, e_inval);
Sage Weil4fc51be2010-02-16 15:55:03 -08001465 pi = __lookup_pg_pool(&map->pg_pools, pool);
Sage Weil2844a762010-04-09 15:46:42 -07001466 if (pi)
1467 __remove_pg_pool(&map->pg_pools, pi);
Sage Weil4fc51be2010-02-16 15:55:03 -08001468 }
Sage Weilf24e9982009-10-06 11:31:10 -07001469
Ilya Dryomov930c5322016-07-19 03:50:28 +02001470 /* new_up_client, new_state, new_weight */
1471 err = decode_new_up_state_weight(p, end, map);
1472 if (err)
1473 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001474
1475 /* new_pg_temp */
Ilya Dryomov10db6342014-03-21 19:05:28 +02001476 err = decode_new_pg_temp(p, end, map);
1477 if (err)
1478 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001479
Ilya Dryomovd286de72014-03-21 19:05:30 +02001480 /* new_primary_temp */
1481 if (struct_v >= 1) {
1482 err = decode_new_primary_temp(p, end, map);
1483 if (err)
1484 goto bad;
1485 }
1486
Ilya Dryomov63a69932014-03-21 19:05:30 +02001487 /* new_primary_affinity */
1488 if (struct_v >= 2) {
1489 err = decode_new_primary_affinity(p, end, map);
1490 if (err)
1491 goto bad;
1492 }
1493
Sage Weilf24e9982009-10-06 11:31:10 -07001494 /* ignore the rest */
1495 *p = end;
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001496
1497 dout("inc osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001498 return map;
1499
Ilya Dryomov86f17422014-03-13 16:36:15 +02001500e_inval:
1501 err = -EINVAL;
Sage Weilf24e9982009-10-06 11:31:10 -07001502bad:
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001503 pr_err("corrupt inc osdmap (%d) epoch %d off %d (%p of %p-%p)\n",
1504 err, epoch, (int)(*p - start), *p, start, end);
Sage Weil9ec7cab2009-12-14 15:13:47 -08001505 print_hex_dump(KERN_DEBUG, "osdmap: ",
1506 DUMP_PREFIX_OFFSET, 16, 1,
1507 start, end - start, true);
Sage Weilf24e9982009-10-06 11:31:10 -07001508 if (newcrush)
1509 crush_destroy(newcrush);
1510 return ERR_PTR(err);
1511}
1512
Ilya Dryomovd30291b2016-04-29 19:54:20 +02001513void ceph_oid_copy(struct ceph_object_id *dest,
1514 const struct ceph_object_id *src)
1515{
1516 WARN_ON(!ceph_oid_empty(dest));
Sage Weilf24e9982009-10-06 11:31:10 -07001517
Ilya Dryomovd30291b2016-04-29 19:54:20 +02001518 if (src->name != src->inline_name) {
1519 /* very rare, see ceph_object_id definition */
1520 dest->name = kmalloc(src->name_len + 1,
1521 GFP_NOIO | __GFP_NOFAIL);
1522 }
Sage Weilf24e9982009-10-06 11:31:10 -07001523
Ilya Dryomovd30291b2016-04-29 19:54:20 +02001524 memcpy(dest->name, src->name, src->name_len + 1);
1525 dest->name_len = src->name_len;
1526}
1527EXPORT_SYMBOL(ceph_oid_copy);
1528
1529static __printf(2, 0)
1530int oid_printf_vargs(struct ceph_object_id *oid, const char *fmt, va_list ap)
1531{
1532 int len;
1533
1534 WARN_ON(!ceph_oid_empty(oid));
1535
1536 len = vsnprintf(oid->inline_name, sizeof(oid->inline_name), fmt, ap);
1537 if (len >= sizeof(oid->inline_name))
1538 return len;
1539
1540 oid->name_len = len;
1541 return 0;
1542}
1543
1544/*
1545 * If oid doesn't fit into inline buffer, BUG.
1546 */
1547void ceph_oid_printf(struct ceph_object_id *oid, const char *fmt, ...)
1548{
1549 va_list ap;
1550
1551 va_start(ap, fmt);
1552 BUG_ON(oid_printf_vargs(oid, fmt, ap));
1553 va_end(ap);
1554}
1555EXPORT_SYMBOL(ceph_oid_printf);
1556
1557static __printf(3, 0)
1558int oid_aprintf_vargs(struct ceph_object_id *oid, gfp_t gfp,
1559 const char *fmt, va_list ap)
1560{
1561 va_list aq;
1562 int len;
1563
1564 va_copy(aq, ap);
1565 len = oid_printf_vargs(oid, fmt, aq);
1566 va_end(aq);
1567
1568 if (len) {
1569 char *external_name;
1570
1571 external_name = kmalloc(len + 1, gfp);
1572 if (!external_name)
1573 return -ENOMEM;
1574
1575 oid->name = external_name;
1576 WARN_ON(vsnprintf(oid->name, len + 1, fmt, ap) != len);
1577 oid->name_len = len;
1578 }
1579
1580 return 0;
1581}
1582
1583/*
1584 * If oid doesn't fit into inline buffer, allocate.
1585 */
1586int ceph_oid_aprintf(struct ceph_object_id *oid, gfp_t gfp,
1587 const char *fmt, ...)
1588{
1589 va_list ap;
1590 int ret;
1591
1592 va_start(ap, fmt);
1593 ret = oid_aprintf_vargs(oid, gfp, fmt, ap);
1594 va_end(ap);
1595
1596 return ret;
1597}
1598EXPORT_SYMBOL(ceph_oid_aprintf);
1599
1600void ceph_oid_destroy(struct ceph_object_id *oid)
1601{
1602 if (oid->name != oid->inline_name)
1603 kfree(oid->name);
1604}
1605EXPORT_SYMBOL(ceph_oid_destroy);
Sage Weilf24e9982009-10-06 11:31:10 -07001606
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001607/*
1608 * osds only
1609 */
1610static bool __osds_equal(const struct ceph_osds *lhs,
1611 const struct ceph_osds *rhs)
1612{
1613 if (lhs->size == rhs->size &&
1614 !memcmp(lhs->osds, rhs->osds, rhs->size * sizeof(rhs->osds[0])))
1615 return true;
1616
1617 return false;
1618}
1619
1620/*
1621 * osds + primary
1622 */
1623static bool osds_equal(const struct ceph_osds *lhs,
1624 const struct ceph_osds *rhs)
1625{
1626 if (__osds_equal(lhs, rhs) &&
1627 lhs->primary == rhs->primary)
1628 return true;
1629
1630 return false;
1631}
1632
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02001633static bool osds_valid(const struct ceph_osds *set)
1634{
1635 /* non-empty set */
1636 if (set->size > 0 && set->primary >= 0)
1637 return true;
1638
1639 /* empty can_shift_osds set */
1640 if (!set->size && set->primary == -1)
1641 return true;
1642
1643 /* empty !can_shift_osds set - all NONE */
1644 if (set->size > 0 && set->primary == -1) {
1645 int i;
1646
1647 for (i = 0; i < set->size; i++) {
1648 if (set->osds[i] != CRUSH_ITEM_NONE)
1649 break;
1650 }
1651 if (i == set->size)
1652 return true;
1653 }
1654
1655 return false;
1656}
1657
1658void ceph_osds_copy(struct ceph_osds *dest, const struct ceph_osds *src)
1659{
1660 memcpy(dest->osds, src->osds, src->size * sizeof(src->osds[0]));
1661 dest->size = src->size;
1662 dest->primary = src->primary;
1663}
1664
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001665static bool is_split(const struct ceph_pg *pgid,
1666 u32 old_pg_num,
1667 u32 new_pg_num)
1668{
1669 int old_bits = calc_bits_of(old_pg_num);
1670 int old_mask = (1 << old_bits) - 1;
1671 int n;
1672
1673 WARN_ON(pgid->seed >= old_pg_num);
1674 if (new_pg_num <= old_pg_num)
1675 return false;
1676
1677 for (n = 1; ; n++) {
1678 int next_bit = n << (old_bits - 1);
1679 u32 s = next_bit | pgid->seed;
1680
1681 if (s < old_pg_num || s == pgid->seed)
1682 continue;
1683 if (s >= new_pg_num)
1684 break;
1685
1686 s = ceph_stable_mod(s, old_pg_num, old_mask);
1687 if (s == pgid->seed)
1688 return true;
1689 }
1690
1691 return false;
1692}
1693
1694bool ceph_is_new_interval(const struct ceph_osds *old_acting,
1695 const struct ceph_osds *new_acting,
1696 const struct ceph_osds *old_up,
1697 const struct ceph_osds *new_up,
1698 int old_size,
1699 int new_size,
1700 int old_min_size,
1701 int new_min_size,
1702 u32 old_pg_num,
1703 u32 new_pg_num,
1704 bool old_sort_bitwise,
1705 bool new_sort_bitwise,
1706 const struct ceph_pg *pgid)
1707{
1708 return !osds_equal(old_acting, new_acting) ||
1709 !osds_equal(old_up, new_up) ||
1710 old_size != new_size ||
1711 old_min_size != new_min_size ||
1712 is_split(pgid, old_pg_num, new_pg_num) ||
1713 old_sort_bitwise != new_sort_bitwise;
1714}
1715
1716static int calc_pg_rank(int osd, const struct ceph_osds *acting)
1717{
1718 int i;
1719
1720 for (i = 0; i < acting->size; i++) {
1721 if (acting->osds[i] == osd)
1722 return i;
1723 }
1724
1725 return -1;
1726}
1727
1728static bool primary_changed(const struct ceph_osds *old_acting,
1729 const struct ceph_osds *new_acting)
1730{
1731 if (!old_acting->size && !new_acting->size)
1732 return false; /* both still empty */
1733
1734 if (!old_acting->size ^ !new_acting->size)
1735 return true; /* was empty, now not, or vice versa */
1736
1737 if (old_acting->primary != new_acting->primary)
1738 return true; /* primary changed */
1739
1740 if (calc_pg_rank(old_acting->primary, old_acting) !=
1741 calc_pg_rank(new_acting->primary, new_acting))
1742 return true;
1743
1744 return false; /* same primary (tho replicas may have changed) */
1745}
1746
1747bool ceph_osds_changed(const struct ceph_osds *old_acting,
1748 const struct ceph_osds *new_acting,
1749 bool any_change)
1750{
1751 if (primary_changed(old_acting, new_acting))
1752 return true;
1753
1754 if (any_change && !__osds_equal(old_acting, new_acting))
1755 return true;
1756
1757 return false;
1758}
1759
Sage Weilf24e9982009-10-06 11:31:10 -07001760/*
1761 * calculate file layout from given offset, length.
1762 * fill in correct oid, logical length, and object extent
1763 * offset, length.
1764 *
1765 * for now, we write only a single su, until we can
1766 * pass a stride back to the caller.
1767 */
Sage Weild63b77f2012-09-24 20:59:48 -07001768int ceph_calc_file_object_mapping(struct ceph_file_layout *layout,
Alex Eldere8afad62012-11-14 09:38:19 -06001769 u64 off, u64 len,
Sage Weil645a1022009-10-28 15:15:05 -07001770 u64 *ono,
Sage Weilf24e9982009-10-06 11:31:10 -07001771 u64 *oxoff, u64 *oxlen)
1772{
1773 u32 osize = le32_to_cpu(layout->fl_object_size);
1774 u32 su = le32_to_cpu(layout->fl_stripe_unit);
1775 u32 sc = le32_to_cpu(layout->fl_stripe_count);
1776 u32 bl, stripeno, stripepos, objsetno;
1777 u32 su_per_object;
Noah Watkinsff1d1f72009-10-30 12:57:30 -07001778 u64 t, su_offset;
Sage Weilf24e9982009-10-06 11:31:10 -07001779
Alex Eldere8afad62012-11-14 09:38:19 -06001780 dout("mapping %llu~%llu osize %u fl_su %u\n", off, len,
Sage Weilf24e9982009-10-06 11:31:10 -07001781 osize, su);
Sage Weild63b77f2012-09-24 20:59:48 -07001782 if (su == 0 || sc == 0)
1783 goto invalid;
Noah Watkins35e054a2009-10-28 14:04:48 -07001784 su_per_object = osize / su;
Sage Weild63b77f2012-09-24 20:59:48 -07001785 if (su_per_object == 0)
1786 goto invalid;
Sage Weilf24e9982009-10-06 11:31:10 -07001787 dout("osize %u / su %u = su_per_object %u\n", osize, su,
1788 su_per_object);
1789
Sage Weild63b77f2012-09-24 20:59:48 -07001790 if ((su & ~PAGE_MASK) != 0)
1791 goto invalid;
1792
Sage Weilf24e9982009-10-06 11:31:10 -07001793 /* bl = *off / su; */
1794 t = off;
1795 do_div(t, su);
1796 bl = t;
1797 dout("off %llu / su %u = bl %u\n", off, su, bl);
1798
1799 stripeno = bl / sc;
1800 stripepos = bl % sc;
1801 objsetno = stripeno / su_per_object;
1802
Sage Weil645a1022009-10-28 15:15:05 -07001803 *ono = objsetno * sc + stripepos;
Eric Dumazet95c96172012-04-15 05:58:06 +00001804 dout("objset %u * sc %u = ono %u\n", objsetno, sc, (unsigned int)*ono);
Sage Weil645a1022009-10-28 15:15:05 -07001805
1806 /* *oxoff = *off % layout->fl_stripe_unit; # offset in su */
Sage Weilf24e9982009-10-06 11:31:10 -07001807 t = off;
Noah Watkinsff1d1f72009-10-30 12:57:30 -07001808 su_offset = do_div(t, su);
1809 *oxoff = su_offset + (stripeno % su_per_object) * su;
Sage Weil645a1022009-10-28 15:15:05 -07001810
Noah Watkinsff1d1f72009-10-30 12:57:30 -07001811 /*
1812 * Calculate the length of the extent being written to the selected
Alex Eldere8afad62012-11-14 09:38:19 -06001813 * object. This is the minimum of the full length requested (len) or
Noah Watkinsff1d1f72009-10-30 12:57:30 -07001814 * the remainder of the current stripe being written to.
1815 */
Alex Eldere8afad62012-11-14 09:38:19 -06001816 *oxlen = min_t(u64, len, su - su_offset);
Sage Weilf24e9982009-10-06 11:31:10 -07001817
1818 dout(" obj extent %llu~%llu\n", *oxoff, *oxlen);
Sage Weild63b77f2012-09-24 20:59:48 -07001819 return 0;
1820
1821invalid:
1822 dout(" invalid layout\n");
1823 *ono = 0;
1824 *oxoff = 0;
1825 *oxlen = 0;
1826 return -EINVAL;
Sage Weilf24e9982009-10-06 11:31:10 -07001827}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001828EXPORT_SYMBOL(ceph_calc_file_object_mapping);
Sage Weilf24e9982009-10-06 11:31:10 -07001829
1830/*
Ilya Dryomovd9591f52016-04-28 16:07:22 +02001831 * Map an object into a PG.
1832 *
1833 * Should only be called with target_oid and target_oloc (as opposed to
1834 * base_oid and base_oloc), since tiering isn't taken into account.
Sage Weilf24e9982009-10-06 11:31:10 -07001835 */
Ilya Dryomovd9591f52016-04-28 16:07:22 +02001836int ceph_object_locator_to_pg(struct ceph_osdmap *osdmap,
1837 struct ceph_object_id *oid,
1838 struct ceph_object_locator *oloc,
1839 struct ceph_pg *raw_pgid)
Sage Weilf24e9982009-10-06 11:31:10 -07001840{
Ilya Dryomov7c13cb62014-01-27 17:40:19 +02001841 struct ceph_pg_pool_info *pi;
Sage Weilf24e9982009-10-06 11:31:10 -07001842
Ilya Dryomovd9591f52016-04-28 16:07:22 +02001843 pi = ceph_pg_pool_by_id(osdmap, oloc->pool);
Ilya Dryomov7c13cb62014-01-27 17:40:19 +02001844 if (!pi)
Ilya Dryomovd9591f52016-04-28 16:07:22 +02001845 return -ENOENT;
Sage Weilf24e9982009-10-06 11:31:10 -07001846
Ilya Dryomovd9591f52016-04-28 16:07:22 +02001847 raw_pgid->pool = oloc->pool;
1848 raw_pgid->seed = ceph_str_hash(pi->object_hash, oid->name,
1849 oid->name_len);
Ilya Dryomov7c13cb62014-01-27 17:40:19 +02001850
Ilya Dryomov4a3262b2016-05-30 18:33:32 +02001851 dout("%s %s -> raw_pgid %llu.%x\n", __func__, oid->name,
1852 raw_pgid->pool, raw_pgid->seed);
Sage Weilf24e9982009-10-06 11:31:10 -07001853 return 0;
1854}
Ilya Dryomovd9591f52016-04-28 16:07:22 +02001855EXPORT_SYMBOL(ceph_object_locator_to_pg);
Sage Weilf24e9982009-10-06 11:31:10 -07001856
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02001857/*
1858 * Map a raw PG (full precision ps) into an actual PG.
1859 */
1860static void raw_pg_to_pg(struct ceph_pg_pool_info *pi,
1861 const struct ceph_pg *raw_pgid,
1862 struct ceph_pg *pgid)
1863{
1864 pgid->pool = raw_pgid->pool;
1865 pgid->seed = ceph_stable_mod(raw_pgid->seed, pi->pg_num,
1866 pi->pg_num_mask);
1867}
1868
1869/*
1870 * Map a raw PG (full precision ps) into a placement ps (placement
1871 * seed). Include pool id in that value so that different pools don't
1872 * use the same seeds.
1873 */
1874static u32 raw_pg_to_pps(struct ceph_pg_pool_info *pi,
1875 const struct ceph_pg *raw_pgid)
1876{
1877 if (pi->flags & CEPH_POOL_FLAG_HASHPSPOOL) {
1878 /* hash pool id and seed so that pool PGs do not overlap */
1879 return crush_hash32_2(CRUSH_HASH_RJENKINS1,
1880 ceph_stable_mod(raw_pgid->seed,
1881 pi->pgp_num,
1882 pi->pgp_num_mask),
1883 raw_pgid->pool);
1884 } else {
1885 /*
1886 * legacy behavior: add ps and pool together. this is
1887 * not a great approach because the PGs from each pool
1888 * will overlap on top of each other: 0.5 == 1.4 ==
1889 * 2.3 == ...
1890 */
1891 return ceph_stable_mod(raw_pgid->seed, pi->pgp_num,
1892 pi->pgp_num_mask) +
1893 (unsigned)raw_pgid->pool;
1894 }
1895}
1896
Ilya Dryomov9d521472014-01-31 17:54:26 +02001897static int do_crush(struct ceph_osdmap *map, int ruleno, int x,
1898 int *result, int result_max,
1899 const __u32 *weight, int weight_max)
Ilya Dryomove8ef19c2013-12-24 21:19:24 +02001900{
Ilya Dryomov9d521472014-01-31 17:54:26 +02001901 int r;
Ilya Dryomove8ef19c2013-12-24 21:19:24 +02001902
Ilya Dryomov9d521472014-01-31 17:54:26 +02001903 BUG_ON(result_max > CEPH_PG_MAX_SIZE);
1904
1905 mutex_lock(&map->crush_scratch_mutex);
1906 r = crush_do_rule(map->crush, ruleno, x, result, result_max,
1907 weight, weight_max, map->crush_scratch_ary);
1908 mutex_unlock(&map->crush_scratch_mutex);
1909
1910 return r;
Ilya Dryomove8ef19c2013-12-24 21:19:24 +02001911}
1912
Sage Weilf24e9982009-10-06 11:31:10 -07001913/*
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02001914 * Calculate raw set (CRUSH output) for given PG. The result may
1915 * contain nonexistent OSDs. ->primary is undefined for a raw set.
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02001916 *
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02001917 * Placement seed (CRUSH input) is returned through @ppps.
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02001918 */
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02001919static void pg_to_raw_osds(struct ceph_osdmap *osdmap,
1920 struct ceph_pg_pool_info *pi,
1921 const struct ceph_pg *raw_pgid,
1922 struct ceph_osds *raw,
1923 u32 *ppps)
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02001924{
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02001925 u32 pps = raw_pg_to_pps(pi, raw_pgid);
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02001926 int ruleno;
1927 int len;
1928
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02001929 ceph_osds_init(raw);
1930 if (ppps)
1931 *ppps = pps;
1932
1933 ruleno = crush_find_rule(osdmap->crush, pi->crush_ruleset, pi->type,
1934 pi->size);
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02001935 if (ruleno < 0) {
1936 pr_err("no crush rule: pool %lld ruleset %d type %d size %d\n",
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02001937 pi->id, pi->crush_ruleset, pi->type, pi->size);
1938 return;
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02001939 }
1940
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02001941 len = do_crush(osdmap, ruleno, pps, raw->osds,
1942 min_t(int, pi->size, ARRAY_SIZE(raw->osds)),
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02001943 osdmap->osd_weight, osdmap->max_osd);
1944 if (len < 0) {
1945 pr_err("error %d from crush rule %d: pool %lld ruleset %d type %d size %d\n",
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02001946 len, ruleno, pi->id, pi->crush_ruleset, pi->type,
1947 pi->size);
1948 return;
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02001949 }
1950
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02001951 raw->size = len;
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02001952}
1953
1954/*
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02001955 * Given raw set, calculate up set and up primary. By definition of an
1956 * up set, the result won't contain nonexistent or down OSDs.
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02001957 *
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02001958 * This is done in-place - on return @set is the up set. If it's
1959 * empty, ->primary will remain undefined.
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02001960 */
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02001961static void raw_to_up_osds(struct ceph_osdmap *osdmap,
1962 struct ceph_pg_pool_info *pi,
1963 struct ceph_osds *set)
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02001964{
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02001965 int i;
1966
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02001967 /* ->primary is undefined for a raw set */
1968 BUG_ON(set->primary != -1);
1969
1970 if (ceph_can_shift_osds(pi)) {
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02001971 int removed = 0;
1972
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02001973 /* shift left */
1974 for (i = 0; i < set->size; i++) {
1975 if (ceph_osd_is_down(osdmap, set->osds[i])) {
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02001976 removed++;
1977 continue;
1978 }
1979 if (removed)
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02001980 set->osds[i - removed] = set->osds[i];
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02001981 }
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02001982 set->size -= removed;
1983 if (set->size > 0)
1984 set->primary = set->osds[0];
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02001985 } else {
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02001986 /* set down/dne devices to NONE */
1987 for (i = set->size - 1; i >= 0; i--) {
1988 if (ceph_osd_is_down(osdmap, set->osds[i]))
1989 set->osds[i] = CRUSH_ITEM_NONE;
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02001990 else
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02001991 set->primary = set->osds[i];
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02001992 }
1993 }
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02001994}
1995
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02001996static void apply_primary_affinity(struct ceph_osdmap *osdmap,
1997 struct ceph_pg_pool_info *pi,
1998 u32 pps,
1999 struct ceph_osds *up)
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002000{
2001 int i;
2002 int pos = -1;
2003
2004 /*
2005 * Do we have any non-default primary_affinity values for these
2006 * osds?
2007 */
2008 if (!osdmap->osd_primary_affinity)
2009 return;
2010
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002011 for (i = 0; i < up->size; i++) {
2012 int osd = up->osds[i];
Ilya Dryomov92b2e752014-04-10 18:09:41 +04002013
2014 if (osd != CRUSH_ITEM_NONE &&
2015 osdmap->osd_primary_affinity[osd] !=
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002016 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY) {
2017 break;
2018 }
2019 }
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002020 if (i == up->size)
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002021 return;
2022
2023 /*
2024 * Pick the primary. Feed both the seed (for the pg) and the
2025 * osd into the hash/rng so that a proportional fraction of an
2026 * osd's pgs get rejected as primary.
2027 */
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002028 for (i = 0; i < up->size; i++) {
2029 int osd = up->osds[i];
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002030 u32 aff;
2031
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002032 if (osd == CRUSH_ITEM_NONE)
2033 continue;
2034
2035 aff = osdmap->osd_primary_affinity[osd];
2036 if (aff < CEPH_OSD_MAX_PRIMARY_AFFINITY &&
2037 (crush_hash32_2(CRUSH_HASH_RJENKINS1,
2038 pps, osd) >> 16) >= aff) {
2039 /*
2040 * We chose not to use this primary. Note it
2041 * anyway as a fallback in case we don't pick
2042 * anyone else, but keep looking.
2043 */
2044 if (pos < 0)
2045 pos = i;
2046 } else {
2047 pos = i;
2048 break;
2049 }
2050 }
2051 if (pos < 0)
2052 return;
2053
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002054 up->primary = up->osds[pos];
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002055
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002056 if (ceph_can_shift_osds(pi) && pos > 0) {
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002057 /* move the new primary to the front */
2058 for (i = pos; i > 0; i--)
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002059 up->osds[i] = up->osds[i - 1];
2060 up->osds[0] = up->primary;
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02002061 }
2062}
2063
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02002064/*
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002065 * Get pg_temp and primary_temp mappings for given PG.
Ilya Dryomov45966c32014-03-24 17:12:47 +02002066 *
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002067 * Note that a PG may have none, only pg_temp, only primary_temp or
2068 * both pg_temp and primary_temp mappings. This means @temp isn't
2069 * always a valid OSD set on return: in the "only primary_temp" case,
2070 * @temp will have its ->primary >= 0 but ->size == 0.
Ilya Dryomov45966c32014-03-24 17:12:47 +02002071 */
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002072static void get_temp_osds(struct ceph_osdmap *osdmap,
2073 struct ceph_pg_pool_info *pi,
2074 const struct ceph_pg *raw_pgid,
2075 struct ceph_osds *temp)
Ilya Dryomov45966c32014-03-24 17:12:47 +02002076{
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002077 struct ceph_pg pgid;
Ilya Dryomov45966c32014-03-24 17:12:47 +02002078 struct ceph_pg_mapping *pg;
Ilya Dryomov45966c32014-03-24 17:12:47 +02002079 int i;
2080
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002081 raw_pg_to_pg(pi, raw_pgid, &pgid);
2082 ceph_osds_init(temp);
Ilya Dryomov45966c32014-03-24 17:12:47 +02002083
2084 /* pg_temp? */
2085 pg = __lookup_pg_mapping(&osdmap->pg_temp, pgid);
2086 if (pg) {
Ilya Dryomov45966c32014-03-24 17:12:47 +02002087 for (i = 0; i < pg->pg_temp.len; i++) {
2088 if (ceph_osd_is_down(osdmap, pg->pg_temp.osds[i])) {
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002089 if (ceph_can_shift_osds(pi))
Ilya Dryomov45966c32014-03-24 17:12:47 +02002090 continue;
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002091
2092 temp->osds[temp->size++] = CRUSH_ITEM_NONE;
Ilya Dryomov45966c32014-03-24 17:12:47 +02002093 } else {
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002094 temp->osds[temp->size++] = pg->pg_temp.osds[i];
Ilya Dryomov45966c32014-03-24 17:12:47 +02002095 }
2096 }
2097
2098 /* apply pg_temp's primary */
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002099 for (i = 0; i < temp->size; i++) {
2100 if (temp->osds[i] != CRUSH_ITEM_NONE) {
2101 temp->primary = temp->osds[i];
Ilya Dryomov45966c32014-03-24 17:12:47 +02002102 break;
2103 }
2104 }
Ilya Dryomov45966c32014-03-24 17:12:47 +02002105 }
2106
Ilya Dryomov5e8d4d32014-03-24 17:12:48 +02002107 /* primary_temp? */
2108 pg = __lookup_pg_mapping(&osdmap->primary_temp, pgid);
2109 if (pg)
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002110 temp->primary = pg->primary_temp.osd;
Ilya Dryomov45966c32014-03-24 17:12:47 +02002111}
2112
2113/*
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002114 * Map a PG to its acting set as well as its up set.
Ilya Dryomovac972232014-03-24 17:12:48 +02002115 *
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002116 * Acting set is used for data mapping purposes, while up set can be
2117 * recorded for detecting interval changes and deciding whether to
2118 * resend a request.
Sage Weild85b7052010-05-10 10:24:48 -07002119 */
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002120void ceph_pg_to_up_acting_osds(struct ceph_osdmap *osdmap,
2121 const struct ceph_pg *raw_pgid,
2122 struct ceph_osds *up,
2123 struct ceph_osds *acting)
Sage Weild85b7052010-05-10 10:24:48 -07002124{
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002125 struct ceph_pg_pool_info *pi;
Ilya Dryomovac972232014-03-24 17:12:48 +02002126 u32 pps;
Sage Weild85b7052010-05-10 10:24:48 -07002127
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002128 pi = ceph_pg_pool_by_id(osdmap, raw_pgid->pool);
2129 if (!pi) {
2130 ceph_osds_init(up);
2131 ceph_osds_init(acting);
2132 goto out;
Ilya Dryomov8008ab12014-03-24 17:12:48 +02002133 }
Sage Weild85b7052010-05-10 10:24:48 -07002134
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002135 pg_to_raw_osds(osdmap, pi, raw_pgid, up, &pps);
2136 raw_to_up_osds(osdmap, pi, up);
2137 apply_primary_affinity(osdmap, pi, pps, up);
2138 get_temp_osds(osdmap, pi, raw_pgid, acting);
2139 if (!acting->size) {
2140 memcpy(acting->osds, up->osds, up->size * sizeof(up->osds[0]));
2141 acting->size = up->size;
2142 if (acting->primary == -1)
2143 acting->primary = up->primary;
Ilya Dryomovac972232014-03-24 17:12:48 +02002144 }
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002145out:
2146 WARN_ON(!osds_valid(up) || !osds_valid(acting));
Sage Weild85b7052010-05-10 10:24:48 -07002147}
2148
2149/*
Ilya Dryomovf81f1632016-04-28 16:07:23 +02002150 * Return acting primary for given PG, or -1 if none.
Sage Weilf24e9982009-10-06 11:31:10 -07002151 */
Ilya Dryomovf81f1632016-04-28 16:07:23 +02002152int ceph_pg_to_acting_primary(struct ceph_osdmap *osdmap,
2153 const struct ceph_pg *raw_pgid)
Sage Weilf24e9982009-10-06 11:31:10 -07002154{
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002155 struct ceph_osds up, acting;
Sage Weilf24e9982009-10-06 11:31:10 -07002156
Ilya Dryomovf81f1632016-04-28 16:07:23 +02002157 ceph_pg_to_up_acting_osds(osdmap, raw_pgid, &up, &acting);
Ilya Dryomov6f3bfd42016-04-28 16:07:22 +02002158 return acting.primary;
Sage Weilf24e9982009-10-06 11:31:10 -07002159}
Ilya Dryomovf81f1632016-04-28 16:07:23 +02002160EXPORT_SYMBOL(ceph_pg_to_acting_primary);