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