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