1a4ce40a9SAlex Elder 23d14c5d2SYehuda Sadeh #include <linux/ceph/ceph_debug.h> 33d14c5d2SYehuda Sadeh 43d14c5d2SYehuda Sadeh #include <linux/module.h> 53d14c5d2SYehuda Sadeh #include <linux/err.h> 63d14c5d2SYehuda Sadeh #include <linux/highmem.h> 73d14c5d2SYehuda Sadeh #include <linux/mm.h> 83d14c5d2SYehuda Sadeh #include <linux/pagemap.h> 93d14c5d2SYehuda Sadeh #include <linux/slab.h> 103d14c5d2SYehuda Sadeh #include <linux/uaccess.h> 113d14c5d2SYehuda Sadeh #ifdef CONFIG_BLOCK 123d14c5d2SYehuda Sadeh #include <linux/bio.h> 133d14c5d2SYehuda Sadeh #endif 143d14c5d2SYehuda Sadeh 158cb441c0SIlya Dryomov #include <linux/ceph/ceph_features.h> 163d14c5d2SYehuda Sadeh #include <linux/ceph/libceph.h> 173d14c5d2SYehuda Sadeh #include <linux/ceph/osd_client.h> 183d14c5d2SYehuda Sadeh #include <linux/ceph/messenger.h> 193d14c5d2SYehuda Sadeh #include <linux/ceph/decode.h> 203d14c5d2SYehuda Sadeh #include <linux/ceph/auth.h> 213d14c5d2SYehuda Sadeh #include <linux/ceph/pagelist.h> 223d14c5d2SYehuda Sadeh 233d14c5d2SYehuda Sadeh #define OSD_OPREPLY_FRONT_LEN 512 243d14c5d2SYehuda Sadeh 255522ae0bSAlex Elder static struct kmem_cache *ceph_osd_request_cache; 265522ae0bSAlex Elder 273d14c5d2SYehuda Sadeh static const struct ceph_connection_operations osd_con_ops; 283d14c5d2SYehuda Sadeh 293d14c5d2SYehuda Sadeh /* 303d14c5d2SYehuda Sadeh * Implement client access to distributed object storage cluster. 313d14c5d2SYehuda Sadeh * 323d14c5d2SYehuda Sadeh * All data objects are stored within a cluster/cloud of OSDs, or 333d14c5d2SYehuda Sadeh * "object storage devices." (Note that Ceph OSDs have _nothing_ to 343d14c5d2SYehuda Sadeh * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply 353d14c5d2SYehuda Sadeh * remote daemons serving up and coordinating consistent and safe 363d14c5d2SYehuda Sadeh * access to storage. 373d14c5d2SYehuda Sadeh * 383d14c5d2SYehuda Sadeh * Cluster membership and the mapping of data objects onto storage devices 393d14c5d2SYehuda Sadeh * are described by the osd map. 403d14c5d2SYehuda Sadeh * 413d14c5d2SYehuda Sadeh * We keep track of pending OSD requests (read, write), resubmit 423d14c5d2SYehuda Sadeh * requests to different OSDs when the cluster topology/data layout 433d14c5d2SYehuda Sadeh * change, or retry the affected requests when the communications 443d14c5d2SYehuda Sadeh * channel with an OSD is reset. 453d14c5d2SYehuda Sadeh */ 463d14c5d2SYehuda Sadeh 475aea3dcdSIlya Dryomov static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req); 485aea3dcdSIlya Dryomov static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req); 49922dab61SIlya Dryomov static void link_linger(struct ceph_osd *osd, 50922dab61SIlya Dryomov struct ceph_osd_linger_request *lreq); 51922dab61SIlya Dryomov static void unlink_linger(struct ceph_osd *osd, 52922dab61SIlya Dryomov struct ceph_osd_linger_request *lreq); 53a02a946dSIlya Dryomov static void clear_backoffs(struct ceph_osd *osd); 545aea3dcdSIlya Dryomov 555aea3dcdSIlya Dryomov #if 1 565aea3dcdSIlya Dryomov static inline bool rwsem_is_wrlocked(struct rw_semaphore *sem) 575aea3dcdSIlya Dryomov { 585aea3dcdSIlya Dryomov bool wrlocked = true; 595aea3dcdSIlya Dryomov 605aea3dcdSIlya Dryomov if (unlikely(down_read_trylock(sem))) { 615aea3dcdSIlya Dryomov wrlocked = false; 625aea3dcdSIlya Dryomov up_read(sem); 635aea3dcdSIlya Dryomov } 645aea3dcdSIlya Dryomov 655aea3dcdSIlya Dryomov return wrlocked; 665aea3dcdSIlya Dryomov } 675aea3dcdSIlya Dryomov static inline void verify_osdc_locked(struct ceph_osd_client *osdc) 685aea3dcdSIlya Dryomov { 695aea3dcdSIlya Dryomov WARN_ON(!rwsem_is_locked(&osdc->lock)); 705aea3dcdSIlya Dryomov } 715aea3dcdSIlya Dryomov static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc) 725aea3dcdSIlya Dryomov { 735aea3dcdSIlya Dryomov WARN_ON(!rwsem_is_wrlocked(&osdc->lock)); 745aea3dcdSIlya Dryomov } 755aea3dcdSIlya Dryomov static inline void verify_osd_locked(struct ceph_osd *osd) 765aea3dcdSIlya Dryomov { 775aea3dcdSIlya Dryomov struct ceph_osd_client *osdc = osd->o_osdc; 785aea3dcdSIlya Dryomov 795aea3dcdSIlya Dryomov WARN_ON(!(mutex_is_locked(&osd->lock) && 805aea3dcdSIlya Dryomov rwsem_is_locked(&osdc->lock)) && 815aea3dcdSIlya Dryomov !rwsem_is_wrlocked(&osdc->lock)); 825aea3dcdSIlya Dryomov } 83922dab61SIlya Dryomov static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq) 84922dab61SIlya Dryomov { 85922dab61SIlya Dryomov WARN_ON(!mutex_is_locked(&lreq->lock)); 86922dab61SIlya Dryomov } 875aea3dcdSIlya Dryomov #else 885aea3dcdSIlya Dryomov static inline void verify_osdc_locked(struct ceph_osd_client *osdc) { } 895aea3dcdSIlya Dryomov static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc) { } 905aea3dcdSIlya Dryomov static inline void verify_osd_locked(struct ceph_osd *osd) { } 91922dab61SIlya Dryomov static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq) { } 925aea3dcdSIlya Dryomov #endif 935aea3dcdSIlya Dryomov 943d14c5d2SYehuda Sadeh /* 953d14c5d2SYehuda Sadeh * calculate the mapping of a file extent onto an object, and fill out the 963d14c5d2SYehuda Sadeh * request accordingly. shorten extent as necessary if it crosses an 973d14c5d2SYehuda Sadeh * object boundary. 983d14c5d2SYehuda Sadeh * 993d14c5d2SYehuda Sadeh * fill osd op in request message. 1003d14c5d2SYehuda Sadeh */ 101dbe0fc41SAlex Elder static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen, 102a19dadfbSAlex Elder u64 *objnum, u64 *objoff, u64 *objlen) 1033d14c5d2SYehuda Sadeh { 10460e56f13SAlex Elder u64 orig_len = *plen; 105d63b77f4SSage Weil int r; 1063d14c5d2SYehuda Sadeh 10760e56f13SAlex Elder /* object extent? */ 10875d1c941SAlex Elder r = ceph_calc_file_object_mapping(layout, off, orig_len, objnum, 10975d1c941SAlex Elder objoff, objlen); 110d63b77f4SSage Weil if (r < 0) 111d63b77f4SSage Weil return r; 11275d1c941SAlex Elder if (*objlen < orig_len) { 11375d1c941SAlex Elder *plen = *objlen; 11460e56f13SAlex Elder dout(" skipping last %llu, final file extent %llu~%llu\n", 11560e56f13SAlex Elder orig_len - *plen, off, *plen); 11660e56f13SAlex Elder } 11760e56f13SAlex Elder 11875d1c941SAlex Elder dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen); 1193d14c5d2SYehuda Sadeh 1203ff5f385SAlex Elder return 0; 1213d14c5d2SYehuda Sadeh } 1223d14c5d2SYehuda Sadeh 123c54d47bfSAlex Elder static void ceph_osd_data_init(struct ceph_osd_data *osd_data) 124c54d47bfSAlex Elder { 125c54d47bfSAlex Elder memset(osd_data, 0, sizeof (*osd_data)); 126c54d47bfSAlex Elder osd_data->type = CEPH_OSD_DATA_TYPE_NONE; 127c54d47bfSAlex Elder } 128c54d47bfSAlex Elder 129a4ce40a9SAlex Elder static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data, 13043bfe5deSAlex Elder struct page **pages, u64 length, u32 alignment, 13143bfe5deSAlex Elder bool pages_from_pool, bool own_pages) 13243bfe5deSAlex Elder { 13343bfe5deSAlex Elder osd_data->type = CEPH_OSD_DATA_TYPE_PAGES; 13443bfe5deSAlex Elder osd_data->pages = pages; 13543bfe5deSAlex Elder osd_data->length = length; 13643bfe5deSAlex Elder osd_data->alignment = alignment; 13743bfe5deSAlex Elder osd_data->pages_from_pool = pages_from_pool; 13843bfe5deSAlex Elder osd_data->own_pages = own_pages; 13943bfe5deSAlex Elder } 14043bfe5deSAlex Elder 141a4ce40a9SAlex Elder static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data, 14243bfe5deSAlex Elder struct ceph_pagelist *pagelist) 14343bfe5deSAlex Elder { 14443bfe5deSAlex Elder osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST; 14543bfe5deSAlex Elder osd_data->pagelist = pagelist; 14643bfe5deSAlex Elder } 14743bfe5deSAlex Elder 14843bfe5deSAlex Elder #ifdef CONFIG_BLOCK 149a4ce40a9SAlex Elder static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data, 15043bfe5deSAlex Elder struct bio *bio, size_t bio_length) 15143bfe5deSAlex Elder { 15243bfe5deSAlex Elder osd_data->type = CEPH_OSD_DATA_TYPE_BIO; 15343bfe5deSAlex Elder osd_data->bio = bio; 15443bfe5deSAlex Elder osd_data->bio_length = bio_length; 15543bfe5deSAlex Elder } 15643bfe5deSAlex Elder #endif /* CONFIG_BLOCK */ 15743bfe5deSAlex Elder 158863c7eb5SAlex Elder #define osd_req_op_data(oreq, whch, typ, fld) \ 159863c7eb5SAlex Elder ({ \ 1608a703a38SIoana Ciornei struct ceph_osd_request *__oreq = (oreq); \ 1618a703a38SIoana Ciornei unsigned int __whch = (whch); \ 1628a703a38SIoana Ciornei BUG_ON(__whch >= __oreq->r_num_ops); \ 1638a703a38SIoana Ciornei &__oreq->r_ops[__whch].typ.fld; \ 164863c7eb5SAlex Elder }) 165863c7eb5SAlex Elder 16649719778SAlex Elder static struct ceph_osd_data * 16749719778SAlex Elder osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which) 16849719778SAlex Elder { 16949719778SAlex Elder BUG_ON(which >= osd_req->r_num_ops); 17049719778SAlex Elder 17149719778SAlex Elder return &osd_req->r_ops[which].raw_data_in; 17249719778SAlex Elder } 17349719778SAlex Elder 174a4ce40a9SAlex Elder struct ceph_osd_data * 175a4ce40a9SAlex Elder osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req, 176406e2c9fSAlex Elder unsigned int which) 177a4ce40a9SAlex Elder { 178863c7eb5SAlex Elder return osd_req_op_data(osd_req, which, extent, osd_data); 179a4ce40a9SAlex Elder } 180a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_osd_data); 181a4ce40a9SAlex Elder 18249719778SAlex Elder void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req, 18349719778SAlex Elder unsigned int which, struct page **pages, 18449719778SAlex Elder u64 length, u32 alignment, 18549719778SAlex Elder bool pages_from_pool, bool own_pages) 18649719778SAlex Elder { 18749719778SAlex Elder struct ceph_osd_data *osd_data; 18849719778SAlex Elder 18949719778SAlex Elder osd_data = osd_req_op_raw_data_in(osd_req, which); 19049719778SAlex Elder ceph_osd_data_pages_init(osd_data, pages, length, alignment, 19149719778SAlex Elder pages_from_pool, own_pages); 19249719778SAlex Elder } 19349719778SAlex Elder EXPORT_SYMBOL(osd_req_op_raw_data_in_pages); 19449719778SAlex Elder 195a4ce40a9SAlex Elder void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req, 196406e2c9fSAlex Elder unsigned int which, struct page **pages, 197406e2c9fSAlex Elder u64 length, u32 alignment, 198a4ce40a9SAlex Elder bool pages_from_pool, bool own_pages) 199a4ce40a9SAlex Elder { 200a4ce40a9SAlex Elder struct ceph_osd_data *osd_data; 201a4ce40a9SAlex Elder 202863c7eb5SAlex Elder osd_data = osd_req_op_data(osd_req, which, extent, osd_data); 203a4ce40a9SAlex Elder ceph_osd_data_pages_init(osd_data, pages, length, alignment, 204a4ce40a9SAlex Elder pages_from_pool, own_pages); 205a4ce40a9SAlex Elder } 206a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages); 207a4ce40a9SAlex Elder 208a4ce40a9SAlex Elder void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req, 209406e2c9fSAlex Elder unsigned int which, struct ceph_pagelist *pagelist) 210a4ce40a9SAlex Elder { 211a4ce40a9SAlex Elder struct ceph_osd_data *osd_data; 212a4ce40a9SAlex Elder 213863c7eb5SAlex Elder osd_data = osd_req_op_data(osd_req, which, extent, osd_data); 214a4ce40a9SAlex Elder ceph_osd_data_pagelist_init(osd_data, pagelist); 215a4ce40a9SAlex Elder } 216a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist); 217a4ce40a9SAlex Elder 218a4ce40a9SAlex Elder #ifdef CONFIG_BLOCK 219a4ce40a9SAlex Elder void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req, 220406e2c9fSAlex Elder unsigned int which, struct bio *bio, size_t bio_length) 221a4ce40a9SAlex Elder { 222a4ce40a9SAlex Elder struct ceph_osd_data *osd_data; 223863c7eb5SAlex Elder 224863c7eb5SAlex Elder osd_data = osd_req_op_data(osd_req, which, extent, osd_data); 225a4ce40a9SAlex Elder ceph_osd_data_bio_init(osd_data, bio, bio_length); 226a4ce40a9SAlex Elder } 227a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio); 228a4ce40a9SAlex Elder #endif /* CONFIG_BLOCK */ 229a4ce40a9SAlex Elder 230a4ce40a9SAlex Elder static void osd_req_op_cls_request_info_pagelist( 231a4ce40a9SAlex Elder struct ceph_osd_request *osd_req, 232a4ce40a9SAlex Elder unsigned int which, struct ceph_pagelist *pagelist) 233a4ce40a9SAlex Elder { 234a4ce40a9SAlex Elder struct ceph_osd_data *osd_data; 235a4ce40a9SAlex Elder 236863c7eb5SAlex Elder osd_data = osd_req_op_data(osd_req, which, cls, request_info); 237a4ce40a9SAlex Elder ceph_osd_data_pagelist_init(osd_data, pagelist); 238a4ce40a9SAlex Elder } 239a4ce40a9SAlex Elder 24004017e29SAlex Elder void osd_req_op_cls_request_data_pagelist( 24104017e29SAlex Elder struct ceph_osd_request *osd_req, 24204017e29SAlex Elder unsigned int which, struct ceph_pagelist *pagelist) 24304017e29SAlex Elder { 24404017e29SAlex Elder struct ceph_osd_data *osd_data; 24504017e29SAlex Elder 246863c7eb5SAlex Elder osd_data = osd_req_op_data(osd_req, which, cls, request_data); 24704017e29SAlex Elder ceph_osd_data_pagelist_init(osd_data, pagelist); 248bb873b53SIlya Dryomov osd_req->r_ops[which].cls.indata_len += pagelist->length; 249bb873b53SIlya Dryomov osd_req->r_ops[which].indata_len += pagelist->length; 25004017e29SAlex Elder } 25104017e29SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist); 25204017e29SAlex Elder 2536c57b554SAlex Elder void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req, 2546c57b554SAlex Elder unsigned int which, struct page **pages, u64 length, 2556c57b554SAlex Elder u32 alignment, bool pages_from_pool, bool own_pages) 2566c57b554SAlex Elder { 2576c57b554SAlex Elder struct ceph_osd_data *osd_data; 2586c57b554SAlex Elder 2596c57b554SAlex Elder osd_data = osd_req_op_data(osd_req, which, cls, request_data); 2606c57b554SAlex Elder ceph_osd_data_pages_init(osd_data, pages, length, alignment, 2616c57b554SAlex Elder pages_from_pool, own_pages); 262bb873b53SIlya Dryomov osd_req->r_ops[which].cls.indata_len += length; 263bb873b53SIlya Dryomov osd_req->r_ops[which].indata_len += length; 2646c57b554SAlex Elder } 2656c57b554SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_request_data_pages); 2666c57b554SAlex Elder 267a4ce40a9SAlex Elder void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req, 268a4ce40a9SAlex Elder unsigned int which, struct page **pages, u64 length, 269a4ce40a9SAlex Elder u32 alignment, bool pages_from_pool, bool own_pages) 270a4ce40a9SAlex Elder { 271a4ce40a9SAlex Elder struct ceph_osd_data *osd_data; 272a4ce40a9SAlex Elder 273863c7eb5SAlex Elder osd_data = osd_req_op_data(osd_req, which, cls, response_data); 274a4ce40a9SAlex Elder ceph_osd_data_pages_init(osd_data, pages, length, alignment, 275a4ce40a9SAlex Elder pages_from_pool, own_pages); 276a4ce40a9SAlex Elder } 277a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_response_data_pages); 278a4ce40a9SAlex Elder 27923c08a9cSAlex Elder static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data) 28023c08a9cSAlex Elder { 28123c08a9cSAlex Elder switch (osd_data->type) { 28223c08a9cSAlex Elder case CEPH_OSD_DATA_TYPE_NONE: 28323c08a9cSAlex Elder return 0; 28423c08a9cSAlex Elder case CEPH_OSD_DATA_TYPE_PAGES: 28523c08a9cSAlex Elder return osd_data->length; 28623c08a9cSAlex Elder case CEPH_OSD_DATA_TYPE_PAGELIST: 28723c08a9cSAlex Elder return (u64)osd_data->pagelist->length; 28823c08a9cSAlex Elder #ifdef CONFIG_BLOCK 28923c08a9cSAlex Elder case CEPH_OSD_DATA_TYPE_BIO: 29023c08a9cSAlex Elder return (u64)osd_data->bio_length; 29123c08a9cSAlex Elder #endif /* CONFIG_BLOCK */ 29223c08a9cSAlex Elder default: 29323c08a9cSAlex Elder WARN(true, "unrecognized data type %d\n", (int)osd_data->type); 29423c08a9cSAlex Elder return 0; 29523c08a9cSAlex Elder } 29623c08a9cSAlex Elder } 29723c08a9cSAlex Elder 298c54d47bfSAlex Elder static void ceph_osd_data_release(struct ceph_osd_data *osd_data) 299c54d47bfSAlex Elder { 3005476492fSAlex Elder if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) { 301c54d47bfSAlex Elder int num_pages; 302c54d47bfSAlex Elder 303c54d47bfSAlex Elder num_pages = calc_pages_for((u64)osd_data->alignment, 304c54d47bfSAlex Elder (u64)osd_data->length); 305c54d47bfSAlex Elder ceph_release_page_vector(osd_data->pages, num_pages); 306c54d47bfSAlex Elder } 3075476492fSAlex Elder ceph_osd_data_init(osd_data); 3085476492fSAlex Elder } 3095476492fSAlex Elder 3105476492fSAlex Elder static void osd_req_op_data_release(struct ceph_osd_request *osd_req, 3115476492fSAlex Elder unsigned int which) 3125476492fSAlex Elder { 3135476492fSAlex Elder struct ceph_osd_req_op *op; 3145476492fSAlex Elder 3155476492fSAlex Elder BUG_ON(which >= osd_req->r_num_ops); 3165476492fSAlex Elder op = &osd_req->r_ops[which]; 3175476492fSAlex Elder 3185476492fSAlex Elder switch (op->op) { 3195476492fSAlex Elder case CEPH_OSD_OP_READ: 3205476492fSAlex Elder case CEPH_OSD_OP_WRITE: 321e30b7577SIlya Dryomov case CEPH_OSD_OP_WRITEFULL: 3225476492fSAlex Elder ceph_osd_data_release(&op->extent.osd_data); 3235476492fSAlex Elder break; 3245476492fSAlex Elder case CEPH_OSD_OP_CALL: 3255476492fSAlex Elder ceph_osd_data_release(&op->cls.request_info); 32604017e29SAlex Elder ceph_osd_data_release(&op->cls.request_data); 3275476492fSAlex Elder ceph_osd_data_release(&op->cls.response_data); 3285476492fSAlex Elder break; 329d74b50beSYan, Zheng case CEPH_OSD_OP_SETXATTR: 330d74b50beSYan, Zheng case CEPH_OSD_OP_CMPXATTR: 331d74b50beSYan, Zheng ceph_osd_data_release(&op->xattr.osd_data); 332d74b50beSYan, Zheng break; 33366ba609fSYan, Zheng case CEPH_OSD_OP_STAT: 33466ba609fSYan, Zheng ceph_osd_data_release(&op->raw_data_in); 33566ba609fSYan, Zheng break; 336922dab61SIlya Dryomov case CEPH_OSD_OP_NOTIFY_ACK: 337922dab61SIlya Dryomov ceph_osd_data_release(&op->notify_ack.request_data); 338922dab61SIlya Dryomov break; 33919079203SIlya Dryomov case CEPH_OSD_OP_NOTIFY: 34019079203SIlya Dryomov ceph_osd_data_release(&op->notify.request_data); 34119079203SIlya Dryomov ceph_osd_data_release(&op->notify.response_data); 34219079203SIlya Dryomov break; 343a4ed38d7SDouglas Fuller case CEPH_OSD_OP_LIST_WATCHERS: 344a4ed38d7SDouglas Fuller ceph_osd_data_release(&op->list_watchers.response_data); 345a4ed38d7SDouglas Fuller break; 3465476492fSAlex Elder default: 3475476492fSAlex Elder break; 3485476492fSAlex Elder } 349c54d47bfSAlex Elder } 350c54d47bfSAlex Elder 3513d14c5d2SYehuda Sadeh /* 35263244fa1SIlya Dryomov * Assumes @t is zero-initialized. 35363244fa1SIlya Dryomov */ 35463244fa1SIlya Dryomov static void target_init(struct ceph_osd_request_target *t) 35563244fa1SIlya Dryomov { 35663244fa1SIlya Dryomov ceph_oid_init(&t->base_oid); 35763244fa1SIlya Dryomov ceph_oloc_init(&t->base_oloc); 35863244fa1SIlya Dryomov ceph_oid_init(&t->target_oid); 35963244fa1SIlya Dryomov ceph_oloc_init(&t->target_oloc); 36063244fa1SIlya Dryomov 36163244fa1SIlya Dryomov ceph_osds_init(&t->acting); 36263244fa1SIlya Dryomov ceph_osds_init(&t->up); 36363244fa1SIlya Dryomov t->size = -1; 36463244fa1SIlya Dryomov t->min_size = -1; 36563244fa1SIlya Dryomov 36663244fa1SIlya Dryomov t->osd = CEPH_HOMELESS_OSD; 36763244fa1SIlya Dryomov } 36863244fa1SIlya Dryomov 369922dab61SIlya Dryomov static void target_copy(struct ceph_osd_request_target *dest, 370922dab61SIlya Dryomov const struct ceph_osd_request_target *src) 371922dab61SIlya Dryomov { 372922dab61SIlya Dryomov ceph_oid_copy(&dest->base_oid, &src->base_oid); 373922dab61SIlya Dryomov ceph_oloc_copy(&dest->base_oloc, &src->base_oloc); 374922dab61SIlya Dryomov ceph_oid_copy(&dest->target_oid, &src->target_oid); 375922dab61SIlya Dryomov ceph_oloc_copy(&dest->target_oloc, &src->target_oloc); 376922dab61SIlya Dryomov 377922dab61SIlya Dryomov dest->pgid = src->pgid; /* struct */ 378dc98ff72SIlya Dryomov dest->spgid = src->spgid; /* struct */ 379922dab61SIlya Dryomov dest->pg_num = src->pg_num; 380922dab61SIlya Dryomov dest->pg_num_mask = src->pg_num_mask; 381922dab61SIlya Dryomov ceph_osds_copy(&dest->acting, &src->acting); 382922dab61SIlya Dryomov ceph_osds_copy(&dest->up, &src->up); 383922dab61SIlya Dryomov dest->size = src->size; 384922dab61SIlya Dryomov dest->min_size = src->min_size; 385922dab61SIlya Dryomov dest->sort_bitwise = src->sort_bitwise; 386922dab61SIlya Dryomov 387922dab61SIlya Dryomov dest->flags = src->flags; 388922dab61SIlya Dryomov dest->paused = src->paused; 389922dab61SIlya Dryomov 39004c7d789SIlya Dryomov dest->epoch = src->epoch; 391dc93e0e2SIlya Dryomov dest->last_force_resend = src->last_force_resend; 392dc93e0e2SIlya Dryomov 393922dab61SIlya Dryomov dest->osd = src->osd; 394922dab61SIlya Dryomov } 395922dab61SIlya Dryomov 39663244fa1SIlya Dryomov static void target_destroy(struct ceph_osd_request_target *t) 39763244fa1SIlya Dryomov { 39863244fa1SIlya Dryomov ceph_oid_destroy(&t->base_oid); 39930c156d9SYan, Zheng ceph_oloc_destroy(&t->base_oloc); 40063244fa1SIlya Dryomov ceph_oid_destroy(&t->target_oid); 40130c156d9SYan, Zheng ceph_oloc_destroy(&t->target_oloc); 40263244fa1SIlya Dryomov } 40363244fa1SIlya Dryomov 40463244fa1SIlya Dryomov /* 4053d14c5d2SYehuda Sadeh * requests 4063d14c5d2SYehuda Sadeh */ 4073540bfdbSIlya Dryomov static void request_release_checks(struct ceph_osd_request *req) 4083540bfdbSIlya Dryomov { 4093540bfdbSIlya Dryomov WARN_ON(!RB_EMPTY_NODE(&req->r_node)); 4104609245eSIlya Dryomov WARN_ON(!RB_EMPTY_NODE(&req->r_mc_node)); 4113540bfdbSIlya Dryomov WARN_ON(!list_empty(&req->r_unsafe_item)); 4123540bfdbSIlya Dryomov WARN_ON(req->r_osd); 4133540bfdbSIlya Dryomov } 4143540bfdbSIlya Dryomov 4159e94af20SIlya Dryomov static void ceph_osdc_release_request(struct kref *kref) 4163d14c5d2SYehuda Sadeh { 4179e94af20SIlya Dryomov struct ceph_osd_request *req = container_of(kref, 4189e94af20SIlya Dryomov struct ceph_osd_request, r_kref); 4195476492fSAlex Elder unsigned int which; 4203d14c5d2SYehuda Sadeh 4219e94af20SIlya Dryomov dout("%s %p (r_request %p r_reply %p)\n", __func__, req, 4229e94af20SIlya Dryomov req->r_request, req->r_reply); 4233540bfdbSIlya Dryomov request_release_checks(req); 4249e94af20SIlya Dryomov 4253d14c5d2SYehuda Sadeh if (req->r_request) 4263d14c5d2SYehuda Sadeh ceph_msg_put(req->r_request); 4275aea3dcdSIlya Dryomov if (req->r_reply) 428ab8cb34aSAlex Elder ceph_msg_put(req->r_reply); 4290fff87ecSAlex Elder 4305476492fSAlex Elder for (which = 0; which < req->r_num_ops; which++) 4315476492fSAlex Elder osd_req_op_data_release(req, which); 4320fff87ecSAlex Elder 433a66dd383SIlya Dryomov target_destroy(&req->r_t); 4343d14c5d2SYehuda Sadeh ceph_put_snap_context(req->r_snapc); 435d30291b9SIlya Dryomov 4363d14c5d2SYehuda Sadeh if (req->r_mempool) 4373d14c5d2SYehuda Sadeh mempool_free(req, req->r_osdc->req_mempool); 4383f1af42aSIlya Dryomov else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS) 4395522ae0bSAlex Elder kmem_cache_free(ceph_osd_request_cache, req); 4403f1af42aSIlya Dryomov else 4413f1af42aSIlya Dryomov kfree(req); 4423d14c5d2SYehuda Sadeh } 4439e94af20SIlya Dryomov 4449e94af20SIlya Dryomov void ceph_osdc_get_request(struct ceph_osd_request *req) 4459e94af20SIlya Dryomov { 4469e94af20SIlya Dryomov dout("%s %p (was %d)\n", __func__, req, 4472c935bc5SPeter Zijlstra kref_read(&req->r_kref)); 4489e94af20SIlya Dryomov kref_get(&req->r_kref); 4499e94af20SIlya Dryomov } 4509e94af20SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_get_request); 4519e94af20SIlya Dryomov 4529e94af20SIlya Dryomov void ceph_osdc_put_request(struct ceph_osd_request *req) 4539e94af20SIlya Dryomov { 4543ed97d63SIlya Dryomov if (req) { 4559e94af20SIlya Dryomov dout("%s %p (was %d)\n", __func__, req, 4562c935bc5SPeter Zijlstra kref_read(&req->r_kref)); 4579e94af20SIlya Dryomov kref_put(&req->r_kref, ceph_osdc_release_request); 4589e94af20SIlya Dryomov } 4593ed97d63SIlya Dryomov } 4609e94af20SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_put_request); 4613d14c5d2SYehuda Sadeh 4623540bfdbSIlya Dryomov static void request_init(struct ceph_osd_request *req) 4633540bfdbSIlya Dryomov { 4643540bfdbSIlya Dryomov /* req only, each op is zeroed in _osd_req_op_init() */ 4653540bfdbSIlya Dryomov memset(req, 0, sizeof(*req)); 4663540bfdbSIlya Dryomov 4673540bfdbSIlya Dryomov kref_init(&req->r_kref); 4683540bfdbSIlya Dryomov init_completion(&req->r_completion); 4693540bfdbSIlya Dryomov RB_CLEAR_NODE(&req->r_node); 4704609245eSIlya Dryomov RB_CLEAR_NODE(&req->r_mc_node); 4713540bfdbSIlya Dryomov INIT_LIST_HEAD(&req->r_unsafe_item); 4723540bfdbSIlya Dryomov 4733540bfdbSIlya Dryomov target_init(&req->r_t); 4743540bfdbSIlya Dryomov } 4753540bfdbSIlya Dryomov 476922dab61SIlya Dryomov /* 477922dab61SIlya Dryomov * This is ugly, but it allows us to reuse linger registration and ping 478922dab61SIlya Dryomov * requests, keeping the structure of the code around send_linger{_ping}() 479922dab61SIlya Dryomov * reasonable. Setting up a min_nr=2 mempool for each linger request 480922dab61SIlya Dryomov * and dealing with copying ops (this blasts req only, watch op remains 481922dab61SIlya Dryomov * intact) isn't any better. 482922dab61SIlya Dryomov */ 483922dab61SIlya Dryomov static void request_reinit(struct ceph_osd_request *req) 484922dab61SIlya Dryomov { 485922dab61SIlya Dryomov struct ceph_osd_client *osdc = req->r_osdc; 486922dab61SIlya Dryomov bool mempool = req->r_mempool; 487922dab61SIlya Dryomov unsigned int num_ops = req->r_num_ops; 488922dab61SIlya Dryomov u64 snapid = req->r_snapid; 489922dab61SIlya Dryomov struct ceph_snap_context *snapc = req->r_snapc; 490922dab61SIlya Dryomov bool linger = req->r_linger; 491922dab61SIlya Dryomov struct ceph_msg *request_msg = req->r_request; 492922dab61SIlya Dryomov struct ceph_msg *reply_msg = req->r_reply; 493922dab61SIlya Dryomov 494922dab61SIlya Dryomov dout("%s req %p\n", __func__, req); 4952c935bc5SPeter Zijlstra WARN_ON(kref_read(&req->r_kref) != 1); 496922dab61SIlya Dryomov request_release_checks(req); 497922dab61SIlya Dryomov 4982c935bc5SPeter Zijlstra WARN_ON(kref_read(&request_msg->kref) != 1); 4992c935bc5SPeter Zijlstra WARN_ON(kref_read(&reply_msg->kref) != 1); 500922dab61SIlya Dryomov target_destroy(&req->r_t); 501922dab61SIlya Dryomov 502922dab61SIlya Dryomov request_init(req); 503922dab61SIlya Dryomov req->r_osdc = osdc; 504922dab61SIlya Dryomov req->r_mempool = mempool; 505922dab61SIlya Dryomov req->r_num_ops = num_ops; 506922dab61SIlya Dryomov req->r_snapid = snapid; 507922dab61SIlya Dryomov req->r_snapc = snapc; 508922dab61SIlya Dryomov req->r_linger = linger; 509922dab61SIlya Dryomov req->r_request = request_msg; 510922dab61SIlya Dryomov req->r_reply = reply_msg; 511922dab61SIlya Dryomov } 512922dab61SIlya Dryomov 5133d14c5d2SYehuda Sadeh struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc, 5143d14c5d2SYehuda Sadeh struct ceph_snap_context *snapc, 5151b83bef2SSage Weil unsigned int num_ops, 5163d14c5d2SYehuda Sadeh bool use_mempool, 51754a54007SAlex Elder gfp_t gfp_flags) 5183d14c5d2SYehuda Sadeh { 5193d14c5d2SYehuda Sadeh struct ceph_osd_request *req; 5203d14c5d2SYehuda Sadeh 5213d14c5d2SYehuda Sadeh if (use_mempool) { 5223f1af42aSIlya Dryomov BUG_ON(num_ops > CEPH_OSD_SLAB_OPS); 5233d14c5d2SYehuda Sadeh req = mempool_alloc(osdc->req_mempool, gfp_flags); 5243f1af42aSIlya Dryomov } else if (num_ops <= CEPH_OSD_SLAB_OPS) { 5253f1af42aSIlya Dryomov req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags); 5263d14c5d2SYehuda Sadeh } else { 5273f1af42aSIlya Dryomov BUG_ON(num_ops > CEPH_OSD_MAX_OPS); 5283f1af42aSIlya Dryomov req = kmalloc(sizeof(*req) + num_ops * sizeof(req->r_ops[0]), 5293f1af42aSIlya Dryomov gfp_flags); 5303d14c5d2SYehuda Sadeh } 5313f1af42aSIlya Dryomov if (unlikely(!req)) 5323d14c5d2SYehuda Sadeh return NULL; 5333d14c5d2SYehuda Sadeh 5343540bfdbSIlya Dryomov request_init(req); 5353d14c5d2SYehuda Sadeh req->r_osdc = osdc; 5363d14c5d2SYehuda Sadeh req->r_mempool = use_mempool; 53779528734SAlex Elder req->r_num_ops = num_ops; 53884127282SIlya Dryomov req->r_snapid = CEPH_NOSNAP; 53984127282SIlya Dryomov req->r_snapc = ceph_get_snap_context(snapc); 5403d14c5d2SYehuda Sadeh 54113d1ad16SIlya Dryomov dout("%s req %p\n", __func__, req); 54213d1ad16SIlya Dryomov return req; 5433f1af42aSIlya Dryomov } 54413d1ad16SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_alloc_request); 5453f1af42aSIlya Dryomov 5462e59ffd1SIlya Dryomov static int ceph_oloc_encoding_size(const struct ceph_object_locator *oloc) 54730c156d9SYan, Zheng { 54830c156d9SYan, Zheng return 8 + 4 + 4 + 4 + (oloc->pool_ns ? oloc->pool_ns->len : 0); 54930c156d9SYan, Zheng } 55030c156d9SYan, Zheng 55113d1ad16SIlya Dryomov int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp) 55213d1ad16SIlya Dryomov { 55313d1ad16SIlya Dryomov struct ceph_osd_client *osdc = req->r_osdc; 55413d1ad16SIlya Dryomov struct ceph_msg *msg; 55513d1ad16SIlya Dryomov int msg_size; 5563d14c5d2SYehuda Sadeh 557d30291b9SIlya Dryomov WARN_ON(ceph_oid_empty(&req->r_base_oid)); 55830c156d9SYan, Zheng WARN_ON(ceph_oloc_empty(&req->r_base_oloc)); 559d30291b9SIlya Dryomov 56013d1ad16SIlya Dryomov /* create request message */ 5618cb441c0SIlya Dryomov msg_size = CEPH_ENCODING_START_BLK_LEN + 5628cb441c0SIlya Dryomov CEPH_PGID_ENCODING_LEN + 1; /* spgid */ 5638cb441c0SIlya Dryomov msg_size += 4 + 4 + 4; /* hash, osdmap_epoch, flags */ 5648cb441c0SIlya Dryomov msg_size += CEPH_ENCODING_START_BLK_LEN + 5658cb441c0SIlya Dryomov sizeof(struct ceph_osd_reqid); /* reqid */ 5668cb441c0SIlya Dryomov msg_size += sizeof(struct ceph_blkin_trace_info); /* trace */ 5678cb441c0SIlya Dryomov msg_size += 4 + sizeof(struct ceph_timespec); /* client_inc, mtime */ 56830c156d9SYan, Zheng msg_size += CEPH_ENCODING_START_BLK_LEN + 56930c156d9SYan, Zheng ceph_oloc_encoding_size(&req->r_base_oloc); /* oloc */ 57013d1ad16SIlya Dryomov msg_size += 4 + req->r_base_oid.name_len; /* oid */ 57113d1ad16SIlya Dryomov msg_size += 2 + req->r_num_ops * sizeof(struct ceph_osd_op); 572ae458f5aSIlya Dryomov msg_size += 8; /* snapid */ 573ae458f5aSIlya Dryomov msg_size += 8; /* snap_seq */ 57413d1ad16SIlya Dryomov msg_size += 4 + 8 * (req->r_snapc ? req->r_snapc->num_snaps : 0); 5758cb441c0SIlya Dryomov msg_size += 4 + 8; /* retry_attempt, features */ 576ae458f5aSIlya Dryomov 57713d1ad16SIlya Dryomov if (req->r_mempool) 5783d14c5d2SYehuda Sadeh msg = ceph_msgpool_get(&osdc->msgpool_op, 0); 5793d14c5d2SYehuda Sadeh else 58013d1ad16SIlya Dryomov msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp, true); 58113d1ad16SIlya Dryomov if (!msg) 58213d1ad16SIlya Dryomov return -ENOMEM; 5833d14c5d2SYehuda Sadeh 5843d14c5d2SYehuda Sadeh memset(msg->front.iov_base, 0, msg->front.iov_len); 5853d14c5d2SYehuda Sadeh req->r_request = msg; 5863d14c5d2SYehuda Sadeh 58713d1ad16SIlya Dryomov /* create reply message */ 58813d1ad16SIlya Dryomov msg_size = OSD_OPREPLY_FRONT_LEN; 589711da55dSIlya Dryomov msg_size += req->r_base_oid.name_len; 590711da55dSIlya Dryomov msg_size += req->r_num_ops * sizeof(struct ceph_osd_op); 59113d1ad16SIlya Dryomov 59213d1ad16SIlya Dryomov if (req->r_mempool) 59313d1ad16SIlya Dryomov msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0); 59413d1ad16SIlya Dryomov else 59513d1ad16SIlya Dryomov msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, msg_size, gfp, true); 59613d1ad16SIlya Dryomov if (!msg) 59713d1ad16SIlya Dryomov return -ENOMEM; 59813d1ad16SIlya Dryomov 59913d1ad16SIlya Dryomov req->r_reply = msg; 60013d1ad16SIlya Dryomov 60113d1ad16SIlya Dryomov return 0; 60213d1ad16SIlya Dryomov } 60313d1ad16SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_alloc_messages); 6043d14c5d2SYehuda Sadeh 605a8dd0a37SAlex Elder static bool osd_req_opcode_valid(u16 opcode) 606a8dd0a37SAlex Elder { 607a8dd0a37SAlex Elder switch (opcode) { 60870b5bfa3SIlya Dryomov #define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true; 60970b5bfa3SIlya Dryomov __CEPH_FORALL_OSD_OPS(GENERATE_CASE) 61070b5bfa3SIlya Dryomov #undef GENERATE_CASE 611a8dd0a37SAlex Elder default: 612a8dd0a37SAlex Elder return false; 613a8dd0a37SAlex Elder } 614a8dd0a37SAlex Elder } 615a8dd0a37SAlex Elder 61633803f33SAlex Elder /* 61733803f33SAlex Elder * This is an osd op init function for opcodes that have no data or 61833803f33SAlex Elder * other information associated with them. It also serves as a 61933803f33SAlex Elder * common init routine for all the other init functions, below. 62033803f33SAlex Elder */ 621c99d2d4aSAlex Elder static struct ceph_osd_req_op * 62249719778SAlex Elder _osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which, 623144cba14SYan, Zheng u16 opcode, u32 flags) 62433803f33SAlex Elder { 625c99d2d4aSAlex Elder struct ceph_osd_req_op *op; 626c99d2d4aSAlex Elder 627c99d2d4aSAlex Elder BUG_ON(which >= osd_req->r_num_ops); 62833803f33SAlex Elder BUG_ON(!osd_req_opcode_valid(opcode)); 62933803f33SAlex Elder 630c99d2d4aSAlex Elder op = &osd_req->r_ops[which]; 63133803f33SAlex Elder memset(op, 0, sizeof (*op)); 63233803f33SAlex Elder op->op = opcode; 633144cba14SYan, Zheng op->flags = flags; 634c99d2d4aSAlex Elder 635c99d2d4aSAlex Elder return op; 63633803f33SAlex Elder } 63733803f33SAlex Elder 63849719778SAlex Elder void osd_req_op_init(struct ceph_osd_request *osd_req, 639144cba14SYan, Zheng unsigned int which, u16 opcode, u32 flags) 64049719778SAlex Elder { 641144cba14SYan, Zheng (void)_osd_req_op_init(osd_req, which, opcode, flags); 64249719778SAlex Elder } 64349719778SAlex Elder EXPORT_SYMBOL(osd_req_op_init); 64449719778SAlex Elder 645c99d2d4aSAlex Elder void osd_req_op_extent_init(struct ceph_osd_request *osd_req, 646c99d2d4aSAlex Elder unsigned int which, u16 opcode, 64733803f33SAlex Elder u64 offset, u64 length, 64833803f33SAlex Elder u64 truncate_size, u32 truncate_seq) 64933803f33SAlex Elder { 650144cba14SYan, Zheng struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, 651144cba14SYan, Zheng opcode, 0); 65233803f33SAlex Elder size_t payload_len = 0; 65333803f33SAlex Elder 654ad7a60deSLi Wang BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE && 655e30b7577SIlya Dryomov opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO && 656e30b7577SIlya Dryomov opcode != CEPH_OSD_OP_TRUNCATE); 65733803f33SAlex Elder 65833803f33SAlex Elder op->extent.offset = offset; 65933803f33SAlex Elder op->extent.length = length; 66033803f33SAlex Elder op->extent.truncate_size = truncate_size; 66133803f33SAlex Elder op->extent.truncate_seq = truncate_seq; 662e30b7577SIlya Dryomov if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL) 66333803f33SAlex Elder payload_len += length; 66433803f33SAlex Elder 665de2aa102SIlya Dryomov op->indata_len = payload_len; 66633803f33SAlex Elder } 66733803f33SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_init); 66833803f33SAlex Elder 669c99d2d4aSAlex Elder void osd_req_op_extent_update(struct ceph_osd_request *osd_req, 670c99d2d4aSAlex Elder unsigned int which, u64 length) 671e5975c7cSAlex Elder { 672c99d2d4aSAlex Elder struct ceph_osd_req_op *op; 673c99d2d4aSAlex Elder u64 previous; 674c99d2d4aSAlex Elder 675c99d2d4aSAlex Elder BUG_ON(which >= osd_req->r_num_ops); 676c99d2d4aSAlex Elder op = &osd_req->r_ops[which]; 677c99d2d4aSAlex Elder previous = op->extent.length; 678e5975c7cSAlex Elder 679e5975c7cSAlex Elder if (length == previous) 680e5975c7cSAlex Elder return; /* Nothing to do */ 681e5975c7cSAlex Elder BUG_ON(length > previous); 682e5975c7cSAlex Elder 683e5975c7cSAlex Elder op->extent.length = length; 684d641df81SYan, Zheng if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL) 685de2aa102SIlya Dryomov op->indata_len -= previous - length; 686e5975c7cSAlex Elder } 687e5975c7cSAlex Elder EXPORT_SYMBOL(osd_req_op_extent_update); 688e5975c7cSAlex Elder 6892c63f49aSYan, Zheng void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req, 6902c63f49aSYan, Zheng unsigned int which, u64 offset_inc) 6912c63f49aSYan, Zheng { 6922c63f49aSYan, Zheng struct ceph_osd_req_op *op, *prev_op; 6932c63f49aSYan, Zheng 6942c63f49aSYan, Zheng BUG_ON(which + 1 >= osd_req->r_num_ops); 6952c63f49aSYan, Zheng 6962c63f49aSYan, Zheng prev_op = &osd_req->r_ops[which]; 6972c63f49aSYan, Zheng op = _osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags); 6982c63f49aSYan, Zheng /* dup previous one */ 6992c63f49aSYan, Zheng op->indata_len = prev_op->indata_len; 7002c63f49aSYan, Zheng op->outdata_len = prev_op->outdata_len; 7012c63f49aSYan, Zheng op->extent = prev_op->extent; 7022c63f49aSYan, Zheng /* adjust offset */ 7032c63f49aSYan, Zheng op->extent.offset += offset_inc; 7042c63f49aSYan, Zheng op->extent.length -= offset_inc; 7052c63f49aSYan, Zheng 7062c63f49aSYan, Zheng if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL) 7072c63f49aSYan, Zheng op->indata_len -= offset_inc; 7082c63f49aSYan, Zheng } 7092c63f49aSYan, Zheng EXPORT_SYMBOL(osd_req_op_extent_dup_last); 7102c63f49aSYan, Zheng 711c99d2d4aSAlex Elder void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which, 71204017e29SAlex Elder u16 opcode, const char *class, const char *method) 71333803f33SAlex Elder { 714144cba14SYan, Zheng struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, 715144cba14SYan, Zheng opcode, 0); 7165f562df5SAlex Elder struct ceph_pagelist *pagelist; 71733803f33SAlex Elder size_t payload_len = 0; 71833803f33SAlex Elder size_t size; 71933803f33SAlex Elder 72033803f33SAlex Elder BUG_ON(opcode != CEPH_OSD_OP_CALL); 72133803f33SAlex Elder 7225f562df5SAlex Elder pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS); 7235f562df5SAlex Elder BUG_ON(!pagelist); 7245f562df5SAlex Elder ceph_pagelist_init(pagelist); 7255f562df5SAlex Elder 72633803f33SAlex Elder op->cls.class_name = class; 72733803f33SAlex Elder size = strlen(class); 72833803f33SAlex Elder BUG_ON(size > (size_t) U8_MAX); 72933803f33SAlex Elder op->cls.class_len = size; 7305f562df5SAlex Elder ceph_pagelist_append(pagelist, class, size); 73133803f33SAlex Elder payload_len += size; 73233803f33SAlex Elder 73333803f33SAlex Elder op->cls.method_name = method; 73433803f33SAlex Elder size = strlen(method); 73533803f33SAlex Elder BUG_ON(size > (size_t) U8_MAX); 73633803f33SAlex Elder op->cls.method_len = size; 7375f562df5SAlex Elder ceph_pagelist_append(pagelist, method, size); 73833803f33SAlex Elder payload_len += size; 73933803f33SAlex Elder 740a4ce40a9SAlex Elder osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist); 7415f562df5SAlex Elder 742de2aa102SIlya Dryomov op->indata_len = payload_len; 74333803f33SAlex Elder } 74433803f33SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_init); 7458c042b0dSAlex Elder 746d74b50beSYan, Zheng int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which, 747d74b50beSYan, Zheng u16 opcode, const char *name, const void *value, 748d74b50beSYan, Zheng size_t size, u8 cmp_op, u8 cmp_mode) 749d74b50beSYan, Zheng { 750144cba14SYan, Zheng struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, 751144cba14SYan, Zheng opcode, 0); 752d74b50beSYan, Zheng struct ceph_pagelist *pagelist; 753d74b50beSYan, Zheng size_t payload_len; 754d74b50beSYan, Zheng 755d74b50beSYan, Zheng BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR); 756d74b50beSYan, Zheng 757d74b50beSYan, Zheng pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS); 758d74b50beSYan, Zheng if (!pagelist) 759d74b50beSYan, Zheng return -ENOMEM; 760d74b50beSYan, Zheng 761d74b50beSYan, Zheng ceph_pagelist_init(pagelist); 762d74b50beSYan, Zheng 763d74b50beSYan, Zheng payload_len = strlen(name); 764d74b50beSYan, Zheng op->xattr.name_len = payload_len; 765d74b50beSYan, Zheng ceph_pagelist_append(pagelist, name, payload_len); 766d74b50beSYan, Zheng 767d74b50beSYan, Zheng op->xattr.value_len = size; 768d74b50beSYan, Zheng ceph_pagelist_append(pagelist, value, size); 769d74b50beSYan, Zheng payload_len += size; 770d74b50beSYan, Zheng 771d74b50beSYan, Zheng op->xattr.cmp_op = cmp_op; 772d74b50beSYan, Zheng op->xattr.cmp_mode = cmp_mode; 773d74b50beSYan, Zheng 774d74b50beSYan, Zheng ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist); 775de2aa102SIlya Dryomov op->indata_len = payload_len; 776d74b50beSYan, Zheng return 0; 777d74b50beSYan, Zheng } 778d74b50beSYan, Zheng EXPORT_SYMBOL(osd_req_op_xattr_init); 779d74b50beSYan, Zheng 780922dab61SIlya Dryomov /* 781922dab61SIlya Dryomov * @watch_opcode: CEPH_OSD_WATCH_OP_* 782922dab61SIlya Dryomov */ 783922dab61SIlya Dryomov static void osd_req_op_watch_init(struct ceph_osd_request *req, int which, 784922dab61SIlya Dryomov u64 cookie, u8 watch_opcode) 78533803f33SAlex Elder { 786922dab61SIlya Dryomov struct ceph_osd_req_op *op; 78733803f33SAlex Elder 788922dab61SIlya Dryomov op = _osd_req_op_init(req, which, CEPH_OSD_OP_WATCH, 0); 78933803f33SAlex Elder op->watch.cookie = cookie; 790922dab61SIlya Dryomov op->watch.op = watch_opcode; 791922dab61SIlya Dryomov op->watch.gen = 0; 79233803f33SAlex Elder } 79333803f33SAlex Elder 794c647b8a8SIlya Dryomov void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req, 795c647b8a8SIlya Dryomov unsigned int which, 796c647b8a8SIlya Dryomov u64 expected_object_size, 797c647b8a8SIlya Dryomov u64 expected_write_size) 798c647b8a8SIlya Dryomov { 799c647b8a8SIlya Dryomov struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, 800144cba14SYan, Zheng CEPH_OSD_OP_SETALLOCHINT, 801144cba14SYan, Zheng 0); 802c647b8a8SIlya Dryomov 803c647b8a8SIlya Dryomov op->alloc_hint.expected_object_size = expected_object_size; 804c647b8a8SIlya Dryomov op->alloc_hint.expected_write_size = expected_write_size; 805c647b8a8SIlya Dryomov 806c647b8a8SIlya Dryomov /* 807c647b8a8SIlya Dryomov * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed 808c647b8a8SIlya Dryomov * not worth a feature bit. Set FAILOK per-op flag to make 809c647b8a8SIlya Dryomov * sure older osds don't trip over an unsupported opcode. 810c647b8a8SIlya Dryomov */ 811c647b8a8SIlya Dryomov op->flags |= CEPH_OSD_OP_FLAG_FAILOK; 812c647b8a8SIlya Dryomov } 813c647b8a8SIlya Dryomov EXPORT_SYMBOL(osd_req_op_alloc_hint_init); 814c647b8a8SIlya Dryomov 81590af3602SAlex Elder static void ceph_osdc_msg_data_add(struct ceph_msg *msg, 816ec9123c5SAlex Elder struct ceph_osd_data *osd_data) 817ec9123c5SAlex Elder { 818ec9123c5SAlex Elder u64 length = ceph_osd_data_length(osd_data); 819ec9123c5SAlex Elder 820ec9123c5SAlex Elder if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) { 821ec9123c5SAlex Elder BUG_ON(length > (u64) SIZE_MAX); 822ec9123c5SAlex Elder if (length) 82390af3602SAlex Elder ceph_msg_data_add_pages(msg, osd_data->pages, 824ec9123c5SAlex Elder length, osd_data->alignment); 825ec9123c5SAlex Elder } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) { 826ec9123c5SAlex Elder BUG_ON(!length); 82790af3602SAlex Elder ceph_msg_data_add_pagelist(msg, osd_data->pagelist); 828ec9123c5SAlex Elder #ifdef CONFIG_BLOCK 829ec9123c5SAlex Elder } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) { 83090af3602SAlex Elder ceph_msg_data_add_bio(msg, osd_data->bio, length); 831ec9123c5SAlex Elder #endif 832ec9123c5SAlex Elder } else { 833ec9123c5SAlex Elder BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE); 834ec9123c5SAlex Elder } 835ec9123c5SAlex Elder } 836ec9123c5SAlex Elder 837bb873b53SIlya Dryomov static u32 osd_req_encode_op(struct ceph_osd_op *dst, 838bb873b53SIlya Dryomov const struct ceph_osd_req_op *src) 8393d14c5d2SYehuda Sadeh { 840a8dd0a37SAlex Elder if (WARN_ON(!osd_req_opcode_valid(src->op))) { 841a8dd0a37SAlex Elder pr_err("unrecognized osd opcode %d\n", src->op); 842a8dd0a37SAlex Elder 843a8dd0a37SAlex Elder return 0; 844a8dd0a37SAlex Elder } 8453d14c5d2SYehuda Sadeh 846065a68f9SAlex Elder switch (src->op) { 847fbfab539SAlex Elder case CEPH_OSD_OP_STAT: 848fbfab539SAlex Elder break; 8493d14c5d2SYehuda Sadeh case CEPH_OSD_OP_READ: 8503d14c5d2SYehuda Sadeh case CEPH_OSD_OP_WRITE: 851e30b7577SIlya Dryomov case CEPH_OSD_OP_WRITEFULL: 852ad7a60deSLi Wang case CEPH_OSD_OP_ZERO: 853ad7a60deSLi Wang case CEPH_OSD_OP_TRUNCATE: 854175face2SAlex Elder dst->extent.offset = cpu_to_le64(src->extent.offset); 855175face2SAlex Elder dst->extent.length = cpu_to_le64(src->extent.length); 8563d14c5d2SYehuda Sadeh dst->extent.truncate_size = 8573d14c5d2SYehuda Sadeh cpu_to_le64(src->extent.truncate_size); 8583d14c5d2SYehuda Sadeh dst->extent.truncate_seq = 8593d14c5d2SYehuda Sadeh cpu_to_le32(src->extent.truncate_seq); 8603d14c5d2SYehuda Sadeh break; 8613d14c5d2SYehuda Sadeh case CEPH_OSD_OP_CALL: 8623d14c5d2SYehuda Sadeh dst->cls.class_len = src->cls.class_len; 8633d14c5d2SYehuda Sadeh dst->cls.method_len = src->cls.method_len; 864bb873b53SIlya Dryomov dst->cls.indata_len = cpu_to_le32(src->cls.indata_len); 8653d14c5d2SYehuda Sadeh break; 8663d14c5d2SYehuda Sadeh case CEPH_OSD_OP_STARTSYNC: 8673d14c5d2SYehuda Sadeh break; 868a40c4f10SYehuda Sadeh case CEPH_OSD_OP_WATCH: 869a40c4f10SYehuda Sadeh dst->watch.cookie = cpu_to_le64(src->watch.cookie); 870922dab61SIlya Dryomov dst->watch.ver = cpu_to_le64(0); 871922dab61SIlya Dryomov dst->watch.op = src->watch.op; 872922dab61SIlya Dryomov dst->watch.gen = cpu_to_le32(src->watch.gen); 873922dab61SIlya Dryomov break; 874922dab61SIlya Dryomov case CEPH_OSD_OP_NOTIFY_ACK: 875a40c4f10SYehuda Sadeh break; 87619079203SIlya Dryomov case CEPH_OSD_OP_NOTIFY: 87719079203SIlya Dryomov dst->notify.cookie = cpu_to_le64(src->notify.cookie); 87819079203SIlya Dryomov break; 879a4ed38d7SDouglas Fuller case CEPH_OSD_OP_LIST_WATCHERS: 880a4ed38d7SDouglas Fuller break; 881c647b8a8SIlya Dryomov case CEPH_OSD_OP_SETALLOCHINT: 882c647b8a8SIlya Dryomov dst->alloc_hint.expected_object_size = 883c647b8a8SIlya Dryomov cpu_to_le64(src->alloc_hint.expected_object_size); 884c647b8a8SIlya Dryomov dst->alloc_hint.expected_write_size = 885c647b8a8SIlya Dryomov cpu_to_le64(src->alloc_hint.expected_write_size); 886c647b8a8SIlya Dryomov break; 887d74b50beSYan, Zheng case CEPH_OSD_OP_SETXATTR: 888d74b50beSYan, Zheng case CEPH_OSD_OP_CMPXATTR: 889d74b50beSYan, Zheng dst->xattr.name_len = cpu_to_le32(src->xattr.name_len); 890d74b50beSYan, Zheng dst->xattr.value_len = cpu_to_le32(src->xattr.value_len); 891d74b50beSYan, Zheng dst->xattr.cmp_op = src->xattr.cmp_op; 892d74b50beSYan, Zheng dst->xattr.cmp_mode = src->xattr.cmp_mode; 893d74b50beSYan, Zheng break; 894864e9197SYan, Zheng case CEPH_OSD_OP_CREATE: 895864e9197SYan, Zheng case CEPH_OSD_OP_DELETE: 896864e9197SYan, Zheng break; 8973d14c5d2SYehuda Sadeh default: 8984c46459cSAlex Elder pr_err("unsupported osd opcode %s\n", 8998f63ca2dSAlex Elder ceph_osd_op_name(src->op)); 9004c46459cSAlex Elder WARN_ON(1); 901a8dd0a37SAlex Elder 902a8dd0a37SAlex Elder return 0; 9033d14c5d2SYehuda Sadeh } 9047b25bf5fSIlya Dryomov 905a8dd0a37SAlex Elder dst->op = cpu_to_le16(src->op); 9067b25bf5fSIlya Dryomov dst->flags = cpu_to_le32(src->flags); 907de2aa102SIlya Dryomov dst->payload_len = cpu_to_le32(src->indata_len); 908175face2SAlex Elder 909bb873b53SIlya Dryomov return src->indata_len; 9103d14c5d2SYehuda Sadeh } 9113d14c5d2SYehuda Sadeh 9123d14c5d2SYehuda Sadeh /* 9133d14c5d2SYehuda Sadeh * build new request AND message, calculate layout, and adjust file 9143d14c5d2SYehuda Sadeh * extent as needed. 9153d14c5d2SYehuda Sadeh * 9163d14c5d2SYehuda Sadeh * if the file was recently truncated, we include information about its 9173d14c5d2SYehuda Sadeh * old and new size so that the object can be updated appropriately. (we 9183d14c5d2SYehuda Sadeh * avoid synchronously deleting truncated objects because it's slow.) 9193d14c5d2SYehuda Sadeh * 9203d14c5d2SYehuda Sadeh * if @do_sync, include a 'startsync' command so that the osd will flush 9213d14c5d2SYehuda Sadeh * data quickly. 9223d14c5d2SYehuda Sadeh */ 9233d14c5d2SYehuda Sadeh struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc, 9243d14c5d2SYehuda Sadeh struct ceph_file_layout *layout, 9253d14c5d2SYehuda Sadeh struct ceph_vino vino, 926715e4cd4SYan, Zheng u64 off, u64 *plen, 927715e4cd4SYan, Zheng unsigned int which, int num_ops, 9283d14c5d2SYehuda Sadeh int opcode, int flags, 9293d14c5d2SYehuda Sadeh struct ceph_snap_context *snapc, 9303d14c5d2SYehuda Sadeh u32 truncate_seq, 9313d14c5d2SYehuda Sadeh u64 truncate_size, 932153e5167SAlex Elder bool use_mempool) 9333d14c5d2SYehuda Sadeh { 9343d14c5d2SYehuda Sadeh struct ceph_osd_request *req; 93575d1c941SAlex Elder u64 objnum = 0; 93675d1c941SAlex Elder u64 objoff = 0; 93775d1c941SAlex Elder u64 objlen = 0; 9386816282dSSage Weil int r; 9393d14c5d2SYehuda Sadeh 940ad7a60deSLi Wang BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE && 941864e9197SYan, Zheng opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE && 942864e9197SYan, Zheng opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE); 9433d14c5d2SYehuda Sadeh 944acead002SAlex Elder req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool, 945ae7ca4a3SAlex Elder GFP_NOFS); 94613d1ad16SIlya Dryomov if (!req) { 94713d1ad16SIlya Dryomov r = -ENOMEM; 94813d1ad16SIlya Dryomov goto fail; 94913d1ad16SIlya Dryomov } 95079528734SAlex Elder 9513d14c5d2SYehuda Sadeh /* calculate max write size */ 952a19dadfbSAlex Elder r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen); 95313d1ad16SIlya Dryomov if (r) 95413d1ad16SIlya Dryomov goto fail; 955a19dadfbSAlex Elder 956864e9197SYan, Zheng if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) { 957144cba14SYan, Zheng osd_req_op_init(req, which, opcode, 0); 958864e9197SYan, Zheng } else { 9597627151eSYan, Zheng u32 object_size = layout->object_size; 960864e9197SYan, Zheng u32 object_base = off - objoff; 961ccca4e37SYan, Zheng if (!(truncate_seq == 1 && truncate_size == -1ULL)) { 962d18d1e28SAlex Elder if (truncate_size <= object_base) { 963d18d1e28SAlex Elder truncate_size = 0; 964d18d1e28SAlex Elder } else { 965d18d1e28SAlex Elder truncate_size -= object_base; 966d18d1e28SAlex Elder if (truncate_size > object_size) 967d18d1e28SAlex Elder truncate_size = object_size; 968d18d1e28SAlex Elder } 969ccca4e37SYan, Zheng } 970715e4cd4SYan, Zheng osd_req_op_extent_init(req, which, opcode, objoff, objlen, 971b0270324SAlex Elder truncate_size, truncate_seq); 972864e9197SYan, Zheng } 973d18d1e28SAlex Elder 974a1f4020aSJeff Layton req->r_abort_on_full = true; 975bb873b53SIlya Dryomov req->r_flags = flags; 9767627151eSYan, Zheng req->r_base_oloc.pool = layout->pool_id; 97730c156d9SYan, Zheng req->r_base_oloc.pool_ns = ceph_try_get_string(layout->pool_ns); 978d30291b9SIlya Dryomov ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum); 979dbe0fc41SAlex Elder 980bb873b53SIlya Dryomov req->r_snapid = vino.snap; 981bb873b53SIlya Dryomov if (flags & CEPH_OSD_FLAG_WRITE) 982bb873b53SIlya Dryomov req->r_data_offset = off; 983bb873b53SIlya Dryomov 98413d1ad16SIlya Dryomov r = ceph_osdc_alloc_messages(req, GFP_NOFS); 98513d1ad16SIlya Dryomov if (r) 98613d1ad16SIlya Dryomov goto fail; 98713d1ad16SIlya Dryomov 9883d14c5d2SYehuda Sadeh return req; 98913d1ad16SIlya Dryomov 99013d1ad16SIlya Dryomov fail: 99113d1ad16SIlya Dryomov ceph_osdc_put_request(req); 99213d1ad16SIlya Dryomov return ERR_PTR(r); 9933d14c5d2SYehuda Sadeh } 9943d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_new_request); 9953d14c5d2SYehuda Sadeh 9963d14c5d2SYehuda Sadeh /* 9973d14c5d2SYehuda Sadeh * We keep osd requests in an rbtree, sorted by ->r_tid. 9983d14c5d2SYehuda Sadeh */ 999fcd00b68SIlya Dryomov DEFINE_RB_FUNCS(request, struct ceph_osd_request, r_tid, r_node) 10004609245eSIlya Dryomov DEFINE_RB_FUNCS(request_mc, struct ceph_osd_request, r_tid, r_mc_node) 10013d14c5d2SYehuda Sadeh 10020247a0cfSIlya Dryomov static bool osd_homeless(struct ceph_osd *osd) 10030247a0cfSIlya Dryomov { 10040247a0cfSIlya Dryomov return osd->o_osd == CEPH_HOMELESS_OSD; 10050247a0cfSIlya Dryomov } 10060247a0cfSIlya Dryomov 10075aea3dcdSIlya Dryomov static bool osd_registered(struct ceph_osd *osd) 10083d14c5d2SYehuda Sadeh { 10095aea3dcdSIlya Dryomov verify_osdc_locked(osd->o_osdc); 10103d14c5d2SYehuda Sadeh 10115aea3dcdSIlya Dryomov return !RB_EMPTY_NODE(&osd->o_node); 10123d14c5d2SYehuda Sadeh } 10133d14c5d2SYehuda Sadeh 10143d14c5d2SYehuda Sadeh /* 10150247a0cfSIlya Dryomov * Assumes @osd is zero-initialized. 10160247a0cfSIlya Dryomov */ 10170247a0cfSIlya Dryomov static void osd_init(struct ceph_osd *osd) 10180247a0cfSIlya Dryomov { 101902113a0fSElena Reshetova refcount_set(&osd->o_ref, 1); 10200247a0cfSIlya Dryomov RB_CLEAR_NODE(&osd->o_node); 10215aea3dcdSIlya Dryomov osd->o_requests = RB_ROOT; 1022922dab61SIlya Dryomov osd->o_linger_requests = RB_ROOT; 1023a02a946dSIlya Dryomov osd->o_backoff_mappings = RB_ROOT; 1024a02a946dSIlya Dryomov osd->o_backoffs_by_id = RB_ROOT; 10250247a0cfSIlya Dryomov INIT_LIST_HEAD(&osd->o_osd_lru); 10260247a0cfSIlya Dryomov INIT_LIST_HEAD(&osd->o_keepalive_item); 10270247a0cfSIlya Dryomov osd->o_incarnation = 1; 10285aea3dcdSIlya Dryomov mutex_init(&osd->lock); 10290247a0cfSIlya Dryomov } 10300247a0cfSIlya Dryomov 10310247a0cfSIlya Dryomov static void osd_cleanup(struct ceph_osd *osd) 10320247a0cfSIlya Dryomov { 10330247a0cfSIlya Dryomov WARN_ON(!RB_EMPTY_NODE(&osd->o_node)); 10345aea3dcdSIlya Dryomov WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests)); 1035922dab61SIlya Dryomov WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests)); 1036a02a946dSIlya Dryomov WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoff_mappings)); 1037a02a946dSIlya Dryomov WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoffs_by_id)); 10380247a0cfSIlya Dryomov WARN_ON(!list_empty(&osd->o_osd_lru)); 10390247a0cfSIlya Dryomov WARN_ON(!list_empty(&osd->o_keepalive_item)); 10400247a0cfSIlya Dryomov 10410247a0cfSIlya Dryomov if (osd->o_auth.authorizer) { 10420247a0cfSIlya Dryomov WARN_ON(osd_homeless(osd)); 10430247a0cfSIlya Dryomov ceph_auth_destroy_authorizer(osd->o_auth.authorizer); 10440247a0cfSIlya Dryomov } 10450247a0cfSIlya Dryomov } 10460247a0cfSIlya Dryomov 10470247a0cfSIlya Dryomov /* 10483d14c5d2SYehuda Sadeh * Track open sessions with osds. 10493d14c5d2SYehuda Sadeh */ 1050e10006f8SAlex Elder static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum) 10513d14c5d2SYehuda Sadeh { 10523d14c5d2SYehuda Sadeh struct ceph_osd *osd; 10533d14c5d2SYehuda Sadeh 10540247a0cfSIlya Dryomov WARN_ON(onum == CEPH_HOMELESS_OSD); 10550247a0cfSIlya Dryomov 10567a28f59bSIlya Dryomov osd = kzalloc(sizeof(*osd), GFP_NOIO | __GFP_NOFAIL); 10570247a0cfSIlya Dryomov osd_init(osd); 10583d14c5d2SYehuda Sadeh osd->o_osdc = osdc; 1059e10006f8SAlex Elder osd->o_osd = onum; 10603d14c5d2SYehuda Sadeh 1061b7a9e5ddSSage Weil ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr); 10623d14c5d2SYehuda Sadeh 10633d14c5d2SYehuda Sadeh return osd; 10643d14c5d2SYehuda Sadeh } 10653d14c5d2SYehuda Sadeh 10663d14c5d2SYehuda Sadeh static struct ceph_osd *get_osd(struct ceph_osd *osd) 10673d14c5d2SYehuda Sadeh { 106802113a0fSElena Reshetova if (refcount_inc_not_zero(&osd->o_ref)) { 106902113a0fSElena Reshetova dout("get_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref)-1, 107002113a0fSElena Reshetova refcount_read(&osd->o_ref)); 10713d14c5d2SYehuda Sadeh return osd; 10723d14c5d2SYehuda Sadeh } else { 10733d14c5d2SYehuda Sadeh dout("get_osd %p FAIL\n", osd); 10743d14c5d2SYehuda Sadeh return NULL; 10753d14c5d2SYehuda Sadeh } 10763d14c5d2SYehuda Sadeh } 10773d14c5d2SYehuda Sadeh 10783d14c5d2SYehuda Sadeh static void put_osd(struct ceph_osd *osd) 10793d14c5d2SYehuda Sadeh { 108002113a0fSElena Reshetova dout("put_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref), 108102113a0fSElena Reshetova refcount_read(&osd->o_ref) - 1); 108202113a0fSElena Reshetova if (refcount_dec_and_test(&osd->o_ref)) { 10830247a0cfSIlya Dryomov osd_cleanup(osd); 10843d14c5d2SYehuda Sadeh kfree(osd); 10853d14c5d2SYehuda Sadeh } 10863d14c5d2SYehuda Sadeh } 10873d14c5d2SYehuda Sadeh 1088fcd00b68SIlya Dryomov DEFINE_RB_FUNCS(osd, struct ceph_osd, o_osd, o_node) 1089fcd00b68SIlya Dryomov 10909dd2845cSIlya Dryomov static void __move_osd_to_lru(struct ceph_osd *osd) 10913d14c5d2SYehuda Sadeh { 10929dd2845cSIlya Dryomov struct ceph_osd_client *osdc = osd->o_osdc; 10939dd2845cSIlya Dryomov 10949dd2845cSIlya Dryomov dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd); 10953d14c5d2SYehuda Sadeh BUG_ON(!list_empty(&osd->o_osd_lru)); 1096bbf37ec3SIlya Dryomov 10979dd2845cSIlya Dryomov spin_lock(&osdc->osd_lru_lock); 10983d14c5d2SYehuda Sadeh list_add_tail(&osd->o_osd_lru, &osdc->osd_lru); 10999dd2845cSIlya Dryomov spin_unlock(&osdc->osd_lru_lock); 11009dd2845cSIlya Dryomov 1101a319bf56SIlya Dryomov osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl; 11023d14c5d2SYehuda Sadeh } 11033d14c5d2SYehuda Sadeh 11049dd2845cSIlya Dryomov static void maybe_move_osd_to_lru(struct ceph_osd *osd) 1105bbf37ec3SIlya Dryomov { 11065aea3dcdSIlya Dryomov if (RB_EMPTY_ROOT(&osd->o_requests) && 1107922dab61SIlya Dryomov RB_EMPTY_ROOT(&osd->o_linger_requests)) 11089dd2845cSIlya Dryomov __move_osd_to_lru(osd); 1109bbf37ec3SIlya Dryomov } 1110bbf37ec3SIlya Dryomov 11113d14c5d2SYehuda Sadeh static void __remove_osd_from_lru(struct ceph_osd *osd) 11123d14c5d2SYehuda Sadeh { 11139dd2845cSIlya Dryomov struct ceph_osd_client *osdc = osd->o_osdc; 11149dd2845cSIlya Dryomov 11159dd2845cSIlya Dryomov dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd); 11169dd2845cSIlya Dryomov 11179dd2845cSIlya Dryomov spin_lock(&osdc->osd_lru_lock); 11183d14c5d2SYehuda Sadeh if (!list_empty(&osd->o_osd_lru)) 11193d14c5d2SYehuda Sadeh list_del_init(&osd->o_osd_lru); 11209dd2845cSIlya Dryomov spin_unlock(&osdc->osd_lru_lock); 11213d14c5d2SYehuda Sadeh } 11223d14c5d2SYehuda Sadeh 11233d14c5d2SYehuda Sadeh /* 11245aea3dcdSIlya Dryomov * Close the connection and assign any leftover requests to the 11255aea3dcdSIlya Dryomov * homeless session. 11265aea3dcdSIlya Dryomov */ 11275aea3dcdSIlya Dryomov static void close_osd(struct ceph_osd *osd) 11285aea3dcdSIlya Dryomov { 11295aea3dcdSIlya Dryomov struct ceph_osd_client *osdc = osd->o_osdc; 11305aea3dcdSIlya Dryomov struct rb_node *n; 11315aea3dcdSIlya Dryomov 11325aea3dcdSIlya Dryomov verify_osdc_wrlocked(osdc); 11335aea3dcdSIlya Dryomov dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd); 11345aea3dcdSIlya Dryomov 11355aea3dcdSIlya Dryomov ceph_con_close(&osd->o_con); 11365aea3dcdSIlya Dryomov 11375aea3dcdSIlya Dryomov for (n = rb_first(&osd->o_requests); n; ) { 11385aea3dcdSIlya Dryomov struct ceph_osd_request *req = 11395aea3dcdSIlya Dryomov rb_entry(n, struct ceph_osd_request, r_node); 11405aea3dcdSIlya Dryomov 11415aea3dcdSIlya Dryomov n = rb_next(n); /* unlink_request() */ 11425aea3dcdSIlya Dryomov 11435aea3dcdSIlya Dryomov dout(" reassigning req %p tid %llu\n", req, req->r_tid); 11445aea3dcdSIlya Dryomov unlink_request(osd, req); 11455aea3dcdSIlya Dryomov link_request(&osdc->homeless_osd, req); 11465aea3dcdSIlya Dryomov } 1147922dab61SIlya Dryomov for (n = rb_first(&osd->o_linger_requests); n; ) { 1148922dab61SIlya Dryomov struct ceph_osd_linger_request *lreq = 1149922dab61SIlya Dryomov rb_entry(n, struct ceph_osd_linger_request, node); 1150922dab61SIlya Dryomov 1151922dab61SIlya Dryomov n = rb_next(n); /* unlink_linger() */ 1152922dab61SIlya Dryomov 1153922dab61SIlya Dryomov dout(" reassigning lreq %p linger_id %llu\n", lreq, 1154922dab61SIlya Dryomov lreq->linger_id); 1155922dab61SIlya Dryomov unlink_linger(osd, lreq); 1156922dab61SIlya Dryomov link_linger(&osdc->homeless_osd, lreq); 1157922dab61SIlya Dryomov } 1158a02a946dSIlya Dryomov clear_backoffs(osd); 11595aea3dcdSIlya Dryomov 11605aea3dcdSIlya Dryomov __remove_osd_from_lru(osd); 11615aea3dcdSIlya Dryomov erase_osd(&osdc->osds, osd); 11625aea3dcdSIlya Dryomov put_osd(osd); 11635aea3dcdSIlya Dryomov } 11645aea3dcdSIlya Dryomov 11655aea3dcdSIlya Dryomov /* 11663d14c5d2SYehuda Sadeh * reset osd connect 11673d14c5d2SYehuda Sadeh */ 11685aea3dcdSIlya Dryomov static int reopen_osd(struct ceph_osd *osd) 11693d14c5d2SYehuda Sadeh { 1170c3acb181SAlex Elder struct ceph_entity_addr *peer_addr; 11713d14c5d2SYehuda Sadeh 11725aea3dcdSIlya Dryomov dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd); 11735aea3dcdSIlya Dryomov 11745aea3dcdSIlya Dryomov if (RB_EMPTY_ROOT(&osd->o_requests) && 1175922dab61SIlya Dryomov RB_EMPTY_ROOT(&osd->o_linger_requests)) { 11765aea3dcdSIlya Dryomov close_osd(osd); 1177c3acb181SAlex Elder return -ENODEV; 1178c3acb181SAlex Elder } 1179c3acb181SAlex Elder 11805aea3dcdSIlya Dryomov peer_addr = &osd->o_osdc->osdmap->osd_addr[osd->o_osd]; 1181c3acb181SAlex Elder if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) && 11823d14c5d2SYehuda Sadeh !ceph_con_opened(&osd->o_con)) { 11835aea3dcdSIlya Dryomov struct rb_node *n; 1184c3acb181SAlex Elder 11853d14c5d2SYehuda Sadeh dout("osd addr hasn't changed and connection never opened, " 11860b4af2e8SIlya Dryomov "letting msgr retry\n"); 11873d14c5d2SYehuda Sadeh /* touch each r_stamp for handle_timeout()'s benfit */ 11885aea3dcdSIlya Dryomov for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) { 11895aea3dcdSIlya Dryomov struct ceph_osd_request *req = 11905aea3dcdSIlya Dryomov rb_entry(n, struct ceph_osd_request, r_node); 11913d14c5d2SYehuda Sadeh req->r_stamp = jiffies; 11925aea3dcdSIlya Dryomov } 1193c3acb181SAlex Elder 1194c3acb181SAlex Elder return -EAGAIN; 11953d14c5d2SYehuda Sadeh } 1196c3acb181SAlex Elder 1197c3acb181SAlex Elder ceph_con_close(&osd->o_con); 1198c3acb181SAlex Elder ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr); 1199c3acb181SAlex Elder osd->o_incarnation++; 1200c3acb181SAlex Elder 1201c3acb181SAlex Elder return 0; 12023d14c5d2SYehuda Sadeh } 12033d14c5d2SYehuda Sadeh 12045aea3dcdSIlya Dryomov static struct ceph_osd *lookup_create_osd(struct ceph_osd_client *osdc, int o, 12055aea3dcdSIlya Dryomov bool wrlocked) 12063d14c5d2SYehuda Sadeh { 12075aea3dcdSIlya Dryomov struct ceph_osd *osd; 12085aea3dcdSIlya Dryomov 12095aea3dcdSIlya Dryomov if (wrlocked) 12105aea3dcdSIlya Dryomov verify_osdc_wrlocked(osdc); 12115aea3dcdSIlya Dryomov else 12125aea3dcdSIlya Dryomov verify_osdc_locked(osdc); 12135aea3dcdSIlya Dryomov 12145aea3dcdSIlya Dryomov if (o != CEPH_HOMELESS_OSD) 12155aea3dcdSIlya Dryomov osd = lookup_osd(&osdc->osds, o); 12165aea3dcdSIlya Dryomov else 12175aea3dcdSIlya Dryomov osd = &osdc->homeless_osd; 12185aea3dcdSIlya Dryomov if (!osd) { 12195aea3dcdSIlya Dryomov if (!wrlocked) 12205aea3dcdSIlya Dryomov return ERR_PTR(-EAGAIN); 12215aea3dcdSIlya Dryomov 12225aea3dcdSIlya Dryomov osd = create_osd(osdc, o); 12235aea3dcdSIlya Dryomov insert_osd(&osdc->osds, osd); 12245aea3dcdSIlya Dryomov ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, 12255aea3dcdSIlya Dryomov &osdc->osdmap->osd_addr[osd->o_osd]); 12265aea3dcdSIlya Dryomov } 12275aea3dcdSIlya Dryomov 12285aea3dcdSIlya Dryomov dout("%s osdc %p osd%d -> osd %p\n", __func__, osdc, o, osd); 12295aea3dcdSIlya Dryomov return osd; 1230a40c4f10SYehuda Sadeh } 1231a40c4f10SYehuda Sadeh 12323d14c5d2SYehuda Sadeh /* 12335aea3dcdSIlya Dryomov * Create request <-> OSD session relation. 12345aea3dcdSIlya Dryomov * 12355aea3dcdSIlya Dryomov * @req has to be assigned a tid, @osd may be homeless. 12363d14c5d2SYehuda Sadeh */ 12375aea3dcdSIlya Dryomov static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req) 12383d14c5d2SYehuda Sadeh { 12395aea3dcdSIlya Dryomov verify_osd_locked(osd); 12405aea3dcdSIlya Dryomov WARN_ON(!req->r_tid || req->r_osd); 12415aea3dcdSIlya Dryomov dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd, 124235f9f8a0SSage Weil req, req->r_tid); 12435aea3dcdSIlya Dryomov 12445aea3dcdSIlya Dryomov if (!osd_homeless(osd)) 12455aea3dcdSIlya Dryomov __remove_osd_from_lru(osd); 12465aea3dcdSIlya Dryomov else 12475aea3dcdSIlya Dryomov atomic_inc(&osd->o_osdc->num_homeless); 12485aea3dcdSIlya Dryomov 12495aea3dcdSIlya Dryomov get_osd(osd); 12505aea3dcdSIlya Dryomov insert_request(&osd->o_requests, req); 12515aea3dcdSIlya Dryomov req->r_osd = osd; 125235f9f8a0SSage Weil } 125335f9f8a0SSage Weil 12545aea3dcdSIlya Dryomov static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req) 12553d14c5d2SYehuda Sadeh { 12565aea3dcdSIlya Dryomov verify_osd_locked(osd); 12575aea3dcdSIlya Dryomov WARN_ON(req->r_osd != osd); 12585aea3dcdSIlya Dryomov dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd, 12595aea3dcdSIlya Dryomov req, req->r_tid); 12605aea3dcdSIlya Dryomov 12615aea3dcdSIlya Dryomov req->r_osd = NULL; 12625aea3dcdSIlya Dryomov erase_request(&osd->o_requests, req); 12635aea3dcdSIlya Dryomov put_osd(osd); 12645aea3dcdSIlya Dryomov 12655aea3dcdSIlya Dryomov if (!osd_homeless(osd)) 12665aea3dcdSIlya Dryomov maybe_move_osd_to_lru(osd); 12675aea3dcdSIlya Dryomov else 12685aea3dcdSIlya Dryomov atomic_dec(&osd->o_osdc->num_homeless); 12693d14c5d2SYehuda Sadeh } 12703d14c5d2SYehuda Sadeh 127163244fa1SIlya Dryomov static bool __pool_full(struct ceph_pg_pool_info *pi) 127263244fa1SIlya Dryomov { 127363244fa1SIlya Dryomov return pi->flags & CEPH_POOL_FLAG_FULL; 127463244fa1SIlya Dryomov } 127563244fa1SIlya Dryomov 127642c1b124SIlya Dryomov static bool have_pool_full(struct ceph_osd_client *osdc) 127742c1b124SIlya Dryomov { 127842c1b124SIlya Dryomov struct rb_node *n; 127942c1b124SIlya Dryomov 128042c1b124SIlya Dryomov for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) { 128142c1b124SIlya Dryomov struct ceph_pg_pool_info *pi = 128242c1b124SIlya Dryomov rb_entry(n, struct ceph_pg_pool_info, node); 128342c1b124SIlya Dryomov 128442c1b124SIlya Dryomov if (__pool_full(pi)) 128542c1b124SIlya Dryomov return true; 128642c1b124SIlya Dryomov } 128742c1b124SIlya Dryomov 128842c1b124SIlya Dryomov return false; 128942c1b124SIlya Dryomov } 129042c1b124SIlya Dryomov 12915aea3dcdSIlya Dryomov static bool pool_full(struct ceph_osd_client *osdc, s64 pool_id) 12925aea3dcdSIlya Dryomov { 12935aea3dcdSIlya Dryomov struct ceph_pg_pool_info *pi; 12945aea3dcdSIlya Dryomov 12955aea3dcdSIlya Dryomov pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id); 12965aea3dcdSIlya Dryomov if (!pi) 12975aea3dcdSIlya Dryomov return false; 12985aea3dcdSIlya Dryomov 12995aea3dcdSIlya Dryomov return __pool_full(pi); 13005aea3dcdSIlya Dryomov } 13015aea3dcdSIlya Dryomov 13023d14c5d2SYehuda Sadeh /* 1303d29adb34SJosh Durgin * Returns whether a request should be blocked from being sent 1304d29adb34SJosh Durgin * based on the current osdmap and osd_client settings. 1305d29adb34SJosh Durgin */ 130663244fa1SIlya Dryomov static bool target_should_be_paused(struct ceph_osd_client *osdc, 130763244fa1SIlya Dryomov const struct ceph_osd_request_target *t, 130863244fa1SIlya Dryomov struct ceph_pg_pool_info *pi) 130963244fa1SIlya Dryomov { 1310b7ec35b3SIlya Dryomov bool pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD); 1311b7ec35b3SIlya Dryomov bool pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) || 1312b7ec35b3SIlya Dryomov ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) || 131363244fa1SIlya Dryomov __pool_full(pi); 131463244fa1SIlya Dryomov 13156d637a54SIlya Dryomov WARN_ON(pi->id != t->target_oloc.pool); 131658eb7932SJeff Layton return ((t->flags & CEPH_OSD_FLAG_READ) && pauserd) || 131758eb7932SJeff Layton ((t->flags & CEPH_OSD_FLAG_WRITE) && pausewr) || 131858eb7932SJeff Layton (osdc->osdmap->epoch < osdc->epoch_barrier); 131963244fa1SIlya Dryomov } 132063244fa1SIlya Dryomov 132163244fa1SIlya Dryomov enum calc_target_result { 132263244fa1SIlya Dryomov CALC_TARGET_NO_ACTION = 0, 132363244fa1SIlya Dryomov CALC_TARGET_NEED_RESEND, 132463244fa1SIlya Dryomov CALC_TARGET_POOL_DNE, 132563244fa1SIlya Dryomov }; 132663244fa1SIlya Dryomov 132763244fa1SIlya Dryomov static enum calc_target_result calc_target(struct ceph_osd_client *osdc, 132863244fa1SIlya Dryomov struct ceph_osd_request_target *t, 13297de030d6SIlya Dryomov struct ceph_connection *con, 133063244fa1SIlya Dryomov bool any_change) 133163244fa1SIlya Dryomov { 133263244fa1SIlya Dryomov struct ceph_pg_pool_info *pi; 133363244fa1SIlya Dryomov struct ceph_pg pgid, last_pgid; 133463244fa1SIlya Dryomov struct ceph_osds up, acting; 133563244fa1SIlya Dryomov bool force_resend = false; 133684ed45dfSIlya Dryomov bool unpaused = false; 133784ed45dfSIlya Dryomov bool legacy_change; 13387de030d6SIlya Dryomov bool split = false; 1339b7ec35b3SIlya Dryomov bool sort_bitwise = ceph_osdmap_flag(osdc, CEPH_OSDMAP_SORTBITWISE); 134063244fa1SIlya Dryomov enum calc_target_result ct_res; 134163244fa1SIlya Dryomov int ret; 134263244fa1SIlya Dryomov 134304c7d789SIlya Dryomov t->epoch = osdc->osdmap->epoch; 134463244fa1SIlya Dryomov pi = ceph_pg_pool_by_id(osdc->osdmap, t->base_oloc.pool); 134563244fa1SIlya Dryomov if (!pi) { 134663244fa1SIlya Dryomov t->osd = CEPH_HOMELESS_OSD; 134763244fa1SIlya Dryomov ct_res = CALC_TARGET_POOL_DNE; 134863244fa1SIlya Dryomov goto out; 134963244fa1SIlya Dryomov } 135063244fa1SIlya Dryomov 135163244fa1SIlya Dryomov if (osdc->osdmap->epoch == pi->last_force_request_resend) { 1352dc93e0e2SIlya Dryomov if (t->last_force_resend < pi->last_force_request_resend) { 1353dc93e0e2SIlya Dryomov t->last_force_resend = pi->last_force_request_resend; 135463244fa1SIlya Dryomov force_resend = true; 1355dc93e0e2SIlya Dryomov } else if (t->last_force_resend == 0) { 135663244fa1SIlya Dryomov force_resend = true; 135763244fa1SIlya Dryomov } 135863244fa1SIlya Dryomov } 135963244fa1SIlya Dryomov 1360db098ec4SIlya Dryomov /* apply tiering */ 1361db098ec4SIlya Dryomov ceph_oid_copy(&t->target_oid, &t->base_oid); 1362db098ec4SIlya Dryomov ceph_oloc_copy(&t->target_oloc, &t->base_oloc); 1363db098ec4SIlya Dryomov if ((t->flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) { 136463244fa1SIlya Dryomov if (t->flags & CEPH_OSD_FLAG_READ && pi->read_tier >= 0) 136563244fa1SIlya Dryomov t->target_oloc.pool = pi->read_tier; 136663244fa1SIlya Dryomov if (t->flags & CEPH_OSD_FLAG_WRITE && pi->write_tier >= 0) 136763244fa1SIlya Dryomov t->target_oloc.pool = pi->write_tier; 13686d637a54SIlya Dryomov 13696d637a54SIlya Dryomov pi = ceph_pg_pool_by_id(osdc->osdmap, t->target_oloc.pool); 13706d637a54SIlya Dryomov if (!pi) { 13716d637a54SIlya Dryomov t->osd = CEPH_HOMELESS_OSD; 13726d637a54SIlya Dryomov ct_res = CALC_TARGET_POOL_DNE; 13736d637a54SIlya Dryomov goto out; 13746d637a54SIlya Dryomov } 137563244fa1SIlya Dryomov } 137663244fa1SIlya Dryomov 1377df28152dSIlya Dryomov ret = __ceph_object_locator_to_pg(pi, &t->target_oid, &t->target_oloc, 1378df28152dSIlya Dryomov &pgid); 137963244fa1SIlya Dryomov if (ret) { 138063244fa1SIlya Dryomov WARN_ON(ret != -ENOENT); 138163244fa1SIlya Dryomov t->osd = CEPH_HOMELESS_OSD; 138263244fa1SIlya Dryomov ct_res = CALC_TARGET_POOL_DNE; 138363244fa1SIlya Dryomov goto out; 138463244fa1SIlya Dryomov } 138563244fa1SIlya Dryomov last_pgid.pool = pgid.pool; 138663244fa1SIlya Dryomov last_pgid.seed = ceph_stable_mod(pgid.seed, t->pg_num, t->pg_num_mask); 138763244fa1SIlya Dryomov 1388df28152dSIlya Dryomov ceph_pg_to_up_acting_osds(osdc->osdmap, pi, &pgid, &up, &acting); 138963244fa1SIlya Dryomov if (any_change && 139063244fa1SIlya Dryomov ceph_is_new_interval(&t->acting, 139163244fa1SIlya Dryomov &acting, 139263244fa1SIlya Dryomov &t->up, 139363244fa1SIlya Dryomov &up, 139463244fa1SIlya Dryomov t->size, 139563244fa1SIlya Dryomov pi->size, 139663244fa1SIlya Dryomov t->min_size, 139763244fa1SIlya Dryomov pi->min_size, 139863244fa1SIlya Dryomov t->pg_num, 139963244fa1SIlya Dryomov pi->pg_num, 140063244fa1SIlya Dryomov t->sort_bitwise, 140163244fa1SIlya Dryomov sort_bitwise, 140263244fa1SIlya Dryomov &last_pgid)) 140363244fa1SIlya Dryomov force_resend = true; 140463244fa1SIlya Dryomov 140563244fa1SIlya Dryomov if (t->paused && !target_should_be_paused(osdc, t, pi)) { 140663244fa1SIlya Dryomov t->paused = false; 140784ed45dfSIlya Dryomov unpaused = true; 140863244fa1SIlya Dryomov } 140984ed45dfSIlya Dryomov legacy_change = ceph_pg_compare(&t->pgid, &pgid) || 141084ed45dfSIlya Dryomov ceph_osds_changed(&t->acting, &acting, any_change); 14117de030d6SIlya Dryomov if (t->pg_num) 14127de030d6SIlya Dryomov split = ceph_pg_is_split(&last_pgid, t->pg_num, pi->pg_num); 141363244fa1SIlya Dryomov 14147de030d6SIlya Dryomov if (legacy_change || force_resend || split) { 141563244fa1SIlya Dryomov t->pgid = pgid; /* struct */ 1416df28152dSIlya Dryomov ceph_pg_to_primary_shard(osdc->osdmap, pi, &pgid, &t->spgid); 141763244fa1SIlya Dryomov ceph_osds_copy(&t->acting, &acting); 141863244fa1SIlya Dryomov ceph_osds_copy(&t->up, &up); 141963244fa1SIlya Dryomov t->size = pi->size; 142063244fa1SIlya Dryomov t->min_size = pi->min_size; 142163244fa1SIlya Dryomov t->pg_num = pi->pg_num; 142263244fa1SIlya Dryomov t->pg_num_mask = pi->pg_num_mask; 142363244fa1SIlya Dryomov t->sort_bitwise = sort_bitwise; 142463244fa1SIlya Dryomov 142563244fa1SIlya Dryomov t->osd = acting.primary; 142663244fa1SIlya Dryomov } 142763244fa1SIlya Dryomov 14287de030d6SIlya Dryomov if (unpaused || legacy_change || force_resend || 14297de030d6SIlya Dryomov (split && con && CEPH_HAVE_FEATURE(con->peer_features, 14307de030d6SIlya Dryomov RESEND_ON_SPLIT))) 143184ed45dfSIlya Dryomov ct_res = CALC_TARGET_NEED_RESEND; 143284ed45dfSIlya Dryomov else 143384ed45dfSIlya Dryomov ct_res = CALC_TARGET_NO_ACTION; 143484ed45dfSIlya Dryomov 143563244fa1SIlya Dryomov out: 143663244fa1SIlya Dryomov dout("%s t %p -> ct_res %d osd %d\n", __func__, t, ct_res, t->osd); 143763244fa1SIlya Dryomov return ct_res; 143863244fa1SIlya Dryomov } 143963244fa1SIlya Dryomov 1440a02a946dSIlya Dryomov static struct ceph_spg_mapping *alloc_spg_mapping(void) 1441a02a946dSIlya Dryomov { 1442a02a946dSIlya Dryomov struct ceph_spg_mapping *spg; 1443a02a946dSIlya Dryomov 1444a02a946dSIlya Dryomov spg = kmalloc(sizeof(*spg), GFP_NOIO); 1445a02a946dSIlya Dryomov if (!spg) 1446a02a946dSIlya Dryomov return NULL; 1447a02a946dSIlya Dryomov 1448a02a946dSIlya Dryomov RB_CLEAR_NODE(&spg->node); 1449a02a946dSIlya Dryomov spg->backoffs = RB_ROOT; 1450a02a946dSIlya Dryomov return spg; 1451a02a946dSIlya Dryomov } 1452a02a946dSIlya Dryomov 1453a02a946dSIlya Dryomov static void free_spg_mapping(struct ceph_spg_mapping *spg) 1454a02a946dSIlya Dryomov { 1455a02a946dSIlya Dryomov WARN_ON(!RB_EMPTY_NODE(&spg->node)); 1456a02a946dSIlya Dryomov WARN_ON(!RB_EMPTY_ROOT(&spg->backoffs)); 1457a02a946dSIlya Dryomov 1458a02a946dSIlya Dryomov kfree(spg); 1459a02a946dSIlya Dryomov } 1460a02a946dSIlya Dryomov 1461a02a946dSIlya Dryomov /* 1462a02a946dSIlya Dryomov * rbtree of ceph_spg_mapping for handling map<spg_t, ...>, similar to 1463a02a946dSIlya Dryomov * ceph_pg_mapping. Used to track OSD backoffs -- a backoff [range] is 1464a02a946dSIlya Dryomov * defined only within a specific spgid; it does not pass anything to 1465a02a946dSIlya Dryomov * children on split, or to another primary. 1466a02a946dSIlya Dryomov */ 1467a02a946dSIlya Dryomov DEFINE_RB_FUNCS2(spg_mapping, struct ceph_spg_mapping, spgid, ceph_spg_compare, 1468a02a946dSIlya Dryomov RB_BYPTR, const struct ceph_spg *, node) 1469a02a946dSIlya Dryomov 1470a02a946dSIlya Dryomov static u64 hoid_get_bitwise_key(const struct ceph_hobject_id *hoid) 1471a02a946dSIlya Dryomov { 1472a02a946dSIlya Dryomov return hoid->is_max ? 0x100000000ull : hoid->hash_reverse_bits; 1473a02a946dSIlya Dryomov } 1474a02a946dSIlya Dryomov 1475a02a946dSIlya Dryomov static void hoid_get_effective_key(const struct ceph_hobject_id *hoid, 1476a02a946dSIlya Dryomov void **pkey, size_t *pkey_len) 1477a02a946dSIlya Dryomov { 1478a02a946dSIlya Dryomov if (hoid->key_len) { 1479a02a946dSIlya Dryomov *pkey = hoid->key; 1480a02a946dSIlya Dryomov *pkey_len = hoid->key_len; 1481a02a946dSIlya Dryomov } else { 1482a02a946dSIlya Dryomov *pkey = hoid->oid; 1483a02a946dSIlya Dryomov *pkey_len = hoid->oid_len; 1484a02a946dSIlya Dryomov } 1485a02a946dSIlya Dryomov } 1486a02a946dSIlya Dryomov 1487a02a946dSIlya Dryomov static int compare_names(const void *name1, size_t name1_len, 1488a02a946dSIlya Dryomov const void *name2, size_t name2_len) 1489a02a946dSIlya Dryomov { 1490a02a946dSIlya Dryomov int ret; 1491a02a946dSIlya Dryomov 1492a02a946dSIlya Dryomov ret = memcmp(name1, name2, min(name1_len, name2_len)); 1493a02a946dSIlya Dryomov if (!ret) { 1494a02a946dSIlya Dryomov if (name1_len < name2_len) 1495a02a946dSIlya Dryomov ret = -1; 1496a02a946dSIlya Dryomov else if (name1_len > name2_len) 1497a02a946dSIlya Dryomov ret = 1; 1498a02a946dSIlya Dryomov } 1499a02a946dSIlya Dryomov return ret; 1500a02a946dSIlya Dryomov } 1501a02a946dSIlya Dryomov 1502a02a946dSIlya Dryomov static int hoid_compare(const struct ceph_hobject_id *lhs, 1503a02a946dSIlya Dryomov const struct ceph_hobject_id *rhs) 1504a02a946dSIlya Dryomov { 1505a02a946dSIlya Dryomov void *effective_key1, *effective_key2; 1506a02a946dSIlya Dryomov size_t effective_key1_len, effective_key2_len; 1507a02a946dSIlya Dryomov int ret; 1508a02a946dSIlya Dryomov 1509a02a946dSIlya Dryomov if (lhs->is_max < rhs->is_max) 1510a02a946dSIlya Dryomov return -1; 1511a02a946dSIlya Dryomov if (lhs->is_max > rhs->is_max) 1512a02a946dSIlya Dryomov return 1; 1513a02a946dSIlya Dryomov 1514a02a946dSIlya Dryomov if (lhs->pool < rhs->pool) 1515a02a946dSIlya Dryomov return -1; 1516a02a946dSIlya Dryomov if (lhs->pool > rhs->pool) 1517a02a946dSIlya Dryomov return 1; 1518a02a946dSIlya Dryomov 1519a02a946dSIlya Dryomov if (hoid_get_bitwise_key(lhs) < hoid_get_bitwise_key(rhs)) 1520a02a946dSIlya Dryomov return -1; 1521a02a946dSIlya Dryomov if (hoid_get_bitwise_key(lhs) > hoid_get_bitwise_key(rhs)) 1522a02a946dSIlya Dryomov return 1; 1523a02a946dSIlya Dryomov 1524a02a946dSIlya Dryomov ret = compare_names(lhs->nspace, lhs->nspace_len, 1525a02a946dSIlya Dryomov rhs->nspace, rhs->nspace_len); 1526a02a946dSIlya Dryomov if (ret) 1527a02a946dSIlya Dryomov return ret; 1528a02a946dSIlya Dryomov 1529a02a946dSIlya Dryomov hoid_get_effective_key(lhs, &effective_key1, &effective_key1_len); 1530a02a946dSIlya Dryomov hoid_get_effective_key(rhs, &effective_key2, &effective_key2_len); 1531a02a946dSIlya Dryomov ret = compare_names(effective_key1, effective_key1_len, 1532a02a946dSIlya Dryomov effective_key2, effective_key2_len); 1533a02a946dSIlya Dryomov if (ret) 1534a02a946dSIlya Dryomov return ret; 1535a02a946dSIlya Dryomov 1536a02a946dSIlya Dryomov ret = compare_names(lhs->oid, lhs->oid_len, rhs->oid, rhs->oid_len); 1537a02a946dSIlya Dryomov if (ret) 1538a02a946dSIlya Dryomov return ret; 1539a02a946dSIlya Dryomov 1540a02a946dSIlya Dryomov if (lhs->snapid < rhs->snapid) 1541a02a946dSIlya Dryomov return -1; 1542a02a946dSIlya Dryomov if (lhs->snapid > rhs->snapid) 1543a02a946dSIlya Dryomov return 1; 1544a02a946dSIlya Dryomov 1545a02a946dSIlya Dryomov return 0; 1546a02a946dSIlya Dryomov } 1547a02a946dSIlya Dryomov 1548a02a946dSIlya Dryomov /* 1549a02a946dSIlya Dryomov * For decoding ->begin and ->end of MOSDBackoff only -- no MIN/MAX 1550a02a946dSIlya Dryomov * compat stuff here. 1551a02a946dSIlya Dryomov * 1552a02a946dSIlya Dryomov * Assumes @hoid is zero-initialized. 1553a02a946dSIlya Dryomov */ 1554a02a946dSIlya Dryomov static int decode_hoid(void **p, void *end, struct ceph_hobject_id *hoid) 1555a02a946dSIlya Dryomov { 1556a02a946dSIlya Dryomov u8 struct_v; 1557a02a946dSIlya Dryomov u32 struct_len; 1558a02a946dSIlya Dryomov int ret; 1559a02a946dSIlya Dryomov 1560a02a946dSIlya Dryomov ret = ceph_start_decoding(p, end, 4, "hobject_t", &struct_v, 1561a02a946dSIlya Dryomov &struct_len); 1562a02a946dSIlya Dryomov if (ret) 1563a02a946dSIlya Dryomov return ret; 1564a02a946dSIlya Dryomov 1565a02a946dSIlya Dryomov if (struct_v < 4) { 1566a02a946dSIlya Dryomov pr_err("got struct_v %d < 4 of hobject_t\n", struct_v); 1567a02a946dSIlya Dryomov goto e_inval; 1568a02a946dSIlya Dryomov } 1569a02a946dSIlya Dryomov 1570a02a946dSIlya Dryomov hoid->key = ceph_extract_encoded_string(p, end, &hoid->key_len, 1571a02a946dSIlya Dryomov GFP_NOIO); 1572a02a946dSIlya Dryomov if (IS_ERR(hoid->key)) { 1573a02a946dSIlya Dryomov ret = PTR_ERR(hoid->key); 1574a02a946dSIlya Dryomov hoid->key = NULL; 1575a02a946dSIlya Dryomov return ret; 1576a02a946dSIlya Dryomov } 1577a02a946dSIlya Dryomov 1578a02a946dSIlya Dryomov hoid->oid = ceph_extract_encoded_string(p, end, &hoid->oid_len, 1579a02a946dSIlya Dryomov GFP_NOIO); 1580a02a946dSIlya Dryomov if (IS_ERR(hoid->oid)) { 1581a02a946dSIlya Dryomov ret = PTR_ERR(hoid->oid); 1582a02a946dSIlya Dryomov hoid->oid = NULL; 1583a02a946dSIlya Dryomov return ret; 1584a02a946dSIlya Dryomov } 1585a02a946dSIlya Dryomov 1586a02a946dSIlya Dryomov ceph_decode_64_safe(p, end, hoid->snapid, e_inval); 1587a02a946dSIlya Dryomov ceph_decode_32_safe(p, end, hoid->hash, e_inval); 1588a02a946dSIlya Dryomov ceph_decode_8_safe(p, end, hoid->is_max, e_inval); 1589a02a946dSIlya Dryomov 1590a02a946dSIlya Dryomov hoid->nspace = ceph_extract_encoded_string(p, end, &hoid->nspace_len, 1591a02a946dSIlya Dryomov GFP_NOIO); 1592a02a946dSIlya Dryomov if (IS_ERR(hoid->nspace)) { 1593a02a946dSIlya Dryomov ret = PTR_ERR(hoid->nspace); 1594a02a946dSIlya Dryomov hoid->nspace = NULL; 1595a02a946dSIlya Dryomov return ret; 1596a02a946dSIlya Dryomov } 1597a02a946dSIlya Dryomov 1598a02a946dSIlya Dryomov ceph_decode_64_safe(p, end, hoid->pool, e_inval); 1599a02a946dSIlya Dryomov 1600a02a946dSIlya Dryomov ceph_hoid_build_hash_cache(hoid); 1601a02a946dSIlya Dryomov return 0; 1602a02a946dSIlya Dryomov 1603a02a946dSIlya Dryomov e_inval: 1604a02a946dSIlya Dryomov return -EINVAL; 1605a02a946dSIlya Dryomov } 1606a02a946dSIlya Dryomov 1607a02a946dSIlya Dryomov static int hoid_encoding_size(const struct ceph_hobject_id *hoid) 1608a02a946dSIlya Dryomov { 1609a02a946dSIlya Dryomov return 8 + 4 + 1 + 8 + /* snapid, hash, is_max, pool */ 1610a02a946dSIlya Dryomov 4 + hoid->key_len + 4 + hoid->oid_len + 4 + hoid->nspace_len; 1611a02a946dSIlya Dryomov } 1612a02a946dSIlya Dryomov 1613a02a946dSIlya Dryomov static void encode_hoid(void **p, void *end, const struct ceph_hobject_id *hoid) 1614a02a946dSIlya Dryomov { 1615a02a946dSIlya Dryomov ceph_start_encoding(p, 4, 3, hoid_encoding_size(hoid)); 1616a02a946dSIlya Dryomov ceph_encode_string(p, end, hoid->key, hoid->key_len); 1617a02a946dSIlya Dryomov ceph_encode_string(p, end, hoid->oid, hoid->oid_len); 1618a02a946dSIlya Dryomov ceph_encode_64(p, hoid->snapid); 1619a02a946dSIlya Dryomov ceph_encode_32(p, hoid->hash); 1620a02a946dSIlya Dryomov ceph_encode_8(p, hoid->is_max); 1621a02a946dSIlya Dryomov ceph_encode_string(p, end, hoid->nspace, hoid->nspace_len); 1622a02a946dSIlya Dryomov ceph_encode_64(p, hoid->pool); 1623a02a946dSIlya Dryomov } 1624a02a946dSIlya Dryomov 1625a02a946dSIlya Dryomov static void free_hoid(struct ceph_hobject_id *hoid) 1626a02a946dSIlya Dryomov { 1627a02a946dSIlya Dryomov if (hoid) { 1628a02a946dSIlya Dryomov kfree(hoid->key); 1629a02a946dSIlya Dryomov kfree(hoid->oid); 1630a02a946dSIlya Dryomov kfree(hoid->nspace); 1631a02a946dSIlya Dryomov kfree(hoid); 1632a02a946dSIlya Dryomov } 1633a02a946dSIlya Dryomov } 1634a02a946dSIlya Dryomov 1635a02a946dSIlya Dryomov static struct ceph_osd_backoff *alloc_backoff(void) 1636a02a946dSIlya Dryomov { 1637a02a946dSIlya Dryomov struct ceph_osd_backoff *backoff; 1638a02a946dSIlya Dryomov 1639a02a946dSIlya Dryomov backoff = kzalloc(sizeof(*backoff), GFP_NOIO); 1640a02a946dSIlya Dryomov if (!backoff) 1641a02a946dSIlya Dryomov return NULL; 1642a02a946dSIlya Dryomov 1643a02a946dSIlya Dryomov RB_CLEAR_NODE(&backoff->spg_node); 1644a02a946dSIlya Dryomov RB_CLEAR_NODE(&backoff->id_node); 1645a02a946dSIlya Dryomov return backoff; 1646a02a946dSIlya Dryomov } 1647a02a946dSIlya Dryomov 1648a02a946dSIlya Dryomov static void free_backoff(struct ceph_osd_backoff *backoff) 1649a02a946dSIlya Dryomov { 1650a02a946dSIlya Dryomov WARN_ON(!RB_EMPTY_NODE(&backoff->spg_node)); 1651a02a946dSIlya Dryomov WARN_ON(!RB_EMPTY_NODE(&backoff->id_node)); 1652a02a946dSIlya Dryomov 1653a02a946dSIlya Dryomov free_hoid(backoff->begin); 1654a02a946dSIlya Dryomov free_hoid(backoff->end); 1655a02a946dSIlya Dryomov kfree(backoff); 1656a02a946dSIlya Dryomov } 1657a02a946dSIlya Dryomov 1658a02a946dSIlya Dryomov /* 1659a02a946dSIlya Dryomov * Within a specific spgid, backoffs are managed by ->begin hoid. 1660a02a946dSIlya Dryomov */ 1661a02a946dSIlya Dryomov DEFINE_RB_INSDEL_FUNCS2(backoff, struct ceph_osd_backoff, begin, hoid_compare, 1662a02a946dSIlya Dryomov RB_BYVAL, spg_node); 1663a02a946dSIlya Dryomov 1664a02a946dSIlya Dryomov static struct ceph_osd_backoff *lookup_containing_backoff(struct rb_root *root, 1665a02a946dSIlya Dryomov const struct ceph_hobject_id *hoid) 1666a02a946dSIlya Dryomov { 1667a02a946dSIlya Dryomov struct rb_node *n = root->rb_node; 1668a02a946dSIlya Dryomov 1669a02a946dSIlya Dryomov while (n) { 1670a02a946dSIlya Dryomov struct ceph_osd_backoff *cur = 1671a02a946dSIlya Dryomov rb_entry(n, struct ceph_osd_backoff, spg_node); 1672a02a946dSIlya Dryomov int cmp; 1673a02a946dSIlya Dryomov 1674a02a946dSIlya Dryomov cmp = hoid_compare(hoid, cur->begin); 1675a02a946dSIlya Dryomov if (cmp < 0) { 1676a02a946dSIlya Dryomov n = n->rb_left; 1677a02a946dSIlya Dryomov } else if (cmp > 0) { 1678a02a946dSIlya Dryomov if (hoid_compare(hoid, cur->end) < 0) 1679a02a946dSIlya Dryomov return cur; 1680a02a946dSIlya Dryomov 1681a02a946dSIlya Dryomov n = n->rb_right; 1682a02a946dSIlya Dryomov } else { 1683a02a946dSIlya Dryomov return cur; 1684a02a946dSIlya Dryomov } 1685a02a946dSIlya Dryomov } 1686a02a946dSIlya Dryomov 1687a02a946dSIlya Dryomov return NULL; 1688a02a946dSIlya Dryomov } 1689a02a946dSIlya Dryomov 1690a02a946dSIlya Dryomov /* 1691a02a946dSIlya Dryomov * Each backoff has a unique id within its OSD session. 1692a02a946dSIlya Dryomov */ 1693a02a946dSIlya Dryomov DEFINE_RB_FUNCS(backoff_by_id, struct ceph_osd_backoff, id, id_node) 1694a02a946dSIlya Dryomov 1695a02a946dSIlya Dryomov static void clear_backoffs(struct ceph_osd *osd) 1696a02a946dSIlya Dryomov { 1697a02a946dSIlya Dryomov while (!RB_EMPTY_ROOT(&osd->o_backoff_mappings)) { 1698a02a946dSIlya Dryomov struct ceph_spg_mapping *spg = 1699a02a946dSIlya Dryomov rb_entry(rb_first(&osd->o_backoff_mappings), 1700a02a946dSIlya Dryomov struct ceph_spg_mapping, node); 1701a02a946dSIlya Dryomov 1702a02a946dSIlya Dryomov while (!RB_EMPTY_ROOT(&spg->backoffs)) { 1703a02a946dSIlya Dryomov struct ceph_osd_backoff *backoff = 1704a02a946dSIlya Dryomov rb_entry(rb_first(&spg->backoffs), 1705a02a946dSIlya Dryomov struct ceph_osd_backoff, spg_node); 1706a02a946dSIlya Dryomov 1707a02a946dSIlya Dryomov erase_backoff(&spg->backoffs, backoff); 1708a02a946dSIlya Dryomov erase_backoff_by_id(&osd->o_backoffs_by_id, backoff); 1709a02a946dSIlya Dryomov free_backoff(backoff); 1710a02a946dSIlya Dryomov } 1711a02a946dSIlya Dryomov erase_spg_mapping(&osd->o_backoff_mappings, spg); 1712a02a946dSIlya Dryomov free_spg_mapping(spg); 1713a02a946dSIlya Dryomov } 1714a02a946dSIlya Dryomov } 1715a02a946dSIlya Dryomov 1716a02a946dSIlya Dryomov /* 1717a02a946dSIlya Dryomov * Set up a temporary, non-owning view into @t. 1718a02a946dSIlya Dryomov */ 1719a02a946dSIlya Dryomov static void hoid_fill_from_target(struct ceph_hobject_id *hoid, 1720a02a946dSIlya Dryomov const struct ceph_osd_request_target *t) 1721a02a946dSIlya Dryomov { 1722a02a946dSIlya Dryomov hoid->key = NULL; 1723a02a946dSIlya Dryomov hoid->key_len = 0; 1724a02a946dSIlya Dryomov hoid->oid = t->target_oid.name; 1725a02a946dSIlya Dryomov hoid->oid_len = t->target_oid.name_len; 1726a02a946dSIlya Dryomov hoid->snapid = CEPH_NOSNAP; 1727a02a946dSIlya Dryomov hoid->hash = t->pgid.seed; 1728a02a946dSIlya Dryomov hoid->is_max = false; 1729a02a946dSIlya Dryomov if (t->target_oloc.pool_ns) { 1730a02a946dSIlya Dryomov hoid->nspace = t->target_oloc.pool_ns->str; 1731a02a946dSIlya Dryomov hoid->nspace_len = t->target_oloc.pool_ns->len; 1732a02a946dSIlya Dryomov } else { 1733a02a946dSIlya Dryomov hoid->nspace = NULL; 1734a02a946dSIlya Dryomov hoid->nspace_len = 0; 1735a02a946dSIlya Dryomov } 1736a02a946dSIlya Dryomov hoid->pool = t->target_oloc.pool; 1737a02a946dSIlya Dryomov ceph_hoid_build_hash_cache(hoid); 1738a02a946dSIlya Dryomov } 1739a02a946dSIlya Dryomov 1740a02a946dSIlya Dryomov static bool should_plug_request(struct ceph_osd_request *req) 1741a02a946dSIlya Dryomov { 1742a02a946dSIlya Dryomov struct ceph_osd *osd = req->r_osd; 1743a02a946dSIlya Dryomov struct ceph_spg_mapping *spg; 1744a02a946dSIlya Dryomov struct ceph_osd_backoff *backoff; 1745a02a946dSIlya Dryomov struct ceph_hobject_id hoid; 1746a02a946dSIlya Dryomov 1747a02a946dSIlya Dryomov spg = lookup_spg_mapping(&osd->o_backoff_mappings, &req->r_t.spgid); 1748a02a946dSIlya Dryomov if (!spg) 1749a02a946dSIlya Dryomov return false; 1750a02a946dSIlya Dryomov 1751a02a946dSIlya Dryomov hoid_fill_from_target(&hoid, &req->r_t); 1752a02a946dSIlya Dryomov backoff = lookup_containing_backoff(&spg->backoffs, &hoid); 1753a02a946dSIlya Dryomov if (!backoff) 1754a02a946dSIlya Dryomov return false; 1755a02a946dSIlya Dryomov 1756a02a946dSIlya Dryomov dout("%s req %p tid %llu backoff osd%d spgid %llu.%xs%d id %llu\n", 1757a02a946dSIlya Dryomov __func__, req, req->r_tid, osd->o_osd, backoff->spgid.pgid.pool, 1758a02a946dSIlya Dryomov backoff->spgid.pgid.seed, backoff->spgid.shard, backoff->id); 1759a02a946dSIlya Dryomov return true; 1760a02a946dSIlya Dryomov } 1761a02a946dSIlya Dryomov 1762bb873b53SIlya Dryomov static void setup_request_data(struct ceph_osd_request *req, 1763bb873b53SIlya Dryomov struct ceph_msg *msg) 17643d14c5d2SYehuda Sadeh { 1765bb873b53SIlya Dryomov u32 data_len = 0; 1766bb873b53SIlya Dryomov int i; 17673d14c5d2SYehuda Sadeh 1768bb873b53SIlya Dryomov if (!list_empty(&msg->data)) 1769bb873b53SIlya Dryomov return; 17703d14c5d2SYehuda Sadeh 1771bb873b53SIlya Dryomov WARN_ON(msg->data_length); 1772bb873b53SIlya Dryomov for (i = 0; i < req->r_num_ops; i++) { 1773bb873b53SIlya Dryomov struct ceph_osd_req_op *op = &req->r_ops[i]; 1774bb873b53SIlya Dryomov 1775bb873b53SIlya Dryomov switch (op->op) { 1776bb873b53SIlya Dryomov /* request */ 1777bb873b53SIlya Dryomov case CEPH_OSD_OP_WRITE: 1778bb873b53SIlya Dryomov case CEPH_OSD_OP_WRITEFULL: 1779bb873b53SIlya Dryomov WARN_ON(op->indata_len != op->extent.length); 1780bb873b53SIlya Dryomov ceph_osdc_msg_data_add(msg, &op->extent.osd_data); 1781bb873b53SIlya Dryomov break; 1782bb873b53SIlya Dryomov case CEPH_OSD_OP_SETXATTR: 1783bb873b53SIlya Dryomov case CEPH_OSD_OP_CMPXATTR: 1784bb873b53SIlya Dryomov WARN_ON(op->indata_len != op->xattr.name_len + 1785bb873b53SIlya Dryomov op->xattr.value_len); 1786bb873b53SIlya Dryomov ceph_osdc_msg_data_add(msg, &op->xattr.osd_data); 1787bb873b53SIlya Dryomov break; 1788922dab61SIlya Dryomov case CEPH_OSD_OP_NOTIFY_ACK: 1789922dab61SIlya Dryomov ceph_osdc_msg_data_add(msg, 1790922dab61SIlya Dryomov &op->notify_ack.request_data); 1791922dab61SIlya Dryomov break; 1792bb873b53SIlya Dryomov 1793bb873b53SIlya Dryomov /* reply */ 1794bb873b53SIlya Dryomov case CEPH_OSD_OP_STAT: 1795bb873b53SIlya Dryomov ceph_osdc_msg_data_add(req->r_reply, 1796bb873b53SIlya Dryomov &op->raw_data_in); 1797bb873b53SIlya Dryomov break; 1798bb873b53SIlya Dryomov case CEPH_OSD_OP_READ: 1799bb873b53SIlya Dryomov ceph_osdc_msg_data_add(req->r_reply, 1800bb873b53SIlya Dryomov &op->extent.osd_data); 1801bb873b53SIlya Dryomov break; 1802a4ed38d7SDouglas Fuller case CEPH_OSD_OP_LIST_WATCHERS: 1803a4ed38d7SDouglas Fuller ceph_osdc_msg_data_add(req->r_reply, 1804a4ed38d7SDouglas Fuller &op->list_watchers.response_data); 1805a4ed38d7SDouglas Fuller break; 1806bb873b53SIlya Dryomov 1807bb873b53SIlya Dryomov /* both */ 1808bb873b53SIlya Dryomov case CEPH_OSD_OP_CALL: 1809bb873b53SIlya Dryomov WARN_ON(op->indata_len != op->cls.class_len + 1810bb873b53SIlya Dryomov op->cls.method_len + 1811bb873b53SIlya Dryomov op->cls.indata_len); 1812bb873b53SIlya Dryomov ceph_osdc_msg_data_add(msg, &op->cls.request_info); 1813bb873b53SIlya Dryomov /* optional, can be NONE */ 1814bb873b53SIlya Dryomov ceph_osdc_msg_data_add(msg, &op->cls.request_data); 1815bb873b53SIlya Dryomov /* optional, can be NONE */ 1816bb873b53SIlya Dryomov ceph_osdc_msg_data_add(req->r_reply, 1817bb873b53SIlya Dryomov &op->cls.response_data); 1818bb873b53SIlya Dryomov break; 181919079203SIlya Dryomov case CEPH_OSD_OP_NOTIFY: 182019079203SIlya Dryomov ceph_osdc_msg_data_add(msg, 182119079203SIlya Dryomov &op->notify.request_data); 182219079203SIlya Dryomov ceph_osdc_msg_data_add(req->r_reply, 182319079203SIlya Dryomov &op->notify.response_data); 182419079203SIlya Dryomov break; 1825bb873b53SIlya Dryomov } 1826bb873b53SIlya Dryomov 1827bb873b53SIlya Dryomov data_len += op->indata_len; 1828bb873b53SIlya Dryomov } 1829bb873b53SIlya Dryomov 1830bb873b53SIlya Dryomov WARN_ON(data_len != msg->data_length); 1831bb873b53SIlya Dryomov } 1832bb873b53SIlya Dryomov 18332e59ffd1SIlya Dryomov static void encode_pgid(void **p, const struct ceph_pg *pgid) 18342e59ffd1SIlya Dryomov { 18352e59ffd1SIlya Dryomov ceph_encode_8(p, 1); 18362e59ffd1SIlya Dryomov ceph_encode_64(p, pgid->pool); 18372e59ffd1SIlya Dryomov ceph_encode_32(p, pgid->seed); 18382e59ffd1SIlya Dryomov ceph_encode_32(p, -1); /* preferred */ 18392e59ffd1SIlya Dryomov } 18402e59ffd1SIlya Dryomov 18418cb441c0SIlya Dryomov static void encode_spgid(void **p, const struct ceph_spg *spgid) 18428cb441c0SIlya Dryomov { 18438cb441c0SIlya Dryomov ceph_start_encoding(p, 1, 1, CEPH_PGID_ENCODING_LEN + 1); 18448cb441c0SIlya Dryomov encode_pgid(p, &spgid->pgid); 18458cb441c0SIlya Dryomov ceph_encode_8(p, spgid->shard); 18468cb441c0SIlya Dryomov } 18478cb441c0SIlya Dryomov 18482e59ffd1SIlya Dryomov static void encode_oloc(void **p, void *end, 18492e59ffd1SIlya Dryomov const struct ceph_object_locator *oloc) 18502e59ffd1SIlya Dryomov { 18512e59ffd1SIlya Dryomov ceph_start_encoding(p, 5, 4, ceph_oloc_encoding_size(oloc)); 18522e59ffd1SIlya Dryomov ceph_encode_64(p, oloc->pool); 18532e59ffd1SIlya Dryomov ceph_encode_32(p, -1); /* preferred */ 18542e59ffd1SIlya Dryomov ceph_encode_32(p, 0); /* key len */ 18552e59ffd1SIlya Dryomov if (oloc->pool_ns) 18562e59ffd1SIlya Dryomov ceph_encode_string(p, end, oloc->pool_ns->str, 18572e59ffd1SIlya Dryomov oloc->pool_ns->len); 18582e59ffd1SIlya Dryomov else 18592e59ffd1SIlya Dryomov ceph_encode_32(p, 0); 18602e59ffd1SIlya Dryomov } 18612e59ffd1SIlya Dryomov 18628cb441c0SIlya Dryomov static void encode_request_partial(struct ceph_osd_request *req, 18638cb441c0SIlya Dryomov struct ceph_msg *msg) 1864bb873b53SIlya Dryomov { 1865bb873b53SIlya Dryomov void *p = msg->front.iov_base; 1866bb873b53SIlya Dryomov void *const end = p + msg->front_alloc_len; 1867bb873b53SIlya Dryomov u32 data_len = 0; 1868bb873b53SIlya Dryomov int i; 1869bb873b53SIlya Dryomov 1870bb873b53SIlya Dryomov if (req->r_flags & CEPH_OSD_FLAG_WRITE) { 1871bb873b53SIlya Dryomov /* snapshots aren't writeable */ 1872bb873b53SIlya Dryomov WARN_ON(req->r_snapid != CEPH_NOSNAP); 1873bb873b53SIlya Dryomov } else { 1874bb873b53SIlya Dryomov WARN_ON(req->r_mtime.tv_sec || req->r_mtime.tv_nsec || 1875bb873b53SIlya Dryomov req->r_data_offset || req->r_snapc); 1876bb873b53SIlya Dryomov } 1877bb873b53SIlya Dryomov 1878bb873b53SIlya Dryomov setup_request_data(req, msg); 1879bb873b53SIlya Dryomov 18808cb441c0SIlya Dryomov encode_spgid(&p, &req->r_t.spgid); /* actual spg */ 18818cb441c0SIlya Dryomov ceph_encode_32(&p, req->r_t.pgid.seed); /* raw hash */ 1882bb873b53SIlya Dryomov ceph_encode_32(&p, req->r_osdc->osdmap->epoch); 1883bb873b53SIlya Dryomov ceph_encode_32(&p, req->r_flags); 18848cb441c0SIlya Dryomov 18858cb441c0SIlya Dryomov /* reqid */ 18868cb441c0SIlya Dryomov ceph_start_encoding(&p, 2, 2, sizeof(struct ceph_osd_reqid)); 18878cb441c0SIlya Dryomov memset(p, 0, sizeof(struct ceph_osd_reqid)); 18888cb441c0SIlya Dryomov p += sizeof(struct ceph_osd_reqid); 18898cb441c0SIlya Dryomov 18908cb441c0SIlya Dryomov /* trace */ 18918cb441c0SIlya Dryomov memset(p, 0, sizeof(struct ceph_blkin_trace_info)); 18928cb441c0SIlya Dryomov p += sizeof(struct ceph_blkin_trace_info); 18938cb441c0SIlya Dryomov 18948cb441c0SIlya Dryomov ceph_encode_32(&p, 0); /* client_inc, always 0 */ 1895bb873b53SIlya Dryomov ceph_encode_timespec(p, &req->r_mtime); 1896bb873b53SIlya Dryomov p += sizeof(struct ceph_timespec); 1897aa26d662SJeff Layton 18982e59ffd1SIlya Dryomov encode_oloc(&p, end, &req->r_t.target_oloc); 18992e59ffd1SIlya Dryomov ceph_encode_string(&p, end, req->r_t.target_oid.name, 19002e59ffd1SIlya Dryomov req->r_t.target_oid.name_len); 1901bb873b53SIlya Dryomov 1902bb873b53SIlya Dryomov /* ops, can imply data */ 1903bb873b53SIlya Dryomov ceph_encode_16(&p, req->r_num_ops); 1904bb873b53SIlya Dryomov for (i = 0; i < req->r_num_ops; i++) { 1905bb873b53SIlya Dryomov data_len += osd_req_encode_op(p, &req->r_ops[i]); 1906bb873b53SIlya Dryomov p += sizeof(struct ceph_osd_op); 1907bb873b53SIlya Dryomov } 1908bb873b53SIlya Dryomov 1909bb873b53SIlya Dryomov ceph_encode_64(&p, req->r_snapid); /* snapid */ 1910bb873b53SIlya Dryomov if (req->r_snapc) { 1911bb873b53SIlya Dryomov ceph_encode_64(&p, req->r_snapc->seq); 1912bb873b53SIlya Dryomov ceph_encode_32(&p, req->r_snapc->num_snaps); 1913bb873b53SIlya Dryomov for (i = 0; i < req->r_snapc->num_snaps; i++) 1914bb873b53SIlya Dryomov ceph_encode_64(&p, req->r_snapc->snaps[i]); 1915bb873b53SIlya Dryomov } else { 1916bb873b53SIlya Dryomov ceph_encode_64(&p, 0); /* snap_seq */ 1917bb873b53SIlya Dryomov ceph_encode_32(&p, 0); /* snaps len */ 1918bb873b53SIlya Dryomov } 1919bb873b53SIlya Dryomov 1920bb873b53SIlya Dryomov ceph_encode_32(&p, req->r_attempts); /* retry_attempt */ 1921986e8989SIlya Dryomov BUG_ON(p > end - 8); /* space for features */ 1922bb873b53SIlya Dryomov 19238cb441c0SIlya Dryomov msg->hdr.version = cpu_to_le16(8); /* MOSDOp v8 */ 19248cb441c0SIlya Dryomov /* front_len is finalized in encode_request_finish() */ 1925986e8989SIlya Dryomov msg->front.iov_len = p - msg->front.iov_base; 1926986e8989SIlya Dryomov msg->hdr.front_len = cpu_to_le32(msg->front.iov_len); 1927bb873b53SIlya Dryomov msg->hdr.data_len = cpu_to_le32(data_len); 1928bb873b53SIlya Dryomov /* 1929bb873b53SIlya Dryomov * The header "data_off" is a hint to the receiver allowing it 1930bb873b53SIlya Dryomov * to align received data into its buffers such that there's no 1931bb873b53SIlya Dryomov * need to re-copy it before writing it to disk (direct I/O). 1932bb873b53SIlya Dryomov */ 1933bb873b53SIlya Dryomov msg->hdr.data_off = cpu_to_le16(req->r_data_offset); 1934bb873b53SIlya Dryomov 19358cb441c0SIlya Dryomov dout("%s req %p msg %p oid %s oid_len %d\n", __func__, req, msg, 19368cb441c0SIlya Dryomov req->r_t.target_oid.name, req->r_t.target_oid.name_len); 19378cb441c0SIlya Dryomov } 19388cb441c0SIlya Dryomov 19398cb441c0SIlya Dryomov static void encode_request_finish(struct ceph_msg *msg) 19408cb441c0SIlya Dryomov { 19418cb441c0SIlya Dryomov void *p = msg->front.iov_base; 1942986e8989SIlya Dryomov void *const partial_end = p + msg->front.iov_len; 19438cb441c0SIlya Dryomov void *const end = p + msg->front_alloc_len; 19448cb441c0SIlya Dryomov 19458cb441c0SIlya Dryomov if (CEPH_HAVE_FEATURE(msg->con->peer_features, RESEND_ON_SPLIT)) { 19468cb441c0SIlya Dryomov /* luminous OSD -- encode features and be done */ 1947986e8989SIlya Dryomov p = partial_end; 19488cb441c0SIlya Dryomov ceph_encode_64(&p, msg->con->peer_features); 19498cb441c0SIlya Dryomov } else { 19508cb441c0SIlya Dryomov struct { 19518cb441c0SIlya Dryomov char spgid[CEPH_ENCODING_START_BLK_LEN + 19528cb441c0SIlya Dryomov CEPH_PGID_ENCODING_LEN + 1]; 19538cb441c0SIlya Dryomov __le32 hash; 19548cb441c0SIlya Dryomov __le32 epoch; 19558cb441c0SIlya Dryomov __le32 flags; 19568cb441c0SIlya Dryomov char reqid[CEPH_ENCODING_START_BLK_LEN + 19578cb441c0SIlya Dryomov sizeof(struct ceph_osd_reqid)]; 19588cb441c0SIlya Dryomov char trace[sizeof(struct ceph_blkin_trace_info)]; 19598cb441c0SIlya Dryomov __le32 client_inc; 19608cb441c0SIlya Dryomov struct ceph_timespec mtime; 19618cb441c0SIlya Dryomov } __packed head; 19628cb441c0SIlya Dryomov struct ceph_pg pgid; 19638cb441c0SIlya Dryomov void *oloc, *oid, *tail; 19648cb441c0SIlya Dryomov int oloc_len, oid_len, tail_len; 19658cb441c0SIlya Dryomov int len; 19668cb441c0SIlya Dryomov 19678cb441c0SIlya Dryomov /* 19688cb441c0SIlya Dryomov * Pre-luminous OSD -- reencode v8 into v4 using @head 19698cb441c0SIlya Dryomov * as a temporary buffer. Encode the raw PG; the rest 19708cb441c0SIlya Dryomov * is just a matter of moving oloc, oid and tail blobs 19718cb441c0SIlya Dryomov * around. 19728cb441c0SIlya Dryomov */ 19738cb441c0SIlya Dryomov memcpy(&head, p, sizeof(head)); 19748cb441c0SIlya Dryomov p += sizeof(head); 19758cb441c0SIlya Dryomov 19768cb441c0SIlya Dryomov oloc = p; 19778cb441c0SIlya Dryomov p += CEPH_ENCODING_START_BLK_LEN; 19788cb441c0SIlya Dryomov pgid.pool = ceph_decode_64(&p); 19798cb441c0SIlya Dryomov p += 4 + 4; /* preferred, key len */ 19808cb441c0SIlya Dryomov len = ceph_decode_32(&p); 19818cb441c0SIlya Dryomov p += len; /* nspace */ 19828cb441c0SIlya Dryomov oloc_len = p - oloc; 19838cb441c0SIlya Dryomov 19848cb441c0SIlya Dryomov oid = p; 19858cb441c0SIlya Dryomov len = ceph_decode_32(&p); 19868cb441c0SIlya Dryomov p += len; 19878cb441c0SIlya Dryomov oid_len = p - oid; 19888cb441c0SIlya Dryomov 19898cb441c0SIlya Dryomov tail = p; 1990986e8989SIlya Dryomov tail_len = partial_end - p; 19918cb441c0SIlya Dryomov 19928cb441c0SIlya Dryomov p = msg->front.iov_base; 19938cb441c0SIlya Dryomov ceph_encode_copy(&p, &head.client_inc, sizeof(head.client_inc)); 19948cb441c0SIlya Dryomov ceph_encode_copy(&p, &head.epoch, sizeof(head.epoch)); 19958cb441c0SIlya Dryomov ceph_encode_copy(&p, &head.flags, sizeof(head.flags)); 19968cb441c0SIlya Dryomov ceph_encode_copy(&p, &head.mtime, sizeof(head.mtime)); 19978cb441c0SIlya Dryomov 19988cb441c0SIlya Dryomov /* reassert_version */ 19998cb441c0SIlya Dryomov memset(p, 0, sizeof(struct ceph_eversion)); 20008cb441c0SIlya Dryomov p += sizeof(struct ceph_eversion); 20018cb441c0SIlya Dryomov 20028cb441c0SIlya Dryomov BUG_ON(p >= oloc); 20038cb441c0SIlya Dryomov memmove(p, oloc, oloc_len); 20048cb441c0SIlya Dryomov p += oloc_len; 20058cb441c0SIlya Dryomov 20068cb441c0SIlya Dryomov pgid.seed = le32_to_cpu(head.hash); 20078cb441c0SIlya Dryomov encode_pgid(&p, &pgid); /* raw pg */ 20088cb441c0SIlya Dryomov 20098cb441c0SIlya Dryomov BUG_ON(p >= oid); 20108cb441c0SIlya Dryomov memmove(p, oid, oid_len); 20118cb441c0SIlya Dryomov p += oid_len; 20128cb441c0SIlya Dryomov 20138cb441c0SIlya Dryomov /* tail -- ops, snapid, snapc, retry_attempt */ 20148cb441c0SIlya Dryomov BUG_ON(p >= tail); 20158cb441c0SIlya Dryomov memmove(p, tail, tail_len); 20168cb441c0SIlya Dryomov p += tail_len; 20178cb441c0SIlya Dryomov 20188cb441c0SIlya Dryomov msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */ 20198cb441c0SIlya Dryomov } 20208cb441c0SIlya Dryomov 20218cb441c0SIlya Dryomov BUG_ON(p > end); 20228cb441c0SIlya Dryomov msg->front.iov_len = p - msg->front.iov_base; 20238cb441c0SIlya Dryomov msg->hdr.front_len = cpu_to_le32(msg->front.iov_len); 20248cb441c0SIlya Dryomov 20258cb441c0SIlya Dryomov dout("%s msg %p tid %llu %u+%u+%u v%d\n", __func__, msg, 20268cb441c0SIlya Dryomov le64_to_cpu(msg->hdr.tid), le32_to_cpu(msg->hdr.front_len), 20278cb441c0SIlya Dryomov le32_to_cpu(msg->hdr.middle_len), le32_to_cpu(msg->hdr.data_len), 20288cb441c0SIlya Dryomov le16_to_cpu(msg->hdr.version)); 2029bb873b53SIlya Dryomov } 2030bb873b53SIlya Dryomov 2031bb873b53SIlya Dryomov /* 2032bb873b53SIlya Dryomov * @req has to be assigned a tid and registered. 2033bb873b53SIlya Dryomov */ 2034bb873b53SIlya Dryomov static void send_request(struct ceph_osd_request *req) 2035bb873b53SIlya Dryomov { 2036bb873b53SIlya Dryomov struct ceph_osd *osd = req->r_osd; 2037bb873b53SIlya Dryomov 20385aea3dcdSIlya Dryomov verify_osd_locked(osd); 2039bb873b53SIlya Dryomov WARN_ON(osd->o_osd != req->r_t.osd); 2040bb873b53SIlya Dryomov 2041a02a946dSIlya Dryomov /* backoff? */ 2042a02a946dSIlya Dryomov if (should_plug_request(req)) 2043a02a946dSIlya Dryomov return; 2044a02a946dSIlya Dryomov 20455aea3dcdSIlya Dryomov /* 20465aea3dcdSIlya Dryomov * We may have a previously queued request message hanging 20475aea3dcdSIlya Dryomov * around. Cancel it to avoid corrupting the msgr. 20485aea3dcdSIlya Dryomov */ 20495aea3dcdSIlya Dryomov if (req->r_sent) 20505aea3dcdSIlya Dryomov ceph_msg_revoke(req->r_request); 20515aea3dcdSIlya Dryomov 2052bb873b53SIlya Dryomov req->r_flags |= CEPH_OSD_FLAG_KNOWN_REDIR; 2053bb873b53SIlya Dryomov if (req->r_attempts) 2054bb873b53SIlya Dryomov req->r_flags |= CEPH_OSD_FLAG_RETRY; 2055bb873b53SIlya Dryomov else 2056bb873b53SIlya Dryomov WARN_ON(req->r_flags & CEPH_OSD_FLAG_RETRY); 2057bb873b53SIlya Dryomov 20588cb441c0SIlya Dryomov encode_request_partial(req, req->r_request); 2059bb873b53SIlya Dryomov 206004c7d789SIlya Dryomov dout("%s req %p tid %llu to pgid %llu.%x spgid %llu.%xs%d osd%d e%u flags 0x%x attempt %d\n", 2061bb873b53SIlya Dryomov __func__, req, req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed, 2062dc98ff72SIlya Dryomov req->r_t.spgid.pgid.pool, req->r_t.spgid.pgid.seed, 206304c7d789SIlya Dryomov req->r_t.spgid.shard, osd->o_osd, req->r_t.epoch, req->r_flags, 206404c7d789SIlya Dryomov req->r_attempts); 2065bb873b53SIlya Dryomov 2066bb873b53SIlya Dryomov req->r_t.paused = false; 20673d14c5d2SYehuda Sadeh req->r_stamp = jiffies; 2068bb873b53SIlya Dryomov req->r_attempts++; 20693d14c5d2SYehuda Sadeh 2070bb873b53SIlya Dryomov req->r_sent = osd->o_incarnation; 2071bb873b53SIlya Dryomov req->r_request->hdr.tid = cpu_to_le64(req->r_tid); 2072bb873b53SIlya Dryomov ceph_con_send(&osd->o_con, ceph_msg_get(req->r_request)); 20733d14c5d2SYehuda Sadeh } 20743d14c5d2SYehuda Sadeh 207542c1b124SIlya Dryomov static void maybe_request_map(struct ceph_osd_client *osdc) 207642c1b124SIlya Dryomov { 207742c1b124SIlya Dryomov bool continuous = false; 207842c1b124SIlya Dryomov 20795aea3dcdSIlya Dryomov verify_osdc_locked(osdc); 208042c1b124SIlya Dryomov WARN_ON(!osdc->osdmap->epoch); 208142c1b124SIlya Dryomov 2082b7ec35b3SIlya Dryomov if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) || 2083b7ec35b3SIlya Dryomov ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD) || 2084b7ec35b3SIlya Dryomov ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) { 208542c1b124SIlya Dryomov dout("%s osdc %p continuous\n", __func__, osdc); 208642c1b124SIlya Dryomov continuous = true; 208742c1b124SIlya Dryomov } else { 208842c1b124SIlya Dryomov dout("%s osdc %p onetime\n", __func__, osdc); 208942c1b124SIlya Dryomov } 209042c1b124SIlya Dryomov 209142c1b124SIlya Dryomov if (ceph_monc_want_map(&osdc->client->monc, CEPH_SUB_OSDMAP, 209242c1b124SIlya Dryomov osdc->osdmap->epoch + 1, continuous)) 209342c1b124SIlya Dryomov ceph_monc_renew_subs(&osdc->client->monc); 209442c1b124SIlya Dryomov } 209542c1b124SIlya Dryomov 2096a1f4020aSJeff Layton static void complete_request(struct ceph_osd_request *req, int err); 20974609245eSIlya Dryomov static void send_map_check(struct ceph_osd_request *req); 20984609245eSIlya Dryomov 20995aea3dcdSIlya Dryomov static void __submit_request(struct ceph_osd_request *req, bool wrlocked) 21000bbfdfe8SIlya Dryomov { 21015aea3dcdSIlya Dryomov struct ceph_osd_client *osdc = req->r_osdc; 21025aea3dcdSIlya Dryomov struct ceph_osd *osd; 21034609245eSIlya Dryomov enum calc_target_result ct_res; 21045aea3dcdSIlya Dryomov bool need_send = false; 21055aea3dcdSIlya Dryomov bool promoted = false; 2106a1f4020aSJeff Layton bool need_abort = false; 21070bbfdfe8SIlya Dryomov 2108b18b9550SIlya Dryomov WARN_ON(req->r_tid); 21095aea3dcdSIlya Dryomov dout("%s req %p wrlocked %d\n", __func__, req, wrlocked); 21105aea3dcdSIlya Dryomov 21115aea3dcdSIlya Dryomov again: 21127de030d6SIlya Dryomov ct_res = calc_target(osdc, &req->r_t, NULL, false); 21134609245eSIlya Dryomov if (ct_res == CALC_TARGET_POOL_DNE && !wrlocked) 21144609245eSIlya Dryomov goto promote; 21154609245eSIlya Dryomov 21165aea3dcdSIlya Dryomov osd = lookup_create_osd(osdc, req->r_t.osd, wrlocked); 21175aea3dcdSIlya Dryomov if (IS_ERR(osd)) { 21185aea3dcdSIlya Dryomov WARN_ON(PTR_ERR(osd) != -EAGAIN || wrlocked); 21195aea3dcdSIlya Dryomov goto promote; 21205aea3dcdSIlya Dryomov } 21215aea3dcdSIlya Dryomov 212258eb7932SJeff Layton if (osdc->osdmap->epoch < osdc->epoch_barrier) { 212358eb7932SJeff Layton dout("req %p epoch %u barrier %u\n", req, osdc->osdmap->epoch, 212458eb7932SJeff Layton osdc->epoch_barrier); 212558eb7932SJeff Layton req->r_t.paused = true; 212658eb7932SJeff Layton maybe_request_map(osdc); 212758eb7932SJeff Layton } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) && 2128b7ec35b3SIlya Dryomov ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) { 21295aea3dcdSIlya Dryomov dout("req %p pausewr\n", req); 21305aea3dcdSIlya Dryomov req->r_t.paused = true; 21315aea3dcdSIlya Dryomov maybe_request_map(osdc); 21325aea3dcdSIlya Dryomov } else if ((req->r_flags & CEPH_OSD_FLAG_READ) && 2133b7ec35b3SIlya Dryomov ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) { 21345aea3dcdSIlya Dryomov dout("req %p pauserd\n", req); 21355aea3dcdSIlya Dryomov req->r_t.paused = true; 21365aea3dcdSIlya Dryomov maybe_request_map(osdc); 21375aea3dcdSIlya Dryomov } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) && 21385aea3dcdSIlya Dryomov !(req->r_flags & (CEPH_OSD_FLAG_FULL_TRY | 21395aea3dcdSIlya Dryomov CEPH_OSD_FLAG_FULL_FORCE)) && 2140b7ec35b3SIlya Dryomov (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) || 21415aea3dcdSIlya Dryomov pool_full(osdc, req->r_t.base_oloc.pool))) { 21425aea3dcdSIlya Dryomov dout("req %p full/pool_full\n", req); 21435aea3dcdSIlya Dryomov pr_warn_ratelimited("FULL or reached pool quota\n"); 21445aea3dcdSIlya Dryomov req->r_t.paused = true; 21455aea3dcdSIlya Dryomov maybe_request_map(osdc); 2146a1f4020aSJeff Layton if (req->r_abort_on_full) 2147a1f4020aSJeff Layton need_abort = true; 21485aea3dcdSIlya Dryomov } else if (!osd_homeless(osd)) { 21495aea3dcdSIlya Dryomov need_send = true; 21500bbfdfe8SIlya Dryomov } else { 21515aea3dcdSIlya Dryomov maybe_request_map(osdc); 21520bbfdfe8SIlya Dryomov } 21530bbfdfe8SIlya Dryomov 21545aea3dcdSIlya Dryomov mutex_lock(&osd->lock); 21555aea3dcdSIlya Dryomov /* 21565aea3dcdSIlya Dryomov * Assign the tid atomically with send_request() to protect 21575aea3dcdSIlya Dryomov * multiple writes to the same object from racing with each 21585aea3dcdSIlya Dryomov * other, resulting in out of order ops on the OSDs. 21595aea3dcdSIlya Dryomov */ 21605aea3dcdSIlya Dryomov req->r_tid = atomic64_inc_return(&osdc->last_tid); 21615aea3dcdSIlya Dryomov link_request(osd, req); 21625aea3dcdSIlya Dryomov if (need_send) 21635aea3dcdSIlya Dryomov send_request(req); 2164a1f4020aSJeff Layton else if (need_abort) 2165a1f4020aSJeff Layton complete_request(req, -ENOSPC); 21665aea3dcdSIlya Dryomov mutex_unlock(&osd->lock); 21675aea3dcdSIlya Dryomov 21684609245eSIlya Dryomov if (ct_res == CALC_TARGET_POOL_DNE) 21694609245eSIlya Dryomov send_map_check(req); 21704609245eSIlya Dryomov 21715aea3dcdSIlya Dryomov if (promoted) 21725aea3dcdSIlya Dryomov downgrade_write(&osdc->lock); 21735aea3dcdSIlya Dryomov return; 21745aea3dcdSIlya Dryomov 21755aea3dcdSIlya Dryomov promote: 21765aea3dcdSIlya Dryomov up_read(&osdc->lock); 21775aea3dcdSIlya Dryomov down_write(&osdc->lock); 21785aea3dcdSIlya Dryomov wrlocked = true; 21795aea3dcdSIlya Dryomov promoted = true; 21805aea3dcdSIlya Dryomov goto again; 21810bbfdfe8SIlya Dryomov } 21820bbfdfe8SIlya Dryomov 21835aea3dcdSIlya Dryomov static void account_request(struct ceph_osd_request *req) 21845aea3dcdSIlya Dryomov { 218554ea0046SIlya Dryomov WARN_ON(req->r_flags & (CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK)); 2186b18b9550SIlya Dryomov WARN_ON(!(req->r_flags & (CEPH_OSD_FLAG_READ | CEPH_OSD_FLAG_WRITE))); 21875aea3dcdSIlya Dryomov 2188b18b9550SIlya Dryomov req->r_flags |= CEPH_OSD_FLAG_ONDISK; 21895aea3dcdSIlya Dryomov atomic_inc(&req->r_osdc->num_requests); 21907cc5e38fSIlya Dryomov 21917cc5e38fSIlya Dryomov req->r_start_stamp = jiffies; 21925aea3dcdSIlya Dryomov } 21935aea3dcdSIlya Dryomov 21945aea3dcdSIlya Dryomov static void submit_request(struct ceph_osd_request *req, bool wrlocked) 21955aea3dcdSIlya Dryomov { 21965aea3dcdSIlya Dryomov ceph_osdc_get_request(req); 21975aea3dcdSIlya Dryomov account_request(req); 21985aea3dcdSIlya Dryomov __submit_request(req, wrlocked); 21995aea3dcdSIlya Dryomov } 22005aea3dcdSIlya Dryomov 220145ee2c1dSIlya Dryomov static void finish_request(struct ceph_osd_request *req) 22025aea3dcdSIlya Dryomov { 22035aea3dcdSIlya Dryomov struct ceph_osd_client *osdc = req->r_osdc; 22045aea3dcdSIlya Dryomov 22054609245eSIlya Dryomov WARN_ON(lookup_request_mc(&osdc->map_checks, req->r_tid)); 220604c7d789SIlya Dryomov dout("%s req %p tid %llu\n", __func__, req, req->r_tid); 220704c7d789SIlya Dryomov 220804c7d789SIlya Dryomov if (req->r_osd) 220904c7d789SIlya Dryomov unlink_request(req->r_osd, req); 22105aea3dcdSIlya Dryomov atomic_dec(&osdc->num_requests); 22115aea3dcdSIlya Dryomov 22125aea3dcdSIlya Dryomov /* 22135aea3dcdSIlya Dryomov * If an OSD has failed or returned and a request has been sent 22145aea3dcdSIlya Dryomov * twice, it's possible to get a reply and end up here while the 22155aea3dcdSIlya Dryomov * request message is queued for delivery. We will ignore the 22165aea3dcdSIlya Dryomov * reply, so not a big deal, but better to try and catch it. 22175aea3dcdSIlya Dryomov */ 22185aea3dcdSIlya Dryomov ceph_msg_revoke(req->r_request); 22195aea3dcdSIlya Dryomov ceph_msg_revoke_incoming(req->r_reply); 22205aea3dcdSIlya Dryomov } 22215aea3dcdSIlya Dryomov 2222fe5da05eSIlya Dryomov static void __complete_request(struct ceph_osd_request *req) 2223fe5da05eSIlya Dryomov { 2224b18b9550SIlya Dryomov if (req->r_callback) { 2225b18b9550SIlya Dryomov dout("%s req %p tid %llu cb %pf result %d\n", __func__, req, 2226b18b9550SIlya Dryomov req->r_tid, req->r_callback, req->r_result); 2227fe5da05eSIlya Dryomov req->r_callback(req); 2228b18b9550SIlya Dryomov } 2229fe5da05eSIlya Dryomov } 2230fe5da05eSIlya Dryomov 22314609245eSIlya Dryomov /* 2232b18b9550SIlya Dryomov * This is open-coded in handle_reply(). 22334609245eSIlya Dryomov */ 22344609245eSIlya Dryomov static void complete_request(struct ceph_osd_request *req, int err) 22354609245eSIlya Dryomov { 22364609245eSIlya Dryomov dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err); 22374609245eSIlya Dryomov 22384609245eSIlya Dryomov req->r_result = err; 223945ee2c1dSIlya Dryomov finish_request(req); 22404609245eSIlya Dryomov __complete_request(req); 2241b18b9550SIlya Dryomov complete_all(&req->r_completion); 22424609245eSIlya Dryomov ceph_osdc_put_request(req); 22434609245eSIlya Dryomov } 22444609245eSIlya Dryomov 22454609245eSIlya Dryomov static void cancel_map_check(struct ceph_osd_request *req) 22464609245eSIlya Dryomov { 22474609245eSIlya Dryomov struct ceph_osd_client *osdc = req->r_osdc; 22484609245eSIlya Dryomov struct ceph_osd_request *lookup_req; 22494609245eSIlya Dryomov 22504609245eSIlya Dryomov verify_osdc_wrlocked(osdc); 22514609245eSIlya Dryomov 22524609245eSIlya Dryomov lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid); 22534609245eSIlya Dryomov if (!lookup_req) 22544609245eSIlya Dryomov return; 22554609245eSIlya Dryomov 22564609245eSIlya Dryomov WARN_ON(lookup_req != req); 22574609245eSIlya Dryomov erase_request_mc(&osdc->map_checks, req); 22584609245eSIlya Dryomov ceph_osdc_put_request(req); 22594609245eSIlya Dryomov } 22604609245eSIlya Dryomov 22615aea3dcdSIlya Dryomov static void cancel_request(struct ceph_osd_request *req) 22625aea3dcdSIlya Dryomov { 22635aea3dcdSIlya Dryomov dout("%s req %p tid %llu\n", __func__, req, req->r_tid); 22645aea3dcdSIlya Dryomov 22654609245eSIlya Dryomov cancel_map_check(req); 226645ee2c1dSIlya Dryomov finish_request(req); 2267b18b9550SIlya Dryomov complete_all(&req->r_completion); 2268c297eb42SIlya Dryomov ceph_osdc_put_request(req); 22695aea3dcdSIlya Dryomov } 22705aea3dcdSIlya Dryomov 22717cc5e38fSIlya Dryomov static void abort_request(struct ceph_osd_request *req, int err) 22727cc5e38fSIlya Dryomov { 22737cc5e38fSIlya Dryomov dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err); 22747cc5e38fSIlya Dryomov 22757cc5e38fSIlya Dryomov cancel_map_check(req); 22767cc5e38fSIlya Dryomov complete_request(req, err); 22777cc5e38fSIlya Dryomov } 22787cc5e38fSIlya Dryomov 227958eb7932SJeff Layton static void update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb) 228058eb7932SJeff Layton { 228158eb7932SJeff Layton if (likely(eb > osdc->epoch_barrier)) { 228258eb7932SJeff Layton dout("updating epoch_barrier from %u to %u\n", 228358eb7932SJeff Layton osdc->epoch_barrier, eb); 228458eb7932SJeff Layton osdc->epoch_barrier = eb; 228558eb7932SJeff Layton /* Request map if we're not to the barrier yet */ 228658eb7932SJeff Layton if (eb > osdc->osdmap->epoch) 228758eb7932SJeff Layton maybe_request_map(osdc); 228858eb7932SJeff Layton } 228958eb7932SJeff Layton } 229058eb7932SJeff Layton 229158eb7932SJeff Layton void ceph_osdc_update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb) 229258eb7932SJeff Layton { 229358eb7932SJeff Layton down_read(&osdc->lock); 229458eb7932SJeff Layton if (unlikely(eb > osdc->epoch_barrier)) { 229558eb7932SJeff Layton up_read(&osdc->lock); 229658eb7932SJeff Layton down_write(&osdc->lock); 229758eb7932SJeff Layton update_epoch_barrier(osdc, eb); 229858eb7932SJeff Layton up_write(&osdc->lock); 229958eb7932SJeff Layton } else { 230058eb7932SJeff Layton up_read(&osdc->lock); 230158eb7932SJeff Layton } 230258eb7932SJeff Layton } 230358eb7932SJeff Layton EXPORT_SYMBOL(ceph_osdc_update_epoch_barrier); 230458eb7932SJeff Layton 2305fc36d0a4SJeff Layton /* 2306fc36d0a4SJeff Layton * Drop all pending requests that are stalled waiting on a full condition to 230758eb7932SJeff Layton * clear, and complete them with ENOSPC as the return code. Set the 230858eb7932SJeff Layton * osdc->epoch_barrier to the latest map epoch that we've seen if any were 230958eb7932SJeff Layton * cancelled. 2310fc36d0a4SJeff Layton */ 2311fc36d0a4SJeff Layton static void ceph_osdc_abort_on_full(struct ceph_osd_client *osdc) 2312fc36d0a4SJeff Layton { 2313fc36d0a4SJeff Layton struct rb_node *n; 231458eb7932SJeff Layton bool victims = false; 2315fc36d0a4SJeff Layton 2316fc36d0a4SJeff Layton dout("enter abort_on_full\n"); 2317fc36d0a4SJeff Layton 2318fc36d0a4SJeff Layton if (!ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) && !have_pool_full(osdc)) 2319fc36d0a4SJeff Layton goto out; 2320fc36d0a4SJeff Layton 232158eb7932SJeff Layton /* Scan list and see if there is anything to abort */ 232258eb7932SJeff Layton for (n = rb_first(&osdc->osds); n; n = rb_next(n)) { 232358eb7932SJeff Layton struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node); 232458eb7932SJeff Layton struct rb_node *m; 232558eb7932SJeff Layton 232658eb7932SJeff Layton m = rb_first(&osd->o_requests); 232758eb7932SJeff Layton while (m) { 232858eb7932SJeff Layton struct ceph_osd_request *req = rb_entry(m, 232958eb7932SJeff Layton struct ceph_osd_request, r_node); 233058eb7932SJeff Layton m = rb_next(m); 233158eb7932SJeff Layton 233258eb7932SJeff Layton if (req->r_abort_on_full) { 233358eb7932SJeff Layton victims = true; 233458eb7932SJeff Layton break; 233558eb7932SJeff Layton } 233658eb7932SJeff Layton } 233758eb7932SJeff Layton if (victims) 233858eb7932SJeff Layton break; 233958eb7932SJeff Layton } 234058eb7932SJeff Layton 234158eb7932SJeff Layton if (!victims) 234258eb7932SJeff Layton goto out; 234358eb7932SJeff Layton 234458eb7932SJeff Layton /* 234558eb7932SJeff Layton * Update the barrier to current epoch if it's behind that point, 234658eb7932SJeff Layton * since we know we have some calls to be aborted in the tree. 234758eb7932SJeff Layton */ 234858eb7932SJeff Layton update_epoch_barrier(osdc, osdc->osdmap->epoch); 234958eb7932SJeff Layton 2350fc36d0a4SJeff Layton for (n = rb_first(&osdc->osds); n; n = rb_next(n)) { 2351fc36d0a4SJeff Layton struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node); 2352fc36d0a4SJeff Layton struct rb_node *m; 2353fc36d0a4SJeff Layton 2354fc36d0a4SJeff Layton m = rb_first(&osd->o_requests); 2355fc36d0a4SJeff Layton while (m) { 2356fc36d0a4SJeff Layton struct ceph_osd_request *req = rb_entry(m, 2357fc36d0a4SJeff Layton struct ceph_osd_request, r_node); 2358fc36d0a4SJeff Layton m = rb_next(m); 2359fc36d0a4SJeff Layton 2360fc36d0a4SJeff Layton if (req->r_abort_on_full && 2361fc36d0a4SJeff Layton (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) || 2362fc36d0a4SJeff Layton pool_full(osdc, req->r_t.target_oloc.pool))) 2363fc36d0a4SJeff Layton abort_request(req, -ENOSPC); 2364fc36d0a4SJeff Layton } 2365fc36d0a4SJeff Layton } 2366fc36d0a4SJeff Layton out: 236758eb7932SJeff Layton dout("return abort_on_full barrier=%u\n", osdc->epoch_barrier); 2368fc36d0a4SJeff Layton } 2369fc36d0a4SJeff Layton 23704609245eSIlya Dryomov static void check_pool_dne(struct ceph_osd_request *req) 23714609245eSIlya Dryomov { 23724609245eSIlya Dryomov struct ceph_osd_client *osdc = req->r_osdc; 23734609245eSIlya Dryomov struct ceph_osdmap *map = osdc->osdmap; 23744609245eSIlya Dryomov 23754609245eSIlya Dryomov verify_osdc_wrlocked(osdc); 23764609245eSIlya Dryomov WARN_ON(!map->epoch); 23774609245eSIlya Dryomov 23784609245eSIlya Dryomov if (req->r_attempts) { 23794609245eSIlya Dryomov /* 23804609245eSIlya Dryomov * We sent a request earlier, which means that 23814609245eSIlya Dryomov * previously the pool existed, and now it does not 23824609245eSIlya Dryomov * (i.e., it was deleted). 23834609245eSIlya Dryomov */ 23844609245eSIlya Dryomov req->r_map_dne_bound = map->epoch; 23854609245eSIlya Dryomov dout("%s req %p tid %llu pool disappeared\n", __func__, req, 23864609245eSIlya Dryomov req->r_tid); 23874609245eSIlya Dryomov } else { 23884609245eSIlya Dryomov dout("%s req %p tid %llu map_dne_bound %u have %u\n", __func__, 23894609245eSIlya Dryomov req, req->r_tid, req->r_map_dne_bound, map->epoch); 23904609245eSIlya Dryomov } 23914609245eSIlya Dryomov 23924609245eSIlya Dryomov if (req->r_map_dne_bound) { 23934609245eSIlya Dryomov if (map->epoch >= req->r_map_dne_bound) { 23944609245eSIlya Dryomov /* we had a new enough map */ 23954609245eSIlya Dryomov pr_info_ratelimited("tid %llu pool does not exist\n", 23964609245eSIlya Dryomov req->r_tid); 23974609245eSIlya Dryomov complete_request(req, -ENOENT); 23984609245eSIlya Dryomov } 23994609245eSIlya Dryomov } else { 24004609245eSIlya Dryomov send_map_check(req); 24014609245eSIlya Dryomov } 24024609245eSIlya Dryomov } 24034609245eSIlya Dryomov 24044609245eSIlya Dryomov static void map_check_cb(struct ceph_mon_generic_request *greq) 24054609245eSIlya Dryomov { 24064609245eSIlya Dryomov struct ceph_osd_client *osdc = &greq->monc->client->osdc; 24074609245eSIlya Dryomov struct ceph_osd_request *req; 24084609245eSIlya Dryomov u64 tid = greq->private_data; 24094609245eSIlya Dryomov 24104609245eSIlya Dryomov WARN_ON(greq->result || !greq->u.newest); 24114609245eSIlya Dryomov 24124609245eSIlya Dryomov down_write(&osdc->lock); 24134609245eSIlya Dryomov req = lookup_request_mc(&osdc->map_checks, tid); 24144609245eSIlya Dryomov if (!req) { 24154609245eSIlya Dryomov dout("%s tid %llu dne\n", __func__, tid); 24164609245eSIlya Dryomov goto out_unlock; 24174609245eSIlya Dryomov } 24184609245eSIlya Dryomov 24194609245eSIlya Dryomov dout("%s req %p tid %llu map_dne_bound %u newest %llu\n", __func__, 24204609245eSIlya Dryomov req, req->r_tid, req->r_map_dne_bound, greq->u.newest); 24214609245eSIlya Dryomov if (!req->r_map_dne_bound) 24224609245eSIlya Dryomov req->r_map_dne_bound = greq->u.newest; 24234609245eSIlya Dryomov erase_request_mc(&osdc->map_checks, req); 24244609245eSIlya Dryomov check_pool_dne(req); 24254609245eSIlya Dryomov 24264609245eSIlya Dryomov ceph_osdc_put_request(req); 24274609245eSIlya Dryomov out_unlock: 24284609245eSIlya Dryomov up_write(&osdc->lock); 24294609245eSIlya Dryomov } 24304609245eSIlya Dryomov 24314609245eSIlya Dryomov static void send_map_check(struct ceph_osd_request *req) 24324609245eSIlya Dryomov { 24334609245eSIlya Dryomov struct ceph_osd_client *osdc = req->r_osdc; 24344609245eSIlya Dryomov struct ceph_osd_request *lookup_req; 24354609245eSIlya Dryomov int ret; 24364609245eSIlya Dryomov 24374609245eSIlya Dryomov verify_osdc_wrlocked(osdc); 24384609245eSIlya Dryomov 24394609245eSIlya Dryomov lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid); 24404609245eSIlya Dryomov if (lookup_req) { 24414609245eSIlya Dryomov WARN_ON(lookup_req != req); 24424609245eSIlya Dryomov return; 24434609245eSIlya Dryomov } 24444609245eSIlya Dryomov 24454609245eSIlya Dryomov ceph_osdc_get_request(req); 24464609245eSIlya Dryomov insert_request_mc(&osdc->map_checks, req); 24474609245eSIlya Dryomov ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap", 24484609245eSIlya Dryomov map_check_cb, req->r_tid); 24494609245eSIlya Dryomov WARN_ON(ret); 24504609245eSIlya Dryomov } 24514609245eSIlya Dryomov 24520bbfdfe8SIlya Dryomov /* 2453922dab61SIlya Dryomov * lingering requests, watch/notify v2 infrastructure 2454922dab61SIlya Dryomov */ 2455922dab61SIlya Dryomov static void linger_release(struct kref *kref) 2456922dab61SIlya Dryomov { 2457922dab61SIlya Dryomov struct ceph_osd_linger_request *lreq = 2458922dab61SIlya Dryomov container_of(kref, struct ceph_osd_linger_request, kref); 2459922dab61SIlya Dryomov 2460922dab61SIlya Dryomov dout("%s lreq %p reg_req %p ping_req %p\n", __func__, lreq, 2461922dab61SIlya Dryomov lreq->reg_req, lreq->ping_req); 2462922dab61SIlya Dryomov WARN_ON(!RB_EMPTY_NODE(&lreq->node)); 2463922dab61SIlya Dryomov WARN_ON(!RB_EMPTY_NODE(&lreq->osdc_node)); 24644609245eSIlya Dryomov WARN_ON(!RB_EMPTY_NODE(&lreq->mc_node)); 2465922dab61SIlya Dryomov WARN_ON(!list_empty(&lreq->scan_item)); 2466b07d3c4bSIlya Dryomov WARN_ON(!list_empty(&lreq->pending_lworks)); 2467922dab61SIlya Dryomov WARN_ON(lreq->osd); 2468922dab61SIlya Dryomov 2469922dab61SIlya Dryomov if (lreq->reg_req) 2470922dab61SIlya Dryomov ceph_osdc_put_request(lreq->reg_req); 2471922dab61SIlya Dryomov if (lreq->ping_req) 2472922dab61SIlya Dryomov ceph_osdc_put_request(lreq->ping_req); 2473922dab61SIlya Dryomov target_destroy(&lreq->t); 2474922dab61SIlya Dryomov kfree(lreq); 2475922dab61SIlya Dryomov } 2476922dab61SIlya Dryomov 2477922dab61SIlya Dryomov static void linger_put(struct ceph_osd_linger_request *lreq) 2478922dab61SIlya Dryomov { 2479922dab61SIlya Dryomov if (lreq) 2480922dab61SIlya Dryomov kref_put(&lreq->kref, linger_release); 2481922dab61SIlya Dryomov } 2482922dab61SIlya Dryomov 2483922dab61SIlya Dryomov static struct ceph_osd_linger_request * 2484922dab61SIlya Dryomov linger_get(struct ceph_osd_linger_request *lreq) 2485922dab61SIlya Dryomov { 2486922dab61SIlya Dryomov kref_get(&lreq->kref); 2487922dab61SIlya Dryomov return lreq; 2488922dab61SIlya Dryomov } 2489922dab61SIlya Dryomov 2490922dab61SIlya Dryomov static struct ceph_osd_linger_request * 2491922dab61SIlya Dryomov linger_alloc(struct ceph_osd_client *osdc) 2492922dab61SIlya Dryomov { 2493922dab61SIlya Dryomov struct ceph_osd_linger_request *lreq; 2494922dab61SIlya Dryomov 2495922dab61SIlya Dryomov lreq = kzalloc(sizeof(*lreq), GFP_NOIO); 2496922dab61SIlya Dryomov if (!lreq) 2497922dab61SIlya Dryomov return NULL; 2498922dab61SIlya Dryomov 2499922dab61SIlya Dryomov kref_init(&lreq->kref); 2500922dab61SIlya Dryomov mutex_init(&lreq->lock); 2501922dab61SIlya Dryomov RB_CLEAR_NODE(&lreq->node); 2502922dab61SIlya Dryomov RB_CLEAR_NODE(&lreq->osdc_node); 25034609245eSIlya Dryomov RB_CLEAR_NODE(&lreq->mc_node); 2504922dab61SIlya Dryomov INIT_LIST_HEAD(&lreq->scan_item); 2505b07d3c4bSIlya Dryomov INIT_LIST_HEAD(&lreq->pending_lworks); 2506922dab61SIlya Dryomov init_completion(&lreq->reg_commit_wait); 250719079203SIlya Dryomov init_completion(&lreq->notify_finish_wait); 2508922dab61SIlya Dryomov 2509922dab61SIlya Dryomov lreq->osdc = osdc; 2510922dab61SIlya Dryomov target_init(&lreq->t); 2511922dab61SIlya Dryomov 2512922dab61SIlya Dryomov dout("%s lreq %p\n", __func__, lreq); 2513922dab61SIlya Dryomov return lreq; 2514922dab61SIlya Dryomov } 2515922dab61SIlya Dryomov 2516922dab61SIlya Dryomov DEFINE_RB_INSDEL_FUNCS(linger, struct ceph_osd_linger_request, linger_id, node) 2517922dab61SIlya Dryomov DEFINE_RB_FUNCS(linger_osdc, struct ceph_osd_linger_request, linger_id, osdc_node) 25184609245eSIlya Dryomov DEFINE_RB_FUNCS(linger_mc, struct ceph_osd_linger_request, linger_id, mc_node) 2519922dab61SIlya Dryomov 2520922dab61SIlya Dryomov /* 2521922dab61SIlya Dryomov * Create linger request <-> OSD session relation. 2522922dab61SIlya Dryomov * 2523922dab61SIlya Dryomov * @lreq has to be registered, @osd may be homeless. 2524922dab61SIlya Dryomov */ 2525922dab61SIlya Dryomov static void link_linger(struct ceph_osd *osd, 2526922dab61SIlya Dryomov struct ceph_osd_linger_request *lreq) 2527922dab61SIlya Dryomov { 2528922dab61SIlya Dryomov verify_osd_locked(osd); 2529922dab61SIlya Dryomov WARN_ON(!lreq->linger_id || lreq->osd); 2530922dab61SIlya Dryomov dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd, 2531922dab61SIlya Dryomov osd->o_osd, lreq, lreq->linger_id); 2532922dab61SIlya Dryomov 2533922dab61SIlya Dryomov if (!osd_homeless(osd)) 2534922dab61SIlya Dryomov __remove_osd_from_lru(osd); 2535922dab61SIlya Dryomov else 2536922dab61SIlya Dryomov atomic_inc(&osd->o_osdc->num_homeless); 2537922dab61SIlya Dryomov 2538922dab61SIlya Dryomov get_osd(osd); 2539922dab61SIlya Dryomov insert_linger(&osd->o_linger_requests, lreq); 2540922dab61SIlya Dryomov lreq->osd = osd; 2541922dab61SIlya Dryomov } 2542922dab61SIlya Dryomov 2543922dab61SIlya Dryomov static void unlink_linger(struct ceph_osd *osd, 2544922dab61SIlya Dryomov struct ceph_osd_linger_request *lreq) 2545922dab61SIlya Dryomov { 2546922dab61SIlya Dryomov verify_osd_locked(osd); 2547922dab61SIlya Dryomov WARN_ON(lreq->osd != osd); 2548922dab61SIlya Dryomov dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd, 2549922dab61SIlya Dryomov osd->o_osd, lreq, lreq->linger_id); 2550922dab61SIlya Dryomov 2551922dab61SIlya Dryomov lreq->osd = NULL; 2552922dab61SIlya Dryomov erase_linger(&osd->o_linger_requests, lreq); 2553922dab61SIlya Dryomov put_osd(osd); 2554922dab61SIlya Dryomov 2555922dab61SIlya Dryomov if (!osd_homeless(osd)) 2556922dab61SIlya Dryomov maybe_move_osd_to_lru(osd); 2557922dab61SIlya Dryomov else 2558922dab61SIlya Dryomov atomic_dec(&osd->o_osdc->num_homeless); 2559922dab61SIlya Dryomov } 2560922dab61SIlya Dryomov 2561922dab61SIlya Dryomov static bool __linger_registered(struct ceph_osd_linger_request *lreq) 2562922dab61SIlya Dryomov { 2563922dab61SIlya Dryomov verify_osdc_locked(lreq->osdc); 2564922dab61SIlya Dryomov 2565922dab61SIlya Dryomov return !RB_EMPTY_NODE(&lreq->osdc_node); 2566922dab61SIlya Dryomov } 2567922dab61SIlya Dryomov 2568922dab61SIlya Dryomov static bool linger_registered(struct ceph_osd_linger_request *lreq) 2569922dab61SIlya Dryomov { 2570922dab61SIlya Dryomov struct ceph_osd_client *osdc = lreq->osdc; 2571922dab61SIlya Dryomov bool registered; 2572922dab61SIlya Dryomov 2573922dab61SIlya Dryomov down_read(&osdc->lock); 2574922dab61SIlya Dryomov registered = __linger_registered(lreq); 2575922dab61SIlya Dryomov up_read(&osdc->lock); 2576922dab61SIlya Dryomov 2577922dab61SIlya Dryomov return registered; 2578922dab61SIlya Dryomov } 2579922dab61SIlya Dryomov 2580922dab61SIlya Dryomov static void linger_register(struct ceph_osd_linger_request *lreq) 2581922dab61SIlya Dryomov { 2582922dab61SIlya Dryomov struct ceph_osd_client *osdc = lreq->osdc; 2583922dab61SIlya Dryomov 2584922dab61SIlya Dryomov verify_osdc_wrlocked(osdc); 2585922dab61SIlya Dryomov WARN_ON(lreq->linger_id); 2586922dab61SIlya Dryomov 2587922dab61SIlya Dryomov linger_get(lreq); 2588922dab61SIlya Dryomov lreq->linger_id = ++osdc->last_linger_id; 2589922dab61SIlya Dryomov insert_linger_osdc(&osdc->linger_requests, lreq); 2590922dab61SIlya Dryomov } 2591922dab61SIlya Dryomov 2592922dab61SIlya Dryomov static void linger_unregister(struct ceph_osd_linger_request *lreq) 2593922dab61SIlya Dryomov { 2594922dab61SIlya Dryomov struct ceph_osd_client *osdc = lreq->osdc; 2595922dab61SIlya Dryomov 2596922dab61SIlya Dryomov verify_osdc_wrlocked(osdc); 2597922dab61SIlya Dryomov 2598922dab61SIlya Dryomov erase_linger_osdc(&osdc->linger_requests, lreq); 2599922dab61SIlya Dryomov linger_put(lreq); 2600922dab61SIlya Dryomov } 2601922dab61SIlya Dryomov 2602922dab61SIlya Dryomov static void cancel_linger_request(struct ceph_osd_request *req) 2603922dab61SIlya Dryomov { 2604922dab61SIlya Dryomov struct ceph_osd_linger_request *lreq = req->r_priv; 2605922dab61SIlya Dryomov 2606922dab61SIlya Dryomov WARN_ON(!req->r_linger); 2607922dab61SIlya Dryomov cancel_request(req); 2608922dab61SIlya Dryomov linger_put(lreq); 2609922dab61SIlya Dryomov } 2610922dab61SIlya Dryomov 2611922dab61SIlya Dryomov struct linger_work { 2612922dab61SIlya Dryomov struct work_struct work; 2613922dab61SIlya Dryomov struct ceph_osd_linger_request *lreq; 2614b07d3c4bSIlya Dryomov struct list_head pending_item; 2615b07d3c4bSIlya Dryomov unsigned long queued_stamp; 2616922dab61SIlya Dryomov 2617922dab61SIlya Dryomov union { 2618922dab61SIlya Dryomov struct { 2619922dab61SIlya Dryomov u64 notify_id; 2620922dab61SIlya Dryomov u64 notifier_id; 2621922dab61SIlya Dryomov void *payload; /* points into @msg front */ 2622922dab61SIlya Dryomov size_t payload_len; 2623922dab61SIlya Dryomov 2624922dab61SIlya Dryomov struct ceph_msg *msg; /* for ceph_msg_put() */ 2625922dab61SIlya Dryomov } notify; 2626922dab61SIlya Dryomov struct { 2627922dab61SIlya Dryomov int err; 2628922dab61SIlya Dryomov } error; 2629922dab61SIlya Dryomov }; 2630922dab61SIlya Dryomov }; 2631922dab61SIlya Dryomov 2632922dab61SIlya Dryomov static struct linger_work *lwork_alloc(struct ceph_osd_linger_request *lreq, 2633922dab61SIlya Dryomov work_func_t workfn) 2634922dab61SIlya Dryomov { 2635922dab61SIlya Dryomov struct linger_work *lwork; 2636922dab61SIlya Dryomov 2637922dab61SIlya Dryomov lwork = kzalloc(sizeof(*lwork), GFP_NOIO); 2638922dab61SIlya Dryomov if (!lwork) 2639922dab61SIlya Dryomov return NULL; 2640922dab61SIlya Dryomov 2641922dab61SIlya Dryomov INIT_WORK(&lwork->work, workfn); 2642b07d3c4bSIlya Dryomov INIT_LIST_HEAD(&lwork->pending_item); 2643922dab61SIlya Dryomov lwork->lreq = linger_get(lreq); 2644922dab61SIlya Dryomov 2645922dab61SIlya Dryomov return lwork; 2646922dab61SIlya Dryomov } 2647922dab61SIlya Dryomov 2648922dab61SIlya Dryomov static void lwork_free(struct linger_work *lwork) 2649922dab61SIlya Dryomov { 2650922dab61SIlya Dryomov struct ceph_osd_linger_request *lreq = lwork->lreq; 2651922dab61SIlya Dryomov 2652b07d3c4bSIlya Dryomov mutex_lock(&lreq->lock); 2653b07d3c4bSIlya Dryomov list_del(&lwork->pending_item); 2654b07d3c4bSIlya Dryomov mutex_unlock(&lreq->lock); 2655b07d3c4bSIlya Dryomov 2656922dab61SIlya Dryomov linger_put(lreq); 2657922dab61SIlya Dryomov kfree(lwork); 2658922dab61SIlya Dryomov } 2659922dab61SIlya Dryomov 2660922dab61SIlya Dryomov static void lwork_queue(struct linger_work *lwork) 2661922dab61SIlya Dryomov { 2662922dab61SIlya Dryomov struct ceph_osd_linger_request *lreq = lwork->lreq; 2663922dab61SIlya Dryomov struct ceph_osd_client *osdc = lreq->osdc; 2664922dab61SIlya Dryomov 2665922dab61SIlya Dryomov verify_lreq_locked(lreq); 2666b07d3c4bSIlya Dryomov WARN_ON(!list_empty(&lwork->pending_item)); 2667b07d3c4bSIlya Dryomov 2668b07d3c4bSIlya Dryomov lwork->queued_stamp = jiffies; 2669b07d3c4bSIlya Dryomov list_add_tail(&lwork->pending_item, &lreq->pending_lworks); 2670922dab61SIlya Dryomov queue_work(osdc->notify_wq, &lwork->work); 2671922dab61SIlya Dryomov } 2672922dab61SIlya Dryomov 2673922dab61SIlya Dryomov static void do_watch_notify(struct work_struct *w) 2674922dab61SIlya Dryomov { 2675922dab61SIlya Dryomov struct linger_work *lwork = container_of(w, struct linger_work, work); 2676922dab61SIlya Dryomov struct ceph_osd_linger_request *lreq = lwork->lreq; 2677922dab61SIlya Dryomov 2678922dab61SIlya Dryomov if (!linger_registered(lreq)) { 2679922dab61SIlya Dryomov dout("%s lreq %p not registered\n", __func__, lreq); 2680922dab61SIlya Dryomov goto out; 2681922dab61SIlya Dryomov } 2682922dab61SIlya Dryomov 268319079203SIlya Dryomov WARN_ON(!lreq->is_watch); 2684922dab61SIlya Dryomov dout("%s lreq %p notify_id %llu notifier_id %llu payload_len %zu\n", 2685922dab61SIlya Dryomov __func__, lreq, lwork->notify.notify_id, lwork->notify.notifier_id, 2686922dab61SIlya Dryomov lwork->notify.payload_len); 2687922dab61SIlya Dryomov lreq->wcb(lreq->data, lwork->notify.notify_id, lreq->linger_id, 2688922dab61SIlya Dryomov lwork->notify.notifier_id, lwork->notify.payload, 2689922dab61SIlya Dryomov lwork->notify.payload_len); 2690922dab61SIlya Dryomov 2691922dab61SIlya Dryomov out: 2692922dab61SIlya Dryomov ceph_msg_put(lwork->notify.msg); 2693922dab61SIlya Dryomov lwork_free(lwork); 2694922dab61SIlya Dryomov } 2695922dab61SIlya Dryomov 2696922dab61SIlya Dryomov static void do_watch_error(struct work_struct *w) 2697922dab61SIlya Dryomov { 2698922dab61SIlya Dryomov struct linger_work *lwork = container_of(w, struct linger_work, work); 2699922dab61SIlya Dryomov struct ceph_osd_linger_request *lreq = lwork->lreq; 2700922dab61SIlya Dryomov 2701922dab61SIlya Dryomov if (!linger_registered(lreq)) { 2702922dab61SIlya Dryomov dout("%s lreq %p not registered\n", __func__, lreq); 2703922dab61SIlya Dryomov goto out; 2704922dab61SIlya Dryomov } 2705922dab61SIlya Dryomov 2706922dab61SIlya Dryomov dout("%s lreq %p err %d\n", __func__, lreq, lwork->error.err); 2707922dab61SIlya Dryomov lreq->errcb(lreq->data, lreq->linger_id, lwork->error.err); 2708922dab61SIlya Dryomov 2709922dab61SIlya Dryomov out: 2710922dab61SIlya Dryomov lwork_free(lwork); 2711922dab61SIlya Dryomov } 2712922dab61SIlya Dryomov 2713922dab61SIlya Dryomov static void queue_watch_error(struct ceph_osd_linger_request *lreq) 2714922dab61SIlya Dryomov { 2715922dab61SIlya Dryomov struct linger_work *lwork; 2716922dab61SIlya Dryomov 2717922dab61SIlya Dryomov lwork = lwork_alloc(lreq, do_watch_error); 2718922dab61SIlya Dryomov if (!lwork) { 2719922dab61SIlya Dryomov pr_err("failed to allocate error-lwork\n"); 2720922dab61SIlya Dryomov return; 2721922dab61SIlya Dryomov } 2722922dab61SIlya Dryomov 2723922dab61SIlya Dryomov lwork->error.err = lreq->last_error; 2724922dab61SIlya Dryomov lwork_queue(lwork); 2725922dab61SIlya Dryomov } 2726922dab61SIlya Dryomov 2727922dab61SIlya Dryomov static void linger_reg_commit_complete(struct ceph_osd_linger_request *lreq, 2728922dab61SIlya Dryomov int result) 2729922dab61SIlya Dryomov { 2730922dab61SIlya Dryomov if (!completion_done(&lreq->reg_commit_wait)) { 2731922dab61SIlya Dryomov lreq->reg_commit_error = (result <= 0 ? result : 0); 2732922dab61SIlya Dryomov complete_all(&lreq->reg_commit_wait); 2733922dab61SIlya Dryomov } 2734922dab61SIlya Dryomov } 2735922dab61SIlya Dryomov 2736922dab61SIlya Dryomov static void linger_commit_cb(struct ceph_osd_request *req) 2737922dab61SIlya Dryomov { 2738922dab61SIlya Dryomov struct ceph_osd_linger_request *lreq = req->r_priv; 2739922dab61SIlya Dryomov 2740922dab61SIlya Dryomov mutex_lock(&lreq->lock); 2741922dab61SIlya Dryomov dout("%s lreq %p linger_id %llu result %d\n", __func__, lreq, 2742922dab61SIlya Dryomov lreq->linger_id, req->r_result); 2743922dab61SIlya Dryomov linger_reg_commit_complete(lreq, req->r_result); 2744922dab61SIlya Dryomov lreq->committed = true; 2745922dab61SIlya Dryomov 274619079203SIlya Dryomov if (!lreq->is_watch) { 274719079203SIlya Dryomov struct ceph_osd_data *osd_data = 274819079203SIlya Dryomov osd_req_op_data(req, 0, notify, response_data); 274919079203SIlya Dryomov void *p = page_address(osd_data->pages[0]); 275019079203SIlya Dryomov 275119079203SIlya Dryomov WARN_ON(req->r_ops[0].op != CEPH_OSD_OP_NOTIFY || 275219079203SIlya Dryomov osd_data->type != CEPH_OSD_DATA_TYPE_PAGES); 275319079203SIlya Dryomov 275419079203SIlya Dryomov /* make note of the notify_id */ 275519079203SIlya Dryomov if (req->r_ops[0].outdata_len >= sizeof(u64)) { 275619079203SIlya Dryomov lreq->notify_id = ceph_decode_64(&p); 275719079203SIlya Dryomov dout("lreq %p notify_id %llu\n", lreq, 275819079203SIlya Dryomov lreq->notify_id); 275919079203SIlya Dryomov } else { 276019079203SIlya Dryomov dout("lreq %p no notify_id\n", lreq); 276119079203SIlya Dryomov } 276219079203SIlya Dryomov } 276319079203SIlya Dryomov 2764922dab61SIlya Dryomov mutex_unlock(&lreq->lock); 2765922dab61SIlya Dryomov linger_put(lreq); 2766922dab61SIlya Dryomov } 2767922dab61SIlya Dryomov 2768922dab61SIlya Dryomov static int normalize_watch_error(int err) 2769922dab61SIlya Dryomov { 2770922dab61SIlya Dryomov /* 2771922dab61SIlya Dryomov * Translate ENOENT -> ENOTCONN so that a delete->disconnection 2772922dab61SIlya Dryomov * notification and a failure to reconnect because we raced with 2773922dab61SIlya Dryomov * the delete appear the same to the user. 2774922dab61SIlya Dryomov */ 2775922dab61SIlya Dryomov if (err == -ENOENT) 2776922dab61SIlya Dryomov err = -ENOTCONN; 2777922dab61SIlya Dryomov 2778922dab61SIlya Dryomov return err; 2779922dab61SIlya Dryomov } 2780922dab61SIlya Dryomov 2781922dab61SIlya Dryomov static void linger_reconnect_cb(struct ceph_osd_request *req) 2782922dab61SIlya Dryomov { 2783922dab61SIlya Dryomov struct ceph_osd_linger_request *lreq = req->r_priv; 2784922dab61SIlya Dryomov 2785922dab61SIlya Dryomov mutex_lock(&lreq->lock); 2786922dab61SIlya Dryomov dout("%s lreq %p linger_id %llu result %d last_error %d\n", __func__, 2787922dab61SIlya Dryomov lreq, lreq->linger_id, req->r_result, lreq->last_error); 2788922dab61SIlya Dryomov if (req->r_result < 0) { 2789922dab61SIlya Dryomov if (!lreq->last_error) { 2790922dab61SIlya Dryomov lreq->last_error = normalize_watch_error(req->r_result); 2791922dab61SIlya Dryomov queue_watch_error(lreq); 2792922dab61SIlya Dryomov } 2793922dab61SIlya Dryomov } 2794922dab61SIlya Dryomov 2795922dab61SIlya Dryomov mutex_unlock(&lreq->lock); 2796922dab61SIlya Dryomov linger_put(lreq); 2797922dab61SIlya Dryomov } 2798922dab61SIlya Dryomov 2799922dab61SIlya Dryomov static void send_linger(struct ceph_osd_linger_request *lreq) 2800922dab61SIlya Dryomov { 2801922dab61SIlya Dryomov struct ceph_osd_request *req = lreq->reg_req; 2802922dab61SIlya Dryomov struct ceph_osd_req_op *op = &req->r_ops[0]; 2803922dab61SIlya Dryomov 2804922dab61SIlya Dryomov verify_osdc_wrlocked(req->r_osdc); 2805922dab61SIlya Dryomov dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id); 2806922dab61SIlya Dryomov 2807922dab61SIlya Dryomov if (req->r_osd) 2808922dab61SIlya Dryomov cancel_linger_request(req); 2809922dab61SIlya Dryomov 2810922dab61SIlya Dryomov request_reinit(req); 2811922dab61SIlya Dryomov ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid); 2812922dab61SIlya Dryomov ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc); 2813922dab61SIlya Dryomov req->r_flags = lreq->t.flags; 2814922dab61SIlya Dryomov req->r_mtime = lreq->mtime; 2815922dab61SIlya Dryomov 2816922dab61SIlya Dryomov mutex_lock(&lreq->lock); 281719079203SIlya Dryomov if (lreq->is_watch && lreq->committed) { 2818922dab61SIlya Dryomov WARN_ON(op->op != CEPH_OSD_OP_WATCH || 2819922dab61SIlya Dryomov op->watch.cookie != lreq->linger_id); 2820922dab61SIlya Dryomov op->watch.op = CEPH_OSD_WATCH_OP_RECONNECT; 2821922dab61SIlya Dryomov op->watch.gen = ++lreq->register_gen; 2822922dab61SIlya Dryomov dout("lreq %p reconnect register_gen %u\n", lreq, 2823922dab61SIlya Dryomov op->watch.gen); 2824922dab61SIlya Dryomov req->r_callback = linger_reconnect_cb; 2825922dab61SIlya Dryomov } else { 282619079203SIlya Dryomov if (!lreq->is_watch) 282719079203SIlya Dryomov lreq->notify_id = 0; 282819079203SIlya Dryomov else 2829922dab61SIlya Dryomov WARN_ON(op->watch.op != CEPH_OSD_WATCH_OP_WATCH); 2830922dab61SIlya Dryomov dout("lreq %p register\n", lreq); 2831922dab61SIlya Dryomov req->r_callback = linger_commit_cb; 2832922dab61SIlya Dryomov } 2833922dab61SIlya Dryomov mutex_unlock(&lreq->lock); 2834922dab61SIlya Dryomov 2835922dab61SIlya Dryomov req->r_priv = linger_get(lreq); 2836922dab61SIlya Dryomov req->r_linger = true; 2837922dab61SIlya Dryomov 2838922dab61SIlya Dryomov submit_request(req, true); 2839922dab61SIlya Dryomov } 2840922dab61SIlya Dryomov 2841922dab61SIlya Dryomov static void linger_ping_cb(struct ceph_osd_request *req) 2842922dab61SIlya Dryomov { 2843922dab61SIlya Dryomov struct ceph_osd_linger_request *lreq = req->r_priv; 2844922dab61SIlya Dryomov 2845922dab61SIlya Dryomov mutex_lock(&lreq->lock); 2846922dab61SIlya Dryomov dout("%s lreq %p linger_id %llu result %d ping_sent %lu last_error %d\n", 2847922dab61SIlya Dryomov __func__, lreq, lreq->linger_id, req->r_result, lreq->ping_sent, 2848922dab61SIlya Dryomov lreq->last_error); 2849922dab61SIlya Dryomov if (lreq->register_gen == req->r_ops[0].watch.gen) { 2850b07d3c4bSIlya Dryomov if (!req->r_result) { 2851b07d3c4bSIlya Dryomov lreq->watch_valid_thru = lreq->ping_sent; 2852b07d3c4bSIlya Dryomov } else if (!lreq->last_error) { 2853922dab61SIlya Dryomov lreq->last_error = normalize_watch_error(req->r_result); 2854922dab61SIlya Dryomov queue_watch_error(lreq); 2855922dab61SIlya Dryomov } 2856922dab61SIlya Dryomov } else { 2857922dab61SIlya Dryomov dout("lreq %p register_gen %u ignoring old pong %u\n", lreq, 2858922dab61SIlya Dryomov lreq->register_gen, req->r_ops[0].watch.gen); 2859922dab61SIlya Dryomov } 2860922dab61SIlya Dryomov 2861922dab61SIlya Dryomov mutex_unlock(&lreq->lock); 2862922dab61SIlya Dryomov linger_put(lreq); 2863922dab61SIlya Dryomov } 2864922dab61SIlya Dryomov 2865922dab61SIlya Dryomov static void send_linger_ping(struct ceph_osd_linger_request *lreq) 2866922dab61SIlya Dryomov { 2867922dab61SIlya Dryomov struct ceph_osd_client *osdc = lreq->osdc; 2868922dab61SIlya Dryomov struct ceph_osd_request *req = lreq->ping_req; 2869922dab61SIlya Dryomov struct ceph_osd_req_op *op = &req->r_ops[0]; 2870922dab61SIlya Dryomov 2871b7ec35b3SIlya Dryomov if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) { 2872922dab61SIlya Dryomov dout("%s PAUSERD\n", __func__); 2873922dab61SIlya Dryomov return; 2874922dab61SIlya Dryomov } 2875922dab61SIlya Dryomov 2876922dab61SIlya Dryomov lreq->ping_sent = jiffies; 2877922dab61SIlya Dryomov dout("%s lreq %p linger_id %llu ping_sent %lu register_gen %u\n", 2878922dab61SIlya Dryomov __func__, lreq, lreq->linger_id, lreq->ping_sent, 2879922dab61SIlya Dryomov lreq->register_gen); 2880922dab61SIlya Dryomov 2881922dab61SIlya Dryomov if (req->r_osd) 2882922dab61SIlya Dryomov cancel_linger_request(req); 2883922dab61SIlya Dryomov 2884922dab61SIlya Dryomov request_reinit(req); 2885922dab61SIlya Dryomov target_copy(&req->r_t, &lreq->t); 2886922dab61SIlya Dryomov 2887922dab61SIlya Dryomov WARN_ON(op->op != CEPH_OSD_OP_WATCH || 2888922dab61SIlya Dryomov op->watch.cookie != lreq->linger_id || 2889922dab61SIlya Dryomov op->watch.op != CEPH_OSD_WATCH_OP_PING); 2890922dab61SIlya Dryomov op->watch.gen = lreq->register_gen; 2891922dab61SIlya Dryomov req->r_callback = linger_ping_cb; 2892922dab61SIlya Dryomov req->r_priv = linger_get(lreq); 2893922dab61SIlya Dryomov req->r_linger = true; 2894922dab61SIlya Dryomov 2895922dab61SIlya Dryomov ceph_osdc_get_request(req); 2896922dab61SIlya Dryomov account_request(req); 2897922dab61SIlya Dryomov req->r_tid = atomic64_inc_return(&osdc->last_tid); 2898922dab61SIlya Dryomov link_request(lreq->osd, req); 2899922dab61SIlya Dryomov send_request(req); 2900922dab61SIlya Dryomov } 2901922dab61SIlya Dryomov 2902922dab61SIlya Dryomov static void linger_submit(struct ceph_osd_linger_request *lreq) 2903922dab61SIlya Dryomov { 2904922dab61SIlya Dryomov struct ceph_osd_client *osdc = lreq->osdc; 2905922dab61SIlya Dryomov struct ceph_osd *osd; 2906922dab61SIlya Dryomov 29077de030d6SIlya Dryomov calc_target(osdc, &lreq->t, NULL, false); 2908922dab61SIlya Dryomov osd = lookup_create_osd(osdc, lreq->t.osd, true); 2909922dab61SIlya Dryomov link_linger(osd, lreq); 2910922dab61SIlya Dryomov 2911922dab61SIlya Dryomov send_linger(lreq); 2912922dab61SIlya Dryomov } 2913922dab61SIlya Dryomov 29144609245eSIlya Dryomov static void cancel_linger_map_check(struct ceph_osd_linger_request *lreq) 29154609245eSIlya Dryomov { 29164609245eSIlya Dryomov struct ceph_osd_client *osdc = lreq->osdc; 29174609245eSIlya Dryomov struct ceph_osd_linger_request *lookup_lreq; 29184609245eSIlya Dryomov 29194609245eSIlya Dryomov verify_osdc_wrlocked(osdc); 29204609245eSIlya Dryomov 29214609245eSIlya Dryomov lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks, 29224609245eSIlya Dryomov lreq->linger_id); 29234609245eSIlya Dryomov if (!lookup_lreq) 29244609245eSIlya Dryomov return; 29254609245eSIlya Dryomov 29264609245eSIlya Dryomov WARN_ON(lookup_lreq != lreq); 29274609245eSIlya Dryomov erase_linger_mc(&osdc->linger_map_checks, lreq); 29284609245eSIlya Dryomov linger_put(lreq); 29294609245eSIlya Dryomov } 29304609245eSIlya Dryomov 2931922dab61SIlya Dryomov /* 2932922dab61SIlya Dryomov * @lreq has to be both registered and linked. 2933922dab61SIlya Dryomov */ 2934922dab61SIlya Dryomov static void __linger_cancel(struct ceph_osd_linger_request *lreq) 2935922dab61SIlya Dryomov { 293619079203SIlya Dryomov if (lreq->is_watch && lreq->ping_req->r_osd) 2937922dab61SIlya Dryomov cancel_linger_request(lreq->ping_req); 2938922dab61SIlya Dryomov if (lreq->reg_req->r_osd) 2939922dab61SIlya Dryomov cancel_linger_request(lreq->reg_req); 29404609245eSIlya Dryomov cancel_linger_map_check(lreq); 2941922dab61SIlya Dryomov unlink_linger(lreq->osd, lreq); 2942922dab61SIlya Dryomov linger_unregister(lreq); 2943922dab61SIlya Dryomov } 2944922dab61SIlya Dryomov 2945922dab61SIlya Dryomov static void linger_cancel(struct ceph_osd_linger_request *lreq) 2946922dab61SIlya Dryomov { 2947922dab61SIlya Dryomov struct ceph_osd_client *osdc = lreq->osdc; 2948922dab61SIlya Dryomov 2949922dab61SIlya Dryomov down_write(&osdc->lock); 2950922dab61SIlya Dryomov if (__linger_registered(lreq)) 2951922dab61SIlya Dryomov __linger_cancel(lreq); 2952922dab61SIlya Dryomov up_write(&osdc->lock); 2953922dab61SIlya Dryomov } 2954922dab61SIlya Dryomov 29554609245eSIlya Dryomov static void send_linger_map_check(struct ceph_osd_linger_request *lreq); 29564609245eSIlya Dryomov 29574609245eSIlya Dryomov static void check_linger_pool_dne(struct ceph_osd_linger_request *lreq) 29584609245eSIlya Dryomov { 29594609245eSIlya Dryomov struct ceph_osd_client *osdc = lreq->osdc; 29604609245eSIlya Dryomov struct ceph_osdmap *map = osdc->osdmap; 29614609245eSIlya Dryomov 29624609245eSIlya Dryomov verify_osdc_wrlocked(osdc); 29634609245eSIlya Dryomov WARN_ON(!map->epoch); 29644609245eSIlya Dryomov 29654609245eSIlya Dryomov if (lreq->register_gen) { 29664609245eSIlya Dryomov lreq->map_dne_bound = map->epoch; 29674609245eSIlya Dryomov dout("%s lreq %p linger_id %llu pool disappeared\n", __func__, 29684609245eSIlya Dryomov lreq, lreq->linger_id); 29694609245eSIlya Dryomov } else { 29704609245eSIlya Dryomov dout("%s lreq %p linger_id %llu map_dne_bound %u have %u\n", 29714609245eSIlya Dryomov __func__, lreq, lreq->linger_id, lreq->map_dne_bound, 29724609245eSIlya Dryomov map->epoch); 29734609245eSIlya Dryomov } 29744609245eSIlya Dryomov 29754609245eSIlya Dryomov if (lreq->map_dne_bound) { 29764609245eSIlya Dryomov if (map->epoch >= lreq->map_dne_bound) { 29774609245eSIlya Dryomov /* we had a new enough map */ 29784609245eSIlya Dryomov pr_info("linger_id %llu pool does not exist\n", 29794609245eSIlya Dryomov lreq->linger_id); 29804609245eSIlya Dryomov linger_reg_commit_complete(lreq, -ENOENT); 29814609245eSIlya Dryomov __linger_cancel(lreq); 29824609245eSIlya Dryomov } 29834609245eSIlya Dryomov } else { 29844609245eSIlya Dryomov send_linger_map_check(lreq); 29854609245eSIlya Dryomov } 29864609245eSIlya Dryomov } 29874609245eSIlya Dryomov 29884609245eSIlya Dryomov static void linger_map_check_cb(struct ceph_mon_generic_request *greq) 29894609245eSIlya Dryomov { 29904609245eSIlya Dryomov struct ceph_osd_client *osdc = &greq->monc->client->osdc; 29914609245eSIlya Dryomov struct ceph_osd_linger_request *lreq; 29924609245eSIlya Dryomov u64 linger_id = greq->private_data; 29934609245eSIlya Dryomov 29944609245eSIlya Dryomov WARN_ON(greq->result || !greq->u.newest); 29954609245eSIlya Dryomov 29964609245eSIlya Dryomov down_write(&osdc->lock); 29974609245eSIlya Dryomov lreq = lookup_linger_mc(&osdc->linger_map_checks, linger_id); 29984609245eSIlya Dryomov if (!lreq) { 29994609245eSIlya Dryomov dout("%s linger_id %llu dne\n", __func__, linger_id); 30004609245eSIlya Dryomov goto out_unlock; 30014609245eSIlya Dryomov } 30024609245eSIlya Dryomov 30034609245eSIlya Dryomov dout("%s lreq %p linger_id %llu map_dne_bound %u newest %llu\n", 30044609245eSIlya Dryomov __func__, lreq, lreq->linger_id, lreq->map_dne_bound, 30054609245eSIlya Dryomov greq->u.newest); 30064609245eSIlya Dryomov if (!lreq->map_dne_bound) 30074609245eSIlya Dryomov lreq->map_dne_bound = greq->u.newest; 30084609245eSIlya Dryomov erase_linger_mc(&osdc->linger_map_checks, lreq); 30094609245eSIlya Dryomov check_linger_pool_dne(lreq); 30104609245eSIlya Dryomov 30114609245eSIlya Dryomov linger_put(lreq); 30124609245eSIlya Dryomov out_unlock: 30134609245eSIlya Dryomov up_write(&osdc->lock); 30144609245eSIlya Dryomov } 30154609245eSIlya Dryomov 30164609245eSIlya Dryomov static void send_linger_map_check(struct ceph_osd_linger_request *lreq) 30174609245eSIlya Dryomov { 30184609245eSIlya Dryomov struct ceph_osd_client *osdc = lreq->osdc; 30194609245eSIlya Dryomov struct ceph_osd_linger_request *lookup_lreq; 30204609245eSIlya Dryomov int ret; 30214609245eSIlya Dryomov 30224609245eSIlya Dryomov verify_osdc_wrlocked(osdc); 30234609245eSIlya Dryomov 30244609245eSIlya Dryomov lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks, 30254609245eSIlya Dryomov lreq->linger_id); 30264609245eSIlya Dryomov if (lookup_lreq) { 30274609245eSIlya Dryomov WARN_ON(lookup_lreq != lreq); 30284609245eSIlya Dryomov return; 30294609245eSIlya Dryomov } 30304609245eSIlya Dryomov 30314609245eSIlya Dryomov linger_get(lreq); 30324609245eSIlya Dryomov insert_linger_mc(&osdc->linger_map_checks, lreq); 30334609245eSIlya Dryomov ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap", 30344609245eSIlya Dryomov linger_map_check_cb, lreq->linger_id); 30354609245eSIlya Dryomov WARN_ON(ret); 30364609245eSIlya Dryomov } 30374609245eSIlya Dryomov 3038922dab61SIlya Dryomov static int linger_reg_commit_wait(struct ceph_osd_linger_request *lreq) 3039922dab61SIlya Dryomov { 3040922dab61SIlya Dryomov int ret; 3041922dab61SIlya Dryomov 3042922dab61SIlya Dryomov dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id); 3043922dab61SIlya Dryomov ret = wait_for_completion_interruptible(&lreq->reg_commit_wait); 3044922dab61SIlya Dryomov return ret ?: lreq->reg_commit_error; 3045922dab61SIlya Dryomov } 3046922dab61SIlya Dryomov 304719079203SIlya Dryomov static int linger_notify_finish_wait(struct ceph_osd_linger_request *lreq) 304819079203SIlya Dryomov { 304919079203SIlya Dryomov int ret; 305019079203SIlya Dryomov 305119079203SIlya Dryomov dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id); 305219079203SIlya Dryomov ret = wait_for_completion_interruptible(&lreq->notify_finish_wait); 305319079203SIlya Dryomov return ret ?: lreq->notify_finish_error; 305419079203SIlya Dryomov } 305519079203SIlya Dryomov 3056922dab61SIlya Dryomov /* 3057fbca9635SIlya Dryomov * Timeout callback, called every N seconds. When 1 or more OSD 3058fbca9635SIlya Dryomov * requests has been active for more than N seconds, we send a keepalive 3059fbca9635SIlya Dryomov * (tag + timestamp) to its OSD to ensure any communications channel 3060fbca9635SIlya Dryomov * reset is detected. 30613d14c5d2SYehuda Sadeh */ 30623d14c5d2SYehuda Sadeh static void handle_timeout(struct work_struct *work) 30633d14c5d2SYehuda Sadeh { 30643d14c5d2SYehuda Sadeh struct ceph_osd_client *osdc = 30653d14c5d2SYehuda Sadeh container_of(work, struct ceph_osd_client, timeout_work.work); 3066a319bf56SIlya Dryomov struct ceph_options *opts = osdc->client->options; 30675aea3dcdSIlya Dryomov unsigned long cutoff = jiffies - opts->osd_keepalive_timeout; 30687cc5e38fSIlya Dryomov unsigned long expiry_cutoff = jiffies - opts->osd_request_timeout; 30695aea3dcdSIlya Dryomov LIST_HEAD(slow_osds); 30705aea3dcdSIlya Dryomov struct rb_node *n, *p; 30713d14c5d2SYehuda Sadeh 30725aea3dcdSIlya Dryomov dout("%s osdc %p\n", __func__, osdc); 30735aea3dcdSIlya Dryomov down_write(&osdc->lock); 30743d14c5d2SYehuda Sadeh 30753d14c5d2SYehuda Sadeh /* 30763d14c5d2SYehuda Sadeh * ping osds that are a bit slow. this ensures that if there 30773d14c5d2SYehuda Sadeh * is a break in the TCP connection we will notice, and reopen 30783d14c5d2SYehuda Sadeh * a connection with that osd (from the fault callback). 30793d14c5d2SYehuda Sadeh */ 30805aea3dcdSIlya Dryomov for (n = rb_first(&osdc->osds); n; n = rb_next(n)) { 30815aea3dcdSIlya Dryomov struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node); 30825aea3dcdSIlya Dryomov bool found = false; 30833d14c5d2SYehuda Sadeh 30847cc5e38fSIlya Dryomov for (p = rb_first(&osd->o_requests); p; ) { 30855aea3dcdSIlya Dryomov struct ceph_osd_request *req = 30865aea3dcdSIlya Dryomov rb_entry(p, struct ceph_osd_request, r_node); 30875aea3dcdSIlya Dryomov 30887cc5e38fSIlya Dryomov p = rb_next(p); /* abort_request() */ 30897cc5e38fSIlya Dryomov 30905aea3dcdSIlya Dryomov if (time_before(req->r_stamp, cutoff)) { 30915aea3dcdSIlya Dryomov dout(" req %p tid %llu on osd%d is laggy\n", 30925aea3dcdSIlya Dryomov req, req->r_tid, osd->o_osd); 30935aea3dcdSIlya Dryomov found = true; 30945aea3dcdSIlya Dryomov } 30957cc5e38fSIlya Dryomov if (opts->osd_request_timeout && 30967cc5e38fSIlya Dryomov time_before(req->r_start_stamp, expiry_cutoff)) { 30977cc5e38fSIlya Dryomov pr_err_ratelimited("tid %llu on osd%d timeout\n", 30987cc5e38fSIlya Dryomov req->r_tid, osd->o_osd); 30997cc5e38fSIlya Dryomov abort_request(req, -ETIMEDOUT); 31007cc5e38fSIlya Dryomov } 31015aea3dcdSIlya Dryomov } 3102922dab61SIlya Dryomov for (p = rb_first(&osd->o_linger_requests); p; p = rb_next(p)) { 3103922dab61SIlya Dryomov struct ceph_osd_linger_request *lreq = 3104922dab61SIlya Dryomov rb_entry(p, struct ceph_osd_linger_request, node); 3105922dab61SIlya Dryomov 3106922dab61SIlya Dryomov dout(" lreq %p linger_id %llu is served by osd%d\n", 3107922dab61SIlya Dryomov lreq, lreq->linger_id, osd->o_osd); 3108922dab61SIlya Dryomov found = true; 3109922dab61SIlya Dryomov 3110922dab61SIlya Dryomov mutex_lock(&lreq->lock); 311119079203SIlya Dryomov if (lreq->is_watch && lreq->committed && !lreq->last_error) 3112922dab61SIlya Dryomov send_linger_ping(lreq); 3113922dab61SIlya Dryomov mutex_unlock(&lreq->lock); 3114922dab61SIlya Dryomov } 31155aea3dcdSIlya Dryomov 31165aea3dcdSIlya Dryomov if (found) 31173d14c5d2SYehuda Sadeh list_move_tail(&osd->o_keepalive_item, &slow_osds); 31183d14c5d2SYehuda Sadeh } 31195aea3dcdSIlya Dryomov 31207cc5e38fSIlya Dryomov if (opts->osd_request_timeout) { 31217cc5e38fSIlya Dryomov for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) { 31227cc5e38fSIlya Dryomov struct ceph_osd_request *req = 31237cc5e38fSIlya Dryomov rb_entry(p, struct ceph_osd_request, r_node); 31247cc5e38fSIlya Dryomov 31257cc5e38fSIlya Dryomov p = rb_next(p); /* abort_request() */ 31267cc5e38fSIlya Dryomov 31277cc5e38fSIlya Dryomov if (time_before(req->r_start_stamp, expiry_cutoff)) { 31287cc5e38fSIlya Dryomov pr_err_ratelimited("tid %llu on osd%d timeout\n", 31297cc5e38fSIlya Dryomov req->r_tid, osdc->homeless_osd.o_osd); 31307cc5e38fSIlya Dryomov abort_request(req, -ETIMEDOUT); 31317cc5e38fSIlya Dryomov } 31327cc5e38fSIlya Dryomov } 31337cc5e38fSIlya Dryomov } 31347cc5e38fSIlya Dryomov 31355aea3dcdSIlya Dryomov if (atomic_read(&osdc->num_homeless) || !list_empty(&slow_osds)) 31365aea3dcdSIlya Dryomov maybe_request_map(osdc); 31375aea3dcdSIlya Dryomov 31383d14c5d2SYehuda Sadeh while (!list_empty(&slow_osds)) { 31395aea3dcdSIlya Dryomov struct ceph_osd *osd = list_first_entry(&slow_osds, 31405aea3dcdSIlya Dryomov struct ceph_osd, 31413d14c5d2SYehuda Sadeh o_keepalive_item); 31423d14c5d2SYehuda Sadeh list_del_init(&osd->o_keepalive_item); 31433d14c5d2SYehuda Sadeh ceph_con_keepalive(&osd->o_con); 31443d14c5d2SYehuda Sadeh } 31453d14c5d2SYehuda Sadeh 31465aea3dcdSIlya Dryomov up_write(&osdc->lock); 3147fbca9635SIlya Dryomov schedule_delayed_work(&osdc->timeout_work, 3148fbca9635SIlya Dryomov osdc->client->options->osd_keepalive_timeout); 31493d14c5d2SYehuda Sadeh } 31503d14c5d2SYehuda Sadeh 31513d14c5d2SYehuda Sadeh static void handle_osds_timeout(struct work_struct *work) 31523d14c5d2SYehuda Sadeh { 31533d14c5d2SYehuda Sadeh struct ceph_osd_client *osdc = 31543d14c5d2SYehuda Sadeh container_of(work, struct ceph_osd_client, 31553d14c5d2SYehuda Sadeh osds_timeout_work.work); 3156a319bf56SIlya Dryomov unsigned long delay = osdc->client->options->osd_idle_ttl / 4; 315742a2c09fSIlya Dryomov struct ceph_osd *osd, *nosd; 31583d14c5d2SYehuda Sadeh 315942a2c09fSIlya Dryomov dout("%s osdc %p\n", __func__, osdc); 31605aea3dcdSIlya Dryomov down_write(&osdc->lock); 316142a2c09fSIlya Dryomov list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) { 316242a2c09fSIlya Dryomov if (time_before(jiffies, osd->lru_ttl)) 316342a2c09fSIlya Dryomov break; 316442a2c09fSIlya Dryomov 31655aea3dcdSIlya Dryomov WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests)); 3166922dab61SIlya Dryomov WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests)); 31675aea3dcdSIlya Dryomov close_osd(osd); 316842a2c09fSIlya Dryomov } 316942a2c09fSIlya Dryomov 31705aea3dcdSIlya Dryomov up_write(&osdc->lock); 31713d14c5d2SYehuda Sadeh schedule_delayed_work(&osdc->osds_timeout_work, 31723d14c5d2SYehuda Sadeh round_jiffies_relative(delay)); 31733d14c5d2SYehuda Sadeh } 31743d14c5d2SYehuda Sadeh 3175205ee118SIlya Dryomov static int ceph_oloc_decode(void **p, void *end, 3176205ee118SIlya Dryomov struct ceph_object_locator *oloc) 3177205ee118SIlya Dryomov { 3178205ee118SIlya Dryomov u8 struct_v, struct_cv; 3179205ee118SIlya Dryomov u32 len; 3180205ee118SIlya Dryomov void *struct_end; 3181205ee118SIlya Dryomov int ret = 0; 3182205ee118SIlya Dryomov 3183205ee118SIlya Dryomov ceph_decode_need(p, end, 1 + 1 + 4, e_inval); 3184205ee118SIlya Dryomov struct_v = ceph_decode_8(p); 3185205ee118SIlya Dryomov struct_cv = ceph_decode_8(p); 3186205ee118SIlya Dryomov if (struct_v < 3) { 3187205ee118SIlya Dryomov pr_warn("got v %d < 3 cv %d of ceph_object_locator\n", 3188205ee118SIlya Dryomov struct_v, struct_cv); 3189205ee118SIlya Dryomov goto e_inval; 3190205ee118SIlya Dryomov } 3191205ee118SIlya Dryomov if (struct_cv > 6) { 3192205ee118SIlya Dryomov pr_warn("got v %d cv %d > 6 of ceph_object_locator\n", 3193205ee118SIlya Dryomov struct_v, struct_cv); 3194205ee118SIlya Dryomov goto e_inval; 3195205ee118SIlya Dryomov } 3196205ee118SIlya Dryomov len = ceph_decode_32(p); 3197205ee118SIlya Dryomov ceph_decode_need(p, end, len, e_inval); 3198205ee118SIlya Dryomov struct_end = *p + len; 3199205ee118SIlya Dryomov 3200205ee118SIlya Dryomov oloc->pool = ceph_decode_64(p); 3201205ee118SIlya Dryomov *p += 4; /* skip preferred */ 3202205ee118SIlya Dryomov 3203205ee118SIlya Dryomov len = ceph_decode_32(p); 3204205ee118SIlya Dryomov if (len > 0) { 3205205ee118SIlya Dryomov pr_warn("ceph_object_locator::key is set\n"); 3206205ee118SIlya Dryomov goto e_inval; 3207205ee118SIlya Dryomov } 3208205ee118SIlya Dryomov 3209205ee118SIlya Dryomov if (struct_v >= 5) { 3210cd08e0a2SYan, Zheng bool changed = false; 3211cd08e0a2SYan, Zheng 3212205ee118SIlya Dryomov len = ceph_decode_32(p); 3213205ee118SIlya Dryomov if (len > 0) { 321430c156d9SYan, Zheng ceph_decode_need(p, end, len, e_inval); 3215cd08e0a2SYan, Zheng if (!oloc->pool_ns || 3216cd08e0a2SYan, Zheng ceph_compare_string(oloc->pool_ns, *p, len)) 3217cd08e0a2SYan, Zheng changed = true; 321830c156d9SYan, Zheng *p += len; 3219cd08e0a2SYan, Zheng } else { 3220cd08e0a2SYan, Zheng if (oloc->pool_ns) 3221cd08e0a2SYan, Zheng changed = true; 3222cd08e0a2SYan, Zheng } 3223cd08e0a2SYan, Zheng if (changed) { 3224cd08e0a2SYan, Zheng /* redirect changes namespace */ 3225cd08e0a2SYan, Zheng pr_warn("ceph_object_locator::nspace is changed\n"); 3226cd08e0a2SYan, Zheng goto e_inval; 3227205ee118SIlya Dryomov } 3228205ee118SIlya Dryomov } 3229205ee118SIlya Dryomov 3230205ee118SIlya Dryomov if (struct_v >= 6) { 3231205ee118SIlya Dryomov s64 hash = ceph_decode_64(p); 3232205ee118SIlya Dryomov if (hash != -1) { 3233205ee118SIlya Dryomov pr_warn("ceph_object_locator::hash is set\n"); 3234205ee118SIlya Dryomov goto e_inval; 3235205ee118SIlya Dryomov } 3236205ee118SIlya Dryomov } 3237205ee118SIlya Dryomov 3238205ee118SIlya Dryomov /* skip the rest */ 3239205ee118SIlya Dryomov *p = struct_end; 3240205ee118SIlya Dryomov out: 3241205ee118SIlya Dryomov return ret; 3242205ee118SIlya Dryomov 3243205ee118SIlya Dryomov e_inval: 3244205ee118SIlya Dryomov ret = -EINVAL; 3245205ee118SIlya Dryomov goto out; 3246205ee118SIlya Dryomov } 3247205ee118SIlya Dryomov 3248205ee118SIlya Dryomov static int ceph_redirect_decode(void **p, void *end, 3249205ee118SIlya Dryomov struct ceph_request_redirect *redir) 3250205ee118SIlya Dryomov { 3251205ee118SIlya Dryomov u8 struct_v, struct_cv; 3252205ee118SIlya Dryomov u32 len; 3253205ee118SIlya Dryomov void *struct_end; 3254205ee118SIlya Dryomov int ret; 3255205ee118SIlya Dryomov 3256205ee118SIlya Dryomov ceph_decode_need(p, end, 1 + 1 + 4, e_inval); 3257205ee118SIlya Dryomov struct_v = ceph_decode_8(p); 3258205ee118SIlya Dryomov struct_cv = ceph_decode_8(p); 3259205ee118SIlya Dryomov if (struct_cv > 1) { 3260205ee118SIlya Dryomov pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n", 3261205ee118SIlya Dryomov struct_v, struct_cv); 3262205ee118SIlya Dryomov goto e_inval; 3263205ee118SIlya Dryomov } 3264205ee118SIlya Dryomov len = ceph_decode_32(p); 3265205ee118SIlya Dryomov ceph_decode_need(p, end, len, e_inval); 3266205ee118SIlya Dryomov struct_end = *p + len; 3267205ee118SIlya Dryomov 3268205ee118SIlya Dryomov ret = ceph_oloc_decode(p, end, &redir->oloc); 3269205ee118SIlya Dryomov if (ret) 3270205ee118SIlya Dryomov goto out; 3271205ee118SIlya Dryomov 3272205ee118SIlya Dryomov len = ceph_decode_32(p); 3273205ee118SIlya Dryomov if (len > 0) { 3274205ee118SIlya Dryomov pr_warn("ceph_request_redirect::object_name is set\n"); 3275205ee118SIlya Dryomov goto e_inval; 3276205ee118SIlya Dryomov } 3277205ee118SIlya Dryomov 3278205ee118SIlya Dryomov len = ceph_decode_32(p); 3279205ee118SIlya Dryomov *p += len; /* skip osd_instructions */ 3280205ee118SIlya Dryomov 3281205ee118SIlya Dryomov /* skip the rest */ 3282205ee118SIlya Dryomov *p = struct_end; 3283205ee118SIlya Dryomov out: 3284205ee118SIlya Dryomov return ret; 3285205ee118SIlya Dryomov 3286205ee118SIlya Dryomov e_inval: 3287205ee118SIlya Dryomov ret = -EINVAL; 3288205ee118SIlya Dryomov goto out; 3289205ee118SIlya Dryomov } 3290205ee118SIlya Dryomov 3291fe5da05eSIlya Dryomov struct MOSDOpReply { 3292fe5da05eSIlya Dryomov struct ceph_pg pgid; 3293fe5da05eSIlya Dryomov u64 flags; 3294fe5da05eSIlya Dryomov int result; 3295fe5da05eSIlya Dryomov u32 epoch; 3296fe5da05eSIlya Dryomov int num_ops; 3297fe5da05eSIlya Dryomov u32 outdata_len[CEPH_OSD_MAX_OPS]; 3298fe5da05eSIlya Dryomov s32 rval[CEPH_OSD_MAX_OPS]; 3299fe5da05eSIlya Dryomov int retry_attempt; 3300fe5da05eSIlya Dryomov struct ceph_eversion replay_version; 3301fe5da05eSIlya Dryomov u64 user_version; 3302fe5da05eSIlya Dryomov struct ceph_request_redirect redirect; 3303fe5da05eSIlya Dryomov }; 330425845472SSage Weil 3305fe5da05eSIlya Dryomov static int decode_MOSDOpReply(const struct ceph_msg *msg, struct MOSDOpReply *m) 33063d14c5d2SYehuda Sadeh { 3307fe5da05eSIlya Dryomov void *p = msg->front.iov_base; 3308fe5da05eSIlya Dryomov void *const end = p + msg->front.iov_len; 3309fe5da05eSIlya Dryomov u16 version = le16_to_cpu(msg->hdr.version); 3310fe5da05eSIlya Dryomov struct ceph_eversion bad_replay_version; 3311b0b31a8fSIlya Dryomov u8 decode_redir; 3312fe5da05eSIlya Dryomov u32 len; 3313fe5da05eSIlya Dryomov int ret; 3314fe5da05eSIlya Dryomov int i; 33153d14c5d2SYehuda Sadeh 3316fe5da05eSIlya Dryomov ceph_decode_32_safe(&p, end, len, e_inval); 3317fe5da05eSIlya Dryomov ceph_decode_need(&p, end, len, e_inval); 3318fe5da05eSIlya Dryomov p += len; /* skip oid */ 33191b83bef2SSage Weil 3320fe5da05eSIlya Dryomov ret = ceph_decode_pgid(&p, end, &m->pgid); 3321fe5da05eSIlya Dryomov if (ret) 3322fe5da05eSIlya Dryomov return ret; 33231b83bef2SSage Weil 3324fe5da05eSIlya Dryomov ceph_decode_64_safe(&p, end, m->flags, e_inval); 3325fe5da05eSIlya Dryomov ceph_decode_32_safe(&p, end, m->result, e_inval); 3326fe5da05eSIlya Dryomov ceph_decode_need(&p, end, sizeof(bad_replay_version), e_inval); 3327fe5da05eSIlya Dryomov memcpy(&bad_replay_version, p, sizeof(bad_replay_version)); 3328fe5da05eSIlya Dryomov p += sizeof(bad_replay_version); 3329fe5da05eSIlya Dryomov ceph_decode_32_safe(&p, end, m->epoch, e_inval); 33301b83bef2SSage Weil 3331fe5da05eSIlya Dryomov ceph_decode_32_safe(&p, end, m->num_ops, e_inval); 3332fe5da05eSIlya Dryomov if (m->num_ops > ARRAY_SIZE(m->outdata_len)) 3333fe5da05eSIlya Dryomov goto e_inval; 33341b83bef2SSage Weil 3335fe5da05eSIlya Dryomov ceph_decode_need(&p, end, m->num_ops * sizeof(struct ceph_osd_op), 3336fe5da05eSIlya Dryomov e_inval); 3337fe5da05eSIlya Dryomov for (i = 0; i < m->num_ops; i++) { 33381b83bef2SSage Weil struct ceph_osd_op *op = p; 33391b83bef2SSage Weil 3340fe5da05eSIlya Dryomov m->outdata_len[i] = le32_to_cpu(op->payload_len); 33411b83bef2SSage Weil p += sizeof(*op); 33421b83bef2SSage Weil } 3343fe5da05eSIlya Dryomov 3344fe5da05eSIlya Dryomov ceph_decode_32_safe(&p, end, m->retry_attempt, e_inval); 3345fe5da05eSIlya Dryomov for (i = 0; i < m->num_ops; i++) 3346fe5da05eSIlya Dryomov ceph_decode_32_safe(&p, end, m->rval[i], e_inval); 3347fe5da05eSIlya Dryomov 3348fe5da05eSIlya Dryomov if (version >= 5) { 3349fe5da05eSIlya Dryomov ceph_decode_need(&p, end, sizeof(m->replay_version), e_inval); 3350fe5da05eSIlya Dryomov memcpy(&m->replay_version, p, sizeof(m->replay_version)); 3351fe5da05eSIlya Dryomov p += sizeof(m->replay_version); 3352fe5da05eSIlya Dryomov ceph_decode_64_safe(&p, end, m->user_version, e_inval); 3353fe5da05eSIlya Dryomov } else { 3354fe5da05eSIlya Dryomov m->replay_version = bad_replay_version; /* struct */ 3355fe5da05eSIlya Dryomov m->user_version = le64_to_cpu(m->replay_version.version); 33561b83bef2SSage Weil } 33571b83bef2SSage Weil 3358fe5da05eSIlya Dryomov if (version >= 6) { 3359fe5da05eSIlya Dryomov if (version >= 7) 3360fe5da05eSIlya Dryomov ceph_decode_8_safe(&p, end, decode_redir, e_inval); 3361b0b31a8fSIlya Dryomov else 3362b0b31a8fSIlya Dryomov decode_redir = 1; 3363b0b31a8fSIlya Dryomov } else { 3364b0b31a8fSIlya Dryomov decode_redir = 0; 3365b0b31a8fSIlya Dryomov } 3366b0b31a8fSIlya Dryomov 3367b0b31a8fSIlya Dryomov if (decode_redir) { 3368fe5da05eSIlya Dryomov ret = ceph_redirect_decode(&p, end, &m->redirect); 3369fe5da05eSIlya Dryomov if (ret) 3370fe5da05eSIlya Dryomov return ret; 3371205ee118SIlya Dryomov } else { 3372fe5da05eSIlya Dryomov ceph_oloc_init(&m->redirect.oloc); 3373205ee118SIlya Dryomov } 3374205ee118SIlya Dryomov 3375fe5da05eSIlya Dryomov return 0; 3376205ee118SIlya Dryomov 3377fe5da05eSIlya Dryomov e_inval: 3378fe5da05eSIlya Dryomov return -EINVAL; 3379fe5da05eSIlya Dryomov } 3380fe5da05eSIlya Dryomov 3381fe5da05eSIlya Dryomov /* 3382b18b9550SIlya Dryomov * Handle MOSDOpReply. Set ->r_result and call the callback if it is 3383b18b9550SIlya Dryomov * specified. 3384fe5da05eSIlya Dryomov */ 33855aea3dcdSIlya Dryomov static void handle_reply(struct ceph_osd *osd, struct ceph_msg *msg) 3386fe5da05eSIlya Dryomov { 33875aea3dcdSIlya Dryomov struct ceph_osd_client *osdc = osd->o_osdc; 3388fe5da05eSIlya Dryomov struct ceph_osd_request *req; 3389fe5da05eSIlya Dryomov struct MOSDOpReply m; 3390fe5da05eSIlya Dryomov u64 tid = le64_to_cpu(msg->hdr.tid); 3391fe5da05eSIlya Dryomov u32 data_len = 0; 3392fe5da05eSIlya Dryomov int ret; 3393fe5da05eSIlya Dryomov int i; 3394fe5da05eSIlya Dryomov 3395fe5da05eSIlya Dryomov dout("%s msg %p tid %llu\n", __func__, msg, tid); 3396fe5da05eSIlya Dryomov 33975aea3dcdSIlya Dryomov down_read(&osdc->lock); 33985aea3dcdSIlya Dryomov if (!osd_registered(osd)) { 33995aea3dcdSIlya Dryomov dout("%s osd%d unknown\n", __func__, osd->o_osd); 34005aea3dcdSIlya Dryomov goto out_unlock_osdc; 3401fe5da05eSIlya Dryomov } 34025aea3dcdSIlya Dryomov WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num)); 34035aea3dcdSIlya Dryomov 34045aea3dcdSIlya Dryomov mutex_lock(&osd->lock); 34055aea3dcdSIlya Dryomov req = lookup_request(&osd->o_requests, tid); 34065aea3dcdSIlya Dryomov if (!req) { 34075aea3dcdSIlya Dryomov dout("%s osd%d tid %llu unknown\n", __func__, osd->o_osd, tid); 34085aea3dcdSIlya Dryomov goto out_unlock_session; 34095aea3dcdSIlya Dryomov } 3410fe5da05eSIlya Dryomov 3411cd08e0a2SYan, Zheng m.redirect.oloc.pool_ns = req->r_t.target_oloc.pool_ns; 3412fe5da05eSIlya Dryomov ret = decode_MOSDOpReply(msg, &m); 3413cd08e0a2SYan, Zheng m.redirect.oloc.pool_ns = NULL; 3414fe5da05eSIlya Dryomov if (ret) { 3415fe5da05eSIlya Dryomov pr_err("failed to decode MOSDOpReply for tid %llu: %d\n", 3416fe5da05eSIlya Dryomov req->r_tid, ret); 3417fe5da05eSIlya Dryomov ceph_msg_dump(msg); 3418fe5da05eSIlya Dryomov goto fail_request; 3419fe5da05eSIlya Dryomov } 3420fe5da05eSIlya Dryomov dout("%s req %p tid %llu flags 0x%llx pgid %llu.%x epoch %u attempt %d v %u'%llu uv %llu\n", 3421fe5da05eSIlya Dryomov __func__, req, req->r_tid, m.flags, m.pgid.pool, m.pgid.seed, 3422fe5da05eSIlya Dryomov m.epoch, m.retry_attempt, le32_to_cpu(m.replay_version.epoch), 3423fe5da05eSIlya Dryomov le64_to_cpu(m.replay_version.version), m.user_version); 3424fe5da05eSIlya Dryomov 3425fe5da05eSIlya Dryomov if (m.retry_attempt >= 0) { 3426fe5da05eSIlya Dryomov if (m.retry_attempt != req->r_attempts - 1) { 3427fe5da05eSIlya Dryomov dout("req %p tid %llu retry_attempt %d != %d, ignoring\n", 3428fe5da05eSIlya Dryomov req, req->r_tid, m.retry_attempt, 3429fe5da05eSIlya Dryomov req->r_attempts - 1); 34305aea3dcdSIlya Dryomov goto out_unlock_session; 3431fe5da05eSIlya Dryomov } 3432fe5da05eSIlya Dryomov } else { 3433fe5da05eSIlya Dryomov WARN_ON(1); /* MOSDOpReply v4 is assumed */ 3434fe5da05eSIlya Dryomov } 3435fe5da05eSIlya Dryomov 3436fe5da05eSIlya Dryomov if (!ceph_oloc_empty(&m.redirect.oloc)) { 3437fe5da05eSIlya Dryomov dout("req %p tid %llu redirect pool %lld\n", req, req->r_tid, 3438fe5da05eSIlya Dryomov m.redirect.oloc.pool); 34395aea3dcdSIlya Dryomov unlink_request(osd, req); 34405aea3dcdSIlya Dryomov mutex_unlock(&osd->lock); 3441205ee118SIlya Dryomov 344230c156d9SYan, Zheng /* 344330c156d9SYan, Zheng * Not ceph_oloc_copy() - changing pool_ns is not 344430c156d9SYan, Zheng * supported. 344530c156d9SYan, Zheng */ 344630c156d9SYan, Zheng req->r_t.target_oloc.pool = m.redirect.oloc.pool; 34475aea3dcdSIlya Dryomov req->r_flags |= CEPH_OSD_FLAG_REDIRECTED; 34485aea3dcdSIlya Dryomov req->r_tid = 0; 34495aea3dcdSIlya Dryomov __submit_request(req, false); 34505aea3dcdSIlya Dryomov goto out_unlock_osdc; 3451205ee118SIlya Dryomov } 3452205ee118SIlya Dryomov 3453fe5da05eSIlya Dryomov if (m.num_ops != req->r_num_ops) { 3454fe5da05eSIlya Dryomov pr_err("num_ops %d != %d for tid %llu\n", m.num_ops, 3455fe5da05eSIlya Dryomov req->r_num_ops, req->r_tid); 3456fe5da05eSIlya Dryomov goto fail_request; 3457fe5da05eSIlya Dryomov } 3458fe5da05eSIlya Dryomov for (i = 0; i < req->r_num_ops; i++) { 3459fe5da05eSIlya Dryomov dout(" req %p tid %llu op %d rval %d len %u\n", req, 3460fe5da05eSIlya Dryomov req->r_tid, i, m.rval[i], m.outdata_len[i]); 3461fe5da05eSIlya Dryomov req->r_ops[i].rval = m.rval[i]; 3462fe5da05eSIlya Dryomov req->r_ops[i].outdata_len = m.outdata_len[i]; 3463fe5da05eSIlya Dryomov data_len += m.outdata_len[i]; 3464fe5da05eSIlya Dryomov } 3465fe5da05eSIlya Dryomov if (data_len != le32_to_cpu(msg->hdr.data_len)) { 3466fe5da05eSIlya Dryomov pr_err("sum of lens %u != %u for tid %llu\n", data_len, 3467fe5da05eSIlya Dryomov le32_to_cpu(msg->hdr.data_len), req->r_tid); 3468fe5da05eSIlya Dryomov goto fail_request; 3469fe5da05eSIlya Dryomov } 3470b18b9550SIlya Dryomov dout("%s req %p tid %llu result %d data_len %u\n", __func__, 3471b18b9550SIlya Dryomov req, req->r_tid, m.result, data_len); 34723d14c5d2SYehuda Sadeh 3473b18b9550SIlya Dryomov /* 3474b18b9550SIlya Dryomov * Since we only ever request ONDISK, we should only ever get 3475b18b9550SIlya Dryomov * one (type of) reply back. 3476b18b9550SIlya Dryomov */ 3477b18b9550SIlya Dryomov WARN_ON(!(m.flags & CEPH_OSD_FLAG_ONDISK)); 3478fe5da05eSIlya Dryomov req->r_result = m.result ?: data_len; 347945ee2c1dSIlya Dryomov finish_request(req); 34805aea3dcdSIlya Dryomov mutex_unlock(&osd->lock); 34815aea3dcdSIlya Dryomov up_read(&osdc->lock); 34823d14c5d2SYehuda Sadeh 3483fe5da05eSIlya Dryomov __complete_request(req); 3484b18b9550SIlya Dryomov complete_all(&req->r_completion); 3485dc045a91SIlya Dryomov ceph_osdc_put_request(req); 34863d14c5d2SYehuda Sadeh return; 3487fe5da05eSIlya Dryomov 3488fe5da05eSIlya Dryomov fail_request: 34894609245eSIlya Dryomov complete_request(req, -EIO); 34905aea3dcdSIlya Dryomov out_unlock_session: 34915aea3dcdSIlya Dryomov mutex_unlock(&osd->lock); 34925aea3dcdSIlya Dryomov out_unlock_osdc: 34935aea3dcdSIlya Dryomov up_read(&osdc->lock); 34943d14c5d2SYehuda Sadeh } 34953d14c5d2SYehuda Sadeh 349642c1b124SIlya Dryomov static void set_pool_was_full(struct ceph_osd_client *osdc) 349742c1b124SIlya Dryomov { 349842c1b124SIlya Dryomov struct rb_node *n; 349942c1b124SIlya Dryomov 350042c1b124SIlya Dryomov for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) { 350142c1b124SIlya Dryomov struct ceph_pg_pool_info *pi = 350242c1b124SIlya Dryomov rb_entry(n, struct ceph_pg_pool_info, node); 350342c1b124SIlya Dryomov 350442c1b124SIlya Dryomov pi->was_full = __pool_full(pi); 350542c1b124SIlya Dryomov } 350642c1b124SIlya Dryomov } 350742c1b124SIlya Dryomov 35085aea3dcdSIlya Dryomov static bool pool_cleared_full(struct ceph_osd_client *osdc, s64 pool_id) 35093d14c5d2SYehuda Sadeh { 35105aea3dcdSIlya Dryomov struct ceph_pg_pool_info *pi; 35113d14c5d2SYehuda Sadeh 35125aea3dcdSIlya Dryomov pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id); 35135aea3dcdSIlya Dryomov if (!pi) 35145aea3dcdSIlya Dryomov return false; 35153d14c5d2SYehuda Sadeh 35165aea3dcdSIlya Dryomov return pi->was_full && !__pool_full(pi); 35173d14c5d2SYehuda Sadeh } 35183d14c5d2SYehuda Sadeh 3519922dab61SIlya Dryomov static enum calc_target_result 3520922dab61SIlya Dryomov recalc_linger_target(struct ceph_osd_linger_request *lreq) 3521922dab61SIlya Dryomov { 3522922dab61SIlya Dryomov struct ceph_osd_client *osdc = lreq->osdc; 3523922dab61SIlya Dryomov enum calc_target_result ct_res; 3524922dab61SIlya Dryomov 35257de030d6SIlya Dryomov ct_res = calc_target(osdc, &lreq->t, NULL, true); 3526922dab61SIlya Dryomov if (ct_res == CALC_TARGET_NEED_RESEND) { 3527922dab61SIlya Dryomov struct ceph_osd *osd; 3528922dab61SIlya Dryomov 3529922dab61SIlya Dryomov osd = lookup_create_osd(osdc, lreq->t.osd, true); 3530922dab61SIlya Dryomov if (osd != lreq->osd) { 3531922dab61SIlya Dryomov unlink_linger(lreq->osd, lreq); 3532922dab61SIlya Dryomov link_linger(osd, lreq); 3533922dab61SIlya Dryomov } 3534922dab61SIlya Dryomov } 3535922dab61SIlya Dryomov 3536922dab61SIlya Dryomov return ct_res; 3537922dab61SIlya Dryomov } 3538922dab61SIlya Dryomov 35393d14c5d2SYehuda Sadeh /* 35405aea3dcdSIlya Dryomov * Requeue requests whose mapping to an OSD has changed. 35413d14c5d2SYehuda Sadeh */ 35425aea3dcdSIlya Dryomov static void scan_requests(struct ceph_osd *osd, 35435aea3dcdSIlya Dryomov bool force_resend, 35445aea3dcdSIlya Dryomov bool cleared_full, 35455aea3dcdSIlya Dryomov bool check_pool_cleared_full, 35465aea3dcdSIlya Dryomov struct rb_root *need_resend, 35475aea3dcdSIlya Dryomov struct list_head *need_resend_linger) 35483d14c5d2SYehuda Sadeh { 35495aea3dcdSIlya Dryomov struct ceph_osd_client *osdc = osd->o_osdc; 35505aea3dcdSIlya Dryomov struct rb_node *n; 35515aea3dcdSIlya Dryomov bool force_resend_writes; 35523d14c5d2SYehuda Sadeh 3553922dab61SIlya Dryomov for (n = rb_first(&osd->o_linger_requests); n; ) { 3554922dab61SIlya Dryomov struct ceph_osd_linger_request *lreq = 3555922dab61SIlya Dryomov rb_entry(n, struct ceph_osd_linger_request, node); 3556922dab61SIlya Dryomov enum calc_target_result ct_res; 3557922dab61SIlya Dryomov 3558922dab61SIlya Dryomov n = rb_next(n); /* recalc_linger_target() */ 3559922dab61SIlya Dryomov 3560922dab61SIlya Dryomov dout("%s lreq %p linger_id %llu\n", __func__, lreq, 3561922dab61SIlya Dryomov lreq->linger_id); 3562922dab61SIlya Dryomov ct_res = recalc_linger_target(lreq); 3563922dab61SIlya Dryomov switch (ct_res) { 3564922dab61SIlya Dryomov case CALC_TARGET_NO_ACTION: 3565922dab61SIlya Dryomov force_resend_writes = cleared_full || 3566922dab61SIlya Dryomov (check_pool_cleared_full && 3567922dab61SIlya Dryomov pool_cleared_full(osdc, lreq->t.base_oloc.pool)); 3568922dab61SIlya Dryomov if (!force_resend && !force_resend_writes) 3569922dab61SIlya Dryomov break; 3570922dab61SIlya Dryomov 3571922dab61SIlya Dryomov /* fall through */ 3572922dab61SIlya Dryomov case CALC_TARGET_NEED_RESEND: 35734609245eSIlya Dryomov cancel_linger_map_check(lreq); 3574922dab61SIlya Dryomov /* 3575922dab61SIlya Dryomov * scan_requests() for the previous epoch(s) 3576922dab61SIlya Dryomov * may have already added it to the list, since 3577922dab61SIlya Dryomov * it's not unlinked here. 3578922dab61SIlya Dryomov */ 3579922dab61SIlya Dryomov if (list_empty(&lreq->scan_item)) 3580922dab61SIlya Dryomov list_add_tail(&lreq->scan_item, need_resend_linger); 3581922dab61SIlya Dryomov break; 3582922dab61SIlya Dryomov case CALC_TARGET_POOL_DNE: 3583a10bcb19SIlya Dryomov list_del_init(&lreq->scan_item); 35844609245eSIlya Dryomov check_linger_pool_dne(lreq); 3585922dab61SIlya Dryomov break; 3586922dab61SIlya Dryomov } 3587922dab61SIlya Dryomov } 3588922dab61SIlya Dryomov 35895aea3dcdSIlya Dryomov for (n = rb_first(&osd->o_requests); n; ) { 35905aea3dcdSIlya Dryomov struct ceph_osd_request *req = 35915aea3dcdSIlya Dryomov rb_entry(n, struct ceph_osd_request, r_node); 35925aea3dcdSIlya Dryomov enum calc_target_result ct_res; 3593ab60b16dSAlex Elder 35944609245eSIlya Dryomov n = rb_next(n); /* unlink_request(), check_pool_dne() */ 3595ab60b16dSAlex Elder 35965aea3dcdSIlya Dryomov dout("%s req %p tid %llu\n", __func__, req, req->r_tid); 35977de030d6SIlya Dryomov ct_res = calc_target(osdc, &req->r_t, &req->r_osd->o_con, 35987de030d6SIlya Dryomov false); 35995aea3dcdSIlya Dryomov switch (ct_res) { 36005aea3dcdSIlya Dryomov case CALC_TARGET_NO_ACTION: 36015aea3dcdSIlya Dryomov force_resend_writes = cleared_full || 36025aea3dcdSIlya Dryomov (check_pool_cleared_full && 36035aea3dcdSIlya Dryomov pool_cleared_full(osdc, req->r_t.base_oloc.pool)); 36045aea3dcdSIlya Dryomov if (!force_resend && 36055aea3dcdSIlya Dryomov (!(req->r_flags & CEPH_OSD_FLAG_WRITE) || 36065aea3dcdSIlya Dryomov !force_resend_writes)) 36075aea3dcdSIlya Dryomov break; 3608a40c4f10SYehuda Sadeh 36095aea3dcdSIlya Dryomov /* fall through */ 36105aea3dcdSIlya Dryomov case CALC_TARGET_NEED_RESEND: 36114609245eSIlya Dryomov cancel_map_check(req); 36125aea3dcdSIlya Dryomov unlink_request(osd, req); 36135aea3dcdSIlya Dryomov insert_request(need_resend, req); 36145aea3dcdSIlya Dryomov break; 36155aea3dcdSIlya Dryomov case CALC_TARGET_POOL_DNE: 36164609245eSIlya Dryomov check_pool_dne(req); 36175aea3dcdSIlya Dryomov break; 3618a40c4f10SYehuda Sadeh } 36193d14c5d2SYehuda Sadeh } 36203d14c5d2SYehuda Sadeh } 36216f6c7006SSage Weil 362242c1b124SIlya Dryomov static int handle_one_map(struct ceph_osd_client *osdc, 36235aea3dcdSIlya Dryomov void *p, void *end, bool incremental, 36245aea3dcdSIlya Dryomov struct rb_root *need_resend, 36255aea3dcdSIlya Dryomov struct list_head *need_resend_linger) 362642c1b124SIlya Dryomov { 362742c1b124SIlya Dryomov struct ceph_osdmap *newmap; 362842c1b124SIlya Dryomov struct rb_node *n; 362942c1b124SIlya Dryomov bool skipped_map = false; 363042c1b124SIlya Dryomov bool was_full; 363142c1b124SIlya Dryomov 3632b7ec35b3SIlya Dryomov was_full = ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL); 363342c1b124SIlya Dryomov set_pool_was_full(osdc); 363442c1b124SIlya Dryomov 363542c1b124SIlya Dryomov if (incremental) 363642c1b124SIlya Dryomov newmap = osdmap_apply_incremental(&p, end, osdc->osdmap); 363742c1b124SIlya Dryomov else 363842c1b124SIlya Dryomov newmap = ceph_osdmap_decode(&p, end); 363942c1b124SIlya Dryomov if (IS_ERR(newmap)) 364042c1b124SIlya Dryomov return PTR_ERR(newmap); 364142c1b124SIlya Dryomov 364242c1b124SIlya Dryomov if (newmap != osdc->osdmap) { 364342c1b124SIlya Dryomov /* 364442c1b124SIlya Dryomov * Preserve ->was_full before destroying the old map. 364542c1b124SIlya Dryomov * For pools that weren't in the old map, ->was_full 364642c1b124SIlya Dryomov * should be false. 364742c1b124SIlya Dryomov */ 364842c1b124SIlya Dryomov for (n = rb_first(&newmap->pg_pools); n; n = rb_next(n)) { 364942c1b124SIlya Dryomov struct ceph_pg_pool_info *pi = 365042c1b124SIlya Dryomov rb_entry(n, struct ceph_pg_pool_info, node); 365142c1b124SIlya Dryomov struct ceph_pg_pool_info *old_pi; 365242c1b124SIlya Dryomov 365342c1b124SIlya Dryomov old_pi = ceph_pg_pool_by_id(osdc->osdmap, pi->id); 365442c1b124SIlya Dryomov if (old_pi) 365542c1b124SIlya Dryomov pi->was_full = old_pi->was_full; 365642c1b124SIlya Dryomov else 365742c1b124SIlya Dryomov WARN_ON(pi->was_full); 365842c1b124SIlya Dryomov } 365942c1b124SIlya Dryomov 366042c1b124SIlya Dryomov if (osdc->osdmap->epoch && 366142c1b124SIlya Dryomov osdc->osdmap->epoch + 1 < newmap->epoch) { 366242c1b124SIlya Dryomov WARN_ON(incremental); 366342c1b124SIlya Dryomov skipped_map = true; 366442c1b124SIlya Dryomov } 366542c1b124SIlya Dryomov 366642c1b124SIlya Dryomov ceph_osdmap_destroy(osdc->osdmap); 366742c1b124SIlya Dryomov osdc->osdmap = newmap; 366842c1b124SIlya Dryomov } 366942c1b124SIlya Dryomov 3670b7ec35b3SIlya Dryomov was_full &= !ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL); 36715aea3dcdSIlya Dryomov scan_requests(&osdc->homeless_osd, skipped_map, was_full, true, 36725aea3dcdSIlya Dryomov need_resend, need_resend_linger); 36735aea3dcdSIlya Dryomov 36745aea3dcdSIlya Dryomov for (n = rb_first(&osdc->osds); n; ) { 36755aea3dcdSIlya Dryomov struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node); 36765aea3dcdSIlya Dryomov 36775aea3dcdSIlya Dryomov n = rb_next(n); /* close_osd() */ 36785aea3dcdSIlya Dryomov 36795aea3dcdSIlya Dryomov scan_requests(osd, skipped_map, was_full, true, need_resend, 36805aea3dcdSIlya Dryomov need_resend_linger); 36815aea3dcdSIlya Dryomov if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) || 36825aea3dcdSIlya Dryomov memcmp(&osd->o_con.peer_addr, 36835aea3dcdSIlya Dryomov ceph_osd_addr(osdc->osdmap, osd->o_osd), 36845aea3dcdSIlya Dryomov sizeof(struct ceph_entity_addr))) 36855aea3dcdSIlya Dryomov close_osd(osd); 36865aea3dcdSIlya Dryomov } 368742c1b124SIlya Dryomov 368842c1b124SIlya Dryomov return 0; 368942c1b124SIlya Dryomov } 36906f6c7006SSage Weil 36915aea3dcdSIlya Dryomov static void kick_requests(struct ceph_osd_client *osdc, 36925aea3dcdSIlya Dryomov struct rb_root *need_resend, 36935aea3dcdSIlya Dryomov struct list_head *need_resend_linger) 36945aea3dcdSIlya Dryomov { 3695922dab61SIlya Dryomov struct ceph_osd_linger_request *lreq, *nlreq; 369604c7d789SIlya Dryomov enum calc_target_result ct_res; 36975aea3dcdSIlya Dryomov struct rb_node *n; 36985aea3dcdSIlya Dryomov 369904c7d789SIlya Dryomov /* make sure need_resend targets reflect latest map */ 370004c7d789SIlya Dryomov for (n = rb_first(need_resend); n; ) { 370104c7d789SIlya Dryomov struct ceph_osd_request *req = 370204c7d789SIlya Dryomov rb_entry(n, struct ceph_osd_request, r_node); 370304c7d789SIlya Dryomov 370404c7d789SIlya Dryomov n = rb_next(n); 370504c7d789SIlya Dryomov 370604c7d789SIlya Dryomov if (req->r_t.epoch < osdc->osdmap->epoch) { 370704c7d789SIlya Dryomov ct_res = calc_target(osdc, &req->r_t, NULL, false); 370804c7d789SIlya Dryomov if (ct_res == CALC_TARGET_POOL_DNE) { 370904c7d789SIlya Dryomov erase_request(need_resend, req); 371004c7d789SIlya Dryomov check_pool_dne(req); 371104c7d789SIlya Dryomov } 371204c7d789SIlya Dryomov } 371304c7d789SIlya Dryomov } 371404c7d789SIlya Dryomov 37155aea3dcdSIlya Dryomov for (n = rb_first(need_resend); n; ) { 37165aea3dcdSIlya Dryomov struct ceph_osd_request *req = 37175aea3dcdSIlya Dryomov rb_entry(n, struct ceph_osd_request, r_node); 37185aea3dcdSIlya Dryomov struct ceph_osd *osd; 37195aea3dcdSIlya Dryomov 37205aea3dcdSIlya Dryomov n = rb_next(n); 37215aea3dcdSIlya Dryomov erase_request(need_resend, req); /* before link_request() */ 37225aea3dcdSIlya Dryomov 37235aea3dcdSIlya Dryomov osd = lookup_create_osd(osdc, req->r_t.osd, true); 37245aea3dcdSIlya Dryomov link_request(osd, req); 37255aea3dcdSIlya Dryomov if (!req->r_linger) { 37265aea3dcdSIlya Dryomov if (!osd_homeless(osd) && !req->r_t.paused) 37275aea3dcdSIlya Dryomov send_request(req); 3728922dab61SIlya Dryomov } else { 3729922dab61SIlya Dryomov cancel_linger_request(req); 37305aea3dcdSIlya Dryomov } 37315aea3dcdSIlya Dryomov } 3732922dab61SIlya Dryomov 3733922dab61SIlya Dryomov list_for_each_entry_safe(lreq, nlreq, need_resend_linger, scan_item) { 3734922dab61SIlya Dryomov if (!osd_homeless(lreq->osd)) 3735922dab61SIlya Dryomov send_linger(lreq); 3736922dab61SIlya Dryomov 3737922dab61SIlya Dryomov list_del_init(&lreq->scan_item); 3738922dab61SIlya Dryomov } 37395aea3dcdSIlya Dryomov } 37405aea3dcdSIlya Dryomov 37413d14c5d2SYehuda Sadeh /* 37423d14c5d2SYehuda Sadeh * Process updated osd map. 37433d14c5d2SYehuda Sadeh * 37443d14c5d2SYehuda Sadeh * The message contains any number of incremental and full maps, normally 37453d14c5d2SYehuda Sadeh * indicating some sort of topology change in the cluster. Kick requests 37463d14c5d2SYehuda Sadeh * off to different OSDs as needed. 37473d14c5d2SYehuda Sadeh */ 37483d14c5d2SYehuda Sadeh void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg) 37493d14c5d2SYehuda Sadeh { 375042c1b124SIlya Dryomov void *p = msg->front.iov_base; 375142c1b124SIlya Dryomov void *const end = p + msg->front.iov_len; 37523d14c5d2SYehuda Sadeh u32 nr_maps, maplen; 37533d14c5d2SYehuda Sadeh u32 epoch; 37543d14c5d2SYehuda Sadeh struct ceph_fsid fsid; 37555aea3dcdSIlya Dryomov struct rb_root need_resend = RB_ROOT; 37565aea3dcdSIlya Dryomov LIST_HEAD(need_resend_linger); 375742c1b124SIlya Dryomov bool handled_incremental = false; 375842c1b124SIlya Dryomov bool was_pauserd, was_pausewr; 375942c1b124SIlya Dryomov bool pauserd, pausewr; 376042c1b124SIlya Dryomov int err; 37613d14c5d2SYehuda Sadeh 376242c1b124SIlya Dryomov dout("%s have %u\n", __func__, osdc->osdmap->epoch); 37635aea3dcdSIlya Dryomov down_write(&osdc->lock); 37643d14c5d2SYehuda Sadeh 37653d14c5d2SYehuda Sadeh /* verify fsid */ 37663d14c5d2SYehuda Sadeh ceph_decode_need(&p, end, sizeof(fsid), bad); 37673d14c5d2SYehuda Sadeh ceph_decode_copy(&p, &fsid, sizeof(fsid)); 37683d14c5d2SYehuda Sadeh if (ceph_check_fsid(osdc->client, &fsid) < 0) 376942c1b124SIlya Dryomov goto bad; 37703d14c5d2SYehuda Sadeh 3771b7ec35b3SIlya Dryomov was_pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD); 3772b7ec35b3SIlya Dryomov was_pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) || 3773b7ec35b3SIlya Dryomov ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) || 377442c1b124SIlya Dryomov have_pool_full(osdc); 37759a1ea2dbSJosh Durgin 37763d14c5d2SYehuda Sadeh /* incremental maps */ 37773d14c5d2SYehuda Sadeh ceph_decode_32_safe(&p, end, nr_maps, bad); 37783d14c5d2SYehuda Sadeh dout(" %d inc maps\n", nr_maps); 37793d14c5d2SYehuda Sadeh while (nr_maps > 0) { 37803d14c5d2SYehuda Sadeh ceph_decode_need(&p, end, 2*sizeof(u32), bad); 37813d14c5d2SYehuda Sadeh epoch = ceph_decode_32(&p); 37823d14c5d2SYehuda Sadeh maplen = ceph_decode_32(&p); 37833d14c5d2SYehuda Sadeh ceph_decode_need(&p, end, maplen, bad); 378442c1b124SIlya Dryomov if (osdc->osdmap->epoch && 378542c1b124SIlya Dryomov osdc->osdmap->epoch + 1 == epoch) { 37863d14c5d2SYehuda Sadeh dout("applying incremental map %u len %d\n", 37873d14c5d2SYehuda Sadeh epoch, maplen); 37885aea3dcdSIlya Dryomov err = handle_one_map(osdc, p, p + maplen, true, 37895aea3dcdSIlya Dryomov &need_resend, &need_resend_linger); 379042c1b124SIlya Dryomov if (err) 37913d14c5d2SYehuda Sadeh goto bad; 379242c1b124SIlya Dryomov handled_incremental = true; 37933d14c5d2SYehuda Sadeh } else { 37943d14c5d2SYehuda Sadeh dout("ignoring incremental map %u len %d\n", 37953d14c5d2SYehuda Sadeh epoch, maplen); 37963d14c5d2SYehuda Sadeh } 379742c1b124SIlya Dryomov p += maplen; 37983d14c5d2SYehuda Sadeh nr_maps--; 37993d14c5d2SYehuda Sadeh } 380042c1b124SIlya Dryomov if (handled_incremental) 38013d14c5d2SYehuda Sadeh goto done; 38023d14c5d2SYehuda Sadeh 38033d14c5d2SYehuda Sadeh /* full maps */ 38043d14c5d2SYehuda Sadeh ceph_decode_32_safe(&p, end, nr_maps, bad); 38053d14c5d2SYehuda Sadeh dout(" %d full maps\n", nr_maps); 38063d14c5d2SYehuda Sadeh while (nr_maps) { 38073d14c5d2SYehuda Sadeh ceph_decode_need(&p, end, 2*sizeof(u32), bad); 38083d14c5d2SYehuda Sadeh epoch = ceph_decode_32(&p); 38093d14c5d2SYehuda Sadeh maplen = ceph_decode_32(&p); 38103d14c5d2SYehuda Sadeh ceph_decode_need(&p, end, maplen, bad); 38113d14c5d2SYehuda Sadeh if (nr_maps > 1) { 38123d14c5d2SYehuda Sadeh dout("skipping non-latest full map %u len %d\n", 38133d14c5d2SYehuda Sadeh epoch, maplen); 3814e5253a7bSIlya Dryomov } else if (osdc->osdmap->epoch >= epoch) { 38153d14c5d2SYehuda Sadeh dout("skipping full map %u len %d, " 38163d14c5d2SYehuda Sadeh "older than our %u\n", epoch, maplen, 38173d14c5d2SYehuda Sadeh osdc->osdmap->epoch); 38183d14c5d2SYehuda Sadeh } else { 38193d14c5d2SYehuda Sadeh dout("taking full map %u len %d\n", epoch, maplen); 38205aea3dcdSIlya Dryomov err = handle_one_map(osdc, p, p + maplen, false, 38215aea3dcdSIlya Dryomov &need_resend, &need_resend_linger); 382242c1b124SIlya Dryomov if (err) 38233d14c5d2SYehuda Sadeh goto bad; 38243d14c5d2SYehuda Sadeh } 38253d14c5d2SYehuda Sadeh p += maplen; 38263d14c5d2SYehuda Sadeh nr_maps--; 38273d14c5d2SYehuda Sadeh } 38283d14c5d2SYehuda Sadeh 38293d14c5d2SYehuda Sadeh done: 3830cd634fb6SSage Weil /* 3831cd634fb6SSage Weil * subscribe to subsequent osdmap updates if full to ensure 3832cd634fb6SSage Weil * we find out when we are no longer full and stop returning 3833cd634fb6SSage Weil * ENOSPC. 3834cd634fb6SSage Weil */ 3835b7ec35b3SIlya Dryomov pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD); 3836b7ec35b3SIlya Dryomov pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) || 3837b7ec35b3SIlya Dryomov ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) || 383842c1b124SIlya Dryomov have_pool_full(osdc); 383958eb7932SJeff Layton if (was_pauserd || was_pausewr || pauserd || pausewr || 384058eb7932SJeff Layton osdc->osdmap->epoch < osdc->epoch_barrier) 384142c1b124SIlya Dryomov maybe_request_map(osdc); 3842cd634fb6SSage Weil 38435aea3dcdSIlya Dryomov kick_requests(osdc, &need_resend, &need_resend_linger); 384442c1b124SIlya Dryomov 3845fc36d0a4SJeff Layton ceph_osdc_abort_on_full(osdc); 384642c1b124SIlya Dryomov ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP, 384742c1b124SIlya Dryomov osdc->osdmap->epoch); 38485aea3dcdSIlya Dryomov up_write(&osdc->lock); 38493d14c5d2SYehuda Sadeh wake_up_all(&osdc->client->auth_wq); 38503d14c5d2SYehuda Sadeh return; 38513d14c5d2SYehuda Sadeh 38523d14c5d2SYehuda Sadeh bad: 38533d14c5d2SYehuda Sadeh pr_err("osdc handle_map corrupt msg\n"); 38543d14c5d2SYehuda Sadeh ceph_msg_dump(msg); 38555aea3dcdSIlya Dryomov up_write(&osdc->lock); 38565aea3dcdSIlya Dryomov } 38575aea3dcdSIlya Dryomov 38585aea3dcdSIlya Dryomov /* 38595aea3dcdSIlya Dryomov * Resubmit requests pending on the given osd. 38605aea3dcdSIlya Dryomov */ 38615aea3dcdSIlya Dryomov static void kick_osd_requests(struct ceph_osd *osd) 38625aea3dcdSIlya Dryomov { 38635aea3dcdSIlya Dryomov struct rb_node *n; 38645aea3dcdSIlya Dryomov 3865a02a946dSIlya Dryomov clear_backoffs(osd); 3866a02a946dSIlya Dryomov 3867922dab61SIlya Dryomov for (n = rb_first(&osd->o_requests); n; ) { 38685aea3dcdSIlya Dryomov struct ceph_osd_request *req = 38695aea3dcdSIlya Dryomov rb_entry(n, struct ceph_osd_request, r_node); 38705aea3dcdSIlya Dryomov 3871922dab61SIlya Dryomov n = rb_next(n); /* cancel_linger_request() */ 3872922dab61SIlya Dryomov 38735aea3dcdSIlya Dryomov if (!req->r_linger) { 38745aea3dcdSIlya Dryomov if (!req->r_t.paused) 38755aea3dcdSIlya Dryomov send_request(req); 3876922dab61SIlya Dryomov } else { 3877922dab61SIlya Dryomov cancel_linger_request(req); 38785aea3dcdSIlya Dryomov } 38795aea3dcdSIlya Dryomov } 3880922dab61SIlya Dryomov for (n = rb_first(&osd->o_linger_requests); n; n = rb_next(n)) { 3881922dab61SIlya Dryomov struct ceph_osd_linger_request *lreq = 3882922dab61SIlya Dryomov rb_entry(n, struct ceph_osd_linger_request, node); 3883922dab61SIlya Dryomov 3884922dab61SIlya Dryomov send_linger(lreq); 3885922dab61SIlya Dryomov } 38865aea3dcdSIlya Dryomov } 38875aea3dcdSIlya Dryomov 38885aea3dcdSIlya Dryomov /* 38895aea3dcdSIlya Dryomov * If the osd connection drops, we need to resubmit all requests. 38905aea3dcdSIlya Dryomov */ 38915aea3dcdSIlya Dryomov static void osd_fault(struct ceph_connection *con) 38925aea3dcdSIlya Dryomov { 38935aea3dcdSIlya Dryomov struct ceph_osd *osd = con->private; 38945aea3dcdSIlya Dryomov struct ceph_osd_client *osdc = osd->o_osdc; 38955aea3dcdSIlya Dryomov 38965aea3dcdSIlya Dryomov dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd); 38975aea3dcdSIlya Dryomov 38985aea3dcdSIlya Dryomov down_write(&osdc->lock); 38995aea3dcdSIlya Dryomov if (!osd_registered(osd)) { 39005aea3dcdSIlya Dryomov dout("%s osd%d unknown\n", __func__, osd->o_osd); 39015aea3dcdSIlya Dryomov goto out_unlock; 39025aea3dcdSIlya Dryomov } 39035aea3dcdSIlya Dryomov 39045aea3dcdSIlya Dryomov if (!reopen_osd(osd)) 39055aea3dcdSIlya Dryomov kick_osd_requests(osd); 39065aea3dcdSIlya Dryomov maybe_request_map(osdc); 39075aea3dcdSIlya Dryomov 39085aea3dcdSIlya Dryomov out_unlock: 39095aea3dcdSIlya Dryomov up_write(&osdc->lock); 39103d14c5d2SYehuda Sadeh } 39113d14c5d2SYehuda Sadeh 3912a02a946dSIlya Dryomov struct MOSDBackoff { 3913a02a946dSIlya Dryomov struct ceph_spg spgid; 3914a02a946dSIlya Dryomov u32 map_epoch; 3915a02a946dSIlya Dryomov u8 op; 3916a02a946dSIlya Dryomov u64 id; 3917a02a946dSIlya Dryomov struct ceph_hobject_id *begin; 3918a02a946dSIlya Dryomov struct ceph_hobject_id *end; 3919a02a946dSIlya Dryomov }; 3920a02a946dSIlya Dryomov 3921a02a946dSIlya Dryomov static int decode_MOSDBackoff(const struct ceph_msg *msg, struct MOSDBackoff *m) 3922a02a946dSIlya Dryomov { 3923a02a946dSIlya Dryomov void *p = msg->front.iov_base; 3924a02a946dSIlya Dryomov void *const end = p + msg->front.iov_len; 3925a02a946dSIlya Dryomov u8 struct_v; 3926a02a946dSIlya Dryomov u32 struct_len; 3927a02a946dSIlya Dryomov int ret; 3928a02a946dSIlya Dryomov 3929a02a946dSIlya Dryomov ret = ceph_start_decoding(&p, end, 1, "spg_t", &struct_v, &struct_len); 3930a02a946dSIlya Dryomov if (ret) 3931a02a946dSIlya Dryomov return ret; 3932a02a946dSIlya Dryomov 3933a02a946dSIlya Dryomov ret = ceph_decode_pgid(&p, end, &m->spgid.pgid); 3934a02a946dSIlya Dryomov if (ret) 3935a02a946dSIlya Dryomov return ret; 3936a02a946dSIlya Dryomov 3937a02a946dSIlya Dryomov ceph_decode_8_safe(&p, end, m->spgid.shard, e_inval); 3938a02a946dSIlya Dryomov ceph_decode_32_safe(&p, end, m->map_epoch, e_inval); 3939a02a946dSIlya Dryomov ceph_decode_8_safe(&p, end, m->op, e_inval); 3940a02a946dSIlya Dryomov ceph_decode_64_safe(&p, end, m->id, e_inval); 3941a02a946dSIlya Dryomov 3942a02a946dSIlya Dryomov m->begin = kzalloc(sizeof(*m->begin), GFP_NOIO); 3943a02a946dSIlya Dryomov if (!m->begin) 3944a02a946dSIlya Dryomov return -ENOMEM; 3945a02a946dSIlya Dryomov 3946a02a946dSIlya Dryomov ret = decode_hoid(&p, end, m->begin); 3947a02a946dSIlya Dryomov if (ret) { 3948a02a946dSIlya Dryomov free_hoid(m->begin); 3949a02a946dSIlya Dryomov return ret; 3950a02a946dSIlya Dryomov } 3951a02a946dSIlya Dryomov 3952a02a946dSIlya Dryomov m->end = kzalloc(sizeof(*m->end), GFP_NOIO); 3953a02a946dSIlya Dryomov if (!m->end) { 3954a02a946dSIlya Dryomov free_hoid(m->begin); 3955a02a946dSIlya Dryomov return -ENOMEM; 3956a02a946dSIlya Dryomov } 3957a02a946dSIlya Dryomov 3958a02a946dSIlya Dryomov ret = decode_hoid(&p, end, m->end); 3959a02a946dSIlya Dryomov if (ret) { 3960a02a946dSIlya Dryomov free_hoid(m->begin); 3961a02a946dSIlya Dryomov free_hoid(m->end); 3962a02a946dSIlya Dryomov return ret; 3963a02a946dSIlya Dryomov } 3964a02a946dSIlya Dryomov 3965a02a946dSIlya Dryomov return 0; 3966a02a946dSIlya Dryomov 3967a02a946dSIlya Dryomov e_inval: 3968a02a946dSIlya Dryomov return -EINVAL; 3969a02a946dSIlya Dryomov } 3970a02a946dSIlya Dryomov 3971a02a946dSIlya Dryomov static struct ceph_msg *create_backoff_message( 3972a02a946dSIlya Dryomov const struct ceph_osd_backoff *backoff, 3973a02a946dSIlya Dryomov u32 map_epoch) 3974a02a946dSIlya Dryomov { 3975a02a946dSIlya Dryomov struct ceph_msg *msg; 3976a02a946dSIlya Dryomov void *p, *end; 3977a02a946dSIlya Dryomov int msg_size; 3978a02a946dSIlya Dryomov 3979a02a946dSIlya Dryomov msg_size = CEPH_ENCODING_START_BLK_LEN + 3980a02a946dSIlya Dryomov CEPH_PGID_ENCODING_LEN + 1; /* spgid */ 3981a02a946dSIlya Dryomov msg_size += 4 + 1 + 8; /* map_epoch, op, id */ 3982a02a946dSIlya Dryomov msg_size += CEPH_ENCODING_START_BLK_LEN + 3983a02a946dSIlya Dryomov hoid_encoding_size(backoff->begin); 3984a02a946dSIlya Dryomov msg_size += CEPH_ENCODING_START_BLK_LEN + 3985a02a946dSIlya Dryomov hoid_encoding_size(backoff->end); 3986a02a946dSIlya Dryomov 3987a02a946dSIlya Dryomov msg = ceph_msg_new(CEPH_MSG_OSD_BACKOFF, msg_size, GFP_NOIO, true); 3988a02a946dSIlya Dryomov if (!msg) 3989a02a946dSIlya Dryomov return NULL; 3990a02a946dSIlya Dryomov 3991a02a946dSIlya Dryomov p = msg->front.iov_base; 3992a02a946dSIlya Dryomov end = p + msg->front_alloc_len; 3993a02a946dSIlya Dryomov 3994a02a946dSIlya Dryomov encode_spgid(&p, &backoff->spgid); 3995a02a946dSIlya Dryomov ceph_encode_32(&p, map_epoch); 3996a02a946dSIlya Dryomov ceph_encode_8(&p, CEPH_OSD_BACKOFF_OP_ACK_BLOCK); 3997a02a946dSIlya Dryomov ceph_encode_64(&p, backoff->id); 3998a02a946dSIlya Dryomov encode_hoid(&p, end, backoff->begin); 3999a02a946dSIlya Dryomov encode_hoid(&p, end, backoff->end); 4000a02a946dSIlya Dryomov BUG_ON(p != end); 4001a02a946dSIlya Dryomov 4002a02a946dSIlya Dryomov msg->front.iov_len = p - msg->front.iov_base; 4003a02a946dSIlya Dryomov msg->hdr.version = cpu_to_le16(1); /* MOSDBackoff v1 */ 4004a02a946dSIlya Dryomov msg->hdr.front_len = cpu_to_le32(msg->front.iov_len); 4005a02a946dSIlya Dryomov 4006a02a946dSIlya Dryomov return msg; 4007a02a946dSIlya Dryomov } 4008a02a946dSIlya Dryomov 4009a02a946dSIlya Dryomov static void handle_backoff_block(struct ceph_osd *osd, struct MOSDBackoff *m) 4010a02a946dSIlya Dryomov { 4011a02a946dSIlya Dryomov struct ceph_spg_mapping *spg; 4012a02a946dSIlya Dryomov struct ceph_osd_backoff *backoff; 4013a02a946dSIlya Dryomov struct ceph_msg *msg; 4014a02a946dSIlya Dryomov 4015a02a946dSIlya Dryomov dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd, 4016a02a946dSIlya Dryomov m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id); 4017a02a946dSIlya Dryomov 4018a02a946dSIlya Dryomov spg = lookup_spg_mapping(&osd->o_backoff_mappings, &m->spgid); 4019a02a946dSIlya Dryomov if (!spg) { 4020a02a946dSIlya Dryomov spg = alloc_spg_mapping(); 4021a02a946dSIlya Dryomov if (!spg) { 4022a02a946dSIlya Dryomov pr_err("%s failed to allocate spg\n", __func__); 4023a02a946dSIlya Dryomov return; 4024a02a946dSIlya Dryomov } 4025a02a946dSIlya Dryomov spg->spgid = m->spgid; /* struct */ 4026a02a946dSIlya Dryomov insert_spg_mapping(&osd->o_backoff_mappings, spg); 4027a02a946dSIlya Dryomov } 4028a02a946dSIlya Dryomov 4029a02a946dSIlya Dryomov backoff = alloc_backoff(); 4030a02a946dSIlya Dryomov if (!backoff) { 4031a02a946dSIlya Dryomov pr_err("%s failed to allocate backoff\n", __func__); 4032a02a946dSIlya Dryomov return; 4033a02a946dSIlya Dryomov } 4034a02a946dSIlya Dryomov backoff->spgid = m->spgid; /* struct */ 4035a02a946dSIlya Dryomov backoff->id = m->id; 4036a02a946dSIlya Dryomov backoff->begin = m->begin; 4037a02a946dSIlya Dryomov m->begin = NULL; /* backoff now owns this */ 4038a02a946dSIlya Dryomov backoff->end = m->end; 4039a02a946dSIlya Dryomov m->end = NULL; /* ditto */ 4040a02a946dSIlya Dryomov 4041a02a946dSIlya Dryomov insert_backoff(&spg->backoffs, backoff); 4042a02a946dSIlya Dryomov insert_backoff_by_id(&osd->o_backoffs_by_id, backoff); 4043a02a946dSIlya Dryomov 4044a02a946dSIlya Dryomov /* 4045a02a946dSIlya Dryomov * Ack with original backoff's epoch so that the OSD can 4046a02a946dSIlya Dryomov * discard this if there was a PG split. 4047a02a946dSIlya Dryomov */ 4048a02a946dSIlya Dryomov msg = create_backoff_message(backoff, m->map_epoch); 4049a02a946dSIlya Dryomov if (!msg) { 4050a02a946dSIlya Dryomov pr_err("%s failed to allocate msg\n", __func__); 4051a02a946dSIlya Dryomov return; 4052a02a946dSIlya Dryomov } 4053a02a946dSIlya Dryomov ceph_con_send(&osd->o_con, msg); 4054a02a946dSIlya Dryomov } 4055a02a946dSIlya Dryomov 4056a02a946dSIlya Dryomov static bool target_contained_by(const struct ceph_osd_request_target *t, 4057a02a946dSIlya Dryomov const struct ceph_hobject_id *begin, 4058a02a946dSIlya Dryomov const struct ceph_hobject_id *end) 4059a02a946dSIlya Dryomov { 4060a02a946dSIlya Dryomov struct ceph_hobject_id hoid; 4061a02a946dSIlya Dryomov int cmp; 4062a02a946dSIlya Dryomov 4063a02a946dSIlya Dryomov hoid_fill_from_target(&hoid, t); 4064a02a946dSIlya Dryomov cmp = hoid_compare(&hoid, begin); 4065a02a946dSIlya Dryomov return !cmp || (cmp > 0 && hoid_compare(&hoid, end) < 0); 4066a02a946dSIlya Dryomov } 4067a02a946dSIlya Dryomov 4068a02a946dSIlya Dryomov static void handle_backoff_unblock(struct ceph_osd *osd, 4069a02a946dSIlya Dryomov const struct MOSDBackoff *m) 4070a02a946dSIlya Dryomov { 4071a02a946dSIlya Dryomov struct ceph_spg_mapping *spg; 4072a02a946dSIlya Dryomov struct ceph_osd_backoff *backoff; 4073a02a946dSIlya Dryomov struct rb_node *n; 4074a02a946dSIlya Dryomov 4075a02a946dSIlya Dryomov dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd, 4076a02a946dSIlya Dryomov m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id); 4077a02a946dSIlya Dryomov 4078a02a946dSIlya Dryomov backoff = lookup_backoff_by_id(&osd->o_backoffs_by_id, m->id); 4079a02a946dSIlya Dryomov if (!backoff) { 4080a02a946dSIlya Dryomov pr_err("%s osd%d spgid %llu.%xs%d id %llu backoff dne\n", 4081a02a946dSIlya Dryomov __func__, osd->o_osd, m->spgid.pgid.pool, 4082a02a946dSIlya Dryomov m->spgid.pgid.seed, m->spgid.shard, m->id); 4083a02a946dSIlya Dryomov return; 4084a02a946dSIlya Dryomov } 4085a02a946dSIlya Dryomov 4086a02a946dSIlya Dryomov if (hoid_compare(backoff->begin, m->begin) && 4087a02a946dSIlya Dryomov hoid_compare(backoff->end, m->end)) { 4088a02a946dSIlya Dryomov pr_err("%s osd%d spgid %llu.%xs%d id %llu bad range?\n", 4089a02a946dSIlya Dryomov __func__, osd->o_osd, m->spgid.pgid.pool, 4090a02a946dSIlya Dryomov m->spgid.pgid.seed, m->spgid.shard, m->id); 4091a02a946dSIlya Dryomov /* unblock it anyway... */ 4092a02a946dSIlya Dryomov } 4093a02a946dSIlya Dryomov 4094a02a946dSIlya Dryomov spg = lookup_spg_mapping(&osd->o_backoff_mappings, &backoff->spgid); 4095a02a946dSIlya Dryomov BUG_ON(!spg); 4096a02a946dSIlya Dryomov 4097a02a946dSIlya Dryomov erase_backoff(&spg->backoffs, backoff); 4098a02a946dSIlya Dryomov erase_backoff_by_id(&osd->o_backoffs_by_id, backoff); 4099a02a946dSIlya Dryomov free_backoff(backoff); 4100a02a946dSIlya Dryomov 4101a02a946dSIlya Dryomov if (RB_EMPTY_ROOT(&spg->backoffs)) { 4102a02a946dSIlya Dryomov erase_spg_mapping(&osd->o_backoff_mappings, spg); 4103a02a946dSIlya Dryomov free_spg_mapping(spg); 4104a02a946dSIlya Dryomov } 4105a02a946dSIlya Dryomov 4106a02a946dSIlya Dryomov for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) { 4107a02a946dSIlya Dryomov struct ceph_osd_request *req = 4108a02a946dSIlya Dryomov rb_entry(n, struct ceph_osd_request, r_node); 4109a02a946dSIlya Dryomov 4110a02a946dSIlya Dryomov if (!ceph_spg_compare(&req->r_t.spgid, &m->spgid)) { 4111a02a946dSIlya Dryomov /* 4112a02a946dSIlya Dryomov * Match against @m, not @backoff -- the PG may 4113a02a946dSIlya Dryomov * have split on the OSD. 4114a02a946dSIlya Dryomov */ 4115a02a946dSIlya Dryomov if (target_contained_by(&req->r_t, m->begin, m->end)) { 4116a02a946dSIlya Dryomov /* 4117a02a946dSIlya Dryomov * If no other installed backoff applies, 4118a02a946dSIlya Dryomov * resend. 4119a02a946dSIlya Dryomov */ 4120a02a946dSIlya Dryomov send_request(req); 4121a02a946dSIlya Dryomov } 4122a02a946dSIlya Dryomov } 4123a02a946dSIlya Dryomov } 4124a02a946dSIlya Dryomov } 4125a02a946dSIlya Dryomov 4126a02a946dSIlya Dryomov static void handle_backoff(struct ceph_osd *osd, struct ceph_msg *msg) 4127a02a946dSIlya Dryomov { 4128a02a946dSIlya Dryomov struct ceph_osd_client *osdc = osd->o_osdc; 4129a02a946dSIlya Dryomov struct MOSDBackoff m; 4130a02a946dSIlya Dryomov int ret; 4131a02a946dSIlya Dryomov 4132a02a946dSIlya Dryomov down_read(&osdc->lock); 4133a02a946dSIlya Dryomov if (!osd_registered(osd)) { 4134a02a946dSIlya Dryomov dout("%s osd%d unknown\n", __func__, osd->o_osd); 4135a02a946dSIlya Dryomov up_read(&osdc->lock); 4136a02a946dSIlya Dryomov return; 4137a02a946dSIlya Dryomov } 4138a02a946dSIlya Dryomov WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num)); 4139a02a946dSIlya Dryomov 4140a02a946dSIlya Dryomov mutex_lock(&osd->lock); 4141a02a946dSIlya Dryomov ret = decode_MOSDBackoff(msg, &m); 4142a02a946dSIlya Dryomov if (ret) { 4143a02a946dSIlya Dryomov pr_err("failed to decode MOSDBackoff: %d\n", ret); 4144a02a946dSIlya Dryomov ceph_msg_dump(msg); 4145a02a946dSIlya Dryomov goto out_unlock; 4146a02a946dSIlya Dryomov } 4147a02a946dSIlya Dryomov 4148a02a946dSIlya Dryomov switch (m.op) { 4149a02a946dSIlya Dryomov case CEPH_OSD_BACKOFF_OP_BLOCK: 4150a02a946dSIlya Dryomov handle_backoff_block(osd, &m); 4151a02a946dSIlya Dryomov break; 4152a02a946dSIlya Dryomov case CEPH_OSD_BACKOFF_OP_UNBLOCK: 4153a02a946dSIlya Dryomov handle_backoff_unblock(osd, &m); 4154a02a946dSIlya Dryomov break; 4155a02a946dSIlya Dryomov default: 4156a02a946dSIlya Dryomov pr_err("%s osd%d unknown op %d\n", __func__, osd->o_osd, m.op); 4157a02a946dSIlya Dryomov } 4158a02a946dSIlya Dryomov 4159a02a946dSIlya Dryomov free_hoid(m.begin); 4160a02a946dSIlya Dryomov free_hoid(m.end); 4161a02a946dSIlya Dryomov 4162a02a946dSIlya Dryomov out_unlock: 4163a02a946dSIlya Dryomov mutex_unlock(&osd->lock); 4164a02a946dSIlya Dryomov up_read(&osdc->lock); 4165a02a946dSIlya Dryomov } 4166a02a946dSIlya Dryomov 41673d14c5d2SYehuda Sadeh /* 4168a40c4f10SYehuda Sadeh * Process osd watch notifications 4169a40c4f10SYehuda Sadeh */ 41703c663bbdSAlex Elder static void handle_watch_notify(struct ceph_osd_client *osdc, 41713c663bbdSAlex Elder struct ceph_msg *msg) 4172a40c4f10SYehuda Sadeh { 4173922dab61SIlya Dryomov void *p = msg->front.iov_base; 4174922dab61SIlya Dryomov void *const end = p + msg->front.iov_len; 4175922dab61SIlya Dryomov struct ceph_osd_linger_request *lreq; 4176922dab61SIlya Dryomov struct linger_work *lwork; 4177922dab61SIlya Dryomov u8 proto_ver, opcode; 4178922dab61SIlya Dryomov u64 cookie, notify_id; 4179922dab61SIlya Dryomov u64 notifier_id = 0; 418019079203SIlya Dryomov s32 return_code = 0; 4181922dab61SIlya Dryomov void *payload = NULL; 4182922dab61SIlya Dryomov u32 payload_len = 0; 4183a40c4f10SYehuda Sadeh 4184a40c4f10SYehuda Sadeh ceph_decode_8_safe(&p, end, proto_ver, bad); 4185a40c4f10SYehuda Sadeh ceph_decode_8_safe(&p, end, opcode, bad); 4186a40c4f10SYehuda Sadeh ceph_decode_64_safe(&p, end, cookie, bad); 4187922dab61SIlya Dryomov p += 8; /* skip ver */ 4188a40c4f10SYehuda Sadeh ceph_decode_64_safe(&p, end, notify_id, bad); 4189a40c4f10SYehuda Sadeh 4190922dab61SIlya Dryomov if (proto_ver >= 1) { 4191922dab61SIlya Dryomov ceph_decode_32_safe(&p, end, payload_len, bad); 4192922dab61SIlya Dryomov ceph_decode_need(&p, end, payload_len, bad); 4193922dab61SIlya Dryomov payload = p; 4194922dab61SIlya Dryomov p += payload_len; 4195a40c4f10SYehuda Sadeh } 4196a40c4f10SYehuda Sadeh 4197922dab61SIlya Dryomov if (le16_to_cpu(msg->hdr.version) >= 2) 419819079203SIlya Dryomov ceph_decode_32_safe(&p, end, return_code, bad); 4199922dab61SIlya Dryomov 4200922dab61SIlya Dryomov if (le16_to_cpu(msg->hdr.version) >= 3) 4201922dab61SIlya Dryomov ceph_decode_64_safe(&p, end, notifier_id, bad); 4202922dab61SIlya Dryomov 4203922dab61SIlya Dryomov down_read(&osdc->lock); 4204922dab61SIlya Dryomov lreq = lookup_linger_osdc(&osdc->linger_requests, cookie); 4205922dab61SIlya Dryomov if (!lreq) { 4206922dab61SIlya Dryomov dout("%s opcode %d cookie %llu dne\n", __func__, opcode, 4207922dab61SIlya Dryomov cookie); 4208922dab61SIlya Dryomov goto out_unlock_osdc; 4209922dab61SIlya Dryomov } 4210922dab61SIlya Dryomov 4211922dab61SIlya Dryomov mutex_lock(&lreq->lock); 421219079203SIlya Dryomov dout("%s opcode %d cookie %llu lreq %p is_watch %d\n", __func__, 421319079203SIlya Dryomov opcode, cookie, lreq, lreq->is_watch); 4214922dab61SIlya Dryomov if (opcode == CEPH_WATCH_EVENT_DISCONNECT) { 4215922dab61SIlya Dryomov if (!lreq->last_error) { 4216922dab61SIlya Dryomov lreq->last_error = -ENOTCONN; 4217922dab61SIlya Dryomov queue_watch_error(lreq); 4218922dab61SIlya Dryomov } 421919079203SIlya Dryomov } else if (!lreq->is_watch) { 422019079203SIlya Dryomov /* CEPH_WATCH_EVENT_NOTIFY_COMPLETE */ 422119079203SIlya Dryomov if (lreq->notify_id && lreq->notify_id != notify_id) { 422219079203SIlya Dryomov dout("lreq %p notify_id %llu != %llu, ignoring\n", lreq, 422319079203SIlya Dryomov lreq->notify_id, notify_id); 422419079203SIlya Dryomov } else if (!completion_done(&lreq->notify_finish_wait)) { 422519079203SIlya Dryomov struct ceph_msg_data *data = 422619079203SIlya Dryomov list_first_entry_or_null(&msg->data, 422719079203SIlya Dryomov struct ceph_msg_data, 422819079203SIlya Dryomov links); 422919079203SIlya Dryomov 423019079203SIlya Dryomov if (data) { 423119079203SIlya Dryomov if (lreq->preply_pages) { 423219079203SIlya Dryomov WARN_ON(data->type != 423319079203SIlya Dryomov CEPH_MSG_DATA_PAGES); 423419079203SIlya Dryomov *lreq->preply_pages = data->pages; 423519079203SIlya Dryomov *lreq->preply_len = data->length; 423619079203SIlya Dryomov } else { 423719079203SIlya Dryomov ceph_release_page_vector(data->pages, 423819079203SIlya Dryomov calc_pages_for(0, data->length)); 423919079203SIlya Dryomov } 424019079203SIlya Dryomov } 424119079203SIlya Dryomov lreq->notify_finish_error = return_code; 424219079203SIlya Dryomov complete_all(&lreq->notify_finish_wait); 424319079203SIlya Dryomov } 4244922dab61SIlya Dryomov } else { 4245922dab61SIlya Dryomov /* CEPH_WATCH_EVENT_NOTIFY */ 4246922dab61SIlya Dryomov lwork = lwork_alloc(lreq, do_watch_notify); 4247922dab61SIlya Dryomov if (!lwork) { 4248922dab61SIlya Dryomov pr_err("failed to allocate notify-lwork\n"); 4249922dab61SIlya Dryomov goto out_unlock_lreq; 4250922dab61SIlya Dryomov } 4251922dab61SIlya Dryomov 4252922dab61SIlya Dryomov lwork->notify.notify_id = notify_id; 4253922dab61SIlya Dryomov lwork->notify.notifier_id = notifier_id; 4254922dab61SIlya Dryomov lwork->notify.payload = payload; 4255922dab61SIlya Dryomov lwork->notify.payload_len = payload_len; 4256922dab61SIlya Dryomov lwork->notify.msg = ceph_msg_get(msg); 4257922dab61SIlya Dryomov lwork_queue(lwork); 4258922dab61SIlya Dryomov } 4259922dab61SIlya Dryomov 4260922dab61SIlya Dryomov out_unlock_lreq: 4261922dab61SIlya Dryomov mutex_unlock(&lreq->lock); 4262922dab61SIlya Dryomov out_unlock_osdc: 4263922dab61SIlya Dryomov up_read(&osdc->lock); 4264a40c4f10SYehuda Sadeh return; 4265a40c4f10SYehuda Sadeh 4266a40c4f10SYehuda Sadeh bad: 4267a40c4f10SYehuda Sadeh pr_err("osdc handle_watch_notify corrupt msg\n"); 4268a40c4f10SYehuda Sadeh } 4269a40c4f10SYehuda Sadeh 4270a40c4f10SYehuda Sadeh /* 42713d14c5d2SYehuda Sadeh * Register request, send initial attempt. 42723d14c5d2SYehuda Sadeh */ 42733d14c5d2SYehuda Sadeh int ceph_osdc_start_request(struct ceph_osd_client *osdc, 42743d14c5d2SYehuda Sadeh struct ceph_osd_request *req, 42753d14c5d2SYehuda Sadeh bool nofail) 42763d14c5d2SYehuda Sadeh { 42775aea3dcdSIlya Dryomov down_read(&osdc->lock); 42785aea3dcdSIlya Dryomov submit_request(req, false); 42795aea3dcdSIlya Dryomov up_read(&osdc->lock); 42803d14c5d2SYehuda Sadeh 42815aea3dcdSIlya Dryomov return 0; 42823d14c5d2SYehuda Sadeh } 42833d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_start_request); 42843d14c5d2SYehuda Sadeh 42853d14c5d2SYehuda Sadeh /* 4286c297eb42SIlya Dryomov * Unregister a registered request. The request is not completed: 4287c297eb42SIlya Dryomov * ->r_result isn't set and __complete_request() isn't called. 4288c9f9b93dSIlya Dryomov */ 4289c9f9b93dSIlya Dryomov void ceph_osdc_cancel_request(struct ceph_osd_request *req) 4290c9f9b93dSIlya Dryomov { 4291c9f9b93dSIlya Dryomov struct ceph_osd_client *osdc = req->r_osdc; 4292c9f9b93dSIlya Dryomov 42935aea3dcdSIlya Dryomov down_write(&osdc->lock); 42945aea3dcdSIlya Dryomov if (req->r_osd) 42955aea3dcdSIlya Dryomov cancel_request(req); 42965aea3dcdSIlya Dryomov up_write(&osdc->lock); 4297c9f9b93dSIlya Dryomov } 4298c9f9b93dSIlya Dryomov EXPORT_SYMBOL(ceph_osdc_cancel_request); 4299c9f9b93dSIlya Dryomov 4300c9f9b93dSIlya Dryomov /* 430142b06965SIlya Dryomov * @timeout: in jiffies, 0 means "wait forever" 430242b06965SIlya Dryomov */ 430342b06965SIlya Dryomov static int wait_request_timeout(struct ceph_osd_request *req, 430442b06965SIlya Dryomov unsigned long timeout) 430542b06965SIlya Dryomov { 430642b06965SIlya Dryomov long left; 430742b06965SIlya Dryomov 430842b06965SIlya Dryomov dout("%s req %p tid %llu\n", __func__, req, req->r_tid); 43090e76abf2SYan, Zheng left = wait_for_completion_killable_timeout(&req->r_completion, 431042b06965SIlya Dryomov ceph_timeout_jiffies(timeout)); 431142b06965SIlya Dryomov if (left <= 0) { 431242b06965SIlya Dryomov left = left ?: -ETIMEDOUT; 431342b06965SIlya Dryomov ceph_osdc_cancel_request(req); 431442b06965SIlya Dryomov } else { 431542b06965SIlya Dryomov left = req->r_result; /* completed */ 431642b06965SIlya Dryomov } 431742b06965SIlya Dryomov 431842b06965SIlya Dryomov return left; 431942b06965SIlya Dryomov } 432042b06965SIlya Dryomov 432142b06965SIlya Dryomov /* 43223d14c5d2SYehuda Sadeh * wait for a request to complete 43233d14c5d2SYehuda Sadeh */ 43243d14c5d2SYehuda Sadeh int ceph_osdc_wait_request(struct ceph_osd_client *osdc, 43253d14c5d2SYehuda Sadeh struct ceph_osd_request *req) 43263d14c5d2SYehuda Sadeh { 432742b06965SIlya Dryomov return wait_request_timeout(req, 0); 43283d14c5d2SYehuda Sadeh } 43293d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_wait_request); 43303d14c5d2SYehuda Sadeh 43313d14c5d2SYehuda Sadeh /* 43323d14c5d2SYehuda Sadeh * sync - wait for all in-flight requests to flush. avoid starvation. 43333d14c5d2SYehuda Sadeh */ 43343d14c5d2SYehuda Sadeh void ceph_osdc_sync(struct ceph_osd_client *osdc) 43353d14c5d2SYehuda Sadeh { 43365aea3dcdSIlya Dryomov struct rb_node *n, *p; 43375aea3dcdSIlya Dryomov u64 last_tid = atomic64_read(&osdc->last_tid); 43383d14c5d2SYehuda Sadeh 43395aea3dcdSIlya Dryomov again: 43405aea3dcdSIlya Dryomov down_read(&osdc->lock); 43415aea3dcdSIlya Dryomov for (n = rb_first(&osdc->osds); n; n = rb_next(n)) { 43425aea3dcdSIlya Dryomov struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node); 43435aea3dcdSIlya Dryomov 43445aea3dcdSIlya Dryomov mutex_lock(&osd->lock); 43455aea3dcdSIlya Dryomov for (p = rb_first(&osd->o_requests); p; p = rb_next(p)) { 43465aea3dcdSIlya Dryomov struct ceph_osd_request *req = 43475aea3dcdSIlya Dryomov rb_entry(p, struct ceph_osd_request, r_node); 43485aea3dcdSIlya Dryomov 43493d14c5d2SYehuda Sadeh if (req->r_tid > last_tid) 43503d14c5d2SYehuda Sadeh break; 43513d14c5d2SYehuda Sadeh 43525aea3dcdSIlya Dryomov if (!(req->r_flags & CEPH_OSD_FLAG_WRITE)) 43533d14c5d2SYehuda Sadeh continue; 43543d14c5d2SYehuda Sadeh 43553d14c5d2SYehuda Sadeh ceph_osdc_get_request(req); 43565aea3dcdSIlya Dryomov mutex_unlock(&osd->lock); 43575aea3dcdSIlya Dryomov up_read(&osdc->lock); 43585aea3dcdSIlya Dryomov dout("%s waiting on req %p tid %llu last_tid %llu\n", 43595aea3dcdSIlya Dryomov __func__, req, req->r_tid, last_tid); 4360b18b9550SIlya Dryomov wait_for_completion(&req->r_completion); 43613d14c5d2SYehuda Sadeh ceph_osdc_put_request(req); 43625aea3dcdSIlya Dryomov goto again; 43633d14c5d2SYehuda Sadeh } 43645aea3dcdSIlya Dryomov 43655aea3dcdSIlya Dryomov mutex_unlock(&osd->lock); 43665aea3dcdSIlya Dryomov } 43675aea3dcdSIlya Dryomov 43685aea3dcdSIlya Dryomov up_read(&osdc->lock); 43695aea3dcdSIlya Dryomov dout("%s done last_tid %llu\n", __func__, last_tid); 43703d14c5d2SYehuda Sadeh } 43713d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_sync); 43723d14c5d2SYehuda Sadeh 4373922dab61SIlya Dryomov static struct ceph_osd_request * 4374922dab61SIlya Dryomov alloc_linger_request(struct ceph_osd_linger_request *lreq) 4375922dab61SIlya Dryomov { 4376922dab61SIlya Dryomov struct ceph_osd_request *req; 4377922dab61SIlya Dryomov 4378922dab61SIlya Dryomov req = ceph_osdc_alloc_request(lreq->osdc, NULL, 1, false, GFP_NOIO); 4379922dab61SIlya Dryomov if (!req) 4380922dab61SIlya Dryomov return NULL; 4381922dab61SIlya Dryomov 4382922dab61SIlya Dryomov ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid); 4383922dab61SIlya Dryomov ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc); 4384922dab61SIlya Dryomov 4385922dab61SIlya Dryomov if (ceph_osdc_alloc_messages(req, GFP_NOIO)) { 4386922dab61SIlya Dryomov ceph_osdc_put_request(req); 4387922dab61SIlya Dryomov return NULL; 4388922dab61SIlya Dryomov } 4389922dab61SIlya Dryomov 4390922dab61SIlya Dryomov return req; 4391922dab61SIlya Dryomov } 4392922dab61SIlya Dryomov 4393922dab61SIlya Dryomov /* 4394922dab61SIlya Dryomov * Returns a handle, caller owns a ref. 4395922dab61SIlya Dryomov */ 4396922dab61SIlya Dryomov struct ceph_osd_linger_request * 4397922dab61SIlya Dryomov ceph_osdc_watch(struct ceph_osd_client *osdc, 4398922dab61SIlya Dryomov struct ceph_object_id *oid, 4399922dab61SIlya Dryomov struct ceph_object_locator *oloc, 4400922dab61SIlya Dryomov rados_watchcb2_t wcb, 4401922dab61SIlya Dryomov rados_watcherrcb_t errcb, 4402922dab61SIlya Dryomov void *data) 4403922dab61SIlya Dryomov { 4404922dab61SIlya Dryomov struct ceph_osd_linger_request *lreq; 4405922dab61SIlya Dryomov int ret; 4406922dab61SIlya Dryomov 4407922dab61SIlya Dryomov lreq = linger_alloc(osdc); 4408922dab61SIlya Dryomov if (!lreq) 4409922dab61SIlya Dryomov return ERR_PTR(-ENOMEM); 4410922dab61SIlya Dryomov 441119079203SIlya Dryomov lreq->is_watch = true; 4412922dab61SIlya Dryomov lreq->wcb = wcb; 4413922dab61SIlya Dryomov lreq->errcb = errcb; 4414922dab61SIlya Dryomov lreq->data = data; 4415b07d3c4bSIlya Dryomov lreq->watch_valid_thru = jiffies; 4416922dab61SIlya Dryomov 4417922dab61SIlya Dryomov ceph_oid_copy(&lreq->t.base_oid, oid); 4418922dab61SIlya Dryomov ceph_oloc_copy(&lreq->t.base_oloc, oloc); 441954ea0046SIlya Dryomov lreq->t.flags = CEPH_OSD_FLAG_WRITE; 44201134e091SDeepa Dinamani ktime_get_real_ts(&lreq->mtime); 4421922dab61SIlya Dryomov 4422922dab61SIlya Dryomov lreq->reg_req = alloc_linger_request(lreq); 4423922dab61SIlya Dryomov if (!lreq->reg_req) { 4424922dab61SIlya Dryomov ret = -ENOMEM; 4425922dab61SIlya Dryomov goto err_put_lreq; 4426922dab61SIlya Dryomov } 4427922dab61SIlya Dryomov 4428922dab61SIlya Dryomov lreq->ping_req = alloc_linger_request(lreq); 4429922dab61SIlya Dryomov if (!lreq->ping_req) { 4430922dab61SIlya Dryomov ret = -ENOMEM; 4431922dab61SIlya Dryomov goto err_put_lreq; 4432922dab61SIlya Dryomov } 4433922dab61SIlya Dryomov 4434922dab61SIlya Dryomov down_write(&osdc->lock); 4435922dab61SIlya Dryomov linger_register(lreq); /* before osd_req_op_* */ 4436922dab61SIlya Dryomov osd_req_op_watch_init(lreq->reg_req, 0, lreq->linger_id, 4437922dab61SIlya Dryomov CEPH_OSD_WATCH_OP_WATCH); 4438922dab61SIlya Dryomov osd_req_op_watch_init(lreq->ping_req, 0, lreq->linger_id, 4439922dab61SIlya Dryomov CEPH_OSD_WATCH_OP_PING); 4440922dab61SIlya Dryomov linger_submit(lreq); 4441922dab61SIlya Dryomov up_write(&osdc->lock); 4442922dab61SIlya Dryomov 4443922dab61SIlya Dryomov ret = linger_reg_commit_wait(lreq); 4444922dab61SIlya Dryomov if (ret) { 4445922dab61SIlya Dryomov linger_cancel(lreq); 4446922dab61SIlya Dryomov goto err_put_lreq; 4447922dab61SIlya Dryomov } 4448922dab61SIlya Dryomov 4449922dab61SIlya Dryomov return lreq; 4450922dab61SIlya Dryomov 4451922dab61SIlya Dryomov err_put_lreq: 4452922dab61SIlya Dryomov linger_put(lreq); 4453922dab61SIlya Dryomov return ERR_PTR(ret); 4454922dab61SIlya Dryomov } 4455922dab61SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_watch); 4456922dab61SIlya Dryomov 4457922dab61SIlya Dryomov /* 4458922dab61SIlya Dryomov * Releases a ref. 4459922dab61SIlya Dryomov * 4460922dab61SIlya Dryomov * Times out after mount_timeout to preserve rbd unmap behaviour 4461922dab61SIlya Dryomov * introduced in 2894e1d76974 ("rbd: timeout watch teardown on unmap 4462922dab61SIlya Dryomov * with mount_timeout"). 4463922dab61SIlya Dryomov */ 4464922dab61SIlya Dryomov int ceph_osdc_unwatch(struct ceph_osd_client *osdc, 4465922dab61SIlya Dryomov struct ceph_osd_linger_request *lreq) 4466922dab61SIlya Dryomov { 4467922dab61SIlya Dryomov struct ceph_options *opts = osdc->client->options; 4468922dab61SIlya Dryomov struct ceph_osd_request *req; 4469922dab61SIlya Dryomov int ret; 4470922dab61SIlya Dryomov 4471922dab61SIlya Dryomov req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO); 4472922dab61SIlya Dryomov if (!req) 4473922dab61SIlya Dryomov return -ENOMEM; 4474922dab61SIlya Dryomov 4475922dab61SIlya Dryomov ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid); 4476922dab61SIlya Dryomov ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc); 447754ea0046SIlya Dryomov req->r_flags = CEPH_OSD_FLAG_WRITE; 44781134e091SDeepa Dinamani ktime_get_real_ts(&req->r_mtime); 4479922dab61SIlya Dryomov osd_req_op_watch_init(req, 0, lreq->linger_id, 4480922dab61SIlya Dryomov CEPH_OSD_WATCH_OP_UNWATCH); 4481922dab61SIlya Dryomov 4482922dab61SIlya Dryomov ret = ceph_osdc_alloc_messages(req, GFP_NOIO); 4483922dab61SIlya Dryomov if (ret) 4484922dab61SIlya Dryomov goto out_put_req; 4485922dab61SIlya Dryomov 4486922dab61SIlya Dryomov ceph_osdc_start_request(osdc, req, false); 4487922dab61SIlya Dryomov linger_cancel(lreq); 4488922dab61SIlya Dryomov linger_put(lreq); 4489922dab61SIlya Dryomov ret = wait_request_timeout(req, opts->mount_timeout); 4490922dab61SIlya Dryomov 4491922dab61SIlya Dryomov out_put_req: 4492922dab61SIlya Dryomov ceph_osdc_put_request(req); 4493922dab61SIlya Dryomov return ret; 4494922dab61SIlya Dryomov } 4495922dab61SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_unwatch); 4496922dab61SIlya Dryomov 4497922dab61SIlya Dryomov static int osd_req_op_notify_ack_init(struct ceph_osd_request *req, int which, 4498922dab61SIlya Dryomov u64 notify_id, u64 cookie, void *payload, 4499922dab61SIlya Dryomov size_t payload_len) 4500922dab61SIlya Dryomov { 4501922dab61SIlya Dryomov struct ceph_osd_req_op *op; 4502922dab61SIlya Dryomov struct ceph_pagelist *pl; 4503922dab61SIlya Dryomov int ret; 4504922dab61SIlya Dryomov 4505922dab61SIlya Dryomov op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY_ACK, 0); 4506922dab61SIlya Dryomov 4507922dab61SIlya Dryomov pl = kmalloc(sizeof(*pl), GFP_NOIO); 4508922dab61SIlya Dryomov if (!pl) 4509922dab61SIlya Dryomov return -ENOMEM; 4510922dab61SIlya Dryomov 4511922dab61SIlya Dryomov ceph_pagelist_init(pl); 4512922dab61SIlya Dryomov ret = ceph_pagelist_encode_64(pl, notify_id); 4513922dab61SIlya Dryomov ret |= ceph_pagelist_encode_64(pl, cookie); 4514922dab61SIlya Dryomov if (payload) { 4515922dab61SIlya Dryomov ret |= ceph_pagelist_encode_32(pl, payload_len); 4516922dab61SIlya Dryomov ret |= ceph_pagelist_append(pl, payload, payload_len); 4517922dab61SIlya Dryomov } else { 4518922dab61SIlya Dryomov ret |= ceph_pagelist_encode_32(pl, 0); 4519922dab61SIlya Dryomov } 4520922dab61SIlya Dryomov if (ret) { 4521922dab61SIlya Dryomov ceph_pagelist_release(pl); 4522922dab61SIlya Dryomov return -ENOMEM; 4523922dab61SIlya Dryomov } 4524922dab61SIlya Dryomov 4525922dab61SIlya Dryomov ceph_osd_data_pagelist_init(&op->notify_ack.request_data, pl); 4526922dab61SIlya Dryomov op->indata_len = pl->length; 4527922dab61SIlya Dryomov return 0; 4528922dab61SIlya Dryomov } 4529922dab61SIlya Dryomov 4530922dab61SIlya Dryomov int ceph_osdc_notify_ack(struct ceph_osd_client *osdc, 4531922dab61SIlya Dryomov struct ceph_object_id *oid, 4532922dab61SIlya Dryomov struct ceph_object_locator *oloc, 4533922dab61SIlya Dryomov u64 notify_id, 4534922dab61SIlya Dryomov u64 cookie, 4535922dab61SIlya Dryomov void *payload, 4536922dab61SIlya Dryomov size_t payload_len) 4537922dab61SIlya Dryomov { 4538922dab61SIlya Dryomov struct ceph_osd_request *req; 4539922dab61SIlya Dryomov int ret; 4540922dab61SIlya Dryomov 4541922dab61SIlya Dryomov req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO); 4542922dab61SIlya Dryomov if (!req) 4543922dab61SIlya Dryomov return -ENOMEM; 4544922dab61SIlya Dryomov 4545922dab61SIlya Dryomov ceph_oid_copy(&req->r_base_oid, oid); 4546922dab61SIlya Dryomov ceph_oloc_copy(&req->r_base_oloc, oloc); 4547922dab61SIlya Dryomov req->r_flags = CEPH_OSD_FLAG_READ; 4548922dab61SIlya Dryomov 4549922dab61SIlya Dryomov ret = ceph_osdc_alloc_messages(req, GFP_NOIO); 4550922dab61SIlya Dryomov if (ret) 4551922dab61SIlya Dryomov goto out_put_req; 4552922dab61SIlya Dryomov 4553922dab61SIlya Dryomov ret = osd_req_op_notify_ack_init(req, 0, notify_id, cookie, payload, 4554922dab61SIlya Dryomov payload_len); 4555922dab61SIlya Dryomov if (ret) 4556922dab61SIlya Dryomov goto out_put_req; 4557922dab61SIlya Dryomov 4558922dab61SIlya Dryomov ceph_osdc_start_request(osdc, req, false); 4559922dab61SIlya Dryomov ret = ceph_osdc_wait_request(osdc, req); 4560922dab61SIlya Dryomov 4561922dab61SIlya Dryomov out_put_req: 4562922dab61SIlya Dryomov ceph_osdc_put_request(req); 4563922dab61SIlya Dryomov return ret; 4564922dab61SIlya Dryomov } 4565922dab61SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_notify_ack); 4566922dab61SIlya Dryomov 456719079203SIlya Dryomov static int osd_req_op_notify_init(struct ceph_osd_request *req, int which, 456819079203SIlya Dryomov u64 cookie, u32 prot_ver, u32 timeout, 456919079203SIlya Dryomov void *payload, size_t payload_len) 457019079203SIlya Dryomov { 457119079203SIlya Dryomov struct ceph_osd_req_op *op; 457219079203SIlya Dryomov struct ceph_pagelist *pl; 457319079203SIlya Dryomov int ret; 457419079203SIlya Dryomov 457519079203SIlya Dryomov op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY, 0); 457619079203SIlya Dryomov op->notify.cookie = cookie; 457719079203SIlya Dryomov 457819079203SIlya Dryomov pl = kmalloc(sizeof(*pl), GFP_NOIO); 457919079203SIlya Dryomov if (!pl) 458019079203SIlya Dryomov return -ENOMEM; 458119079203SIlya Dryomov 458219079203SIlya Dryomov ceph_pagelist_init(pl); 458319079203SIlya Dryomov ret = ceph_pagelist_encode_32(pl, 1); /* prot_ver */ 458419079203SIlya Dryomov ret |= ceph_pagelist_encode_32(pl, timeout); 458519079203SIlya Dryomov ret |= ceph_pagelist_encode_32(pl, payload_len); 458619079203SIlya Dryomov ret |= ceph_pagelist_append(pl, payload, payload_len); 458719079203SIlya Dryomov if (ret) { 458819079203SIlya Dryomov ceph_pagelist_release(pl); 458919079203SIlya Dryomov return -ENOMEM; 459019079203SIlya Dryomov } 459119079203SIlya Dryomov 459219079203SIlya Dryomov ceph_osd_data_pagelist_init(&op->notify.request_data, pl); 459319079203SIlya Dryomov op->indata_len = pl->length; 459419079203SIlya Dryomov return 0; 459519079203SIlya Dryomov } 459619079203SIlya Dryomov 459719079203SIlya Dryomov /* 459819079203SIlya Dryomov * @timeout: in seconds 459919079203SIlya Dryomov * 460019079203SIlya Dryomov * @preply_{pages,len} are initialized both on success and error. 460119079203SIlya Dryomov * The caller is responsible for: 460219079203SIlya Dryomov * 460319079203SIlya Dryomov * ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len)) 460419079203SIlya Dryomov */ 460519079203SIlya Dryomov int ceph_osdc_notify(struct ceph_osd_client *osdc, 460619079203SIlya Dryomov struct ceph_object_id *oid, 460719079203SIlya Dryomov struct ceph_object_locator *oloc, 460819079203SIlya Dryomov void *payload, 460919079203SIlya Dryomov size_t payload_len, 461019079203SIlya Dryomov u32 timeout, 461119079203SIlya Dryomov struct page ***preply_pages, 461219079203SIlya Dryomov size_t *preply_len) 461319079203SIlya Dryomov { 461419079203SIlya Dryomov struct ceph_osd_linger_request *lreq; 461519079203SIlya Dryomov struct page **pages; 461619079203SIlya Dryomov int ret; 461719079203SIlya Dryomov 461819079203SIlya Dryomov WARN_ON(!timeout); 461919079203SIlya Dryomov if (preply_pages) { 462019079203SIlya Dryomov *preply_pages = NULL; 462119079203SIlya Dryomov *preply_len = 0; 462219079203SIlya Dryomov } 462319079203SIlya Dryomov 462419079203SIlya Dryomov lreq = linger_alloc(osdc); 462519079203SIlya Dryomov if (!lreq) 462619079203SIlya Dryomov return -ENOMEM; 462719079203SIlya Dryomov 462819079203SIlya Dryomov lreq->preply_pages = preply_pages; 462919079203SIlya Dryomov lreq->preply_len = preply_len; 463019079203SIlya Dryomov 463119079203SIlya Dryomov ceph_oid_copy(&lreq->t.base_oid, oid); 463219079203SIlya Dryomov ceph_oloc_copy(&lreq->t.base_oloc, oloc); 463319079203SIlya Dryomov lreq->t.flags = CEPH_OSD_FLAG_READ; 463419079203SIlya Dryomov 463519079203SIlya Dryomov lreq->reg_req = alloc_linger_request(lreq); 463619079203SIlya Dryomov if (!lreq->reg_req) { 463719079203SIlya Dryomov ret = -ENOMEM; 463819079203SIlya Dryomov goto out_put_lreq; 463919079203SIlya Dryomov } 464019079203SIlya Dryomov 464119079203SIlya Dryomov /* for notify_id */ 464219079203SIlya Dryomov pages = ceph_alloc_page_vector(1, GFP_NOIO); 464319079203SIlya Dryomov if (IS_ERR(pages)) { 464419079203SIlya Dryomov ret = PTR_ERR(pages); 464519079203SIlya Dryomov goto out_put_lreq; 464619079203SIlya Dryomov } 464719079203SIlya Dryomov 464819079203SIlya Dryomov down_write(&osdc->lock); 464919079203SIlya Dryomov linger_register(lreq); /* before osd_req_op_* */ 465019079203SIlya Dryomov ret = osd_req_op_notify_init(lreq->reg_req, 0, lreq->linger_id, 1, 465119079203SIlya Dryomov timeout, payload, payload_len); 465219079203SIlya Dryomov if (ret) { 465319079203SIlya Dryomov linger_unregister(lreq); 465419079203SIlya Dryomov up_write(&osdc->lock); 465519079203SIlya Dryomov ceph_release_page_vector(pages, 1); 465619079203SIlya Dryomov goto out_put_lreq; 465719079203SIlya Dryomov } 465819079203SIlya Dryomov ceph_osd_data_pages_init(osd_req_op_data(lreq->reg_req, 0, notify, 465919079203SIlya Dryomov response_data), 466019079203SIlya Dryomov pages, PAGE_SIZE, 0, false, true); 466119079203SIlya Dryomov linger_submit(lreq); 466219079203SIlya Dryomov up_write(&osdc->lock); 466319079203SIlya Dryomov 466419079203SIlya Dryomov ret = linger_reg_commit_wait(lreq); 466519079203SIlya Dryomov if (!ret) 466619079203SIlya Dryomov ret = linger_notify_finish_wait(lreq); 466719079203SIlya Dryomov else 466819079203SIlya Dryomov dout("lreq %p failed to initiate notify %d\n", lreq, ret); 466919079203SIlya Dryomov 467019079203SIlya Dryomov linger_cancel(lreq); 467119079203SIlya Dryomov out_put_lreq: 467219079203SIlya Dryomov linger_put(lreq); 467319079203SIlya Dryomov return ret; 467419079203SIlya Dryomov } 467519079203SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_notify); 467619079203SIlya Dryomov 46773d14c5d2SYehuda Sadeh /* 4678b07d3c4bSIlya Dryomov * Return the number of milliseconds since the watch was last 4679b07d3c4bSIlya Dryomov * confirmed, or an error. If there is an error, the watch is no 4680b07d3c4bSIlya Dryomov * longer valid, and should be destroyed with ceph_osdc_unwatch(). 4681b07d3c4bSIlya Dryomov */ 4682b07d3c4bSIlya Dryomov int ceph_osdc_watch_check(struct ceph_osd_client *osdc, 4683b07d3c4bSIlya Dryomov struct ceph_osd_linger_request *lreq) 4684b07d3c4bSIlya Dryomov { 4685b07d3c4bSIlya Dryomov unsigned long stamp, age; 4686b07d3c4bSIlya Dryomov int ret; 4687b07d3c4bSIlya Dryomov 4688b07d3c4bSIlya Dryomov down_read(&osdc->lock); 4689b07d3c4bSIlya Dryomov mutex_lock(&lreq->lock); 4690b07d3c4bSIlya Dryomov stamp = lreq->watch_valid_thru; 4691b07d3c4bSIlya Dryomov if (!list_empty(&lreq->pending_lworks)) { 4692b07d3c4bSIlya Dryomov struct linger_work *lwork = 4693b07d3c4bSIlya Dryomov list_first_entry(&lreq->pending_lworks, 4694b07d3c4bSIlya Dryomov struct linger_work, 4695b07d3c4bSIlya Dryomov pending_item); 4696b07d3c4bSIlya Dryomov 4697b07d3c4bSIlya Dryomov if (time_before(lwork->queued_stamp, stamp)) 4698b07d3c4bSIlya Dryomov stamp = lwork->queued_stamp; 4699b07d3c4bSIlya Dryomov } 4700b07d3c4bSIlya Dryomov age = jiffies - stamp; 4701b07d3c4bSIlya Dryomov dout("%s lreq %p linger_id %llu age %lu last_error %d\n", __func__, 4702b07d3c4bSIlya Dryomov lreq, lreq->linger_id, age, lreq->last_error); 4703b07d3c4bSIlya Dryomov /* we are truncating to msecs, so return a safe upper bound */ 4704b07d3c4bSIlya Dryomov ret = lreq->last_error ?: 1 + jiffies_to_msecs(age); 4705b07d3c4bSIlya Dryomov 4706b07d3c4bSIlya Dryomov mutex_unlock(&lreq->lock); 4707b07d3c4bSIlya Dryomov up_read(&osdc->lock); 4708b07d3c4bSIlya Dryomov return ret; 4709b07d3c4bSIlya Dryomov } 4710b07d3c4bSIlya Dryomov 4711a4ed38d7SDouglas Fuller static int decode_watcher(void **p, void *end, struct ceph_watch_item *item) 4712a4ed38d7SDouglas Fuller { 4713a4ed38d7SDouglas Fuller u8 struct_v; 4714a4ed38d7SDouglas Fuller u32 struct_len; 4715a4ed38d7SDouglas Fuller int ret; 4716a4ed38d7SDouglas Fuller 4717a4ed38d7SDouglas Fuller ret = ceph_start_decoding(p, end, 2, "watch_item_t", 4718a4ed38d7SDouglas Fuller &struct_v, &struct_len); 4719a4ed38d7SDouglas Fuller if (ret) 4720a4ed38d7SDouglas Fuller return ret; 4721a4ed38d7SDouglas Fuller 4722a4ed38d7SDouglas Fuller ceph_decode_copy(p, &item->name, sizeof(item->name)); 4723a4ed38d7SDouglas Fuller item->cookie = ceph_decode_64(p); 4724a4ed38d7SDouglas Fuller *p += 4; /* skip timeout_seconds */ 4725a4ed38d7SDouglas Fuller if (struct_v >= 2) { 4726a4ed38d7SDouglas Fuller ceph_decode_copy(p, &item->addr, sizeof(item->addr)); 4727a4ed38d7SDouglas Fuller ceph_decode_addr(&item->addr); 4728a4ed38d7SDouglas Fuller } 4729a4ed38d7SDouglas Fuller 4730a4ed38d7SDouglas Fuller dout("%s %s%llu cookie %llu addr %s\n", __func__, 4731a4ed38d7SDouglas Fuller ENTITY_NAME(item->name), item->cookie, 4732a4ed38d7SDouglas Fuller ceph_pr_addr(&item->addr.in_addr)); 4733a4ed38d7SDouglas Fuller return 0; 4734a4ed38d7SDouglas Fuller } 4735a4ed38d7SDouglas Fuller 4736a4ed38d7SDouglas Fuller static int decode_watchers(void **p, void *end, 4737a4ed38d7SDouglas Fuller struct ceph_watch_item **watchers, 4738a4ed38d7SDouglas Fuller u32 *num_watchers) 4739a4ed38d7SDouglas Fuller { 4740a4ed38d7SDouglas Fuller u8 struct_v; 4741a4ed38d7SDouglas Fuller u32 struct_len; 4742a4ed38d7SDouglas Fuller int i; 4743a4ed38d7SDouglas Fuller int ret; 4744a4ed38d7SDouglas Fuller 4745a4ed38d7SDouglas Fuller ret = ceph_start_decoding(p, end, 1, "obj_list_watch_response_t", 4746a4ed38d7SDouglas Fuller &struct_v, &struct_len); 4747a4ed38d7SDouglas Fuller if (ret) 4748a4ed38d7SDouglas Fuller return ret; 4749a4ed38d7SDouglas Fuller 4750a4ed38d7SDouglas Fuller *num_watchers = ceph_decode_32(p); 4751a4ed38d7SDouglas Fuller *watchers = kcalloc(*num_watchers, sizeof(**watchers), GFP_NOIO); 4752a4ed38d7SDouglas Fuller if (!*watchers) 4753a4ed38d7SDouglas Fuller return -ENOMEM; 4754a4ed38d7SDouglas Fuller 4755a4ed38d7SDouglas Fuller for (i = 0; i < *num_watchers; i++) { 4756a4ed38d7SDouglas Fuller ret = decode_watcher(p, end, *watchers + i); 4757a4ed38d7SDouglas Fuller if (ret) { 4758a4ed38d7SDouglas Fuller kfree(*watchers); 4759a4ed38d7SDouglas Fuller return ret; 4760a4ed38d7SDouglas Fuller } 4761a4ed38d7SDouglas Fuller } 4762a4ed38d7SDouglas Fuller 4763a4ed38d7SDouglas Fuller return 0; 4764a4ed38d7SDouglas Fuller } 4765a4ed38d7SDouglas Fuller 4766a4ed38d7SDouglas Fuller /* 4767a4ed38d7SDouglas Fuller * On success, the caller is responsible for: 4768a4ed38d7SDouglas Fuller * 4769a4ed38d7SDouglas Fuller * kfree(watchers); 4770a4ed38d7SDouglas Fuller */ 4771a4ed38d7SDouglas Fuller int ceph_osdc_list_watchers(struct ceph_osd_client *osdc, 4772a4ed38d7SDouglas Fuller struct ceph_object_id *oid, 4773a4ed38d7SDouglas Fuller struct ceph_object_locator *oloc, 4774a4ed38d7SDouglas Fuller struct ceph_watch_item **watchers, 4775a4ed38d7SDouglas Fuller u32 *num_watchers) 4776a4ed38d7SDouglas Fuller { 4777a4ed38d7SDouglas Fuller struct ceph_osd_request *req; 4778a4ed38d7SDouglas Fuller struct page **pages; 4779a4ed38d7SDouglas Fuller int ret; 4780a4ed38d7SDouglas Fuller 4781a4ed38d7SDouglas Fuller req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO); 4782a4ed38d7SDouglas Fuller if (!req) 4783a4ed38d7SDouglas Fuller return -ENOMEM; 4784a4ed38d7SDouglas Fuller 4785a4ed38d7SDouglas Fuller ceph_oid_copy(&req->r_base_oid, oid); 4786a4ed38d7SDouglas Fuller ceph_oloc_copy(&req->r_base_oloc, oloc); 4787a4ed38d7SDouglas Fuller req->r_flags = CEPH_OSD_FLAG_READ; 4788a4ed38d7SDouglas Fuller 4789a4ed38d7SDouglas Fuller ret = ceph_osdc_alloc_messages(req, GFP_NOIO); 4790a4ed38d7SDouglas Fuller if (ret) 4791a4ed38d7SDouglas Fuller goto out_put_req; 4792a4ed38d7SDouglas Fuller 4793a4ed38d7SDouglas Fuller pages = ceph_alloc_page_vector(1, GFP_NOIO); 4794a4ed38d7SDouglas Fuller if (IS_ERR(pages)) { 4795a4ed38d7SDouglas Fuller ret = PTR_ERR(pages); 4796a4ed38d7SDouglas Fuller goto out_put_req; 4797a4ed38d7SDouglas Fuller } 4798a4ed38d7SDouglas Fuller 4799a4ed38d7SDouglas Fuller osd_req_op_init(req, 0, CEPH_OSD_OP_LIST_WATCHERS, 0); 4800a4ed38d7SDouglas Fuller ceph_osd_data_pages_init(osd_req_op_data(req, 0, list_watchers, 4801a4ed38d7SDouglas Fuller response_data), 4802a4ed38d7SDouglas Fuller pages, PAGE_SIZE, 0, false, true); 4803a4ed38d7SDouglas Fuller 4804a4ed38d7SDouglas Fuller ceph_osdc_start_request(osdc, req, false); 4805a4ed38d7SDouglas Fuller ret = ceph_osdc_wait_request(osdc, req); 4806a4ed38d7SDouglas Fuller if (ret >= 0) { 4807a4ed38d7SDouglas Fuller void *p = page_address(pages[0]); 4808a4ed38d7SDouglas Fuller void *const end = p + req->r_ops[0].outdata_len; 4809a4ed38d7SDouglas Fuller 4810a4ed38d7SDouglas Fuller ret = decode_watchers(&p, end, watchers, num_watchers); 4811a4ed38d7SDouglas Fuller } 4812a4ed38d7SDouglas Fuller 4813a4ed38d7SDouglas Fuller out_put_req: 4814a4ed38d7SDouglas Fuller ceph_osdc_put_request(req); 4815a4ed38d7SDouglas Fuller return ret; 4816a4ed38d7SDouglas Fuller } 4817a4ed38d7SDouglas Fuller EXPORT_SYMBOL(ceph_osdc_list_watchers); 4818a4ed38d7SDouglas Fuller 4819b07d3c4bSIlya Dryomov /* 4820dd935f44SJosh Durgin * Call all pending notify callbacks - for use after a watch is 4821dd935f44SJosh Durgin * unregistered, to make sure no more callbacks for it will be invoked 4822dd935f44SJosh Durgin */ 4823f6479449Sstephen hemminger void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc) 4824dd935f44SJosh Durgin { 482599d16943SIlya Dryomov dout("%s osdc %p\n", __func__, osdc); 4826dd935f44SJosh Durgin flush_workqueue(osdc->notify_wq); 4827dd935f44SJosh Durgin } 4828dd935f44SJosh Durgin EXPORT_SYMBOL(ceph_osdc_flush_notifies); 4829dd935f44SJosh Durgin 48307cca78c9SIlya Dryomov void ceph_osdc_maybe_request_map(struct ceph_osd_client *osdc) 48317cca78c9SIlya Dryomov { 48327cca78c9SIlya Dryomov down_read(&osdc->lock); 48337cca78c9SIlya Dryomov maybe_request_map(osdc); 48347cca78c9SIlya Dryomov up_read(&osdc->lock); 48357cca78c9SIlya Dryomov } 48367cca78c9SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_maybe_request_map); 4837dd935f44SJosh Durgin 4838dd935f44SJosh Durgin /* 4839428a7158SDouglas Fuller * Execute an OSD class method on an object. 4840428a7158SDouglas Fuller * 4841428a7158SDouglas Fuller * @flags: CEPH_OSD_FLAG_* 48422544a020SIlya Dryomov * @resp_len: in/out param for reply length 4843428a7158SDouglas Fuller */ 4844428a7158SDouglas Fuller int ceph_osdc_call(struct ceph_osd_client *osdc, 4845428a7158SDouglas Fuller struct ceph_object_id *oid, 4846428a7158SDouglas Fuller struct ceph_object_locator *oloc, 4847428a7158SDouglas Fuller const char *class, const char *method, 4848428a7158SDouglas Fuller unsigned int flags, 4849428a7158SDouglas Fuller struct page *req_page, size_t req_len, 4850428a7158SDouglas Fuller struct page *resp_page, size_t *resp_len) 4851428a7158SDouglas Fuller { 4852428a7158SDouglas Fuller struct ceph_osd_request *req; 4853428a7158SDouglas Fuller int ret; 4854428a7158SDouglas Fuller 48552544a020SIlya Dryomov if (req_len > PAGE_SIZE || (resp_page && *resp_len > PAGE_SIZE)) 48562544a020SIlya Dryomov return -E2BIG; 48572544a020SIlya Dryomov 4858428a7158SDouglas Fuller req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO); 4859428a7158SDouglas Fuller if (!req) 4860428a7158SDouglas Fuller return -ENOMEM; 4861428a7158SDouglas Fuller 4862428a7158SDouglas Fuller ceph_oid_copy(&req->r_base_oid, oid); 4863428a7158SDouglas Fuller ceph_oloc_copy(&req->r_base_oloc, oloc); 4864428a7158SDouglas Fuller req->r_flags = flags; 4865428a7158SDouglas Fuller 4866428a7158SDouglas Fuller ret = ceph_osdc_alloc_messages(req, GFP_NOIO); 4867428a7158SDouglas Fuller if (ret) 4868428a7158SDouglas Fuller goto out_put_req; 4869428a7158SDouglas Fuller 4870428a7158SDouglas Fuller osd_req_op_cls_init(req, 0, CEPH_OSD_OP_CALL, class, method); 4871428a7158SDouglas Fuller if (req_page) 4872428a7158SDouglas Fuller osd_req_op_cls_request_data_pages(req, 0, &req_page, req_len, 4873428a7158SDouglas Fuller 0, false, false); 4874428a7158SDouglas Fuller if (resp_page) 4875428a7158SDouglas Fuller osd_req_op_cls_response_data_pages(req, 0, &resp_page, 48762544a020SIlya Dryomov *resp_len, 0, false, false); 4877428a7158SDouglas Fuller 4878428a7158SDouglas Fuller ceph_osdc_start_request(osdc, req, false); 4879428a7158SDouglas Fuller ret = ceph_osdc_wait_request(osdc, req); 4880428a7158SDouglas Fuller if (ret >= 0) { 4881428a7158SDouglas Fuller ret = req->r_ops[0].rval; 4882428a7158SDouglas Fuller if (resp_page) 4883428a7158SDouglas Fuller *resp_len = req->r_ops[0].outdata_len; 4884428a7158SDouglas Fuller } 4885428a7158SDouglas Fuller 4886428a7158SDouglas Fuller out_put_req: 4887428a7158SDouglas Fuller ceph_osdc_put_request(req); 4888428a7158SDouglas Fuller return ret; 4889428a7158SDouglas Fuller } 4890428a7158SDouglas Fuller EXPORT_SYMBOL(ceph_osdc_call); 4891428a7158SDouglas Fuller 4892428a7158SDouglas Fuller /* 48933d14c5d2SYehuda Sadeh * init, shutdown 48943d14c5d2SYehuda Sadeh */ 48953d14c5d2SYehuda Sadeh int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client) 48963d14c5d2SYehuda Sadeh { 48973d14c5d2SYehuda Sadeh int err; 48983d14c5d2SYehuda Sadeh 48993d14c5d2SYehuda Sadeh dout("init\n"); 49003d14c5d2SYehuda Sadeh osdc->client = client; 49015aea3dcdSIlya Dryomov init_rwsem(&osdc->lock); 49023d14c5d2SYehuda Sadeh osdc->osds = RB_ROOT; 49033d14c5d2SYehuda Sadeh INIT_LIST_HEAD(&osdc->osd_lru); 49049dd2845cSIlya Dryomov spin_lock_init(&osdc->osd_lru_lock); 49055aea3dcdSIlya Dryomov osd_init(&osdc->homeless_osd); 49065aea3dcdSIlya Dryomov osdc->homeless_osd.o_osdc = osdc; 49075aea3dcdSIlya Dryomov osdc->homeless_osd.o_osd = CEPH_HOMELESS_OSD; 4908264048afSIlya Dryomov osdc->last_linger_id = CEPH_LINGER_ID_START; 4909922dab61SIlya Dryomov osdc->linger_requests = RB_ROOT; 49104609245eSIlya Dryomov osdc->map_checks = RB_ROOT; 49114609245eSIlya Dryomov osdc->linger_map_checks = RB_ROOT; 49123d14c5d2SYehuda Sadeh INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout); 49133d14c5d2SYehuda Sadeh INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout); 49143d14c5d2SYehuda Sadeh 49153d14c5d2SYehuda Sadeh err = -ENOMEM; 4916e5253a7bSIlya Dryomov osdc->osdmap = ceph_osdmap_alloc(); 4917e5253a7bSIlya Dryomov if (!osdc->osdmap) 4918e5253a7bSIlya Dryomov goto out; 4919e5253a7bSIlya Dryomov 49209e767adbSIlya Dryomov osdc->req_mempool = mempool_create_slab_pool(10, 49219e767adbSIlya Dryomov ceph_osd_request_cache); 49223d14c5d2SYehuda Sadeh if (!osdc->req_mempool) 4923e5253a7bSIlya Dryomov goto out_map; 49243d14c5d2SYehuda Sadeh 4925d50b409fSSage Weil err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP, 4926711da55dSIlya Dryomov PAGE_SIZE, 10, true, "osd_op"); 49273d14c5d2SYehuda Sadeh if (err < 0) 49283d14c5d2SYehuda Sadeh goto out_mempool; 4929d50b409fSSage Weil err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY, 4930711da55dSIlya Dryomov PAGE_SIZE, 10, true, "osd_op_reply"); 49313d14c5d2SYehuda Sadeh if (err < 0) 49323d14c5d2SYehuda Sadeh goto out_msgpool; 4933a40c4f10SYehuda Sadeh 4934dbcae088SDan Carpenter err = -ENOMEM; 4935a40c4f10SYehuda Sadeh osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify"); 4936dbcae088SDan Carpenter if (!osdc->notify_wq) 4937c172ec5cSIlya Dryomov goto out_msgpool_reply; 4938c172ec5cSIlya Dryomov 4939fbca9635SIlya Dryomov schedule_delayed_work(&osdc->timeout_work, 4940fbca9635SIlya Dryomov osdc->client->options->osd_keepalive_timeout); 4941b37ee1b9SIlya Dryomov schedule_delayed_work(&osdc->osds_timeout_work, 4942b37ee1b9SIlya Dryomov round_jiffies_relative(osdc->client->options->osd_idle_ttl)); 4943b37ee1b9SIlya Dryomov 49443d14c5d2SYehuda Sadeh return 0; 49453d14c5d2SYehuda Sadeh 4946c172ec5cSIlya Dryomov out_msgpool_reply: 4947c172ec5cSIlya Dryomov ceph_msgpool_destroy(&osdc->msgpool_op_reply); 49483d14c5d2SYehuda Sadeh out_msgpool: 49493d14c5d2SYehuda Sadeh ceph_msgpool_destroy(&osdc->msgpool_op); 49503d14c5d2SYehuda Sadeh out_mempool: 49513d14c5d2SYehuda Sadeh mempool_destroy(osdc->req_mempool); 4952e5253a7bSIlya Dryomov out_map: 4953e5253a7bSIlya Dryomov ceph_osdmap_destroy(osdc->osdmap); 49543d14c5d2SYehuda Sadeh out: 49553d14c5d2SYehuda Sadeh return err; 49563d14c5d2SYehuda Sadeh } 49573d14c5d2SYehuda Sadeh 49583d14c5d2SYehuda Sadeh void ceph_osdc_stop(struct ceph_osd_client *osdc) 49593d14c5d2SYehuda Sadeh { 4960a40c4f10SYehuda Sadeh flush_workqueue(osdc->notify_wq); 4961a40c4f10SYehuda Sadeh destroy_workqueue(osdc->notify_wq); 49623d14c5d2SYehuda Sadeh cancel_delayed_work_sync(&osdc->timeout_work); 49633d14c5d2SYehuda Sadeh cancel_delayed_work_sync(&osdc->osds_timeout_work); 496442a2c09fSIlya Dryomov 49655aea3dcdSIlya Dryomov down_write(&osdc->lock); 496642a2c09fSIlya Dryomov while (!RB_EMPTY_ROOT(&osdc->osds)) { 496742a2c09fSIlya Dryomov struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds), 496842a2c09fSIlya Dryomov struct ceph_osd, o_node); 49695aea3dcdSIlya Dryomov close_osd(osd); 497042a2c09fSIlya Dryomov } 49715aea3dcdSIlya Dryomov up_write(&osdc->lock); 497202113a0fSElena Reshetova WARN_ON(refcount_read(&osdc->homeless_osd.o_ref) != 1); 49735aea3dcdSIlya Dryomov osd_cleanup(&osdc->homeless_osd); 49745aea3dcdSIlya Dryomov 49755aea3dcdSIlya Dryomov WARN_ON(!list_empty(&osdc->osd_lru)); 4976922dab61SIlya Dryomov WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_requests)); 49774609245eSIlya Dryomov WARN_ON(!RB_EMPTY_ROOT(&osdc->map_checks)); 49784609245eSIlya Dryomov WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_map_checks)); 49795aea3dcdSIlya Dryomov WARN_ON(atomic_read(&osdc->num_requests)); 49805aea3dcdSIlya Dryomov WARN_ON(atomic_read(&osdc->num_homeless)); 498142a2c09fSIlya Dryomov 49823d14c5d2SYehuda Sadeh ceph_osdmap_destroy(osdc->osdmap); 49833d14c5d2SYehuda Sadeh mempool_destroy(osdc->req_mempool); 49843d14c5d2SYehuda Sadeh ceph_msgpool_destroy(&osdc->msgpool_op); 49853d14c5d2SYehuda Sadeh ceph_msgpool_destroy(&osdc->msgpool_op_reply); 49863d14c5d2SYehuda Sadeh } 49873d14c5d2SYehuda Sadeh 49883d14c5d2SYehuda Sadeh /* 49893d14c5d2SYehuda Sadeh * Read some contiguous pages. If we cross a stripe boundary, shorten 49903d14c5d2SYehuda Sadeh * *plen. Return number of bytes read, or error. 49913d14c5d2SYehuda Sadeh */ 49923d14c5d2SYehuda Sadeh int ceph_osdc_readpages(struct ceph_osd_client *osdc, 49933d14c5d2SYehuda Sadeh struct ceph_vino vino, struct ceph_file_layout *layout, 49943d14c5d2SYehuda Sadeh u64 off, u64 *plen, 49953d14c5d2SYehuda Sadeh u32 truncate_seq, u64 truncate_size, 4996b7495fc2SSage Weil struct page **pages, int num_pages, int page_align) 49973d14c5d2SYehuda Sadeh { 49983d14c5d2SYehuda Sadeh struct ceph_osd_request *req; 49993d14c5d2SYehuda Sadeh int rc = 0; 50003d14c5d2SYehuda Sadeh 50013d14c5d2SYehuda Sadeh dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino, 50023d14c5d2SYehuda Sadeh vino.snap, off, *plen); 5003715e4cd4SYan, Zheng req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1, 50043d14c5d2SYehuda Sadeh CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ, 5005acead002SAlex Elder NULL, truncate_seq, truncate_size, 5006153e5167SAlex Elder false); 50076816282dSSage Weil if (IS_ERR(req)) 50086816282dSSage Weil return PTR_ERR(req); 50093d14c5d2SYehuda Sadeh 50103d14c5d2SYehuda Sadeh /* it may be a short read due to an object boundary */ 5011406e2c9fSAlex Elder osd_req_op_extent_osd_data_pages(req, 0, 5012a4ce40a9SAlex Elder pages, *plen, page_align, false, false); 50133d14c5d2SYehuda Sadeh 5014e0c59487SAlex Elder dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n", 501543bfe5deSAlex Elder off, *plen, *plen, page_align); 50163d14c5d2SYehuda Sadeh 50173d14c5d2SYehuda Sadeh rc = ceph_osdc_start_request(osdc, req, false); 50183d14c5d2SYehuda Sadeh if (!rc) 50193d14c5d2SYehuda Sadeh rc = ceph_osdc_wait_request(osdc, req); 50203d14c5d2SYehuda Sadeh 50213d14c5d2SYehuda Sadeh ceph_osdc_put_request(req); 50223d14c5d2SYehuda Sadeh dout("readpages result %d\n", rc); 50233d14c5d2SYehuda Sadeh return rc; 50243d14c5d2SYehuda Sadeh } 50253d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_readpages); 50263d14c5d2SYehuda Sadeh 50273d14c5d2SYehuda Sadeh /* 50283d14c5d2SYehuda Sadeh * do a synchronous write on N pages 50293d14c5d2SYehuda Sadeh */ 50303d14c5d2SYehuda Sadeh int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino, 50313d14c5d2SYehuda Sadeh struct ceph_file_layout *layout, 50323d14c5d2SYehuda Sadeh struct ceph_snap_context *snapc, 50333d14c5d2SYehuda Sadeh u64 off, u64 len, 50343d14c5d2SYehuda Sadeh u32 truncate_seq, u64 truncate_size, 50353d14c5d2SYehuda Sadeh struct timespec *mtime, 503624808826SAlex Elder struct page **pages, int num_pages) 50373d14c5d2SYehuda Sadeh { 50383d14c5d2SYehuda Sadeh struct ceph_osd_request *req; 50393d14c5d2SYehuda Sadeh int rc = 0; 5040b7495fc2SSage Weil int page_align = off & ~PAGE_MASK; 50413d14c5d2SYehuda Sadeh 5042715e4cd4SYan, Zheng req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1, 504354ea0046SIlya Dryomov CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE, 5044acead002SAlex Elder snapc, truncate_seq, truncate_size, 5045153e5167SAlex Elder true); 50466816282dSSage Weil if (IS_ERR(req)) 50476816282dSSage Weil return PTR_ERR(req); 50483d14c5d2SYehuda Sadeh 50493d14c5d2SYehuda Sadeh /* it may be a short write due to an object boundary */ 5050406e2c9fSAlex Elder osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align, 505143bfe5deSAlex Elder false, false); 505243bfe5deSAlex Elder dout("writepages %llu~%llu (%llu bytes)\n", off, len, len); 50533d14c5d2SYehuda Sadeh 5054bb873b53SIlya Dryomov req->r_mtime = *mtime; 505587f979d3SAlex Elder rc = ceph_osdc_start_request(osdc, req, true); 50563d14c5d2SYehuda Sadeh if (!rc) 50573d14c5d2SYehuda Sadeh rc = ceph_osdc_wait_request(osdc, req); 50583d14c5d2SYehuda Sadeh 50593d14c5d2SYehuda Sadeh ceph_osdc_put_request(req); 50603d14c5d2SYehuda Sadeh if (rc == 0) 50613d14c5d2SYehuda Sadeh rc = len; 50623d14c5d2SYehuda Sadeh dout("writepages result %d\n", rc); 50633d14c5d2SYehuda Sadeh return rc; 50643d14c5d2SYehuda Sadeh } 50653d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_writepages); 50663d14c5d2SYehuda Sadeh 50675522ae0bSAlex Elder int ceph_osdc_setup(void) 50685522ae0bSAlex Elder { 50693f1af42aSIlya Dryomov size_t size = sizeof(struct ceph_osd_request) + 50703f1af42aSIlya Dryomov CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op); 50713f1af42aSIlya Dryomov 50725522ae0bSAlex Elder BUG_ON(ceph_osd_request_cache); 50733f1af42aSIlya Dryomov ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size, 50743f1af42aSIlya Dryomov 0, 0, NULL); 50755522ae0bSAlex Elder 50765522ae0bSAlex Elder return ceph_osd_request_cache ? 0 : -ENOMEM; 50775522ae0bSAlex Elder } 50785522ae0bSAlex Elder EXPORT_SYMBOL(ceph_osdc_setup); 50795522ae0bSAlex Elder 50805522ae0bSAlex Elder void ceph_osdc_cleanup(void) 50815522ae0bSAlex Elder { 50825522ae0bSAlex Elder BUG_ON(!ceph_osd_request_cache); 50835522ae0bSAlex Elder kmem_cache_destroy(ceph_osd_request_cache); 50845522ae0bSAlex Elder ceph_osd_request_cache = NULL; 50855522ae0bSAlex Elder } 50865522ae0bSAlex Elder EXPORT_SYMBOL(ceph_osdc_cleanup); 50875522ae0bSAlex Elder 50883d14c5d2SYehuda Sadeh /* 50893d14c5d2SYehuda Sadeh * handle incoming message 50903d14c5d2SYehuda Sadeh */ 50913d14c5d2SYehuda Sadeh static void dispatch(struct ceph_connection *con, struct ceph_msg *msg) 50923d14c5d2SYehuda Sadeh { 50933d14c5d2SYehuda Sadeh struct ceph_osd *osd = con->private; 50945aea3dcdSIlya Dryomov struct ceph_osd_client *osdc = osd->o_osdc; 50953d14c5d2SYehuda Sadeh int type = le16_to_cpu(msg->hdr.type); 50963d14c5d2SYehuda Sadeh 50973d14c5d2SYehuda Sadeh switch (type) { 50983d14c5d2SYehuda Sadeh case CEPH_MSG_OSD_MAP: 50993d14c5d2SYehuda Sadeh ceph_osdc_handle_map(osdc, msg); 51003d14c5d2SYehuda Sadeh break; 51013d14c5d2SYehuda Sadeh case CEPH_MSG_OSD_OPREPLY: 51025aea3dcdSIlya Dryomov handle_reply(osd, msg); 51033d14c5d2SYehuda Sadeh break; 5104a02a946dSIlya Dryomov case CEPH_MSG_OSD_BACKOFF: 5105a02a946dSIlya Dryomov handle_backoff(osd, msg); 5106a02a946dSIlya Dryomov break; 5107a40c4f10SYehuda Sadeh case CEPH_MSG_WATCH_NOTIFY: 5108a40c4f10SYehuda Sadeh handle_watch_notify(osdc, msg); 5109a40c4f10SYehuda Sadeh break; 51103d14c5d2SYehuda Sadeh 51113d14c5d2SYehuda Sadeh default: 51123d14c5d2SYehuda Sadeh pr_err("received unknown message type %d %s\n", type, 51133d14c5d2SYehuda Sadeh ceph_msg_type_name(type)); 51143d14c5d2SYehuda Sadeh } 51155aea3dcdSIlya Dryomov 51163d14c5d2SYehuda Sadeh ceph_msg_put(msg); 51173d14c5d2SYehuda Sadeh } 51183d14c5d2SYehuda Sadeh 51193d14c5d2SYehuda Sadeh /* 5120d15f9d69SIlya Dryomov * Lookup and return message for incoming reply. Don't try to do 5121d15f9d69SIlya Dryomov * anything about a larger than preallocated data portion of the 5122d15f9d69SIlya Dryomov * message at the moment - for now, just skip the message. 51233d14c5d2SYehuda Sadeh */ 51243d14c5d2SYehuda Sadeh static struct ceph_msg *get_reply(struct ceph_connection *con, 51253d14c5d2SYehuda Sadeh struct ceph_msg_header *hdr, 51263d14c5d2SYehuda Sadeh int *skip) 51273d14c5d2SYehuda Sadeh { 51283d14c5d2SYehuda Sadeh struct ceph_osd *osd = con->private; 51293d14c5d2SYehuda Sadeh struct ceph_osd_client *osdc = osd->o_osdc; 51305aea3dcdSIlya Dryomov struct ceph_msg *m = NULL; 51313d14c5d2SYehuda Sadeh struct ceph_osd_request *req; 51323f0a4ac5SIlya Dryomov int front_len = le32_to_cpu(hdr->front_len); 51333d14c5d2SYehuda Sadeh int data_len = le32_to_cpu(hdr->data_len); 51345aea3dcdSIlya Dryomov u64 tid = le64_to_cpu(hdr->tid); 51353d14c5d2SYehuda Sadeh 51365aea3dcdSIlya Dryomov down_read(&osdc->lock); 51375aea3dcdSIlya Dryomov if (!osd_registered(osd)) { 51385aea3dcdSIlya Dryomov dout("%s osd%d unknown, skipping\n", __func__, osd->o_osd); 51395aea3dcdSIlya Dryomov *skip = 1; 51405aea3dcdSIlya Dryomov goto out_unlock_osdc; 51415aea3dcdSIlya Dryomov } 51425aea3dcdSIlya Dryomov WARN_ON(osd->o_osd != le64_to_cpu(hdr->src.num)); 51435aea3dcdSIlya Dryomov 51445aea3dcdSIlya Dryomov mutex_lock(&osd->lock); 51455aea3dcdSIlya Dryomov req = lookup_request(&osd->o_requests, tid); 51463d14c5d2SYehuda Sadeh if (!req) { 5147cd8140c6SIlya Dryomov dout("%s osd%d tid %llu unknown, skipping\n", __func__, 5148cd8140c6SIlya Dryomov osd->o_osd, tid); 5149d15f9d69SIlya Dryomov *skip = 1; 51505aea3dcdSIlya Dryomov goto out_unlock_session; 51513d14c5d2SYehuda Sadeh } 51523d14c5d2SYehuda Sadeh 51538921d114SAlex Elder ceph_msg_revoke_incoming(req->r_reply); 51543d14c5d2SYehuda Sadeh 5155f2be82b0SIlya Dryomov if (front_len > req->r_reply->front_alloc_len) { 5156d15f9d69SIlya Dryomov pr_warn("%s osd%d tid %llu front %d > preallocated %d\n", 5157d15f9d69SIlya Dryomov __func__, osd->o_osd, req->r_tid, front_len, 5158d15f9d69SIlya Dryomov req->r_reply->front_alloc_len); 51593f0a4ac5SIlya Dryomov m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS, 51603f0a4ac5SIlya Dryomov false); 51613d14c5d2SYehuda Sadeh if (!m) 51625aea3dcdSIlya Dryomov goto out_unlock_session; 51633d14c5d2SYehuda Sadeh ceph_msg_put(req->r_reply); 51643d14c5d2SYehuda Sadeh req->r_reply = m; 51653d14c5d2SYehuda Sadeh } 51663d14c5d2SYehuda Sadeh 5167d15f9d69SIlya Dryomov if (data_len > req->r_reply->data_length) { 5168d15f9d69SIlya Dryomov pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n", 5169d15f9d69SIlya Dryomov __func__, osd->o_osd, req->r_tid, data_len, 5170d15f9d69SIlya Dryomov req->r_reply->data_length); 51713d14c5d2SYehuda Sadeh m = NULL; 5172d15f9d69SIlya Dryomov *skip = 1; 51735aea3dcdSIlya Dryomov goto out_unlock_session; 51743d14c5d2SYehuda Sadeh } 5175d15f9d69SIlya Dryomov 5176d15f9d69SIlya Dryomov m = ceph_msg_get(req->r_reply); 51773d14c5d2SYehuda Sadeh dout("get_reply tid %lld %p\n", tid, m); 51783d14c5d2SYehuda Sadeh 51795aea3dcdSIlya Dryomov out_unlock_session: 51805aea3dcdSIlya Dryomov mutex_unlock(&osd->lock); 51815aea3dcdSIlya Dryomov out_unlock_osdc: 51825aea3dcdSIlya Dryomov up_read(&osdc->lock); 51833d14c5d2SYehuda Sadeh return m; 51843d14c5d2SYehuda Sadeh } 51853d14c5d2SYehuda Sadeh 518619079203SIlya Dryomov /* 518719079203SIlya Dryomov * TODO: switch to a msg-owned pagelist 518819079203SIlya Dryomov */ 518919079203SIlya Dryomov static struct ceph_msg *alloc_msg_with_page_vector(struct ceph_msg_header *hdr) 519019079203SIlya Dryomov { 519119079203SIlya Dryomov struct ceph_msg *m; 519219079203SIlya Dryomov int type = le16_to_cpu(hdr->type); 519319079203SIlya Dryomov u32 front_len = le32_to_cpu(hdr->front_len); 519419079203SIlya Dryomov u32 data_len = le32_to_cpu(hdr->data_len); 519519079203SIlya Dryomov 519619079203SIlya Dryomov m = ceph_msg_new(type, front_len, GFP_NOIO, false); 519719079203SIlya Dryomov if (!m) 519819079203SIlya Dryomov return NULL; 519919079203SIlya Dryomov 520019079203SIlya Dryomov if (data_len) { 520119079203SIlya Dryomov struct page **pages; 520219079203SIlya Dryomov struct ceph_osd_data osd_data; 520319079203SIlya Dryomov 520419079203SIlya Dryomov pages = ceph_alloc_page_vector(calc_pages_for(0, data_len), 520519079203SIlya Dryomov GFP_NOIO); 5206c22e853aSWei Yongjun if (IS_ERR(pages)) { 520719079203SIlya Dryomov ceph_msg_put(m); 520819079203SIlya Dryomov return NULL; 520919079203SIlya Dryomov } 521019079203SIlya Dryomov 521119079203SIlya Dryomov ceph_osd_data_pages_init(&osd_data, pages, data_len, 0, false, 521219079203SIlya Dryomov false); 521319079203SIlya Dryomov ceph_osdc_msg_data_add(m, &osd_data); 521419079203SIlya Dryomov } 521519079203SIlya Dryomov 521619079203SIlya Dryomov return m; 521719079203SIlya Dryomov } 521819079203SIlya Dryomov 52193d14c5d2SYehuda Sadeh static struct ceph_msg *alloc_msg(struct ceph_connection *con, 52203d14c5d2SYehuda Sadeh struct ceph_msg_header *hdr, 52213d14c5d2SYehuda Sadeh int *skip) 52223d14c5d2SYehuda Sadeh { 52233d14c5d2SYehuda Sadeh struct ceph_osd *osd = con->private; 52243d14c5d2SYehuda Sadeh int type = le16_to_cpu(hdr->type); 52253d14c5d2SYehuda Sadeh 52261c20f2d2SAlex Elder *skip = 0; 52273d14c5d2SYehuda Sadeh switch (type) { 52283d14c5d2SYehuda Sadeh case CEPH_MSG_OSD_MAP: 5229a02a946dSIlya Dryomov case CEPH_MSG_OSD_BACKOFF: 5230a40c4f10SYehuda Sadeh case CEPH_MSG_WATCH_NOTIFY: 523119079203SIlya Dryomov return alloc_msg_with_page_vector(hdr); 52323d14c5d2SYehuda Sadeh case CEPH_MSG_OSD_OPREPLY: 52333d14c5d2SYehuda Sadeh return get_reply(con, hdr, skip); 52343d14c5d2SYehuda Sadeh default: 52355aea3dcdSIlya Dryomov pr_warn("%s osd%d unknown msg type %d, skipping\n", __func__, 52365aea3dcdSIlya Dryomov osd->o_osd, type); 52373d14c5d2SYehuda Sadeh *skip = 1; 52383d14c5d2SYehuda Sadeh return NULL; 52393d14c5d2SYehuda Sadeh } 52403d14c5d2SYehuda Sadeh } 52413d14c5d2SYehuda Sadeh 52423d14c5d2SYehuda Sadeh /* 52433d14c5d2SYehuda Sadeh * Wrappers to refcount containing ceph_osd struct 52443d14c5d2SYehuda Sadeh */ 52453d14c5d2SYehuda Sadeh static struct ceph_connection *get_osd_con(struct ceph_connection *con) 52463d14c5d2SYehuda Sadeh { 52473d14c5d2SYehuda Sadeh struct ceph_osd *osd = con->private; 52483d14c5d2SYehuda Sadeh if (get_osd(osd)) 52493d14c5d2SYehuda Sadeh return con; 52503d14c5d2SYehuda Sadeh return NULL; 52513d14c5d2SYehuda Sadeh } 52523d14c5d2SYehuda Sadeh 52533d14c5d2SYehuda Sadeh static void put_osd_con(struct ceph_connection *con) 52543d14c5d2SYehuda Sadeh { 52553d14c5d2SYehuda Sadeh struct ceph_osd *osd = con->private; 52563d14c5d2SYehuda Sadeh put_osd(osd); 52573d14c5d2SYehuda Sadeh } 52583d14c5d2SYehuda Sadeh 52593d14c5d2SYehuda Sadeh /* 52603d14c5d2SYehuda Sadeh * authentication 52613d14c5d2SYehuda Sadeh */ 5262a3530df3SAlex Elder /* 5263a3530df3SAlex Elder * Note: returned pointer is the address of a structure that's 5264a3530df3SAlex Elder * managed separately. Caller must *not* attempt to free it. 5265a3530df3SAlex Elder */ 5266a3530df3SAlex Elder static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con, 52678f43fb53SAlex Elder int *proto, int force_new) 52683d14c5d2SYehuda Sadeh { 52693d14c5d2SYehuda Sadeh struct ceph_osd *o = con->private; 52703d14c5d2SYehuda Sadeh struct ceph_osd_client *osdc = o->o_osdc; 52713d14c5d2SYehuda Sadeh struct ceph_auth_client *ac = osdc->client->monc.auth; 527274f1869fSAlex Elder struct ceph_auth_handshake *auth = &o->o_auth; 52733d14c5d2SYehuda Sadeh 527474f1869fSAlex Elder if (force_new && auth->authorizer) { 52756c1ea260SIlya Dryomov ceph_auth_destroy_authorizer(auth->authorizer); 527674f1869fSAlex Elder auth->authorizer = NULL; 52773d14c5d2SYehuda Sadeh } 527827859f97SSage Weil if (!auth->authorizer) { 527927859f97SSage Weil int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD, 5280a3530df3SAlex Elder auth); 52813d14c5d2SYehuda Sadeh if (ret) 5282a3530df3SAlex Elder return ERR_PTR(ret); 528327859f97SSage Weil } else { 528427859f97SSage Weil int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD, 52850bed9b5cSSage Weil auth); 52860bed9b5cSSage Weil if (ret) 52870bed9b5cSSage Weil return ERR_PTR(ret); 52883d14c5d2SYehuda Sadeh } 52893d14c5d2SYehuda Sadeh *proto = ac->protocol; 529074f1869fSAlex Elder 5291a3530df3SAlex Elder return auth; 52923d14c5d2SYehuda Sadeh } 52933d14c5d2SYehuda Sadeh 52943d14c5d2SYehuda Sadeh 52950dde5848SIlya Dryomov static int verify_authorizer_reply(struct ceph_connection *con) 52963d14c5d2SYehuda Sadeh { 52973d14c5d2SYehuda Sadeh struct ceph_osd *o = con->private; 52983d14c5d2SYehuda Sadeh struct ceph_osd_client *osdc = o->o_osdc; 52993d14c5d2SYehuda Sadeh struct ceph_auth_client *ac = osdc->client->monc.auth; 53003d14c5d2SYehuda Sadeh 53010dde5848SIlya Dryomov return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer); 53023d14c5d2SYehuda Sadeh } 53033d14c5d2SYehuda Sadeh 53043d14c5d2SYehuda Sadeh static int invalidate_authorizer(struct ceph_connection *con) 53053d14c5d2SYehuda Sadeh { 53063d14c5d2SYehuda Sadeh struct ceph_osd *o = con->private; 53073d14c5d2SYehuda Sadeh struct ceph_osd_client *osdc = o->o_osdc; 53083d14c5d2SYehuda Sadeh struct ceph_auth_client *ac = osdc->client->monc.auth; 53093d14c5d2SYehuda Sadeh 531027859f97SSage Weil ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD); 53113d14c5d2SYehuda Sadeh return ceph_monc_validate_auth(&osdc->client->monc); 53123d14c5d2SYehuda Sadeh } 53133d14c5d2SYehuda Sadeh 53148cb441c0SIlya Dryomov static void osd_reencode_message(struct ceph_msg *msg) 53158cb441c0SIlya Dryomov { 5316914902afSIlya Dryomov int type = le16_to_cpu(msg->hdr.type); 5317914902afSIlya Dryomov 5318914902afSIlya Dryomov if (type == CEPH_MSG_OSD_OP) 53198cb441c0SIlya Dryomov encode_request_finish(msg); 53208cb441c0SIlya Dryomov } 53218cb441c0SIlya Dryomov 532279dbd1baSIlya Dryomov static int osd_sign_message(struct ceph_msg *msg) 532333d07337SYan, Zheng { 532479dbd1baSIlya Dryomov struct ceph_osd *o = msg->con->private; 532533d07337SYan, Zheng struct ceph_auth_handshake *auth = &o->o_auth; 532679dbd1baSIlya Dryomov 532733d07337SYan, Zheng return ceph_auth_sign_message(auth, msg); 532833d07337SYan, Zheng } 532933d07337SYan, Zheng 533079dbd1baSIlya Dryomov static int osd_check_message_signature(struct ceph_msg *msg) 533133d07337SYan, Zheng { 533279dbd1baSIlya Dryomov struct ceph_osd *o = msg->con->private; 533333d07337SYan, Zheng struct ceph_auth_handshake *auth = &o->o_auth; 533479dbd1baSIlya Dryomov 533533d07337SYan, Zheng return ceph_auth_check_message_signature(auth, msg); 533633d07337SYan, Zheng } 533733d07337SYan, Zheng 53383d14c5d2SYehuda Sadeh static const struct ceph_connection_operations osd_con_ops = { 53393d14c5d2SYehuda Sadeh .get = get_osd_con, 53403d14c5d2SYehuda Sadeh .put = put_osd_con, 53413d14c5d2SYehuda Sadeh .dispatch = dispatch, 53423d14c5d2SYehuda Sadeh .get_authorizer = get_authorizer, 53433d14c5d2SYehuda Sadeh .verify_authorizer_reply = verify_authorizer_reply, 53443d14c5d2SYehuda Sadeh .invalidate_authorizer = invalidate_authorizer, 53453d14c5d2SYehuda Sadeh .alloc_msg = alloc_msg, 53468cb441c0SIlya Dryomov .reencode_message = osd_reencode_message, 534779dbd1baSIlya Dryomov .sign_message = osd_sign_message, 534879dbd1baSIlya Dryomov .check_message_signature = osd_check_message_signature, 53495aea3dcdSIlya Dryomov .fault = osd_fault, 53503d14c5d2SYehuda Sadeh }; 5351