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); |
Ilya Dryomov | 82cd003 | 2015-06-29 19:30:23 +0300 | [diff] [blame] | 92 | ceph_decode_8_safe(p, end, b->num_nodes, bad); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 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 | |
Ilya Dryomov | 958a276 | 2015-04-14 16:54:52 +0300 | [diff] [blame] | 125 | static int crush_decode_straw2_bucket(void **p, void *end, |
| 126 | struct crush_bucket_straw2 *b) |
| 127 | { |
| 128 | int j; |
| 129 | dout("crush_decode_straw2_bucket %p to %p\n", *p, end); |
| 130 | b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS); |
| 131 | if (b->item_weights == NULL) |
| 132 | return -ENOMEM; |
| 133 | ceph_decode_need(p, end, b->h.size * sizeof(u32), bad); |
| 134 | for (j = 0; j < b->h.size; j++) |
| 135 | b->item_weights[j] = ceph_decode_32(p); |
| 136 | return 0; |
| 137 | bad: |
| 138 | return -EINVAL; |
| 139 | } |
| 140 | |
Sage Weil | 546f04e | 2012-07-30 18:15:23 -0700 | [diff] [blame] | 141 | static int skip_name_map(void **p, void *end) |
| 142 | { |
| 143 | int len; |
| 144 | ceph_decode_32_safe(p, end, len ,bad); |
| 145 | while (len--) { |
| 146 | int strlen; |
| 147 | *p += sizeof(u32); |
| 148 | ceph_decode_32_safe(p, end, strlen, bad); |
| 149 | *p += strlen; |
| 150 | } |
| 151 | return 0; |
| 152 | bad: |
| 153 | return -EINVAL; |
| 154 | } |
| 155 | |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 156 | static struct crush_map *crush_decode(void *pbyval, void *end) |
| 157 | { |
| 158 | struct crush_map *c; |
| 159 | int err = -EINVAL; |
| 160 | int i, j; |
| 161 | void **p = &pbyval; |
| 162 | void *start = pbyval; |
| 163 | u32 magic; |
Sage Weil | 546f04e | 2012-07-30 18:15:23 -0700 | [diff] [blame] | 164 | u32 num_name_maps; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 165 | |
| 166 | dout("crush_decode %p to %p len %d\n", *p, end, (int)(end - *p)); |
| 167 | |
| 168 | c = kzalloc(sizeof(*c), GFP_NOFS); |
| 169 | if (c == NULL) |
| 170 | return ERR_PTR(-ENOMEM); |
| 171 | |
Sage Weil | 546f04e | 2012-07-30 18:15:23 -0700 | [diff] [blame] | 172 | /* set tunables to default values */ |
| 173 | c->choose_local_tries = 2; |
| 174 | c->choose_local_fallback_tries = 5; |
| 175 | c->choose_total_tries = 19; |
Jim Schutt | 1604f48 | 2012-11-30 09:15:25 -0700 | [diff] [blame] | 176 | c->chooseleaf_descend_once = 0; |
Sage Weil | 546f04e | 2012-07-30 18:15:23 -0700 | [diff] [blame] | 177 | |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 178 | ceph_decode_need(p, end, 4*sizeof(u32), bad); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 179 | magic = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 180 | if (magic != CRUSH_MAGIC) { |
| 181 | pr_err("crush_decode magic %x != current %x\n", |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 182 | (unsigned int)magic, (unsigned int)CRUSH_MAGIC); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 183 | goto bad; |
| 184 | } |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 185 | c->max_buckets = ceph_decode_32(p); |
| 186 | c->max_rules = ceph_decode_32(p); |
| 187 | c->max_devices = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 188 | |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 189 | c->buckets = kcalloc(c->max_buckets, sizeof(*c->buckets), GFP_NOFS); |
| 190 | if (c->buckets == NULL) |
| 191 | goto badmem; |
| 192 | c->rules = kcalloc(c->max_rules, sizeof(*c->rules), GFP_NOFS); |
| 193 | if (c->rules == NULL) |
| 194 | goto badmem; |
| 195 | |
| 196 | /* buckets */ |
| 197 | for (i = 0; i < c->max_buckets; i++) { |
| 198 | int size = 0; |
| 199 | u32 alg; |
| 200 | struct crush_bucket *b; |
| 201 | |
| 202 | ceph_decode_32_safe(p, end, alg, bad); |
| 203 | if (alg == 0) { |
| 204 | c->buckets[i] = NULL; |
| 205 | continue; |
| 206 | } |
| 207 | dout("crush_decode bucket %d off %x %p to %p\n", |
| 208 | i, (int)(*p-start), *p, end); |
| 209 | |
| 210 | switch (alg) { |
| 211 | case CRUSH_BUCKET_UNIFORM: |
| 212 | size = sizeof(struct crush_bucket_uniform); |
| 213 | break; |
| 214 | case CRUSH_BUCKET_LIST: |
| 215 | size = sizeof(struct crush_bucket_list); |
| 216 | break; |
| 217 | case CRUSH_BUCKET_TREE: |
| 218 | size = sizeof(struct crush_bucket_tree); |
| 219 | break; |
| 220 | case CRUSH_BUCKET_STRAW: |
| 221 | size = sizeof(struct crush_bucket_straw); |
| 222 | break; |
Ilya Dryomov | 958a276 | 2015-04-14 16:54:52 +0300 | [diff] [blame] | 223 | case CRUSH_BUCKET_STRAW2: |
| 224 | size = sizeof(struct crush_bucket_straw2); |
| 225 | break; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 226 | default: |
Sage Weil | 30dc638 | 2009-12-21 14:49:37 -0800 | [diff] [blame] | 227 | err = -EINVAL; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 228 | goto bad; |
| 229 | } |
| 230 | BUG_ON(size == 0); |
| 231 | b = c->buckets[i] = kzalloc(size, GFP_NOFS); |
| 232 | if (b == NULL) |
| 233 | goto badmem; |
| 234 | |
| 235 | ceph_decode_need(p, end, 4*sizeof(u32), bad); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 236 | b->id = ceph_decode_32(p); |
| 237 | b->type = ceph_decode_16(p); |
Sage Weil | fb69039 | 2009-11-07 20:18:22 -0800 | [diff] [blame] | 238 | b->alg = ceph_decode_8(p); |
| 239 | b->hash = ceph_decode_8(p); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 240 | b->weight = ceph_decode_32(p); |
| 241 | b->size = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 242 | |
| 243 | dout("crush_decode bucket size %d off %x %p to %p\n", |
| 244 | b->size, (int)(*p-start), *p, end); |
| 245 | |
| 246 | b->items = kcalloc(b->size, sizeof(__s32), GFP_NOFS); |
| 247 | if (b->items == NULL) |
| 248 | goto badmem; |
| 249 | b->perm = kcalloc(b->size, sizeof(u32), GFP_NOFS); |
| 250 | if (b->perm == NULL) |
| 251 | goto badmem; |
| 252 | b->perm_n = 0; |
| 253 | |
| 254 | ceph_decode_need(p, end, b->size*sizeof(u32), bad); |
| 255 | for (j = 0; j < b->size; j++) |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 256 | b->items[j] = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 257 | |
| 258 | switch (b->alg) { |
| 259 | case CRUSH_BUCKET_UNIFORM: |
| 260 | err = crush_decode_uniform_bucket(p, end, |
| 261 | (struct crush_bucket_uniform *)b); |
| 262 | if (err < 0) |
| 263 | goto bad; |
| 264 | break; |
| 265 | case CRUSH_BUCKET_LIST: |
| 266 | err = crush_decode_list_bucket(p, end, |
| 267 | (struct crush_bucket_list *)b); |
| 268 | if (err < 0) |
| 269 | goto bad; |
| 270 | break; |
| 271 | case CRUSH_BUCKET_TREE: |
| 272 | err = crush_decode_tree_bucket(p, end, |
| 273 | (struct crush_bucket_tree *)b); |
| 274 | if (err < 0) |
| 275 | goto bad; |
| 276 | break; |
| 277 | case CRUSH_BUCKET_STRAW: |
| 278 | err = crush_decode_straw_bucket(p, end, |
| 279 | (struct crush_bucket_straw *)b); |
| 280 | if (err < 0) |
| 281 | goto bad; |
| 282 | break; |
Ilya Dryomov | 958a276 | 2015-04-14 16:54:52 +0300 | [diff] [blame] | 283 | case CRUSH_BUCKET_STRAW2: |
| 284 | err = crush_decode_straw2_bucket(p, end, |
| 285 | (struct crush_bucket_straw2 *)b); |
| 286 | if (err < 0) |
| 287 | goto bad; |
| 288 | break; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
| 292 | /* rules */ |
| 293 | dout("rule vec is %p\n", c->rules); |
| 294 | for (i = 0; i < c->max_rules; i++) { |
| 295 | u32 yes; |
| 296 | struct crush_rule *r; |
| 297 | |
| 298 | ceph_decode_32_safe(p, end, yes, bad); |
| 299 | if (!yes) { |
| 300 | dout("crush_decode NO rule %d off %x %p to %p\n", |
| 301 | i, (int)(*p-start), *p, end); |
| 302 | c->rules[i] = NULL; |
| 303 | continue; |
| 304 | } |
| 305 | |
| 306 | dout("crush_decode rule %d off %x %p to %p\n", |
| 307 | i, (int)(*p-start), *p, end); |
| 308 | |
| 309 | /* len */ |
| 310 | ceph_decode_32_safe(p, end, yes, bad); |
| 311 | #if BITS_PER_LONG == 32 |
Sage Weil | 30dc638 | 2009-12-21 14:49:37 -0800 | [diff] [blame] | 312 | err = -EINVAL; |
Xi Wang | 6448669 | 2012-02-16 11:55:48 -0500 | [diff] [blame] | 313 | if (yes > (ULONG_MAX - sizeof(*r)) |
| 314 | / sizeof(struct crush_rule_step)) |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 315 | goto bad; |
| 316 | #endif |
| 317 | r = c->rules[i] = kmalloc(sizeof(*r) + |
| 318 | yes*sizeof(struct crush_rule_step), |
| 319 | GFP_NOFS); |
| 320 | if (r == NULL) |
| 321 | goto badmem; |
| 322 | dout(" rule %d is at %p\n", i, r); |
| 323 | r->len = yes; |
| 324 | ceph_decode_copy_safe(p, end, &r->mask, 4, bad); /* 4 u8's */ |
| 325 | ceph_decode_need(p, end, r->len*3*sizeof(u32), bad); |
| 326 | for (j = 0; j < r->len; j++) { |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 327 | r->steps[j].op = ceph_decode_32(p); |
| 328 | r->steps[j].arg1 = ceph_decode_32(p); |
| 329 | r->steps[j].arg2 = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
| 333 | /* ignore trailing name maps. */ |
Sage Weil | 546f04e | 2012-07-30 18:15:23 -0700 | [diff] [blame] | 334 | for (num_name_maps = 0; num_name_maps < 3; num_name_maps++) { |
| 335 | err = skip_name_map(p, end); |
| 336 | if (err < 0) |
| 337 | goto done; |
| 338 | } |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 339 | |
Sage Weil | 546f04e | 2012-07-30 18:15:23 -0700 | [diff] [blame] | 340 | /* tunables */ |
| 341 | ceph_decode_need(p, end, 3*sizeof(u32), done); |
| 342 | c->choose_local_tries = ceph_decode_32(p); |
| 343 | c->choose_local_fallback_tries = ceph_decode_32(p); |
| 344 | c->choose_total_tries = ceph_decode_32(p); |
Ilya Dryomov | b9b519b | 2016-02-01 16:57:16 +0100 | [diff] [blame] | 345 | dout("crush decode tunable choose_local_tries = %d\n", |
Sage Weil | 546f04e | 2012-07-30 18:15:23 -0700 | [diff] [blame] | 346 | c->choose_local_tries); |
Ilya Dryomov | b9b519b | 2016-02-01 16:57:16 +0100 | [diff] [blame] | 347 | dout("crush decode tunable choose_local_fallback_tries = %d\n", |
Sage Weil | 546f04e | 2012-07-30 18:15:23 -0700 | [diff] [blame] | 348 | c->choose_local_fallback_tries); |
Ilya Dryomov | b9b519b | 2016-02-01 16:57:16 +0100 | [diff] [blame] | 349 | dout("crush decode tunable choose_total_tries = %d\n", |
Sage Weil | 546f04e | 2012-07-30 18:15:23 -0700 | [diff] [blame] | 350 | c->choose_total_tries); |
| 351 | |
Jim Schutt | 1604f48 | 2012-11-30 09:15:25 -0700 | [diff] [blame] | 352 | ceph_decode_need(p, end, sizeof(u32), done); |
| 353 | c->chooseleaf_descend_once = ceph_decode_32(p); |
Ilya Dryomov | b9b519b | 2016-02-01 16:57:16 +0100 | [diff] [blame] | 354 | dout("crush decode tunable chooseleaf_descend_once = %d\n", |
Jim Schutt | 1604f48 | 2012-11-30 09:15:25 -0700 | [diff] [blame] | 355 | c->chooseleaf_descend_once); |
| 356 | |
Ilya Dryomov | f140662 | 2014-05-09 18:27:34 +0400 | [diff] [blame] | 357 | ceph_decode_need(p, end, sizeof(u8), done); |
| 358 | c->chooseleaf_vary_r = ceph_decode_8(p); |
Ilya Dryomov | b9b519b | 2016-02-01 16:57:16 +0100 | [diff] [blame] | 359 | dout("crush decode tunable chooseleaf_vary_r = %d\n", |
Ilya Dryomov | f140662 | 2014-05-09 18:27:34 +0400 | [diff] [blame] | 360 | c->chooseleaf_vary_r); |
| 361 | |
Ilya Dryomov | b9b519b | 2016-02-01 16:57:16 +0100 | [diff] [blame] | 362 | /* skip straw_calc_version, allowed_bucket_algs */ |
| 363 | ceph_decode_need(p, end, sizeof(u8) + sizeof(u32), done); |
| 364 | *p += sizeof(u8) + sizeof(u32); |
| 365 | |
| 366 | ceph_decode_need(p, end, sizeof(u8), done); |
| 367 | c->chooseleaf_stable = ceph_decode_8(p); |
| 368 | dout("crush decode tunable chooseleaf_stable = %d\n", |
| 369 | c->chooseleaf_stable); |
| 370 | |
Sage Weil | 546f04e | 2012-07-30 18:15:23 -0700 | [diff] [blame] | 371 | done: |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 372 | dout("crush_decode success\n"); |
| 373 | return c; |
| 374 | |
| 375 | badmem: |
| 376 | err = -ENOMEM; |
| 377 | bad: |
| 378 | dout("crush_decode fail %d\n", err); |
| 379 | crush_destroy(c); |
| 380 | return ERR_PTR(err); |
| 381 | } |
| 382 | |
Ilya Dryomov | f984cb7 | 2016-04-28 16:07:23 +0200 | [diff] [blame] | 383 | int ceph_pg_compare(const struct ceph_pg *lhs, const struct ceph_pg *rhs) |
| 384 | { |
| 385 | if (lhs->pool < rhs->pool) |
| 386 | return -1; |
| 387 | if (lhs->pool > rhs->pool) |
| 388 | return 1; |
| 389 | if (lhs->seed < rhs->seed) |
| 390 | return -1; |
| 391 | if (lhs->seed > rhs->seed) |
| 392 | return 1; |
| 393 | |
| 394 | return 0; |
| 395 | } |
| 396 | |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 397 | /* |
Sage Weil | 9794b14 | 2010-02-16 15:53:32 -0800 | [diff] [blame] | 398 | * rbtree of pg_mapping for handling pg_temp (explicit mapping of pgid |
Ilya Dryomov | 9686f94 | 2014-03-21 19:05:29 +0200 | [diff] [blame] | 399 | * to a set of osds) and primary_temp (explicit primary setting) |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 400 | */ |
Sage Weil | 991abb6 | 2009-10-08 22:22:37 -0700 | [diff] [blame] | 401 | static int __insert_pg_mapping(struct ceph_pg_mapping *new, |
| 402 | struct rb_root *root) |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 403 | { |
| 404 | struct rb_node **p = &root->rb_node; |
| 405 | struct rb_node *parent = NULL; |
| 406 | struct ceph_pg_mapping *pg = NULL; |
Sage Weil | 5104212 | 2009-11-04 11:39:12 -0800 | [diff] [blame] | 407 | int c; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 408 | |
Sage Weil | 8adc8b3 | 2011-09-28 10:11:04 -0700 | [diff] [blame] | 409 | dout("__insert_pg_mapping %llx %p\n", *(u64 *)&new->pgid, new); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 410 | while (*p) { |
| 411 | parent = *p; |
| 412 | pg = rb_entry(parent, struct ceph_pg_mapping, node); |
Ilya Dryomov | f984cb7 | 2016-04-28 16:07:23 +0200 | [diff] [blame] | 413 | c = ceph_pg_compare(&new->pgid, &pg->pgid); |
Sage Weil | 5104212 | 2009-11-04 11:39:12 -0800 | [diff] [blame] | 414 | if (c < 0) |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 415 | p = &(*p)->rb_left; |
Sage Weil | 5104212 | 2009-11-04 11:39:12 -0800 | [diff] [blame] | 416 | else if (c > 0) |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 417 | p = &(*p)->rb_right; |
| 418 | else |
Sage Weil | 991abb6 | 2009-10-08 22:22:37 -0700 | [diff] [blame] | 419 | return -EEXIST; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | rb_link_node(&new->node, parent, p); |
| 423 | rb_insert_color(&new->node, root); |
Sage Weil | 991abb6 | 2009-10-08 22:22:37 -0700 | [diff] [blame] | 424 | return 0; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Sage Weil | 9794b14 | 2010-02-16 15:53:32 -0800 | [diff] [blame] | 427 | static struct ceph_pg_mapping *__lookup_pg_mapping(struct rb_root *root, |
Sage Weil | 5b191d9 | 2013-02-23 10:38:16 -0800 | [diff] [blame] | 428 | struct ceph_pg pgid) |
Sage Weil | 9794b14 | 2010-02-16 15:53:32 -0800 | [diff] [blame] | 429 | { |
| 430 | struct rb_node *n = root->rb_node; |
| 431 | struct ceph_pg_mapping *pg; |
| 432 | int c; |
| 433 | |
| 434 | while (n) { |
| 435 | pg = rb_entry(n, struct ceph_pg_mapping, node); |
Ilya Dryomov | f984cb7 | 2016-04-28 16:07:23 +0200 | [diff] [blame] | 436 | c = ceph_pg_compare(&pgid, &pg->pgid); |
Sage Weil | 8adc8b3 | 2011-09-28 10:11:04 -0700 | [diff] [blame] | 437 | if (c < 0) { |
Sage Weil | 9794b14 | 2010-02-16 15:53:32 -0800 | [diff] [blame] | 438 | n = n->rb_left; |
Sage Weil | 8adc8b3 | 2011-09-28 10:11:04 -0700 | [diff] [blame] | 439 | } else if (c > 0) { |
Sage Weil | 9794b14 | 2010-02-16 15:53:32 -0800 | [diff] [blame] | 440 | n = n->rb_right; |
Sage Weil | 8adc8b3 | 2011-09-28 10:11:04 -0700 | [diff] [blame] | 441 | } else { |
Sage Weil | 5b191d9 | 2013-02-23 10:38:16 -0800 | [diff] [blame] | 442 | dout("__lookup_pg_mapping %lld.%x got %p\n", |
| 443 | pgid.pool, pgid.seed, pg); |
Sage Weil | 9794b14 | 2010-02-16 15:53:32 -0800 | [diff] [blame] | 444 | return pg; |
Sage Weil | 8adc8b3 | 2011-09-28 10:11:04 -0700 | [diff] [blame] | 445 | } |
Sage Weil | 9794b14 | 2010-02-16 15:53:32 -0800 | [diff] [blame] | 446 | } |
| 447 | return NULL; |
| 448 | } |
| 449 | |
Sage Weil | 5b191d9 | 2013-02-23 10:38:16 -0800 | [diff] [blame] | 450 | 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] | 451 | { |
| 452 | struct ceph_pg_mapping *pg = __lookup_pg_mapping(root, pgid); |
| 453 | |
| 454 | if (pg) { |
Sage Weil | 5b191d9 | 2013-02-23 10:38:16 -0800 | [diff] [blame] | 455 | dout("__remove_pg_mapping %lld.%x %p\n", pgid.pool, pgid.seed, |
| 456 | pg); |
Sage Weil | 8adc8b3 | 2011-09-28 10:11:04 -0700 | [diff] [blame] | 457 | rb_erase(&pg->node, root); |
| 458 | kfree(pg); |
| 459 | return 0; |
| 460 | } |
Sage Weil | 5b191d9 | 2013-02-23 10:38:16 -0800 | [diff] [blame] | 461 | dout("__remove_pg_mapping %lld.%x dne\n", pgid.pool, pgid.seed); |
Sage Weil | 8adc8b3 | 2011-09-28 10:11:04 -0700 | [diff] [blame] | 462 | return -ENOENT; |
| 463 | } |
| 464 | |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 465 | /* |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 466 | * rbtree of pg pool info |
| 467 | */ |
| 468 | static int __insert_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *new) |
| 469 | { |
| 470 | struct rb_node **p = &root->rb_node; |
| 471 | struct rb_node *parent = NULL; |
| 472 | struct ceph_pg_pool_info *pi = NULL; |
| 473 | |
| 474 | while (*p) { |
| 475 | parent = *p; |
| 476 | pi = rb_entry(parent, struct ceph_pg_pool_info, node); |
| 477 | if (new->id < pi->id) |
| 478 | p = &(*p)->rb_left; |
| 479 | else if (new->id > pi->id) |
| 480 | p = &(*p)->rb_right; |
| 481 | else |
| 482 | return -EEXIST; |
| 483 | } |
| 484 | |
| 485 | rb_link_node(&new->node, parent, p); |
| 486 | rb_insert_color(&new->node, root); |
| 487 | return 0; |
| 488 | } |
| 489 | |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 490 | 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] | 491 | { |
| 492 | struct ceph_pg_pool_info *pi; |
| 493 | struct rb_node *n = root->rb_node; |
| 494 | |
| 495 | while (n) { |
| 496 | pi = rb_entry(n, struct ceph_pg_pool_info, node); |
| 497 | if (id < pi->id) |
| 498 | n = n->rb_left; |
| 499 | else if (id > pi->id) |
| 500 | n = n->rb_right; |
| 501 | else |
| 502 | return pi; |
| 503 | } |
| 504 | return NULL; |
| 505 | } |
| 506 | |
Ilya Dryomov | ce7f6a2 | 2014-01-27 17:40:19 +0200 | [diff] [blame] | 507 | struct ceph_pg_pool_info *ceph_pg_pool_by_id(struct ceph_osdmap *map, u64 id) |
| 508 | { |
| 509 | return __lookup_pg_pool(&map->pg_pools, id); |
| 510 | } |
| 511 | |
Alex Elder | 72afc71 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 512 | const char *ceph_pg_pool_name_by_id(struct ceph_osdmap *map, u64 id) |
| 513 | { |
| 514 | struct ceph_pg_pool_info *pi; |
| 515 | |
| 516 | if (id == CEPH_NOPOOL) |
| 517 | return NULL; |
| 518 | |
| 519 | if (WARN_ON_ONCE(id > (u64) INT_MAX)) |
| 520 | return NULL; |
| 521 | |
| 522 | pi = __lookup_pg_pool(&map->pg_pools, (int) id); |
| 523 | |
| 524 | return pi ? pi->name : NULL; |
| 525 | } |
| 526 | EXPORT_SYMBOL(ceph_pg_pool_name_by_id); |
| 527 | |
Yehuda Sadeh | 7669a2c | 2010-05-17 12:31:35 -0700 | [diff] [blame] | 528 | int ceph_pg_poolid_by_name(struct ceph_osdmap *map, const char *name) |
| 529 | { |
| 530 | struct rb_node *rbp; |
| 531 | |
| 532 | for (rbp = rb_first(&map->pg_pools); rbp; rbp = rb_next(rbp)) { |
| 533 | struct ceph_pg_pool_info *pi = |
| 534 | rb_entry(rbp, struct ceph_pg_pool_info, node); |
| 535 | if (pi->name && strcmp(pi->name, name) == 0) |
| 536 | return pi->id; |
| 537 | } |
| 538 | return -ENOENT; |
| 539 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 540 | EXPORT_SYMBOL(ceph_pg_poolid_by_name); |
Yehuda Sadeh | 7669a2c | 2010-05-17 12:31:35 -0700 | [diff] [blame] | 541 | |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 542 | static void __remove_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *pi) |
| 543 | { |
| 544 | rb_erase(&pi->node, root); |
| 545 | kfree(pi->name); |
| 546 | kfree(pi); |
| 547 | } |
| 548 | |
Ilya Dryomov | 0f70c7e | 2014-03-21 19:05:27 +0200 | [diff] [blame] | 549 | 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] | 550 | { |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 551 | u8 ev, cv; |
| 552 | unsigned len, num; |
| 553 | void *pool_end; |
Sage Weil | 73a7e69 | 2010-08-02 11:00:55 -0700 | [diff] [blame] | 554 | |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 555 | ceph_decode_need(p, end, 2 + 4, bad); |
| 556 | ev = ceph_decode_8(p); /* encoding version */ |
| 557 | cv = ceph_decode_8(p); /* compat version */ |
| 558 | if (ev < 5) { |
Joe Perches | b9a6789 | 2014-09-09 21:17:29 -0700 | [diff] [blame] | 559 | 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] | 560 | return -EINVAL; |
| 561 | } |
Ilya Dryomov | 17a13e40 | 2014-01-27 17:40:19 +0200 | [diff] [blame] | 562 | if (cv > 9) { |
Joe Perches | b9a6789 | 2014-09-09 21:17:29 -0700 | [diff] [blame] | 563 | 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] | 564 | return -EINVAL; |
| 565 | } |
| 566 | len = ceph_decode_32(p); |
| 567 | ceph_decode_need(p, end, len, bad); |
| 568 | pool_end = *p + len; |
Sage Weil | 73a7e69 | 2010-08-02 11:00:55 -0700 | [diff] [blame] | 569 | |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 570 | pi->type = ceph_decode_8(p); |
| 571 | pi->size = ceph_decode_8(p); |
| 572 | pi->crush_ruleset = ceph_decode_8(p); |
| 573 | pi->object_hash = ceph_decode_8(p); |
| 574 | |
| 575 | pi->pg_num = ceph_decode_32(p); |
| 576 | pi->pgp_num = ceph_decode_32(p); |
| 577 | |
| 578 | *p += 4 + 4; /* skip lpg* */ |
| 579 | *p += 4; /* skip last_change */ |
| 580 | *p += 8 + 4; /* skip snap_seq, snap_epoch */ |
| 581 | |
| 582 | /* skip snaps */ |
| 583 | num = ceph_decode_32(p); |
| 584 | while (num--) { |
| 585 | *p += 8; /* snapid key */ |
| 586 | *p += 1 + 1; /* versions */ |
| 587 | len = ceph_decode_32(p); |
| 588 | *p += len; |
Sage Weil | 73a7e69 | 2010-08-02 11:00:55 -0700 | [diff] [blame] | 589 | } |
| 590 | |
Ilya Dryomov | 17a13e40 | 2014-01-27 17:40:19 +0200 | [diff] [blame] | 591 | /* skip removed_snaps */ |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 592 | num = ceph_decode_32(p); |
| 593 | *p += num * (8 + 8); |
| 594 | |
| 595 | *p += 8; /* skip auid */ |
| 596 | pi->flags = ceph_decode_64(p); |
Ilya Dryomov | 17a13e40 | 2014-01-27 17:40:19 +0200 | [diff] [blame] | 597 | *p += 4; /* skip crash_replay_interval */ |
| 598 | |
| 599 | if (ev >= 7) |
Ilya Dryomov | 04812ac | 2016-04-28 16:07:23 +0200 | [diff] [blame^] | 600 | pi->min_size = ceph_decode_8(p); |
| 601 | else |
| 602 | pi->min_size = pi->size - pi->size / 2; |
Ilya Dryomov | 17a13e40 | 2014-01-27 17:40:19 +0200 | [diff] [blame] | 603 | |
| 604 | if (ev >= 8) |
| 605 | *p += 8 + 8; /* skip quota_max_* */ |
| 606 | |
| 607 | if (ev >= 9) { |
| 608 | /* skip tiers */ |
| 609 | num = ceph_decode_32(p); |
| 610 | *p += num * 8; |
| 611 | |
| 612 | *p += 8; /* skip tier_of */ |
| 613 | *p += 1; /* skip cache_mode */ |
| 614 | |
| 615 | pi->read_tier = ceph_decode_64(p); |
| 616 | pi->write_tier = ceph_decode_64(p); |
| 617 | } else { |
| 618 | pi->read_tier = -1; |
| 619 | pi->write_tier = -1; |
| 620 | } |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 621 | |
Ilya Dryomov | 04812ac | 2016-04-28 16:07:23 +0200 | [diff] [blame^] | 622 | if (ev >= 10) { |
| 623 | /* skip properties */ |
| 624 | num = ceph_decode_32(p); |
| 625 | while (num--) { |
| 626 | len = ceph_decode_32(p); |
| 627 | *p += len; /* key */ |
| 628 | len = ceph_decode_32(p); |
| 629 | *p += len; /* val */ |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | if (ev >= 11) { |
| 634 | /* skip hit_set_params */ |
| 635 | *p += 1 + 1; /* versions */ |
| 636 | len = ceph_decode_32(p); |
| 637 | *p += len; |
| 638 | |
| 639 | *p += 4; /* skip hit_set_period */ |
| 640 | *p += 4; /* skip hit_set_count */ |
| 641 | } |
| 642 | |
| 643 | if (ev >= 12) |
| 644 | *p += 4; /* skip stripe_width */ |
| 645 | |
| 646 | if (ev >= 13) { |
| 647 | *p += 8; /* skip target_max_bytes */ |
| 648 | *p += 8; /* skip target_max_objects */ |
| 649 | *p += 4; /* skip cache_target_dirty_ratio_micro */ |
| 650 | *p += 4; /* skip cache_target_full_ratio_micro */ |
| 651 | *p += 4; /* skip cache_min_flush_age */ |
| 652 | *p += 4; /* skip cache_min_evict_age */ |
| 653 | } |
| 654 | |
| 655 | if (ev >= 14) { |
| 656 | /* skip erasure_code_profile */ |
| 657 | len = ceph_decode_32(p); |
| 658 | *p += len; |
| 659 | } |
| 660 | |
| 661 | if (ev >= 15) |
| 662 | pi->last_force_request_resend = ceph_decode_32(p); |
| 663 | else |
| 664 | pi->last_force_request_resend = 0; |
| 665 | |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 666 | /* ignore the rest */ |
| 667 | |
| 668 | *p = pool_end; |
| 669 | calc_pg_masks(pi); |
Sage Weil | 73a7e69 | 2010-08-02 11:00:55 -0700 | [diff] [blame] | 670 | return 0; |
| 671 | |
| 672 | bad: |
| 673 | return -EINVAL; |
Sage Weil | efd7576 | 2010-03-17 10:05:28 -0700 | [diff] [blame] | 674 | } |
| 675 | |
Ilya Dryomov | 0f70c7e | 2014-03-21 19:05:27 +0200 | [diff] [blame] | 676 | 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] | 677 | { |
| 678 | struct ceph_pg_pool_info *pi; |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 679 | u32 num, len; |
| 680 | u64 pool; |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 681 | |
| 682 | ceph_decode_32_safe(p, end, num, bad); |
| 683 | dout(" %d pool names\n", num); |
| 684 | while (num--) { |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 685 | ceph_decode_64_safe(p, end, pool, bad); |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 686 | ceph_decode_32_safe(p, end, len, bad); |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 687 | dout(" pool %llu len %d\n", pool, len); |
Xi Wang | ad3b904 | 2012-06-06 19:35:55 -0500 | [diff] [blame] | 688 | ceph_decode_need(p, end, len, bad); |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 689 | pi = __lookup_pg_pool(&map->pg_pools, pool); |
| 690 | if (pi) { |
Xi Wang | ad3b904 | 2012-06-06 19:35:55 -0500 | [diff] [blame] | 691 | char *name = kstrndup(*p, len, GFP_NOFS); |
| 692 | |
| 693 | if (!name) |
| 694 | return -ENOMEM; |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 695 | kfree(pi->name); |
Xi Wang | ad3b904 | 2012-06-06 19:35:55 -0500 | [diff] [blame] | 696 | pi->name = name; |
| 697 | dout(" name is %s\n", pi->name); |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 698 | } |
| 699 | *p += len; |
| 700 | } |
| 701 | return 0; |
| 702 | |
| 703 | bad: |
| 704 | return -EINVAL; |
| 705 | } |
| 706 | |
| 707 | /* |
| 708 | * osd map |
| 709 | */ |
| 710 | void ceph_osdmap_destroy(struct ceph_osdmap *map) |
| 711 | { |
| 712 | dout("osdmap_destroy %p\n", map); |
| 713 | if (map->crush) |
| 714 | crush_destroy(map->crush); |
| 715 | while (!RB_EMPTY_ROOT(&map->pg_temp)) { |
| 716 | struct ceph_pg_mapping *pg = |
| 717 | rb_entry(rb_first(&map->pg_temp), |
| 718 | struct ceph_pg_mapping, node); |
| 719 | rb_erase(&pg->node, &map->pg_temp); |
| 720 | kfree(pg); |
| 721 | } |
Ilya Dryomov | 9686f94 | 2014-03-21 19:05:29 +0200 | [diff] [blame] | 722 | while (!RB_EMPTY_ROOT(&map->primary_temp)) { |
| 723 | struct ceph_pg_mapping *pg = |
| 724 | rb_entry(rb_first(&map->primary_temp), |
| 725 | struct ceph_pg_mapping, node); |
| 726 | rb_erase(&pg->node, &map->primary_temp); |
| 727 | kfree(pg); |
| 728 | } |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 729 | while (!RB_EMPTY_ROOT(&map->pg_pools)) { |
| 730 | struct ceph_pg_pool_info *pi = |
| 731 | rb_entry(rb_first(&map->pg_pools), |
| 732 | struct ceph_pg_pool_info, node); |
| 733 | __remove_pg_pool(&map->pg_pools, pi); |
| 734 | } |
| 735 | kfree(map->osd_state); |
| 736 | kfree(map->osd_weight); |
| 737 | kfree(map->osd_addr); |
Ilya Dryomov | 2cfa34f | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 738 | kfree(map->osd_primary_affinity); |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 739 | kfree(map); |
| 740 | } |
| 741 | |
| 742 | /* |
Ilya Dryomov | 4d60351 | 2014-03-21 19:05:28 +0200 | [diff] [blame] | 743 | * Adjust max_osd value, (re)allocate arrays. |
| 744 | * |
| 745 | * The new elements are properly initialized. |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 746 | */ |
| 747 | static int osdmap_set_max_osd(struct ceph_osdmap *map, int max) |
| 748 | { |
| 749 | u8 *state; |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 750 | u32 *weight; |
Ilya Dryomov | 4d60351 | 2014-03-21 19:05:28 +0200 | [diff] [blame] | 751 | struct ceph_entity_addr *addr; |
| 752 | int i; |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 753 | |
Ilya Dryomov | 4d60351 | 2014-03-21 19:05:28 +0200 | [diff] [blame] | 754 | state = krealloc(map->osd_state, max*sizeof(*state), GFP_NOFS); |
Li RongQing | 589506f | 2014-09-07 18:10:51 +0800 | [diff] [blame] | 755 | if (!state) |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 756 | return -ENOMEM; |
Li RongQing | 589506f | 2014-09-07 18:10:51 +0800 | [diff] [blame] | 757 | map->osd_state = state; |
| 758 | |
| 759 | weight = krealloc(map->osd_weight, max*sizeof(*weight), GFP_NOFS); |
| 760 | if (!weight) |
| 761 | return -ENOMEM; |
| 762 | map->osd_weight = weight; |
| 763 | |
| 764 | addr = krealloc(map->osd_addr, max*sizeof(*addr), GFP_NOFS); |
| 765 | if (!addr) |
| 766 | return -ENOMEM; |
| 767 | map->osd_addr = addr; |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 768 | |
Ilya Dryomov | 4d60351 | 2014-03-21 19:05:28 +0200 | [diff] [blame] | 769 | for (i = map->max_osd; i < max; i++) { |
Li RongQing | 589506f | 2014-09-07 18:10:51 +0800 | [diff] [blame] | 770 | map->osd_state[i] = 0; |
| 771 | map->osd_weight[i] = CEPH_OSD_OUT; |
| 772 | memset(map->osd_addr + i, 0, sizeof(*map->osd_addr)); |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 773 | } |
| 774 | |
Ilya Dryomov | 2cfa34f | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 775 | if (map->osd_primary_affinity) { |
| 776 | u32 *affinity; |
| 777 | |
| 778 | affinity = krealloc(map->osd_primary_affinity, |
| 779 | max*sizeof(*affinity), GFP_NOFS); |
| 780 | if (!affinity) |
| 781 | return -ENOMEM; |
Li RongQing | 589506f | 2014-09-07 18:10:51 +0800 | [diff] [blame] | 782 | map->osd_primary_affinity = affinity; |
Ilya Dryomov | 2cfa34f | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 783 | |
| 784 | for (i = map->max_osd; i < max; i++) |
Li RongQing | 589506f | 2014-09-07 18:10:51 +0800 | [diff] [blame] | 785 | map->osd_primary_affinity[i] = |
| 786 | CEPH_OSD_DEFAULT_PRIMARY_AFFINITY; |
Ilya Dryomov | 2cfa34f | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 787 | } |
| 788 | |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 789 | map->max_osd = max; |
Ilya Dryomov | 4d60351 | 2014-03-21 19:05:28 +0200 | [diff] [blame] | 790 | |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 791 | return 0; |
| 792 | } |
| 793 | |
Ilya Dryomov | ec7af97 | 2014-03-21 19:05:29 +0200 | [diff] [blame] | 794 | #define OSDMAP_WRAPPER_COMPAT_VER 7 |
| 795 | #define OSDMAP_CLIENT_DATA_COMPAT_VER 1 |
| 796 | |
| 797 | /* |
| 798 | * Return 0 or error. On success, *v is set to 0 for old (v6) osdmaps, |
| 799 | * to struct_v of the client_data section for new (v7 and above) |
| 800 | * osdmaps. |
| 801 | */ |
| 802 | static int get_osdmap_client_data_v(void **p, void *end, |
| 803 | const char *prefix, u8 *v) |
| 804 | { |
| 805 | u8 struct_v; |
| 806 | |
| 807 | ceph_decode_8_safe(p, end, struct_v, e_inval); |
| 808 | if (struct_v >= 7) { |
| 809 | u8 struct_compat; |
| 810 | |
| 811 | ceph_decode_8_safe(p, end, struct_compat, e_inval); |
| 812 | if (struct_compat > OSDMAP_WRAPPER_COMPAT_VER) { |
Joe Perches | b9a6789 | 2014-09-09 21:17:29 -0700 | [diff] [blame] | 813 | pr_warn("got v %d cv %d > %d of %s ceph_osdmap\n", |
| 814 | struct_v, struct_compat, |
| 815 | OSDMAP_WRAPPER_COMPAT_VER, prefix); |
Ilya Dryomov | ec7af97 | 2014-03-21 19:05:29 +0200 | [diff] [blame] | 816 | return -EINVAL; |
| 817 | } |
| 818 | *p += 4; /* ignore wrapper struct_len */ |
| 819 | |
| 820 | ceph_decode_8_safe(p, end, struct_v, e_inval); |
| 821 | ceph_decode_8_safe(p, end, struct_compat, e_inval); |
| 822 | if (struct_compat > OSDMAP_CLIENT_DATA_COMPAT_VER) { |
Joe Perches | b9a6789 | 2014-09-09 21:17:29 -0700 | [diff] [blame] | 823 | pr_warn("got v %d cv %d > %d of %s ceph_osdmap client data\n", |
| 824 | struct_v, struct_compat, |
| 825 | OSDMAP_CLIENT_DATA_COMPAT_VER, prefix); |
Ilya Dryomov | ec7af97 | 2014-03-21 19:05:29 +0200 | [diff] [blame] | 826 | return -EINVAL; |
| 827 | } |
| 828 | *p += 4; /* ignore client data struct_len */ |
| 829 | } else { |
| 830 | u16 version; |
| 831 | |
| 832 | *p -= 1; |
| 833 | ceph_decode_16_safe(p, end, version, e_inval); |
| 834 | if (version < 6) { |
Joe Perches | b9a6789 | 2014-09-09 21:17:29 -0700 | [diff] [blame] | 835 | pr_warn("got v %d < 6 of %s ceph_osdmap\n", |
| 836 | version, prefix); |
Ilya Dryomov | ec7af97 | 2014-03-21 19:05:29 +0200 | [diff] [blame] | 837 | return -EINVAL; |
| 838 | } |
| 839 | |
| 840 | /* old osdmap enconding */ |
| 841 | struct_v = 0; |
| 842 | } |
| 843 | |
| 844 | *v = struct_v; |
| 845 | return 0; |
| 846 | |
| 847 | e_inval: |
| 848 | return -EINVAL; |
| 849 | } |
| 850 | |
Ilya Dryomov | 433fbdd | 2014-03-21 19:05:27 +0200 | [diff] [blame] | 851 | static int __decode_pools(void **p, void *end, struct ceph_osdmap *map, |
| 852 | bool incremental) |
| 853 | { |
| 854 | u32 n; |
| 855 | |
| 856 | ceph_decode_32_safe(p, end, n, e_inval); |
| 857 | while (n--) { |
| 858 | struct ceph_pg_pool_info *pi; |
| 859 | u64 pool; |
| 860 | int ret; |
| 861 | |
| 862 | ceph_decode_64_safe(p, end, pool, e_inval); |
| 863 | |
| 864 | pi = __lookup_pg_pool(&map->pg_pools, pool); |
| 865 | if (!incremental || !pi) { |
| 866 | pi = kzalloc(sizeof(*pi), GFP_NOFS); |
| 867 | if (!pi) |
| 868 | return -ENOMEM; |
| 869 | |
| 870 | pi->id = pool; |
| 871 | |
| 872 | ret = __insert_pg_pool(&map->pg_pools, pi); |
| 873 | if (ret) { |
| 874 | kfree(pi); |
| 875 | return ret; |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | ret = decode_pool(p, end, pi); |
| 880 | if (ret) |
| 881 | return ret; |
| 882 | } |
| 883 | |
| 884 | return 0; |
| 885 | |
| 886 | e_inval: |
| 887 | return -EINVAL; |
| 888 | } |
| 889 | |
| 890 | static int decode_pools(void **p, void *end, struct ceph_osdmap *map) |
| 891 | { |
| 892 | return __decode_pools(p, end, map, false); |
| 893 | } |
| 894 | |
| 895 | static int decode_new_pools(void **p, void *end, struct ceph_osdmap *map) |
| 896 | { |
| 897 | return __decode_pools(p, end, map, true); |
| 898 | } |
| 899 | |
Ilya Dryomov | 10db634 | 2014-03-21 19:05:28 +0200 | [diff] [blame] | 900 | static int __decode_pg_temp(void **p, void *end, struct ceph_osdmap *map, |
| 901 | bool incremental) |
| 902 | { |
| 903 | u32 n; |
| 904 | |
| 905 | ceph_decode_32_safe(p, end, n, e_inval); |
| 906 | while (n--) { |
| 907 | struct ceph_pg pgid; |
| 908 | u32 len, i; |
| 909 | int ret; |
| 910 | |
| 911 | ret = ceph_decode_pgid(p, end, &pgid); |
| 912 | if (ret) |
| 913 | return ret; |
| 914 | |
| 915 | ceph_decode_32_safe(p, end, len, e_inval); |
| 916 | |
| 917 | ret = __remove_pg_mapping(&map->pg_temp, pgid); |
| 918 | BUG_ON(!incremental && ret != -ENOENT); |
| 919 | |
| 920 | if (!incremental || len > 0) { |
| 921 | struct ceph_pg_mapping *pg; |
| 922 | |
| 923 | ceph_decode_need(p, end, len*sizeof(u32), e_inval); |
| 924 | |
| 925 | if (len > (UINT_MAX - sizeof(*pg)) / sizeof(u32)) |
| 926 | return -EINVAL; |
| 927 | |
| 928 | pg = kzalloc(sizeof(*pg) + len*sizeof(u32), GFP_NOFS); |
| 929 | if (!pg) |
| 930 | return -ENOMEM; |
| 931 | |
| 932 | pg->pgid = pgid; |
Ilya Dryomov | 35a935d | 2014-03-21 19:05:29 +0200 | [diff] [blame] | 933 | pg->pg_temp.len = len; |
Ilya Dryomov | 10db634 | 2014-03-21 19:05:28 +0200 | [diff] [blame] | 934 | for (i = 0; i < len; i++) |
Ilya Dryomov | 35a935d | 2014-03-21 19:05:29 +0200 | [diff] [blame] | 935 | pg->pg_temp.osds[i] = ceph_decode_32(p); |
Ilya Dryomov | 10db634 | 2014-03-21 19:05:28 +0200 | [diff] [blame] | 936 | |
| 937 | ret = __insert_pg_mapping(pg, &map->pg_temp); |
| 938 | if (ret) { |
| 939 | kfree(pg); |
| 940 | return ret; |
| 941 | } |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | return 0; |
| 946 | |
| 947 | e_inval: |
| 948 | return -EINVAL; |
| 949 | } |
| 950 | |
| 951 | static int decode_pg_temp(void **p, void *end, struct ceph_osdmap *map) |
| 952 | { |
| 953 | return __decode_pg_temp(p, end, map, false); |
| 954 | } |
| 955 | |
| 956 | static int decode_new_pg_temp(void **p, void *end, struct ceph_osdmap *map) |
| 957 | { |
| 958 | return __decode_pg_temp(p, end, map, true); |
| 959 | } |
| 960 | |
Ilya Dryomov | d286de7 | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 961 | static int __decode_primary_temp(void **p, void *end, struct ceph_osdmap *map, |
| 962 | bool incremental) |
| 963 | { |
| 964 | u32 n; |
| 965 | |
| 966 | ceph_decode_32_safe(p, end, n, e_inval); |
| 967 | while (n--) { |
| 968 | struct ceph_pg pgid; |
| 969 | u32 osd; |
| 970 | int ret; |
| 971 | |
| 972 | ret = ceph_decode_pgid(p, end, &pgid); |
| 973 | if (ret) |
| 974 | return ret; |
| 975 | |
| 976 | ceph_decode_32_safe(p, end, osd, e_inval); |
| 977 | |
| 978 | ret = __remove_pg_mapping(&map->primary_temp, pgid); |
| 979 | BUG_ON(!incremental && ret != -ENOENT); |
| 980 | |
| 981 | if (!incremental || osd != (u32)-1) { |
| 982 | struct ceph_pg_mapping *pg; |
| 983 | |
| 984 | pg = kzalloc(sizeof(*pg), GFP_NOFS); |
| 985 | if (!pg) |
| 986 | return -ENOMEM; |
| 987 | |
| 988 | pg->pgid = pgid; |
| 989 | pg->primary_temp.osd = osd; |
| 990 | |
| 991 | ret = __insert_pg_mapping(pg, &map->primary_temp); |
| 992 | if (ret) { |
| 993 | kfree(pg); |
| 994 | return ret; |
| 995 | } |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | return 0; |
| 1000 | |
| 1001 | e_inval: |
| 1002 | return -EINVAL; |
| 1003 | } |
| 1004 | |
| 1005 | static int decode_primary_temp(void **p, void *end, struct ceph_osdmap *map) |
| 1006 | { |
| 1007 | return __decode_primary_temp(p, end, map, false); |
| 1008 | } |
| 1009 | |
| 1010 | static int decode_new_primary_temp(void **p, void *end, |
| 1011 | struct ceph_osdmap *map) |
| 1012 | { |
| 1013 | return __decode_primary_temp(p, end, map, true); |
| 1014 | } |
| 1015 | |
Ilya Dryomov | 2cfa34f | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 1016 | u32 ceph_get_primary_affinity(struct ceph_osdmap *map, int osd) |
| 1017 | { |
| 1018 | BUG_ON(osd >= map->max_osd); |
| 1019 | |
| 1020 | if (!map->osd_primary_affinity) |
| 1021 | return CEPH_OSD_DEFAULT_PRIMARY_AFFINITY; |
| 1022 | |
| 1023 | return map->osd_primary_affinity[osd]; |
| 1024 | } |
| 1025 | |
| 1026 | static int set_primary_affinity(struct ceph_osdmap *map, int osd, u32 aff) |
| 1027 | { |
| 1028 | BUG_ON(osd >= map->max_osd); |
| 1029 | |
| 1030 | if (!map->osd_primary_affinity) { |
| 1031 | int i; |
| 1032 | |
| 1033 | map->osd_primary_affinity = kmalloc(map->max_osd*sizeof(u32), |
| 1034 | GFP_NOFS); |
| 1035 | if (!map->osd_primary_affinity) |
| 1036 | return -ENOMEM; |
| 1037 | |
| 1038 | for (i = 0; i < map->max_osd; i++) |
| 1039 | map->osd_primary_affinity[i] = |
| 1040 | CEPH_OSD_DEFAULT_PRIMARY_AFFINITY; |
| 1041 | } |
| 1042 | |
| 1043 | map->osd_primary_affinity[osd] = aff; |
| 1044 | |
| 1045 | return 0; |
| 1046 | } |
| 1047 | |
Ilya Dryomov | 63a6993 | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 1048 | static int decode_primary_affinity(void **p, void *end, |
| 1049 | struct ceph_osdmap *map) |
| 1050 | { |
| 1051 | u32 len, i; |
| 1052 | |
| 1053 | ceph_decode_32_safe(p, end, len, e_inval); |
| 1054 | if (len == 0) { |
| 1055 | kfree(map->osd_primary_affinity); |
| 1056 | map->osd_primary_affinity = NULL; |
| 1057 | return 0; |
| 1058 | } |
| 1059 | if (len != map->max_osd) |
| 1060 | goto e_inval; |
| 1061 | |
| 1062 | ceph_decode_need(p, end, map->max_osd*sizeof(u32), e_inval); |
| 1063 | |
| 1064 | for (i = 0; i < map->max_osd; i++) { |
| 1065 | int ret; |
| 1066 | |
| 1067 | ret = set_primary_affinity(map, i, ceph_decode_32(p)); |
| 1068 | if (ret) |
| 1069 | return ret; |
| 1070 | } |
| 1071 | |
| 1072 | return 0; |
| 1073 | |
| 1074 | e_inval: |
| 1075 | return -EINVAL; |
| 1076 | } |
| 1077 | |
| 1078 | static int decode_new_primary_affinity(void **p, void *end, |
| 1079 | struct ceph_osdmap *map) |
| 1080 | { |
| 1081 | u32 n; |
| 1082 | |
| 1083 | ceph_decode_32_safe(p, end, n, e_inval); |
| 1084 | while (n--) { |
| 1085 | u32 osd, aff; |
| 1086 | int ret; |
| 1087 | |
| 1088 | ceph_decode_32_safe(p, end, osd, e_inval); |
| 1089 | ceph_decode_32_safe(p, end, aff, e_inval); |
| 1090 | |
| 1091 | ret = set_primary_affinity(map, osd, aff); |
| 1092 | if (ret) |
| 1093 | return ret; |
Ilya Dryomov | f31da0f | 2014-04-02 20:34:04 +0400 | [diff] [blame] | 1094 | |
| 1095 | pr_info("osd%d primary-affinity 0x%x\n", osd, aff); |
Ilya Dryomov | 63a6993 | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | return 0; |
| 1099 | |
| 1100 | e_inval: |
| 1101 | return -EINVAL; |
| 1102 | } |
| 1103 | |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 1104 | /* |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1105 | * decode a full map. |
| 1106 | */ |
Ilya Dryomov | a2505d6 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1107 | static int osdmap_decode(void **p, void *end, struct ceph_osdmap *map) |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1108 | { |
Ilya Dryomov | ec7af97 | 2014-03-21 19:05:29 +0200 | [diff] [blame] | 1109 | u8 struct_v; |
Ilya Dryomov | 38a8d56 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1110 | u32 epoch = 0; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1111 | void *start = *p; |
Ilya Dryomov | 3977058 | 2014-03-13 16:36:14 +0200 | [diff] [blame] | 1112 | u32 max; |
| 1113 | u32 len, i; |
Ilya Dryomov | 597b52f | 2014-03-13 16:36:14 +0200 | [diff] [blame] | 1114 | int err; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1115 | |
Ilya Dryomov | a2505d6 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1116 | 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] | 1117 | |
Ilya Dryomov | ec7af97 | 2014-03-21 19:05:29 +0200 | [diff] [blame] | 1118 | err = get_osdmap_client_data_v(p, end, "full", &struct_v); |
| 1119 | if (err) |
| 1120 | goto bad; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1121 | |
Ilya Dryomov | 53bbaba | 2014-03-13 16:36:16 +0200 | [diff] [blame] | 1122 | /* fsid, epoch, created, modified */ |
| 1123 | ceph_decode_need(p, end, sizeof(map->fsid) + sizeof(u32) + |
| 1124 | sizeof(map->created) + sizeof(map->modified), e_inval); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1125 | ceph_decode_copy(p, &map->fsid, sizeof(map->fsid)); |
Ilya Dryomov | 38a8d56 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1126 | epoch = map->epoch = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1127 | ceph_decode_copy(p, &map->created, sizeof(map->created)); |
| 1128 | ceph_decode_copy(p, &map->modified, sizeof(map->modified)); |
| 1129 | |
Ilya Dryomov | 433fbdd | 2014-03-21 19:05:27 +0200 | [diff] [blame] | 1130 | /* pools */ |
| 1131 | err = decode_pools(p, end, map); |
| 1132 | if (err) |
| 1133 | goto bad; |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 1134 | |
Ilya Dryomov | 0f70c7e | 2014-03-21 19:05:27 +0200 | [diff] [blame] | 1135 | /* pool_name */ |
| 1136 | err = decode_pool_names(p, end, map); |
Ilya Dryomov | 597b52f | 2014-03-13 16:36:14 +0200 | [diff] [blame] | 1137 | if (err) |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 1138 | goto bad; |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 1139 | |
Ilya Dryomov | 597b52f | 2014-03-13 16:36:14 +0200 | [diff] [blame] | 1140 | ceph_decode_32_safe(p, end, map->pool_max, e_inval); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1141 | |
Ilya Dryomov | 597b52f | 2014-03-13 16:36:14 +0200 | [diff] [blame] | 1142 | ceph_decode_32_safe(p, end, map->flags, e_inval); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1143 | |
Ilya Dryomov | 3977058 | 2014-03-13 16:36:14 +0200 | [diff] [blame] | 1144 | /* max_osd */ |
| 1145 | ceph_decode_32_safe(p, end, max, e_inval); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1146 | |
| 1147 | /* (re)alloc osd arrays */ |
| 1148 | err = osdmap_set_max_osd(map, max); |
Ilya Dryomov | 597b52f | 2014-03-13 16:36:14 +0200 | [diff] [blame] | 1149 | if (err) |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1150 | goto bad; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1151 | |
Ilya Dryomov | 2d88b2e | 2014-03-13 16:36:14 +0200 | [diff] [blame] | 1152 | /* osd_state, osd_weight, osd_addrs->client_addr */ |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1153 | ceph_decode_need(p, end, 3*sizeof(u32) + |
| 1154 | map->max_osd*(1 + sizeof(*map->osd_weight) + |
Ilya Dryomov | 597b52f | 2014-03-13 16:36:14 +0200 | [diff] [blame] | 1155 | sizeof(*map->osd_addr)), e_inval); |
| 1156 | |
Ilya Dryomov | 2d88b2e | 2014-03-13 16:36:14 +0200 | [diff] [blame] | 1157 | if (ceph_decode_32(p) != map->max_osd) |
| 1158 | goto e_inval; |
| 1159 | |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1160 | ceph_decode_copy(p, map->osd_state, map->max_osd); |
| 1161 | |
Ilya Dryomov | 2d88b2e | 2014-03-13 16:36:14 +0200 | [diff] [blame] | 1162 | if (ceph_decode_32(p) != map->max_osd) |
| 1163 | goto e_inval; |
| 1164 | |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1165 | for (i = 0; i < map->max_osd; i++) |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 1166 | map->osd_weight[i] = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1167 | |
Ilya Dryomov | 2d88b2e | 2014-03-13 16:36:14 +0200 | [diff] [blame] | 1168 | if (ceph_decode_32(p) != map->max_osd) |
| 1169 | goto e_inval; |
| 1170 | |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1171 | 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] | 1172 | for (i = 0; i < map->max_osd; i++) |
| 1173 | ceph_decode_addr(&map->osd_addr[i]); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1174 | |
| 1175 | /* pg_temp */ |
Ilya Dryomov | 10db634 | 2014-03-21 19:05:28 +0200 | [diff] [blame] | 1176 | err = decode_pg_temp(p, end, map); |
| 1177 | if (err) |
| 1178 | goto bad; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1179 | |
Ilya Dryomov | d286de7 | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 1180 | /* primary_temp */ |
| 1181 | if (struct_v >= 1) { |
| 1182 | err = decode_primary_temp(p, end, map); |
| 1183 | if (err) |
| 1184 | goto bad; |
| 1185 | } |
| 1186 | |
Ilya Dryomov | 63a6993 | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 1187 | /* primary_affinity */ |
| 1188 | if (struct_v >= 2) { |
| 1189 | err = decode_primary_affinity(p, end, map); |
| 1190 | if (err) |
| 1191 | goto bad; |
| 1192 | } else { |
| 1193 | /* XXX can this happen? */ |
| 1194 | kfree(map->osd_primary_affinity); |
| 1195 | map->osd_primary_affinity = NULL; |
| 1196 | } |
| 1197 | |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1198 | /* crush */ |
Ilya Dryomov | 597b52f | 2014-03-13 16:36:14 +0200 | [diff] [blame] | 1199 | ceph_decode_32_safe(p, end, len, e_inval); |
Ilya Dryomov | 9902e68 | 2014-03-13 16:36:15 +0200 | [diff] [blame] | 1200 | map->crush = crush_decode(*p, min(*p + len, end)); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1201 | if (IS_ERR(map->crush)) { |
| 1202 | err = PTR_ERR(map->crush); |
| 1203 | map->crush = NULL; |
| 1204 | goto bad; |
| 1205 | } |
Ilya Dryomov | 9902e68 | 2014-03-13 16:36:15 +0200 | [diff] [blame] | 1206 | *p += len; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1207 | |
Ilya Dryomov | 38a8d56 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1208 | /* ignore the rest */ |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1209 | *p = end; |
| 1210 | |
Ilya Dryomov | 38a8d56 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1211 | 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] | 1212 | return 0; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1213 | |
Ilya Dryomov | 597b52f | 2014-03-13 16:36:14 +0200 | [diff] [blame] | 1214 | e_inval: |
| 1215 | err = -EINVAL; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1216 | bad: |
Ilya Dryomov | 38a8d56 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1217 | pr_err("corrupt full osdmap (%d) epoch %d off %d (%p of %p-%p)\n", |
| 1218 | err, epoch, (int)(*p - start), *p, start, end); |
| 1219 | print_hex_dump(KERN_DEBUG, "osdmap: ", |
| 1220 | DUMP_PREFIX_OFFSET, 16, 1, |
| 1221 | start, end - start, true); |
Ilya Dryomov | a2505d6 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1222 | return err; |
| 1223 | } |
| 1224 | |
| 1225 | /* |
| 1226 | * Allocate and decode a full map. |
| 1227 | */ |
| 1228 | struct ceph_osdmap *ceph_osdmap_decode(void **p, void *end) |
| 1229 | { |
| 1230 | struct ceph_osdmap *map; |
| 1231 | int ret; |
| 1232 | |
| 1233 | map = kzalloc(sizeof(*map), GFP_NOFS); |
| 1234 | if (!map) |
| 1235 | return ERR_PTR(-ENOMEM); |
| 1236 | |
| 1237 | map->pg_temp = RB_ROOT; |
Ilya Dryomov | 9686f94 | 2014-03-21 19:05:29 +0200 | [diff] [blame] | 1238 | map->primary_temp = RB_ROOT; |
Ilya Dryomov | a2505d6 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1239 | mutex_init(&map->crush_scratch_mutex); |
| 1240 | |
| 1241 | ret = osdmap_decode(p, end, map); |
| 1242 | if (ret) { |
| 1243 | ceph_osdmap_destroy(map); |
| 1244 | return ERR_PTR(ret); |
| 1245 | } |
| 1246 | |
| 1247 | return map; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1248 | } |
| 1249 | |
| 1250 | /* |
| 1251 | * decode and apply an incremental map update. |
| 1252 | */ |
| 1253 | struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end, |
Ilya Dryomov | 0c0a8de | 2016-04-28 16:07:21 +0200 | [diff] [blame] | 1254 | struct ceph_osdmap *map) |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1255 | { |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1256 | struct crush_map *newcrush = NULL; |
| 1257 | struct ceph_fsid fsid; |
| 1258 | u32 epoch = 0; |
| 1259 | struct ceph_timespec modified; |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 1260 | s32 len; |
| 1261 | u64 pool; |
| 1262 | __s64 new_pool_max; |
| 1263 | __s32 new_flags, max; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1264 | void *start = *p; |
Ilya Dryomov | 86f1742 | 2014-03-13 16:36:15 +0200 | [diff] [blame] | 1265 | int err; |
Ilya Dryomov | ec7af97 | 2014-03-21 19:05:29 +0200 | [diff] [blame] | 1266 | u8 struct_v; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1267 | |
Ilya Dryomov | 38a8d56 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1268 | dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p)); |
| 1269 | |
Ilya Dryomov | ec7af97 | 2014-03-21 19:05:29 +0200 | [diff] [blame] | 1270 | err = get_osdmap_client_data_v(p, end, "inc", &struct_v); |
| 1271 | if (err) |
| 1272 | goto bad; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1273 | |
Ilya Dryomov | 53bbaba | 2014-03-13 16:36:16 +0200 | [diff] [blame] | 1274 | /* fsid, epoch, modified, new_pool_max, new_flags */ |
| 1275 | ceph_decode_need(p, end, sizeof(fsid) + sizeof(u32) + sizeof(modified) + |
| 1276 | sizeof(u64) + sizeof(u32), e_inval); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1277 | ceph_decode_copy(p, &fsid, sizeof(fsid)); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 1278 | epoch = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1279 | BUG_ON(epoch != map->epoch+1); |
| 1280 | ceph_decode_copy(p, &modified, sizeof(modified)); |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 1281 | new_pool_max = ceph_decode_64(p); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 1282 | new_flags = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1283 | |
| 1284 | /* full map? */ |
Ilya Dryomov | 86f1742 | 2014-03-13 16:36:15 +0200 | [diff] [blame] | 1285 | ceph_decode_32_safe(p, end, len, e_inval); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1286 | if (len > 0) { |
| 1287 | dout("apply_incremental full map len %d, %p to %p\n", |
| 1288 | len, *p, end); |
Ilya Dryomov | a2505d6 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1289 | return ceph_osdmap_decode(p, min(*p+len, end)); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1290 | } |
| 1291 | |
| 1292 | /* new crush? */ |
Ilya Dryomov | 86f1742 | 2014-03-13 16:36:15 +0200 | [diff] [blame] | 1293 | ceph_decode_32_safe(p, end, len, e_inval); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1294 | if (len > 0) { |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1295 | newcrush = crush_decode(*p, min(*p+len, end)); |
Ilya Dryomov | 86f1742 | 2014-03-13 16:36:15 +0200 | [diff] [blame] | 1296 | if (IS_ERR(newcrush)) { |
| 1297 | err = PTR_ERR(newcrush); |
| 1298 | newcrush = NULL; |
| 1299 | goto bad; |
| 1300 | } |
Sage Weil | cebc5be | 2010-06-17 10:22:48 -0700 | [diff] [blame] | 1301 | *p += len; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | /* new flags? */ |
| 1305 | if (new_flags >= 0) |
| 1306 | map->flags = new_flags; |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 1307 | if (new_pool_max >= 0) |
| 1308 | map->pool_max = new_pool_max; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1309 | |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1310 | /* new max? */ |
Ilya Dryomov | 53bbaba | 2014-03-13 16:36:16 +0200 | [diff] [blame] | 1311 | ceph_decode_32_safe(p, end, max, e_inval); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1312 | if (max >= 0) { |
| 1313 | err = osdmap_set_max_osd(map, max); |
Ilya Dryomov | 86f1742 | 2014-03-13 16:36:15 +0200 | [diff] [blame] | 1314 | if (err) |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1315 | goto bad; |
| 1316 | } |
| 1317 | |
| 1318 | map->epoch++; |
Sage Weil | 3145666 | 2011-05-12 15:18:43 -0700 | [diff] [blame] | 1319 | map->modified = modified; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1320 | if (newcrush) { |
| 1321 | if (map->crush) |
| 1322 | crush_destroy(map->crush); |
| 1323 | map->crush = newcrush; |
| 1324 | newcrush = NULL; |
| 1325 | } |
| 1326 | |
Ilya Dryomov | 433fbdd | 2014-03-21 19:05:27 +0200 | [diff] [blame] | 1327 | /* new_pools */ |
| 1328 | err = decode_new_pools(p, end, map); |
| 1329 | if (err) |
| 1330 | goto bad; |
Ilya Dryomov | 9464d00 | 2014-03-13 16:36:16 +0200 | [diff] [blame] | 1331 | |
Ilya Dryomov | 0f70c7e | 2014-03-21 19:05:27 +0200 | [diff] [blame] | 1332 | /* new_pool_names */ |
| 1333 | err = decode_pool_names(p, end, map); |
Ilya Dryomov | 9464d00 | 2014-03-13 16:36:16 +0200 | [diff] [blame] | 1334 | if (err) |
| 1335 | goto bad; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1336 | |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 1337 | /* old_pool */ |
Ilya Dryomov | 86f1742 | 2014-03-13 16:36:15 +0200 | [diff] [blame] | 1338 | ceph_decode_32_safe(p, end, len, e_inval); |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 1339 | while (len--) { |
| 1340 | struct ceph_pg_pool_info *pi; |
| 1341 | |
Ilya Dryomov | 86f1742 | 2014-03-13 16:36:15 +0200 | [diff] [blame] | 1342 | ceph_decode_64_safe(p, end, pool, e_inval); |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 1343 | pi = __lookup_pg_pool(&map->pg_pools, pool); |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 1344 | if (pi) |
| 1345 | __remove_pg_pool(&map->pg_pools, pi); |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 1346 | } |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1347 | |
| 1348 | /* new_up */ |
Ilya Dryomov | 86f1742 | 2014-03-13 16:36:15 +0200 | [diff] [blame] | 1349 | ceph_decode_32_safe(p, end, len, e_inval); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1350 | while (len--) { |
| 1351 | u32 osd; |
| 1352 | struct ceph_entity_addr addr; |
Ilya Dryomov | 86f1742 | 2014-03-13 16:36:15 +0200 | [diff] [blame] | 1353 | ceph_decode_32_safe(p, end, osd, e_inval); |
| 1354 | ceph_decode_copy_safe(p, end, &addr, sizeof(addr), e_inval); |
Sage Weil | 63f2d21 | 2009-11-03 15:17:56 -0800 | [diff] [blame] | 1355 | ceph_decode_addr(&addr); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1356 | pr_info("osd%d up\n", osd); |
| 1357 | BUG_ON(osd >= map->max_osd); |
Yan, Zheng | 6dd74e4 | 2015-08-28 17:59:35 +0800 | [diff] [blame] | 1358 | map->osd_state[osd] |= CEPH_OSD_UP | CEPH_OSD_EXISTS; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1359 | map->osd_addr[osd] = addr; |
| 1360 | } |
| 1361 | |
Sage Weil | 7662d8f | 2011-05-03 12:52:05 -0700 | [diff] [blame] | 1362 | /* new_state */ |
Ilya Dryomov | 86f1742 | 2014-03-13 16:36:15 +0200 | [diff] [blame] | 1363 | ceph_decode_32_safe(p, end, len, e_inval); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1364 | while (len--) { |
| 1365 | u32 osd; |
Sage Weil | 7662d8f | 2011-05-03 12:52:05 -0700 | [diff] [blame] | 1366 | u8 xorstate; |
Ilya Dryomov | 86f1742 | 2014-03-13 16:36:15 +0200 | [diff] [blame] | 1367 | ceph_decode_32_safe(p, end, osd, e_inval); |
Sage Weil | 7662d8f | 2011-05-03 12:52:05 -0700 | [diff] [blame] | 1368 | xorstate = **(u8 **)p; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1369 | (*p)++; /* clean flag */ |
Sage Weil | 7662d8f | 2011-05-03 12:52:05 -0700 | [diff] [blame] | 1370 | if (xorstate == 0) |
| 1371 | xorstate = CEPH_OSD_UP; |
| 1372 | if (xorstate & CEPH_OSD_UP) |
| 1373 | pr_info("osd%d down\n", osd); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1374 | if (osd < map->max_osd) |
Sage Weil | 7662d8f | 2011-05-03 12:52:05 -0700 | [diff] [blame] | 1375 | map->osd_state[osd] ^= xorstate; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1376 | } |
| 1377 | |
| 1378 | /* new_weight */ |
Ilya Dryomov | 86f1742 | 2014-03-13 16:36:15 +0200 | [diff] [blame] | 1379 | ceph_decode_32_safe(p, end, len, e_inval); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1380 | while (len--) { |
| 1381 | u32 osd, off; |
Ilya Dryomov | 86f1742 | 2014-03-13 16:36:15 +0200 | [diff] [blame] | 1382 | ceph_decode_need(p, end, sizeof(u32)*2, e_inval); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 1383 | osd = ceph_decode_32(p); |
| 1384 | off = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1385 | pr_info("osd%d weight 0x%x %s\n", osd, off, |
| 1386 | off == CEPH_OSD_IN ? "(in)" : |
| 1387 | (off == CEPH_OSD_OUT ? "(out)" : "")); |
| 1388 | if (osd < map->max_osd) |
| 1389 | map->osd_weight[osd] = off; |
| 1390 | } |
| 1391 | |
| 1392 | /* new_pg_temp */ |
Ilya Dryomov | 10db634 | 2014-03-21 19:05:28 +0200 | [diff] [blame] | 1393 | err = decode_new_pg_temp(p, end, map); |
| 1394 | if (err) |
| 1395 | goto bad; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1396 | |
Ilya Dryomov | d286de7 | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 1397 | /* new_primary_temp */ |
| 1398 | if (struct_v >= 1) { |
| 1399 | err = decode_new_primary_temp(p, end, map); |
| 1400 | if (err) |
| 1401 | goto bad; |
| 1402 | } |
| 1403 | |
Ilya Dryomov | 63a6993 | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 1404 | /* new_primary_affinity */ |
| 1405 | if (struct_v >= 2) { |
| 1406 | err = decode_new_primary_affinity(p, end, map); |
| 1407 | if (err) |
| 1408 | goto bad; |
| 1409 | } |
| 1410 | |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1411 | /* ignore the rest */ |
| 1412 | *p = end; |
Ilya Dryomov | 38a8d56 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1413 | |
| 1414 | 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] | 1415 | return map; |
| 1416 | |
Ilya Dryomov | 86f1742 | 2014-03-13 16:36:15 +0200 | [diff] [blame] | 1417 | e_inval: |
| 1418 | err = -EINVAL; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1419 | bad: |
Ilya Dryomov | 38a8d56 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1420 | pr_err("corrupt inc osdmap (%d) epoch %d off %d (%p of %p-%p)\n", |
| 1421 | err, epoch, (int)(*p - start), *p, start, end); |
Sage Weil | 9ec7cab | 2009-12-14 15:13:47 -0800 | [diff] [blame] | 1422 | print_hex_dump(KERN_DEBUG, "osdmap: ", |
| 1423 | DUMP_PREFIX_OFFSET, 16, 1, |
| 1424 | start, end - start, true); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1425 | if (newcrush) |
| 1426 | crush_destroy(newcrush); |
| 1427 | return ERR_PTR(err); |
| 1428 | } |
| 1429 | |
Ilya Dryomov | d30291b | 2016-04-29 19:54:20 +0200 | [diff] [blame] | 1430 | void ceph_oid_copy(struct ceph_object_id *dest, |
| 1431 | const struct ceph_object_id *src) |
| 1432 | { |
| 1433 | WARN_ON(!ceph_oid_empty(dest)); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1434 | |
Ilya Dryomov | d30291b | 2016-04-29 19:54:20 +0200 | [diff] [blame] | 1435 | if (src->name != src->inline_name) { |
| 1436 | /* very rare, see ceph_object_id definition */ |
| 1437 | dest->name = kmalloc(src->name_len + 1, |
| 1438 | GFP_NOIO | __GFP_NOFAIL); |
| 1439 | } |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1440 | |
Ilya Dryomov | d30291b | 2016-04-29 19:54:20 +0200 | [diff] [blame] | 1441 | memcpy(dest->name, src->name, src->name_len + 1); |
| 1442 | dest->name_len = src->name_len; |
| 1443 | } |
| 1444 | EXPORT_SYMBOL(ceph_oid_copy); |
| 1445 | |
| 1446 | static __printf(2, 0) |
| 1447 | int oid_printf_vargs(struct ceph_object_id *oid, const char *fmt, va_list ap) |
| 1448 | { |
| 1449 | int len; |
| 1450 | |
| 1451 | WARN_ON(!ceph_oid_empty(oid)); |
| 1452 | |
| 1453 | len = vsnprintf(oid->inline_name, sizeof(oid->inline_name), fmt, ap); |
| 1454 | if (len >= sizeof(oid->inline_name)) |
| 1455 | return len; |
| 1456 | |
| 1457 | oid->name_len = len; |
| 1458 | return 0; |
| 1459 | } |
| 1460 | |
| 1461 | /* |
| 1462 | * If oid doesn't fit into inline buffer, BUG. |
| 1463 | */ |
| 1464 | void ceph_oid_printf(struct ceph_object_id *oid, const char *fmt, ...) |
| 1465 | { |
| 1466 | va_list ap; |
| 1467 | |
| 1468 | va_start(ap, fmt); |
| 1469 | BUG_ON(oid_printf_vargs(oid, fmt, ap)); |
| 1470 | va_end(ap); |
| 1471 | } |
| 1472 | EXPORT_SYMBOL(ceph_oid_printf); |
| 1473 | |
| 1474 | static __printf(3, 0) |
| 1475 | int oid_aprintf_vargs(struct ceph_object_id *oid, gfp_t gfp, |
| 1476 | const char *fmt, va_list ap) |
| 1477 | { |
| 1478 | va_list aq; |
| 1479 | int len; |
| 1480 | |
| 1481 | va_copy(aq, ap); |
| 1482 | len = oid_printf_vargs(oid, fmt, aq); |
| 1483 | va_end(aq); |
| 1484 | |
| 1485 | if (len) { |
| 1486 | char *external_name; |
| 1487 | |
| 1488 | external_name = kmalloc(len + 1, gfp); |
| 1489 | if (!external_name) |
| 1490 | return -ENOMEM; |
| 1491 | |
| 1492 | oid->name = external_name; |
| 1493 | WARN_ON(vsnprintf(oid->name, len + 1, fmt, ap) != len); |
| 1494 | oid->name_len = len; |
| 1495 | } |
| 1496 | |
| 1497 | return 0; |
| 1498 | } |
| 1499 | |
| 1500 | /* |
| 1501 | * If oid doesn't fit into inline buffer, allocate. |
| 1502 | */ |
| 1503 | int ceph_oid_aprintf(struct ceph_object_id *oid, gfp_t gfp, |
| 1504 | const char *fmt, ...) |
| 1505 | { |
| 1506 | va_list ap; |
| 1507 | int ret; |
| 1508 | |
| 1509 | va_start(ap, fmt); |
| 1510 | ret = oid_aprintf_vargs(oid, gfp, fmt, ap); |
| 1511 | va_end(ap); |
| 1512 | |
| 1513 | return ret; |
| 1514 | } |
| 1515 | EXPORT_SYMBOL(ceph_oid_aprintf); |
| 1516 | |
| 1517 | void ceph_oid_destroy(struct ceph_object_id *oid) |
| 1518 | { |
| 1519 | if (oid->name != oid->inline_name) |
| 1520 | kfree(oid->name); |
| 1521 | } |
| 1522 | EXPORT_SYMBOL(ceph_oid_destroy); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1523 | |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1524 | static bool osds_valid(const struct ceph_osds *set) |
| 1525 | { |
| 1526 | /* non-empty set */ |
| 1527 | if (set->size > 0 && set->primary >= 0) |
| 1528 | return true; |
| 1529 | |
| 1530 | /* empty can_shift_osds set */ |
| 1531 | if (!set->size && set->primary == -1) |
| 1532 | return true; |
| 1533 | |
| 1534 | /* empty !can_shift_osds set - all NONE */ |
| 1535 | if (set->size > 0 && set->primary == -1) { |
| 1536 | int i; |
| 1537 | |
| 1538 | for (i = 0; i < set->size; i++) { |
| 1539 | if (set->osds[i] != CRUSH_ITEM_NONE) |
| 1540 | break; |
| 1541 | } |
| 1542 | if (i == set->size) |
| 1543 | return true; |
| 1544 | } |
| 1545 | |
| 1546 | return false; |
| 1547 | } |
| 1548 | |
| 1549 | void ceph_osds_copy(struct ceph_osds *dest, const struct ceph_osds *src) |
| 1550 | { |
| 1551 | memcpy(dest->osds, src->osds, src->size * sizeof(src->osds[0])); |
| 1552 | dest->size = src->size; |
| 1553 | dest->primary = src->primary; |
| 1554 | } |
| 1555 | |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1556 | /* |
| 1557 | * calculate file layout from given offset, length. |
| 1558 | * fill in correct oid, logical length, and object extent |
| 1559 | * offset, length. |
| 1560 | * |
| 1561 | * for now, we write only a single su, until we can |
| 1562 | * pass a stride back to the caller. |
| 1563 | */ |
Sage Weil | d63b77f | 2012-09-24 20:59:48 -0700 | [diff] [blame] | 1564 | int ceph_calc_file_object_mapping(struct ceph_file_layout *layout, |
Alex Elder | e8afad6 | 2012-11-14 09:38:19 -0600 | [diff] [blame] | 1565 | u64 off, u64 len, |
Sage Weil | 645a102 | 2009-10-28 15:15:05 -0700 | [diff] [blame] | 1566 | u64 *ono, |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1567 | u64 *oxoff, u64 *oxlen) |
| 1568 | { |
| 1569 | u32 osize = le32_to_cpu(layout->fl_object_size); |
| 1570 | u32 su = le32_to_cpu(layout->fl_stripe_unit); |
| 1571 | u32 sc = le32_to_cpu(layout->fl_stripe_count); |
| 1572 | u32 bl, stripeno, stripepos, objsetno; |
| 1573 | u32 su_per_object; |
Noah Watkins | ff1d1f7 | 2009-10-30 12:57:30 -0700 | [diff] [blame] | 1574 | u64 t, su_offset; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1575 | |
Alex Elder | e8afad6 | 2012-11-14 09:38:19 -0600 | [diff] [blame] | 1576 | dout("mapping %llu~%llu osize %u fl_su %u\n", off, len, |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1577 | osize, su); |
Sage Weil | d63b77f | 2012-09-24 20:59:48 -0700 | [diff] [blame] | 1578 | if (su == 0 || sc == 0) |
| 1579 | goto invalid; |
Noah Watkins | 35e054a | 2009-10-28 14:04:48 -0700 | [diff] [blame] | 1580 | su_per_object = osize / su; |
Sage Weil | d63b77f | 2012-09-24 20:59:48 -0700 | [diff] [blame] | 1581 | if (su_per_object == 0) |
| 1582 | goto invalid; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1583 | dout("osize %u / su %u = su_per_object %u\n", osize, su, |
| 1584 | su_per_object); |
| 1585 | |
Sage Weil | d63b77f | 2012-09-24 20:59:48 -0700 | [diff] [blame] | 1586 | if ((su & ~PAGE_MASK) != 0) |
| 1587 | goto invalid; |
| 1588 | |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1589 | /* bl = *off / su; */ |
| 1590 | t = off; |
| 1591 | do_div(t, su); |
| 1592 | bl = t; |
| 1593 | dout("off %llu / su %u = bl %u\n", off, su, bl); |
| 1594 | |
| 1595 | stripeno = bl / sc; |
| 1596 | stripepos = bl % sc; |
| 1597 | objsetno = stripeno / su_per_object; |
| 1598 | |
Sage Weil | 645a102 | 2009-10-28 15:15:05 -0700 | [diff] [blame] | 1599 | *ono = objsetno * sc + stripepos; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 1600 | 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] | 1601 | |
| 1602 | /* *oxoff = *off % layout->fl_stripe_unit; # offset in su */ |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1603 | t = off; |
Noah Watkins | ff1d1f7 | 2009-10-30 12:57:30 -0700 | [diff] [blame] | 1604 | su_offset = do_div(t, su); |
| 1605 | *oxoff = su_offset + (stripeno % su_per_object) * su; |
Sage Weil | 645a102 | 2009-10-28 15:15:05 -0700 | [diff] [blame] | 1606 | |
Noah Watkins | ff1d1f7 | 2009-10-30 12:57:30 -0700 | [diff] [blame] | 1607 | /* |
| 1608 | * Calculate the length of the extent being written to the selected |
Alex Elder | e8afad6 | 2012-11-14 09:38:19 -0600 | [diff] [blame] | 1609 | * object. This is the minimum of the full length requested (len) or |
Noah Watkins | ff1d1f7 | 2009-10-30 12:57:30 -0700 | [diff] [blame] | 1610 | * the remainder of the current stripe being written to. |
| 1611 | */ |
Alex Elder | e8afad6 | 2012-11-14 09:38:19 -0600 | [diff] [blame] | 1612 | *oxlen = min_t(u64, len, su - su_offset); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1613 | |
| 1614 | dout(" obj extent %llu~%llu\n", *oxoff, *oxlen); |
Sage Weil | d63b77f | 2012-09-24 20:59:48 -0700 | [diff] [blame] | 1615 | return 0; |
| 1616 | |
| 1617 | invalid: |
| 1618 | dout(" invalid layout\n"); |
| 1619 | *ono = 0; |
| 1620 | *oxoff = 0; |
| 1621 | *oxlen = 0; |
| 1622 | return -EINVAL; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1623 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1624 | EXPORT_SYMBOL(ceph_calc_file_object_mapping); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1625 | |
| 1626 | /* |
Ilya Dryomov | d9591f5 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1627 | * Map an object into a PG. |
| 1628 | * |
| 1629 | * Should only be called with target_oid and target_oloc (as opposed to |
| 1630 | * base_oid and base_oloc), since tiering isn't taken into account. |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1631 | */ |
Ilya Dryomov | d9591f5 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1632 | int ceph_object_locator_to_pg(struct ceph_osdmap *osdmap, |
| 1633 | struct ceph_object_id *oid, |
| 1634 | struct ceph_object_locator *oloc, |
| 1635 | struct ceph_pg *raw_pgid) |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1636 | { |
Ilya Dryomov | 7c13cb6 | 2014-01-27 17:40:19 +0200 | [diff] [blame] | 1637 | struct ceph_pg_pool_info *pi; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1638 | |
Ilya Dryomov | d9591f5 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1639 | pi = ceph_pg_pool_by_id(osdmap, oloc->pool); |
Ilya Dryomov | 7c13cb6 | 2014-01-27 17:40:19 +0200 | [diff] [blame] | 1640 | if (!pi) |
Ilya Dryomov | d9591f5 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1641 | return -ENOENT; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1642 | |
Ilya Dryomov | d9591f5 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1643 | raw_pgid->pool = oloc->pool; |
| 1644 | raw_pgid->seed = ceph_str_hash(pi->object_hash, oid->name, |
| 1645 | oid->name_len); |
Ilya Dryomov | 7c13cb6 | 2014-01-27 17:40:19 +0200 | [diff] [blame] | 1646 | |
Ilya Dryomov | d9591f5 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1647 | dout("%s %*pE -> raw_pgid %llu.%x\n", __func__, oid->name_len, |
| 1648 | oid->name, raw_pgid->pool, raw_pgid->seed); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1649 | return 0; |
| 1650 | } |
Ilya Dryomov | d9591f5 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1651 | EXPORT_SYMBOL(ceph_object_locator_to_pg); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1652 | |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1653 | /* |
| 1654 | * Map a raw PG (full precision ps) into an actual PG. |
| 1655 | */ |
| 1656 | static void raw_pg_to_pg(struct ceph_pg_pool_info *pi, |
| 1657 | const struct ceph_pg *raw_pgid, |
| 1658 | struct ceph_pg *pgid) |
| 1659 | { |
| 1660 | pgid->pool = raw_pgid->pool; |
| 1661 | pgid->seed = ceph_stable_mod(raw_pgid->seed, pi->pg_num, |
| 1662 | pi->pg_num_mask); |
| 1663 | } |
| 1664 | |
| 1665 | /* |
| 1666 | * Map a raw PG (full precision ps) into a placement ps (placement |
| 1667 | * seed). Include pool id in that value so that different pools don't |
| 1668 | * use the same seeds. |
| 1669 | */ |
| 1670 | static u32 raw_pg_to_pps(struct ceph_pg_pool_info *pi, |
| 1671 | const struct ceph_pg *raw_pgid) |
| 1672 | { |
| 1673 | if (pi->flags & CEPH_POOL_FLAG_HASHPSPOOL) { |
| 1674 | /* hash pool id and seed so that pool PGs do not overlap */ |
| 1675 | return crush_hash32_2(CRUSH_HASH_RJENKINS1, |
| 1676 | ceph_stable_mod(raw_pgid->seed, |
| 1677 | pi->pgp_num, |
| 1678 | pi->pgp_num_mask), |
| 1679 | raw_pgid->pool); |
| 1680 | } else { |
| 1681 | /* |
| 1682 | * legacy behavior: add ps and pool together. this is |
| 1683 | * not a great approach because the PGs from each pool |
| 1684 | * will overlap on top of each other: 0.5 == 1.4 == |
| 1685 | * 2.3 == ... |
| 1686 | */ |
| 1687 | return ceph_stable_mod(raw_pgid->seed, pi->pgp_num, |
| 1688 | pi->pgp_num_mask) + |
| 1689 | (unsigned)raw_pgid->pool; |
| 1690 | } |
| 1691 | } |
| 1692 | |
Ilya Dryomov | 9d52147 | 2014-01-31 17:54:26 +0200 | [diff] [blame] | 1693 | static int do_crush(struct ceph_osdmap *map, int ruleno, int x, |
| 1694 | int *result, int result_max, |
| 1695 | const __u32 *weight, int weight_max) |
Ilya Dryomov | e8ef19c | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 1696 | { |
Ilya Dryomov | 9d52147 | 2014-01-31 17:54:26 +0200 | [diff] [blame] | 1697 | int r; |
Ilya Dryomov | e8ef19c | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 1698 | |
Ilya Dryomov | 9d52147 | 2014-01-31 17:54:26 +0200 | [diff] [blame] | 1699 | BUG_ON(result_max > CEPH_PG_MAX_SIZE); |
| 1700 | |
| 1701 | mutex_lock(&map->crush_scratch_mutex); |
| 1702 | r = crush_do_rule(map->crush, ruleno, x, result, result_max, |
| 1703 | weight, weight_max, map->crush_scratch_ary); |
| 1704 | mutex_unlock(&map->crush_scratch_mutex); |
| 1705 | |
| 1706 | return r; |
Ilya Dryomov | e8ef19c | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 1707 | } |
| 1708 | |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1709 | /* |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1710 | * Calculate raw set (CRUSH output) for given PG. The result may |
| 1711 | * contain nonexistent OSDs. ->primary is undefined for a raw set. |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 1712 | * |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1713 | * Placement seed (CRUSH input) is returned through @ppps. |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 1714 | */ |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1715 | static void pg_to_raw_osds(struct ceph_osdmap *osdmap, |
| 1716 | struct ceph_pg_pool_info *pi, |
| 1717 | const struct ceph_pg *raw_pgid, |
| 1718 | struct ceph_osds *raw, |
| 1719 | u32 *ppps) |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 1720 | { |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1721 | u32 pps = raw_pg_to_pps(pi, raw_pgid); |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 1722 | int ruleno; |
| 1723 | int len; |
| 1724 | |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1725 | ceph_osds_init(raw); |
| 1726 | if (ppps) |
| 1727 | *ppps = pps; |
| 1728 | |
| 1729 | ruleno = crush_find_rule(osdmap->crush, pi->crush_ruleset, pi->type, |
| 1730 | pi->size); |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 1731 | if (ruleno < 0) { |
| 1732 | pr_err("no crush rule: pool %lld ruleset %d type %d size %d\n", |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1733 | pi->id, pi->crush_ruleset, pi->type, pi->size); |
| 1734 | return; |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 1735 | } |
| 1736 | |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1737 | len = do_crush(osdmap, ruleno, pps, raw->osds, |
| 1738 | min_t(int, pi->size, ARRAY_SIZE(raw->osds)), |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 1739 | osdmap->osd_weight, osdmap->max_osd); |
| 1740 | if (len < 0) { |
| 1741 | pr_err("error %d from crush rule %d: pool %lld ruleset %d type %d size %d\n", |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1742 | len, ruleno, pi->id, pi->crush_ruleset, pi->type, |
| 1743 | pi->size); |
| 1744 | return; |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 1745 | } |
| 1746 | |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1747 | raw->size = len; |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 1748 | } |
| 1749 | |
| 1750 | /* |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1751 | * Given raw set, calculate up set and up primary. By definition of an |
| 1752 | * up set, the result won't contain nonexistent or down OSDs. |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 1753 | * |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1754 | * This is done in-place - on return @set is the up set. If it's |
| 1755 | * empty, ->primary will remain undefined. |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 1756 | */ |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1757 | static void raw_to_up_osds(struct ceph_osdmap *osdmap, |
| 1758 | struct ceph_pg_pool_info *pi, |
| 1759 | struct ceph_osds *set) |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 1760 | { |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 1761 | int i; |
| 1762 | |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1763 | /* ->primary is undefined for a raw set */ |
| 1764 | BUG_ON(set->primary != -1); |
| 1765 | |
| 1766 | if (ceph_can_shift_osds(pi)) { |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 1767 | int removed = 0; |
| 1768 | |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1769 | /* shift left */ |
| 1770 | for (i = 0; i < set->size; i++) { |
| 1771 | if (ceph_osd_is_down(osdmap, set->osds[i])) { |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 1772 | removed++; |
| 1773 | continue; |
| 1774 | } |
| 1775 | if (removed) |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1776 | set->osds[i - removed] = set->osds[i]; |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 1777 | } |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1778 | set->size -= removed; |
| 1779 | if (set->size > 0) |
| 1780 | set->primary = set->osds[0]; |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 1781 | } else { |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1782 | /* set down/dne devices to NONE */ |
| 1783 | for (i = set->size - 1; i >= 0; i--) { |
| 1784 | if (ceph_osd_is_down(osdmap, set->osds[i])) |
| 1785 | set->osds[i] = CRUSH_ITEM_NONE; |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 1786 | else |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1787 | set->primary = set->osds[i]; |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 1788 | } |
| 1789 | } |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 1790 | } |
| 1791 | |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1792 | static void apply_primary_affinity(struct ceph_osdmap *osdmap, |
| 1793 | struct ceph_pg_pool_info *pi, |
| 1794 | u32 pps, |
| 1795 | struct ceph_osds *up) |
Ilya Dryomov | 47ec1f3 | 2014-03-24 17:12:49 +0200 | [diff] [blame] | 1796 | { |
| 1797 | int i; |
| 1798 | int pos = -1; |
| 1799 | |
| 1800 | /* |
| 1801 | * Do we have any non-default primary_affinity values for these |
| 1802 | * osds? |
| 1803 | */ |
| 1804 | if (!osdmap->osd_primary_affinity) |
| 1805 | return; |
| 1806 | |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1807 | for (i = 0; i < up->size; i++) { |
| 1808 | int osd = up->osds[i]; |
Ilya Dryomov | 92b2e75 | 2014-04-10 18:09:41 +0400 | [diff] [blame] | 1809 | |
| 1810 | if (osd != CRUSH_ITEM_NONE && |
| 1811 | osdmap->osd_primary_affinity[osd] != |
Ilya Dryomov | 47ec1f3 | 2014-03-24 17:12:49 +0200 | [diff] [blame] | 1812 | CEPH_OSD_DEFAULT_PRIMARY_AFFINITY) { |
| 1813 | break; |
| 1814 | } |
| 1815 | } |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1816 | if (i == up->size) |
Ilya Dryomov | 47ec1f3 | 2014-03-24 17:12:49 +0200 | [diff] [blame] | 1817 | return; |
| 1818 | |
| 1819 | /* |
| 1820 | * Pick the primary. Feed both the seed (for the pg) and the |
| 1821 | * osd into the hash/rng so that a proportional fraction of an |
| 1822 | * osd's pgs get rejected as primary. |
| 1823 | */ |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1824 | for (i = 0; i < up->size; i++) { |
| 1825 | int osd = up->osds[i]; |
Ilya Dryomov | 47ec1f3 | 2014-03-24 17:12:49 +0200 | [diff] [blame] | 1826 | u32 aff; |
| 1827 | |
Ilya Dryomov | 47ec1f3 | 2014-03-24 17:12:49 +0200 | [diff] [blame] | 1828 | if (osd == CRUSH_ITEM_NONE) |
| 1829 | continue; |
| 1830 | |
| 1831 | aff = osdmap->osd_primary_affinity[osd]; |
| 1832 | if (aff < CEPH_OSD_MAX_PRIMARY_AFFINITY && |
| 1833 | (crush_hash32_2(CRUSH_HASH_RJENKINS1, |
| 1834 | pps, osd) >> 16) >= aff) { |
| 1835 | /* |
| 1836 | * We chose not to use this primary. Note it |
| 1837 | * anyway as a fallback in case we don't pick |
| 1838 | * anyone else, but keep looking. |
| 1839 | */ |
| 1840 | if (pos < 0) |
| 1841 | pos = i; |
| 1842 | } else { |
| 1843 | pos = i; |
| 1844 | break; |
| 1845 | } |
| 1846 | } |
| 1847 | if (pos < 0) |
| 1848 | return; |
| 1849 | |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1850 | up->primary = up->osds[pos]; |
Ilya Dryomov | 47ec1f3 | 2014-03-24 17:12:49 +0200 | [diff] [blame] | 1851 | |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1852 | if (ceph_can_shift_osds(pi) && pos > 0) { |
Ilya Dryomov | 47ec1f3 | 2014-03-24 17:12:49 +0200 | [diff] [blame] | 1853 | /* move the new primary to the front */ |
| 1854 | for (i = pos; i > 0; i--) |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1855 | up->osds[i] = up->osds[i - 1]; |
| 1856 | up->osds[0] = up->primary; |
Ilya Dryomov | 47ec1f3 | 2014-03-24 17:12:49 +0200 | [diff] [blame] | 1857 | } |
| 1858 | } |
| 1859 | |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 1860 | /* |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1861 | * Get pg_temp and primary_temp mappings for given PG. |
Ilya Dryomov | 45966c3 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 1862 | * |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1863 | * Note that a PG may have none, only pg_temp, only primary_temp or |
| 1864 | * both pg_temp and primary_temp mappings. This means @temp isn't |
| 1865 | * always a valid OSD set on return: in the "only primary_temp" case, |
| 1866 | * @temp will have its ->primary >= 0 but ->size == 0. |
Ilya Dryomov | 45966c3 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 1867 | */ |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1868 | static void get_temp_osds(struct ceph_osdmap *osdmap, |
| 1869 | struct ceph_pg_pool_info *pi, |
| 1870 | const struct ceph_pg *raw_pgid, |
| 1871 | struct ceph_osds *temp) |
Ilya Dryomov | 45966c3 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 1872 | { |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1873 | struct ceph_pg pgid; |
Ilya Dryomov | 45966c3 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 1874 | struct ceph_pg_mapping *pg; |
Ilya Dryomov | 45966c3 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 1875 | int i; |
| 1876 | |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1877 | raw_pg_to_pg(pi, raw_pgid, &pgid); |
| 1878 | ceph_osds_init(temp); |
Ilya Dryomov | 45966c3 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 1879 | |
| 1880 | /* pg_temp? */ |
| 1881 | pg = __lookup_pg_mapping(&osdmap->pg_temp, pgid); |
| 1882 | if (pg) { |
Ilya Dryomov | 45966c3 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 1883 | for (i = 0; i < pg->pg_temp.len; i++) { |
| 1884 | if (ceph_osd_is_down(osdmap, pg->pg_temp.osds[i])) { |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1885 | if (ceph_can_shift_osds(pi)) |
Ilya Dryomov | 45966c3 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 1886 | continue; |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1887 | |
| 1888 | temp->osds[temp->size++] = CRUSH_ITEM_NONE; |
Ilya Dryomov | 45966c3 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 1889 | } else { |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1890 | temp->osds[temp->size++] = pg->pg_temp.osds[i]; |
Ilya Dryomov | 45966c3 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 1891 | } |
| 1892 | } |
| 1893 | |
| 1894 | /* apply pg_temp's primary */ |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1895 | for (i = 0; i < temp->size; i++) { |
| 1896 | if (temp->osds[i] != CRUSH_ITEM_NONE) { |
| 1897 | temp->primary = temp->osds[i]; |
Ilya Dryomov | 45966c3 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 1898 | break; |
| 1899 | } |
| 1900 | } |
Ilya Dryomov | 45966c3 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 1901 | } |
| 1902 | |
Ilya Dryomov | 5e8d4d3 | 2014-03-24 17:12:48 +0200 | [diff] [blame] | 1903 | /* primary_temp? */ |
| 1904 | pg = __lookup_pg_mapping(&osdmap->primary_temp, pgid); |
| 1905 | if (pg) |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1906 | temp->primary = pg->primary_temp.osd; |
Ilya Dryomov | 45966c3 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 1907 | } |
| 1908 | |
| 1909 | /* |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1910 | * Map a PG to its acting set as well as its up set. |
Ilya Dryomov | ac97223 | 2014-03-24 17:12:48 +0200 | [diff] [blame] | 1911 | * |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1912 | * Acting set is used for data mapping purposes, while up set can be |
| 1913 | * recorded for detecting interval changes and deciding whether to |
| 1914 | * resend a request. |
Sage Weil | d85b705 | 2010-05-10 10:24:48 -0700 | [diff] [blame] | 1915 | */ |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1916 | void ceph_pg_to_up_acting_osds(struct ceph_osdmap *osdmap, |
| 1917 | const struct ceph_pg *raw_pgid, |
| 1918 | struct ceph_osds *up, |
| 1919 | struct ceph_osds *acting) |
Sage Weil | d85b705 | 2010-05-10 10:24:48 -0700 | [diff] [blame] | 1920 | { |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1921 | struct ceph_pg_pool_info *pi; |
Ilya Dryomov | ac97223 | 2014-03-24 17:12:48 +0200 | [diff] [blame] | 1922 | u32 pps; |
Sage Weil | d85b705 | 2010-05-10 10:24:48 -0700 | [diff] [blame] | 1923 | |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1924 | pi = ceph_pg_pool_by_id(osdmap, raw_pgid->pool); |
| 1925 | if (!pi) { |
| 1926 | ceph_osds_init(up); |
| 1927 | ceph_osds_init(acting); |
| 1928 | goto out; |
Ilya Dryomov | 8008ab1 | 2014-03-24 17:12:48 +0200 | [diff] [blame] | 1929 | } |
Sage Weil | d85b705 | 2010-05-10 10:24:48 -0700 | [diff] [blame] | 1930 | |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1931 | pg_to_raw_osds(osdmap, pi, raw_pgid, up, &pps); |
| 1932 | raw_to_up_osds(osdmap, pi, up); |
| 1933 | apply_primary_affinity(osdmap, pi, pps, up); |
| 1934 | get_temp_osds(osdmap, pi, raw_pgid, acting); |
| 1935 | if (!acting->size) { |
| 1936 | memcpy(acting->osds, up->osds, up->size * sizeof(up->osds[0])); |
| 1937 | acting->size = up->size; |
| 1938 | if (acting->primary == -1) |
| 1939 | acting->primary = up->primary; |
Ilya Dryomov | ac97223 | 2014-03-24 17:12:48 +0200 | [diff] [blame] | 1940 | } |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1941 | out: |
| 1942 | WARN_ON(!osds_valid(up) || !osds_valid(acting)); |
Sage Weil | d85b705 | 2010-05-10 10:24:48 -0700 | [diff] [blame] | 1943 | } |
| 1944 | |
| 1945 | /* |
Ilya Dryomov | f81f163 | 2016-04-28 16:07:23 +0200 | [diff] [blame] | 1946 | * Return acting primary for given PG, or -1 if none. |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1947 | */ |
Ilya Dryomov | f81f163 | 2016-04-28 16:07:23 +0200 | [diff] [blame] | 1948 | int ceph_pg_to_acting_primary(struct ceph_osdmap *osdmap, |
| 1949 | const struct ceph_pg *raw_pgid) |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1950 | { |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1951 | struct ceph_osds up, acting; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1952 | |
Ilya Dryomov | f81f163 | 2016-04-28 16:07:23 +0200 | [diff] [blame] | 1953 | ceph_pg_to_up_acting_osds(osdmap, raw_pgid, &up, &acting); |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1954 | return acting.primary; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1955 | } |
Ilya Dryomov | f81f163 | 2016-04-28 16:07:23 +0200 | [diff] [blame] | 1956 | EXPORT_SYMBOL(ceph_pg_to_acting_primary); |