linux/net/ceph/osd_client.c
<<
>>
Prefs
   1#include <linux/ceph/ceph_debug.h>
   2
   3#include <linux/module.h>
   4#include <linux/err.h>
   5#include <linux/highmem.h>
   6#include <linux/mm.h>
   7#include <linux/pagemap.h>
   8#include <linux/slab.h>
   9#include <linux/uaccess.h>
  10#ifdef CONFIG_BLOCK
  11#include <linux/bio.h>
  12#endif
  13
  14#include <linux/ceph/libceph.h>
  15#include <linux/ceph/osd_client.h>
  16#include <linux/ceph/messenger.h>
  17#include <linux/ceph/decode.h>
  18#include <linux/ceph/auth.h>
  19#include <linux/ceph/pagelist.h>
  20
  21#define OSD_OP_FRONT_LEN        4096
  22#define OSD_OPREPLY_FRONT_LEN   512
  23
  24static const struct ceph_connection_operations osd_con_ops;
  25
  26static void send_queued(struct ceph_osd_client *osdc);
  27static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd);
  28static void __register_request(struct ceph_osd_client *osdc,
  29                               struct ceph_osd_request *req);
  30static void __unregister_linger_request(struct ceph_osd_client *osdc,
  31                                        struct ceph_osd_request *req);
  32static void __send_request(struct ceph_osd_client *osdc,
  33                           struct ceph_osd_request *req);
  34
  35static int op_needs_trail(int op)
  36{
  37        switch (op) {
  38        case CEPH_OSD_OP_GETXATTR:
  39        case CEPH_OSD_OP_SETXATTR:
  40        case CEPH_OSD_OP_CMPXATTR:
  41        case CEPH_OSD_OP_CALL:
  42        case CEPH_OSD_OP_NOTIFY:
  43                return 1;
  44        default:
  45                return 0;
  46        }
  47}
  48
  49static int op_has_extent(int op)
  50{
  51        return (op == CEPH_OSD_OP_READ ||
  52                op == CEPH_OSD_OP_WRITE);
  53}
  54
  55int ceph_calc_raw_layout(struct ceph_osd_client *osdc,
  56                        struct ceph_file_layout *layout,
  57                        u64 snapid,
  58                        u64 off, u64 *plen, u64 *bno,
  59                        struct ceph_osd_request *req,
  60                        struct ceph_osd_req_op *op)
  61{
  62        struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
  63        u64 orig_len = *plen;
  64        u64 objoff, objlen;    /* extent in object */
  65        int r;
  66
  67        reqhead->snapid = cpu_to_le64(snapid);
  68
  69        /* object extent? */
  70        r = ceph_calc_file_object_mapping(layout, off, plen, bno,
  71                                          &objoff, &objlen);
  72        if (r < 0)
  73                return r;
  74        if (*plen < orig_len)
  75                dout(" skipping last %llu, final file extent %llu~%llu\n",
  76                     orig_len - *plen, off, *plen);
  77
  78        if (op_has_extent(op->op)) {
  79                op->extent.offset = objoff;
  80                op->extent.length = objlen;
  81        }
  82        req->r_num_pages = calc_pages_for(off, *plen);
  83        req->r_page_alignment = off & ~PAGE_MASK;
  84        if (op->op == CEPH_OSD_OP_WRITE)
  85                op->payload_len = *plen;
  86
  87        dout("calc_layout bno=%llx %llu~%llu (%d pages)\n",
  88             *bno, objoff, objlen, req->r_num_pages);
  89        return 0;
  90}
  91EXPORT_SYMBOL(ceph_calc_raw_layout);
  92
  93/*
  94 * Implement client access to distributed object storage cluster.
  95 *
  96 * All data objects are stored within a cluster/cloud of OSDs, or
  97 * "object storage devices."  (Note that Ceph OSDs have _nothing_ to
  98 * do with the T10 OSD extensions to SCSI.)  Ceph OSDs are simply
  99 * remote daemons serving up and coordinating consistent and safe
 100 * access to storage.
 101 *
 102 * Cluster membership and the mapping of data objects onto storage devices
 103 * are described by the osd map.
 104 *
 105 * We keep track of pending OSD requests (read, write), resubmit
 106 * requests to different OSDs when the cluster topology/data layout
 107 * change, or retry the affected requests when the communications
 108 * channel with an OSD is reset.
 109 */
 110
 111/*
 112 * calculate the mapping of a file extent onto an object, and fill out the
 113 * request accordingly.  shorten extent as necessary if it crosses an
 114 * object boundary.
 115 *
 116 * fill osd op in request message.
 117 */
 118static int calc_layout(struct ceph_osd_client *osdc,
 119                       struct ceph_vino vino,
 120                       struct ceph_file_layout *layout,
 121                       u64 off, u64 *plen,
 122                       struct ceph_osd_request *req,
 123                       struct ceph_osd_req_op *op)
 124{
 125        u64 bno;
 126        int r;
 127
 128        r = ceph_calc_raw_layout(osdc, layout, vino.snap, off,
 129                                 plen, &bno, req, op);
 130        if (r < 0)
 131                return r;
 132
 133        snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx", vino.ino, bno);
 134        req->r_oid_len = strlen(req->r_oid);
 135
 136        return r;
 137}
 138
 139/*
 140 * requests
 141 */
 142void ceph_osdc_release_request(struct kref *kref)
 143{
 144        struct ceph_osd_request *req = container_of(kref,
 145                                                    struct ceph_osd_request,
 146                                                    r_kref);
 147
 148        if (req->r_request)
 149                ceph_msg_put(req->r_request);
 150        if (req->r_con_filling_msg) {
 151                dout("%s revoking pages %p from con %p\n", __func__,
 152                     req->r_pages, req->r_con_filling_msg);
 153                ceph_msg_revoke_incoming(req->r_reply);
 154                req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
 155        }
 156        if (req->r_reply)
 157                ceph_msg_put(req->r_reply);
 158        if (req->r_own_pages)
 159                ceph_release_page_vector(req->r_pages,
 160                                         req->r_num_pages);
 161#ifdef CONFIG_BLOCK
 162        if (req->r_bio)
 163                bio_put(req->r_bio);
 164#endif
 165        ceph_put_snap_context(req->r_snapc);
 166        if (req->r_trail) {
 167                ceph_pagelist_release(req->r_trail);
 168                kfree(req->r_trail);
 169        }
 170        if (req->r_mempool)
 171                mempool_free(req, req->r_osdc->req_mempool);
 172        else
 173                kfree(req);
 174}
 175EXPORT_SYMBOL(ceph_osdc_release_request);
 176
 177static int get_num_ops(struct ceph_osd_req_op *ops, int *needs_trail)
 178{
 179        int i = 0;
 180
 181        if (needs_trail)
 182                *needs_trail = 0;
 183        while (ops[i].op) {
 184                if (needs_trail && op_needs_trail(ops[i].op))
 185                        *needs_trail = 1;
 186                i++;
 187        }
 188
 189        return i;
 190}
 191
 192struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
 193                                               int flags,
 194                                               struct ceph_snap_context *snapc,
 195                                               struct ceph_osd_req_op *ops,
 196                                               bool use_mempool,
 197                                               gfp_t gfp_flags,
 198                                               struct page **pages,
 199                                               struct bio *bio)
 200{
 201        struct ceph_osd_request *req;
 202        struct ceph_msg *msg;
 203        int needs_trail;
 204        int num_op = get_num_ops(ops, &needs_trail);
 205        size_t msg_size = sizeof(struct ceph_osd_request_head);
 206
 207        msg_size += num_op*sizeof(struct ceph_osd_op);
 208
 209        if (use_mempool) {
 210                req = mempool_alloc(osdc->req_mempool, gfp_flags);
 211                memset(req, 0, sizeof(*req));
 212        } else {
 213                req = kzalloc(sizeof(*req), gfp_flags);
 214        }
 215        if (req == NULL)
 216                return NULL;
 217
 218        req->r_osdc = osdc;
 219        req->r_mempool = use_mempool;
 220
 221        kref_init(&req->r_kref);
 222        init_completion(&req->r_completion);
 223        init_completion(&req->r_safe_completion);
 224        rb_init_node(&req->r_node);
 225        INIT_LIST_HEAD(&req->r_unsafe_item);
 226        INIT_LIST_HEAD(&req->r_linger_item);
 227        INIT_LIST_HEAD(&req->r_linger_osd);
 228        INIT_LIST_HEAD(&req->r_req_lru_item);
 229        INIT_LIST_HEAD(&req->r_osd_item);
 230
 231        req->r_flags = flags;
 232
 233        WARN_ON((flags & (CEPH_OSD_FLAG_READ|CEPH_OSD_FLAG_WRITE)) == 0);
 234
 235        /* create reply message */
 236        if (use_mempool)
 237                msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
 238        else
 239                msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
 240                                   OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
 241        if (!msg) {
 242                ceph_osdc_put_request(req);
 243                return NULL;
 244        }
 245        req->r_reply = msg;
 246
 247        /* allocate space for the trailing data */
 248        if (needs_trail) {
 249                req->r_trail = kmalloc(sizeof(struct ceph_pagelist), gfp_flags);
 250                if (!req->r_trail) {
 251                        ceph_osdc_put_request(req);
 252                        return NULL;
 253                }
 254                ceph_pagelist_init(req->r_trail);
 255        }
 256
 257        /* create request message; allow space for oid */
 258        msg_size += MAX_OBJ_NAME_SIZE;
 259        if (snapc)
 260                msg_size += sizeof(u64) * snapc->num_snaps;
 261        if (use_mempool)
 262                msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
 263        else
 264                msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
 265        if (!msg) {
 266                ceph_osdc_put_request(req);
 267                return NULL;
 268        }
 269
 270        memset(msg->front.iov_base, 0, msg->front.iov_len);
 271
 272        req->r_request = msg;
 273        req->r_pages = pages;
 274#ifdef CONFIG_BLOCK
 275        if (bio) {
 276                req->r_bio = bio;
 277                bio_get(req->r_bio);
 278        }
 279#endif
 280
 281        return req;
 282}
 283EXPORT_SYMBOL(ceph_osdc_alloc_request);
 284
 285static void osd_req_encode_op(struct ceph_osd_request *req,
 286                              struct ceph_osd_op *dst,
 287                              struct ceph_osd_req_op *src)
 288{
 289        dst->op = cpu_to_le16(src->op);
 290
 291        switch (src->op) {
 292        case CEPH_OSD_OP_READ:
 293        case CEPH_OSD_OP_WRITE:
 294                dst->extent.offset =
 295                        cpu_to_le64(src->extent.offset);
 296                dst->extent.length =
 297                        cpu_to_le64(src->extent.length);
 298                dst->extent.truncate_size =
 299                        cpu_to_le64(src->extent.truncate_size);
 300                dst->extent.truncate_seq =
 301                        cpu_to_le32(src->extent.truncate_seq);
 302                break;
 303
 304        case CEPH_OSD_OP_GETXATTR:
 305        case CEPH_OSD_OP_SETXATTR:
 306        case CEPH_OSD_OP_CMPXATTR:
 307                BUG_ON(!req->r_trail);
 308
 309                dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
 310                dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
 311                dst->xattr.cmp_op = src->xattr.cmp_op;
 312                dst->xattr.cmp_mode = src->xattr.cmp_mode;
 313                ceph_pagelist_append(req->r_trail, src->xattr.name,
 314                                     src->xattr.name_len);
 315                ceph_pagelist_append(req->r_trail, src->xattr.val,
 316                                     src->xattr.value_len);
 317                break;
 318        case CEPH_OSD_OP_CALL:
 319                BUG_ON(!req->r_trail);
 320
 321                dst->cls.class_len = src->cls.class_len;
 322                dst->cls.method_len = src->cls.method_len;
 323                dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
 324
 325                ceph_pagelist_append(req->r_trail, src->cls.class_name,
 326                                     src->cls.class_len);
 327                ceph_pagelist_append(req->r_trail, src->cls.method_name,
 328                                     src->cls.method_len);
 329                ceph_pagelist_append(req->r_trail, src->cls.indata,
 330                                     src->cls.indata_len);
 331                break;
 332        case CEPH_OSD_OP_ROLLBACK:
 333                dst->snap.snapid = cpu_to_le64(src->snap.snapid);
 334                break;
 335        case CEPH_OSD_OP_STARTSYNC:
 336                break;
 337        case CEPH_OSD_OP_NOTIFY:
 338                {
 339                        __le32 prot_ver = cpu_to_le32(src->watch.prot_ver);
 340                        __le32 timeout = cpu_to_le32(src->watch.timeout);
 341
 342                        BUG_ON(!req->r_trail);
 343
 344                        ceph_pagelist_append(req->r_trail,
 345                                                &prot_ver, sizeof(prot_ver));
 346                        ceph_pagelist_append(req->r_trail,
 347                                                &timeout, sizeof(timeout));
 348                }
 349        case CEPH_OSD_OP_NOTIFY_ACK:
 350        case CEPH_OSD_OP_WATCH:
 351                dst->watch.cookie = cpu_to_le64(src->watch.cookie);
 352                dst->watch.ver = cpu_to_le64(src->watch.ver);
 353                dst->watch.flag = src->watch.flag;
 354                break;
 355        default:
 356                pr_err("unrecognized osd opcode %d\n", dst->op);
 357                WARN_ON(1);
 358                break;
 359        }
 360        dst->payload_len = cpu_to_le32(src->payload_len);
 361}
 362
 363/*
 364 * build new request AND message
 365 *
 366 */
 367void ceph_osdc_build_request(struct ceph_osd_request *req,
 368                             u64 off, u64 *plen,
 369                             struct ceph_osd_req_op *src_ops,
 370                             struct ceph_snap_context *snapc,
 371                             struct timespec *mtime,
 372                             const char *oid,
 373                             int oid_len)
 374{
 375        struct ceph_msg *msg = req->r_request;
 376        struct ceph_osd_request_head *head;
 377        struct ceph_osd_req_op *src_op;
 378        struct ceph_osd_op *op;
 379        void *p;
 380        int num_op = get_num_ops(src_ops, NULL);
 381        size_t msg_size = sizeof(*head) + num_op*sizeof(*op);
 382        int flags = req->r_flags;
 383        u64 data_len = 0;
 384        int i;
 385
 386        head = msg->front.iov_base;
 387        op = (void *)(head + 1);
 388        p = (void *)(op + num_op);
 389
 390        req->r_snapc = ceph_get_snap_context(snapc);
 391
 392        head->client_inc = cpu_to_le32(1); /* always, for now. */
 393        head->flags = cpu_to_le32(flags);
 394        if (flags & CEPH_OSD_FLAG_WRITE)
 395                ceph_encode_timespec(&head->mtime, mtime);
 396        head->num_ops = cpu_to_le16(num_op);
 397
 398
 399        /* fill in oid */
 400        head->object_len = cpu_to_le32(oid_len);
 401        memcpy(p, oid, oid_len);
 402        p += oid_len;
 403
 404        src_op = src_ops;
 405        while (src_op->op) {
 406                osd_req_encode_op(req, op, src_op);
 407                src_op++;
 408                op++;
 409        }
 410
 411        if (req->r_trail)
 412                data_len += req->r_trail->length;
 413
 414        if (snapc) {
 415                head->snap_seq = cpu_to_le64(snapc->seq);
 416                head->num_snaps = cpu_to_le32(snapc->num_snaps);
 417                for (i = 0; i < snapc->num_snaps; i++) {
 418                        put_unaligned_le64(snapc->snaps[i], p);
 419                        p += sizeof(u64);
 420                }
 421        }
 422
 423        if (flags & CEPH_OSD_FLAG_WRITE) {
 424                req->r_request->hdr.data_off = cpu_to_le16(off);
 425                req->r_request->hdr.data_len = cpu_to_le32(*plen + data_len);
 426        } else if (data_len) {
 427                req->r_request->hdr.data_off = 0;
 428                req->r_request->hdr.data_len = cpu_to_le32(data_len);
 429        }
 430
 431        req->r_request->page_alignment = req->r_page_alignment;
 432
 433        BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
 434        msg_size = p - msg->front.iov_base;
 435        msg->front.iov_len = msg_size;
 436        msg->hdr.front_len = cpu_to_le32(msg_size);
 437        return;
 438}
 439EXPORT_SYMBOL(ceph_osdc_build_request);
 440
 441/*
 442 * build new request AND message, calculate layout, and adjust file
 443 * extent as needed.
 444 *
 445 * if the file was recently truncated, we include information about its
 446 * old and new size so that the object can be updated appropriately.  (we
 447 * avoid synchronously deleting truncated objects because it's slow.)
 448 *
 449 * if @do_sync, include a 'startsync' command so that the osd will flush
 450 * data quickly.
 451 */
 452struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
 453                                               struct ceph_file_layout *layout,
 454                                               struct ceph_vino vino,
 455                                               u64 off, u64 *plen,
 456                                               int opcode, int flags,
 457                                               struct ceph_snap_context *snapc,
 458                                               int do_sync,
 459                                               u32 truncate_seq,
 460                                               u64 truncate_size,
 461                                               struct timespec *mtime,
 462                                               bool use_mempool, int num_reply,
 463                                               int page_align)
 464{
 465        struct ceph_osd_req_op ops[3];
 466        struct ceph_osd_request *req;
 467
 468        ops[0].op = opcode;
 469        ops[0].extent.truncate_seq = truncate_seq;
 470        ops[0].extent.truncate_size = truncate_size;
 471        ops[0].payload_len = 0;
 472
 473        if (do_sync) {
 474                ops[1].op = CEPH_OSD_OP_STARTSYNC;
 475                ops[1].payload_len = 0;
 476                ops[2].op = 0;
 477        } else
 478                ops[1].op = 0;
 479
 480        req = ceph_osdc_alloc_request(osdc, flags,
 481                                         snapc, ops,
 482                                         use_mempool,
 483                                         GFP_NOFS, NULL, NULL);
 484        if (!req)
 485                return NULL;
 486
 487        /* calculate max write size */
 488        calc_layout(osdc, vino, layout, off, plen, req, ops);
 489        req->r_file_layout = *layout;  /* keep a copy */
 490
 491        /* in case it differs from natural (file) alignment that
 492           calc_layout filled in for us */
 493        req->r_num_pages = calc_pages_for(page_align, *plen);
 494        req->r_page_alignment = page_align;
 495
 496        ceph_osdc_build_request(req, off, plen, ops,
 497                                snapc,
 498                                mtime,
 499                                req->r_oid, req->r_oid_len);
 500
 501        return req;
 502}
 503EXPORT_SYMBOL(ceph_osdc_new_request);
 504
 505/*
 506 * We keep osd requests in an rbtree, sorted by ->r_tid.
 507 */
 508static void __insert_request(struct ceph_osd_client *osdc,
 509                             struct ceph_osd_request *new)
 510{
 511        struct rb_node **p = &osdc->requests.rb_node;
 512        struct rb_node *parent = NULL;
 513        struct ceph_osd_request *req = NULL;
 514
 515        while (*p) {
 516                parent = *p;
 517                req = rb_entry(parent, struct ceph_osd_request, r_node);
 518                if (new->r_tid < req->r_tid)
 519                        p = &(*p)->rb_left;
 520                else if (new->r_tid > req->r_tid)
 521                        p = &(*p)->rb_right;
 522                else
 523                        BUG();
 524        }
 525
 526        rb_link_node(&new->r_node, parent, p);
 527        rb_insert_color(&new->r_node, &osdc->requests);
 528}
 529
 530static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
 531                                                 u64 tid)
 532{
 533        struct ceph_osd_request *req;
 534        struct rb_node *n = osdc->requests.rb_node;
 535
 536        while (n) {
 537                req = rb_entry(n, struct ceph_osd_request, r_node);
 538                if (tid < req->r_tid)
 539                        n = n->rb_left;
 540                else if (tid > req->r_tid)
 541                        n = n->rb_right;
 542                else
 543                        return req;
 544        }
 545        return NULL;
 546}
 547
 548static struct ceph_osd_request *
 549__lookup_request_ge(struct ceph_osd_client *osdc,
 550                    u64 tid)
 551{
 552        struct ceph_osd_request *req;
 553        struct rb_node *n = osdc->requests.rb_node;
 554
 555        while (n) {
 556                req = rb_entry(n, struct ceph_osd_request, r_node);
 557                if (tid < req->r_tid) {
 558                        if (!n->rb_left)
 559                                return req;
 560                        n = n->rb_left;
 561                } else if (tid > req->r_tid) {
 562                        n = n->rb_right;
 563                } else {
 564                        return req;
 565                }
 566        }
 567        return NULL;
 568}
 569
 570/*
 571 * Resubmit requests pending on the given osd.
 572 */
 573static void __kick_osd_requests(struct ceph_osd_client *osdc,
 574                                struct ceph_osd *osd)
 575{
 576        struct ceph_osd_request *req, *nreq;
 577        int err;
 578
 579        dout("__kick_osd_requests osd%d\n", osd->o_osd);
 580        err = __reset_osd(osdc, osd);
 581        if (err == -EAGAIN)
 582                return;
 583
 584        list_for_each_entry(req, &osd->o_requests, r_osd_item) {
 585                list_move(&req->r_req_lru_item, &osdc->req_unsent);
 586                dout("requeued %p tid %llu osd%d\n", req, req->r_tid,
 587                     osd->o_osd);
 588                if (!req->r_linger)
 589                        req->r_flags |= CEPH_OSD_FLAG_RETRY;
 590        }
 591
 592        list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
 593                                 r_linger_osd) {
 594                /*
 595                 * reregister request prior to unregistering linger so
 596                 * that r_osd is preserved.
 597                 */
 598                BUG_ON(!list_empty(&req->r_req_lru_item));
 599                __register_request(osdc, req);
 600                list_add(&req->r_req_lru_item, &osdc->req_unsent);
 601                list_add(&req->r_osd_item, &req->r_osd->o_requests);
 602                __unregister_linger_request(osdc, req);
 603                dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
 604                     osd->o_osd);
 605        }
 606}
 607
 608static void kick_osd_requests(struct ceph_osd_client *osdc,
 609                              struct ceph_osd *kickosd)
 610{
 611        mutex_lock(&osdc->request_mutex);
 612        __kick_osd_requests(osdc, kickosd);
 613        mutex_unlock(&osdc->request_mutex);
 614}
 615
 616/*
 617 * If the osd connection drops, we need to resubmit all requests.
 618 */
 619static void osd_reset(struct ceph_connection *con)
 620{
 621        struct ceph_osd *osd = con->private;
 622        struct ceph_osd_client *osdc;
 623
 624        if (!osd)
 625                return;
 626        dout("osd_reset osd%d\n", osd->o_osd);
 627        osdc = osd->o_osdc;
 628        down_read(&osdc->map_sem);
 629        kick_osd_requests(osdc, osd);
 630        send_queued(osdc);
 631        up_read(&osdc->map_sem);
 632}
 633
 634/*
 635 * Track open sessions with osds.
 636 */
 637static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
 638{
 639        struct ceph_osd *osd;
 640
 641        osd = kzalloc(sizeof(*osd), GFP_NOFS);
 642        if (!osd)
 643                return NULL;
 644
 645        atomic_set(&osd->o_ref, 1);
 646        osd->o_osdc = osdc;
 647        osd->o_osd = onum;
 648        INIT_LIST_HEAD(&osd->o_requests);
 649        INIT_LIST_HEAD(&osd->o_linger_requests);
 650        INIT_LIST_HEAD(&osd->o_osd_lru);
 651        osd->o_incarnation = 1;
 652
 653        ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
 654
 655        INIT_LIST_HEAD(&osd->o_keepalive_item);
 656        return osd;
 657}
 658
 659static struct ceph_osd *get_osd(struct ceph_osd *osd)
 660{
 661        if (atomic_inc_not_zero(&osd->o_ref)) {
 662                dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
 663                     atomic_read(&osd->o_ref));
 664                return osd;
 665        } else {
 666                dout("get_osd %p FAIL\n", osd);
 667                return NULL;
 668        }
 669}
 670
 671static void put_osd(struct ceph_osd *osd)
 672{
 673        dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
 674             atomic_read(&osd->o_ref) - 1);
 675        if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
 676                struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
 677
 678                if (ac->ops && ac->ops->destroy_authorizer)
 679                        ac->ops->destroy_authorizer(ac, osd->o_auth.authorizer);
 680                kfree(osd);
 681        }
 682}
 683
 684/*
 685 * remove an osd from our map
 686 */
 687static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
 688{
 689        dout("__remove_osd %p\n", osd);
 690        BUG_ON(!list_empty(&osd->o_requests));
 691        rb_erase(&osd->o_node, &osdc->osds);
 692        list_del_init(&osd->o_osd_lru);
 693        ceph_con_close(&osd->o_con);
 694        put_osd(osd);
 695}
 696
 697static void remove_all_osds(struct ceph_osd_client *osdc)
 698{
 699        dout("%s %p\n", __func__, osdc);
 700        mutex_lock(&osdc->request_mutex);
 701        while (!RB_EMPTY_ROOT(&osdc->osds)) {
 702                struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
 703                                                struct ceph_osd, o_node);
 704                __remove_osd(osdc, osd);
 705        }
 706        mutex_unlock(&osdc->request_mutex);
 707}
 708
 709static void __move_osd_to_lru(struct ceph_osd_client *osdc,
 710                              struct ceph_osd *osd)
 711{
 712        dout("__move_osd_to_lru %p\n", osd);
 713        BUG_ON(!list_empty(&osd->o_osd_lru));
 714        list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
 715        osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
 716}
 717
 718static void __remove_osd_from_lru(struct ceph_osd *osd)
 719{
 720        dout("__remove_osd_from_lru %p\n", osd);
 721        if (!list_empty(&osd->o_osd_lru))
 722                list_del_init(&osd->o_osd_lru);
 723}
 724
 725static void remove_old_osds(struct ceph_osd_client *osdc)
 726{
 727        struct ceph_osd *osd, *nosd;
 728
 729        dout("__remove_old_osds %p\n", osdc);
 730        mutex_lock(&osdc->request_mutex);
 731        list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
 732                if (time_before(jiffies, osd->lru_ttl))
 733                        break;
 734                __remove_osd(osdc, osd);
 735        }
 736        mutex_unlock(&osdc->request_mutex);
 737}
 738
 739/*
 740 * reset osd connect
 741 */
 742static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
 743{
 744        struct ceph_osd_request *req;
 745        int ret = 0;
 746
 747        dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
 748        if (list_empty(&osd->o_requests) &&
 749            list_empty(&osd->o_linger_requests)) {
 750                __remove_osd(osdc, osd);
 751        } else if (memcmp(&osdc->osdmap->osd_addr[osd->o_osd],
 752                          &osd->o_con.peer_addr,
 753                          sizeof(osd->o_con.peer_addr)) == 0 &&
 754                   !ceph_con_opened(&osd->o_con)) {
 755                dout(" osd addr hasn't changed and connection never opened,"
 756                     " letting msgr retry");
 757                /* touch each r_stamp for handle_timeout()'s benfit */
 758                list_for_each_entry(req, &osd->o_requests, r_osd_item)
 759                        req->r_stamp = jiffies;
 760                ret = -EAGAIN;
 761        } else {
 762                ceph_con_close(&osd->o_con);
 763                ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
 764                              &osdc->osdmap->osd_addr[osd->o_osd]);
 765                osd->o_incarnation++;
 766        }
 767        return ret;
 768}
 769
 770static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
 771{
 772        struct rb_node **p = &osdc->osds.rb_node;
 773        struct rb_node *parent = NULL;
 774        struct ceph_osd *osd = NULL;
 775
 776        dout("__insert_osd %p osd%d\n", new, new->o_osd);
 777        while (*p) {
 778                parent = *p;
 779                osd = rb_entry(parent, struct ceph_osd, o_node);
 780                if (new->o_osd < osd->o_osd)
 781                        p = &(*p)->rb_left;
 782                else if (new->o_osd > osd->o_osd)
 783                        p = &(*p)->rb_right;
 784                else
 785                        BUG();
 786        }
 787
 788        rb_link_node(&new->o_node, parent, p);
 789        rb_insert_color(&new->o_node, &osdc->osds);
 790}
 791
 792static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
 793{
 794        struct ceph_osd *osd;
 795        struct rb_node *n = osdc->osds.rb_node;
 796
 797        while (n) {
 798                osd = rb_entry(n, struct ceph_osd, o_node);
 799                if (o < osd->o_osd)
 800                        n = n->rb_left;
 801                else if (o > osd->o_osd)
 802                        n = n->rb_right;
 803                else
 804                        return osd;
 805        }
 806        return NULL;
 807}
 808
 809static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
 810{
 811        schedule_delayed_work(&osdc->timeout_work,
 812                        osdc->client->options->osd_keepalive_timeout * HZ);
 813}
 814
 815static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
 816{
 817        cancel_delayed_work(&osdc->timeout_work);
 818}
 819
 820/*
 821 * Register request, assign tid.  If this is the first request, set up
 822 * the timeout event.
 823 */
 824static void __register_request(struct ceph_osd_client *osdc,
 825                               struct ceph_osd_request *req)
 826{
 827        req->r_tid = ++osdc->last_tid;
 828        req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
 829        dout("__register_request %p tid %lld\n", req, req->r_tid);
 830        __insert_request(osdc, req);
 831        ceph_osdc_get_request(req);
 832        osdc->num_requests++;
 833        if (osdc->num_requests == 1) {
 834                dout(" first request, scheduling timeout\n");
 835                __schedule_osd_timeout(osdc);
 836        }
 837}
 838
 839static void register_request(struct ceph_osd_client *osdc,
 840                             struct ceph_osd_request *req)
 841{
 842        mutex_lock(&osdc->request_mutex);
 843        __register_request(osdc, req);
 844        mutex_unlock(&osdc->request_mutex);
 845}
 846
 847/*
 848 * called under osdc->request_mutex
 849 */
 850static void __unregister_request(struct ceph_osd_client *osdc,
 851                                 struct ceph_osd_request *req)
 852{
 853        if (RB_EMPTY_NODE(&req->r_node)) {
 854                dout("__unregister_request %p tid %lld not registered\n",
 855                        req, req->r_tid);
 856                return;
 857        }
 858
 859        dout("__unregister_request %p tid %lld\n", req, req->r_tid);
 860        rb_erase(&req->r_node, &osdc->requests);
 861        osdc->num_requests--;
 862
 863        if (req->r_osd) {
 864                /* make sure the original request isn't in flight. */
 865                ceph_msg_revoke(req->r_request);
 866
 867                list_del_init(&req->r_osd_item);
 868                if (list_empty(&req->r_osd->o_requests) &&
 869                    list_empty(&req->r_osd->o_linger_requests)) {
 870                        dout("moving osd to %p lru\n", req->r_osd);
 871                        __move_osd_to_lru(osdc, req->r_osd);
 872                }
 873                if (list_empty(&req->r_linger_item))
 874                        req->r_osd = NULL;
 875        }
 876
 877        ceph_osdc_put_request(req);
 878
 879        list_del_init(&req->r_req_lru_item);
 880        if (osdc->num_requests == 0) {
 881                dout(" no requests, canceling timeout\n");
 882                __cancel_osd_timeout(osdc);
 883        }
 884}
 885
 886/*
 887 * Cancel a previously queued request message
 888 */
 889static void __cancel_request(struct ceph_osd_request *req)
 890{
 891        if (req->r_sent && req->r_osd) {
 892                ceph_msg_revoke(req->r_request);
 893                req->r_sent = 0;
 894        }
 895}
 896
 897static void __register_linger_request(struct ceph_osd_client *osdc,
 898                                    struct ceph_osd_request *req)
 899{
 900        dout("__register_linger_request %p\n", req);
 901        list_add_tail(&req->r_linger_item, &osdc->req_linger);
 902        if (req->r_osd)
 903                list_add_tail(&req->r_linger_osd,
 904                              &req->r_osd->o_linger_requests);
 905}
 906
 907static void __unregister_linger_request(struct ceph_osd_client *osdc,
 908                                        struct ceph_osd_request *req)
 909{
 910        dout("__unregister_linger_request %p\n", req);
 911        if (req->r_osd) {
 912                list_del_init(&req->r_linger_item);
 913                list_del_init(&req->r_linger_osd);
 914
 915                if (list_empty(&req->r_osd->o_requests) &&
 916                    list_empty(&req->r_osd->o_linger_requests)) {
 917                        dout("moving osd to %p lru\n", req->r_osd);
 918                        __move_osd_to_lru(osdc, req->r_osd);
 919                }
 920                if (list_empty(&req->r_osd_item))
 921                        req->r_osd = NULL;
 922        }
 923}
 924
 925void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
 926                                         struct ceph_osd_request *req)
 927{
 928        mutex_lock(&osdc->request_mutex);
 929        if (req->r_linger) {
 930                __unregister_linger_request(osdc, req);
 931                ceph_osdc_put_request(req);
 932        }
 933        mutex_unlock(&osdc->request_mutex);
 934}
 935EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
 936
 937void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
 938                                  struct ceph_osd_request *req)
 939{
 940        if (!req->r_linger) {
 941                dout("set_request_linger %p\n", req);
 942                req->r_linger = 1;
 943                /*
 944                 * caller is now responsible for calling
 945                 * unregister_linger_request
 946                 */
 947                ceph_osdc_get_request(req);
 948        }
 949}
 950EXPORT_SYMBOL(ceph_osdc_set_request_linger);
 951
 952/*
 953 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
 954 * (as needed), and set the request r_osd appropriately.  If there is
 955 * no up osd, set r_osd to NULL.  Move the request to the appropriate list
 956 * (unsent, homeless) or leave on in-flight lru.
 957 *
 958 * Return 0 if unchanged, 1 if changed, or negative on error.
 959 *
 960 * Caller should hold map_sem for read and request_mutex.
 961 */
 962static int __map_request(struct ceph_osd_client *osdc,
 963                         struct ceph_osd_request *req, int force_resend)
 964{
 965        struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
 966        struct ceph_pg pgid;
 967        int acting[CEPH_PG_MAX_SIZE];
 968        int o = -1, num = 0;
 969        int err;
 970
 971        dout("map_request %p tid %lld\n", req, req->r_tid);
 972        err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
 973                                      &req->r_file_layout, osdc->osdmap);
 974        if (err) {
 975                list_move(&req->r_req_lru_item, &osdc->req_notarget);
 976                return err;
 977        }
 978        pgid = reqhead->layout.ol_pgid;
 979        req->r_pgid = pgid;
 980
 981        err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
 982        if (err > 0) {
 983                o = acting[0];
 984                num = err;
 985        }
 986
 987        if ((!force_resend &&
 988             req->r_osd && req->r_osd->o_osd == o &&
 989             req->r_sent >= req->r_osd->o_incarnation &&
 990             req->r_num_pg_osds == num &&
 991             memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
 992            (req->r_osd == NULL && o == -1))
 993                return 0;  /* no change */
 994
 995        dout("map_request tid %llu pgid %d.%x osd%d (was osd%d)\n",
 996             req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
 997             req->r_osd ? req->r_osd->o_osd : -1);
 998
 999        /* record full pg acting set */
1000        memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1001        req->r_num_pg_osds = num;
1002
1003        if (req->r_osd) {
1004                __cancel_request(req);
1005                list_del_init(&req->r_osd_item);
1006                req->r_osd = NULL;
1007        }
1008
1009        req->r_osd = __lookup_osd(osdc, o);
1010        if (!req->r_osd && o >= 0) {
1011                err = -ENOMEM;
1012                req->r_osd = create_osd(osdc, o);
1013                if (!req->r_osd) {
1014                        list_move(&req->r_req_lru_item, &osdc->req_notarget);
1015                        goto out;
1016                }
1017
1018                dout("map_request osd %p is osd%d\n", req->r_osd, o);
1019                __insert_osd(osdc, req->r_osd);
1020
1021                ceph_con_open(&req->r_osd->o_con,
1022                              CEPH_ENTITY_TYPE_OSD, o,
1023                              &osdc->osdmap->osd_addr[o]);
1024        }
1025
1026        if (req->r_osd) {
1027                __remove_osd_from_lru(req->r_osd);
1028                list_add(&req->r_osd_item, &req->r_osd->o_requests);
1029                list_move(&req->r_req_lru_item, &osdc->req_unsent);
1030        } else {
1031                list_move(&req->r_req_lru_item, &osdc->req_notarget);
1032        }
1033        err = 1;   /* osd or pg changed */
1034
1035out:
1036        return err;
1037}
1038
1039/*
1040 * caller should hold map_sem (for read) and request_mutex
1041 */
1042static void __send_request(struct ceph_osd_client *osdc,
1043                           struct ceph_osd_request *req)
1044{
1045        struct ceph_osd_request_head *reqhead;
1046
1047        dout("send_request %p tid %llu to osd%d flags %d\n",
1048             req, req->r_tid, req->r_osd->o_osd, req->r_flags);
1049
1050        reqhead = req->r_request->front.iov_base;
1051        reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
1052        reqhead->flags |= cpu_to_le32(req->r_flags);  /* e.g., RETRY */
1053        reqhead->reassert_version = req->r_reassert_version;
1054
1055        req->r_stamp = jiffies;
1056        list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
1057
1058        ceph_msg_get(req->r_request); /* send consumes a ref */
1059        ceph_con_send(&req->r_osd->o_con, req->r_request);
1060        req->r_sent = req->r_osd->o_incarnation;
1061}
1062
1063/*
1064 * Send any requests in the queue (req_unsent).
1065 */
1066static void send_queued(struct ceph_osd_client *osdc)
1067{
1068        struct ceph_osd_request *req, *tmp;
1069
1070        dout("send_queued\n");
1071        mutex_lock(&osdc->request_mutex);
1072        list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item) {
1073                __send_request(osdc, req);
1074        }
1075        mutex_unlock(&osdc->request_mutex);
1076}
1077
1078/*
1079 * Timeout callback, called every N seconds when 1 or more osd
1080 * requests has been active for more than N seconds.  When this
1081 * happens, we ping all OSDs with requests who have timed out to
1082 * ensure any communications channel reset is detected.  Reset the
1083 * request timeouts another N seconds in the future as we go.
1084 * Reschedule the timeout event another N seconds in future (unless
1085 * there are no open requests).
1086 */
1087static void handle_timeout(struct work_struct *work)
1088{
1089        struct ceph_osd_client *osdc =
1090                container_of(work, struct ceph_osd_client, timeout_work.work);
1091        struct ceph_osd_request *req, *last_req = NULL;
1092        struct ceph_osd *osd;
1093        unsigned long timeout = osdc->client->options->osd_timeout * HZ;
1094        unsigned long keepalive =
1095                osdc->client->options->osd_keepalive_timeout * HZ;
1096        unsigned long last_stamp = 0;
1097        struct list_head slow_osds;
1098        dout("timeout\n");
1099        down_read(&osdc->map_sem);
1100
1101        ceph_monc_request_next_osdmap(&osdc->client->monc);
1102
1103        mutex_lock(&osdc->request_mutex);
1104
1105        /*
1106         * reset osds that appear to be _really_ unresponsive.  this
1107         * is a failsafe measure.. we really shouldn't be getting to
1108         * this point if the system is working properly.  the monitors
1109         * should mark the osd as failed and we should find out about
1110         * it from an updated osd map.
1111         */
1112        while (timeout && !list_empty(&osdc->req_lru)) {
1113                req = list_entry(osdc->req_lru.next, struct ceph_osd_request,
1114                                 r_req_lru_item);
1115
1116                /* hasn't been long enough since we sent it? */
1117                if (time_before(jiffies, req->r_stamp + timeout))
1118                        break;
1119
1120                /* hasn't been long enough since it was acked? */
1121                if (req->r_request->ack_stamp == 0 ||
1122                    time_before(jiffies, req->r_request->ack_stamp + timeout))
1123                        break;
1124
1125                BUG_ON(req == last_req && req->r_stamp == last_stamp);
1126                last_req = req;
1127                last_stamp = req->r_stamp;
1128
1129                osd = req->r_osd;
1130                BUG_ON(!osd);
1131                pr_warning(" tid %llu timed out on osd%d, will reset osd\n",
1132                           req->r_tid, osd->o_osd);
1133                __kick_osd_requests(osdc, osd);
1134        }
1135
1136        /*
1137         * ping osds that are a bit slow.  this ensures that if there
1138         * is a break in the TCP connection we will notice, and reopen
1139         * a connection with that osd (from the fault callback).
1140         */
1141        INIT_LIST_HEAD(&slow_osds);
1142        list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
1143                if (time_before(jiffies, req->r_stamp + keepalive))
1144                        break;
1145
1146                osd = req->r_osd;
1147                BUG_ON(!osd);
1148                dout(" tid %llu is slow, will send keepalive on osd%d\n",
1149                     req->r_tid, osd->o_osd);
1150                list_move_tail(&osd->o_keepalive_item, &slow_osds);
1151        }
1152        while (!list_empty(&slow_osds)) {
1153                osd = list_entry(slow_osds.next, struct ceph_osd,
1154                                 o_keepalive_item);
1155                list_del_init(&osd->o_keepalive_item);
1156                ceph_con_keepalive(&osd->o_con);
1157        }
1158
1159        __schedule_osd_timeout(osdc);
1160        mutex_unlock(&osdc->request_mutex);
1161        send_queued(osdc);
1162        up_read(&osdc->map_sem);
1163}
1164
1165static void handle_osds_timeout(struct work_struct *work)
1166{
1167        struct ceph_osd_client *osdc =
1168                container_of(work, struct ceph_osd_client,
1169                             osds_timeout_work.work);
1170        unsigned long delay =
1171                osdc->client->options->osd_idle_ttl * HZ >> 2;
1172
1173        dout("osds timeout\n");
1174        down_read(&osdc->map_sem);
1175        remove_old_osds(osdc);
1176        up_read(&osdc->map_sem);
1177
1178        schedule_delayed_work(&osdc->osds_timeout_work,
1179                              round_jiffies_relative(delay));
1180}
1181
1182static void complete_request(struct ceph_osd_request *req)
1183{
1184        if (req->r_safe_callback)
1185                req->r_safe_callback(req, NULL);
1186        complete_all(&req->r_safe_completion);  /* fsync waiter */
1187}
1188
1189/*
1190 * handle osd op reply.  either call the callback if it is specified,
1191 * or do the completion to wake up the waiting thread.
1192 */
1193static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1194                         struct ceph_connection *con)
1195{
1196        struct ceph_osd_reply_head *rhead = msg->front.iov_base;
1197        struct ceph_osd_request *req;
1198        u64 tid;
1199        int numops, object_len, flags;
1200        s32 result;
1201
1202        tid = le64_to_cpu(msg->hdr.tid);
1203        if (msg->front.iov_len < sizeof(*rhead))
1204                goto bad;
1205        numops = le32_to_cpu(rhead->num_ops);
1206        object_len = le32_to_cpu(rhead->object_len);
1207        result = le32_to_cpu(rhead->result);
1208        if (msg->front.iov_len != sizeof(*rhead) + object_len +
1209            numops * sizeof(struct ceph_osd_op))
1210                goto bad;
1211        dout("handle_reply %p tid %llu result %d\n", msg, tid, (int)result);
1212        /* lookup */
1213        mutex_lock(&osdc->request_mutex);
1214        req = __lookup_request(osdc, tid);
1215        if (req == NULL) {
1216                dout("handle_reply tid %llu dne\n", tid);
1217                mutex_unlock(&osdc->request_mutex);
1218                return;
1219        }
1220        ceph_osdc_get_request(req);
1221        flags = le32_to_cpu(rhead->flags);
1222
1223        /*
1224         * if this connection filled our message, drop our reference now, to
1225         * avoid a (safe but slower) revoke later.
1226         */
1227        if (req->r_con_filling_msg == con && req->r_reply == msg) {
1228                dout(" dropping con_filling_msg ref %p\n", con);
1229                req->r_con_filling_msg = NULL;
1230                con->ops->put(con);
1231        }
1232
1233        if (!req->r_got_reply) {
1234                unsigned int bytes;
1235
1236                req->r_result = le32_to_cpu(rhead->result);
1237                bytes = le32_to_cpu(msg->hdr.data_len);
1238                dout("handle_reply result %d bytes %d\n", req->r_result,
1239                     bytes);
1240                if (req->r_result == 0)
1241                        req->r_result = bytes;
1242
1243                /* in case this is a write and we need to replay, */
1244                req->r_reassert_version = rhead->reassert_version;
1245
1246                req->r_got_reply = 1;
1247        } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1248                dout("handle_reply tid %llu dup ack\n", tid);
1249                mutex_unlock(&osdc->request_mutex);
1250                goto done;
1251        }
1252
1253        dout("handle_reply tid %llu flags %d\n", tid, flags);
1254
1255        if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1256                __register_linger_request(osdc, req);
1257
1258        /* either this is a read, or we got the safe response */
1259        if (result < 0 ||
1260            (flags & CEPH_OSD_FLAG_ONDISK) ||
1261            ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1262                __unregister_request(osdc, req);
1263
1264        mutex_unlock(&osdc->request_mutex);
1265
1266        if (req->r_callback)
1267                req->r_callback(req, msg);
1268        else
1269                complete_all(&req->r_completion);
1270
1271        if (flags & CEPH_OSD_FLAG_ONDISK)
1272                complete_request(req);
1273
1274done:
1275        dout("req=%p req->r_linger=%d\n", req, req->r_linger);
1276        ceph_osdc_put_request(req);
1277        return;
1278
1279bad:
1280        pr_err("corrupt osd_op_reply got %d %d expected %d\n",
1281               (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
1282               (int)sizeof(*rhead));
1283        ceph_msg_dump(msg);
1284}
1285
1286static void reset_changed_osds(struct ceph_osd_client *osdc)
1287{
1288        struct rb_node *p, *n;
1289
1290        for (p = rb_first(&osdc->osds); p; p = n) {
1291                struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
1292
1293                n = rb_next(p);
1294                if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1295                    memcmp(&osd->o_con.peer_addr,
1296                           ceph_osd_addr(osdc->osdmap,
1297                                         osd->o_osd),
1298                           sizeof(struct ceph_entity_addr)) != 0)
1299                        __reset_osd(osdc, osd);
1300        }
1301}
1302
1303/*
1304 * Requeue requests whose mapping to an OSD has changed.  If requests map to
1305 * no osd, request a new map.
1306 *
1307 * Caller should hold map_sem for read and request_mutex.
1308 */
1309static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
1310{
1311        struct ceph_osd_request *req, *nreq;
1312        struct rb_node *p;
1313        int needmap = 0;
1314        int err;
1315
1316        dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
1317        mutex_lock(&osdc->request_mutex);
1318        for (p = rb_first(&osdc->requests); p; ) {
1319                req = rb_entry(p, struct ceph_osd_request, r_node);
1320                p = rb_next(p);
1321                err = __map_request(osdc, req, force_resend);
1322                if (err < 0)
1323                        continue;  /* error */
1324                if (req->r_osd == NULL) {
1325                        dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1326                        needmap++;  /* request a newer map */
1327                } else if (err > 0) {
1328                        if (!req->r_linger) {
1329                                dout("%p tid %llu requeued on osd%d\n", req,
1330                                     req->r_tid,
1331                                     req->r_osd ? req->r_osd->o_osd : -1);
1332                                req->r_flags |= CEPH_OSD_FLAG_RETRY;
1333                        }
1334                }
1335                if (req->r_linger && list_empty(&req->r_linger_item)) {
1336                        /*
1337                         * register as a linger so that we will
1338                         * re-submit below and get a new tid
1339                         */
1340                        dout("%p tid %llu restart on osd%d\n",
1341                             req, req->r_tid,
1342                             req->r_osd ? req->r_osd->o_osd : -1);
1343                        __register_linger_request(osdc, req);
1344                        __unregister_request(osdc, req);
1345                }
1346        }
1347
1348        list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1349                                 r_linger_item) {
1350                dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1351
1352                err = __map_request(osdc, req, force_resend);
1353                if (err == 0)
1354                        continue;  /* no change and no osd was specified */
1355                if (err < 0)
1356                        continue;  /* hrm! */
1357                if (req->r_osd == NULL) {
1358                        dout("tid %llu maps to no valid osd\n", req->r_tid);
1359                        needmap++;  /* request a newer map */
1360                        continue;
1361                }
1362
1363                dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1364                     req->r_osd ? req->r_osd->o_osd : -1);
1365                __unregister_linger_request(osdc, req);
1366                __register_request(osdc, req);
1367        }
1368        mutex_unlock(&osdc->request_mutex);
1369
1370        if (needmap) {
1371                dout("%d requests for down osds, need new map\n", needmap);
1372                ceph_monc_request_next_osdmap(&osdc->client->monc);
1373        }
1374}
1375
1376
1377/*
1378 * Process updated osd map.
1379 *
1380 * The message contains any number of incremental and full maps, normally
1381 * indicating some sort of topology change in the cluster.  Kick requests
1382 * off to different OSDs as needed.
1383 */
1384void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1385{
1386        void *p, *end, *next;
1387        u32 nr_maps, maplen;
1388        u32 epoch;
1389        struct ceph_osdmap *newmap = NULL, *oldmap;
1390        int err;
1391        struct ceph_fsid fsid;
1392
1393        dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1394        p = msg->front.iov_base;
1395        end = p + msg->front.iov_len;
1396
1397        /* verify fsid */
1398        ceph_decode_need(&p, end, sizeof(fsid), bad);
1399        ceph_decode_copy(&p, &fsid, sizeof(fsid));
1400        if (ceph_check_fsid(osdc->client, &fsid) < 0)
1401                return;
1402
1403        down_write(&osdc->map_sem);
1404
1405        /* incremental maps */
1406        ceph_decode_32_safe(&p, end, nr_maps, bad);
1407        dout(" %d inc maps\n", nr_maps);
1408        while (nr_maps > 0) {
1409                ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1410                epoch = ceph_decode_32(&p);
1411                maplen = ceph_decode_32(&p);
1412                ceph_decode_need(&p, end, maplen, bad);
1413                next = p + maplen;
1414                if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1415                        dout("applying incremental map %u len %d\n",
1416                             epoch, maplen);
1417                        newmap = osdmap_apply_incremental(&p, next,
1418                                                          osdc->osdmap,
1419                                                          &osdc->client->msgr);
1420                        if (IS_ERR(newmap)) {
1421                                err = PTR_ERR(newmap);
1422                                goto bad;
1423                        }
1424                        BUG_ON(!newmap);
1425                        if (newmap != osdc->osdmap) {
1426                                ceph_osdmap_destroy(osdc->osdmap);
1427                                osdc->osdmap = newmap;
1428                        }
1429                        kick_requests(osdc, 0);
1430                        reset_changed_osds(osdc);
1431                } else {
1432                        dout("ignoring incremental map %u len %d\n",
1433                             epoch, maplen);
1434                }
1435                p = next;
1436                nr_maps--;
1437        }
1438        if (newmap)
1439                goto done;
1440
1441        /* full maps */
1442        ceph_decode_32_safe(&p, end, nr_maps, bad);
1443        dout(" %d full maps\n", nr_maps);
1444        while (nr_maps) {
1445                ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1446                epoch = ceph_decode_32(&p);
1447                maplen = ceph_decode_32(&p);
1448                ceph_decode_need(&p, end, maplen, bad);
1449                if (nr_maps > 1) {
1450                        dout("skipping non-latest full map %u len %d\n",
1451                             epoch, maplen);
1452                } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1453                        dout("skipping full map %u len %d, "
1454                             "older than our %u\n", epoch, maplen,
1455                             osdc->osdmap->epoch);
1456                } else {
1457                        int skipped_map = 0;
1458
1459                        dout("taking full map %u len %d\n", epoch, maplen);
1460                        newmap = osdmap_decode(&p, p+maplen);
1461                        if (IS_ERR(newmap)) {
1462                                err = PTR_ERR(newmap);
1463                                goto bad;
1464                        }
1465                        BUG_ON(!newmap);
1466                        oldmap = osdc->osdmap;
1467                        osdc->osdmap = newmap;
1468                        if (oldmap) {
1469                                if (oldmap->epoch + 1 < newmap->epoch)
1470                                        skipped_map = 1;
1471                                ceph_osdmap_destroy(oldmap);
1472                        }
1473                        kick_requests(osdc, skipped_map);
1474                }
1475                p += maplen;
1476                nr_maps--;
1477        }
1478
1479done:
1480        downgrade_write(&osdc->map_sem);
1481        ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
1482
1483        /*
1484         * subscribe to subsequent osdmap updates if full to ensure
1485         * we find out when we are no longer full and stop returning
1486         * ENOSPC.
1487         */
1488        if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1489                ceph_monc_request_next_osdmap(&osdc->client->monc);
1490
1491        send_queued(osdc);
1492        up_read(&osdc->map_sem);
1493        wake_up_all(&osdc->client->auth_wq);
1494        return;
1495
1496bad:
1497        pr_err("osdc handle_map corrupt msg\n");
1498        ceph_msg_dump(msg);
1499        up_write(&osdc->map_sem);
1500        return;
1501}
1502
1503/*
1504 * watch/notify callback event infrastructure
1505 *
1506 * These callbacks are used both for watch and notify operations.
1507 */
1508static void __release_event(struct kref *kref)
1509{
1510        struct ceph_osd_event *event =
1511                container_of(kref, struct ceph_osd_event, kref);
1512
1513        dout("__release_event %p\n", event);
1514        kfree(event);
1515}
1516
1517static void get_event(struct ceph_osd_event *event)
1518{
1519        kref_get(&event->kref);
1520}
1521
1522void ceph_osdc_put_event(struct ceph_osd_event *event)
1523{
1524        kref_put(&event->kref, __release_event);
1525}
1526EXPORT_SYMBOL(ceph_osdc_put_event);
1527
1528static void __insert_event(struct ceph_osd_client *osdc,
1529                             struct ceph_osd_event *new)
1530{
1531        struct rb_node **p = &osdc->event_tree.rb_node;
1532        struct rb_node *parent = NULL;
1533        struct ceph_osd_event *event = NULL;
1534
1535        while (*p) {
1536                parent = *p;
1537                event = rb_entry(parent, struct ceph_osd_event, node);
1538                if (new->cookie < event->cookie)
1539                        p = &(*p)->rb_left;
1540                else if (new->cookie > event->cookie)
1541                        p = &(*p)->rb_right;
1542                else
1543                        BUG();
1544        }
1545
1546        rb_link_node(&new->node, parent, p);
1547        rb_insert_color(&new->node, &osdc->event_tree);
1548}
1549
1550static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1551                                                u64 cookie)
1552{
1553        struct rb_node **p = &osdc->event_tree.rb_node;
1554        struct rb_node *parent = NULL;
1555        struct ceph_osd_event *event = NULL;
1556
1557        while (*p) {
1558                parent = *p;
1559                event = rb_entry(parent, struct ceph_osd_event, node);
1560                if (cookie < event->cookie)
1561                        p = &(*p)->rb_left;
1562                else if (cookie > event->cookie)
1563                        p = &(*p)->rb_right;
1564                else
1565                        return event;
1566        }
1567        return NULL;
1568}
1569
1570static void __remove_event(struct ceph_osd_event *event)
1571{
1572        struct ceph_osd_client *osdc = event->osdc;
1573
1574        if (!RB_EMPTY_NODE(&event->node)) {
1575                dout("__remove_event removed %p\n", event);
1576                rb_erase(&event->node, &osdc->event_tree);
1577                ceph_osdc_put_event(event);
1578        } else {
1579                dout("__remove_event didn't remove %p\n", event);
1580        }
1581}
1582
1583int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1584                           void (*event_cb)(u64, u64, u8, void *),
1585                           int one_shot, void *data,
1586                           struct ceph_osd_event **pevent)
1587{
1588        struct ceph_osd_event *event;
1589
1590        event = kmalloc(sizeof(*event), GFP_NOIO);
1591        if (!event)
1592                return -ENOMEM;
1593
1594        dout("create_event %p\n", event);
1595        event->cb = event_cb;
1596        event->one_shot = one_shot;
1597        event->data = data;
1598        event->osdc = osdc;
1599        INIT_LIST_HEAD(&event->osd_node);
1600        kref_init(&event->kref);   /* one ref for us */
1601        kref_get(&event->kref);    /* one ref for the caller */
1602        init_completion(&event->completion);
1603
1604        spin_lock(&osdc->event_lock);
1605        event->cookie = ++osdc->event_count;
1606        __insert_event(osdc, event);
1607        spin_unlock(&osdc->event_lock);
1608
1609        *pevent = event;
1610        return 0;
1611}
1612EXPORT_SYMBOL(ceph_osdc_create_event);
1613
1614void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1615{
1616        struct ceph_osd_client *osdc = event->osdc;
1617
1618        dout("cancel_event %p\n", event);
1619        spin_lock(&osdc->event_lock);
1620        __remove_event(event);
1621        spin_unlock(&osdc->event_lock);
1622        ceph_osdc_put_event(event); /* caller's */
1623}
1624EXPORT_SYMBOL(ceph_osdc_cancel_event);
1625
1626
1627static void do_event_work(struct work_struct *work)
1628{
1629        struct ceph_osd_event_work *event_work =
1630                container_of(work, struct ceph_osd_event_work, work);
1631        struct ceph_osd_event *event = event_work->event;
1632        u64 ver = event_work->ver;
1633        u64 notify_id = event_work->notify_id;
1634        u8 opcode = event_work->opcode;
1635
1636        dout("do_event_work completing %p\n", event);
1637        event->cb(ver, notify_id, opcode, event->data);
1638        complete(&event->completion);
1639        dout("do_event_work completed %p\n", event);
1640        ceph_osdc_put_event(event);
1641        kfree(event_work);
1642}
1643
1644
1645/*
1646 * Process osd watch notifications
1647 */
1648void handle_watch_notify(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1649{
1650        void *p, *end;
1651        u8 proto_ver;
1652        u64 cookie, ver, notify_id;
1653        u8 opcode;
1654        struct ceph_osd_event *event;
1655        struct ceph_osd_event_work *event_work;
1656
1657        p = msg->front.iov_base;
1658        end = p + msg->front.iov_len;
1659
1660        ceph_decode_8_safe(&p, end, proto_ver, bad);
1661        ceph_decode_8_safe(&p, end, opcode, bad);
1662        ceph_decode_64_safe(&p, end, cookie, bad);
1663        ceph_decode_64_safe(&p, end, ver, bad);
1664        ceph_decode_64_safe(&p, end, notify_id, bad);
1665
1666        spin_lock(&osdc->event_lock);
1667        event = __find_event(osdc, cookie);
1668        if (event) {
1669                get_event(event);
1670                if (event->one_shot)
1671                        __remove_event(event);
1672        }
1673        spin_unlock(&osdc->event_lock);
1674        dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1675             cookie, ver, event);
1676        if (event) {
1677                event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
1678                if (!event_work) {
1679                        dout("ERROR: could not allocate event_work\n");
1680                        goto done_err;
1681                }
1682                INIT_WORK(&event_work->work, do_event_work);
1683                event_work->event = event;
1684                event_work->ver = ver;
1685                event_work->notify_id = notify_id;
1686                event_work->opcode = opcode;
1687                if (!queue_work(osdc->notify_wq, &event_work->work)) {
1688                        dout("WARNING: failed to queue notify event work\n");
1689                        goto done_err;
1690                }
1691        }
1692
1693        return;
1694
1695done_err:
1696        complete(&event->completion);
1697        ceph_osdc_put_event(event);
1698        return;
1699
1700bad:
1701        pr_err("osdc handle_watch_notify corrupt msg\n");
1702        return;
1703}
1704
1705int ceph_osdc_wait_event(struct ceph_osd_event *event, unsigned long timeout)
1706{
1707        int err;
1708
1709        dout("wait_event %p\n", event);
1710        err = wait_for_completion_interruptible_timeout(&event->completion,
1711                                                        timeout * HZ);
1712        ceph_osdc_put_event(event);
1713        if (err > 0)
1714                err = 0;
1715        dout("wait_event %p returns %d\n", event, err);
1716        return err;
1717}
1718EXPORT_SYMBOL(ceph_osdc_wait_event);
1719
1720/*
1721 * Register request, send initial attempt.
1722 */
1723int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1724                            struct ceph_osd_request *req,
1725                            bool nofail)
1726{
1727        int rc = 0;
1728
1729        req->r_request->pages = req->r_pages;
1730        req->r_request->nr_pages = req->r_num_pages;
1731#ifdef CONFIG_BLOCK
1732        req->r_request->bio = req->r_bio;
1733#endif
1734        req->r_request->trail = req->r_trail;
1735
1736        register_request(osdc, req);
1737
1738        down_read(&osdc->map_sem);
1739        mutex_lock(&osdc->request_mutex);
1740        /*
1741         * a racing kick_requests() may have sent the message for us
1742         * while we dropped request_mutex above, so only send now if
1743         * the request still han't been touched yet.
1744         */
1745        if (req->r_sent == 0) {
1746                rc = __map_request(osdc, req, 0);
1747                if (rc < 0) {
1748                        if (nofail) {
1749                                dout("osdc_start_request failed map, "
1750                                     " will retry %lld\n", req->r_tid);
1751                                rc = 0;
1752                        }
1753                        goto out_unlock;
1754                }
1755                if (req->r_osd == NULL) {
1756                        dout("send_request %p no up osds in pg\n", req);
1757                        ceph_monc_request_next_osdmap(&osdc->client->monc);
1758                } else {
1759                        __send_request(osdc, req);
1760                }
1761                rc = 0;
1762        }
1763
1764out_unlock:
1765        mutex_unlock(&osdc->request_mutex);
1766        up_read(&osdc->map_sem);
1767        return rc;
1768}
1769EXPORT_SYMBOL(ceph_osdc_start_request);
1770
1771/*
1772 * wait for a request to complete
1773 */
1774int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1775                           struct ceph_osd_request *req)
1776{
1777        int rc;
1778
1779        rc = wait_for_completion_interruptible(&req->r_completion);
1780        if (rc < 0) {
1781                mutex_lock(&osdc->request_mutex);
1782                __cancel_request(req);
1783                __unregister_request(osdc, req);
1784                mutex_unlock(&osdc->request_mutex);
1785                complete_request(req);
1786                dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
1787                return rc;
1788        }
1789
1790        dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1791        return req->r_result;
1792}
1793EXPORT_SYMBOL(ceph_osdc_wait_request);
1794
1795/*
1796 * sync - wait for all in-flight requests to flush.  avoid starvation.
1797 */
1798void ceph_osdc_sync(struct ceph_osd_client *osdc)
1799{
1800        struct ceph_osd_request *req;
1801        u64 last_tid, next_tid = 0;
1802
1803        mutex_lock(&osdc->request_mutex);
1804        last_tid = osdc->last_tid;
1805        while (1) {
1806                req = __lookup_request_ge(osdc, next_tid);
1807                if (!req)
1808                        break;
1809                if (req->r_tid > last_tid)
1810                        break;
1811
1812                next_tid = req->r_tid + 1;
1813                if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1814                        continue;
1815
1816                ceph_osdc_get_request(req);
1817                mutex_unlock(&osdc->request_mutex);
1818                dout("sync waiting on tid %llu (last is %llu)\n",
1819                     req->r_tid, last_tid);
1820                wait_for_completion(&req->r_safe_completion);
1821                mutex_lock(&osdc->request_mutex);
1822                ceph_osdc_put_request(req);
1823        }
1824        mutex_unlock(&osdc->request_mutex);
1825        dout("sync done (thru tid %llu)\n", last_tid);
1826}
1827EXPORT_SYMBOL(ceph_osdc_sync);
1828
1829/*
1830 * init, shutdown
1831 */
1832int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1833{
1834        int err;
1835
1836        dout("init\n");
1837        osdc->client = client;
1838        osdc->osdmap = NULL;
1839        init_rwsem(&osdc->map_sem);
1840        init_completion(&osdc->map_waiters);
1841        osdc->last_requested_map = 0;
1842        mutex_init(&osdc->request_mutex);
1843        osdc->last_tid = 0;
1844        osdc->osds = RB_ROOT;
1845        INIT_LIST_HEAD(&osdc->osd_lru);
1846        osdc->requests = RB_ROOT;
1847        INIT_LIST_HEAD(&osdc->req_lru);
1848        INIT_LIST_HEAD(&osdc->req_unsent);
1849        INIT_LIST_HEAD(&osdc->req_notarget);
1850        INIT_LIST_HEAD(&osdc->req_linger);
1851        osdc->num_requests = 0;
1852        INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
1853        INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
1854        spin_lock_init(&osdc->event_lock);
1855        osdc->event_tree = RB_ROOT;
1856        osdc->event_count = 0;
1857
1858        schedule_delayed_work(&osdc->osds_timeout_work,
1859           round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
1860
1861        err = -ENOMEM;
1862        osdc->req_mempool = mempool_create_kmalloc_pool(10,
1863                                        sizeof(struct ceph_osd_request));
1864        if (!osdc->req_mempool)
1865                goto out;
1866
1867        err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
1868                                OSD_OP_FRONT_LEN, 10, true,
1869                                "osd_op");
1870        if (err < 0)
1871                goto out_mempool;
1872        err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
1873                                OSD_OPREPLY_FRONT_LEN, 10, true,
1874                                "osd_op_reply");
1875        if (err < 0)
1876                goto out_msgpool;
1877
1878        osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
1879        if (IS_ERR(osdc->notify_wq)) {
1880                err = PTR_ERR(osdc->notify_wq);
1881                osdc->notify_wq = NULL;
1882                goto out_msgpool;
1883        }
1884        return 0;
1885
1886out_msgpool:
1887        ceph_msgpool_destroy(&osdc->msgpool_op);
1888out_mempool:
1889        mempool_destroy(osdc->req_mempool);
1890out:
1891        return err;
1892}
1893EXPORT_SYMBOL(ceph_osdc_init);
1894
1895void ceph_osdc_stop(struct ceph_osd_client *osdc)
1896{
1897        flush_workqueue(osdc->notify_wq);
1898        destroy_workqueue(osdc->notify_wq);
1899        cancel_delayed_work_sync(&osdc->timeout_work);
1900        cancel_delayed_work_sync(&osdc->osds_timeout_work);
1901        if (osdc->osdmap) {
1902                ceph_osdmap_destroy(osdc->osdmap);
1903                osdc->osdmap = NULL;
1904        }
1905        remove_all_osds(osdc);
1906        mempool_destroy(osdc->req_mempool);
1907        ceph_msgpool_destroy(&osdc->msgpool_op);
1908        ceph_msgpool_destroy(&osdc->msgpool_op_reply);
1909}
1910EXPORT_SYMBOL(ceph_osdc_stop);
1911
1912/*
1913 * Read some contiguous pages.  If we cross a stripe boundary, shorten
1914 * *plen.  Return number of bytes read, or error.
1915 */
1916int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1917                        struct ceph_vino vino, struct ceph_file_layout *layout,
1918                        u64 off, u64 *plen,
1919                        u32 truncate_seq, u64 truncate_size,
1920                        struct page **pages, int num_pages, int page_align)
1921{
1922        struct ceph_osd_request *req;
1923        int rc = 0;
1924
1925        dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1926             vino.snap, off, *plen);
1927        req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1928                                    CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1929                                    NULL, 0, truncate_seq, truncate_size, NULL,
1930                                    false, 1, page_align);
1931        if (!req)
1932                return -ENOMEM;
1933
1934        /* it may be a short read due to an object boundary */
1935        req->r_pages = pages;
1936
1937        dout("readpages  final extent is %llu~%llu (%d pages align %d)\n",
1938             off, *plen, req->r_num_pages, page_align);
1939
1940        rc = ceph_osdc_start_request(osdc, req, false);
1941        if (!rc)
1942                rc = ceph_osdc_wait_request(osdc, req);
1943
1944        ceph_osdc_put_request(req);
1945        dout("readpages result %d\n", rc);
1946        return rc;
1947}
1948EXPORT_SYMBOL(ceph_osdc_readpages);
1949
1950/*
1951 * do a synchronous write on N pages
1952 */
1953int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
1954                         struct ceph_file_layout *layout,
1955                         struct ceph_snap_context *snapc,
1956                         u64 off, u64 len,
1957                         u32 truncate_seq, u64 truncate_size,
1958                         struct timespec *mtime,
1959                         struct page **pages, int num_pages,
1960                         int flags, int do_sync, bool nofail)
1961{
1962        struct ceph_osd_request *req;
1963        int rc = 0;
1964        int page_align = off & ~PAGE_MASK;
1965
1966        BUG_ON(vino.snap != CEPH_NOSNAP);
1967        req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
1968                                    CEPH_OSD_OP_WRITE,
1969                                    flags | CEPH_OSD_FLAG_ONDISK |
1970                                            CEPH_OSD_FLAG_WRITE,
1971                                    snapc, do_sync,
1972                                    truncate_seq, truncate_size, mtime,
1973                                    nofail, 1, page_align);
1974        if (!req)
1975                return -ENOMEM;
1976
1977        /* it may be a short write due to an object boundary */
1978        req->r_pages = pages;
1979        dout("writepages %llu~%llu (%d pages)\n", off, len,
1980             req->r_num_pages);
1981
1982        rc = ceph_osdc_start_request(osdc, req, nofail);
1983        if (!rc)
1984                rc = ceph_osdc_wait_request(osdc, req);
1985
1986        ceph_osdc_put_request(req);
1987        if (rc == 0)
1988                rc = len;
1989        dout("writepages result %d\n", rc);
1990        return rc;
1991}
1992EXPORT_SYMBOL(ceph_osdc_writepages);
1993
1994/*
1995 * handle incoming message
1996 */
1997static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
1998{
1999        struct ceph_osd *osd = con->private;
2000        struct ceph_osd_client *osdc;
2001        int type = le16_to_cpu(msg->hdr.type);
2002
2003        if (!osd)
2004                goto out;
2005        osdc = osd->o_osdc;
2006
2007        switch (type) {
2008        case CEPH_MSG_OSD_MAP:
2009                ceph_osdc_handle_map(osdc, msg);
2010                break;
2011        case CEPH_MSG_OSD_OPREPLY:
2012                handle_reply(osdc, msg, con);
2013                break;
2014        case CEPH_MSG_WATCH_NOTIFY:
2015                handle_watch_notify(osdc, msg);
2016                break;
2017
2018        default:
2019                pr_err("received unknown message type %d %s\n", type,
2020                       ceph_msg_type_name(type));
2021        }
2022out:
2023        ceph_msg_put(msg);
2024}
2025
2026/*
2027 * lookup and return message for incoming reply.  set up reply message
2028 * pages.
2029 */
2030static struct ceph_msg *get_reply(struct ceph_connection *con,
2031                                  struct ceph_msg_header *hdr,
2032                                  int *skip)
2033{
2034        struct ceph_osd *osd = con->private;
2035        struct ceph_osd_client *osdc = osd->o_osdc;
2036        struct ceph_msg *m;
2037        struct ceph_osd_request *req;
2038        int front = le32_to_cpu(hdr->front_len);
2039        int data_len = le32_to_cpu(hdr->data_len);
2040        u64 tid;
2041
2042        tid = le64_to_cpu(hdr->tid);
2043        mutex_lock(&osdc->request_mutex);
2044        req = __lookup_request(osdc, tid);
2045        if (!req) {
2046                *skip = 1;
2047                m = NULL;
2048                dout("get_reply unknown tid %llu from osd%d\n", tid,
2049                     osd->o_osd);
2050                goto out;
2051        }
2052
2053        if (req->r_con_filling_msg) {
2054                dout("%s revoking msg %p from old con %p\n", __func__,
2055                     req->r_reply, req->r_con_filling_msg);
2056                ceph_msg_revoke_incoming(req->r_reply);
2057                req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
2058                req->r_con_filling_msg = NULL;
2059        }
2060
2061        if (front > req->r_reply->front.iov_len) {
2062                pr_warning("get_reply front %d > preallocated %d\n",
2063                           front, (int)req->r_reply->front.iov_len);
2064                m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
2065                if (!m)
2066                        goto out;
2067                ceph_msg_put(req->r_reply);
2068                req->r_reply = m;
2069        }
2070        m = ceph_msg_get(req->r_reply);
2071
2072        if (data_len > 0) {
2073                int want = calc_pages_for(req->r_page_alignment, data_len);
2074
2075                if (unlikely(req->r_num_pages < want)) {
2076                        pr_warning("tid %lld reply has %d bytes %d pages, we"
2077                                   " had only %d pages ready\n", tid, data_len,
2078                                   want, req->r_num_pages);
2079                        *skip = 1;
2080                        ceph_msg_put(m);
2081                        m = NULL;
2082                        goto out;
2083                }
2084                m->pages = req->r_pages;
2085                m->nr_pages = req->r_num_pages;
2086                m->page_alignment = req->r_page_alignment;
2087#ifdef CONFIG_BLOCK
2088                m->bio = req->r_bio;
2089#endif
2090        }
2091        *skip = 0;
2092        req->r_con_filling_msg = con->ops->get(con);
2093        dout("get_reply tid %lld %p\n", tid, m);
2094
2095out:
2096        mutex_unlock(&osdc->request_mutex);
2097        return m;
2098
2099}
2100
2101static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2102                                  struct ceph_msg_header *hdr,
2103                                  int *skip)
2104{
2105        struct ceph_osd *osd = con->private;
2106        int type = le16_to_cpu(hdr->type);
2107        int front = le32_to_cpu(hdr->front_len);
2108
2109        *skip = 0;
2110        switch (type) {
2111        case CEPH_MSG_OSD_MAP:
2112        case CEPH_MSG_WATCH_NOTIFY:
2113                return ceph_msg_new(type, front, GFP_NOFS, false);
2114        case CEPH_MSG_OSD_OPREPLY:
2115                return get_reply(con, hdr, skip);
2116        default:
2117                pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2118                        osd->o_osd);
2119                *skip = 1;
2120                return NULL;
2121        }
2122}
2123
2124/*
2125 * Wrappers to refcount containing ceph_osd struct
2126 */
2127static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2128{
2129        struct ceph_osd *osd = con->private;
2130        if (get_osd(osd))
2131                return con;
2132        return NULL;
2133}
2134
2135static void put_osd_con(struct ceph_connection *con)
2136{
2137        struct ceph_osd *osd = con->private;
2138        put_osd(osd);
2139}
2140
2141/*
2142 * authentication
2143 */
2144/*
2145 * Note: returned pointer is the address of a structure that's
2146 * managed separately.  Caller must *not* attempt to free it.
2147 */
2148static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
2149                                        int *proto, int force_new)
2150{
2151        struct ceph_osd *o = con->private;
2152        struct ceph_osd_client *osdc = o->o_osdc;
2153        struct ceph_auth_client *ac = osdc->client->monc.auth;
2154        struct ceph_auth_handshake *auth = &o->o_auth;
2155
2156        if (force_new && auth->authorizer) {
2157                if (ac->ops && ac->ops->destroy_authorizer)
2158                        ac->ops->destroy_authorizer(ac, auth->authorizer);
2159                auth->authorizer = NULL;
2160        }
2161        if (!auth->authorizer && ac->ops && ac->ops->create_authorizer) {
2162                int ret = ac->ops->create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2163                                                        auth);
2164                if (ret)
2165                        return ERR_PTR(ret);
2166        }
2167        *proto = ac->protocol;
2168
2169        return auth;
2170}
2171
2172
2173static int verify_authorizer_reply(struct ceph_connection *con, int len)
2174{
2175        struct ceph_osd *o = con->private;
2176        struct ceph_osd_client *osdc = o->o_osdc;
2177        struct ceph_auth_client *ac = osdc->client->monc.auth;
2178
2179        /*
2180         * XXX If ac->ops or ac->ops->verify_authorizer_reply is null,
2181         * XXX which do we do:  succeed or fail?
2182         */
2183        return ac->ops->verify_authorizer_reply(ac, o->o_auth.authorizer, len);
2184}
2185
2186static int invalidate_authorizer(struct ceph_connection *con)
2187{
2188        struct ceph_osd *o = con->private;
2189        struct ceph_osd_client *osdc = o->o_osdc;
2190        struct ceph_auth_client *ac = osdc->client->monc.auth;
2191
2192        if (ac->ops && ac->ops->invalidate_authorizer)
2193                ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2194
2195        return ceph_monc_validate_auth(&osdc->client->monc);
2196}
2197
2198static const struct ceph_connection_operations osd_con_ops = {
2199        .get = get_osd_con,
2200        .put = put_osd_con,
2201        .dispatch = dispatch,
2202        .get_authorizer = get_authorizer,
2203        .verify_authorizer_reply = verify_authorizer_reply,
2204        .invalidate_authorizer = invalidate_authorizer,
2205        .alloc_msg = alloc_msg,
2206        .fault = osd_reset,
2207};
2208
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.