blob: 20a38a37794cedd554c23f70ca97e02748e3d4a1 [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
Sage Weil546f04e2012-07-30 18:15:23 -0700332done:
Sage Weilf24e9982009-10-06 11:31:10 -0700333 dout("crush_decode success\n");
334 return c;
335
336badmem:
337 err = -ENOMEM;
338bad:
339 dout("crush_decode fail %d\n", err);
340 crush_destroy(c);
341 return ERR_PTR(err);
342}
343
Sage Weilf24e9982009-10-06 11:31:10 -0700344/*
Sage Weil9794b142010-02-16 15:53:32 -0800345 * rbtree of pg_mapping for handling pg_temp (explicit mapping of pgid
Ilya Dryomov9686f942014-03-21 19:05:29 +0200346 * to a set of osds) and primary_temp (explicit primary setting)
Sage Weilf24e9982009-10-06 11:31:10 -0700347 */
Sage Weil5b191d92013-02-23 10:38:16 -0800348static int pgid_cmp(struct ceph_pg l, struct ceph_pg r)
Sage Weil51042122009-11-04 11:39:12 -0800349{
Sage Weil5b191d92013-02-23 10:38:16 -0800350 if (l.pool < r.pool)
Sage Weil51042122009-11-04 11:39:12 -0800351 return -1;
Sage Weil5b191d92013-02-23 10:38:16 -0800352 if (l.pool > r.pool)
353 return 1;
354 if (l.seed < r.seed)
355 return -1;
356 if (l.seed > r.seed)
Sage Weil51042122009-11-04 11:39:12 -0800357 return 1;
358 return 0;
359}
360
Sage Weil991abb62009-10-08 22:22:37 -0700361static int __insert_pg_mapping(struct ceph_pg_mapping *new,
362 struct rb_root *root)
Sage Weilf24e9982009-10-06 11:31:10 -0700363{
364 struct rb_node **p = &root->rb_node;
365 struct rb_node *parent = NULL;
366 struct ceph_pg_mapping *pg = NULL;
Sage Weil51042122009-11-04 11:39:12 -0800367 int c;
Sage Weilf24e9982009-10-06 11:31:10 -0700368
Sage Weil8adc8b32011-09-28 10:11:04 -0700369 dout("__insert_pg_mapping %llx %p\n", *(u64 *)&new->pgid, new);
Sage Weilf24e9982009-10-06 11:31:10 -0700370 while (*p) {
371 parent = *p;
372 pg = rb_entry(parent, struct ceph_pg_mapping, node);
Sage Weil51042122009-11-04 11:39:12 -0800373 c = pgid_cmp(new->pgid, pg->pgid);
374 if (c < 0)
Sage Weilf24e9982009-10-06 11:31:10 -0700375 p = &(*p)->rb_left;
Sage Weil51042122009-11-04 11:39:12 -0800376 else if (c > 0)
Sage Weilf24e9982009-10-06 11:31:10 -0700377 p = &(*p)->rb_right;
378 else
Sage Weil991abb62009-10-08 22:22:37 -0700379 return -EEXIST;
Sage Weilf24e9982009-10-06 11:31:10 -0700380 }
381
382 rb_link_node(&new->node, parent, p);
383 rb_insert_color(&new->node, root);
Sage Weil991abb62009-10-08 22:22:37 -0700384 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700385}
386
Sage Weil9794b142010-02-16 15:53:32 -0800387static struct ceph_pg_mapping *__lookup_pg_mapping(struct rb_root *root,
Sage Weil5b191d92013-02-23 10:38:16 -0800388 struct ceph_pg pgid)
Sage Weil9794b142010-02-16 15:53:32 -0800389{
390 struct rb_node *n = root->rb_node;
391 struct ceph_pg_mapping *pg;
392 int c;
393
394 while (n) {
395 pg = rb_entry(n, struct ceph_pg_mapping, node);
396 c = pgid_cmp(pgid, pg->pgid);
Sage Weil8adc8b32011-09-28 10:11:04 -0700397 if (c < 0) {
Sage Weil9794b142010-02-16 15:53:32 -0800398 n = n->rb_left;
Sage Weil8adc8b32011-09-28 10:11:04 -0700399 } else if (c > 0) {
Sage Weil9794b142010-02-16 15:53:32 -0800400 n = n->rb_right;
Sage Weil8adc8b32011-09-28 10:11:04 -0700401 } else {
Sage Weil5b191d92013-02-23 10:38:16 -0800402 dout("__lookup_pg_mapping %lld.%x got %p\n",
403 pgid.pool, pgid.seed, pg);
Sage Weil9794b142010-02-16 15:53:32 -0800404 return pg;
Sage Weil8adc8b32011-09-28 10:11:04 -0700405 }
Sage Weil9794b142010-02-16 15:53:32 -0800406 }
407 return NULL;
408}
409
Sage Weil5b191d92013-02-23 10:38:16 -0800410static int __remove_pg_mapping(struct rb_root *root, struct ceph_pg pgid)
Sage Weil8adc8b32011-09-28 10:11:04 -0700411{
412 struct ceph_pg_mapping *pg = __lookup_pg_mapping(root, pgid);
413
414 if (pg) {
Sage Weil5b191d92013-02-23 10:38:16 -0800415 dout("__remove_pg_mapping %lld.%x %p\n", pgid.pool, pgid.seed,
416 pg);
Sage Weil8adc8b32011-09-28 10:11:04 -0700417 rb_erase(&pg->node, root);
418 kfree(pg);
419 return 0;
420 }
Sage Weil5b191d92013-02-23 10:38:16 -0800421 dout("__remove_pg_mapping %lld.%x dne\n", pgid.pool, pgid.seed);
Sage Weil8adc8b32011-09-28 10:11:04 -0700422 return -ENOENT;
423}
424
Sage Weilf24e9982009-10-06 11:31:10 -0700425/*
Sage Weil4fc51be2010-02-16 15:55:03 -0800426 * rbtree of pg pool info
427 */
428static int __insert_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *new)
429{
430 struct rb_node **p = &root->rb_node;
431 struct rb_node *parent = NULL;
432 struct ceph_pg_pool_info *pi = NULL;
433
434 while (*p) {
435 parent = *p;
436 pi = rb_entry(parent, struct ceph_pg_pool_info, node);
437 if (new->id < pi->id)
438 p = &(*p)->rb_left;
439 else if (new->id > pi->id)
440 p = &(*p)->rb_right;
441 else
442 return -EEXIST;
443 }
444
445 rb_link_node(&new->node, parent, p);
446 rb_insert_color(&new->node, root);
447 return 0;
448}
449
Sage Weil4f6a7e52013-02-23 10:41:09 -0800450static struct ceph_pg_pool_info *__lookup_pg_pool(struct rb_root *root, u64 id)
Sage Weil4fc51be2010-02-16 15:55:03 -0800451{
452 struct ceph_pg_pool_info *pi;
453 struct rb_node *n = root->rb_node;
454
455 while (n) {
456 pi = rb_entry(n, struct ceph_pg_pool_info, node);
457 if (id < pi->id)
458 n = n->rb_left;
459 else if (id > pi->id)
460 n = n->rb_right;
461 else
462 return pi;
463 }
464 return NULL;
465}
466
Ilya Dryomovce7f6a22014-01-27 17:40:19 +0200467struct ceph_pg_pool_info *ceph_pg_pool_by_id(struct ceph_osdmap *map, u64 id)
468{
469 return __lookup_pg_pool(&map->pg_pools, id);
470}
471
Alex Elder72afc712012-10-30 19:40:33 -0500472const char *ceph_pg_pool_name_by_id(struct ceph_osdmap *map, u64 id)
473{
474 struct ceph_pg_pool_info *pi;
475
476 if (id == CEPH_NOPOOL)
477 return NULL;
478
479 if (WARN_ON_ONCE(id > (u64) INT_MAX))
480 return NULL;
481
482 pi = __lookup_pg_pool(&map->pg_pools, (int) id);
483
484 return pi ? pi->name : NULL;
485}
486EXPORT_SYMBOL(ceph_pg_pool_name_by_id);
487
Yehuda Sadeh7669a2c2010-05-17 12:31:35 -0700488int ceph_pg_poolid_by_name(struct ceph_osdmap *map, const char *name)
489{
490 struct rb_node *rbp;
491
492 for (rbp = rb_first(&map->pg_pools); rbp; rbp = rb_next(rbp)) {
493 struct ceph_pg_pool_info *pi =
494 rb_entry(rbp, struct ceph_pg_pool_info, node);
495 if (pi->name && strcmp(pi->name, name) == 0)
496 return pi->id;
497 }
498 return -ENOENT;
499}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700500EXPORT_SYMBOL(ceph_pg_poolid_by_name);
Yehuda Sadeh7669a2c2010-05-17 12:31:35 -0700501
Sage Weil2844a762010-04-09 15:46:42 -0700502static void __remove_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *pi)
503{
504 rb_erase(&pi->node, root);
505 kfree(pi->name);
506 kfree(pi);
507}
508
Ilya Dryomov0f70c7e2014-03-21 19:05:27 +0200509static int decode_pool(void **p, void *end, struct ceph_pg_pool_info *pi)
Sage Weilefd75762010-03-17 10:05:28 -0700510{
Sage Weil4f6a7e52013-02-23 10:41:09 -0800511 u8 ev, cv;
512 unsigned len, num;
513 void *pool_end;
Sage Weil73a7e692010-08-02 11:00:55 -0700514
Sage Weil4f6a7e52013-02-23 10:41:09 -0800515 ceph_decode_need(p, end, 2 + 4, bad);
516 ev = ceph_decode_8(p); /* encoding version */
517 cv = ceph_decode_8(p); /* compat version */
518 if (ev < 5) {
519 pr_warning("got v %d < 5 cv %d of ceph_pg_pool\n", ev, cv);
520 return -EINVAL;
521 }
Ilya Dryomov17a13e402014-01-27 17:40:19 +0200522 if (cv > 9) {
523 pr_warning("got v %d cv %d > 9 of ceph_pg_pool\n", ev, cv);
Sage Weil4f6a7e52013-02-23 10:41:09 -0800524 return -EINVAL;
525 }
526 len = ceph_decode_32(p);
527 ceph_decode_need(p, end, len, bad);
528 pool_end = *p + len;
Sage Weil73a7e692010-08-02 11:00:55 -0700529
Sage Weil4f6a7e52013-02-23 10:41:09 -0800530 pi->type = ceph_decode_8(p);
531 pi->size = ceph_decode_8(p);
532 pi->crush_ruleset = ceph_decode_8(p);
533 pi->object_hash = ceph_decode_8(p);
534
535 pi->pg_num = ceph_decode_32(p);
536 pi->pgp_num = ceph_decode_32(p);
537
538 *p += 4 + 4; /* skip lpg* */
539 *p += 4; /* skip last_change */
540 *p += 8 + 4; /* skip snap_seq, snap_epoch */
541
542 /* skip snaps */
543 num = ceph_decode_32(p);
544 while (num--) {
545 *p += 8; /* snapid key */
546 *p += 1 + 1; /* versions */
547 len = ceph_decode_32(p);
548 *p += len;
Sage Weil73a7e692010-08-02 11:00:55 -0700549 }
550
Ilya Dryomov17a13e402014-01-27 17:40:19 +0200551 /* skip removed_snaps */
Sage Weil4f6a7e52013-02-23 10:41:09 -0800552 num = ceph_decode_32(p);
553 *p += num * (8 + 8);
554
555 *p += 8; /* skip auid */
556 pi->flags = ceph_decode_64(p);
Ilya Dryomov17a13e402014-01-27 17:40:19 +0200557 *p += 4; /* skip crash_replay_interval */
558
559 if (ev >= 7)
560 *p += 1; /* skip min_size */
561
562 if (ev >= 8)
563 *p += 8 + 8; /* skip quota_max_* */
564
565 if (ev >= 9) {
566 /* skip tiers */
567 num = ceph_decode_32(p);
568 *p += num * 8;
569
570 *p += 8; /* skip tier_of */
571 *p += 1; /* skip cache_mode */
572
573 pi->read_tier = ceph_decode_64(p);
574 pi->write_tier = ceph_decode_64(p);
575 } else {
576 pi->read_tier = -1;
577 pi->write_tier = -1;
578 }
Sage Weil4f6a7e52013-02-23 10:41:09 -0800579
580 /* ignore the rest */
581
582 *p = pool_end;
583 calc_pg_masks(pi);
Sage Weil73a7e692010-08-02 11:00:55 -0700584 return 0;
585
586bad:
587 return -EINVAL;
Sage Weilefd75762010-03-17 10:05:28 -0700588}
589
Ilya Dryomov0f70c7e2014-03-21 19:05:27 +0200590static int decode_pool_names(void **p, void *end, struct ceph_osdmap *map)
Sage Weil2844a762010-04-09 15:46:42 -0700591{
592 struct ceph_pg_pool_info *pi;
Sage Weil4f6a7e52013-02-23 10:41:09 -0800593 u32 num, len;
594 u64 pool;
Sage Weil2844a762010-04-09 15:46:42 -0700595
596 ceph_decode_32_safe(p, end, num, bad);
597 dout(" %d pool names\n", num);
598 while (num--) {
Sage Weil4f6a7e52013-02-23 10:41:09 -0800599 ceph_decode_64_safe(p, end, pool, bad);
Sage Weil2844a762010-04-09 15:46:42 -0700600 ceph_decode_32_safe(p, end, len, bad);
Sage Weil4f6a7e52013-02-23 10:41:09 -0800601 dout(" pool %llu len %d\n", pool, len);
Xi Wangad3b9042012-06-06 19:35:55 -0500602 ceph_decode_need(p, end, len, bad);
Sage Weil2844a762010-04-09 15:46:42 -0700603 pi = __lookup_pg_pool(&map->pg_pools, pool);
604 if (pi) {
Xi Wangad3b9042012-06-06 19:35:55 -0500605 char *name = kstrndup(*p, len, GFP_NOFS);
606
607 if (!name)
608 return -ENOMEM;
Sage Weil2844a762010-04-09 15:46:42 -0700609 kfree(pi->name);
Xi Wangad3b9042012-06-06 19:35:55 -0500610 pi->name = name;
611 dout(" name is %s\n", pi->name);
Sage Weil2844a762010-04-09 15:46:42 -0700612 }
613 *p += len;
614 }
615 return 0;
616
617bad:
618 return -EINVAL;
619}
620
621/*
622 * osd map
623 */
624void ceph_osdmap_destroy(struct ceph_osdmap *map)
625{
626 dout("osdmap_destroy %p\n", map);
627 if (map->crush)
628 crush_destroy(map->crush);
629 while (!RB_EMPTY_ROOT(&map->pg_temp)) {
630 struct ceph_pg_mapping *pg =
631 rb_entry(rb_first(&map->pg_temp),
632 struct ceph_pg_mapping, node);
633 rb_erase(&pg->node, &map->pg_temp);
634 kfree(pg);
635 }
Ilya Dryomov9686f942014-03-21 19:05:29 +0200636 while (!RB_EMPTY_ROOT(&map->primary_temp)) {
637 struct ceph_pg_mapping *pg =
638 rb_entry(rb_first(&map->primary_temp),
639 struct ceph_pg_mapping, node);
640 rb_erase(&pg->node, &map->primary_temp);
641 kfree(pg);
642 }
Sage Weil2844a762010-04-09 15:46:42 -0700643 while (!RB_EMPTY_ROOT(&map->pg_pools)) {
644 struct ceph_pg_pool_info *pi =
645 rb_entry(rb_first(&map->pg_pools),
646 struct ceph_pg_pool_info, node);
647 __remove_pg_pool(&map->pg_pools, pi);
648 }
649 kfree(map->osd_state);
650 kfree(map->osd_weight);
651 kfree(map->osd_addr);
Ilya Dryomov2cfa34f2014-03-21 19:05:30 +0200652 kfree(map->osd_primary_affinity);
Sage Weil2844a762010-04-09 15:46:42 -0700653 kfree(map);
654}
655
656/*
Ilya Dryomov4d603512014-03-21 19:05:28 +0200657 * Adjust max_osd value, (re)allocate arrays.
658 *
659 * The new elements are properly initialized.
Sage Weil2844a762010-04-09 15:46:42 -0700660 */
661static int osdmap_set_max_osd(struct ceph_osdmap *map, int max)
662{
663 u8 *state;
Sage Weil2844a762010-04-09 15:46:42 -0700664 u32 *weight;
Ilya Dryomov4d603512014-03-21 19:05:28 +0200665 struct ceph_entity_addr *addr;
666 int i;
Sage Weil2844a762010-04-09 15:46:42 -0700667
Ilya Dryomov4d603512014-03-21 19:05:28 +0200668 state = krealloc(map->osd_state, max*sizeof(*state), GFP_NOFS);
669 weight = krealloc(map->osd_weight, max*sizeof(*weight), GFP_NOFS);
670 addr = krealloc(map->osd_addr, max*sizeof(*addr), GFP_NOFS);
671 if (!state || !weight || !addr) {
Sage Weil2844a762010-04-09 15:46:42 -0700672 kfree(state);
Sage Weil2844a762010-04-09 15:46:42 -0700673 kfree(weight);
Ilya Dryomov4d603512014-03-21 19:05:28 +0200674 kfree(addr);
675
Sage Weil2844a762010-04-09 15:46:42 -0700676 return -ENOMEM;
677 }
678
Ilya Dryomov4d603512014-03-21 19:05:28 +0200679 for (i = map->max_osd; i < max; i++) {
680 state[i] = 0;
681 weight[i] = CEPH_OSD_OUT;
682 memset(addr + i, 0, sizeof(*addr));
Sage Weil2844a762010-04-09 15:46:42 -0700683 }
684
685 map->osd_state = state;
686 map->osd_weight = weight;
687 map->osd_addr = addr;
Ilya Dryomov4d603512014-03-21 19:05:28 +0200688
Ilya Dryomov2cfa34f2014-03-21 19:05:30 +0200689 if (map->osd_primary_affinity) {
690 u32 *affinity;
691
692 affinity = krealloc(map->osd_primary_affinity,
693 max*sizeof(*affinity), GFP_NOFS);
694 if (!affinity)
695 return -ENOMEM;
696
697 for (i = map->max_osd; i < max; i++)
698 affinity[i] = CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
699
700 map->osd_primary_affinity = affinity;
701 }
702
Sage Weil2844a762010-04-09 15:46:42 -0700703 map->max_osd = max;
Ilya Dryomov4d603512014-03-21 19:05:28 +0200704
Sage Weil2844a762010-04-09 15:46:42 -0700705 return 0;
706}
707
Ilya Dryomovec7af972014-03-21 19:05:29 +0200708#define OSDMAP_WRAPPER_COMPAT_VER 7
709#define OSDMAP_CLIENT_DATA_COMPAT_VER 1
710
711/*
712 * Return 0 or error. On success, *v is set to 0 for old (v6) osdmaps,
713 * to struct_v of the client_data section for new (v7 and above)
714 * osdmaps.
715 */
716static int get_osdmap_client_data_v(void **p, void *end,
717 const char *prefix, u8 *v)
718{
719 u8 struct_v;
720
721 ceph_decode_8_safe(p, end, struct_v, e_inval);
722 if (struct_v >= 7) {
723 u8 struct_compat;
724
725 ceph_decode_8_safe(p, end, struct_compat, e_inval);
726 if (struct_compat > OSDMAP_WRAPPER_COMPAT_VER) {
727 pr_warning("got v %d cv %d > %d of %s ceph_osdmap\n",
728 struct_v, struct_compat,
729 OSDMAP_WRAPPER_COMPAT_VER, prefix);
730 return -EINVAL;
731 }
732 *p += 4; /* ignore wrapper struct_len */
733
734 ceph_decode_8_safe(p, end, struct_v, e_inval);
735 ceph_decode_8_safe(p, end, struct_compat, e_inval);
736 if (struct_compat > OSDMAP_CLIENT_DATA_COMPAT_VER) {
737 pr_warning("got v %d cv %d > %d of %s ceph_osdmap client data\n",
738 struct_v, struct_compat,
739 OSDMAP_CLIENT_DATA_COMPAT_VER, prefix);
740 return -EINVAL;
741 }
742 *p += 4; /* ignore client data struct_len */
743 } else {
744 u16 version;
745
746 *p -= 1;
747 ceph_decode_16_safe(p, end, version, e_inval);
748 if (version < 6) {
749 pr_warning("got v %d < 6 of %s ceph_osdmap\n", version,
750 prefix);
751 return -EINVAL;
752 }
753
754 /* old osdmap enconding */
755 struct_v = 0;
756 }
757
758 *v = struct_v;
759 return 0;
760
761e_inval:
762 return -EINVAL;
763}
764
Ilya Dryomov433fbdd2014-03-21 19:05:27 +0200765static int __decode_pools(void **p, void *end, struct ceph_osdmap *map,
766 bool incremental)
767{
768 u32 n;
769
770 ceph_decode_32_safe(p, end, n, e_inval);
771 while (n--) {
772 struct ceph_pg_pool_info *pi;
773 u64 pool;
774 int ret;
775
776 ceph_decode_64_safe(p, end, pool, e_inval);
777
778 pi = __lookup_pg_pool(&map->pg_pools, pool);
779 if (!incremental || !pi) {
780 pi = kzalloc(sizeof(*pi), GFP_NOFS);
781 if (!pi)
782 return -ENOMEM;
783
784 pi->id = pool;
785
786 ret = __insert_pg_pool(&map->pg_pools, pi);
787 if (ret) {
788 kfree(pi);
789 return ret;
790 }
791 }
792
793 ret = decode_pool(p, end, pi);
794 if (ret)
795 return ret;
796 }
797
798 return 0;
799
800e_inval:
801 return -EINVAL;
802}
803
804static int decode_pools(void **p, void *end, struct ceph_osdmap *map)
805{
806 return __decode_pools(p, end, map, false);
807}
808
809static int decode_new_pools(void **p, void *end, struct ceph_osdmap *map)
810{
811 return __decode_pools(p, end, map, true);
812}
813
Ilya Dryomov10db6342014-03-21 19:05:28 +0200814static int __decode_pg_temp(void **p, void *end, struct ceph_osdmap *map,
815 bool incremental)
816{
817 u32 n;
818
819 ceph_decode_32_safe(p, end, n, e_inval);
820 while (n--) {
821 struct ceph_pg pgid;
822 u32 len, i;
823 int ret;
824
825 ret = ceph_decode_pgid(p, end, &pgid);
826 if (ret)
827 return ret;
828
829 ceph_decode_32_safe(p, end, len, e_inval);
830
831 ret = __remove_pg_mapping(&map->pg_temp, pgid);
832 BUG_ON(!incremental && ret != -ENOENT);
833
834 if (!incremental || len > 0) {
835 struct ceph_pg_mapping *pg;
836
837 ceph_decode_need(p, end, len*sizeof(u32), e_inval);
838
839 if (len > (UINT_MAX - sizeof(*pg)) / sizeof(u32))
840 return -EINVAL;
841
842 pg = kzalloc(sizeof(*pg) + len*sizeof(u32), GFP_NOFS);
843 if (!pg)
844 return -ENOMEM;
845
846 pg->pgid = pgid;
Ilya Dryomov35a935d2014-03-21 19:05:29 +0200847 pg->pg_temp.len = len;
Ilya Dryomov10db6342014-03-21 19:05:28 +0200848 for (i = 0; i < len; i++)
Ilya Dryomov35a935d2014-03-21 19:05:29 +0200849 pg->pg_temp.osds[i] = ceph_decode_32(p);
Ilya Dryomov10db6342014-03-21 19:05:28 +0200850
851 ret = __insert_pg_mapping(pg, &map->pg_temp);
852 if (ret) {
853 kfree(pg);
854 return ret;
855 }
856 }
857 }
858
859 return 0;
860
861e_inval:
862 return -EINVAL;
863}
864
865static int decode_pg_temp(void **p, void *end, struct ceph_osdmap *map)
866{
867 return __decode_pg_temp(p, end, map, false);
868}
869
870static int decode_new_pg_temp(void **p, void *end, struct ceph_osdmap *map)
871{
872 return __decode_pg_temp(p, end, map, true);
873}
874
Ilya Dryomovd286de72014-03-21 19:05:30 +0200875static int __decode_primary_temp(void **p, void *end, struct ceph_osdmap *map,
876 bool incremental)
877{
878 u32 n;
879
880 ceph_decode_32_safe(p, end, n, e_inval);
881 while (n--) {
882 struct ceph_pg pgid;
883 u32 osd;
884 int ret;
885
886 ret = ceph_decode_pgid(p, end, &pgid);
887 if (ret)
888 return ret;
889
890 ceph_decode_32_safe(p, end, osd, e_inval);
891
892 ret = __remove_pg_mapping(&map->primary_temp, pgid);
893 BUG_ON(!incremental && ret != -ENOENT);
894
895 if (!incremental || osd != (u32)-1) {
896 struct ceph_pg_mapping *pg;
897
898 pg = kzalloc(sizeof(*pg), GFP_NOFS);
899 if (!pg)
900 return -ENOMEM;
901
902 pg->pgid = pgid;
903 pg->primary_temp.osd = osd;
904
905 ret = __insert_pg_mapping(pg, &map->primary_temp);
906 if (ret) {
907 kfree(pg);
908 return ret;
909 }
910 }
911 }
912
913 return 0;
914
915e_inval:
916 return -EINVAL;
917}
918
919static int decode_primary_temp(void **p, void *end, struct ceph_osdmap *map)
920{
921 return __decode_primary_temp(p, end, map, false);
922}
923
924static int decode_new_primary_temp(void **p, void *end,
925 struct ceph_osdmap *map)
926{
927 return __decode_primary_temp(p, end, map, true);
928}
929
Ilya Dryomov2cfa34f2014-03-21 19:05:30 +0200930u32 ceph_get_primary_affinity(struct ceph_osdmap *map, int osd)
931{
932 BUG_ON(osd >= map->max_osd);
933
934 if (!map->osd_primary_affinity)
935 return CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
936
937 return map->osd_primary_affinity[osd];
938}
939
940static int set_primary_affinity(struct ceph_osdmap *map, int osd, u32 aff)
941{
942 BUG_ON(osd >= map->max_osd);
943
944 if (!map->osd_primary_affinity) {
945 int i;
946
947 map->osd_primary_affinity = kmalloc(map->max_osd*sizeof(u32),
948 GFP_NOFS);
949 if (!map->osd_primary_affinity)
950 return -ENOMEM;
951
952 for (i = 0; i < map->max_osd; i++)
953 map->osd_primary_affinity[i] =
954 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY;
955 }
956
957 map->osd_primary_affinity[osd] = aff;
958
959 return 0;
960}
961
Ilya Dryomov63a69932014-03-21 19:05:30 +0200962static int decode_primary_affinity(void **p, void *end,
963 struct ceph_osdmap *map)
964{
965 u32 len, i;
966
967 ceph_decode_32_safe(p, end, len, e_inval);
968 if (len == 0) {
969 kfree(map->osd_primary_affinity);
970 map->osd_primary_affinity = NULL;
971 return 0;
972 }
973 if (len != map->max_osd)
974 goto e_inval;
975
976 ceph_decode_need(p, end, map->max_osd*sizeof(u32), e_inval);
977
978 for (i = 0; i < map->max_osd; i++) {
979 int ret;
980
981 ret = set_primary_affinity(map, i, ceph_decode_32(p));
982 if (ret)
983 return ret;
984 }
985
986 return 0;
987
988e_inval:
989 return -EINVAL;
990}
991
992static int decode_new_primary_affinity(void **p, void *end,
993 struct ceph_osdmap *map)
994{
995 u32 n;
996
997 ceph_decode_32_safe(p, end, n, e_inval);
998 while (n--) {
999 u32 osd, aff;
1000 int ret;
1001
1002 ceph_decode_32_safe(p, end, osd, e_inval);
1003 ceph_decode_32_safe(p, end, aff, e_inval);
1004
1005 ret = set_primary_affinity(map, osd, aff);
1006 if (ret)
1007 return ret;
1008 }
1009
1010 return 0;
1011
1012e_inval:
1013 return -EINVAL;
1014}
1015
Sage Weil4fc51be2010-02-16 15:55:03 -08001016/*
Sage Weilf24e9982009-10-06 11:31:10 -07001017 * decode a full map.
1018 */
Ilya Dryomova2505d62014-03-13 16:36:13 +02001019static int osdmap_decode(void **p, void *end, struct ceph_osdmap *map)
Sage Weilf24e9982009-10-06 11:31:10 -07001020{
Ilya Dryomovec7af972014-03-21 19:05:29 +02001021 u8 struct_v;
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001022 u32 epoch = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001023 void *start = *p;
Ilya Dryomov39770582014-03-13 16:36:14 +02001024 u32 max;
1025 u32 len, i;
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001026 int err;
Sage Weilf24e9982009-10-06 11:31:10 -07001027
Ilya Dryomova2505d62014-03-13 16:36:13 +02001028 dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p));
Sage Weilf24e9982009-10-06 11:31:10 -07001029
Ilya Dryomovec7af972014-03-21 19:05:29 +02001030 err = get_osdmap_client_data_v(p, end, "full", &struct_v);
1031 if (err)
1032 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001033
Ilya Dryomov53bbaba2014-03-13 16:36:16 +02001034 /* fsid, epoch, created, modified */
1035 ceph_decode_need(p, end, sizeof(map->fsid) + sizeof(u32) +
1036 sizeof(map->created) + sizeof(map->modified), e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001037 ceph_decode_copy(p, &map->fsid, sizeof(map->fsid));
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001038 epoch = map->epoch = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -07001039 ceph_decode_copy(p, &map->created, sizeof(map->created));
1040 ceph_decode_copy(p, &map->modified, sizeof(map->modified));
1041
Ilya Dryomov433fbdd2014-03-21 19:05:27 +02001042 /* pools */
1043 err = decode_pools(p, end, map);
1044 if (err)
1045 goto bad;
Sage Weil2844a762010-04-09 15:46:42 -07001046
Ilya Dryomov0f70c7e2014-03-21 19:05:27 +02001047 /* pool_name */
1048 err = decode_pool_names(p, end, map);
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001049 if (err)
Sage Weil4f6a7e52013-02-23 10:41:09 -08001050 goto bad;
Sage Weil2844a762010-04-09 15:46:42 -07001051
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001052 ceph_decode_32_safe(p, end, map->pool_max, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001053
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001054 ceph_decode_32_safe(p, end, map->flags, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001055
Ilya Dryomov39770582014-03-13 16:36:14 +02001056 /* max_osd */
1057 ceph_decode_32_safe(p, end, max, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001058
1059 /* (re)alloc osd arrays */
1060 err = osdmap_set_max_osd(map, max);
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001061 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001062 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001063
Ilya Dryomov2d88b2e2014-03-13 16:36:14 +02001064 /* osd_state, osd_weight, osd_addrs->client_addr */
Sage Weilf24e9982009-10-06 11:31:10 -07001065 ceph_decode_need(p, end, 3*sizeof(u32) +
1066 map->max_osd*(1 + sizeof(*map->osd_weight) +
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001067 sizeof(*map->osd_addr)), e_inval);
1068
Ilya Dryomov2d88b2e2014-03-13 16:36:14 +02001069 if (ceph_decode_32(p) != map->max_osd)
1070 goto e_inval;
1071
Sage Weilf24e9982009-10-06 11:31:10 -07001072 ceph_decode_copy(p, map->osd_state, map->max_osd);
1073
Ilya Dryomov2d88b2e2014-03-13 16:36:14 +02001074 if (ceph_decode_32(p) != map->max_osd)
1075 goto e_inval;
1076
Sage Weilf24e9982009-10-06 11:31:10 -07001077 for (i = 0; i < map->max_osd; i++)
Sage Weilc89136e2009-10-14 09:59:09 -07001078 map->osd_weight[i] = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -07001079
Ilya Dryomov2d88b2e2014-03-13 16:36:14 +02001080 if (ceph_decode_32(p) != map->max_osd)
1081 goto e_inval;
1082
Sage Weilf24e9982009-10-06 11:31:10 -07001083 ceph_decode_copy(p, map->osd_addr, map->max_osd*sizeof(*map->osd_addr));
Sage Weil63f2d212009-11-03 15:17:56 -08001084 for (i = 0; i < map->max_osd; i++)
1085 ceph_decode_addr(&map->osd_addr[i]);
Sage Weilf24e9982009-10-06 11:31:10 -07001086
1087 /* pg_temp */
Ilya Dryomov10db6342014-03-21 19:05:28 +02001088 err = decode_pg_temp(p, end, map);
1089 if (err)
1090 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001091
Ilya Dryomovd286de72014-03-21 19:05:30 +02001092 /* primary_temp */
1093 if (struct_v >= 1) {
1094 err = decode_primary_temp(p, end, map);
1095 if (err)
1096 goto bad;
1097 }
1098
Ilya Dryomov63a69932014-03-21 19:05:30 +02001099 /* primary_affinity */
1100 if (struct_v >= 2) {
1101 err = decode_primary_affinity(p, end, map);
1102 if (err)
1103 goto bad;
1104 } else {
1105 /* XXX can this happen? */
1106 kfree(map->osd_primary_affinity);
1107 map->osd_primary_affinity = NULL;
1108 }
1109
Sage Weilf24e9982009-10-06 11:31:10 -07001110 /* crush */
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001111 ceph_decode_32_safe(p, end, len, e_inval);
Ilya Dryomov9902e682014-03-13 16:36:15 +02001112 map->crush = crush_decode(*p, min(*p + len, end));
Sage Weilf24e9982009-10-06 11:31:10 -07001113 if (IS_ERR(map->crush)) {
1114 err = PTR_ERR(map->crush);
1115 map->crush = NULL;
1116 goto bad;
1117 }
Ilya Dryomov9902e682014-03-13 16:36:15 +02001118 *p += len;
Sage Weilf24e9982009-10-06 11:31:10 -07001119
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001120 /* ignore the rest */
Sage Weilf24e9982009-10-06 11:31:10 -07001121 *p = end;
1122
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001123 dout("full osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd);
Ilya Dryomova2505d62014-03-13 16:36:13 +02001124 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001125
Ilya Dryomov597b52f2014-03-13 16:36:14 +02001126e_inval:
1127 err = -EINVAL;
Sage Weilf24e9982009-10-06 11:31:10 -07001128bad:
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001129 pr_err("corrupt full osdmap (%d) epoch %d off %d (%p of %p-%p)\n",
1130 err, epoch, (int)(*p - start), *p, start, end);
1131 print_hex_dump(KERN_DEBUG, "osdmap: ",
1132 DUMP_PREFIX_OFFSET, 16, 1,
1133 start, end - start, true);
Ilya Dryomova2505d62014-03-13 16:36:13 +02001134 return err;
1135}
1136
1137/*
1138 * Allocate and decode a full map.
1139 */
1140struct ceph_osdmap *ceph_osdmap_decode(void **p, void *end)
1141{
1142 struct ceph_osdmap *map;
1143 int ret;
1144
1145 map = kzalloc(sizeof(*map), GFP_NOFS);
1146 if (!map)
1147 return ERR_PTR(-ENOMEM);
1148
1149 map->pg_temp = RB_ROOT;
Ilya Dryomov9686f942014-03-21 19:05:29 +02001150 map->primary_temp = RB_ROOT;
Ilya Dryomova2505d62014-03-13 16:36:13 +02001151 mutex_init(&map->crush_scratch_mutex);
1152
1153 ret = osdmap_decode(p, end, map);
1154 if (ret) {
1155 ceph_osdmap_destroy(map);
1156 return ERR_PTR(ret);
1157 }
1158
1159 return map;
Sage Weilf24e9982009-10-06 11:31:10 -07001160}
1161
1162/*
1163 * decode and apply an incremental map update.
1164 */
1165struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end,
1166 struct ceph_osdmap *map,
1167 struct ceph_messenger *msgr)
1168{
Sage Weilf24e9982009-10-06 11:31:10 -07001169 struct crush_map *newcrush = NULL;
1170 struct ceph_fsid fsid;
1171 u32 epoch = 0;
1172 struct ceph_timespec modified;
Sage Weil4f6a7e52013-02-23 10:41:09 -08001173 s32 len;
1174 u64 pool;
1175 __s64 new_pool_max;
1176 __s32 new_flags, max;
Sage Weilf24e9982009-10-06 11:31:10 -07001177 void *start = *p;
Ilya Dryomov86f17422014-03-13 16:36:15 +02001178 int err;
Ilya Dryomovec7af972014-03-21 19:05:29 +02001179 u8 struct_v;
Sage Weilf24e9982009-10-06 11:31:10 -07001180
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001181 dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p));
1182
Ilya Dryomovec7af972014-03-21 19:05:29 +02001183 err = get_osdmap_client_data_v(p, end, "inc", &struct_v);
1184 if (err)
1185 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001186
Ilya Dryomov53bbaba2014-03-13 16:36:16 +02001187 /* fsid, epoch, modified, new_pool_max, new_flags */
1188 ceph_decode_need(p, end, sizeof(fsid) + sizeof(u32) + sizeof(modified) +
1189 sizeof(u64) + sizeof(u32), e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001190 ceph_decode_copy(p, &fsid, sizeof(fsid));
Sage Weilc89136e2009-10-14 09:59:09 -07001191 epoch = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -07001192 BUG_ON(epoch != map->epoch+1);
1193 ceph_decode_copy(p, &modified, sizeof(modified));
Sage Weil4f6a7e52013-02-23 10:41:09 -08001194 new_pool_max = ceph_decode_64(p);
Sage Weilc89136e2009-10-14 09:59:09 -07001195 new_flags = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -07001196
1197 /* full map? */
Ilya Dryomov86f17422014-03-13 16:36:15 +02001198 ceph_decode_32_safe(p, end, len, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001199 if (len > 0) {
1200 dout("apply_incremental full map len %d, %p to %p\n",
1201 len, *p, end);
Ilya Dryomova2505d62014-03-13 16:36:13 +02001202 return ceph_osdmap_decode(p, min(*p+len, end));
Sage Weilf24e9982009-10-06 11:31:10 -07001203 }
1204
1205 /* new crush? */
Ilya Dryomov86f17422014-03-13 16:36:15 +02001206 ceph_decode_32_safe(p, end, len, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001207 if (len > 0) {
Sage Weilf24e9982009-10-06 11:31:10 -07001208 newcrush = crush_decode(*p, min(*p+len, end));
Ilya Dryomov86f17422014-03-13 16:36:15 +02001209 if (IS_ERR(newcrush)) {
1210 err = PTR_ERR(newcrush);
1211 newcrush = NULL;
1212 goto bad;
1213 }
Sage Weilcebc5be2010-06-17 10:22:48 -07001214 *p += len;
Sage Weilf24e9982009-10-06 11:31:10 -07001215 }
1216
1217 /* new flags? */
1218 if (new_flags >= 0)
1219 map->flags = new_flags;
Sage Weil4fc51be2010-02-16 15:55:03 -08001220 if (new_pool_max >= 0)
1221 map->pool_max = new_pool_max;
Sage Weilf24e9982009-10-06 11:31:10 -07001222
Sage Weilf24e9982009-10-06 11:31:10 -07001223 /* new max? */
Ilya Dryomov53bbaba2014-03-13 16:36:16 +02001224 ceph_decode_32_safe(p, end, max, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001225 if (max >= 0) {
1226 err = osdmap_set_max_osd(map, max);
Ilya Dryomov86f17422014-03-13 16:36:15 +02001227 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001228 goto bad;
1229 }
1230
1231 map->epoch++;
Sage Weil31456662011-05-12 15:18:43 -07001232 map->modified = modified;
Sage Weilf24e9982009-10-06 11:31:10 -07001233 if (newcrush) {
1234 if (map->crush)
1235 crush_destroy(map->crush);
1236 map->crush = newcrush;
1237 newcrush = NULL;
1238 }
1239
Ilya Dryomov433fbdd2014-03-21 19:05:27 +02001240 /* new_pools */
1241 err = decode_new_pools(p, end, map);
1242 if (err)
1243 goto bad;
Ilya Dryomov9464d002014-03-13 16:36:16 +02001244
Ilya Dryomov0f70c7e2014-03-21 19:05:27 +02001245 /* new_pool_names */
1246 err = decode_pool_names(p, end, map);
Ilya Dryomov9464d002014-03-13 16:36:16 +02001247 if (err)
1248 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001249
Sage Weil4fc51be2010-02-16 15:55:03 -08001250 /* old_pool */
Ilya Dryomov86f17422014-03-13 16:36:15 +02001251 ceph_decode_32_safe(p, end, len, e_inval);
Sage Weil4fc51be2010-02-16 15:55:03 -08001252 while (len--) {
1253 struct ceph_pg_pool_info *pi;
1254
Ilya Dryomov86f17422014-03-13 16:36:15 +02001255 ceph_decode_64_safe(p, end, pool, e_inval);
Sage Weil4fc51be2010-02-16 15:55:03 -08001256 pi = __lookup_pg_pool(&map->pg_pools, pool);
Sage Weil2844a762010-04-09 15:46:42 -07001257 if (pi)
1258 __remove_pg_pool(&map->pg_pools, pi);
Sage Weil4fc51be2010-02-16 15:55:03 -08001259 }
Sage Weilf24e9982009-10-06 11:31:10 -07001260
1261 /* new_up */
Ilya Dryomov86f17422014-03-13 16:36:15 +02001262 ceph_decode_32_safe(p, end, len, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001263 while (len--) {
1264 u32 osd;
1265 struct ceph_entity_addr addr;
Ilya Dryomov86f17422014-03-13 16:36:15 +02001266 ceph_decode_32_safe(p, end, osd, e_inval);
1267 ceph_decode_copy_safe(p, end, &addr, sizeof(addr), e_inval);
Sage Weil63f2d212009-11-03 15:17:56 -08001268 ceph_decode_addr(&addr);
Sage Weilf24e9982009-10-06 11:31:10 -07001269 pr_info("osd%d up\n", osd);
1270 BUG_ON(osd >= map->max_osd);
1271 map->osd_state[osd] |= CEPH_OSD_UP;
1272 map->osd_addr[osd] = addr;
1273 }
1274
Sage Weil7662d8f2011-05-03 12:52:05 -07001275 /* new_state */
Ilya Dryomov86f17422014-03-13 16:36:15 +02001276 ceph_decode_32_safe(p, end, len, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001277 while (len--) {
1278 u32 osd;
Sage Weil7662d8f2011-05-03 12:52:05 -07001279 u8 xorstate;
Ilya Dryomov86f17422014-03-13 16:36:15 +02001280 ceph_decode_32_safe(p, end, osd, e_inval);
Sage Weil7662d8f2011-05-03 12:52:05 -07001281 xorstate = **(u8 **)p;
Sage Weilf24e9982009-10-06 11:31:10 -07001282 (*p)++; /* clean flag */
Sage Weil7662d8f2011-05-03 12:52:05 -07001283 if (xorstate == 0)
1284 xorstate = CEPH_OSD_UP;
1285 if (xorstate & CEPH_OSD_UP)
1286 pr_info("osd%d down\n", osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001287 if (osd < map->max_osd)
Sage Weil7662d8f2011-05-03 12:52:05 -07001288 map->osd_state[osd] ^= xorstate;
Sage Weilf24e9982009-10-06 11:31:10 -07001289 }
1290
1291 /* new_weight */
Ilya Dryomov86f17422014-03-13 16:36:15 +02001292 ceph_decode_32_safe(p, end, len, e_inval);
Sage Weilf24e9982009-10-06 11:31:10 -07001293 while (len--) {
1294 u32 osd, off;
Ilya Dryomov86f17422014-03-13 16:36:15 +02001295 ceph_decode_need(p, end, sizeof(u32)*2, e_inval);
Sage Weilc89136e2009-10-14 09:59:09 -07001296 osd = ceph_decode_32(p);
1297 off = ceph_decode_32(p);
Sage Weilf24e9982009-10-06 11:31:10 -07001298 pr_info("osd%d weight 0x%x %s\n", osd, off,
1299 off == CEPH_OSD_IN ? "(in)" :
1300 (off == CEPH_OSD_OUT ? "(out)" : ""));
1301 if (osd < map->max_osd)
1302 map->osd_weight[osd] = off;
1303 }
1304
1305 /* new_pg_temp */
Ilya Dryomov10db6342014-03-21 19:05:28 +02001306 err = decode_new_pg_temp(p, end, map);
1307 if (err)
1308 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001309
Ilya Dryomovd286de72014-03-21 19:05:30 +02001310 /* new_primary_temp */
1311 if (struct_v >= 1) {
1312 err = decode_new_primary_temp(p, end, map);
1313 if (err)
1314 goto bad;
1315 }
1316
Ilya Dryomov63a69932014-03-21 19:05:30 +02001317 /* new_primary_affinity */
1318 if (struct_v >= 2) {
1319 err = decode_new_primary_affinity(p, end, map);
1320 if (err)
1321 goto bad;
1322 }
1323
Sage Weilf24e9982009-10-06 11:31:10 -07001324 /* ignore the rest */
1325 *p = end;
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001326
1327 dout("inc osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001328 return map;
1329
Ilya Dryomov86f17422014-03-13 16:36:15 +02001330e_inval:
1331 err = -EINVAL;
Sage Weilf24e9982009-10-06 11:31:10 -07001332bad:
Ilya Dryomov38a8d562014-03-13 16:36:13 +02001333 pr_err("corrupt inc osdmap (%d) epoch %d off %d (%p of %p-%p)\n",
1334 err, epoch, (int)(*p - start), *p, start, end);
Sage Weil9ec7cab2009-12-14 15:13:47 -08001335 print_hex_dump(KERN_DEBUG, "osdmap: ",
1336 DUMP_PREFIX_OFFSET, 16, 1,
1337 start, end - start, true);
Sage Weilf24e9982009-10-06 11:31:10 -07001338 if (newcrush)
1339 crush_destroy(newcrush);
1340 return ERR_PTR(err);
1341}
1342
1343
1344
1345
1346/*
1347 * calculate file layout from given offset, length.
1348 * fill in correct oid, logical length, and object extent
1349 * offset, length.
1350 *
1351 * for now, we write only a single su, until we can
1352 * pass a stride back to the caller.
1353 */
Sage Weild63b77f2012-09-24 20:59:48 -07001354int ceph_calc_file_object_mapping(struct ceph_file_layout *layout,
Alex Eldere8afad62012-11-14 09:38:19 -06001355 u64 off, u64 len,
Sage Weil645a1022009-10-28 15:15:05 -07001356 u64 *ono,
Sage Weilf24e9982009-10-06 11:31:10 -07001357 u64 *oxoff, u64 *oxlen)
1358{
1359 u32 osize = le32_to_cpu(layout->fl_object_size);
1360 u32 su = le32_to_cpu(layout->fl_stripe_unit);
1361 u32 sc = le32_to_cpu(layout->fl_stripe_count);
1362 u32 bl, stripeno, stripepos, objsetno;
1363 u32 su_per_object;
Noah Watkinsff1d1f72009-10-30 12:57:30 -07001364 u64 t, su_offset;
Sage Weilf24e9982009-10-06 11:31:10 -07001365
Alex Eldere8afad62012-11-14 09:38:19 -06001366 dout("mapping %llu~%llu osize %u fl_su %u\n", off, len,
Sage Weilf24e9982009-10-06 11:31:10 -07001367 osize, su);
Sage Weild63b77f2012-09-24 20:59:48 -07001368 if (su == 0 || sc == 0)
1369 goto invalid;
Noah Watkins35e054a2009-10-28 14:04:48 -07001370 su_per_object = osize / su;
Sage Weild63b77f2012-09-24 20:59:48 -07001371 if (su_per_object == 0)
1372 goto invalid;
Sage Weilf24e9982009-10-06 11:31:10 -07001373 dout("osize %u / su %u = su_per_object %u\n", osize, su,
1374 su_per_object);
1375
Sage Weild63b77f2012-09-24 20:59:48 -07001376 if ((su & ~PAGE_MASK) != 0)
1377 goto invalid;
1378
Sage Weilf24e9982009-10-06 11:31:10 -07001379 /* bl = *off / su; */
1380 t = off;
1381 do_div(t, su);
1382 bl = t;
1383 dout("off %llu / su %u = bl %u\n", off, su, bl);
1384
1385 stripeno = bl / sc;
1386 stripepos = bl % sc;
1387 objsetno = stripeno / su_per_object;
1388
Sage Weil645a1022009-10-28 15:15:05 -07001389 *ono = objsetno * sc + stripepos;
Eric Dumazet95c96172012-04-15 05:58:06 +00001390 dout("objset %u * sc %u = ono %u\n", objsetno, sc, (unsigned int)*ono);
Sage Weil645a1022009-10-28 15:15:05 -07001391
1392 /* *oxoff = *off % layout->fl_stripe_unit; # offset in su */
Sage Weilf24e9982009-10-06 11:31:10 -07001393 t = off;
Noah Watkinsff1d1f72009-10-30 12:57:30 -07001394 su_offset = do_div(t, su);
1395 *oxoff = su_offset + (stripeno % su_per_object) * su;
Sage Weil645a1022009-10-28 15:15:05 -07001396
Noah Watkinsff1d1f72009-10-30 12:57:30 -07001397 /*
1398 * Calculate the length of the extent being written to the selected
Alex Eldere8afad62012-11-14 09:38:19 -06001399 * object. This is the minimum of the full length requested (len) or
Noah Watkinsff1d1f72009-10-30 12:57:30 -07001400 * the remainder of the current stripe being written to.
1401 */
Alex Eldere8afad62012-11-14 09:38:19 -06001402 *oxlen = min_t(u64, len, su - su_offset);
Sage Weilf24e9982009-10-06 11:31:10 -07001403
1404 dout(" obj extent %llu~%llu\n", *oxoff, *oxlen);
Sage Weild63b77f2012-09-24 20:59:48 -07001405 return 0;
1406
1407invalid:
1408 dout(" invalid layout\n");
1409 *ono = 0;
1410 *oxoff = 0;
1411 *oxlen = 0;
1412 return -EINVAL;
Sage Weilf24e9982009-10-06 11:31:10 -07001413}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001414EXPORT_SYMBOL(ceph_calc_file_object_mapping);
Sage Weilf24e9982009-10-06 11:31:10 -07001415
1416/*
Ilya Dryomov7c13cb62014-01-27 17:40:19 +02001417 * Calculate mapping of a (oloc, oid) pair to a PG. Should only be
1418 * called with target's (oloc, oid), since tiering isn't taken into
1419 * account.
Sage Weilf24e9982009-10-06 11:31:10 -07001420 */
Ilya Dryomov7c13cb62014-01-27 17:40:19 +02001421int ceph_oloc_oid_to_pg(struct ceph_osdmap *osdmap,
1422 struct ceph_object_locator *oloc,
1423 struct ceph_object_id *oid,
1424 struct ceph_pg *pg_out)
Sage Weilf24e9982009-10-06 11:31:10 -07001425{
Ilya Dryomov7c13cb62014-01-27 17:40:19 +02001426 struct ceph_pg_pool_info *pi;
Sage Weilf24e9982009-10-06 11:31:10 -07001427
Ilya Dryomov7c13cb62014-01-27 17:40:19 +02001428 pi = __lookup_pg_pool(&osdmap->pg_pools, oloc->pool);
1429 if (!pi)
Sage Weil4fc51be2010-02-16 15:55:03 -08001430 return -EIO;
Sage Weilf24e9982009-10-06 11:31:10 -07001431
Ilya Dryomov7c13cb62014-01-27 17:40:19 +02001432 pg_out->pool = oloc->pool;
1433 pg_out->seed = ceph_str_hash(pi->object_hash, oid->name,
1434 oid->name_len);
1435
1436 dout("%s '%.*s' pgid %llu.%x\n", __func__, oid->name_len, oid->name,
1437 pg_out->pool, pg_out->seed);
Sage Weilf24e9982009-10-06 11:31:10 -07001438 return 0;
1439}
Ilya Dryomov7c13cb62014-01-27 17:40:19 +02001440EXPORT_SYMBOL(ceph_oloc_oid_to_pg);
Sage Weilf24e9982009-10-06 11:31:10 -07001441
Ilya Dryomov9d521472014-01-31 17:54:26 +02001442static int do_crush(struct ceph_osdmap *map, int ruleno, int x,
1443 int *result, int result_max,
1444 const __u32 *weight, int weight_max)
Ilya Dryomove8ef19c2013-12-24 21:19:24 +02001445{
Ilya Dryomov9d521472014-01-31 17:54:26 +02001446 int r;
Ilya Dryomove8ef19c2013-12-24 21:19:24 +02001447
Ilya Dryomov9d521472014-01-31 17:54:26 +02001448 BUG_ON(result_max > CEPH_PG_MAX_SIZE);
1449
1450 mutex_lock(&map->crush_scratch_mutex);
1451 r = crush_do_rule(map->crush, ruleno, x, result, result_max,
1452 weight, weight_max, map->crush_scratch_ary);
1453 mutex_unlock(&map->crush_scratch_mutex);
1454
1455 return r;
Ilya Dryomove8ef19c2013-12-24 21:19:24 +02001456}
1457
Sage Weilf24e9982009-10-06 11:31:10 -07001458/*
1459 * Calculate raw osd vector for the given pgid. Return pointer to osd
1460 * array, or NULL on failure.
1461 */
Sage Weil5b191d92013-02-23 10:38:16 -08001462static int *calc_pg_raw(struct ceph_osdmap *osdmap, struct ceph_pg pgid,
Sage Weilf24e9982009-10-06 11:31:10 -07001463 int *osds, int *num)
1464{
Sage Weilf24e9982009-10-06 11:31:10 -07001465 struct ceph_pg_mapping *pg;
1466 struct ceph_pg_pool_info *pool;
1467 int ruleno;
Sage Weil83ca14f2013-02-26 10:39:09 -08001468 int r;
1469 u32 pps;
Sage Weilf24e9982009-10-06 11:31:10 -07001470
Sage Weil83ca14f2013-02-26 10:39:09 -08001471 pool = __lookup_pg_pool(&osdmap->pg_pools, pgid.pool);
Sage Weil782e1822011-09-28 10:08:27 -07001472 if (!pool)
1473 return NULL;
1474
Sage Weilf24e9982009-10-06 11:31:10 -07001475 /* pg_temp? */
Sage Weil83ca14f2013-02-26 10:39:09 -08001476 pgid.seed = ceph_stable_mod(pgid.seed, pool->pg_num,
Sage Weil9542cf02013-08-28 17:17:29 -07001477 pool->pg_num_mask);
Sage Weil9794b142010-02-16 15:53:32 -08001478 pg = __lookup_pg_mapping(&osdmap->pg_temp, pgid);
1479 if (pg) {
Ilya Dryomov35a935d2014-03-21 19:05:29 +02001480 *num = pg->pg_temp.len;
1481 return pg->pg_temp.osds;
Sage Weilf24e9982009-10-06 11:31:10 -07001482 }
1483
1484 /* crush */
Sage Weil4f6a7e52013-02-23 10:41:09 -08001485 ruleno = crush_find_rule(osdmap->crush, pool->crush_ruleset,
1486 pool->type, pool->size);
Sage Weilf24e9982009-10-06 11:31:10 -07001487 if (ruleno < 0) {
Sage Weil83ca14f2013-02-26 10:39:09 -08001488 pr_err("no crush rule pool %lld ruleset %d type %d size %d\n",
1489 pgid.pool, pool->crush_ruleset, pool->type,
Sage Weil4f6a7e52013-02-23 10:41:09 -08001490 pool->size);
Sage Weilf24e9982009-10-06 11:31:10 -07001491 return NULL;
1492 }
1493
Sage Weil83ca14f2013-02-26 10:39:09 -08001494 if (pool->flags & CEPH_POOL_FLAG_HASHPSPOOL) {
1495 /* hash pool id and seed sothat pool PGs do not overlap */
1496 pps = crush_hash32_2(CRUSH_HASH_RJENKINS1,
1497 ceph_stable_mod(pgid.seed, pool->pgp_num,
1498 pool->pgp_num_mask),
1499 pgid.pool);
1500 } else {
1501 /*
1502 * legacy ehavior: add ps and pool together. this is
1503 * not a great approach because the PGs from each pool
1504 * will overlap on top of each other: 0.5 == 1.4 ==
1505 * 2.3 == ...
1506 */
1507 pps = ceph_stable_mod(pgid.seed, pool->pgp_num,
1508 pool->pgp_num_mask) +
1509 (unsigned)pgid.pool;
1510 }
Ilya Dryomov9d521472014-01-31 17:54:26 +02001511 r = do_crush(osdmap, ruleno, pps, osds, min_t(int, pool->size, *num),
1512 osdmap->osd_weight, osdmap->max_osd);
Sage Weil8b393262012-05-07 15:37:23 -07001513 if (r < 0) {
Sage Weil83ca14f2013-02-26 10:39:09 -08001514 pr_err("error %d from crush rule: pool %lld ruleset %d type %d"
1515 " size %d\n", r, pgid.pool, pool->crush_ruleset,
Sage Weil4f6a7e52013-02-23 10:41:09 -08001516 pool->type, pool->size);
Sage Weil8b393262012-05-07 15:37:23 -07001517 return NULL;
1518 }
1519 *num = r;
Sage Weilf24e9982009-10-06 11:31:10 -07001520 return osds;
1521}
1522
1523/*
Ilya Dryomov2bd93d42014-03-24 17:12:47 +02001524 * Calculate raw (crush) set for given pgid.
1525 *
1526 * Return raw set length, or error.
1527 */
1528static int pg_to_raw_osds(struct ceph_osdmap *osdmap,
1529 struct ceph_pg_pool_info *pool,
1530 struct ceph_pg pgid, u32 pps, int *osds)
1531{
1532 int ruleno;
1533 int len;
1534
1535 /* crush */
1536 ruleno = crush_find_rule(osdmap->crush, pool->crush_ruleset,
1537 pool->type, pool->size);
1538 if (ruleno < 0) {
1539 pr_err("no crush rule: pool %lld ruleset %d type %d size %d\n",
1540 pgid.pool, pool->crush_ruleset, pool->type,
1541 pool->size);
1542 return -ENOENT;
1543 }
1544
1545 len = do_crush(osdmap, ruleno, pps, osds,
1546 min_t(int, pool->size, CEPH_PG_MAX_SIZE),
1547 osdmap->osd_weight, osdmap->max_osd);
1548 if (len < 0) {
1549 pr_err("error %d from crush rule %d: pool %lld ruleset %d type %d size %d\n",
1550 len, ruleno, pgid.pool, pool->crush_ruleset,
1551 pool->type, pool->size);
1552 return len;
1553 }
1554
1555 return len;
1556}
1557
1558/*
1559 * Given raw set, calculate up set and up primary.
1560 *
1561 * Return up set length. *primary is set to up primary osd id, or -1
1562 * if up set is empty.
1563 */
1564static int raw_to_up_osds(struct ceph_osdmap *osdmap,
1565 struct ceph_pg_pool_info *pool,
1566 int *osds, int len, int *primary)
1567{
1568 int up_primary = -1;
1569 int i;
1570
1571 if (ceph_can_shift_osds(pool)) {
1572 int removed = 0;
1573
1574 for (i = 0; i < len; i++) {
1575 if (ceph_osd_is_down(osdmap, osds[i])) {
1576 removed++;
1577 continue;
1578 }
1579 if (removed)
1580 osds[i - removed] = osds[i];
1581 }
1582
1583 len -= removed;
1584 if (len > 0)
1585 up_primary = osds[0];
1586 } else {
1587 for (i = len - 1; i >= 0; i--) {
1588 if (ceph_osd_is_down(osdmap, osds[i]))
1589 osds[i] = CRUSH_ITEM_NONE;
1590 else
1591 up_primary = osds[i];
1592 }
1593 }
1594
1595 *primary = up_primary;
1596 return len;
1597}
1598
1599/*
Ilya Dryomov5e8d4d32014-03-24 17:12:48 +02001600 * Given up set, apply pg_temp and primary_temp mappings.
Ilya Dryomov45966c32014-03-24 17:12:47 +02001601 *
1602 * Return acting set length. *primary is set to acting primary osd id,
1603 * or -1 if acting set is empty.
1604 */
1605static int apply_temps(struct ceph_osdmap *osdmap,
1606 struct ceph_pg_pool_info *pool, struct ceph_pg pgid,
1607 int *osds, int len, int *primary)
1608{
1609 struct ceph_pg_mapping *pg;
1610 int temp_len;
1611 int temp_primary;
1612 int i;
1613
1614 /* raw_pg -> pg */
1615 pgid.seed = ceph_stable_mod(pgid.seed, pool->pg_num,
1616 pool->pg_num_mask);
1617
1618 /* pg_temp? */
1619 pg = __lookup_pg_mapping(&osdmap->pg_temp, pgid);
1620 if (pg) {
1621 temp_len = 0;
1622 temp_primary = -1;
1623
1624 for (i = 0; i < pg->pg_temp.len; i++) {
1625 if (ceph_osd_is_down(osdmap, pg->pg_temp.osds[i])) {
1626 if (ceph_can_shift_osds(pool))
1627 continue;
1628 else
1629 osds[temp_len++] = CRUSH_ITEM_NONE;
1630 } else {
1631 osds[temp_len++] = pg->pg_temp.osds[i];
1632 }
1633 }
1634
1635 /* apply pg_temp's primary */
1636 for (i = 0; i < temp_len; i++) {
1637 if (osds[i] != CRUSH_ITEM_NONE) {
1638 temp_primary = osds[i];
1639 break;
1640 }
1641 }
1642 } else {
1643 temp_len = len;
1644 temp_primary = *primary;
1645 }
1646
Ilya Dryomov5e8d4d32014-03-24 17:12:48 +02001647 /* primary_temp? */
1648 pg = __lookup_pg_mapping(&osdmap->primary_temp, pgid);
1649 if (pg)
1650 temp_primary = pg->primary_temp.osd;
1651
Ilya Dryomov45966c32014-03-24 17:12:47 +02001652 *primary = temp_primary;
1653 return temp_len;
1654}
1655
1656/*
Ilya Dryomovac972232014-03-24 17:12:48 +02001657 * Calculate acting set for given pgid.
1658 *
Ilya Dryomov8008ab12014-03-24 17:12:48 +02001659 * Return acting set length, or error. *primary is set to acting
1660 * primary osd id, or -1 if acting set is empty or on error.
Sage Weild85b7052010-05-10 10:24:48 -07001661 */
Sage Weil5b191d92013-02-23 10:38:16 -08001662int ceph_calc_pg_acting(struct ceph_osdmap *osdmap, struct ceph_pg pgid,
Ilya Dryomov8008ab12014-03-24 17:12:48 +02001663 int *osds, int *primary)
Sage Weild85b7052010-05-10 10:24:48 -07001664{
Ilya Dryomovac972232014-03-24 17:12:48 +02001665 struct ceph_pg_pool_info *pool;
1666 u32 pps;
1667 int len;
Sage Weild85b7052010-05-10 10:24:48 -07001668
Ilya Dryomovac972232014-03-24 17:12:48 +02001669 pool = __lookup_pg_pool(&osdmap->pg_pools, pgid.pool);
Ilya Dryomov8008ab12014-03-24 17:12:48 +02001670 if (!pool) {
1671 *primary = -1;
1672 return -ENOENT;
1673 }
Sage Weild85b7052010-05-10 10:24:48 -07001674
Ilya Dryomovac972232014-03-24 17:12:48 +02001675 if (pool->flags & CEPH_POOL_FLAG_HASHPSPOOL) {
1676 /* hash pool id and seed so that pool PGs do not overlap */
1677 pps = crush_hash32_2(CRUSH_HASH_RJENKINS1,
1678 ceph_stable_mod(pgid.seed, pool->pgp_num,
1679 pool->pgp_num_mask),
1680 pgid.pool);
1681 } else {
1682 /*
1683 * legacy behavior: add ps and pool together. this is
1684 * not a great approach because the PGs from each pool
1685 * will overlap on top of each other: 0.5 == 1.4 ==
1686 * 2.3 == ...
1687 */
1688 pps = ceph_stable_mod(pgid.seed, pool->pgp_num,
1689 pool->pgp_num_mask) +
1690 (unsigned)pgid.pool;
1691 }
1692
1693 len = pg_to_raw_osds(osdmap, pool, pgid, pps, osds);
Ilya Dryomov8008ab12014-03-24 17:12:48 +02001694 if (len < 0) {
1695 *primary = -1;
Ilya Dryomovac972232014-03-24 17:12:48 +02001696 return len;
Ilya Dryomov8008ab12014-03-24 17:12:48 +02001697 }
Ilya Dryomovac972232014-03-24 17:12:48 +02001698
Ilya Dryomov8008ab12014-03-24 17:12:48 +02001699 len = raw_to_up_osds(osdmap, pool, osds, len, primary);
Ilya Dryomovac972232014-03-24 17:12:48 +02001700
Ilya Dryomov8008ab12014-03-24 17:12:48 +02001701 len = apply_temps(osdmap, pool, pgid, osds, len, primary);
Ilya Dryomovac972232014-03-24 17:12:48 +02001702
1703 return len;
Sage Weild85b7052010-05-10 10:24:48 -07001704}
1705
1706/*
Sage Weilf24e9982009-10-06 11:31:10 -07001707 * Return primary osd for given pgid, or -1 if none.
1708 */
Sage Weil5b191d92013-02-23 10:38:16 -08001709int ceph_calc_pg_primary(struct ceph_osdmap *osdmap, struct ceph_pg pgid)
Sage Weilf24e9982009-10-06 11:31:10 -07001710{
Sage Weild85b7052010-05-10 10:24:48 -07001711 int rawosds[CEPH_PG_MAX_SIZE], *osds;
1712 int i, num = CEPH_PG_MAX_SIZE;
Sage Weilf24e9982009-10-06 11:31:10 -07001713
1714 osds = calc_pg_raw(osdmap, pgid, rawosds, &num);
1715 if (!osds)
1716 return -1;
1717
1718 /* primary is first up osd */
1719 for (i = 0; i < num; i++)
Sage Weild85b7052010-05-10 10:24:48 -07001720 if (ceph_osd_is_up(osdmap, osds[i]))
Sage Weilf24e9982009-10-06 11:31:10 -07001721 return osds[i];
Sage Weilf24e9982009-10-06 11:31:10 -07001722 return -1;
1723}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001724EXPORT_SYMBOL(ceph_calc_pg_primary);