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 | { |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 48 | pi->pg_num_mask = (1 << calc_bits_of(pi->pg_num-1)) - 1; |
| 49 | pi->pgp_num_mask = (1 << calc_bits_of(pi->pgp_num-1)) - 1; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | /* |
| 53 | * decode crush map |
| 54 | */ |
| 55 | static int crush_decode_uniform_bucket(void **p, void *end, |
| 56 | struct crush_bucket_uniform *b) |
| 57 | { |
| 58 | dout("crush_decode_uniform_bucket %p to %p\n", *p, end); |
| 59 | ceph_decode_need(p, end, (1+b->h.size) * sizeof(u32), bad); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 60 | b->item_weight = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 61 | return 0; |
| 62 | bad: |
| 63 | return -EINVAL; |
| 64 | } |
| 65 | |
| 66 | static int crush_decode_list_bucket(void **p, void *end, |
| 67 | struct crush_bucket_list *b) |
| 68 | { |
| 69 | int j; |
| 70 | dout("crush_decode_list_bucket %p to %p\n", *p, end); |
| 71 | b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS); |
| 72 | if (b->item_weights == NULL) |
| 73 | return -ENOMEM; |
| 74 | b->sum_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS); |
| 75 | if (b->sum_weights == NULL) |
| 76 | return -ENOMEM; |
| 77 | ceph_decode_need(p, end, 2 * b->h.size * sizeof(u32), bad); |
| 78 | for (j = 0; j < b->h.size; j++) { |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 79 | b->item_weights[j] = ceph_decode_32(p); |
| 80 | b->sum_weights[j] = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 81 | } |
| 82 | return 0; |
| 83 | bad: |
| 84 | return -EINVAL; |
| 85 | } |
| 86 | |
| 87 | static int crush_decode_tree_bucket(void **p, void *end, |
| 88 | struct crush_bucket_tree *b) |
| 89 | { |
| 90 | int j; |
| 91 | dout("crush_decode_tree_bucket %p to %p\n", *p, end); |
| 92 | ceph_decode_32_safe(p, end, b->num_nodes, bad); |
| 93 | b->node_weights = kcalloc(b->num_nodes, sizeof(u32), GFP_NOFS); |
| 94 | if (b->node_weights == NULL) |
| 95 | return -ENOMEM; |
| 96 | ceph_decode_need(p, end, b->num_nodes * sizeof(u32), bad); |
| 97 | for (j = 0; j < b->num_nodes; j++) |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 98 | b->node_weights[j] = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 99 | return 0; |
| 100 | bad: |
| 101 | return -EINVAL; |
| 102 | } |
| 103 | |
| 104 | static int crush_decode_straw_bucket(void **p, void *end, |
| 105 | struct crush_bucket_straw *b) |
| 106 | { |
| 107 | int j; |
| 108 | dout("crush_decode_straw_bucket %p to %p\n", *p, end); |
| 109 | b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS); |
| 110 | if (b->item_weights == NULL) |
| 111 | return -ENOMEM; |
| 112 | b->straws = kcalloc(b->h.size, sizeof(u32), GFP_NOFS); |
| 113 | if (b->straws == NULL) |
| 114 | return -ENOMEM; |
| 115 | ceph_decode_need(p, end, 2 * b->h.size * sizeof(u32), bad); |
| 116 | for (j = 0; j < b->h.size; j++) { |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 117 | b->item_weights[j] = ceph_decode_32(p); |
| 118 | b->straws[j] = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 119 | } |
| 120 | return 0; |
| 121 | bad: |
| 122 | return -EINVAL; |
| 123 | } |
| 124 | |
Sage Weil | 546f04e | 2012-07-30 18:15:23 -0700 | [diff] [blame] | 125 | static int skip_name_map(void **p, void *end) |
| 126 | { |
| 127 | int len; |
| 128 | ceph_decode_32_safe(p, end, len ,bad); |
| 129 | while (len--) { |
| 130 | int strlen; |
| 131 | *p += sizeof(u32); |
| 132 | ceph_decode_32_safe(p, end, strlen, bad); |
| 133 | *p += strlen; |
| 134 | } |
| 135 | return 0; |
| 136 | bad: |
| 137 | return -EINVAL; |
| 138 | } |
| 139 | |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 140 | static struct crush_map *crush_decode(void *pbyval, void *end) |
| 141 | { |
| 142 | struct crush_map *c; |
| 143 | int err = -EINVAL; |
| 144 | int i, j; |
| 145 | void **p = &pbyval; |
| 146 | void *start = pbyval; |
| 147 | u32 magic; |
Sage Weil | 546f04e | 2012-07-30 18:15:23 -0700 | [diff] [blame] | 148 | u32 num_name_maps; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 149 | |
| 150 | dout("crush_decode %p to %p len %d\n", *p, end, (int)(end - *p)); |
| 151 | |
| 152 | c = kzalloc(sizeof(*c), GFP_NOFS); |
| 153 | if (c == NULL) |
| 154 | return ERR_PTR(-ENOMEM); |
| 155 | |
Sage Weil | 546f04e | 2012-07-30 18:15:23 -0700 | [diff] [blame] | 156 | /* set tunables to default values */ |
| 157 | c->choose_local_tries = 2; |
| 158 | c->choose_local_fallback_tries = 5; |
| 159 | c->choose_total_tries = 19; |
Jim Schutt | 1604f48 | 2012-11-30 09:15:25 -0700 | [diff] [blame] | 160 | c->chooseleaf_descend_once = 0; |
Sage Weil | 546f04e | 2012-07-30 18:15:23 -0700 | [diff] [blame] | 161 | |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 162 | ceph_decode_need(p, end, 4*sizeof(u32), bad); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 163 | magic = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 164 | if (magic != CRUSH_MAGIC) { |
| 165 | pr_err("crush_decode magic %x != current %x\n", |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 166 | (unsigned int)magic, (unsigned int)CRUSH_MAGIC); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 167 | goto bad; |
| 168 | } |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 169 | c->max_buckets = ceph_decode_32(p); |
| 170 | c->max_rules = ceph_decode_32(p); |
| 171 | c->max_devices = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 172 | |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 173 | c->buckets = kcalloc(c->max_buckets, sizeof(*c->buckets), GFP_NOFS); |
| 174 | if (c->buckets == NULL) |
| 175 | goto badmem; |
| 176 | c->rules = kcalloc(c->max_rules, sizeof(*c->rules), GFP_NOFS); |
| 177 | if (c->rules == NULL) |
| 178 | goto badmem; |
| 179 | |
| 180 | /* buckets */ |
| 181 | for (i = 0; i < c->max_buckets; i++) { |
| 182 | int size = 0; |
| 183 | u32 alg; |
| 184 | struct crush_bucket *b; |
| 185 | |
| 186 | ceph_decode_32_safe(p, end, alg, bad); |
| 187 | if (alg == 0) { |
| 188 | c->buckets[i] = NULL; |
| 189 | continue; |
| 190 | } |
| 191 | dout("crush_decode bucket %d off %x %p to %p\n", |
| 192 | i, (int)(*p-start), *p, end); |
| 193 | |
| 194 | switch (alg) { |
| 195 | case CRUSH_BUCKET_UNIFORM: |
| 196 | size = sizeof(struct crush_bucket_uniform); |
| 197 | break; |
| 198 | case CRUSH_BUCKET_LIST: |
| 199 | size = sizeof(struct crush_bucket_list); |
| 200 | break; |
| 201 | case CRUSH_BUCKET_TREE: |
| 202 | size = sizeof(struct crush_bucket_tree); |
| 203 | break; |
| 204 | case CRUSH_BUCKET_STRAW: |
| 205 | size = sizeof(struct crush_bucket_straw); |
| 206 | break; |
| 207 | default: |
Sage Weil | 30dc638 | 2009-12-21 14:49:37 -0800 | [diff] [blame] | 208 | err = -EINVAL; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 209 | goto bad; |
| 210 | } |
| 211 | BUG_ON(size == 0); |
| 212 | b = c->buckets[i] = kzalloc(size, GFP_NOFS); |
| 213 | if (b == NULL) |
| 214 | goto badmem; |
| 215 | |
| 216 | ceph_decode_need(p, end, 4*sizeof(u32), bad); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 217 | b->id = ceph_decode_32(p); |
| 218 | b->type = ceph_decode_16(p); |
Sage Weil | fb69039 | 2009-11-07 20:18:22 -0800 | [diff] [blame] | 219 | b->alg = ceph_decode_8(p); |
| 220 | b->hash = ceph_decode_8(p); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 221 | b->weight = ceph_decode_32(p); |
| 222 | b->size = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 223 | |
| 224 | dout("crush_decode bucket size %d off %x %p to %p\n", |
| 225 | b->size, (int)(*p-start), *p, end); |
| 226 | |
| 227 | b->items = kcalloc(b->size, sizeof(__s32), GFP_NOFS); |
| 228 | if (b->items == NULL) |
| 229 | goto badmem; |
| 230 | b->perm = kcalloc(b->size, sizeof(u32), GFP_NOFS); |
| 231 | if (b->perm == NULL) |
| 232 | goto badmem; |
| 233 | b->perm_n = 0; |
| 234 | |
| 235 | ceph_decode_need(p, end, b->size*sizeof(u32), bad); |
| 236 | for (j = 0; j < b->size; j++) |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 237 | b->items[j] = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 238 | |
| 239 | switch (b->alg) { |
| 240 | case CRUSH_BUCKET_UNIFORM: |
| 241 | err = crush_decode_uniform_bucket(p, end, |
| 242 | (struct crush_bucket_uniform *)b); |
| 243 | if (err < 0) |
| 244 | goto bad; |
| 245 | break; |
| 246 | case CRUSH_BUCKET_LIST: |
| 247 | err = crush_decode_list_bucket(p, end, |
| 248 | (struct crush_bucket_list *)b); |
| 249 | if (err < 0) |
| 250 | goto bad; |
| 251 | break; |
| 252 | case CRUSH_BUCKET_TREE: |
| 253 | err = crush_decode_tree_bucket(p, end, |
| 254 | (struct crush_bucket_tree *)b); |
| 255 | if (err < 0) |
| 256 | goto bad; |
| 257 | break; |
| 258 | case CRUSH_BUCKET_STRAW: |
| 259 | err = crush_decode_straw_bucket(p, end, |
| 260 | (struct crush_bucket_straw *)b); |
| 261 | if (err < 0) |
| 262 | goto bad; |
| 263 | break; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | /* rules */ |
| 268 | dout("rule vec is %p\n", c->rules); |
| 269 | for (i = 0; i < c->max_rules; i++) { |
| 270 | u32 yes; |
| 271 | struct crush_rule *r; |
| 272 | |
| 273 | ceph_decode_32_safe(p, end, yes, bad); |
| 274 | if (!yes) { |
| 275 | dout("crush_decode NO rule %d off %x %p to %p\n", |
| 276 | i, (int)(*p-start), *p, end); |
| 277 | c->rules[i] = NULL; |
| 278 | continue; |
| 279 | } |
| 280 | |
| 281 | dout("crush_decode rule %d off %x %p to %p\n", |
| 282 | i, (int)(*p-start), *p, end); |
| 283 | |
| 284 | /* len */ |
| 285 | ceph_decode_32_safe(p, end, yes, bad); |
| 286 | #if BITS_PER_LONG == 32 |
Sage Weil | 30dc638 | 2009-12-21 14:49:37 -0800 | [diff] [blame] | 287 | err = -EINVAL; |
Xi Wang | 6448669 | 2012-02-16 11:55:48 -0500 | [diff] [blame] | 288 | if (yes > (ULONG_MAX - sizeof(*r)) |
| 289 | / sizeof(struct crush_rule_step)) |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 290 | goto bad; |
| 291 | #endif |
| 292 | r = c->rules[i] = kmalloc(sizeof(*r) + |
| 293 | yes*sizeof(struct crush_rule_step), |
| 294 | GFP_NOFS); |
| 295 | if (r == NULL) |
| 296 | goto badmem; |
| 297 | dout(" rule %d is at %p\n", i, r); |
| 298 | r->len = yes; |
| 299 | ceph_decode_copy_safe(p, end, &r->mask, 4, bad); /* 4 u8's */ |
| 300 | ceph_decode_need(p, end, r->len*3*sizeof(u32), bad); |
| 301 | for (j = 0; j < r->len; j++) { |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 302 | r->steps[j].op = ceph_decode_32(p); |
| 303 | r->steps[j].arg1 = ceph_decode_32(p); |
| 304 | r->steps[j].arg2 = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
| 308 | /* ignore trailing name maps. */ |
Sage Weil | 546f04e | 2012-07-30 18:15:23 -0700 | [diff] [blame] | 309 | for (num_name_maps = 0; num_name_maps < 3; num_name_maps++) { |
| 310 | err = skip_name_map(p, end); |
| 311 | if (err < 0) |
| 312 | goto done; |
| 313 | } |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 314 | |
Sage Weil | 546f04e | 2012-07-30 18:15:23 -0700 | [diff] [blame] | 315 | /* tunables */ |
| 316 | ceph_decode_need(p, end, 3*sizeof(u32), done); |
| 317 | c->choose_local_tries = ceph_decode_32(p); |
| 318 | c->choose_local_fallback_tries = ceph_decode_32(p); |
| 319 | c->choose_total_tries = ceph_decode_32(p); |
| 320 | dout("crush decode tunable choose_local_tries = %d", |
| 321 | c->choose_local_tries); |
| 322 | dout("crush decode tunable choose_local_fallback_tries = %d", |
| 323 | c->choose_local_fallback_tries); |
| 324 | dout("crush decode tunable choose_total_tries = %d", |
| 325 | c->choose_total_tries); |
| 326 | |
Jim Schutt | 1604f48 | 2012-11-30 09:15:25 -0700 | [diff] [blame] | 327 | ceph_decode_need(p, end, sizeof(u32), done); |
| 328 | c->chooseleaf_descend_once = ceph_decode_32(p); |
| 329 | dout("crush decode tunable chooseleaf_descend_once = %d", |
| 330 | c->chooseleaf_descend_once); |
| 331 | |
Ilya Dryomov | f140662 | 2014-05-09 18:27:34 +0400 | [diff] [blame] | 332 | ceph_decode_need(p, end, sizeof(u8), done); |
| 333 | c->chooseleaf_vary_r = ceph_decode_8(p); |
| 334 | dout("crush decode tunable chooseleaf_vary_r = %d", |
| 335 | c->chooseleaf_vary_r); |
| 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 |
Ilya Dryomov | 9686f94 | 2014-03-21 19:05:29 +0200 | [diff] [blame] | 351 | * to a set of osds) and primary_temp (explicit primary setting) |
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 | |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 455 | static struct ceph_pg_pool_info *__lookup_pg_pool(struct rb_root *root, u64 id) |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 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 | |
Ilya Dryomov | ce7f6a2 | 2014-01-27 17:40:19 +0200 | [diff] [blame] | 472 | struct ceph_pg_pool_info *ceph_pg_pool_by_id(struct ceph_osdmap *map, u64 id) |
| 473 | { |
| 474 | return __lookup_pg_pool(&map->pg_pools, id); |
| 475 | } |
| 476 | |
Alex Elder | 72afc71 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 477 | const char *ceph_pg_pool_name_by_id(struct ceph_osdmap *map, u64 id) |
| 478 | { |
| 479 | struct ceph_pg_pool_info *pi; |
| 480 | |
| 481 | if (id == CEPH_NOPOOL) |
| 482 | return NULL; |
| 483 | |
| 484 | if (WARN_ON_ONCE(id > (u64) INT_MAX)) |
| 485 | return NULL; |
| 486 | |
| 487 | pi = __lookup_pg_pool(&map->pg_pools, (int) id); |
| 488 | |
| 489 | return pi ? pi->name : NULL; |
| 490 | } |
| 491 | EXPORT_SYMBOL(ceph_pg_pool_name_by_id); |
| 492 | |
Yehuda Sadeh | 7669a2c | 2010-05-17 12:31:35 -0700 | [diff] [blame] | 493 | int ceph_pg_poolid_by_name(struct ceph_osdmap *map, const char *name) |
| 494 | { |
| 495 | struct rb_node *rbp; |
| 496 | |
| 497 | for (rbp = rb_first(&map->pg_pools); rbp; rbp = rb_next(rbp)) { |
| 498 | struct ceph_pg_pool_info *pi = |
| 499 | rb_entry(rbp, struct ceph_pg_pool_info, node); |
| 500 | if (pi->name && strcmp(pi->name, name) == 0) |
| 501 | return pi->id; |
| 502 | } |
| 503 | return -ENOENT; |
| 504 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 505 | EXPORT_SYMBOL(ceph_pg_poolid_by_name); |
Yehuda Sadeh | 7669a2c | 2010-05-17 12:31:35 -0700 | [diff] [blame] | 506 | |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 507 | static void __remove_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *pi) |
| 508 | { |
| 509 | rb_erase(&pi->node, root); |
| 510 | kfree(pi->name); |
| 511 | kfree(pi); |
| 512 | } |
| 513 | |
Ilya Dryomov | 0f70c7e | 2014-03-21 19:05:27 +0200 | [diff] [blame] | 514 | 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] | 515 | { |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 516 | u8 ev, cv; |
| 517 | unsigned len, num; |
| 518 | void *pool_end; |
Sage Weil | 73a7e69 | 2010-08-02 11:00:55 -0700 | [diff] [blame] | 519 | |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 520 | ceph_decode_need(p, end, 2 + 4, bad); |
| 521 | ev = ceph_decode_8(p); /* encoding version */ |
| 522 | cv = ceph_decode_8(p); /* compat version */ |
| 523 | if (ev < 5) { |
Joe Perches | b9a6789 | 2014-09-09 21:17:29 -0700 | [diff] [blame] | 524 | pr_warn("got v %d < 5 cv %d of ceph_pg_pool\n", ev, cv); |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 525 | return -EINVAL; |
| 526 | } |
Ilya Dryomov | 17a13e40 | 2014-01-27 17:40:19 +0200 | [diff] [blame] | 527 | if (cv > 9) { |
Joe Perches | b9a6789 | 2014-09-09 21:17:29 -0700 | [diff] [blame] | 528 | pr_warn("got v %d cv %d > 9 of ceph_pg_pool\n", ev, cv); |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 529 | return -EINVAL; |
| 530 | } |
| 531 | len = ceph_decode_32(p); |
| 532 | ceph_decode_need(p, end, len, bad); |
| 533 | pool_end = *p + len; |
Sage Weil | 73a7e69 | 2010-08-02 11:00:55 -0700 | [diff] [blame] | 534 | |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 535 | pi->type = ceph_decode_8(p); |
| 536 | pi->size = ceph_decode_8(p); |
| 537 | pi->crush_ruleset = ceph_decode_8(p); |
| 538 | pi->object_hash = ceph_decode_8(p); |
| 539 | |
| 540 | pi->pg_num = ceph_decode_32(p); |
| 541 | pi->pgp_num = ceph_decode_32(p); |
| 542 | |
| 543 | *p += 4 + 4; /* skip lpg* */ |
| 544 | *p += 4; /* skip last_change */ |
| 545 | *p += 8 + 4; /* skip snap_seq, snap_epoch */ |
| 546 | |
| 547 | /* skip snaps */ |
| 548 | num = ceph_decode_32(p); |
| 549 | while (num--) { |
| 550 | *p += 8; /* snapid key */ |
| 551 | *p += 1 + 1; /* versions */ |
| 552 | len = ceph_decode_32(p); |
| 553 | *p += len; |
Sage Weil | 73a7e69 | 2010-08-02 11:00:55 -0700 | [diff] [blame] | 554 | } |
| 555 | |
Ilya Dryomov | 17a13e40 | 2014-01-27 17:40:19 +0200 | [diff] [blame] | 556 | /* skip removed_snaps */ |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 557 | num = ceph_decode_32(p); |
| 558 | *p += num * (8 + 8); |
| 559 | |
| 560 | *p += 8; /* skip auid */ |
| 561 | pi->flags = ceph_decode_64(p); |
Ilya Dryomov | 17a13e40 | 2014-01-27 17:40:19 +0200 | [diff] [blame] | 562 | *p += 4; /* skip crash_replay_interval */ |
| 563 | |
| 564 | if (ev >= 7) |
| 565 | *p += 1; /* skip min_size */ |
| 566 | |
| 567 | if (ev >= 8) |
| 568 | *p += 8 + 8; /* skip quota_max_* */ |
| 569 | |
| 570 | if (ev >= 9) { |
| 571 | /* skip tiers */ |
| 572 | num = ceph_decode_32(p); |
| 573 | *p += num * 8; |
| 574 | |
| 575 | *p += 8; /* skip tier_of */ |
| 576 | *p += 1; /* skip cache_mode */ |
| 577 | |
| 578 | pi->read_tier = ceph_decode_64(p); |
| 579 | pi->write_tier = ceph_decode_64(p); |
| 580 | } else { |
| 581 | pi->read_tier = -1; |
| 582 | pi->write_tier = -1; |
| 583 | } |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 584 | |
| 585 | /* ignore the rest */ |
| 586 | |
| 587 | *p = pool_end; |
| 588 | calc_pg_masks(pi); |
Sage Weil | 73a7e69 | 2010-08-02 11:00:55 -0700 | [diff] [blame] | 589 | return 0; |
| 590 | |
| 591 | bad: |
| 592 | return -EINVAL; |
Sage Weil | efd7576 | 2010-03-17 10:05:28 -0700 | [diff] [blame] | 593 | } |
| 594 | |
Ilya Dryomov | 0f70c7e | 2014-03-21 19:05:27 +0200 | [diff] [blame] | 595 | static int decode_pool_names(void **p, void *end, struct ceph_osdmap *map) |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 596 | { |
| 597 | struct ceph_pg_pool_info *pi; |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 598 | u32 num, len; |
| 599 | u64 pool; |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 600 | |
| 601 | ceph_decode_32_safe(p, end, num, bad); |
| 602 | dout(" %d pool names\n", num); |
| 603 | while (num--) { |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 604 | ceph_decode_64_safe(p, end, pool, bad); |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 605 | ceph_decode_32_safe(p, end, len, bad); |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 606 | dout(" pool %llu len %d\n", pool, len); |
Xi Wang | ad3b904 | 2012-06-06 19:35:55 -0500 | [diff] [blame] | 607 | ceph_decode_need(p, end, len, bad); |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 608 | pi = __lookup_pg_pool(&map->pg_pools, pool); |
| 609 | if (pi) { |
Xi Wang | ad3b904 | 2012-06-06 19:35:55 -0500 | [diff] [blame] | 610 | char *name = kstrndup(*p, len, GFP_NOFS); |
| 611 | |
| 612 | if (!name) |
| 613 | return -ENOMEM; |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 614 | kfree(pi->name); |
Xi Wang | ad3b904 | 2012-06-06 19:35:55 -0500 | [diff] [blame] | 615 | pi->name = name; |
| 616 | dout(" name is %s\n", pi->name); |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 617 | } |
| 618 | *p += len; |
| 619 | } |
| 620 | return 0; |
| 621 | |
| 622 | bad: |
| 623 | return -EINVAL; |
| 624 | } |
| 625 | |
| 626 | /* |
| 627 | * osd map |
| 628 | */ |
| 629 | void ceph_osdmap_destroy(struct ceph_osdmap *map) |
| 630 | { |
| 631 | dout("osdmap_destroy %p\n", map); |
| 632 | if (map->crush) |
| 633 | crush_destroy(map->crush); |
| 634 | while (!RB_EMPTY_ROOT(&map->pg_temp)) { |
| 635 | struct ceph_pg_mapping *pg = |
| 636 | rb_entry(rb_first(&map->pg_temp), |
| 637 | struct ceph_pg_mapping, node); |
| 638 | rb_erase(&pg->node, &map->pg_temp); |
| 639 | kfree(pg); |
| 640 | } |
Ilya Dryomov | 9686f94 | 2014-03-21 19:05:29 +0200 | [diff] [blame] | 641 | while (!RB_EMPTY_ROOT(&map->primary_temp)) { |
| 642 | struct ceph_pg_mapping *pg = |
| 643 | rb_entry(rb_first(&map->primary_temp), |
| 644 | struct ceph_pg_mapping, node); |
| 645 | rb_erase(&pg->node, &map->primary_temp); |
| 646 | kfree(pg); |
| 647 | } |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 648 | while (!RB_EMPTY_ROOT(&map->pg_pools)) { |
| 649 | struct ceph_pg_pool_info *pi = |
| 650 | rb_entry(rb_first(&map->pg_pools), |
| 651 | struct ceph_pg_pool_info, node); |
| 652 | __remove_pg_pool(&map->pg_pools, pi); |
| 653 | } |
| 654 | kfree(map->osd_state); |
| 655 | kfree(map->osd_weight); |
| 656 | kfree(map->osd_addr); |
Ilya Dryomov | 2cfa34f | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 657 | kfree(map->osd_primary_affinity); |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 658 | kfree(map); |
| 659 | } |
| 660 | |
| 661 | /* |
Ilya Dryomov | 4d60351 | 2014-03-21 19:05:28 +0200 | [diff] [blame] | 662 | * Adjust max_osd value, (re)allocate arrays. |
| 663 | * |
| 664 | * The new elements are properly initialized. |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 665 | */ |
| 666 | static int osdmap_set_max_osd(struct ceph_osdmap *map, int max) |
| 667 | { |
| 668 | u8 *state; |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 669 | u32 *weight; |
Ilya Dryomov | 4d60351 | 2014-03-21 19:05:28 +0200 | [diff] [blame] | 670 | struct ceph_entity_addr *addr; |
| 671 | int i; |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 672 | |
Ilya Dryomov | 4d60351 | 2014-03-21 19:05:28 +0200 | [diff] [blame] | 673 | state = krealloc(map->osd_state, max*sizeof(*state), GFP_NOFS); |
Li RongQing | 589506f | 2014-09-07 18:10:51 +0800 | [diff] [blame] | 674 | if (!state) |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 675 | return -ENOMEM; |
Li RongQing | 589506f | 2014-09-07 18:10:51 +0800 | [diff] [blame] | 676 | map->osd_state = state; |
| 677 | |
| 678 | weight = krealloc(map->osd_weight, max*sizeof(*weight), GFP_NOFS); |
| 679 | if (!weight) |
| 680 | return -ENOMEM; |
| 681 | map->osd_weight = weight; |
| 682 | |
| 683 | addr = krealloc(map->osd_addr, max*sizeof(*addr), GFP_NOFS); |
| 684 | if (!addr) |
| 685 | return -ENOMEM; |
| 686 | map->osd_addr = addr; |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 687 | |
Ilya Dryomov | 4d60351 | 2014-03-21 19:05:28 +0200 | [diff] [blame] | 688 | for (i = map->max_osd; i < max; i++) { |
Li RongQing | 589506f | 2014-09-07 18:10:51 +0800 | [diff] [blame] | 689 | map->osd_state[i] = 0; |
| 690 | map->osd_weight[i] = CEPH_OSD_OUT; |
| 691 | memset(map->osd_addr + i, 0, sizeof(*map->osd_addr)); |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 692 | } |
| 693 | |
Ilya Dryomov | 2cfa34f | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 694 | if (map->osd_primary_affinity) { |
| 695 | u32 *affinity; |
| 696 | |
| 697 | affinity = krealloc(map->osd_primary_affinity, |
| 698 | max*sizeof(*affinity), GFP_NOFS); |
| 699 | if (!affinity) |
| 700 | return -ENOMEM; |
Li RongQing | 589506f | 2014-09-07 18:10:51 +0800 | [diff] [blame] | 701 | map->osd_primary_affinity = affinity; |
Ilya Dryomov | 2cfa34f | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 702 | |
| 703 | for (i = map->max_osd; i < max; i++) |
Li RongQing | 589506f | 2014-09-07 18:10:51 +0800 | [diff] [blame] | 704 | map->osd_primary_affinity[i] = |
| 705 | CEPH_OSD_DEFAULT_PRIMARY_AFFINITY; |
Ilya Dryomov | 2cfa34f | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 706 | } |
| 707 | |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 708 | map->max_osd = max; |
Ilya Dryomov | 4d60351 | 2014-03-21 19:05:28 +0200 | [diff] [blame] | 709 | |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 710 | return 0; |
| 711 | } |
| 712 | |
Ilya Dryomov | ec7af97 | 2014-03-21 19:05:29 +0200 | [diff] [blame] | 713 | #define OSDMAP_WRAPPER_COMPAT_VER 7 |
| 714 | #define OSDMAP_CLIENT_DATA_COMPAT_VER 1 |
| 715 | |
| 716 | /* |
| 717 | * Return 0 or error. On success, *v is set to 0 for old (v6) osdmaps, |
| 718 | * to struct_v of the client_data section for new (v7 and above) |
| 719 | * osdmaps. |
| 720 | */ |
| 721 | static int get_osdmap_client_data_v(void **p, void *end, |
| 722 | const char *prefix, u8 *v) |
| 723 | { |
| 724 | u8 struct_v; |
| 725 | |
| 726 | ceph_decode_8_safe(p, end, struct_v, e_inval); |
| 727 | if (struct_v >= 7) { |
| 728 | u8 struct_compat; |
| 729 | |
| 730 | ceph_decode_8_safe(p, end, struct_compat, e_inval); |
| 731 | if (struct_compat > OSDMAP_WRAPPER_COMPAT_VER) { |
Joe Perches | b9a6789 | 2014-09-09 21:17:29 -0700 | [diff] [blame] | 732 | pr_warn("got v %d cv %d > %d of %s ceph_osdmap\n", |
| 733 | struct_v, struct_compat, |
| 734 | OSDMAP_WRAPPER_COMPAT_VER, prefix); |
Ilya Dryomov | ec7af97 | 2014-03-21 19:05:29 +0200 | [diff] [blame] | 735 | return -EINVAL; |
| 736 | } |
| 737 | *p += 4; /* ignore wrapper struct_len */ |
| 738 | |
| 739 | ceph_decode_8_safe(p, end, struct_v, e_inval); |
| 740 | ceph_decode_8_safe(p, end, struct_compat, e_inval); |
| 741 | if (struct_compat > OSDMAP_CLIENT_DATA_COMPAT_VER) { |
Joe Perches | b9a6789 | 2014-09-09 21:17:29 -0700 | [diff] [blame] | 742 | pr_warn("got v %d cv %d > %d of %s ceph_osdmap client data\n", |
| 743 | struct_v, struct_compat, |
| 744 | OSDMAP_CLIENT_DATA_COMPAT_VER, prefix); |
Ilya Dryomov | ec7af97 | 2014-03-21 19:05:29 +0200 | [diff] [blame] | 745 | return -EINVAL; |
| 746 | } |
| 747 | *p += 4; /* ignore client data struct_len */ |
| 748 | } else { |
| 749 | u16 version; |
| 750 | |
| 751 | *p -= 1; |
| 752 | ceph_decode_16_safe(p, end, version, e_inval); |
| 753 | if (version < 6) { |
Joe Perches | b9a6789 | 2014-09-09 21:17:29 -0700 | [diff] [blame] | 754 | pr_warn("got v %d < 6 of %s ceph_osdmap\n", |
| 755 | version, prefix); |
Ilya Dryomov | ec7af97 | 2014-03-21 19:05:29 +0200 | [diff] [blame] | 756 | return -EINVAL; |
| 757 | } |
| 758 | |
| 759 | /* old osdmap enconding */ |
| 760 | struct_v = 0; |
| 761 | } |
| 762 | |
| 763 | *v = struct_v; |
| 764 | return 0; |
| 765 | |
| 766 | e_inval: |
| 767 | return -EINVAL; |
| 768 | } |
| 769 | |
Ilya Dryomov | 433fbdd | 2014-03-21 19:05:27 +0200 | [diff] [blame] | 770 | static int __decode_pools(void **p, void *end, struct ceph_osdmap *map, |
| 771 | bool incremental) |
| 772 | { |
| 773 | u32 n; |
| 774 | |
| 775 | ceph_decode_32_safe(p, end, n, e_inval); |
| 776 | while (n--) { |
| 777 | struct ceph_pg_pool_info *pi; |
| 778 | u64 pool; |
| 779 | int ret; |
| 780 | |
| 781 | ceph_decode_64_safe(p, end, pool, e_inval); |
| 782 | |
| 783 | pi = __lookup_pg_pool(&map->pg_pools, pool); |
| 784 | if (!incremental || !pi) { |
| 785 | pi = kzalloc(sizeof(*pi), GFP_NOFS); |
| 786 | if (!pi) |
| 787 | return -ENOMEM; |
| 788 | |
| 789 | pi->id = pool; |
| 790 | |
| 791 | ret = __insert_pg_pool(&map->pg_pools, pi); |
| 792 | if (ret) { |
| 793 | kfree(pi); |
| 794 | return ret; |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | ret = decode_pool(p, end, pi); |
| 799 | if (ret) |
| 800 | return ret; |
| 801 | } |
| 802 | |
| 803 | return 0; |
| 804 | |
| 805 | e_inval: |
| 806 | return -EINVAL; |
| 807 | } |
| 808 | |
| 809 | static int decode_pools(void **p, void *end, struct ceph_osdmap *map) |
| 810 | { |
| 811 | return __decode_pools(p, end, map, false); |
| 812 | } |
| 813 | |
| 814 | static int decode_new_pools(void **p, void *end, struct ceph_osdmap *map) |
| 815 | { |
| 816 | return __decode_pools(p, end, map, true); |
| 817 | } |
| 818 | |
Ilya Dryomov | 10db634 | 2014-03-21 19:05:28 +0200 | [diff] [blame] | 819 | static int __decode_pg_temp(void **p, void *end, struct ceph_osdmap *map, |
| 820 | bool incremental) |
| 821 | { |
| 822 | u32 n; |
| 823 | |
| 824 | ceph_decode_32_safe(p, end, n, e_inval); |
| 825 | while (n--) { |
| 826 | struct ceph_pg pgid; |
| 827 | u32 len, i; |
| 828 | int ret; |
| 829 | |
| 830 | ret = ceph_decode_pgid(p, end, &pgid); |
| 831 | if (ret) |
| 832 | return ret; |
| 833 | |
| 834 | ceph_decode_32_safe(p, end, len, e_inval); |
| 835 | |
| 836 | ret = __remove_pg_mapping(&map->pg_temp, pgid); |
| 837 | BUG_ON(!incremental && ret != -ENOENT); |
| 838 | |
| 839 | if (!incremental || len > 0) { |
| 840 | struct ceph_pg_mapping *pg; |
| 841 | |
| 842 | ceph_decode_need(p, end, len*sizeof(u32), e_inval); |
| 843 | |
| 844 | if (len > (UINT_MAX - sizeof(*pg)) / sizeof(u32)) |
| 845 | return -EINVAL; |
| 846 | |
| 847 | pg = kzalloc(sizeof(*pg) + len*sizeof(u32), GFP_NOFS); |
| 848 | if (!pg) |
| 849 | return -ENOMEM; |
| 850 | |
| 851 | pg->pgid = pgid; |
Ilya Dryomov | 35a935d | 2014-03-21 19:05:29 +0200 | [diff] [blame] | 852 | pg->pg_temp.len = len; |
Ilya Dryomov | 10db634 | 2014-03-21 19:05:28 +0200 | [diff] [blame] | 853 | for (i = 0; i < len; i++) |
Ilya Dryomov | 35a935d | 2014-03-21 19:05:29 +0200 | [diff] [blame] | 854 | pg->pg_temp.osds[i] = ceph_decode_32(p); |
Ilya Dryomov | 10db634 | 2014-03-21 19:05:28 +0200 | [diff] [blame] | 855 | |
| 856 | ret = __insert_pg_mapping(pg, &map->pg_temp); |
| 857 | if (ret) { |
| 858 | kfree(pg); |
| 859 | return ret; |
| 860 | } |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | return 0; |
| 865 | |
| 866 | e_inval: |
| 867 | return -EINVAL; |
| 868 | } |
| 869 | |
| 870 | static int decode_pg_temp(void **p, void *end, struct ceph_osdmap *map) |
| 871 | { |
| 872 | return __decode_pg_temp(p, end, map, false); |
| 873 | } |
| 874 | |
| 875 | static int decode_new_pg_temp(void **p, void *end, struct ceph_osdmap *map) |
| 876 | { |
| 877 | return __decode_pg_temp(p, end, map, true); |
| 878 | } |
| 879 | |
Ilya Dryomov | d286de7 | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 880 | static int __decode_primary_temp(void **p, void *end, struct ceph_osdmap *map, |
| 881 | bool incremental) |
| 882 | { |
| 883 | u32 n; |
| 884 | |
| 885 | ceph_decode_32_safe(p, end, n, e_inval); |
| 886 | while (n--) { |
| 887 | struct ceph_pg pgid; |
| 888 | u32 osd; |
| 889 | int ret; |
| 890 | |
| 891 | ret = ceph_decode_pgid(p, end, &pgid); |
| 892 | if (ret) |
| 893 | return ret; |
| 894 | |
| 895 | ceph_decode_32_safe(p, end, osd, e_inval); |
| 896 | |
| 897 | ret = __remove_pg_mapping(&map->primary_temp, pgid); |
| 898 | BUG_ON(!incremental && ret != -ENOENT); |
| 899 | |
| 900 | if (!incremental || osd != (u32)-1) { |
| 901 | struct ceph_pg_mapping *pg; |
| 902 | |
| 903 | pg = kzalloc(sizeof(*pg), GFP_NOFS); |
| 904 | if (!pg) |
| 905 | return -ENOMEM; |
| 906 | |
| 907 | pg->pgid = pgid; |
| 908 | pg->primary_temp.osd = osd; |
| 909 | |
| 910 | ret = __insert_pg_mapping(pg, &map->primary_temp); |
| 911 | if (ret) { |
| 912 | kfree(pg); |
| 913 | return ret; |
| 914 | } |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | return 0; |
| 919 | |
| 920 | e_inval: |
| 921 | return -EINVAL; |
| 922 | } |
| 923 | |
| 924 | static int decode_primary_temp(void **p, void *end, struct ceph_osdmap *map) |
| 925 | { |
| 926 | return __decode_primary_temp(p, end, map, false); |
| 927 | } |
| 928 | |
| 929 | static int decode_new_primary_temp(void **p, void *end, |
| 930 | struct ceph_osdmap *map) |
| 931 | { |
| 932 | return __decode_primary_temp(p, end, map, true); |
| 933 | } |
| 934 | |
Ilya Dryomov | 2cfa34f | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 935 | u32 ceph_get_primary_affinity(struct ceph_osdmap *map, int osd) |
| 936 | { |
| 937 | BUG_ON(osd >= map->max_osd); |
| 938 | |
| 939 | if (!map->osd_primary_affinity) |
| 940 | return CEPH_OSD_DEFAULT_PRIMARY_AFFINITY; |
| 941 | |
| 942 | return map->osd_primary_affinity[osd]; |
| 943 | } |
| 944 | |
| 945 | static int set_primary_affinity(struct ceph_osdmap *map, int osd, u32 aff) |
| 946 | { |
| 947 | BUG_ON(osd >= map->max_osd); |
| 948 | |
| 949 | if (!map->osd_primary_affinity) { |
| 950 | int i; |
| 951 | |
| 952 | map->osd_primary_affinity = kmalloc(map->max_osd*sizeof(u32), |
| 953 | GFP_NOFS); |
| 954 | if (!map->osd_primary_affinity) |
| 955 | return -ENOMEM; |
| 956 | |
| 957 | for (i = 0; i < map->max_osd; i++) |
| 958 | map->osd_primary_affinity[i] = |
| 959 | CEPH_OSD_DEFAULT_PRIMARY_AFFINITY; |
| 960 | } |
| 961 | |
| 962 | map->osd_primary_affinity[osd] = aff; |
| 963 | |
| 964 | return 0; |
| 965 | } |
| 966 | |
Ilya Dryomov | 63a6993 | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 967 | static int decode_primary_affinity(void **p, void *end, |
| 968 | struct ceph_osdmap *map) |
| 969 | { |
| 970 | u32 len, i; |
| 971 | |
| 972 | ceph_decode_32_safe(p, end, len, e_inval); |
| 973 | if (len == 0) { |
| 974 | kfree(map->osd_primary_affinity); |
| 975 | map->osd_primary_affinity = NULL; |
| 976 | return 0; |
| 977 | } |
| 978 | if (len != map->max_osd) |
| 979 | goto e_inval; |
| 980 | |
| 981 | ceph_decode_need(p, end, map->max_osd*sizeof(u32), e_inval); |
| 982 | |
| 983 | for (i = 0; i < map->max_osd; i++) { |
| 984 | int ret; |
| 985 | |
| 986 | ret = set_primary_affinity(map, i, ceph_decode_32(p)); |
| 987 | if (ret) |
| 988 | return ret; |
| 989 | } |
| 990 | |
| 991 | return 0; |
| 992 | |
| 993 | e_inval: |
| 994 | return -EINVAL; |
| 995 | } |
| 996 | |
| 997 | static int decode_new_primary_affinity(void **p, void *end, |
| 998 | struct ceph_osdmap *map) |
| 999 | { |
| 1000 | u32 n; |
| 1001 | |
| 1002 | ceph_decode_32_safe(p, end, n, e_inval); |
| 1003 | while (n--) { |
| 1004 | u32 osd, aff; |
| 1005 | int ret; |
| 1006 | |
| 1007 | ceph_decode_32_safe(p, end, osd, e_inval); |
| 1008 | ceph_decode_32_safe(p, end, aff, e_inval); |
| 1009 | |
| 1010 | ret = set_primary_affinity(map, osd, aff); |
| 1011 | if (ret) |
| 1012 | return ret; |
Ilya Dryomov | f31da0f | 2014-04-02 20:34:04 +0400 | [diff] [blame] | 1013 | |
| 1014 | pr_info("osd%d primary-affinity 0x%x\n", osd, aff); |
Ilya Dryomov | 63a6993 | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | return 0; |
| 1018 | |
| 1019 | e_inval: |
| 1020 | return -EINVAL; |
| 1021 | } |
| 1022 | |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 1023 | /* |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1024 | * decode a full map. |
| 1025 | */ |
Ilya Dryomov | a2505d6 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1026 | static int osdmap_decode(void **p, void *end, struct ceph_osdmap *map) |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1027 | { |
Ilya Dryomov | ec7af97 | 2014-03-21 19:05:29 +0200 | [diff] [blame] | 1028 | u8 struct_v; |
Ilya Dryomov | 38a8d56 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1029 | u32 epoch = 0; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1030 | void *start = *p; |
Ilya Dryomov | 3977058 | 2014-03-13 16:36:14 +0200 | [diff] [blame] | 1031 | u32 max; |
| 1032 | u32 len, i; |
Ilya Dryomov | 597b52f | 2014-03-13 16:36:14 +0200 | [diff] [blame] | 1033 | int err; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1034 | |
Ilya Dryomov | a2505d6 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1035 | dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p)); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1036 | |
Ilya Dryomov | ec7af97 | 2014-03-21 19:05:29 +0200 | [diff] [blame] | 1037 | err = get_osdmap_client_data_v(p, end, "full", &struct_v); |
| 1038 | if (err) |
| 1039 | goto bad; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1040 | |
Ilya Dryomov | 53bbaba | 2014-03-13 16:36:16 +0200 | [diff] [blame] | 1041 | /* fsid, epoch, created, modified */ |
| 1042 | ceph_decode_need(p, end, sizeof(map->fsid) + sizeof(u32) + |
| 1043 | sizeof(map->created) + sizeof(map->modified), e_inval); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1044 | ceph_decode_copy(p, &map->fsid, sizeof(map->fsid)); |
Ilya Dryomov | 38a8d56 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1045 | epoch = map->epoch = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1046 | ceph_decode_copy(p, &map->created, sizeof(map->created)); |
| 1047 | ceph_decode_copy(p, &map->modified, sizeof(map->modified)); |
| 1048 | |
Ilya Dryomov | 433fbdd | 2014-03-21 19:05:27 +0200 | [diff] [blame] | 1049 | /* pools */ |
| 1050 | err = decode_pools(p, end, map); |
| 1051 | if (err) |
| 1052 | goto bad; |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 1053 | |
Ilya Dryomov | 0f70c7e | 2014-03-21 19:05:27 +0200 | [diff] [blame] | 1054 | /* pool_name */ |
| 1055 | err = decode_pool_names(p, end, map); |
Ilya Dryomov | 597b52f | 2014-03-13 16:36:14 +0200 | [diff] [blame] | 1056 | if (err) |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 1057 | goto bad; |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 1058 | |
Ilya Dryomov | 597b52f | 2014-03-13 16:36:14 +0200 | [diff] [blame] | 1059 | ceph_decode_32_safe(p, end, map->pool_max, e_inval); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1060 | |
Ilya Dryomov | 597b52f | 2014-03-13 16:36:14 +0200 | [diff] [blame] | 1061 | ceph_decode_32_safe(p, end, map->flags, e_inval); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1062 | |
Ilya Dryomov | 3977058 | 2014-03-13 16:36:14 +0200 | [diff] [blame] | 1063 | /* max_osd */ |
| 1064 | ceph_decode_32_safe(p, end, max, e_inval); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1065 | |
| 1066 | /* (re)alloc osd arrays */ |
| 1067 | err = osdmap_set_max_osd(map, max); |
Ilya Dryomov | 597b52f | 2014-03-13 16:36:14 +0200 | [diff] [blame] | 1068 | if (err) |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1069 | goto bad; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1070 | |
Ilya Dryomov | 2d88b2e | 2014-03-13 16:36:14 +0200 | [diff] [blame] | 1071 | /* osd_state, osd_weight, osd_addrs->client_addr */ |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1072 | ceph_decode_need(p, end, 3*sizeof(u32) + |
| 1073 | map->max_osd*(1 + sizeof(*map->osd_weight) + |
Ilya Dryomov | 597b52f | 2014-03-13 16:36:14 +0200 | [diff] [blame] | 1074 | sizeof(*map->osd_addr)), e_inval); |
| 1075 | |
Ilya Dryomov | 2d88b2e | 2014-03-13 16:36:14 +0200 | [diff] [blame] | 1076 | if (ceph_decode_32(p) != map->max_osd) |
| 1077 | goto e_inval; |
| 1078 | |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1079 | ceph_decode_copy(p, map->osd_state, map->max_osd); |
| 1080 | |
Ilya Dryomov | 2d88b2e | 2014-03-13 16:36:14 +0200 | [diff] [blame] | 1081 | if (ceph_decode_32(p) != map->max_osd) |
| 1082 | goto e_inval; |
| 1083 | |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1084 | for (i = 0; i < map->max_osd; i++) |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 1085 | map->osd_weight[i] = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1086 | |
Ilya Dryomov | 2d88b2e | 2014-03-13 16:36:14 +0200 | [diff] [blame] | 1087 | if (ceph_decode_32(p) != map->max_osd) |
| 1088 | goto e_inval; |
| 1089 | |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1090 | 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] | 1091 | for (i = 0; i < map->max_osd; i++) |
| 1092 | ceph_decode_addr(&map->osd_addr[i]); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1093 | |
| 1094 | /* pg_temp */ |
Ilya Dryomov | 10db634 | 2014-03-21 19:05:28 +0200 | [diff] [blame] | 1095 | err = decode_pg_temp(p, end, map); |
| 1096 | if (err) |
| 1097 | goto bad; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1098 | |
Ilya Dryomov | d286de7 | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 1099 | /* primary_temp */ |
| 1100 | if (struct_v >= 1) { |
| 1101 | err = decode_primary_temp(p, end, map); |
| 1102 | if (err) |
| 1103 | goto bad; |
| 1104 | } |
| 1105 | |
Ilya Dryomov | 63a6993 | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 1106 | /* primary_affinity */ |
| 1107 | if (struct_v >= 2) { |
| 1108 | err = decode_primary_affinity(p, end, map); |
| 1109 | if (err) |
| 1110 | goto bad; |
| 1111 | } else { |
| 1112 | /* XXX can this happen? */ |
| 1113 | kfree(map->osd_primary_affinity); |
| 1114 | map->osd_primary_affinity = NULL; |
| 1115 | } |
| 1116 | |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1117 | /* crush */ |
Ilya Dryomov | 597b52f | 2014-03-13 16:36:14 +0200 | [diff] [blame] | 1118 | ceph_decode_32_safe(p, end, len, e_inval); |
Ilya Dryomov | 9902e68 | 2014-03-13 16:36:15 +0200 | [diff] [blame] | 1119 | map->crush = crush_decode(*p, min(*p + len, end)); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1120 | if (IS_ERR(map->crush)) { |
| 1121 | err = PTR_ERR(map->crush); |
| 1122 | map->crush = NULL; |
| 1123 | goto bad; |
| 1124 | } |
Ilya Dryomov | 9902e68 | 2014-03-13 16:36:15 +0200 | [diff] [blame] | 1125 | *p += len; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1126 | |
Ilya Dryomov | 38a8d56 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1127 | /* ignore the rest */ |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1128 | *p = end; |
| 1129 | |
Ilya Dryomov | 38a8d56 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1130 | dout("full osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd); |
Ilya Dryomov | a2505d6 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1131 | return 0; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1132 | |
Ilya Dryomov | 597b52f | 2014-03-13 16:36:14 +0200 | [diff] [blame] | 1133 | e_inval: |
| 1134 | err = -EINVAL; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1135 | bad: |
Ilya Dryomov | 38a8d56 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1136 | pr_err("corrupt full osdmap (%d) epoch %d off %d (%p of %p-%p)\n", |
| 1137 | err, epoch, (int)(*p - start), *p, start, end); |
| 1138 | print_hex_dump(KERN_DEBUG, "osdmap: ", |
| 1139 | DUMP_PREFIX_OFFSET, 16, 1, |
| 1140 | start, end - start, true); |
Ilya Dryomov | a2505d6 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1141 | return err; |
| 1142 | } |
| 1143 | |
| 1144 | /* |
| 1145 | * Allocate and decode a full map. |
| 1146 | */ |
| 1147 | struct ceph_osdmap *ceph_osdmap_decode(void **p, void *end) |
| 1148 | { |
| 1149 | struct ceph_osdmap *map; |
| 1150 | int ret; |
| 1151 | |
| 1152 | map = kzalloc(sizeof(*map), GFP_NOFS); |
| 1153 | if (!map) |
| 1154 | return ERR_PTR(-ENOMEM); |
| 1155 | |
| 1156 | map->pg_temp = RB_ROOT; |
Ilya Dryomov | 9686f94 | 2014-03-21 19:05:29 +0200 | [diff] [blame] | 1157 | map->primary_temp = RB_ROOT; |
Ilya Dryomov | a2505d6 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1158 | mutex_init(&map->crush_scratch_mutex); |
| 1159 | |
| 1160 | ret = osdmap_decode(p, end, map); |
| 1161 | if (ret) { |
| 1162 | ceph_osdmap_destroy(map); |
| 1163 | return ERR_PTR(ret); |
| 1164 | } |
| 1165 | |
| 1166 | return map; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | /* |
| 1170 | * decode and apply an incremental map update. |
| 1171 | */ |
| 1172 | struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end, |
| 1173 | struct ceph_osdmap *map, |
| 1174 | struct ceph_messenger *msgr) |
| 1175 | { |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1176 | struct crush_map *newcrush = NULL; |
| 1177 | struct ceph_fsid fsid; |
| 1178 | u32 epoch = 0; |
| 1179 | struct ceph_timespec modified; |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 1180 | s32 len; |
| 1181 | u64 pool; |
| 1182 | __s64 new_pool_max; |
| 1183 | __s32 new_flags, max; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1184 | void *start = *p; |
Ilya Dryomov | 86f1742 | 2014-03-13 16:36:15 +0200 | [diff] [blame] | 1185 | int err; |
Ilya Dryomov | ec7af97 | 2014-03-21 19:05:29 +0200 | [diff] [blame] | 1186 | u8 struct_v; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1187 | |
Ilya Dryomov | 38a8d56 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1188 | dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p)); |
| 1189 | |
Ilya Dryomov | ec7af97 | 2014-03-21 19:05:29 +0200 | [diff] [blame] | 1190 | err = get_osdmap_client_data_v(p, end, "inc", &struct_v); |
| 1191 | if (err) |
| 1192 | goto bad; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1193 | |
Ilya Dryomov | 53bbaba | 2014-03-13 16:36:16 +0200 | [diff] [blame] | 1194 | /* fsid, epoch, modified, new_pool_max, new_flags */ |
| 1195 | ceph_decode_need(p, end, sizeof(fsid) + sizeof(u32) + sizeof(modified) + |
| 1196 | sizeof(u64) + sizeof(u32), e_inval); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1197 | ceph_decode_copy(p, &fsid, sizeof(fsid)); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 1198 | epoch = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1199 | BUG_ON(epoch != map->epoch+1); |
| 1200 | ceph_decode_copy(p, &modified, sizeof(modified)); |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 1201 | new_pool_max = ceph_decode_64(p); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 1202 | new_flags = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1203 | |
| 1204 | /* full map? */ |
Ilya Dryomov | 86f1742 | 2014-03-13 16:36:15 +0200 | [diff] [blame] | 1205 | ceph_decode_32_safe(p, end, len, e_inval); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1206 | if (len > 0) { |
| 1207 | dout("apply_incremental full map len %d, %p to %p\n", |
| 1208 | len, *p, end); |
Ilya Dryomov | a2505d6 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1209 | return ceph_osdmap_decode(p, min(*p+len, end)); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1210 | } |
| 1211 | |
| 1212 | /* new crush? */ |
Ilya Dryomov | 86f1742 | 2014-03-13 16:36:15 +0200 | [diff] [blame] | 1213 | ceph_decode_32_safe(p, end, len, e_inval); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1214 | if (len > 0) { |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1215 | newcrush = crush_decode(*p, min(*p+len, end)); |
Ilya Dryomov | 86f1742 | 2014-03-13 16:36:15 +0200 | [diff] [blame] | 1216 | if (IS_ERR(newcrush)) { |
| 1217 | err = PTR_ERR(newcrush); |
| 1218 | newcrush = NULL; |
| 1219 | goto bad; |
| 1220 | } |
Sage Weil | cebc5be | 2010-06-17 10:22:48 -0700 | [diff] [blame] | 1221 | *p += len; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | /* new flags? */ |
| 1225 | if (new_flags >= 0) |
| 1226 | map->flags = new_flags; |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 1227 | if (new_pool_max >= 0) |
| 1228 | map->pool_max = new_pool_max; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1229 | |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1230 | /* new max? */ |
Ilya Dryomov | 53bbaba | 2014-03-13 16:36:16 +0200 | [diff] [blame] | 1231 | ceph_decode_32_safe(p, end, max, e_inval); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1232 | if (max >= 0) { |
| 1233 | err = osdmap_set_max_osd(map, max); |
Ilya Dryomov | 86f1742 | 2014-03-13 16:36:15 +0200 | [diff] [blame] | 1234 | if (err) |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1235 | goto bad; |
| 1236 | } |
| 1237 | |
| 1238 | map->epoch++; |
Sage Weil | 3145666 | 2011-05-12 15:18:43 -0700 | [diff] [blame] | 1239 | map->modified = modified; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1240 | if (newcrush) { |
| 1241 | if (map->crush) |
| 1242 | crush_destroy(map->crush); |
| 1243 | map->crush = newcrush; |
| 1244 | newcrush = NULL; |
| 1245 | } |
| 1246 | |
Ilya Dryomov | 433fbdd | 2014-03-21 19:05:27 +0200 | [diff] [blame] | 1247 | /* new_pools */ |
| 1248 | err = decode_new_pools(p, end, map); |
| 1249 | if (err) |
| 1250 | goto bad; |
Ilya Dryomov | 9464d00 | 2014-03-13 16:36:16 +0200 | [diff] [blame] | 1251 | |
Ilya Dryomov | 0f70c7e | 2014-03-21 19:05:27 +0200 | [diff] [blame] | 1252 | /* new_pool_names */ |
| 1253 | err = decode_pool_names(p, end, map); |
Ilya Dryomov | 9464d00 | 2014-03-13 16:36:16 +0200 | [diff] [blame] | 1254 | if (err) |
| 1255 | goto bad; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1256 | |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 1257 | /* old_pool */ |
Ilya Dryomov | 86f1742 | 2014-03-13 16:36:15 +0200 | [diff] [blame] | 1258 | ceph_decode_32_safe(p, end, len, e_inval); |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 1259 | while (len--) { |
| 1260 | struct ceph_pg_pool_info *pi; |
| 1261 | |
Ilya Dryomov | 86f1742 | 2014-03-13 16:36:15 +0200 | [diff] [blame] | 1262 | ceph_decode_64_safe(p, end, pool, e_inval); |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 1263 | pi = __lookup_pg_pool(&map->pg_pools, pool); |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 1264 | if (pi) |
| 1265 | __remove_pg_pool(&map->pg_pools, pi); |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 1266 | } |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1267 | |
| 1268 | /* new_up */ |
Ilya Dryomov | 86f1742 | 2014-03-13 16:36:15 +0200 | [diff] [blame] | 1269 | ceph_decode_32_safe(p, end, len, e_inval); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1270 | while (len--) { |
| 1271 | u32 osd; |
| 1272 | struct ceph_entity_addr addr; |
Ilya Dryomov | 86f1742 | 2014-03-13 16:36:15 +0200 | [diff] [blame] | 1273 | ceph_decode_32_safe(p, end, osd, e_inval); |
| 1274 | ceph_decode_copy_safe(p, end, &addr, sizeof(addr), e_inval); |
Sage Weil | 63f2d21 | 2009-11-03 15:17:56 -0800 | [diff] [blame] | 1275 | ceph_decode_addr(&addr); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1276 | pr_info("osd%d up\n", osd); |
| 1277 | BUG_ON(osd >= map->max_osd); |
| 1278 | map->osd_state[osd] |= CEPH_OSD_UP; |
| 1279 | map->osd_addr[osd] = addr; |
| 1280 | } |
| 1281 | |
Sage Weil | 7662d8f | 2011-05-03 12:52:05 -0700 | [diff] [blame] | 1282 | /* new_state */ |
Ilya Dryomov | 86f1742 | 2014-03-13 16:36:15 +0200 | [diff] [blame] | 1283 | ceph_decode_32_safe(p, end, len, e_inval); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1284 | while (len--) { |
| 1285 | u32 osd; |
Sage Weil | 7662d8f | 2011-05-03 12:52:05 -0700 | [diff] [blame] | 1286 | u8 xorstate; |
Ilya Dryomov | 86f1742 | 2014-03-13 16:36:15 +0200 | [diff] [blame] | 1287 | ceph_decode_32_safe(p, end, osd, e_inval); |
Sage Weil | 7662d8f | 2011-05-03 12:52:05 -0700 | [diff] [blame] | 1288 | xorstate = **(u8 **)p; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1289 | (*p)++; /* clean flag */ |
Sage Weil | 7662d8f | 2011-05-03 12:52:05 -0700 | [diff] [blame] | 1290 | if (xorstate == 0) |
| 1291 | xorstate = CEPH_OSD_UP; |
| 1292 | if (xorstate & CEPH_OSD_UP) |
| 1293 | pr_info("osd%d down\n", osd); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1294 | if (osd < map->max_osd) |
Sage Weil | 7662d8f | 2011-05-03 12:52:05 -0700 | [diff] [blame] | 1295 | map->osd_state[osd] ^= xorstate; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | /* new_weight */ |
Ilya Dryomov | 86f1742 | 2014-03-13 16:36:15 +0200 | [diff] [blame] | 1299 | ceph_decode_32_safe(p, end, len, e_inval); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1300 | while (len--) { |
| 1301 | u32 osd, off; |
Ilya Dryomov | 86f1742 | 2014-03-13 16:36:15 +0200 | [diff] [blame] | 1302 | ceph_decode_need(p, end, sizeof(u32)*2, e_inval); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 1303 | osd = ceph_decode_32(p); |
| 1304 | off = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1305 | pr_info("osd%d weight 0x%x %s\n", osd, off, |
| 1306 | off == CEPH_OSD_IN ? "(in)" : |
| 1307 | (off == CEPH_OSD_OUT ? "(out)" : "")); |
| 1308 | if (osd < map->max_osd) |
| 1309 | map->osd_weight[osd] = off; |
| 1310 | } |
| 1311 | |
| 1312 | /* new_pg_temp */ |
Ilya Dryomov | 10db634 | 2014-03-21 19:05:28 +0200 | [diff] [blame] | 1313 | err = decode_new_pg_temp(p, end, map); |
| 1314 | if (err) |
| 1315 | goto bad; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1316 | |
Ilya Dryomov | d286de7 | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 1317 | /* new_primary_temp */ |
| 1318 | if (struct_v >= 1) { |
| 1319 | err = decode_new_primary_temp(p, end, map); |
| 1320 | if (err) |
| 1321 | goto bad; |
| 1322 | } |
| 1323 | |
Ilya Dryomov | 63a6993 | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 1324 | /* new_primary_affinity */ |
| 1325 | if (struct_v >= 2) { |
| 1326 | err = decode_new_primary_affinity(p, end, map); |
| 1327 | if (err) |
| 1328 | goto bad; |
| 1329 | } |
| 1330 | |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1331 | /* ignore the rest */ |
| 1332 | *p = end; |
Ilya Dryomov | 38a8d56 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1333 | |
| 1334 | dout("inc osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1335 | return map; |
| 1336 | |
Ilya Dryomov | 86f1742 | 2014-03-13 16:36:15 +0200 | [diff] [blame] | 1337 | e_inval: |
| 1338 | err = -EINVAL; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1339 | bad: |
Ilya Dryomov | 38a8d56 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1340 | pr_err("corrupt inc osdmap (%d) epoch %d off %d (%p of %p-%p)\n", |
| 1341 | err, epoch, (int)(*p - start), *p, start, end); |
Sage Weil | 9ec7cab | 2009-12-14 15:13:47 -0800 | [diff] [blame] | 1342 | print_hex_dump(KERN_DEBUG, "osdmap: ", |
| 1343 | DUMP_PREFIX_OFFSET, 16, 1, |
| 1344 | start, end - start, true); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1345 | if (newcrush) |
| 1346 | crush_destroy(newcrush); |
| 1347 | return ERR_PTR(err); |
| 1348 | } |
| 1349 | |
| 1350 | |
| 1351 | |
| 1352 | |
| 1353 | /* |
| 1354 | * calculate file layout from given offset, length. |
| 1355 | * fill in correct oid, logical length, and object extent |
| 1356 | * offset, length. |
| 1357 | * |
| 1358 | * for now, we write only a single su, until we can |
| 1359 | * pass a stride back to the caller. |
| 1360 | */ |
Sage Weil | d63b77f | 2012-09-24 20:59:48 -0700 | [diff] [blame] | 1361 | int ceph_calc_file_object_mapping(struct ceph_file_layout *layout, |
Alex Elder | e8afad6 | 2012-11-14 09:38:19 -0600 | [diff] [blame] | 1362 | u64 off, u64 len, |
Sage Weil | 645a102 | 2009-10-28 15:15:05 -0700 | [diff] [blame] | 1363 | u64 *ono, |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1364 | u64 *oxoff, u64 *oxlen) |
| 1365 | { |
| 1366 | u32 osize = le32_to_cpu(layout->fl_object_size); |
| 1367 | u32 su = le32_to_cpu(layout->fl_stripe_unit); |
| 1368 | u32 sc = le32_to_cpu(layout->fl_stripe_count); |
| 1369 | u32 bl, stripeno, stripepos, objsetno; |
| 1370 | u32 su_per_object; |
Noah Watkins | ff1d1f7 | 2009-10-30 12:57:30 -0700 | [diff] [blame] | 1371 | u64 t, su_offset; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1372 | |
Alex Elder | e8afad6 | 2012-11-14 09:38:19 -0600 | [diff] [blame] | 1373 | dout("mapping %llu~%llu osize %u fl_su %u\n", off, len, |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1374 | osize, su); |
Sage Weil | d63b77f | 2012-09-24 20:59:48 -0700 | [diff] [blame] | 1375 | if (su == 0 || sc == 0) |
| 1376 | goto invalid; |
Noah Watkins | 35e054a | 2009-10-28 14:04:48 -0700 | [diff] [blame] | 1377 | su_per_object = osize / su; |
Sage Weil | d63b77f | 2012-09-24 20:59:48 -0700 | [diff] [blame] | 1378 | if (su_per_object == 0) |
| 1379 | goto invalid; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1380 | dout("osize %u / su %u = su_per_object %u\n", osize, su, |
| 1381 | su_per_object); |
| 1382 | |
Sage Weil | d63b77f | 2012-09-24 20:59:48 -0700 | [diff] [blame] | 1383 | if ((su & ~PAGE_MASK) != 0) |
| 1384 | goto invalid; |
| 1385 | |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1386 | /* bl = *off / su; */ |
| 1387 | t = off; |
| 1388 | do_div(t, su); |
| 1389 | bl = t; |
| 1390 | dout("off %llu / su %u = bl %u\n", off, su, bl); |
| 1391 | |
| 1392 | stripeno = bl / sc; |
| 1393 | stripepos = bl % sc; |
| 1394 | objsetno = stripeno / su_per_object; |
| 1395 | |
Sage Weil | 645a102 | 2009-10-28 15:15:05 -0700 | [diff] [blame] | 1396 | *ono = objsetno * sc + stripepos; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 1397 | 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] | 1398 | |
| 1399 | /* *oxoff = *off % layout->fl_stripe_unit; # offset in su */ |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1400 | t = off; |
Noah Watkins | ff1d1f7 | 2009-10-30 12:57:30 -0700 | [diff] [blame] | 1401 | su_offset = do_div(t, su); |
| 1402 | *oxoff = su_offset + (stripeno % su_per_object) * su; |
Sage Weil | 645a102 | 2009-10-28 15:15:05 -0700 | [diff] [blame] | 1403 | |
Noah Watkins | ff1d1f7 | 2009-10-30 12:57:30 -0700 | [diff] [blame] | 1404 | /* |
| 1405 | * Calculate the length of the extent being written to the selected |
Alex Elder | e8afad6 | 2012-11-14 09:38:19 -0600 | [diff] [blame] | 1406 | * object. This is the minimum of the full length requested (len) or |
Noah Watkins | ff1d1f7 | 2009-10-30 12:57:30 -0700 | [diff] [blame] | 1407 | * the remainder of the current stripe being written to. |
| 1408 | */ |
Alex Elder | e8afad6 | 2012-11-14 09:38:19 -0600 | [diff] [blame] | 1409 | *oxlen = min_t(u64, len, su - su_offset); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1410 | |
| 1411 | dout(" obj extent %llu~%llu\n", *oxoff, *oxlen); |
Sage Weil | d63b77f | 2012-09-24 20:59:48 -0700 | [diff] [blame] | 1412 | return 0; |
| 1413 | |
| 1414 | invalid: |
| 1415 | dout(" invalid layout\n"); |
| 1416 | *ono = 0; |
| 1417 | *oxoff = 0; |
| 1418 | *oxlen = 0; |
| 1419 | return -EINVAL; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1420 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1421 | EXPORT_SYMBOL(ceph_calc_file_object_mapping); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1422 | |
| 1423 | /* |
Ilya Dryomov | 7c13cb6 | 2014-01-27 17:40:19 +0200 | [diff] [blame] | 1424 | * Calculate mapping of a (oloc, oid) pair to a PG. Should only be |
| 1425 | * called with target's (oloc, oid), since tiering isn't taken into |
| 1426 | * account. |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1427 | */ |
Ilya Dryomov | 7c13cb6 | 2014-01-27 17:40:19 +0200 | [diff] [blame] | 1428 | int ceph_oloc_oid_to_pg(struct ceph_osdmap *osdmap, |
| 1429 | struct ceph_object_locator *oloc, |
| 1430 | struct ceph_object_id *oid, |
| 1431 | struct ceph_pg *pg_out) |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1432 | { |
Ilya Dryomov | 7c13cb6 | 2014-01-27 17:40:19 +0200 | [diff] [blame] | 1433 | struct ceph_pg_pool_info *pi; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1434 | |
Ilya Dryomov | 7c13cb6 | 2014-01-27 17:40:19 +0200 | [diff] [blame] | 1435 | pi = __lookup_pg_pool(&osdmap->pg_pools, oloc->pool); |
| 1436 | if (!pi) |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 1437 | return -EIO; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1438 | |
Ilya Dryomov | 7c13cb6 | 2014-01-27 17:40:19 +0200 | [diff] [blame] | 1439 | pg_out->pool = oloc->pool; |
| 1440 | pg_out->seed = ceph_str_hash(pi->object_hash, oid->name, |
| 1441 | oid->name_len); |
| 1442 | |
| 1443 | dout("%s '%.*s' pgid %llu.%x\n", __func__, oid->name_len, oid->name, |
| 1444 | pg_out->pool, pg_out->seed); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1445 | return 0; |
| 1446 | } |
Ilya Dryomov | 7c13cb6 | 2014-01-27 17:40:19 +0200 | [diff] [blame] | 1447 | EXPORT_SYMBOL(ceph_oloc_oid_to_pg); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1448 | |
Ilya Dryomov | 9d52147 | 2014-01-31 17:54:26 +0200 | [diff] [blame] | 1449 | static int do_crush(struct ceph_osdmap *map, int ruleno, int x, |
| 1450 | int *result, int result_max, |
| 1451 | const __u32 *weight, int weight_max) |
Ilya Dryomov | e8ef19c | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 1452 | { |
Ilya Dryomov | 9d52147 | 2014-01-31 17:54:26 +0200 | [diff] [blame] | 1453 | int r; |
Ilya Dryomov | e8ef19c | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 1454 | |
Ilya Dryomov | 9d52147 | 2014-01-31 17:54:26 +0200 | [diff] [blame] | 1455 | BUG_ON(result_max > CEPH_PG_MAX_SIZE); |
| 1456 | |
| 1457 | mutex_lock(&map->crush_scratch_mutex); |
| 1458 | r = crush_do_rule(map->crush, ruleno, x, result, result_max, |
| 1459 | weight, weight_max, map->crush_scratch_ary); |
| 1460 | mutex_unlock(&map->crush_scratch_mutex); |
| 1461 | |
| 1462 | return r; |
Ilya Dryomov | e8ef19c | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 1463 | } |
| 1464 | |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1465 | /* |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 1466 | * Calculate raw (crush) set for given pgid. |
| 1467 | * |
| 1468 | * Return raw set length, or error. |
| 1469 | */ |
| 1470 | static int pg_to_raw_osds(struct ceph_osdmap *osdmap, |
| 1471 | struct ceph_pg_pool_info *pool, |
| 1472 | struct ceph_pg pgid, u32 pps, int *osds) |
| 1473 | { |
| 1474 | int ruleno; |
| 1475 | int len; |
| 1476 | |
| 1477 | /* crush */ |
| 1478 | ruleno = crush_find_rule(osdmap->crush, pool->crush_ruleset, |
| 1479 | pool->type, pool->size); |
| 1480 | if (ruleno < 0) { |
| 1481 | pr_err("no crush rule: pool %lld ruleset %d type %d size %d\n", |
| 1482 | pgid.pool, pool->crush_ruleset, pool->type, |
| 1483 | pool->size); |
| 1484 | return -ENOENT; |
| 1485 | } |
| 1486 | |
| 1487 | len = do_crush(osdmap, ruleno, pps, osds, |
| 1488 | min_t(int, pool->size, CEPH_PG_MAX_SIZE), |
| 1489 | osdmap->osd_weight, osdmap->max_osd); |
| 1490 | if (len < 0) { |
| 1491 | pr_err("error %d from crush rule %d: pool %lld ruleset %d type %d size %d\n", |
| 1492 | len, ruleno, pgid.pool, pool->crush_ruleset, |
| 1493 | pool->type, pool->size); |
| 1494 | return len; |
| 1495 | } |
| 1496 | |
| 1497 | return len; |
| 1498 | } |
| 1499 | |
| 1500 | /* |
| 1501 | * Given raw set, calculate up set and up primary. |
| 1502 | * |
| 1503 | * Return up set length. *primary is set to up primary osd id, or -1 |
| 1504 | * if up set is empty. |
| 1505 | */ |
| 1506 | static int raw_to_up_osds(struct ceph_osdmap *osdmap, |
| 1507 | struct ceph_pg_pool_info *pool, |
| 1508 | int *osds, int len, int *primary) |
| 1509 | { |
| 1510 | int up_primary = -1; |
| 1511 | int i; |
| 1512 | |
| 1513 | if (ceph_can_shift_osds(pool)) { |
| 1514 | int removed = 0; |
| 1515 | |
| 1516 | for (i = 0; i < len; i++) { |
| 1517 | if (ceph_osd_is_down(osdmap, osds[i])) { |
| 1518 | removed++; |
| 1519 | continue; |
| 1520 | } |
| 1521 | if (removed) |
| 1522 | osds[i - removed] = osds[i]; |
| 1523 | } |
| 1524 | |
| 1525 | len -= removed; |
| 1526 | if (len > 0) |
| 1527 | up_primary = osds[0]; |
| 1528 | } else { |
| 1529 | for (i = len - 1; i >= 0; i--) { |
| 1530 | if (ceph_osd_is_down(osdmap, osds[i])) |
| 1531 | osds[i] = CRUSH_ITEM_NONE; |
| 1532 | else |
| 1533 | up_primary = osds[i]; |
| 1534 | } |
| 1535 | } |
| 1536 | |
| 1537 | *primary = up_primary; |
| 1538 | return len; |
| 1539 | } |
| 1540 | |
Ilya Dryomov | 47ec1f3 | 2014-03-24 17:12:49 +0200 | [diff] [blame] | 1541 | static void apply_primary_affinity(struct ceph_osdmap *osdmap, u32 pps, |
| 1542 | struct ceph_pg_pool_info *pool, |
| 1543 | int *osds, int len, int *primary) |
| 1544 | { |
| 1545 | int i; |
| 1546 | int pos = -1; |
| 1547 | |
| 1548 | /* |
| 1549 | * Do we have any non-default primary_affinity values for these |
| 1550 | * osds? |
| 1551 | */ |
| 1552 | if (!osdmap->osd_primary_affinity) |
| 1553 | return; |
| 1554 | |
| 1555 | for (i = 0; i < len; i++) { |
Ilya Dryomov | 92b2e75 | 2014-04-10 18:09:41 +0400 | [diff] [blame] | 1556 | int osd = osds[i]; |
| 1557 | |
| 1558 | if (osd != CRUSH_ITEM_NONE && |
| 1559 | osdmap->osd_primary_affinity[osd] != |
Ilya Dryomov | 47ec1f3 | 2014-03-24 17:12:49 +0200 | [diff] [blame] | 1560 | CEPH_OSD_DEFAULT_PRIMARY_AFFINITY) { |
| 1561 | break; |
| 1562 | } |
| 1563 | } |
| 1564 | if (i == len) |
| 1565 | return; |
| 1566 | |
| 1567 | /* |
| 1568 | * Pick the primary. Feed both the seed (for the pg) and the |
| 1569 | * osd into the hash/rng so that a proportional fraction of an |
| 1570 | * osd's pgs get rejected as primary. |
| 1571 | */ |
| 1572 | for (i = 0; i < len; i++) { |
Ilya Dryomov | 92b2e75 | 2014-04-10 18:09:41 +0400 | [diff] [blame] | 1573 | int osd = osds[i]; |
Ilya Dryomov | 47ec1f3 | 2014-03-24 17:12:49 +0200 | [diff] [blame] | 1574 | u32 aff; |
| 1575 | |
Ilya Dryomov | 47ec1f3 | 2014-03-24 17:12:49 +0200 | [diff] [blame] | 1576 | if (osd == CRUSH_ITEM_NONE) |
| 1577 | continue; |
| 1578 | |
| 1579 | aff = osdmap->osd_primary_affinity[osd]; |
| 1580 | if (aff < CEPH_OSD_MAX_PRIMARY_AFFINITY && |
| 1581 | (crush_hash32_2(CRUSH_HASH_RJENKINS1, |
| 1582 | pps, osd) >> 16) >= aff) { |
| 1583 | /* |
| 1584 | * We chose not to use this primary. Note it |
| 1585 | * anyway as a fallback in case we don't pick |
| 1586 | * anyone else, but keep looking. |
| 1587 | */ |
| 1588 | if (pos < 0) |
| 1589 | pos = i; |
| 1590 | } else { |
| 1591 | pos = i; |
| 1592 | break; |
| 1593 | } |
| 1594 | } |
| 1595 | if (pos < 0) |
| 1596 | return; |
| 1597 | |
| 1598 | *primary = osds[pos]; |
| 1599 | |
| 1600 | if (ceph_can_shift_osds(pool) && pos > 0) { |
| 1601 | /* move the new primary to the front */ |
| 1602 | for (i = pos; i > 0; i--) |
| 1603 | osds[i] = osds[i - 1]; |
| 1604 | osds[0] = *primary; |
| 1605 | } |
| 1606 | } |
| 1607 | |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 1608 | /* |
Ilya Dryomov | 5e8d4d3 | 2014-03-24 17:12:48 +0200 | [diff] [blame] | 1609 | * Given up set, apply pg_temp and primary_temp mappings. |
Ilya Dryomov | 45966c3 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 1610 | * |
| 1611 | * Return acting set length. *primary is set to acting primary osd id, |
| 1612 | * or -1 if acting set is empty. |
| 1613 | */ |
| 1614 | static int apply_temps(struct ceph_osdmap *osdmap, |
| 1615 | struct ceph_pg_pool_info *pool, struct ceph_pg pgid, |
| 1616 | int *osds, int len, int *primary) |
| 1617 | { |
| 1618 | struct ceph_pg_mapping *pg; |
| 1619 | int temp_len; |
| 1620 | int temp_primary; |
| 1621 | int i; |
| 1622 | |
| 1623 | /* raw_pg -> pg */ |
| 1624 | pgid.seed = ceph_stable_mod(pgid.seed, pool->pg_num, |
| 1625 | pool->pg_num_mask); |
| 1626 | |
| 1627 | /* pg_temp? */ |
| 1628 | pg = __lookup_pg_mapping(&osdmap->pg_temp, pgid); |
| 1629 | if (pg) { |
| 1630 | temp_len = 0; |
| 1631 | temp_primary = -1; |
| 1632 | |
| 1633 | for (i = 0; i < pg->pg_temp.len; i++) { |
| 1634 | if (ceph_osd_is_down(osdmap, pg->pg_temp.osds[i])) { |
| 1635 | if (ceph_can_shift_osds(pool)) |
| 1636 | continue; |
| 1637 | else |
| 1638 | osds[temp_len++] = CRUSH_ITEM_NONE; |
| 1639 | } else { |
| 1640 | osds[temp_len++] = pg->pg_temp.osds[i]; |
| 1641 | } |
| 1642 | } |
| 1643 | |
| 1644 | /* apply pg_temp's primary */ |
| 1645 | for (i = 0; i < temp_len; i++) { |
| 1646 | if (osds[i] != CRUSH_ITEM_NONE) { |
| 1647 | temp_primary = osds[i]; |
| 1648 | break; |
| 1649 | } |
| 1650 | } |
| 1651 | } else { |
| 1652 | temp_len = len; |
| 1653 | temp_primary = *primary; |
| 1654 | } |
| 1655 | |
Ilya Dryomov | 5e8d4d3 | 2014-03-24 17:12:48 +0200 | [diff] [blame] | 1656 | /* primary_temp? */ |
| 1657 | pg = __lookup_pg_mapping(&osdmap->primary_temp, pgid); |
| 1658 | if (pg) |
| 1659 | temp_primary = pg->primary_temp.osd; |
| 1660 | |
Ilya Dryomov | 45966c3 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 1661 | *primary = temp_primary; |
| 1662 | return temp_len; |
| 1663 | } |
| 1664 | |
| 1665 | /* |
Ilya Dryomov | ac97223 | 2014-03-24 17:12:48 +0200 | [diff] [blame] | 1666 | * Calculate acting set for given pgid. |
| 1667 | * |
Ilya Dryomov | 8008ab1 | 2014-03-24 17:12:48 +0200 | [diff] [blame] | 1668 | * Return acting set length, or error. *primary is set to acting |
| 1669 | * primary osd id, or -1 if acting set is empty or on error. |
Sage Weil | d85b705 | 2010-05-10 10:24:48 -0700 | [diff] [blame] | 1670 | */ |
Sage Weil | 5b191d9 | 2013-02-23 10:38:16 -0800 | [diff] [blame] | 1671 | int ceph_calc_pg_acting(struct ceph_osdmap *osdmap, struct ceph_pg pgid, |
Ilya Dryomov | 8008ab1 | 2014-03-24 17:12:48 +0200 | [diff] [blame] | 1672 | int *osds, int *primary) |
Sage Weil | d85b705 | 2010-05-10 10:24:48 -0700 | [diff] [blame] | 1673 | { |
Ilya Dryomov | ac97223 | 2014-03-24 17:12:48 +0200 | [diff] [blame] | 1674 | struct ceph_pg_pool_info *pool; |
| 1675 | u32 pps; |
| 1676 | int len; |
Sage Weil | d85b705 | 2010-05-10 10:24:48 -0700 | [diff] [blame] | 1677 | |
Ilya Dryomov | ac97223 | 2014-03-24 17:12:48 +0200 | [diff] [blame] | 1678 | pool = __lookup_pg_pool(&osdmap->pg_pools, pgid.pool); |
Ilya Dryomov | 8008ab1 | 2014-03-24 17:12:48 +0200 | [diff] [blame] | 1679 | if (!pool) { |
| 1680 | *primary = -1; |
| 1681 | return -ENOENT; |
| 1682 | } |
Sage Weil | d85b705 | 2010-05-10 10:24:48 -0700 | [diff] [blame] | 1683 | |
Ilya Dryomov | ac97223 | 2014-03-24 17:12:48 +0200 | [diff] [blame] | 1684 | if (pool->flags & CEPH_POOL_FLAG_HASHPSPOOL) { |
| 1685 | /* hash pool id and seed so that pool PGs do not overlap */ |
| 1686 | pps = crush_hash32_2(CRUSH_HASH_RJENKINS1, |
| 1687 | ceph_stable_mod(pgid.seed, pool->pgp_num, |
| 1688 | pool->pgp_num_mask), |
| 1689 | pgid.pool); |
| 1690 | } else { |
| 1691 | /* |
| 1692 | * legacy behavior: add ps and pool together. this is |
| 1693 | * not a great approach because the PGs from each pool |
| 1694 | * will overlap on top of each other: 0.5 == 1.4 == |
| 1695 | * 2.3 == ... |
| 1696 | */ |
| 1697 | pps = ceph_stable_mod(pgid.seed, pool->pgp_num, |
| 1698 | pool->pgp_num_mask) + |
| 1699 | (unsigned)pgid.pool; |
| 1700 | } |
| 1701 | |
| 1702 | len = pg_to_raw_osds(osdmap, pool, pgid, pps, osds); |
Ilya Dryomov | 8008ab1 | 2014-03-24 17:12:48 +0200 | [diff] [blame] | 1703 | if (len < 0) { |
| 1704 | *primary = -1; |
Ilya Dryomov | ac97223 | 2014-03-24 17:12:48 +0200 | [diff] [blame] | 1705 | return len; |
Ilya Dryomov | 8008ab1 | 2014-03-24 17:12:48 +0200 | [diff] [blame] | 1706 | } |
Ilya Dryomov | ac97223 | 2014-03-24 17:12:48 +0200 | [diff] [blame] | 1707 | |
Ilya Dryomov | 8008ab1 | 2014-03-24 17:12:48 +0200 | [diff] [blame] | 1708 | len = raw_to_up_osds(osdmap, pool, osds, len, primary); |
Ilya Dryomov | ac97223 | 2014-03-24 17:12:48 +0200 | [diff] [blame] | 1709 | |
Ilya Dryomov | 47ec1f3 | 2014-03-24 17:12:49 +0200 | [diff] [blame] | 1710 | apply_primary_affinity(osdmap, pps, pool, osds, len, primary); |
| 1711 | |
Ilya Dryomov | 8008ab1 | 2014-03-24 17:12:48 +0200 | [diff] [blame] | 1712 | len = apply_temps(osdmap, pool, pgid, osds, len, primary); |
Ilya Dryomov | ac97223 | 2014-03-24 17:12:48 +0200 | [diff] [blame] | 1713 | |
| 1714 | return len; |
Sage Weil | d85b705 | 2010-05-10 10:24:48 -0700 | [diff] [blame] | 1715 | } |
| 1716 | |
| 1717 | /* |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1718 | * Return primary osd for given pgid, or -1 if none. |
| 1719 | */ |
Sage Weil | 5b191d9 | 2013-02-23 10:38:16 -0800 | [diff] [blame] | 1720 | 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] | 1721 | { |
Ilya Dryomov | c4c1228 | 2014-03-24 17:12:49 +0200 | [diff] [blame] | 1722 | int osds[CEPH_PG_MAX_SIZE]; |
| 1723 | int primary; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1724 | |
Ilya Dryomov | c4c1228 | 2014-03-24 17:12:49 +0200 | [diff] [blame] | 1725 | ceph_calc_pg_acting(osdmap, pgid, osds, &primary); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1726 | |
Ilya Dryomov | c4c1228 | 2014-03-24 17:12:49 +0200 | [diff] [blame] | 1727 | return primary; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1728 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1729 | EXPORT_SYMBOL(ceph_calc_pg_primary); |