linux/net/ceph/osd_client.c
<<
>>
Prefs
   1
   2#include <linux/ceph/ceph_debug.h>
   3
   4#include <linux/module.h>
   5#include <linux/err.h>
   6#include <linux/highmem.h>
   7#include <linux/mm.h>
   8#include <linux/pagemap.h>
   9#include <linux/slab.h>
  10#include <linux/uaccess.h>
  11#ifdef CONFIG_BLOCK
  12#include <linux/bio.h>
  13#endif
  14
  15#include <linux/ceph/libceph.h>
  16#include <linux/ceph/osd_client.h>
  17#include <linux/ceph/messenger.h>
  18#include <linux/ceph/decode.h>
  19#include <linux/ceph/auth.h>
  20#include <linux/ceph/pagelist.h>
  21
  22#define OSD_OPREPLY_FRONT_LEN   512
  23
  24static struct kmem_cache        *ceph_osd_request_cache;
  25
  26static const struct ceph_connection_operations osd_con_ops;
  27
  28/*
  29 * Implement client access to distributed object storage cluster.
  30 *
  31 * All data objects are stored within a cluster/cloud of OSDs, or
  32 * "object storage devices."  (Note that Ceph OSDs have _nothing_ to
  33 * do with the T10 OSD extensions to SCSI.)  Ceph OSDs are simply
  34 * remote daemons serving up and coordinating consistent and safe
  35 * access to storage.
  36 *
  37 * Cluster membership and the mapping of data objects onto storage devices
  38 * are described by the osd map.
  39 *
  40 * We keep track of pending OSD requests (read, write), resubmit
  41 * requests to different OSDs when the cluster topology/data layout
  42 * change, or retry the affected requests when the communications
  43 * channel with an OSD is reset.
  44 */
  45
  46static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req);
  47static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req);
  48static void link_linger(struct ceph_osd *osd,
  49                        struct ceph_osd_linger_request *lreq);
  50static void unlink_linger(struct ceph_osd *osd,
  51                          struct ceph_osd_linger_request *lreq);
  52
  53#if 1
  54static inline bool rwsem_is_wrlocked(struct rw_semaphore *sem)
  55{
  56        bool wrlocked = true;
  57
  58        if (unlikely(down_read_trylock(sem))) {
  59                wrlocked = false;
  60                up_read(sem);
  61        }
  62
  63        return wrlocked;
  64}
  65static inline void verify_osdc_locked(struct ceph_osd_client *osdc)
  66{
  67        WARN_ON(!rwsem_is_locked(&osdc->lock));
  68}
  69static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc)
  70{
  71        WARN_ON(!rwsem_is_wrlocked(&osdc->lock));
  72}
  73static inline void verify_osd_locked(struct ceph_osd *osd)
  74{
  75        struct ceph_osd_client *osdc = osd->o_osdc;
  76
  77        WARN_ON(!(mutex_is_locked(&osd->lock) &&
  78                  rwsem_is_locked(&osdc->lock)) &&
  79                !rwsem_is_wrlocked(&osdc->lock));
  80}
  81static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq)
  82{
  83        WARN_ON(!mutex_is_locked(&lreq->lock));
  84}
  85#else
  86static inline void verify_osdc_locked(struct ceph_osd_client *osdc) { }
  87static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc) { }
  88static inline void verify_osd_locked(struct ceph_osd *osd) { }
  89static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq) { }
  90#endif
  91
  92/*
  93 * calculate the mapping of a file extent onto an object, and fill out the
  94 * request accordingly.  shorten extent as necessary if it crosses an
  95 * object boundary.
  96 *
  97 * fill osd op in request message.
  98 */
  99static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
 100                        u64 *objnum, u64 *objoff, u64 *objlen)
 101{
 102        u64 orig_len = *plen;
 103        int r;
 104
 105        /* object extent? */
 106        r = ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
 107                                          objoff, objlen);
 108        if (r < 0)
 109                return r;
 110        if (*objlen < orig_len) {
 111                *plen = *objlen;
 112                dout(" skipping last %llu, final file extent %llu~%llu\n",
 113                     orig_len - *plen, off, *plen);
 114        }
 115
 116        dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
 117
 118        return 0;
 119}
 120
 121static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
 122{
 123        memset(osd_data, 0, sizeof (*osd_data));
 124        osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
 125}
 126
 127static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
 128                        struct page **pages, u64 length, u32 alignment,
 129                        bool pages_from_pool, bool own_pages)
 130{
 131        osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
 132        osd_data->pages = pages;
 133        osd_data->length = length;
 134        osd_data->alignment = alignment;
 135        osd_data->pages_from_pool = pages_from_pool;
 136        osd_data->own_pages = own_pages;
 137}
 138
 139static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
 140                        struct ceph_pagelist *pagelist)
 141{
 142        osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
 143        osd_data->pagelist = pagelist;
 144}
 145
 146#ifdef CONFIG_BLOCK
 147static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
 148                        struct bio *bio, size_t bio_length)
 149{
 150        osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
 151        osd_data->bio = bio;
 152        osd_data->bio_length = bio_length;
 153}
 154#endif /* CONFIG_BLOCK */
 155
 156#define osd_req_op_data(oreq, whch, typ, fld)                           \
 157({                                                                      \
 158        struct ceph_osd_request *__oreq = (oreq);                       \
 159        unsigned int __whch = (whch);                                   \
 160        BUG_ON(__whch >= __oreq->r_num_ops);                            \
 161        &__oreq->r_ops[__whch].typ.fld;                                 \
 162})
 163
 164static struct ceph_osd_data *
 165osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
 166{
 167        BUG_ON(which >= osd_req->r_num_ops);
 168
 169        return &osd_req->r_ops[which].raw_data_in;
 170}
 171
 172struct ceph_osd_data *
 173osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
 174                        unsigned int which)
 175{
 176        return osd_req_op_data(osd_req, which, extent, osd_data);
 177}
 178EXPORT_SYMBOL(osd_req_op_extent_osd_data);
 179
 180void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
 181                        unsigned int which, struct page **pages,
 182                        u64 length, u32 alignment,
 183                        bool pages_from_pool, bool own_pages)
 184{
 185        struct ceph_osd_data *osd_data;
 186
 187        osd_data = osd_req_op_raw_data_in(osd_req, which);
 188        ceph_osd_data_pages_init(osd_data, pages, length, alignment,
 189                                pages_from_pool, own_pages);
 190}
 191EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
 192
 193void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
 194                        unsigned int which, struct page **pages,
 195                        u64 length, u32 alignment,
 196                        bool pages_from_pool, bool own_pages)
 197{
 198        struct ceph_osd_data *osd_data;
 199
 200        osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
 201        ceph_osd_data_pages_init(osd_data, pages, length, alignment,
 202                                pages_from_pool, own_pages);
 203}
 204EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
 205
 206void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
 207                        unsigned int which, struct ceph_pagelist *pagelist)
 208{
 209        struct ceph_osd_data *osd_data;
 210
 211        osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
 212        ceph_osd_data_pagelist_init(osd_data, pagelist);
 213}
 214EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
 215
 216#ifdef CONFIG_BLOCK
 217void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
 218                        unsigned int which, struct bio *bio, size_t bio_length)
 219{
 220        struct ceph_osd_data *osd_data;
 221
 222        osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
 223        ceph_osd_data_bio_init(osd_data, bio, bio_length);
 224}
 225EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
 226#endif /* CONFIG_BLOCK */
 227
 228static void osd_req_op_cls_request_info_pagelist(
 229                        struct ceph_osd_request *osd_req,
 230                        unsigned int which, struct ceph_pagelist *pagelist)
 231{
 232        struct ceph_osd_data *osd_data;
 233
 234        osd_data = osd_req_op_data(osd_req, which, cls, request_info);
 235        ceph_osd_data_pagelist_init(osd_data, pagelist);
 236}
 237
 238void osd_req_op_cls_request_data_pagelist(
 239                        struct ceph_osd_request *osd_req,
 240                        unsigned int which, struct ceph_pagelist *pagelist)
 241{
 242        struct ceph_osd_data *osd_data;
 243
 244        osd_data = osd_req_op_data(osd_req, which, cls, request_data);
 245        ceph_osd_data_pagelist_init(osd_data, pagelist);
 246        osd_req->r_ops[which].cls.indata_len += pagelist->length;
 247        osd_req->r_ops[which].indata_len += pagelist->length;
 248}
 249EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
 250
 251void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
 252                        unsigned int which, struct page **pages, u64 length,
 253                        u32 alignment, bool pages_from_pool, bool own_pages)
 254{
 255        struct ceph_osd_data *osd_data;
 256
 257        osd_data = osd_req_op_data(osd_req, which, cls, request_data);
 258        ceph_osd_data_pages_init(osd_data, pages, length, alignment,
 259                                pages_from_pool, own_pages);
 260        osd_req->r_ops[which].cls.indata_len += length;
 261        osd_req->r_ops[which].indata_len += length;
 262}
 263EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
 264
 265void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
 266                        unsigned int which, struct page **pages, u64 length,
 267                        u32 alignment, bool pages_from_pool, bool own_pages)
 268{
 269        struct ceph_osd_data *osd_data;
 270
 271        osd_data = osd_req_op_data(osd_req, which, cls, response_data);
 272        ceph_osd_data_pages_init(osd_data, pages, length, alignment,
 273                                pages_from_pool, own_pages);
 274}
 275EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
 276
 277static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
 278{
 279        switch (osd_data->type) {
 280        case CEPH_OSD_DATA_TYPE_NONE:
 281                return 0;
 282        case CEPH_OSD_DATA_TYPE_PAGES:
 283                return osd_data->length;
 284        case CEPH_OSD_DATA_TYPE_PAGELIST:
 285                return (u64)osd_data->pagelist->length;
 286#ifdef CONFIG_BLOCK
 287        case CEPH_OSD_DATA_TYPE_BIO:
 288                return (u64)osd_data->bio_length;
 289#endif /* CONFIG_BLOCK */
 290        default:
 291                WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
 292                return 0;
 293        }
 294}
 295
 296static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
 297{
 298        if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
 299                int num_pages;
 300
 301                num_pages = calc_pages_for((u64)osd_data->alignment,
 302                                                (u64)osd_data->length);
 303                ceph_release_page_vector(osd_data->pages, num_pages);
 304        }
 305        ceph_osd_data_init(osd_data);
 306}
 307
 308static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
 309                        unsigned int which)
 310{
 311        struct ceph_osd_req_op *op;
 312
 313        BUG_ON(which >= osd_req->r_num_ops);
 314        op = &osd_req->r_ops[which];
 315
 316        switch (op->op) {
 317        case CEPH_OSD_OP_READ:
 318        case CEPH_OSD_OP_WRITE:
 319        case CEPH_OSD_OP_WRITEFULL:
 320                ceph_osd_data_release(&op->extent.osd_data);
 321                break;
 322        case CEPH_OSD_OP_CALL:
 323                ceph_osd_data_release(&op->cls.request_info);
 324                ceph_osd_data_release(&op->cls.request_data);
 325                ceph_osd_data_release(&op->cls.response_data);
 326                break;
 327        case CEPH_OSD_OP_SETXATTR:
 328        case CEPH_OSD_OP_CMPXATTR:
 329                ceph_osd_data_release(&op->xattr.osd_data);
 330                break;
 331        case CEPH_OSD_OP_STAT:
 332                ceph_osd_data_release(&op->raw_data_in);
 333                break;
 334        case CEPH_OSD_OP_NOTIFY_ACK:
 335                ceph_osd_data_release(&op->notify_ack.request_data);
 336                break;
 337        case CEPH_OSD_OP_NOTIFY:
 338                ceph_osd_data_release(&op->notify.request_data);
 339                ceph_osd_data_release(&op->notify.response_data);
 340                break;
 341        case CEPH_OSD_OP_LIST_WATCHERS:
 342                ceph_osd_data_release(&op->list_watchers.response_data);
 343                break;
 344        default:
 345                break;
 346        }
 347}
 348
 349/*
 350 * Assumes @t is zero-initialized.
 351 */
 352static void target_init(struct ceph_osd_request_target *t)
 353{
 354        ceph_oid_init(&t->base_oid);
 355        ceph_oloc_init(&t->base_oloc);
 356        ceph_oid_init(&t->target_oid);
 357        ceph_oloc_init(&t->target_oloc);
 358
 359        ceph_osds_init(&t->acting);
 360        ceph_osds_init(&t->up);
 361        t->size = -1;
 362        t->min_size = -1;
 363
 364        t->osd = CEPH_HOMELESS_OSD;
 365}
 366
 367static void target_copy(struct ceph_osd_request_target *dest,
 368                        const struct ceph_osd_request_target *src)
 369{
 370        ceph_oid_copy(&dest->base_oid, &src->base_oid);
 371        ceph_oloc_copy(&dest->base_oloc, &src->base_oloc);
 372        ceph_oid_copy(&dest->target_oid, &src->target_oid);
 373        ceph_oloc_copy(&dest->target_oloc, &src->target_oloc);
 374
 375        dest->pgid = src->pgid; /* struct */
 376        dest->pg_num = src->pg_num;
 377        dest->pg_num_mask = src->pg_num_mask;
 378        ceph_osds_copy(&dest->acting, &src->acting);
 379        ceph_osds_copy(&dest->up, &src->up);
 380        dest->size = src->size;
 381        dest->min_size = src->min_size;
 382        dest->sort_bitwise = src->sort_bitwise;
 383
 384        dest->flags = src->flags;
 385        dest->paused = src->paused;
 386
 387        dest->osd = src->osd;
 388}
 389
 390static void target_destroy(struct ceph_osd_request_target *t)
 391{
 392        ceph_oid_destroy(&t->base_oid);
 393        ceph_oloc_destroy(&t->base_oloc);
 394        ceph_oid_destroy(&t->target_oid);
 395        ceph_oloc_destroy(&t->target_oloc);
 396}
 397
 398/*
 399 * requests
 400 */
 401static void request_release_checks(struct ceph_osd_request *req)
 402{
 403        WARN_ON(!RB_EMPTY_NODE(&req->r_node));
 404        WARN_ON(!RB_EMPTY_NODE(&req->r_mc_node));
 405        WARN_ON(!list_empty(&req->r_unsafe_item));
 406        WARN_ON(req->r_osd);
 407}
 408
 409static void ceph_osdc_release_request(struct kref *kref)
 410{
 411        struct ceph_osd_request *req = container_of(kref,
 412                                            struct ceph_osd_request, r_kref);
 413        unsigned int which;
 414
 415        dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
 416             req->r_request, req->r_reply);
 417        request_release_checks(req);
 418
 419        if (req->r_request)
 420                ceph_msg_put(req->r_request);
 421        if (req->r_reply)
 422                ceph_msg_put(req->r_reply);
 423
 424        for (which = 0; which < req->r_num_ops; which++)
 425                osd_req_op_data_release(req, which);
 426
 427        target_destroy(&req->r_t);
 428        ceph_put_snap_context(req->r_snapc);
 429
 430        if (req->r_mempool)
 431                mempool_free(req, req->r_osdc->req_mempool);
 432        else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS)
 433                kmem_cache_free(ceph_osd_request_cache, req);
 434        else
 435                kfree(req);
 436}
 437
 438void ceph_osdc_get_request(struct ceph_osd_request *req)
 439{
 440        dout("%s %p (was %d)\n", __func__, req,
 441             atomic_read(&req->r_kref.refcount));
 442        kref_get(&req->r_kref);
 443}
 444EXPORT_SYMBOL(ceph_osdc_get_request);
 445
 446void ceph_osdc_put_request(struct ceph_osd_request *req)
 447{
 448        if (req) {
 449                dout("%s %p (was %d)\n", __func__, req,
 450                     atomic_read(&req->r_kref.refcount));
 451                kref_put(&req->r_kref, ceph_osdc_release_request);
 452        }
 453}
 454EXPORT_SYMBOL(ceph_osdc_put_request);
 455
 456static void request_init(struct ceph_osd_request *req)
 457{
 458        /* req only, each op is zeroed in _osd_req_op_init() */
 459        memset(req, 0, sizeof(*req));
 460
 461        kref_init(&req->r_kref);
 462        init_completion(&req->r_completion);
 463        init_completion(&req->r_done_completion);
 464        RB_CLEAR_NODE(&req->r_node);
 465        RB_CLEAR_NODE(&req->r_mc_node);
 466        INIT_LIST_HEAD(&req->r_unsafe_item);
 467
 468        target_init(&req->r_t);
 469}
 470
 471/*
 472 * This is ugly, but it allows us to reuse linger registration and ping
 473 * requests, keeping the structure of the code around send_linger{_ping}()
 474 * reasonable.  Setting up a min_nr=2 mempool for each linger request
 475 * and dealing with copying ops (this blasts req only, watch op remains
 476 * intact) isn't any better.
 477 */
 478static void request_reinit(struct ceph_osd_request *req)
 479{
 480        struct ceph_osd_client *osdc = req->r_osdc;
 481        bool mempool = req->r_mempool;
 482        unsigned int num_ops = req->r_num_ops;
 483        u64 snapid = req->r_snapid;
 484        struct ceph_snap_context *snapc = req->r_snapc;
 485        bool linger = req->r_linger;
 486        struct ceph_msg *request_msg = req->r_request;
 487        struct ceph_msg *reply_msg = req->r_reply;
 488
 489        dout("%s req %p\n", __func__, req);
 490        WARN_ON(atomic_read(&req->r_kref.refcount) != 1);
 491        request_release_checks(req);
 492
 493        WARN_ON(atomic_read(&request_msg->kref.refcount) != 1);
 494        WARN_ON(atomic_read(&reply_msg->kref.refcount) != 1);
 495        target_destroy(&req->r_t);
 496
 497        request_init(req);
 498        req->r_osdc = osdc;
 499        req->r_mempool = mempool;
 500        req->r_num_ops = num_ops;
 501        req->r_snapid = snapid;
 502        req->r_snapc = snapc;
 503        req->r_linger = linger;
 504        req->r_request = request_msg;
 505        req->r_reply = reply_msg;
 506}
 507
 508struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
 509                                               struct ceph_snap_context *snapc,
 510                                               unsigned int num_ops,
 511                                               bool use_mempool,
 512                                               gfp_t gfp_flags)
 513{
 514        struct ceph_osd_request *req;
 515
 516        if (use_mempool) {
 517                BUG_ON(num_ops > CEPH_OSD_SLAB_OPS);
 518                req = mempool_alloc(osdc->req_mempool, gfp_flags);
 519        } else if (num_ops <= CEPH_OSD_SLAB_OPS) {
 520                req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
 521        } else {
 522                BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
 523                req = kmalloc(sizeof(*req) + num_ops * sizeof(req->r_ops[0]),
 524                              gfp_flags);
 525        }
 526        if (unlikely(!req))
 527                return NULL;
 528
 529        request_init(req);
 530        req->r_osdc = osdc;
 531        req->r_mempool = use_mempool;
 532        req->r_num_ops = num_ops;
 533        req->r_snapid = CEPH_NOSNAP;
 534        req->r_snapc = ceph_get_snap_context(snapc);
 535
 536        dout("%s req %p\n", __func__, req);
 537        return req;
 538}
 539EXPORT_SYMBOL(ceph_osdc_alloc_request);
 540
 541static int ceph_oloc_encoding_size(struct ceph_object_locator *oloc)
 542{
 543        return 8 + 4 + 4 + 4 + (oloc->pool_ns ? oloc->pool_ns->len : 0);
 544}
 545
 546int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp)
 547{
 548        struct ceph_osd_client *osdc = req->r_osdc;
 549        struct ceph_msg *msg;
 550        int msg_size;
 551
 552        WARN_ON(ceph_oid_empty(&req->r_base_oid));
 553        WARN_ON(ceph_oloc_empty(&req->r_base_oloc));
 554
 555        /* create request message */
 556        msg_size = 4 + 4 + 4; /* client_inc, osdmap_epoch, flags */
 557        msg_size += 4 + 4 + 4 + 8; /* mtime, reassert_version */
 558        msg_size += CEPH_ENCODING_START_BLK_LEN +
 559                        ceph_oloc_encoding_size(&req->r_base_oloc); /* oloc */
 560        msg_size += 1 + 8 + 4 + 4; /* pgid */
 561        msg_size += 4 + req->r_base_oid.name_len; /* oid */
 562        msg_size += 2 + req->r_num_ops * sizeof(struct ceph_osd_op);
 563        msg_size += 8; /* snapid */
 564        msg_size += 8; /* snap_seq */
 565        msg_size += 4 + 8 * (req->r_snapc ? req->r_snapc->num_snaps : 0);
 566        msg_size += 4; /* retry_attempt */
 567
 568        if (req->r_mempool)
 569                msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
 570        else
 571                msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp, true);
 572        if (!msg)
 573                return -ENOMEM;
 574
 575        memset(msg->front.iov_base, 0, msg->front.iov_len);
 576        req->r_request = msg;
 577
 578        /* create reply message */
 579        msg_size = OSD_OPREPLY_FRONT_LEN;
 580        msg_size += req->r_base_oid.name_len;
 581        msg_size += req->r_num_ops * sizeof(struct ceph_osd_op);
 582
 583        if (req->r_mempool)
 584                msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
 585        else
 586                msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, msg_size, gfp, true);
 587        if (!msg)
 588                return -ENOMEM;
 589
 590        req->r_reply = msg;
 591
 592        return 0;
 593}
 594EXPORT_SYMBOL(ceph_osdc_alloc_messages);
 595
 596static bool osd_req_opcode_valid(u16 opcode)
 597{
 598        switch (opcode) {
 599#define GENERATE_CASE(op, opcode, str)  case CEPH_OSD_OP_##op: return true;
 600__CEPH_FORALL_OSD_OPS(GENERATE_CASE)
 601#undef GENERATE_CASE
 602        default:
 603                return false;
 604        }
 605}
 606
 607/*
 608 * This is an osd op init function for opcodes that have no data or
 609 * other information associated with them.  It also serves as a
 610 * common init routine for all the other init functions, below.
 611 */
 612static struct ceph_osd_req_op *
 613_osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
 614                 u16 opcode, u32 flags)
 615{
 616        struct ceph_osd_req_op *op;
 617
 618        BUG_ON(which >= osd_req->r_num_ops);
 619        BUG_ON(!osd_req_opcode_valid(opcode));
 620
 621        op = &osd_req->r_ops[which];
 622        memset(op, 0, sizeof (*op));
 623        op->op = opcode;
 624        op->flags = flags;
 625
 626        return op;
 627}
 628
 629void osd_req_op_init(struct ceph_osd_request *osd_req,
 630                     unsigned int which, u16 opcode, u32 flags)
 631{
 632        (void)_osd_req_op_init(osd_req, which, opcode, flags);
 633}
 634EXPORT_SYMBOL(osd_req_op_init);
 635
 636void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
 637                                unsigned int which, u16 opcode,
 638                                u64 offset, u64 length,
 639                                u64 truncate_size, u32 truncate_seq)
 640{
 641        struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
 642                                                      opcode, 0);
 643        size_t payload_len = 0;
 644
 645        BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
 646               opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
 647               opcode != CEPH_OSD_OP_TRUNCATE);
 648
 649        op->extent.offset = offset;
 650        op->extent.length = length;
 651        op->extent.truncate_size = truncate_size;
 652        op->extent.truncate_seq = truncate_seq;
 653        if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
 654                payload_len += length;
 655
 656        op->indata_len = payload_len;
 657}
 658EXPORT_SYMBOL(osd_req_op_extent_init);
 659
 660void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
 661                                unsigned int which, u64 length)
 662{
 663        struct ceph_osd_req_op *op;
 664        u64 previous;
 665
 666        BUG_ON(which >= osd_req->r_num_ops);
 667        op = &osd_req->r_ops[which];
 668        previous = op->extent.length;
 669
 670        if (length == previous)
 671                return;         /* Nothing to do */
 672        BUG_ON(length > previous);
 673
 674        op->extent.length = length;
 675        op->indata_len -= previous - length;
 676}
 677EXPORT_SYMBOL(osd_req_op_extent_update);
 678
 679void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
 680                                unsigned int which, u64 offset_inc)
 681{
 682        struct ceph_osd_req_op *op, *prev_op;
 683
 684        BUG_ON(which + 1 >= osd_req->r_num_ops);
 685
 686        prev_op = &osd_req->r_ops[which];
 687        op = _osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags);
 688        /* dup previous one */
 689        op->indata_len = prev_op->indata_len;
 690        op->outdata_len = prev_op->outdata_len;
 691        op->extent = prev_op->extent;
 692        /* adjust offset */
 693        op->extent.offset += offset_inc;
 694        op->extent.length -= offset_inc;
 695
 696        if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
 697                op->indata_len -= offset_inc;
 698}
 699EXPORT_SYMBOL(osd_req_op_extent_dup_last);
 700
 701void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
 702                        u16 opcode, const char *class, const char *method)
 703{
 704        struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
 705                                                      opcode, 0);
 706        struct ceph_pagelist *pagelist;
 707        size_t payload_len = 0;
 708        size_t size;
 709
 710        BUG_ON(opcode != CEPH_OSD_OP_CALL);
 711
 712        pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
 713        BUG_ON(!pagelist);
 714        ceph_pagelist_init(pagelist);
 715
 716        op->cls.class_name = class;
 717        size = strlen(class);
 718        BUG_ON(size > (size_t) U8_MAX);
 719        op->cls.class_len = size;
 720        ceph_pagelist_append(pagelist, class, size);
 721        payload_len += size;
 722
 723        op->cls.method_name = method;
 724        size = strlen(method);
 725        BUG_ON(size > (size_t) U8_MAX);
 726        op->cls.method_len = size;
 727        ceph_pagelist_append(pagelist, method, size);
 728        payload_len += size;
 729
 730        osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
 731
 732        op->indata_len = payload_len;
 733}
 734EXPORT_SYMBOL(osd_req_op_cls_init);
 735
 736int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
 737                          u16 opcode, const char *name, const void *value,
 738                          size_t size, u8 cmp_op, u8 cmp_mode)
 739{
 740        struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
 741                                                      opcode, 0);
 742        struct ceph_pagelist *pagelist;
 743        size_t payload_len;
 744
 745        BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
 746
 747        pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
 748        if (!pagelist)
 749                return -ENOMEM;
 750
 751        ceph_pagelist_init(pagelist);
 752
 753        payload_len = strlen(name);
 754        op->xattr.name_len = payload_len;
 755        ceph_pagelist_append(pagelist, name, payload_len);
 756
 757        op->xattr.value_len = size;
 758        ceph_pagelist_append(pagelist, value, size);
 759        payload_len += size;
 760
 761        op->xattr.cmp_op = cmp_op;
 762        op->xattr.cmp_mode = cmp_mode;
 763
 764        ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
 765        op->indata_len = payload_len;
 766        return 0;
 767}
 768EXPORT_SYMBOL(osd_req_op_xattr_init);
 769
 770/*
 771 * @watch_opcode: CEPH_OSD_WATCH_OP_*
 772 */
 773static void osd_req_op_watch_init(struct ceph_osd_request *req, int which,
 774                                  u64 cookie, u8 watch_opcode)
 775{
 776        struct ceph_osd_req_op *op;
 777
 778        op = _osd_req_op_init(req, which, CEPH_OSD_OP_WATCH, 0);
 779        op->watch.cookie = cookie;
 780        op->watch.op = watch_opcode;
 781        op->watch.gen = 0;
 782}
 783
 784void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
 785                                unsigned int which,
 786                                u64 expected_object_size,
 787                                u64 expected_write_size)
 788{
 789        struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
 790                                                      CEPH_OSD_OP_SETALLOCHINT,
 791                                                      0);
 792
 793        op->alloc_hint.expected_object_size = expected_object_size;
 794        op->alloc_hint.expected_write_size = expected_write_size;
 795
 796        /*
 797         * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
 798         * not worth a feature bit.  Set FAILOK per-op flag to make
 799         * sure older osds don't trip over an unsupported opcode.
 800         */
 801        op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
 802}
 803EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
 804
 805static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
 806                                struct ceph_osd_data *osd_data)
 807{
 808        u64 length = ceph_osd_data_length(osd_data);
 809
 810        if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
 811                BUG_ON(length > (u64) SIZE_MAX);
 812                if (length)
 813                        ceph_msg_data_add_pages(msg, osd_data->pages,
 814                                        length, osd_data->alignment);
 815        } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
 816                BUG_ON(!length);
 817                ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
 818#ifdef CONFIG_BLOCK
 819        } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
 820                ceph_msg_data_add_bio(msg, osd_data->bio, length);
 821#endif
 822        } else {
 823                BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
 824        }
 825}
 826
 827static u32 osd_req_encode_op(struct ceph_osd_op *dst,
 828                             const struct ceph_osd_req_op *src)
 829{
 830        if (WARN_ON(!osd_req_opcode_valid(src->op))) {
 831                pr_err("unrecognized osd opcode %d\n", src->op);
 832
 833                return 0;
 834        }
 835
 836        switch (src->op) {
 837        case CEPH_OSD_OP_STAT:
 838                break;
 839        case CEPH_OSD_OP_READ:
 840        case CEPH_OSD_OP_WRITE:
 841        case CEPH_OSD_OP_WRITEFULL:
 842        case CEPH_OSD_OP_ZERO:
 843        case CEPH_OSD_OP_TRUNCATE:
 844                dst->extent.offset = cpu_to_le64(src->extent.offset);
 845                dst->extent.length = cpu_to_le64(src->extent.length);
 846                dst->extent.truncate_size =
 847                        cpu_to_le64(src->extent.truncate_size);
 848                dst->extent.truncate_seq =
 849                        cpu_to_le32(src->extent.truncate_seq);
 850                break;
 851        case CEPH_OSD_OP_CALL:
 852                dst->cls.class_len = src->cls.class_len;
 853                dst->cls.method_len = src->cls.method_len;
 854                dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
 855                break;
 856        case CEPH_OSD_OP_STARTSYNC:
 857                break;
 858        case CEPH_OSD_OP_WATCH:
 859                dst->watch.cookie = cpu_to_le64(src->watch.cookie);
 860                dst->watch.ver = cpu_to_le64(0);
 861                dst->watch.op = src->watch.op;
 862                dst->watch.gen = cpu_to_le32(src->watch.gen);
 863                break;
 864        case CEPH_OSD_OP_NOTIFY_ACK:
 865                break;
 866        case CEPH_OSD_OP_NOTIFY:
 867                dst->notify.cookie = cpu_to_le64(src->notify.cookie);
 868                break;
 869        case CEPH_OSD_OP_LIST_WATCHERS:
 870                break;
 871        case CEPH_OSD_OP_SETALLOCHINT:
 872                dst->alloc_hint.expected_object_size =
 873                    cpu_to_le64(src->alloc_hint.expected_object_size);
 874                dst->alloc_hint.expected_write_size =
 875                    cpu_to_le64(src->alloc_hint.expected_write_size);
 876                break;
 877        case CEPH_OSD_OP_SETXATTR:
 878        case CEPH_OSD_OP_CMPXATTR:
 879                dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
 880                dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
 881                dst->xattr.cmp_op = src->xattr.cmp_op;
 882                dst->xattr.cmp_mode = src->xattr.cmp_mode;
 883                break;
 884        case CEPH_OSD_OP_CREATE:
 885        case CEPH_OSD_OP_DELETE:
 886                break;
 887        default:
 888                pr_err("unsupported osd opcode %s\n",
 889                        ceph_osd_op_name(src->op));
 890                WARN_ON(1);
 891
 892                return 0;
 893        }
 894
 895        dst->op = cpu_to_le16(src->op);
 896        dst->flags = cpu_to_le32(src->flags);
 897        dst->payload_len = cpu_to_le32(src->indata_len);
 898
 899        return src->indata_len;
 900}
 901
 902/*
 903 * build new request AND message, calculate layout, and adjust file
 904 * extent as needed.
 905 *
 906 * if the file was recently truncated, we include information about its
 907 * old and new size so that the object can be updated appropriately.  (we
 908 * avoid synchronously deleting truncated objects because it's slow.)
 909 *
 910 * if @do_sync, include a 'startsync' command so that the osd will flush
 911 * data quickly.
 912 */
 913struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
 914                                               struct ceph_file_layout *layout,
 915                                               struct ceph_vino vino,
 916                                               u64 off, u64 *plen,
 917                                               unsigned int which, int num_ops,
 918                                               int opcode, int flags,
 919                                               struct ceph_snap_context *snapc,
 920                                               u32 truncate_seq,
 921                                               u64 truncate_size,
 922                                               bool use_mempool)
 923{
 924        struct ceph_osd_request *req;
 925        u64 objnum = 0;
 926        u64 objoff = 0;
 927        u64 objlen = 0;
 928        int r;
 929
 930        BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
 931               opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
 932               opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
 933
 934        req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
 935                                        GFP_NOFS);
 936        if (!req) {
 937                r = -ENOMEM;
 938                goto fail;
 939        }
 940
 941        /* calculate max write size */
 942        r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
 943        if (r)
 944                goto fail;
 945
 946        if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
 947                osd_req_op_init(req, which, opcode, 0);
 948        } else {
 949                u32 object_size = layout->object_size;
 950                u32 object_base = off - objoff;
 951                if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
 952                        if (truncate_size <= object_base) {
 953                                truncate_size = 0;
 954                        } else {
 955                                truncate_size -= object_base;
 956                                if (truncate_size > object_size)
 957                                        truncate_size = object_size;
 958                        }
 959                }
 960                osd_req_op_extent_init(req, which, opcode, objoff, objlen,
 961                                       truncate_size, truncate_seq);
 962        }
 963
 964        req->r_flags = flags;
 965        req->r_base_oloc.pool = layout->pool_id;
 966        req->r_base_oloc.pool_ns = ceph_try_get_string(layout->pool_ns);
 967        ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum);
 968
 969        req->r_snapid = vino.snap;
 970        if (flags & CEPH_OSD_FLAG_WRITE)
 971                req->r_data_offset = off;
 972
 973        r = ceph_osdc_alloc_messages(req, GFP_NOFS);
 974        if (r)
 975                goto fail;
 976
 977        return req;
 978
 979fail:
 980        ceph_osdc_put_request(req);
 981        return ERR_PTR(r);
 982}
 983EXPORT_SYMBOL(ceph_osdc_new_request);
 984
 985/*
 986 * We keep osd requests in an rbtree, sorted by ->r_tid.
 987 */
 988DEFINE_RB_FUNCS(request, struct ceph_osd_request, r_tid, r_node)
 989DEFINE_RB_FUNCS(request_mc, struct ceph_osd_request, r_tid, r_mc_node)
 990
 991static bool osd_homeless(struct ceph_osd *osd)
 992{
 993        return osd->o_osd == CEPH_HOMELESS_OSD;
 994}
 995
 996static bool osd_registered(struct ceph_osd *osd)
 997{
 998        verify_osdc_locked(osd->o_osdc);
 999
1000        return !RB_EMPTY_NODE(&osd->o_node);
1001}
1002
1003/*
1004 * Assumes @osd is zero-initialized.
1005 */
1006static void osd_init(struct ceph_osd *osd)
1007{
1008        atomic_set(&osd->o_ref, 1);
1009        RB_CLEAR_NODE(&osd->o_node);
1010        osd->o_requests = RB_ROOT;
1011        osd->o_linger_requests = RB_ROOT;
1012        INIT_LIST_HEAD(&osd->o_osd_lru);
1013        INIT_LIST_HEAD(&osd->o_keepalive_item);
1014        osd->o_incarnation = 1;
1015        mutex_init(&osd->lock);
1016}
1017
1018static void osd_cleanup(struct ceph_osd *osd)
1019{
1020        WARN_ON(!RB_EMPTY_NODE(&osd->o_node));
1021        WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
1022        WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
1023        WARN_ON(!list_empty(&osd->o_osd_lru));
1024        WARN_ON(!list_empty(&osd->o_keepalive_item));
1025
1026        if (osd->o_auth.authorizer) {
1027                WARN_ON(osd_homeless(osd));
1028                ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
1029        }
1030}
1031
1032/*
1033 * Track open sessions with osds.
1034 */
1035static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
1036{
1037        struct ceph_osd *osd;
1038
1039        WARN_ON(onum == CEPH_HOMELESS_OSD);
1040
1041        osd = kzalloc(sizeof(*osd), GFP_NOIO | __GFP_NOFAIL);
1042        osd_init(osd);
1043        osd->o_osdc = osdc;
1044        osd->o_osd = onum;
1045
1046        ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
1047
1048        return osd;
1049}
1050
1051static struct ceph_osd *get_osd(struct ceph_osd *osd)
1052{
1053        if (atomic_inc_not_zero(&osd->o_ref)) {
1054                dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
1055                     atomic_read(&osd->o_ref));
1056                return osd;
1057        } else {
1058                dout("get_osd %p FAIL\n", osd);
1059                return NULL;
1060        }
1061}
1062
1063static void put_osd(struct ceph_osd *osd)
1064{
1065        dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
1066             atomic_read(&osd->o_ref) - 1);
1067        if (atomic_dec_and_test(&osd->o_ref)) {
1068                osd_cleanup(osd);
1069                kfree(osd);
1070        }
1071}
1072
1073DEFINE_RB_FUNCS(osd, struct ceph_osd, o_osd, o_node)
1074
1075static void __move_osd_to_lru(struct ceph_osd *osd)
1076{
1077        struct ceph_osd_client *osdc = osd->o_osdc;
1078
1079        dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1080        BUG_ON(!list_empty(&osd->o_osd_lru));
1081
1082        spin_lock(&osdc->osd_lru_lock);
1083        list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
1084        spin_unlock(&osdc->osd_lru_lock);
1085
1086        osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
1087}
1088
1089static void maybe_move_osd_to_lru(struct ceph_osd *osd)
1090{
1091        if (RB_EMPTY_ROOT(&osd->o_requests) &&
1092            RB_EMPTY_ROOT(&osd->o_linger_requests))
1093                __move_osd_to_lru(osd);
1094}
1095
1096static void __remove_osd_from_lru(struct ceph_osd *osd)
1097{
1098        struct ceph_osd_client *osdc = osd->o_osdc;
1099
1100        dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1101
1102        spin_lock(&osdc->osd_lru_lock);
1103        if (!list_empty(&osd->o_osd_lru))
1104                list_del_init(&osd->o_osd_lru);
1105        spin_unlock(&osdc->osd_lru_lock);
1106}
1107
1108/*
1109 * Close the connection and assign any leftover requests to the
1110 * homeless session.
1111 */
1112static void close_osd(struct ceph_osd *osd)
1113{
1114        struct ceph_osd_client *osdc = osd->o_osdc;
1115        struct rb_node *n;
1116
1117        verify_osdc_wrlocked(osdc);
1118        dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1119
1120        ceph_con_close(&osd->o_con);
1121
1122        for (n = rb_first(&osd->o_requests); n; ) {
1123                struct ceph_osd_request *req =
1124                    rb_entry(n, struct ceph_osd_request, r_node);
1125
1126                n = rb_next(n); /* unlink_request() */
1127
1128                dout(" reassigning req %p tid %llu\n", req, req->r_tid);
1129                unlink_request(osd, req);
1130                link_request(&osdc->homeless_osd, req);
1131        }
1132        for (n = rb_first(&osd->o_linger_requests); n; ) {
1133                struct ceph_osd_linger_request *lreq =
1134                    rb_entry(n, struct ceph_osd_linger_request, node);
1135
1136                n = rb_next(n); /* unlink_linger() */
1137
1138                dout(" reassigning lreq %p linger_id %llu\n", lreq,
1139                     lreq->linger_id);
1140                unlink_linger(osd, lreq);
1141                link_linger(&osdc->homeless_osd, lreq);
1142        }
1143
1144        __remove_osd_from_lru(osd);
1145        erase_osd(&osdc->osds, osd);
1146        put_osd(osd);
1147}
1148
1149/*
1150 * reset osd connect
1151 */
1152static int reopen_osd(struct ceph_osd *osd)
1153{
1154        struct ceph_entity_addr *peer_addr;
1155
1156        dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1157
1158        if (RB_EMPTY_ROOT(&osd->o_requests) &&
1159            RB_EMPTY_ROOT(&osd->o_linger_requests)) {
1160                close_osd(osd);
1161                return -ENODEV;
1162        }
1163
1164        peer_addr = &osd->o_osdc->osdmap->osd_addr[osd->o_osd];
1165        if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1166                        !ceph_con_opened(&osd->o_con)) {
1167                struct rb_node *n;
1168
1169                dout("osd addr hasn't changed and connection never opened, "
1170                     "letting msgr retry\n");
1171                /* touch each r_stamp for handle_timeout()'s benfit */
1172                for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
1173                        struct ceph_osd_request *req =
1174                            rb_entry(n, struct ceph_osd_request, r_node);
1175                        req->r_stamp = jiffies;
1176                }
1177
1178                return -EAGAIN;
1179        }
1180
1181        ceph_con_close(&osd->o_con);
1182        ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1183        osd->o_incarnation++;
1184
1185        return 0;
1186}
1187
1188static struct ceph_osd *lookup_create_osd(struct ceph_osd_client *osdc, int o,
1189                                          bool wrlocked)
1190{
1191        struct ceph_osd *osd;
1192
1193        if (wrlocked)
1194                verify_osdc_wrlocked(osdc);
1195        else
1196                verify_osdc_locked(osdc);
1197
1198        if (o != CEPH_HOMELESS_OSD)
1199                osd = lookup_osd(&osdc->osds, o);
1200        else
1201                osd = &osdc->homeless_osd;
1202        if (!osd) {
1203                if (!wrlocked)
1204                        return ERR_PTR(-EAGAIN);
1205
1206                osd = create_osd(osdc, o);
1207                insert_osd(&osdc->osds, osd);
1208                ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
1209                              &osdc->osdmap->osd_addr[osd->o_osd]);
1210        }
1211
1212        dout("%s osdc %p osd%d -> osd %p\n", __func__, osdc, o, osd);
1213        return osd;
1214}
1215
1216/*
1217 * Create request <-> OSD session relation.
1218 *
1219 * @req has to be assigned a tid, @osd may be homeless.
1220 */
1221static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req)
1222{
1223        verify_osd_locked(osd);
1224        WARN_ON(!req->r_tid || req->r_osd);
1225        dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
1226             req, req->r_tid);
1227
1228        if (!osd_homeless(osd))
1229                __remove_osd_from_lru(osd);
1230        else
1231                atomic_inc(&osd->o_osdc->num_homeless);
1232
1233        get_osd(osd);
1234        insert_request(&osd->o_requests, req);
1235        req->r_osd = osd;
1236}
1237
1238static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req)
1239{
1240        verify_osd_locked(osd);
1241        WARN_ON(req->r_osd != osd);
1242        dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
1243             req, req->r_tid);
1244
1245        req->r_osd = NULL;
1246        erase_request(&osd->o_requests, req);
1247        put_osd(osd);
1248
1249        if (!osd_homeless(osd))
1250                maybe_move_osd_to_lru(osd);
1251        else
1252                atomic_dec(&osd->o_osdc->num_homeless);
1253}
1254
1255static bool __pool_full(struct ceph_pg_pool_info *pi)
1256{
1257        return pi->flags & CEPH_POOL_FLAG_FULL;
1258}
1259
1260static bool have_pool_full(struct ceph_osd_client *osdc)
1261{
1262        struct rb_node *n;
1263
1264        for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
1265                struct ceph_pg_pool_info *pi =
1266                    rb_entry(n, struct ceph_pg_pool_info, node);
1267
1268                if (__pool_full(pi))
1269                        return true;
1270        }
1271
1272        return false;
1273}
1274
1275static bool pool_full(struct ceph_osd_client *osdc, s64 pool_id)
1276{
1277        struct ceph_pg_pool_info *pi;
1278
1279        pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
1280        if (!pi)
1281                return false;
1282
1283        return __pool_full(pi);
1284}
1285
1286/*
1287 * Returns whether a request should be blocked from being sent
1288 * based on the current osdmap and osd_client settings.
1289 */
1290static bool target_should_be_paused(struct ceph_osd_client *osdc,
1291                                    const struct ceph_osd_request_target *t,
1292                                    struct ceph_pg_pool_info *pi)
1293{
1294        bool pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
1295        bool pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
1296                       ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
1297                       __pool_full(pi);
1298
1299        WARN_ON(pi->id != t->base_oloc.pool);
1300        return (t->flags & CEPH_OSD_FLAG_READ && pauserd) ||
1301               (t->flags & CEPH_OSD_FLAG_WRITE && pausewr);
1302}
1303
1304enum calc_target_result {
1305        CALC_TARGET_NO_ACTION = 0,
1306        CALC_TARGET_NEED_RESEND,
1307        CALC_TARGET_POOL_DNE,
1308};
1309
1310static enum calc_target_result calc_target(struct ceph_osd_client *osdc,
1311                                           struct ceph_osd_request_target *t,
1312                                           u32 *last_force_resend,
1313                                           bool any_change)
1314{
1315        struct ceph_pg_pool_info *pi;
1316        struct ceph_pg pgid, last_pgid;
1317        struct ceph_osds up, acting;
1318        bool force_resend = false;
1319        bool need_check_tiering = false;
1320        bool need_resend = false;
1321        bool sort_bitwise = ceph_osdmap_flag(osdc, CEPH_OSDMAP_SORTBITWISE);
1322        enum calc_target_result ct_res;
1323        int ret;
1324
1325        pi = ceph_pg_pool_by_id(osdc->osdmap, t->base_oloc.pool);
1326        if (!pi) {
1327                t->osd = CEPH_HOMELESS_OSD;
1328                ct_res = CALC_TARGET_POOL_DNE;
1329                goto out;
1330        }
1331
1332        if (osdc->osdmap->epoch == pi->last_force_request_resend) {
1333                if (last_force_resend &&
1334                    *last_force_resend < pi->last_force_request_resend) {
1335                        *last_force_resend = pi->last_force_request_resend;
1336                        force_resend = true;
1337                } else if (!last_force_resend) {
1338                        force_resend = true;
1339                }
1340        }
1341        if (ceph_oid_empty(&t->target_oid) || force_resend) {
1342                ceph_oid_copy(&t->target_oid, &t->base_oid);
1343                need_check_tiering = true;
1344        }
1345        if (ceph_oloc_empty(&t->target_oloc) || force_resend) {
1346                ceph_oloc_copy(&t->target_oloc, &t->base_oloc);
1347                need_check_tiering = true;
1348        }
1349
1350        if (need_check_tiering &&
1351            (t->flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
1352                if (t->flags & CEPH_OSD_FLAG_READ && pi->read_tier >= 0)
1353                        t->target_oloc.pool = pi->read_tier;
1354                if (t->flags & CEPH_OSD_FLAG_WRITE && pi->write_tier >= 0)
1355                        t->target_oloc.pool = pi->write_tier;
1356        }
1357
1358        ret = ceph_object_locator_to_pg(osdc->osdmap, &t->target_oid,
1359                                        &t->target_oloc, &pgid);
1360        if (ret) {
1361                WARN_ON(ret != -ENOENT);
1362                t->osd = CEPH_HOMELESS_OSD;
1363                ct_res = CALC_TARGET_POOL_DNE;
1364                goto out;
1365        }
1366        last_pgid.pool = pgid.pool;
1367        last_pgid.seed = ceph_stable_mod(pgid.seed, t->pg_num, t->pg_num_mask);
1368
1369        ceph_pg_to_up_acting_osds(osdc->osdmap, &pgid, &up, &acting);
1370        if (any_change &&
1371            ceph_is_new_interval(&t->acting,
1372                                 &acting,
1373                                 &t->up,
1374                                 &up,
1375                                 t->size,
1376                                 pi->size,
1377                                 t->min_size,
1378                                 pi->min_size,
1379                                 t->pg_num,
1380                                 pi->pg_num,
1381                                 t->sort_bitwise,
1382                                 sort_bitwise,
1383                                 &last_pgid))
1384                force_resend = true;
1385
1386        if (t->paused && !target_should_be_paused(osdc, t, pi)) {
1387                t->paused = false;
1388                need_resend = true;
1389        }
1390
1391        if (ceph_pg_compare(&t->pgid, &pgid) ||
1392            ceph_osds_changed(&t->acting, &acting, any_change) ||
1393            force_resend) {
1394                t->pgid = pgid; /* struct */
1395                ceph_osds_copy(&t->acting, &acting);
1396                ceph_osds_copy(&t->up, &up);
1397                t->size = pi->size;
1398                t->min_size = pi->min_size;
1399                t->pg_num = pi->pg_num;
1400                t->pg_num_mask = pi->pg_num_mask;
1401                t->sort_bitwise = sort_bitwise;
1402
1403                t->osd = acting.primary;
1404                need_resend = true;
1405        }
1406
1407        ct_res = need_resend ? CALC_TARGET_NEED_RESEND : CALC_TARGET_NO_ACTION;
1408out:
1409        dout("%s t %p -> ct_res %d osd %d\n", __func__, t, ct_res, t->osd);
1410        return ct_res;
1411}
1412
1413static void setup_request_data(struct ceph_osd_request *req,
1414                               struct ceph_msg *msg)
1415{
1416        u32 data_len = 0;
1417        int i;
1418
1419        if (!list_empty(&msg->data))
1420                return;
1421
1422        WARN_ON(msg->data_length);
1423        for (i = 0; i < req->r_num_ops; i++) {
1424                struct ceph_osd_req_op *op = &req->r_ops[i];
1425
1426                switch (op->op) {
1427                /* request */
1428                case CEPH_OSD_OP_WRITE:
1429                case CEPH_OSD_OP_WRITEFULL:
1430                        WARN_ON(op->indata_len != op->extent.length);
1431                        ceph_osdc_msg_data_add(msg, &op->extent.osd_data);
1432                        break;
1433                case CEPH_OSD_OP_SETXATTR:
1434                case CEPH_OSD_OP_CMPXATTR:
1435                        WARN_ON(op->indata_len != op->xattr.name_len +
1436                                                  op->xattr.value_len);
1437                        ceph_osdc_msg_data_add(msg, &op->xattr.osd_data);
1438                        break;
1439                case CEPH_OSD_OP_NOTIFY_ACK:
1440                        ceph_osdc_msg_data_add(msg,
1441                                               &op->notify_ack.request_data);
1442                        break;
1443
1444                /* reply */
1445                case CEPH_OSD_OP_STAT:
1446                        ceph_osdc_msg_data_add(req->r_reply,
1447                                               &op->raw_data_in);
1448                        break;
1449                case CEPH_OSD_OP_READ:
1450                        ceph_osdc_msg_data_add(req->r_reply,
1451                                               &op->extent.osd_data);
1452                        break;
1453                case CEPH_OSD_OP_LIST_WATCHERS:
1454                        ceph_osdc_msg_data_add(req->r_reply,
1455                                               &op->list_watchers.response_data);
1456                        break;
1457
1458                /* both */
1459                case CEPH_OSD_OP_CALL:
1460                        WARN_ON(op->indata_len != op->cls.class_len +
1461                                                  op->cls.method_len +
1462                                                  op->cls.indata_len);
1463                        ceph_osdc_msg_data_add(msg, &op->cls.request_info);
1464                        /* optional, can be NONE */
1465                        ceph_osdc_msg_data_add(msg, &op->cls.request_data);
1466                        /* optional, can be NONE */
1467                        ceph_osdc_msg_data_add(req->r_reply,
1468                                               &op->cls.response_data);
1469                        break;
1470                case CEPH_OSD_OP_NOTIFY:
1471                        ceph_osdc_msg_data_add(msg,
1472                                               &op->notify.request_data);
1473                        ceph_osdc_msg_data_add(req->r_reply,
1474                                               &op->notify.response_data);
1475                        break;
1476                }
1477
1478                data_len += op->indata_len;
1479        }
1480
1481        WARN_ON(data_len != msg->data_length);
1482}
1483
1484static void encode_request(struct ceph_osd_request *req, struct ceph_msg *msg)
1485{
1486        void *p = msg->front.iov_base;
1487        void *const end = p + msg->front_alloc_len;
1488        u32 data_len = 0;
1489        int i;
1490
1491        if (req->r_flags & CEPH_OSD_FLAG_WRITE) {
1492                /* snapshots aren't writeable */
1493                WARN_ON(req->r_snapid != CEPH_NOSNAP);
1494        } else {
1495                WARN_ON(req->r_mtime.tv_sec || req->r_mtime.tv_nsec ||
1496                        req->r_data_offset || req->r_snapc);
1497        }
1498
1499        setup_request_data(req, msg);
1500
1501        ceph_encode_32(&p, 1); /* client_inc, always 1 */
1502        ceph_encode_32(&p, req->r_osdc->osdmap->epoch);
1503        ceph_encode_32(&p, req->r_flags);
1504        ceph_encode_timespec(p, &req->r_mtime);
1505        p += sizeof(struct ceph_timespec);
1506        /* aka reassert_version */
1507        memcpy(p, &req->r_replay_version, sizeof(req->r_replay_version));
1508        p += sizeof(req->r_replay_version);
1509
1510        /* oloc */
1511        ceph_start_encoding(&p, 5, 4,
1512                            ceph_oloc_encoding_size(&req->r_t.target_oloc));
1513        ceph_encode_64(&p, req->r_t.target_oloc.pool);
1514        ceph_encode_32(&p, -1); /* preferred */
1515        ceph_encode_32(&p, 0); /* key len */
1516        if (req->r_t.target_oloc.pool_ns)
1517                ceph_encode_string(&p, end, req->r_t.target_oloc.pool_ns->str,
1518                                   req->r_t.target_oloc.pool_ns->len);
1519        else
1520                ceph_encode_32(&p, 0);
1521
1522        /* pgid */
1523        ceph_encode_8(&p, 1);
1524        ceph_encode_64(&p, req->r_t.pgid.pool);
1525        ceph_encode_32(&p, req->r_t.pgid.seed);
1526        ceph_encode_32(&p, -1); /* preferred */
1527
1528        /* oid */
1529        ceph_encode_32(&p, req->r_t.target_oid.name_len);
1530        memcpy(p, req->r_t.target_oid.name, req->r_t.target_oid.name_len);
1531        p += req->r_t.target_oid.name_len;
1532
1533        /* ops, can imply data */
1534        ceph_encode_16(&p, req->r_num_ops);
1535        for (i = 0; i < req->r_num_ops; i++) {
1536                data_len += osd_req_encode_op(p, &req->r_ops[i]);
1537                p += sizeof(struct ceph_osd_op);
1538        }
1539
1540        ceph_encode_64(&p, req->r_snapid); /* snapid */
1541        if (req->r_snapc) {
1542                ceph_encode_64(&p, req->r_snapc->seq);
1543                ceph_encode_32(&p, req->r_snapc->num_snaps);
1544                for (i = 0; i < req->r_snapc->num_snaps; i++)
1545                        ceph_encode_64(&p, req->r_snapc->snaps[i]);
1546        } else {
1547                ceph_encode_64(&p, 0); /* snap_seq */
1548                ceph_encode_32(&p, 0); /* snaps len */
1549        }
1550
1551        ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
1552
1553        BUG_ON(p > end);
1554        msg->front.iov_len = p - msg->front.iov_base;
1555        msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
1556        msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1557        msg->hdr.data_len = cpu_to_le32(data_len);
1558        /*
1559         * The header "data_off" is a hint to the receiver allowing it
1560         * to align received data into its buffers such that there's no
1561         * need to re-copy it before writing it to disk (direct I/O).
1562         */
1563        msg->hdr.data_off = cpu_to_le16(req->r_data_offset);
1564
1565        dout("%s req %p oid %s oid_len %d front %zu data %u\n", __func__,
1566             req, req->r_t.target_oid.name, req->r_t.target_oid.name_len,
1567             msg->front.iov_len, data_len);
1568}
1569
1570/*
1571 * @req has to be assigned a tid and registered.
1572 */
1573static void send_request(struct ceph_osd_request *req)
1574{
1575        struct ceph_osd *osd = req->r_osd;
1576
1577        verify_osd_locked(osd);
1578        WARN_ON(osd->o_osd != req->r_t.osd);
1579
1580        /*
1581         * We may have a previously queued request message hanging
1582         * around.  Cancel it to avoid corrupting the msgr.
1583         */
1584        if (req->r_sent)
1585                ceph_msg_revoke(req->r_request);
1586
1587        req->r_flags |= CEPH_OSD_FLAG_KNOWN_REDIR;
1588        if (req->r_attempts)
1589                req->r_flags |= CEPH_OSD_FLAG_RETRY;
1590        else
1591                WARN_ON(req->r_flags & CEPH_OSD_FLAG_RETRY);
1592
1593        encode_request(req, req->r_request);
1594
1595        dout("%s req %p tid %llu to pg %llu.%x osd%d flags 0x%x attempt %d\n",
1596             __func__, req, req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed,
1597             req->r_t.osd, req->r_flags, req->r_attempts);
1598
1599        req->r_t.paused = false;
1600        req->r_stamp = jiffies;
1601        req->r_attempts++;
1602
1603        req->r_sent = osd->o_incarnation;
1604        req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
1605        ceph_con_send(&osd->o_con, ceph_msg_get(req->r_request));
1606}
1607
1608static void maybe_request_map(struct ceph_osd_client *osdc)
1609{
1610        bool continuous = false;
1611
1612        verify_osdc_locked(osdc);
1613        WARN_ON(!osdc->osdmap->epoch);
1614
1615        if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
1616            ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD) ||
1617            ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
1618                dout("%s osdc %p continuous\n", __func__, osdc);
1619                continuous = true;
1620        } else {
1621                dout("%s osdc %p onetime\n", __func__, osdc);
1622        }
1623
1624        if (ceph_monc_want_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
1625                               osdc->osdmap->epoch + 1, continuous))
1626                ceph_monc_renew_subs(&osdc->client->monc);
1627}
1628
1629static void send_map_check(struct ceph_osd_request *req);
1630
1631static void __submit_request(struct ceph_osd_request *req, bool wrlocked)
1632{
1633        struct ceph_osd_client *osdc = req->r_osdc;
1634        struct ceph_osd *osd;
1635        enum calc_target_result ct_res;
1636        bool need_send = false;
1637        bool promoted = false;
1638
1639        WARN_ON(req->r_tid || req->r_got_reply);
1640        dout("%s req %p wrlocked %d\n", __func__, req, wrlocked);
1641
1642again:
1643        ct_res = calc_target(osdc, &req->r_t, &req->r_last_force_resend, false);
1644        if (ct_res == CALC_TARGET_POOL_DNE && !wrlocked)
1645                goto promote;
1646
1647        osd = lookup_create_osd(osdc, req->r_t.osd, wrlocked);
1648        if (IS_ERR(osd)) {
1649                WARN_ON(PTR_ERR(osd) != -EAGAIN || wrlocked);
1650                goto promote;
1651        }
1652
1653        if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
1654            ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
1655                dout("req %p pausewr\n", req);
1656                req->r_t.paused = true;
1657                maybe_request_map(osdc);
1658        } else if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
1659                   ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
1660                dout("req %p pauserd\n", req);
1661                req->r_t.paused = true;
1662                maybe_request_map(osdc);
1663        } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
1664                   !(req->r_flags & (CEPH_OSD_FLAG_FULL_TRY |
1665                                     CEPH_OSD_FLAG_FULL_FORCE)) &&
1666                   (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
1667                    pool_full(osdc, req->r_t.base_oloc.pool))) {
1668                dout("req %p full/pool_full\n", req);
1669                pr_warn_ratelimited("FULL or reached pool quota\n");
1670                req->r_t.paused = true;
1671                maybe_request_map(osdc);
1672        } else if (!osd_homeless(osd)) {
1673                need_send = true;
1674        } else {
1675                maybe_request_map(osdc);
1676        }
1677
1678        mutex_lock(&osd->lock);
1679        /*
1680         * Assign the tid atomically with send_request() to protect
1681         * multiple writes to the same object from racing with each
1682         * other, resulting in out of order ops on the OSDs.
1683         */
1684        req->r_tid = atomic64_inc_return(&osdc->last_tid);
1685        link_request(osd, req);
1686        if (need_send)
1687                send_request(req);
1688        mutex_unlock(&osd->lock);
1689
1690        if (ct_res == CALC_TARGET_POOL_DNE)
1691                send_map_check(req);
1692
1693        if (promoted)
1694                downgrade_write(&osdc->lock);
1695        return;
1696
1697promote:
1698        up_read(&osdc->lock);
1699        down_write(&osdc->lock);
1700        wrlocked = true;
1701        promoted = true;
1702        goto again;
1703}
1704
1705static void account_request(struct ceph_osd_request *req)
1706{
1707        unsigned int mask = CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK;
1708
1709        if (req->r_flags & CEPH_OSD_FLAG_READ) {
1710                WARN_ON(req->r_flags & mask);
1711                req->r_flags |= CEPH_OSD_FLAG_ACK;
1712        } else if (req->r_flags & CEPH_OSD_FLAG_WRITE)
1713                WARN_ON(!(req->r_flags & mask));
1714        else
1715                WARN_ON(1);
1716
1717        WARN_ON(req->r_unsafe_callback && (req->r_flags & mask) != mask);
1718        atomic_inc(&req->r_osdc->num_requests);
1719}
1720
1721static void submit_request(struct ceph_osd_request *req, bool wrlocked)
1722{
1723        ceph_osdc_get_request(req);
1724        account_request(req);
1725        __submit_request(req, wrlocked);
1726}
1727
1728static void finish_request(struct ceph_osd_request *req)
1729{
1730        struct ceph_osd_client *osdc = req->r_osdc;
1731        struct ceph_osd *osd = req->r_osd;
1732
1733        verify_osd_locked(osd);
1734        dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
1735
1736        WARN_ON(lookup_request_mc(&osdc->map_checks, req->r_tid));
1737        unlink_request(osd, req);
1738        atomic_dec(&osdc->num_requests);
1739
1740        /*
1741         * If an OSD has failed or returned and a request has been sent
1742         * twice, it's possible to get a reply and end up here while the
1743         * request message is queued for delivery.  We will ignore the
1744         * reply, so not a big deal, but better to try and catch it.
1745         */
1746        ceph_msg_revoke(req->r_request);
1747        ceph_msg_revoke_incoming(req->r_reply);
1748}
1749
1750static void __complete_request(struct ceph_osd_request *req)
1751{
1752        if (req->r_callback)
1753                req->r_callback(req);
1754        else
1755                complete_all(&req->r_completion);
1756}
1757
1758/*
1759 * Note that this is open-coded in handle_reply(), which has to deal
1760 * with ack vs commit, dup acks, etc.
1761 */
1762static void complete_request(struct ceph_osd_request *req, int err)
1763{
1764        dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
1765
1766        req->r_result = err;
1767        finish_request(req);
1768        __complete_request(req);
1769        complete_all(&req->r_done_completion);
1770        ceph_osdc_put_request(req);
1771}
1772
1773static void cancel_map_check(struct ceph_osd_request *req)
1774{
1775        struct ceph_osd_client *osdc = req->r_osdc;
1776        struct ceph_osd_request *lookup_req;
1777
1778        verify_osdc_wrlocked(osdc);
1779
1780        lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
1781        if (!lookup_req)
1782                return;
1783
1784        WARN_ON(lookup_req != req);
1785        erase_request_mc(&osdc->map_checks, req);
1786        ceph_osdc_put_request(req);
1787}
1788
1789static void cancel_request(struct ceph_osd_request *req)
1790{
1791        dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
1792
1793        cancel_map_check(req);
1794        finish_request(req);
1795        complete_all(&req->r_done_completion);
1796        ceph_osdc_put_request(req);
1797}
1798
1799static void check_pool_dne(struct ceph_osd_request *req)
1800{
1801        struct ceph_osd_client *osdc = req->r_osdc;
1802        struct ceph_osdmap *map = osdc->osdmap;
1803
1804        verify_osdc_wrlocked(osdc);
1805        WARN_ON(!map->epoch);
1806
1807        if (req->r_attempts) {
1808                /*
1809                 * We sent a request earlier, which means that
1810                 * previously the pool existed, and now it does not
1811                 * (i.e., it was deleted).
1812                 */
1813                req->r_map_dne_bound = map->epoch;
1814                dout("%s req %p tid %llu pool disappeared\n", __func__, req,
1815                     req->r_tid);
1816        } else {
1817                dout("%s req %p tid %llu map_dne_bound %u have %u\n", __func__,
1818                     req, req->r_tid, req->r_map_dne_bound, map->epoch);
1819        }
1820
1821        if (req->r_map_dne_bound) {
1822                if (map->epoch >= req->r_map_dne_bound) {
1823                        /* we had a new enough map */
1824                        pr_info_ratelimited("tid %llu pool does not exist\n",
1825                                            req->r_tid);
1826                        complete_request(req, -ENOENT);
1827                }
1828        } else {
1829                send_map_check(req);
1830        }
1831}
1832
1833static void map_check_cb(struct ceph_mon_generic_request *greq)
1834{
1835        struct ceph_osd_client *osdc = &greq->monc->client->osdc;
1836        struct ceph_osd_request *req;
1837        u64 tid = greq->private_data;
1838
1839        WARN_ON(greq->result || !greq->u.newest);
1840
1841        down_write(&osdc->lock);
1842        req = lookup_request_mc(&osdc->map_checks, tid);
1843        if (!req) {
1844                dout("%s tid %llu dne\n", __func__, tid);
1845                goto out_unlock;
1846        }
1847
1848        dout("%s req %p tid %llu map_dne_bound %u newest %llu\n", __func__,
1849             req, req->r_tid, req->r_map_dne_bound, greq->u.newest);
1850        if (!req->r_map_dne_bound)
1851                req->r_map_dne_bound = greq->u.newest;
1852        erase_request_mc(&osdc->map_checks, req);
1853        check_pool_dne(req);
1854
1855        ceph_osdc_put_request(req);
1856out_unlock:
1857        up_write(&osdc->lock);
1858}
1859
1860static void send_map_check(struct ceph_osd_request *req)
1861{
1862        struct ceph_osd_client *osdc = req->r_osdc;
1863        struct ceph_osd_request *lookup_req;
1864        int ret;
1865
1866        verify_osdc_wrlocked(osdc);
1867
1868        lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
1869        if (lookup_req) {
1870                WARN_ON(lookup_req != req);
1871                return;
1872        }
1873
1874        ceph_osdc_get_request(req);
1875        insert_request_mc(&osdc->map_checks, req);
1876        ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
1877                                          map_check_cb, req->r_tid);
1878        WARN_ON(ret);
1879}
1880
1881/*
1882 * lingering requests, watch/notify v2 infrastructure
1883 */
1884static void linger_release(struct kref *kref)
1885{
1886        struct ceph_osd_linger_request *lreq =
1887            container_of(kref, struct ceph_osd_linger_request, kref);
1888
1889        dout("%s lreq %p reg_req %p ping_req %p\n", __func__, lreq,
1890             lreq->reg_req, lreq->ping_req);
1891        WARN_ON(!RB_EMPTY_NODE(&lreq->node));
1892        WARN_ON(!RB_EMPTY_NODE(&lreq->osdc_node));
1893        WARN_ON(!RB_EMPTY_NODE(&lreq->mc_node));
1894        WARN_ON(!list_empty(&lreq->scan_item));
1895        WARN_ON(!list_empty(&lreq->pending_lworks));
1896        WARN_ON(lreq->osd);
1897
1898        if (lreq->reg_req)
1899                ceph_osdc_put_request(lreq->reg_req);
1900        if (lreq->ping_req)
1901                ceph_osdc_put_request(lreq->ping_req);
1902        target_destroy(&lreq->t);
1903        kfree(lreq);
1904}
1905
1906static void linger_put(struct ceph_osd_linger_request *lreq)
1907{
1908        if (lreq)
1909                kref_put(&lreq->kref, linger_release);
1910}
1911
1912static struct ceph_osd_linger_request *
1913linger_get(struct ceph_osd_linger_request *lreq)
1914{
1915        kref_get(&lreq->kref);
1916        return lreq;
1917}
1918
1919static struct ceph_osd_linger_request *
1920linger_alloc(struct ceph_osd_client *osdc)
1921{
1922        struct ceph_osd_linger_request *lreq;
1923
1924        lreq = kzalloc(sizeof(*lreq), GFP_NOIO);
1925        if (!lreq)
1926                return NULL;
1927
1928        kref_init(&lreq->kref);
1929        mutex_init(&lreq->lock);
1930        RB_CLEAR_NODE(&lreq->node);
1931        RB_CLEAR_NODE(&lreq->osdc_node);
1932        RB_CLEAR_NODE(&lreq->mc_node);
1933        INIT_LIST_HEAD(&lreq->scan_item);
1934        INIT_LIST_HEAD(&lreq->pending_lworks);
1935        init_completion(&lreq->reg_commit_wait);
1936        init_completion(&lreq->notify_finish_wait);
1937
1938        lreq->osdc = osdc;
1939        target_init(&lreq->t);
1940
1941        dout("%s lreq %p\n", __func__, lreq);
1942        return lreq;
1943}
1944
1945DEFINE_RB_INSDEL_FUNCS(linger, struct ceph_osd_linger_request, linger_id, node)
1946DEFINE_RB_FUNCS(linger_osdc, struct ceph_osd_linger_request, linger_id, osdc_node)
1947DEFINE_RB_FUNCS(linger_mc, struct ceph_osd_linger_request, linger_id, mc_node)
1948
1949/*
1950 * Create linger request <-> OSD session relation.
1951 *
1952 * @lreq has to be registered, @osd may be homeless.
1953 */
1954static void link_linger(struct ceph_osd *osd,
1955                        struct ceph_osd_linger_request *lreq)
1956{
1957        verify_osd_locked(osd);
1958        WARN_ON(!lreq->linger_id || lreq->osd);
1959        dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
1960             osd->o_osd, lreq, lreq->linger_id);
1961
1962        if (!osd_homeless(osd))
1963                __remove_osd_from_lru(osd);
1964        else
1965                atomic_inc(&osd->o_osdc->num_homeless);
1966
1967        get_osd(osd);
1968        insert_linger(&osd->o_linger_requests, lreq);
1969        lreq->osd = osd;
1970}
1971
1972static void unlink_linger(struct ceph_osd *osd,
1973                          struct ceph_osd_linger_request *lreq)
1974{
1975        verify_osd_locked(osd);
1976        WARN_ON(lreq->osd != osd);
1977        dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
1978             osd->o_osd, lreq, lreq->linger_id);
1979
1980        lreq->osd = NULL;
1981        erase_linger(&osd->o_linger_requests, lreq);
1982        put_osd(osd);
1983
1984        if (!osd_homeless(osd))
1985                maybe_move_osd_to_lru(osd);
1986        else
1987                atomic_dec(&osd->o_osdc->num_homeless);
1988}
1989
1990static bool __linger_registered(struct ceph_osd_linger_request *lreq)
1991{
1992        verify_osdc_locked(lreq->osdc);
1993
1994        return !RB_EMPTY_NODE(&lreq->osdc_node);
1995}
1996
1997static bool linger_registered(struct ceph_osd_linger_request *lreq)
1998{
1999        struct ceph_osd_client *osdc = lreq->osdc;
2000        bool registered;
2001
2002        down_read(&osdc->lock);
2003        registered = __linger_registered(lreq);
2004        up_read(&osdc->lock);
2005
2006        return registered;
2007}
2008
2009static void linger_register(struct ceph_osd_linger_request *lreq)
2010{
2011        struct ceph_osd_client *osdc = lreq->osdc;
2012
2013        verify_osdc_wrlocked(osdc);
2014        WARN_ON(lreq->linger_id);
2015
2016        linger_get(lreq);
2017        lreq->linger_id = ++osdc->last_linger_id;
2018        insert_linger_osdc(&osdc->linger_requests, lreq);
2019}
2020
2021static void linger_unregister(struct ceph_osd_linger_request *lreq)
2022{
2023        struct ceph_osd_client *osdc = lreq->osdc;
2024
2025        verify_osdc_wrlocked(osdc);
2026
2027        erase_linger_osdc(&osdc->linger_requests, lreq);
2028        linger_put(lreq);
2029}
2030
2031static void cancel_linger_request(struct ceph_osd_request *req)
2032{
2033        struct ceph_osd_linger_request *lreq = req->r_priv;
2034
2035        WARN_ON(!req->r_linger);
2036        cancel_request(req);
2037        linger_put(lreq);
2038}
2039
2040struct linger_work {
2041        struct work_struct work;
2042        struct ceph_osd_linger_request *lreq;
2043        struct list_head pending_item;
2044        unsigned long queued_stamp;
2045
2046        union {
2047                struct {
2048                        u64 notify_id;
2049                        u64 notifier_id;
2050                        void *payload; /* points into @msg front */
2051                        size_t payload_len;
2052
2053                        struct ceph_msg *msg; /* for ceph_msg_put() */
2054                } notify;
2055                struct {
2056                        int err;
2057                } error;
2058        };
2059};
2060
2061static struct linger_work *lwork_alloc(struct ceph_osd_linger_request *lreq,
2062                                       work_func_t workfn)
2063{
2064        struct linger_work *lwork;
2065
2066        lwork = kzalloc(sizeof(*lwork), GFP_NOIO);
2067        if (!lwork)
2068                return NULL;
2069
2070        INIT_WORK(&lwork->work, workfn);
2071        INIT_LIST_HEAD(&lwork->pending_item);
2072        lwork->lreq = linger_get(lreq);
2073
2074        return lwork;
2075}
2076
2077static void lwork_free(struct linger_work *lwork)
2078{
2079        struct ceph_osd_linger_request *lreq = lwork->lreq;
2080
2081        mutex_lock(&lreq->lock);
2082        list_del(&lwork->pending_item);
2083        mutex_unlock(&lreq->lock);
2084
2085        linger_put(lreq);
2086        kfree(lwork);
2087}
2088
2089static void lwork_queue(struct linger_work *lwork)
2090{
2091        struct ceph_osd_linger_request *lreq = lwork->lreq;
2092        struct ceph_osd_client *osdc = lreq->osdc;
2093
2094        verify_lreq_locked(lreq);
2095        WARN_ON(!list_empty(&lwork->pending_item));
2096
2097        lwork->queued_stamp = jiffies;
2098        list_add_tail(&lwork->pending_item, &lreq->pending_lworks);
2099        queue_work(osdc->notify_wq, &lwork->work);
2100}
2101
2102static void do_watch_notify(struct work_struct *w)
2103{
2104        struct linger_work *lwork = container_of(w, struct linger_work, work);
2105        struct ceph_osd_linger_request *lreq = lwork->lreq;
2106
2107        if (!linger_registered(lreq)) {
2108                dout("%s lreq %p not registered\n", __func__, lreq);
2109                goto out;
2110        }
2111
2112        WARN_ON(!lreq->is_watch);
2113        dout("%s lreq %p notify_id %llu notifier_id %llu payload_len %zu\n",
2114             __func__, lreq, lwork->notify.notify_id, lwork->notify.notifier_id,
2115             lwork->notify.payload_len);
2116        lreq->wcb(lreq->data, lwork->notify.notify_id, lreq->linger_id,
2117                  lwork->notify.notifier_id, lwork->notify.payload,
2118                  lwork->notify.payload_len);
2119
2120out:
2121        ceph_msg_put(lwork->notify.msg);
2122        lwork_free(lwork);
2123}
2124
2125static void do_watch_error(struct work_struct *w)
2126{
2127        struct linger_work *lwork = container_of(w, struct linger_work, work);
2128        struct ceph_osd_linger_request *lreq = lwork->lreq;
2129
2130        if (!linger_registered(lreq)) {
2131                dout("%s lreq %p not registered\n", __func__, lreq);
2132                goto out;
2133        }
2134
2135        dout("%s lreq %p err %d\n", __func__, lreq, lwork->error.err);
2136        lreq->errcb(lreq->data, lreq->linger_id, lwork->error.err);
2137
2138out:
2139        lwork_free(lwork);
2140}
2141
2142static void queue_watch_error(struct ceph_osd_linger_request *lreq)
2143{
2144        struct linger_work *lwork;
2145
2146        lwork = lwork_alloc(lreq, do_watch_error);
2147        if (!lwork) {
2148                pr_err("failed to allocate error-lwork\n");
2149                return;
2150        }
2151
2152        lwork->error.err = lreq->last_error;
2153        lwork_queue(lwork);
2154}
2155
2156static void linger_reg_commit_complete(struct ceph_osd_linger_request *lreq,
2157                                       int result)
2158{
2159        if (!completion_done(&lreq->reg_commit_wait)) {
2160                lreq->reg_commit_error = (result <= 0 ? result : 0);
2161                complete_all(&lreq->reg_commit_wait);
2162        }
2163}
2164
2165static void linger_commit_cb(struct ceph_osd_request *req)
2166{
2167        struct ceph_osd_linger_request *lreq = req->r_priv;
2168
2169        mutex_lock(&lreq->lock);
2170        dout("%s lreq %p linger_id %llu result %d\n", __func__, lreq,
2171             lreq->linger_id, req->r_result);
2172        WARN_ON(!__linger_registered(lreq));
2173        linger_reg_commit_complete(lreq, req->r_result);
2174        lreq->committed = true;
2175
2176        if (!lreq->is_watch) {
2177                struct ceph_osd_data *osd_data =
2178                    osd_req_op_data(req, 0, notify, response_data);
2179                void *p = page_address(osd_data->pages[0]);
2180
2181                WARN_ON(req->r_ops[0].op != CEPH_OSD_OP_NOTIFY ||
2182                        osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
2183
2184                /* make note of the notify_id */
2185                if (req->r_ops[0].outdata_len >= sizeof(u64)) {
2186                        lreq->notify_id = ceph_decode_64(&p);
2187                        dout("lreq %p notify_id %llu\n", lreq,
2188                             lreq->notify_id);
2189                } else {
2190                        dout("lreq %p no notify_id\n", lreq);
2191                }
2192        }
2193
2194        mutex_unlock(&lreq->lock);
2195        linger_put(lreq);
2196}
2197
2198static int normalize_watch_error(int err)
2199{
2200        /*
2201         * Translate ENOENT -> ENOTCONN so that a delete->disconnection
2202         * notification and a failure to reconnect because we raced with
2203         * the delete appear the same to the user.
2204         */
2205        if (err == -ENOENT)
2206                err = -ENOTCONN;
2207
2208        return err;
2209}
2210
2211static void linger_reconnect_cb(struct ceph_osd_request *req)
2212{
2213        struct ceph_osd_linger_request *lreq = req->r_priv;
2214
2215        mutex_lock(&lreq->lock);
2216        dout("%s lreq %p linger_id %llu result %d last_error %d\n", __func__,
2217             lreq, lreq->linger_id, req->r_result, lreq->last_error);
2218        if (req->r_result < 0) {
2219                if (!lreq->last_error) {
2220                        lreq->last_error = normalize_watch_error(req->r_result);
2221                        queue_watch_error(lreq);
2222                }
2223        }
2224
2225        mutex_unlock(&lreq->lock);
2226        linger_put(lreq);
2227}
2228
2229static void send_linger(struct ceph_osd_linger_request *lreq)
2230{
2231        struct ceph_osd_request *req = lreq->reg_req;
2232        struct ceph_osd_req_op *op = &req->r_ops[0];
2233
2234        verify_osdc_wrlocked(req->r_osdc);
2235        dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2236
2237        if (req->r_osd)
2238                cancel_linger_request(req);
2239
2240        request_reinit(req);
2241        ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
2242        ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
2243        req->r_flags = lreq->t.flags;
2244        req->r_mtime = lreq->mtime;
2245
2246        mutex_lock(&lreq->lock);
2247        if (lreq->is_watch && lreq->committed) {
2248                WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2249                        op->watch.cookie != lreq->linger_id);
2250                op->watch.op = CEPH_OSD_WATCH_OP_RECONNECT;
2251                op->watch.gen = ++lreq->register_gen;
2252                dout("lreq %p reconnect register_gen %u\n", lreq,
2253                     op->watch.gen);
2254                req->r_callback = linger_reconnect_cb;
2255        } else {
2256                if (!lreq->is_watch)
2257                        lreq->notify_id = 0;
2258                else
2259                        WARN_ON(op->watch.op != CEPH_OSD_WATCH_OP_WATCH);
2260                dout("lreq %p register\n", lreq);
2261                req->r_callback = linger_commit_cb;
2262        }
2263        mutex_unlock(&lreq->lock);
2264
2265        req->r_priv = linger_get(lreq);
2266        req->r_linger = true;
2267
2268        submit_request(req, true);
2269}
2270
2271static void linger_ping_cb(struct ceph_osd_request *req)
2272{
2273        struct ceph_osd_linger_request *lreq = req->r_priv;
2274
2275        mutex_lock(&lreq->lock);
2276        dout("%s lreq %p linger_id %llu result %d ping_sent %lu last_error %d\n",
2277             __func__, lreq, lreq->linger_id, req->r_result, lreq->ping_sent,
2278             lreq->last_error);
2279        if (lreq->register_gen == req->r_ops[0].watch.gen) {
2280                if (!req->r_result) {
2281                        lreq->watch_valid_thru = lreq->ping_sent;
2282                } else if (!lreq->last_error) {
2283                        lreq->last_error = normalize_watch_error(req->r_result);
2284                        queue_watch_error(lreq);
2285                }
2286        } else {
2287                dout("lreq %p register_gen %u ignoring old pong %u\n", lreq,
2288                     lreq->register_gen, req->r_ops[0].watch.gen);
2289        }
2290
2291        mutex_unlock(&lreq->lock);
2292        linger_put(lreq);
2293}
2294
2295static void send_linger_ping(struct ceph_osd_linger_request *lreq)
2296{
2297        struct ceph_osd_client *osdc = lreq->osdc;
2298        struct ceph_osd_request *req = lreq->ping_req;
2299        struct ceph_osd_req_op *op = &req->r_ops[0];
2300
2301        if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
2302                dout("%s PAUSERD\n", __func__);
2303                return;
2304        }
2305
2306        lreq->ping_sent = jiffies;
2307        dout("%s lreq %p linger_id %llu ping_sent %lu register_gen %u\n",
2308             __func__, lreq, lreq->linger_id, lreq->ping_sent,
2309             lreq->register_gen);
2310
2311        if (req->r_osd)
2312                cancel_linger_request(req);
2313
2314        request_reinit(req);
2315        target_copy(&req->r_t, &lreq->t);
2316
2317        WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2318                op->watch.cookie != lreq->linger_id ||
2319                op->watch.op != CEPH_OSD_WATCH_OP_PING);
2320        op->watch.gen = lreq->register_gen;
2321        req->r_callback = linger_ping_cb;
2322        req->r_priv = linger_get(lreq);
2323        req->r_linger = true;
2324
2325        ceph_osdc_get_request(req);
2326        account_request(req);
2327        req->r_tid = atomic64_inc_return(&osdc->last_tid);
2328        link_request(lreq->osd, req);
2329        send_request(req);
2330}
2331
2332static void linger_submit(struct ceph_osd_linger_request *lreq)
2333{
2334        struct ceph_osd_client *osdc = lreq->osdc;
2335        struct ceph_osd *osd;
2336
2337        calc_target(osdc, &lreq->t, &lreq->last_force_resend, false);
2338        osd = lookup_create_osd(osdc, lreq->t.osd, true);
2339        link_linger(osd, lreq);
2340
2341        send_linger(lreq);
2342}
2343
2344static void cancel_linger_map_check(struct ceph_osd_linger_request *lreq)
2345{
2346        struct ceph_osd_client *osdc = lreq->osdc;
2347        struct ceph_osd_linger_request *lookup_lreq;
2348
2349        verify_osdc_wrlocked(osdc);
2350
2351        lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
2352                                       lreq->linger_id);
2353        if (!lookup_lreq)
2354                return;
2355
2356        WARN_ON(lookup_lreq != lreq);
2357        erase_linger_mc(&osdc->linger_map_checks, lreq);
2358        linger_put(lreq);
2359}
2360
2361/*
2362 * @lreq has to be both registered and linked.
2363 */
2364static void __linger_cancel(struct ceph_osd_linger_request *lreq)
2365{
2366        if (lreq->is_watch && lreq->ping_req->r_osd)
2367                cancel_linger_request(lreq->ping_req);
2368        if (lreq->reg_req->r_osd)
2369                cancel_linger_request(lreq->reg_req);
2370        cancel_linger_map_check(lreq);
2371        unlink_linger(lreq->osd, lreq);
2372        linger_unregister(lreq);
2373}
2374
2375static void linger_cancel(struct ceph_osd_linger_request *lreq)
2376{
2377        struct ceph_osd_client *osdc = lreq->osdc;
2378
2379        down_write(&osdc->lock);
2380        if (__linger_registered(lreq))
2381                __linger_cancel(lreq);
2382        up_write(&osdc->lock);
2383}
2384
2385static void send_linger_map_check(struct ceph_osd_linger_request *lreq);
2386
2387static void check_linger_pool_dne(struct ceph_osd_linger_request *lreq)
2388{
2389        struct ceph_osd_client *osdc = lreq->osdc;
2390        struct ceph_osdmap *map = osdc->osdmap;
2391
2392        verify_osdc_wrlocked(osdc);
2393        WARN_ON(!map->epoch);
2394
2395        if (lreq->register_gen) {
2396                lreq->map_dne_bound = map->epoch;
2397                dout("%s lreq %p linger_id %llu pool disappeared\n", __func__,
2398                     lreq, lreq->linger_id);
2399        } else {
2400                dout("%s lreq %p linger_id %llu map_dne_bound %u have %u\n",
2401                     __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
2402                     map->epoch);
2403        }
2404
2405        if (lreq->map_dne_bound) {
2406                if (map->epoch >= lreq->map_dne_bound) {
2407                        /* we had a new enough map */
2408                        pr_info("linger_id %llu pool does not exist\n",
2409                                lreq->linger_id);
2410                        linger_reg_commit_complete(lreq, -ENOENT);
2411                        __linger_cancel(lreq);
2412                }
2413        } else {
2414                send_linger_map_check(lreq);
2415        }
2416}
2417
2418static void linger_map_check_cb(struct ceph_mon_generic_request *greq)
2419{
2420        struct ceph_osd_client *osdc = &greq->monc->client->osdc;
2421        struct ceph_osd_linger_request *lreq;
2422        u64 linger_id = greq->private_data;
2423
2424        WARN_ON(greq->result || !greq->u.newest);
2425
2426        down_write(&osdc->lock);
2427        lreq = lookup_linger_mc(&osdc->linger_map_checks, linger_id);
2428        if (!lreq) {
2429                dout("%s linger_id %llu dne\n", __func__, linger_id);
2430                goto out_unlock;
2431        }
2432
2433        dout("%s lreq %p linger_id %llu map_dne_bound %u newest %llu\n",
2434             __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
2435             greq->u.newest);
2436        if (!lreq->map_dne_bound)
2437                lreq->map_dne_bound = greq->u.newest;
2438        erase_linger_mc(&osdc->linger_map_checks, lreq);
2439        check_linger_pool_dne(lreq);
2440
2441        linger_put(lreq);
2442out_unlock:
2443        up_write(&osdc->lock);
2444}
2445
2446static void send_linger_map_check(struct ceph_osd_linger_request *lreq)
2447{
2448        struct ceph_osd_client *osdc = lreq->osdc;
2449        struct ceph_osd_linger_request *lookup_lreq;
2450        int ret;
2451
2452        verify_osdc_wrlocked(osdc);
2453
2454        lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
2455                                       lreq->linger_id);
2456        if (lookup_lreq) {
2457                WARN_ON(lookup_lreq != lreq);
2458                return;
2459        }
2460
2461        linger_get(lreq);
2462        insert_linger_mc(&osdc->linger_map_checks, lreq);
2463        ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
2464                                          linger_map_check_cb, lreq->linger_id);
2465        WARN_ON(ret);
2466}
2467
2468static int linger_reg_commit_wait(struct ceph_osd_linger_request *lreq)
2469{
2470        int ret;
2471
2472        dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2473        ret = wait_for_completion_interruptible(&lreq->reg_commit_wait);
2474        return ret ?: lreq->reg_commit_error;
2475}
2476
2477static int linger_notify_finish_wait(struct ceph_osd_linger_request *lreq)
2478{
2479        int ret;
2480
2481        dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2482        ret = wait_for_completion_interruptible(&lreq->notify_finish_wait);
2483        return ret ?: lreq->notify_finish_error;
2484}
2485
2486/*
2487 * Timeout callback, called every N seconds.  When 1 or more OSD
2488 * requests has been active for more than N seconds, we send a keepalive
2489 * (tag + timestamp) to its OSD to ensure any communications channel
2490 * reset is detected.
2491 */
2492static void handle_timeout(struct work_struct *work)
2493{
2494        struct ceph_osd_client *osdc =
2495                container_of(work, struct ceph_osd_client, timeout_work.work);
2496        struct ceph_options *opts = osdc->client->options;
2497        unsigned long cutoff = jiffies - opts->osd_keepalive_timeout;
2498        LIST_HEAD(slow_osds);
2499        struct rb_node *n, *p;
2500
2501        dout("%s osdc %p\n", __func__, osdc);
2502        down_write(&osdc->lock);
2503
2504        /*
2505         * ping osds that are a bit slow.  this ensures that if there
2506         * is a break in the TCP connection we will notice, and reopen
2507         * a connection with that osd (from the fault callback).
2508         */
2509        for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
2510                struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
2511                bool found = false;
2512
2513                for (p = rb_first(&osd->o_requests); p; p = rb_next(p)) {
2514                        struct ceph_osd_request *req =
2515                            rb_entry(p, struct ceph_osd_request, r_node);
2516
2517                        if (time_before(req->r_stamp, cutoff)) {
2518                                dout(" req %p tid %llu on osd%d is laggy\n",
2519                                     req, req->r_tid, osd->o_osd);
2520                                found = true;
2521                        }
2522                }
2523                for (p = rb_first(&osd->o_linger_requests); p; p = rb_next(p)) {
2524                        struct ceph_osd_linger_request *lreq =
2525                            rb_entry(p, struct ceph_osd_linger_request, node);
2526
2527                        dout(" lreq %p linger_id %llu is served by osd%d\n",
2528                             lreq, lreq->linger_id, osd->o_osd);
2529                        found = true;
2530
2531                        mutex_lock(&lreq->lock);
2532                        if (lreq->is_watch && lreq->committed && !lreq->last_error)
2533                                send_linger_ping(lreq);
2534                        mutex_unlock(&lreq->lock);
2535                }
2536
2537                if (found)
2538                        list_move_tail(&osd->o_keepalive_item, &slow_osds);
2539        }
2540
2541        if (atomic_read(&osdc->num_homeless) || !list_empty(&slow_osds))
2542                maybe_request_map(osdc);
2543
2544        while (!list_empty(&slow_osds)) {
2545                struct ceph_osd *osd = list_first_entry(&slow_osds,
2546                                                        struct ceph_osd,
2547                                                        o_keepalive_item);
2548                list_del_init(&osd->o_keepalive_item);
2549                ceph_con_keepalive(&osd->o_con);
2550        }
2551
2552        up_write(&osdc->lock);
2553        schedule_delayed_work(&osdc->timeout_work,
2554                              osdc->client->options->osd_keepalive_timeout);
2555}
2556
2557static void handle_osds_timeout(struct work_struct *work)
2558{
2559        struct ceph_osd_client *osdc =
2560                container_of(work, struct ceph_osd_client,
2561                             osds_timeout_work.work);
2562        unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
2563        struct ceph_osd *osd, *nosd;
2564
2565        dout("%s osdc %p\n", __func__, osdc);
2566        down_write(&osdc->lock);
2567        list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
2568                if (time_before(jiffies, osd->lru_ttl))
2569                        break;
2570
2571                WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
2572                WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
2573                close_osd(osd);
2574        }
2575
2576        up_write(&osdc->lock);
2577        schedule_delayed_work(&osdc->osds_timeout_work,
2578                              round_jiffies_relative(delay));
2579}
2580
2581static int ceph_oloc_decode(void **p, void *end,
2582                            struct ceph_object_locator *oloc)
2583{
2584        u8 struct_v, struct_cv;
2585        u32 len;
2586        void *struct_end;
2587        int ret = 0;
2588
2589        ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
2590        struct_v = ceph_decode_8(p);
2591        struct_cv = ceph_decode_8(p);
2592        if (struct_v < 3) {
2593                pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
2594                        struct_v, struct_cv);
2595                goto e_inval;
2596        }
2597        if (struct_cv > 6) {
2598                pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
2599                        struct_v, struct_cv);
2600                goto e_inval;
2601        }
2602        len = ceph_decode_32(p);
2603        ceph_decode_need(p, end, len, e_inval);
2604        struct_end = *p + len;
2605
2606        oloc->pool = ceph_decode_64(p);
2607        *p += 4; /* skip preferred */
2608
2609        len = ceph_decode_32(p);
2610        if (len > 0) {
2611                pr_warn("ceph_object_locator::key is set\n");
2612                goto e_inval;
2613        }
2614
2615        if (struct_v >= 5) {
2616                bool changed = false;
2617
2618                len = ceph_decode_32(p);
2619                if (len > 0) {
2620                        ceph_decode_need(p, end, len, e_inval);
2621                        if (!oloc->pool_ns ||
2622                            ceph_compare_string(oloc->pool_ns, *p, len))
2623                                changed = true;
2624                        *p += len;
2625                } else {
2626                        if (oloc->pool_ns)
2627                                changed = true;
2628                }
2629                if (changed) {
2630                        /* redirect changes namespace */
2631                        pr_warn("ceph_object_locator::nspace is changed\n");
2632                        goto e_inval;
2633                }
2634        }
2635
2636        if (struct_v >= 6) {
2637                s64 hash = ceph_decode_64(p);
2638                if (hash != -1) {
2639                        pr_warn("ceph_object_locator::hash is set\n");
2640                        goto e_inval;
2641                }
2642        }
2643
2644        /* skip the rest */
2645        *p = struct_end;
2646out:
2647        return ret;
2648
2649e_inval:
2650        ret = -EINVAL;
2651        goto out;
2652}
2653
2654static int ceph_redirect_decode(void **p, void *end,
2655                                struct ceph_request_redirect *redir)
2656{
2657        u8 struct_v, struct_cv;
2658        u32 len;
2659        void *struct_end;
2660        int ret;
2661
2662        ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
2663        struct_v = ceph_decode_8(p);
2664        struct_cv = ceph_decode_8(p);
2665        if (struct_cv > 1) {
2666                pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
2667                        struct_v, struct_cv);
2668                goto e_inval;
2669        }
2670        len = ceph_decode_32(p);
2671        ceph_decode_need(p, end, len, e_inval);
2672        struct_end = *p + len;
2673
2674        ret = ceph_oloc_decode(p, end, &redir->oloc);
2675        if (ret)
2676                goto out;
2677
2678        len = ceph_decode_32(p);
2679        if (len > 0) {
2680                pr_warn("ceph_request_redirect::object_name is set\n");
2681                goto e_inval;
2682        }
2683
2684        len = ceph_decode_32(p);
2685        *p += len; /* skip osd_instructions */
2686
2687        /* skip the rest */
2688        *p = struct_end;
2689out:
2690        return ret;
2691
2692e_inval:
2693        ret = -EINVAL;
2694        goto out;
2695}
2696
2697struct MOSDOpReply {
2698        struct ceph_pg pgid;
2699        u64 flags;
2700        int result;
2701        u32 epoch;
2702        int num_ops;
2703        u32 outdata_len[CEPH_OSD_MAX_OPS];
2704        s32 rval[CEPH_OSD_MAX_OPS];
2705        int retry_attempt;
2706        struct ceph_eversion replay_version;
2707        u64 user_version;
2708        struct ceph_request_redirect redirect;
2709};
2710
2711static int decode_MOSDOpReply(const struct ceph_msg *msg, struct MOSDOpReply *m)
2712{
2713        void *p = msg->front.iov_base;
2714        void *const end = p + msg->front.iov_len;
2715        u16 version = le16_to_cpu(msg->hdr.version);
2716        struct ceph_eversion bad_replay_version;
2717        u8 decode_redir;
2718        u32 len;
2719        int ret;
2720        int i;
2721
2722        ceph_decode_32_safe(&p, end, len, e_inval);
2723        ceph_decode_need(&p, end, len, e_inval);
2724        p += len; /* skip oid */
2725
2726        ret = ceph_decode_pgid(&p, end, &m->pgid);
2727        if (ret)
2728                return ret;
2729
2730        ceph_decode_64_safe(&p, end, m->flags, e_inval);
2731        ceph_decode_32_safe(&p, end, m->result, e_inval);
2732        ceph_decode_need(&p, end, sizeof(bad_replay_version), e_inval);
2733        memcpy(&bad_replay_version, p, sizeof(bad_replay_version));
2734        p += sizeof(bad_replay_version);
2735        ceph_decode_32_safe(&p, end, m->epoch, e_inval);
2736
2737        ceph_decode_32_safe(&p, end, m->num_ops, e_inval);
2738        if (m->num_ops > ARRAY_SIZE(m->outdata_len))
2739                goto e_inval;
2740
2741        ceph_decode_need(&p, end, m->num_ops * sizeof(struct ceph_osd_op),
2742                         e_inval);
2743        for (i = 0; i < m->num_ops; i++) {
2744                struct ceph_osd_op *op = p;
2745
2746                m->outdata_len[i] = le32_to_cpu(op->payload_len);
2747                p += sizeof(*op);
2748        }
2749
2750        ceph_decode_32_safe(&p, end, m->retry_attempt, e_inval);
2751        for (i = 0; i < m->num_ops; i++)
2752                ceph_decode_32_safe(&p, end, m->rval[i], e_inval);
2753
2754        if (version >= 5) {
2755                ceph_decode_need(&p, end, sizeof(m->replay_version), e_inval);
2756                memcpy(&m->replay_version, p, sizeof(m->replay_version));
2757                p += sizeof(m->replay_version);
2758                ceph_decode_64_safe(&p, end, m->user_version, e_inval);
2759        } else {
2760                m->replay_version = bad_replay_version; /* struct */
2761                m->user_version = le64_to_cpu(m->replay_version.version);
2762        }
2763
2764        if (version >= 6) {
2765                if (version >= 7)
2766                        ceph_decode_8_safe(&p, end, decode_redir, e_inval);
2767                else
2768                        decode_redir = 1;
2769        } else {
2770                decode_redir = 0;
2771        }
2772
2773        if (decode_redir) {
2774                ret = ceph_redirect_decode(&p, end, &m->redirect);
2775                if (ret)
2776                        return ret;
2777        } else {
2778                ceph_oloc_init(&m->redirect.oloc);
2779        }
2780
2781        return 0;
2782
2783e_inval:
2784        return -EINVAL;
2785}
2786
2787/*
2788 * We are done with @req if
2789 *   - @m is a safe reply, or
2790 *   - @m is an unsafe reply and we didn't want a safe one
2791 */
2792static bool done_request(const struct ceph_osd_request *req,
2793                         const struct MOSDOpReply *m)
2794{
2795        return (m->result < 0 ||
2796                (m->flags & CEPH_OSD_FLAG_ONDISK) ||
2797                !(req->r_flags & CEPH_OSD_FLAG_ONDISK));
2798}
2799
2800/*
2801 * handle osd op reply.  either call the callback if it is specified,
2802 * or do the completion to wake up the waiting thread.
2803 *
2804 * ->r_unsafe_callback is set?  yes                     no
2805 *
2806 * first reply is OK (needed    r_cb/r_completion,      r_cb/r_completion,
2807 * any or needed/got safe)      r_done_completion       r_done_completion
2808 *
2809 * first reply is unsafe        r_unsafe_cb(true)       (nothing)
2810 *
2811 * when we get the safe reply   r_unsafe_cb(false),     r_cb/r_completion,
2812 *                              r_done_completion       r_done_completion
2813 */
2814static void handle_reply(struct ceph_osd *osd, struct ceph_msg *msg)
2815{
2816        struct ceph_osd_client *osdc = osd->o_osdc;
2817        struct ceph_osd_request *req;
2818        struct MOSDOpReply m;
2819        u64 tid = le64_to_cpu(msg->hdr.tid);
2820        u32 data_len = 0;
2821        bool already_acked;
2822        int ret;
2823        int i;
2824
2825        dout("%s msg %p tid %llu\n", __func__, msg, tid);
2826
2827        down_read(&osdc->lock);
2828        if (!osd_registered(osd)) {
2829                dout("%s osd%d unknown\n", __func__, osd->o_osd);
2830                goto out_unlock_osdc;
2831        }
2832        WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
2833
2834        mutex_lock(&osd->lock);
2835        req = lookup_request(&osd->o_requests, tid);
2836        if (!req) {
2837                dout("%s osd%d tid %llu unknown\n", __func__, osd->o_osd, tid);
2838                goto out_unlock_session;
2839        }
2840
2841        m.redirect.oloc.pool_ns = req->r_t.target_oloc.pool_ns;
2842        ret = decode_MOSDOpReply(msg, &m);
2843        m.redirect.oloc.pool_ns = NULL;
2844        if (ret) {
2845                pr_err("failed to decode MOSDOpReply for tid %llu: %d\n",
2846                       req->r_tid, ret);
2847                ceph_msg_dump(msg);
2848                goto fail_request;
2849        }
2850        dout("%s req %p tid %llu flags 0x%llx pgid %llu.%x epoch %u attempt %d v %u'%llu uv %llu\n",
2851             __func__, req, req->r_tid, m.flags, m.pgid.pool, m.pgid.seed,
2852             m.epoch, m.retry_attempt, le32_to_cpu(m.replay_version.epoch),
2853             le64_to_cpu(m.replay_version.version), m.user_version);
2854
2855        if (m.retry_attempt >= 0) {
2856                if (m.retry_attempt != req->r_attempts - 1) {
2857                        dout("req %p tid %llu retry_attempt %d != %d, ignoring\n",
2858                             req, req->r_tid, m.retry_attempt,
2859                             req->r_attempts - 1);
2860                        goto out_unlock_session;
2861                }
2862        } else {
2863                WARN_ON(1); /* MOSDOpReply v4 is assumed */
2864        }
2865
2866        if (!ceph_oloc_empty(&m.redirect.oloc)) {
2867                dout("req %p tid %llu redirect pool %lld\n", req, req->r_tid,
2868                     m.redirect.oloc.pool);
2869                unlink_request(osd, req);
2870                mutex_unlock(&osd->lock);
2871
2872                /*
2873                 * Not ceph_oloc_copy() - changing pool_ns is not
2874                 * supported.
2875                 */
2876                req->r_t.target_oloc.pool = m.redirect.oloc.pool;
2877                req->r_flags |= CEPH_OSD_FLAG_REDIRECTED;
2878                req->r_tid = 0;
2879                __submit_request(req, false);
2880                goto out_unlock_osdc;
2881        }
2882
2883        if (m.num_ops != req->r_num_ops) {
2884                pr_err("num_ops %d != %d for tid %llu\n", m.num_ops,
2885                       req->r_num_ops, req->r_tid);
2886                goto fail_request;
2887        }
2888        for (i = 0; i < req->r_num_ops; i++) {
2889                dout(" req %p tid %llu op %d rval %d len %u\n", req,
2890                     req->r_tid, i, m.rval[i], m.outdata_len[i]);
2891                req->r_ops[i].rval = m.rval[i];
2892                req->r_ops[i].outdata_len = m.outdata_len[i];
2893                data_len += m.outdata_len[i];
2894        }
2895        if (data_len != le32_to_cpu(msg->hdr.data_len)) {
2896                pr_err("sum of lens %u != %u for tid %llu\n", data_len,
2897                       le32_to_cpu(msg->hdr.data_len), req->r_tid);
2898                goto fail_request;
2899        }
2900        dout("%s req %p tid %llu acked %d result %d data_len %u\n", __func__,
2901             req, req->r_tid, req->r_got_reply, m.result, data_len);
2902
2903        already_acked = req->r_got_reply;
2904        if (!already_acked) {
2905                req->r_result = m.result ?: data_len;
2906                req->r_replay_version = m.replay_version; /* struct */
2907                req->r_got_reply = true;
2908        } else if (!(m.flags & CEPH_OSD_FLAG_ONDISK)) {
2909                dout("req %p tid %llu dup ack\n", req, req->r_tid);
2910                goto out_unlock_session;
2911        }
2912
2913        if (done_request(req, &m)) {
2914                finish_request(req);
2915                if (req->r_linger) {
2916                        WARN_ON(req->r_unsafe_callback);
2917                        dout("req %p tid %llu cb (locked)\n", req, req->r_tid);
2918                        __complete_request(req);
2919                }
2920        }
2921
2922        mutex_unlock(&osd->lock);
2923        up_read(&osdc->lock);
2924
2925        if (done_request(req, &m)) {
2926                if (already_acked && req->r_unsafe_callback) {
2927                        dout("req %p tid %llu safe-cb\n", req, req->r_tid);
2928                        req->r_unsafe_callback(req, false);
2929                } else if (!req->r_linger) {
2930                        dout("req %p tid %llu cb\n", req, req->r_tid);
2931                        __complete_request(req);
2932                }
2933                complete_all(&req->r_done_completion);
2934                ceph_osdc_put_request(req);
2935        } else {
2936                if (req->r_unsafe_callback) {
2937                        dout("req %p tid %llu unsafe-cb\n", req, req->r_tid);
2938                        req->r_unsafe_callback(req, true);
2939                } else {
2940                        WARN_ON(1);
2941                }
2942        }
2943
2944        return;
2945
2946fail_request:
2947        complete_request(req, -EIO);
2948out_unlock_session:
2949        mutex_unlock(&osd->lock);
2950out_unlock_osdc:
2951        up_read(&osdc->lock);
2952}
2953
2954static void set_pool_was_full(struct ceph_osd_client *osdc)
2955{
2956        struct rb_node *n;
2957
2958        for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
2959                struct ceph_pg_pool_info *pi =
2960                    rb_entry(n, struct ceph_pg_pool_info, node);
2961
2962                pi->was_full = __pool_full(pi);
2963        }
2964}
2965
2966static bool pool_cleared_full(struct ceph_osd_client *osdc, s64 pool_id)
2967{
2968        struct ceph_pg_pool_info *pi;
2969
2970        pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
2971        if (!pi)
2972                return false;
2973
2974        return pi->was_full && !__pool_full(pi);
2975}
2976
2977static enum calc_target_result
2978recalc_linger_target(struct ceph_osd_linger_request *lreq)
2979{
2980        struct ceph_osd_client *osdc = lreq->osdc;
2981        enum calc_target_result ct_res;
2982
2983        ct_res = calc_target(osdc, &lreq->t, &lreq->last_force_resend, true);
2984        if (ct_res == CALC_TARGET_NEED_RESEND) {
2985                struct ceph_osd *osd;
2986
2987                osd = lookup_create_osd(osdc, lreq->t.osd, true);
2988                if (osd != lreq->osd) {
2989                        unlink_linger(lreq->osd, lreq);
2990                        link_linger(osd, lreq);
2991                }
2992        }
2993
2994        return ct_res;
2995}
2996
2997/*
2998 * Requeue requests whose mapping to an OSD has changed.
2999 */
3000static void scan_requests(struct ceph_osd *osd,
3001                          bool force_resend,
3002                          bool cleared_full,
3003                          bool check_pool_cleared_full,
3004                          struct rb_root *need_resend,
3005                          struct list_head *need_resend_linger)
3006{
3007        struct ceph_osd_client *osdc = osd->o_osdc;
3008        struct rb_node *n;
3009        bool force_resend_writes;
3010
3011        for (n = rb_first(&osd->o_linger_requests); n; ) {
3012                struct ceph_osd_linger_request *lreq =
3013                    rb_entry(n, struct ceph_osd_linger_request, node);
3014                enum calc_target_result ct_res;
3015
3016                n = rb_next(n); /* recalc_linger_target() */
3017
3018                dout("%s lreq %p linger_id %llu\n", __func__, lreq,
3019                     lreq->linger_id);
3020                ct_res = recalc_linger_target(lreq);
3021                switch (ct_res) {
3022                case CALC_TARGET_NO_ACTION:
3023                        force_resend_writes = cleared_full ||
3024                            (check_pool_cleared_full &&
3025                             pool_cleared_full(osdc, lreq->t.base_oloc.pool));
3026                        if (!force_resend && !force_resend_writes)
3027                                break;
3028
3029                        /* fall through */
3030                case CALC_TARGET_NEED_RESEND:
3031                        cancel_linger_map_check(lreq);
3032                        /*
3033                         * scan_requests() for the previous epoch(s)
3034                         * may have already added it to the list, since
3035                         * it's not unlinked here.
3036                         */
3037                        if (list_empty(&lreq->scan_item))
3038                                list_add_tail(&lreq->scan_item, need_resend_linger);
3039                        break;
3040                case CALC_TARGET_POOL_DNE:
3041                        check_linger_pool_dne(lreq);
3042                        break;
3043                }
3044        }
3045
3046        for (n = rb_first(&osd->o_requests); n; ) {
3047                struct ceph_osd_request *req =
3048                    rb_entry(n, struct ceph_osd_request, r_node);
3049                enum calc_target_result ct_res;
3050
3051                n = rb_next(n); /* unlink_request(), check_pool_dne() */
3052
3053                dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
3054                ct_res = calc_target(osdc, &req->r_t,
3055                                     &req->r_last_force_resend, false);
3056                switch (ct_res) {
3057                case CALC_TARGET_NO_ACTION:
3058                        force_resend_writes = cleared_full ||
3059                            (check_pool_cleared_full &&
3060                             pool_cleared_full(osdc, req->r_t.base_oloc.pool));
3061                        if (!force_resend &&
3062                            (!(req->r_flags & CEPH_OSD_FLAG_WRITE) ||
3063                             !force_resend_writes))
3064                                break;
3065
3066                        /* fall through */
3067                case CALC_TARGET_NEED_RESEND:
3068                        cancel_map_check(req);
3069                        unlink_request(osd, req);
3070                        insert_request(need_resend, req);
3071                        break;
3072                case CALC_TARGET_POOL_DNE:
3073                        check_pool_dne(req);
3074                        break;
3075                }
3076        }
3077}
3078
3079static int handle_one_map(struct ceph_osd_client *osdc,
3080                          void *p, void *end, bool incremental,
3081                          struct rb_root *need_resend,
3082                          struct list_head *need_resend_linger)
3083{
3084        struct ceph_osdmap *newmap;
3085        struct rb_node *n;
3086        bool skipped_map = false;
3087        bool was_full;
3088
3089        was_full = ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
3090        set_pool_was_full(osdc);
3091
3092        if (incremental)
3093                newmap = osdmap_apply_incremental(&p, end, osdc->osdmap);
3094        else
3095                newmap = ceph_osdmap_decode(&p, end);
3096        if (IS_ERR(newmap))
3097                return PTR_ERR(newmap);
3098
3099        if (newmap != osdc->osdmap) {
3100                /*
3101                 * Preserve ->was_full before destroying the old map.
3102                 * For pools that weren't in the old map, ->was_full
3103                 * should be false.
3104                 */
3105                for (n = rb_first(&newmap->pg_pools); n; n = rb_next(n)) {
3106                        struct ceph_pg_pool_info *pi =
3107                            rb_entry(n, struct ceph_pg_pool_info, node);
3108                        struct ceph_pg_pool_info *old_pi;
3109
3110                        old_pi = ceph_pg_pool_by_id(osdc->osdmap, pi->id);
3111                        if (old_pi)
3112                                pi->was_full = old_pi->was_full;
3113                        else
3114                                WARN_ON(pi->was_full);
3115                }
3116
3117                if (osdc->osdmap->epoch &&
3118                    osdc->osdmap->epoch + 1 < newmap->epoch) {
3119                        WARN_ON(incremental);
3120                        skipped_map = true;
3121                }
3122
3123                ceph_osdmap_destroy(osdc->osdmap);
3124                osdc->osdmap = newmap;
3125        }
3126
3127        was_full &= !ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
3128        scan_requests(&osdc->homeless_osd, skipped_map, was_full, true,
3129                      need_resend, need_resend_linger);
3130
3131        for (n = rb_first(&osdc->osds); n; ) {
3132                struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
3133
3134                n = rb_next(n); /* close_osd() */
3135
3136                scan_requests(osd, skipped_map, was_full, true, need_resend,
3137                              need_resend_linger);
3138                if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
3139                    memcmp(&osd->o_con.peer_addr,
3140                           ceph_osd_addr(osdc->osdmap, osd->o_osd),
3141                           sizeof(struct ceph_entity_addr)))
3142                        close_osd(osd);
3143        }
3144
3145        return 0;
3146}
3147
3148static void kick_requests(struct ceph_osd_client *osdc,
3149                          struct rb_root *need_resend,
3150                          struct list_head *need_resend_linger)
3151{
3152        struct ceph_osd_linger_request *lreq, *nlreq;
3153        struct rb_node *n;
3154
3155        for (n = rb_first(need_resend); n; ) {
3156                struct ceph_osd_request *req =
3157                    rb_entry(n, struct ceph_osd_request, r_node);
3158                struct ceph_osd *osd;
3159
3160                n = rb_next(n);
3161                erase_request(need_resend, req); /* before link_request() */
3162
3163                WARN_ON(req->r_osd);
3164                calc_target(osdc, &req->r_t, NULL, false);
3165                osd = lookup_create_osd(osdc, req->r_t.osd, true);
3166                link_request(osd, req);
3167                if (!req->r_linger) {
3168                        if (!osd_homeless(osd) && !req->r_t.paused)
3169                                send_request(req);
3170                } else {
3171                        cancel_linger_request(req);
3172                }
3173        }
3174
3175        list_for_each_entry_safe(lreq, nlreq, need_resend_linger, scan_item) {
3176                if (!osd_homeless(lreq->osd))
3177                        send_linger(lreq);
3178
3179                list_del_init(&lreq->scan_item);
3180        }
3181}
3182
3183/*
3184 * Process updated osd map.
3185 *
3186 * The message contains any number of incremental and full maps, normally
3187 * indicating some sort of topology change in the cluster.  Kick requests
3188 * off to different OSDs as needed.
3189 */
3190void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
3191{
3192        void *p = msg->front.iov_base;
3193        void *const end = p + msg->front.iov_len;
3194        u32 nr_maps, maplen;
3195        u32 epoch;
3196        struct ceph_fsid fsid;
3197        struct rb_root need_resend = RB_ROOT;
3198        LIST_HEAD(need_resend_linger);
3199        bool handled_incremental = false;
3200        bool was_pauserd, was_pausewr;
3201        bool pauserd, pausewr;
3202        int err;
3203
3204        dout("%s have %u\n", __func__, osdc->osdmap->epoch);
3205        down_write(&osdc->lock);
3206
3207        /* verify fsid */
3208        ceph_decode_need(&p, end, sizeof(fsid), bad);
3209        ceph_decode_copy(&p, &fsid, sizeof(fsid));
3210        if (ceph_check_fsid(osdc->client, &fsid) < 0)
3211                goto bad;
3212
3213        was_pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3214        was_pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3215                      ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
3216                      have_pool_full(osdc);
3217
3218        /* incremental maps */
3219        ceph_decode_32_safe(&p, end, nr_maps, bad);
3220        dout(" %d inc maps\n", nr_maps);
3221        while (nr_maps > 0) {
3222                ceph_decode_need(&p, end, 2*sizeof(u32), bad);
3223                epoch = ceph_decode_32(&p);
3224                maplen = ceph_decode_32(&p);
3225                ceph_decode_need(&p, end, maplen, bad);
3226                if (osdc->osdmap->epoch &&
3227                    osdc->osdmap->epoch + 1 == epoch) {
3228                        dout("applying incremental map %u len %d\n",
3229                             epoch, maplen);
3230                        err = handle_one_map(osdc, p, p + maplen, true,
3231                                             &need_resend, &need_resend_linger);
3232                        if (err)
3233                                goto bad;
3234                        handled_incremental = true;
3235                } else {
3236                        dout("ignoring incremental map %u len %d\n",
3237                             epoch, maplen);
3238                }
3239                p += maplen;
3240                nr_maps--;
3241        }
3242        if (handled_incremental)
3243                goto done;
3244
3245        /* full maps */
3246        ceph_decode_32_safe(&p, end, nr_maps, bad);
3247        dout(" %d full maps\n", nr_maps);
3248        while (nr_maps) {
3249                ceph_decode_need(&p, end, 2*sizeof(u32), bad);
3250                epoch = ceph_decode_32(&p);
3251                maplen = ceph_decode_32(&p);
3252                ceph_decode_need(&p, end, maplen, bad);
3253                if (nr_maps > 1) {
3254                        dout("skipping non-latest full map %u len %d\n",
3255                             epoch, maplen);
3256                } else if (osdc->osdmap->epoch >= epoch) {
3257                        dout("skipping full map %u len %d, "
3258                             "older than our %u\n", epoch, maplen,
3259                             osdc->osdmap->epoch);
3260                } else {
3261                        dout("taking full map %u len %d\n", epoch, maplen);
3262                        err = handle_one_map(osdc, p, p + maplen, false,
3263                                             &need_resend, &need_resend_linger);
3264                        if (err)
3265                                goto bad;
3266                }
3267                p += maplen;
3268                nr_maps--;
3269        }
3270
3271done:
3272        /*
3273         * subscribe to subsequent osdmap updates if full to ensure
3274         * we find out when we are no longer full and stop returning
3275         * ENOSPC.
3276         */
3277        pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3278        pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3279                  ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
3280                  have_pool_full(osdc);
3281        if (was_pauserd || was_pausewr || pauserd || pausewr)
3282                maybe_request_map(osdc);
3283
3284        kick_requests(osdc, &need_resend, &need_resend_linger);
3285
3286        ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
3287                          osdc->osdmap->epoch);
3288        up_write(&osdc->lock);
3289        wake_up_all(&osdc->client->auth_wq);
3290        return;
3291
3292bad:
3293        pr_err("osdc handle_map corrupt msg\n");
3294        ceph_msg_dump(msg);
3295        up_write(&osdc->lock);
3296}
3297
3298/*
3299 * Resubmit requests pending on the given osd.
3300 */
3301static void kick_osd_requests(struct ceph_osd *osd)
3302{
3303        struct rb_node *n;
3304
3305        for (n = rb_first(&osd->o_requests); n; ) {
3306                struct ceph_osd_request *req =
3307                    rb_entry(n, struct ceph_osd_request, r_node);
3308
3309                n = rb_next(n); /* cancel_linger_request() */
3310
3311                if (!req->r_linger) {
3312                        if (!req->r_t.paused)
3313                                send_request(req);
3314                } else {
3315                        cancel_linger_request(req);
3316                }
3317        }
3318        for (n = rb_first(&osd->o_linger_requests); n; n = rb_next(n)) {
3319                struct ceph_osd_linger_request *lreq =
3320                    rb_entry(n, struct ceph_osd_linger_request, node);
3321
3322                send_linger(lreq);
3323        }
3324}
3325
3326/*
3327 * If the osd connection drops, we need to resubmit all requests.
3328 */
3329static void osd_fault(struct ceph_connection *con)
3330{
3331        struct ceph_osd *osd = con->private;
3332        struct ceph_osd_client *osdc = osd->o_osdc;
3333
3334        dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
3335
3336        down_write(&osdc->lock);
3337        if (!osd_registered(osd)) {
3338                dout("%s osd%d unknown\n", __func__, osd->o_osd);
3339                goto out_unlock;
3340        }
3341
3342        if (!reopen_osd(osd))
3343                kick_osd_requests(osd);
3344        maybe_request_map(osdc);
3345
3346out_unlock:
3347        up_write(&osdc->lock);
3348}
3349
3350/*
3351 * Process osd watch notifications
3352 */
3353static void handle_watch_notify(struct ceph_osd_client *osdc,
3354                                struct ceph_msg *msg)
3355{
3356        void *p = msg->front.iov_base;
3357        void *const end = p + msg->front.iov_len;
3358        struct ceph_osd_linger_request *lreq;
3359        struct linger_work *lwork;
3360        u8 proto_ver, opcode;
3361        u64 cookie, notify_id;
3362        u64 notifier_id = 0;
3363        s32 return_code = 0;
3364        void *payload = NULL;
3365        u32 payload_len = 0;
3366
3367        ceph_decode_8_safe(&p, end, proto_ver, bad);
3368        ceph_decode_8_safe(&p, end, opcode, bad);
3369        ceph_decode_64_safe(&p, end, cookie, bad);
3370        p += 8; /* skip ver */
3371        ceph_decode_64_safe(&p, end, notify_id, bad);
3372
3373        if (proto_ver >= 1) {
3374                ceph_decode_32_safe(&p, end, payload_len, bad);
3375                ceph_decode_need(&p, end, payload_len, bad);
3376                payload = p;
3377                p += payload_len;
3378        }
3379
3380        if (le16_to_cpu(msg->hdr.version) >= 2)
3381                ceph_decode_32_safe(&p, end, return_code, bad);
3382
3383        if (le16_to_cpu(msg->hdr.version) >= 3)
3384                ceph_decode_64_safe(&p, end, notifier_id, bad);
3385
3386        down_read(&osdc->lock);
3387        lreq = lookup_linger_osdc(&osdc->linger_requests, cookie);
3388        if (!lreq) {
3389                dout("%s opcode %d cookie %llu dne\n", __func__, opcode,
3390                     cookie);
3391                goto out_unlock_osdc;
3392        }
3393
3394        mutex_lock(&lreq->lock);
3395        dout("%s opcode %d cookie %llu lreq %p is_watch %d\n", __func__,
3396             opcode, cookie, lreq, lreq->is_watch);
3397        if (opcode == CEPH_WATCH_EVENT_DISCONNECT) {
3398                if (!lreq->last_error) {
3399                        lreq->last_error = -ENOTCONN;
3400                        queue_watch_error(lreq);
3401                }
3402        } else if (!lreq->is_watch) {
3403                /* CEPH_WATCH_EVENT_NOTIFY_COMPLETE */
3404                if (lreq->notify_id && lreq->notify_id != notify_id) {
3405                        dout("lreq %p notify_id %llu != %llu, ignoring\n", lreq,
3406                             lreq->notify_id, notify_id);
3407                } else if (!completion_done(&lreq->notify_finish_wait)) {
3408                        struct ceph_msg_data *data =
3409                            list_first_entry_or_null(&msg->data,
3410                                                     struct ceph_msg_data,
3411                                                     links);
3412
3413                        if (data) {
3414                                if (lreq->preply_pages) {
3415                                        WARN_ON(data->type !=
3416                                                        CEPH_MSG_DATA_PAGES);
3417                                        *lreq->preply_pages = data->pages;
3418                                        *lreq->preply_len = data->length;
3419                                } else {
3420                                        ceph_release_page_vector(data->pages,
3421                                               calc_pages_for(0, data->length));
3422                                }
3423                        }
3424                        lreq->notify_finish_error = return_code;
3425                        complete_all(&lreq->notify_finish_wait);
3426                }
3427        } else {
3428                /* CEPH_WATCH_EVENT_NOTIFY */
3429                lwork = lwork_alloc(lreq, do_watch_notify);
3430                if (!lwork) {
3431                        pr_err("failed to allocate notify-lwork\n");
3432                        goto out_unlock_lreq;
3433                }
3434
3435                lwork->notify.notify_id = notify_id;
3436                lwork->notify.notifier_id = notifier_id;
3437                lwork->notify.payload = payload;
3438                lwork->notify.payload_len = payload_len;
3439                lwork->notify.msg = ceph_msg_get(msg);
3440                lwork_queue(lwork);
3441        }
3442
3443out_unlock_lreq:
3444        mutex_unlock(&lreq->lock);
3445out_unlock_osdc:
3446        up_read(&osdc->lock);
3447        return;
3448
3449bad:
3450        pr_err("osdc handle_watch_notify corrupt msg\n");
3451}
3452
3453/*
3454 * Register request, send initial attempt.
3455 */
3456int ceph_osdc_start_request(struct ceph_osd_client *osdc,
3457                            struct ceph_osd_request *req,
3458                            bool nofail)
3459{
3460        down_read(&osdc->lock);
3461        submit_request(req, false);
3462        up_read(&osdc->lock);
3463
3464        return 0;
3465}
3466EXPORT_SYMBOL(ceph_osdc_start_request);
3467
3468/*
3469 * Unregister a registered request.  The request is not completed:
3470 * ->r_result isn't set and __complete_request() isn't called.
3471 */
3472void ceph_osdc_cancel_request(struct ceph_osd_request *req)
3473{
3474        struct ceph_osd_client *osdc = req->r_osdc;
3475
3476        down_write(&osdc->lock);
3477        if (req->r_osd)
3478                cancel_request(req);
3479        up_write(&osdc->lock);
3480}
3481EXPORT_SYMBOL(ceph_osdc_cancel_request);
3482
3483/*
3484 * @timeout: in jiffies, 0 means "wait forever"
3485 */
3486static int wait_request_timeout(struct ceph_osd_request *req,
3487                                unsigned long timeout)
3488{
3489        long left;
3490
3491        dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
3492        left = wait_for_completion_killable_timeout(&req->r_completion,
3493                                                ceph_timeout_jiffies(timeout));
3494        if (left <= 0) {
3495                left = left ?: -ETIMEDOUT;
3496                ceph_osdc_cancel_request(req);
3497        } else {
3498                left = req->r_result; /* completed */
3499        }
3500
3501        return left;
3502}
3503
3504/*
3505 * wait for a request to complete
3506 */
3507int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
3508                           struct ceph_osd_request *req)
3509{
3510        return wait_request_timeout(req, 0);
3511}
3512EXPORT_SYMBOL(ceph_osdc_wait_request);
3513
3514/*
3515 * sync - wait for all in-flight requests to flush.  avoid starvation.
3516 */
3517void ceph_osdc_sync(struct ceph_osd_client *osdc)
3518{
3519        struct rb_node *n, *p;
3520        u64 last_tid = atomic64_read(&osdc->last_tid);
3521
3522again:
3523        down_read(&osdc->lock);
3524        for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
3525                struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
3526
3527                mutex_lock(&osd->lock);
3528                for (p = rb_first(&osd->o_requests); p; p = rb_next(p)) {
3529                        struct ceph_osd_request *req =
3530                            rb_entry(p, struct ceph_osd_request, r_node);
3531
3532                        if (req->r_tid > last_tid)
3533                                break;
3534
3535                        if (!(req->r_flags & CEPH_OSD_FLAG_WRITE))
3536                                continue;
3537
3538                        ceph_osdc_get_request(req);
3539                        mutex_unlock(&osd->lock);
3540                        up_read(&osdc->lock);
3541                        dout("%s waiting on req %p tid %llu last_tid %llu\n",
3542                             __func__, req, req->r_tid, last_tid);
3543                        wait_for_completion(&req->r_done_completion);
3544                        ceph_osdc_put_request(req);
3545                        goto again;
3546                }
3547
3548                mutex_unlock(&osd->lock);
3549        }
3550
3551        up_read(&osdc->lock);
3552        dout("%s done last_tid %llu\n", __func__, last_tid);
3553}
3554EXPORT_SYMBOL(ceph_osdc_sync);
3555
3556static struct ceph_osd_request *
3557alloc_linger_request(struct ceph_osd_linger_request *lreq)
3558{
3559        struct ceph_osd_request *req;
3560
3561        req = ceph_osdc_alloc_request(lreq->osdc, NULL, 1, false, GFP_NOIO);
3562        if (!req)
3563                return NULL;
3564
3565        ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
3566        ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
3567
3568        if (ceph_osdc_alloc_messages(req, GFP_NOIO)) {
3569                ceph_osdc_put_request(req);
3570                return NULL;
3571        }
3572
3573        return req;
3574}
3575
3576/*
3577 * Returns a handle, caller owns a ref.
3578 */
3579struct ceph_osd_linger_request *
3580ceph_osdc_watch(struct ceph_osd_client *osdc,
3581                struct ceph_object_id *oid,
3582                struct ceph_object_locator *oloc,
3583                rados_watchcb2_t wcb,
3584                rados_watcherrcb_t errcb,
3585                void *data)
3586{
3587        struct ceph_osd_linger_request *lreq;
3588        int ret;
3589
3590        lreq = linger_alloc(osdc);
3591        if (!lreq)
3592                return ERR_PTR(-ENOMEM);
3593
3594        lreq->is_watch = true;
3595        lreq->wcb = wcb;
3596        lreq->errcb = errcb;
3597        lreq->data = data;
3598        lreq->watch_valid_thru = jiffies;
3599
3600        ceph_oid_copy(&lreq->t.base_oid, oid);
3601        ceph_oloc_copy(&lreq->t.base_oloc, oloc);
3602        lreq->t.flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK;
3603        lreq->mtime = CURRENT_TIME;
3604
3605        lreq->reg_req = alloc_linger_request(lreq);
3606        if (!lreq->reg_req) {
3607                ret = -ENOMEM;
3608                goto err_put_lreq;
3609        }
3610
3611        lreq->ping_req = alloc_linger_request(lreq);
3612        if (!lreq->ping_req) {
3613                ret = -ENOMEM;
3614                goto err_put_lreq;
3615        }
3616
3617        down_write(&osdc->lock);
3618        linger_register(lreq); /* before osd_req_op_* */
3619        osd_req_op_watch_init(lreq->reg_req, 0, lreq->linger_id,
3620                              CEPH_OSD_WATCH_OP_WATCH);
3621        osd_req_op_watch_init(lreq->ping_req, 0, lreq->linger_id,
3622                              CEPH_OSD_WATCH_OP_PING);
3623        linger_submit(lreq);
3624        up_write(&osdc->lock);
3625
3626        ret = linger_reg_commit_wait(lreq);
3627        if (ret) {
3628                linger_cancel(lreq);
3629                goto err_put_lreq;
3630        }
3631
3632        return lreq;
3633
3634err_put_lreq:
3635        linger_put(lreq);
3636        return ERR_PTR(ret);
3637}
3638EXPORT_SYMBOL(ceph_osdc_watch);
3639
3640/*
3641 * Releases a ref.
3642 *
3643 * Times out after mount_timeout to preserve rbd unmap behaviour
3644 * introduced in 2894e1d76974 ("rbd: timeout watch teardown on unmap
3645 * with mount_timeout").
3646 */
3647int ceph_osdc_unwatch(struct ceph_osd_client *osdc,
3648                      struct ceph_osd_linger_request *lreq)
3649{
3650        struct ceph_options *opts = osdc->client->options;
3651        struct ceph_osd_request *req;
3652        int ret;
3653
3654        req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
3655        if (!req)
3656                return -ENOMEM;
3657
3658        ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
3659        ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
3660        req->r_flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK;
3661        req->r_mtime = CURRENT_TIME;
3662        osd_req_op_watch_init(req, 0, lreq->linger_id,
3663                              CEPH_OSD_WATCH_OP_UNWATCH);
3664
3665        ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
3666        if (ret)
3667                goto out_put_req;
3668
3669        ceph_osdc_start_request(osdc, req, false);
3670        linger_cancel(lreq);
3671        linger_put(lreq);
3672        ret = wait_request_timeout(req, opts->mount_timeout);
3673
3674out_put_req:
3675        ceph_osdc_put_request(req);
3676        return ret;
3677}
3678EXPORT_SYMBOL(ceph_osdc_unwatch);
3679
3680static int osd_req_op_notify_ack_init(struct ceph_osd_request *req, int which,
3681                                      u64 notify_id, u64 cookie, void *payload,
3682                                      size_t payload_len)
3683{
3684        struct ceph_osd_req_op *op;
3685        struct ceph_pagelist *pl;
3686        int ret;
3687
3688        op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY_ACK, 0);
3689
3690        pl = kmalloc(sizeof(*pl), GFP_NOIO);
3691        if (!pl)
3692                return -ENOMEM;
3693
3694        ceph_pagelist_init(pl);
3695        ret = ceph_pagelist_encode_64(pl, notify_id);
3696        ret |= ceph_pagelist_encode_64(pl, cookie);
3697        if (payload) {
3698                ret |= ceph_pagelist_encode_32(pl, payload_len);
3699                ret |= ceph_pagelist_append(pl, payload, payload_len);
3700        } else {
3701                ret |= ceph_pagelist_encode_32(pl, 0);
3702        }
3703        if (ret) {
3704                ceph_pagelist_release(pl);
3705                return -ENOMEM;
3706        }
3707
3708        ceph_osd_data_pagelist_init(&op->notify_ack.request_data, pl);
3709        op->indata_len = pl->length;
3710        return 0;
3711}
3712
3713int ceph_osdc_notify_ack(struct ceph_osd_client *osdc,
3714                         struct ceph_object_id *oid,
3715                         struct ceph_object_locator *oloc,
3716                         u64 notify_id,
3717                         u64 cookie,
3718                         void *payload,
3719                         size_t payload_len)
3720{
3721        struct ceph_osd_request *req;
3722        int ret;
3723
3724        req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
3725        if (!req)
3726                return -ENOMEM;
3727
3728        ceph_oid_copy(&req->r_base_oid, oid);
3729        ceph_oloc_copy(&req->r_base_oloc, oloc);
3730        req->r_flags = CEPH_OSD_FLAG_READ;
3731
3732        ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
3733        if (ret)
3734                goto out_put_req;
3735
3736        ret = osd_req_op_notify_ack_init(req, 0, notify_id, cookie, payload,
3737                                         payload_len);
3738        if (ret)
3739                goto out_put_req;
3740
3741        ceph_osdc_start_request(osdc, req, false);
3742        ret = ceph_osdc_wait_request(osdc, req);
3743
3744out_put_req:
3745        ceph_osdc_put_request(req);
3746        return ret;
3747}
3748EXPORT_SYMBOL(ceph_osdc_notify_ack);
3749
3750static int osd_req_op_notify_init(struct ceph_osd_request *req, int which,
3751                                  u64 cookie, u32 prot_ver, u32 timeout,
3752                                  void *payload, size_t payload_len)
3753{
3754        struct ceph_osd_req_op *op;
3755        struct ceph_pagelist *pl;
3756        int ret;
3757
3758        op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY, 0);
3759        op->notify.cookie = cookie;
3760
3761        pl = kmalloc(sizeof(*pl), GFP_NOIO);
3762        if (!pl)
3763                return -ENOMEM;
3764
3765        ceph_pagelist_init(pl);
3766        ret = ceph_pagelist_encode_32(pl, 1); /* prot_ver */
3767        ret |= ceph_pagelist_encode_32(pl, timeout);
3768        ret |= ceph_pagelist_encode_32(pl, payload_len);
3769        ret |= ceph_pagelist_append(pl, payload, payload_len);
3770        if (ret) {
3771                ceph_pagelist_release(pl);
3772                return -ENOMEM;
3773        }
3774
3775        ceph_osd_data_pagelist_init(&op->notify.request_data, pl);
3776        op->indata_len = pl->length;
3777        return 0;
3778}
3779
3780/*
3781 * @timeout: in seconds
3782 *
3783 * @preply_{pages,len} are initialized both on success and error.
3784 * The caller is responsible for:
3785 *
3786 *     ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len))
3787 */
3788int ceph_osdc_notify(struct ceph_osd_client *osdc,
3789                     struct ceph_object_id *oid,
3790                     struct ceph_object_locator *oloc,
3791                     void *payload,
3792                     size_t payload_len,
3793                     u32 timeout,
3794                     struct page ***preply_pages,
3795                     size_t *preply_len)
3796{
3797        struct ceph_osd_linger_request *lreq;
3798        struct page **pages;
3799        int ret;
3800
3801        WARN_ON(!timeout);
3802        if (preply_pages) {
3803                *preply_pages = NULL;
3804                *preply_len = 0;
3805        }
3806
3807        lreq = linger_alloc(osdc);
3808        if (!lreq)
3809                return -ENOMEM;
3810
3811        lreq->preply_pages = preply_pages;
3812        lreq->preply_len = preply_len;
3813
3814        ceph_oid_copy(&lreq->t.base_oid, oid);
3815        ceph_oloc_copy(&lreq->t.base_oloc, oloc);
3816        lreq->t.flags = CEPH_OSD_FLAG_READ;
3817
3818        lreq->reg_req = alloc_linger_request(lreq);
3819        if (!lreq->reg_req) {
3820                ret = -ENOMEM;
3821                goto out_put_lreq;
3822        }
3823
3824        /* for notify_id */
3825        pages = ceph_alloc_page_vector(1, GFP_NOIO);
3826        if (IS_ERR(pages)) {
3827                ret = PTR_ERR(pages);
3828                goto out_put_lreq;
3829        }
3830
3831        down_write(&osdc->lock);
3832        linger_register(lreq); /* before osd_req_op_* */
3833        ret = osd_req_op_notify_init(lreq->reg_req, 0, lreq->linger_id, 1,
3834                                     timeout, payload, payload_len);
3835        if (ret) {
3836                linger_unregister(lreq);
3837                up_write(&osdc->lock);
3838                ceph_release_page_vector(pages, 1);
3839                goto out_put_lreq;
3840        }
3841        ceph_osd_data_pages_init(osd_req_op_data(lreq->reg_req, 0, notify,
3842                                                 response_data),
3843                                 pages, PAGE_SIZE, 0, false, true);
3844        linger_submit(lreq);
3845        up_write(&osdc->lock);
3846
3847        ret = linger_reg_commit_wait(lreq);
3848        if (!ret)
3849                ret = linger_notify_finish_wait(lreq);
3850        else
3851                dout("lreq %p failed to initiate notify %d\n", lreq, ret);
3852
3853        linger_cancel(lreq);
3854out_put_lreq:
3855        linger_put(lreq);
3856        return ret;
3857}
3858EXPORT_SYMBOL(ceph_osdc_notify);
3859
3860/*
3861 * Return the number of milliseconds since the watch was last
3862 * confirmed, or an error.  If there is an error, the watch is no
3863 * longer valid, and should be destroyed with ceph_osdc_unwatch().
3864 */
3865int ceph_osdc_watch_check(struct ceph_osd_client *osdc,
3866                          struct ceph_osd_linger_request *lreq)
3867{
3868        unsigned long stamp, age;
3869        int ret;
3870
3871        down_read(&osdc->lock);
3872        mutex_lock(&lreq->lock);
3873        stamp = lreq->watch_valid_thru;
3874        if (!list_empty(&lreq->pending_lworks)) {
3875                struct linger_work *lwork =
3876                    list_first_entry(&lreq->pending_lworks,
3877                                     struct linger_work,
3878                                     pending_item);
3879
3880                if (time_before(lwork->queued_stamp, stamp))
3881                        stamp = lwork->queued_stamp;
3882        }
3883        age = jiffies - stamp;
3884        dout("%s lreq %p linger_id %llu age %lu last_error %d\n", __func__,
3885             lreq, lreq->linger_id, age, lreq->last_error);
3886        /* we are truncating to msecs, so return a safe upper bound */
3887        ret = lreq->last_error ?: 1 + jiffies_to_msecs(age);
3888
3889        mutex_unlock(&lreq->lock);
3890        up_read(&osdc->lock);
3891        return ret;
3892}
3893
3894static int decode_watcher(void **p, void *end, struct ceph_watch_item *item)
3895{
3896        u8 struct_v;
3897        u32 struct_len;
3898        int ret;
3899
3900        ret = ceph_start_decoding(p, end, 2, "watch_item_t",
3901                                  &struct_v, &struct_len);
3902        if (ret)
3903                return ret;
3904
3905        ceph_decode_copy(p, &item->name, sizeof(item->name));
3906        item->cookie = ceph_decode_64(p);
3907        *p += 4; /* skip timeout_seconds */
3908        if (struct_v >= 2) {
3909                ceph_decode_copy(p, &item->addr, sizeof(item->addr));
3910                ceph_decode_addr(&item->addr);
3911        }
3912
3913        dout("%s %s%llu cookie %llu addr %s\n", __func__,
3914             ENTITY_NAME(item->name), item->cookie,
3915             ceph_pr_addr(&item->addr.in_addr));
3916        return 0;
3917}
3918
3919static int decode_watchers(void **p, void *end,
3920                           struct ceph_watch_item **watchers,
3921                           u32 *num_watchers)
3922{
3923        u8 struct_v;
3924        u32 struct_len;
3925        int i;
3926        int ret;
3927
3928        ret = ceph_start_decoding(p, end, 1, "obj_list_watch_response_t",
3929                                  &struct_v, &struct_len);
3930        if (ret)
3931                return ret;
3932
3933        *num_watchers = ceph_decode_32(p);
3934        *watchers = kcalloc(*num_watchers, sizeof(**watchers), GFP_NOIO);
3935        if (!*watchers)
3936                return -ENOMEM;
3937
3938        for (i = 0; i < *num_watchers; i++) {
3939                ret = decode_watcher(p, end, *watchers + i);
3940                if (ret) {
3941                        kfree(*watchers);
3942                        return ret;
3943                }
3944        }
3945
3946        return 0;
3947}
3948
3949/*
3950 * On success, the caller is responsible for:
3951 *
3952 *     kfree(watchers);
3953 */
3954int ceph_osdc_list_watchers(struct ceph_osd_client *osdc,
3955                            struct ceph_object_id *oid,
3956                            struct ceph_object_locator *oloc,
3957                            struct ceph_watch_item **watchers,
3958                            u32 *num_watchers)
3959{
3960        struct ceph_osd_request *req;
3961        struct page **pages;
3962        int ret;
3963
3964        req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
3965        if (!req)
3966                return -ENOMEM;
3967
3968        ceph_oid_copy(&req->r_base_oid, oid);
3969        ceph_oloc_copy(&req->r_base_oloc, oloc);
3970        req->r_flags = CEPH_OSD_FLAG_READ;
3971
3972        ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
3973        if (ret)
3974                goto out_put_req;
3975
3976        pages = ceph_alloc_page_vector(1, GFP_NOIO);
3977        if (IS_ERR(pages)) {
3978                ret = PTR_ERR(pages);
3979                goto out_put_req;
3980        }
3981
3982        osd_req_op_init(req, 0, CEPH_OSD_OP_LIST_WATCHERS, 0);
3983        ceph_osd_data_pages_init(osd_req_op_data(req, 0, list_watchers,
3984                                                 response_data),
3985                                 pages, PAGE_SIZE, 0, false, true);
3986
3987        ceph_osdc_start_request(osdc, req, false);
3988        ret = ceph_osdc_wait_request(osdc, req);
3989        if (ret >= 0) {
3990                void *p = page_address(pages[0]);
3991                void *const end = p + req->r_ops[0].outdata_len;
3992
3993                ret = decode_watchers(&p, end, watchers, num_watchers);
3994        }
3995
3996out_put_req:
3997        ceph_osdc_put_request(req);
3998        return ret;
3999}
4000EXPORT_SYMBOL(ceph_osdc_list_watchers);
4001
4002/*
4003 * Call all pending notify callbacks - for use after a watch is
4004 * unregistered, to make sure no more callbacks for it will be invoked
4005 */
4006void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
4007{
4008        dout("%s osdc %p\n", __func__, osdc);
4009        flush_workqueue(osdc->notify_wq);
4010}
4011EXPORT_SYMBOL(ceph_osdc_flush_notifies);
4012
4013void ceph_osdc_maybe_request_map(struct ceph_osd_client *osdc)
4014{
4015        down_read(&osdc->lock);
4016        maybe_request_map(osdc);
4017        up_read(&osdc->lock);
4018}
4019EXPORT_SYMBOL(ceph_osdc_maybe_request_map);
4020
4021/*
4022 * Execute an OSD class method on an object.
4023 *
4024 * @flags: CEPH_OSD_FLAG_*
4025 * @resp_len: out param for reply length
4026 */
4027int ceph_osdc_call(struct ceph_osd_client *osdc,
4028                   struct ceph_object_id *oid,
4029                   struct ceph_object_locator *oloc,
4030                   const char *class, const char *method,
4031                   unsigned int flags,
4032                   struct page *req_page, size_t req_len,
4033                   struct page *resp_page, size_t *resp_len)
4034{
4035        struct ceph_osd_request *req;
4036        int ret;
4037
4038        req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4039        if (!req)
4040                return -ENOMEM;
4041
4042        ceph_oid_copy(&req->r_base_oid, oid);
4043        ceph_oloc_copy(&req->r_base_oloc, oloc);
4044        req->r_flags = flags;
4045
4046        ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4047        if (ret)
4048                goto out_put_req;
4049
4050        osd_req_op_cls_init(req, 0, CEPH_OSD_OP_CALL, class, method);
4051        if (req_page)
4052                osd_req_op_cls_request_data_pages(req, 0, &req_page, req_len,
4053                                                  0, false, false);
4054        if (resp_page)
4055                osd_req_op_cls_response_data_pages(req, 0, &resp_page,
4056                                                   PAGE_SIZE, 0, false, false);
4057
4058        ceph_osdc_start_request(osdc, req, false);
4059        ret = ceph_osdc_wait_request(osdc, req);
4060        if (ret >= 0) {
4061                ret = req->r_ops[0].rval;
4062                if (resp_page)
4063                        *resp_len = req->r_ops[0].outdata_len;
4064        }
4065
4066out_put_req:
4067        ceph_osdc_put_request(req);
4068        return ret;
4069}
4070EXPORT_SYMBOL(ceph_osdc_call);
4071
4072/*
4073 * init, shutdown
4074 */
4075int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
4076{
4077        int err;
4078
4079        dout("init\n");
4080        osdc->client = client;
4081        init_rwsem(&osdc->lock);
4082        osdc->osds = RB_ROOT;
4083        INIT_LIST_HEAD(&osdc->osd_lru);
4084        spin_lock_init(&osdc->osd_lru_lock);
4085        osd_init(&osdc->homeless_osd);
4086        osdc->homeless_osd.o_osdc = osdc;
4087        osdc->homeless_osd.o_osd = CEPH_HOMELESS_OSD;
4088        osdc->last_linger_id = CEPH_LINGER_ID_START;
4089        osdc->linger_requests = RB_ROOT;
4090        osdc->map_checks = RB_ROOT;
4091        osdc->linger_map_checks = RB_ROOT;
4092        INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
4093        INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
4094
4095        err = -ENOMEM;
4096        osdc->osdmap = ceph_osdmap_alloc();
4097        if (!osdc->osdmap)
4098                goto out;
4099
4100        osdc->req_mempool = mempool_create_slab_pool(10,
4101                                                     ceph_osd_request_cache);
4102        if (!osdc->req_mempool)
4103                goto out_map;
4104
4105        err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
4106                                PAGE_SIZE, 10, true, "osd_op");
4107        if (err < 0)
4108                goto out_mempool;
4109        err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
4110                                PAGE_SIZE, 10, true, "osd_op_reply");
4111        if (err < 0)
4112                goto out_msgpool;
4113
4114        err = -ENOMEM;
4115        osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
4116        if (!osdc->notify_wq)
4117                goto out_msgpool_reply;
4118
4119        schedule_delayed_work(&osdc->timeout_work,
4120                              osdc->client->options->osd_keepalive_timeout);
4121        schedule_delayed_work(&osdc->osds_timeout_work,
4122            round_jiffies_relative(osdc->client->options->osd_idle_ttl));
4123
4124        return 0;
4125
4126out_msgpool_reply:
4127        ceph_msgpool_destroy(&osdc->msgpool_op_reply);
4128out_msgpool:
4129        ceph_msgpool_destroy(&osdc->msgpool_op);
4130out_mempool:
4131        mempool_destroy(osdc->req_mempool);
4132out_map:
4133        ceph_osdmap_destroy(osdc->osdmap);
4134out:
4135        return err;
4136}
4137
4138void ceph_osdc_stop(struct ceph_osd_client *osdc)
4139{
4140        flush_workqueue(osdc->notify_wq);
4141        destroy_workqueue(osdc->notify_wq);
4142        cancel_delayed_work_sync(&osdc->timeout_work);
4143        cancel_delayed_work_sync(&osdc->osds_timeout_work);
4144
4145        down_write(&osdc->lock);
4146        while (!RB_EMPTY_ROOT(&osdc->osds)) {
4147                struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
4148                                                struct ceph_osd, o_node);
4149                close_osd(osd);
4150        }
4151        up_write(&osdc->lock);
4152        WARN_ON(atomic_read(&osdc->homeless_osd.o_ref) != 1);
4153        osd_cleanup(&osdc->homeless_osd);
4154
4155        WARN_ON(!list_empty(&osdc->osd_lru));
4156        WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_requests));
4157        WARN_ON(!RB_EMPTY_ROOT(&osdc->map_checks));
4158        WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_map_checks));
4159        WARN_ON(atomic_read(&osdc->num_requests));
4160        WARN_ON(atomic_read(&osdc->num_homeless));
4161
4162        ceph_osdmap_destroy(osdc->osdmap);
4163        mempool_destroy(osdc->req_mempool);
4164        ceph_msgpool_destroy(&osdc->msgpool_op);
4165        ceph_msgpool_destroy(&osdc->msgpool_op_reply);
4166}
4167
4168/*
4169 * Read some contiguous pages.  If we cross a stripe boundary, shorten
4170 * *plen.  Return number of bytes read, or error.
4171 */
4172int ceph_osdc_readpages(struct ceph_osd_client *osdc,
4173                        struct ceph_vino vino, struct ceph_file_layout *layout,
4174                        u64 off, u64 *plen,
4175                        u32 truncate_seq, u64 truncate_size,
4176                        struct page **pages, int num_pages, int page_align)
4177{
4178        struct ceph_osd_request *req;
4179        int rc = 0;
4180
4181        dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
4182             vino.snap, off, *plen);
4183        req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
4184                                    CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
4185                                    NULL, truncate_seq, truncate_size,
4186                                    false);
4187        if (IS_ERR(req))
4188                return PTR_ERR(req);
4189
4190        /* it may be a short read due to an object boundary */
4191        osd_req_op_extent_osd_data_pages(req, 0,
4192                                pages, *plen, page_align, false, false);
4193
4194        dout("readpages  final extent is %llu~%llu (%llu bytes align %d)\n",
4195             off, *plen, *plen, page_align);
4196
4197        rc = ceph_osdc_start_request(osdc, req, false);
4198        if (!rc)
4199                rc = ceph_osdc_wait_request(osdc, req);
4200
4201        ceph_osdc_put_request(req);
4202        dout("readpages result %d\n", rc);
4203        return rc;
4204}
4205EXPORT_SYMBOL(ceph_osdc_readpages);
4206
4207/*
4208 * do a synchronous write on N pages
4209 */
4210int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
4211                         struct ceph_file_layout *layout,
4212                         struct ceph_snap_context *snapc,
4213                         u64 off, u64 len,
4214                         u32 truncate_seq, u64 truncate_size,
4215                         struct timespec *mtime,
4216                         struct page **pages, int num_pages)
4217{
4218        struct ceph_osd_request *req;
4219        int rc = 0;
4220        int page_align = off & ~PAGE_MASK;
4221
4222        req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
4223                                    CEPH_OSD_OP_WRITE,
4224                                    CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
4225                                    snapc, truncate_seq, truncate_size,
4226                                    true);
4227        if (IS_ERR(req))
4228                return PTR_ERR(req);
4229
4230        /* it may be a short write due to an object boundary */
4231        osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
4232                                false, false);
4233        dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
4234
4235        req->r_mtime = *mtime;
4236        rc = ceph_osdc_start_request(osdc, req, true);
4237        if (!rc)
4238                rc = ceph_osdc_wait_request(osdc, req);
4239
4240        ceph_osdc_put_request(req);
4241        if (rc == 0)
4242                rc = len;
4243        dout("writepages result %d\n", rc);
4244        return rc;
4245}
4246EXPORT_SYMBOL(ceph_osdc_writepages);
4247
4248int ceph_osdc_setup(void)
4249{
4250        size_t size = sizeof(struct ceph_osd_request) +
4251            CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
4252
4253        BUG_ON(ceph_osd_request_cache);
4254        ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
4255                                                   0, 0, NULL);
4256
4257        return ceph_osd_request_cache ? 0 : -ENOMEM;
4258}
4259EXPORT_SYMBOL(ceph_osdc_setup);
4260
4261void ceph_osdc_cleanup(void)
4262{
4263        BUG_ON(!ceph_osd_request_cache);
4264        kmem_cache_destroy(ceph_osd_request_cache);
4265        ceph_osd_request_cache = NULL;
4266}
4267EXPORT_SYMBOL(ceph_osdc_cleanup);
4268
4269/*
4270 * handle incoming message
4271 */
4272static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
4273{
4274        struct ceph_osd *osd = con->private;
4275        struct ceph_osd_client *osdc = osd->o_osdc;
4276        int type = le16_to_cpu(msg->hdr.type);
4277
4278        switch (type) {
4279        case CEPH_MSG_OSD_MAP:
4280                ceph_osdc_handle_map(osdc, msg);
4281                break;
4282        case CEPH_MSG_OSD_OPREPLY:
4283                handle_reply(osd, msg);
4284                break;
4285        case CEPH_MSG_WATCH_NOTIFY:
4286                handle_watch_notify(osdc, msg);
4287                break;
4288
4289        default:
4290                pr_err("received unknown message type %d %s\n", type,
4291                       ceph_msg_type_name(type));
4292        }
4293
4294        ceph_msg_put(msg);
4295}
4296
4297/*
4298 * Lookup and return message for incoming reply.  Don't try to do
4299 * anything about a larger than preallocated data portion of the
4300 * message at the moment - for now, just skip the message.
4301 */
4302static struct ceph_msg *get_reply(struct ceph_connection *con,
4303                                  struct ceph_msg_header *hdr,
4304                                  int *skip)
4305{
4306        struct ceph_osd *osd = con->private;
4307        struct ceph_osd_client *osdc = osd->o_osdc;
4308        struct ceph_msg *m = NULL;
4309        struct ceph_osd_request *req;
4310        int front_len = le32_to_cpu(hdr->front_len);
4311        int data_len = le32_to_cpu(hdr->data_len);
4312        u64 tid = le64_to_cpu(hdr->tid);
4313
4314        down_read(&osdc->lock);
4315        if (!osd_registered(osd)) {
4316                dout("%s osd%d unknown, skipping\n", __func__, osd->o_osd);
4317                *skip = 1;
4318                goto out_unlock_osdc;
4319        }
4320        WARN_ON(osd->o_osd != le64_to_cpu(hdr->src.num));
4321
4322        mutex_lock(&osd->lock);
4323        req = lookup_request(&osd->o_requests, tid);
4324        if (!req) {
4325                dout("%s osd%d tid %llu unknown, skipping\n", __func__,
4326                     osd->o_osd, tid);
4327                *skip = 1;
4328                goto out_unlock_session;
4329        }
4330
4331        ceph_msg_revoke_incoming(req->r_reply);
4332
4333        if (front_len > req->r_reply->front_alloc_len) {
4334                pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
4335                        __func__, osd->o_osd, req->r_tid, front_len,
4336                        req->r_reply->front_alloc_len);
4337                m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
4338                                 false);
4339                if (!m)
4340                        goto out_unlock_session;
4341                ceph_msg_put(req->r_reply);
4342                req->r_reply = m;
4343        }
4344
4345        if (data_len > req->r_reply->data_length) {
4346                pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
4347                        __func__, osd->o_osd, req->r_tid, data_len,
4348                        req->r_reply->data_length);
4349                m = NULL;
4350                *skip = 1;
4351                goto out_unlock_session;
4352        }
4353
4354        m = ceph_msg_get(req->r_reply);
4355        dout("get_reply tid %lld %p\n", tid, m);
4356
4357out_unlock_session:
4358        mutex_unlock(&osd->lock);
4359out_unlock_osdc:
4360        up_read(&osdc->lock);
4361        return m;
4362}
4363
4364/*
4365 * TODO: switch to a msg-owned pagelist
4366 */
4367static struct ceph_msg *alloc_msg_with_page_vector(struct ceph_msg_header *hdr)
4368{
4369        struct ceph_msg *m;
4370        int type = le16_to_cpu(hdr->type);
4371        u32 front_len = le32_to_cpu(hdr->front_len);
4372        u32 data_len = le32_to_cpu(hdr->data_len);
4373
4374        m = ceph_msg_new(type, front_len, GFP_NOIO, false);
4375        if (!m)
4376                return NULL;
4377
4378        if (data_len) {
4379                struct page **pages;
4380                struct ceph_osd_data osd_data;
4381
4382                pages = ceph_alloc_page_vector(calc_pages_for(0, data_len),
4383                                               GFP_NOIO);
4384                if (IS_ERR(pages)) {
4385                        ceph_msg_put(m);
4386                        return NULL;
4387                }
4388
4389                ceph_osd_data_pages_init(&osd_data, pages, data_len, 0, false,
4390                                         false);
4391                ceph_osdc_msg_data_add(m, &osd_data);
4392        }
4393
4394        return m;
4395}
4396
4397static struct ceph_msg *alloc_msg(struct ceph_connection *con,
4398                                  struct ceph_msg_header *hdr,
4399                                  int *skip)
4400{
4401        struct ceph_osd *osd = con->private;
4402        int type = le16_to_cpu(hdr->type);
4403
4404        *skip = 0;
4405        switch (type) {
4406        case CEPH_MSG_OSD_MAP:
4407        case CEPH_MSG_WATCH_NOTIFY:
4408                return alloc_msg_with_page_vector(hdr);
4409        case CEPH_MSG_OSD_OPREPLY:
4410                return get_reply(con, hdr, skip);
4411        default:
4412                pr_warn("%s osd%d unknown msg type %d, skipping\n", __func__,
4413                        osd->o_osd, type);
4414                *skip = 1;
4415                return NULL;
4416        }
4417}
4418
4419/*
4420 * Wrappers to refcount containing ceph_osd struct
4421 */
4422static struct ceph_connection *get_osd_con(struct ceph_connection *con)
4423{
4424        struct ceph_osd *osd = con->private;
4425        if (get_osd(osd))
4426                return con;
4427        return NULL;
4428}
4429
4430static void put_osd_con(struct ceph_connection *con)
4431{
4432        struct ceph_osd *osd = con->private;
4433        put_osd(osd);
4434}
4435
4436/*
4437 * authentication
4438 */
4439/*
4440 * Note: returned pointer is the address of a structure that's
4441 * managed separately.  Caller must *not* attempt to free it.
4442 */
4443static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
4444                                        int *proto, int force_new)
4445{
4446        struct ceph_osd *o = con->private;
4447        struct ceph_osd_client *osdc = o->o_osdc;
4448        struct ceph_auth_client *ac = osdc->client->monc.auth;
4449        struct ceph_auth_handshake *auth = &o->o_auth;
4450
4451        if (force_new && auth->authorizer) {
4452                ceph_auth_destroy_authorizer(auth->authorizer);
4453                auth->authorizer = NULL;
4454        }
4455        if (!auth->authorizer) {
4456                int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
4457                                                      auth);
4458                if (ret)
4459                        return ERR_PTR(ret);
4460        } else {
4461                int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
4462                                                     auth);
4463                if (ret)
4464                        return ERR_PTR(ret);
4465        }
4466        *proto = ac->protocol;
4467
4468        return auth;
4469}
4470
4471
4472static int verify_authorizer_reply(struct ceph_connection *con)
4473{
4474        struct ceph_osd *o = con->private;
4475        struct ceph_osd_client *osdc = o->o_osdc;
4476        struct ceph_auth_client *ac = osdc->client->monc.auth;
4477
4478        return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer);
4479}
4480
4481static int invalidate_authorizer(struct ceph_connection *con)
4482{
4483        struct ceph_osd *o = con->private;
4484        struct ceph_osd_client *osdc = o->o_osdc;
4485        struct ceph_auth_client *ac = osdc->client->monc.auth;
4486
4487        ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
4488        return ceph_monc_validate_auth(&osdc->client->monc);
4489}
4490
4491static int osd_sign_message(struct ceph_msg *msg)
4492{
4493        struct ceph_osd *o = msg->con->private;
4494        struct ceph_auth_handshake *auth = &o->o_auth;
4495
4496        return ceph_auth_sign_message(auth, msg);
4497}
4498
4499static int osd_check_message_signature(struct ceph_msg *msg)
4500{
4501        struct ceph_osd *o = msg->con->private;
4502        struct ceph_auth_handshake *auth = &o->o_auth;
4503
4504        return ceph_auth_check_message_signature(auth, msg);
4505}
4506
4507static const struct ceph_connection_operations osd_con_ops = {
4508        .get = get_osd_con,
4509        .put = put_osd_con,
4510        .dispatch = dispatch,
4511        .get_authorizer = get_authorizer,
4512        .verify_authorizer_reply = verify_authorizer_reply,
4513        .invalidate_authorizer = invalidate_authorizer,
4514        .alloc_msg = alloc_msg,
4515        .sign_message = osd_sign_message,
4516        .check_message_signature = osd_check_message_signature,
4517        .fault = osd_fault,
4518};
4519