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