blob: c547e46084d360c14abd34f97b6d1d3592d1c641 [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);
92 ceph_decode_32_safe(p, end, b->num_nodes, bad);
93 b->node_weights = kcalloc(b->num_nodes, sizeof(u32), GFP_NOFS);
94 if (b->node_weights == NULL)
95 return -ENOMEM;
96 ceph_decode_need(p, end, b->num_nodes * sizeof(u32), bad);
97 for (j = 0; j < b->num_nodes; j++)
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
Sage Weil546f04e2012-07-30 18:15:23 -0700125static int skip_name_map(void **p, void *end)
126{
127 int len;
128 ceph_decode_32_safe(p, end, len ,bad);
129 while (len--) {
130 int strlen;
131 *p += sizeof(u32);
132 ceph_decode_32_safe(p, end, strlen, bad);
133 *p += strlen;
134}
135 return 0;
136bad:
137 return -EINVAL;
138}
139
Sage Weilf24e9982009-10-06 11:31:10 -0700140static struct crush_map *crush_decode(void *pbyval, void *end)
141{
142 struct crush_map *c;
143 int err = -EINVAL;
144 int i, j;
145 void **p = &pbyval;
146 void *start = pbyval;
147 u32 magic;
Sage Weil546f04e2012-07-30 18:15:23 -0700148 u32 num_name_maps;
Sage Weilf24e9982009-10-06 11:31:10 -0700149
150 dout("crush_decode %p to %p len %d\n", *p, end, (int)(end - *p));
151
152 c = kzalloc(sizeof(*c), GFP_NOFS);
153 if (c == NULL)
154 return ERR_PTR(-ENOMEM);
155
Sage Weil546f04e2012-07-30 18:15:23 -0700156 /* set tunables to default values */
157 c->choose_local_tries = 2;
158 c->choose_local_fallback_tries = 5;
159 c->choose_total_tries = 19;
Jim Schutt1604f482012-11-30 09:15:25 -0700160 c->chooseleaf_descend_once = 0;
Sage Weil546f04e2012-07-30 18:15:23 -0700161
Sage Weilf24e9982009-10-06 11:31:10 -0700162 ceph_decode_need(p, end, 4*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700163 magic = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700164 if (magic != CRUSH_MAGIC) {
165 pr_err("crush_decode magic %x != current %x\n",
Eric Dumazet95c96172012-04-15 05:58:06 +0000166 (unsigned int)magic, (unsigned int)CRUSH_MAGIC);
Sage Weilf24e9982009-10-06 11:31:10 -0700167 goto bad;
168 }
Sage Weilc89136e2009-10-14 09:59:09 -0700169 c->max_buckets = ceph_decode_32(p);
170 c->max_rules = ceph_decode_32(p);
171 c->max_devices = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700172
Sage Weilf24e9982009-10-06 11:31:10 -0700173 c->buckets = kcalloc(c->max_buckets, sizeof(*c->buckets), GFP_NOFS);
174 if (c->buckets == NULL)
175 goto badmem;
176 c->rules = kcalloc(c->max_rules, sizeof(*c->rules), GFP_NOFS);
177 if (c->rules == NULL)
178 goto badmem;
179
180 /* buckets */
181 for (i = 0; i < c->max_buckets; i++) {
182 int size = 0;
183 u32 alg;
184 struct crush_bucket *b;
185
186 ceph_decode_32_safe(p, end, alg, bad);
187 if (alg == 0) {
188 c->buckets[i] = NULL;
189 continue;
190 }
191 dout("crush_decode bucket %d off %x %p to %p\n",
192 i, (int)(*p-start), *p, end);
193
194 switch (alg) {
195 case CRUSH_BUCKET_UNIFORM:
196 size = sizeof(struct crush_bucket_uniform);
197 break;
198 case CRUSH_BUCKET_LIST:
199 size = sizeof(struct crush_bucket_list);
200 break;
201 case CRUSH_BUCKET_TREE:
202 size = sizeof(struct crush_bucket_tree);
203 break;
204 case CRUSH_BUCKET_STRAW:
205 size = sizeof(struct crush_bucket_straw);
206 break;
207 default:
Sage Weil30dc6382009-12-21 14:49:37 -0800208 err = -EINVAL;
Sage Weilf24e9982009-10-06 11:31:10 -0700209 goto bad;
210 }
211 BUG_ON(size == 0);
212 b = c->buckets[i] = kzalloc(size, GFP_NOFS);
213 if (b == NULL)
214 goto badmem;
215
216 ceph_decode_need(p, end, 4*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700217 b->id = ceph_decode_32(p);
218 b->type = ceph_decode_16(p);
Sage Weilfb690392009-11-07 20:18:22 -0800219 b->alg = ceph_decode_8(p);
220 b->hash = ceph_decode_8(p);
Sage Weilc89136e2009-10-14 09:59:09 -0700221 b->weight = ceph_decode_32(p);
222 b->size = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700223
224 dout("crush_decode bucket size %d off %x %p to %p\n",
225 b->size, (int)(*p-start), *p, end);
226
227 b->items = kcalloc(b->size, sizeof(__s32), GFP_NOFS);
228 if (b->items == NULL)
229 goto badmem;
230 b->perm = kcalloc(b->size, sizeof(u32), GFP_NOFS);
231 if (b->perm == NULL)
232 goto badmem;
233 b->perm_n = 0;
234
235 ceph_decode_need(p, end, b->size*sizeof(u32), bad);
236 for (j = 0; j < b->size; j++)
Sage Weilc89136e2009-10-14 09:59:09 -0700237 b->items[j] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700238
239 switch (b->alg) {
240 case CRUSH_BUCKET_UNIFORM:
241 err = crush_decode_uniform_bucket(p, end,
242 (struct crush_bucket_uniform *)b);
243 if (err < 0)
244 goto bad;
245 break;
246 case CRUSH_BUCKET_LIST:
247 err = crush_decode_list_bucket(p, end,
248 (struct crush_bucket_list *)b);
249 if (err < 0)
250 goto bad;
251 break;
252 case CRUSH_BUCKET_TREE:
253 err = crush_decode_tree_bucket(p, end,
254 (struct crush_bucket_tree *)b);
255 if (err < 0)
256 goto bad;
257 break;
258 case CRUSH_BUCKET_STRAW:
259 err = crush_decode_straw_bucket(p, end,
260 (struct crush_bucket_straw *)b);
261 if (err < 0)
262 goto bad;
263 break;
264 }
265 }
266
267 /* rules */
268 dout("rule vec is %p\n", c->rules);
269 for (i = 0; i < c->max_rules; i++) {
270 u32 yes;
271 struct crush_rule *r;
272
273 ceph_decode_32_safe(p, end, yes, bad);
274 if (!yes) {
275 dout("crush_decode NO rule %d off %x %p to %p\n",
276 i, (int)(*p-start), *p, end);
277 c->rules[i] = NULL;
278 continue;
279 }
280
281 dout("crush_decode rule %d off %x %p to %p\n",
282 i, (int)(*p-start), *p, end);
283
284 /* len */
285 ceph_decode_32_safe(p, end, yes, bad);
286#if BITS_PER_LONG == 32
Sage Weil30dc6382009-12-21 14:49:37 -0800287 err = -EINVAL;
Xi Wang64486692012-02-16 11:55:48 -0500288 if (yes > (ULONG_MAX - sizeof(*r))
289 / sizeof(struct crush_rule_step))
Sage Weilf24e9982009-10-06 11:31:10 -0700290 goto bad;
291#endif
292 r = c->rules[i] = kmalloc(sizeof(*r) +
293 yes*sizeof(struct crush_rule_step),
294 GFP_NOFS);
295 if (r == NULL)
296 goto badmem;
297 dout(" rule %d is at %p\n", i, r);
298 r->len = yes;
299 ceph_decode_copy_safe(p, end, &r->mask, 4, bad); /* 4 u8's */
300 ceph_decode_need(p, end, r->len*3*sizeof(u32), bad);
301 for (j = 0; j < r->len; j++) {
Sage Weilc89136e2009-10-14 09:59:09 -0700302 r->steps[j].op = ceph_decode_32(p);
303 r->steps[j].arg1 = ceph_decode_32(p);
304 r->steps[j].arg2 = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -0700305 }
306 }
307
308 /* ignore trailing name maps. */
Sage Weil546f04e2012-07-30 18:15:23 -0700309 for (num_name_maps = 0; num_name_maps < 3; num_name_maps++) {
310 err = skip_name_map(p, end);
311 if (err < 0)
312 goto done;
313 }
Sage Weilf24e9982009-10-06 11:31:10 -0700314
Sage Weil546f04e2012-07-30 18:15:23 -0700315 /* tunables */
316 ceph_decode_need(p, end, 3*sizeof(u32), done);
317 c->choose_local_tries = ceph_decode_32(p);
318 c->choose_local_fallback_tries = ceph_decode_32(p);
319 c->choose_total_tries = ceph_decode_32(p);
320 dout("crush decode tunable choose_local_tries = %d",
321 c->choose_local_tries);
322 dout("crush decode tunable choose_local_fallback_tries = %d",
323 c->choose_local_fallback_tries);
324 dout("crush decode tunable choose_total_tries = %d",
325 c->choose_total_tries);
326
Jim Schutt1604f482012-11-30 09:15:25 -0700327 ceph_decode_need(p, end, sizeof(u32), done);
328 c->chooseleaf_descend_once = ceph_decode_32(p);
329 dout("crush decode tunable chooseleaf_descend_once = %d",
330 c->chooseleaf_descend_once);
331
Ilya Dryomovf1406622014-05-09 18:27:34 +0400332 ceph_decode_need(p, end, sizeof(u8), done);
333 c->chooseleaf_vary_r = ceph_decode_8(p);
334 dout("crush decode tunable chooseleaf_vary_r = %d",
335 c->chooseleaf_vary_r);
336
Sage Weil546f04e2012-07-30 18:15:23 -0700337done:
Sage Weilf24e9982009-10-06 11:31:10 -0700338 dout("crush_decode success\n");
339 return c;
340
341badmem:
342 err = -ENOMEM;
343bad:
344 dout("crush_decode fail %d\n", err);
345 crush_destroy(c);
346 return ERR_PTR(err);
347}
348
Sage Weilf24e9982009-10-06 11:31:10 -0700349/*
Sage Weil9794b142010-02-16 15:53:32 -0800350 * rbtree of pg_mapping for handling pg_temp (explicit mapping of pgid
Ilya Dryomov9686f942014-03-21 19:05:29 +0200351 * to a set of osds) and primary_temp (explicit primary setting)
Sage Weilf24e9982009-10-06 11:31:10 -0700352 */
Sage Weil5b191d92013-02-23 10:38:16 -0800353static int pgid_cmp(struct ceph_pg l, struct ceph_pg r)
Sage Weil51042122009-11-04 11:39:12 -0800354{
Sage Weil5b191d92013-02-23 10:38:16 -0800355 if (l.pool < r.pool)
Sage Weil51042122009-11-04 11:39:12 -0800356 return -1;
Sage Weil5b191d92013-02-23 10:38:16 -0800357 if (l.pool > r.pool)
358 return 1;
359 if (l.seed < r.seed)
360 return -1;
361 if (l.seed > r.seed)
Sage Weil51042122009-11-04 11:39:12 -0800362 return 1;
363 return 0;
364}
365
Sage Weil991abb62009-10-08 22:22:37 -0700366static int __insert_pg_mapping(struct ceph_pg_mapping *new,
367 struct rb_root *root)
Sage Weilf24e9982009-10-06 11:31:10 -0700368{
369 struct rb_node **p = &root->rb_node;
370 struct rb_node *parent = NULL;
371 struct ceph_pg_mapping *pg = NULL;
Sage Weil51042122009-11-04 11:39:12 -0800372 int c;
Sage Weilf24e9982009-10-06 11:31:10 -0700373
Sage Weil8adc8b32011-09-28 10:11:04 -0700374 dout("__insert_pg_mapping %llx %p\n", *(u64 *)&new->pgid, new);
Sage Weilf24e9982009-10-06 11:31:10 -0700375 while (*p) {
376 parent = *p;
377 pg = rb_entry(parent, struct ceph_pg_mapping, node);
Sage Weil51042122009-11-04 11:39:12 -0800378 c = pgid_cmp(new->pgid, pg->pgid);
379 if (c < 0)
Sage Weilf24e9982009-10-06 11:31:10 -0700380 p = &(*p)->rb_left;
Sage Weil51042122009-11-04 11:39:12 -0800381 else if (c > 0)
Sage Weilf24e9982009-10-06 11:31:10 -0700382 p = &(*p)->rb_right;
383 else
Sage Weil991abb62009-10-08 22:22:37 -0700384 return -EEXIST;
Sage Weilf24e9982009-10-06 11:31:10 -0700385 }
386
387 rb_link_node(&new->node, parent, p);
388 rb_insert_color(&new->node, root);
Sage Weil991abb62009-10-08 22:22:37 -0700389 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700390}
391
Sage Weil9794b142010-02-16 15:53:32 -0800392static struct ceph_pg_mapping *__lookup_pg_mapping(struct rb_root *root,
Sage Weil5b191d92013-02-23 10:38:16 -0800393 struct ceph_pg pgid)
Sage Weil9794b142010-02-16 15:53:32 -0800394{
395 struct rb_node *n = root->rb_node;
396 struct ceph_pg_mapping *pg;
397 int c;
398
399 while (n) {
400 pg = rb_entry(n, struct ceph_pg_mapping, node);
401 c = pgid_cmp(pgid, pg->pgid);
Sage Weil8adc8b32011-09-28 10:11:04 -0700402 if (c < 0) {
Sage Weil9794b142010-02-16 15:53:32 -0800403 n = n->rb_left;
Sage Weil8adc8b32011-09-28 10:11:04 -0700404 } else if (c > 0) {
Sage Weil9794b142010-02-16 15:53:32 -0800405 n = n->rb_right;
Sage Weil8adc8b32011-09-28 10:11:04 -0700406 } else {
Sage Weil5b191d92013-02-23 10:38:16 -0800407 dout("__lookup_pg_mapping %lld.%x got %p\n",
408 pgid.pool, pgid.seed, pg);
Sage Weil9794b142010-02-16 15:53:32 -0800409 return pg;
Sage Weil8adc8b32011-09-28 10:11:04 -0700410 }
Sage Weil9794b142010-02-16 15:53:32 -0800411 }
412 return NULL;
413}
414
Sage Weil5b191d92013-02-23 10:38:16 -0800415static int __remove_pg_mapping(struct rb_root *root, struct ceph_pg pgid)
Sage Weil8adc8b32011-09-28 10:11:04 -0700416{
417 struct ceph_pg_mapping *pg = __lookup_pg_mapping(root, pgid);
418
419 if (pg) {
Sage Weil5b191d92013-02-23 10:38:16 -0800420 dout("__remove_pg_mapping %lld.%x %p\n", pgid.pool, pgid.seed,
421 pg);
Sage Weil8adc8b32011-09-28 10:11:04 -0700422 rb_erase(&pg->node, root);
423 kfree(pg);
424 return 0;
425 }
Sage Weil5b191d92013-02-23 10:38:16 -0800426 dout("__remove_pg_mapping %lld.%x dne\n", pgid.pool, pgid.seed);
Sage Weil8adc8b32011-09-28 10:11:04 -0700427 return -ENOENT;
428}
429
Sage Weilf24e9982009-10-06 11:31:10 -0700430/*
Sage Weil4fc51be2010-02-16 15:55:03 -0800431 * rbtree of pg pool info
432 */
433static int __insert_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *new)
434{
435 struct rb_node **p = &root->rb_node;
436 struct rb_node *parent = NULL;
437 struct ceph_pg_pool_info *pi = NULL;
438
439 while (*p) {
440 parent = *p;
441 pi = rb_entry(parent, struct ceph_pg_pool_info, node);
442 if (new->id < pi->id)
443 p = &(*p)->rb_left;
444 else if (new->id > pi->id)
445 p = &(*p)->rb_right;
446 else
447 return -EEXIST;
448 }
449
450 rb_link_node(&new->node, parent, p);
451 rb_insert_color(&new->node, root);
452 return 0;
453}
454
Sage Weil4f6a7e52013-02-23 10:41:09 -0800455static struct ceph_pg_pool_info *__lookup_pg_pool(struct rb_root *root, u64 id)
Sage Weil4fc51be2010-02-16 15:55:03 -0800456{
457 struct ceph_pg_pool_info *pi;
458 struct rb_node *n = root->rb_node;
459
460 while (n) {
461 pi = rb_entry(n, struct ceph_pg_pool_info, node);
462 if (id < pi->id)
463 n = n->rb_left;
464 else if (id > pi->id)
465 n = n->rb_right;
466 else
467 return pi;
468 }
469 return NULL;
470}
471
Ilya Dryomovce7f6a22014-01-27 17:40:19 +0200472struct ceph_pg_pool_info *ceph_pg_pool_by_id(struct ceph_osdmap *map, u64 id)
473{
474 return __lookup_pg_pool(&map->pg_pools, id);
475}
476
Alex Elder72afc712012-10-30 19:40:33 -0500477const char *ceph_pg_pool_name_by_id(struct ceph_osdmap *map, u64 id)
478{
479 struct ceph_pg_pool_info *pi;
480
481 if (id == CEPH_NOPOOL)
482 return NULL;
483
484 if (WARN_ON_ONCE(id > (u64) INT_MAX))
485 return NULL;
486
487 pi = __lookup_pg_pool(&map->pg_pools, (int) id);
488
489 return pi ? pi->name : NULL;
490}
491EXPORT_SYMBOL(ceph_pg_pool_name_by_id);
492
Yehuda Sadeh7669a2c2010-05-17 12:31:35 -0700493int ceph_pg_poolid_by_name(struct ceph_osdmap *map, const char *name)
494{
495 struct rb_node *rbp;
496
497 for (rbp = rb_first(&map->pg_pools); rbp; rbp = rb_next(rbp)) {
498 struct ceph_pg_pool_info *pi =
499 rb_entry(rbp, struct ceph_pg_pool_info, node);
500 if (pi->name && strcmp(pi->name, name) == 0)
501 return pi->id;
502 }
503 return -ENOENT;
504}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700505EXPORT_SYMBOL(ceph_pg_poolid_by_name);
Yehuda Sadeh7669a2c2010-05-17 12:31:35 -0700506
Sage Weil2844a762010-04-09 15:46:42 -0700507static void __remove_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *pi)
508{
509 rb_erase(&pi->node, root);
510 kfree(pi->name);
511 kfree(pi);
512}
513
Ilya Dryomov0f70c7e2014-03-21 19:05:27 +0200514static int decode_pool(void **p, void *end, struct ceph_pg_pool_info *pi)
Sage Weilefd75762010-03-17 10:05:28 -0700515{
Sage Weil4f6a7e52013-02-23 10:41:09 -0800516 u8 ev, cv;
517 unsigned len, num;
518 void *pool_end;
Sage Weil73a7e692010-08-02 11:00:55 -0700519
Sage Weil4f6a7e52013-02-23 10:41:09 -0800520 ceph_decode_need(p, end, 2 + 4, bad);
521 ev = ceph_decode_8(p); /* encoding version */
522 cv = ceph_decode_8(p); /* compat version */
523 if (ev < 5) {
524 pr_warning("got v %d < 5 cv %d of ceph_pg_pool\n", ev, cv);
525 return -EINVAL;
526 }
Ilya Dryomov17a13e402014-01-27 17:40:19 +0200527 if (cv > 9) {
528 pr_warning("got v %d cv %d > 9 of ceph_pg_pool\n", ev, cv);
Sage Weil4f6a7e52013-02-23 10:41:09 -0800529 return -EINVAL;
530 }
531 len = ceph_decode_32(p);
532 ceph_decode_need(p, end, len, bad);
533 pool_end = *p + len;
Sage Weil73a7e692010-08-02 11:00:55 -0700534
Sage Weil4f6a7e52013-02-23 10:41:09 -0800535 pi->type = ceph_decode_8(p);
536 pi->size = ceph_decode_8(p);
537 pi->crush_ruleset = ceph_decode_8(p);
538 pi->object_hash = ceph_decode_8(p);
539
540 pi->pg_num = ceph_decode_32(p);
541 pi->pgp_num = ceph_decode_32(p);
542
543 *p += 4 + 4; /* skip lpg* */
544 *p += 4; /* skip last_change */
545 *p += 8 + 4; /* skip snap_seq, snap_epoch */
546
547 /* skip snaps */
548 num = ceph_decode_32(p);
549 while (num--) {
550 *p += 8; /* snapid key */
551 *p += 1 + 1; /* versions */
552 len = ceph_decode_32(p);
553 *p += len;
Sage Weil73a7e692010-08-02 11:00:55 -0700554 }
555
Ilya Dryomov17a13e402014-01-27 17:40:19 +0200556 /* skip removed_snaps */
Sage Weil4f6a7e52013-02-23 10:41:09 -0800557 num = ceph_decode_32(p);
558 *p += num * (8 + 8);
559
560 *p += 8; /* skip auid */
561 pi->flags = ceph_decode_64(p);
Ilya Dryomov17a13e402014-01-27 17:40:19 +0200562 *p += 4; /* skip crash_replay_interval */
563
564 if (ev >= 7)
565 *p += 1; /* skip min_size */
566
567 if (ev >= 8)
568 *p += 8 + 8; /* skip quota_max_* */
569
570 if (ev >= 9) {
571 /* skip tiers */
572 num = ceph_decode_32(p);
573 *p += num * 8;
574
575 *p += 8; /* skip tier_of */
576 *p += 1; /* skip cache_mode */
577
578 pi->read_tier = ceph_decode_64(p);
579 pi->write_tier = ceph_decode_64(p);
580 } else {
581 pi->read_tier = -1;
582 pi->write_tier = -1;
583 }
Sage Weil4f6a7e52013-02-23 10:41:09 -0800584
585 /* ignore the rest */
586
587 *p = pool_end;
588 calc_pg_masks(pi);
Sage Weil73a7e692010-08-02 11:00:55 -0700589 return 0;
590
591bad:
592 return -EINVAL;
Sage Weilefd75762010-03-17 10:05:28 -0700593}
594
Ilya Dryomov0f70c7e2014-03-21 19:05:27 +0200595static int decode_pool_names(void **p, void *end, struct ceph_osdmap *map)
Sage Weil2844a762010-04-09 15:46:42 -0700596{
597 struct ceph_pg_pool_info *pi;
Sage Weil4f6a7e52013-02-23 10:41:09 -0800598 u32 num, len;
599 u64 pool;
Sage Weil2844a762010-04-09 15:46:42 -0700600
601 ceph_decode_32_safe(p, end, num, bad);
602 dout(" %d pool names\n", num);
603 while (num--) {
Sage Weil4f6a7e52013-02-23 10:41:09 -0800604 ceph_decode_64_safe(p, end, pool, bad);
Sage Weil2844a762010-04-09 15:46:42 -0700605 ceph_decode_32_safe(p, end, len, bad);
Sage Weil4f6a7e52013-02-23 10:41:09 -0800606 dout(" pool %llu len %d\n", pool, len);
Xi Wangad3b9042012-06-06 19:35:55 -0500607 ceph_decode_need(p, end, len, bad);
Sage Weil2844a762010-04-09 15:46:42 -0700608 pi = __lookup_pg_pool(&map->pg_pools, pool);
609 if (pi) {
Xi Wangad3b9042012-06-06 19:35:55 -0500610 char *name = kstrndup(*p, len, GFP_NOFS);
611
612 if (!name)
613 return -ENOMEM;
Sage Weil2844a762010-04-09 15:46:42 -0700614 kfree(pi->name);
Xi Wangad3b9042012-06-06 19:35:55 -0500615 pi->name = name;
616 dout(" name is %s\n", pi->name);
Sage Weil2844a762010-04-09 15:46:42 -0700617 }
618 *p += len;
619 }
620 return 0;
621
622bad:
623 return -EINVAL;
624}
625
626/*
627 * osd map
628 */
629void ceph_osdmap_destroy(struct ceph_osdmap *map)
630{
631 dout("osdmap_destroy %p\n", map);
632 if (map->crush)
633 crush_destroy(map->crush);
634 while (!RB_EMPTY_ROOT(&map->pg_temp)) {
635 struct ceph_pg_mapping *pg =
636 rb_entry(rb_first(&map->pg_temp),
637 struct ceph_pg_mapping, node);
638 rb_erase(&pg->node, &map->pg_temp);
639 kfree(pg);
640 }
Ilya Dryomov9686f942014-03-21 19:05:29 +0200641 while (!RB_EMPTY_ROOT(&map->primary_temp)) {
642 struct ceph_pg_mapping *pg =
643 rb_entry(rb_first(&map->primary_temp),
644 struct ceph_pg_mapping, node);
645 rb_erase(&pg->node, &map->primary_temp);
646 kfree(pg);
647 }
Sage Weil2844a762010-04-09 15:46:42 -0700648 while (!RB_EMPTY_ROOT(&map->pg_pools)) {
649 struct ceph_pg_pool_info *pi =
650 rb_entry(rb_first(&map->pg_pools),
651 struct ceph_pg_pool_info, node);
652 __remove_pg_pool(&map->pg_pools, pi);
653 }
654 kfree(map->osd_state);
655 kfree(map->osd_weight);
656 kfree(map->osd_addr);
Ilya Dryomov2cfa34f2014-03-21 19:05:30 +0200657 kfree(map->osd_primary_affinity);
Sage Weil2844a762010-04-09 15:46:42 -0700658 kfree(map);
659}
660
661/*
Ilya Dryomov4d603512014-03-21 19:05:28 +0200662 * Adjust max_osd value, (re)allocate arrays.
663 *
664 * The new elements are properly initialized.
Sage Weil2844a762010-04-09 15:46:42 -0700665 */
666static int osdmap_set_max_osd(struct ceph_osdmap *map, int max)
667{
668 u8 *state;
Sage Weil2844a762010-04-09 15:46:42 -0700669 u32 *weight;
Ilya Dryomov4d603512014-03-21 19:05:28 +0200670 struct ceph_entity_addr *addr;
671 int i;
Sage Weil2844a762010-04-09 15:46:42 -0700672
Ilya Dryomov4d603512014-03-21 19:05:28 +0200673 state = krealloc(map->osd_state, max*sizeof(*state), GFP_NOFS);
674 weight = krealloc(map->osd_weight, max*sizeof(*weight), GFP_NOFS);
675 addr = krealloc(map->osd_addr, max*sizeof(*addr), GFP_NOFS);
676 if (!state || !weight || !addr) {
Sage Weil2844a762010-04-09 15:46:42 -0700677 kfree(state);
Sage Weil2844a762010-04-09 15:46:42 -0700678 kfree(weight);
Ilya Dryomov4d603512014-03-21 19:05:28 +0200679 kfree(addr);
680
Sage Weil2844a762010-04-09 15:46:42 -0700681 return -ENOMEM;
682 }
683
Ilya Dryomov4d603512014-03-21 19:05:28 +0200684 for (i = map->max_osd; i < max; i++) {
685 state[i] = 0;
686 weight[i] = CEPH_OSD_OUT;
687 memset(addr + i, 0, sizeof(*addr));
Sage Weil2844a762010-04-09 15:46:42 -0700688 }
689
690 map->osd_state = state;
691 map->osd_weight = weight;
692 map->osd_addr = addr;
Ilya Dryomov4d603512014-03-21 19:05:28 +0200693
Ilya Dryomov2cfa34f2014-03-21 19:05:30 +0200694 if (map->osd_primary_affinity) {
695 u32 *affinity;
696
697 affinity = krealloc(map->osd_primary_affinity,
698 max*sizeof(*affinity), GFP_NOFS);
699 if (!affinity)
700 return -ENOMEM;
701
702 for (i = map->max_osd; i < max; i++)
703 affinity[i] = CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
704
705 map->osd_primary_affinity = affinity;
706 }
707
Sage Weil2844a762010-04-09 15:46:42 -0700708 map->max_osd = max;
Ilya Dryomov4d603512014-03-21 19:05:28 +0200709
Sage Weil2844a762010-04-09 15:46:42 -0700710 return 0;
711}
712
Ilya Dryomovec7af972014-03-21 19:05:29 +0200713#define OSDMAP_WRAPPER_COMPAT_VER 7
714#define OSDMAP_CLIENT_DATA_COMPAT_VER 1
715
716/*
717 * Return 0 or error. On success, *v is set to 0 for old (v6) osdmaps,
718 * to struct_v of the client_data section for new (v7 and above)
719 * osdmaps.
720 */
721static int get_osdmap_client_data_v(void **p, void *end,
722 const char *prefix, u8 *v)
723{
724 u8 struct_v;
725
726 ceph_decode_8_safe(p, end, struct_v, e_inval);
727 if (struct_v >= 7) {
728 u8 struct_compat;
729
730 ceph_decode_8_safe(p, end, struct_compat, e_inval);
731 if (struct_compat > OSDMAP_WRAPPER_COMPAT_VER) {
732 pr_warning("got v %d cv %d > %d of %s ceph_osdmap\n",
733 struct_v, struct_compat,
734 OSDMAP_WRAPPER_COMPAT_VER, prefix);
735 return -EINVAL;
736 }
737 *p += 4; /* ignore wrapper struct_len */
738
739 ceph_decode_8_safe(p, end, struct_v, e_inval);
740 ceph_decode_8_safe(p, end, struct_compat, e_inval);
741 if (struct_compat > OSDMAP_CLIENT_DATA_COMPAT_VER) {
742 pr_warning("got v %d cv %d > %d of %s ceph_osdmap client data\n",
743 struct_v, struct_compat,
744 OSDMAP_CLIENT_DATA_COMPAT_VER, prefix);
745 return -EINVAL;
746 }
747 *p += 4; /* ignore client data struct_len */
748 } else {
749 u16 version;
750
751 *p -= 1;
752 ceph_decode_16_safe(p, end, version, e_inval);
753 if (version < 6) {
754 pr_warning("got v %d < 6 of %s ceph_osdmap\n", version,
755 prefix);
756 return -EINVAL;
757 }
758
759 /* old osdmap enconding */
760 struct_v = 0;
761 }
762
763 *v = struct_v;
764 return 0;
765
766e_inval:
767 return -EINVAL;
768}
769
Ilya Dryomov433fbdd2014-03-21 19:05:27 +0200770static int __decode_pools(void **p, void *end, struct ceph_osdmap *map,
771 bool incremental)
772{
773 u32 n;
774
775 ceph_decode_32_safe(p, end, n, e_inval);
776 while (n--) {
777 struct ceph_pg_pool_info *pi;
778 u64 pool;
779 int ret;
780
781 ceph_decode_64_safe(p, end, pool, e_inval);
782
783 pi = __lookup_pg_pool(&map->pg_pools, pool);
784 if (!incremental || !pi) {
785 pi = kzalloc(sizeof(*pi), GFP_NOFS);
786 if (!pi)
787 return -ENOMEM;
788
789 pi->id = pool;
790
791 ret = __insert_pg_pool(&map->pg_pools, pi);
792 if (ret) {
793 kfree(pi);
794 return ret;
795 }
796 }
797
798 ret = decode_pool(p, end, pi);
799 if (ret)
800 return ret;
801 }
802
803 return 0;
804
805e_inval:
806 return -EINVAL;
807}
808
809static int decode_pools(void **p, void *end, struct ceph_osdmap *map)
810{
811 return __decode_pools(p, end, map, false);
812}
813
814static int decode_new_pools(void **p, void *end, struct ceph_osdmap *map)
815{
816 return __decode_pools(p, end, map, true);
817}
818
Ilya Dryomov10db6342014-03-21 19:05:28 +0200819static int __decode_pg_temp(void **p, void *end, struct ceph_osdmap *map,
820 bool incremental)
821{
822 u32 n;
823
824 ceph_decode_32_safe(p, end, n, e_inval);
825 while (n--) {
826 struct ceph_pg pgid;
827 u32 len, i;
828 int ret;
829
830 ret = ceph_decode_pgid(p, end, &pgid);
831 if (ret)
832 return ret;
833
834 ceph_decode_32_safe(p, end, len, e_inval);
835
836 ret = __remove_pg_mapping(&map->pg_temp, pgid);
837 BUG_ON(!incremental && ret != -ENOENT);
838
839 if (!incremental || len > 0) {
840 struct ceph_pg_mapping *pg;
841
842 ceph_decode_need(p, end, len*sizeof(u32), e_inval);
843
844 if (len > (UINT_MAX - sizeof(*pg)) / sizeof(u32))
845 return -EINVAL;
846
847 pg = kzalloc(sizeof(*pg) + len*sizeof(u32), GFP_NOFS);
848 if (!pg)
849 return -ENOMEM;
850
851 pg->pgid = pgid;
Ilya Dryomov35a935d2014-03-21 19:05:29 +0200852 pg->pg_temp.len = len;
Ilya Dryomov10db6342014-03-21 19:05:28 +0200853 for (i = 0; i < len; i++)
Ilya Dryomov35a935d2014-03-21 19:05:29 +0200854 pg->pg_temp.osds[i] = ceph_decode_32(p);
Ilya Dryomov10db6342014-03-21 19:05:28 +0200855
856 ret = __insert_pg_mapping(pg, &map->pg_temp);
857 if (ret) {
858 kfree(pg);
859 return ret;
860 }
861 }
862 }
863
864 return 0;
865
866e_inval:
867 return -EINVAL;
868}
869
870static int decode_pg_temp(void **p, void *end, struct ceph_osdmap *map)
871{
872 return __decode_pg_temp(p, end, map, false);
873}
874
875static int decode_new_pg_temp(void **p, void *end, struct ceph_osdmap *map)
876{
877 return __decode_pg_temp(p, end, map, true);
878}
879
Ilya Dryomovd286de72014-03-21 19:05:30 +0200880static int __decode_primary_temp(void **p, void *end, struct ceph_osdmap *map,
881 bool incremental)
882{
883 u32 n;
884
885 ceph_decode_32_safe(p, end, n, e_inval);
886 while (n--) {
887 struct ceph_pg pgid;
888 u32 osd;
889 int ret;
890
891 ret = ceph_decode_pgid(p, end, &pgid);
892 if (ret)
893 return ret;
894
895 ceph_decode_32_safe(p, end, osd, e_inval);
896
897 ret = __remove_pg_mapping(&map->primary_temp, pgid);
898 BUG_ON(!incremental && ret != -ENOENT);
899
900 if (!incremental || osd != (u32)-1) {
901 struct ceph_pg_mapping *pg;
902
903 pg = kzalloc(sizeof(*pg), GFP_NOFS);
904 if (!pg)
905 return -ENOMEM;
906
907 pg->pgid = pgid;
908 pg->primary_temp.osd = osd;
909
910 ret = __insert_pg_mapping(pg, &map->primary_temp);
911 if (ret) {
912 kfree(pg);
913 return ret;
914 }
915 }
916 }
917
918 return 0;
919
920e_inval:
921 return -EINVAL;
922}
923
924static int decode_primary_temp(void **p, void *end, struct ceph_osdmap *map)
925{
926 return __decode_primary_temp(p, end, map, false);
927}
928
929static int decode_new_primary_temp(void **p, void *end,
930 struct ceph_osdmap *map)
931{
932 return __decode_primary_temp(p, end, map, true);
933}
934
Ilya Dryomov2cfa34f2014-03-21 19:05:30 +0200935u32 ceph_get_primary_affinity(struct ceph_osdmap *map, int osd)
936{
937 BUG_ON(osd >= map->max_osd);
938
939 if (!map->osd_primary_affinity)
940 return CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
941
942 return map->osd_primary_affinity[osd];
943}
944
945static int set_primary_affinity(struct ceph_osdmap *map, int osd, u32 aff)
946{
947 BUG_ON(osd >= map->max_osd);
948
949 if (!map->osd_primary_affinity) {
950 int i;
951
952 map->osd_primary_affinity = kmalloc(map->max_osd*sizeof(u32),
953 GFP_NOFS);
954 if (!map->osd_primary_affinity)
955 return -ENOMEM;
956
957 for (i = 0; i < map->max_osd; i++)
958 map->osd_primary_affinity[i] =
959 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
960 }
961
962 map->osd_primary_affinity[osd] = aff;
963
964 return 0;
965}
966
Ilya Dryomov63a69932014-03-21 19:05:30 +0200967static int decode_primary_affinity(void **p, void *end,
968 struct ceph_osdmap *map)
969{
970 u32 len, i;
971
972 ceph_decode_32_safe(p, end, len, e_inval);
973 if (len == 0) {
974 kfree(map->osd_primary_affinity);
975 map->osd_primary_affinity = NULL;
976 return 0;
977 }
978 if (len != map->max_osd)
979 goto e_inval;
980
981 ceph_decode_need(p, end, map->max_osd*sizeof(u32), e_inval);
982
983 for (i = 0; i < map->max_osd; i++) {
984 int ret;
985
986 ret = set_primary_affinity(map, i, ceph_decode_32(p));
987 if (ret)
988 return ret;
989 }
990
991 return 0;
992
993e_inval:
994 return -EINVAL;
995}
996
997static int decode_new_primary_affinity(void **p, void *end,
998 struct ceph_osdmap *map)
999{
1000 u32 n;
1001
1002 ceph_decode_32_safe(p, end, n, e_inval);
1003 while (n--) {
1004 u32 osd, aff;
1005 int ret;
1006
1007 ceph_decode_32_safe(p, end, osd, e_inval);
1008 ceph_decode_32_safe(p, end, aff, e_inval);
1009
1010 ret = set_primary_affinity(map, osd, aff);
1011 if (ret)
1012 return ret;
Ilya Dryomovf31da0f2014-04-02 20:34:04 +04001013
1014 pr_info("osd%d primary-affinity 0x%x\n", osd, aff);
Ilya Dryomov63a69932014-03-21 19:05:30 +02001015 }
1016
1017 return 0;
1018
1019e_inval:
1020 return -EINVAL;
1021}
1022
Sage Weil4fc51be2010-02-16 15:55:03 -08001023/*
Sage Weilf24e9982009-10-06 11:31:10 -07001024 * decode a full map.
1025 */
Ilya Dryomova2505d62014-03-13 16:36:13 +02001026static int osdmap_decode(void **p, void *end, struct ceph_osdmap *map)
Sage Weilf24e9982009-10-06 11:31:10 -07001027{
Ilya Dryomovec7af972014-03-21 19:05:29 +02001028 u8 struct_v;
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001029 u32 epoch = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001030 void *start = *p;
Ilya Dryomov39770582014-03-13 16:36:14 +02001031 u32 max;
1032 u32 len, i;
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001033 int err;
Sage Weilf24e9982009-10-06 11:31:10 -07001034
Ilya Dryomova2505d62014-03-13 16:36:13 +02001035 dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p));
Sage Weilf24e9982009-10-06 11:31:10 -07001036
Ilya Dryomovec7af972014-03-21 19:05:29 +02001037 err = get_osdmap_client_data_v(p, end, "full", &struct_v);
1038 if (err)
1039 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001040
Ilya Dryomov53bbaba2014-03-13 16:36:16 +02001041 /* fsid, epoch, created, modified */
1042 ceph_decode_need(p, end, sizeof(map->fsid) + sizeof(u32) +
1043 sizeof(map->created) + sizeof(map->modified), e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001044 ceph_decode_copy(p, &map->fsid, sizeof(map->fsid));
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001045 epoch = map->epoch = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -07001046 ceph_decode_copy(p, &map->created, sizeof(map->created));
1047 ceph_decode_copy(p, &map->modified, sizeof(map->modified));
1048
Ilya Dryomov433fbdd2014-03-21 19:05:27 +02001049 /* pools */
1050 err = decode_pools(p, end, map);
1051 if (err)
1052 goto bad;
Sage Weil2844a762010-04-09 15:46:42 -07001053
Ilya Dryomov0f70c7e2014-03-21 19:05:27 +02001054 /* pool_name */
1055 err = decode_pool_names(p, end, map);
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001056 if (err)
Sage Weil4f6a7e52013-02-23 10:41:09 -08001057 goto bad;
Sage Weil2844a762010-04-09 15:46:42 -07001058
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001059 ceph_decode_32_safe(p, end, map->pool_max, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001060
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001061 ceph_decode_32_safe(p, end, map->flags, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001062
Ilya Dryomov39770582014-03-13 16:36:14 +02001063 /* max_osd */
1064 ceph_decode_32_safe(p, end, max, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001065
1066 /* (re)alloc osd arrays */
1067 err = osdmap_set_max_osd(map, max);
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001068 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001069 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001070
Ilya Dryomov2d88b2e2014-03-13 16:36:14 +02001071 /* osd_state, osd_weight, osd_addrs->client_addr */
Sage Weilf24e9982009-10-06 11:31:10 -07001072 ceph_decode_need(p, end, 3*sizeof(u32) +
1073 map->max_osd*(1 + sizeof(*map->osd_weight) +
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001074 sizeof(*map->osd_addr)), e_inval);
1075
Ilya Dryomov2d88b2e2014-03-13 16:36:14 +02001076 if (ceph_decode_32(p) != map->max_osd)
1077 goto e_inval;
1078
Sage Weilf24e9982009-10-06 11:31:10 -07001079 ceph_decode_copy(p, map->osd_state, map->max_osd);
1080
Ilya Dryomov2d88b2e2014-03-13 16:36:14 +02001081 if (ceph_decode_32(p) != map->max_osd)
1082 goto e_inval;
1083
Sage Weilf24e9982009-10-06 11:31:10 -07001084 for (i = 0; i < map->max_osd; i++)
Sage Weilc89136e2009-10-14 09:59:09 -07001085 map->osd_weight[i] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -07001086
Ilya Dryomov2d88b2e2014-03-13 16:36:14 +02001087 if (ceph_decode_32(p) != map->max_osd)
1088 goto e_inval;
1089
Sage Weilf24e9982009-10-06 11:31:10 -07001090 ceph_decode_copy(p, map->osd_addr, map->max_osd*sizeof(*map->osd_addr));
Sage Weil63f2d212009-11-03 15:17:56 -08001091 for (i = 0; i < map->max_osd; i++)
1092 ceph_decode_addr(&map->osd_addr[i]);
Sage Weilf24e9982009-10-06 11:31:10 -07001093
1094 /* pg_temp */
Ilya Dryomov10db6342014-03-21 19:05:28 +02001095 err = decode_pg_temp(p, end, map);
1096 if (err)
1097 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001098
Ilya Dryomovd286de72014-03-21 19:05:30 +02001099 /* primary_temp */
1100 if (struct_v >= 1) {
1101 err = decode_primary_temp(p, end, map);
1102 if (err)
1103 goto bad;
1104 }
1105
Ilya Dryomov63a69932014-03-21 19:05:30 +02001106 /* primary_affinity */
1107 if (struct_v >= 2) {
1108 err = decode_primary_affinity(p, end, map);
1109 if (err)
1110 goto bad;
1111 } else {
1112 /* XXX can this happen? */
1113 kfree(map->osd_primary_affinity);
1114 map->osd_primary_affinity = NULL;
1115 }
1116
Sage Weilf24e9982009-10-06 11:31:10 -07001117 /* crush */
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001118 ceph_decode_32_safe(p, end, len, e_inval);
Ilya Dryomov9902e682014-03-13 16:36:15 +02001119 map->crush = crush_decode(*p, min(*p + len, end));
Sage Weilf24e9982009-10-06 11:31:10 -07001120 if (IS_ERR(map->crush)) {
1121 err = PTR_ERR(map->crush);
1122 map->crush = NULL;
1123 goto bad;
1124 }
Ilya Dryomov9902e682014-03-13 16:36:15 +02001125 *p += len;
Sage Weilf24e9982009-10-06 11:31:10 -07001126
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001127 /* ignore the rest */
Sage Weilf24e9982009-10-06 11:31:10 -07001128 *p = end;
1129
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001130 dout("full osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd);
Ilya Dryomova2505d62014-03-13 16:36:13 +02001131 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001132
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001133e_inval:
1134 err = -EINVAL;
Sage Weilf24e9982009-10-06 11:31:10 -07001135bad:
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001136 pr_err("corrupt full osdmap (%d) epoch %d off %d (%p of %p-%p)\n",
1137 err, epoch, (int)(*p - start), *p, start, end);
1138 print_hex_dump(KERN_DEBUG, "osdmap: ",
1139 DUMP_PREFIX_OFFSET, 16, 1,
1140 start, end - start, true);
Ilya Dryomova2505d62014-03-13 16:36:13 +02001141 return err;
1142}
1143
1144/*
1145 * Allocate and decode a full map.
1146 */
1147struct ceph_osdmap *ceph_osdmap_decode(void **p, void *end)
1148{
1149 struct ceph_osdmap *map;
1150 int ret;
1151
1152 map = kzalloc(sizeof(*map), GFP_NOFS);
1153 if (!map)
1154 return ERR_PTR(-ENOMEM);
1155
1156 map->pg_temp = RB_ROOT;
Ilya Dryomov9686f942014-03-21 19:05:29 +02001157 map->primary_temp = RB_ROOT;
Ilya Dryomova2505d62014-03-13 16:36:13 +02001158 mutex_init(&map->crush_scratch_mutex);
1159
1160 ret = osdmap_decode(p, end, map);
1161 if (ret) {
1162 ceph_osdmap_destroy(map);
1163 return ERR_PTR(ret);
1164 }
1165
1166 return map;
Sage Weilf24e9982009-10-06 11:31:10 -07001167}
1168
1169/*
1170 * decode and apply an incremental map update.
1171 */
1172struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end,
1173 struct ceph_osdmap *map,
1174 struct ceph_messenger *msgr)
1175{
Sage Weilf24e9982009-10-06 11:31:10 -07001176 struct crush_map *newcrush = NULL;
1177 struct ceph_fsid fsid;
1178 u32 epoch = 0;
1179 struct ceph_timespec modified;
Sage Weil4f6a7e52013-02-23 10:41:09 -08001180 s32 len;
1181 u64 pool;
1182 __s64 new_pool_max;
1183 __s32 new_flags, max;
Sage Weilf24e9982009-10-06 11:31:10 -07001184 void *start = *p;
Ilya Dryomov86f17422014-03-13 16:36:15 +02001185 int err;
Ilya Dryomovec7af972014-03-21 19:05:29 +02001186 u8 struct_v;
Sage Weilf24e9982009-10-06 11:31:10 -07001187
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001188 dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p));
1189
Ilya Dryomovec7af972014-03-21 19:05:29 +02001190 err = get_osdmap_client_data_v(p, end, "inc", &struct_v);
1191 if (err)
1192 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001193
Ilya Dryomov53bbaba2014-03-13 16:36:16 +02001194 /* fsid, epoch, modified, new_pool_max, new_flags */
1195 ceph_decode_need(p, end, sizeof(fsid) + sizeof(u32) + sizeof(modified) +
1196 sizeof(u64) + sizeof(u32), e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001197 ceph_decode_copy(p, &fsid, sizeof(fsid));
Sage Weilc89136e2009-10-14 09:59:09 -07001198 epoch = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -07001199 BUG_ON(epoch != map->epoch+1);
1200 ceph_decode_copy(p, &modified, sizeof(modified));
Sage Weil4f6a7e52013-02-23 10:41:09 -08001201 new_pool_max = ceph_decode_64(p);
Sage Weilc89136e2009-10-14 09:59:09 -07001202 new_flags = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -07001203
1204 /* full map? */
Ilya Dryomov86f17422014-03-13 16:36:15 +02001205 ceph_decode_32_safe(p, end, len, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001206 if (len > 0) {
1207 dout("apply_incremental full map len %d, %p to %p\n",
1208 len, *p, end);
Ilya Dryomova2505d62014-03-13 16:36:13 +02001209 return ceph_osdmap_decode(p, min(*p+len, end));
Sage Weilf24e9982009-10-06 11:31:10 -07001210 }
1211
1212 /* new crush? */
Ilya Dryomov86f17422014-03-13 16:36:15 +02001213 ceph_decode_32_safe(p, end, len, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001214 if (len > 0) {
Sage Weilf24e9982009-10-06 11:31:10 -07001215 newcrush = crush_decode(*p, min(*p+len, end));
Ilya Dryomov86f17422014-03-13 16:36:15 +02001216 if (IS_ERR(newcrush)) {
1217 err = PTR_ERR(newcrush);
1218 newcrush = NULL;
1219 goto bad;
1220 }
Sage Weilcebc5be2010-06-17 10:22:48 -07001221 *p += len;
Sage Weilf24e9982009-10-06 11:31:10 -07001222 }
1223
1224 /* new flags? */
1225 if (new_flags >= 0)
1226 map->flags = new_flags;
Sage Weil4fc51be2010-02-16 15:55:03 -08001227 if (new_pool_max >= 0)
1228 map->pool_max = new_pool_max;
Sage Weilf24e9982009-10-06 11:31:10 -07001229
Sage Weilf24e9982009-10-06 11:31:10 -07001230 /* new max? */
Ilya Dryomov53bbaba2014-03-13 16:36:16 +02001231 ceph_decode_32_safe(p, end, max, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001232 if (max >= 0) {
1233 err = osdmap_set_max_osd(map, max);
Ilya Dryomov86f17422014-03-13 16:36:15 +02001234 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001235 goto bad;
1236 }
1237
1238 map->epoch++;
Sage Weil31456662011-05-12 15:18:43 -07001239 map->modified = modified;
Sage Weilf24e9982009-10-06 11:31:10 -07001240 if (newcrush) {
1241 if (map->crush)
1242 crush_destroy(map->crush);
1243 map->crush = newcrush;
1244 newcrush = NULL;
1245 }
1246
Ilya Dryomov433fbdd2014-03-21 19:05:27 +02001247 /* new_pools */
1248 err = decode_new_pools(p, end, map);
1249 if (err)
1250 goto bad;
Ilya Dryomov9464d002014-03-13 16:36:16 +02001251
Ilya Dryomov0f70c7e2014-03-21 19:05:27 +02001252 /* new_pool_names */
1253 err = decode_pool_names(p, end, map);
Ilya Dryomov9464d002014-03-13 16:36:16 +02001254 if (err)
1255 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001256
Sage Weil4fc51be2010-02-16 15:55:03 -08001257 /* old_pool */
Ilya Dryomov86f17422014-03-13 16:36:15 +02001258 ceph_decode_32_safe(p, end, len, e_inval);
Sage Weil4fc51be2010-02-16 15:55:03 -08001259 while (len--) {
1260 struct ceph_pg_pool_info *pi;
1261
Ilya Dryomov86f17422014-03-13 16:36:15 +02001262 ceph_decode_64_safe(p, end, pool, e_inval);
Sage Weil4fc51be2010-02-16 15:55:03 -08001263 pi = __lookup_pg_pool(&map->pg_pools, pool);
Sage Weil2844a762010-04-09 15:46:42 -07001264 if (pi)
1265 __remove_pg_pool(&map->pg_pools, pi);
Sage Weil4fc51be2010-02-16 15:55:03 -08001266 }
Sage Weilf24e9982009-10-06 11:31:10 -07001267
1268 /* new_up */
Ilya Dryomov86f17422014-03-13 16:36:15 +02001269 ceph_decode_32_safe(p, end, len, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001270 while (len--) {
1271 u32 osd;
1272 struct ceph_entity_addr addr;
Ilya Dryomov86f17422014-03-13 16:36:15 +02001273 ceph_decode_32_safe(p, end, osd, e_inval);
1274 ceph_decode_copy_safe(p, end, &addr, sizeof(addr), e_inval);
Sage Weil63f2d212009-11-03 15:17:56 -08001275 ceph_decode_addr(&addr);
Sage Weilf24e9982009-10-06 11:31:10 -07001276 pr_info("osd%d up\n", osd);
1277 BUG_ON(osd >= map->max_osd);
1278 map->osd_state[osd] |= CEPH_OSD_UP;
1279 map->osd_addr[osd] = addr;
1280 }
1281
Sage Weil7662d8f2011-05-03 12:52:05 -07001282 /* new_state */
Ilya Dryomov86f17422014-03-13 16:36:15 +02001283 ceph_decode_32_safe(p, end, len, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001284 while (len--) {
1285 u32 osd;
Sage Weil7662d8f2011-05-03 12:52:05 -07001286 u8 xorstate;
Ilya Dryomov86f17422014-03-13 16:36:15 +02001287 ceph_decode_32_safe(p, end, osd, e_inval);
Sage Weil7662d8f2011-05-03 12:52:05 -07001288 xorstate = **(u8 **)p;
Sage Weilf24e9982009-10-06 11:31:10 -07001289 (*p)++; /* clean flag */
Sage Weil7662d8f2011-05-03 12:52:05 -07001290 if (xorstate == 0)
1291 xorstate = CEPH_OSD_UP;
1292 if (xorstate & CEPH_OSD_UP)
1293 pr_info("osd%d down\n", osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001294 if (osd < map->max_osd)
Sage Weil7662d8f2011-05-03 12:52:05 -07001295 map->osd_state[osd] ^= xorstate;
Sage Weilf24e9982009-10-06 11:31:10 -07001296 }
1297
1298 /* new_weight */
Ilya Dryomov86f17422014-03-13 16:36:15 +02001299 ceph_decode_32_safe(p, end, len, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001300 while (len--) {
1301 u32 osd, off;
Ilya Dryomov86f17422014-03-13 16:36:15 +02001302 ceph_decode_need(p, end, sizeof(u32)*2, e_inval);
Sage Weilc89136e2009-10-14 09:59:09 -07001303 osd = ceph_decode_32(p);
1304 off = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -07001305 pr_info("osd%d weight 0x%x %s\n", osd, off,
1306 off == CEPH_OSD_IN ? "(in)" :
1307 (off == CEPH_OSD_OUT ? "(out)" : ""));
1308 if (osd < map->max_osd)
1309 map->osd_weight[osd] = off;
1310 }
1311
1312 /* new_pg_temp */
Ilya Dryomov10db6342014-03-21 19:05:28 +02001313 err = decode_new_pg_temp(p, end, map);
1314 if (err)
1315 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001316
Ilya Dryomovd286de72014-03-21 19:05:30 +02001317 /* new_primary_temp */
1318 if (struct_v >= 1) {
1319 err = decode_new_primary_temp(p, end, map);
1320 if (err)
1321 goto bad;
1322 }
1323
Ilya Dryomov63a69932014-03-21 19:05:30 +02001324 /* new_primary_affinity */
1325 if (struct_v >= 2) {
1326 err = decode_new_primary_affinity(p, end, map);
1327 if (err)
1328 goto bad;
1329 }
1330
Sage Weilf24e9982009-10-06 11:31:10 -07001331 /* ignore the rest */
1332 *p = end;
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001333
1334 dout("inc osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001335 return map;
1336
Ilya Dryomov86f17422014-03-13 16:36:15 +02001337e_inval:
1338 err = -EINVAL;
Sage Weilf24e9982009-10-06 11:31:10 -07001339bad:
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001340 pr_err("corrupt inc osdmap (%d) epoch %d off %d (%p of %p-%p)\n",
1341 err, epoch, (int)(*p - start), *p, start, end);
Sage Weil9ec7cab2009-12-14 15:13:47 -08001342 print_hex_dump(KERN_DEBUG, "osdmap: ",
1343 DUMP_PREFIX_OFFSET, 16, 1,
1344 start, end - start, true);
Sage Weilf24e9982009-10-06 11:31:10 -07001345 if (newcrush)
1346 crush_destroy(newcrush);
1347 return ERR_PTR(err);
1348}
1349
1350
1351
1352
1353/*
1354 * calculate file layout from given offset, length.
1355 * fill in correct oid, logical length, and object extent
1356 * offset, length.
1357 *
1358 * for now, we write only a single su, until we can
1359 * pass a stride back to the caller.
1360 */
Sage Weild63b77f2012-09-24 20:59:48 -07001361int ceph_calc_file_object_mapping(struct ceph_file_layout *layout,
Alex Eldere8afad62012-11-14 09:38:19 -06001362 u64 off, u64 len,
Sage Weil645a1022009-10-28 15:15:05 -07001363 u64 *ono,
Sage Weilf24e9982009-10-06 11:31:10 -07001364 u64 *oxoff, u64 *oxlen)
1365{
1366 u32 osize = le32_to_cpu(layout->fl_object_size);
1367 u32 su = le32_to_cpu(layout->fl_stripe_unit);
1368 u32 sc = le32_to_cpu(layout->fl_stripe_count);
1369 u32 bl, stripeno, stripepos, objsetno;
1370 u32 su_per_object;
Noah Watkinsff1d1f72009-10-30 12:57:30 -07001371 u64 t, su_offset;
Sage Weilf24e9982009-10-06 11:31:10 -07001372
Alex Eldere8afad62012-11-14 09:38:19 -06001373 dout("mapping %llu~%llu osize %u fl_su %u\n", off, len,
Sage Weilf24e9982009-10-06 11:31:10 -07001374 osize, su);
Sage Weild63b77f2012-09-24 20:59:48 -07001375 if (su == 0 || sc == 0)
1376 goto invalid;
Noah Watkins35e054a2009-10-28 14:04:48 -07001377 su_per_object = osize / su;
Sage Weild63b77f2012-09-24 20:59:48 -07001378 if (su_per_object == 0)
1379 goto invalid;
Sage Weilf24e9982009-10-06 11:31:10 -07001380 dout("osize %u / su %u = su_per_object %u\n", osize, su,
1381 su_per_object);
1382
Sage Weild63b77f2012-09-24 20:59:48 -07001383 if ((su & ~PAGE_MASK) != 0)
1384 goto invalid;
1385
Sage Weilf24e9982009-10-06 11:31:10 -07001386 /* bl = *off / su; */
1387 t = off;
1388 do_div(t, su);
1389 bl = t;
1390 dout("off %llu / su %u = bl %u\n", off, su, bl);
1391
1392 stripeno = bl / sc;
1393 stripepos = bl % sc;
1394 objsetno = stripeno / su_per_object;
1395
Sage Weil645a1022009-10-28 15:15:05 -07001396 *ono = objsetno * sc + stripepos;
Eric Dumazet95c96172012-04-15 05:58:06 +00001397 dout("objset %u * sc %u = ono %u\n", objsetno, sc, (unsigned int)*ono);
Sage Weil645a1022009-10-28 15:15:05 -07001398
1399 /* *oxoff = *off % layout->fl_stripe_unit; # offset in su */
Sage Weilf24e9982009-10-06 11:31:10 -07001400 t = off;
Noah Watkinsff1d1f72009-10-30 12:57:30 -07001401 su_offset = do_div(t, su);
1402 *oxoff = su_offset + (stripeno % su_per_object) * su;
Sage Weil645a1022009-10-28 15:15:05 -07001403
Noah Watkinsff1d1f72009-10-30 12:57:30 -07001404 /*
1405 * Calculate the length of the extent being written to the selected
Alex Eldere8afad62012-11-14 09:38:19 -06001406 * object. This is the minimum of the full length requested (len) or
Noah Watkinsff1d1f72009-10-30 12:57:30 -07001407 * the remainder of the current stripe being written to.
1408 */
Alex Eldere8afad62012-11-14 09:38:19 -06001409 *oxlen = min_t(u64, len, su - su_offset);
Sage Weilf24e9982009-10-06 11:31:10 -07001410
1411 dout(" obj extent %llu~%llu\n", *oxoff, *oxlen);
Sage Weild63b77f2012-09-24 20:59:48 -07001412 return 0;
1413
1414invalid:
1415 dout(" invalid layout\n");
1416 *ono = 0;
1417 *oxoff = 0;
1418 *oxlen = 0;
1419 return -EINVAL;
Sage Weilf24e9982009-10-06 11:31:10 -07001420}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001421EXPORT_SYMBOL(ceph_calc_file_object_mapping);
Sage Weilf24e9982009-10-06 11:31:10 -07001422
1423/*
Ilya Dryomov7c13cb62014-01-27 17:40:19 +02001424 * Calculate mapping of a (oloc, oid) pair to a PG. Should only be
1425 * called with target's (oloc, oid), since tiering isn't taken into
1426 * account.
Sage Weilf24e9982009-10-06 11:31:10 -07001427 */
Ilya Dryomov7c13cb62014-01-27 17:40:19 +02001428int ceph_oloc_oid_to_pg(struct ceph_osdmap *osdmap,
1429 struct ceph_object_locator *oloc,
1430 struct ceph_object_id *oid,
1431 struct ceph_pg *pg_out)
Sage Weilf24e9982009-10-06 11:31:10 -07001432{
Ilya Dryomov7c13cb62014-01-27 17:40:19 +02001433 struct ceph_pg_pool_info *pi;
Sage Weilf24e9982009-10-06 11:31:10 -07001434
Ilya Dryomov7c13cb62014-01-27 17:40:19 +02001435 pi = __lookup_pg_pool(&osdmap->pg_pools, oloc->pool);
1436 if (!pi)
Sage Weil4fc51be2010-02-16 15:55:03 -08001437 return -EIO;
Sage Weilf24e9982009-10-06 11:31:10 -07001438
Ilya Dryomov7c13cb62014-01-27 17:40:19 +02001439 pg_out->pool = oloc->pool;
1440 pg_out->seed = ceph_str_hash(pi->object_hash, oid->name,
1441 oid->name_len);
1442
1443 dout("%s '%.*s' pgid %llu.%x\n", __func__, oid->name_len, oid->name,
1444 pg_out->pool, pg_out->seed);
Sage Weilf24e9982009-10-06 11:31:10 -07001445 return 0;
1446}
Ilya Dryomov7c13cb62014-01-27 17:40:19 +02001447EXPORT_SYMBOL(ceph_oloc_oid_to_pg);
Sage Weilf24e9982009-10-06 11:31:10 -07001448
Ilya Dryomov9d521472014-01-31 17:54:26 +02001449static int do_crush(struct ceph_osdmap *map, int ruleno, int x,
1450 int *result, int result_max,
1451 const __u32 *weight, int weight_max)
Ilya Dryomove8ef19c2013-12-24 21:19:24 +02001452{
Ilya Dryomov9d521472014-01-31 17:54:26 +02001453 int r;
Ilya Dryomove8ef19c2013-12-24 21:19:24 +02001454
Ilya Dryomov9d521472014-01-31 17:54:26 +02001455 BUG_ON(result_max > CEPH_PG_MAX_SIZE);
1456
1457 mutex_lock(&map->crush_scratch_mutex);
1458 r = crush_do_rule(map->crush, ruleno, x, result, result_max,
1459 weight, weight_max, map->crush_scratch_ary);
1460 mutex_unlock(&map->crush_scratch_mutex);
1461
1462 return r;
Ilya Dryomove8ef19c2013-12-24 21:19:24 +02001463}
1464
Sage Weilf24e9982009-10-06 11:31:10 -07001465/*
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02001466 * Calculate raw (crush) set for given pgid.
1467 *
1468 * Return raw set length, or error.
1469 */
1470static int pg_to_raw_osds(struct ceph_osdmap *osdmap,
1471 struct ceph_pg_pool_info *pool,
1472 struct ceph_pg pgid, u32 pps, int *osds)
1473{
1474 int ruleno;
1475 int len;
1476
1477 /* crush */
1478 ruleno = crush_find_rule(osdmap->crush, pool->crush_ruleset,
1479 pool->type, pool->size);
1480 if (ruleno < 0) {
1481 pr_err("no crush rule: pool %lld ruleset %d type %d size %d\n",
1482 pgid.pool, pool->crush_ruleset, pool->type,
1483 pool->size);
1484 return -ENOENT;
1485 }
1486
1487 len = do_crush(osdmap, ruleno, pps, osds,
1488 min_t(int, pool->size, CEPH_PG_MAX_SIZE),
1489 osdmap->osd_weight, osdmap->max_osd);
1490 if (len < 0) {
1491 pr_err("error %d from crush rule %d: pool %lld ruleset %d type %d size %d\n",
1492 len, ruleno, pgid.pool, pool->crush_ruleset,
1493 pool->type, pool->size);
1494 return len;
1495 }
1496
1497 return len;
1498}
1499
1500/*
1501 * Given raw set, calculate up set and up primary.
1502 *
1503 * Return up set length. *primary is set to up primary osd id, or -1
1504 * if up set is empty.
1505 */
1506static int raw_to_up_osds(struct ceph_osdmap *osdmap,
1507 struct ceph_pg_pool_info *pool,
1508 int *osds, int len, int *primary)
1509{
1510 int up_primary = -1;
1511 int i;
1512
1513 if (ceph_can_shift_osds(pool)) {
1514 int removed = 0;
1515
1516 for (i = 0; i < len; i++) {
1517 if (ceph_osd_is_down(osdmap, osds[i])) {
1518 removed++;
1519 continue;
1520 }
1521 if (removed)
1522 osds[i - removed] = osds[i];
1523 }
1524
1525 len -= removed;
1526 if (len > 0)
1527 up_primary = osds[0];
1528 } else {
1529 for (i = len - 1; i >= 0; i--) {
1530 if (ceph_osd_is_down(osdmap, osds[i]))
1531 osds[i] = CRUSH_ITEM_NONE;
1532 else
1533 up_primary = osds[i];
1534 }
1535 }
1536
1537 *primary = up_primary;
1538 return len;
1539}
1540
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02001541static void apply_primary_affinity(struct ceph_osdmap *osdmap, u32 pps,
1542 struct ceph_pg_pool_info *pool,
1543 int *osds, int len, int *primary)
1544{
1545 int i;
1546 int pos = -1;
1547
1548 /*
1549 * Do we have any non-default primary_affinity values for these
1550 * osds?
1551 */
1552 if (!osdmap->osd_primary_affinity)
1553 return;
1554
1555 for (i = 0; i < len; i++) {
Ilya Dryomov92b2e752014-04-10 18:09:41 +04001556 int osd = osds[i];
1557
1558 if (osd != CRUSH_ITEM_NONE &&
1559 osdmap->osd_primary_affinity[osd] !=
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02001560 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY) {
1561 break;
1562 }
1563 }
1564 if (i == len)
1565 return;
1566
1567 /*
1568 * Pick the primary. Feed both the seed (for the pg) and the
1569 * osd into the hash/rng so that a proportional fraction of an
1570 * osd's pgs get rejected as primary.
1571 */
1572 for (i = 0; i < len; i++) {
Ilya Dryomov92b2e752014-04-10 18:09:41 +04001573 int osd = osds[i];
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02001574 u32 aff;
1575
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02001576 if (osd == CRUSH_ITEM_NONE)
1577 continue;
1578
1579 aff = osdmap->osd_primary_affinity[osd];
1580 if (aff < CEPH_OSD_MAX_PRIMARY_AFFINITY &&
1581 (crush_hash32_2(CRUSH_HASH_RJENKINS1,
1582 pps, osd) >> 16) >= aff) {
1583 /*
1584 * We chose not to use this primary. Note it
1585 * anyway as a fallback in case we don't pick
1586 * anyone else, but keep looking.
1587 */
1588 if (pos < 0)
1589 pos = i;
1590 } else {
1591 pos = i;
1592 break;
1593 }
1594 }
1595 if (pos < 0)
1596 return;
1597
1598 *primary = osds[pos];
1599
1600 if (ceph_can_shift_osds(pool) && pos > 0) {
1601 /* move the new primary to the front */
1602 for (i = pos; i > 0; i--)
1603 osds[i] = osds[i - 1];
1604 osds[0] = *primary;
1605 }
1606}
1607
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02001608/*
Ilya Dryomov5e8d4d32014-03-24 17:12:48 +02001609 * Given up set, apply pg_temp and primary_temp mappings.
Ilya Dryomov45966c32014-03-24 17:12:47 +02001610 *
1611 * Return acting set length. *primary is set to acting primary osd id,
1612 * or -1 if acting set is empty.
1613 */
1614static int apply_temps(struct ceph_osdmap *osdmap,
1615 struct ceph_pg_pool_info *pool, struct ceph_pg pgid,
1616 int *osds, int len, int *primary)
1617{
1618 struct ceph_pg_mapping *pg;
1619 int temp_len;
1620 int temp_primary;
1621 int i;
1622
1623 /* raw_pg -> pg */
1624 pgid.seed = ceph_stable_mod(pgid.seed, pool->pg_num,
1625 pool->pg_num_mask);
1626
1627 /* pg_temp? */
1628 pg = __lookup_pg_mapping(&osdmap->pg_temp, pgid);
1629 if (pg) {
1630 temp_len = 0;
1631 temp_primary = -1;
1632
1633 for (i = 0; i < pg->pg_temp.len; i++) {
1634 if (ceph_osd_is_down(osdmap, pg->pg_temp.osds[i])) {
1635 if (ceph_can_shift_osds(pool))
1636 continue;
1637 else
1638 osds[temp_len++] = CRUSH_ITEM_NONE;
1639 } else {
1640 osds[temp_len++] = pg->pg_temp.osds[i];
1641 }
1642 }
1643
1644 /* apply pg_temp's primary */
1645 for (i = 0; i < temp_len; i++) {
1646 if (osds[i] != CRUSH_ITEM_NONE) {
1647 temp_primary = osds[i];
1648 break;
1649 }
1650 }
1651 } else {
1652 temp_len = len;
1653 temp_primary = *primary;
1654 }
1655
Ilya Dryomov5e8d4d32014-03-24 17:12:48 +02001656 /* primary_temp? */
1657 pg = __lookup_pg_mapping(&osdmap->primary_temp, pgid);
1658 if (pg)
1659 temp_primary = pg->primary_temp.osd;
1660
Ilya Dryomov45966c32014-03-24 17:12:47 +02001661 *primary = temp_primary;
1662 return temp_len;
1663}
1664
1665/*
Ilya Dryomovac972232014-03-24 17:12:48 +02001666 * Calculate acting set for given pgid.
1667 *
Ilya Dryomov8008ab12014-03-24 17:12:48 +02001668 * Return acting set length, or error. *primary is set to acting
1669 * primary osd id, or -1 if acting set is empty or on error.
Sage Weild85b7052010-05-10 10:24:48 -07001670 */
Sage Weil5b191d92013-02-23 10:38:16 -08001671int ceph_calc_pg_acting(struct ceph_osdmap *osdmap, struct ceph_pg pgid,
Ilya Dryomov8008ab12014-03-24 17:12:48 +02001672 int *osds, int *primary)
Sage Weild85b7052010-05-10 10:24:48 -07001673{
Ilya Dryomovac972232014-03-24 17:12:48 +02001674 struct ceph_pg_pool_info *pool;
1675 u32 pps;
1676 int len;
Sage Weild85b7052010-05-10 10:24:48 -07001677
Ilya Dryomovac972232014-03-24 17:12:48 +02001678 pool = __lookup_pg_pool(&osdmap->pg_pools, pgid.pool);
Ilya Dryomov8008ab12014-03-24 17:12:48 +02001679 if (!pool) {
1680 *primary = -1;
1681 return -ENOENT;
1682 }
Sage Weild85b7052010-05-10 10:24:48 -07001683
Ilya Dryomovac972232014-03-24 17:12:48 +02001684 if (pool->flags & CEPH_POOL_FLAG_HASHPSPOOL) {
1685 /* hash pool id and seed so that pool PGs do not overlap */
1686 pps = crush_hash32_2(CRUSH_HASH_RJENKINS1,
1687 ceph_stable_mod(pgid.seed, pool->pgp_num,
1688 pool->pgp_num_mask),
1689 pgid.pool);
1690 } else {
1691 /*
1692 * legacy behavior: add ps and pool together. this is
1693 * not a great approach because the PGs from each pool
1694 * will overlap on top of each other: 0.5 == 1.4 ==
1695 * 2.3 == ...
1696 */
1697 pps = ceph_stable_mod(pgid.seed, pool->pgp_num,
1698 pool->pgp_num_mask) +
1699 (unsigned)pgid.pool;
1700 }
1701
1702 len = pg_to_raw_osds(osdmap, pool, pgid, pps, osds);
Ilya Dryomov8008ab12014-03-24 17:12:48 +02001703 if (len < 0) {
1704 *primary = -1;
Ilya Dryomovac972232014-03-24 17:12:48 +02001705 return len;
Ilya Dryomov8008ab12014-03-24 17:12:48 +02001706 }
Ilya Dryomovac972232014-03-24 17:12:48 +02001707
Ilya Dryomov8008ab12014-03-24 17:12:48 +02001708 len = raw_to_up_osds(osdmap, pool, osds, len, primary);
Ilya Dryomovac972232014-03-24 17:12:48 +02001709
Ilya Dryomov47ec1f32014-03-24 17:12:49 +02001710 apply_primary_affinity(osdmap, pps, pool, osds, len, primary);
1711
Ilya Dryomov8008ab12014-03-24 17:12:48 +02001712 len = apply_temps(osdmap, pool, pgid, osds, len, primary);
Ilya Dryomovac972232014-03-24 17:12:48 +02001713
1714 return len;
Sage Weild85b7052010-05-10 10:24:48 -07001715}
1716
1717/*
Sage Weilf24e9982009-10-06 11:31:10 -07001718 * Return primary osd for given pgid, or -1 if none.
1719 */
Sage Weil5b191d92013-02-23 10:38:16 -08001720int ceph_calc_pg_primary(struct ceph_osdmap *osdmap, struct ceph_pg pgid)
Sage Weilf24e9982009-10-06 11:31:10 -07001721{
Ilya Dryomovc4c12282014-03-24 17:12:49 +02001722 int osds[CEPH_PG_MAX_SIZE];
1723 int primary;
Sage Weilf24e9982009-10-06 11:31:10 -07001724
Ilya Dryomovc4c12282014-03-24 17:12:49 +02001725 ceph_calc_pg_acting(osdmap, pgid, osds, &primary);
Sage Weilf24e9982009-10-06 11:31:10 -07001726
Ilya Dryomovc4c12282014-03-24 17:12:49 +02001727 return primary;
Sage Weilf24e9982009-10-06 11:31:10 -07001728}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001729EXPORT_SYMBOL(ceph_calc_pg_primary);