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