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