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