Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1 | |
| 2 | #include <asm/div64.h> |
| 3 | |
| 4 | #include "super.h" |
| 5 | #include "osdmap.h" |
| 6 | #include "crush/hash.h" |
| 7 | #include "crush/mapper.h" |
| 8 | #include "decode.h" |
| 9 | #include "ceph_debug.h" |
| 10 | |
| 11 | char *ceph_osdmap_state_str(char *str, int len, int state) |
| 12 | { |
| 13 | int flag = 0; |
| 14 | |
| 15 | if (!len) |
| 16 | goto done; |
| 17 | |
| 18 | *str = '\0'; |
| 19 | if (state) { |
| 20 | if (state & CEPH_OSD_EXISTS) { |
| 21 | snprintf(str, len, "exists"); |
| 22 | flag = 1; |
| 23 | } |
| 24 | if (state & CEPH_OSD_UP) { |
| 25 | snprintf(str, len, "%s%s%s", str, (flag ? ", " : ""), |
| 26 | "up"); |
| 27 | flag = 1; |
| 28 | } |
| 29 | } else { |
| 30 | snprintf(str, len, "doesn't exist"); |
| 31 | } |
| 32 | done: |
| 33 | return str; |
| 34 | } |
| 35 | |
| 36 | /* maps */ |
| 37 | |
| 38 | static int calc_bits_of(unsigned t) |
| 39 | { |
| 40 | int b = 0; |
| 41 | while (t) { |
| 42 | t = t >> 1; |
| 43 | b++; |
| 44 | } |
| 45 | return b; |
| 46 | } |
| 47 | |
| 48 | /* |
| 49 | * the foo_mask is the smallest value 2^n-1 that is >= foo. |
| 50 | */ |
| 51 | static void calc_pg_masks(struct ceph_pg_pool_info *pi) |
| 52 | { |
| 53 | pi->pg_num_mask = (1 << calc_bits_of(le32_to_cpu(pi->v.pg_num)-1)) - 1; |
| 54 | pi->pgp_num_mask = |
| 55 | (1 << calc_bits_of(le32_to_cpu(pi->v.pgp_num)-1)) - 1; |
| 56 | pi->lpg_num_mask = |
| 57 | (1 << calc_bits_of(le32_to_cpu(pi->v.lpg_num)-1)) - 1; |
| 58 | pi->lpgp_num_mask = |
| 59 | (1 << calc_bits_of(le32_to_cpu(pi->v.lpgp_num)-1)) - 1; |
| 60 | } |
| 61 | |
| 62 | /* |
| 63 | * decode crush map |
| 64 | */ |
| 65 | static int crush_decode_uniform_bucket(void **p, void *end, |
| 66 | struct crush_bucket_uniform *b) |
| 67 | { |
| 68 | dout("crush_decode_uniform_bucket %p to %p\n", *p, end); |
| 69 | ceph_decode_need(p, end, (1+b->h.size) * sizeof(u32), bad); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 70 | b->item_weight = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 71 | return 0; |
| 72 | bad: |
| 73 | return -EINVAL; |
| 74 | } |
| 75 | |
| 76 | static int crush_decode_list_bucket(void **p, void *end, |
| 77 | struct crush_bucket_list *b) |
| 78 | { |
| 79 | int j; |
| 80 | dout("crush_decode_list_bucket %p to %p\n", *p, end); |
| 81 | b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS); |
| 82 | if (b->item_weights == NULL) |
| 83 | return -ENOMEM; |
| 84 | b->sum_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS); |
| 85 | if (b->sum_weights == NULL) |
| 86 | return -ENOMEM; |
| 87 | ceph_decode_need(p, end, 2 * b->h.size * sizeof(u32), bad); |
| 88 | for (j = 0; j < b->h.size; j++) { |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 89 | b->item_weights[j] = ceph_decode_32(p); |
| 90 | b->sum_weights[j] = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 91 | } |
| 92 | return 0; |
| 93 | bad: |
| 94 | return -EINVAL; |
| 95 | } |
| 96 | |
| 97 | static int crush_decode_tree_bucket(void **p, void *end, |
| 98 | struct crush_bucket_tree *b) |
| 99 | { |
| 100 | int j; |
| 101 | dout("crush_decode_tree_bucket %p to %p\n", *p, end); |
| 102 | ceph_decode_32_safe(p, end, b->num_nodes, bad); |
| 103 | b->node_weights = kcalloc(b->num_nodes, sizeof(u32), GFP_NOFS); |
| 104 | if (b->node_weights == NULL) |
| 105 | return -ENOMEM; |
| 106 | ceph_decode_need(p, end, b->num_nodes * sizeof(u32), bad); |
| 107 | for (j = 0; j < b->num_nodes; j++) |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 108 | b->node_weights[j] = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 109 | return 0; |
| 110 | bad: |
| 111 | return -EINVAL; |
| 112 | } |
| 113 | |
| 114 | static int crush_decode_straw_bucket(void **p, void *end, |
| 115 | struct crush_bucket_straw *b) |
| 116 | { |
| 117 | int j; |
| 118 | dout("crush_decode_straw_bucket %p to %p\n", *p, end); |
| 119 | b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS); |
| 120 | if (b->item_weights == NULL) |
| 121 | return -ENOMEM; |
| 122 | b->straws = kcalloc(b->h.size, sizeof(u32), GFP_NOFS); |
| 123 | if (b->straws == NULL) |
| 124 | return -ENOMEM; |
| 125 | ceph_decode_need(p, end, 2 * b->h.size * sizeof(u32), bad); |
| 126 | for (j = 0; j < b->h.size; j++) { |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 127 | b->item_weights[j] = ceph_decode_32(p); |
| 128 | b->straws[j] = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 129 | } |
| 130 | return 0; |
| 131 | bad: |
| 132 | return -EINVAL; |
| 133 | } |
| 134 | |
| 135 | static struct crush_map *crush_decode(void *pbyval, void *end) |
| 136 | { |
| 137 | struct crush_map *c; |
| 138 | int err = -EINVAL; |
| 139 | int i, j; |
| 140 | void **p = &pbyval; |
| 141 | void *start = pbyval; |
| 142 | u32 magic; |
| 143 | |
| 144 | dout("crush_decode %p to %p len %d\n", *p, end, (int)(end - *p)); |
| 145 | |
| 146 | c = kzalloc(sizeof(*c), GFP_NOFS); |
| 147 | if (c == NULL) |
| 148 | return ERR_PTR(-ENOMEM); |
| 149 | |
| 150 | ceph_decode_need(p, end, 4*sizeof(u32), bad); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 151 | magic = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 152 | if (magic != CRUSH_MAGIC) { |
| 153 | pr_err("crush_decode magic %x != current %x\n", |
| 154 | (unsigned)magic, (unsigned)CRUSH_MAGIC); |
| 155 | goto bad; |
| 156 | } |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 157 | c->max_buckets = ceph_decode_32(p); |
| 158 | c->max_rules = ceph_decode_32(p); |
| 159 | c->max_devices = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 160 | |
| 161 | c->device_parents = kcalloc(c->max_devices, sizeof(u32), GFP_NOFS); |
| 162 | if (c->device_parents == NULL) |
| 163 | goto badmem; |
| 164 | c->bucket_parents = kcalloc(c->max_buckets, sizeof(u32), GFP_NOFS); |
| 165 | if (c->bucket_parents == NULL) |
| 166 | goto badmem; |
| 167 | |
| 168 | c->buckets = kcalloc(c->max_buckets, sizeof(*c->buckets), GFP_NOFS); |
| 169 | if (c->buckets == NULL) |
| 170 | goto badmem; |
| 171 | c->rules = kcalloc(c->max_rules, sizeof(*c->rules), GFP_NOFS); |
| 172 | if (c->rules == NULL) |
| 173 | goto badmem; |
| 174 | |
| 175 | /* buckets */ |
| 176 | for (i = 0; i < c->max_buckets; i++) { |
| 177 | int size = 0; |
| 178 | u32 alg; |
| 179 | struct crush_bucket *b; |
| 180 | |
| 181 | ceph_decode_32_safe(p, end, alg, bad); |
| 182 | if (alg == 0) { |
| 183 | c->buckets[i] = NULL; |
| 184 | continue; |
| 185 | } |
| 186 | dout("crush_decode bucket %d off %x %p to %p\n", |
| 187 | i, (int)(*p-start), *p, end); |
| 188 | |
| 189 | switch (alg) { |
| 190 | case CRUSH_BUCKET_UNIFORM: |
| 191 | size = sizeof(struct crush_bucket_uniform); |
| 192 | break; |
| 193 | case CRUSH_BUCKET_LIST: |
| 194 | size = sizeof(struct crush_bucket_list); |
| 195 | break; |
| 196 | case CRUSH_BUCKET_TREE: |
| 197 | size = sizeof(struct crush_bucket_tree); |
| 198 | break; |
| 199 | case CRUSH_BUCKET_STRAW: |
| 200 | size = sizeof(struct crush_bucket_straw); |
| 201 | break; |
| 202 | default: |
Sage Weil | 30dc638 | 2009-12-21 14:49:37 -0800 | [diff] [blame] | 203 | err = -EINVAL; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 204 | goto bad; |
| 205 | } |
| 206 | BUG_ON(size == 0); |
| 207 | b = c->buckets[i] = kzalloc(size, GFP_NOFS); |
| 208 | if (b == NULL) |
| 209 | goto badmem; |
| 210 | |
| 211 | ceph_decode_need(p, end, 4*sizeof(u32), bad); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 212 | b->id = ceph_decode_32(p); |
| 213 | b->type = ceph_decode_16(p); |
Sage Weil | fb69039 | 2009-11-07 20:18:22 -0800 | [diff] [blame] | 214 | b->alg = ceph_decode_8(p); |
| 215 | b->hash = ceph_decode_8(p); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 216 | b->weight = ceph_decode_32(p); |
| 217 | b->size = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 218 | |
| 219 | dout("crush_decode bucket size %d off %x %p to %p\n", |
| 220 | b->size, (int)(*p-start), *p, end); |
| 221 | |
| 222 | b->items = kcalloc(b->size, sizeof(__s32), GFP_NOFS); |
| 223 | if (b->items == NULL) |
| 224 | goto badmem; |
| 225 | b->perm = kcalloc(b->size, sizeof(u32), GFP_NOFS); |
| 226 | if (b->perm == NULL) |
| 227 | goto badmem; |
| 228 | b->perm_n = 0; |
| 229 | |
| 230 | ceph_decode_need(p, end, b->size*sizeof(u32), bad); |
| 231 | for (j = 0; j < b->size; j++) |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 232 | b->items[j] = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 233 | |
| 234 | switch (b->alg) { |
| 235 | case CRUSH_BUCKET_UNIFORM: |
| 236 | err = crush_decode_uniform_bucket(p, end, |
| 237 | (struct crush_bucket_uniform *)b); |
| 238 | if (err < 0) |
| 239 | goto bad; |
| 240 | break; |
| 241 | case CRUSH_BUCKET_LIST: |
| 242 | err = crush_decode_list_bucket(p, end, |
| 243 | (struct crush_bucket_list *)b); |
| 244 | if (err < 0) |
| 245 | goto bad; |
| 246 | break; |
| 247 | case CRUSH_BUCKET_TREE: |
| 248 | err = crush_decode_tree_bucket(p, end, |
| 249 | (struct crush_bucket_tree *)b); |
| 250 | if (err < 0) |
| 251 | goto bad; |
| 252 | break; |
| 253 | case CRUSH_BUCKET_STRAW: |
| 254 | err = crush_decode_straw_bucket(p, end, |
| 255 | (struct crush_bucket_straw *)b); |
| 256 | if (err < 0) |
| 257 | goto bad; |
| 258 | break; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | /* rules */ |
| 263 | dout("rule vec is %p\n", c->rules); |
| 264 | for (i = 0; i < c->max_rules; i++) { |
| 265 | u32 yes; |
| 266 | struct crush_rule *r; |
| 267 | |
| 268 | ceph_decode_32_safe(p, end, yes, bad); |
| 269 | if (!yes) { |
| 270 | dout("crush_decode NO rule %d off %x %p to %p\n", |
| 271 | i, (int)(*p-start), *p, end); |
| 272 | c->rules[i] = NULL; |
| 273 | continue; |
| 274 | } |
| 275 | |
| 276 | dout("crush_decode rule %d off %x %p to %p\n", |
| 277 | i, (int)(*p-start), *p, end); |
| 278 | |
| 279 | /* len */ |
| 280 | ceph_decode_32_safe(p, end, yes, bad); |
| 281 | #if BITS_PER_LONG == 32 |
Sage Weil | 30dc638 | 2009-12-21 14:49:37 -0800 | [diff] [blame] | 282 | err = -EINVAL; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 283 | if (yes > ULONG_MAX / sizeof(struct crush_rule_step)) |
| 284 | goto bad; |
| 285 | #endif |
| 286 | r = c->rules[i] = kmalloc(sizeof(*r) + |
| 287 | yes*sizeof(struct crush_rule_step), |
| 288 | GFP_NOFS); |
| 289 | if (r == NULL) |
| 290 | goto badmem; |
| 291 | dout(" rule %d is at %p\n", i, r); |
| 292 | r->len = yes; |
| 293 | ceph_decode_copy_safe(p, end, &r->mask, 4, bad); /* 4 u8's */ |
| 294 | ceph_decode_need(p, end, r->len*3*sizeof(u32), bad); |
| 295 | for (j = 0; j < r->len; j++) { |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 296 | r->steps[j].op = ceph_decode_32(p); |
| 297 | r->steps[j].arg1 = ceph_decode_32(p); |
| 298 | r->steps[j].arg2 = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | |
| 302 | /* ignore trailing name maps. */ |
| 303 | |
| 304 | dout("crush_decode success\n"); |
| 305 | return c; |
| 306 | |
| 307 | badmem: |
| 308 | err = -ENOMEM; |
| 309 | bad: |
| 310 | dout("crush_decode fail %d\n", err); |
| 311 | crush_destroy(c); |
| 312 | return ERR_PTR(err); |
| 313 | } |
| 314 | |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 315 | /* |
Sage Weil | 9794b14 | 2010-02-16 15:53:32 -0800 | [diff] [blame] | 316 | * rbtree of pg_mapping for handling pg_temp (explicit mapping of pgid |
| 317 | * to a set of osds) |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 318 | */ |
Sage Weil | 5104212 | 2009-11-04 11:39:12 -0800 | [diff] [blame] | 319 | static int pgid_cmp(struct ceph_pg l, struct ceph_pg r) |
| 320 | { |
| 321 | u64 a = *(u64 *)&l; |
| 322 | u64 b = *(u64 *)&r; |
| 323 | |
| 324 | if (a < b) |
| 325 | return -1; |
| 326 | if (a > b) |
| 327 | return 1; |
| 328 | return 0; |
| 329 | } |
| 330 | |
Sage Weil | 991abb6 | 2009-10-08 22:22:37 -0700 | [diff] [blame] | 331 | static int __insert_pg_mapping(struct ceph_pg_mapping *new, |
| 332 | struct rb_root *root) |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 333 | { |
| 334 | struct rb_node **p = &root->rb_node; |
| 335 | struct rb_node *parent = NULL; |
| 336 | struct ceph_pg_mapping *pg = NULL; |
Sage Weil | 5104212 | 2009-11-04 11:39:12 -0800 | [diff] [blame] | 337 | int c; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 338 | |
| 339 | while (*p) { |
| 340 | parent = *p; |
| 341 | pg = rb_entry(parent, struct ceph_pg_mapping, node); |
Sage Weil | 5104212 | 2009-11-04 11:39:12 -0800 | [diff] [blame] | 342 | c = pgid_cmp(new->pgid, pg->pgid); |
| 343 | if (c < 0) |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 344 | p = &(*p)->rb_left; |
Sage Weil | 5104212 | 2009-11-04 11:39:12 -0800 | [diff] [blame] | 345 | else if (c > 0) |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 346 | p = &(*p)->rb_right; |
| 347 | else |
Sage Weil | 991abb6 | 2009-10-08 22:22:37 -0700 | [diff] [blame] | 348 | return -EEXIST; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | rb_link_node(&new->node, parent, p); |
| 352 | rb_insert_color(&new->node, root); |
Sage Weil | 991abb6 | 2009-10-08 22:22:37 -0700 | [diff] [blame] | 353 | return 0; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Sage Weil | 9794b14 | 2010-02-16 15:53:32 -0800 | [diff] [blame] | 356 | static struct ceph_pg_mapping *__lookup_pg_mapping(struct rb_root *root, |
| 357 | struct ceph_pg pgid) |
| 358 | { |
| 359 | struct rb_node *n = root->rb_node; |
| 360 | struct ceph_pg_mapping *pg; |
| 361 | int c; |
| 362 | |
| 363 | while (n) { |
| 364 | pg = rb_entry(n, struct ceph_pg_mapping, node); |
| 365 | c = pgid_cmp(pgid, pg->pgid); |
| 366 | if (c < 0) |
| 367 | n = n->rb_left; |
| 368 | else if (c > 0) |
| 369 | n = n->rb_right; |
| 370 | else |
| 371 | return pg; |
| 372 | } |
| 373 | return NULL; |
| 374 | } |
| 375 | |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 376 | /* |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 377 | * rbtree of pg pool info |
| 378 | */ |
| 379 | static int __insert_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *new) |
| 380 | { |
| 381 | struct rb_node **p = &root->rb_node; |
| 382 | struct rb_node *parent = NULL; |
| 383 | struct ceph_pg_pool_info *pi = NULL; |
| 384 | |
| 385 | while (*p) { |
| 386 | parent = *p; |
| 387 | pi = rb_entry(parent, struct ceph_pg_pool_info, node); |
| 388 | if (new->id < pi->id) |
| 389 | p = &(*p)->rb_left; |
| 390 | else if (new->id > pi->id) |
| 391 | p = &(*p)->rb_right; |
| 392 | else |
| 393 | return -EEXIST; |
| 394 | } |
| 395 | |
| 396 | rb_link_node(&new->node, parent, p); |
| 397 | rb_insert_color(&new->node, root); |
| 398 | return 0; |
| 399 | } |
| 400 | |
| 401 | static struct ceph_pg_pool_info *__lookup_pg_pool(struct rb_root *root, int id) |
| 402 | { |
| 403 | struct ceph_pg_pool_info *pi; |
| 404 | struct rb_node *n = root->rb_node; |
| 405 | |
| 406 | while (n) { |
| 407 | pi = rb_entry(n, struct ceph_pg_pool_info, node); |
| 408 | if (id < pi->id) |
| 409 | n = n->rb_left; |
| 410 | else if (id > pi->id) |
| 411 | n = n->rb_right; |
| 412 | else |
| 413 | return pi; |
| 414 | } |
| 415 | return NULL; |
| 416 | } |
| 417 | |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame^] | 418 | static void __remove_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *pi) |
| 419 | { |
| 420 | rb_erase(&pi->node, root); |
| 421 | kfree(pi->name); |
| 422 | kfree(pi); |
| 423 | } |
| 424 | |
Sage Weil | efd7576 | 2010-03-17 10:05:28 -0700 | [diff] [blame] | 425 | void __decode_pool(void **p, struct ceph_pg_pool_info *pi) |
| 426 | { |
| 427 | ceph_decode_copy(p, &pi->v, sizeof(pi->v)); |
| 428 | calc_pg_masks(pi); |
| 429 | *p += le32_to_cpu(pi->v.num_snaps) * sizeof(u64); |
| 430 | *p += le32_to_cpu(pi->v.num_removed_snap_intervals) * sizeof(u64) * 2; |
| 431 | } |
| 432 | |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame^] | 433 | static int __decode_pool_names(void **p, void *end, struct ceph_osdmap *map) |
| 434 | { |
| 435 | struct ceph_pg_pool_info *pi; |
| 436 | u32 num, len, pool; |
| 437 | |
| 438 | ceph_decode_32_safe(p, end, num, bad); |
| 439 | dout(" %d pool names\n", num); |
| 440 | while (num--) { |
| 441 | ceph_decode_32_safe(p, end, pool, bad); |
| 442 | ceph_decode_32_safe(p, end, len, bad); |
| 443 | dout(" pool %d len %d\n", pool, len); |
| 444 | pi = __lookup_pg_pool(&map->pg_pools, pool); |
| 445 | if (pi) { |
| 446 | kfree(pi->name); |
| 447 | pi->name = kmalloc(len + 1, GFP_NOFS); |
| 448 | if (pi->name) { |
| 449 | memcpy(pi->name, *p, len); |
| 450 | pi->name[len] = '\0'; |
| 451 | dout(" name is %s\n", pi->name); |
| 452 | } |
| 453 | } |
| 454 | *p += len; |
| 455 | } |
| 456 | return 0; |
| 457 | |
| 458 | bad: |
| 459 | return -EINVAL; |
| 460 | } |
| 461 | |
| 462 | /* |
| 463 | * osd map |
| 464 | */ |
| 465 | void ceph_osdmap_destroy(struct ceph_osdmap *map) |
| 466 | { |
| 467 | dout("osdmap_destroy %p\n", map); |
| 468 | if (map->crush) |
| 469 | crush_destroy(map->crush); |
| 470 | while (!RB_EMPTY_ROOT(&map->pg_temp)) { |
| 471 | struct ceph_pg_mapping *pg = |
| 472 | rb_entry(rb_first(&map->pg_temp), |
| 473 | struct ceph_pg_mapping, node); |
| 474 | rb_erase(&pg->node, &map->pg_temp); |
| 475 | kfree(pg); |
| 476 | } |
| 477 | while (!RB_EMPTY_ROOT(&map->pg_pools)) { |
| 478 | struct ceph_pg_pool_info *pi = |
| 479 | rb_entry(rb_first(&map->pg_pools), |
| 480 | struct ceph_pg_pool_info, node); |
| 481 | __remove_pg_pool(&map->pg_pools, pi); |
| 482 | } |
| 483 | kfree(map->osd_state); |
| 484 | kfree(map->osd_weight); |
| 485 | kfree(map->osd_addr); |
| 486 | kfree(map); |
| 487 | } |
| 488 | |
| 489 | /* |
| 490 | * adjust max osd value. reallocate arrays. |
| 491 | */ |
| 492 | static int osdmap_set_max_osd(struct ceph_osdmap *map, int max) |
| 493 | { |
| 494 | u8 *state; |
| 495 | struct ceph_entity_addr *addr; |
| 496 | u32 *weight; |
| 497 | |
| 498 | state = kcalloc(max, sizeof(*state), GFP_NOFS); |
| 499 | addr = kcalloc(max, sizeof(*addr), GFP_NOFS); |
| 500 | weight = kcalloc(max, sizeof(*weight), GFP_NOFS); |
| 501 | if (state == NULL || addr == NULL || weight == NULL) { |
| 502 | kfree(state); |
| 503 | kfree(addr); |
| 504 | kfree(weight); |
| 505 | return -ENOMEM; |
| 506 | } |
| 507 | |
| 508 | /* copy old? */ |
| 509 | if (map->osd_state) { |
| 510 | memcpy(state, map->osd_state, map->max_osd*sizeof(*state)); |
| 511 | memcpy(addr, map->osd_addr, map->max_osd*sizeof(*addr)); |
| 512 | memcpy(weight, map->osd_weight, map->max_osd*sizeof(*weight)); |
| 513 | kfree(map->osd_state); |
| 514 | kfree(map->osd_addr); |
| 515 | kfree(map->osd_weight); |
| 516 | } |
| 517 | |
| 518 | map->osd_state = state; |
| 519 | map->osd_weight = weight; |
| 520 | map->osd_addr = addr; |
| 521 | map->max_osd = max; |
| 522 | return 0; |
| 523 | } |
| 524 | |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 525 | /* |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 526 | * decode a full map. |
| 527 | */ |
| 528 | struct ceph_osdmap *osdmap_decode(void **p, void *end) |
| 529 | { |
| 530 | struct ceph_osdmap *map; |
| 531 | u16 version; |
| 532 | u32 len, max, i; |
Sage Weil | 361be86 | 2010-01-25 16:03:02 -0800 | [diff] [blame] | 533 | u8 ev; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 534 | int err = -EINVAL; |
| 535 | void *start = *p; |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 536 | struct ceph_pg_pool_info *pi; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 537 | |
| 538 | dout("osdmap_decode %p to %p len %d\n", *p, end, (int)(end - *p)); |
| 539 | |
| 540 | map = kzalloc(sizeof(*map), GFP_NOFS); |
| 541 | if (map == NULL) |
| 542 | return ERR_PTR(-ENOMEM); |
| 543 | map->pg_temp = RB_ROOT; |
| 544 | |
| 545 | ceph_decode_16_safe(p, end, version, bad); |
Sage Weil | 02f90c6 | 2010-02-04 16:18:10 -0800 | [diff] [blame] | 546 | if (version > CEPH_OSDMAP_VERSION) { |
| 547 | pr_warning("got unknown v %d > %d of osdmap\n", version, |
| 548 | CEPH_OSDMAP_VERSION); |
| 549 | goto bad; |
| 550 | } |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 551 | |
| 552 | ceph_decode_need(p, end, 2*sizeof(u64)+6*sizeof(u32), bad); |
| 553 | ceph_decode_copy(p, &map->fsid, sizeof(map->fsid)); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 554 | map->epoch = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 555 | ceph_decode_copy(p, &map->created, sizeof(map->created)); |
| 556 | ceph_decode_copy(p, &map->modified, sizeof(map->modified)); |
| 557 | |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 558 | ceph_decode_32_safe(p, end, max, bad); |
| 559 | while (max--) { |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 560 | ceph_decode_need(p, end, 4 + 1 + sizeof(pi->v), bad); |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame^] | 561 | pi = kzalloc(sizeof(*pi), GFP_NOFS); |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 562 | if (!pi) |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 563 | goto bad; |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 564 | pi->id = ceph_decode_32(p); |
Sage Weil | 361be86 | 2010-01-25 16:03:02 -0800 | [diff] [blame] | 565 | ev = ceph_decode_8(p); /* encoding version */ |
Sage Weil | 02f90c6 | 2010-02-04 16:18:10 -0800 | [diff] [blame] | 566 | if (ev > CEPH_PG_POOL_VERSION) { |
| 567 | pr_warning("got unknown v %d > %d of ceph_pg_pool\n", |
| 568 | ev, CEPH_PG_POOL_VERSION); |
| 569 | goto bad; |
| 570 | } |
Sage Weil | efd7576 | 2010-03-17 10:05:28 -0700 | [diff] [blame] | 571 | __decode_pool(p, pi); |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 572 | __insert_pg_pool(&map->pg_pools, pi); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 573 | } |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame^] | 574 | |
| 575 | if (version >= 5 && __decode_pool_names(p, end, map) < 0) |
| 576 | goto bad; |
| 577 | |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 578 | ceph_decode_32_safe(p, end, map->pool_max, bad); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 579 | |
| 580 | ceph_decode_32_safe(p, end, map->flags, bad); |
| 581 | |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 582 | max = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 583 | |
| 584 | /* (re)alloc osd arrays */ |
| 585 | err = osdmap_set_max_osd(map, max); |
| 586 | if (err < 0) |
| 587 | goto bad; |
| 588 | dout("osdmap_decode max_osd = %d\n", map->max_osd); |
| 589 | |
| 590 | /* osds */ |
| 591 | err = -EINVAL; |
| 592 | ceph_decode_need(p, end, 3*sizeof(u32) + |
| 593 | map->max_osd*(1 + sizeof(*map->osd_weight) + |
| 594 | sizeof(*map->osd_addr)), bad); |
| 595 | *p += 4; /* skip length field (should match max) */ |
| 596 | ceph_decode_copy(p, map->osd_state, map->max_osd); |
| 597 | |
| 598 | *p += 4; /* skip length field (should match max) */ |
| 599 | for (i = 0; i < map->max_osd; i++) |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 600 | map->osd_weight[i] = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 601 | |
| 602 | *p += 4; /* skip length field (should match max) */ |
| 603 | ceph_decode_copy(p, map->osd_addr, map->max_osd*sizeof(*map->osd_addr)); |
Sage Weil | 63f2d21 | 2009-11-03 15:17:56 -0800 | [diff] [blame] | 604 | for (i = 0; i < map->max_osd; i++) |
| 605 | ceph_decode_addr(&map->osd_addr[i]); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 606 | |
| 607 | /* pg_temp */ |
| 608 | ceph_decode_32_safe(p, end, len, bad); |
| 609 | for (i = 0; i < len; i++) { |
| 610 | int n, j; |
Sage Weil | 5104212 | 2009-11-04 11:39:12 -0800 | [diff] [blame] | 611 | struct ceph_pg pgid; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 612 | struct ceph_pg_mapping *pg; |
| 613 | |
| 614 | ceph_decode_need(p, end, sizeof(u32) + sizeof(u64), bad); |
Sage Weil | 5104212 | 2009-11-04 11:39:12 -0800 | [diff] [blame] | 615 | ceph_decode_copy(p, &pgid, sizeof(pgid)); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 616 | n = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 617 | ceph_decode_need(p, end, n * sizeof(u32), bad); |
Sage Weil | 30dc638 | 2009-12-21 14:49:37 -0800 | [diff] [blame] | 618 | err = -ENOMEM; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 619 | pg = kmalloc(sizeof(*pg) + n*sizeof(u32), GFP_NOFS); |
Sage Weil | 30dc638 | 2009-12-21 14:49:37 -0800 | [diff] [blame] | 620 | if (!pg) |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 621 | goto bad; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 622 | pg->pgid = pgid; |
| 623 | pg->len = n; |
| 624 | for (j = 0; j < n; j++) |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 625 | pg->osds[j] = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 626 | |
Sage Weil | 991abb6 | 2009-10-08 22:22:37 -0700 | [diff] [blame] | 627 | err = __insert_pg_mapping(pg, &map->pg_temp); |
| 628 | if (err) |
| 629 | goto bad; |
Sage Weil | 5104212 | 2009-11-04 11:39:12 -0800 | [diff] [blame] | 630 | dout(" added pg_temp %llx len %d\n", *(u64 *)&pgid, len); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | /* crush */ |
| 634 | ceph_decode_32_safe(p, end, len, bad); |
| 635 | dout("osdmap_decode crush len %d from off 0x%x\n", len, |
| 636 | (int)(*p - start)); |
| 637 | ceph_decode_need(p, end, len, bad); |
| 638 | map->crush = crush_decode(*p, end); |
| 639 | *p += len; |
| 640 | if (IS_ERR(map->crush)) { |
| 641 | err = PTR_ERR(map->crush); |
| 642 | map->crush = NULL; |
| 643 | goto bad; |
| 644 | } |
| 645 | |
| 646 | /* ignore the rest of the map */ |
| 647 | *p = end; |
| 648 | |
| 649 | dout("osdmap_decode done %p %p\n", *p, end); |
| 650 | return map; |
| 651 | |
| 652 | bad: |
| 653 | dout("osdmap_decode fail\n"); |
| 654 | ceph_osdmap_destroy(map); |
| 655 | return ERR_PTR(err); |
| 656 | } |
| 657 | |
| 658 | /* |
| 659 | * decode and apply an incremental map update. |
| 660 | */ |
| 661 | struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end, |
| 662 | struct ceph_osdmap *map, |
| 663 | struct ceph_messenger *msgr) |
| 664 | { |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 665 | struct crush_map *newcrush = NULL; |
| 666 | struct ceph_fsid fsid; |
| 667 | u32 epoch = 0; |
| 668 | struct ceph_timespec modified; |
| 669 | u32 len, pool; |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 670 | __s32 new_pool_max, new_flags, max; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 671 | void *start = *p; |
| 672 | int err = -EINVAL; |
| 673 | u16 version; |
| 674 | struct rb_node *rbp; |
| 675 | |
| 676 | ceph_decode_16_safe(p, end, version, bad); |
Sage Weil | 02f90c6 | 2010-02-04 16:18:10 -0800 | [diff] [blame] | 677 | if (version > CEPH_OSDMAP_INC_VERSION) { |
| 678 | pr_warning("got unknown v %d > %d of inc osdmap\n", version, |
| 679 | CEPH_OSDMAP_INC_VERSION); |
| 680 | goto bad; |
| 681 | } |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 682 | |
| 683 | ceph_decode_need(p, end, sizeof(fsid)+sizeof(modified)+2*sizeof(u32), |
| 684 | bad); |
| 685 | ceph_decode_copy(p, &fsid, sizeof(fsid)); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 686 | epoch = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 687 | BUG_ON(epoch != map->epoch+1); |
| 688 | ceph_decode_copy(p, &modified, sizeof(modified)); |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 689 | new_pool_max = ceph_decode_32(p); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 690 | new_flags = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 691 | |
| 692 | /* full map? */ |
| 693 | ceph_decode_32_safe(p, end, len, bad); |
| 694 | if (len > 0) { |
| 695 | dout("apply_incremental full map len %d, %p to %p\n", |
| 696 | len, *p, end); |
Sage Weil | 30dc638 | 2009-12-21 14:49:37 -0800 | [diff] [blame] | 697 | return osdmap_decode(p, min(*p+len, end)); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | /* new crush? */ |
| 701 | ceph_decode_32_safe(p, end, len, bad); |
| 702 | if (len > 0) { |
| 703 | dout("apply_incremental new crush map len %d, %p to %p\n", |
| 704 | len, *p, end); |
| 705 | newcrush = crush_decode(*p, min(*p+len, end)); |
| 706 | if (IS_ERR(newcrush)) |
| 707 | return ERR_PTR(PTR_ERR(newcrush)); |
| 708 | } |
| 709 | |
| 710 | /* new flags? */ |
| 711 | if (new_flags >= 0) |
| 712 | map->flags = new_flags; |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 713 | if (new_pool_max >= 0) |
| 714 | map->pool_max = new_pool_max; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 715 | |
| 716 | ceph_decode_need(p, end, 5*sizeof(u32), bad); |
| 717 | |
| 718 | /* new max? */ |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 719 | max = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 720 | if (max >= 0) { |
| 721 | err = osdmap_set_max_osd(map, max); |
| 722 | if (err < 0) |
| 723 | goto bad; |
| 724 | } |
| 725 | |
| 726 | map->epoch++; |
| 727 | map->modified = map->modified; |
| 728 | if (newcrush) { |
| 729 | if (map->crush) |
| 730 | crush_destroy(map->crush); |
| 731 | map->crush = newcrush; |
| 732 | newcrush = NULL; |
| 733 | } |
| 734 | |
| 735 | /* new_pool */ |
| 736 | ceph_decode_32_safe(p, end, len, bad); |
| 737 | while (len--) { |
Sage Weil | 361be86 | 2010-01-25 16:03:02 -0800 | [diff] [blame] | 738 | __u8 ev; |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 739 | struct ceph_pg_pool_info *pi; |
Sage Weil | 361be86 | 2010-01-25 16:03:02 -0800 | [diff] [blame] | 740 | |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 741 | ceph_decode_32_safe(p, end, pool, bad); |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 742 | ceph_decode_need(p, end, 1 + sizeof(pi->v), bad); |
Sage Weil | 361be86 | 2010-01-25 16:03:02 -0800 | [diff] [blame] | 743 | ev = ceph_decode_8(p); /* encoding version */ |
Sage Weil | 02f90c6 | 2010-02-04 16:18:10 -0800 | [diff] [blame] | 744 | if (ev > CEPH_PG_POOL_VERSION) { |
| 745 | pr_warning("got unknown v %d > %d of ceph_pg_pool\n", |
| 746 | ev, CEPH_PG_POOL_VERSION); |
| 747 | goto bad; |
| 748 | } |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 749 | pi = __lookup_pg_pool(&map->pg_pools, pool); |
| 750 | if (!pi) { |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame^] | 751 | pi = kzalloc(sizeof(*pi), GFP_NOFS); |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 752 | if (!pi) { |
| 753 | err = -ENOMEM; |
| 754 | goto bad; |
| 755 | } |
| 756 | pi->id = pool; |
| 757 | __insert_pg_pool(&map->pg_pools, pi); |
| 758 | } |
Sage Weil | efd7576 | 2010-03-17 10:05:28 -0700 | [diff] [blame] | 759 | __decode_pool(p, pi); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 760 | } |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame^] | 761 | if (version >= 5 && __decode_pool_names(p, end, map) < 0) |
| 762 | goto bad; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 763 | |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 764 | /* old_pool */ |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 765 | ceph_decode_32_safe(p, end, len, bad); |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 766 | while (len--) { |
| 767 | struct ceph_pg_pool_info *pi; |
| 768 | |
| 769 | ceph_decode_32_safe(p, end, pool, bad); |
| 770 | pi = __lookup_pg_pool(&map->pg_pools, pool); |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame^] | 771 | if (pi) |
| 772 | __remove_pg_pool(&map->pg_pools, pi); |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 773 | } |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 774 | |
| 775 | /* new_up */ |
| 776 | err = -EINVAL; |
| 777 | ceph_decode_32_safe(p, end, len, bad); |
| 778 | while (len--) { |
| 779 | u32 osd; |
| 780 | struct ceph_entity_addr addr; |
| 781 | ceph_decode_32_safe(p, end, osd, bad); |
| 782 | ceph_decode_copy_safe(p, end, &addr, sizeof(addr), bad); |
Sage Weil | 63f2d21 | 2009-11-03 15:17:56 -0800 | [diff] [blame] | 783 | ceph_decode_addr(&addr); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 784 | pr_info("osd%d up\n", osd); |
| 785 | BUG_ON(osd >= map->max_osd); |
| 786 | map->osd_state[osd] |= CEPH_OSD_UP; |
| 787 | map->osd_addr[osd] = addr; |
| 788 | } |
| 789 | |
| 790 | /* new_down */ |
| 791 | ceph_decode_32_safe(p, end, len, bad); |
| 792 | while (len--) { |
| 793 | u32 osd; |
| 794 | ceph_decode_32_safe(p, end, osd, bad); |
| 795 | (*p)++; /* clean flag */ |
Sage Weil | 1bdb70e | 2009-11-06 13:57:49 -0800 | [diff] [blame] | 796 | pr_info("osd%d down\n", osd); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 797 | if (osd < map->max_osd) |
| 798 | map->osd_state[osd] &= ~CEPH_OSD_UP; |
| 799 | } |
| 800 | |
| 801 | /* new_weight */ |
| 802 | ceph_decode_32_safe(p, end, len, bad); |
| 803 | while (len--) { |
| 804 | u32 osd, off; |
| 805 | ceph_decode_need(p, end, sizeof(u32)*2, bad); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 806 | osd = ceph_decode_32(p); |
| 807 | off = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 808 | pr_info("osd%d weight 0x%x %s\n", osd, off, |
| 809 | off == CEPH_OSD_IN ? "(in)" : |
| 810 | (off == CEPH_OSD_OUT ? "(out)" : "")); |
| 811 | if (osd < map->max_osd) |
| 812 | map->osd_weight[osd] = off; |
| 813 | } |
| 814 | |
| 815 | /* new_pg_temp */ |
| 816 | rbp = rb_first(&map->pg_temp); |
| 817 | ceph_decode_32_safe(p, end, len, bad); |
| 818 | while (len--) { |
| 819 | struct ceph_pg_mapping *pg; |
| 820 | int j; |
Sage Weil | 5104212 | 2009-11-04 11:39:12 -0800 | [diff] [blame] | 821 | struct ceph_pg pgid; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 822 | u32 pglen; |
| 823 | ceph_decode_need(p, end, sizeof(u64) + sizeof(u32), bad); |
Sage Weil | 5104212 | 2009-11-04 11:39:12 -0800 | [diff] [blame] | 824 | ceph_decode_copy(p, &pgid, sizeof(pgid)); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 825 | pglen = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 826 | |
| 827 | /* remove any? */ |
Sage Weil | 5104212 | 2009-11-04 11:39:12 -0800 | [diff] [blame] | 828 | while (rbp && pgid_cmp(rb_entry(rbp, struct ceph_pg_mapping, |
| 829 | node)->pgid, pgid) <= 0) { |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 830 | struct rb_node *cur = rbp; |
| 831 | rbp = rb_next(rbp); |
| 832 | dout(" removed pg_temp %llx\n", |
Sage Weil | 5104212 | 2009-11-04 11:39:12 -0800 | [diff] [blame] | 833 | *(u64 *)&rb_entry(cur, struct ceph_pg_mapping, |
| 834 | node)->pgid); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 835 | rb_erase(cur, &map->pg_temp); |
| 836 | } |
| 837 | |
| 838 | if (pglen) { |
| 839 | /* insert */ |
| 840 | ceph_decode_need(p, end, pglen*sizeof(u32), bad); |
| 841 | pg = kmalloc(sizeof(*pg) + sizeof(u32)*pglen, GFP_NOFS); |
| 842 | if (!pg) { |
| 843 | err = -ENOMEM; |
| 844 | goto bad; |
| 845 | } |
| 846 | pg->pgid = pgid; |
| 847 | pg->len = pglen; |
Sage Weil | 7067f79 | 2009-12-21 16:02:37 -0800 | [diff] [blame] | 848 | for (j = 0; j < pglen; j++) |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 849 | pg->osds[j] = ceph_decode_32(p); |
Sage Weil | 991abb6 | 2009-10-08 22:22:37 -0700 | [diff] [blame] | 850 | err = __insert_pg_mapping(pg, &map->pg_temp); |
| 851 | if (err) |
| 852 | goto bad; |
Sage Weil | 5104212 | 2009-11-04 11:39:12 -0800 | [diff] [blame] | 853 | dout(" added pg_temp %llx len %d\n", *(u64 *)&pgid, |
| 854 | pglen); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 855 | } |
| 856 | } |
| 857 | while (rbp) { |
| 858 | struct rb_node *cur = rbp; |
| 859 | rbp = rb_next(rbp); |
| 860 | dout(" removed pg_temp %llx\n", |
Sage Weil | 5104212 | 2009-11-04 11:39:12 -0800 | [diff] [blame] | 861 | *(u64 *)&rb_entry(cur, struct ceph_pg_mapping, |
| 862 | node)->pgid); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 863 | rb_erase(cur, &map->pg_temp); |
| 864 | } |
| 865 | |
| 866 | /* ignore the rest */ |
| 867 | *p = end; |
| 868 | return map; |
| 869 | |
| 870 | bad: |
| 871 | pr_err("corrupt inc osdmap epoch %d off %d (%p of %p-%p)\n", |
| 872 | epoch, (int)(*p - start), *p, start, end); |
Sage Weil | 9ec7cab | 2009-12-14 15:13:47 -0800 | [diff] [blame] | 873 | print_hex_dump(KERN_DEBUG, "osdmap: ", |
| 874 | DUMP_PREFIX_OFFSET, 16, 1, |
| 875 | start, end - start, true); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 876 | if (newcrush) |
| 877 | crush_destroy(newcrush); |
| 878 | return ERR_PTR(err); |
| 879 | } |
| 880 | |
| 881 | |
| 882 | |
| 883 | |
| 884 | /* |
| 885 | * calculate file layout from given offset, length. |
| 886 | * fill in correct oid, logical length, and object extent |
| 887 | * offset, length. |
| 888 | * |
| 889 | * for now, we write only a single su, until we can |
| 890 | * pass a stride back to the caller. |
| 891 | */ |
| 892 | void ceph_calc_file_object_mapping(struct ceph_file_layout *layout, |
| 893 | u64 off, u64 *plen, |
Sage Weil | 645a102 | 2009-10-28 15:15:05 -0700 | [diff] [blame] | 894 | u64 *ono, |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 895 | u64 *oxoff, u64 *oxlen) |
| 896 | { |
| 897 | u32 osize = le32_to_cpu(layout->fl_object_size); |
| 898 | u32 su = le32_to_cpu(layout->fl_stripe_unit); |
| 899 | u32 sc = le32_to_cpu(layout->fl_stripe_count); |
| 900 | u32 bl, stripeno, stripepos, objsetno; |
| 901 | u32 su_per_object; |
Noah Watkins | ff1d1f7 | 2009-10-30 12:57:30 -0700 | [diff] [blame] | 902 | u64 t, su_offset; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 903 | |
| 904 | dout("mapping %llu~%llu osize %u fl_su %u\n", off, *plen, |
| 905 | osize, su); |
Noah Watkins | 35e054a | 2009-10-28 14:04:48 -0700 | [diff] [blame] | 906 | su_per_object = osize / su; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 907 | dout("osize %u / su %u = su_per_object %u\n", osize, su, |
| 908 | su_per_object); |
| 909 | |
| 910 | BUG_ON((su & ~PAGE_MASK) != 0); |
| 911 | /* bl = *off / su; */ |
| 912 | t = off; |
| 913 | do_div(t, su); |
| 914 | bl = t; |
| 915 | dout("off %llu / su %u = bl %u\n", off, su, bl); |
| 916 | |
| 917 | stripeno = bl / sc; |
| 918 | stripepos = bl % sc; |
| 919 | objsetno = stripeno / su_per_object; |
| 920 | |
Sage Weil | 645a102 | 2009-10-28 15:15:05 -0700 | [diff] [blame] | 921 | *ono = objsetno * sc + stripepos; |
| 922 | dout("objset %u * sc %u = ono %u\n", objsetno, sc, (unsigned)*ono); |
| 923 | |
| 924 | /* *oxoff = *off % layout->fl_stripe_unit; # offset in su */ |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 925 | t = off; |
Noah Watkins | ff1d1f7 | 2009-10-30 12:57:30 -0700 | [diff] [blame] | 926 | su_offset = do_div(t, su); |
| 927 | *oxoff = su_offset + (stripeno % su_per_object) * su; |
Sage Weil | 645a102 | 2009-10-28 15:15:05 -0700 | [diff] [blame] | 928 | |
Noah Watkins | ff1d1f7 | 2009-10-30 12:57:30 -0700 | [diff] [blame] | 929 | /* |
| 930 | * Calculate the length of the extent being written to the selected |
| 931 | * object. This is the minimum of the full length requested (plen) or |
| 932 | * the remainder of the current stripe being written to. |
| 933 | */ |
| 934 | *oxlen = min_t(u64, *plen, su - su_offset); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 935 | *plen = *oxlen; |
| 936 | |
| 937 | dout(" obj extent %llu~%llu\n", *oxoff, *oxlen); |
| 938 | } |
| 939 | |
| 940 | /* |
| 941 | * calculate an object layout (i.e. pgid) from an oid, |
| 942 | * file_layout, and osdmap |
| 943 | */ |
| 944 | int ceph_calc_object_layout(struct ceph_object_layout *ol, |
| 945 | const char *oid, |
| 946 | struct ceph_file_layout *fl, |
| 947 | struct ceph_osdmap *osdmap) |
| 948 | { |
| 949 | unsigned num, num_mask; |
Sage Weil | 5104212 | 2009-11-04 11:39:12 -0800 | [diff] [blame] | 950 | struct ceph_pg pgid; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 951 | s32 preferred = (s32)le32_to_cpu(fl->fl_pg_preferred); |
| 952 | int poolid = le32_to_cpu(fl->fl_pg_pool); |
| 953 | struct ceph_pg_pool_info *pool; |
Sage Weil | 5104212 | 2009-11-04 11:39:12 -0800 | [diff] [blame] | 954 | unsigned ps; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 955 | |
Sage Weil | 30dc638 | 2009-12-21 14:49:37 -0800 | [diff] [blame] | 956 | BUG_ON(!osdmap); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 957 | |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 958 | pool = __lookup_pg_pool(&osdmap->pg_pools, poolid); |
| 959 | if (!pool) |
| 960 | return -EIO; |
Sage Weil | 1654dd0 | 2009-11-06 21:55:25 -0800 | [diff] [blame] | 961 | ps = ceph_str_hash(pool->v.object_hash, oid, strlen(oid)); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 962 | if (preferred >= 0) { |
Sage Weil | 5104212 | 2009-11-04 11:39:12 -0800 | [diff] [blame] | 963 | ps += preferred; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 964 | num = le32_to_cpu(pool->v.lpg_num); |
| 965 | num_mask = pool->lpg_num_mask; |
| 966 | } else { |
| 967 | num = le32_to_cpu(pool->v.pg_num); |
| 968 | num_mask = pool->pg_num_mask; |
| 969 | } |
| 970 | |
Sage Weil | 5104212 | 2009-11-04 11:39:12 -0800 | [diff] [blame] | 971 | pgid.ps = cpu_to_le16(ps); |
| 972 | pgid.preferred = cpu_to_le16(preferred); |
| 973 | pgid.pool = fl->fl_pg_pool; |
Sage Weil | ee7fdfa | 2009-10-19 11:41:51 -0700 | [diff] [blame] | 974 | if (preferred >= 0) |
Sage Weil | 5104212 | 2009-11-04 11:39:12 -0800 | [diff] [blame] | 975 | dout("calc_object_layout '%s' pgid %d.%xp%d\n", oid, poolid, ps, |
| 976 | (int)preferred); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 977 | else |
Sage Weil | 5104212 | 2009-11-04 11:39:12 -0800 | [diff] [blame] | 978 | dout("calc_object_layout '%s' pgid %d.%x\n", oid, poolid, ps); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 979 | |
Sage Weil | 5104212 | 2009-11-04 11:39:12 -0800 | [diff] [blame] | 980 | ol->ol_pgid = pgid; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 981 | ol->ol_stripe_unit = fl->fl_object_stripe_unit; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 982 | return 0; |
| 983 | } |
| 984 | |
| 985 | /* |
| 986 | * Calculate raw osd vector for the given pgid. Return pointer to osd |
| 987 | * array, or NULL on failure. |
| 988 | */ |
Sage Weil | 5104212 | 2009-11-04 11:39:12 -0800 | [diff] [blame] | 989 | static int *calc_pg_raw(struct ceph_osdmap *osdmap, struct ceph_pg pgid, |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 990 | int *osds, int *num) |
| 991 | { |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 992 | struct ceph_pg_mapping *pg; |
| 993 | struct ceph_pg_pool_info *pool; |
| 994 | int ruleno; |
Sage Weil | 5104212 | 2009-11-04 11:39:12 -0800 | [diff] [blame] | 995 | unsigned poolid, ps, pps; |
| 996 | int preferred; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 997 | |
| 998 | /* pg_temp? */ |
Sage Weil | 9794b14 | 2010-02-16 15:53:32 -0800 | [diff] [blame] | 999 | pg = __lookup_pg_mapping(&osdmap->pg_temp, pgid); |
| 1000 | if (pg) { |
| 1001 | *num = pg->len; |
| 1002 | return pg->osds; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | /* crush */ |
Sage Weil | 5104212 | 2009-11-04 11:39:12 -0800 | [diff] [blame] | 1006 | poolid = le32_to_cpu(pgid.pool); |
| 1007 | ps = le16_to_cpu(pgid.ps); |
| 1008 | preferred = (s16)le16_to_cpu(pgid.preferred); |
| 1009 | |
Sage Weil | 767ea5c | 2009-12-09 12:34:01 -0800 | [diff] [blame] | 1010 | /* don't forcefeed bad device ids to crush */ |
| 1011 | if (preferred >= osdmap->max_osd || |
| 1012 | preferred >= osdmap->crush->max_devices) |
| 1013 | preferred = -1; |
| 1014 | |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 1015 | pool = __lookup_pg_pool(&osdmap->pg_pools, poolid); |
| 1016 | if (!pool) |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1017 | return NULL; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1018 | ruleno = crush_find_rule(osdmap->crush, pool->v.crush_ruleset, |
| 1019 | pool->v.type, pool->v.size); |
| 1020 | if (ruleno < 0) { |
| 1021 | pr_err("no crush rule pool %d type %d size %d\n", |
Sage Weil | 5104212 | 2009-11-04 11:39:12 -0800 | [diff] [blame] | 1022 | poolid, pool->v.type, pool->v.size); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1023 | return NULL; |
| 1024 | } |
| 1025 | |
Sage Weil | 5104212 | 2009-11-04 11:39:12 -0800 | [diff] [blame] | 1026 | if (preferred >= 0) |
| 1027 | pps = ceph_stable_mod(ps, |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1028 | le32_to_cpu(pool->v.lpgp_num), |
| 1029 | pool->lpgp_num_mask); |
| 1030 | else |
Sage Weil | 5104212 | 2009-11-04 11:39:12 -0800 | [diff] [blame] | 1031 | pps = ceph_stable_mod(ps, |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1032 | le32_to_cpu(pool->v.pgp_num), |
| 1033 | pool->pgp_num_mask); |
Sage Weil | 5104212 | 2009-11-04 11:39:12 -0800 | [diff] [blame] | 1034 | pps += poolid; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1035 | *num = crush_do_rule(osdmap->crush, ruleno, pps, osds, |
| 1036 | min_t(int, pool->v.size, *num), |
Sage Weil | 5104212 | 2009-11-04 11:39:12 -0800 | [diff] [blame] | 1037 | preferred, osdmap->osd_weight); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1038 | return osds; |
| 1039 | } |
| 1040 | |
| 1041 | /* |
| 1042 | * Return primary osd for given pgid, or -1 if none. |
| 1043 | */ |
Sage Weil | 5104212 | 2009-11-04 11:39:12 -0800 | [diff] [blame] | 1044 | int ceph_calc_pg_primary(struct ceph_osdmap *osdmap, struct ceph_pg pgid) |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1045 | { |
| 1046 | int rawosds[10], *osds; |
| 1047 | int i, num = ARRAY_SIZE(rawosds); |
| 1048 | |
| 1049 | osds = calc_pg_raw(osdmap, pgid, rawosds, &num); |
| 1050 | if (!osds) |
| 1051 | return -1; |
| 1052 | |
| 1053 | /* primary is first up osd */ |
| 1054 | for (i = 0; i < num; i++) |
| 1055 | if (ceph_osd_is_up(osdmap, osds[i])) { |
| 1056 | return osds[i]; |
| 1057 | break; |
| 1058 | } |
| 1059 | return -1; |
| 1060 | } |