xref: /openbmc/linux/net/ceph/osd_client.c (revision 5359a17d)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2a4ce40a9SAlex Elder 
33d14c5d2SYehuda Sadeh #include <linux/ceph/ceph_debug.h>
43d14c5d2SYehuda Sadeh 
53d14c5d2SYehuda Sadeh #include <linux/module.h>
63d14c5d2SYehuda Sadeh #include <linux/err.h>
73d14c5d2SYehuda Sadeh #include <linux/highmem.h>
83d14c5d2SYehuda Sadeh #include <linux/mm.h>
93d14c5d2SYehuda Sadeh #include <linux/pagemap.h>
103d14c5d2SYehuda Sadeh #include <linux/slab.h>
113d14c5d2SYehuda Sadeh #include <linux/uaccess.h>
123d14c5d2SYehuda Sadeh #ifdef CONFIG_BLOCK
133d14c5d2SYehuda Sadeh #include <linux/bio.h>
143d14c5d2SYehuda Sadeh #endif
153d14c5d2SYehuda Sadeh 
168cb441c0SIlya Dryomov #include <linux/ceph/ceph_features.h>
173d14c5d2SYehuda Sadeh #include <linux/ceph/libceph.h>
183d14c5d2SYehuda Sadeh #include <linux/ceph/osd_client.h>
193d14c5d2SYehuda Sadeh #include <linux/ceph/messenger.h>
203d14c5d2SYehuda Sadeh #include <linux/ceph/decode.h>
213d14c5d2SYehuda Sadeh #include <linux/ceph/auth.h>
223d14c5d2SYehuda Sadeh #include <linux/ceph/pagelist.h>
233d14c5d2SYehuda Sadeh 
243d14c5d2SYehuda Sadeh #define OSD_OPREPLY_FRONT_LEN	512
253d14c5d2SYehuda Sadeh 
265522ae0bSAlex Elder static struct kmem_cache	*ceph_osd_request_cache;
275522ae0bSAlex Elder 
283d14c5d2SYehuda Sadeh static const struct ceph_connection_operations osd_con_ops;
293d14c5d2SYehuda Sadeh 
303d14c5d2SYehuda Sadeh /*
313d14c5d2SYehuda Sadeh  * Implement client access to distributed object storage cluster.
323d14c5d2SYehuda Sadeh  *
333d14c5d2SYehuda Sadeh  * All data objects are stored within a cluster/cloud of OSDs, or
343d14c5d2SYehuda Sadeh  * "object storage devices."  (Note that Ceph OSDs have _nothing_ to
353d14c5d2SYehuda Sadeh  * do with the T10 OSD extensions to SCSI.)  Ceph OSDs are simply
363d14c5d2SYehuda Sadeh  * remote daemons serving up and coordinating consistent and safe
373d14c5d2SYehuda Sadeh  * access to storage.
383d14c5d2SYehuda Sadeh  *
393d14c5d2SYehuda Sadeh  * Cluster membership and the mapping of data objects onto storage devices
403d14c5d2SYehuda Sadeh  * are described by the osd map.
413d14c5d2SYehuda Sadeh  *
423d14c5d2SYehuda Sadeh  * We keep track of pending OSD requests (read, write), resubmit
433d14c5d2SYehuda Sadeh  * requests to different OSDs when the cluster topology/data layout
443d14c5d2SYehuda Sadeh  * change, or retry the affected requests when the communications
453d14c5d2SYehuda Sadeh  * channel with an OSD is reset.
463d14c5d2SYehuda Sadeh  */
473d14c5d2SYehuda Sadeh 
485aea3dcdSIlya Dryomov static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req);
495aea3dcdSIlya Dryomov static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req);
50922dab61SIlya Dryomov static void link_linger(struct ceph_osd *osd,
51922dab61SIlya Dryomov 			struct ceph_osd_linger_request *lreq);
52922dab61SIlya Dryomov static void unlink_linger(struct ceph_osd *osd,
53922dab61SIlya Dryomov 			  struct ceph_osd_linger_request *lreq);
54a02a946dSIlya Dryomov static void clear_backoffs(struct ceph_osd *osd);
555aea3dcdSIlya Dryomov 
565aea3dcdSIlya Dryomov #if 1
575aea3dcdSIlya Dryomov static inline bool rwsem_is_wrlocked(struct rw_semaphore *sem)
585aea3dcdSIlya Dryomov {
595aea3dcdSIlya Dryomov 	bool wrlocked = true;
605aea3dcdSIlya Dryomov 
615aea3dcdSIlya Dryomov 	if (unlikely(down_read_trylock(sem))) {
625aea3dcdSIlya Dryomov 		wrlocked = false;
635aea3dcdSIlya Dryomov 		up_read(sem);
645aea3dcdSIlya Dryomov 	}
655aea3dcdSIlya Dryomov 
665aea3dcdSIlya Dryomov 	return wrlocked;
675aea3dcdSIlya Dryomov }
685aea3dcdSIlya Dryomov static inline void verify_osdc_locked(struct ceph_osd_client *osdc)
695aea3dcdSIlya Dryomov {
705aea3dcdSIlya Dryomov 	WARN_ON(!rwsem_is_locked(&osdc->lock));
715aea3dcdSIlya Dryomov }
725aea3dcdSIlya Dryomov static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc)
735aea3dcdSIlya Dryomov {
745aea3dcdSIlya Dryomov 	WARN_ON(!rwsem_is_wrlocked(&osdc->lock));
755aea3dcdSIlya Dryomov }
765aea3dcdSIlya Dryomov static inline void verify_osd_locked(struct ceph_osd *osd)
775aea3dcdSIlya Dryomov {
785aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
795aea3dcdSIlya Dryomov 
805aea3dcdSIlya Dryomov 	WARN_ON(!(mutex_is_locked(&osd->lock) &&
815aea3dcdSIlya Dryomov 		  rwsem_is_locked(&osdc->lock)) &&
825aea3dcdSIlya Dryomov 		!rwsem_is_wrlocked(&osdc->lock));
835aea3dcdSIlya Dryomov }
84922dab61SIlya Dryomov static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq)
85922dab61SIlya Dryomov {
86922dab61SIlya Dryomov 	WARN_ON(!mutex_is_locked(&lreq->lock));
87922dab61SIlya Dryomov }
885aea3dcdSIlya Dryomov #else
895aea3dcdSIlya Dryomov static inline void verify_osdc_locked(struct ceph_osd_client *osdc) { }
905aea3dcdSIlya Dryomov static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc) { }
915aea3dcdSIlya Dryomov static inline void verify_osd_locked(struct ceph_osd *osd) { }
92922dab61SIlya Dryomov static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq) { }
935aea3dcdSIlya Dryomov #endif
945aea3dcdSIlya Dryomov 
953d14c5d2SYehuda Sadeh /*
963d14c5d2SYehuda Sadeh  * calculate the mapping of a file extent onto an object, and fill out the
973d14c5d2SYehuda Sadeh  * request accordingly.  shorten extent as necessary if it crosses an
983d14c5d2SYehuda Sadeh  * object boundary.
993d14c5d2SYehuda Sadeh  *
1003d14c5d2SYehuda Sadeh  * fill osd op in request message.
1013d14c5d2SYehuda Sadeh  */
102dbe0fc41SAlex Elder static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
103a19dadfbSAlex Elder 			u64 *objnum, u64 *objoff, u64 *objlen)
1043d14c5d2SYehuda Sadeh {
10560e56f13SAlex Elder 	u64 orig_len = *plen;
106dccbf080SIlya Dryomov 	u32 xlen;
1073d14c5d2SYehuda Sadeh 
10860e56f13SAlex Elder 	/* object extent? */
109dccbf080SIlya Dryomov 	ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
110dccbf080SIlya Dryomov 					  objoff, &xlen);
111dccbf080SIlya Dryomov 	*objlen = xlen;
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);
1193ff5f385SAlex Elder 	return 0;
1203d14c5d2SYehuda Sadeh }
1213d14c5d2SYehuda Sadeh 
122c54d47bfSAlex Elder static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
123c54d47bfSAlex Elder {
124c54d47bfSAlex Elder 	memset(osd_data, 0, sizeof (*osd_data));
125c54d47bfSAlex Elder 	osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
126c54d47bfSAlex Elder }
127c54d47bfSAlex Elder 
128a4ce40a9SAlex Elder static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
12943bfe5deSAlex Elder 			struct page **pages, u64 length, u32 alignment,
13043bfe5deSAlex Elder 			bool pages_from_pool, bool own_pages)
13143bfe5deSAlex Elder {
13243bfe5deSAlex Elder 	osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
13343bfe5deSAlex Elder 	osd_data->pages = pages;
13443bfe5deSAlex Elder 	osd_data->length = length;
13543bfe5deSAlex Elder 	osd_data->alignment = alignment;
13643bfe5deSAlex Elder 	osd_data->pages_from_pool = pages_from_pool;
13743bfe5deSAlex Elder 	osd_data->own_pages = own_pages;
13843bfe5deSAlex Elder }
13943bfe5deSAlex Elder 
140a4ce40a9SAlex Elder static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
14143bfe5deSAlex Elder 			struct ceph_pagelist *pagelist)
14243bfe5deSAlex Elder {
14343bfe5deSAlex Elder 	osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
14443bfe5deSAlex Elder 	osd_data->pagelist = pagelist;
14543bfe5deSAlex Elder }
14643bfe5deSAlex Elder 
14743bfe5deSAlex Elder #ifdef CONFIG_BLOCK
148a4ce40a9SAlex Elder static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
1495359a17dSIlya Dryomov 				   struct ceph_bio_iter *bio_pos,
1505359a17dSIlya Dryomov 				   u32 bio_length)
15143bfe5deSAlex Elder {
15243bfe5deSAlex Elder 	osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
1535359a17dSIlya Dryomov 	osd_data->bio_pos = *bio_pos;
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,
2205359a17dSIlya Dryomov 				    unsigned int which,
2215359a17dSIlya Dryomov 				    struct ceph_bio_iter *bio_pos,
2225359a17dSIlya Dryomov 				    u32 bio_length)
223a4ce40a9SAlex Elder {
224a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
225863c7eb5SAlex Elder 
226863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
2275359a17dSIlya Dryomov 	ceph_osd_data_bio_init(osd_data, bio_pos, bio_length);
228a4ce40a9SAlex Elder }
229a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
230a4ce40a9SAlex Elder #endif /* CONFIG_BLOCK */
231a4ce40a9SAlex Elder 
232a4ce40a9SAlex Elder static void osd_req_op_cls_request_info_pagelist(
233a4ce40a9SAlex Elder 			struct ceph_osd_request *osd_req,
234a4ce40a9SAlex Elder 			unsigned int which, struct ceph_pagelist *pagelist)
235a4ce40a9SAlex Elder {
236a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
237a4ce40a9SAlex Elder 
238863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, request_info);
239a4ce40a9SAlex Elder 	ceph_osd_data_pagelist_init(osd_data, pagelist);
240a4ce40a9SAlex Elder }
241a4ce40a9SAlex Elder 
24204017e29SAlex Elder void osd_req_op_cls_request_data_pagelist(
24304017e29SAlex Elder 			struct ceph_osd_request *osd_req,
24404017e29SAlex Elder 			unsigned int which, struct ceph_pagelist *pagelist)
24504017e29SAlex Elder {
24604017e29SAlex Elder 	struct ceph_osd_data *osd_data;
24704017e29SAlex Elder 
248863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, request_data);
24904017e29SAlex Elder 	ceph_osd_data_pagelist_init(osd_data, pagelist);
250bb873b53SIlya Dryomov 	osd_req->r_ops[which].cls.indata_len += pagelist->length;
251bb873b53SIlya Dryomov 	osd_req->r_ops[which].indata_len += pagelist->length;
25204017e29SAlex Elder }
25304017e29SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
25404017e29SAlex Elder 
2556c57b554SAlex Elder void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
2566c57b554SAlex Elder 			unsigned int which, struct page **pages, u64 length,
2576c57b554SAlex Elder 			u32 alignment, bool pages_from_pool, bool own_pages)
2586c57b554SAlex Elder {
2596c57b554SAlex Elder 	struct ceph_osd_data *osd_data;
2606c57b554SAlex Elder 
2616c57b554SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, request_data);
2626c57b554SAlex Elder 	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
2636c57b554SAlex Elder 				pages_from_pool, own_pages);
264bb873b53SIlya Dryomov 	osd_req->r_ops[which].cls.indata_len += length;
265bb873b53SIlya Dryomov 	osd_req->r_ops[which].indata_len += length;
2666c57b554SAlex Elder }
2676c57b554SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
2686c57b554SAlex Elder 
269a4ce40a9SAlex Elder void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
270a4ce40a9SAlex Elder 			unsigned int which, struct page **pages, u64 length,
271a4ce40a9SAlex Elder 			u32 alignment, bool pages_from_pool, bool own_pages)
272a4ce40a9SAlex Elder {
273a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
274a4ce40a9SAlex Elder 
275863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, response_data);
276a4ce40a9SAlex Elder 	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
277a4ce40a9SAlex Elder 				pages_from_pool, own_pages);
278a4ce40a9SAlex Elder }
279a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
280a4ce40a9SAlex Elder 
28123c08a9cSAlex Elder static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
28223c08a9cSAlex Elder {
28323c08a9cSAlex Elder 	switch (osd_data->type) {
28423c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_NONE:
28523c08a9cSAlex Elder 		return 0;
28623c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_PAGES:
28723c08a9cSAlex Elder 		return osd_data->length;
28823c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_PAGELIST:
28923c08a9cSAlex Elder 		return (u64)osd_data->pagelist->length;
29023c08a9cSAlex Elder #ifdef CONFIG_BLOCK
29123c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_BIO:
29223c08a9cSAlex Elder 		return (u64)osd_data->bio_length;
29323c08a9cSAlex Elder #endif /* CONFIG_BLOCK */
29423c08a9cSAlex Elder 	default:
29523c08a9cSAlex Elder 		WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
29623c08a9cSAlex Elder 		return 0;
29723c08a9cSAlex Elder 	}
29823c08a9cSAlex Elder }
29923c08a9cSAlex Elder 
300c54d47bfSAlex Elder static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
301c54d47bfSAlex Elder {
3025476492fSAlex Elder 	if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
303c54d47bfSAlex Elder 		int num_pages;
304c54d47bfSAlex Elder 
305c54d47bfSAlex Elder 		num_pages = calc_pages_for((u64)osd_data->alignment,
306c54d47bfSAlex Elder 						(u64)osd_data->length);
307c54d47bfSAlex Elder 		ceph_release_page_vector(osd_data->pages, num_pages);
308c54d47bfSAlex Elder 	}
3095476492fSAlex Elder 	ceph_osd_data_init(osd_data);
3105476492fSAlex Elder }
3115476492fSAlex Elder 
3125476492fSAlex Elder static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
3135476492fSAlex Elder 			unsigned int which)
3145476492fSAlex Elder {
3155476492fSAlex Elder 	struct ceph_osd_req_op *op;
3165476492fSAlex Elder 
3175476492fSAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
3185476492fSAlex Elder 	op = &osd_req->r_ops[which];
3195476492fSAlex Elder 
3205476492fSAlex Elder 	switch (op->op) {
3215476492fSAlex Elder 	case CEPH_OSD_OP_READ:
3225476492fSAlex Elder 	case CEPH_OSD_OP_WRITE:
323e30b7577SIlya Dryomov 	case CEPH_OSD_OP_WRITEFULL:
3245476492fSAlex Elder 		ceph_osd_data_release(&op->extent.osd_data);
3255476492fSAlex Elder 		break;
3265476492fSAlex Elder 	case CEPH_OSD_OP_CALL:
3275476492fSAlex Elder 		ceph_osd_data_release(&op->cls.request_info);
32804017e29SAlex Elder 		ceph_osd_data_release(&op->cls.request_data);
3295476492fSAlex Elder 		ceph_osd_data_release(&op->cls.response_data);
3305476492fSAlex Elder 		break;
331d74b50beSYan, Zheng 	case CEPH_OSD_OP_SETXATTR:
332d74b50beSYan, Zheng 	case CEPH_OSD_OP_CMPXATTR:
333d74b50beSYan, Zheng 		ceph_osd_data_release(&op->xattr.osd_data);
334d74b50beSYan, Zheng 		break;
33566ba609fSYan, Zheng 	case CEPH_OSD_OP_STAT:
33666ba609fSYan, Zheng 		ceph_osd_data_release(&op->raw_data_in);
33766ba609fSYan, Zheng 		break;
338922dab61SIlya Dryomov 	case CEPH_OSD_OP_NOTIFY_ACK:
339922dab61SIlya Dryomov 		ceph_osd_data_release(&op->notify_ack.request_data);
340922dab61SIlya Dryomov 		break;
34119079203SIlya Dryomov 	case CEPH_OSD_OP_NOTIFY:
34219079203SIlya Dryomov 		ceph_osd_data_release(&op->notify.request_data);
34319079203SIlya Dryomov 		ceph_osd_data_release(&op->notify.response_data);
34419079203SIlya Dryomov 		break;
345a4ed38d7SDouglas Fuller 	case CEPH_OSD_OP_LIST_WATCHERS:
346a4ed38d7SDouglas Fuller 		ceph_osd_data_release(&op->list_watchers.response_data);
347a4ed38d7SDouglas Fuller 		break;
3485476492fSAlex Elder 	default:
3495476492fSAlex Elder 		break;
3505476492fSAlex Elder 	}
351c54d47bfSAlex Elder }
352c54d47bfSAlex Elder 
3533d14c5d2SYehuda Sadeh /*
35463244fa1SIlya Dryomov  * Assumes @t is zero-initialized.
35563244fa1SIlya Dryomov  */
35663244fa1SIlya Dryomov static void target_init(struct ceph_osd_request_target *t)
35763244fa1SIlya Dryomov {
35863244fa1SIlya Dryomov 	ceph_oid_init(&t->base_oid);
35963244fa1SIlya Dryomov 	ceph_oloc_init(&t->base_oloc);
36063244fa1SIlya Dryomov 	ceph_oid_init(&t->target_oid);
36163244fa1SIlya Dryomov 	ceph_oloc_init(&t->target_oloc);
36263244fa1SIlya Dryomov 
36363244fa1SIlya Dryomov 	ceph_osds_init(&t->acting);
36463244fa1SIlya Dryomov 	ceph_osds_init(&t->up);
36563244fa1SIlya Dryomov 	t->size = -1;
36663244fa1SIlya Dryomov 	t->min_size = -1;
36763244fa1SIlya Dryomov 
36863244fa1SIlya Dryomov 	t->osd = CEPH_HOMELESS_OSD;
36963244fa1SIlya Dryomov }
37063244fa1SIlya Dryomov 
371922dab61SIlya Dryomov static void target_copy(struct ceph_osd_request_target *dest,
372922dab61SIlya Dryomov 			const struct ceph_osd_request_target *src)
373922dab61SIlya Dryomov {
374922dab61SIlya Dryomov 	ceph_oid_copy(&dest->base_oid, &src->base_oid);
375922dab61SIlya Dryomov 	ceph_oloc_copy(&dest->base_oloc, &src->base_oloc);
376922dab61SIlya Dryomov 	ceph_oid_copy(&dest->target_oid, &src->target_oid);
377922dab61SIlya Dryomov 	ceph_oloc_copy(&dest->target_oloc, &src->target_oloc);
378922dab61SIlya Dryomov 
379922dab61SIlya Dryomov 	dest->pgid = src->pgid; /* struct */
380dc98ff72SIlya Dryomov 	dest->spgid = src->spgid; /* struct */
381922dab61SIlya Dryomov 	dest->pg_num = src->pg_num;
382922dab61SIlya Dryomov 	dest->pg_num_mask = src->pg_num_mask;
383922dab61SIlya Dryomov 	ceph_osds_copy(&dest->acting, &src->acting);
384922dab61SIlya Dryomov 	ceph_osds_copy(&dest->up, &src->up);
385922dab61SIlya Dryomov 	dest->size = src->size;
386922dab61SIlya Dryomov 	dest->min_size = src->min_size;
387922dab61SIlya Dryomov 	dest->sort_bitwise = src->sort_bitwise;
388922dab61SIlya Dryomov 
389922dab61SIlya Dryomov 	dest->flags = src->flags;
390922dab61SIlya Dryomov 	dest->paused = src->paused;
391922dab61SIlya Dryomov 
39204c7d789SIlya Dryomov 	dest->epoch = src->epoch;
393dc93e0e2SIlya Dryomov 	dest->last_force_resend = src->last_force_resend;
394dc93e0e2SIlya Dryomov 
395922dab61SIlya Dryomov 	dest->osd = src->osd;
396922dab61SIlya Dryomov }
397922dab61SIlya Dryomov 
39863244fa1SIlya Dryomov static void target_destroy(struct ceph_osd_request_target *t)
39963244fa1SIlya Dryomov {
40063244fa1SIlya Dryomov 	ceph_oid_destroy(&t->base_oid);
40130c156d9SYan, Zheng 	ceph_oloc_destroy(&t->base_oloc);
40263244fa1SIlya Dryomov 	ceph_oid_destroy(&t->target_oid);
40330c156d9SYan, Zheng 	ceph_oloc_destroy(&t->target_oloc);
40463244fa1SIlya Dryomov }
40563244fa1SIlya Dryomov 
40663244fa1SIlya Dryomov /*
4073d14c5d2SYehuda Sadeh  * requests
4083d14c5d2SYehuda Sadeh  */
4093540bfdbSIlya Dryomov static void request_release_checks(struct ceph_osd_request *req)
4103540bfdbSIlya Dryomov {
4113540bfdbSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&req->r_node));
4124609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&req->r_mc_node));
4133540bfdbSIlya Dryomov 	WARN_ON(!list_empty(&req->r_unsafe_item));
4143540bfdbSIlya Dryomov 	WARN_ON(req->r_osd);
4153540bfdbSIlya Dryomov }
4163540bfdbSIlya Dryomov 
4179e94af20SIlya Dryomov static void ceph_osdc_release_request(struct kref *kref)
4183d14c5d2SYehuda Sadeh {
4199e94af20SIlya Dryomov 	struct ceph_osd_request *req = container_of(kref,
4209e94af20SIlya Dryomov 					    struct ceph_osd_request, r_kref);
4215476492fSAlex Elder 	unsigned int which;
4223d14c5d2SYehuda Sadeh 
4239e94af20SIlya Dryomov 	dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
4249e94af20SIlya Dryomov 	     req->r_request, req->r_reply);
4253540bfdbSIlya Dryomov 	request_release_checks(req);
4269e94af20SIlya Dryomov 
4273d14c5d2SYehuda Sadeh 	if (req->r_request)
4283d14c5d2SYehuda Sadeh 		ceph_msg_put(req->r_request);
4295aea3dcdSIlya Dryomov 	if (req->r_reply)
430ab8cb34aSAlex Elder 		ceph_msg_put(req->r_reply);
4310fff87ecSAlex Elder 
4325476492fSAlex Elder 	for (which = 0; which < req->r_num_ops; which++)
4335476492fSAlex Elder 		osd_req_op_data_release(req, which);
4340fff87ecSAlex Elder 
435a66dd383SIlya Dryomov 	target_destroy(&req->r_t);
4363d14c5d2SYehuda Sadeh 	ceph_put_snap_context(req->r_snapc);
437d30291b9SIlya Dryomov 
4383d14c5d2SYehuda Sadeh 	if (req->r_mempool)
4393d14c5d2SYehuda Sadeh 		mempool_free(req, req->r_osdc->req_mempool);
4403f1af42aSIlya Dryomov 	else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS)
4415522ae0bSAlex Elder 		kmem_cache_free(ceph_osd_request_cache, req);
4423f1af42aSIlya Dryomov 	else
4433f1af42aSIlya Dryomov 		kfree(req);
4443d14c5d2SYehuda Sadeh }
4459e94af20SIlya Dryomov 
4469e94af20SIlya Dryomov void ceph_osdc_get_request(struct ceph_osd_request *req)
4479e94af20SIlya Dryomov {
4489e94af20SIlya Dryomov 	dout("%s %p (was %d)\n", __func__, req,
4492c935bc5SPeter Zijlstra 	     kref_read(&req->r_kref));
4509e94af20SIlya Dryomov 	kref_get(&req->r_kref);
4519e94af20SIlya Dryomov }
4529e94af20SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_get_request);
4539e94af20SIlya Dryomov 
4549e94af20SIlya Dryomov void ceph_osdc_put_request(struct ceph_osd_request *req)
4559e94af20SIlya Dryomov {
4563ed97d63SIlya Dryomov 	if (req) {
4579e94af20SIlya Dryomov 		dout("%s %p (was %d)\n", __func__, req,
4582c935bc5SPeter Zijlstra 		     kref_read(&req->r_kref));
4599e94af20SIlya Dryomov 		kref_put(&req->r_kref, ceph_osdc_release_request);
4609e94af20SIlya Dryomov 	}
4613ed97d63SIlya Dryomov }
4629e94af20SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_put_request);
4633d14c5d2SYehuda Sadeh 
4643540bfdbSIlya Dryomov static void request_init(struct ceph_osd_request *req)
4653540bfdbSIlya Dryomov {
4663540bfdbSIlya Dryomov 	/* req only, each op is zeroed in _osd_req_op_init() */
4673540bfdbSIlya Dryomov 	memset(req, 0, sizeof(*req));
4683540bfdbSIlya Dryomov 
4693540bfdbSIlya Dryomov 	kref_init(&req->r_kref);
4703540bfdbSIlya Dryomov 	init_completion(&req->r_completion);
4713540bfdbSIlya Dryomov 	RB_CLEAR_NODE(&req->r_node);
4724609245eSIlya Dryomov 	RB_CLEAR_NODE(&req->r_mc_node);
4733540bfdbSIlya Dryomov 	INIT_LIST_HEAD(&req->r_unsafe_item);
4743540bfdbSIlya Dryomov 
4753540bfdbSIlya Dryomov 	target_init(&req->r_t);
4763540bfdbSIlya Dryomov }
4773540bfdbSIlya Dryomov 
478922dab61SIlya Dryomov /*
479922dab61SIlya Dryomov  * This is ugly, but it allows us to reuse linger registration and ping
480922dab61SIlya Dryomov  * requests, keeping the structure of the code around send_linger{_ping}()
481922dab61SIlya Dryomov  * reasonable.  Setting up a min_nr=2 mempool for each linger request
482922dab61SIlya Dryomov  * and dealing with copying ops (this blasts req only, watch op remains
483922dab61SIlya Dryomov  * intact) isn't any better.
484922dab61SIlya Dryomov  */
485922dab61SIlya Dryomov static void request_reinit(struct ceph_osd_request *req)
486922dab61SIlya Dryomov {
487922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
488922dab61SIlya Dryomov 	bool mempool = req->r_mempool;
489922dab61SIlya Dryomov 	unsigned int num_ops = req->r_num_ops;
490922dab61SIlya Dryomov 	u64 snapid = req->r_snapid;
491922dab61SIlya Dryomov 	struct ceph_snap_context *snapc = req->r_snapc;
492922dab61SIlya Dryomov 	bool linger = req->r_linger;
493922dab61SIlya Dryomov 	struct ceph_msg *request_msg = req->r_request;
494922dab61SIlya Dryomov 	struct ceph_msg *reply_msg = req->r_reply;
495922dab61SIlya Dryomov 
496922dab61SIlya Dryomov 	dout("%s req %p\n", __func__, req);
4972c935bc5SPeter Zijlstra 	WARN_ON(kref_read(&req->r_kref) != 1);
498922dab61SIlya Dryomov 	request_release_checks(req);
499922dab61SIlya Dryomov 
5002c935bc5SPeter Zijlstra 	WARN_ON(kref_read(&request_msg->kref) != 1);
5012c935bc5SPeter Zijlstra 	WARN_ON(kref_read(&reply_msg->kref) != 1);
502922dab61SIlya Dryomov 	target_destroy(&req->r_t);
503922dab61SIlya Dryomov 
504922dab61SIlya Dryomov 	request_init(req);
505922dab61SIlya Dryomov 	req->r_osdc = osdc;
506922dab61SIlya Dryomov 	req->r_mempool = mempool;
507922dab61SIlya Dryomov 	req->r_num_ops = num_ops;
508922dab61SIlya Dryomov 	req->r_snapid = snapid;
509922dab61SIlya Dryomov 	req->r_snapc = snapc;
510922dab61SIlya Dryomov 	req->r_linger = linger;
511922dab61SIlya Dryomov 	req->r_request = request_msg;
512922dab61SIlya Dryomov 	req->r_reply = reply_msg;
513922dab61SIlya Dryomov }
514922dab61SIlya Dryomov 
5153d14c5d2SYehuda Sadeh struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
5163d14c5d2SYehuda Sadeh 					       struct ceph_snap_context *snapc,
5171b83bef2SSage Weil 					       unsigned int num_ops,
5183d14c5d2SYehuda Sadeh 					       bool use_mempool,
51954a54007SAlex Elder 					       gfp_t gfp_flags)
5203d14c5d2SYehuda Sadeh {
5213d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
5223d14c5d2SYehuda Sadeh 
5233d14c5d2SYehuda Sadeh 	if (use_mempool) {
5243f1af42aSIlya Dryomov 		BUG_ON(num_ops > CEPH_OSD_SLAB_OPS);
5253d14c5d2SYehuda Sadeh 		req = mempool_alloc(osdc->req_mempool, gfp_flags);
5263f1af42aSIlya Dryomov 	} else if (num_ops <= CEPH_OSD_SLAB_OPS) {
5273f1af42aSIlya Dryomov 		req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
5283d14c5d2SYehuda Sadeh 	} else {
5293f1af42aSIlya Dryomov 		BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
5303f1af42aSIlya Dryomov 		req = kmalloc(sizeof(*req) + num_ops * sizeof(req->r_ops[0]),
5313f1af42aSIlya Dryomov 			      gfp_flags);
5323d14c5d2SYehuda Sadeh 	}
5333f1af42aSIlya Dryomov 	if (unlikely(!req))
5343d14c5d2SYehuda Sadeh 		return NULL;
5353d14c5d2SYehuda Sadeh 
5363540bfdbSIlya Dryomov 	request_init(req);
5373d14c5d2SYehuda Sadeh 	req->r_osdc = osdc;
5383d14c5d2SYehuda Sadeh 	req->r_mempool = use_mempool;
53979528734SAlex Elder 	req->r_num_ops = num_ops;
54084127282SIlya Dryomov 	req->r_snapid = CEPH_NOSNAP;
54184127282SIlya Dryomov 	req->r_snapc = ceph_get_snap_context(snapc);
5423d14c5d2SYehuda Sadeh 
54313d1ad16SIlya Dryomov 	dout("%s req %p\n", __func__, req);
54413d1ad16SIlya Dryomov 	return req;
5453f1af42aSIlya Dryomov }
54613d1ad16SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_alloc_request);
5473f1af42aSIlya Dryomov 
5482e59ffd1SIlya Dryomov static int ceph_oloc_encoding_size(const struct ceph_object_locator *oloc)
54930c156d9SYan, Zheng {
55030c156d9SYan, Zheng 	return 8 + 4 + 4 + 4 + (oloc->pool_ns ? oloc->pool_ns->len : 0);
55130c156d9SYan, Zheng }
55230c156d9SYan, Zheng 
55313d1ad16SIlya Dryomov int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp)
55413d1ad16SIlya Dryomov {
55513d1ad16SIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
55613d1ad16SIlya Dryomov 	struct ceph_msg *msg;
55713d1ad16SIlya Dryomov 	int msg_size;
5583d14c5d2SYehuda Sadeh 
559d30291b9SIlya Dryomov 	WARN_ON(ceph_oid_empty(&req->r_base_oid));
56030c156d9SYan, Zheng 	WARN_ON(ceph_oloc_empty(&req->r_base_oloc));
561d30291b9SIlya Dryomov 
56213d1ad16SIlya Dryomov 	/* create request message */
5638cb441c0SIlya Dryomov 	msg_size = CEPH_ENCODING_START_BLK_LEN +
5648cb441c0SIlya Dryomov 			CEPH_PGID_ENCODING_LEN + 1; /* spgid */
5658cb441c0SIlya Dryomov 	msg_size += 4 + 4 + 4; /* hash, osdmap_epoch, flags */
5668cb441c0SIlya Dryomov 	msg_size += CEPH_ENCODING_START_BLK_LEN +
5678cb441c0SIlya Dryomov 			sizeof(struct ceph_osd_reqid); /* reqid */
5688cb441c0SIlya Dryomov 	msg_size += sizeof(struct ceph_blkin_trace_info); /* trace */
5698cb441c0SIlya Dryomov 	msg_size += 4 + sizeof(struct ceph_timespec); /* client_inc, mtime */
57030c156d9SYan, Zheng 	msg_size += CEPH_ENCODING_START_BLK_LEN +
57130c156d9SYan, Zheng 			ceph_oloc_encoding_size(&req->r_base_oloc); /* oloc */
57213d1ad16SIlya Dryomov 	msg_size += 4 + req->r_base_oid.name_len; /* oid */
57313d1ad16SIlya Dryomov 	msg_size += 2 + req->r_num_ops * sizeof(struct ceph_osd_op);
574ae458f5aSIlya Dryomov 	msg_size += 8; /* snapid */
575ae458f5aSIlya Dryomov 	msg_size += 8; /* snap_seq */
57613d1ad16SIlya Dryomov 	msg_size += 4 + 8 * (req->r_snapc ? req->r_snapc->num_snaps : 0);
5778cb441c0SIlya Dryomov 	msg_size += 4 + 8; /* retry_attempt, features */
578ae458f5aSIlya Dryomov 
57913d1ad16SIlya Dryomov 	if (req->r_mempool)
5803d14c5d2SYehuda Sadeh 		msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
5813d14c5d2SYehuda Sadeh 	else
58213d1ad16SIlya Dryomov 		msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp, true);
58313d1ad16SIlya Dryomov 	if (!msg)
58413d1ad16SIlya Dryomov 		return -ENOMEM;
5853d14c5d2SYehuda Sadeh 
5863d14c5d2SYehuda Sadeh 	memset(msg->front.iov_base, 0, msg->front.iov_len);
5873d14c5d2SYehuda Sadeh 	req->r_request = msg;
5883d14c5d2SYehuda Sadeh 
58913d1ad16SIlya Dryomov 	/* create reply message */
59013d1ad16SIlya Dryomov 	msg_size = OSD_OPREPLY_FRONT_LEN;
591711da55dSIlya Dryomov 	msg_size += req->r_base_oid.name_len;
592711da55dSIlya Dryomov 	msg_size += req->r_num_ops * sizeof(struct ceph_osd_op);
59313d1ad16SIlya Dryomov 
59413d1ad16SIlya Dryomov 	if (req->r_mempool)
59513d1ad16SIlya Dryomov 		msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
59613d1ad16SIlya Dryomov 	else
59713d1ad16SIlya Dryomov 		msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, msg_size, gfp, true);
59813d1ad16SIlya Dryomov 	if (!msg)
59913d1ad16SIlya Dryomov 		return -ENOMEM;
60013d1ad16SIlya Dryomov 
60113d1ad16SIlya Dryomov 	req->r_reply = msg;
60213d1ad16SIlya Dryomov 
60313d1ad16SIlya Dryomov 	return 0;
60413d1ad16SIlya Dryomov }
60513d1ad16SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_alloc_messages);
6063d14c5d2SYehuda Sadeh 
607a8dd0a37SAlex Elder static bool osd_req_opcode_valid(u16 opcode)
608a8dd0a37SAlex Elder {
609a8dd0a37SAlex Elder 	switch (opcode) {
61070b5bfa3SIlya Dryomov #define GENERATE_CASE(op, opcode, str)	case CEPH_OSD_OP_##op: return true;
61170b5bfa3SIlya Dryomov __CEPH_FORALL_OSD_OPS(GENERATE_CASE)
61270b5bfa3SIlya Dryomov #undef GENERATE_CASE
613a8dd0a37SAlex Elder 	default:
614a8dd0a37SAlex Elder 		return false;
615a8dd0a37SAlex Elder 	}
616a8dd0a37SAlex Elder }
617a8dd0a37SAlex Elder 
61833803f33SAlex Elder /*
61933803f33SAlex Elder  * This is an osd op init function for opcodes that have no data or
62033803f33SAlex Elder  * other information associated with them.  It also serves as a
62133803f33SAlex Elder  * common init routine for all the other init functions, below.
62233803f33SAlex Elder  */
623c99d2d4aSAlex Elder static struct ceph_osd_req_op *
62449719778SAlex Elder _osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
625144cba14SYan, Zheng 		 u16 opcode, u32 flags)
62633803f33SAlex Elder {
627c99d2d4aSAlex Elder 	struct ceph_osd_req_op *op;
628c99d2d4aSAlex Elder 
629c99d2d4aSAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
63033803f33SAlex Elder 	BUG_ON(!osd_req_opcode_valid(opcode));
63133803f33SAlex Elder 
632c99d2d4aSAlex Elder 	op = &osd_req->r_ops[which];
63333803f33SAlex Elder 	memset(op, 0, sizeof (*op));
63433803f33SAlex Elder 	op->op = opcode;
635144cba14SYan, Zheng 	op->flags = flags;
636c99d2d4aSAlex Elder 
637c99d2d4aSAlex Elder 	return op;
63833803f33SAlex Elder }
63933803f33SAlex Elder 
64049719778SAlex Elder void osd_req_op_init(struct ceph_osd_request *osd_req,
641144cba14SYan, Zheng 		     unsigned int which, u16 opcode, u32 flags)
64249719778SAlex Elder {
643144cba14SYan, Zheng 	(void)_osd_req_op_init(osd_req, which, opcode, flags);
64449719778SAlex Elder }
64549719778SAlex Elder EXPORT_SYMBOL(osd_req_op_init);
64649719778SAlex Elder 
647c99d2d4aSAlex Elder void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
648c99d2d4aSAlex Elder 				unsigned int which, u16 opcode,
64933803f33SAlex Elder 				u64 offset, u64 length,
65033803f33SAlex Elder 				u64 truncate_size, u32 truncate_seq)
65133803f33SAlex Elder {
652144cba14SYan, Zheng 	struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
653144cba14SYan, Zheng 						      opcode, 0);
65433803f33SAlex Elder 	size_t payload_len = 0;
65533803f33SAlex Elder 
656ad7a60deSLi Wang 	BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
657e30b7577SIlya Dryomov 	       opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
658e30b7577SIlya Dryomov 	       opcode != CEPH_OSD_OP_TRUNCATE);
65933803f33SAlex Elder 
66033803f33SAlex Elder 	op->extent.offset = offset;
66133803f33SAlex Elder 	op->extent.length = length;
66233803f33SAlex Elder 	op->extent.truncate_size = truncate_size;
66333803f33SAlex Elder 	op->extent.truncate_seq = truncate_seq;
664e30b7577SIlya Dryomov 	if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
66533803f33SAlex Elder 		payload_len += length;
66633803f33SAlex Elder 
667de2aa102SIlya Dryomov 	op->indata_len = payload_len;
66833803f33SAlex Elder }
66933803f33SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_init);
67033803f33SAlex Elder 
671c99d2d4aSAlex Elder void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
672c99d2d4aSAlex Elder 				unsigned int which, u64 length)
673e5975c7cSAlex Elder {
674c99d2d4aSAlex Elder 	struct ceph_osd_req_op *op;
675c99d2d4aSAlex Elder 	u64 previous;
676c99d2d4aSAlex Elder 
677c99d2d4aSAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
678c99d2d4aSAlex Elder 	op = &osd_req->r_ops[which];
679c99d2d4aSAlex Elder 	previous = op->extent.length;
680e5975c7cSAlex Elder 
681e5975c7cSAlex Elder 	if (length == previous)
682e5975c7cSAlex Elder 		return;		/* Nothing to do */
683e5975c7cSAlex Elder 	BUG_ON(length > previous);
684e5975c7cSAlex Elder 
685e5975c7cSAlex Elder 	op->extent.length = length;
686d641df81SYan, Zheng 	if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
687de2aa102SIlya Dryomov 		op->indata_len -= previous - length;
688e5975c7cSAlex Elder }
689e5975c7cSAlex Elder EXPORT_SYMBOL(osd_req_op_extent_update);
690e5975c7cSAlex Elder 
6912c63f49aSYan, Zheng void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
6922c63f49aSYan, Zheng 				unsigned int which, u64 offset_inc)
6932c63f49aSYan, Zheng {
6942c63f49aSYan, Zheng 	struct ceph_osd_req_op *op, *prev_op;
6952c63f49aSYan, Zheng 
6962c63f49aSYan, Zheng 	BUG_ON(which + 1 >= osd_req->r_num_ops);
6972c63f49aSYan, Zheng 
6982c63f49aSYan, Zheng 	prev_op = &osd_req->r_ops[which];
6992c63f49aSYan, Zheng 	op = _osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags);
7002c63f49aSYan, Zheng 	/* dup previous one */
7012c63f49aSYan, Zheng 	op->indata_len = prev_op->indata_len;
7022c63f49aSYan, Zheng 	op->outdata_len = prev_op->outdata_len;
7032c63f49aSYan, Zheng 	op->extent = prev_op->extent;
7042c63f49aSYan, Zheng 	/* adjust offset */
7052c63f49aSYan, Zheng 	op->extent.offset += offset_inc;
7062c63f49aSYan, Zheng 	op->extent.length -= offset_inc;
7072c63f49aSYan, Zheng 
7082c63f49aSYan, Zheng 	if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
7092c63f49aSYan, Zheng 		op->indata_len -= offset_inc;
7102c63f49aSYan, Zheng }
7112c63f49aSYan, Zheng EXPORT_SYMBOL(osd_req_op_extent_dup_last);
7122c63f49aSYan, Zheng 
713c99d2d4aSAlex Elder void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
71404017e29SAlex Elder 			u16 opcode, const char *class, const char *method)
71533803f33SAlex Elder {
716144cba14SYan, Zheng 	struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
717144cba14SYan, Zheng 						      opcode, 0);
7185f562df5SAlex Elder 	struct ceph_pagelist *pagelist;
71933803f33SAlex Elder 	size_t payload_len = 0;
72033803f33SAlex Elder 	size_t size;
72133803f33SAlex Elder 
72233803f33SAlex Elder 	BUG_ON(opcode != CEPH_OSD_OP_CALL);
72333803f33SAlex Elder 
7245f562df5SAlex Elder 	pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
7255f562df5SAlex Elder 	BUG_ON(!pagelist);
7265f562df5SAlex Elder 	ceph_pagelist_init(pagelist);
7275f562df5SAlex Elder 
72833803f33SAlex Elder 	op->cls.class_name = class;
72933803f33SAlex Elder 	size = strlen(class);
73033803f33SAlex Elder 	BUG_ON(size > (size_t) U8_MAX);
73133803f33SAlex Elder 	op->cls.class_len = size;
7325f562df5SAlex Elder 	ceph_pagelist_append(pagelist, class, size);
73333803f33SAlex Elder 	payload_len += size;
73433803f33SAlex Elder 
73533803f33SAlex Elder 	op->cls.method_name = method;
73633803f33SAlex Elder 	size = strlen(method);
73733803f33SAlex Elder 	BUG_ON(size > (size_t) U8_MAX);
73833803f33SAlex Elder 	op->cls.method_len = size;
7395f562df5SAlex Elder 	ceph_pagelist_append(pagelist, method, size);
74033803f33SAlex Elder 	payload_len += size;
74133803f33SAlex Elder 
742a4ce40a9SAlex Elder 	osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
7435f562df5SAlex Elder 
744de2aa102SIlya Dryomov 	op->indata_len = payload_len;
74533803f33SAlex Elder }
74633803f33SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_init);
7478c042b0dSAlex Elder 
748d74b50beSYan, Zheng int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
749d74b50beSYan, Zheng 			  u16 opcode, const char *name, const void *value,
750d74b50beSYan, Zheng 			  size_t size, u8 cmp_op, u8 cmp_mode)
751d74b50beSYan, Zheng {
752144cba14SYan, Zheng 	struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
753144cba14SYan, Zheng 						      opcode, 0);
754d74b50beSYan, Zheng 	struct ceph_pagelist *pagelist;
755d74b50beSYan, Zheng 	size_t payload_len;
756d74b50beSYan, Zheng 
757d74b50beSYan, Zheng 	BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
758d74b50beSYan, Zheng 
759d74b50beSYan, Zheng 	pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
760d74b50beSYan, Zheng 	if (!pagelist)
761d74b50beSYan, Zheng 		return -ENOMEM;
762d74b50beSYan, Zheng 
763d74b50beSYan, Zheng 	ceph_pagelist_init(pagelist);
764d74b50beSYan, Zheng 
765d74b50beSYan, Zheng 	payload_len = strlen(name);
766d74b50beSYan, Zheng 	op->xattr.name_len = payload_len;
767d74b50beSYan, Zheng 	ceph_pagelist_append(pagelist, name, payload_len);
768d74b50beSYan, Zheng 
769d74b50beSYan, Zheng 	op->xattr.value_len = size;
770d74b50beSYan, Zheng 	ceph_pagelist_append(pagelist, value, size);
771d74b50beSYan, Zheng 	payload_len += size;
772d74b50beSYan, Zheng 
773d74b50beSYan, Zheng 	op->xattr.cmp_op = cmp_op;
774d74b50beSYan, Zheng 	op->xattr.cmp_mode = cmp_mode;
775d74b50beSYan, Zheng 
776d74b50beSYan, Zheng 	ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
777de2aa102SIlya Dryomov 	op->indata_len = payload_len;
778d74b50beSYan, Zheng 	return 0;
779d74b50beSYan, Zheng }
780d74b50beSYan, Zheng EXPORT_SYMBOL(osd_req_op_xattr_init);
781d74b50beSYan, Zheng 
782922dab61SIlya Dryomov /*
783922dab61SIlya Dryomov  * @watch_opcode: CEPH_OSD_WATCH_OP_*
784922dab61SIlya Dryomov  */
785922dab61SIlya Dryomov static void osd_req_op_watch_init(struct ceph_osd_request *req, int which,
786922dab61SIlya Dryomov 				  u64 cookie, u8 watch_opcode)
78733803f33SAlex Elder {
788922dab61SIlya Dryomov 	struct ceph_osd_req_op *op;
78933803f33SAlex Elder 
790922dab61SIlya Dryomov 	op = _osd_req_op_init(req, which, CEPH_OSD_OP_WATCH, 0);
79133803f33SAlex Elder 	op->watch.cookie = cookie;
792922dab61SIlya Dryomov 	op->watch.op = watch_opcode;
793922dab61SIlya Dryomov 	op->watch.gen = 0;
79433803f33SAlex Elder }
79533803f33SAlex Elder 
796c647b8a8SIlya Dryomov void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
797c647b8a8SIlya Dryomov 				unsigned int which,
798c647b8a8SIlya Dryomov 				u64 expected_object_size,
799c647b8a8SIlya Dryomov 				u64 expected_write_size)
800c647b8a8SIlya Dryomov {
801c647b8a8SIlya Dryomov 	struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
802144cba14SYan, Zheng 						      CEPH_OSD_OP_SETALLOCHINT,
803144cba14SYan, Zheng 						      0);
804c647b8a8SIlya Dryomov 
805c647b8a8SIlya Dryomov 	op->alloc_hint.expected_object_size = expected_object_size;
806c647b8a8SIlya Dryomov 	op->alloc_hint.expected_write_size = expected_write_size;
807c647b8a8SIlya Dryomov 
808c647b8a8SIlya Dryomov 	/*
809c647b8a8SIlya Dryomov 	 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
810c647b8a8SIlya Dryomov 	 * not worth a feature bit.  Set FAILOK per-op flag to make
811c647b8a8SIlya Dryomov 	 * sure older osds don't trip over an unsupported opcode.
812c647b8a8SIlya Dryomov 	 */
813c647b8a8SIlya Dryomov 	op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
814c647b8a8SIlya Dryomov }
815c647b8a8SIlya Dryomov EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
816c647b8a8SIlya Dryomov 
81790af3602SAlex Elder static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
818ec9123c5SAlex Elder 				struct ceph_osd_data *osd_data)
819ec9123c5SAlex Elder {
820ec9123c5SAlex Elder 	u64 length = ceph_osd_data_length(osd_data);
821ec9123c5SAlex Elder 
822ec9123c5SAlex Elder 	if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
823ec9123c5SAlex Elder 		BUG_ON(length > (u64) SIZE_MAX);
824ec9123c5SAlex Elder 		if (length)
82590af3602SAlex Elder 			ceph_msg_data_add_pages(msg, osd_data->pages,
826ec9123c5SAlex Elder 					length, osd_data->alignment);
827ec9123c5SAlex Elder 	} else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
828ec9123c5SAlex Elder 		BUG_ON(!length);
82990af3602SAlex Elder 		ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
830ec9123c5SAlex Elder #ifdef CONFIG_BLOCK
831ec9123c5SAlex Elder 	} else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
8325359a17dSIlya Dryomov 		ceph_msg_data_add_bio(msg, &osd_data->bio_pos, length);
833ec9123c5SAlex Elder #endif
834ec9123c5SAlex Elder 	} else {
835ec9123c5SAlex Elder 		BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
836ec9123c5SAlex Elder 	}
837ec9123c5SAlex Elder }
838ec9123c5SAlex Elder 
839bb873b53SIlya Dryomov static u32 osd_req_encode_op(struct ceph_osd_op *dst,
840bb873b53SIlya Dryomov 			     const struct ceph_osd_req_op *src)
8413d14c5d2SYehuda Sadeh {
842a8dd0a37SAlex Elder 	if (WARN_ON(!osd_req_opcode_valid(src->op))) {
843a8dd0a37SAlex Elder 		pr_err("unrecognized osd opcode %d\n", src->op);
844a8dd0a37SAlex Elder 
845a8dd0a37SAlex Elder 		return 0;
846a8dd0a37SAlex Elder 	}
8473d14c5d2SYehuda Sadeh 
848065a68f9SAlex Elder 	switch (src->op) {
849fbfab539SAlex Elder 	case CEPH_OSD_OP_STAT:
850fbfab539SAlex Elder 		break;
8513d14c5d2SYehuda Sadeh 	case CEPH_OSD_OP_READ:
8523d14c5d2SYehuda Sadeh 	case CEPH_OSD_OP_WRITE:
853e30b7577SIlya Dryomov 	case CEPH_OSD_OP_WRITEFULL:
854ad7a60deSLi Wang 	case CEPH_OSD_OP_ZERO:
855ad7a60deSLi Wang 	case CEPH_OSD_OP_TRUNCATE:
856175face2SAlex Elder 		dst->extent.offset = cpu_to_le64(src->extent.offset);
857175face2SAlex Elder 		dst->extent.length = cpu_to_le64(src->extent.length);
8583d14c5d2SYehuda Sadeh 		dst->extent.truncate_size =
8593d14c5d2SYehuda Sadeh 			cpu_to_le64(src->extent.truncate_size);
8603d14c5d2SYehuda Sadeh 		dst->extent.truncate_seq =
8613d14c5d2SYehuda Sadeh 			cpu_to_le32(src->extent.truncate_seq);
8623d14c5d2SYehuda Sadeh 		break;
8633d14c5d2SYehuda Sadeh 	case CEPH_OSD_OP_CALL:
8643d14c5d2SYehuda Sadeh 		dst->cls.class_len = src->cls.class_len;
8653d14c5d2SYehuda Sadeh 		dst->cls.method_len = src->cls.method_len;
866bb873b53SIlya Dryomov 		dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
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 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
9213d14c5d2SYehuda Sadeh 					       struct ceph_file_layout *layout,
9223d14c5d2SYehuda Sadeh 					       struct ceph_vino vino,
923715e4cd4SYan, Zheng 					       u64 off, u64 *plen,
924715e4cd4SYan, Zheng 					       unsigned int which, int num_ops,
9253d14c5d2SYehuda Sadeh 					       int opcode, int flags,
9263d14c5d2SYehuda Sadeh 					       struct ceph_snap_context *snapc,
9273d14c5d2SYehuda Sadeh 					       u32 truncate_seq,
9283d14c5d2SYehuda Sadeh 					       u64 truncate_size,
929153e5167SAlex Elder 					       bool use_mempool)
9303d14c5d2SYehuda Sadeh {
9313d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
93275d1c941SAlex Elder 	u64 objnum = 0;
93375d1c941SAlex Elder 	u64 objoff = 0;
93475d1c941SAlex Elder 	u64 objlen = 0;
9356816282dSSage Weil 	int r;
9363d14c5d2SYehuda Sadeh 
937ad7a60deSLi Wang 	BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
938864e9197SYan, Zheng 	       opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
939864e9197SYan, Zheng 	       opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
9403d14c5d2SYehuda Sadeh 
941acead002SAlex Elder 	req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
942ae7ca4a3SAlex Elder 					GFP_NOFS);
94313d1ad16SIlya Dryomov 	if (!req) {
94413d1ad16SIlya Dryomov 		r = -ENOMEM;
94513d1ad16SIlya Dryomov 		goto fail;
94613d1ad16SIlya Dryomov 	}
94779528734SAlex Elder 
9483d14c5d2SYehuda Sadeh 	/* calculate max write size */
949a19dadfbSAlex Elder 	r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
95013d1ad16SIlya Dryomov 	if (r)
95113d1ad16SIlya Dryomov 		goto fail;
952a19dadfbSAlex Elder 
953864e9197SYan, Zheng 	if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
954144cba14SYan, Zheng 		osd_req_op_init(req, which, opcode, 0);
955864e9197SYan, Zheng 	} else {
9567627151eSYan, Zheng 		u32 object_size = layout->object_size;
957864e9197SYan, Zheng 		u32 object_base = off - objoff;
958ccca4e37SYan, Zheng 		if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
959d18d1e28SAlex Elder 			if (truncate_size <= object_base) {
960d18d1e28SAlex Elder 				truncate_size = 0;
961d18d1e28SAlex Elder 			} else {
962d18d1e28SAlex Elder 				truncate_size -= object_base;
963d18d1e28SAlex Elder 				if (truncate_size > object_size)
964d18d1e28SAlex Elder 					truncate_size = object_size;
965d18d1e28SAlex Elder 			}
966ccca4e37SYan, Zheng 		}
967715e4cd4SYan, Zheng 		osd_req_op_extent_init(req, which, opcode, objoff, objlen,
968b0270324SAlex Elder 				       truncate_size, truncate_seq);
969864e9197SYan, Zheng 	}
970d18d1e28SAlex Elder 
971a1f4020aSJeff Layton 	req->r_abort_on_full = true;
972bb873b53SIlya Dryomov 	req->r_flags = flags;
9737627151eSYan, Zheng 	req->r_base_oloc.pool = layout->pool_id;
97430c156d9SYan, Zheng 	req->r_base_oloc.pool_ns = ceph_try_get_string(layout->pool_ns);
975d30291b9SIlya Dryomov 	ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum);
976dbe0fc41SAlex Elder 
977bb873b53SIlya Dryomov 	req->r_snapid = vino.snap;
978bb873b53SIlya Dryomov 	if (flags & CEPH_OSD_FLAG_WRITE)
979bb873b53SIlya Dryomov 		req->r_data_offset = off;
980bb873b53SIlya Dryomov 
98113d1ad16SIlya Dryomov 	r = ceph_osdc_alloc_messages(req, GFP_NOFS);
98213d1ad16SIlya Dryomov 	if (r)
98313d1ad16SIlya Dryomov 		goto fail;
98413d1ad16SIlya Dryomov 
9853d14c5d2SYehuda Sadeh 	return req;
98613d1ad16SIlya Dryomov 
98713d1ad16SIlya Dryomov fail:
98813d1ad16SIlya Dryomov 	ceph_osdc_put_request(req);
98913d1ad16SIlya Dryomov 	return ERR_PTR(r);
9903d14c5d2SYehuda Sadeh }
9913d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_new_request);
9923d14c5d2SYehuda Sadeh 
9933d14c5d2SYehuda Sadeh /*
9943d14c5d2SYehuda Sadeh  * We keep osd requests in an rbtree, sorted by ->r_tid.
9953d14c5d2SYehuda Sadeh  */
996fcd00b68SIlya Dryomov DEFINE_RB_FUNCS(request, struct ceph_osd_request, r_tid, r_node)
9974609245eSIlya Dryomov DEFINE_RB_FUNCS(request_mc, struct ceph_osd_request, r_tid, r_mc_node)
9983d14c5d2SYehuda Sadeh 
9990247a0cfSIlya Dryomov static bool osd_homeless(struct ceph_osd *osd)
10000247a0cfSIlya Dryomov {
10010247a0cfSIlya Dryomov 	return osd->o_osd == CEPH_HOMELESS_OSD;
10020247a0cfSIlya Dryomov }
10030247a0cfSIlya Dryomov 
10045aea3dcdSIlya Dryomov static bool osd_registered(struct ceph_osd *osd)
10053d14c5d2SYehuda Sadeh {
10065aea3dcdSIlya Dryomov 	verify_osdc_locked(osd->o_osdc);
10073d14c5d2SYehuda Sadeh 
10085aea3dcdSIlya Dryomov 	return !RB_EMPTY_NODE(&osd->o_node);
10093d14c5d2SYehuda Sadeh }
10103d14c5d2SYehuda Sadeh 
10113d14c5d2SYehuda Sadeh /*
10120247a0cfSIlya Dryomov  * Assumes @osd is zero-initialized.
10130247a0cfSIlya Dryomov  */
10140247a0cfSIlya Dryomov static void osd_init(struct ceph_osd *osd)
10150247a0cfSIlya Dryomov {
101602113a0fSElena Reshetova 	refcount_set(&osd->o_ref, 1);
10170247a0cfSIlya Dryomov 	RB_CLEAR_NODE(&osd->o_node);
10185aea3dcdSIlya Dryomov 	osd->o_requests = RB_ROOT;
1019922dab61SIlya Dryomov 	osd->o_linger_requests = RB_ROOT;
1020a02a946dSIlya Dryomov 	osd->o_backoff_mappings = RB_ROOT;
1021a02a946dSIlya Dryomov 	osd->o_backoffs_by_id = RB_ROOT;
10220247a0cfSIlya Dryomov 	INIT_LIST_HEAD(&osd->o_osd_lru);
10230247a0cfSIlya Dryomov 	INIT_LIST_HEAD(&osd->o_keepalive_item);
10240247a0cfSIlya Dryomov 	osd->o_incarnation = 1;
10255aea3dcdSIlya Dryomov 	mutex_init(&osd->lock);
10260247a0cfSIlya Dryomov }
10270247a0cfSIlya Dryomov 
10280247a0cfSIlya Dryomov static void osd_cleanup(struct ceph_osd *osd)
10290247a0cfSIlya Dryomov {
10300247a0cfSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&osd->o_node));
10315aea3dcdSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
1032922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
1033a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoff_mappings));
1034a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoffs_by_id));
10350247a0cfSIlya Dryomov 	WARN_ON(!list_empty(&osd->o_osd_lru));
10360247a0cfSIlya Dryomov 	WARN_ON(!list_empty(&osd->o_keepalive_item));
10370247a0cfSIlya Dryomov 
10380247a0cfSIlya Dryomov 	if (osd->o_auth.authorizer) {
10390247a0cfSIlya Dryomov 		WARN_ON(osd_homeless(osd));
10400247a0cfSIlya Dryomov 		ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
10410247a0cfSIlya Dryomov 	}
10420247a0cfSIlya Dryomov }
10430247a0cfSIlya Dryomov 
10440247a0cfSIlya Dryomov /*
10453d14c5d2SYehuda Sadeh  * Track open sessions with osds.
10463d14c5d2SYehuda Sadeh  */
1047e10006f8SAlex Elder static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
10483d14c5d2SYehuda Sadeh {
10493d14c5d2SYehuda Sadeh 	struct ceph_osd *osd;
10503d14c5d2SYehuda Sadeh 
10510247a0cfSIlya Dryomov 	WARN_ON(onum == CEPH_HOMELESS_OSD);
10520247a0cfSIlya Dryomov 
10537a28f59bSIlya Dryomov 	osd = kzalloc(sizeof(*osd), GFP_NOIO | __GFP_NOFAIL);
10540247a0cfSIlya Dryomov 	osd_init(osd);
10553d14c5d2SYehuda Sadeh 	osd->o_osdc = osdc;
1056e10006f8SAlex Elder 	osd->o_osd = onum;
10573d14c5d2SYehuda Sadeh 
1058b7a9e5ddSSage Weil 	ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
10593d14c5d2SYehuda Sadeh 
10603d14c5d2SYehuda Sadeh 	return osd;
10613d14c5d2SYehuda Sadeh }
10623d14c5d2SYehuda Sadeh 
10633d14c5d2SYehuda Sadeh static struct ceph_osd *get_osd(struct ceph_osd *osd)
10643d14c5d2SYehuda Sadeh {
106502113a0fSElena Reshetova 	if (refcount_inc_not_zero(&osd->o_ref)) {
106602113a0fSElena Reshetova 		dout("get_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref)-1,
106702113a0fSElena Reshetova 		     refcount_read(&osd->o_ref));
10683d14c5d2SYehuda Sadeh 		return osd;
10693d14c5d2SYehuda Sadeh 	} else {
10703d14c5d2SYehuda Sadeh 		dout("get_osd %p FAIL\n", osd);
10713d14c5d2SYehuda Sadeh 		return NULL;
10723d14c5d2SYehuda Sadeh 	}
10733d14c5d2SYehuda Sadeh }
10743d14c5d2SYehuda Sadeh 
10753d14c5d2SYehuda Sadeh static void put_osd(struct ceph_osd *osd)
10763d14c5d2SYehuda Sadeh {
107702113a0fSElena Reshetova 	dout("put_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref),
107802113a0fSElena Reshetova 	     refcount_read(&osd->o_ref) - 1);
107902113a0fSElena Reshetova 	if (refcount_dec_and_test(&osd->o_ref)) {
10800247a0cfSIlya Dryomov 		osd_cleanup(osd);
10813d14c5d2SYehuda Sadeh 		kfree(osd);
10823d14c5d2SYehuda Sadeh 	}
10833d14c5d2SYehuda Sadeh }
10843d14c5d2SYehuda Sadeh 
1085fcd00b68SIlya Dryomov DEFINE_RB_FUNCS(osd, struct ceph_osd, o_osd, o_node)
1086fcd00b68SIlya Dryomov 
10879dd2845cSIlya Dryomov static void __move_osd_to_lru(struct ceph_osd *osd)
10883d14c5d2SYehuda Sadeh {
10899dd2845cSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
10909dd2845cSIlya Dryomov 
10919dd2845cSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
10923d14c5d2SYehuda Sadeh 	BUG_ON(!list_empty(&osd->o_osd_lru));
1093bbf37ec3SIlya Dryomov 
10949dd2845cSIlya Dryomov 	spin_lock(&osdc->osd_lru_lock);
10953d14c5d2SYehuda Sadeh 	list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
10969dd2845cSIlya Dryomov 	spin_unlock(&osdc->osd_lru_lock);
10979dd2845cSIlya Dryomov 
1098a319bf56SIlya Dryomov 	osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
10993d14c5d2SYehuda Sadeh }
11003d14c5d2SYehuda Sadeh 
11019dd2845cSIlya Dryomov static void maybe_move_osd_to_lru(struct ceph_osd *osd)
1102bbf37ec3SIlya Dryomov {
11035aea3dcdSIlya Dryomov 	if (RB_EMPTY_ROOT(&osd->o_requests) &&
1104922dab61SIlya Dryomov 	    RB_EMPTY_ROOT(&osd->o_linger_requests))
11059dd2845cSIlya Dryomov 		__move_osd_to_lru(osd);
1106bbf37ec3SIlya Dryomov }
1107bbf37ec3SIlya Dryomov 
11083d14c5d2SYehuda Sadeh static void __remove_osd_from_lru(struct ceph_osd *osd)
11093d14c5d2SYehuda Sadeh {
11109dd2845cSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
11119dd2845cSIlya Dryomov 
11129dd2845cSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
11139dd2845cSIlya Dryomov 
11149dd2845cSIlya Dryomov 	spin_lock(&osdc->osd_lru_lock);
11153d14c5d2SYehuda Sadeh 	if (!list_empty(&osd->o_osd_lru))
11163d14c5d2SYehuda Sadeh 		list_del_init(&osd->o_osd_lru);
11179dd2845cSIlya Dryomov 	spin_unlock(&osdc->osd_lru_lock);
11183d14c5d2SYehuda Sadeh }
11193d14c5d2SYehuda Sadeh 
11203d14c5d2SYehuda Sadeh /*
11215aea3dcdSIlya Dryomov  * Close the connection and assign any leftover requests to the
11225aea3dcdSIlya Dryomov  * homeless session.
11235aea3dcdSIlya Dryomov  */
11245aea3dcdSIlya Dryomov static void close_osd(struct ceph_osd *osd)
11255aea3dcdSIlya Dryomov {
11265aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
11275aea3dcdSIlya Dryomov 	struct rb_node *n;
11285aea3dcdSIlya Dryomov 
11295aea3dcdSIlya Dryomov 	verify_osdc_wrlocked(osdc);
11305aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
11315aea3dcdSIlya Dryomov 
11325aea3dcdSIlya Dryomov 	ceph_con_close(&osd->o_con);
11335aea3dcdSIlya Dryomov 
11345aea3dcdSIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; ) {
11355aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
11365aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
11375aea3dcdSIlya Dryomov 
11385aea3dcdSIlya Dryomov 		n = rb_next(n); /* unlink_request() */
11395aea3dcdSIlya Dryomov 
11405aea3dcdSIlya Dryomov 		dout(" reassigning req %p tid %llu\n", req, req->r_tid);
11415aea3dcdSIlya Dryomov 		unlink_request(osd, req);
11425aea3dcdSIlya Dryomov 		link_request(&osdc->homeless_osd, req);
11435aea3dcdSIlya Dryomov 	}
1144922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_linger_requests); n; ) {
1145922dab61SIlya Dryomov 		struct ceph_osd_linger_request *lreq =
1146922dab61SIlya Dryomov 		    rb_entry(n, struct ceph_osd_linger_request, node);
1147922dab61SIlya Dryomov 
1148922dab61SIlya Dryomov 		n = rb_next(n); /* unlink_linger() */
1149922dab61SIlya Dryomov 
1150922dab61SIlya Dryomov 		dout(" reassigning lreq %p linger_id %llu\n", lreq,
1151922dab61SIlya Dryomov 		     lreq->linger_id);
1152922dab61SIlya Dryomov 		unlink_linger(osd, lreq);
1153922dab61SIlya Dryomov 		link_linger(&osdc->homeless_osd, lreq);
1154922dab61SIlya Dryomov 	}
1155a02a946dSIlya Dryomov 	clear_backoffs(osd);
11565aea3dcdSIlya Dryomov 
11575aea3dcdSIlya Dryomov 	__remove_osd_from_lru(osd);
11585aea3dcdSIlya Dryomov 	erase_osd(&osdc->osds, osd);
11595aea3dcdSIlya Dryomov 	put_osd(osd);
11605aea3dcdSIlya Dryomov }
11615aea3dcdSIlya Dryomov 
11625aea3dcdSIlya Dryomov /*
11633d14c5d2SYehuda Sadeh  * reset osd connect
11643d14c5d2SYehuda Sadeh  */
11655aea3dcdSIlya Dryomov static int reopen_osd(struct ceph_osd *osd)
11663d14c5d2SYehuda Sadeh {
1167c3acb181SAlex Elder 	struct ceph_entity_addr *peer_addr;
11683d14c5d2SYehuda Sadeh 
11695aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
11705aea3dcdSIlya Dryomov 
11715aea3dcdSIlya Dryomov 	if (RB_EMPTY_ROOT(&osd->o_requests) &&
1172922dab61SIlya Dryomov 	    RB_EMPTY_ROOT(&osd->o_linger_requests)) {
11735aea3dcdSIlya Dryomov 		close_osd(osd);
1174c3acb181SAlex Elder 		return -ENODEV;
1175c3acb181SAlex Elder 	}
1176c3acb181SAlex Elder 
11775aea3dcdSIlya Dryomov 	peer_addr = &osd->o_osdc->osdmap->osd_addr[osd->o_osd];
1178c3acb181SAlex Elder 	if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
11793d14c5d2SYehuda Sadeh 			!ceph_con_opened(&osd->o_con)) {
11805aea3dcdSIlya Dryomov 		struct rb_node *n;
1181c3acb181SAlex Elder 
11823d14c5d2SYehuda Sadeh 		dout("osd addr hasn't changed and connection never opened, "
11830b4af2e8SIlya Dryomov 		     "letting msgr retry\n");
11843d14c5d2SYehuda Sadeh 		/* touch each r_stamp for handle_timeout()'s benfit */
11855aea3dcdSIlya Dryomov 		for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
11865aea3dcdSIlya Dryomov 			struct ceph_osd_request *req =
11875aea3dcdSIlya Dryomov 			    rb_entry(n, struct ceph_osd_request, r_node);
11883d14c5d2SYehuda Sadeh 			req->r_stamp = jiffies;
11895aea3dcdSIlya Dryomov 		}
1190c3acb181SAlex Elder 
1191c3acb181SAlex Elder 		return -EAGAIN;
11923d14c5d2SYehuda Sadeh 	}
1193c3acb181SAlex Elder 
1194c3acb181SAlex Elder 	ceph_con_close(&osd->o_con);
1195c3acb181SAlex Elder 	ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1196c3acb181SAlex Elder 	osd->o_incarnation++;
1197c3acb181SAlex Elder 
1198c3acb181SAlex Elder 	return 0;
11993d14c5d2SYehuda Sadeh }
12003d14c5d2SYehuda Sadeh 
12015aea3dcdSIlya Dryomov static struct ceph_osd *lookup_create_osd(struct ceph_osd_client *osdc, int o,
12025aea3dcdSIlya Dryomov 					  bool wrlocked)
12033d14c5d2SYehuda Sadeh {
12045aea3dcdSIlya Dryomov 	struct ceph_osd *osd;
12055aea3dcdSIlya Dryomov 
12065aea3dcdSIlya Dryomov 	if (wrlocked)
12075aea3dcdSIlya Dryomov 		verify_osdc_wrlocked(osdc);
12085aea3dcdSIlya Dryomov 	else
12095aea3dcdSIlya Dryomov 		verify_osdc_locked(osdc);
12105aea3dcdSIlya Dryomov 
12115aea3dcdSIlya Dryomov 	if (o != CEPH_HOMELESS_OSD)
12125aea3dcdSIlya Dryomov 		osd = lookup_osd(&osdc->osds, o);
12135aea3dcdSIlya Dryomov 	else
12145aea3dcdSIlya Dryomov 		osd = &osdc->homeless_osd;
12155aea3dcdSIlya Dryomov 	if (!osd) {
12165aea3dcdSIlya Dryomov 		if (!wrlocked)
12175aea3dcdSIlya Dryomov 			return ERR_PTR(-EAGAIN);
12185aea3dcdSIlya Dryomov 
12195aea3dcdSIlya Dryomov 		osd = create_osd(osdc, o);
12205aea3dcdSIlya Dryomov 		insert_osd(&osdc->osds, osd);
12215aea3dcdSIlya Dryomov 		ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
12225aea3dcdSIlya Dryomov 			      &osdc->osdmap->osd_addr[osd->o_osd]);
12235aea3dcdSIlya Dryomov 	}
12245aea3dcdSIlya Dryomov 
12255aea3dcdSIlya Dryomov 	dout("%s osdc %p osd%d -> osd %p\n", __func__, osdc, o, osd);
12265aea3dcdSIlya Dryomov 	return osd;
1227a40c4f10SYehuda Sadeh }
1228a40c4f10SYehuda Sadeh 
12293d14c5d2SYehuda Sadeh /*
12305aea3dcdSIlya Dryomov  * Create request <-> OSD session relation.
12315aea3dcdSIlya Dryomov  *
12325aea3dcdSIlya Dryomov  * @req has to be assigned a tid, @osd may be homeless.
12333d14c5d2SYehuda Sadeh  */
12345aea3dcdSIlya Dryomov static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req)
12353d14c5d2SYehuda Sadeh {
12365aea3dcdSIlya Dryomov 	verify_osd_locked(osd);
12375aea3dcdSIlya Dryomov 	WARN_ON(!req->r_tid || req->r_osd);
12385aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
123935f9f8a0SSage Weil 	     req, req->r_tid);
12405aea3dcdSIlya Dryomov 
12415aea3dcdSIlya Dryomov 	if (!osd_homeless(osd))
12425aea3dcdSIlya Dryomov 		__remove_osd_from_lru(osd);
12435aea3dcdSIlya Dryomov 	else
12445aea3dcdSIlya Dryomov 		atomic_inc(&osd->o_osdc->num_homeless);
12455aea3dcdSIlya Dryomov 
12465aea3dcdSIlya Dryomov 	get_osd(osd);
12475aea3dcdSIlya Dryomov 	insert_request(&osd->o_requests, req);
12485aea3dcdSIlya Dryomov 	req->r_osd = osd;
124935f9f8a0SSage Weil }
125035f9f8a0SSage Weil 
12515aea3dcdSIlya Dryomov static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req)
12523d14c5d2SYehuda Sadeh {
12535aea3dcdSIlya Dryomov 	verify_osd_locked(osd);
12545aea3dcdSIlya Dryomov 	WARN_ON(req->r_osd != osd);
12555aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
12565aea3dcdSIlya Dryomov 	     req, req->r_tid);
12575aea3dcdSIlya Dryomov 
12585aea3dcdSIlya Dryomov 	req->r_osd = NULL;
12595aea3dcdSIlya Dryomov 	erase_request(&osd->o_requests, req);
12605aea3dcdSIlya Dryomov 	put_osd(osd);
12615aea3dcdSIlya Dryomov 
12625aea3dcdSIlya Dryomov 	if (!osd_homeless(osd))
12635aea3dcdSIlya Dryomov 		maybe_move_osd_to_lru(osd);
12645aea3dcdSIlya Dryomov 	else
12655aea3dcdSIlya Dryomov 		atomic_dec(&osd->o_osdc->num_homeless);
12663d14c5d2SYehuda Sadeh }
12673d14c5d2SYehuda Sadeh 
126863244fa1SIlya Dryomov static bool __pool_full(struct ceph_pg_pool_info *pi)
126963244fa1SIlya Dryomov {
127063244fa1SIlya Dryomov 	return pi->flags & CEPH_POOL_FLAG_FULL;
127163244fa1SIlya Dryomov }
127263244fa1SIlya Dryomov 
127342c1b124SIlya Dryomov static bool have_pool_full(struct ceph_osd_client *osdc)
127442c1b124SIlya Dryomov {
127542c1b124SIlya Dryomov 	struct rb_node *n;
127642c1b124SIlya Dryomov 
127742c1b124SIlya Dryomov 	for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
127842c1b124SIlya Dryomov 		struct ceph_pg_pool_info *pi =
127942c1b124SIlya Dryomov 		    rb_entry(n, struct ceph_pg_pool_info, node);
128042c1b124SIlya Dryomov 
128142c1b124SIlya Dryomov 		if (__pool_full(pi))
128242c1b124SIlya Dryomov 			return true;
128342c1b124SIlya Dryomov 	}
128442c1b124SIlya Dryomov 
128542c1b124SIlya Dryomov 	return false;
128642c1b124SIlya Dryomov }
128742c1b124SIlya Dryomov 
12885aea3dcdSIlya Dryomov static bool pool_full(struct ceph_osd_client *osdc, s64 pool_id)
12895aea3dcdSIlya Dryomov {
12905aea3dcdSIlya Dryomov 	struct ceph_pg_pool_info *pi;
12915aea3dcdSIlya Dryomov 
12925aea3dcdSIlya Dryomov 	pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
12935aea3dcdSIlya Dryomov 	if (!pi)
12945aea3dcdSIlya Dryomov 		return false;
12955aea3dcdSIlya Dryomov 
12965aea3dcdSIlya Dryomov 	return __pool_full(pi);
12975aea3dcdSIlya Dryomov }
12985aea3dcdSIlya Dryomov 
12993d14c5d2SYehuda Sadeh /*
1300d29adb34SJosh Durgin  * Returns whether a request should be blocked from being sent
1301d29adb34SJosh Durgin  * based on the current osdmap and osd_client settings.
1302d29adb34SJosh Durgin  */
130363244fa1SIlya Dryomov static bool target_should_be_paused(struct ceph_osd_client *osdc,
130463244fa1SIlya Dryomov 				    const struct ceph_osd_request_target *t,
130563244fa1SIlya Dryomov 				    struct ceph_pg_pool_info *pi)
130663244fa1SIlya Dryomov {
1307b7ec35b3SIlya Dryomov 	bool pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
1308b7ec35b3SIlya Dryomov 	bool pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
1309b7ec35b3SIlya Dryomov 		       ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
131063244fa1SIlya Dryomov 		       __pool_full(pi);
131163244fa1SIlya Dryomov 
13126d637a54SIlya Dryomov 	WARN_ON(pi->id != t->target_oloc.pool);
131358eb7932SJeff Layton 	return ((t->flags & CEPH_OSD_FLAG_READ) && pauserd) ||
131458eb7932SJeff Layton 	       ((t->flags & CEPH_OSD_FLAG_WRITE) && pausewr) ||
131558eb7932SJeff Layton 	       (osdc->osdmap->epoch < osdc->epoch_barrier);
131663244fa1SIlya Dryomov }
131763244fa1SIlya Dryomov 
131863244fa1SIlya Dryomov enum calc_target_result {
131963244fa1SIlya Dryomov 	CALC_TARGET_NO_ACTION = 0,
132063244fa1SIlya Dryomov 	CALC_TARGET_NEED_RESEND,
132163244fa1SIlya Dryomov 	CALC_TARGET_POOL_DNE,
132263244fa1SIlya Dryomov };
132363244fa1SIlya Dryomov 
132463244fa1SIlya Dryomov static enum calc_target_result calc_target(struct ceph_osd_client *osdc,
132563244fa1SIlya Dryomov 					   struct ceph_osd_request_target *t,
13267de030d6SIlya Dryomov 					   struct ceph_connection *con,
132763244fa1SIlya Dryomov 					   bool any_change)
132863244fa1SIlya Dryomov {
132963244fa1SIlya Dryomov 	struct ceph_pg_pool_info *pi;
133063244fa1SIlya Dryomov 	struct ceph_pg pgid, last_pgid;
133163244fa1SIlya Dryomov 	struct ceph_osds up, acting;
133263244fa1SIlya Dryomov 	bool force_resend = false;
133384ed45dfSIlya Dryomov 	bool unpaused = false;
133484ed45dfSIlya Dryomov 	bool legacy_change;
13357de030d6SIlya Dryomov 	bool split = false;
1336b7ec35b3SIlya Dryomov 	bool sort_bitwise = ceph_osdmap_flag(osdc, CEPH_OSDMAP_SORTBITWISE);
1337ae78dd81SIlya Dryomov 	bool recovery_deletes = ceph_osdmap_flag(osdc,
1338ae78dd81SIlya Dryomov 						 CEPH_OSDMAP_RECOVERY_DELETES);
133963244fa1SIlya Dryomov 	enum calc_target_result ct_res;
134063244fa1SIlya Dryomov 	int ret;
134163244fa1SIlya Dryomov 
134204c7d789SIlya Dryomov 	t->epoch = osdc->osdmap->epoch;
134363244fa1SIlya Dryomov 	pi = ceph_pg_pool_by_id(osdc->osdmap, t->base_oloc.pool);
134463244fa1SIlya Dryomov 	if (!pi) {
134563244fa1SIlya Dryomov 		t->osd = CEPH_HOMELESS_OSD;
134663244fa1SIlya Dryomov 		ct_res = CALC_TARGET_POOL_DNE;
134763244fa1SIlya Dryomov 		goto out;
134863244fa1SIlya Dryomov 	}
134963244fa1SIlya Dryomov 
135063244fa1SIlya Dryomov 	if (osdc->osdmap->epoch == pi->last_force_request_resend) {
1351dc93e0e2SIlya Dryomov 		if (t->last_force_resend < pi->last_force_request_resend) {
1352dc93e0e2SIlya Dryomov 			t->last_force_resend = pi->last_force_request_resend;
135363244fa1SIlya Dryomov 			force_resend = true;
1354dc93e0e2SIlya Dryomov 		} else if (t->last_force_resend == 0) {
135563244fa1SIlya Dryomov 			force_resend = true;
135663244fa1SIlya Dryomov 		}
135763244fa1SIlya Dryomov 	}
135863244fa1SIlya Dryomov 
1359db098ec4SIlya Dryomov 	/* apply tiering */
1360db098ec4SIlya Dryomov 	ceph_oid_copy(&t->target_oid, &t->base_oid);
1361db098ec4SIlya Dryomov 	ceph_oloc_copy(&t->target_oloc, &t->base_oloc);
1362db098ec4SIlya Dryomov 	if ((t->flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
136363244fa1SIlya Dryomov 		if (t->flags & CEPH_OSD_FLAG_READ && pi->read_tier >= 0)
136463244fa1SIlya Dryomov 			t->target_oloc.pool = pi->read_tier;
136563244fa1SIlya Dryomov 		if (t->flags & CEPH_OSD_FLAG_WRITE && pi->write_tier >= 0)
136663244fa1SIlya Dryomov 			t->target_oloc.pool = pi->write_tier;
13676d637a54SIlya Dryomov 
13686d637a54SIlya Dryomov 		pi = ceph_pg_pool_by_id(osdc->osdmap, t->target_oloc.pool);
13696d637a54SIlya Dryomov 		if (!pi) {
13706d637a54SIlya Dryomov 			t->osd = CEPH_HOMELESS_OSD;
13716d637a54SIlya Dryomov 			ct_res = CALC_TARGET_POOL_DNE;
13726d637a54SIlya Dryomov 			goto out;
13736d637a54SIlya Dryomov 		}
137463244fa1SIlya Dryomov 	}
137563244fa1SIlya Dryomov 
1376df28152dSIlya Dryomov 	ret = __ceph_object_locator_to_pg(pi, &t->target_oid, &t->target_oloc,
1377df28152dSIlya Dryomov 					  &pgid);
137863244fa1SIlya Dryomov 	if (ret) {
137963244fa1SIlya Dryomov 		WARN_ON(ret != -ENOENT);
138063244fa1SIlya Dryomov 		t->osd = CEPH_HOMELESS_OSD;
138163244fa1SIlya Dryomov 		ct_res = CALC_TARGET_POOL_DNE;
138263244fa1SIlya Dryomov 		goto out;
138363244fa1SIlya Dryomov 	}
138463244fa1SIlya Dryomov 	last_pgid.pool = pgid.pool;
138563244fa1SIlya Dryomov 	last_pgid.seed = ceph_stable_mod(pgid.seed, t->pg_num, t->pg_num_mask);
138663244fa1SIlya Dryomov 
1387df28152dSIlya Dryomov 	ceph_pg_to_up_acting_osds(osdc->osdmap, pi, &pgid, &up, &acting);
138863244fa1SIlya Dryomov 	if (any_change &&
138963244fa1SIlya Dryomov 	    ceph_is_new_interval(&t->acting,
139063244fa1SIlya Dryomov 				 &acting,
139163244fa1SIlya Dryomov 				 &t->up,
139263244fa1SIlya Dryomov 				 &up,
139363244fa1SIlya Dryomov 				 t->size,
139463244fa1SIlya Dryomov 				 pi->size,
139563244fa1SIlya Dryomov 				 t->min_size,
139663244fa1SIlya Dryomov 				 pi->min_size,
139763244fa1SIlya Dryomov 				 t->pg_num,
139863244fa1SIlya Dryomov 				 pi->pg_num,
139963244fa1SIlya Dryomov 				 t->sort_bitwise,
140063244fa1SIlya Dryomov 				 sort_bitwise,
1401ae78dd81SIlya Dryomov 				 t->recovery_deletes,
1402ae78dd81SIlya Dryomov 				 recovery_deletes,
140363244fa1SIlya Dryomov 				 &last_pgid))
140463244fa1SIlya Dryomov 		force_resend = true;
140563244fa1SIlya Dryomov 
140663244fa1SIlya Dryomov 	if (t->paused && !target_should_be_paused(osdc, t, pi)) {
140763244fa1SIlya Dryomov 		t->paused = false;
140884ed45dfSIlya Dryomov 		unpaused = true;
140963244fa1SIlya Dryomov 	}
141084ed45dfSIlya Dryomov 	legacy_change = ceph_pg_compare(&t->pgid, &pgid) ||
141184ed45dfSIlya Dryomov 			ceph_osds_changed(&t->acting, &acting, any_change);
14127de030d6SIlya Dryomov 	if (t->pg_num)
14137de030d6SIlya Dryomov 		split = ceph_pg_is_split(&last_pgid, t->pg_num, pi->pg_num);
141463244fa1SIlya Dryomov 
14157de030d6SIlya Dryomov 	if (legacy_change || force_resend || split) {
141663244fa1SIlya Dryomov 		t->pgid = pgid; /* struct */
1417df28152dSIlya Dryomov 		ceph_pg_to_primary_shard(osdc->osdmap, pi, &pgid, &t->spgid);
141863244fa1SIlya Dryomov 		ceph_osds_copy(&t->acting, &acting);
141963244fa1SIlya Dryomov 		ceph_osds_copy(&t->up, &up);
142063244fa1SIlya Dryomov 		t->size = pi->size;
142163244fa1SIlya Dryomov 		t->min_size = pi->min_size;
142263244fa1SIlya Dryomov 		t->pg_num = pi->pg_num;
142363244fa1SIlya Dryomov 		t->pg_num_mask = pi->pg_num_mask;
142463244fa1SIlya Dryomov 		t->sort_bitwise = sort_bitwise;
1425ae78dd81SIlya Dryomov 		t->recovery_deletes = recovery_deletes;
142663244fa1SIlya Dryomov 
142763244fa1SIlya Dryomov 		t->osd = acting.primary;
142863244fa1SIlya Dryomov 	}
142963244fa1SIlya Dryomov 
14307de030d6SIlya Dryomov 	if (unpaused || legacy_change || force_resend ||
14317de030d6SIlya Dryomov 	    (split && con && CEPH_HAVE_FEATURE(con->peer_features,
14327de030d6SIlya Dryomov 					       RESEND_ON_SPLIT)))
143384ed45dfSIlya Dryomov 		ct_res = CALC_TARGET_NEED_RESEND;
143484ed45dfSIlya Dryomov 	else
143584ed45dfSIlya Dryomov 		ct_res = CALC_TARGET_NO_ACTION;
143684ed45dfSIlya Dryomov 
143763244fa1SIlya Dryomov out:
143863244fa1SIlya Dryomov 	dout("%s t %p -> ct_res %d osd %d\n", __func__, t, ct_res, t->osd);
143963244fa1SIlya Dryomov 	return ct_res;
144063244fa1SIlya Dryomov }
144163244fa1SIlya Dryomov 
1442a02a946dSIlya Dryomov static struct ceph_spg_mapping *alloc_spg_mapping(void)
1443a02a946dSIlya Dryomov {
1444a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
1445a02a946dSIlya Dryomov 
1446a02a946dSIlya Dryomov 	spg = kmalloc(sizeof(*spg), GFP_NOIO);
1447a02a946dSIlya Dryomov 	if (!spg)
1448a02a946dSIlya Dryomov 		return NULL;
1449a02a946dSIlya Dryomov 
1450a02a946dSIlya Dryomov 	RB_CLEAR_NODE(&spg->node);
1451a02a946dSIlya Dryomov 	spg->backoffs = RB_ROOT;
1452a02a946dSIlya Dryomov 	return spg;
1453a02a946dSIlya Dryomov }
1454a02a946dSIlya Dryomov 
1455a02a946dSIlya Dryomov static void free_spg_mapping(struct ceph_spg_mapping *spg)
1456a02a946dSIlya Dryomov {
1457a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&spg->node));
1458a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&spg->backoffs));
1459a02a946dSIlya Dryomov 
1460a02a946dSIlya Dryomov 	kfree(spg);
1461a02a946dSIlya Dryomov }
1462a02a946dSIlya Dryomov 
1463a02a946dSIlya Dryomov /*
1464a02a946dSIlya Dryomov  * rbtree of ceph_spg_mapping for handling map<spg_t, ...>, similar to
1465a02a946dSIlya Dryomov  * ceph_pg_mapping.  Used to track OSD backoffs -- a backoff [range] is
1466a02a946dSIlya Dryomov  * defined only within a specific spgid; it does not pass anything to
1467a02a946dSIlya Dryomov  * children on split, or to another primary.
1468a02a946dSIlya Dryomov  */
1469a02a946dSIlya Dryomov DEFINE_RB_FUNCS2(spg_mapping, struct ceph_spg_mapping, spgid, ceph_spg_compare,
1470a02a946dSIlya Dryomov 		 RB_BYPTR, const struct ceph_spg *, node)
1471a02a946dSIlya Dryomov 
1472a02a946dSIlya Dryomov static u64 hoid_get_bitwise_key(const struct ceph_hobject_id *hoid)
1473a02a946dSIlya Dryomov {
1474a02a946dSIlya Dryomov 	return hoid->is_max ? 0x100000000ull : hoid->hash_reverse_bits;
1475a02a946dSIlya Dryomov }
1476a02a946dSIlya Dryomov 
1477a02a946dSIlya Dryomov static void hoid_get_effective_key(const struct ceph_hobject_id *hoid,
1478a02a946dSIlya Dryomov 				   void **pkey, size_t *pkey_len)
1479a02a946dSIlya Dryomov {
1480a02a946dSIlya Dryomov 	if (hoid->key_len) {
1481a02a946dSIlya Dryomov 		*pkey = hoid->key;
1482a02a946dSIlya Dryomov 		*pkey_len = hoid->key_len;
1483a02a946dSIlya Dryomov 	} else {
1484a02a946dSIlya Dryomov 		*pkey = hoid->oid;
1485a02a946dSIlya Dryomov 		*pkey_len = hoid->oid_len;
1486a02a946dSIlya Dryomov 	}
1487a02a946dSIlya Dryomov }
1488a02a946dSIlya Dryomov 
1489a02a946dSIlya Dryomov static int compare_names(const void *name1, size_t name1_len,
1490a02a946dSIlya Dryomov 			 const void *name2, size_t name2_len)
1491a02a946dSIlya Dryomov {
1492a02a946dSIlya Dryomov 	int ret;
1493a02a946dSIlya Dryomov 
1494a02a946dSIlya Dryomov 	ret = memcmp(name1, name2, min(name1_len, name2_len));
1495a02a946dSIlya Dryomov 	if (!ret) {
1496a02a946dSIlya Dryomov 		if (name1_len < name2_len)
1497a02a946dSIlya Dryomov 			ret = -1;
1498a02a946dSIlya Dryomov 		else if (name1_len > name2_len)
1499a02a946dSIlya Dryomov 			ret = 1;
1500a02a946dSIlya Dryomov 	}
1501a02a946dSIlya Dryomov 	return ret;
1502a02a946dSIlya Dryomov }
1503a02a946dSIlya Dryomov 
1504a02a946dSIlya Dryomov static int hoid_compare(const struct ceph_hobject_id *lhs,
1505a02a946dSIlya Dryomov 			const struct ceph_hobject_id *rhs)
1506a02a946dSIlya Dryomov {
1507a02a946dSIlya Dryomov 	void *effective_key1, *effective_key2;
1508a02a946dSIlya Dryomov 	size_t effective_key1_len, effective_key2_len;
1509a02a946dSIlya Dryomov 	int ret;
1510a02a946dSIlya Dryomov 
1511a02a946dSIlya Dryomov 	if (lhs->is_max < rhs->is_max)
1512a02a946dSIlya Dryomov 		return -1;
1513a02a946dSIlya Dryomov 	if (lhs->is_max > rhs->is_max)
1514a02a946dSIlya Dryomov 		return 1;
1515a02a946dSIlya Dryomov 
1516a02a946dSIlya Dryomov 	if (lhs->pool < rhs->pool)
1517a02a946dSIlya Dryomov 		return -1;
1518a02a946dSIlya Dryomov 	if (lhs->pool > rhs->pool)
1519a02a946dSIlya Dryomov 		return 1;
1520a02a946dSIlya Dryomov 
1521a02a946dSIlya Dryomov 	if (hoid_get_bitwise_key(lhs) < hoid_get_bitwise_key(rhs))
1522a02a946dSIlya Dryomov 		return -1;
1523a02a946dSIlya Dryomov 	if (hoid_get_bitwise_key(lhs) > hoid_get_bitwise_key(rhs))
1524a02a946dSIlya Dryomov 		return 1;
1525a02a946dSIlya Dryomov 
1526a02a946dSIlya Dryomov 	ret = compare_names(lhs->nspace, lhs->nspace_len,
1527a02a946dSIlya Dryomov 			    rhs->nspace, rhs->nspace_len);
1528a02a946dSIlya Dryomov 	if (ret)
1529a02a946dSIlya Dryomov 		return ret;
1530a02a946dSIlya Dryomov 
1531a02a946dSIlya Dryomov 	hoid_get_effective_key(lhs, &effective_key1, &effective_key1_len);
1532a02a946dSIlya Dryomov 	hoid_get_effective_key(rhs, &effective_key2, &effective_key2_len);
1533a02a946dSIlya Dryomov 	ret = compare_names(effective_key1, effective_key1_len,
1534a02a946dSIlya Dryomov 			    effective_key2, effective_key2_len);
1535a02a946dSIlya Dryomov 	if (ret)
1536a02a946dSIlya Dryomov 		return ret;
1537a02a946dSIlya Dryomov 
1538a02a946dSIlya Dryomov 	ret = compare_names(lhs->oid, lhs->oid_len, rhs->oid, rhs->oid_len);
1539a02a946dSIlya Dryomov 	if (ret)
1540a02a946dSIlya Dryomov 		return ret;
1541a02a946dSIlya Dryomov 
1542a02a946dSIlya Dryomov 	if (lhs->snapid < rhs->snapid)
1543a02a946dSIlya Dryomov 		return -1;
1544a02a946dSIlya Dryomov 	if (lhs->snapid > rhs->snapid)
1545a02a946dSIlya Dryomov 		return 1;
1546a02a946dSIlya Dryomov 
1547a02a946dSIlya Dryomov 	return 0;
1548a02a946dSIlya Dryomov }
1549a02a946dSIlya Dryomov 
1550a02a946dSIlya Dryomov /*
1551a02a946dSIlya Dryomov  * For decoding ->begin and ->end of MOSDBackoff only -- no MIN/MAX
1552a02a946dSIlya Dryomov  * compat stuff here.
1553a02a946dSIlya Dryomov  *
1554a02a946dSIlya Dryomov  * Assumes @hoid is zero-initialized.
1555a02a946dSIlya Dryomov  */
1556a02a946dSIlya Dryomov static int decode_hoid(void **p, void *end, struct ceph_hobject_id *hoid)
1557a02a946dSIlya Dryomov {
1558a02a946dSIlya Dryomov 	u8 struct_v;
1559a02a946dSIlya Dryomov 	u32 struct_len;
1560a02a946dSIlya Dryomov 	int ret;
1561a02a946dSIlya Dryomov 
1562a02a946dSIlya Dryomov 	ret = ceph_start_decoding(p, end, 4, "hobject_t", &struct_v,
1563a02a946dSIlya Dryomov 				  &struct_len);
1564a02a946dSIlya Dryomov 	if (ret)
1565a02a946dSIlya Dryomov 		return ret;
1566a02a946dSIlya Dryomov 
1567a02a946dSIlya Dryomov 	if (struct_v < 4) {
1568a02a946dSIlya Dryomov 		pr_err("got struct_v %d < 4 of hobject_t\n", struct_v);
1569a02a946dSIlya Dryomov 		goto e_inval;
1570a02a946dSIlya Dryomov 	}
1571a02a946dSIlya Dryomov 
1572a02a946dSIlya Dryomov 	hoid->key = ceph_extract_encoded_string(p, end, &hoid->key_len,
1573a02a946dSIlya Dryomov 						GFP_NOIO);
1574a02a946dSIlya Dryomov 	if (IS_ERR(hoid->key)) {
1575a02a946dSIlya Dryomov 		ret = PTR_ERR(hoid->key);
1576a02a946dSIlya Dryomov 		hoid->key = NULL;
1577a02a946dSIlya Dryomov 		return ret;
1578a02a946dSIlya Dryomov 	}
1579a02a946dSIlya Dryomov 
1580a02a946dSIlya Dryomov 	hoid->oid = ceph_extract_encoded_string(p, end, &hoid->oid_len,
1581a02a946dSIlya Dryomov 						GFP_NOIO);
1582a02a946dSIlya Dryomov 	if (IS_ERR(hoid->oid)) {
1583a02a946dSIlya Dryomov 		ret = PTR_ERR(hoid->oid);
1584a02a946dSIlya Dryomov 		hoid->oid = NULL;
1585a02a946dSIlya Dryomov 		return ret;
1586a02a946dSIlya Dryomov 	}
1587a02a946dSIlya Dryomov 
1588a02a946dSIlya Dryomov 	ceph_decode_64_safe(p, end, hoid->snapid, e_inval);
1589a02a946dSIlya Dryomov 	ceph_decode_32_safe(p, end, hoid->hash, e_inval);
1590a02a946dSIlya Dryomov 	ceph_decode_8_safe(p, end, hoid->is_max, e_inval);
1591a02a946dSIlya Dryomov 
1592a02a946dSIlya Dryomov 	hoid->nspace = ceph_extract_encoded_string(p, end, &hoid->nspace_len,
1593a02a946dSIlya Dryomov 						   GFP_NOIO);
1594a02a946dSIlya Dryomov 	if (IS_ERR(hoid->nspace)) {
1595a02a946dSIlya Dryomov 		ret = PTR_ERR(hoid->nspace);
1596a02a946dSIlya Dryomov 		hoid->nspace = NULL;
1597a02a946dSIlya Dryomov 		return ret;
1598a02a946dSIlya Dryomov 	}
1599a02a946dSIlya Dryomov 
1600a02a946dSIlya Dryomov 	ceph_decode_64_safe(p, end, hoid->pool, e_inval);
1601a02a946dSIlya Dryomov 
1602a02a946dSIlya Dryomov 	ceph_hoid_build_hash_cache(hoid);
1603a02a946dSIlya Dryomov 	return 0;
1604a02a946dSIlya Dryomov 
1605a02a946dSIlya Dryomov e_inval:
1606a02a946dSIlya Dryomov 	return -EINVAL;
1607a02a946dSIlya Dryomov }
1608a02a946dSIlya Dryomov 
1609a02a946dSIlya Dryomov static int hoid_encoding_size(const struct ceph_hobject_id *hoid)
1610a02a946dSIlya Dryomov {
1611a02a946dSIlya Dryomov 	return 8 + 4 + 1 + 8 + /* snapid, hash, is_max, pool */
1612a02a946dSIlya Dryomov 	       4 + hoid->key_len + 4 + hoid->oid_len + 4 + hoid->nspace_len;
1613a02a946dSIlya Dryomov }
1614a02a946dSIlya Dryomov 
1615a02a946dSIlya Dryomov static void encode_hoid(void **p, void *end, const struct ceph_hobject_id *hoid)
1616a02a946dSIlya Dryomov {
1617a02a946dSIlya Dryomov 	ceph_start_encoding(p, 4, 3, hoid_encoding_size(hoid));
1618a02a946dSIlya Dryomov 	ceph_encode_string(p, end, hoid->key, hoid->key_len);
1619a02a946dSIlya Dryomov 	ceph_encode_string(p, end, hoid->oid, hoid->oid_len);
1620a02a946dSIlya Dryomov 	ceph_encode_64(p, hoid->snapid);
1621a02a946dSIlya Dryomov 	ceph_encode_32(p, hoid->hash);
1622a02a946dSIlya Dryomov 	ceph_encode_8(p, hoid->is_max);
1623a02a946dSIlya Dryomov 	ceph_encode_string(p, end, hoid->nspace, hoid->nspace_len);
1624a02a946dSIlya Dryomov 	ceph_encode_64(p, hoid->pool);
1625a02a946dSIlya Dryomov }
1626a02a946dSIlya Dryomov 
1627a02a946dSIlya Dryomov static void free_hoid(struct ceph_hobject_id *hoid)
1628a02a946dSIlya Dryomov {
1629a02a946dSIlya Dryomov 	if (hoid) {
1630a02a946dSIlya Dryomov 		kfree(hoid->key);
1631a02a946dSIlya Dryomov 		kfree(hoid->oid);
1632a02a946dSIlya Dryomov 		kfree(hoid->nspace);
1633a02a946dSIlya Dryomov 		kfree(hoid);
1634a02a946dSIlya Dryomov 	}
1635a02a946dSIlya Dryomov }
1636a02a946dSIlya Dryomov 
1637a02a946dSIlya Dryomov static struct ceph_osd_backoff *alloc_backoff(void)
1638a02a946dSIlya Dryomov {
1639a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
1640a02a946dSIlya Dryomov 
1641a02a946dSIlya Dryomov 	backoff = kzalloc(sizeof(*backoff), GFP_NOIO);
1642a02a946dSIlya Dryomov 	if (!backoff)
1643a02a946dSIlya Dryomov 		return NULL;
1644a02a946dSIlya Dryomov 
1645a02a946dSIlya Dryomov 	RB_CLEAR_NODE(&backoff->spg_node);
1646a02a946dSIlya Dryomov 	RB_CLEAR_NODE(&backoff->id_node);
1647a02a946dSIlya Dryomov 	return backoff;
1648a02a946dSIlya Dryomov }
1649a02a946dSIlya Dryomov 
1650a02a946dSIlya Dryomov static void free_backoff(struct ceph_osd_backoff *backoff)
1651a02a946dSIlya Dryomov {
1652a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&backoff->spg_node));
1653a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&backoff->id_node));
1654a02a946dSIlya Dryomov 
1655a02a946dSIlya Dryomov 	free_hoid(backoff->begin);
1656a02a946dSIlya Dryomov 	free_hoid(backoff->end);
1657a02a946dSIlya Dryomov 	kfree(backoff);
1658a02a946dSIlya Dryomov }
1659a02a946dSIlya Dryomov 
1660a02a946dSIlya Dryomov /*
1661a02a946dSIlya Dryomov  * Within a specific spgid, backoffs are managed by ->begin hoid.
1662a02a946dSIlya Dryomov  */
1663a02a946dSIlya Dryomov DEFINE_RB_INSDEL_FUNCS2(backoff, struct ceph_osd_backoff, begin, hoid_compare,
1664a02a946dSIlya Dryomov 			RB_BYVAL, spg_node);
1665a02a946dSIlya Dryomov 
1666a02a946dSIlya Dryomov static struct ceph_osd_backoff *lookup_containing_backoff(struct rb_root *root,
1667a02a946dSIlya Dryomov 					    const struct ceph_hobject_id *hoid)
1668a02a946dSIlya Dryomov {
1669a02a946dSIlya Dryomov 	struct rb_node *n = root->rb_node;
1670a02a946dSIlya Dryomov 
1671a02a946dSIlya Dryomov 	while (n) {
1672a02a946dSIlya Dryomov 		struct ceph_osd_backoff *cur =
1673a02a946dSIlya Dryomov 		    rb_entry(n, struct ceph_osd_backoff, spg_node);
1674a02a946dSIlya Dryomov 		int cmp;
1675a02a946dSIlya Dryomov 
1676a02a946dSIlya Dryomov 		cmp = hoid_compare(hoid, cur->begin);
1677a02a946dSIlya Dryomov 		if (cmp < 0) {
1678a02a946dSIlya Dryomov 			n = n->rb_left;
1679a02a946dSIlya Dryomov 		} else if (cmp > 0) {
1680a02a946dSIlya Dryomov 			if (hoid_compare(hoid, cur->end) < 0)
1681a02a946dSIlya Dryomov 				return cur;
1682a02a946dSIlya Dryomov 
1683a02a946dSIlya Dryomov 			n = n->rb_right;
1684a02a946dSIlya Dryomov 		} else {
1685a02a946dSIlya Dryomov 			return cur;
1686a02a946dSIlya Dryomov 		}
1687a02a946dSIlya Dryomov 	}
1688a02a946dSIlya Dryomov 
1689a02a946dSIlya Dryomov 	return NULL;
1690a02a946dSIlya Dryomov }
1691a02a946dSIlya Dryomov 
1692a02a946dSIlya Dryomov /*
1693a02a946dSIlya Dryomov  * Each backoff has a unique id within its OSD session.
1694a02a946dSIlya Dryomov  */
1695a02a946dSIlya Dryomov DEFINE_RB_FUNCS(backoff_by_id, struct ceph_osd_backoff, id, id_node)
1696a02a946dSIlya Dryomov 
1697a02a946dSIlya Dryomov static void clear_backoffs(struct ceph_osd *osd)
1698a02a946dSIlya Dryomov {
1699a02a946dSIlya Dryomov 	while (!RB_EMPTY_ROOT(&osd->o_backoff_mappings)) {
1700a02a946dSIlya Dryomov 		struct ceph_spg_mapping *spg =
1701a02a946dSIlya Dryomov 		    rb_entry(rb_first(&osd->o_backoff_mappings),
1702a02a946dSIlya Dryomov 			     struct ceph_spg_mapping, node);
1703a02a946dSIlya Dryomov 
1704a02a946dSIlya Dryomov 		while (!RB_EMPTY_ROOT(&spg->backoffs)) {
1705a02a946dSIlya Dryomov 			struct ceph_osd_backoff *backoff =
1706a02a946dSIlya Dryomov 			    rb_entry(rb_first(&spg->backoffs),
1707a02a946dSIlya Dryomov 				     struct ceph_osd_backoff, spg_node);
1708a02a946dSIlya Dryomov 
1709a02a946dSIlya Dryomov 			erase_backoff(&spg->backoffs, backoff);
1710a02a946dSIlya Dryomov 			erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
1711a02a946dSIlya Dryomov 			free_backoff(backoff);
1712a02a946dSIlya Dryomov 		}
1713a02a946dSIlya Dryomov 		erase_spg_mapping(&osd->o_backoff_mappings, spg);
1714a02a946dSIlya Dryomov 		free_spg_mapping(spg);
1715a02a946dSIlya Dryomov 	}
1716a02a946dSIlya Dryomov }
1717a02a946dSIlya Dryomov 
1718a02a946dSIlya Dryomov /*
1719a02a946dSIlya Dryomov  * Set up a temporary, non-owning view into @t.
1720a02a946dSIlya Dryomov  */
1721a02a946dSIlya Dryomov static void hoid_fill_from_target(struct ceph_hobject_id *hoid,
1722a02a946dSIlya Dryomov 				  const struct ceph_osd_request_target *t)
1723a02a946dSIlya Dryomov {
1724a02a946dSIlya Dryomov 	hoid->key = NULL;
1725a02a946dSIlya Dryomov 	hoid->key_len = 0;
1726a02a946dSIlya Dryomov 	hoid->oid = t->target_oid.name;
1727a02a946dSIlya Dryomov 	hoid->oid_len = t->target_oid.name_len;
1728a02a946dSIlya Dryomov 	hoid->snapid = CEPH_NOSNAP;
1729a02a946dSIlya Dryomov 	hoid->hash = t->pgid.seed;
1730a02a946dSIlya Dryomov 	hoid->is_max = false;
1731a02a946dSIlya Dryomov 	if (t->target_oloc.pool_ns) {
1732a02a946dSIlya Dryomov 		hoid->nspace = t->target_oloc.pool_ns->str;
1733a02a946dSIlya Dryomov 		hoid->nspace_len = t->target_oloc.pool_ns->len;
1734a02a946dSIlya Dryomov 	} else {
1735a02a946dSIlya Dryomov 		hoid->nspace = NULL;
1736a02a946dSIlya Dryomov 		hoid->nspace_len = 0;
1737a02a946dSIlya Dryomov 	}
1738a02a946dSIlya Dryomov 	hoid->pool = t->target_oloc.pool;
1739a02a946dSIlya Dryomov 	ceph_hoid_build_hash_cache(hoid);
1740a02a946dSIlya Dryomov }
1741a02a946dSIlya Dryomov 
1742a02a946dSIlya Dryomov static bool should_plug_request(struct ceph_osd_request *req)
1743a02a946dSIlya Dryomov {
1744a02a946dSIlya Dryomov 	struct ceph_osd *osd = req->r_osd;
1745a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
1746a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
1747a02a946dSIlya Dryomov 	struct ceph_hobject_id hoid;
1748a02a946dSIlya Dryomov 
1749a02a946dSIlya Dryomov 	spg = lookup_spg_mapping(&osd->o_backoff_mappings, &req->r_t.spgid);
1750a02a946dSIlya Dryomov 	if (!spg)
1751a02a946dSIlya Dryomov 		return false;
1752a02a946dSIlya Dryomov 
1753a02a946dSIlya Dryomov 	hoid_fill_from_target(&hoid, &req->r_t);
1754a02a946dSIlya Dryomov 	backoff = lookup_containing_backoff(&spg->backoffs, &hoid);
1755a02a946dSIlya Dryomov 	if (!backoff)
1756a02a946dSIlya Dryomov 		return false;
1757a02a946dSIlya Dryomov 
1758a02a946dSIlya Dryomov 	dout("%s req %p tid %llu backoff osd%d spgid %llu.%xs%d id %llu\n",
1759a02a946dSIlya Dryomov 	     __func__, req, req->r_tid, osd->o_osd, backoff->spgid.pgid.pool,
1760a02a946dSIlya Dryomov 	     backoff->spgid.pgid.seed, backoff->spgid.shard, backoff->id);
1761a02a946dSIlya Dryomov 	return true;
1762a02a946dSIlya Dryomov }
1763a02a946dSIlya Dryomov 
1764bb873b53SIlya Dryomov static void setup_request_data(struct ceph_osd_request *req,
1765bb873b53SIlya Dryomov 			       struct ceph_msg *msg)
17663d14c5d2SYehuda Sadeh {
1767bb873b53SIlya Dryomov 	u32 data_len = 0;
1768bb873b53SIlya Dryomov 	int i;
17693d14c5d2SYehuda Sadeh 
1770bb873b53SIlya Dryomov 	if (!list_empty(&msg->data))
1771bb873b53SIlya Dryomov 		return;
17723d14c5d2SYehuda Sadeh 
1773bb873b53SIlya Dryomov 	WARN_ON(msg->data_length);
1774bb873b53SIlya Dryomov 	for (i = 0; i < req->r_num_ops; i++) {
1775bb873b53SIlya Dryomov 		struct ceph_osd_req_op *op = &req->r_ops[i];
1776bb873b53SIlya Dryomov 
1777bb873b53SIlya Dryomov 		switch (op->op) {
1778bb873b53SIlya Dryomov 		/* request */
1779bb873b53SIlya Dryomov 		case CEPH_OSD_OP_WRITE:
1780bb873b53SIlya Dryomov 		case CEPH_OSD_OP_WRITEFULL:
1781bb873b53SIlya Dryomov 			WARN_ON(op->indata_len != op->extent.length);
1782bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(msg, &op->extent.osd_data);
1783bb873b53SIlya Dryomov 			break;
1784bb873b53SIlya Dryomov 		case CEPH_OSD_OP_SETXATTR:
1785bb873b53SIlya Dryomov 		case CEPH_OSD_OP_CMPXATTR:
1786bb873b53SIlya Dryomov 			WARN_ON(op->indata_len != op->xattr.name_len +
1787bb873b53SIlya Dryomov 						  op->xattr.value_len);
1788bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(msg, &op->xattr.osd_data);
1789bb873b53SIlya Dryomov 			break;
1790922dab61SIlya Dryomov 		case CEPH_OSD_OP_NOTIFY_ACK:
1791922dab61SIlya Dryomov 			ceph_osdc_msg_data_add(msg,
1792922dab61SIlya Dryomov 					       &op->notify_ack.request_data);
1793922dab61SIlya Dryomov 			break;
1794bb873b53SIlya Dryomov 
1795bb873b53SIlya Dryomov 		/* reply */
1796bb873b53SIlya Dryomov 		case CEPH_OSD_OP_STAT:
1797bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(req->r_reply,
1798bb873b53SIlya Dryomov 					       &op->raw_data_in);
1799bb873b53SIlya Dryomov 			break;
1800bb873b53SIlya Dryomov 		case CEPH_OSD_OP_READ:
1801bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(req->r_reply,
1802bb873b53SIlya Dryomov 					       &op->extent.osd_data);
1803bb873b53SIlya Dryomov 			break;
1804a4ed38d7SDouglas Fuller 		case CEPH_OSD_OP_LIST_WATCHERS:
1805a4ed38d7SDouglas Fuller 			ceph_osdc_msg_data_add(req->r_reply,
1806a4ed38d7SDouglas Fuller 					       &op->list_watchers.response_data);
1807a4ed38d7SDouglas Fuller 			break;
1808bb873b53SIlya Dryomov 
1809bb873b53SIlya Dryomov 		/* both */
1810bb873b53SIlya Dryomov 		case CEPH_OSD_OP_CALL:
1811bb873b53SIlya Dryomov 			WARN_ON(op->indata_len != op->cls.class_len +
1812bb873b53SIlya Dryomov 						  op->cls.method_len +
1813bb873b53SIlya Dryomov 						  op->cls.indata_len);
1814bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(msg, &op->cls.request_info);
1815bb873b53SIlya Dryomov 			/* optional, can be NONE */
1816bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(msg, &op->cls.request_data);
1817bb873b53SIlya Dryomov 			/* optional, can be NONE */
1818bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(req->r_reply,
1819bb873b53SIlya Dryomov 					       &op->cls.response_data);
1820bb873b53SIlya Dryomov 			break;
182119079203SIlya Dryomov 		case CEPH_OSD_OP_NOTIFY:
182219079203SIlya Dryomov 			ceph_osdc_msg_data_add(msg,
182319079203SIlya Dryomov 					       &op->notify.request_data);
182419079203SIlya Dryomov 			ceph_osdc_msg_data_add(req->r_reply,
182519079203SIlya Dryomov 					       &op->notify.response_data);
182619079203SIlya Dryomov 			break;
1827bb873b53SIlya Dryomov 		}
1828bb873b53SIlya Dryomov 
1829bb873b53SIlya Dryomov 		data_len += op->indata_len;
1830bb873b53SIlya Dryomov 	}
1831bb873b53SIlya Dryomov 
1832bb873b53SIlya Dryomov 	WARN_ON(data_len != msg->data_length);
1833bb873b53SIlya Dryomov }
1834bb873b53SIlya Dryomov 
18352e59ffd1SIlya Dryomov static void encode_pgid(void **p, const struct ceph_pg *pgid)
18362e59ffd1SIlya Dryomov {
18372e59ffd1SIlya Dryomov 	ceph_encode_8(p, 1);
18382e59ffd1SIlya Dryomov 	ceph_encode_64(p, pgid->pool);
18392e59ffd1SIlya Dryomov 	ceph_encode_32(p, pgid->seed);
18402e59ffd1SIlya Dryomov 	ceph_encode_32(p, -1); /* preferred */
18412e59ffd1SIlya Dryomov }
18422e59ffd1SIlya Dryomov 
18438cb441c0SIlya Dryomov static void encode_spgid(void **p, const struct ceph_spg *spgid)
18448cb441c0SIlya Dryomov {
18458cb441c0SIlya Dryomov 	ceph_start_encoding(p, 1, 1, CEPH_PGID_ENCODING_LEN + 1);
18468cb441c0SIlya Dryomov 	encode_pgid(p, &spgid->pgid);
18478cb441c0SIlya Dryomov 	ceph_encode_8(p, spgid->shard);
18488cb441c0SIlya Dryomov }
18498cb441c0SIlya Dryomov 
18502e59ffd1SIlya Dryomov static void encode_oloc(void **p, void *end,
18512e59ffd1SIlya Dryomov 			const struct ceph_object_locator *oloc)
18522e59ffd1SIlya Dryomov {
18532e59ffd1SIlya Dryomov 	ceph_start_encoding(p, 5, 4, ceph_oloc_encoding_size(oloc));
18542e59ffd1SIlya Dryomov 	ceph_encode_64(p, oloc->pool);
18552e59ffd1SIlya Dryomov 	ceph_encode_32(p, -1); /* preferred */
18562e59ffd1SIlya Dryomov 	ceph_encode_32(p, 0);  /* key len */
18572e59ffd1SIlya Dryomov 	if (oloc->pool_ns)
18582e59ffd1SIlya Dryomov 		ceph_encode_string(p, end, oloc->pool_ns->str,
18592e59ffd1SIlya Dryomov 				   oloc->pool_ns->len);
18602e59ffd1SIlya Dryomov 	else
18612e59ffd1SIlya Dryomov 		ceph_encode_32(p, 0);
18622e59ffd1SIlya Dryomov }
18632e59ffd1SIlya Dryomov 
18648cb441c0SIlya Dryomov static void encode_request_partial(struct ceph_osd_request *req,
18658cb441c0SIlya Dryomov 				   struct ceph_msg *msg)
1866bb873b53SIlya Dryomov {
1867bb873b53SIlya Dryomov 	void *p = msg->front.iov_base;
1868bb873b53SIlya Dryomov 	void *const end = p + msg->front_alloc_len;
1869bb873b53SIlya Dryomov 	u32 data_len = 0;
1870bb873b53SIlya Dryomov 	int i;
1871bb873b53SIlya Dryomov 
1872bb873b53SIlya Dryomov 	if (req->r_flags & CEPH_OSD_FLAG_WRITE) {
1873bb873b53SIlya Dryomov 		/* snapshots aren't writeable */
1874bb873b53SIlya Dryomov 		WARN_ON(req->r_snapid != CEPH_NOSNAP);
1875bb873b53SIlya Dryomov 	} else {
1876bb873b53SIlya Dryomov 		WARN_ON(req->r_mtime.tv_sec || req->r_mtime.tv_nsec ||
1877bb873b53SIlya Dryomov 			req->r_data_offset || req->r_snapc);
1878bb873b53SIlya Dryomov 	}
1879bb873b53SIlya Dryomov 
1880bb873b53SIlya Dryomov 	setup_request_data(req, msg);
1881bb873b53SIlya Dryomov 
18828cb441c0SIlya Dryomov 	encode_spgid(&p, &req->r_t.spgid); /* actual spg */
18838cb441c0SIlya Dryomov 	ceph_encode_32(&p, req->r_t.pgid.seed); /* raw hash */
1884bb873b53SIlya Dryomov 	ceph_encode_32(&p, req->r_osdc->osdmap->epoch);
1885bb873b53SIlya Dryomov 	ceph_encode_32(&p, req->r_flags);
18868cb441c0SIlya Dryomov 
18878cb441c0SIlya Dryomov 	/* reqid */
18888cb441c0SIlya Dryomov 	ceph_start_encoding(&p, 2, 2, sizeof(struct ceph_osd_reqid));
18898cb441c0SIlya Dryomov 	memset(p, 0, sizeof(struct ceph_osd_reqid));
18908cb441c0SIlya Dryomov 	p += sizeof(struct ceph_osd_reqid);
18918cb441c0SIlya Dryomov 
18928cb441c0SIlya Dryomov 	/* trace */
18938cb441c0SIlya Dryomov 	memset(p, 0, sizeof(struct ceph_blkin_trace_info));
18948cb441c0SIlya Dryomov 	p += sizeof(struct ceph_blkin_trace_info);
18958cb441c0SIlya Dryomov 
18968cb441c0SIlya Dryomov 	ceph_encode_32(&p, 0); /* client_inc, always 0 */
1897bb873b53SIlya Dryomov 	ceph_encode_timespec(p, &req->r_mtime);
1898bb873b53SIlya Dryomov 	p += sizeof(struct ceph_timespec);
1899aa26d662SJeff Layton 
19002e59ffd1SIlya Dryomov 	encode_oloc(&p, end, &req->r_t.target_oloc);
19012e59ffd1SIlya Dryomov 	ceph_encode_string(&p, end, req->r_t.target_oid.name,
19022e59ffd1SIlya Dryomov 			   req->r_t.target_oid.name_len);
1903bb873b53SIlya Dryomov 
1904bb873b53SIlya Dryomov 	/* ops, can imply data */
1905bb873b53SIlya Dryomov 	ceph_encode_16(&p, req->r_num_ops);
1906bb873b53SIlya Dryomov 	for (i = 0; i < req->r_num_ops; i++) {
1907bb873b53SIlya Dryomov 		data_len += osd_req_encode_op(p, &req->r_ops[i]);
1908bb873b53SIlya Dryomov 		p += sizeof(struct ceph_osd_op);
1909bb873b53SIlya Dryomov 	}
1910bb873b53SIlya Dryomov 
1911bb873b53SIlya Dryomov 	ceph_encode_64(&p, req->r_snapid); /* snapid */
1912bb873b53SIlya Dryomov 	if (req->r_snapc) {
1913bb873b53SIlya Dryomov 		ceph_encode_64(&p, req->r_snapc->seq);
1914bb873b53SIlya Dryomov 		ceph_encode_32(&p, req->r_snapc->num_snaps);
1915bb873b53SIlya Dryomov 		for (i = 0; i < req->r_snapc->num_snaps; i++)
1916bb873b53SIlya Dryomov 			ceph_encode_64(&p, req->r_snapc->snaps[i]);
1917bb873b53SIlya Dryomov 	} else {
1918bb873b53SIlya Dryomov 		ceph_encode_64(&p, 0); /* snap_seq */
1919bb873b53SIlya Dryomov 		ceph_encode_32(&p, 0); /* snaps len */
1920bb873b53SIlya Dryomov 	}
1921bb873b53SIlya Dryomov 
1922bb873b53SIlya Dryomov 	ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
1923986e8989SIlya Dryomov 	BUG_ON(p > end - 8); /* space for features */
1924bb873b53SIlya Dryomov 
19258cb441c0SIlya Dryomov 	msg->hdr.version = cpu_to_le16(8); /* MOSDOp v8 */
19268cb441c0SIlya Dryomov 	/* front_len is finalized in encode_request_finish() */
1927986e8989SIlya Dryomov 	msg->front.iov_len = p - msg->front.iov_base;
1928986e8989SIlya Dryomov 	msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1929bb873b53SIlya Dryomov 	msg->hdr.data_len = cpu_to_le32(data_len);
1930bb873b53SIlya Dryomov 	/*
1931bb873b53SIlya Dryomov 	 * The header "data_off" is a hint to the receiver allowing it
1932bb873b53SIlya Dryomov 	 * to align received data into its buffers such that there's no
1933bb873b53SIlya Dryomov 	 * need to re-copy it before writing it to disk (direct I/O).
1934bb873b53SIlya Dryomov 	 */
1935bb873b53SIlya Dryomov 	msg->hdr.data_off = cpu_to_le16(req->r_data_offset);
1936bb873b53SIlya Dryomov 
19378cb441c0SIlya Dryomov 	dout("%s req %p msg %p oid %s oid_len %d\n", __func__, req, msg,
19388cb441c0SIlya Dryomov 	     req->r_t.target_oid.name, req->r_t.target_oid.name_len);
19398cb441c0SIlya Dryomov }
19408cb441c0SIlya Dryomov 
19418cb441c0SIlya Dryomov static void encode_request_finish(struct ceph_msg *msg)
19428cb441c0SIlya Dryomov {
19438cb441c0SIlya Dryomov 	void *p = msg->front.iov_base;
1944986e8989SIlya Dryomov 	void *const partial_end = p + msg->front.iov_len;
19458cb441c0SIlya Dryomov 	void *const end = p + msg->front_alloc_len;
19468cb441c0SIlya Dryomov 
19478cb441c0SIlya Dryomov 	if (CEPH_HAVE_FEATURE(msg->con->peer_features, RESEND_ON_SPLIT)) {
19488cb441c0SIlya Dryomov 		/* luminous OSD -- encode features and be done */
1949986e8989SIlya Dryomov 		p = partial_end;
19508cb441c0SIlya Dryomov 		ceph_encode_64(&p, msg->con->peer_features);
19518cb441c0SIlya Dryomov 	} else {
19528cb441c0SIlya Dryomov 		struct {
19538cb441c0SIlya Dryomov 			char spgid[CEPH_ENCODING_START_BLK_LEN +
19548cb441c0SIlya Dryomov 				   CEPH_PGID_ENCODING_LEN + 1];
19558cb441c0SIlya Dryomov 			__le32 hash;
19568cb441c0SIlya Dryomov 			__le32 epoch;
19578cb441c0SIlya Dryomov 			__le32 flags;
19588cb441c0SIlya Dryomov 			char reqid[CEPH_ENCODING_START_BLK_LEN +
19598cb441c0SIlya Dryomov 				   sizeof(struct ceph_osd_reqid)];
19608cb441c0SIlya Dryomov 			char trace[sizeof(struct ceph_blkin_trace_info)];
19618cb441c0SIlya Dryomov 			__le32 client_inc;
19628cb441c0SIlya Dryomov 			struct ceph_timespec mtime;
19638cb441c0SIlya Dryomov 		} __packed head;
19648cb441c0SIlya Dryomov 		struct ceph_pg pgid;
19658cb441c0SIlya Dryomov 		void *oloc, *oid, *tail;
19668cb441c0SIlya Dryomov 		int oloc_len, oid_len, tail_len;
19678cb441c0SIlya Dryomov 		int len;
19688cb441c0SIlya Dryomov 
19698cb441c0SIlya Dryomov 		/*
19708cb441c0SIlya Dryomov 		 * Pre-luminous OSD -- reencode v8 into v4 using @head
19718cb441c0SIlya Dryomov 		 * as a temporary buffer.  Encode the raw PG; the rest
19728cb441c0SIlya Dryomov 		 * is just a matter of moving oloc, oid and tail blobs
19738cb441c0SIlya Dryomov 		 * around.
19748cb441c0SIlya Dryomov 		 */
19758cb441c0SIlya Dryomov 		memcpy(&head, p, sizeof(head));
19768cb441c0SIlya Dryomov 		p += sizeof(head);
19778cb441c0SIlya Dryomov 
19788cb441c0SIlya Dryomov 		oloc = p;
19798cb441c0SIlya Dryomov 		p += CEPH_ENCODING_START_BLK_LEN;
19808cb441c0SIlya Dryomov 		pgid.pool = ceph_decode_64(&p);
19818cb441c0SIlya Dryomov 		p += 4 + 4; /* preferred, key len */
19828cb441c0SIlya Dryomov 		len = ceph_decode_32(&p);
19838cb441c0SIlya Dryomov 		p += len;   /* nspace */
19848cb441c0SIlya Dryomov 		oloc_len = p - oloc;
19858cb441c0SIlya Dryomov 
19868cb441c0SIlya Dryomov 		oid = p;
19878cb441c0SIlya Dryomov 		len = ceph_decode_32(&p);
19888cb441c0SIlya Dryomov 		p += len;
19898cb441c0SIlya Dryomov 		oid_len = p - oid;
19908cb441c0SIlya Dryomov 
19918cb441c0SIlya Dryomov 		tail = p;
1992986e8989SIlya Dryomov 		tail_len = partial_end - p;
19938cb441c0SIlya Dryomov 
19948cb441c0SIlya Dryomov 		p = msg->front.iov_base;
19958cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.client_inc, sizeof(head.client_inc));
19968cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.epoch, sizeof(head.epoch));
19978cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.flags, sizeof(head.flags));
19988cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.mtime, sizeof(head.mtime));
19998cb441c0SIlya Dryomov 
20008cb441c0SIlya Dryomov 		/* reassert_version */
20018cb441c0SIlya Dryomov 		memset(p, 0, sizeof(struct ceph_eversion));
20028cb441c0SIlya Dryomov 		p += sizeof(struct ceph_eversion);
20038cb441c0SIlya Dryomov 
20048cb441c0SIlya Dryomov 		BUG_ON(p >= oloc);
20058cb441c0SIlya Dryomov 		memmove(p, oloc, oloc_len);
20068cb441c0SIlya Dryomov 		p += oloc_len;
20078cb441c0SIlya Dryomov 
20088cb441c0SIlya Dryomov 		pgid.seed = le32_to_cpu(head.hash);
20098cb441c0SIlya Dryomov 		encode_pgid(&p, &pgid); /* raw pg */
20108cb441c0SIlya Dryomov 
20118cb441c0SIlya Dryomov 		BUG_ON(p >= oid);
20128cb441c0SIlya Dryomov 		memmove(p, oid, oid_len);
20138cb441c0SIlya Dryomov 		p += oid_len;
20148cb441c0SIlya Dryomov 
20158cb441c0SIlya Dryomov 		/* tail -- ops, snapid, snapc, retry_attempt */
20168cb441c0SIlya Dryomov 		BUG_ON(p >= tail);
20178cb441c0SIlya Dryomov 		memmove(p, tail, tail_len);
20188cb441c0SIlya Dryomov 		p += tail_len;
20198cb441c0SIlya Dryomov 
20208cb441c0SIlya Dryomov 		msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
20218cb441c0SIlya Dryomov 	}
20228cb441c0SIlya Dryomov 
20238cb441c0SIlya Dryomov 	BUG_ON(p > end);
20248cb441c0SIlya Dryomov 	msg->front.iov_len = p - msg->front.iov_base;
20258cb441c0SIlya Dryomov 	msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
20268cb441c0SIlya Dryomov 
20278cb441c0SIlya Dryomov 	dout("%s msg %p tid %llu %u+%u+%u v%d\n", __func__, msg,
20288cb441c0SIlya Dryomov 	     le64_to_cpu(msg->hdr.tid), le32_to_cpu(msg->hdr.front_len),
20298cb441c0SIlya Dryomov 	     le32_to_cpu(msg->hdr.middle_len), le32_to_cpu(msg->hdr.data_len),
20308cb441c0SIlya Dryomov 	     le16_to_cpu(msg->hdr.version));
2031bb873b53SIlya Dryomov }
2032bb873b53SIlya Dryomov 
2033bb873b53SIlya Dryomov /*
2034bb873b53SIlya Dryomov  * @req has to be assigned a tid and registered.
2035bb873b53SIlya Dryomov  */
2036bb873b53SIlya Dryomov static void send_request(struct ceph_osd_request *req)
2037bb873b53SIlya Dryomov {
2038bb873b53SIlya Dryomov 	struct ceph_osd *osd = req->r_osd;
2039bb873b53SIlya Dryomov 
20405aea3dcdSIlya Dryomov 	verify_osd_locked(osd);
2041bb873b53SIlya Dryomov 	WARN_ON(osd->o_osd != req->r_t.osd);
2042bb873b53SIlya Dryomov 
2043a02a946dSIlya Dryomov 	/* backoff? */
2044a02a946dSIlya Dryomov 	if (should_plug_request(req))
2045a02a946dSIlya Dryomov 		return;
2046a02a946dSIlya Dryomov 
20475aea3dcdSIlya Dryomov 	/*
20485aea3dcdSIlya Dryomov 	 * We may have a previously queued request message hanging
20495aea3dcdSIlya Dryomov 	 * around.  Cancel it to avoid corrupting the msgr.
20505aea3dcdSIlya Dryomov 	 */
20515aea3dcdSIlya Dryomov 	if (req->r_sent)
20525aea3dcdSIlya Dryomov 		ceph_msg_revoke(req->r_request);
20535aea3dcdSIlya Dryomov 
2054bb873b53SIlya Dryomov 	req->r_flags |= CEPH_OSD_FLAG_KNOWN_REDIR;
2055bb873b53SIlya Dryomov 	if (req->r_attempts)
2056bb873b53SIlya Dryomov 		req->r_flags |= CEPH_OSD_FLAG_RETRY;
2057bb873b53SIlya Dryomov 	else
2058bb873b53SIlya Dryomov 		WARN_ON(req->r_flags & CEPH_OSD_FLAG_RETRY);
2059bb873b53SIlya Dryomov 
20608cb441c0SIlya Dryomov 	encode_request_partial(req, req->r_request);
2061bb873b53SIlya Dryomov 
206204c7d789SIlya 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",
2063bb873b53SIlya Dryomov 	     __func__, req, req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed,
2064dc98ff72SIlya Dryomov 	     req->r_t.spgid.pgid.pool, req->r_t.spgid.pgid.seed,
206504c7d789SIlya Dryomov 	     req->r_t.spgid.shard, osd->o_osd, req->r_t.epoch, req->r_flags,
206604c7d789SIlya Dryomov 	     req->r_attempts);
2067bb873b53SIlya Dryomov 
2068bb873b53SIlya Dryomov 	req->r_t.paused = false;
20693d14c5d2SYehuda Sadeh 	req->r_stamp = jiffies;
2070bb873b53SIlya Dryomov 	req->r_attempts++;
20713d14c5d2SYehuda Sadeh 
2072bb873b53SIlya Dryomov 	req->r_sent = osd->o_incarnation;
2073bb873b53SIlya Dryomov 	req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
2074bb873b53SIlya Dryomov 	ceph_con_send(&osd->o_con, ceph_msg_get(req->r_request));
20753d14c5d2SYehuda Sadeh }
20763d14c5d2SYehuda Sadeh 
207742c1b124SIlya Dryomov static void maybe_request_map(struct ceph_osd_client *osdc)
207842c1b124SIlya Dryomov {
207942c1b124SIlya Dryomov 	bool continuous = false;
208042c1b124SIlya Dryomov 
20815aea3dcdSIlya Dryomov 	verify_osdc_locked(osdc);
208242c1b124SIlya Dryomov 	WARN_ON(!osdc->osdmap->epoch);
208342c1b124SIlya Dryomov 
2084b7ec35b3SIlya Dryomov 	if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2085b7ec35b3SIlya Dryomov 	    ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD) ||
2086b7ec35b3SIlya Dryomov 	    ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
208742c1b124SIlya Dryomov 		dout("%s osdc %p continuous\n", __func__, osdc);
208842c1b124SIlya Dryomov 		continuous = true;
208942c1b124SIlya Dryomov 	} else {
209042c1b124SIlya Dryomov 		dout("%s osdc %p onetime\n", __func__, osdc);
209142c1b124SIlya Dryomov 	}
209242c1b124SIlya Dryomov 
209342c1b124SIlya Dryomov 	if (ceph_monc_want_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
209442c1b124SIlya Dryomov 			       osdc->osdmap->epoch + 1, continuous))
209542c1b124SIlya Dryomov 		ceph_monc_renew_subs(&osdc->client->monc);
209642c1b124SIlya Dryomov }
209742c1b124SIlya Dryomov 
2098a1f4020aSJeff Layton static void complete_request(struct ceph_osd_request *req, int err);
20994609245eSIlya Dryomov static void send_map_check(struct ceph_osd_request *req);
21004609245eSIlya Dryomov 
21015aea3dcdSIlya Dryomov static void __submit_request(struct ceph_osd_request *req, bool wrlocked)
21020bbfdfe8SIlya Dryomov {
21035aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
21045aea3dcdSIlya Dryomov 	struct ceph_osd *osd;
21054609245eSIlya Dryomov 	enum calc_target_result ct_res;
21065aea3dcdSIlya Dryomov 	bool need_send = false;
21075aea3dcdSIlya Dryomov 	bool promoted = false;
2108a1f4020aSJeff Layton 	bool need_abort = false;
21090bbfdfe8SIlya Dryomov 
2110b18b9550SIlya Dryomov 	WARN_ON(req->r_tid);
21115aea3dcdSIlya Dryomov 	dout("%s req %p wrlocked %d\n", __func__, req, wrlocked);
21125aea3dcdSIlya Dryomov 
21135aea3dcdSIlya Dryomov again:
21147de030d6SIlya Dryomov 	ct_res = calc_target(osdc, &req->r_t, NULL, false);
21154609245eSIlya Dryomov 	if (ct_res == CALC_TARGET_POOL_DNE && !wrlocked)
21164609245eSIlya Dryomov 		goto promote;
21174609245eSIlya Dryomov 
21185aea3dcdSIlya Dryomov 	osd = lookup_create_osd(osdc, req->r_t.osd, wrlocked);
21195aea3dcdSIlya Dryomov 	if (IS_ERR(osd)) {
21205aea3dcdSIlya Dryomov 		WARN_ON(PTR_ERR(osd) != -EAGAIN || wrlocked);
21215aea3dcdSIlya Dryomov 		goto promote;
21225aea3dcdSIlya Dryomov 	}
21235aea3dcdSIlya Dryomov 
212458eb7932SJeff Layton 	if (osdc->osdmap->epoch < osdc->epoch_barrier) {
212558eb7932SJeff Layton 		dout("req %p epoch %u barrier %u\n", req, osdc->osdmap->epoch,
212658eb7932SJeff Layton 		     osdc->epoch_barrier);
212758eb7932SJeff Layton 		req->r_t.paused = true;
212858eb7932SJeff Layton 		maybe_request_map(osdc);
212958eb7932SJeff Layton 	} else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2130b7ec35b3SIlya Dryomov 		   ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
21315aea3dcdSIlya Dryomov 		dout("req %p pausewr\n", req);
21325aea3dcdSIlya Dryomov 		req->r_t.paused = true;
21335aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
21345aea3dcdSIlya Dryomov 	} else if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
2135b7ec35b3SIlya Dryomov 		   ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
21365aea3dcdSIlya Dryomov 		dout("req %p pauserd\n", req);
21375aea3dcdSIlya Dryomov 		req->r_t.paused = true;
21385aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
21395aea3dcdSIlya Dryomov 	} else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
21405aea3dcdSIlya Dryomov 		   !(req->r_flags & (CEPH_OSD_FLAG_FULL_TRY |
21415aea3dcdSIlya Dryomov 				     CEPH_OSD_FLAG_FULL_FORCE)) &&
2142b7ec35b3SIlya Dryomov 		   (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
21435aea3dcdSIlya Dryomov 		    pool_full(osdc, req->r_t.base_oloc.pool))) {
21445aea3dcdSIlya Dryomov 		dout("req %p full/pool_full\n", req);
21455aea3dcdSIlya Dryomov 		pr_warn_ratelimited("FULL or reached pool quota\n");
21465aea3dcdSIlya Dryomov 		req->r_t.paused = true;
21475aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
2148a1f4020aSJeff Layton 		if (req->r_abort_on_full)
2149a1f4020aSJeff Layton 			need_abort = true;
21505aea3dcdSIlya Dryomov 	} else if (!osd_homeless(osd)) {
21515aea3dcdSIlya Dryomov 		need_send = true;
21520bbfdfe8SIlya Dryomov 	} else {
21535aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
21540bbfdfe8SIlya Dryomov 	}
21550bbfdfe8SIlya Dryomov 
21565aea3dcdSIlya Dryomov 	mutex_lock(&osd->lock);
21575aea3dcdSIlya Dryomov 	/*
21585aea3dcdSIlya Dryomov 	 * Assign the tid atomically with send_request() to protect
21595aea3dcdSIlya Dryomov 	 * multiple writes to the same object from racing with each
21605aea3dcdSIlya Dryomov 	 * other, resulting in out of order ops on the OSDs.
21615aea3dcdSIlya Dryomov 	 */
21625aea3dcdSIlya Dryomov 	req->r_tid = atomic64_inc_return(&osdc->last_tid);
21635aea3dcdSIlya Dryomov 	link_request(osd, req);
21645aea3dcdSIlya Dryomov 	if (need_send)
21655aea3dcdSIlya Dryomov 		send_request(req);
2166a1f4020aSJeff Layton 	else if (need_abort)
2167a1f4020aSJeff Layton 		complete_request(req, -ENOSPC);
21685aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
21695aea3dcdSIlya Dryomov 
21704609245eSIlya Dryomov 	if (ct_res == CALC_TARGET_POOL_DNE)
21714609245eSIlya Dryomov 		send_map_check(req);
21724609245eSIlya Dryomov 
21735aea3dcdSIlya Dryomov 	if (promoted)
21745aea3dcdSIlya Dryomov 		downgrade_write(&osdc->lock);
21755aea3dcdSIlya Dryomov 	return;
21765aea3dcdSIlya Dryomov 
21775aea3dcdSIlya Dryomov promote:
21785aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
21795aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
21805aea3dcdSIlya Dryomov 	wrlocked = true;
21815aea3dcdSIlya Dryomov 	promoted = true;
21825aea3dcdSIlya Dryomov 	goto again;
21830bbfdfe8SIlya Dryomov }
21840bbfdfe8SIlya Dryomov 
21855aea3dcdSIlya Dryomov static void account_request(struct ceph_osd_request *req)
21865aea3dcdSIlya Dryomov {
218754ea0046SIlya Dryomov 	WARN_ON(req->r_flags & (CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK));
2188b18b9550SIlya Dryomov 	WARN_ON(!(req->r_flags & (CEPH_OSD_FLAG_READ | CEPH_OSD_FLAG_WRITE)));
21895aea3dcdSIlya Dryomov 
2190b18b9550SIlya Dryomov 	req->r_flags |= CEPH_OSD_FLAG_ONDISK;
21915aea3dcdSIlya Dryomov 	atomic_inc(&req->r_osdc->num_requests);
21927cc5e38fSIlya Dryomov 
21937cc5e38fSIlya Dryomov 	req->r_start_stamp = jiffies;
21945aea3dcdSIlya Dryomov }
21955aea3dcdSIlya Dryomov 
21965aea3dcdSIlya Dryomov static void submit_request(struct ceph_osd_request *req, bool wrlocked)
21975aea3dcdSIlya Dryomov {
21985aea3dcdSIlya Dryomov 	ceph_osdc_get_request(req);
21995aea3dcdSIlya Dryomov 	account_request(req);
22005aea3dcdSIlya Dryomov 	__submit_request(req, wrlocked);
22015aea3dcdSIlya Dryomov }
22025aea3dcdSIlya Dryomov 
220345ee2c1dSIlya Dryomov static void finish_request(struct ceph_osd_request *req)
22045aea3dcdSIlya Dryomov {
22055aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
22065aea3dcdSIlya Dryomov 
22074609245eSIlya Dryomov 	WARN_ON(lookup_request_mc(&osdc->map_checks, req->r_tid));
220804c7d789SIlya Dryomov 	dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
220904c7d789SIlya Dryomov 
221004c7d789SIlya Dryomov 	if (req->r_osd)
221104c7d789SIlya Dryomov 		unlink_request(req->r_osd, req);
22125aea3dcdSIlya Dryomov 	atomic_dec(&osdc->num_requests);
22135aea3dcdSIlya Dryomov 
22145aea3dcdSIlya Dryomov 	/*
22155aea3dcdSIlya Dryomov 	 * If an OSD has failed or returned and a request has been sent
22165aea3dcdSIlya Dryomov 	 * twice, it's possible to get a reply and end up here while the
22175aea3dcdSIlya Dryomov 	 * request message is queued for delivery.  We will ignore the
22185aea3dcdSIlya Dryomov 	 * reply, so not a big deal, but better to try and catch it.
22195aea3dcdSIlya Dryomov 	 */
22205aea3dcdSIlya Dryomov 	ceph_msg_revoke(req->r_request);
22215aea3dcdSIlya Dryomov 	ceph_msg_revoke_incoming(req->r_reply);
22225aea3dcdSIlya Dryomov }
22235aea3dcdSIlya Dryomov 
2224fe5da05eSIlya Dryomov static void __complete_request(struct ceph_osd_request *req)
2225fe5da05eSIlya Dryomov {
2226b18b9550SIlya Dryomov 	if (req->r_callback) {
2227b18b9550SIlya Dryomov 		dout("%s req %p tid %llu cb %pf result %d\n", __func__, req,
2228b18b9550SIlya Dryomov 		     req->r_tid, req->r_callback, req->r_result);
2229fe5da05eSIlya Dryomov 		req->r_callback(req);
2230b18b9550SIlya Dryomov 	}
2231fe5da05eSIlya Dryomov }
2232fe5da05eSIlya Dryomov 
22334609245eSIlya Dryomov /*
2234b18b9550SIlya Dryomov  * This is open-coded in handle_reply().
22354609245eSIlya Dryomov  */
22364609245eSIlya Dryomov static void complete_request(struct ceph_osd_request *req, int err)
22374609245eSIlya Dryomov {
22384609245eSIlya Dryomov 	dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
22394609245eSIlya Dryomov 
22404609245eSIlya Dryomov 	req->r_result = err;
224145ee2c1dSIlya Dryomov 	finish_request(req);
22424609245eSIlya Dryomov 	__complete_request(req);
2243b18b9550SIlya Dryomov 	complete_all(&req->r_completion);
22444609245eSIlya Dryomov 	ceph_osdc_put_request(req);
22454609245eSIlya Dryomov }
22464609245eSIlya Dryomov 
22474609245eSIlya Dryomov static void cancel_map_check(struct ceph_osd_request *req)
22484609245eSIlya Dryomov {
22494609245eSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
22504609245eSIlya Dryomov 	struct ceph_osd_request *lookup_req;
22514609245eSIlya Dryomov 
22524609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
22534609245eSIlya Dryomov 
22544609245eSIlya Dryomov 	lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
22554609245eSIlya Dryomov 	if (!lookup_req)
22564609245eSIlya Dryomov 		return;
22574609245eSIlya Dryomov 
22584609245eSIlya Dryomov 	WARN_ON(lookup_req != req);
22594609245eSIlya Dryomov 	erase_request_mc(&osdc->map_checks, req);
22604609245eSIlya Dryomov 	ceph_osdc_put_request(req);
22614609245eSIlya Dryomov }
22624609245eSIlya Dryomov 
22635aea3dcdSIlya Dryomov static void cancel_request(struct ceph_osd_request *req)
22645aea3dcdSIlya Dryomov {
22655aea3dcdSIlya Dryomov 	dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
22665aea3dcdSIlya Dryomov 
22674609245eSIlya Dryomov 	cancel_map_check(req);
226845ee2c1dSIlya Dryomov 	finish_request(req);
2269b18b9550SIlya Dryomov 	complete_all(&req->r_completion);
2270c297eb42SIlya Dryomov 	ceph_osdc_put_request(req);
22715aea3dcdSIlya Dryomov }
22725aea3dcdSIlya Dryomov 
22737cc5e38fSIlya Dryomov static void abort_request(struct ceph_osd_request *req, int err)
22747cc5e38fSIlya Dryomov {
22757cc5e38fSIlya Dryomov 	dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
22767cc5e38fSIlya Dryomov 
22777cc5e38fSIlya Dryomov 	cancel_map_check(req);
22787cc5e38fSIlya Dryomov 	complete_request(req, err);
22797cc5e38fSIlya Dryomov }
22807cc5e38fSIlya Dryomov 
228158eb7932SJeff Layton static void update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
228258eb7932SJeff Layton {
228358eb7932SJeff Layton 	if (likely(eb > osdc->epoch_barrier)) {
228458eb7932SJeff Layton 		dout("updating epoch_barrier from %u to %u\n",
228558eb7932SJeff Layton 				osdc->epoch_barrier, eb);
228658eb7932SJeff Layton 		osdc->epoch_barrier = eb;
228758eb7932SJeff Layton 		/* Request map if we're not to the barrier yet */
228858eb7932SJeff Layton 		if (eb > osdc->osdmap->epoch)
228958eb7932SJeff Layton 			maybe_request_map(osdc);
229058eb7932SJeff Layton 	}
229158eb7932SJeff Layton }
229258eb7932SJeff Layton 
229358eb7932SJeff Layton void ceph_osdc_update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
229458eb7932SJeff Layton {
229558eb7932SJeff Layton 	down_read(&osdc->lock);
229658eb7932SJeff Layton 	if (unlikely(eb > osdc->epoch_barrier)) {
229758eb7932SJeff Layton 		up_read(&osdc->lock);
229858eb7932SJeff Layton 		down_write(&osdc->lock);
229958eb7932SJeff Layton 		update_epoch_barrier(osdc, eb);
230058eb7932SJeff Layton 		up_write(&osdc->lock);
230158eb7932SJeff Layton 	} else {
230258eb7932SJeff Layton 		up_read(&osdc->lock);
230358eb7932SJeff Layton 	}
230458eb7932SJeff Layton }
230558eb7932SJeff Layton EXPORT_SYMBOL(ceph_osdc_update_epoch_barrier);
230658eb7932SJeff Layton 
2307fc36d0a4SJeff Layton /*
2308fc36d0a4SJeff Layton  * Drop all pending requests that are stalled waiting on a full condition to
230958eb7932SJeff Layton  * clear, and complete them with ENOSPC as the return code. Set the
231058eb7932SJeff Layton  * osdc->epoch_barrier to the latest map epoch that we've seen if any were
231158eb7932SJeff Layton  * cancelled.
2312fc36d0a4SJeff Layton  */
2313fc36d0a4SJeff Layton static void ceph_osdc_abort_on_full(struct ceph_osd_client *osdc)
2314fc36d0a4SJeff Layton {
2315fc36d0a4SJeff Layton 	struct rb_node *n;
231658eb7932SJeff Layton 	bool victims = false;
2317fc36d0a4SJeff Layton 
2318fc36d0a4SJeff Layton 	dout("enter abort_on_full\n");
2319fc36d0a4SJeff Layton 
2320fc36d0a4SJeff Layton 	if (!ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) && !have_pool_full(osdc))
2321fc36d0a4SJeff Layton 		goto out;
2322fc36d0a4SJeff Layton 
232358eb7932SJeff Layton 	/* Scan list and see if there is anything to abort */
232458eb7932SJeff Layton 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
232558eb7932SJeff Layton 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
232658eb7932SJeff Layton 		struct rb_node *m;
232758eb7932SJeff Layton 
232858eb7932SJeff Layton 		m = rb_first(&osd->o_requests);
232958eb7932SJeff Layton 		while (m) {
233058eb7932SJeff Layton 			struct ceph_osd_request *req = rb_entry(m,
233158eb7932SJeff Layton 					struct ceph_osd_request, r_node);
233258eb7932SJeff Layton 			m = rb_next(m);
233358eb7932SJeff Layton 
233458eb7932SJeff Layton 			if (req->r_abort_on_full) {
233558eb7932SJeff Layton 				victims = true;
233658eb7932SJeff Layton 				break;
233758eb7932SJeff Layton 			}
233858eb7932SJeff Layton 		}
233958eb7932SJeff Layton 		if (victims)
234058eb7932SJeff Layton 			break;
234158eb7932SJeff Layton 	}
234258eb7932SJeff Layton 
234358eb7932SJeff Layton 	if (!victims)
234458eb7932SJeff Layton 		goto out;
234558eb7932SJeff Layton 
234658eb7932SJeff Layton 	/*
234758eb7932SJeff Layton 	 * Update the barrier to current epoch if it's behind that point,
234858eb7932SJeff Layton 	 * since we know we have some calls to be aborted in the tree.
234958eb7932SJeff Layton 	 */
235058eb7932SJeff Layton 	update_epoch_barrier(osdc, osdc->osdmap->epoch);
235158eb7932SJeff Layton 
2352fc36d0a4SJeff Layton 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
2353fc36d0a4SJeff Layton 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
2354fc36d0a4SJeff Layton 		struct rb_node *m;
2355fc36d0a4SJeff Layton 
2356fc36d0a4SJeff Layton 		m = rb_first(&osd->o_requests);
2357fc36d0a4SJeff Layton 		while (m) {
2358fc36d0a4SJeff Layton 			struct ceph_osd_request *req = rb_entry(m,
2359fc36d0a4SJeff Layton 					struct ceph_osd_request, r_node);
2360fc36d0a4SJeff Layton 			m = rb_next(m);
2361fc36d0a4SJeff Layton 
2362fc36d0a4SJeff Layton 			if (req->r_abort_on_full &&
2363fc36d0a4SJeff Layton 			    (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2364fc36d0a4SJeff Layton 			     pool_full(osdc, req->r_t.target_oloc.pool)))
2365fc36d0a4SJeff Layton 				abort_request(req, -ENOSPC);
2366fc36d0a4SJeff Layton 		}
2367fc36d0a4SJeff Layton 	}
2368fc36d0a4SJeff Layton out:
236958eb7932SJeff Layton 	dout("return abort_on_full barrier=%u\n", osdc->epoch_barrier);
2370fc36d0a4SJeff Layton }
2371fc36d0a4SJeff Layton 
23724609245eSIlya Dryomov static void check_pool_dne(struct ceph_osd_request *req)
23734609245eSIlya Dryomov {
23744609245eSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
23754609245eSIlya Dryomov 	struct ceph_osdmap *map = osdc->osdmap;
23764609245eSIlya Dryomov 
23774609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
23784609245eSIlya Dryomov 	WARN_ON(!map->epoch);
23794609245eSIlya Dryomov 
23804609245eSIlya Dryomov 	if (req->r_attempts) {
23814609245eSIlya Dryomov 		/*
23824609245eSIlya Dryomov 		 * We sent a request earlier, which means that
23834609245eSIlya Dryomov 		 * previously the pool existed, and now it does not
23844609245eSIlya Dryomov 		 * (i.e., it was deleted).
23854609245eSIlya Dryomov 		 */
23864609245eSIlya Dryomov 		req->r_map_dne_bound = map->epoch;
23874609245eSIlya Dryomov 		dout("%s req %p tid %llu pool disappeared\n", __func__, req,
23884609245eSIlya Dryomov 		     req->r_tid);
23894609245eSIlya Dryomov 	} else {
23904609245eSIlya Dryomov 		dout("%s req %p tid %llu map_dne_bound %u have %u\n", __func__,
23914609245eSIlya Dryomov 		     req, req->r_tid, req->r_map_dne_bound, map->epoch);
23924609245eSIlya Dryomov 	}
23934609245eSIlya Dryomov 
23944609245eSIlya Dryomov 	if (req->r_map_dne_bound) {
23954609245eSIlya Dryomov 		if (map->epoch >= req->r_map_dne_bound) {
23964609245eSIlya Dryomov 			/* we had a new enough map */
23974609245eSIlya Dryomov 			pr_info_ratelimited("tid %llu pool does not exist\n",
23984609245eSIlya Dryomov 					    req->r_tid);
23994609245eSIlya Dryomov 			complete_request(req, -ENOENT);
24004609245eSIlya Dryomov 		}
24014609245eSIlya Dryomov 	} else {
24024609245eSIlya Dryomov 		send_map_check(req);
24034609245eSIlya Dryomov 	}
24044609245eSIlya Dryomov }
24054609245eSIlya Dryomov 
24064609245eSIlya Dryomov static void map_check_cb(struct ceph_mon_generic_request *greq)
24074609245eSIlya Dryomov {
24084609245eSIlya Dryomov 	struct ceph_osd_client *osdc = &greq->monc->client->osdc;
24094609245eSIlya Dryomov 	struct ceph_osd_request *req;
24104609245eSIlya Dryomov 	u64 tid = greq->private_data;
24114609245eSIlya Dryomov 
24124609245eSIlya Dryomov 	WARN_ON(greq->result || !greq->u.newest);
24134609245eSIlya Dryomov 
24144609245eSIlya Dryomov 	down_write(&osdc->lock);
24154609245eSIlya Dryomov 	req = lookup_request_mc(&osdc->map_checks, tid);
24164609245eSIlya Dryomov 	if (!req) {
24174609245eSIlya Dryomov 		dout("%s tid %llu dne\n", __func__, tid);
24184609245eSIlya Dryomov 		goto out_unlock;
24194609245eSIlya Dryomov 	}
24204609245eSIlya Dryomov 
24214609245eSIlya Dryomov 	dout("%s req %p tid %llu map_dne_bound %u newest %llu\n", __func__,
24224609245eSIlya Dryomov 	     req, req->r_tid, req->r_map_dne_bound, greq->u.newest);
24234609245eSIlya Dryomov 	if (!req->r_map_dne_bound)
24244609245eSIlya Dryomov 		req->r_map_dne_bound = greq->u.newest;
24254609245eSIlya Dryomov 	erase_request_mc(&osdc->map_checks, req);
24264609245eSIlya Dryomov 	check_pool_dne(req);
24274609245eSIlya Dryomov 
24284609245eSIlya Dryomov 	ceph_osdc_put_request(req);
24294609245eSIlya Dryomov out_unlock:
24304609245eSIlya Dryomov 	up_write(&osdc->lock);
24314609245eSIlya Dryomov }
24324609245eSIlya Dryomov 
24334609245eSIlya Dryomov static void send_map_check(struct ceph_osd_request *req)
24344609245eSIlya Dryomov {
24354609245eSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
24364609245eSIlya Dryomov 	struct ceph_osd_request *lookup_req;
24374609245eSIlya Dryomov 	int ret;
24384609245eSIlya Dryomov 
24394609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
24404609245eSIlya Dryomov 
24414609245eSIlya Dryomov 	lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
24424609245eSIlya Dryomov 	if (lookup_req) {
24434609245eSIlya Dryomov 		WARN_ON(lookup_req != req);
24444609245eSIlya Dryomov 		return;
24454609245eSIlya Dryomov 	}
24464609245eSIlya Dryomov 
24474609245eSIlya Dryomov 	ceph_osdc_get_request(req);
24484609245eSIlya Dryomov 	insert_request_mc(&osdc->map_checks, req);
24494609245eSIlya Dryomov 	ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
24504609245eSIlya Dryomov 					  map_check_cb, req->r_tid);
24514609245eSIlya Dryomov 	WARN_ON(ret);
24524609245eSIlya Dryomov }
24534609245eSIlya Dryomov 
24540bbfdfe8SIlya Dryomov /*
2455922dab61SIlya Dryomov  * lingering requests, watch/notify v2 infrastructure
2456922dab61SIlya Dryomov  */
2457922dab61SIlya Dryomov static void linger_release(struct kref *kref)
2458922dab61SIlya Dryomov {
2459922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq =
2460922dab61SIlya Dryomov 	    container_of(kref, struct ceph_osd_linger_request, kref);
2461922dab61SIlya Dryomov 
2462922dab61SIlya Dryomov 	dout("%s lreq %p reg_req %p ping_req %p\n", __func__, lreq,
2463922dab61SIlya Dryomov 	     lreq->reg_req, lreq->ping_req);
2464922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&lreq->node));
2465922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&lreq->osdc_node));
24664609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&lreq->mc_node));
2467922dab61SIlya Dryomov 	WARN_ON(!list_empty(&lreq->scan_item));
2468b07d3c4bSIlya Dryomov 	WARN_ON(!list_empty(&lreq->pending_lworks));
2469922dab61SIlya Dryomov 	WARN_ON(lreq->osd);
2470922dab61SIlya Dryomov 
2471922dab61SIlya Dryomov 	if (lreq->reg_req)
2472922dab61SIlya Dryomov 		ceph_osdc_put_request(lreq->reg_req);
2473922dab61SIlya Dryomov 	if (lreq->ping_req)
2474922dab61SIlya Dryomov 		ceph_osdc_put_request(lreq->ping_req);
2475922dab61SIlya Dryomov 	target_destroy(&lreq->t);
2476922dab61SIlya Dryomov 	kfree(lreq);
2477922dab61SIlya Dryomov }
2478922dab61SIlya Dryomov 
2479922dab61SIlya Dryomov static void linger_put(struct ceph_osd_linger_request *lreq)
2480922dab61SIlya Dryomov {
2481922dab61SIlya Dryomov 	if (lreq)
2482922dab61SIlya Dryomov 		kref_put(&lreq->kref, linger_release);
2483922dab61SIlya Dryomov }
2484922dab61SIlya Dryomov 
2485922dab61SIlya Dryomov static struct ceph_osd_linger_request *
2486922dab61SIlya Dryomov linger_get(struct ceph_osd_linger_request *lreq)
2487922dab61SIlya Dryomov {
2488922dab61SIlya Dryomov 	kref_get(&lreq->kref);
2489922dab61SIlya Dryomov 	return lreq;
2490922dab61SIlya Dryomov }
2491922dab61SIlya Dryomov 
2492922dab61SIlya Dryomov static struct ceph_osd_linger_request *
2493922dab61SIlya Dryomov linger_alloc(struct ceph_osd_client *osdc)
2494922dab61SIlya Dryomov {
2495922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
2496922dab61SIlya Dryomov 
2497922dab61SIlya Dryomov 	lreq = kzalloc(sizeof(*lreq), GFP_NOIO);
2498922dab61SIlya Dryomov 	if (!lreq)
2499922dab61SIlya Dryomov 		return NULL;
2500922dab61SIlya Dryomov 
2501922dab61SIlya Dryomov 	kref_init(&lreq->kref);
2502922dab61SIlya Dryomov 	mutex_init(&lreq->lock);
2503922dab61SIlya Dryomov 	RB_CLEAR_NODE(&lreq->node);
2504922dab61SIlya Dryomov 	RB_CLEAR_NODE(&lreq->osdc_node);
25054609245eSIlya Dryomov 	RB_CLEAR_NODE(&lreq->mc_node);
2506922dab61SIlya Dryomov 	INIT_LIST_HEAD(&lreq->scan_item);
2507b07d3c4bSIlya Dryomov 	INIT_LIST_HEAD(&lreq->pending_lworks);
2508922dab61SIlya Dryomov 	init_completion(&lreq->reg_commit_wait);
250919079203SIlya Dryomov 	init_completion(&lreq->notify_finish_wait);
2510922dab61SIlya Dryomov 
2511922dab61SIlya Dryomov 	lreq->osdc = osdc;
2512922dab61SIlya Dryomov 	target_init(&lreq->t);
2513922dab61SIlya Dryomov 
2514922dab61SIlya Dryomov 	dout("%s lreq %p\n", __func__, lreq);
2515922dab61SIlya Dryomov 	return lreq;
2516922dab61SIlya Dryomov }
2517922dab61SIlya Dryomov 
2518922dab61SIlya Dryomov DEFINE_RB_INSDEL_FUNCS(linger, struct ceph_osd_linger_request, linger_id, node)
2519922dab61SIlya Dryomov DEFINE_RB_FUNCS(linger_osdc, struct ceph_osd_linger_request, linger_id, osdc_node)
25204609245eSIlya Dryomov DEFINE_RB_FUNCS(linger_mc, struct ceph_osd_linger_request, linger_id, mc_node)
2521922dab61SIlya Dryomov 
2522922dab61SIlya Dryomov /*
2523922dab61SIlya Dryomov  * Create linger request <-> OSD session relation.
2524922dab61SIlya Dryomov  *
2525922dab61SIlya Dryomov  * @lreq has to be registered, @osd may be homeless.
2526922dab61SIlya Dryomov  */
2527922dab61SIlya Dryomov static void link_linger(struct ceph_osd *osd,
2528922dab61SIlya Dryomov 			struct ceph_osd_linger_request *lreq)
2529922dab61SIlya Dryomov {
2530922dab61SIlya Dryomov 	verify_osd_locked(osd);
2531922dab61SIlya Dryomov 	WARN_ON(!lreq->linger_id || lreq->osd);
2532922dab61SIlya Dryomov 	dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2533922dab61SIlya Dryomov 	     osd->o_osd, lreq, lreq->linger_id);
2534922dab61SIlya Dryomov 
2535922dab61SIlya Dryomov 	if (!osd_homeless(osd))
2536922dab61SIlya Dryomov 		__remove_osd_from_lru(osd);
2537922dab61SIlya Dryomov 	else
2538922dab61SIlya Dryomov 		atomic_inc(&osd->o_osdc->num_homeless);
2539922dab61SIlya Dryomov 
2540922dab61SIlya Dryomov 	get_osd(osd);
2541922dab61SIlya Dryomov 	insert_linger(&osd->o_linger_requests, lreq);
2542922dab61SIlya Dryomov 	lreq->osd = osd;
2543922dab61SIlya Dryomov }
2544922dab61SIlya Dryomov 
2545922dab61SIlya Dryomov static void unlink_linger(struct ceph_osd *osd,
2546922dab61SIlya Dryomov 			  struct ceph_osd_linger_request *lreq)
2547922dab61SIlya Dryomov {
2548922dab61SIlya Dryomov 	verify_osd_locked(osd);
2549922dab61SIlya Dryomov 	WARN_ON(lreq->osd != osd);
2550922dab61SIlya Dryomov 	dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2551922dab61SIlya Dryomov 	     osd->o_osd, lreq, lreq->linger_id);
2552922dab61SIlya Dryomov 
2553922dab61SIlya Dryomov 	lreq->osd = NULL;
2554922dab61SIlya Dryomov 	erase_linger(&osd->o_linger_requests, lreq);
2555922dab61SIlya Dryomov 	put_osd(osd);
2556922dab61SIlya Dryomov 
2557922dab61SIlya Dryomov 	if (!osd_homeless(osd))
2558922dab61SIlya Dryomov 		maybe_move_osd_to_lru(osd);
2559922dab61SIlya Dryomov 	else
2560922dab61SIlya Dryomov 		atomic_dec(&osd->o_osdc->num_homeless);
2561922dab61SIlya Dryomov }
2562922dab61SIlya Dryomov 
2563922dab61SIlya Dryomov static bool __linger_registered(struct ceph_osd_linger_request *lreq)
2564922dab61SIlya Dryomov {
2565922dab61SIlya Dryomov 	verify_osdc_locked(lreq->osdc);
2566922dab61SIlya Dryomov 
2567922dab61SIlya Dryomov 	return !RB_EMPTY_NODE(&lreq->osdc_node);
2568922dab61SIlya Dryomov }
2569922dab61SIlya Dryomov 
2570922dab61SIlya Dryomov static bool linger_registered(struct ceph_osd_linger_request *lreq)
2571922dab61SIlya Dryomov {
2572922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2573922dab61SIlya Dryomov 	bool registered;
2574922dab61SIlya Dryomov 
2575922dab61SIlya Dryomov 	down_read(&osdc->lock);
2576922dab61SIlya Dryomov 	registered = __linger_registered(lreq);
2577922dab61SIlya Dryomov 	up_read(&osdc->lock);
2578922dab61SIlya Dryomov 
2579922dab61SIlya Dryomov 	return registered;
2580922dab61SIlya Dryomov }
2581922dab61SIlya Dryomov 
2582922dab61SIlya Dryomov static void linger_register(struct ceph_osd_linger_request *lreq)
2583922dab61SIlya Dryomov {
2584922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2585922dab61SIlya Dryomov 
2586922dab61SIlya Dryomov 	verify_osdc_wrlocked(osdc);
2587922dab61SIlya Dryomov 	WARN_ON(lreq->linger_id);
2588922dab61SIlya Dryomov 
2589922dab61SIlya Dryomov 	linger_get(lreq);
2590922dab61SIlya Dryomov 	lreq->linger_id = ++osdc->last_linger_id;
2591922dab61SIlya Dryomov 	insert_linger_osdc(&osdc->linger_requests, lreq);
2592922dab61SIlya Dryomov }
2593922dab61SIlya Dryomov 
2594922dab61SIlya Dryomov static void linger_unregister(struct ceph_osd_linger_request *lreq)
2595922dab61SIlya Dryomov {
2596922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2597922dab61SIlya Dryomov 
2598922dab61SIlya Dryomov 	verify_osdc_wrlocked(osdc);
2599922dab61SIlya Dryomov 
2600922dab61SIlya Dryomov 	erase_linger_osdc(&osdc->linger_requests, lreq);
2601922dab61SIlya Dryomov 	linger_put(lreq);
2602922dab61SIlya Dryomov }
2603922dab61SIlya Dryomov 
2604922dab61SIlya Dryomov static void cancel_linger_request(struct ceph_osd_request *req)
2605922dab61SIlya Dryomov {
2606922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
2607922dab61SIlya Dryomov 
2608922dab61SIlya Dryomov 	WARN_ON(!req->r_linger);
2609922dab61SIlya Dryomov 	cancel_request(req);
2610922dab61SIlya Dryomov 	linger_put(lreq);
2611922dab61SIlya Dryomov }
2612922dab61SIlya Dryomov 
2613922dab61SIlya Dryomov struct linger_work {
2614922dab61SIlya Dryomov 	struct work_struct work;
2615922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
2616b07d3c4bSIlya Dryomov 	struct list_head pending_item;
2617b07d3c4bSIlya Dryomov 	unsigned long queued_stamp;
2618922dab61SIlya Dryomov 
2619922dab61SIlya Dryomov 	union {
2620922dab61SIlya Dryomov 		struct {
2621922dab61SIlya Dryomov 			u64 notify_id;
2622922dab61SIlya Dryomov 			u64 notifier_id;
2623922dab61SIlya Dryomov 			void *payload; /* points into @msg front */
2624922dab61SIlya Dryomov 			size_t payload_len;
2625922dab61SIlya Dryomov 
2626922dab61SIlya Dryomov 			struct ceph_msg *msg; /* for ceph_msg_put() */
2627922dab61SIlya Dryomov 		} notify;
2628922dab61SIlya Dryomov 		struct {
2629922dab61SIlya Dryomov 			int err;
2630922dab61SIlya Dryomov 		} error;
2631922dab61SIlya Dryomov 	};
2632922dab61SIlya Dryomov };
2633922dab61SIlya Dryomov 
2634922dab61SIlya Dryomov static struct linger_work *lwork_alloc(struct ceph_osd_linger_request *lreq,
2635922dab61SIlya Dryomov 				       work_func_t workfn)
2636922dab61SIlya Dryomov {
2637922dab61SIlya Dryomov 	struct linger_work *lwork;
2638922dab61SIlya Dryomov 
2639922dab61SIlya Dryomov 	lwork = kzalloc(sizeof(*lwork), GFP_NOIO);
2640922dab61SIlya Dryomov 	if (!lwork)
2641922dab61SIlya Dryomov 		return NULL;
2642922dab61SIlya Dryomov 
2643922dab61SIlya Dryomov 	INIT_WORK(&lwork->work, workfn);
2644b07d3c4bSIlya Dryomov 	INIT_LIST_HEAD(&lwork->pending_item);
2645922dab61SIlya Dryomov 	lwork->lreq = linger_get(lreq);
2646922dab61SIlya Dryomov 
2647922dab61SIlya Dryomov 	return lwork;
2648922dab61SIlya Dryomov }
2649922dab61SIlya Dryomov 
2650922dab61SIlya Dryomov static void lwork_free(struct linger_work *lwork)
2651922dab61SIlya Dryomov {
2652922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2653922dab61SIlya Dryomov 
2654b07d3c4bSIlya Dryomov 	mutex_lock(&lreq->lock);
2655b07d3c4bSIlya Dryomov 	list_del(&lwork->pending_item);
2656b07d3c4bSIlya Dryomov 	mutex_unlock(&lreq->lock);
2657b07d3c4bSIlya Dryomov 
2658922dab61SIlya Dryomov 	linger_put(lreq);
2659922dab61SIlya Dryomov 	kfree(lwork);
2660922dab61SIlya Dryomov }
2661922dab61SIlya Dryomov 
2662922dab61SIlya Dryomov static void lwork_queue(struct linger_work *lwork)
2663922dab61SIlya Dryomov {
2664922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2665922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2666922dab61SIlya Dryomov 
2667922dab61SIlya Dryomov 	verify_lreq_locked(lreq);
2668b07d3c4bSIlya Dryomov 	WARN_ON(!list_empty(&lwork->pending_item));
2669b07d3c4bSIlya Dryomov 
2670b07d3c4bSIlya Dryomov 	lwork->queued_stamp = jiffies;
2671b07d3c4bSIlya Dryomov 	list_add_tail(&lwork->pending_item, &lreq->pending_lworks);
2672922dab61SIlya Dryomov 	queue_work(osdc->notify_wq, &lwork->work);
2673922dab61SIlya Dryomov }
2674922dab61SIlya Dryomov 
2675922dab61SIlya Dryomov static void do_watch_notify(struct work_struct *w)
2676922dab61SIlya Dryomov {
2677922dab61SIlya Dryomov 	struct linger_work *lwork = container_of(w, struct linger_work, work);
2678922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2679922dab61SIlya Dryomov 
2680922dab61SIlya Dryomov 	if (!linger_registered(lreq)) {
2681922dab61SIlya Dryomov 		dout("%s lreq %p not registered\n", __func__, lreq);
2682922dab61SIlya Dryomov 		goto out;
2683922dab61SIlya Dryomov 	}
2684922dab61SIlya Dryomov 
268519079203SIlya Dryomov 	WARN_ON(!lreq->is_watch);
2686922dab61SIlya Dryomov 	dout("%s lreq %p notify_id %llu notifier_id %llu payload_len %zu\n",
2687922dab61SIlya Dryomov 	     __func__, lreq, lwork->notify.notify_id, lwork->notify.notifier_id,
2688922dab61SIlya Dryomov 	     lwork->notify.payload_len);
2689922dab61SIlya Dryomov 	lreq->wcb(lreq->data, lwork->notify.notify_id, lreq->linger_id,
2690922dab61SIlya Dryomov 		  lwork->notify.notifier_id, lwork->notify.payload,
2691922dab61SIlya Dryomov 		  lwork->notify.payload_len);
2692922dab61SIlya Dryomov 
2693922dab61SIlya Dryomov out:
2694922dab61SIlya Dryomov 	ceph_msg_put(lwork->notify.msg);
2695922dab61SIlya Dryomov 	lwork_free(lwork);
2696922dab61SIlya Dryomov }
2697922dab61SIlya Dryomov 
2698922dab61SIlya Dryomov static void do_watch_error(struct work_struct *w)
2699922dab61SIlya Dryomov {
2700922dab61SIlya Dryomov 	struct linger_work *lwork = container_of(w, struct linger_work, work);
2701922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2702922dab61SIlya Dryomov 
2703922dab61SIlya Dryomov 	if (!linger_registered(lreq)) {
2704922dab61SIlya Dryomov 		dout("%s lreq %p not registered\n", __func__, lreq);
2705922dab61SIlya Dryomov 		goto out;
2706922dab61SIlya Dryomov 	}
2707922dab61SIlya Dryomov 
2708922dab61SIlya Dryomov 	dout("%s lreq %p err %d\n", __func__, lreq, lwork->error.err);
2709922dab61SIlya Dryomov 	lreq->errcb(lreq->data, lreq->linger_id, lwork->error.err);
2710922dab61SIlya Dryomov 
2711922dab61SIlya Dryomov out:
2712922dab61SIlya Dryomov 	lwork_free(lwork);
2713922dab61SIlya Dryomov }
2714922dab61SIlya Dryomov 
2715922dab61SIlya Dryomov static void queue_watch_error(struct ceph_osd_linger_request *lreq)
2716922dab61SIlya Dryomov {
2717922dab61SIlya Dryomov 	struct linger_work *lwork;
2718922dab61SIlya Dryomov 
2719922dab61SIlya Dryomov 	lwork = lwork_alloc(lreq, do_watch_error);
2720922dab61SIlya Dryomov 	if (!lwork) {
2721922dab61SIlya Dryomov 		pr_err("failed to allocate error-lwork\n");
2722922dab61SIlya Dryomov 		return;
2723922dab61SIlya Dryomov 	}
2724922dab61SIlya Dryomov 
2725922dab61SIlya Dryomov 	lwork->error.err = lreq->last_error;
2726922dab61SIlya Dryomov 	lwork_queue(lwork);
2727922dab61SIlya Dryomov }
2728922dab61SIlya Dryomov 
2729922dab61SIlya Dryomov static void linger_reg_commit_complete(struct ceph_osd_linger_request *lreq,
2730922dab61SIlya Dryomov 				       int result)
2731922dab61SIlya Dryomov {
2732922dab61SIlya Dryomov 	if (!completion_done(&lreq->reg_commit_wait)) {
2733922dab61SIlya Dryomov 		lreq->reg_commit_error = (result <= 0 ? result : 0);
2734922dab61SIlya Dryomov 		complete_all(&lreq->reg_commit_wait);
2735922dab61SIlya Dryomov 	}
2736922dab61SIlya Dryomov }
2737922dab61SIlya Dryomov 
2738922dab61SIlya Dryomov static void linger_commit_cb(struct ceph_osd_request *req)
2739922dab61SIlya Dryomov {
2740922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
2741922dab61SIlya Dryomov 
2742922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
2743922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu result %d\n", __func__, lreq,
2744922dab61SIlya Dryomov 	     lreq->linger_id, req->r_result);
2745922dab61SIlya Dryomov 	linger_reg_commit_complete(lreq, req->r_result);
2746922dab61SIlya Dryomov 	lreq->committed = true;
2747922dab61SIlya Dryomov 
274819079203SIlya Dryomov 	if (!lreq->is_watch) {
274919079203SIlya Dryomov 		struct ceph_osd_data *osd_data =
275019079203SIlya Dryomov 		    osd_req_op_data(req, 0, notify, response_data);
275119079203SIlya Dryomov 		void *p = page_address(osd_data->pages[0]);
275219079203SIlya Dryomov 
275319079203SIlya Dryomov 		WARN_ON(req->r_ops[0].op != CEPH_OSD_OP_NOTIFY ||
275419079203SIlya Dryomov 			osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
275519079203SIlya Dryomov 
275619079203SIlya Dryomov 		/* make note of the notify_id */
275719079203SIlya Dryomov 		if (req->r_ops[0].outdata_len >= sizeof(u64)) {
275819079203SIlya Dryomov 			lreq->notify_id = ceph_decode_64(&p);
275919079203SIlya Dryomov 			dout("lreq %p notify_id %llu\n", lreq,
276019079203SIlya Dryomov 			     lreq->notify_id);
276119079203SIlya Dryomov 		} else {
276219079203SIlya Dryomov 			dout("lreq %p no notify_id\n", lreq);
276319079203SIlya Dryomov 		}
276419079203SIlya Dryomov 	}
276519079203SIlya Dryomov 
2766922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
2767922dab61SIlya Dryomov 	linger_put(lreq);
2768922dab61SIlya Dryomov }
2769922dab61SIlya Dryomov 
2770922dab61SIlya Dryomov static int normalize_watch_error(int err)
2771922dab61SIlya Dryomov {
2772922dab61SIlya Dryomov 	/*
2773922dab61SIlya Dryomov 	 * Translate ENOENT -> ENOTCONN so that a delete->disconnection
2774922dab61SIlya Dryomov 	 * notification and a failure to reconnect because we raced with
2775922dab61SIlya Dryomov 	 * the delete appear the same to the user.
2776922dab61SIlya Dryomov 	 */
2777922dab61SIlya Dryomov 	if (err == -ENOENT)
2778922dab61SIlya Dryomov 		err = -ENOTCONN;
2779922dab61SIlya Dryomov 
2780922dab61SIlya Dryomov 	return err;
2781922dab61SIlya Dryomov }
2782922dab61SIlya Dryomov 
2783922dab61SIlya Dryomov static void linger_reconnect_cb(struct ceph_osd_request *req)
2784922dab61SIlya Dryomov {
2785922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
2786922dab61SIlya Dryomov 
2787922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
2788922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu result %d last_error %d\n", __func__,
2789922dab61SIlya Dryomov 	     lreq, lreq->linger_id, req->r_result, lreq->last_error);
2790922dab61SIlya Dryomov 	if (req->r_result < 0) {
2791922dab61SIlya Dryomov 		if (!lreq->last_error) {
2792922dab61SIlya Dryomov 			lreq->last_error = normalize_watch_error(req->r_result);
2793922dab61SIlya Dryomov 			queue_watch_error(lreq);
2794922dab61SIlya Dryomov 		}
2795922dab61SIlya Dryomov 	}
2796922dab61SIlya Dryomov 
2797922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
2798922dab61SIlya Dryomov 	linger_put(lreq);
2799922dab61SIlya Dryomov }
2800922dab61SIlya Dryomov 
2801922dab61SIlya Dryomov static void send_linger(struct ceph_osd_linger_request *lreq)
2802922dab61SIlya Dryomov {
2803922dab61SIlya Dryomov 	struct ceph_osd_request *req = lreq->reg_req;
2804922dab61SIlya Dryomov 	struct ceph_osd_req_op *op = &req->r_ops[0];
2805922dab61SIlya Dryomov 
2806922dab61SIlya Dryomov 	verify_osdc_wrlocked(req->r_osdc);
2807922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2808922dab61SIlya Dryomov 
2809922dab61SIlya Dryomov 	if (req->r_osd)
2810922dab61SIlya Dryomov 		cancel_linger_request(req);
2811922dab61SIlya Dryomov 
2812922dab61SIlya Dryomov 	request_reinit(req);
2813922dab61SIlya Dryomov 	ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
2814922dab61SIlya Dryomov 	ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
2815922dab61SIlya Dryomov 	req->r_flags = lreq->t.flags;
2816922dab61SIlya Dryomov 	req->r_mtime = lreq->mtime;
2817922dab61SIlya Dryomov 
2818922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
281919079203SIlya Dryomov 	if (lreq->is_watch && lreq->committed) {
2820922dab61SIlya Dryomov 		WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2821922dab61SIlya Dryomov 			op->watch.cookie != lreq->linger_id);
2822922dab61SIlya Dryomov 		op->watch.op = CEPH_OSD_WATCH_OP_RECONNECT;
2823922dab61SIlya Dryomov 		op->watch.gen = ++lreq->register_gen;
2824922dab61SIlya Dryomov 		dout("lreq %p reconnect register_gen %u\n", lreq,
2825922dab61SIlya Dryomov 		     op->watch.gen);
2826922dab61SIlya Dryomov 		req->r_callback = linger_reconnect_cb;
2827922dab61SIlya Dryomov 	} else {
282819079203SIlya Dryomov 		if (!lreq->is_watch)
282919079203SIlya Dryomov 			lreq->notify_id = 0;
283019079203SIlya Dryomov 		else
2831922dab61SIlya Dryomov 			WARN_ON(op->watch.op != CEPH_OSD_WATCH_OP_WATCH);
2832922dab61SIlya Dryomov 		dout("lreq %p register\n", lreq);
2833922dab61SIlya Dryomov 		req->r_callback = linger_commit_cb;
2834922dab61SIlya Dryomov 	}
2835922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
2836922dab61SIlya Dryomov 
2837922dab61SIlya Dryomov 	req->r_priv = linger_get(lreq);
2838922dab61SIlya Dryomov 	req->r_linger = true;
2839922dab61SIlya Dryomov 
2840922dab61SIlya Dryomov 	submit_request(req, true);
2841922dab61SIlya Dryomov }
2842922dab61SIlya Dryomov 
2843922dab61SIlya Dryomov static void linger_ping_cb(struct ceph_osd_request *req)
2844922dab61SIlya Dryomov {
2845922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
2846922dab61SIlya Dryomov 
2847922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
2848922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu result %d ping_sent %lu last_error %d\n",
2849922dab61SIlya Dryomov 	     __func__, lreq, lreq->linger_id, req->r_result, lreq->ping_sent,
2850922dab61SIlya Dryomov 	     lreq->last_error);
2851922dab61SIlya Dryomov 	if (lreq->register_gen == req->r_ops[0].watch.gen) {
2852b07d3c4bSIlya Dryomov 		if (!req->r_result) {
2853b07d3c4bSIlya Dryomov 			lreq->watch_valid_thru = lreq->ping_sent;
2854b07d3c4bSIlya Dryomov 		} else if (!lreq->last_error) {
2855922dab61SIlya Dryomov 			lreq->last_error = normalize_watch_error(req->r_result);
2856922dab61SIlya Dryomov 			queue_watch_error(lreq);
2857922dab61SIlya Dryomov 		}
2858922dab61SIlya Dryomov 	} else {
2859922dab61SIlya Dryomov 		dout("lreq %p register_gen %u ignoring old pong %u\n", lreq,
2860922dab61SIlya Dryomov 		     lreq->register_gen, req->r_ops[0].watch.gen);
2861922dab61SIlya Dryomov 	}
2862922dab61SIlya Dryomov 
2863922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
2864922dab61SIlya Dryomov 	linger_put(lreq);
2865922dab61SIlya Dryomov }
2866922dab61SIlya Dryomov 
2867922dab61SIlya Dryomov static void send_linger_ping(struct ceph_osd_linger_request *lreq)
2868922dab61SIlya Dryomov {
2869922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2870922dab61SIlya Dryomov 	struct ceph_osd_request *req = lreq->ping_req;
2871922dab61SIlya Dryomov 	struct ceph_osd_req_op *op = &req->r_ops[0];
2872922dab61SIlya Dryomov 
2873b7ec35b3SIlya Dryomov 	if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
2874922dab61SIlya Dryomov 		dout("%s PAUSERD\n", __func__);
2875922dab61SIlya Dryomov 		return;
2876922dab61SIlya Dryomov 	}
2877922dab61SIlya Dryomov 
2878922dab61SIlya Dryomov 	lreq->ping_sent = jiffies;
2879922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu ping_sent %lu register_gen %u\n",
2880922dab61SIlya Dryomov 	     __func__, lreq, lreq->linger_id, lreq->ping_sent,
2881922dab61SIlya Dryomov 	     lreq->register_gen);
2882922dab61SIlya Dryomov 
2883922dab61SIlya Dryomov 	if (req->r_osd)
2884922dab61SIlya Dryomov 		cancel_linger_request(req);
2885922dab61SIlya Dryomov 
2886922dab61SIlya Dryomov 	request_reinit(req);
2887922dab61SIlya Dryomov 	target_copy(&req->r_t, &lreq->t);
2888922dab61SIlya Dryomov 
2889922dab61SIlya Dryomov 	WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2890922dab61SIlya Dryomov 		op->watch.cookie != lreq->linger_id ||
2891922dab61SIlya Dryomov 		op->watch.op != CEPH_OSD_WATCH_OP_PING);
2892922dab61SIlya Dryomov 	op->watch.gen = lreq->register_gen;
2893922dab61SIlya Dryomov 	req->r_callback = linger_ping_cb;
2894922dab61SIlya Dryomov 	req->r_priv = linger_get(lreq);
2895922dab61SIlya Dryomov 	req->r_linger = true;
2896922dab61SIlya Dryomov 
2897922dab61SIlya Dryomov 	ceph_osdc_get_request(req);
2898922dab61SIlya Dryomov 	account_request(req);
2899922dab61SIlya Dryomov 	req->r_tid = atomic64_inc_return(&osdc->last_tid);
2900922dab61SIlya Dryomov 	link_request(lreq->osd, req);
2901922dab61SIlya Dryomov 	send_request(req);
2902922dab61SIlya Dryomov }
2903922dab61SIlya Dryomov 
2904922dab61SIlya Dryomov static void linger_submit(struct ceph_osd_linger_request *lreq)
2905922dab61SIlya Dryomov {
2906922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2907922dab61SIlya Dryomov 	struct ceph_osd *osd;
2908922dab61SIlya Dryomov 
29097de030d6SIlya Dryomov 	calc_target(osdc, &lreq->t, NULL, false);
2910922dab61SIlya Dryomov 	osd = lookup_create_osd(osdc, lreq->t.osd, true);
2911922dab61SIlya Dryomov 	link_linger(osd, lreq);
2912922dab61SIlya Dryomov 
2913922dab61SIlya Dryomov 	send_linger(lreq);
2914922dab61SIlya Dryomov }
2915922dab61SIlya Dryomov 
29164609245eSIlya Dryomov static void cancel_linger_map_check(struct ceph_osd_linger_request *lreq)
29174609245eSIlya Dryomov {
29184609245eSIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
29194609245eSIlya Dryomov 	struct ceph_osd_linger_request *lookup_lreq;
29204609245eSIlya Dryomov 
29214609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
29224609245eSIlya Dryomov 
29234609245eSIlya Dryomov 	lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
29244609245eSIlya Dryomov 				       lreq->linger_id);
29254609245eSIlya Dryomov 	if (!lookup_lreq)
29264609245eSIlya Dryomov 		return;
29274609245eSIlya Dryomov 
29284609245eSIlya Dryomov 	WARN_ON(lookup_lreq != lreq);
29294609245eSIlya Dryomov 	erase_linger_mc(&osdc->linger_map_checks, lreq);
29304609245eSIlya Dryomov 	linger_put(lreq);
29314609245eSIlya Dryomov }
29324609245eSIlya Dryomov 
2933922dab61SIlya Dryomov /*
2934922dab61SIlya Dryomov  * @lreq has to be both registered and linked.
2935922dab61SIlya Dryomov  */
2936922dab61SIlya Dryomov static void __linger_cancel(struct ceph_osd_linger_request *lreq)
2937922dab61SIlya Dryomov {
293819079203SIlya Dryomov 	if (lreq->is_watch && lreq->ping_req->r_osd)
2939922dab61SIlya Dryomov 		cancel_linger_request(lreq->ping_req);
2940922dab61SIlya Dryomov 	if (lreq->reg_req->r_osd)
2941922dab61SIlya Dryomov 		cancel_linger_request(lreq->reg_req);
29424609245eSIlya Dryomov 	cancel_linger_map_check(lreq);
2943922dab61SIlya Dryomov 	unlink_linger(lreq->osd, lreq);
2944922dab61SIlya Dryomov 	linger_unregister(lreq);
2945922dab61SIlya Dryomov }
2946922dab61SIlya Dryomov 
2947922dab61SIlya Dryomov static void linger_cancel(struct ceph_osd_linger_request *lreq)
2948922dab61SIlya Dryomov {
2949922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2950922dab61SIlya Dryomov 
2951922dab61SIlya Dryomov 	down_write(&osdc->lock);
2952922dab61SIlya Dryomov 	if (__linger_registered(lreq))
2953922dab61SIlya Dryomov 		__linger_cancel(lreq);
2954922dab61SIlya Dryomov 	up_write(&osdc->lock);
2955922dab61SIlya Dryomov }
2956922dab61SIlya Dryomov 
29574609245eSIlya Dryomov static void send_linger_map_check(struct ceph_osd_linger_request *lreq);
29584609245eSIlya Dryomov 
29594609245eSIlya Dryomov static void check_linger_pool_dne(struct ceph_osd_linger_request *lreq)
29604609245eSIlya Dryomov {
29614609245eSIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
29624609245eSIlya Dryomov 	struct ceph_osdmap *map = osdc->osdmap;
29634609245eSIlya Dryomov 
29644609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
29654609245eSIlya Dryomov 	WARN_ON(!map->epoch);
29664609245eSIlya Dryomov 
29674609245eSIlya Dryomov 	if (lreq->register_gen) {
29684609245eSIlya Dryomov 		lreq->map_dne_bound = map->epoch;
29694609245eSIlya Dryomov 		dout("%s lreq %p linger_id %llu pool disappeared\n", __func__,
29704609245eSIlya Dryomov 		     lreq, lreq->linger_id);
29714609245eSIlya Dryomov 	} else {
29724609245eSIlya Dryomov 		dout("%s lreq %p linger_id %llu map_dne_bound %u have %u\n",
29734609245eSIlya Dryomov 		     __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
29744609245eSIlya Dryomov 		     map->epoch);
29754609245eSIlya Dryomov 	}
29764609245eSIlya Dryomov 
29774609245eSIlya Dryomov 	if (lreq->map_dne_bound) {
29784609245eSIlya Dryomov 		if (map->epoch >= lreq->map_dne_bound) {
29794609245eSIlya Dryomov 			/* we had a new enough map */
29804609245eSIlya Dryomov 			pr_info("linger_id %llu pool does not exist\n",
29814609245eSIlya Dryomov 				lreq->linger_id);
29824609245eSIlya Dryomov 			linger_reg_commit_complete(lreq, -ENOENT);
29834609245eSIlya Dryomov 			__linger_cancel(lreq);
29844609245eSIlya Dryomov 		}
29854609245eSIlya Dryomov 	} else {
29864609245eSIlya Dryomov 		send_linger_map_check(lreq);
29874609245eSIlya Dryomov 	}
29884609245eSIlya Dryomov }
29894609245eSIlya Dryomov 
29904609245eSIlya Dryomov static void linger_map_check_cb(struct ceph_mon_generic_request *greq)
29914609245eSIlya Dryomov {
29924609245eSIlya Dryomov 	struct ceph_osd_client *osdc = &greq->monc->client->osdc;
29934609245eSIlya Dryomov 	struct ceph_osd_linger_request *lreq;
29944609245eSIlya Dryomov 	u64 linger_id = greq->private_data;
29954609245eSIlya Dryomov 
29964609245eSIlya Dryomov 	WARN_ON(greq->result || !greq->u.newest);
29974609245eSIlya Dryomov 
29984609245eSIlya Dryomov 	down_write(&osdc->lock);
29994609245eSIlya Dryomov 	lreq = lookup_linger_mc(&osdc->linger_map_checks, linger_id);
30004609245eSIlya Dryomov 	if (!lreq) {
30014609245eSIlya Dryomov 		dout("%s linger_id %llu dne\n", __func__, linger_id);
30024609245eSIlya Dryomov 		goto out_unlock;
30034609245eSIlya Dryomov 	}
30044609245eSIlya Dryomov 
30054609245eSIlya Dryomov 	dout("%s lreq %p linger_id %llu map_dne_bound %u newest %llu\n",
30064609245eSIlya Dryomov 	     __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
30074609245eSIlya Dryomov 	     greq->u.newest);
30084609245eSIlya Dryomov 	if (!lreq->map_dne_bound)
30094609245eSIlya Dryomov 		lreq->map_dne_bound = greq->u.newest;
30104609245eSIlya Dryomov 	erase_linger_mc(&osdc->linger_map_checks, lreq);
30114609245eSIlya Dryomov 	check_linger_pool_dne(lreq);
30124609245eSIlya Dryomov 
30134609245eSIlya Dryomov 	linger_put(lreq);
30144609245eSIlya Dryomov out_unlock:
30154609245eSIlya Dryomov 	up_write(&osdc->lock);
30164609245eSIlya Dryomov }
30174609245eSIlya Dryomov 
30184609245eSIlya Dryomov static void send_linger_map_check(struct ceph_osd_linger_request *lreq)
30194609245eSIlya Dryomov {
30204609245eSIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
30214609245eSIlya Dryomov 	struct ceph_osd_linger_request *lookup_lreq;
30224609245eSIlya Dryomov 	int ret;
30234609245eSIlya Dryomov 
30244609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
30254609245eSIlya Dryomov 
30264609245eSIlya Dryomov 	lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
30274609245eSIlya Dryomov 				       lreq->linger_id);
30284609245eSIlya Dryomov 	if (lookup_lreq) {
30294609245eSIlya Dryomov 		WARN_ON(lookup_lreq != lreq);
30304609245eSIlya Dryomov 		return;
30314609245eSIlya Dryomov 	}
30324609245eSIlya Dryomov 
30334609245eSIlya Dryomov 	linger_get(lreq);
30344609245eSIlya Dryomov 	insert_linger_mc(&osdc->linger_map_checks, lreq);
30354609245eSIlya Dryomov 	ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
30364609245eSIlya Dryomov 					  linger_map_check_cb, lreq->linger_id);
30374609245eSIlya Dryomov 	WARN_ON(ret);
30384609245eSIlya Dryomov }
30394609245eSIlya Dryomov 
3040922dab61SIlya Dryomov static int linger_reg_commit_wait(struct ceph_osd_linger_request *lreq)
3041922dab61SIlya Dryomov {
3042922dab61SIlya Dryomov 	int ret;
3043922dab61SIlya Dryomov 
3044922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3045922dab61SIlya Dryomov 	ret = wait_for_completion_interruptible(&lreq->reg_commit_wait);
3046922dab61SIlya Dryomov 	return ret ?: lreq->reg_commit_error;
3047922dab61SIlya Dryomov }
3048922dab61SIlya Dryomov 
304919079203SIlya Dryomov static int linger_notify_finish_wait(struct ceph_osd_linger_request *lreq)
305019079203SIlya Dryomov {
305119079203SIlya Dryomov 	int ret;
305219079203SIlya Dryomov 
305319079203SIlya Dryomov 	dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
305419079203SIlya Dryomov 	ret = wait_for_completion_interruptible(&lreq->notify_finish_wait);
305519079203SIlya Dryomov 	return ret ?: lreq->notify_finish_error;
305619079203SIlya Dryomov }
305719079203SIlya Dryomov 
3058922dab61SIlya Dryomov /*
3059fbca9635SIlya Dryomov  * Timeout callback, called every N seconds.  When 1 or more OSD
3060fbca9635SIlya Dryomov  * requests has been active for more than N seconds, we send a keepalive
3061fbca9635SIlya Dryomov  * (tag + timestamp) to its OSD to ensure any communications channel
3062fbca9635SIlya Dryomov  * reset is detected.
30633d14c5d2SYehuda Sadeh  */
30643d14c5d2SYehuda Sadeh static void handle_timeout(struct work_struct *work)
30653d14c5d2SYehuda Sadeh {
30663d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc =
30673d14c5d2SYehuda Sadeh 		container_of(work, struct ceph_osd_client, timeout_work.work);
3068a319bf56SIlya Dryomov 	struct ceph_options *opts = osdc->client->options;
30695aea3dcdSIlya Dryomov 	unsigned long cutoff = jiffies - opts->osd_keepalive_timeout;
30707cc5e38fSIlya Dryomov 	unsigned long expiry_cutoff = jiffies - opts->osd_request_timeout;
30715aea3dcdSIlya Dryomov 	LIST_HEAD(slow_osds);
30725aea3dcdSIlya Dryomov 	struct rb_node *n, *p;
30733d14c5d2SYehuda Sadeh 
30745aea3dcdSIlya Dryomov 	dout("%s osdc %p\n", __func__, osdc);
30755aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
30763d14c5d2SYehuda Sadeh 
30773d14c5d2SYehuda Sadeh 	/*
30783d14c5d2SYehuda Sadeh 	 * ping osds that are a bit slow.  this ensures that if there
30793d14c5d2SYehuda Sadeh 	 * is a break in the TCP connection we will notice, and reopen
30803d14c5d2SYehuda Sadeh 	 * a connection with that osd (from the fault callback).
30813d14c5d2SYehuda Sadeh 	 */
30825aea3dcdSIlya Dryomov 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
30835aea3dcdSIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
30845aea3dcdSIlya Dryomov 		bool found = false;
30853d14c5d2SYehuda Sadeh 
30867cc5e38fSIlya Dryomov 		for (p = rb_first(&osd->o_requests); p; ) {
30875aea3dcdSIlya Dryomov 			struct ceph_osd_request *req =
30885aea3dcdSIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
30895aea3dcdSIlya Dryomov 
30907cc5e38fSIlya Dryomov 			p = rb_next(p); /* abort_request() */
30917cc5e38fSIlya Dryomov 
30925aea3dcdSIlya Dryomov 			if (time_before(req->r_stamp, cutoff)) {
30935aea3dcdSIlya Dryomov 				dout(" req %p tid %llu on osd%d is laggy\n",
30945aea3dcdSIlya Dryomov 				     req, req->r_tid, osd->o_osd);
30955aea3dcdSIlya Dryomov 				found = true;
30965aea3dcdSIlya Dryomov 			}
30977cc5e38fSIlya Dryomov 			if (opts->osd_request_timeout &&
30987cc5e38fSIlya Dryomov 			    time_before(req->r_start_stamp, expiry_cutoff)) {
30997cc5e38fSIlya Dryomov 				pr_err_ratelimited("tid %llu on osd%d timeout\n",
31007cc5e38fSIlya Dryomov 				       req->r_tid, osd->o_osd);
31017cc5e38fSIlya Dryomov 				abort_request(req, -ETIMEDOUT);
31027cc5e38fSIlya Dryomov 			}
31035aea3dcdSIlya Dryomov 		}
3104922dab61SIlya Dryomov 		for (p = rb_first(&osd->o_linger_requests); p; p = rb_next(p)) {
3105922dab61SIlya Dryomov 			struct ceph_osd_linger_request *lreq =
3106922dab61SIlya Dryomov 			    rb_entry(p, struct ceph_osd_linger_request, node);
3107922dab61SIlya Dryomov 
3108922dab61SIlya Dryomov 			dout(" lreq %p linger_id %llu is served by osd%d\n",
3109922dab61SIlya Dryomov 			     lreq, lreq->linger_id, osd->o_osd);
3110922dab61SIlya Dryomov 			found = true;
3111922dab61SIlya Dryomov 
3112922dab61SIlya Dryomov 			mutex_lock(&lreq->lock);
311319079203SIlya Dryomov 			if (lreq->is_watch && lreq->committed && !lreq->last_error)
3114922dab61SIlya Dryomov 				send_linger_ping(lreq);
3115922dab61SIlya Dryomov 			mutex_unlock(&lreq->lock);
3116922dab61SIlya Dryomov 		}
31175aea3dcdSIlya Dryomov 
31185aea3dcdSIlya Dryomov 		if (found)
31193d14c5d2SYehuda Sadeh 			list_move_tail(&osd->o_keepalive_item, &slow_osds);
31203d14c5d2SYehuda Sadeh 	}
31215aea3dcdSIlya Dryomov 
31227cc5e38fSIlya Dryomov 	if (opts->osd_request_timeout) {
31237cc5e38fSIlya Dryomov 		for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
31247cc5e38fSIlya Dryomov 			struct ceph_osd_request *req =
31257cc5e38fSIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
31267cc5e38fSIlya Dryomov 
31277cc5e38fSIlya Dryomov 			p = rb_next(p); /* abort_request() */
31287cc5e38fSIlya Dryomov 
31297cc5e38fSIlya Dryomov 			if (time_before(req->r_start_stamp, expiry_cutoff)) {
31307cc5e38fSIlya Dryomov 				pr_err_ratelimited("tid %llu on osd%d timeout\n",
31317cc5e38fSIlya Dryomov 				       req->r_tid, osdc->homeless_osd.o_osd);
31327cc5e38fSIlya Dryomov 				abort_request(req, -ETIMEDOUT);
31337cc5e38fSIlya Dryomov 			}
31347cc5e38fSIlya Dryomov 		}
31357cc5e38fSIlya Dryomov 	}
31367cc5e38fSIlya Dryomov 
31375aea3dcdSIlya Dryomov 	if (atomic_read(&osdc->num_homeless) || !list_empty(&slow_osds))
31385aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
31395aea3dcdSIlya Dryomov 
31403d14c5d2SYehuda Sadeh 	while (!list_empty(&slow_osds)) {
31415aea3dcdSIlya Dryomov 		struct ceph_osd *osd = list_first_entry(&slow_osds,
31425aea3dcdSIlya Dryomov 							struct ceph_osd,
31433d14c5d2SYehuda Sadeh 							o_keepalive_item);
31443d14c5d2SYehuda Sadeh 		list_del_init(&osd->o_keepalive_item);
31453d14c5d2SYehuda Sadeh 		ceph_con_keepalive(&osd->o_con);
31463d14c5d2SYehuda Sadeh 	}
31473d14c5d2SYehuda Sadeh 
31485aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
3149fbca9635SIlya Dryomov 	schedule_delayed_work(&osdc->timeout_work,
3150fbca9635SIlya Dryomov 			      osdc->client->options->osd_keepalive_timeout);
31513d14c5d2SYehuda Sadeh }
31523d14c5d2SYehuda Sadeh 
31533d14c5d2SYehuda Sadeh static void handle_osds_timeout(struct work_struct *work)
31543d14c5d2SYehuda Sadeh {
31553d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc =
31563d14c5d2SYehuda Sadeh 		container_of(work, struct ceph_osd_client,
31573d14c5d2SYehuda Sadeh 			     osds_timeout_work.work);
3158a319bf56SIlya Dryomov 	unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
315942a2c09fSIlya Dryomov 	struct ceph_osd *osd, *nosd;
31603d14c5d2SYehuda Sadeh 
316142a2c09fSIlya Dryomov 	dout("%s osdc %p\n", __func__, osdc);
31625aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
316342a2c09fSIlya Dryomov 	list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
316442a2c09fSIlya Dryomov 		if (time_before(jiffies, osd->lru_ttl))
316542a2c09fSIlya Dryomov 			break;
316642a2c09fSIlya Dryomov 
31675aea3dcdSIlya Dryomov 		WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
3168922dab61SIlya Dryomov 		WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
31695aea3dcdSIlya Dryomov 		close_osd(osd);
317042a2c09fSIlya Dryomov 	}
317142a2c09fSIlya Dryomov 
31725aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
31733d14c5d2SYehuda Sadeh 	schedule_delayed_work(&osdc->osds_timeout_work,
31743d14c5d2SYehuda Sadeh 			      round_jiffies_relative(delay));
31753d14c5d2SYehuda Sadeh }
31763d14c5d2SYehuda Sadeh 
3177205ee118SIlya Dryomov static int ceph_oloc_decode(void **p, void *end,
3178205ee118SIlya Dryomov 			    struct ceph_object_locator *oloc)
3179205ee118SIlya Dryomov {
3180205ee118SIlya Dryomov 	u8 struct_v, struct_cv;
3181205ee118SIlya Dryomov 	u32 len;
3182205ee118SIlya Dryomov 	void *struct_end;
3183205ee118SIlya Dryomov 	int ret = 0;
3184205ee118SIlya Dryomov 
3185205ee118SIlya Dryomov 	ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3186205ee118SIlya Dryomov 	struct_v = ceph_decode_8(p);
3187205ee118SIlya Dryomov 	struct_cv = ceph_decode_8(p);
3188205ee118SIlya Dryomov 	if (struct_v < 3) {
3189205ee118SIlya Dryomov 		pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
3190205ee118SIlya Dryomov 			struct_v, struct_cv);
3191205ee118SIlya Dryomov 		goto e_inval;
3192205ee118SIlya Dryomov 	}
3193205ee118SIlya Dryomov 	if (struct_cv > 6) {
3194205ee118SIlya Dryomov 		pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
3195205ee118SIlya Dryomov 			struct_v, struct_cv);
3196205ee118SIlya Dryomov 		goto e_inval;
3197205ee118SIlya Dryomov 	}
3198205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3199205ee118SIlya Dryomov 	ceph_decode_need(p, end, len, e_inval);
3200205ee118SIlya Dryomov 	struct_end = *p + len;
3201205ee118SIlya Dryomov 
3202205ee118SIlya Dryomov 	oloc->pool = ceph_decode_64(p);
3203205ee118SIlya Dryomov 	*p += 4; /* skip preferred */
3204205ee118SIlya Dryomov 
3205205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3206205ee118SIlya Dryomov 	if (len > 0) {
3207205ee118SIlya Dryomov 		pr_warn("ceph_object_locator::key is set\n");
3208205ee118SIlya Dryomov 		goto e_inval;
3209205ee118SIlya Dryomov 	}
3210205ee118SIlya Dryomov 
3211205ee118SIlya Dryomov 	if (struct_v >= 5) {
3212cd08e0a2SYan, Zheng 		bool changed = false;
3213cd08e0a2SYan, Zheng 
3214205ee118SIlya Dryomov 		len = ceph_decode_32(p);
3215205ee118SIlya Dryomov 		if (len > 0) {
321630c156d9SYan, Zheng 			ceph_decode_need(p, end, len, e_inval);
3217cd08e0a2SYan, Zheng 			if (!oloc->pool_ns ||
3218cd08e0a2SYan, Zheng 			    ceph_compare_string(oloc->pool_ns, *p, len))
3219cd08e0a2SYan, Zheng 				changed = true;
322030c156d9SYan, Zheng 			*p += len;
3221cd08e0a2SYan, Zheng 		} else {
3222cd08e0a2SYan, Zheng 			if (oloc->pool_ns)
3223cd08e0a2SYan, Zheng 				changed = true;
3224cd08e0a2SYan, Zheng 		}
3225cd08e0a2SYan, Zheng 		if (changed) {
3226cd08e0a2SYan, Zheng 			/* redirect changes namespace */
3227cd08e0a2SYan, Zheng 			pr_warn("ceph_object_locator::nspace is changed\n");
3228cd08e0a2SYan, Zheng 			goto e_inval;
3229205ee118SIlya Dryomov 		}
3230205ee118SIlya Dryomov 	}
3231205ee118SIlya Dryomov 
3232205ee118SIlya Dryomov 	if (struct_v >= 6) {
3233205ee118SIlya Dryomov 		s64 hash = ceph_decode_64(p);
3234205ee118SIlya Dryomov 		if (hash != -1) {
3235205ee118SIlya Dryomov 			pr_warn("ceph_object_locator::hash is set\n");
3236205ee118SIlya Dryomov 			goto e_inval;
3237205ee118SIlya Dryomov 		}
3238205ee118SIlya Dryomov 	}
3239205ee118SIlya Dryomov 
3240205ee118SIlya Dryomov 	/* skip the rest */
3241205ee118SIlya Dryomov 	*p = struct_end;
3242205ee118SIlya Dryomov out:
3243205ee118SIlya Dryomov 	return ret;
3244205ee118SIlya Dryomov 
3245205ee118SIlya Dryomov e_inval:
3246205ee118SIlya Dryomov 	ret = -EINVAL;
3247205ee118SIlya Dryomov 	goto out;
3248205ee118SIlya Dryomov }
3249205ee118SIlya Dryomov 
3250205ee118SIlya Dryomov static int ceph_redirect_decode(void **p, void *end,
3251205ee118SIlya Dryomov 				struct ceph_request_redirect *redir)
3252205ee118SIlya Dryomov {
3253205ee118SIlya Dryomov 	u8 struct_v, struct_cv;
3254205ee118SIlya Dryomov 	u32 len;
3255205ee118SIlya Dryomov 	void *struct_end;
3256205ee118SIlya Dryomov 	int ret;
3257205ee118SIlya Dryomov 
3258205ee118SIlya Dryomov 	ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3259205ee118SIlya Dryomov 	struct_v = ceph_decode_8(p);
3260205ee118SIlya Dryomov 	struct_cv = ceph_decode_8(p);
3261205ee118SIlya Dryomov 	if (struct_cv > 1) {
3262205ee118SIlya Dryomov 		pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
3263205ee118SIlya Dryomov 			struct_v, struct_cv);
3264205ee118SIlya Dryomov 		goto e_inval;
3265205ee118SIlya Dryomov 	}
3266205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3267205ee118SIlya Dryomov 	ceph_decode_need(p, end, len, e_inval);
3268205ee118SIlya Dryomov 	struct_end = *p + len;
3269205ee118SIlya Dryomov 
3270205ee118SIlya Dryomov 	ret = ceph_oloc_decode(p, end, &redir->oloc);
3271205ee118SIlya Dryomov 	if (ret)
3272205ee118SIlya Dryomov 		goto out;
3273205ee118SIlya Dryomov 
3274205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3275205ee118SIlya Dryomov 	if (len > 0) {
3276205ee118SIlya Dryomov 		pr_warn("ceph_request_redirect::object_name is set\n");
3277205ee118SIlya Dryomov 		goto e_inval;
3278205ee118SIlya Dryomov 	}
3279205ee118SIlya Dryomov 
3280205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3281205ee118SIlya Dryomov 	*p += len; /* skip osd_instructions */
3282205ee118SIlya Dryomov 
3283205ee118SIlya Dryomov 	/* skip the rest */
3284205ee118SIlya Dryomov 	*p = struct_end;
3285205ee118SIlya Dryomov out:
3286205ee118SIlya Dryomov 	return ret;
3287205ee118SIlya Dryomov 
3288205ee118SIlya Dryomov e_inval:
3289205ee118SIlya Dryomov 	ret = -EINVAL;
3290205ee118SIlya Dryomov 	goto out;
3291205ee118SIlya Dryomov }
3292205ee118SIlya Dryomov 
3293fe5da05eSIlya Dryomov struct MOSDOpReply {
3294fe5da05eSIlya Dryomov 	struct ceph_pg pgid;
3295fe5da05eSIlya Dryomov 	u64 flags;
3296fe5da05eSIlya Dryomov 	int result;
3297fe5da05eSIlya Dryomov 	u32 epoch;
3298fe5da05eSIlya Dryomov 	int num_ops;
3299fe5da05eSIlya Dryomov 	u32 outdata_len[CEPH_OSD_MAX_OPS];
3300fe5da05eSIlya Dryomov 	s32 rval[CEPH_OSD_MAX_OPS];
3301fe5da05eSIlya Dryomov 	int retry_attempt;
3302fe5da05eSIlya Dryomov 	struct ceph_eversion replay_version;
3303fe5da05eSIlya Dryomov 	u64 user_version;
3304fe5da05eSIlya Dryomov 	struct ceph_request_redirect redirect;
3305fe5da05eSIlya Dryomov };
330625845472SSage Weil 
3307fe5da05eSIlya Dryomov static int decode_MOSDOpReply(const struct ceph_msg *msg, struct MOSDOpReply *m)
33083d14c5d2SYehuda Sadeh {
3309fe5da05eSIlya Dryomov 	void *p = msg->front.iov_base;
3310fe5da05eSIlya Dryomov 	void *const end = p + msg->front.iov_len;
3311fe5da05eSIlya Dryomov 	u16 version = le16_to_cpu(msg->hdr.version);
3312fe5da05eSIlya Dryomov 	struct ceph_eversion bad_replay_version;
3313b0b31a8fSIlya Dryomov 	u8 decode_redir;
3314fe5da05eSIlya Dryomov 	u32 len;
3315fe5da05eSIlya Dryomov 	int ret;
3316fe5da05eSIlya Dryomov 	int i;
33173d14c5d2SYehuda Sadeh 
3318fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, len, e_inval);
3319fe5da05eSIlya Dryomov 	ceph_decode_need(&p, end, len, e_inval);
3320fe5da05eSIlya Dryomov 	p += len; /* skip oid */
33211b83bef2SSage Weil 
3322fe5da05eSIlya Dryomov 	ret = ceph_decode_pgid(&p, end, &m->pgid);
3323fe5da05eSIlya Dryomov 	if (ret)
3324fe5da05eSIlya Dryomov 		return ret;
33251b83bef2SSage Weil 
3326fe5da05eSIlya Dryomov 	ceph_decode_64_safe(&p, end, m->flags, e_inval);
3327fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->result, e_inval);
3328fe5da05eSIlya Dryomov 	ceph_decode_need(&p, end, sizeof(bad_replay_version), e_inval);
3329fe5da05eSIlya Dryomov 	memcpy(&bad_replay_version, p, sizeof(bad_replay_version));
3330fe5da05eSIlya Dryomov 	p += sizeof(bad_replay_version);
3331fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->epoch, e_inval);
33321b83bef2SSage Weil 
3333fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->num_ops, e_inval);
3334fe5da05eSIlya Dryomov 	if (m->num_ops > ARRAY_SIZE(m->outdata_len))
3335fe5da05eSIlya Dryomov 		goto e_inval;
33361b83bef2SSage Weil 
3337fe5da05eSIlya Dryomov 	ceph_decode_need(&p, end, m->num_ops * sizeof(struct ceph_osd_op),
3338fe5da05eSIlya Dryomov 			 e_inval);
3339fe5da05eSIlya Dryomov 	for (i = 0; i < m->num_ops; i++) {
33401b83bef2SSage Weil 		struct ceph_osd_op *op = p;
33411b83bef2SSage Weil 
3342fe5da05eSIlya Dryomov 		m->outdata_len[i] = le32_to_cpu(op->payload_len);
33431b83bef2SSage Weil 		p += sizeof(*op);
33441b83bef2SSage Weil 	}
3345fe5da05eSIlya Dryomov 
3346fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->retry_attempt, e_inval);
3347fe5da05eSIlya Dryomov 	for (i = 0; i < m->num_ops; i++)
3348fe5da05eSIlya Dryomov 		ceph_decode_32_safe(&p, end, m->rval[i], e_inval);
3349fe5da05eSIlya Dryomov 
3350fe5da05eSIlya Dryomov 	if (version >= 5) {
3351fe5da05eSIlya Dryomov 		ceph_decode_need(&p, end, sizeof(m->replay_version), e_inval);
3352fe5da05eSIlya Dryomov 		memcpy(&m->replay_version, p, sizeof(m->replay_version));
3353fe5da05eSIlya Dryomov 		p += sizeof(m->replay_version);
3354fe5da05eSIlya Dryomov 		ceph_decode_64_safe(&p, end, m->user_version, e_inval);
3355fe5da05eSIlya Dryomov 	} else {
3356fe5da05eSIlya Dryomov 		m->replay_version = bad_replay_version; /* struct */
3357fe5da05eSIlya Dryomov 		m->user_version = le64_to_cpu(m->replay_version.version);
33581b83bef2SSage Weil 	}
33591b83bef2SSage Weil 
3360fe5da05eSIlya Dryomov 	if (version >= 6) {
3361fe5da05eSIlya Dryomov 		if (version >= 7)
3362fe5da05eSIlya Dryomov 			ceph_decode_8_safe(&p, end, decode_redir, e_inval);
3363b0b31a8fSIlya Dryomov 		else
3364b0b31a8fSIlya Dryomov 			decode_redir = 1;
3365b0b31a8fSIlya Dryomov 	} else {
3366b0b31a8fSIlya Dryomov 		decode_redir = 0;
3367b0b31a8fSIlya Dryomov 	}
3368b0b31a8fSIlya Dryomov 
3369b0b31a8fSIlya Dryomov 	if (decode_redir) {
3370fe5da05eSIlya Dryomov 		ret = ceph_redirect_decode(&p, end, &m->redirect);
3371fe5da05eSIlya Dryomov 		if (ret)
3372fe5da05eSIlya Dryomov 			return ret;
3373205ee118SIlya Dryomov 	} else {
3374fe5da05eSIlya Dryomov 		ceph_oloc_init(&m->redirect.oloc);
3375205ee118SIlya Dryomov 	}
3376205ee118SIlya Dryomov 
3377fe5da05eSIlya Dryomov 	return 0;
3378205ee118SIlya Dryomov 
3379fe5da05eSIlya Dryomov e_inval:
3380fe5da05eSIlya Dryomov 	return -EINVAL;
3381fe5da05eSIlya Dryomov }
3382fe5da05eSIlya Dryomov 
3383fe5da05eSIlya Dryomov /*
3384b18b9550SIlya Dryomov  * Handle MOSDOpReply.  Set ->r_result and call the callback if it is
3385b18b9550SIlya Dryomov  * specified.
3386fe5da05eSIlya Dryomov  */
33875aea3dcdSIlya Dryomov static void handle_reply(struct ceph_osd *osd, struct ceph_msg *msg)
3388fe5da05eSIlya Dryomov {
33895aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
3390fe5da05eSIlya Dryomov 	struct ceph_osd_request *req;
3391fe5da05eSIlya Dryomov 	struct MOSDOpReply m;
3392fe5da05eSIlya Dryomov 	u64 tid = le64_to_cpu(msg->hdr.tid);
3393fe5da05eSIlya Dryomov 	u32 data_len = 0;
3394fe5da05eSIlya Dryomov 	int ret;
3395fe5da05eSIlya Dryomov 	int i;
3396fe5da05eSIlya Dryomov 
3397fe5da05eSIlya Dryomov 	dout("%s msg %p tid %llu\n", __func__, msg, tid);
3398fe5da05eSIlya Dryomov 
33995aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
34005aea3dcdSIlya Dryomov 	if (!osd_registered(osd)) {
34015aea3dcdSIlya Dryomov 		dout("%s osd%d unknown\n", __func__, osd->o_osd);
34025aea3dcdSIlya Dryomov 		goto out_unlock_osdc;
3403fe5da05eSIlya Dryomov 	}
34045aea3dcdSIlya Dryomov 	WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
34055aea3dcdSIlya Dryomov 
34065aea3dcdSIlya Dryomov 	mutex_lock(&osd->lock);
34075aea3dcdSIlya Dryomov 	req = lookup_request(&osd->o_requests, tid);
34085aea3dcdSIlya Dryomov 	if (!req) {
34095aea3dcdSIlya Dryomov 		dout("%s osd%d tid %llu unknown\n", __func__, osd->o_osd, tid);
34105aea3dcdSIlya Dryomov 		goto out_unlock_session;
34115aea3dcdSIlya Dryomov 	}
3412fe5da05eSIlya Dryomov 
3413cd08e0a2SYan, Zheng 	m.redirect.oloc.pool_ns = req->r_t.target_oloc.pool_ns;
3414fe5da05eSIlya Dryomov 	ret = decode_MOSDOpReply(msg, &m);
3415cd08e0a2SYan, Zheng 	m.redirect.oloc.pool_ns = NULL;
3416fe5da05eSIlya Dryomov 	if (ret) {
3417fe5da05eSIlya Dryomov 		pr_err("failed to decode MOSDOpReply for tid %llu: %d\n",
3418fe5da05eSIlya Dryomov 		       req->r_tid, ret);
3419fe5da05eSIlya Dryomov 		ceph_msg_dump(msg);
3420fe5da05eSIlya Dryomov 		goto fail_request;
3421fe5da05eSIlya Dryomov 	}
3422fe5da05eSIlya Dryomov 	dout("%s req %p tid %llu flags 0x%llx pgid %llu.%x epoch %u attempt %d v %u'%llu uv %llu\n",
3423fe5da05eSIlya Dryomov 	     __func__, req, req->r_tid, m.flags, m.pgid.pool, m.pgid.seed,
3424fe5da05eSIlya Dryomov 	     m.epoch, m.retry_attempt, le32_to_cpu(m.replay_version.epoch),
3425fe5da05eSIlya Dryomov 	     le64_to_cpu(m.replay_version.version), m.user_version);
3426fe5da05eSIlya Dryomov 
3427fe5da05eSIlya Dryomov 	if (m.retry_attempt >= 0) {
3428fe5da05eSIlya Dryomov 		if (m.retry_attempt != req->r_attempts - 1) {
3429fe5da05eSIlya Dryomov 			dout("req %p tid %llu retry_attempt %d != %d, ignoring\n",
3430fe5da05eSIlya Dryomov 			     req, req->r_tid, m.retry_attempt,
3431fe5da05eSIlya Dryomov 			     req->r_attempts - 1);
34325aea3dcdSIlya Dryomov 			goto out_unlock_session;
3433fe5da05eSIlya Dryomov 		}
3434fe5da05eSIlya Dryomov 	} else {
3435fe5da05eSIlya Dryomov 		WARN_ON(1); /* MOSDOpReply v4 is assumed */
3436fe5da05eSIlya Dryomov 	}
3437fe5da05eSIlya Dryomov 
3438fe5da05eSIlya Dryomov 	if (!ceph_oloc_empty(&m.redirect.oloc)) {
3439fe5da05eSIlya Dryomov 		dout("req %p tid %llu redirect pool %lld\n", req, req->r_tid,
3440fe5da05eSIlya Dryomov 		     m.redirect.oloc.pool);
34415aea3dcdSIlya Dryomov 		unlink_request(osd, req);
34425aea3dcdSIlya Dryomov 		mutex_unlock(&osd->lock);
3443205ee118SIlya Dryomov 
344430c156d9SYan, Zheng 		/*
344530c156d9SYan, Zheng 		 * Not ceph_oloc_copy() - changing pool_ns is not
344630c156d9SYan, Zheng 		 * supported.
344730c156d9SYan, Zheng 		 */
344830c156d9SYan, Zheng 		req->r_t.target_oloc.pool = m.redirect.oloc.pool;
34495aea3dcdSIlya Dryomov 		req->r_flags |= CEPH_OSD_FLAG_REDIRECTED;
34505aea3dcdSIlya Dryomov 		req->r_tid = 0;
34515aea3dcdSIlya Dryomov 		__submit_request(req, false);
34525aea3dcdSIlya Dryomov 		goto out_unlock_osdc;
3453205ee118SIlya Dryomov 	}
3454205ee118SIlya Dryomov 
3455fe5da05eSIlya Dryomov 	if (m.num_ops != req->r_num_ops) {
3456fe5da05eSIlya Dryomov 		pr_err("num_ops %d != %d for tid %llu\n", m.num_ops,
3457fe5da05eSIlya Dryomov 		       req->r_num_ops, req->r_tid);
3458fe5da05eSIlya Dryomov 		goto fail_request;
3459fe5da05eSIlya Dryomov 	}
3460fe5da05eSIlya Dryomov 	for (i = 0; i < req->r_num_ops; i++) {
3461fe5da05eSIlya Dryomov 		dout(" req %p tid %llu op %d rval %d len %u\n", req,
3462fe5da05eSIlya Dryomov 		     req->r_tid, i, m.rval[i], m.outdata_len[i]);
3463fe5da05eSIlya Dryomov 		req->r_ops[i].rval = m.rval[i];
3464fe5da05eSIlya Dryomov 		req->r_ops[i].outdata_len = m.outdata_len[i];
3465fe5da05eSIlya Dryomov 		data_len += m.outdata_len[i];
3466fe5da05eSIlya Dryomov 	}
3467fe5da05eSIlya Dryomov 	if (data_len != le32_to_cpu(msg->hdr.data_len)) {
3468fe5da05eSIlya Dryomov 		pr_err("sum of lens %u != %u for tid %llu\n", data_len,
3469fe5da05eSIlya Dryomov 		       le32_to_cpu(msg->hdr.data_len), req->r_tid);
3470fe5da05eSIlya Dryomov 		goto fail_request;
3471fe5da05eSIlya Dryomov 	}
3472b18b9550SIlya Dryomov 	dout("%s req %p tid %llu result %d data_len %u\n", __func__,
3473b18b9550SIlya Dryomov 	     req, req->r_tid, m.result, data_len);
34743d14c5d2SYehuda Sadeh 
3475b18b9550SIlya Dryomov 	/*
3476b18b9550SIlya Dryomov 	 * Since we only ever request ONDISK, we should only ever get
3477b18b9550SIlya Dryomov 	 * one (type of) reply back.
3478b18b9550SIlya Dryomov 	 */
3479b18b9550SIlya Dryomov 	WARN_ON(!(m.flags & CEPH_OSD_FLAG_ONDISK));
3480fe5da05eSIlya Dryomov 	req->r_result = m.result ?: data_len;
348145ee2c1dSIlya Dryomov 	finish_request(req);
34825aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
34835aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
34843d14c5d2SYehuda Sadeh 
3485fe5da05eSIlya Dryomov 	__complete_request(req);
3486b18b9550SIlya Dryomov 	complete_all(&req->r_completion);
3487dc045a91SIlya Dryomov 	ceph_osdc_put_request(req);
34883d14c5d2SYehuda Sadeh 	return;
3489fe5da05eSIlya Dryomov 
3490fe5da05eSIlya Dryomov fail_request:
34914609245eSIlya Dryomov 	complete_request(req, -EIO);
34925aea3dcdSIlya Dryomov out_unlock_session:
34935aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
34945aea3dcdSIlya Dryomov out_unlock_osdc:
34955aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
34963d14c5d2SYehuda Sadeh }
34973d14c5d2SYehuda Sadeh 
349842c1b124SIlya Dryomov static void set_pool_was_full(struct ceph_osd_client *osdc)
349942c1b124SIlya Dryomov {
350042c1b124SIlya Dryomov 	struct rb_node *n;
350142c1b124SIlya Dryomov 
350242c1b124SIlya Dryomov 	for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
350342c1b124SIlya Dryomov 		struct ceph_pg_pool_info *pi =
350442c1b124SIlya Dryomov 		    rb_entry(n, struct ceph_pg_pool_info, node);
350542c1b124SIlya Dryomov 
350642c1b124SIlya Dryomov 		pi->was_full = __pool_full(pi);
350742c1b124SIlya Dryomov 	}
350842c1b124SIlya Dryomov }
350942c1b124SIlya Dryomov 
35105aea3dcdSIlya Dryomov static bool pool_cleared_full(struct ceph_osd_client *osdc, s64 pool_id)
35113d14c5d2SYehuda Sadeh {
35125aea3dcdSIlya Dryomov 	struct ceph_pg_pool_info *pi;
35133d14c5d2SYehuda Sadeh 
35145aea3dcdSIlya Dryomov 	pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
35155aea3dcdSIlya Dryomov 	if (!pi)
35165aea3dcdSIlya Dryomov 		return false;
35173d14c5d2SYehuda Sadeh 
35185aea3dcdSIlya Dryomov 	return pi->was_full && !__pool_full(pi);
35193d14c5d2SYehuda Sadeh }
35203d14c5d2SYehuda Sadeh 
3521922dab61SIlya Dryomov static enum calc_target_result
3522922dab61SIlya Dryomov recalc_linger_target(struct ceph_osd_linger_request *lreq)
3523922dab61SIlya Dryomov {
3524922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
3525922dab61SIlya Dryomov 	enum calc_target_result ct_res;
3526922dab61SIlya Dryomov 
35277de030d6SIlya Dryomov 	ct_res = calc_target(osdc, &lreq->t, NULL, true);
3528922dab61SIlya Dryomov 	if (ct_res == CALC_TARGET_NEED_RESEND) {
3529922dab61SIlya Dryomov 		struct ceph_osd *osd;
3530922dab61SIlya Dryomov 
3531922dab61SIlya Dryomov 		osd = lookup_create_osd(osdc, lreq->t.osd, true);
3532922dab61SIlya Dryomov 		if (osd != lreq->osd) {
3533922dab61SIlya Dryomov 			unlink_linger(lreq->osd, lreq);
3534922dab61SIlya Dryomov 			link_linger(osd, lreq);
3535922dab61SIlya Dryomov 		}
3536922dab61SIlya Dryomov 	}
3537922dab61SIlya Dryomov 
3538922dab61SIlya Dryomov 	return ct_res;
3539922dab61SIlya Dryomov }
3540922dab61SIlya Dryomov 
35413d14c5d2SYehuda Sadeh /*
35425aea3dcdSIlya Dryomov  * Requeue requests whose mapping to an OSD has changed.
35433d14c5d2SYehuda Sadeh  */
35445aea3dcdSIlya Dryomov static void scan_requests(struct ceph_osd *osd,
35455aea3dcdSIlya Dryomov 			  bool force_resend,
35465aea3dcdSIlya Dryomov 			  bool cleared_full,
35475aea3dcdSIlya Dryomov 			  bool check_pool_cleared_full,
35485aea3dcdSIlya Dryomov 			  struct rb_root *need_resend,
35495aea3dcdSIlya Dryomov 			  struct list_head *need_resend_linger)
35503d14c5d2SYehuda Sadeh {
35515aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
35525aea3dcdSIlya Dryomov 	struct rb_node *n;
35535aea3dcdSIlya Dryomov 	bool force_resend_writes;
35543d14c5d2SYehuda Sadeh 
3555922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_linger_requests); n; ) {
3556922dab61SIlya Dryomov 		struct ceph_osd_linger_request *lreq =
3557922dab61SIlya Dryomov 		    rb_entry(n, struct ceph_osd_linger_request, node);
3558922dab61SIlya Dryomov 		enum calc_target_result ct_res;
3559922dab61SIlya Dryomov 
3560922dab61SIlya Dryomov 		n = rb_next(n); /* recalc_linger_target() */
3561922dab61SIlya Dryomov 
3562922dab61SIlya Dryomov 		dout("%s lreq %p linger_id %llu\n", __func__, lreq,
3563922dab61SIlya Dryomov 		     lreq->linger_id);
3564922dab61SIlya Dryomov 		ct_res = recalc_linger_target(lreq);
3565922dab61SIlya Dryomov 		switch (ct_res) {
3566922dab61SIlya Dryomov 		case CALC_TARGET_NO_ACTION:
3567922dab61SIlya Dryomov 			force_resend_writes = cleared_full ||
3568922dab61SIlya Dryomov 			    (check_pool_cleared_full &&
3569922dab61SIlya Dryomov 			     pool_cleared_full(osdc, lreq->t.base_oloc.pool));
3570922dab61SIlya Dryomov 			if (!force_resend && !force_resend_writes)
3571922dab61SIlya Dryomov 				break;
3572922dab61SIlya Dryomov 
3573922dab61SIlya Dryomov 			/* fall through */
3574922dab61SIlya Dryomov 		case CALC_TARGET_NEED_RESEND:
35754609245eSIlya Dryomov 			cancel_linger_map_check(lreq);
3576922dab61SIlya Dryomov 			/*
3577922dab61SIlya Dryomov 			 * scan_requests() for the previous epoch(s)
3578922dab61SIlya Dryomov 			 * may have already added it to the list, since
3579922dab61SIlya Dryomov 			 * it's not unlinked here.
3580922dab61SIlya Dryomov 			 */
3581922dab61SIlya Dryomov 			if (list_empty(&lreq->scan_item))
3582922dab61SIlya Dryomov 				list_add_tail(&lreq->scan_item, need_resend_linger);
3583922dab61SIlya Dryomov 			break;
3584922dab61SIlya Dryomov 		case CALC_TARGET_POOL_DNE:
3585a10bcb19SIlya Dryomov 			list_del_init(&lreq->scan_item);
35864609245eSIlya Dryomov 			check_linger_pool_dne(lreq);
3587922dab61SIlya Dryomov 			break;
3588922dab61SIlya Dryomov 		}
3589922dab61SIlya Dryomov 	}
3590922dab61SIlya Dryomov 
35915aea3dcdSIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; ) {
35925aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
35935aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
35945aea3dcdSIlya Dryomov 		enum calc_target_result ct_res;
3595ab60b16dSAlex Elder 
35964609245eSIlya Dryomov 		n = rb_next(n); /* unlink_request(), check_pool_dne() */
3597ab60b16dSAlex Elder 
35985aea3dcdSIlya Dryomov 		dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
35997de030d6SIlya Dryomov 		ct_res = calc_target(osdc, &req->r_t, &req->r_osd->o_con,
36007de030d6SIlya Dryomov 				     false);
36015aea3dcdSIlya Dryomov 		switch (ct_res) {
36025aea3dcdSIlya Dryomov 		case CALC_TARGET_NO_ACTION:
36035aea3dcdSIlya Dryomov 			force_resend_writes = cleared_full ||
36045aea3dcdSIlya Dryomov 			    (check_pool_cleared_full &&
36055aea3dcdSIlya Dryomov 			     pool_cleared_full(osdc, req->r_t.base_oloc.pool));
36065aea3dcdSIlya Dryomov 			if (!force_resend &&
36075aea3dcdSIlya Dryomov 			    (!(req->r_flags & CEPH_OSD_FLAG_WRITE) ||
36085aea3dcdSIlya Dryomov 			     !force_resend_writes))
36095aea3dcdSIlya Dryomov 				break;
3610a40c4f10SYehuda Sadeh 
36115aea3dcdSIlya Dryomov 			/* fall through */
36125aea3dcdSIlya Dryomov 		case CALC_TARGET_NEED_RESEND:
36134609245eSIlya Dryomov 			cancel_map_check(req);
36145aea3dcdSIlya Dryomov 			unlink_request(osd, req);
36155aea3dcdSIlya Dryomov 			insert_request(need_resend, req);
36165aea3dcdSIlya Dryomov 			break;
36175aea3dcdSIlya Dryomov 		case CALC_TARGET_POOL_DNE:
36184609245eSIlya Dryomov 			check_pool_dne(req);
36195aea3dcdSIlya Dryomov 			break;
3620a40c4f10SYehuda Sadeh 		}
36213d14c5d2SYehuda Sadeh 	}
36223d14c5d2SYehuda Sadeh }
36236f6c7006SSage Weil 
362442c1b124SIlya Dryomov static int handle_one_map(struct ceph_osd_client *osdc,
36255aea3dcdSIlya Dryomov 			  void *p, void *end, bool incremental,
36265aea3dcdSIlya Dryomov 			  struct rb_root *need_resend,
36275aea3dcdSIlya Dryomov 			  struct list_head *need_resend_linger)
362842c1b124SIlya Dryomov {
362942c1b124SIlya Dryomov 	struct ceph_osdmap *newmap;
363042c1b124SIlya Dryomov 	struct rb_node *n;
363142c1b124SIlya Dryomov 	bool skipped_map = false;
363242c1b124SIlya Dryomov 	bool was_full;
363342c1b124SIlya Dryomov 
3634b7ec35b3SIlya Dryomov 	was_full = ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
363542c1b124SIlya Dryomov 	set_pool_was_full(osdc);
363642c1b124SIlya Dryomov 
363742c1b124SIlya Dryomov 	if (incremental)
363842c1b124SIlya Dryomov 		newmap = osdmap_apply_incremental(&p, end, osdc->osdmap);
363942c1b124SIlya Dryomov 	else
364042c1b124SIlya Dryomov 		newmap = ceph_osdmap_decode(&p, end);
364142c1b124SIlya Dryomov 	if (IS_ERR(newmap))
364242c1b124SIlya Dryomov 		return PTR_ERR(newmap);
364342c1b124SIlya Dryomov 
364442c1b124SIlya Dryomov 	if (newmap != osdc->osdmap) {
364542c1b124SIlya Dryomov 		/*
364642c1b124SIlya Dryomov 		 * Preserve ->was_full before destroying the old map.
364742c1b124SIlya Dryomov 		 * For pools that weren't in the old map, ->was_full
364842c1b124SIlya Dryomov 		 * should be false.
364942c1b124SIlya Dryomov 		 */
365042c1b124SIlya Dryomov 		for (n = rb_first(&newmap->pg_pools); n; n = rb_next(n)) {
365142c1b124SIlya Dryomov 			struct ceph_pg_pool_info *pi =
365242c1b124SIlya Dryomov 			    rb_entry(n, struct ceph_pg_pool_info, node);
365342c1b124SIlya Dryomov 			struct ceph_pg_pool_info *old_pi;
365442c1b124SIlya Dryomov 
365542c1b124SIlya Dryomov 			old_pi = ceph_pg_pool_by_id(osdc->osdmap, pi->id);
365642c1b124SIlya Dryomov 			if (old_pi)
365742c1b124SIlya Dryomov 				pi->was_full = old_pi->was_full;
365842c1b124SIlya Dryomov 			else
365942c1b124SIlya Dryomov 				WARN_ON(pi->was_full);
366042c1b124SIlya Dryomov 		}
366142c1b124SIlya Dryomov 
366242c1b124SIlya Dryomov 		if (osdc->osdmap->epoch &&
366342c1b124SIlya Dryomov 		    osdc->osdmap->epoch + 1 < newmap->epoch) {
366442c1b124SIlya Dryomov 			WARN_ON(incremental);
366542c1b124SIlya Dryomov 			skipped_map = true;
366642c1b124SIlya Dryomov 		}
366742c1b124SIlya Dryomov 
366842c1b124SIlya Dryomov 		ceph_osdmap_destroy(osdc->osdmap);
366942c1b124SIlya Dryomov 		osdc->osdmap = newmap;
367042c1b124SIlya Dryomov 	}
367142c1b124SIlya Dryomov 
3672b7ec35b3SIlya Dryomov 	was_full &= !ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
36735aea3dcdSIlya Dryomov 	scan_requests(&osdc->homeless_osd, skipped_map, was_full, true,
36745aea3dcdSIlya Dryomov 		      need_resend, need_resend_linger);
36755aea3dcdSIlya Dryomov 
36765aea3dcdSIlya Dryomov 	for (n = rb_first(&osdc->osds); n; ) {
36775aea3dcdSIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
36785aea3dcdSIlya Dryomov 
36795aea3dcdSIlya Dryomov 		n = rb_next(n); /* close_osd() */
36805aea3dcdSIlya Dryomov 
36815aea3dcdSIlya Dryomov 		scan_requests(osd, skipped_map, was_full, true, need_resend,
36825aea3dcdSIlya Dryomov 			      need_resend_linger);
36835aea3dcdSIlya Dryomov 		if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
36845aea3dcdSIlya Dryomov 		    memcmp(&osd->o_con.peer_addr,
36855aea3dcdSIlya Dryomov 			   ceph_osd_addr(osdc->osdmap, osd->o_osd),
36865aea3dcdSIlya Dryomov 			   sizeof(struct ceph_entity_addr)))
36875aea3dcdSIlya Dryomov 			close_osd(osd);
36885aea3dcdSIlya Dryomov 	}
368942c1b124SIlya Dryomov 
369042c1b124SIlya Dryomov 	return 0;
369142c1b124SIlya Dryomov }
36926f6c7006SSage Weil 
36935aea3dcdSIlya Dryomov static void kick_requests(struct ceph_osd_client *osdc,
36945aea3dcdSIlya Dryomov 			  struct rb_root *need_resend,
36955aea3dcdSIlya Dryomov 			  struct list_head *need_resend_linger)
36965aea3dcdSIlya Dryomov {
3697922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq, *nlreq;
369804c7d789SIlya Dryomov 	enum calc_target_result ct_res;
36995aea3dcdSIlya Dryomov 	struct rb_node *n;
37005aea3dcdSIlya Dryomov 
370104c7d789SIlya Dryomov 	/* make sure need_resend targets reflect latest map */
370204c7d789SIlya Dryomov 	for (n = rb_first(need_resend); n; ) {
370304c7d789SIlya Dryomov 		struct ceph_osd_request *req =
370404c7d789SIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
370504c7d789SIlya Dryomov 
370604c7d789SIlya Dryomov 		n = rb_next(n);
370704c7d789SIlya Dryomov 
370804c7d789SIlya Dryomov 		if (req->r_t.epoch < osdc->osdmap->epoch) {
370904c7d789SIlya Dryomov 			ct_res = calc_target(osdc, &req->r_t, NULL, false);
371004c7d789SIlya Dryomov 			if (ct_res == CALC_TARGET_POOL_DNE) {
371104c7d789SIlya Dryomov 				erase_request(need_resend, req);
371204c7d789SIlya Dryomov 				check_pool_dne(req);
371304c7d789SIlya Dryomov 			}
371404c7d789SIlya Dryomov 		}
371504c7d789SIlya Dryomov 	}
371604c7d789SIlya Dryomov 
37175aea3dcdSIlya Dryomov 	for (n = rb_first(need_resend); n; ) {
37185aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
37195aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
37205aea3dcdSIlya Dryomov 		struct ceph_osd *osd;
37215aea3dcdSIlya Dryomov 
37225aea3dcdSIlya Dryomov 		n = rb_next(n);
37235aea3dcdSIlya Dryomov 		erase_request(need_resend, req); /* before link_request() */
37245aea3dcdSIlya Dryomov 
37255aea3dcdSIlya Dryomov 		osd = lookup_create_osd(osdc, req->r_t.osd, true);
37265aea3dcdSIlya Dryomov 		link_request(osd, req);
37275aea3dcdSIlya Dryomov 		if (!req->r_linger) {
37285aea3dcdSIlya Dryomov 			if (!osd_homeless(osd) && !req->r_t.paused)
37295aea3dcdSIlya Dryomov 				send_request(req);
3730922dab61SIlya Dryomov 		} else {
3731922dab61SIlya Dryomov 			cancel_linger_request(req);
37325aea3dcdSIlya Dryomov 		}
37335aea3dcdSIlya Dryomov 	}
3734922dab61SIlya Dryomov 
3735922dab61SIlya Dryomov 	list_for_each_entry_safe(lreq, nlreq, need_resend_linger, scan_item) {
3736922dab61SIlya Dryomov 		if (!osd_homeless(lreq->osd))
3737922dab61SIlya Dryomov 			send_linger(lreq);
3738922dab61SIlya Dryomov 
3739922dab61SIlya Dryomov 		list_del_init(&lreq->scan_item);
3740922dab61SIlya Dryomov 	}
37415aea3dcdSIlya Dryomov }
37425aea3dcdSIlya Dryomov 
37433d14c5d2SYehuda Sadeh /*
37443d14c5d2SYehuda Sadeh  * Process updated osd map.
37453d14c5d2SYehuda Sadeh  *
37463d14c5d2SYehuda Sadeh  * The message contains any number of incremental and full maps, normally
37473d14c5d2SYehuda Sadeh  * indicating some sort of topology change in the cluster.  Kick requests
37483d14c5d2SYehuda Sadeh  * off to different OSDs as needed.
37493d14c5d2SYehuda Sadeh  */
37503d14c5d2SYehuda Sadeh void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
37513d14c5d2SYehuda Sadeh {
375242c1b124SIlya Dryomov 	void *p = msg->front.iov_base;
375342c1b124SIlya Dryomov 	void *const end = p + msg->front.iov_len;
37543d14c5d2SYehuda Sadeh 	u32 nr_maps, maplen;
37553d14c5d2SYehuda Sadeh 	u32 epoch;
37563d14c5d2SYehuda Sadeh 	struct ceph_fsid fsid;
37575aea3dcdSIlya Dryomov 	struct rb_root need_resend = RB_ROOT;
37585aea3dcdSIlya Dryomov 	LIST_HEAD(need_resend_linger);
375942c1b124SIlya Dryomov 	bool handled_incremental = false;
376042c1b124SIlya Dryomov 	bool was_pauserd, was_pausewr;
376142c1b124SIlya Dryomov 	bool pauserd, pausewr;
376242c1b124SIlya Dryomov 	int err;
37633d14c5d2SYehuda Sadeh 
376442c1b124SIlya Dryomov 	dout("%s have %u\n", __func__, osdc->osdmap->epoch);
37655aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
37663d14c5d2SYehuda Sadeh 
37673d14c5d2SYehuda Sadeh 	/* verify fsid */
37683d14c5d2SYehuda Sadeh 	ceph_decode_need(&p, end, sizeof(fsid), bad);
37693d14c5d2SYehuda Sadeh 	ceph_decode_copy(&p, &fsid, sizeof(fsid));
37703d14c5d2SYehuda Sadeh 	if (ceph_check_fsid(osdc->client, &fsid) < 0)
377142c1b124SIlya Dryomov 		goto bad;
37723d14c5d2SYehuda Sadeh 
3773b7ec35b3SIlya Dryomov 	was_pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3774b7ec35b3SIlya Dryomov 	was_pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3775b7ec35b3SIlya Dryomov 		      ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
377642c1b124SIlya Dryomov 		      have_pool_full(osdc);
37779a1ea2dbSJosh Durgin 
37783d14c5d2SYehuda Sadeh 	/* incremental maps */
37793d14c5d2SYehuda Sadeh 	ceph_decode_32_safe(&p, end, nr_maps, bad);
37803d14c5d2SYehuda Sadeh 	dout(" %d inc maps\n", nr_maps);
37813d14c5d2SYehuda Sadeh 	while (nr_maps > 0) {
37823d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, 2*sizeof(u32), bad);
37833d14c5d2SYehuda Sadeh 		epoch = ceph_decode_32(&p);
37843d14c5d2SYehuda Sadeh 		maplen = ceph_decode_32(&p);
37853d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, maplen, bad);
378642c1b124SIlya Dryomov 		if (osdc->osdmap->epoch &&
378742c1b124SIlya Dryomov 		    osdc->osdmap->epoch + 1 == epoch) {
37883d14c5d2SYehuda Sadeh 			dout("applying incremental map %u len %d\n",
37893d14c5d2SYehuda Sadeh 			     epoch, maplen);
37905aea3dcdSIlya Dryomov 			err = handle_one_map(osdc, p, p + maplen, true,
37915aea3dcdSIlya Dryomov 					     &need_resend, &need_resend_linger);
379242c1b124SIlya Dryomov 			if (err)
37933d14c5d2SYehuda Sadeh 				goto bad;
379442c1b124SIlya Dryomov 			handled_incremental = true;
37953d14c5d2SYehuda Sadeh 		} else {
37963d14c5d2SYehuda Sadeh 			dout("ignoring incremental map %u len %d\n",
37973d14c5d2SYehuda Sadeh 			     epoch, maplen);
37983d14c5d2SYehuda Sadeh 		}
379942c1b124SIlya Dryomov 		p += maplen;
38003d14c5d2SYehuda Sadeh 		nr_maps--;
38013d14c5d2SYehuda Sadeh 	}
380242c1b124SIlya Dryomov 	if (handled_incremental)
38033d14c5d2SYehuda Sadeh 		goto done;
38043d14c5d2SYehuda Sadeh 
38053d14c5d2SYehuda Sadeh 	/* full maps */
38063d14c5d2SYehuda Sadeh 	ceph_decode_32_safe(&p, end, nr_maps, bad);
38073d14c5d2SYehuda Sadeh 	dout(" %d full maps\n", nr_maps);
38083d14c5d2SYehuda Sadeh 	while (nr_maps) {
38093d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, 2*sizeof(u32), bad);
38103d14c5d2SYehuda Sadeh 		epoch = ceph_decode_32(&p);
38113d14c5d2SYehuda Sadeh 		maplen = ceph_decode_32(&p);
38123d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, maplen, bad);
38133d14c5d2SYehuda Sadeh 		if (nr_maps > 1) {
38143d14c5d2SYehuda Sadeh 			dout("skipping non-latest full map %u len %d\n",
38153d14c5d2SYehuda Sadeh 			     epoch, maplen);
3816e5253a7bSIlya Dryomov 		} else if (osdc->osdmap->epoch >= epoch) {
38173d14c5d2SYehuda Sadeh 			dout("skipping full map %u len %d, "
38183d14c5d2SYehuda Sadeh 			     "older than our %u\n", epoch, maplen,
38193d14c5d2SYehuda Sadeh 			     osdc->osdmap->epoch);
38203d14c5d2SYehuda Sadeh 		} else {
38213d14c5d2SYehuda Sadeh 			dout("taking full map %u len %d\n", epoch, maplen);
38225aea3dcdSIlya Dryomov 			err = handle_one_map(osdc, p, p + maplen, false,
38235aea3dcdSIlya Dryomov 					     &need_resend, &need_resend_linger);
382442c1b124SIlya Dryomov 			if (err)
38253d14c5d2SYehuda Sadeh 				goto bad;
38263d14c5d2SYehuda Sadeh 		}
38273d14c5d2SYehuda Sadeh 		p += maplen;
38283d14c5d2SYehuda Sadeh 		nr_maps--;
38293d14c5d2SYehuda Sadeh 	}
38303d14c5d2SYehuda Sadeh 
38313d14c5d2SYehuda Sadeh done:
3832cd634fb6SSage Weil 	/*
3833cd634fb6SSage Weil 	 * subscribe to subsequent osdmap updates if full to ensure
3834cd634fb6SSage Weil 	 * we find out when we are no longer full and stop returning
3835cd634fb6SSage Weil 	 * ENOSPC.
3836cd634fb6SSage Weil 	 */
3837b7ec35b3SIlya Dryomov 	pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3838b7ec35b3SIlya Dryomov 	pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3839b7ec35b3SIlya Dryomov 		  ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
384042c1b124SIlya Dryomov 		  have_pool_full(osdc);
384158eb7932SJeff Layton 	if (was_pauserd || was_pausewr || pauserd || pausewr ||
384258eb7932SJeff Layton 	    osdc->osdmap->epoch < osdc->epoch_barrier)
384342c1b124SIlya Dryomov 		maybe_request_map(osdc);
3844cd634fb6SSage Weil 
38455aea3dcdSIlya Dryomov 	kick_requests(osdc, &need_resend, &need_resend_linger);
384642c1b124SIlya Dryomov 
3847fc36d0a4SJeff Layton 	ceph_osdc_abort_on_full(osdc);
384842c1b124SIlya Dryomov 	ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
384942c1b124SIlya Dryomov 			  osdc->osdmap->epoch);
38505aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
38513d14c5d2SYehuda Sadeh 	wake_up_all(&osdc->client->auth_wq);
38523d14c5d2SYehuda Sadeh 	return;
38533d14c5d2SYehuda Sadeh 
38543d14c5d2SYehuda Sadeh bad:
38553d14c5d2SYehuda Sadeh 	pr_err("osdc handle_map corrupt msg\n");
38563d14c5d2SYehuda Sadeh 	ceph_msg_dump(msg);
38575aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
38585aea3dcdSIlya Dryomov }
38595aea3dcdSIlya Dryomov 
38605aea3dcdSIlya Dryomov /*
38615aea3dcdSIlya Dryomov  * Resubmit requests pending on the given osd.
38625aea3dcdSIlya Dryomov  */
38635aea3dcdSIlya Dryomov static void kick_osd_requests(struct ceph_osd *osd)
38645aea3dcdSIlya Dryomov {
38655aea3dcdSIlya Dryomov 	struct rb_node *n;
38665aea3dcdSIlya Dryomov 
3867a02a946dSIlya Dryomov 	clear_backoffs(osd);
3868a02a946dSIlya Dryomov 
3869922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; ) {
38705aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
38715aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
38725aea3dcdSIlya Dryomov 
3873922dab61SIlya Dryomov 		n = rb_next(n); /* cancel_linger_request() */
3874922dab61SIlya Dryomov 
38755aea3dcdSIlya Dryomov 		if (!req->r_linger) {
38765aea3dcdSIlya Dryomov 			if (!req->r_t.paused)
38775aea3dcdSIlya Dryomov 				send_request(req);
3878922dab61SIlya Dryomov 		} else {
3879922dab61SIlya Dryomov 			cancel_linger_request(req);
38805aea3dcdSIlya Dryomov 		}
38815aea3dcdSIlya Dryomov 	}
3882922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_linger_requests); n; n = rb_next(n)) {
3883922dab61SIlya Dryomov 		struct ceph_osd_linger_request *lreq =
3884922dab61SIlya Dryomov 		    rb_entry(n, struct ceph_osd_linger_request, node);
3885922dab61SIlya Dryomov 
3886922dab61SIlya Dryomov 		send_linger(lreq);
3887922dab61SIlya Dryomov 	}
38885aea3dcdSIlya Dryomov }
38895aea3dcdSIlya Dryomov 
38905aea3dcdSIlya Dryomov /*
38915aea3dcdSIlya Dryomov  * If the osd connection drops, we need to resubmit all requests.
38925aea3dcdSIlya Dryomov  */
38935aea3dcdSIlya Dryomov static void osd_fault(struct ceph_connection *con)
38945aea3dcdSIlya Dryomov {
38955aea3dcdSIlya Dryomov 	struct ceph_osd *osd = con->private;
38965aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
38975aea3dcdSIlya Dryomov 
38985aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
38995aea3dcdSIlya Dryomov 
39005aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
39015aea3dcdSIlya Dryomov 	if (!osd_registered(osd)) {
39025aea3dcdSIlya Dryomov 		dout("%s osd%d unknown\n", __func__, osd->o_osd);
39035aea3dcdSIlya Dryomov 		goto out_unlock;
39045aea3dcdSIlya Dryomov 	}
39055aea3dcdSIlya Dryomov 
39065aea3dcdSIlya Dryomov 	if (!reopen_osd(osd))
39075aea3dcdSIlya Dryomov 		kick_osd_requests(osd);
39085aea3dcdSIlya Dryomov 	maybe_request_map(osdc);
39095aea3dcdSIlya Dryomov 
39105aea3dcdSIlya Dryomov out_unlock:
39115aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
39123d14c5d2SYehuda Sadeh }
39133d14c5d2SYehuda Sadeh 
3914a02a946dSIlya Dryomov struct MOSDBackoff {
3915a02a946dSIlya Dryomov 	struct ceph_spg spgid;
3916a02a946dSIlya Dryomov 	u32 map_epoch;
3917a02a946dSIlya Dryomov 	u8 op;
3918a02a946dSIlya Dryomov 	u64 id;
3919a02a946dSIlya Dryomov 	struct ceph_hobject_id *begin;
3920a02a946dSIlya Dryomov 	struct ceph_hobject_id *end;
3921a02a946dSIlya Dryomov };
3922a02a946dSIlya Dryomov 
3923a02a946dSIlya Dryomov static int decode_MOSDBackoff(const struct ceph_msg *msg, struct MOSDBackoff *m)
3924a02a946dSIlya Dryomov {
3925a02a946dSIlya Dryomov 	void *p = msg->front.iov_base;
3926a02a946dSIlya Dryomov 	void *const end = p + msg->front.iov_len;
3927a02a946dSIlya Dryomov 	u8 struct_v;
3928a02a946dSIlya Dryomov 	u32 struct_len;
3929a02a946dSIlya Dryomov 	int ret;
3930a02a946dSIlya Dryomov 
3931a02a946dSIlya Dryomov 	ret = ceph_start_decoding(&p, end, 1, "spg_t", &struct_v, &struct_len);
3932a02a946dSIlya Dryomov 	if (ret)
3933a02a946dSIlya Dryomov 		return ret;
3934a02a946dSIlya Dryomov 
3935a02a946dSIlya Dryomov 	ret = ceph_decode_pgid(&p, end, &m->spgid.pgid);
3936a02a946dSIlya Dryomov 	if (ret)
3937a02a946dSIlya Dryomov 		return ret;
3938a02a946dSIlya Dryomov 
3939a02a946dSIlya Dryomov 	ceph_decode_8_safe(&p, end, m->spgid.shard, e_inval);
3940a02a946dSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->map_epoch, e_inval);
3941a02a946dSIlya Dryomov 	ceph_decode_8_safe(&p, end, m->op, e_inval);
3942a02a946dSIlya Dryomov 	ceph_decode_64_safe(&p, end, m->id, e_inval);
3943a02a946dSIlya Dryomov 
3944a02a946dSIlya Dryomov 	m->begin = kzalloc(sizeof(*m->begin), GFP_NOIO);
3945a02a946dSIlya Dryomov 	if (!m->begin)
3946a02a946dSIlya Dryomov 		return -ENOMEM;
3947a02a946dSIlya Dryomov 
3948a02a946dSIlya Dryomov 	ret = decode_hoid(&p, end, m->begin);
3949a02a946dSIlya Dryomov 	if (ret) {
3950a02a946dSIlya Dryomov 		free_hoid(m->begin);
3951a02a946dSIlya Dryomov 		return ret;
3952a02a946dSIlya Dryomov 	}
3953a02a946dSIlya Dryomov 
3954a02a946dSIlya Dryomov 	m->end = kzalloc(sizeof(*m->end), GFP_NOIO);
3955a02a946dSIlya Dryomov 	if (!m->end) {
3956a02a946dSIlya Dryomov 		free_hoid(m->begin);
3957a02a946dSIlya Dryomov 		return -ENOMEM;
3958a02a946dSIlya Dryomov 	}
3959a02a946dSIlya Dryomov 
3960a02a946dSIlya Dryomov 	ret = decode_hoid(&p, end, m->end);
3961a02a946dSIlya Dryomov 	if (ret) {
3962a02a946dSIlya Dryomov 		free_hoid(m->begin);
3963a02a946dSIlya Dryomov 		free_hoid(m->end);
3964a02a946dSIlya Dryomov 		return ret;
3965a02a946dSIlya Dryomov 	}
3966a02a946dSIlya Dryomov 
3967a02a946dSIlya Dryomov 	return 0;
3968a02a946dSIlya Dryomov 
3969a02a946dSIlya Dryomov e_inval:
3970a02a946dSIlya Dryomov 	return -EINVAL;
3971a02a946dSIlya Dryomov }
3972a02a946dSIlya Dryomov 
3973a02a946dSIlya Dryomov static struct ceph_msg *create_backoff_message(
3974a02a946dSIlya Dryomov 				const struct ceph_osd_backoff *backoff,
3975a02a946dSIlya Dryomov 				u32 map_epoch)
3976a02a946dSIlya Dryomov {
3977a02a946dSIlya Dryomov 	struct ceph_msg *msg;
3978a02a946dSIlya Dryomov 	void *p, *end;
3979a02a946dSIlya Dryomov 	int msg_size;
3980a02a946dSIlya Dryomov 
3981a02a946dSIlya Dryomov 	msg_size = CEPH_ENCODING_START_BLK_LEN +
3982a02a946dSIlya Dryomov 			CEPH_PGID_ENCODING_LEN + 1; /* spgid */
3983a02a946dSIlya Dryomov 	msg_size += 4 + 1 + 8; /* map_epoch, op, id */
3984a02a946dSIlya Dryomov 	msg_size += CEPH_ENCODING_START_BLK_LEN +
3985a02a946dSIlya Dryomov 			hoid_encoding_size(backoff->begin);
3986a02a946dSIlya Dryomov 	msg_size += CEPH_ENCODING_START_BLK_LEN +
3987a02a946dSIlya Dryomov 			hoid_encoding_size(backoff->end);
3988a02a946dSIlya Dryomov 
3989a02a946dSIlya Dryomov 	msg = ceph_msg_new(CEPH_MSG_OSD_BACKOFF, msg_size, GFP_NOIO, true);
3990a02a946dSIlya Dryomov 	if (!msg)
3991a02a946dSIlya Dryomov 		return NULL;
3992a02a946dSIlya Dryomov 
3993a02a946dSIlya Dryomov 	p = msg->front.iov_base;
3994a02a946dSIlya Dryomov 	end = p + msg->front_alloc_len;
3995a02a946dSIlya Dryomov 
3996a02a946dSIlya Dryomov 	encode_spgid(&p, &backoff->spgid);
3997a02a946dSIlya Dryomov 	ceph_encode_32(&p, map_epoch);
3998a02a946dSIlya Dryomov 	ceph_encode_8(&p, CEPH_OSD_BACKOFF_OP_ACK_BLOCK);
3999a02a946dSIlya Dryomov 	ceph_encode_64(&p, backoff->id);
4000a02a946dSIlya Dryomov 	encode_hoid(&p, end, backoff->begin);
4001a02a946dSIlya Dryomov 	encode_hoid(&p, end, backoff->end);
4002a02a946dSIlya Dryomov 	BUG_ON(p != end);
4003a02a946dSIlya Dryomov 
4004a02a946dSIlya Dryomov 	msg->front.iov_len = p - msg->front.iov_base;
4005a02a946dSIlya Dryomov 	msg->hdr.version = cpu_to_le16(1); /* MOSDBackoff v1 */
4006a02a946dSIlya Dryomov 	msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
4007a02a946dSIlya Dryomov 
4008a02a946dSIlya Dryomov 	return msg;
4009a02a946dSIlya Dryomov }
4010a02a946dSIlya Dryomov 
4011a02a946dSIlya Dryomov static void handle_backoff_block(struct ceph_osd *osd, struct MOSDBackoff *m)
4012a02a946dSIlya Dryomov {
4013a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
4014a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
4015a02a946dSIlya Dryomov 	struct ceph_msg *msg;
4016a02a946dSIlya Dryomov 
4017a02a946dSIlya Dryomov 	dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4018a02a946dSIlya Dryomov 	     m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4019a02a946dSIlya Dryomov 
4020a02a946dSIlya Dryomov 	spg = lookup_spg_mapping(&osd->o_backoff_mappings, &m->spgid);
4021a02a946dSIlya Dryomov 	if (!spg) {
4022a02a946dSIlya Dryomov 		spg = alloc_spg_mapping();
4023a02a946dSIlya Dryomov 		if (!spg) {
4024a02a946dSIlya Dryomov 			pr_err("%s failed to allocate spg\n", __func__);
4025a02a946dSIlya Dryomov 			return;
4026a02a946dSIlya Dryomov 		}
4027a02a946dSIlya Dryomov 		spg->spgid = m->spgid; /* struct */
4028a02a946dSIlya Dryomov 		insert_spg_mapping(&osd->o_backoff_mappings, spg);
4029a02a946dSIlya Dryomov 	}
4030a02a946dSIlya Dryomov 
4031a02a946dSIlya Dryomov 	backoff = alloc_backoff();
4032a02a946dSIlya Dryomov 	if (!backoff) {
4033a02a946dSIlya Dryomov 		pr_err("%s failed to allocate backoff\n", __func__);
4034a02a946dSIlya Dryomov 		return;
4035a02a946dSIlya Dryomov 	}
4036a02a946dSIlya Dryomov 	backoff->spgid = m->spgid; /* struct */
4037a02a946dSIlya Dryomov 	backoff->id = m->id;
4038a02a946dSIlya Dryomov 	backoff->begin = m->begin;
4039a02a946dSIlya Dryomov 	m->begin = NULL; /* backoff now owns this */
4040a02a946dSIlya Dryomov 	backoff->end = m->end;
4041a02a946dSIlya Dryomov 	m->end = NULL;   /* ditto */
4042a02a946dSIlya Dryomov 
4043a02a946dSIlya Dryomov 	insert_backoff(&spg->backoffs, backoff);
4044a02a946dSIlya Dryomov 	insert_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4045a02a946dSIlya Dryomov 
4046a02a946dSIlya Dryomov 	/*
4047a02a946dSIlya Dryomov 	 * Ack with original backoff's epoch so that the OSD can
4048a02a946dSIlya Dryomov 	 * discard this if there was a PG split.
4049a02a946dSIlya Dryomov 	 */
4050a02a946dSIlya Dryomov 	msg = create_backoff_message(backoff, m->map_epoch);
4051a02a946dSIlya Dryomov 	if (!msg) {
4052a02a946dSIlya Dryomov 		pr_err("%s failed to allocate msg\n", __func__);
4053a02a946dSIlya Dryomov 		return;
4054a02a946dSIlya Dryomov 	}
4055a02a946dSIlya Dryomov 	ceph_con_send(&osd->o_con, msg);
4056a02a946dSIlya Dryomov }
4057a02a946dSIlya Dryomov 
4058a02a946dSIlya Dryomov static bool target_contained_by(const struct ceph_osd_request_target *t,
4059a02a946dSIlya Dryomov 				const struct ceph_hobject_id *begin,
4060a02a946dSIlya Dryomov 				const struct ceph_hobject_id *end)
4061a02a946dSIlya Dryomov {
4062a02a946dSIlya Dryomov 	struct ceph_hobject_id hoid;
4063a02a946dSIlya Dryomov 	int cmp;
4064a02a946dSIlya Dryomov 
4065a02a946dSIlya Dryomov 	hoid_fill_from_target(&hoid, t);
4066a02a946dSIlya Dryomov 	cmp = hoid_compare(&hoid, begin);
4067a02a946dSIlya Dryomov 	return !cmp || (cmp > 0 && hoid_compare(&hoid, end) < 0);
4068a02a946dSIlya Dryomov }
4069a02a946dSIlya Dryomov 
4070a02a946dSIlya Dryomov static void handle_backoff_unblock(struct ceph_osd *osd,
4071a02a946dSIlya Dryomov 				   const struct MOSDBackoff *m)
4072a02a946dSIlya Dryomov {
4073a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
4074a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
4075a02a946dSIlya Dryomov 	struct rb_node *n;
4076a02a946dSIlya Dryomov 
4077a02a946dSIlya Dryomov 	dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4078a02a946dSIlya Dryomov 	     m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4079a02a946dSIlya Dryomov 
4080a02a946dSIlya Dryomov 	backoff = lookup_backoff_by_id(&osd->o_backoffs_by_id, m->id);
4081a02a946dSIlya Dryomov 	if (!backoff) {
4082a02a946dSIlya Dryomov 		pr_err("%s osd%d spgid %llu.%xs%d id %llu backoff dne\n",
4083a02a946dSIlya Dryomov 		       __func__, osd->o_osd, m->spgid.pgid.pool,
4084a02a946dSIlya Dryomov 		       m->spgid.pgid.seed, m->spgid.shard, m->id);
4085a02a946dSIlya Dryomov 		return;
4086a02a946dSIlya Dryomov 	}
4087a02a946dSIlya Dryomov 
4088a02a946dSIlya Dryomov 	if (hoid_compare(backoff->begin, m->begin) &&
4089a02a946dSIlya Dryomov 	    hoid_compare(backoff->end, m->end)) {
4090a02a946dSIlya Dryomov 		pr_err("%s osd%d spgid %llu.%xs%d id %llu bad range?\n",
4091a02a946dSIlya Dryomov 		       __func__, osd->o_osd, m->spgid.pgid.pool,
4092a02a946dSIlya Dryomov 		       m->spgid.pgid.seed, m->spgid.shard, m->id);
4093a02a946dSIlya Dryomov 		/* unblock it anyway... */
4094a02a946dSIlya Dryomov 	}
4095a02a946dSIlya Dryomov 
4096a02a946dSIlya Dryomov 	spg = lookup_spg_mapping(&osd->o_backoff_mappings, &backoff->spgid);
4097a02a946dSIlya Dryomov 	BUG_ON(!spg);
4098a02a946dSIlya Dryomov 
4099a02a946dSIlya Dryomov 	erase_backoff(&spg->backoffs, backoff);
4100a02a946dSIlya Dryomov 	erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4101a02a946dSIlya Dryomov 	free_backoff(backoff);
4102a02a946dSIlya Dryomov 
4103a02a946dSIlya Dryomov 	if (RB_EMPTY_ROOT(&spg->backoffs)) {
4104a02a946dSIlya Dryomov 		erase_spg_mapping(&osd->o_backoff_mappings, spg);
4105a02a946dSIlya Dryomov 		free_spg_mapping(spg);
4106a02a946dSIlya Dryomov 	}
4107a02a946dSIlya Dryomov 
4108a02a946dSIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
4109a02a946dSIlya Dryomov 		struct ceph_osd_request *req =
4110a02a946dSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
4111a02a946dSIlya Dryomov 
4112a02a946dSIlya Dryomov 		if (!ceph_spg_compare(&req->r_t.spgid, &m->spgid)) {
4113a02a946dSIlya Dryomov 			/*
4114a02a946dSIlya Dryomov 			 * Match against @m, not @backoff -- the PG may
4115a02a946dSIlya Dryomov 			 * have split on the OSD.
4116a02a946dSIlya Dryomov 			 */
4117a02a946dSIlya Dryomov 			if (target_contained_by(&req->r_t, m->begin, m->end)) {
4118a02a946dSIlya Dryomov 				/*
4119a02a946dSIlya Dryomov 				 * If no other installed backoff applies,
4120a02a946dSIlya Dryomov 				 * resend.
4121a02a946dSIlya Dryomov 				 */
4122a02a946dSIlya Dryomov 				send_request(req);
4123a02a946dSIlya Dryomov 			}
4124a02a946dSIlya Dryomov 		}
4125a02a946dSIlya Dryomov 	}
4126a02a946dSIlya Dryomov }
4127a02a946dSIlya Dryomov 
4128a02a946dSIlya Dryomov static void handle_backoff(struct ceph_osd *osd, struct ceph_msg *msg)
4129a02a946dSIlya Dryomov {
4130a02a946dSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
4131a02a946dSIlya Dryomov 	struct MOSDBackoff m;
4132a02a946dSIlya Dryomov 	int ret;
4133a02a946dSIlya Dryomov 
4134a02a946dSIlya Dryomov 	down_read(&osdc->lock);
4135a02a946dSIlya Dryomov 	if (!osd_registered(osd)) {
4136a02a946dSIlya Dryomov 		dout("%s osd%d unknown\n", __func__, osd->o_osd);
4137a02a946dSIlya Dryomov 		up_read(&osdc->lock);
4138a02a946dSIlya Dryomov 		return;
4139a02a946dSIlya Dryomov 	}
4140a02a946dSIlya Dryomov 	WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
4141a02a946dSIlya Dryomov 
4142a02a946dSIlya Dryomov 	mutex_lock(&osd->lock);
4143a02a946dSIlya Dryomov 	ret = decode_MOSDBackoff(msg, &m);
4144a02a946dSIlya Dryomov 	if (ret) {
4145a02a946dSIlya Dryomov 		pr_err("failed to decode MOSDBackoff: %d\n", ret);
4146a02a946dSIlya Dryomov 		ceph_msg_dump(msg);
4147a02a946dSIlya Dryomov 		goto out_unlock;
4148a02a946dSIlya Dryomov 	}
4149a02a946dSIlya Dryomov 
4150a02a946dSIlya Dryomov 	switch (m.op) {
4151a02a946dSIlya Dryomov 	case CEPH_OSD_BACKOFF_OP_BLOCK:
4152a02a946dSIlya Dryomov 		handle_backoff_block(osd, &m);
4153a02a946dSIlya Dryomov 		break;
4154a02a946dSIlya Dryomov 	case CEPH_OSD_BACKOFF_OP_UNBLOCK:
4155a02a946dSIlya Dryomov 		handle_backoff_unblock(osd, &m);
4156a02a946dSIlya Dryomov 		break;
4157a02a946dSIlya Dryomov 	default:
4158a02a946dSIlya Dryomov 		pr_err("%s osd%d unknown op %d\n", __func__, osd->o_osd, m.op);
4159a02a946dSIlya Dryomov 	}
4160a02a946dSIlya Dryomov 
4161a02a946dSIlya Dryomov 	free_hoid(m.begin);
4162a02a946dSIlya Dryomov 	free_hoid(m.end);
4163a02a946dSIlya Dryomov 
4164a02a946dSIlya Dryomov out_unlock:
4165a02a946dSIlya Dryomov 	mutex_unlock(&osd->lock);
4166a02a946dSIlya Dryomov 	up_read(&osdc->lock);
4167a02a946dSIlya Dryomov }
4168a02a946dSIlya Dryomov 
41693d14c5d2SYehuda Sadeh /*
4170a40c4f10SYehuda Sadeh  * Process osd watch notifications
4171a40c4f10SYehuda Sadeh  */
41723c663bbdSAlex Elder static void handle_watch_notify(struct ceph_osd_client *osdc,
41733c663bbdSAlex Elder 				struct ceph_msg *msg)
4174a40c4f10SYehuda Sadeh {
4175922dab61SIlya Dryomov 	void *p = msg->front.iov_base;
4176922dab61SIlya Dryomov 	void *const end = p + msg->front.iov_len;
4177922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
4178922dab61SIlya Dryomov 	struct linger_work *lwork;
4179922dab61SIlya Dryomov 	u8 proto_ver, opcode;
4180922dab61SIlya Dryomov 	u64 cookie, notify_id;
4181922dab61SIlya Dryomov 	u64 notifier_id = 0;
418219079203SIlya Dryomov 	s32 return_code = 0;
4183922dab61SIlya Dryomov 	void *payload = NULL;
4184922dab61SIlya Dryomov 	u32 payload_len = 0;
4185a40c4f10SYehuda Sadeh 
4186a40c4f10SYehuda Sadeh 	ceph_decode_8_safe(&p, end, proto_ver, bad);
4187a40c4f10SYehuda Sadeh 	ceph_decode_8_safe(&p, end, opcode, bad);
4188a40c4f10SYehuda Sadeh 	ceph_decode_64_safe(&p, end, cookie, bad);
4189922dab61SIlya Dryomov 	p += 8; /* skip ver */
4190a40c4f10SYehuda Sadeh 	ceph_decode_64_safe(&p, end, notify_id, bad);
4191a40c4f10SYehuda Sadeh 
4192922dab61SIlya Dryomov 	if (proto_ver >= 1) {
4193922dab61SIlya Dryomov 		ceph_decode_32_safe(&p, end, payload_len, bad);
4194922dab61SIlya Dryomov 		ceph_decode_need(&p, end, payload_len, bad);
4195922dab61SIlya Dryomov 		payload = p;
4196922dab61SIlya Dryomov 		p += payload_len;
4197a40c4f10SYehuda Sadeh 	}
4198a40c4f10SYehuda Sadeh 
4199922dab61SIlya Dryomov 	if (le16_to_cpu(msg->hdr.version) >= 2)
420019079203SIlya Dryomov 		ceph_decode_32_safe(&p, end, return_code, bad);
4201922dab61SIlya Dryomov 
4202922dab61SIlya Dryomov 	if (le16_to_cpu(msg->hdr.version) >= 3)
4203922dab61SIlya Dryomov 		ceph_decode_64_safe(&p, end, notifier_id, bad);
4204922dab61SIlya Dryomov 
4205922dab61SIlya Dryomov 	down_read(&osdc->lock);
4206922dab61SIlya Dryomov 	lreq = lookup_linger_osdc(&osdc->linger_requests, cookie);
4207922dab61SIlya Dryomov 	if (!lreq) {
4208922dab61SIlya Dryomov 		dout("%s opcode %d cookie %llu dne\n", __func__, opcode,
4209922dab61SIlya Dryomov 		     cookie);
4210922dab61SIlya Dryomov 		goto out_unlock_osdc;
4211922dab61SIlya Dryomov 	}
4212922dab61SIlya Dryomov 
4213922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
421419079203SIlya Dryomov 	dout("%s opcode %d cookie %llu lreq %p is_watch %d\n", __func__,
421519079203SIlya Dryomov 	     opcode, cookie, lreq, lreq->is_watch);
4216922dab61SIlya Dryomov 	if (opcode == CEPH_WATCH_EVENT_DISCONNECT) {
4217922dab61SIlya Dryomov 		if (!lreq->last_error) {
4218922dab61SIlya Dryomov 			lreq->last_error = -ENOTCONN;
4219922dab61SIlya Dryomov 			queue_watch_error(lreq);
4220922dab61SIlya Dryomov 		}
422119079203SIlya Dryomov 	} else if (!lreq->is_watch) {
422219079203SIlya Dryomov 		/* CEPH_WATCH_EVENT_NOTIFY_COMPLETE */
422319079203SIlya Dryomov 		if (lreq->notify_id && lreq->notify_id != notify_id) {
422419079203SIlya Dryomov 			dout("lreq %p notify_id %llu != %llu, ignoring\n", lreq,
422519079203SIlya Dryomov 			     lreq->notify_id, notify_id);
422619079203SIlya Dryomov 		} else if (!completion_done(&lreq->notify_finish_wait)) {
422719079203SIlya Dryomov 			struct ceph_msg_data *data =
422819079203SIlya Dryomov 			    list_first_entry_or_null(&msg->data,
422919079203SIlya Dryomov 						     struct ceph_msg_data,
423019079203SIlya Dryomov 						     links);
423119079203SIlya Dryomov 
423219079203SIlya Dryomov 			if (data) {
423319079203SIlya Dryomov 				if (lreq->preply_pages) {
423419079203SIlya Dryomov 					WARN_ON(data->type !=
423519079203SIlya Dryomov 							CEPH_MSG_DATA_PAGES);
423619079203SIlya Dryomov 					*lreq->preply_pages = data->pages;
423719079203SIlya Dryomov 					*lreq->preply_len = data->length;
423819079203SIlya Dryomov 				} else {
423919079203SIlya Dryomov 					ceph_release_page_vector(data->pages,
424019079203SIlya Dryomov 					       calc_pages_for(0, data->length));
424119079203SIlya Dryomov 				}
424219079203SIlya Dryomov 			}
424319079203SIlya Dryomov 			lreq->notify_finish_error = return_code;
424419079203SIlya Dryomov 			complete_all(&lreq->notify_finish_wait);
424519079203SIlya Dryomov 		}
4246922dab61SIlya Dryomov 	} else {
4247922dab61SIlya Dryomov 		/* CEPH_WATCH_EVENT_NOTIFY */
4248922dab61SIlya Dryomov 		lwork = lwork_alloc(lreq, do_watch_notify);
4249922dab61SIlya Dryomov 		if (!lwork) {
4250922dab61SIlya Dryomov 			pr_err("failed to allocate notify-lwork\n");
4251922dab61SIlya Dryomov 			goto out_unlock_lreq;
4252922dab61SIlya Dryomov 		}
4253922dab61SIlya Dryomov 
4254922dab61SIlya Dryomov 		lwork->notify.notify_id = notify_id;
4255922dab61SIlya Dryomov 		lwork->notify.notifier_id = notifier_id;
4256922dab61SIlya Dryomov 		lwork->notify.payload = payload;
4257922dab61SIlya Dryomov 		lwork->notify.payload_len = payload_len;
4258922dab61SIlya Dryomov 		lwork->notify.msg = ceph_msg_get(msg);
4259922dab61SIlya Dryomov 		lwork_queue(lwork);
4260922dab61SIlya Dryomov 	}
4261922dab61SIlya Dryomov 
4262922dab61SIlya Dryomov out_unlock_lreq:
4263922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
4264922dab61SIlya Dryomov out_unlock_osdc:
4265922dab61SIlya Dryomov 	up_read(&osdc->lock);
4266a40c4f10SYehuda Sadeh 	return;
4267a40c4f10SYehuda Sadeh 
4268a40c4f10SYehuda Sadeh bad:
4269a40c4f10SYehuda Sadeh 	pr_err("osdc handle_watch_notify corrupt msg\n");
4270a40c4f10SYehuda Sadeh }
4271a40c4f10SYehuda Sadeh 
4272a40c4f10SYehuda Sadeh /*
42733d14c5d2SYehuda Sadeh  * Register request, send initial attempt.
42743d14c5d2SYehuda Sadeh  */
42753d14c5d2SYehuda Sadeh int ceph_osdc_start_request(struct ceph_osd_client *osdc,
42763d14c5d2SYehuda Sadeh 			    struct ceph_osd_request *req,
42773d14c5d2SYehuda Sadeh 			    bool nofail)
42783d14c5d2SYehuda Sadeh {
42795aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
42805aea3dcdSIlya Dryomov 	submit_request(req, false);
42815aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
42823d14c5d2SYehuda Sadeh 
42835aea3dcdSIlya Dryomov 	return 0;
42843d14c5d2SYehuda Sadeh }
42853d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_start_request);
42863d14c5d2SYehuda Sadeh 
42873d14c5d2SYehuda Sadeh /*
4288c297eb42SIlya Dryomov  * Unregister a registered request.  The request is not completed:
4289c297eb42SIlya Dryomov  * ->r_result isn't set and __complete_request() isn't called.
4290c9f9b93dSIlya Dryomov  */
4291c9f9b93dSIlya Dryomov void ceph_osdc_cancel_request(struct ceph_osd_request *req)
4292c9f9b93dSIlya Dryomov {
4293c9f9b93dSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
4294c9f9b93dSIlya Dryomov 
42955aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
42965aea3dcdSIlya Dryomov 	if (req->r_osd)
42975aea3dcdSIlya Dryomov 		cancel_request(req);
42985aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
4299c9f9b93dSIlya Dryomov }
4300c9f9b93dSIlya Dryomov EXPORT_SYMBOL(ceph_osdc_cancel_request);
4301c9f9b93dSIlya Dryomov 
4302c9f9b93dSIlya Dryomov /*
430342b06965SIlya Dryomov  * @timeout: in jiffies, 0 means "wait forever"
430442b06965SIlya Dryomov  */
430542b06965SIlya Dryomov static int wait_request_timeout(struct ceph_osd_request *req,
430642b06965SIlya Dryomov 				unsigned long timeout)
430742b06965SIlya Dryomov {
430842b06965SIlya Dryomov 	long left;
430942b06965SIlya Dryomov 
431042b06965SIlya Dryomov 	dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
43110e76abf2SYan, Zheng 	left = wait_for_completion_killable_timeout(&req->r_completion,
431242b06965SIlya Dryomov 						ceph_timeout_jiffies(timeout));
431342b06965SIlya Dryomov 	if (left <= 0) {
431442b06965SIlya Dryomov 		left = left ?: -ETIMEDOUT;
431542b06965SIlya Dryomov 		ceph_osdc_cancel_request(req);
431642b06965SIlya Dryomov 	} else {
431742b06965SIlya Dryomov 		left = req->r_result; /* completed */
431842b06965SIlya Dryomov 	}
431942b06965SIlya Dryomov 
432042b06965SIlya Dryomov 	return left;
432142b06965SIlya Dryomov }
432242b06965SIlya Dryomov 
432342b06965SIlya Dryomov /*
43243d14c5d2SYehuda Sadeh  * wait for a request to complete
43253d14c5d2SYehuda Sadeh  */
43263d14c5d2SYehuda Sadeh int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
43273d14c5d2SYehuda Sadeh 			   struct ceph_osd_request *req)
43283d14c5d2SYehuda Sadeh {
432942b06965SIlya Dryomov 	return wait_request_timeout(req, 0);
43303d14c5d2SYehuda Sadeh }
43313d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_wait_request);
43323d14c5d2SYehuda Sadeh 
43333d14c5d2SYehuda Sadeh /*
43343d14c5d2SYehuda Sadeh  * sync - wait for all in-flight requests to flush.  avoid starvation.
43353d14c5d2SYehuda Sadeh  */
43363d14c5d2SYehuda Sadeh void ceph_osdc_sync(struct ceph_osd_client *osdc)
43373d14c5d2SYehuda Sadeh {
43385aea3dcdSIlya Dryomov 	struct rb_node *n, *p;
43395aea3dcdSIlya Dryomov 	u64 last_tid = atomic64_read(&osdc->last_tid);
43403d14c5d2SYehuda Sadeh 
43415aea3dcdSIlya Dryomov again:
43425aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
43435aea3dcdSIlya Dryomov 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
43445aea3dcdSIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
43455aea3dcdSIlya Dryomov 
43465aea3dcdSIlya Dryomov 		mutex_lock(&osd->lock);
43475aea3dcdSIlya Dryomov 		for (p = rb_first(&osd->o_requests); p; p = rb_next(p)) {
43485aea3dcdSIlya Dryomov 			struct ceph_osd_request *req =
43495aea3dcdSIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
43505aea3dcdSIlya Dryomov 
43513d14c5d2SYehuda Sadeh 			if (req->r_tid > last_tid)
43523d14c5d2SYehuda Sadeh 				break;
43533d14c5d2SYehuda Sadeh 
43545aea3dcdSIlya Dryomov 			if (!(req->r_flags & CEPH_OSD_FLAG_WRITE))
43553d14c5d2SYehuda Sadeh 				continue;
43563d14c5d2SYehuda Sadeh 
43573d14c5d2SYehuda Sadeh 			ceph_osdc_get_request(req);
43585aea3dcdSIlya Dryomov 			mutex_unlock(&osd->lock);
43595aea3dcdSIlya Dryomov 			up_read(&osdc->lock);
43605aea3dcdSIlya Dryomov 			dout("%s waiting on req %p tid %llu last_tid %llu\n",
43615aea3dcdSIlya Dryomov 			     __func__, req, req->r_tid, last_tid);
4362b18b9550SIlya Dryomov 			wait_for_completion(&req->r_completion);
43633d14c5d2SYehuda Sadeh 			ceph_osdc_put_request(req);
43645aea3dcdSIlya Dryomov 			goto again;
43653d14c5d2SYehuda Sadeh 		}
43665aea3dcdSIlya Dryomov 
43675aea3dcdSIlya Dryomov 		mutex_unlock(&osd->lock);
43685aea3dcdSIlya Dryomov 	}
43695aea3dcdSIlya Dryomov 
43705aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
43715aea3dcdSIlya Dryomov 	dout("%s done last_tid %llu\n", __func__, last_tid);
43723d14c5d2SYehuda Sadeh }
43733d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_sync);
43743d14c5d2SYehuda Sadeh 
4375922dab61SIlya Dryomov static struct ceph_osd_request *
4376922dab61SIlya Dryomov alloc_linger_request(struct ceph_osd_linger_request *lreq)
4377922dab61SIlya Dryomov {
4378922dab61SIlya Dryomov 	struct ceph_osd_request *req;
4379922dab61SIlya Dryomov 
4380922dab61SIlya Dryomov 	req = ceph_osdc_alloc_request(lreq->osdc, NULL, 1, false, GFP_NOIO);
4381922dab61SIlya Dryomov 	if (!req)
4382922dab61SIlya Dryomov 		return NULL;
4383922dab61SIlya Dryomov 
4384922dab61SIlya Dryomov 	ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
4385922dab61SIlya Dryomov 	ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
4386922dab61SIlya Dryomov 
4387922dab61SIlya Dryomov 	if (ceph_osdc_alloc_messages(req, GFP_NOIO)) {
4388922dab61SIlya Dryomov 		ceph_osdc_put_request(req);
4389922dab61SIlya Dryomov 		return NULL;
4390922dab61SIlya Dryomov 	}
4391922dab61SIlya Dryomov 
4392922dab61SIlya Dryomov 	return req;
4393922dab61SIlya Dryomov }
4394922dab61SIlya Dryomov 
4395922dab61SIlya Dryomov /*
4396922dab61SIlya Dryomov  * Returns a handle, caller owns a ref.
4397922dab61SIlya Dryomov  */
4398922dab61SIlya Dryomov struct ceph_osd_linger_request *
4399922dab61SIlya Dryomov ceph_osdc_watch(struct ceph_osd_client *osdc,
4400922dab61SIlya Dryomov 		struct ceph_object_id *oid,
4401922dab61SIlya Dryomov 		struct ceph_object_locator *oloc,
4402922dab61SIlya Dryomov 		rados_watchcb2_t wcb,
4403922dab61SIlya Dryomov 		rados_watcherrcb_t errcb,
4404922dab61SIlya Dryomov 		void *data)
4405922dab61SIlya Dryomov {
4406922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
4407922dab61SIlya Dryomov 	int ret;
4408922dab61SIlya Dryomov 
4409922dab61SIlya Dryomov 	lreq = linger_alloc(osdc);
4410922dab61SIlya Dryomov 	if (!lreq)
4411922dab61SIlya Dryomov 		return ERR_PTR(-ENOMEM);
4412922dab61SIlya Dryomov 
441319079203SIlya Dryomov 	lreq->is_watch = true;
4414922dab61SIlya Dryomov 	lreq->wcb = wcb;
4415922dab61SIlya Dryomov 	lreq->errcb = errcb;
4416922dab61SIlya Dryomov 	lreq->data = data;
4417b07d3c4bSIlya Dryomov 	lreq->watch_valid_thru = jiffies;
4418922dab61SIlya Dryomov 
4419922dab61SIlya Dryomov 	ceph_oid_copy(&lreq->t.base_oid, oid);
4420922dab61SIlya Dryomov 	ceph_oloc_copy(&lreq->t.base_oloc, oloc);
442154ea0046SIlya Dryomov 	lreq->t.flags = CEPH_OSD_FLAG_WRITE;
44221134e091SDeepa Dinamani 	ktime_get_real_ts(&lreq->mtime);
4423922dab61SIlya Dryomov 
4424922dab61SIlya Dryomov 	lreq->reg_req = alloc_linger_request(lreq);
4425922dab61SIlya Dryomov 	if (!lreq->reg_req) {
4426922dab61SIlya Dryomov 		ret = -ENOMEM;
4427922dab61SIlya Dryomov 		goto err_put_lreq;
4428922dab61SIlya Dryomov 	}
4429922dab61SIlya Dryomov 
4430922dab61SIlya Dryomov 	lreq->ping_req = alloc_linger_request(lreq);
4431922dab61SIlya Dryomov 	if (!lreq->ping_req) {
4432922dab61SIlya Dryomov 		ret = -ENOMEM;
4433922dab61SIlya Dryomov 		goto err_put_lreq;
4434922dab61SIlya Dryomov 	}
4435922dab61SIlya Dryomov 
4436922dab61SIlya Dryomov 	down_write(&osdc->lock);
4437922dab61SIlya Dryomov 	linger_register(lreq); /* before osd_req_op_* */
4438922dab61SIlya Dryomov 	osd_req_op_watch_init(lreq->reg_req, 0, lreq->linger_id,
4439922dab61SIlya Dryomov 			      CEPH_OSD_WATCH_OP_WATCH);
4440922dab61SIlya Dryomov 	osd_req_op_watch_init(lreq->ping_req, 0, lreq->linger_id,
4441922dab61SIlya Dryomov 			      CEPH_OSD_WATCH_OP_PING);
4442922dab61SIlya Dryomov 	linger_submit(lreq);
4443922dab61SIlya Dryomov 	up_write(&osdc->lock);
4444922dab61SIlya Dryomov 
4445922dab61SIlya Dryomov 	ret = linger_reg_commit_wait(lreq);
4446922dab61SIlya Dryomov 	if (ret) {
4447922dab61SIlya Dryomov 		linger_cancel(lreq);
4448922dab61SIlya Dryomov 		goto err_put_lreq;
4449922dab61SIlya Dryomov 	}
4450922dab61SIlya Dryomov 
4451922dab61SIlya Dryomov 	return lreq;
4452922dab61SIlya Dryomov 
4453922dab61SIlya Dryomov err_put_lreq:
4454922dab61SIlya Dryomov 	linger_put(lreq);
4455922dab61SIlya Dryomov 	return ERR_PTR(ret);
4456922dab61SIlya Dryomov }
4457922dab61SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_watch);
4458922dab61SIlya Dryomov 
4459922dab61SIlya Dryomov /*
4460922dab61SIlya Dryomov  * Releases a ref.
4461922dab61SIlya Dryomov  *
4462922dab61SIlya Dryomov  * Times out after mount_timeout to preserve rbd unmap behaviour
4463922dab61SIlya Dryomov  * introduced in 2894e1d76974 ("rbd: timeout watch teardown on unmap
4464922dab61SIlya Dryomov  * with mount_timeout").
4465922dab61SIlya Dryomov  */
4466922dab61SIlya Dryomov int ceph_osdc_unwatch(struct ceph_osd_client *osdc,
4467922dab61SIlya Dryomov 		      struct ceph_osd_linger_request *lreq)
4468922dab61SIlya Dryomov {
4469922dab61SIlya Dryomov 	struct ceph_options *opts = osdc->client->options;
4470922dab61SIlya Dryomov 	struct ceph_osd_request *req;
4471922dab61SIlya Dryomov 	int ret;
4472922dab61SIlya Dryomov 
4473922dab61SIlya Dryomov 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4474922dab61SIlya Dryomov 	if (!req)
4475922dab61SIlya Dryomov 		return -ENOMEM;
4476922dab61SIlya Dryomov 
4477922dab61SIlya Dryomov 	ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
4478922dab61SIlya Dryomov 	ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
447954ea0046SIlya Dryomov 	req->r_flags = CEPH_OSD_FLAG_WRITE;
44801134e091SDeepa Dinamani 	ktime_get_real_ts(&req->r_mtime);
4481922dab61SIlya Dryomov 	osd_req_op_watch_init(req, 0, lreq->linger_id,
4482922dab61SIlya Dryomov 			      CEPH_OSD_WATCH_OP_UNWATCH);
4483922dab61SIlya Dryomov 
4484922dab61SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4485922dab61SIlya Dryomov 	if (ret)
4486922dab61SIlya Dryomov 		goto out_put_req;
4487922dab61SIlya Dryomov 
4488922dab61SIlya Dryomov 	ceph_osdc_start_request(osdc, req, false);
4489922dab61SIlya Dryomov 	linger_cancel(lreq);
4490922dab61SIlya Dryomov 	linger_put(lreq);
4491922dab61SIlya Dryomov 	ret = wait_request_timeout(req, opts->mount_timeout);
4492922dab61SIlya Dryomov 
4493922dab61SIlya Dryomov out_put_req:
4494922dab61SIlya Dryomov 	ceph_osdc_put_request(req);
4495922dab61SIlya Dryomov 	return ret;
4496922dab61SIlya Dryomov }
4497922dab61SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_unwatch);
4498922dab61SIlya Dryomov 
4499922dab61SIlya Dryomov static int osd_req_op_notify_ack_init(struct ceph_osd_request *req, int which,
4500922dab61SIlya Dryomov 				      u64 notify_id, u64 cookie, void *payload,
4501922dab61SIlya Dryomov 				      size_t payload_len)
4502922dab61SIlya Dryomov {
4503922dab61SIlya Dryomov 	struct ceph_osd_req_op *op;
4504922dab61SIlya Dryomov 	struct ceph_pagelist *pl;
4505922dab61SIlya Dryomov 	int ret;
4506922dab61SIlya Dryomov 
4507922dab61SIlya Dryomov 	op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY_ACK, 0);
4508922dab61SIlya Dryomov 
4509922dab61SIlya Dryomov 	pl = kmalloc(sizeof(*pl), GFP_NOIO);
4510922dab61SIlya Dryomov 	if (!pl)
4511922dab61SIlya Dryomov 		return -ENOMEM;
4512922dab61SIlya Dryomov 
4513922dab61SIlya Dryomov 	ceph_pagelist_init(pl);
4514922dab61SIlya Dryomov 	ret = ceph_pagelist_encode_64(pl, notify_id);
4515922dab61SIlya Dryomov 	ret |= ceph_pagelist_encode_64(pl, cookie);
4516922dab61SIlya Dryomov 	if (payload) {
4517922dab61SIlya Dryomov 		ret |= ceph_pagelist_encode_32(pl, payload_len);
4518922dab61SIlya Dryomov 		ret |= ceph_pagelist_append(pl, payload, payload_len);
4519922dab61SIlya Dryomov 	} else {
4520922dab61SIlya Dryomov 		ret |= ceph_pagelist_encode_32(pl, 0);
4521922dab61SIlya Dryomov 	}
4522922dab61SIlya Dryomov 	if (ret) {
4523922dab61SIlya Dryomov 		ceph_pagelist_release(pl);
4524922dab61SIlya Dryomov 		return -ENOMEM;
4525922dab61SIlya Dryomov 	}
4526922dab61SIlya Dryomov 
4527922dab61SIlya Dryomov 	ceph_osd_data_pagelist_init(&op->notify_ack.request_data, pl);
4528922dab61SIlya Dryomov 	op->indata_len = pl->length;
4529922dab61SIlya Dryomov 	return 0;
4530922dab61SIlya Dryomov }
4531922dab61SIlya Dryomov 
4532922dab61SIlya Dryomov int ceph_osdc_notify_ack(struct ceph_osd_client *osdc,
4533922dab61SIlya Dryomov 			 struct ceph_object_id *oid,
4534922dab61SIlya Dryomov 			 struct ceph_object_locator *oloc,
4535922dab61SIlya Dryomov 			 u64 notify_id,
4536922dab61SIlya Dryomov 			 u64 cookie,
4537922dab61SIlya Dryomov 			 void *payload,
4538922dab61SIlya Dryomov 			 size_t payload_len)
4539922dab61SIlya Dryomov {
4540922dab61SIlya Dryomov 	struct ceph_osd_request *req;
4541922dab61SIlya Dryomov 	int ret;
4542922dab61SIlya Dryomov 
4543922dab61SIlya Dryomov 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4544922dab61SIlya Dryomov 	if (!req)
4545922dab61SIlya Dryomov 		return -ENOMEM;
4546922dab61SIlya Dryomov 
4547922dab61SIlya Dryomov 	ceph_oid_copy(&req->r_base_oid, oid);
4548922dab61SIlya Dryomov 	ceph_oloc_copy(&req->r_base_oloc, oloc);
4549922dab61SIlya Dryomov 	req->r_flags = CEPH_OSD_FLAG_READ;
4550922dab61SIlya Dryomov 
4551922dab61SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4552922dab61SIlya Dryomov 	if (ret)
4553922dab61SIlya Dryomov 		goto out_put_req;
4554922dab61SIlya Dryomov 
4555922dab61SIlya Dryomov 	ret = osd_req_op_notify_ack_init(req, 0, notify_id, cookie, payload,
4556922dab61SIlya Dryomov 					 payload_len);
4557922dab61SIlya Dryomov 	if (ret)
4558922dab61SIlya Dryomov 		goto out_put_req;
4559922dab61SIlya Dryomov 
4560922dab61SIlya Dryomov 	ceph_osdc_start_request(osdc, req, false);
4561922dab61SIlya Dryomov 	ret = ceph_osdc_wait_request(osdc, req);
4562922dab61SIlya Dryomov 
4563922dab61SIlya Dryomov out_put_req:
4564922dab61SIlya Dryomov 	ceph_osdc_put_request(req);
4565922dab61SIlya Dryomov 	return ret;
4566922dab61SIlya Dryomov }
4567922dab61SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_notify_ack);
4568922dab61SIlya Dryomov 
456919079203SIlya Dryomov static int osd_req_op_notify_init(struct ceph_osd_request *req, int which,
457019079203SIlya Dryomov 				  u64 cookie, u32 prot_ver, u32 timeout,
457119079203SIlya Dryomov 				  void *payload, size_t payload_len)
457219079203SIlya Dryomov {
457319079203SIlya Dryomov 	struct ceph_osd_req_op *op;
457419079203SIlya Dryomov 	struct ceph_pagelist *pl;
457519079203SIlya Dryomov 	int ret;
457619079203SIlya Dryomov 
457719079203SIlya Dryomov 	op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY, 0);
457819079203SIlya Dryomov 	op->notify.cookie = cookie;
457919079203SIlya Dryomov 
458019079203SIlya Dryomov 	pl = kmalloc(sizeof(*pl), GFP_NOIO);
458119079203SIlya Dryomov 	if (!pl)
458219079203SIlya Dryomov 		return -ENOMEM;
458319079203SIlya Dryomov 
458419079203SIlya Dryomov 	ceph_pagelist_init(pl);
458519079203SIlya Dryomov 	ret = ceph_pagelist_encode_32(pl, 1); /* prot_ver */
458619079203SIlya Dryomov 	ret |= ceph_pagelist_encode_32(pl, timeout);
458719079203SIlya Dryomov 	ret |= ceph_pagelist_encode_32(pl, payload_len);
458819079203SIlya Dryomov 	ret |= ceph_pagelist_append(pl, payload, payload_len);
458919079203SIlya Dryomov 	if (ret) {
459019079203SIlya Dryomov 		ceph_pagelist_release(pl);
459119079203SIlya Dryomov 		return -ENOMEM;
459219079203SIlya Dryomov 	}
459319079203SIlya Dryomov 
459419079203SIlya Dryomov 	ceph_osd_data_pagelist_init(&op->notify.request_data, pl);
459519079203SIlya Dryomov 	op->indata_len = pl->length;
459619079203SIlya Dryomov 	return 0;
459719079203SIlya Dryomov }
459819079203SIlya Dryomov 
459919079203SIlya Dryomov /*
460019079203SIlya Dryomov  * @timeout: in seconds
460119079203SIlya Dryomov  *
460219079203SIlya Dryomov  * @preply_{pages,len} are initialized both on success and error.
460319079203SIlya Dryomov  * The caller is responsible for:
460419079203SIlya Dryomov  *
460519079203SIlya Dryomov  *     ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len))
460619079203SIlya Dryomov  */
460719079203SIlya Dryomov int ceph_osdc_notify(struct ceph_osd_client *osdc,
460819079203SIlya Dryomov 		     struct ceph_object_id *oid,
460919079203SIlya Dryomov 		     struct ceph_object_locator *oloc,
461019079203SIlya Dryomov 		     void *payload,
461119079203SIlya Dryomov 		     size_t payload_len,
461219079203SIlya Dryomov 		     u32 timeout,
461319079203SIlya Dryomov 		     struct page ***preply_pages,
461419079203SIlya Dryomov 		     size_t *preply_len)
461519079203SIlya Dryomov {
461619079203SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
461719079203SIlya Dryomov 	struct page **pages;
461819079203SIlya Dryomov 	int ret;
461919079203SIlya Dryomov 
462019079203SIlya Dryomov 	WARN_ON(!timeout);
462119079203SIlya Dryomov 	if (preply_pages) {
462219079203SIlya Dryomov 		*preply_pages = NULL;
462319079203SIlya Dryomov 		*preply_len = 0;
462419079203SIlya Dryomov 	}
462519079203SIlya Dryomov 
462619079203SIlya Dryomov 	lreq = linger_alloc(osdc);
462719079203SIlya Dryomov 	if (!lreq)
462819079203SIlya Dryomov 		return -ENOMEM;
462919079203SIlya Dryomov 
463019079203SIlya Dryomov 	lreq->preply_pages = preply_pages;
463119079203SIlya Dryomov 	lreq->preply_len = preply_len;
463219079203SIlya Dryomov 
463319079203SIlya Dryomov 	ceph_oid_copy(&lreq->t.base_oid, oid);
463419079203SIlya Dryomov 	ceph_oloc_copy(&lreq->t.base_oloc, oloc);
463519079203SIlya Dryomov 	lreq->t.flags = CEPH_OSD_FLAG_READ;
463619079203SIlya Dryomov 
463719079203SIlya Dryomov 	lreq->reg_req = alloc_linger_request(lreq);
463819079203SIlya Dryomov 	if (!lreq->reg_req) {
463919079203SIlya Dryomov 		ret = -ENOMEM;
464019079203SIlya Dryomov 		goto out_put_lreq;
464119079203SIlya Dryomov 	}
464219079203SIlya Dryomov 
464319079203SIlya Dryomov 	/* for notify_id */
464419079203SIlya Dryomov 	pages = ceph_alloc_page_vector(1, GFP_NOIO);
464519079203SIlya Dryomov 	if (IS_ERR(pages)) {
464619079203SIlya Dryomov 		ret = PTR_ERR(pages);
464719079203SIlya Dryomov 		goto out_put_lreq;
464819079203SIlya Dryomov 	}
464919079203SIlya Dryomov 
465019079203SIlya Dryomov 	down_write(&osdc->lock);
465119079203SIlya Dryomov 	linger_register(lreq); /* before osd_req_op_* */
465219079203SIlya Dryomov 	ret = osd_req_op_notify_init(lreq->reg_req, 0, lreq->linger_id, 1,
465319079203SIlya Dryomov 				     timeout, payload, payload_len);
465419079203SIlya Dryomov 	if (ret) {
465519079203SIlya Dryomov 		linger_unregister(lreq);
465619079203SIlya Dryomov 		up_write(&osdc->lock);
465719079203SIlya Dryomov 		ceph_release_page_vector(pages, 1);
465819079203SIlya Dryomov 		goto out_put_lreq;
465919079203SIlya Dryomov 	}
466019079203SIlya Dryomov 	ceph_osd_data_pages_init(osd_req_op_data(lreq->reg_req, 0, notify,
466119079203SIlya Dryomov 						 response_data),
466219079203SIlya Dryomov 				 pages, PAGE_SIZE, 0, false, true);
466319079203SIlya Dryomov 	linger_submit(lreq);
466419079203SIlya Dryomov 	up_write(&osdc->lock);
466519079203SIlya Dryomov 
466619079203SIlya Dryomov 	ret = linger_reg_commit_wait(lreq);
466719079203SIlya Dryomov 	if (!ret)
466819079203SIlya Dryomov 		ret = linger_notify_finish_wait(lreq);
466919079203SIlya Dryomov 	else
467019079203SIlya Dryomov 		dout("lreq %p failed to initiate notify %d\n", lreq, ret);
467119079203SIlya Dryomov 
467219079203SIlya Dryomov 	linger_cancel(lreq);
467319079203SIlya Dryomov out_put_lreq:
467419079203SIlya Dryomov 	linger_put(lreq);
467519079203SIlya Dryomov 	return ret;
467619079203SIlya Dryomov }
467719079203SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_notify);
467819079203SIlya Dryomov 
46793d14c5d2SYehuda Sadeh /*
4680b07d3c4bSIlya Dryomov  * Return the number of milliseconds since the watch was last
4681b07d3c4bSIlya Dryomov  * confirmed, or an error.  If there is an error, the watch is no
4682b07d3c4bSIlya Dryomov  * longer valid, and should be destroyed with ceph_osdc_unwatch().
4683b07d3c4bSIlya Dryomov  */
4684b07d3c4bSIlya Dryomov int ceph_osdc_watch_check(struct ceph_osd_client *osdc,
4685b07d3c4bSIlya Dryomov 			  struct ceph_osd_linger_request *lreq)
4686b07d3c4bSIlya Dryomov {
4687b07d3c4bSIlya Dryomov 	unsigned long stamp, age;
4688b07d3c4bSIlya Dryomov 	int ret;
4689b07d3c4bSIlya Dryomov 
4690b07d3c4bSIlya Dryomov 	down_read(&osdc->lock);
4691b07d3c4bSIlya Dryomov 	mutex_lock(&lreq->lock);
4692b07d3c4bSIlya Dryomov 	stamp = lreq->watch_valid_thru;
4693b07d3c4bSIlya Dryomov 	if (!list_empty(&lreq->pending_lworks)) {
4694b07d3c4bSIlya Dryomov 		struct linger_work *lwork =
4695b07d3c4bSIlya Dryomov 		    list_first_entry(&lreq->pending_lworks,
4696b07d3c4bSIlya Dryomov 				     struct linger_work,
4697b07d3c4bSIlya Dryomov 				     pending_item);
4698b07d3c4bSIlya Dryomov 
4699b07d3c4bSIlya Dryomov 		if (time_before(lwork->queued_stamp, stamp))
4700b07d3c4bSIlya Dryomov 			stamp = lwork->queued_stamp;
4701b07d3c4bSIlya Dryomov 	}
4702b07d3c4bSIlya Dryomov 	age = jiffies - stamp;
4703b07d3c4bSIlya Dryomov 	dout("%s lreq %p linger_id %llu age %lu last_error %d\n", __func__,
4704b07d3c4bSIlya Dryomov 	     lreq, lreq->linger_id, age, lreq->last_error);
4705b07d3c4bSIlya Dryomov 	/* we are truncating to msecs, so return a safe upper bound */
4706b07d3c4bSIlya Dryomov 	ret = lreq->last_error ?: 1 + jiffies_to_msecs(age);
4707b07d3c4bSIlya Dryomov 
4708b07d3c4bSIlya Dryomov 	mutex_unlock(&lreq->lock);
4709b07d3c4bSIlya Dryomov 	up_read(&osdc->lock);
4710b07d3c4bSIlya Dryomov 	return ret;
4711b07d3c4bSIlya Dryomov }
4712b07d3c4bSIlya Dryomov 
4713a4ed38d7SDouglas Fuller static int decode_watcher(void **p, void *end, struct ceph_watch_item *item)
4714a4ed38d7SDouglas Fuller {
4715a4ed38d7SDouglas Fuller 	u8 struct_v;
4716a4ed38d7SDouglas Fuller 	u32 struct_len;
4717a4ed38d7SDouglas Fuller 	int ret;
4718a4ed38d7SDouglas Fuller 
4719a4ed38d7SDouglas Fuller 	ret = ceph_start_decoding(p, end, 2, "watch_item_t",
4720a4ed38d7SDouglas Fuller 				  &struct_v, &struct_len);
4721a4ed38d7SDouglas Fuller 	if (ret)
4722a4ed38d7SDouglas Fuller 		return ret;
4723a4ed38d7SDouglas Fuller 
4724a4ed38d7SDouglas Fuller 	ceph_decode_copy(p, &item->name, sizeof(item->name));
4725a4ed38d7SDouglas Fuller 	item->cookie = ceph_decode_64(p);
4726a4ed38d7SDouglas Fuller 	*p += 4; /* skip timeout_seconds */
4727a4ed38d7SDouglas Fuller 	if (struct_v >= 2) {
4728a4ed38d7SDouglas Fuller 		ceph_decode_copy(p, &item->addr, sizeof(item->addr));
4729a4ed38d7SDouglas Fuller 		ceph_decode_addr(&item->addr);
4730a4ed38d7SDouglas Fuller 	}
4731a4ed38d7SDouglas Fuller 
4732a4ed38d7SDouglas Fuller 	dout("%s %s%llu cookie %llu addr %s\n", __func__,
4733a4ed38d7SDouglas Fuller 	     ENTITY_NAME(item->name), item->cookie,
4734a4ed38d7SDouglas Fuller 	     ceph_pr_addr(&item->addr.in_addr));
4735a4ed38d7SDouglas Fuller 	return 0;
4736a4ed38d7SDouglas Fuller }
4737a4ed38d7SDouglas Fuller 
4738a4ed38d7SDouglas Fuller static int decode_watchers(void **p, void *end,
4739a4ed38d7SDouglas Fuller 			   struct ceph_watch_item **watchers,
4740a4ed38d7SDouglas Fuller 			   u32 *num_watchers)
4741a4ed38d7SDouglas Fuller {
4742a4ed38d7SDouglas Fuller 	u8 struct_v;
4743a4ed38d7SDouglas Fuller 	u32 struct_len;
4744a4ed38d7SDouglas Fuller 	int i;
4745a4ed38d7SDouglas Fuller 	int ret;
4746a4ed38d7SDouglas Fuller 
4747a4ed38d7SDouglas Fuller 	ret = ceph_start_decoding(p, end, 1, "obj_list_watch_response_t",
4748a4ed38d7SDouglas Fuller 				  &struct_v, &struct_len);
4749a4ed38d7SDouglas Fuller 	if (ret)
4750a4ed38d7SDouglas Fuller 		return ret;
4751a4ed38d7SDouglas Fuller 
4752a4ed38d7SDouglas Fuller 	*num_watchers = ceph_decode_32(p);
4753a4ed38d7SDouglas Fuller 	*watchers = kcalloc(*num_watchers, sizeof(**watchers), GFP_NOIO);
4754a4ed38d7SDouglas Fuller 	if (!*watchers)
4755a4ed38d7SDouglas Fuller 		return -ENOMEM;
4756a4ed38d7SDouglas Fuller 
4757a4ed38d7SDouglas Fuller 	for (i = 0; i < *num_watchers; i++) {
4758a4ed38d7SDouglas Fuller 		ret = decode_watcher(p, end, *watchers + i);
4759a4ed38d7SDouglas Fuller 		if (ret) {
4760a4ed38d7SDouglas Fuller 			kfree(*watchers);
4761a4ed38d7SDouglas Fuller 			return ret;
4762a4ed38d7SDouglas Fuller 		}
4763a4ed38d7SDouglas Fuller 	}
4764a4ed38d7SDouglas Fuller 
4765a4ed38d7SDouglas Fuller 	return 0;
4766a4ed38d7SDouglas Fuller }
4767a4ed38d7SDouglas Fuller 
4768a4ed38d7SDouglas Fuller /*
4769a4ed38d7SDouglas Fuller  * On success, the caller is responsible for:
4770a4ed38d7SDouglas Fuller  *
4771a4ed38d7SDouglas Fuller  *     kfree(watchers);
4772a4ed38d7SDouglas Fuller  */
4773a4ed38d7SDouglas Fuller int ceph_osdc_list_watchers(struct ceph_osd_client *osdc,
4774a4ed38d7SDouglas Fuller 			    struct ceph_object_id *oid,
4775a4ed38d7SDouglas Fuller 			    struct ceph_object_locator *oloc,
4776a4ed38d7SDouglas Fuller 			    struct ceph_watch_item **watchers,
4777a4ed38d7SDouglas Fuller 			    u32 *num_watchers)
4778a4ed38d7SDouglas Fuller {
4779a4ed38d7SDouglas Fuller 	struct ceph_osd_request *req;
4780a4ed38d7SDouglas Fuller 	struct page **pages;
4781a4ed38d7SDouglas Fuller 	int ret;
4782a4ed38d7SDouglas Fuller 
4783a4ed38d7SDouglas Fuller 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4784a4ed38d7SDouglas Fuller 	if (!req)
4785a4ed38d7SDouglas Fuller 		return -ENOMEM;
4786a4ed38d7SDouglas Fuller 
4787a4ed38d7SDouglas Fuller 	ceph_oid_copy(&req->r_base_oid, oid);
4788a4ed38d7SDouglas Fuller 	ceph_oloc_copy(&req->r_base_oloc, oloc);
4789a4ed38d7SDouglas Fuller 	req->r_flags = CEPH_OSD_FLAG_READ;
4790a4ed38d7SDouglas Fuller 
4791a4ed38d7SDouglas Fuller 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4792a4ed38d7SDouglas Fuller 	if (ret)
4793a4ed38d7SDouglas Fuller 		goto out_put_req;
4794a4ed38d7SDouglas Fuller 
4795a4ed38d7SDouglas Fuller 	pages = ceph_alloc_page_vector(1, GFP_NOIO);
4796a4ed38d7SDouglas Fuller 	if (IS_ERR(pages)) {
4797a4ed38d7SDouglas Fuller 		ret = PTR_ERR(pages);
4798a4ed38d7SDouglas Fuller 		goto out_put_req;
4799a4ed38d7SDouglas Fuller 	}
4800a4ed38d7SDouglas Fuller 
4801a4ed38d7SDouglas Fuller 	osd_req_op_init(req, 0, CEPH_OSD_OP_LIST_WATCHERS, 0);
4802a4ed38d7SDouglas Fuller 	ceph_osd_data_pages_init(osd_req_op_data(req, 0, list_watchers,
4803a4ed38d7SDouglas Fuller 						 response_data),
4804a4ed38d7SDouglas Fuller 				 pages, PAGE_SIZE, 0, false, true);
4805a4ed38d7SDouglas Fuller 
4806a4ed38d7SDouglas Fuller 	ceph_osdc_start_request(osdc, req, false);
4807a4ed38d7SDouglas Fuller 	ret = ceph_osdc_wait_request(osdc, req);
4808a4ed38d7SDouglas Fuller 	if (ret >= 0) {
4809a4ed38d7SDouglas Fuller 		void *p = page_address(pages[0]);
4810a4ed38d7SDouglas Fuller 		void *const end = p + req->r_ops[0].outdata_len;
4811a4ed38d7SDouglas Fuller 
4812a4ed38d7SDouglas Fuller 		ret = decode_watchers(&p, end, watchers, num_watchers);
4813a4ed38d7SDouglas Fuller 	}
4814a4ed38d7SDouglas Fuller 
4815a4ed38d7SDouglas Fuller out_put_req:
4816a4ed38d7SDouglas Fuller 	ceph_osdc_put_request(req);
4817a4ed38d7SDouglas Fuller 	return ret;
4818a4ed38d7SDouglas Fuller }
4819a4ed38d7SDouglas Fuller EXPORT_SYMBOL(ceph_osdc_list_watchers);
4820a4ed38d7SDouglas Fuller 
4821b07d3c4bSIlya Dryomov /*
4822dd935f44SJosh Durgin  * Call all pending notify callbacks - for use after a watch is
4823dd935f44SJosh Durgin  * unregistered, to make sure no more callbacks for it will be invoked
4824dd935f44SJosh Durgin  */
4825f6479449Sstephen hemminger void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
4826dd935f44SJosh Durgin {
482799d16943SIlya Dryomov 	dout("%s osdc %p\n", __func__, osdc);
4828dd935f44SJosh Durgin 	flush_workqueue(osdc->notify_wq);
4829dd935f44SJosh Durgin }
4830dd935f44SJosh Durgin EXPORT_SYMBOL(ceph_osdc_flush_notifies);
4831dd935f44SJosh Durgin 
48327cca78c9SIlya Dryomov void ceph_osdc_maybe_request_map(struct ceph_osd_client *osdc)
48337cca78c9SIlya Dryomov {
48347cca78c9SIlya Dryomov 	down_read(&osdc->lock);
48357cca78c9SIlya Dryomov 	maybe_request_map(osdc);
48367cca78c9SIlya Dryomov 	up_read(&osdc->lock);
48377cca78c9SIlya Dryomov }
48387cca78c9SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_maybe_request_map);
4839dd935f44SJosh Durgin 
4840dd935f44SJosh Durgin /*
4841428a7158SDouglas Fuller  * Execute an OSD class method on an object.
4842428a7158SDouglas Fuller  *
4843428a7158SDouglas Fuller  * @flags: CEPH_OSD_FLAG_*
48442544a020SIlya Dryomov  * @resp_len: in/out param for reply length
4845428a7158SDouglas Fuller  */
4846428a7158SDouglas Fuller int ceph_osdc_call(struct ceph_osd_client *osdc,
4847428a7158SDouglas Fuller 		   struct ceph_object_id *oid,
4848428a7158SDouglas Fuller 		   struct ceph_object_locator *oloc,
4849428a7158SDouglas Fuller 		   const char *class, const char *method,
4850428a7158SDouglas Fuller 		   unsigned int flags,
4851428a7158SDouglas Fuller 		   struct page *req_page, size_t req_len,
4852428a7158SDouglas Fuller 		   struct page *resp_page, size_t *resp_len)
4853428a7158SDouglas Fuller {
4854428a7158SDouglas Fuller 	struct ceph_osd_request *req;
4855428a7158SDouglas Fuller 	int ret;
4856428a7158SDouglas Fuller 
48572544a020SIlya Dryomov 	if (req_len > PAGE_SIZE || (resp_page && *resp_len > PAGE_SIZE))
48582544a020SIlya Dryomov 		return -E2BIG;
48592544a020SIlya Dryomov 
4860428a7158SDouglas Fuller 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4861428a7158SDouglas Fuller 	if (!req)
4862428a7158SDouglas Fuller 		return -ENOMEM;
4863428a7158SDouglas Fuller 
4864428a7158SDouglas Fuller 	ceph_oid_copy(&req->r_base_oid, oid);
4865428a7158SDouglas Fuller 	ceph_oloc_copy(&req->r_base_oloc, oloc);
4866428a7158SDouglas Fuller 	req->r_flags = flags;
4867428a7158SDouglas Fuller 
4868428a7158SDouglas Fuller 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4869428a7158SDouglas Fuller 	if (ret)
4870428a7158SDouglas Fuller 		goto out_put_req;
4871428a7158SDouglas Fuller 
4872428a7158SDouglas Fuller 	osd_req_op_cls_init(req, 0, CEPH_OSD_OP_CALL, class, method);
4873428a7158SDouglas Fuller 	if (req_page)
4874428a7158SDouglas Fuller 		osd_req_op_cls_request_data_pages(req, 0, &req_page, req_len,
4875428a7158SDouglas Fuller 						  0, false, false);
4876428a7158SDouglas Fuller 	if (resp_page)
4877428a7158SDouglas Fuller 		osd_req_op_cls_response_data_pages(req, 0, &resp_page,
48782544a020SIlya Dryomov 						   *resp_len, 0, false, false);
4879428a7158SDouglas Fuller 
4880428a7158SDouglas Fuller 	ceph_osdc_start_request(osdc, req, false);
4881428a7158SDouglas Fuller 	ret = ceph_osdc_wait_request(osdc, req);
4882428a7158SDouglas Fuller 	if (ret >= 0) {
4883428a7158SDouglas Fuller 		ret = req->r_ops[0].rval;
4884428a7158SDouglas Fuller 		if (resp_page)
4885428a7158SDouglas Fuller 			*resp_len = req->r_ops[0].outdata_len;
4886428a7158SDouglas Fuller 	}
4887428a7158SDouglas Fuller 
4888428a7158SDouglas Fuller out_put_req:
4889428a7158SDouglas Fuller 	ceph_osdc_put_request(req);
4890428a7158SDouglas Fuller 	return ret;
4891428a7158SDouglas Fuller }
4892428a7158SDouglas Fuller EXPORT_SYMBOL(ceph_osdc_call);
4893428a7158SDouglas Fuller 
4894428a7158SDouglas Fuller /*
48953d14c5d2SYehuda Sadeh  * init, shutdown
48963d14c5d2SYehuda Sadeh  */
48973d14c5d2SYehuda Sadeh int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
48983d14c5d2SYehuda Sadeh {
48993d14c5d2SYehuda Sadeh 	int err;
49003d14c5d2SYehuda Sadeh 
49013d14c5d2SYehuda Sadeh 	dout("init\n");
49023d14c5d2SYehuda Sadeh 	osdc->client = client;
49035aea3dcdSIlya Dryomov 	init_rwsem(&osdc->lock);
49043d14c5d2SYehuda Sadeh 	osdc->osds = RB_ROOT;
49053d14c5d2SYehuda Sadeh 	INIT_LIST_HEAD(&osdc->osd_lru);
49069dd2845cSIlya Dryomov 	spin_lock_init(&osdc->osd_lru_lock);
49075aea3dcdSIlya Dryomov 	osd_init(&osdc->homeless_osd);
49085aea3dcdSIlya Dryomov 	osdc->homeless_osd.o_osdc = osdc;
49095aea3dcdSIlya Dryomov 	osdc->homeless_osd.o_osd = CEPH_HOMELESS_OSD;
4910264048afSIlya Dryomov 	osdc->last_linger_id = CEPH_LINGER_ID_START;
4911922dab61SIlya Dryomov 	osdc->linger_requests = RB_ROOT;
49124609245eSIlya Dryomov 	osdc->map_checks = RB_ROOT;
49134609245eSIlya Dryomov 	osdc->linger_map_checks = RB_ROOT;
49143d14c5d2SYehuda Sadeh 	INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
49153d14c5d2SYehuda Sadeh 	INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
49163d14c5d2SYehuda Sadeh 
49173d14c5d2SYehuda Sadeh 	err = -ENOMEM;
4918e5253a7bSIlya Dryomov 	osdc->osdmap = ceph_osdmap_alloc();
4919e5253a7bSIlya Dryomov 	if (!osdc->osdmap)
4920e5253a7bSIlya Dryomov 		goto out;
4921e5253a7bSIlya Dryomov 
49229e767adbSIlya Dryomov 	osdc->req_mempool = mempool_create_slab_pool(10,
49239e767adbSIlya Dryomov 						     ceph_osd_request_cache);
49243d14c5d2SYehuda Sadeh 	if (!osdc->req_mempool)
4925e5253a7bSIlya Dryomov 		goto out_map;
49263d14c5d2SYehuda Sadeh 
4927d50b409fSSage Weil 	err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
4928711da55dSIlya Dryomov 				PAGE_SIZE, 10, true, "osd_op");
49293d14c5d2SYehuda Sadeh 	if (err < 0)
49303d14c5d2SYehuda Sadeh 		goto out_mempool;
4931d50b409fSSage Weil 	err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
4932711da55dSIlya Dryomov 				PAGE_SIZE, 10, true, "osd_op_reply");
49333d14c5d2SYehuda Sadeh 	if (err < 0)
49343d14c5d2SYehuda Sadeh 		goto out_msgpool;
4935a40c4f10SYehuda Sadeh 
4936dbcae088SDan Carpenter 	err = -ENOMEM;
4937a40c4f10SYehuda Sadeh 	osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
4938dbcae088SDan Carpenter 	if (!osdc->notify_wq)
4939c172ec5cSIlya Dryomov 		goto out_msgpool_reply;
4940c172ec5cSIlya Dryomov 
4941fbca9635SIlya Dryomov 	schedule_delayed_work(&osdc->timeout_work,
4942fbca9635SIlya Dryomov 			      osdc->client->options->osd_keepalive_timeout);
4943b37ee1b9SIlya Dryomov 	schedule_delayed_work(&osdc->osds_timeout_work,
4944b37ee1b9SIlya Dryomov 	    round_jiffies_relative(osdc->client->options->osd_idle_ttl));
4945b37ee1b9SIlya Dryomov 
49463d14c5d2SYehuda Sadeh 	return 0;
49473d14c5d2SYehuda Sadeh 
4948c172ec5cSIlya Dryomov out_msgpool_reply:
4949c172ec5cSIlya Dryomov 	ceph_msgpool_destroy(&osdc->msgpool_op_reply);
49503d14c5d2SYehuda Sadeh out_msgpool:
49513d14c5d2SYehuda Sadeh 	ceph_msgpool_destroy(&osdc->msgpool_op);
49523d14c5d2SYehuda Sadeh out_mempool:
49533d14c5d2SYehuda Sadeh 	mempool_destroy(osdc->req_mempool);
4954e5253a7bSIlya Dryomov out_map:
4955e5253a7bSIlya Dryomov 	ceph_osdmap_destroy(osdc->osdmap);
49563d14c5d2SYehuda Sadeh out:
49573d14c5d2SYehuda Sadeh 	return err;
49583d14c5d2SYehuda Sadeh }
49593d14c5d2SYehuda Sadeh 
49603d14c5d2SYehuda Sadeh void ceph_osdc_stop(struct ceph_osd_client *osdc)
49613d14c5d2SYehuda Sadeh {
4962a40c4f10SYehuda Sadeh 	flush_workqueue(osdc->notify_wq);
4963a40c4f10SYehuda Sadeh 	destroy_workqueue(osdc->notify_wq);
49643d14c5d2SYehuda Sadeh 	cancel_delayed_work_sync(&osdc->timeout_work);
49653d14c5d2SYehuda Sadeh 	cancel_delayed_work_sync(&osdc->osds_timeout_work);
496642a2c09fSIlya Dryomov 
49675aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
496842a2c09fSIlya Dryomov 	while (!RB_EMPTY_ROOT(&osdc->osds)) {
496942a2c09fSIlya Dryomov 		struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
497042a2c09fSIlya Dryomov 						struct ceph_osd, o_node);
49715aea3dcdSIlya Dryomov 		close_osd(osd);
497242a2c09fSIlya Dryomov 	}
49735aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
497402113a0fSElena Reshetova 	WARN_ON(refcount_read(&osdc->homeless_osd.o_ref) != 1);
49755aea3dcdSIlya Dryomov 	osd_cleanup(&osdc->homeless_osd);
49765aea3dcdSIlya Dryomov 
49775aea3dcdSIlya Dryomov 	WARN_ON(!list_empty(&osdc->osd_lru));
4978922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_requests));
49794609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osdc->map_checks));
49804609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_map_checks));
49815aea3dcdSIlya Dryomov 	WARN_ON(atomic_read(&osdc->num_requests));
49825aea3dcdSIlya Dryomov 	WARN_ON(atomic_read(&osdc->num_homeless));
498342a2c09fSIlya Dryomov 
49843d14c5d2SYehuda Sadeh 	ceph_osdmap_destroy(osdc->osdmap);
49853d14c5d2SYehuda Sadeh 	mempool_destroy(osdc->req_mempool);
49863d14c5d2SYehuda Sadeh 	ceph_msgpool_destroy(&osdc->msgpool_op);
49873d14c5d2SYehuda Sadeh 	ceph_msgpool_destroy(&osdc->msgpool_op_reply);
49883d14c5d2SYehuda Sadeh }
49893d14c5d2SYehuda Sadeh 
49903d14c5d2SYehuda Sadeh /*
49913d14c5d2SYehuda Sadeh  * Read some contiguous pages.  If we cross a stripe boundary, shorten
49923d14c5d2SYehuda Sadeh  * *plen.  Return number of bytes read, or error.
49933d14c5d2SYehuda Sadeh  */
49943d14c5d2SYehuda Sadeh int ceph_osdc_readpages(struct ceph_osd_client *osdc,
49953d14c5d2SYehuda Sadeh 			struct ceph_vino vino, struct ceph_file_layout *layout,
49963d14c5d2SYehuda Sadeh 			u64 off, u64 *plen,
49973d14c5d2SYehuda Sadeh 			u32 truncate_seq, u64 truncate_size,
4998b7495fc2SSage Weil 			struct page **pages, int num_pages, int page_align)
49993d14c5d2SYehuda Sadeh {
50003d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
50013d14c5d2SYehuda Sadeh 	int rc = 0;
50023d14c5d2SYehuda Sadeh 
50033d14c5d2SYehuda Sadeh 	dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
50043d14c5d2SYehuda Sadeh 	     vino.snap, off, *plen);
5005715e4cd4SYan, Zheng 	req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
50063d14c5d2SYehuda Sadeh 				    CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
5007acead002SAlex Elder 				    NULL, truncate_seq, truncate_size,
5008153e5167SAlex Elder 				    false);
50096816282dSSage Weil 	if (IS_ERR(req))
50106816282dSSage Weil 		return PTR_ERR(req);
50113d14c5d2SYehuda Sadeh 
50123d14c5d2SYehuda Sadeh 	/* it may be a short read due to an object boundary */
5013406e2c9fSAlex Elder 	osd_req_op_extent_osd_data_pages(req, 0,
5014a4ce40a9SAlex Elder 				pages, *plen, page_align, false, false);
50153d14c5d2SYehuda Sadeh 
5016e0c59487SAlex Elder 	dout("readpages  final extent is %llu~%llu (%llu bytes align %d)\n",
501743bfe5deSAlex Elder 	     off, *plen, *plen, page_align);
50183d14c5d2SYehuda Sadeh 
50193d14c5d2SYehuda Sadeh 	rc = ceph_osdc_start_request(osdc, req, false);
50203d14c5d2SYehuda Sadeh 	if (!rc)
50213d14c5d2SYehuda Sadeh 		rc = ceph_osdc_wait_request(osdc, req);
50223d14c5d2SYehuda Sadeh 
50233d14c5d2SYehuda Sadeh 	ceph_osdc_put_request(req);
50243d14c5d2SYehuda Sadeh 	dout("readpages result %d\n", rc);
50253d14c5d2SYehuda Sadeh 	return rc;
50263d14c5d2SYehuda Sadeh }
50273d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_readpages);
50283d14c5d2SYehuda Sadeh 
50293d14c5d2SYehuda Sadeh /*
50303d14c5d2SYehuda Sadeh  * do a synchronous write on N pages
50313d14c5d2SYehuda Sadeh  */
50323d14c5d2SYehuda Sadeh int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
50333d14c5d2SYehuda Sadeh 			 struct ceph_file_layout *layout,
50343d14c5d2SYehuda Sadeh 			 struct ceph_snap_context *snapc,
50353d14c5d2SYehuda Sadeh 			 u64 off, u64 len,
50363d14c5d2SYehuda Sadeh 			 u32 truncate_seq, u64 truncate_size,
50373d14c5d2SYehuda Sadeh 			 struct timespec *mtime,
503824808826SAlex Elder 			 struct page **pages, int num_pages)
50393d14c5d2SYehuda Sadeh {
50403d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
50413d14c5d2SYehuda Sadeh 	int rc = 0;
5042b7495fc2SSage Weil 	int page_align = off & ~PAGE_MASK;
50433d14c5d2SYehuda Sadeh 
5044715e4cd4SYan, Zheng 	req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
504554ea0046SIlya Dryomov 				    CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
5046acead002SAlex Elder 				    snapc, truncate_seq, truncate_size,
5047153e5167SAlex Elder 				    true);
50486816282dSSage Weil 	if (IS_ERR(req))
50496816282dSSage Weil 		return PTR_ERR(req);
50503d14c5d2SYehuda Sadeh 
50513d14c5d2SYehuda Sadeh 	/* it may be a short write due to an object boundary */
5052406e2c9fSAlex Elder 	osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
505343bfe5deSAlex Elder 				false, false);
505443bfe5deSAlex Elder 	dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
50553d14c5d2SYehuda Sadeh 
5056bb873b53SIlya Dryomov 	req->r_mtime = *mtime;
505787f979d3SAlex Elder 	rc = ceph_osdc_start_request(osdc, req, true);
50583d14c5d2SYehuda Sadeh 	if (!rc)
50593d14c5d2SYehuda Sadeh 		rc = ceph_osdc_wait_request(osdc, req);
50603d14c5d2SYehuda Sadeh 
50613d14c5d2SYehuda Sadeh 	ceph_osdc_put_request(req);
50623d14c5d2SYehuda Sadeh 	if (rc == 0)
50633d14c5d2SYehuda Sadeh 		rc = len;
50643d14c5d2SYehuda Sadeh 	dout("writepages result %d\n", rc);
50653d14c5d2SYehuda Sadeh 	return rc;
50663d14c5d2SYehuda Sadeh }
50673d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_writepages);
50683d14c5d2SYehuda Sadeh 
50695522ae0bSAlex Elder int ceph_osdc_setup(void)
50705522ae0bSAlex Elder {
50713f1af42aSIlya Dryomov 	size_t size = sizeof(struct ceph_osd_request) +
50723f1af42aSIlya Dryomov 	    CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
50733f1af42aSIlya Dryomov 
50745522ae0bSAlex Elder 	BUG_ON(ceph_osd_request_cache);
50753f1af42aSIlya Dryomov 	ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
50763f1af42aSIlya Dryomov 						   0, 0, NULL);
50775522ae0bSAlex Elder 
50785522ae0bSAlex Elder 	return ceph_osd_request_cache ? 0 : -ENOMEM;
50795522ae0bSAlex Elder }
50805522ae0bSAlex Elder EXPORT_SYMBOL(ceph_osdc_setup);
50815522ae0bSAlex Elder 
50825522ae0bSAlex Elder void ceph_osdc_cleanup(void)
50835522ae0bSAlex Elder {
50845522ae0bSAlex Elder 	BUG_ON(!ceph_osd_request_cache);
50855522ae0bSAlex Elder 	kmem_cache_destroy(ceph_osd_request_cache);
50865522ae0bSAlex Elder 	ceph_osd_request_cache = NULL;
50875522ae0bSAlex Elder }
50885522ae0bSAlex Elder EXPORT_SYMBOL(ceph_osdc_cleanup);
50895522ae0bSAlex Elder 
50903d14c5d2SYehuda Sadeh /*
50913d14c5d2SYehuda Sadeh  * handle incoming message
50923d14c5d2SYehuda Sadeh  */
50933d14c5d2SYehuda Sadeh static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
50943d14c5d2SYehuda Sadeh {
50953d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
50965aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
50973d14c5d2SYehuda Sadeh 	int type = le16_to_cpu(msg->hdr.type);
50983d14c5d2SYehuda Sadeh 
50993d14c5d2SYehuda Sadeh 	switch (type) {
51003d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_MAP:
51013d14c5d2SYehuda Sadeh 		ceph_osdc_handle_map(osdc, msg);
51023d14c5d2SYehuda Sadeh 		break;
51033d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_OPREPLY:
51045aea3dcdSIlya Dryomov 		handle_reply(osd, msg);
51053d14c5d2SYehuda Sadeh 		break;
5106a02a946dSIlya Dryomov 	case CEPH_MSG_OSD_BACKOFF:
5107a02a946dSIlya Dryomov 		handle_backoff(osd, msg);
5108a02a946dSIlya Dryomov 		break;
5109a40c4f10SYehuda Sadeh 	case CEPH_MSG_WATCH_NOTIFY:
5110a40c4f10SYehuda Sadeh 		handle_watch_notify(osdc, msg);
5111a40c4f10SYehuda Sadeh 		break;
51123d14c5d2SYehuda Sadeh 
51133d14c5d2SYehuda Sadeh 	default:
51143d14c5d2SYehuda Sadeh 		pr_err("received unknown message type %d %s\n", type,
51153d14c5d2SYehuda Sadeh 		       ceph_msg_type_name(type));
51163d14c5d2SYehuda Sadeh 	}
51175aea3dcdSIlya Dryomov 
51183d14c5d2SYehuda Sadeh 	ceph_msg_put(msg);
51193d14c5d2SYehuda Sadeh }
51203d14c5d2SYehuda Sadeh 
51213d14c5d2SYehuda Sadeh /*
5122d15f9d69SIlya Dryomov  * Lookup and return message for incoming reply.  Don't try to do
5123d15f9d69SIlya Dryomov  * anything about a larger than preallocated data portion of the
5124d15f9d69SIlya Dryomov  * message at the moment - for now, just skip the message.
51253d14c5d2SYehuda Sadeh  */
51263d14c5d2SYehuda Sadeh static struct ceph_msg *get_reply(struct ceph_connection *con,
51273d14c5d2SYehuda Sadeh 				  struct ceph_msg_header *hdr,
51283d14c5d2SYehuda Sadeh 				  int *skip)
51293d14c5d2SYehuda Sadeh {
51303d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
51313d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = osd->o_osdc;
51325aea3dcdSIlya Dryomov 	struct ceph_msg *m = NULL;
51333d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
51343f0a4ac5SIlya Dryomov 	int front_len = le32_to_cpu(hdr->front_len);
51353d14c5d2SYehuda Sadeh 	int data_len = le32_to_cpu(hdr->data_len);
51365aea3dcdSIlya Dryomov 	u64 tid = le64_to_cpu(hdr->tid);
51373d14c5d2SYehuda Sadeh 
51385aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
51395aea3dcdSIlya Dryomov 	if (!osd_registered(osd)) {
51405aea3dcdSIlya Dryomov 		dout("%s osd%d unknown, skipping\n", __func__, osd->o_osd);
51415aea3dcdSIlya Dryomov 		*skip = 1;
51425aea3dcdSIlya Dryomov 		goto out_unlock_osdc;
51435aea3dcdSIlya Dryomov 	}
51445aea3dcdSIlya Dryomov 	WARN_ON(osd->o_osd != le64_to_cpu(hdr->src.num));
51455aea3dcdSIlya Dryomov 
51465aea3dcdSIlya Dryomov 	mutex_lock(&osd->lock);
51475aea3dcdSIlya Dryomov 	req = lookup_request(&osd->o_requests, tid);
51483d14c5d2SYehuda Sadeh 	if (!req) {
5149cd8140c6SIlya Dryomov 		dout("%s osd%d tid %llu unknown, skipping\n", __func__,
5150cd8140c6SIlya Dryomov 		     osd->o_osd, tid);
5151d15f9d69SIlya Dryomov 		*skip = 1;
51525aea3dcdSIlya Dryomov 		goto out_unlock_session;
51533d14c5d2SYehuda Sadeh 	}
51543d14c5d2SYehuda Sadeh 
51558921d114SAlex Elder 	ceph_msg_revoke_incoming(req->r_reply);
51563d14c5d2SYehuda Sadeh 
5157f2be82b0SIlya Dryomov 	if (front_len > req->r_reply->front_alloc_len) {
5158d15f9d69SIlya Dryomov 		pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
5159d15f9d69SIlya Dryomov 			__func__, osd->o_osd, req->r_tid, front_len,
5160d15f9d69SIlya Dryomov 			req->r_reply->front_alloc_len);
51613f0a4ac5SIlya Dryomov 		m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
51623f0a4ac5SIlya Dryomov 				 false);
51633d14c5d2SYehuda Sadeh 		if (!m)
51645aea3dcdSIlya Dryomov 			goto out_unlock_session;
51653d14c5d2SYehuda Sadeh 		ceph_msg_put(req->r_reply);
51663d14c5d2SYehuda Sadeh 		req->r_reply = m;
51673d14c5d2SYehuda Sadeh 	}
51683d14c5d2SYehuda Sadeh 
5169d15f9d69SIlya Dryomov 	if (data_len > req->r_reply->data_length) {
5170d15f9d69SIlya Dryomov 		pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
5171d15f9d69SIlya Dryomov 			__func__, osd->o_osd, req->r_tid, data_len,
5172d15f9d69SIlya Dryomov 			req->r_reply->data_length);
51733d14c5d2SYehuda Sadeh 		m = NULL;
5174d15f9d69SIlya Dryomov 		*skip = 1;
51755aea3dcdSIlya Dryomov 		goto out_unlock_session;
51763d14c5d2SYehuda Sadeh 	}
5177d15f9d69SIlya Dryomov 
5178d15f9d69SIlya Dryomov 	m = ceph_msg_get(req->r_reply);
51793d14c5d2SYehuda Sadeh 	dout("get_reply tid %lld %p\n", tid, m);
51803d14c5d2SYehuda Sadeh 
51815aea3dcdSIlya Dryomov out_unlock_session:
51825aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
51835aea3dcdSIlya Dryomov out_unlock_osdc:
51845aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
51853d14c5d2SYehuda Sadeh 	return m;
51863d14c5d2SYehuda Sadeh }
51873d14c5d2SYehuda Sadeh 
518819079203SIlya Dryomov /*
518919079203SIlya Dryomov  * TODO: switch to a msg-owned pagelist
519019079203SIlya Dryomov  */
519119079203SIlya Dryomov static struct ceph_msg *alloc_msg_with_page_vector(struct ceph_msg_header *hdr)
519219079203SIlya Dryomov {
519319079203SIlya Dryomov 	struct ceph_msg *m;
519419079203SIlya Dryomov 	int type = le16_to_cpu(hdr->type);
519519079203SIlya Dryomov 	u32 front_len = le32_to_cpu(hdr->front_len);
519619079203SIlya Dryomov 	u32 data_len = le32_to_cpu(hdr->data_len);
519719079203SIlya Dryomov 
519819079203SIlya Dryomov 	m = ceph_msg_new(type, front_len, GFP_NOIO, false);
519919079203SIlya Dryomov 	if (!m)
520019079203SIlya Dryomov 		return NULL;
520119079203SIlya Dryomov 
520219079203SIlya Dryomov 	if (data_len) {
520319079203SIlya Dryomov 		struct page **pages;
520419079203SIlya Dryomov 		struct ceph_osd_data osd_data;
520519079203SIlya Dryomov 
520619079203SIlya Dryomov 		pages = ceph_alloc_page_vector(calc_pages_for(0, data_len),
520719079203SIlya Dryomov 					       GFP_NOIO);
5208c22e853aSWei Yongjun 		if (IS_ERR(pages)) {
520919079203SIlya Dryomov 			ceph_msg_put(m);
521019079203SIlya Dryomov 			return NULL;
521119079203SIlya Dryomov 		}
521219079203SIlya Dryomov 
521319079203SIlya Dryomov 		ceph_osd_data_pages_init(&osd_data, pages, data_len, 0, false,
521419079203SIlya Dryomov 					 false);
521519079203SIlya Dryomov 		ceph_osdc_msg_data_add(m, &osd_data);
521619079203SIlya Dryomov 	}
521719079203SIlya Dryomov 
521819079203SIlya Dryomov 	return m;
521919079203SIlya Dryomov }
522019079203SIlya Dryomov 
52213d14c5d2SYehuda Sadeh static struct ceph_msg *alloc_msg(struct ceph_connection *con,
52223d14c5d2SYehuda Sadeh 				  struct ceph_msg_header *hdr,
52233d14c5d2SYehuda Sadeh 				  int *skip)
52243d14c5d2SYehuda Sadeh {
52253d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
52263d14c5d2SYehuda Sadeh 	int type = le16_to_cpu(hdr->type);
52273d14c5d2SYehuda Sadeh 
52281c20f2d2SAlex Elder 	*skip = 0;
52293d14c5d2SYehuda Sadeh 	switch (type) {
52303d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_MAP:
5231a02a946dSIlya Dryomov 	case CEPH_MSG_OSD_BACKOFF:
5232a40c4f10SYehuda Sadeh 	case CEPH_MSG_WATCH_NOTIFY:
523319079203SIlya Dryomov 		return alloc_msg_with_page_vector(hdr);
52343d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_OPREPLY:
52353d14c5d2SYehuda Sadeh 		return get_reply(con, hdr, skip);
52363d14c5d2SYehuda Sadeh 	default:
52375aea3dcdSIlya Dryomov 		pr_warn("%s osd%d unknown msg type %d, skipping\n", __func__,
52385aea3dcdSIlya Dryomov 			osd->o_osd, type);
52393d14c5d2SYehuda Sadeh 		*skip = 1;
52403d14c5d2SYehuda Sadeh 		return NULL;
52413d14c5d2SYehuda Sadeh 	}
52423d14c5d2SYehuda Sadeh }
52433d14c5d2SYehuda Sadeh 
52443d14c5d2SYehuda Sadeh /*
52453d14c5d2SYehuda Sadeh  * Wrappers to refcount containing ceph_osd struct
52463d14c5d2SYehuda Sadeh  */
52473d14c5d2SYehuda Sadeh static struct ceph_connection *get_osd_con(struct ceph_connection *con)
52483d14c5d2SYehuda Sadeh {
52493d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
52503d14c5d2SYehuda Sadeh 	if (get_osd(osd))
52513d14c5d2SYehuda Sadeh 		return con;
52523d14c5d2SYehuda Sadeh 	return NULL;
52533d14c5d2SYehuda Sadeh }
52543d14c5d2SYehuda Sadeh 
52553d14c5d2SYehuda Sadeh static void put_osd_con(struct ceph_connection *con)
52563d14c5d2SYehuda Sadeh {
52573d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
52583d14c5d2SYehuda Sadeh 	put_osd(osd);
52593d14c5d2SYehuda Sadeh }
52603d14c5d2SYehuda Sadeh 
52613d14c5d2SYehuda Sadeh /*
52623d14c5d2SYehuda Sadeh  * authentication
52633d14c5d2SYehuda Sadeh  */
5264a3530df3SAlex Elder /*
5265a3530df3SAlex Elder  * Note: returned pointer is the address of a structure that's
5266a3530df3SAlex Elder  * managed separately.  Caller must *not* attempt to free it.
5267a3530df3SAlex Elder  */
5268a3530df3SAlex Elder static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
52698f43fb53SAlex Elder 					int *proto, int force_new)
52703d14c5d2SYehuda Sadeh {
52713d14c5d2SYehuda Sadeh 	struct ceph_osd *o = con->private;
52723d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = o->o_osdc;
52733d14c5d2SYehuda Sadeh 	struct ceph_auth_client *ac = osdc->client->monc.auth;
527474f1869fSAlex Elder 	struct ceph_auth_handshake *auth = &o->o_auth;
52753d14c5d2SYehuda Sadeh 
527674f1869fSAlex Elder 	if (force_new && auth->authorizer) {
52776c1ea260SIlya Dryomov 		ceph_auth_destroy_authorizer(auth->authorizer);
527874f1869fSAlex Elder 		auth->authorizer = NULL;
52793d14c5d2SYehuda Sadeh 	}
528027859f97SSage Weil 	if (!auth->authorizer) {
528127859f97SSage Weil 		int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
5282a3530df3SAlex Elder 						      auth);
52833d14c5d2SYehuda Sadeh 		if (ret)
5284a3530df3SAlex Elder 			return ERR_PTR(ret);
528527859f97SSage Weil 	} else {
528627859f97SSage Weil 		int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
52870bed9b5cSSage Weil 						     auth);
52880bed9b5cSSage Weil 		if (ret)
52890bed9b5cSSage Weil 			return ERR_PTR(ret);
52903d14c5d2SYehuda Sadeh 	}
52913d14c5d2SYehuda Sadeh 	*proto = ac->protocol;
529274f1869fSAlex Elder 
5293a3530df3SAlex Elder 	return auth;
52943d14c5d2SYehuda Sadeh }
52953d14c5d2SYehuda Sadeh 
52963d14c5d2SYehuda Sadeh 
52970dde5848SIlya Dryomov static int verify_authorizer_reply(struct ceph_connection *con)
52983d14c5d2SYehuda Sadeh {
52993d14c5d2SYehuda Sadeh 	struct ceph_osd *o = con->private;
53003d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = o->o_osdc;
53013d14c5d2SYehuda Sadeh 	struct ceph_auth_client *ac = osdc->client->monc.auth;
53023d14c5d2SYehuda Sadeh 
53030dde5848SIlya Dryomov 	return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer);
53043d14c5d2SYehuda Sadeh }
53053d14c5d2SYehuda Sadeh 
53063d14c5d2SYehuda Sadeh static int invalidate_authorizer(struct ceph_connection *con)
53073d14c5d2SYehuda Sadeh {
53083d14c5d2SYehuda Sadeh 	struct ceph_osd *o = con->private;
53093d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = o->o_osdc;
53103d14c5d2SYehuda Sadeh 	struct ceph_auth_client *ac = osdc->client->monc.auth;
53113d14c5d2SYehuda Sadeh 
531227859f97SSage Weil 	ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
53133d14c5d2SYehuda Sadeh 	return ceph_monc_validate_auth(&osdc->client->monc);
53143d14c5d2SYehuda Sadeh }
53153d14c5d2SYehuda Sadeh 
53168cb441c0SIlya Dryomov static void osd_reencode_message(struct ceph_msg *msg)
53178cb441c0SIlya Dryomov {
5318914902afSIlya Dryomov 	int type = le16_to_cpu(msg->hdr.type);
5319914902afSIlya Dryomov 
5320914902afSIlya Dryomov 	if (type == CEPH_MSG_OSD_OP)
53218cb441c0SIlya Dryomov 		encode_request_finish(msg);
53228cb441c0SIlya Dryomov }
53238cb441c0SIlya Dryomov 
532479dbd1baSIlya Dryomov static int osd_sign_message(struct ceph_msg *msg)
532533d07337SYan, Zheng {
532679dbd1baSIlya Dryomov 	struct ceph_osd *o = msg->con->private;
532733d07337SYan, Zheng 	struct ceph_auth_handshake *auth = &o->o_auth;
532879dbd1baSIlya Dryomov 
532933d07337SYan, Zheng 	return ceph_auth_sign_message(auth, msg);
533033d07337SYan, Zheng }
533133d07337SYan, Zheng 
533279dbd1baSIlya Dryomov static int osd_check_message_signature(struct ceph_msg *msg)
533333d07337SYan, Zheng {
533479dbd1baSIlya Dryomov 	struct ceph_osd *o = msg->con->private;
533533d07337SYan, Zheng 	struct ceph_auth_handshake *auth = &o->o_auth;
533679dbd1baSIlya Dryomov 
533733d07337SYan, Zheng 	return ceph_auth_check_message_signature(auth, msg);
533833d07337SYan, Zheng }
533933d07337SYan, Zheng 
53403d14c5d2SYehuda Sadeh static const struct ceph_connection_operations osd_con_ops = {
53413d14c5d2SYehuda Sadeh 	.get = get_osd_con,
53423d14c5d2SYehuda Sadeh 	.put = put_osd_con,
53433d14c5d2SYehuda Sadeh 	.dispatch = dispatch,
53443d14c5d2SYehuda Sadeh 	.get_authorizer = get_authorizer,
53453d14c5d2SYehuda Sadeh 	.verify_authorizer_reply = verify_authorizer_reply,
53463d14c5d2SYehuda Sadeh 	.invalidate_authorizer = invalidate_authorizer,
53473d14c5d2SYehuda Sadeh 	.alloc_msg = alloc_msg,
53488cb441c0SIlya Dryomov 	.reencode_message = osd_reencode_message,
534979dbd1baSIlya Dryomov 	.sign_message = osd_sign_message,
535079dbd1baSIlya Dryomov 	.check_message_signature = osd_check_message_signature,
53515aea3dcdSIlya Dryomov 	.fault = osd_fault,
53523d14c5d2SYehuda Sadeh };
5353