xref: /openbmc/linux/net/ceph/osd_client.c (revision ae78dd81)
1a4ce40a9SAlex Elder 
23d14c5d2SYehuda Sadeh #include <linux/ceph/ceph_debug.h>
33d14c5d2SYehuda Sadeh 
43d14c5d2SYehuda Sadeh #include <linux/module.h>
53d14c5d2SYehuda Sadeh #include <linux/err.h>
63d14c5d2SYehuda Sadeh #include <linux/highmem.h>
73d14c5d2SYehuda Sadeh #include <linux/mm.h>
83d14c5d2SYehuda Sadeh #include <linux/pagemap.h>
93d14c5d2SYehuda Sadeh #include <linux/slab.h>
103d14c5d2SYehuda Sadeh #include <linux/uaccess.h>
113d14c5d2SYehuda Sadeh #ifdef CONFIG_BLOCK
123d14c5d2SYehuda Sadeh #include <linux/bio.h>
133d14c5d2SYehuda Sadeh #endif
143d14c5d2SYehuda Sadeh 
158cb441c0SIlya Dryomov #include <linux/ceph/ceph_features.h>
163d14c5d2SYehuda Sadeh #include <linux/ceph/libceph.h>
173d14c5d2SYehuda Sadeh #include <linux/ceph/osd_client.h>
183d14c5d2SYehuda Sadeh #include <linux/ceph/messenger.h>
193d14c5d2SYehuda Sadeh #include <linux/ceph/decode.h>
203d14c5d2SYehuda Sadeh #include <linux/ceph/auth.h>
213d14c5d2SYehuda Sadeh #include <linux/ceph/pagelist.h>
223d14c5d2SYehuda Sadeh 
233d14c5d2SYehuda Sadeh #define OSD_OPREPLY_FRONT_LEN	512
243d14c5d2SYehuda Sadeh 
255522ae0bSAlex Elder static struct kmem_cache	*ceph_osd_request_cache;
265522ae0bSAlex Elder 
273d14c5d2SYehuda Sadeh static const struct ceph_connection_operations osd_con_ops;
283d14c5d2SYehuda Sadeh 
293d14c5d2SYehuda Sadeh /*
303d14c5d2SYehuda Sadeh  * Implement client access to distributed object storage cluster.
313d14c5d2SYehuda Sadeh  *
323d14c5d2SYehuda Sadeh  * All data objects are stored within a cluster/cloud of OSDs, or
333d14c5d2SYehuda Sadeh  * "object storage devices."  (Note that Ceph OSDs have _nothing_ to
343d14c5d2SYehuda Sadeh  * do with the T10 OSD extensions to SCSI.)  Ceph OSDs are simply
353d14c5d2SYehuda Sadeh  * remote daemons serving up and coordinating consistent and safe
363d14c5d2SYehuda Sadeh  * access to storage.
373d14c5d2SYehuda Sadeh  *
383d14c5d2SYehuda Sadeh  * Cluster membership and the mapping of data objects onto storage devices
393d14c5d2SYehuda Sadeh  * are described by the osd map.
403d14c5d2SYehuda Sadeh  *
413d14c5d2SYehuda Sadeh  * We keep track of pending OSD requests (read, write), resubmit
423d14c5d2SYehuda Sadeh  * requests to different OSDs when the cluster topology/data layout
433d14c5d2SYehuda Sadeh  * change, or retry the affected requests when the communications
443d14c5d2SYehuda Sadeh  * channel with an OSD is reset.
453d14c5d2SYehuda Sadeh  */
463d14c5d2SYehuda Sadeh 
475aea3dcdSIlya Dryomov static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req);
485aea3dcdSIlya Dryomov static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req);
49922dab61SIlya Dryomov static void link_linger(struct ceph_osd *osd,
50922dab61SIlya Dryomov 			struct ceph_osd_linger_request *lreq);
51922dab61SIlya Dryomov static void unlink_linger(struct ceph_osd *osd,
52922dab61SIlya Dryomov 			  struct ceph_osd_linger_request *lreq);
53a02a946dSIlya Dryomov static void clear_backoffs(struct ceph_osd *osd);
545aea3dcdSIlya Dryomov 
555aea3dcdSIlya Dryomov #if 1
565aea3dcdSIlya Dryomov static inline bool rwsem_is_wrlocked(struct rw_semaphore *sem)
575aea3dcdSIlya Dryomov {
585aea3dcdSIlya Dryomov 	bool wrlocked = true;
595aea3dcdSIlya Dryomov 
605aea3dcdSIlya Dryomov 	if (unlikely(down_read_trylock(sem))) {
615aea3dcdSIlya Dryomov 		wrlocked = false;
625aea3dcdSIlya Dryomov 		up_read(sem);
635aea3dcdSIlya Dryomov 	}
645aea3dcdSIlya Dryomov 
655aea3dcdSIlya Dryomov 	return wrlocked;
665aea3dcdSIlya Dryomov }
675aea3dcdSIlya Dryomov static inline void verify_osdc_locked(struct ceph_osd_client *osdc)
685aea3dcdSIlya Dryomov {
695aea3dcdSIlya Dryomov 	WARN_ON(!rwsem_is_locked(&osdc->lock));
705aea3dcdSIlya Dryomov }
715aea3dcdSIlya Dryomov static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc)
725aea3dcdSIlya Dryomov {
735aea3dcdSIlya Dryomov 	WARN_ON(!rwsem_is_wrlocked(&osdc->lock));
745aea3dcdSIlya Dryomov }
755aea3dcdSIlya Dryomov static inline void verify_osd_locked(struct ceph_osd *osd)
765aea3dcdSIlya Dryomov {
775aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
785aea3dcdSIlya Dryomov 
795aea3dcdSIlya Dryomov 	WARN_ON(!(mutex_is_locked(&osd->lock) &&
805aea3dcdSIlya Dryomov 		  rwsem_is_locked(&osdc->lock)) &&
815aea3dcdSIlya Dryomov 		!rwsem_is_wrlocked(&osdc->lock));
825aea3dcdSIlya Dryomov }
83922dab61SIlya Dryomov static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq)
84922dab61SIlya Dryomov {
85922dab61SIlya Dryomov 	WARN_ON(!mutex_is_locked(&lreq->lock));
86922dab61SIlya Dryomov }
875aea3dcdSIlya Dryomov #else
885aea3dcdSIlya Dryomov static inline void verify_osdc_locked(struct ceph_osd_client *osdc) { }
895aea3dcdSIlya Dryomov static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc) { }
905aea3dcdSIlya Dryomov static inline void verify_osd_locked(struct ceph_osd *osd) { }
91922dab61SIlya Dryomov static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq) { }
925aea3dcdSIlya Dryomov #endif
935aea3dcdSIlya Dryomov 
943d14c5d2SYehuda Sadeh /*
953d14c5d2SYehuda Sadeh  * calculate the mapping of a file extent onto an object, and fill out the
963d14c5d2SYehuda Sadeh  * request accordingly.  shorten extent as necessary if it crosses an
973d14c5d2SYehuda Sadeh  * object boundary.
983d14c5d2SYehuda Sadeh  *
993d14c5d2SYehuda Sadeh  * fill osd op in request message.
1003d14c5d2SYehuda Sadeh  */
101dbe0fc41SAlex Elder static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
102a19dadfbSAlex Elder 			u64 *objnum, u64 *objoff, u64 *objlen)
1033d14c5d2SYehuda Sadeh {
10460e56f13SAlex Elder 	u64 orig_len = *plen;
105d63b77f4SSage Weil 	int r;
1063d14c5d2SYehuda Sadeh 
10760e56f13SAlex Elder 	/* object extent? */
10875d1c941SAlex Elder 	r = ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
10975d1c941SAlex Elder 					  objoff, objlen);
110d63b77f4SSage Weil 	if (r < 0)
111d63b77f4SSage Weil 		return r;
11275d1c941SAlex Elder 	if (*objlen < orig_len) {
11375d1c941SAlex Elder 		*plen = *objlen;
11460e56f13SAlex Elder 		dout(" skipping last %llu, final file extent %llu~%llu\n",
11560e56f13SAlex Elder 		     orig_len - *plen, off, *plen);
11660e56f13SAlex Elder 	}
11760e56f13SAlex Elder 
11875d1c941SAlex Elder 	dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
1193d14c5d2SYehuda Sadeh 
1203ff5f385SAlex Elder 	return 0;
1213d14c5d2SYehuda Sadeh }
1223d14c5d2SYehuda Sadeh 
123c54d47bfSAlex Elder static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
124c54d47bfSAlex Elder {
125c54d47bfSAlex Elder 	memset(osd_data, 0, sizeof (*osd_data));
126c54d47bfSAlex Elder 	osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
127c54d47bfSAlex Elder }
128c54d47bfSAlex Elder 
129a4ce40a9SAlex Elder static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
13043bfe5deSAlex Elder 			struct page **pages, u64 length, u32 alignment,
13143bfe5deSAlex Elder 			bool pages_from_pool, bool own_pages)
13243bfe5deSAlex Elder {
13343bfe5deSAlex Elder 	osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
13443bfe5deSAlex Elder 	osd_data->pages = pages;
13543bfe5deSAlex Elder 	osd_data->length = length;
13643bfe5deSAlex Elder 	osd_data->alignment = alignment;
13743bfe5deSAlex Elder 	osd_data->pages_from_pool = pages_from_pool;
13843bfe5deSAlex Elder 	osd_data->own_pages = own_pages;
13943bfe5deSAlex Elder }
14043bfe5deSAlex Elder 
141a4ce40a9SAlex Elder static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
14243bfe5deSAlex Elder 			struct ceph_pagelist *pagelist)
14343bfe5deSAlex Elder {
14443bfe5deSAlex Elder 	osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
14543bfe5deSAlex Elder 	osd_data->pagelist = pagelist;
14643bfe5deSAlex Elder }
14743bfe5deSAlex Elder 
14843bfe5deSAlex Elder #ifdef CONFIG_BLOCK
149a4ce40a9SAlex Elder static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
15043bfe5deSAlex Elder 			struct bio *bio, size_t bio_length)
15143bfe5deSAlex Elder {
15243bfe5deSAlex Elder 	osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
15343bfe5deSAlex Elder 	osd_data->bio = bio;
15443bfe5deSAlex Elder 	osd_data->bio_length = bio_length;
15543bfe5deSAlex Elder }
15643bfe5deSAlex Elder #endif /* CONFIG_BLOCK */
15743bfe5deSAlex Elder 
158863c7eb5SAlex Elder #define osd_req_op_data(oreq, whch, typ, fld)				\
159863c7eb5SAlex Elder ({									\
1608a703a38SIoana Ciornei 	struct ceph_osd_request *__oreq = (oreq);			\
1618a703a38SIoana Ciornei 	unsigned int __whch = (whch);					\
1628a703a38SIoana Ciornei 	BUG_ON(__whch >= __oreq->r_num_ops);				\
1638a703a38SIoana Ciornei 	&__oreq->r_ops[__whch].typ.fld;					\
164863c7eb5SAlex Elder })
165863c7eb5SAlex Elder 
16649719778SAlex Elder static struct ceph_osd_data *
16749719778SAlex Elder osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
16849719778SAlex Elder {
16949719778SAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
17049719778SAlex Elder 
17149719778SAlex Elder 	return &osd_req->r_ops[which].raw_data_in;
17249719778SAlex Elder }
17349719778SAlex Elder 
174a4ce40a9SAlex Elder struct ceph_osd_data *
175a4ce40a9SAlex Elder osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
176406e2c9fSAlex Elder 			unsigned int which)
177a4ce40a9SAlex Elder {
178863c7eb5SAlex Elder 	return osd_req_op_data(osd_req, which, extent, osd_data);
179a4ce40a9SAlex Elder }
180a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_osd_data);
181a4ce40a9SAlex Elder 
18249719778SAlex Elder void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
18349719778SAlex Elder 			unsigned int which, struct page **pages,
18449719778SAlex Elder 			u64 length, u32 alignment,
18549719778SAlex Elder 			bool pages_from_pool, bool own_pages)
18649719778SAlex Elder {
18749719778SAlex Elder 	struct ceph_osd_data *osd_data;
18849719778SAlex Elder 
18949719778SAlex Elder 	osd_data = osd_req_op_raw_data_in(osd_req, which);
19049719778SAlex Elder 	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
19149719778SAlex Elder 				pages_from_pool, own_pages);
19249719778SAlex Elder }
19349719778SAlex Elder EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
19449719778SAlex Elder 
195a4ce40a9SAlex Elder void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
196406e2c9fSAlex Elder 			unsigned int which, struct page **pages,
197406e2c9fSAlex Elder 			u64 length, u32 alignment,
198a4ce40a9SAlex Elder 			bool pages_from_pool, bool own_pages)
199a4ce40a9SAlex Elder {
200a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
201a4ce40a9SAlex Elder 
202863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
203a4ce40a9SAlex Elder 	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
204a4ce40a9SAlex Elder 				pages_from_pool, own_pages);
205a4ce40a9SAlex Elder }
206a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
207a4ce40a9SAlex Elder 
208a4ce40a9SAlex Elder void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
209406e2c9fSAlex Elder 			unsigned int which, struct ceph_pagelist *pagelist)
210a4ce40a9SAlex Elder {
211a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
212a4ce40a9SAlex Elder 
213863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
214a4ce40a9SAlex Elder 	ceph_osd_data_pagelist_init(osd_data, pagelist);
215a4ce40a9SAlex Elder }
216a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
217a4ce40a9SAlex Elder 
218a4ce40a9SAlex Elder #ifdef CONFIG_BLOCK
219a4ce40a9SAlex Elder void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
220406e2c9fSAlex Elder 			unsigned int which, struct bio *bio, size_t bio_length)
221a4ce40a9SAlex Elder {
222a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
223863c7eb5SAlex Elder 
224863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
225a4ce40a9SAlex Elder 	ceph_osd_data_bio_init(osd_data, bio, bio_length);
226a4ce40a9SAlex Elder }
227a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
228a4ce40a9SAlex Elder #endif /* CONFIG_BLOCK */
229a4ce40a9SAlex Elder 
230a4ce40a9SAlex Elder static void osd_req_op_cls_request_info_pagelist(
231a4ce40a9SAlex Elder 			struct ceph_osd_request *osd_req,
232a4ce40a9SAlex Elder 			unsigned int which, struct ceph_pagelist *pagelist)
233a4ce40a9SAlex Elder {
234a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
235a4ce40a9SAlex Elder 
236863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, request_info);
237a4ce40a9SAlex Elder 	ceph_osd_data_pagelist_init(osd_data, pagelist);
238a4ce40a9SAlex Elder }
239a4ce40a9SAlex Elder 
24004017e29SAlex Elder void osd_req_op_cls_request_data_pagelist(
24104017e29SAlex Elder 			struct ceph_osd_request *osd_req,
24204017e29SAlex Elder 			unsigned int which, struct ceph_pagelist *pagelist)
24304017e29SAlex Elder {
24404017e29SAlex Elder 	struct ceph_osd_data *osd_data;
24504017e29SAlex Elder 
246863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, request_data);
24704017e29SAlex Elder 	ceph_osd_data_pagelist_init(osd_data, pagelist);
248bb873b53SIlya Dryomov 	osd_req->r_ops[which].cls.indata_len += pagelist->length;
249bb873b53SIlya Dryomov 	osd_req->r_ops[which].indata_len += pagelist->length;
25004017e29SAlex Elder }
25104017e29SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
25204017e29SAlex Elder 
2536c57b554SAlex Elder void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
2546c57b554SAlex Elder 			unsigned int which, struct page **pages, u64 length,
2556c57b554SAlex Elder 			u32 alignment, bool pages_from_pool, bool own_pages)
2566c57b554SAlex Elder {
2576c57b554SAlex Elder 	struct ceph_osd_data *osd_data;
2586c57b554SAlex Elder 
2596c57b554SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, request_data);
2606c57b554SAlex Elder 	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
2616c57b554SAlex Elder 				pages_from_pool, own_pages);
262bb873b53SIlya Dryomov 	osd_req->r_ops[which].cls.indata_len += length;
263bb873b53SIlya Dryomov 	osd_req->r_ops[which].indata_len += length;
2646c57b554SAlex Elder }
2656c57b554SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
2666c57b554SAlex Elder 
267a4ce40a9SAlex Elder void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
268a4ce40a9SAlex Elder 			unsigned int which, struct page **pages, u64 length,
269a4ce40a9SAlex Elder 			u32 alignment, bool pages_from_pool, bool own_pages)
270a4ce40a9SAlex Elder {
271a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
272a4ce40a9SAlex Elder 
273863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, response_data);
274a4ce40a9SAlex Elder 	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
275a4ce40a9SAlex Elder 				pages_from_pool, own_pages);
276a4ce40a9SAlex Elder }
277a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
278a4ce40a9SAlex Elder 
27923c08a9cSAlex Elder static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
28023c08a9cSAlex Elder {
28123c08a9cSAlex Elder 	switch (osd_data->type) {
28223c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_NONE:
28323c08a9cSAlex Elder 		return 0;
28423c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_PAGES:
28523c08a9cSAlex Elder 		return osd_data->length;
28623c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_PAGELIST:
28723c08a9cSAlex Elder 		return (u64)osd_data->pagelist->length;
28823c08a9cSAlex Elder #ifdef CONFIG_BLOCK
28923c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_BIO:
29023c08a9cSAlex Elder 		return (u64)osd_data->bio_length;
29123c08a9cSAlex Elder #endif /* CONFIG_BLOCK */
29223c08a9cSAlex Elder 	default:
29323c08a9cSAlex Elder 		WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
29423c08a9cSAlex Elder 		return 0;
29523c08a9cSAlex Elder 	}
29623c08a9cSAlex Elder }
29723c08a9cSAlex Elder 
298c54d47bfSAlex Elder static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
299c54d47bfSAlex Elder {
3005476492fSAlex Elder 	if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
301c54d47bfSAlex Elder 		int num_pages;
302c54d47bfSAlex Elder 
303c54d47bfSAlex Elder 		num_pages = calc_pages_for((u64)osd_data->alignment,
304c54d47bfSAlex Elder 						(u64)osd_data->length);
305c54d47bfSAlex Elder 		ceph_release_page_vector(osd_data->pages, num_pages);
306c54d47bfSAlex Elder 	}
3075476492fSAlex Elder 	ceph_osd_data_init(osd_data);
3085476492fSAlex Elder }
3095476492fSAlex Elder 
3105476492fSAlex Elder static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
3115476492fSAlex Elder 			unsigned int which)
3125476492fSAlex Elder {
3135476492fSAlex Elder 	struct ceph_osd_req_op *op;
3145476492fSAlex Elder 
3155476492fSAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
3165476492fSAlex Elder 	op = &osd_req->r_ops[which];
3175476492fSAlex Elder 
3185476492fSAlex Elder 	switch (op->op) {
3195476492fSAlex Elder 	case CEPH_OSD_OP_READ:
3205476492fSAlex Elder 	case CEPH_OSD_OP_WRITE:
321e30b7577SIlya Dryomov 	case CEPH_OSD_OP_WRITEFULL:
3225476492fSAlex Elder 		ceph_osd_data_release(&op->extent.osd_data);
3235476492fSAlex Elder 		break;
3245476492fSAlex Elder 	case CEPH_OSD_OP_CALL:
3255476492fSAlex Elder 		ceph_osd_data_release(&op->cls.request_info);
32604017e29SAlex Elder 		ceph_osd_data_release(&op->cls.request_data);
3275476492fSAlex Elder 		ceph_osd_data_release(&op->cls.response_data);
3285476492fSAlex Elder 		break;
329d74b50beSYan, Zheng 	case CEPH_OSD_OP_SETXATTR:
330d74b50beSYan, Zheng 	case CEPH_OSD_OP_CMPXATTR:
331d74b50beSYan, Zheng 		ceph_osd_data_release(&op->xattr.osd_data);
332d74b50beSYan, Zheng 		break;
33366ba609fSYan, Zheng 	case CEPH_OSD_OP_STAT:
33466ba609fSYan, Zheng 		ceph_osd_data_release(&op->raw_data_in);
33566ba609fSYan, Zheng 		break;
336922dab61SIlya Dryomov 	case CEPH_OSD_OP_NOTIFY_ACK:
337922dab61SIlya Dryomov 		ceph_osd_data_release(&op->notify_ack.request_data);
338922dab61SIlya Dryomov 		break;
33919079203SIlya Dryomov 	case CEPH_OSD_OP_NOTIFY:
34019079203SIlya Dryomov 		ceph_osd_data_release(&op->notify.request_data);
34119079203SIlya Dryomov 		ceph_osd_data_release(&op->notify.response_data);
34219079203SIlya Dryomov 		break;
343a4ed38d7SDouglas Fuller 	case CEPH_OSD_OP_LIST_WATCHERS:
344a4ed38d7SDouglas Fuller 		ceph_osd_data_release(&op->list_watchers.response_data);
345a4ed38d7SDouglas Fuller 		break;
3465476492fSAlex Elder 	default:
3475476492fSAlex Elder 		break;
3485476492fSAlex Elder 	}
349c54d47bfSAlex Elder }
350c54d47bfSAlex Elder 
3513d14c5d2SYehuda Sadeh /*
35263244fa1SIlya Dryomov  * Assumes @t is zero-initialized.
35363244fa1SIlya Dryomov  */
35463244fa1SIlya Dryomov static void target_init(struct ceph_osd_request_target *t)
35563244fa1SIlya Dryomov {
35663244fa1SIlya Dryomov 	ceph_oid_init(&t->base_oid);
35763244fa1SIlya Dryomov 	ceph_oloc_init(&t->base_oloc);
35863244fa1SIlya Dryomov 	ceph_oid_init(&t->target_oid);
35963244fa1SIlya Dryomov 	ceph_oloc_init(&t->target_oloc);
36063244fa1SIlya Dryomov 
36163244fa1SIlya Dryomov 	ceph_osds_init(&t->acting);
36263244fa1SIlya Dryomov 	ceph_osds_init(&t->up);
36363244fa1SIlya Dryomov 	t->size = -1;
36463244fa1SIlya Dryomov 	t->min_size = -1;
36563244fa1SIlya Dryomov 
36663244fa1SIlya Dryomov 	t->osd = CEPH_HOMELESS_OSD;
36763244fa1SIlya Dryomov }
36863244fa1SIlya Dryomov 
369922dab61SIlya Dryomov static void target_copy(struct ceph_osd_request_target *dest,
370922dab61SIlya Dryomov 			const struct ceph_osd_request_target *src)
371922dab61SIlya Dryomov {
372922dab61SIlya Dryomov 	ceph_oid_copy(&dest->base_oid, &src->base_oid);
373922dab61SIlya Dryomov 	ceph_oloc_copy(&dest->base_oloc, &src->base_oloc);
374922dab61SIlya Dryomov 	ceph_oid_copy(&dest->target_oid, &src->target_oid);
375922dab61SIlya Dryomov 	ceph_oloc_copy(&dest->target_oloc, &src->target_oloc);
376922dab61SIlya Dryomov 
377922dab61SIlya Dryomov 	dest->pgid = src->pgid; /* struct */
378dc98ff72SIlya Dryomov 	dest->spgid = src->spgid; /* struct */
379922dab61SIlya Dryomov 	dest->pg_num = src->pg_num;
380922dab61SIlya Dryomov 	dest->pg_num_mask = src->pg_num_mask;
381922dab61SIlya Dryomov 	ceph_osds_copy(&dest->acting, &src->acting);
382922dab61SIlya Dryomov 	ceph_osds_copy(&dest->up, &src->up);
383922dab61SIlya Dryomov 	dest->size = src->size;
384922dab61SIlya Dryomov 	dest->min_size = src->min_size;
385922dab61SIlya Dryomov 	dest->sort_bitwise = src->sort_bitwise;
386922dab61SIlya Dryomov 
387922dab61SIlya Dryomov 	dest->flags = src->flags;
388922dab61SIlya Dryomov 	dest->paused = src->paused;
389922dab61SIlya Dryomov 
39004c7d789SIlya Dryomov 	dest->epoch = src->epoch;
391dc93e0e2SIlya Dryomov 	dest->last_force_resend = src->last_force_resend;
392dc93e0e2SIlya Dryomov 
393922dab61SIlya Dryomov 	dest->osd = src->osd;
394922dab61SIlya Dryomov }
395922dab61SIlya Dryomov 
39663244fa1SIlya Dryomov static void target_destroy(struct ceph_osd_request_target *t)
39763244fa1SIlya Dryomov {
39863244fa1SIlya Dryomov 	ceph_oid_destroy(&t->base_oid);
39930c156d9SYan, Zheng 	ceph_oloc_destroy(&t->base_oloc);
40063244fa1SIlya Dryomov 	ceph_oid_destroy(&t->target_oid);
40130c156d9SYan, Zheng 	ceph_oloc_destroy(&t->target_oloc);
40263244fa1SIlya Dryomov }
40363244fa1SIlya Dryomov 
40463244fa1SIlya Dryomov /*
4053d14c5d2SYehuda Sadeh  * requests
4063d14c5d2SYehuda Sadeh  */
4073540bfdbSIlya Dryomov static void request_release_checks(struct ceph_osd_request *req)
4083540bfdbSIlya Dryomov {
4093540bfdbSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&req->r_node));
4104609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&req->r_mc_node));
4113540bfdbSIlya Dryomov 	WARN_ON(!list_empty(&req->r_unsafe_item));
4123540bfdbSIlya Dryomov 	WARN_ON(req->r_osd);
4133540bfdbSIlya Dryomov }
4143540bfdbSIlya Dryomov 
4159e94af20SIlya Dryomov static void ceph_osdc_release_request(struct kref *kref)
4163d14c5d2SYehuda Sadeh {
4179e94af20SIlya Dryomov 	struct ceph_osd_request *req = container_of(kref,
4189e94af20SIlya Dryomov 					    struct ceph_osd_request, r_kref);
4195476492fSAlex Elder 	unsigned int which;
4203d14c5d2SYehuda Sadeh 
4219e94af20SIlya Dryomov 	dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
4229e94af20SIlya Dryomov 	     req->r_request, req->r_reply);
4233540bfdbSIlya Dryomov 	request_release_checks(req);
4249e94af20SIlya Dryomov 
4253d14c5d2SYehuda Sadeh 	if (req->r_request)
4263d14c5d2SYehuda Sadeh 		ceph_msg_put(req->r_request);
4275aea3dcdSIlya Dryomov 	if (req->r_reply)
428ab8cb34aSAlex Elder 		ceph_msg_put(req->r_reply);
4290fff87ecSAlex Elder 
4305476492fSAlex Elder 	for (which = 0; which < req->r_num_ops; which++)
4315476492fSAlex Elder 		osd_req_op_data_release(req, which);
4320fff87ecSAlex Elder 
433a66dd383SIlya Dryomov 	target_destroy(&req->r_t);
4343d14c5d2SYehuda Sadeh 	ceph_put_snap_context(req->r_snapc);
435d30291b9SIlya Dryomov 
4363d14c5d2SYehuda Sadeh 	if (req->r_mempool)
4373d14c5d2SYehuda Sadeh 		mempool_free(req, req->r_osdc->req_mempool);
4383f1af42aSIlya Dryomov 	else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS)
4395522ae0bSAlex Elder 		kmem_cache_free(ceph_osd_request_cache, req);
4403f1af42aSIlya Dryomov 	else
4413f1af42aSIlya Dryomov 		kfree(req);
4423d14c5d2SYehuda Sadeh }
4439e94af20SIlya Dryomov 
4449e94af20SIlya Dryomov void ceph_osdc_get_request(struct ceph_osd_request *req)
4459e94af20SIlya Dryomov {
4469e94af20SIlya Dryomov 	dout("%s %p (was %d)\n", __func__, req,
4472c935bc5SPeter Zijlstra 	     kref_read(&req->r_kref));
4489e94af20SIlya Dryomov 	kref_get(&req->r_kref);
4499e94af20SIlya Dryomov }
4509e94af20SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_get_request);
4519e94af20SIlya Dryomov 
4529e94af20SIlya Dryomov void ceph_osdc_put_request(struct ceph_osd_request *req)
4539e94af20SIlya Dryomov {
4543ed97d63SIlya Dryomov 	if (req) {
4559e94af20SIlya Dryomov 		dout("%s %p (was %d)\n", __func__, req,
4562c935bc5SPeter Zijlstra 		     kref_read(&req->r_kref));
4579e94af20SIlya Dryomov 		kref_put(&req->r_kref, ceph_osdc_release_request);
4589e94af20SIlya Dryomov 	}
4593ed97d63SIlya Dryomov }
4609e94af20SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_put_request);
4613d14c5d2SYehuda Sadeh 
4623540bfdbSIlya Dryomov static void request_init(struct ceph_osd_request *req)
4633540bfdbSIlya Dryomov {
4643540bfdbSIlya Dryomov 	/* req only, each op is zeroed in _osd_req_op_init() */
4653540bfdbSIlya Dryomov 	memset(req, 0, sizeof(*req));
4663540bfdbSIlya Dryomov 
4673540bfdbSIlya Dryomov 	kref_init(&req->r_kref);
4683540bfdbSIlya Dryomov 	init_completion(&req->r_completion);
4693540bfdbSIlya Dryomov 	RB_CLEAR_NODE(&req->r_node);
4704609245eSIlya Dryomov 	RB_CLEAR_NODE(&req->r_mc_node);
4713540bfdbSIlya Dryomov 	INIT_LIST_HEAD(&req->r_unsafe_item);
4723540bfdbSIlya Dryomov 
4733540bfdbSIlya Dryomov 	target_init(&req->r_t);
4743540bfdbSIlya Dryomov }
4753540bfdbSIlya Dryomov 
476922dab61SIlya Dryomov /*
477922dab61SIlya Dryomov  * This is ugly, but it allows us to reuse linger registration and ping
478922dab61SIlya Dryomov  * requests, keeping the structure of the code around send_linger{_ping}()
479922dab61SIlya Dryomov  * reasonable.  Setting up a min_nr=2 mempool for each linger request
480922dab61SIlya Dryomov  * and dealing with copying ops (this blasts req only, watch op remains
481922dab61SIlya Dryomov  * intact) isn't any better.
482922dab61SIlya Dryomov  */
483922dab61SIlya Dryomov static void request_reinit(struct ceph_osd_request *req)
484922dab61SIlya Dryomov {
485922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
486922dab61SIlya Dryomov 	bool mempool = req->r_mempool;
487922dab61SIlya Dryomov 	unsigned int num_ops = req->r_num_ops;
488922dab61SIlya Dryomov 	u64 snapid = req->r_snapid;
489922dab61SIlya Dryomov 	struct ceph_snap_context *snapc = req->r_snapc;
490922dab61SIlya Dryomov 	bool linger = req->r_linger;
491922dab61SIlya Dryomov 	struct ceph_msg *request_msg = req->r_request;
492922dab61SIlya Dryomov 	struct ceph_msg *reply_msg = req->r_reply;
493922dab61SIlya Dryomov 
494922dab61SIlya Dryomov 	dout("%s req %p\n", __func__, req);
4952c935bc5SPeter Zijlstra 	WARN_ON(kref_read(&req->r_kref) != 1);
496922dab61SIlya Dryomov 	request_release_checks(req);
497922dab61SIlya Dryomov 
4982c935bc5SPeter Zijlstra 	WARN_ON(kref_read(&request_msg->kref) != 1);
4992c935bc5SPeter Zijlstra 	WARN_ON(kref_read(&reply_msg->kref) != 1);
500922dab61SIlya Dryomov 	target_destroy(&req->r_t);
501922dab61SIlya Dryomov 
502922dab61SIlya Dryomov 	request_init(req);
503922dab61SIlya Dryomov 	req->r_osdc = osdc;
504922dab61SIlya Dryomov 	req->r_mempool = mempool;
505922dab61SIlya Dryomov 	req->r_num_ops = num_ops;
506922dab61SIlya Dryomov 	req->r_snapid = snapid;
507922dab61SIlya Dryomov 	req->r_snapc = snapc;
508922dab61SIlya Dryomov 	req->r_linger = linger;
509922dab61SIlya Dryomov 	req->r_request = request_msg;
510922dab61SIlya Dryomov 	req->r_reply = reply_msg;
511922dab61SIlya Dryomov }
512922dab61SIlya Dryomov 
5133d14c5d2SYehuda Sadeh struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
5143d14c5d2SYehuda Sadeh 					       struct ceph_snap_context *snapc,
5151b83bef2SSage Weil 					       unsigned int num_ops,
5163d14c5d2SYehuda Sadeh 					       bool use_mempool,
51754a54007SAlex Elder 					       gfp_t gfp_flags)
5183d14c5d2SYehuda Sadeh {
5193d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
5203d14c5d2SYehuda Sadeh 
5213d14c5d2SYehuda Sadeh 	if (use_mempool) {
5223f1af42aSIlya Dryomov 		BUG_ON(num_ops > CEPH_OSD_SLAB_OPS);
5233d14c5d2SYehuda Sadeh 		req = mempool_alloc(osdc->req_mempool, gfp_flags);
5243f1af42aSIlya Dryomov 	} else if (num_ops <= CEPH_OSD_SLAB_OPS) {
5253f1af42aSIlya Dryomov 		req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
5263d14c5d2SYehuda Sadeh 	} else {
5273f1af42aSIlya Dryomov 		BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
5283f1af42aSIlya Dryomov 		req = kmalloc(sizeof(*req) + num_ops * sizeof(req->r_ops[0]),
5293f1af42aSIlya Dryomov 			      gfp_flags);
5303d14c5d2SYehuda Sadeh 	}
5313f1af42aSIlya Dryomov 	if (unlikely(!req))
5323d14c5d2SYehuda Sadeh 		return NULL;
5333d14c5d2SYehuda Sadeh 
5343540bfdbSIlya Dryomov 	request_init(req);
5353d14c5d2SYehuda Sadeh 	req->r_osdc = osdc;
5363d14c5d2SYehuda Sadeh 	req->r_mempool = use_mempool;
53779528734SAlex Elder 	req->r_num_ops = num_ops;
53884127282SIlya Dryomov 	req->r_snapid = CEPH_NOSNAP;
53984127282SIlya Dryomov 	req->r_snapc = ceph_get_snap_context(snapc);
5403d14c5d2SYehuda Sadeh 
54113d1ad16SIlya Dryomov 	dout("%s req %p\n", __func__, req);
54213d1ad16SIlya Dryomov 	return req;
5433f1af42aSIlya Dryomov }
54413d1ad16SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_alloc_request);
5453f1af42aSIlya Dryomov 
5462e59ffd1SIlya Dryomov static int ceph_oloc_encoding_size(const struct ceph_object_locator *oloc)
54730c156d9SYan, Zheng {
54830c156d9SYan, Zheng 	return 8 + 4 + 4 + 4 + (oloc->pool_ns ? oloc->pool_ns->len : 0);
54930c156d9SYan, Zheng }
55030c156d9SYan, Zheng 
55113d1ad16SIlya Dryomov int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp)
55213d1ad16SIlya Dryomov {
55313d1ad16SIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
55413d1ad16SIlya Dryomov 	struct ceph_msg *msg;
55513d1ad16SIlya Dryomov 	int msg_size;
5563d14c5d2SYehuda Sadeh 
557d30291b9SIlya Dryomov 	WARN_ON(ceph_oid_empty(&req->r_base_oid));
55830c156d9SYan, Zheng 	WARN_ON(ceph_oloc_empty(&req->r_base_oloc));
559d30291b9SIlya Dryomov 
56013d1ad16SIlya Dryomov 	/* create request message */
5618cb441c0SIlya Dryomov 	msg_size = CEPH_ENCODING_START_BLK_LEN +
5628cb441c0SIlya Dryomov 			CEPH_PGID_ENCODING_LEN + 1; /* spgid */
5638cb441c0SIlya Dryomov 	msg_size += 4 + 4 + 4; /* hash, osdmap_epoch, flags */
5648cb441c0SIlya Dryomov 	msg_size += CEPH_ENCODING_START_BLK_LEN +
5658cb441c0SIlya Dryomov 			sizeof(struct ceph_osd_reqid); /* reqid */
5668cb441c0SIlya Dryomov 	msg_size += sizeof(struct ceph_blkin_trace_info); /* trace */
5678cb441c0SIlya Dryomov 	msg_size += 4 + sizeof(struct ceph_timespec); /* client_inc, mtime */
56830c156d9SYan, Zheng 	msg_size += CEPH_ENCODING_START_BLK_LEN +
56930c156d9SYan, Zheng 			ceph_oloc_encoding_size(&req->r_base_oloc); /* oloc */
57013d1ad16SIlya Dryomov 	msg_size += 4 + req->r_base_oid.name_len; /* oid */
57113d1ad16SIlya Dryomov 	msg_size += 2 + req->r_num_ops * sizeof(struct ceph_osd_op);
572ae458f5aSIlya Dryomov 	msg_size += 8; /* snapid */
573ae458f5aSIlya Dryomov 	msg_size += 8; /* snap_seq */
57413d1ad16SIlya Dryomov 	msg_size += 4 + 8 * (req->r_snapc ? req->r_snapc->num_snaps : 0);
5758cb441c0SIlya Dryomov 	msg_size += 4 + 8; /* retry_attempt, features */
576ae458f5aSIlya Dryomov 
57713d1ad16SIlya Dryomov 	if (req->r_mempool)
5783d14c5d2SYehuda Sadeh 		msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
5793d14c5d2SYehuda Sadeh 	else
58013d1ad16SIlya Dryomov 		msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp, true);
58113d1ad16SIlya Dryomov 	if (!msg)
58213d1ad16SIlya Dryomov 		return -ENOMEM;
5833d14c5d2SYehuda Sadeh 
5843d14c5d2SYehuda Sadeh 	memset(msg->front.iov_base, 0, msg->front.iov_len);
5853d14c5d2SYehuda Sadeh 	req->r_request = msg;
5863d14c5d2SYehuda Sadeh 
58713d1ad16SIlya Dryomov 	/* create reply message */
58813d1ad16SIlya Dryomov 	msg_size = OSD_OPREPLY_FRONT_LEN;
589711da55dSIlya Dryomov 	msg_size += req->r_base_oid.name_len;
590711da55dSIlya Dryomov 	msg_size += req->r_num_ops * sizeof(struct ceph_osd_op);
59113d1ad16SIlya Dryomov 
59213d1ad16SIlya Dryomov 	if (req->r_mempool)
59313d1ad16SIlya Dryomov 		msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
59413d1ad16SIlya Dryomov 	else
59513d1ad16SIlya Dryomov 		msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, msg_size, gfp, true);
59613d1ad16SIlya Dryomov 	if (!msg)
59713d1ad16SIlya Dryomov 		return -ENOMEM;
59813d1ad16SIlya Dryomov 
59913d1ad16SIlya Dryomov 	req->r_reply = msg;
60013d1ad16SIlya Dryomov 
60113d1ad16SIlya Dryomov 	return 0;
60213d1ad16SIlya Dryomov }
60313d1ad16SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_alloc_messages);
6043d14c5d2SYehuda Sadeh 
605a8dd0a37SAlex Elder static bool osd_req_opcode_valid(u16 opcode)
606a8dd0a37SAlex Elder {
607a8dd0a37SAlex Elder 	switch (opcode) {
60870b5bfa3SIlya Dryomov #define GENERATE_CASE(op, opcode, str)	case CEPH_OSD_OP_##op: return true;
60970b5bfa3SIlya Dryomov __CEPH_FORALL_OSD_OPS(GENERATE_CASE)
61070b5bfa3SIlya Dryomov #undef GENERATE_CASE
611a8dd0a37SAlex Elder 	default:
612a8dd0a37SAlex Elder 		return false;
613a8dd0a37SAlex Elder 	}
614a8dd0a37SAlex Elder }
615a8dd0a37SAlex Elder 
61633803f33SAlex Elder /*
61733803f33SAlex Elder  * This is an osd op init function for opcodes that have no data or
61833803f33SAlex Elder  * other information associated with them.  It also serves as a
61933803f33SAlex Elder  * common init routine for all the other init functions, below.
62033803f33SAlex Elder  */
621c99d2d4aSAlex Elder static struct ceph_osd_req_op *
62249719778SAlex Elder _osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
623144cba14SYan, Zheng 		 u16 opcode, u32 flags)
62433803f33SAlex Elder {
625c99d2d4aSAlex Elder 	struct ceph_osd_req_op *op;
626c99d2d4aSAlex Elder 
627c99d2d4aSAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
62833803f33SAlex Elder 	BUG_ON(!osd_req_opcode_valid(opcode));
62933803f33SAlex Elder 
630c99d2d4aSAlex Elder 	op = &osd_req->r_ops[which];
63133803f33SAlex Elder 	memset(op, 0, sizeof (*op));
63233803f33SAlex Elder 	op->op = opcode;
633144cba14SYan, Zheng 	op->flags = flags;
634c99d2d4aSAlex Elder 
635c99d2d4aSAlex Elder 	return op;
63633803f33SAlex Elder }
63733803f33SAlex Elder 
63849719778SAlex Elder void osd_req_op_init(struct ceph_osd_request *osd_req,
639144cba14SYan, Zheng 		     unsigned int which, u16 opcode, u32 flags)
64049719778SAlex Elder {
641144cba14SYan, Zheng 	(void)_osd_req_op_init(osd_req, which, opcode, flags);
64249719778SAlex Elder }
64349719778SAlex Elder EXPORT_SYMBOL(osd_req_op_init);
64449719778SAlex Elder 
645c99d2d4aSAlex Elder void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
646c99d2d4aSAlex Elder 				unsigned int which, u16 opcode,
64733803f33SAlex Elder 				u64 offset, u64 length,
64833803f33SAlex Elder 				u64 truncate_size, u32 truncate_seq)
64933803f33SAlex Elder {
650144cba14SYan, Zheng 	struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
651144cba14SYan, Zheng 						      opcode, 0);
65233803f33SAlex Elder 	size_t payload_len = 0;
65333803f33SAlex Elder 
654ad7a60deSLi Wang 	BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
655e30b7577SIlya Dryomov 	       opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
656e30b7577SIlya Dryomov 	       opcode != CEPH_OSD_OP_TRUNCATE);
65733803f33SAlex Elder 
65833803f33SAlex Elder 	op->extent.offset = offset;
65933803f33SAlex Elder 	op->extent.length = length;
66033803f33SAlex Elder 	op->extent.truncate_size = truncate_size;
66133803f33SAlex Elder 	op->extent.truncate_seq = truncate_seq;
662e30b7577SIlya Dryomov 	if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
66333803f33SAlex Elder 		payload_len += length;
66433803f33SAlex Elder 
665de2aa102SIlya Dryomov 	op->indata_len = payload_len;
66633803f33SAlex Elder }
66733803f33SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_init);
66833803f33SAlex Elder 
669c99d2d4aSAlex Elder void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
670c99d2d4aSAlex Elder 				unsigned int which, u64 length)
671e5975c7cSAlex Elder {
672c99d2d4aSAlex Elder 	struct ceph_osd_req_op *op;
673c99d2d4aSAlex Elder 	u64 previous;
674c99d2d4aSAlex Elder 
675c99d2d4aSAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
676c99d2d4aSAlex Elder 	op = &osd_req->r_ops[which];
677c99d2d4aSAlex Elder 	previous = op->extent.length;
678e5975c7cSAlex Elder 
679e5975c7cSAlex Elder 	if (length == previous)
680e5975c7cSAlex Elder 		return;		/* Nothing to do */
681e5975c7cSAlex Elder 	BUG_ON(length > previous);
682e5975c7cSAlex Elder 
683e5975c7cSAlex Elder 	op->extent.length = length;
684d641df81SYan, Zheng 	if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
685de2aa102SIlya Dryomov 		op->indata_len -= previous - length;
686e5975c7cSAlex Elder }
687e5975c7cSAlex Elder EXPORT_SYMBOL(osd_req_op_extent_update);
688e5975c7cSAlex Elder 
6892c63f49aSYan, Zheng void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
6902c63f49aSYan, Zheng 				unsigned int which, u64 offset_inc)
6912c63f49aSYan, Zheng {
6922c63f49aSYan, Zheng 	struct ceph_osd_req_op *op, *prev_op;
6932c63f49aSYan, Zheng 
6942c63f49aSYan, Zheng 	BUG_ON(which + 1 >= osd_req->r_num_ops);
6952c63f49aSYan, Zheng 
6962c63f49aSYan, Zheng 	prev_op = &osd_req->r_ops[which];
6972c63f49aSYan, Zheng 	op = _osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags);
6982c63f49aSYan, Zheng 	/* dup previous one */
6992c63f49aSYan, Zheng 	op->indata_len = prev_op->indata_len;
7002c63f49aSYan, Zheng 	op->outdata_len = prev_op->outdata_len;
7012c63f49aSYan, Zheng 	op->extent = prev_op->extent;
7022c63f49aSYan, Zheng 	/* adjust offset */
7032c63f49aSYan, Zheng 	op->extent.offset += offset_inc;
7042c63f49aSYan, Zheng 	op->extent.length -= offset_inc;
7052c63f49aSYan, Zheng 
7062c63f49aSYan, Zheng 	if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
7072c63f49aSYan, Zheng 		op->indata_len -= offset_inc;
7082c63f49aSYan, Zheng }
7092c63f49aSYan, Zheng EXPORT_SYMBOL(osd_req_op_extent_dup_last);
7102c63f49aSYan, Zheng 
711c99d2d4aSAlex Elder void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
71204017e29SAlex Elder 			u16 opcode, const char *class, const char *method)
71333803f33SAlex Elder {
714144cba14SYan, Zheng 	struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
715144cba14SYan, Zheng 						      opcode, 0);
7165f562df5SAlex Elder 	struct ceph_pagelist *pagelist;
71733803f33SAlex Elder 	size_t payload_len = 0;
71833803f33SAlex Elder 	size_t size;
71933803f33SAlex Elder 
72033803f33SAlex Elder 	BUG_ON(opcode != CEPH_OSD_OP_CALL);
72133803f33SAlex Elder 
7225f562df5SAlex Elder 	pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
7235f562df5SAlex Elder 	BUG_ON(!pagelist);
7245f562df5SAlex Elder 	ceph_pagelist_init(pagelist);
7255f562df5SAlex Elder 
72633803f33SAlex Elder 	op->cls.class_name = class;
72733803f33SAlex Elder 	size = strlen(class);
72833803f33SAlex Elder 	BUG_ON(size > (size_t) U8_MAX);
72933803f33SAlex Elder 	op->cls.class_len = size;
7305f562df5SAlex Elder 	ceph_pagelist_append(pagelist, class, size);
73133803f33SAlex Elder 	payload_len += size;
73233803f33SAlex Elder 
73333803f33SAlex Elder 	op->cls.method_name = method;
73433803f33SAlex Elder 	size = strlen(method);
73533803f33SAlex Elder 	BUG_ON(size > (size_t) U8_MAX);
73633803f33SAlex Elder 	op->cls.method_len = size;
7375f562df5SAlex Elder 	ceph_pagelist_append(pagelist, method, size);
73833803f33SAlex Elder 	payload_len += size;
73933803f33SAlex Elder 
740a4ce40a9SAlex Elder 	osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
7415f562df5SAlex Elder 
742de2aa102SIlya Dryomov 	op->indata_len = payload_len;
74333803f33SAlex Elder }
74433803f33SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_init);
7458c042b0dSAlex Elder 
746d74b50beSYan, Zheng int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
747d74b50beSYan, Zheng 			  u16 opcode, const char *name, const void *value,
748d74b50beSYan, Zheng 			  size_t size, u8 cmp_op, u8 cmp_mode)
749d74b50beSYan, Zheng {
750144cba14SYan, Zheng 	struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
751144cba14SYan, Zheng 						      opcode, 0);
752d74b50beSYan, Zheng 	struct ceph_pagelist *pagelist;
753d74b50beSYan, Zheng 	size_t payload_len;
754d74b50beSYan, Zheng 
755d74b50beSYan, Zheng 	BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
756d74b50beSYan, Zheng 
757d74b50beSYan, Zheng 	pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
758d74b50beSYan, Zheng 	if (!pagelist)
759d74b50beSYan, Zheng 		return -ENOMEM;
760d74b50beSYan, Zheng 
761d74b50beSYan, Zheng 	ceph_pagelist_init(pagelist);
762d74b50beSYan, Zheng 
763d74b50beSYan, Zheng 	payload_len = strlen(name);
764d74b50beSYan, Zheng 	op->xattr.name_len = payload_len;
765d74b50beSYan, Zheng 	ceph_pagelist_append(pagelist, name, payload_len);
766d74b50beSYan, Zheng 
767d74b50beSYan, Zheng 	op->xattr.value_len = size;
768d74b50beSYan, Zheng 	ceph_pagelist_append(pagelist, value, size);
769d74b50beSYan, Zheng 	payload_len += size;
770d74b50beSYan, Zheng 
771d74b50beSYan, Zheng 	op->xattr.cmp_op = cmp_op;
772d74b50beSYan, Zheng 	op->xattr.cmp_mode = cmp_mode;
773d74b50beSYan, Zheng 
774d74b50beSYan, Zheng 	ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
775de2aa102SIlya Dryomov 	op->indata_len = payload_len;
776d74b50beSYan, Zheng 	return 0;
777d74b50beSYan, Zheng }
778d74b50beSYan, Zheng EXPORT_SYMBOL(osd_req_op_xattr_init);
779d74b50beSYan, Zheng 
780922dab61SIlya Dryomov /*
781922dab61SIlya Dryomov  * @watch_opcode: CEPH_OSD_WATCH_OP_*
782922dab61SIlya Dryomov  */
783922dab61SIlya Dryomov static void osd_req_op_watch_init(struct ceph_osd_request *req, int which,
784922dab61SIlya Dryomov 				  u64 cookie, u8 watch_opcode)
78533803f33SAlex Elder {
786922dab61SIlya Dryomov 	struct ceph_osd_req_op *op;
78733803f33SAlex Elder 
788922dab61SIlya Dryomov 	op = _osd_req_op_init(req, which, CEPH_OSD_OP_WATCH, 0);
78933803f33SAlex Elder 	op->watch.cookie = cookie;
790922dab61SIlya Dryomov 	op->watch.op = watch_opcode;
791922dab61SIlya Dryomov 	op->watch.gen = 0;
79233803f33SAlex Elder }
79333803f33SAlex Elder 
794c647b8a8SIlya Dryomov void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
795c647b8a8SIlya Dryomov 				unsigned int which,
796c647b8a8SIlya Dryomov 				u64 expected_object_size,
797c647b8a8SIlya Dryomov 				u64 expected_write_size)
798c647b8a8SIlya Dryomov {
799c647b8a8SIlya Dryomov 	struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
800144cba14SYan, Zheng 						      CEPH_OSD_OP_SETALLOCHINT,
801144cba14SYan, Zheng 						      0);
802c647b8a8SIlya Dryomov 
803c647b8a8SIlya Dryomov 	op->alloc_hint.expected_object_size = expected_object_size;
804c647b8a8SIlya Dryomov 	op->alloc_hint.expected_write_size = expected_write_size;
805c647b8a8SIlya Dryomov 
806c647b8a8SIlya Dryomov 	/*
807c647b8a8SIlya Dryomov 	 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
808c647b8a8SIlya Dryomov 	 * not worth a feature bit.  Set FAILOK per-op flag to make
809c647b8a8SIlya Dryomov 	 * sure older osds don't trip over an unsupported opcode.
810c647b8a8SIlya Dryomov 	 */
811c647b8a8SIlya Dryomov 	op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
812c647b8a8SIlya Dryomov }
813c647b8a8SIlya Dryomov EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
814c647b8a8SIlya Dryomov 
81590af3602SAlex Elder static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
816ec9123c5SAlex Elder 				struct ceph_osd_data *osd_data)
817ec9123c5SAlex Elder {
818ec9123c5SAlex Elder 	u64 length = ceph_osd_data_length(osd_data);
819ec9123c5SAlex Elder 
820ec9123c5SAlex Elder 	if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
821ec9123c5SAlex Elder 		BUG_ON(length > (u64) SIZE_MAX);
822ec9123c5SAlex Elder 		if (length)
82390af3602SAlex Elder 			ceph_msg_data_add_pages(msg, osd_data->pages,
824ec9123c5SAlex Elder 					length, osd_data->alignment);
825ec9123c5SAlex Elder 	} else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
826ec9123c5SAlex Elder 		BUG_ON(!length);
82790af3602SAlex Elder 		ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
828ec9123c5SAlex Elder #ifdef CONFIG_BLOCK
829ec9123c5SAlex Elder 	} else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
83090af3602SAlex Elder 		ceph_msg_data_add_bio(msg, osd_data->bio, length);
831ec9123c5SAlex Elder #endif
832ec9123c5SAlex Elder 	} else {
833ec9123c5SAlex Elder 		BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
834ec9123c5SAlex Elder 	}
835ec9123c5SAlex Elder }
836ec9123c5SAlex Elder 
837bb873b53SIlya Dryomov static u32 osd_req_encode_op(struct ceph_osd_op *dst,
838bb873b53SIlya Dryomov 			     const struct ceph_osd_req_op *src)
8393d14c5d2SYehuda Sadeh {
840a8dd0a37SAlex Elder 	if (WARN_ON(!osd_req_opcode_valid(src->op))) {
841a8dd0a37SAlex Elder 		pr_err("unrecognized osd opcode %d\n", src->op);
842a8dd0a37SAlex Elder 
843a8dd0a37SAlex Elder 		return 0;
844a8dd0a37SAlex Elder 	}
8453d14c5d2SYehuda Sadeh 
846065a68f9SAlex Elder 	switch (src->op) {
847fbfab539SAlex Elder 	case CEPH_OSD_OP_STAT:
848fbfab539SAlex Elder 		break;
8493d14c5d2SYehuda Sadeh 	case CEPH_OSD_OP_READ:
8503d14c5d2SYehuda Sadeh 	case CEPH_OSD_OP_WRITE:
851e30b7577SIlya Dryomov 	case CEPH_OSD_OP_WRITEFULL:
852ad7a60deSLi Wang 	case CEPH_OSD_OP_ZERO:
853ad7a60deSLi Wang 	case CEPH_OSD_OP_TRUNCATE:
854175face2SAlex Elder 		dst->extent.offset = cpu_to_le64(src->extent.offset);
855175face2SAlex Elder 		dst->extent.length = cpu_to_le64(src->extent.length);
8563d14c5d2SYehuda Sadeh 		dst->extent.truncate_size =
8573d14c5d2SYehuda Sadeh 			cpu_to_le64(src->extent.truncate_size);
8583d14c5d2SYehuda Sadeh 		dst->extent.truncate_seq =
8593d14c5d2SYehuda Sadeh 			cpu_to_le32(src->extent.truncate_seq);
8603d14c5d2SYehuda Sadeh 		break;
8613d14c5d2SYehuda Sadeh 	case CEPH_OSD_OP_CALL:
8623d14c5d2SYehuda Sadeh 		dst->cls.class_len = src->cls.class_len;
8633d14c5d2SYehuda Sadeh 		dst->cls.method_len = src->cls.method_len;
864bb873b53SIlya Dryomov 		dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
8653d14c5d2SYehuda Sadeh 		break;
8663d14c5d2SYehuda Sadeh 	case CEPH_OSD_OP_STARTSYNC:
8673d14c5d2SYehuda Sadeh 		break;
868a40c4f10SYehuda Sadeh 	case CEPH_OSD_OP_WATCH:
869a40c4f10SYehuda Sadeh 		dst->watch.cookie = cpu_to_le64(src->watch.cookie);
870922dab61SIlya Dryomov 		dst->watch.ver = cpu_to_le64(0);
871922dab61SIlya Dryomov 		dst->watch.op = src->watch.op;
872922dab61SIlya Dryomov 		dst->watch.gen = cpu_to_le32(src->watch.gen);
873922dab61SIlya Dryomov 		break;
874922dab61SIlya Dryomov 	case CEPH_OSD_OP_NOTIFY_ACK:
875a40c4f10SYehuda Sadeh 		break;
87619079203SIlya Dryomov 	case CEPH_OSD_OP_NOTIFY:
87719079203SIlya Dryomov 		dst->notify.cookie = cpu_to_le64(src->notify.cookie);
87819079203SIlya Dryomov 		break;
879a4ed38d7SDouglas Fuller 	case CEPH_OSD_OP_LIST_WATCHERS:
880a4ed38d7SDouglas Fuller 		break;
881c647b8a8SIlya Dryomov 	case CEPH_OSD_OP_SETALLOCHINT:
882c647b8a8SIlya Dryomov 		dst->alloc_hint.expected_object_size =
883c647b8a8SIlya Dryomov 		    cpu_to_le64(src->alloc_hint.expected_object_size);
884c647b8a8SIlya Dryomov 		dst->alloc_hint.expected_write_size =
885c647b8a8SIlya Dryomov 		    cpu_to_le64(src->alloc_hint.expected_write_size);
886c647b8a8SIlya Dryomov 		break;
887d74b50beSYan, Zheng 	case CEPH_OSD_OP_SETXATTR:
888d74b50beSYan, Zheng 	case CEPH_OSD_OP_CMPXATTR:
889d74b50beSYan, Zheng 		dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
890d74b50beSYan, Zheng 		dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
891d74b50beSYan, Zheng 		dst->xattr.cmp_op = src->xattr.cmp_op;
892d74b50beSYan, Zheng 		dst->xattr.cmp_mode = src->xattr.cmp_mode;
893d74b50beSYan, Zheng 		break;
894864e9197SYan, Zheng 	case CEPH_OSD_OP_CREATE:
895864e9197SYan, Zheng 	case CEPH_OSD_OP_DELETE:
896864e9197SYan, Zheng 		break;
8973d14c5d2SYehuda Sadeh 	default:
8984c46459cSAlex Elder 		pr_err("unsupported osd opcode %s\n",
8998f63ca2dSAlex Elder 			ceph_osd_op_name(src->op));
9004c46459cSAlex Elder 		WARN_ON(1);
901a8dd0a37SAlex Elder 
902a8dd0a37SAlex Elder 		return 0;
9033d14c5d2SYehuda Sadeh 	}
9047b25bf5fSIlya Dryomov 
905a8dd0a37SAlex Elder 	dst->op = cpu_to_le16(src->op);
9067b25bf5fSIlya Dryomov 	dst->flags = cpu_to_le32(src->flags);
907de2aa102SIlya Dryomov 	dst->payload_len = cpu_to_le32(src->indata_len);
908175face2SAlex Elder 
909bb873b53SIlya Dryomov 	return src->indata_len;
9103d14c5d2SYehuda Sadeh }
9113d14c5d2SYehuda Sadeh 
9123d14c5d2SYehuda Sadeh /*
9133d14c5d2SYehuda Sadeh  * build new request AND message, calculate layout, and adjust file
9143d14c5d2SYehuda Sadeh  * extent as needed.
9153d14c5d2SYehuda Sadeh  *
9163d14c5d2SYehuda Sadeh  * if the file was recently truncated, we include information about its
9173d14c5d2SYehuda Sadeh  * old and new size so that the object can be updated appropriately.  (we
9183d14c5d2SYehuda Sadeh  * avoid synchronously deleting truncated objects because it's slow.)
9193d14c5d2SYehuda Sadeh  *
9203d14c5d2SYehuda Sadeh  * if @do_sync, include a 'startsync' command so that the osd will flush
9213d14c5d2SYehuda Sadeh  * data quickly.
9223d14c5d2SYehuda Sadeh  */
9233d14c5d2SYehuda Sadeh struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
9243d14c5d2SYehuda Sadeh 					       struct ceph_file_layout *layout,
9253d14c5d2SYehuda Sadeh 					       struct ceph_vino vino,
926715e4cd4SYan, Zheng 					       u64 off, u64 *plen,
927715e4cd4SYan, Zheng 					       unsigned int which, int num_ops,
9283d14c5d2SYehuda Sadeh 					       int opcode, int flags,
9293d14c5d2SYehuda Sadeh 					       struct ceph_snap_context *snapc,
9303d14c5d2SYehuda Sadeh 					       u32 truncate_seq,
9313d14c5d2SYehuda Sadeh 					       u64 truncate_size,
932153e5167SAlex Elder 					       bool use_mempool)
9333d14c5d2SYehuda Sadeh {
9343d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
93575d1c941SAlex Elder 	u64 objnum = 0;
93675d1c941SAlex Elder 	u64 objoff = 0;
93775d1c941SAlex Elder 	u64 objlen = 0;
9386816282dSSage Weil 	int r;
9393d14c5d2SYehuda Sadeh 
940ad7a60deSLi Wang 	BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
941864e9197SYan, Zheng 	       opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
942864e9197SYan, Zheng 	       opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
9433d14c5d2SYehuda Sadeh 
944acead002SAlex Elder 	req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
945ae7ca4a3SAlex Elder 					GFP_NOFS);
94613d1ad16SIlya Dryomov 	if (!req) {
94713d1ad16SIlya Dryomov 		r = -ENOMEM;
94813d1ad16SIlya Dryomov 		goto fail;
94913d1ad16SIlya Dryomov 	}
95079528734SAlex Elder 
9513d14c5d2SYehuda Sadeh 	/* calculate max write size */
952a19dadfbSAlex Elder 	r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
95313d1ad16SIlya Dryomov 	if (r)
95413d1ad16SIlya Dryomov 		goto fail;
955a19dadfbSAlex Elder 
956864e9197SYan, Zheng 	if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
957144cba14SYan, Zheng 		osd_req_op_init(req, which, opcode, 0);
958864e9197SYan, Zheng 	} else {
9597627151eSYan, Zheng 		u32 object_size = layout->object_size;
960864e9197SYan, Zheng 		u32 object_base = off - objoff;
961ccca4e37SYan, Zheng 		if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
962d18d1e28SAlex Elder 			if (truncate_size <= object_base) {
963d18d1e28SAlex Elder 				truncate_size = 0;
964d18d1e28SAlex Elder 			} else {
965d18d1e28SAlex Elder 				truncate_size -= object_base;
966d18d1e28SAlex Elder 				if (truncate_size > object_size)
967d18d1e28SAlex Elder 					truncate_size = object_size;
968d18d1e28SAlex Elder 			}
969ccca4e37SYan, Zheng 		}
970715e4cd4SYan, Zheng 		osd_req_op_extent_init(req, which, opcode, objoff, objlen,
971b0270324SAlex Elder 				       truncate_size, truncate_seq);
972864e9197SYan, Zheng 	}
973d18d1e28SAlex Elder 
974a1f4020aSJeff Layton 	req->r_abort_on_full = true;
975bb873b53SIlya Dryomov 	req->r_flags = flags;
9767627151eSYan, Zheng 	req->r_base_oloc.pool = layout->pool_id;
97730c156d9SYan, Zheng 	req->r_base_oloc.pool_ns = ceph_try_get_string(layout->pool_ns);
978d30291b9SIlya Dryomov 	ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum);
979dbe0fc41SAlex Elder 
980bb873b53SIlya Dryomov 	req->r_snapid = vino.snap;
981bb873b53SIlya Dryomov 	if (flags & CEPH_OSD_FLAG_WRITE)
982bb873b53SIlya Dryomov 		req->r_data_offset = off;
983bb873b53SIlya Dryomov 
98413d1ad16SIlya Dryomov 	r = ceph_osdc_alloc_messages(req, GFP_NOFS);
98513d1ad16SIlya Dryomov 	if (r)
98613d1ad16SIlya Dryomov 		goto fail;
98713d1ad16SIlya Dryomov 
9883d14c5d2SYehuda Sadeh 	return req;
98913d1ad16SIlya Dryomov 
99013d1ad16SIlya Dryomov fail:
99113d1ad16SIlya Dryomov 	ceph_osdc_put_request(req);
99213d1ad16SIlya Dryomov 	return ERR_PTR(r);
9933d14c5d2SYehuda Sadeh }
9943d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_new_request);
9953d14c5d2SYehuda Sadeh 
9963d14c5d2SYehuda Sadeh /*
9973d14c5d2SYehuda Sadeh  * We keep osd requests in an rbtree, sorted by ->r_tid.
9983d14c5d2SYehuda Sadeh  */
999fcd00b68SIlya Dryomov DEFINE_RB_FUNCS(request, struct ceph_osd_request, r_tid, r_node)
10004609245eSIlya Dryomov DEFINE_RB_FUNCS(request_mc, struct ceph_osd_request, r_tid, r_mc_node)
10013d14c5d2SYehuda Sadeh 
10020247a0cfSIlya Dryomov static bool osd_homeless(struct ceph_osd *osd)
10030247a0cfSIlya Dryomov {
10040247a0cfSIlya Dryomov 	return osd->o_osd == CEPH_HOMELESS_OSD;
10050247a0cfSIlya Dryomov }
10060247a0cfSIlya Dryomov 
10075aea3dcdSIlya Dryomov static bool osd_registered(struct ceph_osd *osd)
10083d14c5d2SYehuda Sadeh {
10095aea3dcdSIlya Dryomov 	verify_osdc_locked(osd->o_osdc);
10103d14c5d2SYehuda Sadeh 
10115aea3dcdSIlya Dryomov 	return !RB_EMPTY_NODE(&osd->o_node);
10123d14c5d2SYehuda Sadeh }
10133d14c5d2SYehuda Sadeh 
10143d14c5d2SYehuda Sadeh /*
10150247a0cfSIlya Dryomov  * Assumes @osd is zero-initialized.
10160247a0cfSIlya Dryomov  */
10170247a0cfSIlya Dryomov static void osd_init(struct ceph_osd *osd)
10180247a0cfSIlya Dryomov {
101902113a0fSElena Reshetova 	refcount_set(&osd->o_ref, 1);
10200247a0cfSIlya Dryomov 	RB_CLEAR_NODE(&osd->o_node);
10215aea3dcdSIlya Dryomov 	osd->o_requests = RB_ROOT;
1022922dab61SIlya Dryomov 	osd->o_linger_requests = RB_ROOT;
1023a02a946dSIlya Dryomov 	osd->o_backoff_mappings = RB_ROOT;
1024a02a946dSIlya Dryomov 	osd->o_backoffs_by_id = RB_ROOT;
10250247a0cfSIlya Dryomov 	INIT_LIST_HEAD(&osd->o_osd_lru);
10260247a0cfSIlya Dryomov 	INIT_LIST_HEAD(&osd->o_keepalive_item);
10270247a0cfSIlya Dryomov 	osd->o_incarnation = 1;
10285aea3dcdSIlya Dryomov 	mutex_init(&osd->lock);
10290247a0cfSIlya Dryomov }
10300247a0cfSIlya Dryomov 
10310247a0cfSIlya Dryomov static void osd_cleanup(struct ceph_osd *osd)
10320247a0cfSIlya Dryomov {
10330247a0cfSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&osd->o_node));
10345aea3dcdSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
1035922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
1036a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoff_mappings));
1037a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoffs_by_id));
10380247a0cfSIlya Dryomov 	WARN_ON(!list_empty(&osd->o_osd_lru));
10390247a0cfSIlya Dryomov 	WARN_ON(!list_empty(&osd->o_keepalive_item));
10400247a0cfSIlya Dryomov 
10410247a0cfSIlya Dryomov 	if (osd->o_auth.authorizer) {
10420247a0cfSIlya Dryomov 		WARN_ON(osd_homeless(osd));
10430247a0cfSIlya Dryomov 		ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
10440247a0cfSIlya Dryomov 	}
10450247a0cfSIlya Dryomov }
10460247a0cfSIlya Dryomov 
10470247a0cfSIlya Dryomov /*
10483d14c5d2SYehuda Sadeh  * Track open sessions with osds.
10493d14c5d2SYehuda Sadeh  */
1050e10006f8SAlex Elder static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
10513d14c5d2SYehuda Sadeh {
10523d14c5d2SYehuda Sadeh 	struct ceph_osd *osd;
10533d14c5d2SYehuda Sadeh 
10540247a0cfSIlya Dryomov 	WARN_ON(onum == CEPH_HOMELESS_OSD);
10550247a0cfSIlya Dryomov 
10567a28f59bSIlya Dryomov 	osd = kzalloc(sizeof(*osd), GFP_NOIO | __GFP_NOFAIL);
10570247a0cfSIlya Dryomov 	osd_init(osd);
10583d14c5d2SYehuda Sadeh 	osd->o_osdc = osdc;
1059e10006f8SAlex Elder 	osd->o_osd = onum;
10603d14c5d2SYehuda Sadeh 
1061b7a9e5ddSSage Weil 	ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
10623d14c5d2SYehuda Sadeh 
10633d14c5d2SYehuda Sadeh 	return osd;
10643d14c5d2SYehuda Sadeh }
10653d14c5d2SYehuda Sadeh 
10663d14c5d2SYehuda Sadeh static struct ceph_osd *get_osd(struct ceph_osd *osd)
10673d14c5d2SYehuda Sadeh {
106802113a0fSElena Reshetova 	if (refcount_inc_not_zero(&osd->o_ref)) {
106902113a0fSElena Reshetova 		dout("get_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref)-1,
107002113a0fSElena Reshetova 		     refcount_read(&osd->o_ref));
10713d14c5d2SYehuda Sadeh 		return osd;
10723d14c5d2SYehuda Sadeh 	} else {
10733d14c5d2SYehuda Sadeh 		dout("get_osd %p FAIL\n", osd);
10743d14c5d2SYehuda Sadeh 		return NULL;
10753d14c5d2SYehuda Sadeh 	}
10763d14c5d2SYehuda Sadeh }
10773d14c5d2SYehuda Sadeh 
10783d14c5d2SYehuda Sadeh static void put_osd(struct ceph_osd *osd)
10793d14c5d2SYehuda Sadeh {
108002113a0fSElena Reshetova 	dout("put_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref),
108102113a0fSElena Reshetova 	     refcount_read(&osd->o_ref) - 1);
108202113a0fSElena Reshetova 	if (refcount_dec_and_test(&osd->o_ref)) {
10830247a0cfSIlya Dryomov 		osd_cleanup(osd);
10843d14c5d2SYehuda Sadeh 		kfree(osd);
10853d14c5d2SYehuda Sadeh 	}
10863d14c5d2SYehuda Sadeh }
10873d14c5d2SYehuda Sadeh 
1088fcd00b68SIlya Dryomov DEFINE_RB_FUNCS(osd, struct ceph_osd, o_osd, o_node)
1089fcd00b68SIlya Dryomov 
10909dd2845cSIlya Dryomov static void __move_osd_to_lru(struct ceph_osd *osd)
10913d14c5d2SYehuda Sadeh {
10929dd2845cSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
10939dd2845cSIlya Dryomov 
10949dd2845cSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
10953d14c5d2SYehuda Sadeh 	BUG_ON(!list_empty(&osd->o_osd_lru));
1096bbf37ec3SIlya Dryomov 
10979dd2845cSIlya Dryomov 	spin_lock(&osdc->osd_lru_lock);
10983d14c5d2SYehuda Sadeh 	list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
10999dd2845cSIlya Dryomov 	spin_unlock(&osdc->osd_lru_lock);
11009dd2845cSIlya Dryomov 
1101a319bf56SIlya Dryomov 	osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
11023d14c5d2SYehuda Sadeh }
11033d14c5d2SYehuda Sadeh 
11049dd2845cSIlya Dryomov static void maybe_move_osd_to_lru(struct ceph_osd *osd)
1105bbf37ec3SIlya Dryomov {
11065aea3dcdSIlya Dryomov 	if (RB_EMPTY_ROOT(&osd->o_requests) &&
1107922dab61SIlya Dryomov 	    RB_EMPTY_ROOT(&osd->o_linger_requests))
11089dd2845cSIlya Dryomov 		__move_osd_to_lru(osd);
1109bbf37ec3SIlya Dryomov }
1110bbf37ec3SIlya Dryomov 
11113d14c5d2SYehuda Sadeh static void __remove_osd_from_lru(struct ceph_osd *osd)
11123d14c5d2SYehuda Sadeh {
11139dd2845cSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
11149dd2845cSIlya Dryomov 
11159dd2845cSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
11169dd2845cSIlya Dryomov 
11179dd2845cSIlya Dryomov 	spin_lock(&osdc->osd_lru_lock);
11183d14c5d2SYehuda Sadeh 	if (!list_empty(&osd->o_osd_lru))
11193d14c5d2SYehuda Sadeh 		list_del_init(&osd->o_osd_lru);
11209dd2845cSIlya Dryomov 	spin_unlock(&osdc->osd_lru_lock);
11213d14c5d2SYehuda Sadeh }
11223d14c5d2SYehuda Sadeh 
11233d14c5d2SYehuda Sadeh /*
11245aea3dcdSIlya Dryomov  * Close the connection and assign any leftover requests to the
11255aea3dcdSIlya Dryomov  * homeless session.
11265aea3dcdSIlya Dryomov  */
11275aea3dcdSIlya Dryomov static void close_osd(struct ceph_osd *osd)
11285aea3dcdSIlya Dryomov {
11295aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
11305aea3dcdSIlya Dryomov 	struct rb_node *n;
11315aea3dcdSIlya Dryomov 
11325aea3dcdSIlya Dryomov 	verify_osdc_wrlocked(osdc);
11335aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
11345aea3dcdSIlya Dryomov 
11355aea3dcdSIlya Dryomov 	ceph_con_close(&osd->o_con);
11365aea3dcdSIlya Dryomov 
11375aea3dcdSIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; ) {
11385aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
11395aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
11405aea3dcdSIlya Dryomov 
11415aea3dcdSIlya Dryomov 		n = rb_next(n); /* unlink_request() */
11425aea3dcdSIlya Dryomov 
11435aea3dcdSIlya Dryomov 		dout(" reassigning req %p tid %llu\n", req, req->r_tid);
11445aea3dcdSIlya Dryomov 		unlink_request(osd, req);
11455aea3dcdSIlya Dryomov 		link_request(&osdc->homeless_osd, req);
11465aea3dcdSIlya Dryomov 	}
1147922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_linger_requests); n; ) {
1148922dab61SIlya Dryomov 		struct ceph_osd_linger_request *lreq =
1149922dab61SIlya Dryomov 		    rb_entry(n, struct ceph_osd_linger_request, node);
1150922dab61SIlya Dryomov 
1151922dab61SIlya Dryomov 		n = rb_next(n); /* unlink_linger() */
1152922dab61SIlya Dryomov 
1153922dab61SIlya Dryomov 		dout(" reassigning lreq %p linger_id %llu\n", lreq,
1154922dab61SIlya Dryomov 		     lreq->linger_id);
1155922dab61SIlya Dryomov 		unlink_linger(osd, lreq);
1156922dab61SIlya Dryomov 		link_linger(&osdc->homeless_osd, lreq);
1157922dab61SIlya Dryomov 	}
1158a02a946dSIlya Dryomov 	clear_backoffs(osd);
11595aea3dcdSIlya Dryomov 
11605aea3dcdSIlya Dryomov 	__remove_osd_from_lru(osd);
11615aea3dcdSIlya Dryomov 	erase_osd(&osdc->osds, osd);
11625aea3dcdSIlya Dryomov 	put_osd(osd);
11635aea3dcdSIlya Dryomov }
11645aea3dcdSIlya Dryomov 
11655aea3dcdSIlya Dryomov /*
11663d14c5d2SYehuda Sadeh  * reset osd connect
11673d14c5d2SYehuda Sadeh  */
11685aea3dcdSIlya Dryomov static int reopen_osd(struct ceph_osd *osd)
11693d14c5d2SYehuda Sadeh {
1170c3acb181SAlex Elder 	struct ceph_entity_addr *peer_addr;
11713d14c5d2SYehuda Sadeh 
11725aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
11735aea3dcdSIlya Dryomov 
11745aea3dcdSIlya Dryomov 	if (RB_EMPTY_ROOT(&osd->o_requests) &&
1175922dab61SIlya Dryomov 	    RB_EMPTY_ROOT(&osd->o_linger_requests)) {
11765aea3dcdSIlya Dryomov 		close_osd(osd);
1177c3acb181SAlex Elder 		return -ENODEV;
1178c3acb181SAlex Elder 	}
1179c3acb181SAlex Elder 
11805aea3dcdSIlya Dryomov 	peer_addr = &osd->o_osdc->osdmap->osd_addr[osd->o_osd];
1181c3acb181SAlex Elder 	if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
11823d14c5d2SYehuda Sadeh 			!ceph_con_opened(&osd->o_con)) {
11835aea3dcdSIlya Dryomov 		struct rb_node *n;
1184c3acb181SAlex Elder 
11853d14c5d2SYehuda Sadeh 		dout("osd addr hasn't changed and connection never opened, "
11860b4af2e8SIlya Dryomov 		     "letting msgr retry\n");
11873d14c5d2SYehuda Sadeh 		/* touch each r_stamp for handle_timeout()'s benfit */
11885aea3dcdSIlya Dryomov 		for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
11895aea3dcdSIlya Dryomov 			struct ceph_osd_request *req =
11905aea3dcdSIlya Dryomov 			    rb_entry(n, struct ceph_osd_request, r_node);
11913d14c5d2SYehuda Sadeh 			req->r_stamp = jiffies;
11925aea3dcdSIlya Dryomov 		}
1193c3acb181SAlex Elder 
1194c3acb181SAlex Elder 		return -EAGAIN;
11953d14c5d2SYehuda Sadeh 	}
1196c3acb181SAlex Elder 
1197c3acb181SAlex Elder 	ceph_con_close(&osd->o_con);
1198c3acb181SAlex Elder 	ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1199c3acb181SAlex Elder 	osd->o_incarnation++;
1200c3acb181SAlex Elder 
1201c3acb181SAlex Elder 	return 0;
12023d14c5d2SYehuda Sadeh }
12033d14c5d2SYehuda Sadeh 
12045aea3dcdSIlya Dryomov static struct ceph_osd *lookup_create_osd(struct ceph_osd_client *osdc, int o,
12055aea3dcdSIlya Dryomov 					  bool wrlocked)
12063d14c5d2SYehuda Sadeh {
12075aea3dcdSIlya Dryomov 	struct ceph_osd *osd;
12085aea3dcdSIlya Dryomov 
12095aea3dcdSIlya Dryomov 	if (wrlocked)
12105aea3dcdSIlya Dryomov 		verify_osdc_wrlocked(osdc);
12115aea3dcdSIlya Dryomov 	else
12125aea3dcdSIlya Dryomov 		verify_osdc_locked(osdc);
12135aea3dcdSIlya Dryomov 
12145aea3dcdSIlya Dryomov 	if (o != CEPH_HOMELESS_OSD)
12155aea3dcdSIlya Dryomov 		osd = lookup_osd(&osdc->osds, o);
12165aea3dcdSIlya Dryomov 	else
12175aea3dcdSIlya Dryomov 		osd = &osdc->homeless_osd;
12185aea3dcdSIlya Dryomov 	if (!osd) {
12195aea3dcdSIlya Dryomov 		if (!wrlocked)
12205aea3dcdSIlya Dryomov 			return ERR_PTR(-EAGAIN);
12215aea3dcdSIlya Dryomov 
12225aea3dcdSIlya Dryomov 		osd = create_osd(osdc, o);
12235aea3dcdSIlya Dryomov 		insert_osd(&osdc->osds, osd);
12245aea3dcdSIlya Dryomov 		ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
12255aea3dcdSIlya Dryomov 			      &osdc->osdmap->osd_addr[osd->o_osd]);
12265aea3dcdSIlya Dryomov 	}
12275aea3dcdSIlya Dryomov 
12285aea3dcdSIlya Dryomov 	dout("%s osdc %p osd%d -> osd %p\n", __func__, osdc, o, osd);
12295aea3dcdSIlya Dryomov 	return osd;
1230a40c4f10SYehuda Sadeh }
1231a40c4f10SYehuda Sadeh 
12323d14c5d2SYehuda Sadeh /*
12335aea3dcdSIlya Dryomov  * Create request <-> OSD session relation.
12345aea3dcdSIlya Dryomov  *
12355aea3dcdSIlya Dryomov  * @req has to be assigned a tid, @osd may be homeless.
12363d14c5d2SYehuda Sadeh  */
12375aea3dcdSIlya Dryomov static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req)
12383d14c5d2SYehuda Sadeh {
12395aea3dcdSIlya Dryomov 	verify_osd_locked(osd);
12405aea3dcdSIlya Dryomov 	WARN_ON(!req->r_tid || req->r_osd);
12415aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
124235f9f8a0SSage Weil 	     req, req->r_tid);
12435aea3dcdSIlya Dryomov 
12445aea3dcdSIlya Dryomov 	if (!osd_homeless(osd))
12455aea3dcdSIlya Dryomov 		__remove_osd_from_lru(osd);
12465aea3dcdSIlya Dryomov 	else
12475aea3dcdSIlya Dryomov 		atomic_inc(&osd->o_osdc->num_homeless);
12485aea3dcdSIlya Dryomov 
12495aea3dcdSIlya Dryomov 	get_osd(osd);
12505aea3dcdSIlya Dryomov 	insert_request(&osd->o_requests, req);
12515aea3dcdSIlya Dryomov 	req->r_osd = osd;
125235f9f8a0SSage Weil }
125335f9f8a0SSage Weil 
12545aea3dcdSIlya Dryomov static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req)
12553d14c5d2SYehuda Sadeh {
12565aea3dcdSIlya Dryomov 	verify_osd_locked(osd);
12575aea3dcdSIlya Dryomov 	WARN_ON(req->r_osd != osd);
12585aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
12595aea3dcdSIlya Dryomov 	     req, req->r_tid);
12605aea3dcdSIlya Dryomov 
12615aea3dcdSIlya Dryomov 	req->r_osd = NULL;
12625aea3dcdSIlya Dryomov 	erase_request(&osd->o_requests, req);
12635aea3dcdSIlya Dryomov 	put_osd(osd);
12645aea3dcdSIlya Dryomov 
12655aea3dcdSIlya Dryomov 	if (!osd_homeless(osd))
12665aea3dcdSIlya Dryomov 		maybe_move_osd_to_lru(osd);
12675aea3dcdSIlya Dryomov 	else
12685aea3dcdSIlya Dryomov 		atomic_dec(&osd->o_osdc->num_homeless);
12693d14c5d2SYehuda Sadeh }
12703d14c5d2SYehuda Sadeh 
127163244fa1SIlya Dryomov static bool __pool_full(struct ceph_pg_pool_info *pi)
127263244fa1SIlya Dryomov {
127363244fa1SIlya Dryomov 	return pi->flags & CEPH_POOL_FLAG_FULL;
127463244fa1SIlya Dryomov }
127563244fa1SIlya Dryomov 
127642c1b124SIlya Dryomov static bool have_pool_full(struct ceph_osd_client *osdc)
127742c1b124SIlya Dryomov {
127842c1b124SIlya Dryomov 	struct rb_node *n;
127942c1b124SIlya Dryomov 
128042c1b124SIlya Dryomov 	for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
128142c1b124SIlya Dryomov 		struct ceph_pg_pool_info *pi =
128242c1b124SIlya Dryomov 		    rb_entry(n, struct ceph_pg_pool_info, node);
128342c1b124SIlya Dryomov 
128442c1b124SIlya Dryomov 		if (__pool_full(pi))
128542c1b124SIlya Dryomov 			return true;
128642c1b124SIlya Dryomov 	}
128742c1b124SIlya Dryomov 
128842c1b124SIlya Dryomov 	return false;
128942c1b124SIlya Dryomov }
129042c1b124SIlya Dryomov 
12915aea3dcdSIlya Dryomov static bool pool_full(struct ceph_osd_client *osdc, s64 pool_id)
12925aea3dcdSIlya Dryomov {
12935aea3dcdSIlya Dryomov 	struct ceph_pg_pool_info *pi;
12945aea3dcdSIlya Dryomov 
12955aea3dcdSIlya Dryomov 	pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
12965aea3dcdSIlya Dryomov 	if (!pi)
12975aea3dcdSIlya Dryomov 		return false;
12985aea3dcdSIlya Dryomov 
12995aea3dcdSIlya Dryomov 	return __pool_full(pi);
13005aea3dcdSIlya Dryomov }
13015aea3dcdSIlya Dryomov 
13023d14c5d2SYehuda Sadeh /*
1303d29adb34SJosh Durgin  * Returns whether a request should be blocked from being sent
1304d29adb34SJosh Durgin  * based on the current osdmap and osd_client settings.
1305d29adb34SJosh Durgin  */
130663244fa1SIlya Dryomov static bool target_should_be_paused(struct ceph_osd_client *osdc,
130763244fa1SIlya Dryomov 				    const struct ceph_osd_request_target *t,
130863244fa1SIlya Dryomov 				    struct ceph_pg_pool_info *pi)
130963244fa1SIlya Dryomov {
1310b7ec35b3SIlya Dryomov 	bool pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
1311b7ec35b3SIlya Dryomov 	bool pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
1312b7ec35b3SIlya Dryomov 		       ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
131363244fa1SIlya Dryomov 		       __pool_full(pi);
131463244fa1SIlya Dryomov 
13156d637a54SIlya Dryomov 	WARN_ON(pi->id != t->target_oloc.pool);
131658eb7932SJeff Layton 	return ((t->flags & CEPH_OSD_FLAG_READ) && pauserd) ||
131758eb7932SJeff Layton 	       ((t->flags & CEPH_OSD_FLAG_WRITE) && pausewr) ||
131858eb7932SJeff Layton 	       (osdc->osdmap->epoch < osdc->epoch_barrier);
131963244fa1SIlya Dryomov }
132063244fa1SIlya Dryomov 
132163244fa1SIlya Dryomov enum calc_target_result {
132263244fa1SIlya Dryomov 	CALC_TARGET_NO_ACTION = 0,
132363244fa1SIlya Dryomov 	CALC_TARGET_NEED_RESEND,
132463244fa1SIlya Dryomov 	CALC_TARGET_POOL_DNE,
132563244fa1SIlya Dryomov };
132663244fa1SIlya Dryomov 
132763244fa1SIlya Dryomov static enum calc_target_result calc_target(struct ceph_osd_client *osdc,
132863244fa1SIlya Dryomov 					   struct ceph_osd_request_target *t,
13297de030d6SIlya Dryomov 					   struct ceph_connection *con,
133063244fa1SIlya Dryomov 					   bool any_change)
133163244fa1SIlya Dryomov {
133263244fa1SIlya Dryomov 	struct ceph_pg_pool_info *pi;
133363244fa1SIlya Dryomov 	struct ceph_pg pgid, last_pgid;
133463244fa1SIlya Dryomov 	struct ceph_osds up, acting;
133563244fa1SIlya Dryomov 	bool force_resend = false;
133684ed45dfSIlya Dryomov 	bool unpaused = false;
133784ed45dfSIlya Dryomov 	bool legacy_change;
13387de030d6SIlya Dryomov 	bool split = false;
1339b7ec35b3SIlya Dryomov 	bool sort_bitwise = ceph_osdmap_flag(osdc, CEPH_OSDMAP_SORTBITWISE);
1340ae78dd81SIlya Dryomov 	bool recovery_deletes = ceph_osdmap_flag(osdc,
1341ae78dd81SIlya Dryomov 						 CEPH_OSDMAP_RECOVERY_DELETES);
134263244fa1SIlya Dryomov 	enum calc_target_result ct_res;
134363244fa1SIlya Dryomov 	int ret;
134463244fa1SIlya Dryomov 
134504c7d789SIlya Dryomov 	t->epoch = osdc->osdmap->epoch;
134663244fa1SIlya Dryomov 	pi = ceph_pg_pool_by_id(osdc->osdmap, t->base_oloc.pool);
134763244fa1SIlya Dryomov 	if (!pi) {
134863244fa1SIlya Dryomov 		t->osd = CEPH_HOMELESS_OSD;
134963244fa1SIlya Dryomov 		ct_res = CALC_TARGET_POOL_DNE;
135063244fa1SIlya Dryomov 		goto out;
135163244fa1SIlya Dryomov 	}
135263244fa1SIlya Dryomov 
135363244fa1SIlya Dryomov 	if (osdc->osdmap->epoch == pi->last_force_request_resend) {
1354dc93e0e2SIlya Dryomov 		if (t->last_force_resend < pi->last_force_request_resend) {
1355dc93e0e2SIlya Dryomov 			t->last_force_resend = pi->last_force_request_resend;
135663244fa1SIlya Dryomov 			force_resend = true;
1357dc93e0e2SIlya Dryomov 		} else if (t->last_force_resend == 0) {
135863244fa1SIlya Dryomov 			force_resend = true;
135963244fa1SIlya Dryomov 		}
136063244fa1SIlya Dryomov 	}
136163244fa1SIlya Dryomov 
1362db098ec4SIlya Dryomov 	/* apply tiering */
1363db098ec4SIlya Dryomov 	ceph_oid_copy(&t->target_oid, &t->base_oid);
1364db098ec4SIlya Dryomov 	ceph_oloc_copy(&t->target_oloc, &t->base_oloc);
1365db098ec4SIlya Dryomov 	if ((t->flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
136663244fa1SIlya Dryomov 		if (t->flags & CEPH_OSD_FLAG_READ && pi->read_tier >= 0)
136763244fa1SIlya Dryomov 			t->target_oloc.pool = pi->read_tier;
136863244fa1SIlya Dryomov 		if (t->flags & CEPH_OSD_FLAG_WRITE && pi->write_tier >= 0)
136963244fa1SIlya Dryomov 			t->target_oloc.pool = pi->write_tier;
13706d637a54SIlya Dryomov 
13716d637a54SIlya Dryomov 		pi = ceph_pg_pool_by_id(osdc->osdmap, t->target_oloc.pool);
13726d637a54SIlya Dryomov 		if (!pi) {
13736d637a54SIlya Dryomov 			t->osd = CEPH_HOMELESS_OSD;
13746d637a54SIlya Dryomov 			ct_res = CALC_TARGET_POOL_DNE;
13756d637a54SIlya Dryomov 			goto out;
13766d637a54SIlya Dryomov 		}
137763244fa1SIlya Dryomov 	}
137863244fa1SIlya Dryomov 
1379df28152dSIlya Dryomov 	ret = __ceph_object_locator_to_pg(pi, &t->target_oid, &t->target_oloc,
1380df28152dSIlya Dryomov 					  &pgid);
138163244fa1SIlya Dryomov 	if (ret) {
138263244fa1SIlya Dryomov 		WARN_ON(ret != -ENOENT);
138363244fa1SIlya Dryomov 		t->osd = CEPH_HOMELESS_OSD;
138463244fa1SIlya Dryomov 		ct_res = CALC_TARGET_POOL_DNE;
138563244fa1SIlya Dryomov 		goto out;
138663244fa1SIlya Dryomov 	}
138763244fa1SIlya Dryomov 	last_pgid.pool = pgid.pool;
138863244fa1SIlya Dryomov 	last_pgid.seed = ceph_stable_mod(pgid.seed, t->pg_num, t->pg_num_mask);
138963244fa1SIlya Dryomov 
1390df28152dSIlya Dryomov 	ceph_pg_to_up_acting_osds(osdc->osdmap, pi, &pgid, &up, &acting);
139163244fa1SIlya Dryomov 	if (any_change &&
139263244fa1SIlya Dryomov 	    ceph_is_new_interval(&t->acting,
139363244fa1SIlya Dryomov 				 &acting,
139463244fa1SIlya Dryomov 				 &t->up,
139563244fa1SIlya Dryomov 				 &up,
139663244fa1SIlya Dryomov 				 t->size,
139763244fa1SIlya Dryomov 				 pi->size,
139863244fa1SIlya Dryomov 				 t->min_size,
139963244fa1SIlya Dryomov 				 pi->min_size,
140063244fa1SIlya Dryomov 				 t->pg_num,
140163244fa1SIlya Dryomov 				 pi->pg_num,
140263244fa1SIlya Dryomov 				 t->sort_bitwise,
140363244fa1SIlya Dryomov 				 sort_bitwise,
1404ae78dd81SIlya Dryomov 				 t->recovery_deletes,
1405ae78dd81SIlya Dryomov 				 recovery_deletes,
140663244fa1SIlya Dryomov 				 &last_pgid))
140763244fa1SIlya Dryomov 		force_resend = true;
140863244fa1SIlya Dryomov 
140963244fa1SIlya Dryomov 	if (t->paused && !target_should_be_paused(osdc, t, pi)) {
141063244fa1SIlya Dryomov 		t->paused = false;
141184ed45dfSIlya Dryomov 		unpaused = true;
141263244fa1SIlya Dryomov 	}
141384ed45dfSIlya Dryomov 	legacy_change = ceph_pg_compare(&t->pgid, &pgid) ||
141484ed45dfSIlya Dryomov 			ceph_osds_changed(&t->acting, &acting, any_change);
14157de030d6SIlya Dryomov 	if (t->pg_num)
14167de030d6SIlya Dryomov 		split = ceph_pg_is_split(&last_pgid, t->pg_num, pi->pg_num);
141763244fa1SIlya Dryomov 
14187de030d6SIlya Dryomov 	if (legacy_change || force_resend || split) {
141963244fa1SIlya Dryomov 		t->pgid = pgid; /* struct */
1420df28152dSIlya Dryomov 		ceph_pg_to_primary_shard(osdc->osdmap, pi, &pgid, &t->spgid);
142163244fa1SIlya Dryomov 		ceph_osds_copy(&t->acting, &acting);
142263244fa1SIlya Dryomov 		ceph_osds_copy(&t->up, &up);
142363244fa1SIlya Dryomov 		t->size = pi->size;
142463244fa1SIlya Dryomov 		t->min_size = pi->min_size;
142563244fa1SIlya Dryomov 		t->pg_num = pi->pg_num;
142663244fa1SIlya Dryomov 		t->pg_num_mask = pi->pg_num_mask;
142763244fa1SIlya Dryomov 		t->sort_bitwise = sort_bitwise;
1428ae78dd81SIlya Dryomov 		t->recovery_deletes = recovery_deletes;
142963244fa1SIlya Dryomov 
143063244fa1SIlya Dryomov 		t->osd = acting.primary;
143163244fa1SIlya Dryomov 	}
143263244fa1SIlya Dryomov 
14337de030d6SIlya Dryomov 	if (unpaused || legacy_change || force_resend ||
14347de030d6SIlya Dryomov 	    (split && con && CEPH_HAVE_FEATURE(con->peer_features,
14357de030d6SIlya Dryomov 					       RESEND_ON_SPLIT)))
143684ed45dfSIlya Dryomov 		ct_res = CALC_TARGET_NEED_RESEND;
143784ed45dfSIlya Dryomov 	else
143884ed45dfSIlya Dryomov 		ct_res = CALC_TARGET_NO_ACTION;
143984ed45dfSIlya Dryomov 
144063244fa1SIlya Dryomov out:
144163244fa1SIlya Dryomov 	dout("%s t %p -> ct_res %d osd %d\n", __func__, t, ct_res, t->osd);
144263244fa1SIlya Dryomov 	return ct_res;
144363244fa1SIlya Dryomov }
144463244fa1SIlya Dryomov 
1445a02a946dSIlya Dryomov static struct ceph_spg_mapping *alloc_spg_mapping(void)
1446a02a946dSIlya Dryomov {
1447a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
1448a02a946dSIlya Dryomov 
1449a02a946dSIlya Dryomov 	spg = kmalloc(sizeof(*spg), GFP_NOIO);
1450a02a946dSIlya Dryomov 	if (!spg)
1451a02a946dSIlya Dryomov 		return NULL;
1452a02a946dSIlya Dryomov 
1453a02a946dSIlya Dryomov 	RB_CLEAR_NODE(&spg->node);
1454a02a946dSIlya Dryomov 	spg->backoffs = RB_ROOT;
1455a02a946dSIlya Dryomov 	return spg;
1456a02a946dSIlya Dryomov }
1457a02a946dSIlya Dryomov 
1458a02a946dSIlya Dryomov static void free_spg_mapping(struct ceph_spg_mapping *spg)
1459a02a946dSIlya Dryomov {
1460a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&spg->node));
1461a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&spg->backoffs));
1462a02a946dSIlya Dryomov 
1463a02a946dSIlya Dryomov 	kfree(spg);
1464a02a946dSIlya Dryomov }
1465a02a946dSIlya Dryomov 
1466a02a946dSIlya Dryomov /*
1467a02a946dSIlya Dryomov  * rbtree of ceph_spg_mapping for handling map<spg_t, ...>, similar to
1468a02a946dSIlya Dryomov  * ceph_pg_mapping.  Used to track OSD backoffs -- a backoff [range] is
1469a02a946dSIlya Dryomov  * defined only within a specific spgid; it does not pass anything to
1470a02a946dSIlya Dryomov  * children on split, or to another primary.
1471a02a946dSIlya Dryomov  */
1472a02a946dSIlya Dryomov DEFINE_RB_FUNCS2(spg_mapping, struct ceph_spg_mapping, spgid, ceph_spg_compare,
1473a02a946dSIlya Dryomov 		 RB_BYPTR, const struct ceph_spg *, node)
1474a02a946dSIlya Dryomov 
1475a02a946dSIlya Dryomov static u64 hoid_get_bitwise_key(const struct ceph_hobject_id *hoid)
1476a02a946dSIlya Dryomov {
1477a02a946dSIlya Dryomov 	return hoid->is_max ? 0x100000000ull : hoid->hash_reverse_bits;
1478a02a946dSIlya Dryomov }
1479a02a946dSIlya Dryomov 
1480a02a946dSIlya Dryomov static void hoid_get_effective_key(const struct ceph_hobject_id *hoid,
1481a02a946dSIlya Dryomov 				   void **pkey, size_t *pkey_len)
1482a02a946dSIlya Dryomov {
1483a02a946dSIlya Dryomov 	if (hoid->key_len) {
1484a02a946dSIlya Dryomov 		*pkey = hoid->key;
1485a02a946dSIlya Dryomov 		*pkey_len = hoid->key_len;
1486a02a946dSIlya Dryomov 	} else {
1487a02a946dSIlya Dryomov 		*pkey = hoid->oid;
1488a02a946dSIlya Dryomov 		*pkey_len = hoid->oid_len;
1489a02a946dSIlya Dryomov 	}
1490a02a946dSIlya Dryomov }
1491a02a946dSIlya Dryomov 
1492a02a946dSIlya Dryomov static int compare_names(const void *name1, size_t name1_len,
1493a02a946dSIlya Dryomov 			 const void *name2, size_t name2_len)
1494a02a946dSIlya Dryomov {
1495a02a946dSIlya Dryomov 	int ret;
1496a02a946dSIlya Dryomov 
1497a02a946dSIlya Dryomov 	ret = memcmp(name1, name2, min(name1_len, name2_len));
1498a02a946dSIlya Dryomov 	if (!ret) {
1499a02a946dSIlya Dryomov 		if (name1_len < name2_len)
1500a02a946dSIlya Dryomov 			ret = -1;
1501a02a946dSIlya Dryomov 		else if (name1_len > name2_len)
1502a02a946dSIlya Dryomov 			ret = 1;
1503a02a946dSIlya Dryomov 	}
1504a02a946dSIlya Dryomov 	return ret;
1505a02a946dSIlya Dryomov }
1506a02a946dSIlya Dryomov 
1507a02a946dSIlya Dryomov static int hoid_compare(const struct ceph_hobject_id *lhs,
1508a02a946dSIlya Dryomov 			const struct ceph_hobject_id *rhs)
1509a02a946dSIlya Dryomov {
1510a02a946dSIlya Dryomov 	void *effective_key1, *effective_key2;
1511a02a946dSIlya Dryomov 	size_t effective_key1_len, effective_key2_len;
1512a02a946dSIlya Dryomov 	int ret;
1513a02a946dSIlya Dryomov 
1514a02a946dSIlya Dryomov 	if (lhs->is_max < rhs->is_max)
1515a02a946dSIlya Dryomov 		return -1;
1516a02a946dSIlya Dryomov 	if (lhs->is_max > rhs->is_max)
1517a02a946dSIlya Dryomov 		return 1;
1518a02a946dSIlya Dryomov 
1519a02a946dSIlya Dryomov 	if (lhs->pool < rhs->pool)
1520a02a946dSIlya Dryomov 		return -1;
1521a02a946dSIlya Dryomov 	if (lhs->pool > rhs->pool)
1522a02a946dSIlya Dryomov 		return 1;
1523a02a946dSIlya Dryomov 
1524a02a946dSIlya Dryomov 	if (hoid_get_bitwise_key(lhs) < hoid_get_bitwise_key(rhs))
1525a02a946dSIlya Dryomov 		return -1;
1526a02a946dSIlya Dryomov 	if (hoid_get_bitwise_key(lhs) > hoid_get_bitwise_key(rhs))
1527a02a946dSIlya Dryomov 		return 1;
1528a02a946dSIlya Dryomov 
1529a02a946dSIlya Dryomov 	ret = compare_names(lhs->nspace, lhs->nspace_len,
1530a02a946dSIlya Dryomov 			    rhs->nspace, rhs->nspace_len);
1531a02a946dSIlya Dryomov 	if (ret)
1532a02a946dSIlya Dryomov 		return ret;
1533a02a946dSIlya Dryomov 
1534a02a946dSIlya Dryomov 	hoid_get_effective_key(lhs, &effective_key1, &effective_key1_len);
1535a02a946dSIlya Dryomov 	hoid_get_effective_key(rhs, &effective_key2, &effective_key2_len);
1536a02a946dSIlya Dryomov 	ret = compare_names(effective_key1, effective_key1_len,
1537a02a946dSIlya Dryomov 			    effective_key2, effective_key2_len);
1538a02a946dSIlya Dryomov 	if (ret)
1539a02a946dSIlya Dryomov 		return ret;
1540a02a946dSIlya Dryomov 
1541a02a946dSIlya Dryomov 	ret = compare_names(lhs->oid, lhs->oid_len, rhs->oid, rhs->oid_len);
1542a02a946dSIlya Dryomov 	if (ret)
1543a02a946dSIlya Dryomov 		return ret;
1544a02a946dSIlya Dryomov 
1545a02a946dSIlya Dryomov 	if (lhs->snapid < rhs->snapid)
1546a02a946dSIlya Dryomov 		return -1;
1547a02a946dSIlya Dryomov 	if (lhs->snapid > rhs->snapid)
1548a02a946dSIlya Dryomov 		return 1;
1549a02a946dSIlya Dryomov 
1550a02a946dSIlya Dryomov 	return 0;
1551a02a946dSIlya Dryomov }
1552a02a946dSIlya Dryomov 
1553a02a946dSIlya Dryomov /*
1554a02a946dSIlya Dryomov  * For decoding ->begin and ->end of MOSDBackoff only -- no MIN/MAX
1555a02a946dSIlya Dryomov  * compat stuff here.
1556a02a946dSIlya Dryomov  *
1557a02a946dSIlya Dryomov  * Assumes @hoid is zero-initialized.
1558a02a946dSIlya Dryomov  */
1559a02a946dSIlya Dryomov static int decode_hoid(void **p, void *end, struct ceph_hobject_id *hoid)
1560a02a946dSIlya Dryomov {
1561a02a946dSIlya Dryomov 	u8 struct_v;
1562a02a946dSIlya Dryomov 	u32 struct_len;
1563a02a946dSIlya Dryomov 	int ret;
1564a02a946dSIlya Dryomov 
1565a02a946dSIlya Dryomov 	ret = ceph_start_decoding(p, end, 4, "hobject_t", &struct_v,
1566a02a946dSIlya Dryomov 				  &struct_len);
1567a02a946dSIlya Dryomov 	if (ret)
1568a02a946dSIlya Dryomov 		return ret;
1569a02a946dSIlya Dryomov 
1570a02a946dSIlya Dryomov 	if (struct_v < 4) {
1571a02a946dSIlya Dryomov 		pr_err("got struct_v %d < 4 of hobject_t\n", struct_v);
1572a02a946dSIlya Dryomov 		goto e_inval;
1573a02a946dSIlya Dryomov 	}
1574a02a946dSIlya Dryomov 
1575a02a946dSIlya Dryomov 	hoid->key = ceph_extract_encoded_string(p, end, &hoid->key_len,
1576a02a946dSIlya Dryomov 						GFP_NOIO);
1577a02a946dSIlya Dryomov 	if (IS_ERR(hoid->key)) {
1578a02a946dSIlya Dryomov 		ret = PTR_ERR(hoid->key);
1579a02a946dSIlya Dryomov 		hoid->key = NULL;
1580a02a946dSIlya Dryomov 		return ret;
1581a02a946dSIlya Dryomov 	}
1582a02a946dSIlya Dryomov 
1583a02a946dSIlya Dryomov 	hoid->oid = ceph_extract_encoded_string(p, end, &hoid->oid_len,
1584a02a946dSIlya Dryomov 						GFP_NOIO);
1585a02a946dSIlya Dryomov 	if (IS_ERR(hoid->oid)) {
1586a02a946dSIlya Dryomov 		ret = PTR_ERR(hoid->oid);
1587a02a946dSIlya Dryomov 		hoid->oid = NULL;
1588a02a946dSIlya Dryomov 		return ret;
1589a02a946dSIlya Dryomov 	}
1590a02a946dSIlya Dryomov 
1591a02a946dSIlya Dryomov 	ceph_decode_64_safe(p, end, hoid->snapid, e_inval);
1592a02a946dSIlya Dryomov 	ceph_decode_32_safe(p, end, hoid->hash, e_inval);
1593a02a946dSIlya Dryomov 	ceph_decode_8_safe(p, end, hoid->is_max, e_inval);
1594a02a946dSIlya Dryomov 
1595a02a946dSIlya Dryomov 	hoid->nspace = ceph_extract_encoded_string(p, end, &hoid->nspace_len,
1596a02a946dSIlya Dryomov 						   GFP_NOIO);
1597a02a946dSIlya Dryomov 	if (IS_ERR(hoid->nspace)) {
1598a02a946dSIlya Dryomov 		ret = PTR_ERR(hoid->nspace);
1599a02a946dSIlya Dryomov 		hoid->nspace = NULL;
1600a02a946dSIlya Dryomov 		return ret;
1601a02a946dSIlya Dryomov 	}
1602a02a946dSIlya Dryomov 
1603a02a946dSIlya Dryomov 	ceph_decode_64_safe(p, end, hoid->pool, e_inval);
1604a02a946dSIlya Dryomov 
1605a02a946dSIlya Dryomov 	ceph_hoid_build_hash_cache(hoid);
1606a02a946dSIlya Dryomov 	return 0;
1607a02a946dSIlya Dryomov 
1608a02a946dSIlya Dryomov e_inval:
1609a02a946dSIlya Dryomov 	return -EINVAL;
1610a02a946dSIlya Dryomov }
1611a02a946dSIlya Dryomov 
1612a02a946dSIlya Dryomov static int hoid_encoding_size(const struct ceph_hobject_id *hoid)
1613a02a946dSIlya Dryomov {
1614a02a946dSIlya Dryomov 	return 8 + 4 + 1 + 8 + /* snapid, hash, is_max, pool */
1615a02a946dSIlya Dryomov 	       4 + hoid->key_len + 4 + hoid->oid_len + 4 + hoid->nspace_len;
1616a02a946dSIlya Dryomov }
1617a02a946dSIlya Dryomov 
1618a02a946dSIlya Dryomov static void encode_hoid(void **p, void *end, const struct ceph_hobject_id *hoid)
1619a02a946dSIlya Dryomov {
1620a02a946dSIlya Dryomov 	ceph_start_encoding(p, 4, 3, hoid_encoding_size(hoid));
1621a02a946dSIlya Dryomov 	ceph_encode_string(p, end, hoid->key, hoid->key_len);
1622a02a946dSIlya Dryomov 	ceph_encode_string(p, end, hoid->oid, hoid->oid_len);
1623a02a946dSIlya Dryomov 	ceph_encode_64(p, hoid->snapid);
1624a02a946dSIlya Dryomov 	ceph_encode_32(p, hoid->hash);
1625a02a946dSIlya Dryomov 	ceph_encode_8(p, hoid->is_max);
1626a02a946dSIlya Dryomov 	ceph_encode_string(p, end, hoid->nspace, hoid->nspace_len);
1627a02a946dSIlya Dryomov 	ceph_encode_64(p, hoid->pool);
1628a02a946dSIlya Dryomov }
1629a02a946dSIlya Dryomov 
1630a02a946dSIlya Dryomov static void free_hoid(struct ceph_hobject_id *hoid)
1631a02a946dSIlya Dryomov {
1632a02a946dSIlya Dryomov 	if (hoid) {
1633a02a946dSIlya Dryomov 		kfree(hoid->key);
1634a02a946dSIlya Dryomov 		kfree(hoid->oid);
1635a02a946dSIlya Dryomov 		kfree(hoid->nspace);
1636a02a946dSIlya Dryomov 		kfree(hoid);
1637a02a946dSIlya Dryomov 	}
1638a02a946dSIlya Dryomov }
1639a02a946dSIlya Dryomov 
1640a02a946dSIlya Dryomov static struct ceph_osd_backoff *alloc_backoff(void)
1641a02a946dSIlya Dryomov {
1642a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
1643a02a946dSIlya Dryomov 
1644a02a946dSIlya Dryomov 	backoff = kzalloc(sizeof(*backoff), GFP_NOIO);
1645a02a946dSIlya Dryomov 	if (!backoff)
1646a02a946dSIlya Dryomov 		return NULL;
1647a02a946dSIlya Dryomov 
1648a02a946dSIlya Dryomov 	RB_CLEAR_NODE(&backoff->spg_node);
1649a02a946dSIlya Dryomov 	RB_CLEAR_NODE(&backoff->id_node);
1650a02a946dSIlya Dryomov 	return backoff;
1651a02a946dSIlya Dryomov }
1652a02a946dSIlya Dryomov 
1653a02a946dSIlya Dryomov static void free_backoff(struct ceph_osd_backoff *backoff)
1654a02a946dSIlya Dryomov {
1655a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&backoff->spg_node));
1656a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&backoff->id_node));
1657a02a946dSIlya Dryomov 
1658a02a946dSIlya Dryomov 	free_hoid(backoff->begin);
1659a02a946dSIlya Dryomov 	free_hoid(backoff->end);
1660a02a946dSIlya Dryomov 	kfree(backoff);
1661a02a946dSIlya Dryomov }
1662a02a946dSIlya Dryomov 
1663a02a946dSIlya Dryomov /*
1664a02a946dSIlya Dryomov  * Within a specific spgid, backoffs are managed by ->begin hoid.
1665a02a946dSIlya Dryomov  */
1666a02a946dSIlya Dryomov DEFINE_RB_INSDEL_FUNCS2(backoff, struct ceph_osd_backoff, begin, hoid_compare,
1667a02a946dSIlya Dryomov 			RB_BYVAL, spg_node);
1668a02a946dSIlya Dryomov 
1669a02a946dSIlya Dryomov static struct ceph_osd_backoff *lookup_containing_backoff(struct rb_root *root,
1670a02a946dSIlya Dryomov 					    const struct ceph_hobject_id *hoid)
1671a02a946dSIlya Dryomov {
1672a02a946dSIlya Dryomov 	struct rb_node *n = root->rb_node;
1673a02a946dSIlya Dryomov 
1674a02a946dSIlya Dryomov 	while (n) {
1675a02a946dSIlya Dryomov 		struct ceph_osd_backoff *cur =
1676a02a946dSIlya Dryomov 		    rb_entry(n, struct ceph_osd_backoff, spg_node);
1677a02a946dSIlya Dryomov 		int cmp;
1678a02a946dSIlya Dryomov 
1679a02a946dSIlya Dryomov 		cmp = hoid_compare(hoid, cur->begin);
1680a02a946dSIlya Dryomov 		if (cmp < 0) {
1681a02a946dSIlya Dryomov 			n = n->rb_left;
1682a02a946dSIlya Dryomov 		} else if (cmp > 0) {
1683a02a946dSIlya Dryomov 			if (hoid_compare(hoid, cur->end) < 0)
1684a02a946dSIlya Dryomov 				return cur;
1685a02a946dSIlya Dryomov 
1686a02a946dSIlya Dryomov 			n = n->rb_right;
1687a02a946dSIlya Dryomov 		} else {
1688a02a946dSIlya Dryomov 			return cur;
1689a02a946dSIlya Dryomov 		}
1690a02a946dSIlya Dryomov 	}
1691a02a946dSIlya Dryomov 
1692a02a946dSIlya Dryomov 	return NULL;
1693a02a946dSIlya Dryomov }
1694a02a946dSIlya Dryomov 
1695a02a946dSIlya Dryomov /*
1696a02a946dSIlya Dryomov  * Each backoff has a unique id within its OSD session.
1697a02a946dSIlya Dryomov  */
1698a02a946dSIlya Dryomov DEFINE_RB_FUNCS(backoff_by_id, struct ceph_osd_backoff, id, id_node)
1699a02a946dSIlya Dryomov 
1700a02a946dSIlya Dryomov static void clear_backoffs(struct ceph_osd *osd)
1701a02a946dSIlya Dryomov {
1702a02a946dSIlya Dryomov 	while (!RB_EMPTY_ROOT(&osd->o_backoff_mappings)) {
1703a02a946dSIlya Dryomov 		struct ceph_spg_mapping *spg =
1704a02a946dSIlya Dryomov 		    rb_entry(rb_first(&osd->o_backoff_mappings),
1705a02a946dSIlya Dryomov 			     struct ceph_spg_mapping, node);
1706a02a946dSIlya Dryomov 
1707a02a946dSIlya Dryomov 		while (!RB_EMPTY_ROOT(&spg->backoffs)) {
1708a02a946dSIlya Dryomov 			struct ceph_osd_backoff *backoff =
1709a02a946dSIlya Dryomov 			    rb_entry(rb_first(&spg->backoffs),
1710a02a946dSIlya Dryomov 				     struct ceph_osd_backoff, spg_node);
1711a02a946dSIlya Dryomov 
1712a02a946dSIlya Dryomov 			erase_backoff(&spg->backoffs, backoff);
1713a02a946dSIlya Dryomov 			erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
1714a02a946dSIlya Dryomov 			free_backoff(backoff);
1715a02a946dSIlya Dryomov 		}
1716a02a946dSIlya Dryomov 		erase_spg_mapping(&osd->o_backoff_mappings, spg);
1717a02a946dSIlya Dryomov 		free_spg_mapping(spg);
1718a02a946dSIlya Dryomov 	}
1719a02a946dSIlya Dryomov }
1720a02a946dSIlya Dryomov 
1721a02a946dSIlya Dryomov /*
1722a02a946dSIlya Dryomov  * Set up a temporary, non-owning view into @t.
1723a02a946dSIlya Dryomov  */
1724a02a946dSIlya Dryomov static void hoid_fill_from_target(struct ceph_hobject_id *hoid,
1725a02a946dSIlya Dryomov 				  const struct ceph_osd_request_target *t)
1726a02a946dSIlya Dryomov {
1727a02a946dSIlya Dryomov 	hoid->key = NULL;
1728a02a946dSIlya Dryomov 	hoid->key_len = 0;
1729a02a946dSIlya Dryomov 	hoid->oid = t->target_oid.name;
1730a02a946dSIlya Dryomov 	hoid->oid_len = t->target_oid.name_len;
1731a02a946dSIlya Dryomov 	hoid->snapid = CEPH_NOSNAP;
1732a02a946dSIlya Dryomov 	hoid->hash = t->pgid.seed;
1733a02a946dSIlya Dryomov 	hoid->is_max = false;
1734a02a946dSIlya Dryomov 	if (t->target_oloc.pool_ns) {
1735a02a946dSIlya Dryomov 		hoid->nspace = t->target_oloc.pool_ns->str;
1736a02a946dSIlya Dryomov 		hoid->nspace_len = t->target_oloc.pool_ns->len;
1737a02a946dSIlya Dryomov 	} else {
1738a02a946dSIlya Dryomov 		hoid->nspace = NULL;
1739a02a946dSIlya Dryomov 		hoid->nspace_len = 0;
1740a02a946dSIlya Dryomov 	}
1741a02a946dSIlya Dryomov 	hoid->pool = t->target_oloc.pool;
1742a02a946dSIlya Dryomov 	ceph_hoid_build_hash_cache(hoid);
1743a02a946dSIlya Dryomov }
1744a02a946dSIlya Dryomov 
1745a02a946dSIlya Dryomov static bool should_plug_request(struct ceph_osd_request *req)
1746a02a946dSIlya Dryomov {
1747a02a946dSIlya Dryomov 	struct ceph_osd *osd = req->r_osd;
1748a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
1749a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
1750a02a946dSIlya Dryomov 	struct ceph_hobject_id hoid;
1751a02a946dSIlya Dryomov 
1752a02a946dSIlya Dryomov 	spg = lookup_spg_mapping(&osd->o_backoff_mappings, &req->r_t.spgid);
1753a02a946dSIlya Dryomov 	if (!spg)
1754a02a946dSIlya Dryomov 		return false;
1755a02a946dSIlya Dryomov 
1756a02a946dSIlya Dryomov 	hoid_fill_from_target(&hoid, &req->r_t);
1757a02a946dSIlya Dryomov 	backoff = lookup_containing_backoff(&spg->backoffs, &hoid);
1758a02a946dSIlya Dryomov 	if (!backoff)
1759a02a946dSIlya Dryomov 		return false;
1760a02a946dSIlya Dryomov 
1761a02a946dSIlya Dryomov 	dout("%s req %p tid %llu backoff osd%d spgid %llu.%xs%d id %llu\n",
1762a02a946dSIlya Dryomov 	     __func__, req, req->r_tid, osd->o_osd, backoff->spgid.pgid.pool,
1763a02a946dSIlya Dryomov 	     backoff->spgid.pgid.seed, backoff->spgid.shard, backoff->id);
1764a02a946dSIlya Dryomov 	return true;
1765a02a946dSIlya Dryomov }
1766a02a946dSIlya Dryomov 
1767bb873b53SIlya Dryomov static void setup_request_data(struct ceph_osd_request *req,
1768bb873b53SIlya Dryomov 			       struct ceph_msg *msg)
17693d14c5d2SYehuda Sadeh {
1770bb873b53SIlya Dryomov 	u32 data_len = 0;
1771bb873b53SIlya Dryomov 	int i;
17723d14c5d2SYehuda Sadeh 
1773bb873b53SIlya Dryomov 	if (!list_empty(&msg->data))
1774bb873b53SIlya Dryomov 		return;
17753d14c5d2SYehuda Sadeh 
1776bb873b53SIlya Dryomov 	WARN_ON(msg->data_length);
1777bb873b53SIlya Dryomov 	for (i = 0; i < req->r_num_ops; i++) {
1778bb873b53SIlya Dryomov 		struct ceph_osd_req_op *op = &req->r_ops[i];
1779bb873b53SIlya Dryomov 
1780bb873b53SIlya Dryomov 		switch (op->op) {
1781bb873b53SIlya Dryomov 		/* request */
1782bb873b53SIlya Dryomov 		case CEPH_OSD_OP_WRITE:
1783bb873b53SIlya Dryomov 		case CEPH_OSD_OP_WRITEFULL:
1784bb873b53SIlya Dryomov 			WARN_ON(op->indata_len != op->extent.length);
1785bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(msg, &op->extent.osd_data);
1786bb873b53SIlya Dryomov 			break;
1787bb873b53SIlya Dryomov 		case CEPH_OSD_OP_SETXATTR:
1788bb873b53SIlya Dryomov 		case CEPH_OSD_OP_CMPXATTR:
1789bb873b53SIlya Dryomov 			WARN_ON(op->indata_len != op->xattr.name_len +
1790bb873b53SIlya Dryomov 						  op->xattr.value_len);
1791bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(msg, &op->xattr.osd_data);
1792bb873b53SIlya Dryomov 			break;
1793922dab61SIlya Dryomov 		case CEPH_OSD_OP_NOTIFY_ACK:
1794922dab61SIlya Dryomov 			ceph_osdc_msg_data_add(msg,
1795922dab61SIlya Dryomov 					       &op->notify_ack.request_data);
1796922dab61SIlya Dryomov 			break;
1797bb873b53SIlya Dryomov 
1798bb873b53SIlya Dryomov 		/* reply */
1799bb873b53SIlya Dryomov 		case CEPH_OSD_OP_STAT:
1800bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(req->r_reply,
1801bb873b53SIlya Dryomov 					       &op->raw_data_in);
1802bb873b53SIlya Dryomov 			break;
1803bb873b53SIlya Dryomov 		case CEPH_OSD_OP_READ:
1804bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(req->r_reply,
1805bb873b53SIlya Dryomov 					       &op->extent.osd_data);
1806bb873b53SIlya Dryomov 			break;
1807a4ed38d7SDouglas Fuller 		case CEPH_OSD_OP_LIST_WATCHERS:
1808a4ed38d7SDouglas Fuller 			ceph_osdc_msg_data_add(req->r_reply,
1809a4ed38d7SDouglas Fuller 					       &op->list_watchers.response_data);
1810a4ed38d7SDouglas Fuller 			break;
1811bb873b53SIlya Dryomov 
1812bb873b53SIlya Dryomov 		/* both */
1813bb873b53SIlya Dryomov 		case CEPH_OSD_OP_CALL:
1814bb873b53SIlya Dryomov 			WARN_ON(op->indata_len != op->cls.class_len +
1815bb873b53SIlya Dryomov 						  op->cls.method_len +
1816bb873b53SIlya Dryomov 						  op->cls.indata_len);
1817bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(msg, &op->cls.request_info);
1818bb873b53SIlya Dryomov 			/* optional, can be NONE */
1819bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(msg, &op->cls.request_data);
1820bb873b53SIlya Dryomov 			/* optional, can be NONE */
1821bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(req->r_reply,
1822bb873b53SIlya Dryomov 					       &op->cls.response_data);
1823bb873b53SIlya Dryomov 			break;
182419079203SIlya Dryomov 		case CEPH_OSD_OP_NOTIFY:
182519079203SIlya Dryomov 			ceph_osdc_msg_data_add(msg,
182619079203SIlya Dryomov 					       &op->notify.request_data);
182719079203SIlya Dryomov 			ceph_osdc_msg_data_add(req->r_reply,
182819079203SIlya Dryomov 					       &op->notify.response_data);
182919079203SIlya Dryomov 			break;
1830bb873b53SIlya Dryomov 		}
1831bb873b53SIlya Dryomov 
1832bb873b53SIlya Dryomov 		data_len += op->indata_len;
1833bb873b53SIlya Dryomov 	}
1834bb873b53SIlya Dryomov 
1835bb873b53SIlya Dryomov 	WARN_ON(data_len != msg->data_length);
1836bb873b53SIlya Dryomov }
1837bb873b53SIlya Dryomov 
18382e59ffd1SIlya Dryomov static void encode_pgid(void **p, const struct ceph_pg *pgid)
18392e59ffd1SIlya Dryomov {
18402e59ffd1SIlya Dryomov 	ceph_encode_8(p, 1);
18412e59ffd1SIlya Dryomov 	ceph_encode_64(p, pgid->pool);
18422e59ffd1SIlya Dryomov 	ceph_encode_32(p, pgid->seed);
18432e59ffd1SIlya Dryomov 	ceph_encode_32(p, -1); /* preferred */
18442e59ffd1SIlya Dryomov }
18452e59ffd1SIlya Dryomov 
18468cb441c0SIlya Dryomov static void encode_spgid(void **p, const struct ceph_spg *spgid)
18478cb441c0SIlya Dryomov {
18488cb441c0SIlya Dryomov 	ceph_start_encoding(p, 1, 1, CEPH_PGID_ENCODING_LEN + 1);
18498cb441c0SIlya Dryomov 	encode_pgid(p, &spgid->pgid);
18508cb441c0SIlya Dryomov 	ceph_encode_8(p, spgid->shard);
18518cb441c0SIlya Dryomov }
18528cb441c0SIlya Dryomov 
18532e59ffd1SIlya Dryomov static void encode_oloc(void **p, void *end,
18542e59ffd1SIlya Dryomov 			const struct ceph_object_locator *oloc)
18552e59ffd1SIlya Dryomov {
18562e59ffd1SIlya Dryomov 	ceph_start_encoding(p, 5, 4, ceph_oloc_encoding_size(oloc));
18572e59ffd1SIlya Dryomov 	ceph_encode_64(p, oloc->pool);
18582e59ffd1SIlya Dryomov 	ceph_encode_32(p, -1); /* preferred */
18592e59ffd1SIlya Dryomov 	ceph_encode_32(p, 0);  /* key len */
18602e59ffd1SIlya Dryomov 	if (oloc->pool_ns)
18612e59ffd1SIlya Dryomov 		ceph_encode_string(p, end, oloc->pool_ns->str,
18622e59ffd1SIlya Dryomov 				   oloc->pool_ns->len);
18632e59ffd1SIlya Dryomov 	else
18642e59ffd1SIlya Dryomov 		ceph_encode_32(p, 0);
18652e59ffd1SIlya Dryomov }
18662e59ffd1SIlya Dryomov 
18678cb441c0SIlya Dryomov static void encode_request_partial(struct ceph_osd_request *req,
18688cb441c0SIlya Dryomov 				   struct ceph_msg *msg)
1869bb873b53SIlya Dryomov {
1870bb873b53SIlya Dryomov 	void *p = msg->front.iov_base;
1871bb873b53SIlya Dryomov 	void *const end = p + msg->front_alloc_len;
1872bb873b53SIlya Dryomov 	u32 data_len = 0;
1873bb873b53SIlya Dryomov 	int i;
1874bb873b53SIlya Dryomov 
1875bb873b53SIlya Dryomov 	if (req->r_flags & CEPH_OSD_FLAG_WRITE) {
1876bb873b53SIlya Dryomov 		/* snapshots aren't writeable */
1877bb873b53SIlya Dryomov 		WARN_ON(req->r_snapid != CEPH_NOSNAP);
1878bb873b53SIlya Dryomov 	} else {
1879bb873b53SIlya Dryomov 		WARN_ON(req->r_mtime.tv_sec || req->r_mtime.tv_nsec ||
1880bb873b53SIlya Dryomov 			req->r_data_offset || req->r_snapc);
1881bb873b53SIlya Dryomov 	}
1882bb873b53SIlya Dryomov 
1883bb873b53SIlya Dryomov 	setup_request_data(req, msg);
1884bb873b53SIlya Dryomov 
18858cb441c0SIlya Dryomov 	encode_spgid(&p, &req->r_t.spgid); /* actual spg */
18868cb441c0SIlya Dryomov 	ceph_encode_32(&p, req->r_t.pgid.seed); /* raw hash */
1887bb873b53SIlya Dryomov 	ceph_encode_32(&p, req->r_osdc->osdmap->epoch);
1888bb873b53SIlya Dryomov 	ceph_encode_32(&p, req->r_flags);
18898cb441c0SIlya Dryomov 
18908cb441c0SIlya Dryomov 	/* reqid */
18918cb441c0SIlya Dryomov 	ceph_start_encoding(&p, 2, 2, sizeof(struct ceph_osd_reqid));
18928cb441c0SIlya Dryomov 	memset(p, 0, sizeof(struct ceph_osd_reqid));
18938cb441c0SIlya Dryomov 	p += sizeof(struct ceph_osd_reqid);
18948cb441c0SIlya Dryomov 
18958cb441c0SIlya Dryomov 	/* trace */
18968cb441c0SIlya Dryomov 	memset(p, 0, sizeof(struct ceph_blkin_trace_info));
18978cb441c0SIlya Dryomov 	p += sizeof(struct ceph_blkin_trace_info);
18988cb441c0SIlya Dryomov 
18998cb441c0SIlya Dryomov 	ceph_encode_32(&p, 0); /* client_inc, always 0 */
1900bb873b53SIlya Dryomov 	ceph_encode_timespec(p, &req->r_mtime);
1901bb873b53SIlya Dryomov 	p += sizeof(struct ceph_timespec);
1902aa26d662SJeff Layton 
19032e59ffd1SIlya Dryomov 	encode_oloc(&p, end, &req->r_t.target_oloc);
19042e59ffd1SIlya Dryomov 	ceph_encode_string(&p, end, req->r_t.target_oid.name,
19052e59ffd1SIlya Dryomov 			   req->r_t.target_oid.name_len);
1906bb873b53SIlya Dryomov 
1907bb873b53SIlya Dryomov 	/* ops, can imply data */
1908bb873b53SIlya Dryomov 	ceph_encode_16(&p, req->r_num_ops);
1909bb873b53SIlya Dryomov 	for (i = 0; i < req->r_num_ops; i++) {
1910bb873b53SIlya Dryomov 		data_len += osd_req_encode_op(p, &req->r_ops[i]);
1911bb873b53SIlya Dryomov 		p += sizeof(struct ceph_osd_op);
1912bb873b53SIlya Dryomov 	}
1913bb873b53SIlya Dryomov 
1914bb873b53SIlya Dryomov 	ceph_encode_64(&p, req->r_snapid); /* snapid */
1915bb873b53SIlya Dryomov 	if (req->r_snapc) {
1916bb873b53SIlya Dryomov 		ceph_encode_64(&p, req->r_snapc->seq);
1917bb873b53SIlya Dryomov 		ceph_encode_32(&p, req->r_snapc->num_snaps);
1918bb873b53SIlya Dryomov 		for (i = 0; i < req->r_snapc->num_snaps; i++)
1919bb873b53SIlya Dryomov 			ceph_encode_64(&p, req->r_snapc->snaps[i]);
1920bb873b53SIlya Dryomov 	} else {
1921bb873b53SIlya Dryomov 		ceph_encode_64(&p, 0); /* snap_seq */
1922bb873b53SIlya Dryomov 		ceph_encode_32(&p, 0); /* snaps len */
1923bb873b53SIlya Dryomov 	}
1924bb873b53SIlya Dryomov 
1925bb873b53SIlya Dryomov 	ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
1926986e8989SIlya Dryomov 	BUG_ON(p > end - 8); /* space for features */
1927bb873b53SIlya Dryomov 
19288cb441c0SIlya Dryomov 	msg->hdr.version = cpu_to_le16(8); /* MOSDOp v8 */
19298cb441c0SIlya Dryomov 	/* front_len is finalized in encode_request_finish() */
1930986e8989SIlya Dryomov 	msg->front.iov_len = p - msg->front.iov_base;
1931986e8989SIlya Dryomov 	msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1932bb873b53SIlya Dryomov 	msg->hdr.data_len = cpu_to_le32(data_len);
1933bb873b53SIlya Dryomov 	/*
1934bb873b53SIlya Dryomov 	 * The header "data_off" is a hint to the receiver allowing it
1935bb873b53SIlya Dryomov 	 * to align received data into its buffers such that there's no
1936bb873b53SIlya Dryomov 	 * need to re-copy it before writing it to disk (direct I/O).
1937bb873b53SIlya Dryomov 	 */
1938bb873b53SIlya Dryomov 	msg->hdr.data_off = cpu_to_le16(req->r_data_offset);
1939bb873b53SIlya Dryomov 
19408cb441c0SIlya Dryomov 	dout("%s req %p msg %p oid %s oid_len %d\n", __func__, req, msg,
19418cb441c0SIlya Dryomov 	     req->r_t.target_oid.name, req->r_t.target_oid.name_len);
19428cb441c0SIlya Dryomov }
19438cb441c0SIlya Dryomov 
19448cb441c0SIlya Dryomov static void encode_request_finish(struct ceph_msg *msg)
19458cb441c0SIlya Dryomov {
19468cb441c0SIlya Dryomov 	void *p = msg->front.iov_base;
1947986e8989SIlya Dryomov 	void *const partial_end = p + msg->front.iov_len;
19488cb441c0SIlya Dryomov 	void *const end = p + msg->front_alloc_len;
19498cb441c0SIlya Dryomov 
19508cb441c0SIlya Dryomov 	if (CEPH_HAVE_FEATURE(msg->con->peer_features, RESEND_ON_SPLIT)) {
19518cb441c0SIlya Dryomov 		/* luminous OSD -- encode features and be done */
1952986e8989SIlya Dryomov 		p = partial_end;
19538cb441c0SIlya Dryomov 		ceph_encode_64(&p, msg->con->peer_features);
19548cb441c0SIlya Dryomov 	} else {
19558cb441c0SIlya Dryomov 		struct {
19568cb441c0SIlya Dryomov 			char spgid[CEPH_ENCODING_START_BLK_LEN +
19578cb441c0SIlya Dryomov 				   CEPH_PGID_ENCODING_LEN + 1];
19588cb441c0SIlya Dryomov 			__le32 hash;
19598cb441c0SIlya Dryomov 			__le32 epoch;
19608cb441c0SIlya Dryomov 			__le32 flags;
19618cb441c0SIlya Dryomov 			char reqid[CEPH_ENCODING_START_BLK_LEN +
19628cb441c0SIlya Dryomov 				   sizeof(struct ceph_osd_reqid)];
19638cb441c0SIlya Dryomov 			char trace[sizeof(struct ceph_blkin_trace_info)];
19648cb441c0SIlya Dryomov 			__le32 client_inc;
19658cb441c0SIlya Dryomov 			struct ceph_timespec mtime;
19668cb441c0SIlya Dryomov 		} __packed head;
19678cb441c0SIlya Dryomov 		struct ceph_pg pgid;
19688cb441c0SIlya Dryomov 		void *oloc, *oid, *tail;
19698cb441c0SIlya Dryomov 		int oloc_len, oid_len, tail_len;
19708cb441c0SIlya Dryomov 		int len;
19718cb441c0SIlya Dryomov 
19728cb441c0SIlya Dryomov 		/*
19738cb441c0SIlya Dryomov 		 * Pre-luminous OSD -- reencode v8 into v4 using @head
19748cb441c0SIlya Dryomov 		 * as a temporary buffer.  Encode the raw PG; the rest
19758cb441c0SIlya Dryomov 		 * is just a matter of moving oloc, oid and tail blobs
19768cb441c0SIlya Dryomov 		 * around.
19778cb441c0SIlya Dryomov 		 */
19788cb441c0SIlya Dryomov 		memcpy(&head, p, sizeof(head));
19798cb441c0SIlya Dryomov 		p += sizeof(head);
19808cb441c0SIlya Dryomov 
19818cb441c0SIlya Dryomov 		oloc = p;
19828cb441c0SIlya Dryomov 		p += CEPH_ENCODING_START_BLK_LEN;
19838cb441c0SIlya Dryomov 		pgid.pool = ceph_decode_64(&p);
19848cb441c0SIlya Dryomov 		p += 4 + 4; /* preferred, key len */
19858cb441c0SIlya Dryomov 		len = ceph_decode_32(&p);
19868cb441c0SIlya Dryomov 		p += len;   /* nspace */
19878cb441c0SIlya Dryomov 		oloc_len = p - oloc;
19888cb441c0SIlya Dryomov 
19898cb441c0SIlya Dryomov 		oid = p;
19908cb441c0SIlya Dryomov 		len = ceph_decode_32(&p);
19918cb441c0SIlya Dryomov 		p += len;
19928cb441c0SIlya Dryomov 		oid_len = p - oid;
19938cb441c0SIlya Dryomov 
19948cb441c0SIlya Dryomov 		tail = p;
1995986e8989SIlya Dryomov 		tail_len = partial_end - p;
19968cb441c0SIlya Dryomov 
19978cb441c0SIlya Dryomov 		p = msg->front.iov_base;
19988cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.client_inc, sizeof(head.client_inc));
19998cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.epoch, sizeof(head.epoch));
20008cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.flags, sizeof(head.flags));
20018cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.mtime, sizeof(head.mtime));
20028cb441c0SIlya Dryomov 
20038cb441c0SIlya Dryomov 		/* reassert_version */
20048cb441c0SIlya Dryomov 		memset(p, 0, sizeof(struct ceph_eversion));
20058cb441c0SIlya Dryomov 		p += sizeof(struct ceph_eversion);
20068cb441c0SIlya Dryomov 
20078cb441c0SIlya Dryomov 		BUG_ON(p >= oloc);
20088cb441c0SIlya Dryomov 		memmove(p, oloc, oloc_len);
20098cb441c0SIlya Dryomov 		p += oloc_len;
20108cb441c0SIlya Dryomov 
20118cb441c0SIlya Dryomov 		pgid.seed = le32_to_cpu(head.hash);
20128cb441c0SIlya Dryomov 		encode_pgid(&p, &pgid); /* raw pg */
20138cb441c0SIlya Dryomov 
20148cb441c0SIlya Dryomov 		BUG_ON(p >= oid);
20158cb441c0SIlya Dryomov 		memmove(p, oid, oid_len);
20168cb441c0SIlya Dryomov 		p += oid_len;
20178cb441c0SIlya Dryomov 
20188cb441c0SIlya Dryomov 		/* tail -- ops, snapid, snapc, retry_attempt */
20198cb441c0SIlya Dryomov 		BUG_ON(p >= tail);
20208cb441c0SIlya Dryomov 		memmove(p, tail, tail_len);
20218cb441c0SIlya Dryomov 		p += tail_len;
20228cb441c0SIlya Dryomov 
20238cb441c0SIlya Dryomov 		msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
20248cb441c0SIlya Dryomov 	}
20258cb441c0SIlya Dryomov 
20268cb441c0SIlya Dryomov 	BUG_ON(p > end);
20278cb441c0SIlya Dryomov 	msg->front.iov_len = p - msg->front.iov_base;
20288cb441c0SIlya Dryomov 	msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
20298cb441c0SIlya Dryomov 
20308cb441c0SIlya Dryomov 	dout("%s msg %p tid %llu %u+%u+%u v%d\n", __func__, msg,
20318cb441c0SIlya Dryomov 	     le64_to_cpu(msg->hdr.tid), le32_to_cpu(msg->hdr.front_len),
20328cb441c0SIlya Dryomov 	     le32_to_cpu(msg->hdr.middle_len), le32_to_cpu(msg->hdr.data_len),
20338cb441c0SIlya Dryomov 	     le16_to_cpu(msg->hdr.version));
2034bb873b53SIlya Dryomov }
2035bb873b53SIlya Dryomov 
2036bb873b53SIlya Dryomov /*
2037bb873b53SIlya Dryomov  * @req has to be assigned a tid and registered.
2038bb873b53SIlya Dryomov  */
2039bb873b53SIlya Dryomov static void send_request(struct ceph_osd_request *req)
2040bb873b53SIlya Dryomov {
2041bb873b53SIlya Dryomov 	struct ceph_osd *osd = req->r_osd;
2042bb873b53SIlya Dryomov 
20435aea3dcdSIlya Dryomov 	verify_osd_locked(osd);
2044bb873b53SIlya Dryomov 	WARN_ON(osd->o_osd != req->r_t.osd);
2045bb873b53SIlya Dryomov 
2046a02a946dSIlya Dryomov 	/* backoff? */
2047a02a946dSIlya Dryomov 	if (should_plug_request(req))
2048a02a946dSIlya Dryomov 		return;
2049a02a946dSIlya Dryomov 
20505aea3dcdSIlya Dryomov 	/*
20515aea3dcdSIlya Dryomov 	 * We may have a previously queued request message hanging
20525aea3dcdSIlya Dryomov 	 * around.  Cancel it to avoid corrupting the msgr.
20535aea3dcdSIlya Dryomov 	 */
20545aea3dcdSIlya Dryomov 	if (req->r_sent)
20555aea3dcdSIlya Dryomov 		ceph_msg_revoke(req->r_request);
20565aea3dcdSIlya Dryomov 
2057bb873b53SIlya Dryomov 	req->r_flags |= CEPH_OSD_FLAG_KNOWN_REDIR;
2058bb873b53SIlya Dryomov 	if (req->r_attempts)
2059bb873b53SIlya Dryomov 		req->r_flags |= CEPH_OSD_FLAG_RETRY;
2060bb873b53SIlya Dryomov 	else
2061bb873b53SIlya Dryomov 		WARN_ON(req->r_flags & CEPH_OSD_FLAG_RETRY);
2062bb873b53SIlya Dryomov 
20638cb441c0SIlya Dryomov 	encode_request_partial(req, req->r_request);
2064bb873b53SIlya Dryomov 
206504c7d789SIlya 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",
2066bb873b53SIlya Dryomov 	     __func__, req, req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed,
2067dc98ff72SIlya Dryomov 	     req->r_t.spgid.pgid.pool, req->r_t.spgid.pgid.seed,
206804c7d789SIlya Dryomov 	     req->r_t.spgid.shard, osd->o_osd, req->r_t.epoch, req->r_flags,
206904c7d789SIlya Dryomov 	     req->r_attempts);
2070bb873b53SIlya Dryomov 
2071bb873b53SIlya Dryomov 	req->r_t.paused = false;
20723d14c5d2SYehuda Sadeh 	req->r_stamp = jiffies;
2073bb873b53SIlya Dryomov 	req->r_attempts++;
20743d14c5d2SYehuda Sadeh 
2075bb873b53SIlya Dryomov 	req->r_sent = osd->o_incarnation;
2076bb873b53SIlya Dryomov 	req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
2077bb873b53SIlya Dryomov 	ceph_con_send(&osd->o_con, ceph_msg_get(req->r_request));
20783d14c5d2SYehuda Sadeh }
20793d14c5d2SYehuda Sadeh 
208042c1b124SIlya Dryomov static void maybe_request_map(struct ceph_osd_client *osdc)
208142c1b124SIlya Dryomov {
208242c1b124SIlya Dryomov 	bool continuous = false;
208342c1b124SIlya Dryomov 
20845aea3dcdSIlya Dryomov 	verify_osdc_locked(osdc);
208542c1b124SIlya Dryomov 	WARN_ON(!osdc->osdmap->epoch);
208642c1b124SIlya Dryomov 
2087b7ec35b3SIlya Dryomov 	if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2088b7ec35b3SIlya Dryomov 	    ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD) ||
2089b7ec35b3SIlya Dryomov 	    ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
209042c1b124SIlya Dryomov 		dout("%s osdc %p continuous\n", __func__, osdc);
209142c1b124SIlya Dryomov 		continuous = true;
209242c1b124SIlya Dryomov 	} else {
209342c1b124SIlya Dryomov 		dout("%s osdc %p onetime\n", __func__, osdc);
209442c1b124SIlya Dryomov 	}
209542c1b124SIlya Dryomov 
209642c1b124SIlya Dryomov 	if (ceph_monc_want_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
209742c1b124SIlya Dryomov 			       osdc->osdmap->epoch + 1, continuous))
209842c1b124SIlya Dryomov 		ceph_monc_renew_subs(&osdc->client->monc);
209942c1b124SIlya Dryomov }
210042c1b124SIlya Dryomov 
2101a1f4020aSJeff Layton static void complete_request(struct ceph_osd_request *req, int err);
21024609245eSIlya Dryomov static void send_map_check(struct ceph_osd_request *req);
21034609245eSIlya Dryomov 
21045aea3dcdSIlya Dryomov static void __submit_request(struct ceph_osd_request *req, bool wrlocked)
21050bbfdfe8SIlya Dryomov {
21065aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
21075aea3dcdSIlya Dryomov 	struct ceph_osd *osd;
21084609245eSIlya Dryomov 	enum calc_target_result ct_res;
21095aea3dcdSIlya Dryomov 	bool need_send = false;
21105aea3dcdSIlya Dryomov 	bool promoted = false;
2111a1f4020aSJeff Layton 	bool need_abort = false;
21120bbfdfe8SIlya Dryomov 
2113b18b9550SIlya Dryomov 	WARN_ON(req->r_tid);
21145aea3dcdSIlya Dryomov 	dout("%s req %p wrlocked %d\n", __func__, req, wrlocked);
21155aea3dcdSIlya Dryomov 
21165aea3dcdSIlya Dryomov again:
21177de030d6SIlya Dryomov 	ct_res = calc_target(osdc, &req->r_t, NULL, false);
21184609245eSIlya Dryomov 	if (ct_res == CALC_TARGET_POOL_DNE && !wrlocked)
21194609245eSIlya Dryomov 		goto promote;
21204609245eSIlya Dryomov 
21215aea3dcdSIlya Dryomov 	osd = lookup_create_osd(osdc, req->r_t.osd, wrlocked);
21225aea3dcdSIlya Dryomov 	if (IS_ERR(osd)) {
21235aea3dcdSIlya Dryomov 		WARN_ON(PTR_ERR(osd) != -EAGAIN || wrlocked);
21245aea3dcdSIlya Dryomov 		goto promote;
21255aea3dcdSIlya Dryomov 	}
21265aea3dcdSIlya Dryomov 
212758eb7932SJeff Layton 	if (osdc->osdmap->epoch < osdc->epoch_barrier) {
212858eb7932SJeff Layton 		dout("req %p epoch %u barrier %u\n", req, osdc->osdmap->epoch,
212958eb7932SJeff Layton 		     osdc->epoch_barrier);
213058eb7932SJeff Layton 		req->r_t.paused = true;
213158eb7932SJeff Layton 		maybe_request_map(osdc);
213258eb7932SJeff Layton 	} else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2133b7ec35b3SIlya Dryomov 		   ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
21345aea3dcdSIlya Dryomov 		dout("req %p pausewr\n", req);
21355aea3dcdSIlya Dryomov 		req->r_t.paused = true;
21365aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
21375aea3dcdSIlya Dryomov 	} else if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
2138b7ec35b3SIlya Dryomov 		   ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
21395aea3dcdSIlya Dryomov 		dout("req %p pauserd\n", req);
21405aea3dcdSIlya Dryomov 		req->r_t.paused = true;
21415aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
21425aea3dcdSIlya Dryomov 	} else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
21435aea3dcdSIlya Dryomov 		   !(req->r_flags & (CEPH_OSD_FLAG_FULL_TRY |
21445aea3dcdSIlya Dryomov 				     CEPH_OSD_FLAG_FULL_FORCE)) &&
2145b7ec35b3SIlya Dryomov 		   (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
21465aea3dcdSIlya Dryomov 		    pool_full(osdc, req->r_t.base_oloc.pool))) {
21475aea3dcdSIlya Dryomov 		dout("req %p full/pool_full\n", req);
21485aea3dcdSIlya Dryomov 		pr_warn_ratelimited("FULL or reached pool quota\n");
21495aea3dcdSIlya Dryomov 		req->r_t.paused = true;
21505aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
2151a1f4020aSJeff Layton 		if (req->r_abort_on_full)
2152a1f4020aSJeff Layton 			need_abort = true;
21535aea3dcdSIlya Dryomov 	} else if (!osd_homeless(osd)) {
21545aea3dcdSIlya Dryomov 		need_send = true;
21550bbfdfe8SIlya Dryomov 	} else {
21565aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
21570bbfdfe8SIlya Dryomov 	}
21580bbfdfe8SIlya Dryomov 
21595aea3dcdSIlya Dryomov 	mutex_lock(&osd->lock);
21605aea3dcdSIlya Dryomov 	/*
21615aea3dcdSIlya Dryomov 	 * Assign the tid atomically with send_request() to protect
21625aea3dcdSIlya Dryomov 	 * multiple writes to the same object from racing with each
21635aea3dcdSIlya Dryomov 	 * other, resulting in out of order ops on the OSDs.
21645aea3dcdSIlya Dryomov 	 */
21655aea3dcdSIlya Dryomov 	req->r_tid = atomic64_inc_return(&osdc->last_tid);
21665aea3dcdSIlya Dryomov 	link_request(osd, req);
21675aea3dcdSIlya Dryomov 	if (need_send)
21685aea3dcdSIlya Dryomov 		send_request(req);
2169a1f4020aSJeff Layton 	else if (need_abort)
2170a1f4020aSJeff Layton 		complete_request(req, -ENOSPC);
21715aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
21725aea3dcdSIlya Dryomov 
21734609245eSIlya Dryomov 	if (ct_res == CALC_TARGET_POOL_DNE)
21744609245eSIlya Dryomov 		send_map_check(req);
21754609245eSIlya Dryomov 
21765aea3dcdSIlya Dryomov 	if (promoted)
21775aea3dcdSIlya Dryomov 		downgrade_write(&osdc->lock);
21785aea3dcdSIlya Dryomov 	return;
21795aea3dcdSIlya Dryomov 
21805aea3dcdSIlya Dryomov promote:
21815aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
21825aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
21835aea3dcdSIlya Dryomov 	wrlocked = true;
21845aea3dcdSIlya Dryomov 	promoted = true;
21855aea3dcdSIlya Dryomov 	goto again;
21860bbfdfe8SIlya Dryomov }
21870bbfdfe8SIlya Dryomov 
21885aea3dcdSIlya Dryomov static void account_request(struct ceph_osd_request *req)
21895aea3dcdSIlya Dryomov {
219054ea0046SIlya Dryomov 	WARN_ON(req->r_flags & (CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK));
2191b18b9550SIlya Dryomov 	WARN_ON(!(req->r_flags & (CEPH_OSD_FLAG_READ | CEPH_OSD_FLAG_WRITE)));
21925aea3dcdSIlya Dryomov 
2193b18b9550SIlya Dryomov 	req->r_flags |= CEPH_OSD_FLAG_ONDISK;
21945aea3dcdSIlya Dryomov 	atomic_inc(&req->r_osdc->num_requests);
21957cc5e38fSIlya Dryomov 
21967cc5e38fSIlya Dryomov 	req->r_start_stamp = jiffies;
21975aea3dcdSIlya Dryomov }
21985aea3dcdSIlya Dryomov 
21995aea3dcdSIlya Dryomov static void submit_request(struct ceph_osd_request *req, bool wrlocked)
22005aea3dcdSIlya Dryomov {
22015aea3dcdSIlya Dryomov 	ceph_osdc_get_request(req);
22025aea3dcdSIlya Dryomov 	account_request(req);
22035aea3dcdSIlya Dryomov 	__submit_request(req, wrlocked);
22045aea3dcdSIlya Dryomov }
22055aea3dcdSIlya Dryomov 
220645ee2c1dSIlya Dryomov static void finish_request(struct ceph_osd_request *req)
22075aea3dcdSIlya Dryomov {
22085aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
22095aea3dcdSIlya Dryomov 
22104609245eSIlya Dryomov 	WARN_ON(lookup_request_mc(&osdc->map_checks, req->r_tid));
221104c7d789SIlya Dryomov 	dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
221204c7d789SIlya Dryomov 
221304c7d789SIlya Dryomov 	if (req->r_osd)
221404c7d789SIlya Dryomov 		unlink_request(req->r_osd, req);
22155aea3dcdSIlya Dryomov 	atomic_dec(&osdc->num_requests);
22165aea3dcdSIlya Dryomov 
22175aea3dcdSIlya Dryomov 	/*
22185aea3dcdSIlya Dryomov 	 * If an OSD has failed or returned and a request has been sent
22195aea3dcdSIlya Dryomov 	 * twice, it's possible to get a reply and end up here while the
22205aea3dcdSIlya Dryomov 	 * request message is queued for delivery.  We will ignore the
22215aea3dcdSIlya Dryomov 	 * reply, so not a big deal, but better to try and catch it.
22225aea3dcdSIlya Dryomov 	 */
22235aea3dcdSIlya Dryomov 	ceph_msg_revoke(req->r_request);
22245aea3dcdSIlya Dryomov 	ceph_msg_revoke_incoming(req->r_reply);
22255aea3dcdSIlya Dryomov }
22265aea3dcdSIlya Dryomov 
2227fe5da05eSIlya Dryomov static void __complete_request(struct ceph_osd_request *req)
2228fe5da05eSIlya Dryomov {
2229b18b9550SIlya Dryomov 	if (req->r_callback) {
2230b18b9550SIlya Dryomov 		dout("%s req %p tid %llu cb %pf result %d\n", __func__, req,
2231b18b9550SIlya Dryomov 		     req->r_tid, req->r_callback, req->r_result);
2232fe5da05eSIlya Dryomov 		req->r_callback(req);
2233b18b9550SIlya Dryomov 	}
2234fe5da05eSIlya Dryomov }
2235fe5da05eSIlya Dryomov 
22364609245eSIlya Dryomov /*
2237b18b9550SIlya Dryomov  * This is open-coded in handle_reply().
22384609245eSIlya Dryomov  */
22394609245eSIlya Dryomov static void complete_request(struct ceph_osd_request *req, int err)
22404609245eSIlya Dryomov {
22414609245eSIlya Dryomov 	dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
22424609245eSIlya Dryomov 
22434609245eSIlya Dryomov 	req->r_result = err;
224445ee2c1dSIlya Dryomov 	finish_request(req);
22454609245eSIlya Dryomov 	__complete_request(req);
2246b18b9550SIlya Dryomov 	complete_all(&req->r_completion);
22474609245eSIlya Dryomov 	ceph_osdc_put_request(req);
22484609245eSIlya Dryomov }
22494609245eSIlya Dryomov 
22504609245eSIlya Dryomov static void cancel_map_check(struct ceph_osd_request *req)
22514609245eSIlya Dryomov {
22524609245eSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
22534609245eSIlya Dryomov 	struct ceph_osd_request *lookup_req;
22544609245eSIlya Dryomov 
22554609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
22564609245eSIlya Dryomov 
22574609245eSIlya Dryomov 	lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
22584609245eSIlya Dryomov 	if (!lookup_req)
22594609245eSIlya Dryomov 		return;
22604609245eSIlya Dryomov 
22614609245eSIlya Dryomov 	WARN_ON(lookup_req != req);
22624609245eSIlya Dryomov 	erase_request_mc(&osdc->map_checks, req);
22634609245eSIlya Dryomov 	ceph_osdc_put_request(req);
22644609245eSIlya Dryomov }
22654609245eSIlya Dryomov 
22665aea3dcdSIlya Dryomov static void cancel_request(struct ceph_osd_request *req)
22675aea3dcdSIlya Dryomov {
22685aea3dcdSIlya Dryomov 	dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
22695aea3dcdSIlya Dryomov 
22704609245eSIlya Dryomov 	cancel_map_check(req);
227145ee2c1dSIlya Dryomov 	finish_request(req);
2272b18b9550SIlya Dryomov 	complete_all(&req->r_completion);
2273c297eb42SIlya Dryomov 	ceph_osdc_put_request(req);
22745aea3dcdSIlya Dryomov }
22755aea3dcdSIlya Dryomov 
22767cc5e38fSIlya Dryomov static void abort_request(struct ceph_osd_request *req, int err)
22777cc5e38fSIlya Dryomov {
22787cc5e38fSIlya Dryomov 	dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
22797cc5e38fSIlya Dryomov 
22807cc5e38fSIlya Dryomov 	cancel_map_check(req);
22817cc5e38fSIlya Dryomov 	complete_request(req, err);
22827cc5e38fSIlya Dryomov }
22837cc5e38fSIlya Dryomov 
228458eb7932SJeff Layton static void update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
228558eb7932SJeff Layton {
228658eb7932SJeff Layton 	if (likely(eb > osdc->epoch_barrier)) {
228758eb7932SJeff Layton 		dout("updating epoch_barrier from %u to %u\n",
228858eb7932SJeff Layton 				osdc->epoch_barrier, eb);
228958eb7932SJeff Layton 		osdc->epoch_barrier = eb;
229058eb7932SJeff Layton 		/* Request map if we're not to the barrier yet */
229158eb7932SJeff Layton 		if (eb > osdc->osdmap->epoch)
229258eb7932SJeff Layton 			maybe_request_map(osdc);
229358eb7932SJeff Layton 	}
229458eb7932SJeff Layton }
229558eb7932SJeff Layton 
229658eb7932SJeff Layton void ceph_osdc_update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
229758eb7932SJeff Layton {
229858eb7932SJeff Layton 	down_read(&osdc->lock);
229958eb7932SJeff Layton 	if (unlikely(eb > osdc->epoch_barrier)) {
230058eb7932SJeff Layton 		up_read(&osdc->lock);
230158eb7932SJeff Layton 		down_write(&osdc->lock);
230258eb7932SJeff Layton 		update_epoch_barrier(osdc, eb);
230358eb7932SJeff Layton 		up_write(&osdc->lock);
230458eb7932SJeff Layton 	} else {
230558eb7932SJeff Layton 		up_read(&osdc->lock);
230658eb7932SJeff Layton 	}
230758eb7932SJeff Layton }
230858eb7932SJeff Layton EXPORT_SYMBOL(ceph_osdc_update_epoch_barrier);
230958eb7932SJeff Layton 
2310fc36d0a4SJeff Layton /*
2311fc36d0a4SJeff Layton  * Drop all pending requests that are stalled waiting on a full condition to
231258eb7932SJeff Layton  * clear, and complete them with ENOSPC as the return code. Set the
231358eb7932SJeff Layton  * osdc->epoch_barrier to the latest map epoch that we've seen if any were
231458eb7932SJeff Layton  * cancelled.
2315fc36d0a4SJeff Layton  */
2316fc36d0a4SJeff Layton static void ceph_osdc_abort_on_full(struct ceph_osd_client *osdc)
2317fc36d0a4SJeff Layton {
2318fc36d0a4SJeff Layton 	struct rb_node *n;
231958eb7932SJeff Layton 	bool victims = false;
2320fc36d0a4SJeff Layton 
2321fc36d0a4SJeff Layton 	dout("enter abort_on_full\n");
2322fc36d0a4SJeff Layton 
2323fc36d0a4SJeff Layton 	if (!ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) && !have_pool_full(osdc))
2324fc36d0a4SJeff Layton 		goto out;
2325fc36d0a4SJeff Layton 
232658eb7932SJeff Layton 	/* Scan list and see if there is anything to abort */
232758eb7932SJeff Layton 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
232858eb7932SJeff Layton 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
232958eb7932SJeff Layton 		struct rb_node *m;
233058eb7932SJeff Layton 
233158eb7932SJeff Layton 		m = rb_first(&osd->o_requests);
233258eb7932SJeff Layton 		while (m) {
233358eb7932SJeff Layton 			struct ceph_osd_request *req = rb_entry(m,
233458eb7932SJeff Layton 					struct ceph_osd_request, r_node);
233558eb7932SJeff Layton 			m = rb_next(m);
233658eb7932SJeff Layton 
233758eb7932SJeff Layton 			if (req->r_abort_on_full) {
233858eb7932SJeff Layton 				victims = true;
233958eb7932SJeff Layton 				break;
234058eb7932SJeff Layton 			}
234158eb7932SJeff Layton 		}
234258eb7932SJeff Layton 		if (victims)
234358eb7932SJeff Layton 			break;
234458eb7932SJeff Layton 	}
234558eb7932SJeff Layton 
234658eb7932SJeff Layton 	if (!victims)
234758eb7932SJeff Layton 		goto out;
234858eb7932SJeff Layton 
234958eb7932SJeff Layton 	/*
235058eb7932SJeff Layton 	 * Update the barrier to current epoch if it's behind that point,
235158eb7932SJeff Layton 	 * since we know we have some calls to be aborted in the tree.
235258eb7932SJeff Layton 	 */
235358eb7932SJeff Layton 	update_epoch_barrier(osdc, osdc->osdmap->epoch);
235458eb7932SJeff Layton 
2355fc36d0a4SJeff Layton 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
2356fc36d0a4SJeff Layton 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
2357fc36d0a4SJeff Layton 		struct rb_node *m;
2358fc36d0a4SJeff Layton 
2359fc36d0a4SJeff Layton 		m = rb_first(&osd->o_requests);
2360fc36d0a4SJeff Layton 		while (m) {
2361fc36d0a4SJeff Layton 			struct ceph_osd_request *req = rb_entry(m,
2362fc36d0a4SJeff Layton 					struct ceph_osd_request, r_node);
2363fc36d0a4SJeff Layton 			m = rb_next(m);
2364fc36d0a4SJeff Layton 
2365fc36d0a4SJeff Layton 			if (req->r_abort_on_full &&
2366fc36d0a4SJeff Layton 			    (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2367fc36d0a4SJeff Layton 			     pool_full(osdc, req->r_t.target_oloc.pool)))
2368fc36d0a4SJeff Layton 				abort_request(req, -ENOSPC);
2369fc36d0a4SJeff Layton 		}
2370fc36d0a4SJeff Layton 	}
2371fc36d0a4SJeff Layton out:
237258eb7932SJeff Layton 	dout("return abort_on_full barrier=%u\n", osdc->epoch_barrier);
2373fc36d0a4SJeff Layton }
2374fc36d0a4SJeff Layton 
23754609245eSIlya Dryomov static void check_pool_dne(struct ceph_osd_request *req)
23764609245eSIlya Dryomov {
23774609245eSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
23784609245eSIlya Dryomov 	struct ceph_osdmap *map = osdc->osdmap;
23794609245eSIlya Dryomov 
23804609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
23814609245eSIlya Dryomov 	WARN_ON(!map->epoch);
23824609245eSIlya Dryomov 
23834609245eSIlya Dryomov 	if (req->r_attempts) {
23844609245eSIlya Dryomov 		/*
23854609245eSIlya Dryomov 		 * We sent a request earlier, which means that
23864609245eSIlya Dryomov 		 * previously the pool existed, and now it does not
23874609245eSIlya Dryomov 		 * (i.e., it was deleted).
23884609245eSIlya Dryomov 		 */
23894609245eSIlya Dryomov 		req->r_map_dne_bound = map->epoch;
23904609245eSIlya Dryomov 		dout("%s req %p tid %llu pool disappeared\n", __func__, req,
23914609245eSIlya Dryomov 		     req->r_tid);
23924609245eSIlya Dryomov 	} else {
23934609245eSIlya Dryomov 		dout("%s req %p tid %llu map_dne_bound %u have %u\n", __func__,
23944609245eSIlya Dryomov 		     req, req->r_tid, req->r_map_dne_bound, map->epoch);
23954609245eSIlya Dryomov 	}
23964609245eSIlya Dryomov 
23974609245eSIlya Dryomov 	if (req->r_map_dne_bound) {
23984609245eSIlya Dryomov 		if (map->epoch >= req->r_map_dne_bound) {
23994609245eSIlya Dryomov 			/* we had a new enough map */
24004609245eSIlya Dryomov 			pr_info_ratelimited("tid %llu pool does not exist\n",
24014609245eSIlya Dryomov 					    req->r_tid);
24024609245eSIlya Dryomov 			complete_request(req, -ENOENT);
24034609245eSIlya Dryomov 		}
24044609245eSIlya Dryomov 	} else {
24054609245eSIlya Dryomov 		send_map_check(req);
24064609245eSIlya Dryomov 	}
24074609245eSIlya Dryomov }
24084609245eSIlya Dryomov 
24094609245eSIlya Dryomov static void map_check_cb(struct ceph_mon_generic_request *greq)
24104609245eSIlya Dryomov {
24114609245eSIlya Dryomov 	struct ceph_osd_client *osdc = &greq->monc->client->osdc;
24124609245eSIlya Dryomov 	struct ceph_osd_request *req;
24134609245eSIlya Dryomov 	u64 tid = greq->private_data;
24144609245eSIlya Dryomov 
24154609245eSIlya Dryomov 	WARN_ON(greq->result || !greq->u.newest);
24164609245eSIlya Dryomov 
24174609245eSIlya Dryomov 	down_write(&osdc->lock);
24184609245eSIlya Dryomov 	req = lookup_request_mc(&osdc->map_checks, tid);
24194609245eSIlya Dryomov 	if (!req) {
24204609245eSIlya Dryomov 		dout("%s tid %llu dne\n", __func__, tid);
24214609245eSIlya Dryomov 		goto out_unlock;
24224609245eSIlya Dryomov 	}
24234609245eSIlya Dryomov 
24244609245eSIlya Dryomov 	dout("%s req %p tid %llu map_dne_bound %u newest %llu\n", __func__,
24254609245eSIlya Dryomov 	     req, req->r_tid, req->r_map_dne_bound, greq->u.newest);
24264609245eSIlya Dryomov 	if (!req->r_map_dne_bound)
24274609245eSIlya Dryomov 		req->r_map_dne_bound = greq->u.newest;
24284609245eSIlya Dryomov 	erase_request_mc(&osdc->map_checks, req);
24294609245eSIlya Dryomov 	check_pool_dne(req);
24304609245eSIlya Dryomov 
24314609245eSIlya Dryomov 	ceph_osdc_put_request(req);
24324609245eSIlya Dryomov out_unlock:
24334609245eSIlya Dryomov 	up_write(&osdc->lock);
24344609245eSIlya Dryomov }
24354609245eSIlya Dryomov 
24364609245eSIlya Dryomov static void send_map_check(struct ceph_osd_request *req)
24374609245eSIlya Dryomov {
24384609245eSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
24394609245eSIlya Dryomov 	struct ceph_osd_request *lookup_req;
24404609245eSIlya Dryomov 	int ret;
24414609245eSIlya Dryomov 
24424609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
24434609245eSIlya Dryomov 
24444609245eSIlya Dryomov 	lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
24454609245eSIlya Dryomov 	if (lookup_req) {
24464609245eSIlya Dryomov 		WARN_ON(lookup_req != req);
24474609245eSIlya Dryomov 		return;
24484609245eSIlya Dryomov 	}
24494609245eSIlya Dryomov 
24504609245eSIlya Dryomov 	ceph_osdc_get_request(req);
24514609245eSIlya Dryomov 	insert_request_mc(&osdc->map_checks, req);
24524609245eSIlya Dryomov 	ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
24534609245eSIlya Dryomov 					  map_check_cb, req->r_tid);
24544609245eSIlya Dryomov 	WARN_ON(ret);
24554609245eSIlya Dryomov }
24564609245eSIlya Dryomov 
24570bbfdfe8SIlya Dryomov /*
2458922dab61SIlya Dryomov  * lingering requests, watch/notify v2 infrastructure
2459922dab61SIlya Dryomov  */
2460922dab61SIlya Dryomov static void linger_release(struct kref *kref)
2461922dab61SIlya Dryomov {
2462922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq =
2463922dab61SIlya Dryomov 	    container_of(kref, struct ceph_osd_linger_request, kref);
2464922dab61SIlya Dryomov 
2465922dab61SIlya Dryomov 	dout("%s lreq %p reg_req %p ping_req %p\n", __func__, lreq,
2466922dab61SIlya Dryomov 	     lreq->reg_req, lreq->ping_req);
2467922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&lreq->node));
2468922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&lreq->osdc_node));
24694609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&lreq->mc_node));
2470922dab61SIlya Dryomov 	WARN_ON(!list_empty(&lreq->scan_item));
2471b07d3c4bSIlya Dryomov 	WARN_ON(!list_empty(&lreq->pending_lworks));
2472922dab61SIlya Dryomov 	WARN_ON(lreq->osd);
2473922dab61SIlya Dryomov 
2474922dab61SIlya Dryomov 	if (lreq->reg_req)
2475922dab61SIlya Dryomov 		ceph_osdc_put_request(lreq->reg_req);
2476922dab61SIlya Dryomov 	if (lreq->ping_req)
2477922dab61SIlya Dryomov 		ceph_osdc_put_request(lreq->ping_req);
2478922dab61SIlya Dryomov 	target_destroy(&lreq->t);
2479922dab61SIlya Dryomov 	kfree(lreq);
2480922dab61SIlya Dryomov }
2481922dab61SIlya Dryomov 
2482922dab61SIlya Dryomov static void linger_put(struct ceph_osd_linger_request *lreq)
2483922dab61SIlya Dryomov {
2484922dab61SIlya Dryomov 	if (lreq)
2485922dab61SIlya Dryomov 		kref_put(&lreq->kref, linger_release);
2486922dab61SIlya Dryomov }
2487922dab61SIlya Dryomov 
2488922dab61SIlya Dryomov static struct ceph_osd_linger_request *
2489922dab61SIlya Dryomov linger_get(struct ceph_osd_linger_request *lreq)
2490922dab61SIlya Dryomov {
2491922dab61SIlya Dryomov 	kref_get(&lreq->kref);
2492922dab61SIlya Dryomov 	return lreq;
2493922dab61SIlya Dryomov }
2494922dab61SIlya Dryomov 
2495922dab61SIlya Dryomov static struct ceph_osd_linger_request *
2496922dab61SIlya Dryomov linger_alloc(struct ceph_osd_client *osdc)
2497922dab61SIlya Dryomov {
2498922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
2499922dab61SIlya Dryomov 
2500922dab61SIlya Dryomov 	lreq = kzalloc(sizeof(*lreq), GFP_NOIO);
2501922dab61SIlya Dryomov 	if (!lreq)
2502922dab61SIlya Dryomov 		return NULL;
2503922dab61SIlya Dryomov 
2504922dab61SIlya Dryomov 	kref_init(&lreq->kref);
2505922dab61SIlya Dryomov 	mutex_init(&lreq->lock);
2506922dab61SIlya Dryomov 	RB_CLEAR_NODE(&lreq->node);
2507922dab61SIlya Dryomov 	RB_CLEAR_NODE(&lreq->osdc_node);
25084609245eSIlya Dryomov 	RB_CLEAR_NODE(&lreq->mc_node);
2509922dab61SIlya Dryomov 	INIT_LIST_HEAD(&lreq->scan_item);
2510b07d3c4bSIlya Dryomov 	INIT_LIST_HEAD(&lreq->pending_lworks);
2511922dab61SIlya Dryomov 	init_completion(&lreq->reg_commit_wait);
251219079203SIlya Dryomov 	init_completion(&lreq->notify_finish_wait);
2513922dab61SIlya Dryomov 
2514922dab61SIlya Dryomov 	lreq->osdc = osdc;
2515922dab61SIlya Dryomov 	target_init(&lreq->t);
2516922dab61SIlya Dryomov 
2517922dab61SIlya Dryomov 	dout("%s lreq %p\n", __func__, lreq);
2518922dab61SIlya Dryomov 	return lreq;
2519922dab61SIlya Dryomov }
2520922dab61SIlya Dryomov 
2521922dab61SIlya Dryomov DEFINE_RB_INSDEL_FUNCS(linger, struct ceph_osd_linger_request, linger_id, node)
2522922dab61SIlya Dryomov DEFINE_RB_FUNCS(linger_osdc, struct ceph_osd_linger_request, linger_id, osdc_node)
25234609245eSIlya Dryomov DEFINE_RB_FUNCS(linger_mc, struct ceph_osd_linger_request, linger_id, mc_node)
2524922dab61SIlya Dryomov 
2525922dab61SIlya Dryomov /*
2526922dab61SIlya Dryomov  * Create linger request <-> OSD session relation.
2527922dab61SIlya Dryomov  *
2528922dab61SIlya Dryomov  * @lreq has to be registered, @osd may be homeless.
2529922dab61SIlya Dryomov  */
2530922dab61SIlya Dryomov static void link_linger(struct ceph_osd *osd,
2531922dab61SIlya Dryomov 			struct ceph_osd_linger_request *lreq)
2532922dab61SIlya Dryomov {
2533922dab61SIlya Dryomov 	verify_osd_locked(osd);
2534922dab61SIlya Dryomov 	WARN_ON(!lreq->linger_id || lreq->osd);
2535922dab61SIlya Dryomov 	dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2536922dab61SIlya Dryomov 	     osd->o_osd, lreq, lreq->linger_id);
2537922dab61SIlya Dryomov 
2538922dab61SIlya Dryomov 	if (!osd_homeless(osd))
2539922dab61SIlya Dryomov 		__remove_osd_from_lru(osd);
2540922dab61SIlya Dryomov 	else
2541922dab61SIlya Dryomov 		atomic_inc(&osd->o_osdc->num_homeless);
2542922dab61SIlya Dryomov 
2543922dab61SIlya Dryomov 	get_osd(osd);
2544922dab61SIlya Dryomov 	insert_linger(&osd->o_linger_requests, lreq);
2545922dab61SIlya Dryomov 	lreq->osd = osd;
2546922dab61SIlya Dryomov }
2547922dab61SIlya Dryomov 
2548922dab61SIlya Dryomov static void unlink_linger(struct ceph_osd *osd,
2549922dab61SIlya Dryomov 			  struct ceph_osd_linger_request *lreq)
2550922dab61SIlya Dryomov {
2551922dab61SIlya Dryomov 	verify_osd_locked(osd);
2552922dab61SIlya Dryomov 	WARN_ON(lreq->osd != osd);
2553922dab61SIlya Dryomov 	dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2554922dab61SIlya Dryomov 	     osd->o_osd, lreq, lreq->linger_id);
2555922dab61SIlya Dryomov 
2556922dab61SIlya Dryomov 	lreq->osd = NULL;
2557922dab61SIlya Dryomov 	erase_linger(&osd->o_linger_requests, lreq);
2558922dab61SIlya Dryomov 	put_osd(osd);
2559922dab61SIlya Dryomov 
2560922dab61SIlya Dryomov 	if (!osd_homeless(osd))
2561922dab61SIlya Dryomov 		maybe_move_osd_to_lru(osd);
2562922dab61SIlya Dryomov 	else
2563922dab61SIlya Dryomov 		atomic_dec(&osd->o_osdc->num_homeless);
2564922dab61SIlya Dryomov }
2565922dab61SIlya Dryomov 
2566922dab61SIlya Dryomov static bool __linger_registered(struct ceph_osd_linger_request *lreq)
2567922dab61SIlya Dryomov {
2568922dab61SIlya Dryomov 	verify_osdc_locked(lreq->osdc);
2569922dab61SIlya Dryomov 
2570922dab61SIlya Dryomov 	return !RB_EMPTY_NODE(&lreq->osdc_node);
2571922dab61SIlya Dryomov }
2572922dab61SIlya Dryomov 
2573922dab61SIlya Dryomov static bool linger_registered(struct ceph_osd_linger_request *lreq)
2574922dab61SIlya Dryomov {
2575922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2576922dab61SIlya Dryomov 	bool registered;
2577922dab61SIlya Dryomov 
2578922dab61SIlya Dryomov 	down_read(&osdc->lock);
2579922dab61SIlya Dryomov 	registered = __linger_registered(lreq);
2580922dab61SIlya Dryomov 	up_read(&osdc->lock);
2581922dab61SIlya Dryomov 
2582922dab61SIlya Dryomov 	return registered;
2583922dab61SIlya Dryomov }
2584922dab61SIlya Dryomov 
2585922dab61SIlya Dryomov static void linger_register(struct ceph_osd_linger_request *lreq)
2586922dab61SIlya Dryomov {
2587922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2588922dab61SIlya Dryomov 
2589922dab61SIlya Dryomov 	verify_osdc_wrlocked(osdc);
2590922dab61SIlya Dryomov 	WARN_ON(lreq->linger_id);
2591922dab61SIlya Dryomov 
2592922dab61SIlya Dryomov 	linger_get(lreq);
2593922dab61SIlya Dryomov 	lreq->linger_id = ++osdc->last_linger_id;
2594922dab61SIlya Dryomov 	insert_linger_osdc(&osdc->linger_requests, lreq);
2595922dab61SIlya Dryomov }
2596922dab61SIlya Dryomov 
2597922dab61SIlya Dryomov static void linger_unregister(struct ceph_osd_linger_request *lreq)
2598922dab61SIlya Dryomov {
2599922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2600922dab61SIlya Dryomov 
2601922dab61SIlya Dryomov 	verify_osdc_wrlocked(osdc);
2602922dab61SIlya Dryomov 
2603922dab61SIlya Dryomov 	erase_linger_osdc(&osdc->linger_requests, lreq);
2604922dab61SIlya Dryomov 	linger_put(lreq);
2605922dab61SIlya Dryomov }
2606922dab61SIlya Dryomov 
2607922dab61SIlya Dryomov static void cancel_linger_request(struct ceph_osd_request *req)
2608922dab61SIlya Dryomov {
2609922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
2610922dab61SIlya Dryomov 
2611922dab61SIlya Dryomov 	WARN_ON(!req->r_linger);
2612922dab61SIlya Dryomov 	cancel_request(req);
2613922dab61SIlya Dryomov 	linger_put(lreq);
2614922dab61SIlya Dryomov }
2615922dab61SIlya Dryomov 
2616922dab61SIlya Dryomov struct linger_work {
2617922dab61SIlya Dryomov 	struct work_struct work;
2618922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
2619b07d3c4bSIlya Dryomov 	struct list_head pending_item;
2620b07d3c4bSIlya Dryomov 	unsigned long queued_stamp;
2621922dab61SIlya Dryomov 
2622922dab61SIlya Dryomov 	union {
2623922dab61SIlya Dryomov 		struct {
2624922dab61SIlya Dryomov 			u64 notify_id;
2625922dab61SIlya Dryomov 			u64 notifier_id;
2626922dab61SIlya Dryomov 			void *payload; /* points into @msg front */
2627922dab61SIlya Dryomov 			size_t payload_len;
2628922dab61SIlya Dryomov 
2629922dab61SIlya Dryomov 			struct ceph_msg *msg; /* for ceph_msg_put() */
2630922dab61SIlya Dryomov 		} notify;
2631922dab61SIlya Dryomov 		struct {
2632922dab61SIlya Dryomov 			int err;
2633922dab61SIlya Dryomov 		} error;
2634922dab61SIlya Dryomov 	};
2635922dab61SIlya Dryomov };
2636922dab61SIlya Dryomov 
2637922dab61SIlya Dryomov static struct linger_work *lwork_alloc(struct ceph_osd_linger_request *lreq,
2638922dab61SIlya Dryomov 				       work_func_t workfn)
2639922dab61SIlya Dryomov {
2640922dab61SIlya Dryomov 	struct linger_work *lwork;
2641922dab61SIlya Dryomov 
2642922dab61SIlya Dryomov 	lwork = kzalloc(sizeof(*lwork), GFP_NOIO);
2643922dab61SIlya Dryomov 	if (!lwork)
2644922dab61SIlya Dryomov 		return NULL;
2645922dab61SIlya Dryomov 
2646922dab61SIlya Dryomov 	INIT_WORK(&lwork->work, workfn);
2647b07d3c4bSIlya Dryomov 	INIT_LIST_HEAD(&lwork->pending_item);
2648922dab61SIlya Dryomov 	lwork->lreq = linger_get(lreq);
2649922dab61SIlya Dryomov 
2650922dab61SIlya Dryomov 	return lwork;
2651922dab61SIlya Dryomov }
2652922dab61SIlya Dryomov 
2653922dab61SIlya Dryomov static void lwork_free(struct linger_work *lwork)
2654922dab61SIlya Dryomov {
2655922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2656922dab61SIlya Dryomov 
2657b07d3c4bSIlya Dryomov 	mutex_lock(&lreq->lock);
2658b07d3c4bSIlya Dryomov 	list_del(&lwork->pending_item);
2659b07d3c4bSIlya Dryomov 	mutex_unlock(&lreq->lock);
2660b07d3c4bSIlya Dryomov 
2661922dab61SIlya Dryomov 	linger_put(lreq);
2662922dab61SIlya Dryomov 	kfree(lwork);
2663922dab61SIlya Dryomov }
2664922dab61SIlya Dryomov 
2665922dab61SIlya Dryomov static void lwork_queue(struct linger_work *lwork)
2666922dab61SIlya Dryomov {
2667922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2668922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2669922dab61SIlya Dryomov 
2670922dab61SIlya Dryomov 	verify_lreq_locked(lreq);
2671b07d3c4bSIlya Dryomov 	WARN_ON(!list_empty(&lwork->pending_item));
2672b07d3c4bSIlya Dryomov 
2673b07d3c4bSIlya Dryomov 	lwork->queued_stamp = jiffies;
2674b07d3c4bSIlya Dryomov 	list_add_tail(&lwork->pending_item, &lreq->pending_lworks);
2675922dab61SIlya Dryomov 	queue_work(osdc->notify_wq, &lwork->work);
2676922dab61SIlya Dryomov }
2677922dab61SIlya Dryomov 
2678922dab61SIlya Dryomov static void do_watch_notify(struct work_struct *w)
2679922dab61SIlya Dryomov {
2680922dab61SIlya Dryomov 	struct linger_work *lwork = container_of(w, struct linger_work, work);
2681922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2682922dab61SIlya Dryomov 
2683922dab61SIlya Dryomov 	if (!linger_registered(lreq)) {
2684922dab61SIlya Dryomov 		dout("%s lreq %p not registered\n", __func__, lreq);
2685922dab61SIlya Dryomov 		goto out;
2686922dab61SIlya Dryomov 	}
2687922dab61SIlya Dryomov 
268819079203SIlya Dryomov 	WARN_ON(!lreq->is_watch);
2689922dab61SIlya Dryomov 	dout("%s lreq %p notify_id %llu notifier_id %llu payload_len %zu\n",
2690922dab61SIlya Dryomov 	     __func__, lreq, lwork->notify.notify_id, lwork->notify.notifier_id,
2691922dab61SIlya Dryomov 	     lwork->notify.payload_len);
2692922dab61SIlya Dryomov 	lreq->wcb(lreq->data, lwork->notify.notify_id, lreq->linger_id,
2693922dab61SIlya Dryomov 		  lwork->notify.notifier_id, lwork->notify.payload,
2694922dab61SIlya Dryomov 		  lwork->notify.payload_len);
2695922dab61SIlya Dryomov 
2696922dab61SIlya Dryomov out:
2697922dab61SIlya Dryomov 	ceph_msg_put(lwork->notify.msg);
2698922dab61SIlya Dryomov 	lwork_free(lwork);
2699922dab61SIlya Dryomov }
2700922dab61SIlya Dryomov 
2701922dab61SIlya Dryomov static void do_watch_error(struct work_struct *w)
2702922dab61SIlya Dryomov {
2703922dab61SIlya Dryomov 	struct linger_work *lwork = container_of(w, struct linger_work, work);
2704922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2705922dab61SIlya Dryomov 
2706922dab61SIlya Dryomov 	if (!linger_registered(lreq)) {
2707922dab61SIlya Dryomov 		dout("%s lreq %p not registered\n", __func__, lreq);
2708922dab61SIlya Dryomov 		goto out;
2709922dab61SIlya Dryomov 	}
2710922dab61SIlya Dryomov 
2711922dab61SIlya Dryomov 	dout("%s lreq %p err %d\n", __func__, lreq, lwork->error.err);
2712922dab61SIlya Dryomov 	lreq->errcb(lreq->data, lreq->linger_id, lwork->error.err);
2713922dab61SIlya Dryomov 
2714922dab61SIlya Dryomov out:
2715922dab61SIlya Dryomov 	lwork_free(lwork);
2716922dab61SIlya Dryomov }
2717922dab61SIlya Dryomov 
2718922dab61SIlya Dryomov static void queue_watch_error(struct ceph_osd_linger_request *lreq)
2719922dab61SIlya Dryomov {
2720922dab61SIlya Dryomov 	struct linger_work *lwork;
2721922dab61SIlya Dryomov 
2722922dab61SIlya Dryomov 	lwork = lwork_alloc(lreq, do_watch_error);
2723922dab61SIlya Dryomov 	if (!lwork) {
2724922dab61SIlya Dryomov 		pr_err("failed to allocate error-lwork\n");
2725922dab61SIlya Dryomov 		return;
2726922dab61SIlya Dryomov 	}
2727922dab61SIlya Dryomov 
2728922dab61SIlya Dryomov 	lwork->error.err = lreq->last_error;
2729922dab61SIlya Dryomov 	lwork_queue(lwork);
2730922dab61SIlya Dryomov }
2731922dab61SIlya Dryomov 
2732922dab61SIlya Dryomov static void linger_reg_commit_complete(struct ceph_osd_linger_request *lreq,
2733922dab61SIlya Dryomov 				       int result)
2734922dab61SIlya Dryomov {
2735922dab61SIlya Dryomov 	if (!completion_done(&lreq->reg_commit_wait)) {
2736922dab61SIlya Dryomov 		lreq->reg_commit_error = (result <= 0 ? result : 0);
2737922dab61SIlya Dryomov 		complete_all(&lreq->reg_commit_wait);
2738922dab61SIlya Dryomov 	}
2739922dab61SIlya Dryomov }
2740922dab61SIlya Dryomov 
2741922dab61SIlya Dryomov static void linger_commit_cb(struct ceph_osd_request *req)
2742922dab61SIlya Dryomov {
2743922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
2744922dab61SIlya Dryomov 
2745922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
2746922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu result %d\n", __func__, lreq,
2747922dab61SIlya Dryomov 	     lreq->linger_id, req->r_result);
2748922dab61SIlya Dryomov 	linger_reg_commit_complete(lreq, req->r_result);
2749922dab61SIlya Dryomov 	lreq->committed = true;
2750922dab61SIlya Dryomov 
275119079203SIlya Dryomov 	if (!lreq->is_watch) {
275219079203SIlya Dryomov 		struct ceph_osd_data *osd_data =
275319079203SIlya Dryomov 		    osd_req_op_data(req, 0, notify, response_data);
275419079203SIlya Dryomov 		void *p = page_address(osd_data->pages[0]);
275519079203SIlya Dryomov 
275619079203SIlya Dryomov 		WARN_ON(req->r_ops[0].op != CEPH_OSD_OP_NOTIFY ||
275719079203SIlya Dryomov 			osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
275819079203SIlya Dryomov 
275919079203SIlya Dryomov 		/* make note of the notify_id */
276019079203SIlya Dryomov 		if (req->r_ops[0].outdata_len >= sizeof(u64)) {
276119079203SIlya Dryomov 			lreq->notify_id = ceph_decode_64(&p);
276219079203SIlya Dryomov 			dout("lreq %p notify_id %llu\n", lreq,
276319079203SIlya Dryomov 			     lreq->notify_id);
276419079203SIlya Dryomov 		} else {
276519079203SIlya Dryomov 			dout("lreq %p no notify_id\n", lreq);
276619079203SIlya Dryomov 		}
276719079203SIlya Dryomov 	}
276819079203SIlya Dryomov 
2769922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
2770922dab61SIlya Dryomov 	linger_put(lreq);
2771922dab61SIlya Dryomov }
2772922dab61SIlya Dryomov 
2773922dab61SIlya Dryomov static int normalize_watch_error(int err)
2774922dab61SIlya Dryomov {
2775922dab61SIlya Dryomov 	/*
2776922dab61SIlya Dryomov 	 * Translate ENOENT -> ENOTCONN so that a delete->disconnection
2777922dab61SIlya Dryomov 	 * notification and a failure to reconnect because we raced with
2778922dab61SIlya Dryomov 	 * the delete appear the same to the user.
2779922dab61SIlya Dryomov 	 */
2780922dab61SIlya Dryomov 	if (err == -ENOENT)
2781922dab61SIlya Dryomov 		err = -ENOTCONN;
2782922dab61SIlya Dryomov 
2783922dab61SIlya Dryomov 	return err;
2784922dab61SIlya Dryomov }
2785922dab61SIlya Dryomov 
2786922dab61SIlya Dryomov static void linger_reconnect_cb(struct ceph_osd_request *req)
2787922dab61SIlya Dryomov {
2788922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
2789922dab61SIlya Dryomov 
2790922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
2791922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu result %d last_error %d\n", __func__,
2792922dab61SIlya Dryomov 	     lreq, lreq->linger_id, req->r_result, lreq->last_error);
2793922dab61SIlya Dryomov 	if (req->r_result < 0) {
2794922dab61SIlya Dryomov 		if (!lreq->last_error) {
2795922dab61SIlya Dryomov 			lreq->last_error = normalize_watch_error(req->r_result);
2796922dab61SIlya Dryomov 			queue_watch_error(lreq);
2797922dab61SIlya Dryomov 		}
2798922dab61SIlya Dryomov 	}
2799922dab61SIlya Dryomov 
2800922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
2801922dab61SIlya Dryomov 	linger_put(lreq);
2802922dab61SIlya Dryomov }
2803922dab61SIlya Dryomov 
2804922dab61SIlya Dryomov static void send_linger(struct ceph_osd_linger_request *lreq)
2805922dab61SIlya Dryomov {
2806922dab61SIlya Dryomov 	struct ceph_osd_request *req = lreq->reg_req;
2807922dab61SIlya Dryomov 	struct ceph_osd_req_op *op = &req->r_ops[0];
2808922dab61SIlya Dryomov 
2809922dab61SIlya Dryomov 	verify_osdc_wrlocked(req->r_osdc);
2810922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2811922dab61SIlya Dryomov 
2812922dab61SIlya Dryomov 	if (req->r_osd)
2813922dab61SIlya Dryomov 		cancel_linger_request(req);
2814922dab61SIlya Dryomov 
2815922dab61SIlya Dryomov 	request_reinit(req);
2816922dab61SIlya Dryomov 	ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
2817922dab61SIlya Dryomov 	ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
2818922dab61SIlya Dryomov 	req->r_flags = lreq->t.flags;
2819922dab61SIlya Dryomov 	req->r_mtime = lreq->mtime;
2820922dab61SIlya Dryomov 
2821922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
282219079203SIlya Dryomov 	if (lreq->is_watch && lreq->committed) {
2823922dab61SIlya Dryomov 		WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2824922dab61SIlya Dryomov 			op->watch.cookie != lreq->linger_id);
2825922dab61SIlya Dryomov 		op->watch.op = CEPH_OSD_WATCH_OP_RECONNECT;
2826922dab61SIlya Dryomov 		op->watch.gen = ++lreq->register_gen;
2827922dab61SIlya Dryomov 		dout("lreq %p reconnect register_gen %u\n", lreq,
2828922dab61SIlya Dryomov 		     op->watch.gen);
2829922dab61SIlya Dryomov 		req->r_callback = linger_reconnect_cb;
2830922dab61SIlya Dryomov 	} else {
283119079203SIlya Dryomov 		if (!lreq->is_watch)
283219079203SIlya Dryomov 			lreq->notify_id = 0;
283319079203SIlya Dryomov 		else
2834922dab61SIlya Dryomov 			WARN_ON(op->watch.op != CEPH_OSD_WATCH_OP_WATCH);
2835922dab61SIlya Dryomov 		dout("lreq %p register\n", lreq);
2836922dab61SIlya Dryomov 		req->r_callback = linger_commit_cb;
2837922dab61SIlya Dryomov 	}
2838922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
2839922dab61SIlya Dryomov 
2840922dab61SIlya Dryomov 	req->r_priv = linger_get(lreq);
2841922dab61SIlya Dryomov 	req->r_linger = true;
2842922dab61SIlya Dryomov 
2843922dab61SIlya Dryomov 	submit_request(req, true);
2844922dab61SIlya Dryomov }
2845922dab61SIlya Dryomov 
2846922dab61SIlya Dryomov static void linger_ping_cb(struct ceph_osd_request *req)
2847922dab61SIlya Dryomov {
2848922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
2849922dab61SIlya Dryomov 
2850922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
2851922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu result %d ping_sent %lu last_error %d\n",
2852922dab61SIlya Dryomov 	     __func__, lreq, lreq->linger_id, req->r_result, lreq->ping_sent,
2853922dab61SIlya Dryomov 	     lreq->last_error);
2854922dab61SIlya Dryomov 	if (lreq->register_gen == req->r_ops[0].watch.gen) {
2855b07d3c4bSIlya Dryomov 		if (!req->r_result) {
2856b07d3c4bSIlya Dryomov 			lreq->watch_valid_thru = lreq->ping_sent;
2857b07d3c4bSIlya Dryomov 		} else if (!lreq->last_error) {
2858922dab61SIlya Dryomov 			lreq->last_error = normalize_watch_error(req->r_result);
2859922dab61SIlya Dryomov 			queue_watch_error(lreq);
2860922dab61SIlya Dryomov 		}
2861922dab61SIlya Dryomov 	} else {
2862922dab61SIlya Dryomov 		dout("lreq %p register_gen %u ignoring old pong %u\n", lreq,
2863922dab61SIlya Dryomov 		     lreq->register_gen, req->r_ops[0].watch.gen);
2864922dab61SIlya Dryomov 	}
2865922dab61SIlya Dryomov 
2866922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
2867922dab61SIlya Dryomov 	linger_put(lreq);
2868922dab61SIlya Dryomov }
2869922dab61SIlya Dryomov 
2870922dab61SIlya Dryomov static void send_linger_ping(struct ceph_osd_linger_request *lreq)
2871922dab61SIlya Dryomov {
2872922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2873922dab61SIlya Dryomov 	struct ceph_osd_request *req = lreq->ping_req;
2874922dab61SIlya Dryomov 	struct ceph_osd_req_op *op = &req->r_ops[0];
2875922dab61SIlya Dryomov 
2876b7ec35b3SIlya Dryomov 	if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
2877922dab61SIlya Dryomov 		dout("%s PAUSERD\n", __func__);
2878922dab61SIlya Dryomov 		return;
2879922dab61SIlya Dryomov 	}
2880922dab61SIlya Dryomov 
2881922dab61SIlya Dryomov 	lreq->ping_sent = jiffies;
2882922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu ping_sent %lu register_gen %u\n",
2883922dab61SIlya Dryomov 	     __func__, lreq, lreq->linger_id, lreq->ping_sent,
2884922dab61SIlya Dryomov 	     lreq->register_gen);
2885922dab61SIlya Dryomov 
2886922dab61SIlya Dryomov 	if (req->r_osd)
2887922dab61SIlya Dryomov 		cancel_linger_request(req);
2888922dab61SIlya Dryomov 
2889922dab61SIlya Dryomov 	request_reinit(req);
2890922dab61SIlya Dryomov 	target_copy(&req->r_t, &lreq->t);
2891922dab61SIlya Dryomov 
2892922dab61SIlya Dryomov 	WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2893922dab61SIlya Dryomov 		op->watch.cookie != lreq->linger_id ||
2894922dab61SIlya Dryomov 		op->watch.op != CEPH_OSD_WATCH_OP_PING);
2895922dab61SIlya Dryomov 	op->watch.gen = lreq->register_gen;
2896922dab61SIlya Dryomov 	req->r_callback = linger_ping_cb;
2897922dab61SIlya Dryomov 	req->r_priv = linger_get(lreq);
2898922dab61SIlya Dryomov 	req->r_linger = true;
2899922dab61SIlya Dryomov 
2900922dab61SIlya Dryomov 	ceph_osdc_get_request(req);
2901922dab61SIlya Dryomov 	account_request(req);
2902922dab61SIlya Dryomov 	req->r_tid = atomic64_inc_return(&osdc->last_tid);
2903922dab61SIlya Dryomov 	link_request(lreq->osd, req);
2904922dab61SIlya Dryomov 	send_request(req);
2905922dab61SIlya Dryomov }
2906922dab61SIlya Dryomov 
2907922dab61SIlya Dryomov static void linger_submit(struct ceph_osd_linger_request *lreq)
2908922dab61SIlya Dryomov {
2909922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2910922dab61SIlya Dryomov 	struct ceph_osd *osd;
2911922dab61SIlya Dryomov 
29127de030d6SIlya Dryomov 	calc_target(osdc, &lreq->t, NULL, false);
2913922dab61SIlya Dryomov 	osd = lookup_create_osd(osdc, lreq->t.osd, true);
2914922dab61SIlya Dryomov 	link_linger(osd, lreq);
2915922dab61SIlya Dryomov 
2916922dab61SIlya Dryomov 	send_linger(lreq);
2917922dab61SIlya Dryomov }
2918922dab61SIlya Dryomov 
29194609245eSIlya Dryomov static void cancel_linger_map_check(struct ceph_osd_linger_request *lreq)
29204609245eSIlya Dryomov {
29214609245eSIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
29224609245eSIlya Dryomov 	struct ceph_osd_linger_request *lookup_lreq;
29234609245eSIlya Dryomov 
29244609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
29254609245eSIlya Dryomov 
29264609245eSIlya Dryomov 	lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
29274609245eSIlya Dryomov 				       lreq->linger_id);
29284609245eSIlya Dryomov 	if (!lookup_lreq)
29294609245eSIlya Dryomov 		return;
29304609245eSIlya Dryomov 
29314609245eSIlya Dryomov 	WARN_ON(lookup_lreq != lreq);
29324609245eSIlya Dryomov 	erase_linger_mc(&osdc->linger_map_checks, lreq);
29334609245eSIlya Dryomov 	linger_put(lreq);
29344609245eSIlya Dryomov }
29354609245eSIlya Dryomov 
2936922dab61SIlya Dryomov /*
2937922dab61SIlya Dryomov  * @lreq has to be both registered and linked.
2938922dab61SIlya Dryomov  */
2939922dab61SIlya Dryomov static void __linger_cancel(struct ceph_osd_linger_request *lreq)
2940922dab61SIlya Dryomov {
294119079203SIlya Dryomov 	if (lreq->is_watch && lreq->ping_req->r_osd)
2942922dab61SIlya Dryomov 		cancel_linger_request(lreq->ping_req);
2943922dab61SIlya Dryomov 	if (lreq->reg_req->r_osd)
2944922dab61SIlya Dryomov 		cancel_linger_request(lreq->reg_req);
29454609245eSIlya Dryomov 	cancel_linger_map_check(lreq);
2946922dab61SIlya Dryomov 	unlink_linger(lreq->osd, lreq);
2947922dab61SIlya Dryomov 	linger_unregister(lreq);
2948922dab61SIlya Dryomov }
2949922dab61SIlya Dryomov 
2950922dab61SIlya Dryomov static void linger_cancel(struct ceph_osd_linger_request *lreq)
2951922dab61SIlya Dryomov {
2952922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2953922dab61SIlya Dryomov 
2954922dab61SIlya Dryomov 	down_write(&osdc->lock);
2955922dab61SIlya Dryomov 	if (__linger_registered(lreq))
2956922dab61SIlya Dryomov 		__linger_cancel(lreq);
2957922dab61SIlya Dryomov 	up_write(&osdc->lock);
2958922dab61SIlya Dryomov }
2959922dab61SIlya Dryomov 
29604609245eSIlya Dryomov static void send_linger_map_check(struct ceph_osd_linger_request *lreq);
29614609245eSIlya Dryomov 
29624609245eSIlya Dryomov static void check_linger_pool_dne(struct ceph_osd_linger_request *lreq)
29634609245eSIlya Dryomov {
29644609245eSIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
29654609245eSIlya Dryomov 	struct ceph_osdmap *map = osdc->osdmap;
29664609245eSIlya Dryomov 
29674609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
29684609245eSIlya Dryomov 	WARN_ON(!map->epoch);
29694609245eSIlya Dryomov 
29704609245eSIlya Dryomov 	if (lreq->register_gen) {
29714609245eSIlya Dryomov 		lreq->map_dne_bound = map->epoch;
29724609245eSIlya Dryomov 		dout("%s lreq %p linger_id %llu pool disappeared\n", __func__,
29734609245eSIlya Dryomov 		     lreq, lreq->linger_id);
29744609245eSIlya Dryomov 	} else {
29754609245eSIlya Dryomov 		dout("%s lreq %p linger_id %llu map_dne_bound %u have %u\n",
29764609245eSIlya Dryomov 		     __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
29774609245eSIlya Dryomov 		     map->epoch);
29784609245eSIlya Dryomov 	}
29794609245eSIlya Dryomov 
29804609245eSIlya Dryomov 	if (lreq->map_dne_bound) {
29814609245eSIlya Dryomov 		if (map->epoch >= lreq->map_dne_bound) {
29824609245eSIlya Dryomov 			/* we had a new enough map */
29834609245eSIlya Dryomov 			pr_info("linger_id %llu pool does not exist\n",
29844609245eSIlya Dryomov 				lreq->linger_id);
29854609245eSIlya Dryomov 			linger_reg_commit_complete(lreq, -ENOENT);
29864609245eSIlya Dryomov 			__linger_cancel(lreq);
29874609245eSIlya Dryomov 		}
29884609245eSIlya Dryomov 	} else {
29894609245eSIlya Dryomov 		send_linger_map_check(lreq);
29904609245eSIlya Dryomov 	}
29914609245eSIlya Dryomov }
29924609245eSIlya Dryomov 
29934609245eSIlya Dryomov static void linger_map_check_cb(struct ceph_mon_generic_request *greq)
29944609245eSIlya Dryomov {
29954609245eSIlya Dryomov 	struct ceph_osd_client *osdc = &greq->monc->client->osdc;
29964609245eSIlya Dryomov 	struct ceph_osd_linger_request *lreq;
29974609245eSIlya Dryomov 	u64 linger_id = greq->private_data;
29984609245eSIlya Dryomov 
29994609245eSIlya Dryomov 	WARN_ON(greq->result || !greq->u.newest);
30004609245eSIlya Dryomov 
30014609245eSIlya Dryomov 	down_write(&osdc->lock);
30024609245eSIlya Dryomov 	lreq = lookup_linger_mc(&osdc->linger_map_checks, linger_id);
30034609245eSIlya Dryomov 	if (!lreq) {
30044609245eSIlya Dryomov 		dout("%s linger_id %llu dne\n", __func__, linger_id);
30054609245eSIlya Dryomov 		goto out_unlock;
30064609245eSIlya Dryomov 	}
30074609245eSIlya Dryomov 
30084609245eSIlya Dryomov 	dout("%s lreq %p linger_id %llu map_dne_bound %u newest %llu\n",
30094609245eSIlya Dryomov 	     __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
30104609245eSIlya Dryomov 	     greq->u.newest);
30114609245eSIlya Dryomov 	if (!lreq->map_dne_bound)
30124609245eSIlya Dryomov 		lreq->map_dne_bound = greq->u.newest;
30134609245eSIlya Dryomov 	erase_linger_mc(&osdc->linger_map_checks, lreq);
30144609245eSIlya Dryomov 	check_linger_pool_dne(lreq);
30154609245eSIlya Dryomov 
30164609245eSIlya Dryomov 	linger_put(lreq);
30174609245eSIlya Dryomov out_unlock:
30184609245eSIlya Dryomov 	up_write(&osdc->lock);
30194609245eSIlya Dryomov }
30204609245eSIlya Dryomov 
30214609245eSIlya Dryomov static void send_linger_map_check(struct ceph_osd_linger_request *lreq)
30224609245eSIlya Dryomov {
30234609245eSIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
30244609245eSIlya Dryomov 	struct ceph_osd_linger_request *lookup_lreq;
30254609245eSIlya Dryomov 	int ret;
30264609245eSIlya Dryomov 
30274609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
30284609245eSIlya Dryomov 
30294609245eSIlya Dryomov 	lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
30304609245eSIlya Dryomov 				       lreq->linger_id);
30314609245eSIlya Dryomov 	if (lookup_lreq) {
30324609245eSIlya Dryomov 		WARN_ON(lookup_lreq != lreq);
30334609245eSIlya Dryomov 		return;
30344609245eSIlya Dryomov 	}
30354609245eSIlya Dryomov 
30364609245eSIlya Dryomov 	linger_get(lreq);
30374609245eSIlya Dryomov 	insert_linger_mc(&osdc->linger_map_checks, lreq);
30384609245eSIlya Dryomov 	ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
30394609245eSIlya Dryomov 					  linger_map_check_cb, lreq->linger_id);
30404609245eSIlya Dryomov 	WARN_ON(ret);
30414609245eSIlya Dryomov }
30424609245eSIlya Dryomov 
3043922dab61SIlya Dryomov static int linger_reg_commit_wait(struct ceph_osd_linger_request *lreq)
3044922dab61SIlya Dryomov {
3045922dab61SIlya Dryomov 	int ret;
3046922dab61SIlya Dryomov 
3047922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3048922dab61SIlya Dryomov 	ret = wait_for_completion_interruptible(&lreq->reg_commit_wait);
3049922dab61SIlya Dryomov 	return ret ?: lreq->reg_commit_error;
3050922dab61SIlya Dryomov }
3051922dab61SIlya Dryomov 
305219079203SIlya Dryomov static int linger_notify_finish_wait(struct ceph_osd_linger_request *lreq)
305319079203SIlya Dryomov {
305419079203SIlya Dryomov 	int ret;
305519079203SIlya Dryomov 
305619079203SIlya Dryomov 	dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
305719079203SIlya Dryomov 	ret = wait_for_completion_interruptible(&lreq->notify_finish_wait);
305819079203SIlya Dryomov 	return ret ?: lreq->notify_finish_error;
305919079203SIlya Dryomov }
306019079203SIlya Dryomov 
3061922dab61SIlya Dryomov /*
3062fbca9635SIlya Dryomov  * Timeout callback, called every N seconds.  When 1 or more OSD
3063fbca9635SIlya Dryomov  * requests has been active for more than N seconds, we send a keepalive
3064fbca9635SIlya Dryomov  * (tag + timestamp) to its OSD to ensure any communications channel
3065fbca9635SIlya Dryomov  * reset is detected.
30663d14c5d2SYehuda Sadeh  */
30673d14c5d2SYehuda Sadeh static void handle_timeout(struct work_struct *work)
30683d14c5d2SYehuda Sadeh {
30693d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc =
30703d14c5d2SYehuda Sadeh 		container_of(work, struct ceph_osd_client, timeout_work.work);
3071a319bf56SIlya Dryomov 	struct ceph_options *opts = osdc->client->options;
30725aea3dcdSIlya Dryomov 	unsigned long cutoff = jiffies - opts->osd_keepalive_timeout;
30737cc5e38fSIlya Dryomov 	unsigned long expiry_cutoff = jiffies - opts->osd_request_timeout;
30745aea3dcdSIlya Dryomov 	LIST_HEAD(slow_osds);
30755aea3dcdSIlya Dryomov 	struct rb_node *n, *p;
30763d14c5d2SYehuda Sadeh 
30775aea3dcdSIlya Dryomov 	dout("%s osdc %p\n", __func__, osdc);
30785aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
30793d14c5d2SYehuda Sadeh 
30803d14c5d2SYehuda Sadeh 	/*
30813d14c5d2SYehuda Sadeh 	 * ping osds that are a bit slow.  this ensures that if there
30823d14c5d2SYehuda Sadeh 	 * is a break in the TCP connection we will notice, and reopen
30833d14c5d2SYehuda Sadeh 	 * a connection with that osd (from the fault callback).
30843d14c5d2SYehuda Sadeh 	 */
30855aea3dcdSIlya Dryomov 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
30865aea3dcdSIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
30875aea3dcdSIlya Dryomov 		bool found = false;
30883d14c5d2SYehuda Sadeh 
30897cc5e38fSIlya Dryomov 		for (p = rb_first(&osd->o_requests); p; ) {
30905aea3dcdSIlya Dryomov 			struct ceph_osd_request *req =
30915aea3dcdSIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
30925aea3dcdSIlya Dryomov 
30937cc5e38fSIlya Dryomov 			p = rb_next(p); /* abort_request() */
30947cc5e38fSIlya Dryomov 
30955aea3dcdSIlya Dryomov 			if (time_before(req->r_stamp, cutoff)) {
30965aea3dcdSIlya Dryomov 				dout(" req %p tid %llu on osd%d is laggy\n",
30975aea3dcdSIlya Dryomov 				     req, req->r_tid, osd->o_osd);
30985aea3dcdSIlya Dryomov 				found = true;
30995aea3dcdSIlya Dryomov 			}
31007cc5e38fSIlya Dryomov 			if (opts->osd_request_timeout &&
31017cc5e38fSIlya Dryomov 			    time_before(req->r_start_stamp, expiry_cutoff)) {
31027cc5e38fSIlya Dryomov 				pr_err_ratelimited("tid %llu on osd%d timeout\n",
31037cc5e38fSIlya Dryomov 				       req->r_tid, osd->o_osd);
31047cc5e38fSIlya Dryomov 				abort_request(req, -ETIMEDOUT);
31057cc5e38fSIlya Dryomov 			}
31065aea3dcdSIlya Dryomov 		}
3107922dab61SIlya Dryomov 		for (p = rb_first(&osd->o_linger_requests); p; p = rb_next(p)) {
3108922dab61SIlya Dryomov 			struct ceph_osd_linger_request *lreq =
3109922dab61SIlya Dryomov 			    rb_entry(p, struct ceph_osd_linger_request, node);
3110922dab61SIlya Dryomov 
3111922dab61SIlya Dryomov 			dout(" lreq %p linger_id %llu is served by osd%d\n",
3112922dab61SIlya Dryomov 			     lreq, lreq->linger_id, osd->o_osd);
3113922dab61SIlya Dryomov 			found = true;
3114922dab61SIlya Dryomov 
3115922dab61SIlya Dryomov 			mutex_lock(&lreq->lock);
311619079203SIlya Dryomov 			if (lreq->is_watch && lreq->committed && !lreq->last_error)
3117922dab61SIlya Dryomov 				send_linger_ping(lreq);
3118922dab61SIlya Dryomov 			mutex_unlock(&lreq->lock);
3119922dab61SIlya Dryomov 		}
31205aea3dcdSIlya Dryomov 
31215aea3dcdSIlya Dryomov 		if (found)
31223d14c5d2SYehuda Sadeh 			list_move_tail(&osd->o_keepalive_item, &slow_osds);
31233d14c5d2SYehuda Sadeh 	}
31245aea3dcdSIlya Dryomov 
31257cc5e38fSIlya Dryomov 	if (opts->osd_request_timeout) {
31267cc5e38fSIlya Dryomov 		for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
31277cc5e38fSIlya Dryomov 			struct ceph_osd_request *req =
31287cc5e38fSIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
31297cc5e38fSIlya Dryomov 
31307cc5e38fSIlya Dryomov 			p = rb_next(p); /* abort_request() */
31317cc5e38fSIlya Dryomov 
31327cc5e38fSIlya Dryomov 			if (time_before(req->r_start_stamp, expiry_cutoff)) {
31337cc5e38fSIlya Dryomov 				pr_err_ratelimited("tid %llu on osd%d timeout\n",
31347cc5e38fSIlya Dryomov 				       req->r_tid, osdc->homeless_osd.o_osd);
31357cc5e38fSIlya Dryomov 				abort_request(req, -ETIMEDOUT);
31367cc5e38fSIlya Dryomov 			}
31377cc5e38fSIlya Dryomov 		}
31387cc5e38fSIlya Dryomov 	}
31397cc5e38fSIlya Dryomov 
31405aea3dcdSIlya Dryomov 	if (atomic_read(&osdc->num_homeless) || !list_empty(&slow_osds))
31415aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
31425aea3dcdSIlya Dryomov 
31433d14c5d2SYehuda Sadeh 	while (!list_empty(&slow_osds)) {
31445aea3dcdSIlya Dryomov 		struct ceph_osd *osd = list_first_entry(&slow_osds,
31455aea3dcdSIlya Dryomov 							struct ceph_osd,
31463d14c5d2SYehuda Sadeh 							o_keepalive_item);
31473d14c5d2SYehuda Sadeh 		list_del_init(&osd->o_keepalive_item);
31483d14c5d2SYehuda Sadeh 		ceph_con_keepalive(&osd->o_con);
31493d14c5d2SYehuda Sadeh 	}
31503d14c5d2SYehuda Sadeh 
31515aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
3152fbca9635SIlya Dryomov 	schedule_delayed_work(&osdc->timeout_work,
3153fbca9635SIlya Dryomov 			      osdc->client->options->osd_keepalive_timeout);
31543d14c5d2SYehuda Sadeh }
31553d14c5d2SYehuda Sadeh 
31563d14c5d2SYehuda Sadeh static void handle_osds_timeout(struct work_struct *work)
31573d14c5d2SYehuda Sadeh {
31583d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc =
31593d14c5d2SYehuda Sadeh 		container_of(work, struct ceph_osd_client,
31603d14c5d2SYehuda Sadeh 			     osds_timeout_work.work);
3161a319bf56SIlya Dryomov 	unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
316242a2c09fSIlya Dryomov 	struct ceph_osd *osd, *nosd;
31633d14c5d2SYehuda Sadeh 
316442a2c09fSIlya Dryomov 	dout("%s osdc %p\n", __func__, osdc);
31655aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
316642a2c09fSIlya Dryomov 	list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
316742a2c09fSIlya Dryomov 		if (time_before(jiffies, osd->lru_ttl))
316842a2c09fSIlya Dryomov 			break;
316942a2c09fSIlya Dryomov 
31705aea3dcdSIlya Dryomov 		WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
3171922dab61SIlya Dryomov 		WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
31725aea3dcdSIlya Dryomov 		close_osd(osd);
317342a2c09fSIlya Dryomov 	}
317442a2c09fSIlya Dryomov 
31755aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
31763d14c5d2SYehuda Sadeh 	schedule_delayed_work(&osdc->osds_timeout_work,
31773d14c5d2SYehuda Sadeh 			      round_jiffies_relative(delay));
31783d14c5d2SYehuda Sadeh }
31793d14c5d2SYehuda Sadeh 
3180205ee118SIlya Dryomov static int ceph_oloc_decode(void **p, void *end,
3181205ee118SIlya Dryomov 			    struct ceph_object_locator *oloc)
3182205ee118SIlya Dryomov {
3183205ee118SIlya Dryomov 	u8 struct_v, struct_cv;
3184205ee118SIlya Dryomov 	u32 len;
3185205ee118SIlya Dryomov 	void *struct_end;
3186205ee118SIlya Dryomov 	int ret = 0;
3187205ee118SIlya Dryomov 
3188205ee118SIlya Dryomov 	ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3189205ee118SIlya Dryomov 	struct_v = ceph_decode_8(p);
3190205ee118SIlya Dryomov 	struct_cv = ceph_decode_8(p);
3191205ee118SIlya Dryomov 	if (struct_v < 3) {
3192205ee118SIlya Dryomov 		pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
3193205ee118SIlya Dryomov 			struct_v, struct_cv);
3194205ee118SIlya Dryomov 		goto e_inval;
3195205ee118SIlya Dryomov 	}
3196205ee118SIlya Dryomov 	if (struct_cv > 6) {
3197205ee118SIlya Dryomov 		pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
3198205ee118SIlya Dryomov 			struct_v, struct_cv);
3199205ee118SIlya Dryomov 		goto e_inval;
3200205ee118SIlya Dryomov 	}
3201205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3202205ee118SIlya Dryomov 	ceph_decode_need(p, end, len, e_inval);
3203205ee118SIlya Dryomov 	struct_end = *p + len;
3204205ee118SIlya Dryomov 
3205205ee118SIlya Dryomov 	oloc->pool = ceph_decode_64(p);
3206205ee118SIlya Dryomov 	*p += 4; /* skip preferred */
3207205ee118SIlya Dryomov 
3208205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3209205ee118SIlya Dryomov 	if (len > 0) {
3210205ee118SIlya Dryomov 		pr_warn("ceph_object_locator::key is set\n");
3211205ee118SIlya Dryomov 		goto e_inval;
3212205ee118SIlya Dryomov 	}
3213205ee118SIlya Dryomov 
3214205ee118SIlya Dryomov 	if (struct_v >= 5) {
3215cd08e0a2SYan, Zheng 		bool changed = false;
3216cd08e0a2SYan, Zheng 
3217205ee118SIlya Dryomov 		len = ceph_decode_32(p);
3218205ee118SIlya Dryomov 		if (len > 0) {
321930c156d9SYan, Zheng 			ceph_decode_need(p, end, len, e_inval);
3220cd08e0a2SYan, Zheng 			if (!oloc->pool_ns ||
3221cd08e0a2SYan, Zheng 			    ceph_compare_string(oloc->pool_ns, *p, len))
3222cd08e0a2SYan, Zheng 				changed = true;
322330c156d9SYan, Zheng 			*p += len;
3224cd08e0a2SYan, Zheng 		} else {
3225cd08e0a2SYan, Zheng 			if (oloc->pool_ns)
3226cd08e0a2SYan, Zheng 				changed = true;
3227cd08e0a2SYan, Zheng 		}
3228cd08e0a2SYan, Zheng 		if (changed) {
3229cd08e0a2SYan, Zheng 			/* redirect changes namespace */
3230cd08e0a2SYan, Zheng 			pr_warn("ceph_object_locator::nspace is changed\n");
3231cd08e0a2SYan, Zheng 			goto e_inval;
3232205ee118SIlya Dryomov 		}
3233205ee118SIlya Dryomov 	}
3234205ee118SIlya Dryomov 
3235205ee118SIlya Dryomov 	if (struct_v >= 6) {
3236205ee118SIlya Dryomov 		s64 hash = ceph_decode_64(p);
3237205ee118SIlya Dryomov 		if (hash != -1) {
3238205ee118SIlya Dryomov 			pr_warn("ceph_object_locator::hash is set\n");
3239205ee118SIlya Dryomov 			goto e_inval;
3240205ee118SIlya Dryomov 		}
3241205ee118SIlya Dryomov 	}
3242205ee118SIlya Dryomov 
3243205ee118SIlya Dryomov 	/* skip the rest */
3244205ee118SIlya Dryomov 	*p = struct_end;
3245205ee118SIlya Dryomov out:
3246205ee118SIlya Dryomov 	return ret;
3247205ee118SIlya Dryomov 
3248205ee118SIlya Dryomov e_inval:
3249205ee118SIlya Dryomov 	ret = -EINVAL;
3250205ee118SIlya Dryomov 	goto out;
3251205ee118SIlya Dryomov }
3252205ee118SIlya Dryomov 
3253205ee118SIlya Dryomov static int ceph_redirect_decode(void **p, void *end,
3254205ee118SIlya Dryomov 				struct ceph_request_redirect *redir)
3255205ee118SIlya Dryomov {
3256205ee118SIlya Dryomov 	u8 struct_v, struct_cv;
3257205ee118SIlya Dryomov 	u32 len;
3258205ee118SIlya Dryomov 	void *struct_end;
3259205ee118SIlya Dryomov 	int ret;
3260205ee118SIlya Dryomov 
3261205ee118SIlya Dryomov 	ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3262205ee118SIlya Dryomov 	struct_v = ceph_decode_8(p);
3263205ee118SIlya Dryomov 	struct_cv = ceph_decode_8(p);
3264205ee118SIlya Dryomov 	if (struct_cv > 1) {
3265205ee118SIlya Dryomov 		pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
3266205ee118SIlya Dryomov 			struct_v, struct_cv);
3267205ee118SIlya Dryomov 		goto e_inval;
3268205ee118SIlya Dryomov 	}
3269205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3270205ee118SIlya Dryomov 	ceph_decode_need(p, end, len, e_inval);
3271205ee118SIlya Dryomov 	struct_end = *p + len;
3272205ee118SIlya Dryomov 
3273205ee118SIlya Dryomov 	ret = ceph_oloc_decode(p, end, &redir->oloc);
3274205ee118SIlya Dryomov 	if (ret)
3275205ee118SIlya Dryomov 		goto out;
3276205ee118SIlya Dryomov 
3277205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3278205ee118SIlya Dryomov 	if (len > 0) {
3279205ee118SIlya Dryomov 		pr_warn("ceph_request_redirect::object_name is set\n");
3280205ee118SIlya Dryomov 		goto e_inval;
3281205ee118SIlya Dryomov 	}
3282205ee118SIlya Dryomov 
3283205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3284205ee118SIlya Dryomov 	*p += len; /* skip osd_instructions */
3285205ee118SIlya Dryomov 
3286205ee118SIlya Dryomov 	/* skip the rest */
3287205ee118SIlya Dryomov 	*p = struct_end;
3288205ee118SIlya Dryomov out:
3289205ee118SIlya Dryomov 	return ret;
3290205ee118SIlya Dryomov 
3291205ee118SIlya Dryomov e_inval:
3292205ee118SIlya Dryomov 	ret = -EINVAL;
3293205ee118SIlya Dryomov 	goto out;
3294205ee118SIlya Dryomov }
3295205ee118SIlya Dryomov 
3296fe5da05eSIlya Dryomov struct MOSDOpReply {
3297fe5da05eSIlya Dryomov 	struct ceph_pg pgid;
3298fe5da05eSIlya Dryomov 	u64 flags;
3299fe5da05eSIlya Dryomov 	int result;
3300fe5da05eSIlya Dryomov 	u32 epoch;
3301fe5da05eSIlya Dryomov 	int num_ops;
3302fe5da05eSIlya Dryomov 	u32 outdata_len[CEPH_OSD_MAX_OPS];
3303fe5da05eSIlya Dryomov 	s32 rval[CEPH_OSD_MAX_OPS];
3304fe5da05eSIlya Dryomov 	int retry_attempt;
3305fe5da05eSIlya Dryomov 	struct ceph_eversion replay_version;
3306fe5da05eSIlya Dryomov 	u64 user_version;
3307fe5da05eSIlya Dryomov 	struct ceph_request_redirect redirect;
3308fe5da05eSIlya Dryomov };
330925845472SSage Weil 
3310fe5da05eSIlya Dryomov static int decode_MOSDOpReply(const struct ceph_msg *msg, struct MOSDOpReply *m)
33113d14c5d2SYehuda Sadeh {
3312fe5da05eSIlya Dryomov 	void *p = msg->front.iov_base;
3313fe5da05eSIlya Dryomov 	void *const end = p + msg->front.iov_len;
3314fe5da05eSIlya Dryomov 	u16 version = le16_to_cpu(msg->hdr.version);
3315fe5da05eSIlya Dryomov 	struct ceph_eversion bad_replay_version;
3316b0b31a8fSIlya Dryomov 	u8 decode_redir;
3317fe5da05eSIlya Dryomov 	u32 len;
3318fe5da05eSIlya Dryomov 	int ret;
3319fe5da05eSIlya Dryomov 	int i;
33203d14c5d2SYehuda Sadeh 
3321fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, len, e_inval);
3322fe5da05eSIlya Dryomov 	ceph_decode_need(&p, end, len, e_inval);
3323fe5da05eSIlya Dryomov 	p += len; /* skip oid */
33241b83bef2SSage Weil 
3325fe5da05eSIlya Dryomov 	ret = ceph_decode_pgid(&p, end, &m->pgid);
3326fe5da05eSIlya Dryomov 	if (ret)
3327fe5da05eSIlya Dryomov 		return ret;
33281b83bef2SSage Weil 
3329fe5da05eSIlya Dryomov 	ceph_decode_64_safe(&p, end, m->flags, e_inval);
3330fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->result, e_inval);
3331fe5da05eSIlya Dryomov 	ceph_decode_need(&p, end, sizeof(bad_replay_version), e_inval);
3332fe5da05eSIlya Dryomov 	memcpy(&bad_replay_version, p, sizeof(bad_replay_version));
3333fe5da05eSIlya Dryomov 	p += sizeof(bad_replay_version);
3334fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->epoch, e_inval);
33351b83bef2SSage Weil 
3336fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->num_ops, e_inval);
3337fe5da05eSIlya Dryomov 	if (m->num_ops > ARRAY_SIZE(m->outdata_len))
3338fe5da05eSIlya Dryomov 		goto e_inval;
33391b83bef2SSage Weil 
3340fe5da05eSIlya Dryomov 	ceph_decode_need(&p, end, m->num_ops * sizeof(struct ceph_osd_op),
3341fe5da05eSIlya Dryomov 			 e_inval);
3342fe5da05eSIlya Dryomov 	for (i = 0; i < m->num_ops; i++) {
33431b83bef2SSage Weil 		struct ceph_osd_op *op = p;
33441b83bef2SSage Weil 
3345fe5da05eSIlya Dryomov 		m->outdata_len[i] = le32_to_cpu(op->payload_len);
33461b83bef2SSage Weil 		p += sizeof(*op);
33471b83bef2SSage Weil 	}
3348fe5da05eSIlya Dryomov 
3349fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->retry_attempt, e_inval);
3350fe5da05eSIlya Dryomov 	for (i = 0; i < m->num_ops; i++)
3351fe5da05eSIlya Dryomov 		ceph_decode_32_safe(&p, end, m->rval[i], e_inval);
3352fe5da05eSIlya Dryomov 
3353fe5da05eSIlya Dryomov 	if (version >= 5) {
3354fe5da05eSIlya Dryomov 		ceph_decode_need(&p, end, sizeof(m->replay_version), e_inval);
3355fe5da05eSIlya Dryomov 		memcpy(&m->replay_version, p, sizeof(m->replay_version));
3356fe5da05eSIlya Dryomov 		p += sizeof(m->replay_version);
3357fe5da05eSIlya Dryomov 		ceph_decode_64_safe(&p, end, m->user_version, e_inval);
3358fe5da05eSIlya Dryomov 	} else {
3359fe5da05eSIlya Dryomov 		m->replay_version = bad_replay_version; /* struct */
3360fe5da05eSIlya Dryomov 		m->user_version = le64_to_cpu(m->replay_version.version);
33611b83bef2SSage Weil 	}
33621b83bef2SSage Weil 
3363fe5da05eSIlya Dryomov 	if (version >= 6) {
3364fe5da05eSIlya Dryomov 		if (version >= 7)
3365fe5da05eSIlya Dryomov 			ceph_decode_8_safe(&p, end, decode_redir, e_inval);
3366b0b31a8fSIlya Dryomov 		else
3367b0b31a8fSIlya Dryomov 			decode_redir = 1;
3368b0b31a8fSIlya Dryomov 	} else {
3369b0b31a8fSIlya Dryomov 		decode_redir = 0;
3370b0b31a8fSIlya Dryomov 	}
3371b0b31a8fSIlya Dryomov 
3372b0b31a8fSIlya Dryomov 	if (decode_redir) {
3373fe5da05eSIlya Dryomov 		ret = ceph_redirect_decode(&p, end, &m->redirect);
3374fe5da05eSIlya Dryomov 		if (ret)
3375fe5da05eSIlya Dryomov 			return ret;
3376205ee118SIlya Dryomov 	} else {
3377fe5da05eSIlya Dryomov 		ceph_oloc_init(&m->redirect.oloc);
3378205ee118SIlya Dryomov 	}
3379205ee118SIlya Dryomov 
3380fe5da05eSIlya Dryomov 	return 0;
3381205ee118SIlya Dryomov 
3382fe5da05eSIlya Dryomov e_inval:
3383fe5da05eSIlya Dryomov 	return -EINVAL;
3384fe5da05eSIlya Dryomov }
3385fe5da05eSIlya Dryomov 
3386fe5da05eSIlya Dryomov /*
3387b18b9550SIlya Dryomov  * Handle MOSDOpReply.  Set ->r_result and call the callback if it is
3388b18b9550SIlya Dryomov  * specified.
3389fe5da05eSIlya Dryomov  */
33905aea3dcdSIlya Dryomov static void handle_reply(struct ceph_osd *osd, struct ceph_msg *msg)
3391fe5da05eSIlya Dryomov {
33925aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
3393fe5da05eSIlya Dryomov 	struct ceph_osd_request *req;
3394fe5da05eSIlya Dryomov 	struct MOSDOpReply m;
3395fe5da05eSIlya Dryomov 	u64 tid = le64_to_cpu(msg->hdr.tid);
3396fe5da05eSIlya Dryomov 	u32 data_len = 0;
3397fe5da05eSIlya Dryomov 	int ret;
3398fe5da05eSIlya Dryomov 	int i;
3399fe5da05eSIlya Dryomov 
3400fe5da05eSIlya Dryomov 	dout("%s msg %p tid %llu\n", __func__, msg, tid);
3401fe5da05eSIlya Dryomov 
34025aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
34035aea3dcdSIlya Dryomov 	if (!osd_registered(osd)) {
34045aea3dcdSIlya Dryomov 		dout("%s osd%d unknown\n", __func__, osd->o_osd);
34055aea3dcdSIlya Dryomov 		goto out_unlock_osdc;
3406fe5da05eSIlya Dryomov 	}
34075aea3dcdSIlya Dryomov 	WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
34085aea3dcdSIlya Dryomov 
34095aea3dcdSIlya Dryomov 	mutex_lock(&osd->lock);
34105aea3dcdSIlya Dryomov 	req = lookup_request(&osd->o_requests, tid);
34115aea3dcdSIlya Dryomov 	if (!req) {
34125aea3dcdSIlya Dryomov 		dout("%s osd%d tid %llu unknown\n", __func__, osd->o_osd, tid);
34135aea3dcdSIlya Dryomov 		goto out_unlock_session;
34145aea3dcdSIlya Dryomov 	}
3415fe5da05eSIlya Dryomov 
3416cd08e0a2SYan, Zheng 	m.redirect.oloc.pool_ns = req->r_t.target_oloc.pool_ns;
3417fe5da05eSIlya Dryomov 	ret = decode_MOSDOpReply(msg, &m);
3418cd08e0a2SYan, Zheng 	m.redirect.oloc.pool_ns = NULL;
3419fe5da05eSIlya Dryomov 	if (ret) {
3420fe5da05eSIlya Dryomov 		pr_err("failed to decode MOSDOpReply for tid %llu: %d\n",
3421fe5da05eSIlya Dryomov 		       req->r_tid, ret);
3422fe5da05eSIlya Dryomov 		ceph_msg_dump(msg);
3423fe5da05eSIlya Dryomov 		goto fail_request;
3424fe5da05eSIlya Dryomov 	}
3425fe5da05eSIlya Dryomov 	dout("%s req %p tid %llu flags 0x%llx pgid %llu.%x epoch %u attempt %d v %u'%llu uv %llu\n",
3426fe5da05eSIlya Dryomov 	     __func__, req, req->r_tid, m.flags, m.pgid.pool, m.pgid.seed,
3427fe5da05eSIlya Dryomov 	     m.epoch, m.retry_attempt, le32_to_cpu(m.replay_version.epoch),
3428fe5da05eSIlya Dryomov 	     le64_to_cpu(m.replay_version.version), m.user_version);
3429fe5da05eSIlya Dryomov 
3430fe5da05eSIlya Dryomov 	if (m.retry_attempt >= 0) {
3431fe5da05eSIlya Dryomov 		if (m.retry_attempt != req->r_attempts - 1) {
3432fe5da05eSIlya Dryomov 			dout("req %p tid %llu retry_attempt %d != %d, ignoring\n",
3433fe5da05eSIlya Dryomov 			     req, req->r_tid, m.retry_attempt,
3434fe5da05eSIlya Dryomov 			     req->r_attempts - 1);
34355aea3dcdSIlya Dryomov 			goto out_unlock_session;
3436fe5da05eSIlya Dryomov 		}
3437fe5da05eSIlya Dryomov 	} else {
3438fe5da05eSIlya Dryomov 		WARN_ON(1); /* MOSDOpReply v4 is assumed */
3439fe5da05eSIlya Dryomov 	}
3440fe5da05eSIlya Dryomov 
3441fe5da05eSIlya Dryomov 	if (!ceph_oloc_empty(&m.redirect.oloc)) {
3442fe5da05eSIlya Dryomov 		dout("req %p tid %llu redirect pool %lld\n", req, req->r_tid,
3443fe5da05eSIlya Dryomov 		     m.redirect.oloc.pool);
34445aea3dcdSIlya Dryomov 		unlink_request(osd, req);
34455aea3dcdSIlya Dryomov 		mutex_unlock(&osd->lock);
3446205ee118SIlya Dryomov 
344730c156d9SYan, Zheng 		/*
344830c156d9SYan, Zheng 		 * Not ceph_oloc_copy() - changing pool_ns is not
344930c156d9SYan, Zheng 		 * supported.
345030c156d9SYan, Zheng 		 */
345130c156d9SYan, Zheng 		req->r_t.target_oloc.pool = m.redirect.oloc.pool;
34525aea3dcdSIlya Dryomov 		req->r_flags |= CEPH_OSD_FLAG_REDIRECTED;
34535aea3dcdSIlya Dryomov 		req->r_tid = 0;
34545aea3dcdSIlya Dryomov 		__submit_request(req, false);
34555aea3dcdSIlya Dryomov 		goto out_unlock_osdc;
3456205ee118SIlya Dryomov 	}
3457205ee118SIlya Dryomov 
3458fe5da05eSIlya Dryomov 	if (m.num_ops != req->r_num_ops) {
3459fe5da05eSIlya Dryomov 		pr_err("num_ops %d != %d for tid %llu\n", m.num_ops,
3460fe5da05eSIlya Dryomov 		       req->r_num_ops, req->r_tid);
3461fe5da05eSIlya Dryomov 		goto fail_request;
3462fe5da05eSIlya Dryomov 	}
3463fe5da05eSIlya Dryomov 	for (i = 0; i < req->r_num_ops; i++) {
3464fe5da05eSIlya Dryomov 		dout(" req %p tid %llu op %d rval %d len %u\n", req,
3465fe5da05eSIlya Dryomov 		     req->r_tid, i, m.rval[i], m.outdata_len[i]);
3466fe5da05eSIlya Dryomov 		req->r_ops[i].rval = m.rval[i];
3467fe5da05eSIlya Dryomov 		req->r_ops[i].outdata_len = m.outdata_len[i];
3468fe5da05eSIlya Dryomov 		data_len += m.outdata_len[i];
3469fe5da05eSIlya Dryomov 	}
3470fe5da05eSIlya Dryomov 	if (data_len != le32_to_cpu(msg->hdr.data_len)) {
3471fe5da05eSIlya Dryomov 		pr_err("sum of lens %u != %u for tid %llu\n", data_len,
3472fe5da05eSIlya Dryomov 		       le32_to_cpu(msg->hdr.data_len), req->r_tid);
3473fe5da05eSIlya Dryomov 		goto fail_request;
3474fe5da05eSIlya Dryomov 	}
3475b18b9550SIlya Dryomov 	dout("%s req %p tid %llu result %d data_len %u\n", __func__,
3476b18b9550SIlya Dryomov 	     req, req->r_tid, m.result, data_len);
34773d14c5d2SYehuda Sadeh 
3478b18b9550SIlya Dryomov 	/*
3479b18b9550SIlya Dryomov 	 * Since we only ever request ONDISK, we should only ever get
3480b18b9550SIlya Dryomov 	 * one (type of) reply back.
3481b18b9550SIlya Dryomov 	 */
3482b18b9550SIlya Dryomov 	WARN_ON(!(m.flags & CEPH_OSD_FLAG_ONDISK));
3483fe5da05eSIlya Dryomov 	req->r_result = m.result ?: data_len;
348445ee2c1dSIlya Dryomov 	finish_request(req);
34855aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
34865aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
34873d14c5d2SYehuda Sadeh 
3488fe5da05eSIlya Dryomov 	__complete_request(req);
3489b18b9550SIlya Dryomov 	complete_all(&req->r_completion);
3490dc045a91SIlya Dryomov 	ceph_osdc_put_request(req);
34913d14c5d2SYehuda Sadeh 	return;
3492fe5da05eSIlya Dryomov 
3493fe5da05eSIlya Dryomov fail_request:
34944609245eSIlya Dryomov 	complete_request(req, -EIO);
34955aea3dcdSIlya Dryomov out_unlock_session:
34965aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
34975aea3dcdSIlya Dryomov out_unlock_osdc:
34985aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
34993d14c5d2SYehuda Sadeh }
35003d14c5d2SYehuda Sadeh 
350142c1b124SIlya Dryomov static void set_pool_was_full(struct ceph_osd_client *osdc)
350242c1b124SIlya Dryomov {
350342c1b124SIlya Dryomov 	struct rb_node *n;
350442c1b124SIlya Dryomov 
350542c1b124SIlya Dryomov 	for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
350642c1b124SIlya Dryomov 		struct ceph_pg_pool_info *pi =
350742c1b124SIlya Dryomov 		    rb_entry(n, struct ceph_pg_pool_info, node);
350842c1b124SIlya Dryomov 
350942c1b124SIlya Dryomov 		pi->was_full = __pool_full(pi);
351042c1b124SIlya Dryomov 	}
351142c1b124SIlya Dryomov }
351242c1b124SIlya Dryomov 
35135aea3dcdSIlya Dryomov static bool pool_cleared_full(struct ceph_osd_client *osdc, s64 pool_id)
35143d14c5d2SYehuda Sadeh {
35155aea3dcdSIlya Dryomov 	struct ceph_pg_pool_info *pi;
35163d14c5d2SYehuda Sadeh 
35175aea3dcdSIlya Dryomov 	pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
35185aea3dcdSIlya Dryomov 	if (!pi)
35195aea3dcdSIlya Dryomov 		return false;
35203d14c5d2SYehuda Sadeh 
35215aea3dcdSIlya Dryomov 	return pi->was_full && !__pool_full(pi);
35223d14c5d2SYehuda Sadeh }
35233d14c5d2SYehuda Sadeh 
3524922dab61SIlya Dryomov static enum calc_target_result
3525922dab61SIlya Dryomov recalc_linger_target(struct ceph_osd_linger_request *lreq)
3526922dab61SIlya Dryomov {
3527922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
3528922dab61SIlya Dryomov 	enum calc_target_result ct_res;
3529922dab61SIlya Dryomov 
35307de030d6SIlya Dryomov 	ct_res = calc_target(osdc, &lreq->t, NULL, true);
3531922dab61SIlya Dryomov 	if (ct_res == CALC_TARGET_NEED_RESEND) {
3532922dab61SIlya Dryomov 		struct ceph_osd *osd;
3533922dab61SIlya Dryomov 
3534922dab61SIlya Dryomov 		osd = lookup_create_osd(osdc, lreq->t.osd, true);
3535922dab61SIlya Dryomov 		if (osd != lreq->osd) {
3536922dab61SIlya Dryomov 			unlink_linger(lreq->osd, lreq);
3537922dab61SIlya Dryomov 			link_linger(osd, lreq);
3538922dab61SIlya Dryomov 		}
3539922dab61SIlya Dryomov 	}
3540922dab61SIlya Dryomov 
3541922dab61SIlya Dryomov 	return ct_res;
3542922dab61SIlya Dryomov }
3543922dab61SIlya Dryomov 
35443d14c5d2SYehuda Sadeh /*
35455aea3dcdSIlya Dryomov  * Requeue requests whose mapping to an OSD has changed.
35463d14c5d2SYehuda Sadeh  */
35475aea3dcdSIlya Dryomov static void scan_requests(struct ceph_osd *osd,
35485aea3dcdSIlya Dryomov 			  bool force_resend,
35495aea3dcdSIlya Dryomov 			  bool cleared_full,
35505aea3dcdSIlya Dryomov 			  bool check_pool_cleared_full,
35515aea3dcdSIlya Dryomov 			  struct rb_root *need_resend,
35525aea3dcdSIlya Dryomov 			  struct list_head *need_resend_linger)
35533d14c5d2SYehuda Sadeh {
35545aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
35555aea3dcdSIlya Dryomov 	struct rb_node *n;
35565aea3dcdSIlya Dryomov 	bool force_resend_writes;
35573d14c5d2SYehuda Sadeh 
3558922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_linger_requests); n; ) {
3559922dab61SIlya Dryomov 		struct ceph_osd_linger_request *lreq =
3560922dab61SIlya Dryomov 		    rb_entry(n, struct ceph_osd_linger_request, node);
3561922dab61SIlya Dryomov 		enum calc_target_result ct_res;
3562922dab61SIlya Dryomov 
3563922dab61SIlya Dryomov 		n = rb_next(n); /* recalc_linger_target() */
3564922dab61SIlya Dryomov 
3565922dab61SIlya Dryomov 		dout("%s lreq %p linger_id %llu\n", __func__, lreq,
3566922dab61SIlya Dryomov 		     lreq->linger_id);
3567922dab61SIlya Dryomov 		ct_res = recalc_linger_target(lreq);
3568922dab61SIlya Dryomov 		switch (ct_res) {
3569922dab61SIlya Dryomov 		case CALC_TARGET_NO_ACTION:
3570922dab61SIlya Dryomov 			force_resend_writes = cleared_full ||
3571922dab61SIlya Dryomov 			    (check_pool_cleared_full &&
3572922dab61SIlya Dryomov 			     pool_cleared_full(osdc, lreq->t.base_oloc.pool));
3573922dab61SIlya Dryomov 			if (!force_resend && !force_resend_writes)
3574922dab61SIlya Dryomov 				break;
3575922dab61SIlya Dryomov 
3576922dab61SIlya Dryomov 			/* fall through */
3577922dab61SIlya Dryomov 		case CALC_TARGET_NEED_RESEND:
35784609245eSIlya Dryomov 			cancel_linger_map_check(lreq);
3579922dab61SIlya Dryomov 			/*
3580922dab61SIlya Dryomov 			 * scan_requests() for the previous epoch(s)
3581922dab61SIlya Dryomov 			 * may have already added it to the list, since
3582922dab61SIlya Dryomov 			 * it's not unlinked here.
3583922dab61SIlya Dryomov 			 */
3584922dab61SIlya Dryomov 			if (list_empty(&lreq->scan_item))
3585922dab61SIlya Dryomov 				list_add_tail(&lreq->scan_item, need_resend_linger);
3586922dab61SIlya Dryomov 			break;
3587922dab61SIlya Dryomov 		case CALC_TARGET_POOL_DNE:
3588a10bcb19SIlya Dryomov 			list_del_init(&lreq->scan_item);
35894609245eSIlya Dryomov 			check_linger_pool_dne(lreq);
3590922dab61SIlya Dryomov 			break;
3591922dab61SIlya Dryomov 		}
3592922dab61SIlya Dryomov 	}
3593922dab61SIlya Dryomov 
35945aea3dcdSIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; ) {
35955aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
35965aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
35975aea3dcdSIlya Dryomov 		enum calc_target_result ct_res;
3598ab60b16dSAlex Elder 
35994609245eSIlya Dryomov 		n = rb_next(n); /* unlink_request(), check_pool_dne() */
3600ab60b16dSAlex Elder 
36015aea3dcdSIlya Dryomov 		dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
36027de030d6SIlya Dryomov 		ct_res = calc_target(osdc, &req->r_t, &req->r_osd->o_con,
36037de030d6SIlya Dryomov 				     false);
36045aea3dcdSIlya Dryomov 		switch (ct_res) {
36055aea3dcdSIlya Dryomov 		case CALC_TARGET_NO_ACTION:
36065aea3dcdSIlya Dryomov 			force_resend_writes = cleared_full ||
36075aea3dcdSIlya Dryomov 			    (check_pool_cleared_full &&
36085aea3dcdSIlya Dryomov 			     pool_cleared_full(osdc, req->r_t.base_oloc.pool));
36095aea3dcdSIlya Dryomov 			if (!force_resend &&
36105aea3dcdSIlya Dryomov 			    (!(req->r_flags & CEPH_OSD_FLAG_WRITE) ||
36115aea3dcdSIlya Dryomov 			     !force_resend_writes))
36125aea3dcdSIlya Dryomov 				break;
3613a40c4f10SYehuda Sadeh 
36145aea3dcdSIlya Dryomov 			/* fall through */
36155aea3dcdSIlya Dryomov 		case CALC_TARGET_NEED_RESEND:
36164609245eSIlya Dryomov 			cancel_map_check(req);
36175aea3dcdSIlya Dryomov 			unlink_request(osd, req);
36185aea3dcdSIlya Dryomov 			insert_request(need_resend, req);
36195aea3dcdSIlya Dryomov 			break;
36205aea3dcdSIlya Dryomov 		case CALC_TARGET_POOL_DNE:
36214609245eSIlya Dryomov 			check_pool_dne(req);
36225aea3dcdSIlya Dryomov 			break;
3623a40c4f10SYehuda Sadeh 		}
36243d14c5d2SYehuda Sadeh 	}
36253d14c5d2SYehuda Sadeh }
36266f6c7006SSage Weil 
362742c1b124SIlya Dryomov static int handle_one_map(struct ceph_osd_client *osdc,
36285aea3dcdSIlya Dryomov 			  void *p, void *end, bool incremental,
36295aea3dcdSIlya Dryomov 			  struct rb_root *need_resend,
36305aea3dcdSIlya Dryomov 			  struct list_head *need_resend_linger)
363142c1b124SIlya Dryomov {
363242c1b124SIlya Dryomov 	struct ceph_osdmap *newmap;
363342c1b124SIlya Dryomov 	struct rb_node *n;
363442c1b124SIlya Dryomov 	bool skipped_map = false;
363542c1b124SIlya Dryomov 	bool was_full;
363642c1b124SIlya Dryomov 
3637b7ec35b3SIlya Dryomov 	was_full = ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
363842c1b124SIlya Dryomov 	set_pool_was_full(osdc);
363942c1b124SIlya Dryomov 
364042c1b124SIlya Dryomov 	if (incremental)
364142c1b124SIlya Dryomov 		newmap = osdmap_apply_incremental(&p, end, osdc->osdmap);
364242c1b124SIlya Dryomov 	else
364342c1b124SIlya Dryomov 		newmap = ceph_osdmap_decode(&p, end);
364442c1b124SIlya Dryomov 	if (IS_ERR(newmap))
364542c1b124SIlya Dryomov 		return PTR_ERR(newmap);
364642c1b124SIlya Dryomov 
364742c1b124SIlya Dryomov 	if (newmap != osdc->osdmap) {
364842c1b124SIlya Dryomov 		/*
364942c1b124SIlya Dryomov 		 * Preserve ->was_full before destroying the old map.
365042c1b124SIlya Dryomov 		 * For pools that weren't in the old map, ->was_full
365142c1b124SIlya Dryomov 		 * should be false.
365242c1b124SIlya Dryomov 		 */
365342c1b124SIlya Dryomov 		for (n = rb_first(&newmap->pg_pools); n; n = rb_next(n)) {
365442c1b124SIlya Dryomov 			struct ceph_pg_pool_info *pi =
365542c1b124SIlya Dryomov 			    rb_entry(n, struct ceph_pg_pool_info, node);
365642c1b124SIlya Dryomov 			struct ceph_pg_pool_info *old_pi;
365742c1b124SIlya Dryomov 
365842c1b124SIlya Dryomov 			old_pi = ceph_pg_pool_by_id(osdc->osdmap, pi->id);
365942c1b124SIlya Dryomov 			if (old_pi)
366042c1b124SIlya Dryomov 				pi->was_full = old_pi->was_full;
366142c1b124SIlya Dryomov 			else
366242c1b124SIlya Dryomov 				WARN_ON(pi->was_full);
366342c1b124SIlya Dryomov 		}
366442c1b124SIlya Dryomov 
366542c1b124SIlya Dryomov 		if (osdc->osdmap->epoch &&
366642c1b124SIlya Dryomov 		    osdc->osdmap->epoch + 1 < newmap->epoch) {
366742c1b124SIlya Dryomov 			WARN_ON(incremental);
366842c1b124SIlya Dryomov 			skipped_map = true;
366942c1b124SIlya Dryomov 		}
367042c1b124SIlya Dryomov 
367142c1b124SIlya Dryomov 		ceph_osdmap_destroy(osdc->osdmap);
367242c1b124SIlya Dryomov 		osdc->osdmap = newmap;
367342c1b124SIlya Dryomov 	}
367442c1b124SIlya Dryomov 
3675b7ec35b3SIlya Dryomov 	was_full &= !ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
36765aea3dcdSIlya Dryomov 	scan_requests(&osdc->homeless_osd, skipped_map, was_full, true,
36775aea3dcdSIlya Dryomov 		      need_resend, need_resend_linger);
36785aea3dcdSIlya Dryomov 
36795aea3dcdSIlya Dryomov 	for (n = rb_first(&osdc->osds); n; ) {
36805aea3dcdSIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
36815aea3dcdSIlya Dryomov 
36825aea3dcdSIlya Dryomov 		n = rb_next(n); /* close_osd() */
36835aea3dcdSIlya Dryomov 
36845aea3dcdSIlya Dryomov 		scan_requests(osd, skipped_map, was_full, true, need_resend,
36855aea3dcdSIlya Dryomov 			      need_resend_linger);
36865aea3dcdSIlya Dryomov 		if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
36875aea3dcdSIlya Dryomov 		    memcmp(&osd->o_con.peer_addr,
36885aea3dcdSIlya Dryomov 			   ceph_osd_addr(osdc->osdmap, osd->o_osd),
36895aea3dcdSIlya Dryomov 			   sizeof(struct ceph_entity_addr)))
36905aea3dcdSIlya Dryomov 			close_osd(osd);
36915aea3dcdSIlya Dryomov 	}
369242c1b124SIlya Dryomov 
369342c1b124SIlya Dryomov 	return 0;
369442c1b124SIlya Dryomov }
36956f6c7006SSage Weil 
36965aea3dcdSIlya Dryomov static void kick_requests(struct ceph_osd_client *osdc,
36975aea3dcdSIlya Dryomov 			  struct rb_root *need_resend,
36985aea3dcdSIlya Dryomov 			  struct list_head *need_resend_linger)
36995aea3dcdSIlya Dryomov {
3700922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq, *nlreq;
370104c7d789SIlya Dryomov 	enum calc_target_result ct_res;
37025aea3dcdSIlya Dryomov 	struct rb_node *n;
37035aea3dcdSIlya Dryomov 
370404c7d789SIlya Dryomov 	/* make sure need_resend targets reflect latest map */
370504c7d789SIlya Dryomov 	for (n = rb_first(need_resend); n; ) {
370604c7d789SIlya Dryomov 		struct ceph_osd_request *req =
370704c7d789SIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
370804c7d789SIlya Dryomov 
370904c7d789SIlya Dryomov 		n = rb_next(n);
371004c7d789SIlya Dryomov 
371104c7d789SIlya Dryomov 		if (req->r_t.epoch < osdc->osdmap->epoch) {
371204c7d789SIlya Dryomov 			ct_res = calc_target(osdc, &req->r_t, NULL, false);
371304c7d789SIlya Dryomov 			if (ct_res == CALC_TARGET_POOL_DNE) {
371404c7d789SIlya Dryomov 				erase_request(need_resend, req);
371504c7d789SIlya Dryomov 				check_pool_dne(req);
371604c7d789SIlya Dryomov 			}
371704c7d789SIlya Dryomov 		}
371804c7d789SIlya Dryomov 	}
371904c7d789SIlya Dryomov 
37205aea3dcdSIlya Dryomov 	for (n = rb_first(need_resend); n; ) {
37215aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
37225aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
37235aea3dcdSIlya Dryomov 		struct ceph_osd *osd;
37245aea3dcdSIlya Dryomov 
37255aea3dcdSIlya Dryomov 		n = rb_next(n);
37265aea3dcdSIlya Dryomov 		erase_request(need_resend, req); /* before link_request() */
37275aea3dcdSIlya Dryomov 
37285aea3dcdSIlya Dryomov 		osd = lookup_create_osd(osdc, req->r_t.osd, true);
37295aea3dcdSIlya Dryomov 		link_request(osd, req);
37305aea3dcdSIlya Dryomov 		if (!req->r_linger) {
37315aea3dcdSIlya Dryomov 			if (!osd_homeless(osd) && !req->r_t.paused)
37325aea3dcdSIlya Dryomov 				send_request(req);
3733922dab61SIlya Dryomov 		} else {
3734922dab61SIlya Dryomov 			cancel_linger_request(req);
37355aea3dcdSIlya Dryomov 		}
37365aea3dcdSIlya Dryomov 	}
3737922dab61SIlya Dryomov 
3738922dab61SIlya Dryomov 	list_for_each_entry_safe(lreq, nlreq, need_resend_linger, scan_item) {
3739922dab61SIlya Dryomov 		if (!osd_homeless(lreq->osd))
3740922dab61SIlya Dryomov 			send_linger(lreq);
3741922dab61SIlya Dryomov 
3742922dab61SIlya Dryomov 		list_del_init(&lreq->scan_item);
3743922dab61SIlya Dryomov 	}
37445aea3dcdSIlya Dryomov }
37455aea3dcdSIlya Dryomov 
37463d14c5d2SYehuda Sadeh /*
37473d14c5d2SYehuda Sadeh  * Process updated osd map.
37483d14c5d2SYehuda Sadeh  *
37493d14c5d2SYehuda Sadeh  * The message contains any number of incremental and full maps, normally
37503d14c5d2SYehuda Sadeh  * indicating some sort of topology change in the cluster.  Kick requests
37513d14c5d2SYehuda Sadeh  * off to different OSDs as needed.
37523d14c5d2SYehuda Sadeh  */
37533d14c5d2SYehuda Sadeh void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
37543d14c5d2SYehuda Sadeh {
375542c1b124SIlya Dryomov 	void *p = msg->front.iov_base;
375642c1b124SIlya Dryomov 	void *const end = p + msg->front.iov_len;
37573d14c5d2SYehuda Sadeh 	u32 nr_maps, maplen;
37583d14c5d2SYehuda Sadeh 	u32 epoch;
37593d14c5d2SYehuda Sadeh 	struct ceph_fsid fsid;
37605aea3dcdSIlya Dryomov 	struct rb_root need_resend = RB_ROOT;
37615aea3dcdSIlya Dryomov 	LIST_HEAD(need_resend_linger);
376242c1b124SIlya Dryomov 	bool handled_incremental = false;
376342c1b124SIlya Dryomov 	bool was_pauserd, was_pausewr;
376442c1b124SIlya Dryomov 	bool pauserd, pausewr;
376542c1b124SIlya Dryomov 	int err;
37663d14c5d2SYehuda Sadeh 
376742c1b124SIlya Dryomov 	dout("%s have %u\n", __func__, osdc->osdmap->epoch);
37685aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
37693d14c5d2SYehuda Sadeh 
37703d14c5d2SYehuda Sadeh 	/* verify fsid */
37713d14c5d2SYehuda Sadeh 	ceph_decode_need(&p, end, sizeof(fsid), bad);
37723d14c5d2SYehuda Sadeh 	ceph_decode_copy(&p, &fsid, sizeof(fsid));
37733d14c5d2SYehuda Sadeh 	if (ceph_check_fsid(osdc->client, &fsid) < 0)
377442c1b124SIlya Dryomov 		goto bad;
37753d14c5d2SYehuda Sadeh 
3776b7ec35b3SIlya Dryomov 	was_pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3777b7ec35b3SIlya Dryomov 	was_pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3778b7ec35b3SIlya Dryomov 		      ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
377942c1b124SIlya Dryomov 		      have_pool_full(osdc);
37809a1ea2dbSJosh Durgin 
37813d14c5d2SYehuda Sadeh 	/* incremental maps */
37823d14c5d2SYehuda Sadeh 	ceph_decode_32_safe(&p, end, nr_maps, bad);
37833d14c5d2SYehuda Sadeh 	dout(" %d inc maps\n", nr_maps);
37843d14c5d2SYehuda Sadeh 	while (nr_maps > 0) {
37853d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, 2*sizeof(u32), bad);
37863d14c5d2SYehuda Sadeh 		epoch = ceph_decode_32(&p);
37873d14c5d2SYehuda Sadeh 		maplen = ceph_decode_32(&p);
37883d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, maplen, bad);
378942c1b124SIlya Dryomov 		if (osdc->osdmap->epoch &&
379042c1b124SIlya Dryomov 		    osdc->osdmap->epoch + 1 == epoch) {
37913d14c5d2SYehuda Sadeh 			dout("applying incremental map %u len %d\n",
37923d14c5d2SYehuda Sadeh 			     epoch, maplen);
37935aea3dcdSIlya Dryomov 			err = handle_one_map(osdc, p, p + maplen, true,
37945aea3dcdSIlya Dryomov 					     &need_resend, &need_resend_linger);
379542c1b124SIlya Dryomov 			if (err)
37963d14c5d2SYehuda Sadeh 				goto bad;
379742c1b124SIlya Dryomov 			handled_incremental = true;
37983d14c5d2SYehuda Sadeh 		} else {
37993d14c5d2SYehuda Sadeh 			dout("ignoring incremental map %u len %d\n",
38003d14c5d2SYehuda Sadeh 			     epoch, maplen);
38013d14c5d2SYehuda Sadeh 		}
380242c1b124SIlya Dryomov 		p += maplen;
38033d14c5d2SYehuda Sadeh 		nr_maps--;
38043d14c5d2SYehuda Sadeh 	}
380542c1b124SIlya Dryomov 	if (handled_incremental)
38063d14c5d2SYehuda Sadeh 		goto done;
38073d14c5d2SYehuda Sadeh 
38083d14c5d2SYehuda Sadeh 	/* full maps */
38093d14c5d2SYehuda Sadeh 	ceph_decode_32_safe(&p, end, nr_maps, bad);
38103d14c5d2SYehuda Sadeh 	dout(" %d full maps\n", nr_maps);
38113d14c5d2SYehuda Sadeh 	while (nr_maps) {
38123d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, 2*sizeof(u32), bad);
38133d14c5d2SYehuda Sadeh 		epoch = ceph_decode_32(&p);
38143d14c5d2SYehuda Sadeh 		maplen = ceph_decode_32(&p);
38153d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, maplen, bad);
38163d14c5d2SYehuda Sadeh 		if (nr_maps > 1) {
38173d14c5d2SYehuda Sadeh 			dout("skipping non-latest full map %u len %d\n",
38183d14c5d2SYehuda Sadeh 			     epoch, maplen);
3819e5253a7bSIlya Dryomov 		} else if (osdc->osdmap->epoch >= epoch) {
38203d14c5d2SYehuda Sadeh 			dout("skipping full map %u len %d, "
38213d14c5d2SYehuda Sadeh 			     "older than our %u\n", epoch, maplen,
38223d14c5d2SYehuda Sadeh 			     osdc->osdmap->epoch);
38233d14c5d2SYehuda Sadeh 		} else {
38243d14c5d2SYehuda Sadeh 			dout("taking full map %u len %d\n", epoch, maplen);
38255aea3dcdSIlya Dryomov 			err = handle_one_map(osdc, p, p + maplen, false,
38265aea3dcdSIlya Dryomov 					     &need_resend, &need_resend_linger);
382742c1b124SIlya Dryomov 			if (err)
38283d14c5d2SYehuda Sadeh 				goto bad;
38293d14c5d2SYehuda Sadeh 		}
38303d14c5d2SYehuda Sadeh 		p += maplen;
38313d14c5d2SYehuda Sadeh 		nr_maps--;
38323d14c5d2SYehuda Sadeh 	}
38333d14c5d2SYehuda Sadeh 
38343d14c5d2SYehuda Sadeh done:
3835cd634fb6SSage Weil 	/*
3836cd634fb6SSage Weil 	 * subscribe to subsequent osdmap updates if full to ensure
3837cd634fb6SSage Weil 	 * we find out when we are no longer full and stop returning
3838cd634fb6SSage Weil 	 * ENOSPC.
3839cd634fb6SSage Weil 	 */
3840b7ec35b3SIlya Dryomov 	pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3841b7ec35b3SIlya Dryomov 	pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3842b7ec35b3SIlya Dryomov 		  ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
384342c1b124SIlya Dryomov 		  have_pool_full(osdc);
384458eb7932SJeff Layton 	if (was_pauserd || was_pausewr || pauserd || pausewr ||
384558eb7932SJeff Layton 	    osdc->osdmap->epoch < osdc->epoch_barrier)
384642c1b124SIlya Dryomov 		maybe_request_map(osdc);
3847cd634fb6SSage Weil 
38485aea3dcdSIlya Dryomov 	kick_requests(osdc, &need_resend, &need_resend_linger);
384942c1b124SIlya Dryomov 
3850fc36d0a4SJeff Layton 	ceph_osdc_abort_on_full(osdc);
385142c1b124SIlya Dryomov 	ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
385242c1b124SIlya Dryomov 			  osdc->osdmap->epoch);
38535aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
38543d14c5d2SYehuda Sadeh 	wake_up_all(&osdc->client->auth_wq);
38553d14c5d2SYehuda Sadeh 	return;
38563d14c5d2SYehuda Sadeh 
38573d14c5d2SYehuda Sadeh bad:
38583d14c5d2SYehuda Sadeh 	pr_err("osdc handle_map corrupt msg\n");
38593d14c5d2SYehuda Sadeh 	ceph_msg_dump(msg);
38605aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
38615aea3dcdSIlya Dryomov }
38625aea3dcdSIlya Dryomov 
38635aea3dcdSIlya Dryomov /*
38645aea3dcdSIlya Dryomov  * Resubmit requests pending on the given osd.
38655aea3dcdSIlya Dryomov  */
38665aea3dcdSIlya Dryomov static void kick_osd_requests(struct ceph_osd *osd)
38675aea3dcdSIlya Dryomov {
38685aea3dcdSIlya Dryomov 	struct rb_node *n;
38695aea3dcdSIlya Dryomov 
3870a02a946dSIlya Dryomov 	clear_backoffs(osd);
3871a02a946dSIlya Dryomov 
3872922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; ) {
38735aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
38745aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
38755aea3dcdSIlya Dryomov 
3876922dab61SIlya Dryomov 		n = rb_next(n); /* cancel_linger_request() */
3877922dab61SIlya Dryomov 
38785aea3dcdSIlya Dryomov 		if (!req->r_linger) {
38795aea3dcdSIlya Dryomov 			if (!req->r_t.paused)
38805aea3dcdSIlya Dryomov 				send_request(req);
3881922dab61SIlya Dryomov 		} else {
3882922dab61SIlya Dryomov 			cancel_linger_request(req);
38835aea3dcdSIlya Dryomov 		}
38845aea3dcdSIlya Dryomov 	}
3885922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_linger_requests); n; n = rb_next(n)) {
3886922dab61SIlya Dryomov 		struct ceph_osd_linger_request *lreq =
3887922dab61SIlya Dryomov 		    rb_entry(n, struct ceph_osd_linger_request, node);
3888922dab61SIlya Dryomov 
3889922dab61SIlya Dryomov 		send_linger(lreq);
3890922dab61SIlya Dryomov 	}
38915aea3dcdSIlya Dryomov }
38925aea3dcdSIlya Dryomov 
38935aea3dcdSIlya Dryomov /*
38945aea3dcdSIlya Dryomov  * If the osd connection drops, we need to resubmit all requests.
38955aea3dcdSIlya Dryomov  */
38965aea3dcdSIlya Dryomov static void osd_fault(struct ceph_connection *con)
38975aea3dcdSIlya Dryomov {
38985aea3dcdSIlya Dryomov 	struct ceph_osd *osd = con->private;
38995aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
39005aea3dcdSIlya Dryomov 
39015aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
39025aea3dcdSIlya Dryomov 
39035aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
39045aea3dcdSIlya Dryomov 	if (!osd_registered(osd)) {
39055aea3dcdSIlya Dryomov 		dout("%s osd%d unknown\n", __func__, osd->o_osd);
39065aea3dcdSIlya Dryomov 		goto out_unlock;
39075aea3dcdSIlya Dryomov 	}
39085aea3dcdSIlya Dryomov 
39095aea3dcdSIlya Dryomov 	if (!reopen_osd(osd))
39105aea3dcdSIlya Dryomov 		kick_osd_requests(osd);
39115aea3dcdSIlya Dryomov 	maybe_request_map(osdc);
39125aea3dcdSIlya Dryomov 
39135aea3dcdSIlya Dryomov out_unlock:
39145aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
39153d14c5d2SYehuda Sadeh }
39163d14c5d2SYehuda Sadeh 
3917a02a946dSIlya Dryomov struct MOSDBackoff {
3918a02a946dSIlya Dryomov 	struct ceph_spg spgid;
3919a02a946dSIlya Dryomov 	u32 map_epoch;
3920a02a946dSIlya Dryomov 	u8 op;
3921a02a946dSIlya Dryomov 	u64 id;
3922a02a946dSIlya Dryomov 	struct ceph_hobject_id *begin;
3923a02a946dSIlya Dryomov 	struct ceph_hobject_id *end;
3924a02a946dSIlya Dryomov };
3925a02a946dSIlya Dryomov 
3926a02a946dSIlya Dryomov static int decode_MOSDBackoff(const struct ceph_msg *msg, struct MOSDBackoff *m)
3927a02a946dSIlya Dryomov {
3928a02a946dSIlya Dryomov 	void *p = msg->front.iov_base;
3929a02a946dSIlya Dryomov 	void *const end = p + msg->front.iov_len;
3930a02a946dSIlya Dryomov 	u8 struct_v;
3931a02a946dSIlya Dryomov 	u32 struct_len;
3932a02a946dSIlya Dryomov 	int ret;
3933a02a946dSIlya Dryomov 
3934a02a946dSIlya Dryomov 	ret = ceph_start_decoding(&p, end, 1, "spg_t", &struct_v, &struct_len);
3935a02a946dSIlya Dryomov 	if (ret)
3936a02a946dSIlya Dryomov 		return ret;
3937a02a946dSIlya Dryomov 
3938a02a946dSIlya Dryomov 	ret = ceph_decode_pgid(&p, end, &m->spgid.pgid);
3939a02a946dSIlya Dryomov 	if (ret)
3940a02a946dSIlya Dryomov 		return ret;
3941a02a946dSIlya Dryomov 
3942a02a946dSIlya Dryomov 	ceph_decode_8_safe(&p, end, m->spgid.shard, e_inval);
3943a02a946dSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->map_epoch, e_inval);
3944a02a946dSIlya Dryomov 	ceph_decode_8_safe(&p, end, m->op, e_inval);
3945a02a946dSIlya Dryomov 	ceph_decode_64_safe(&p, end, m->id, e_inval);
3946a02a946dSIlya Dryomov 
3947a02a946dSIlya Dryomov 	m->begin = kzalloc(sizeof(*m->begin), GFP_NOIO);
3948a02a946dSIlya Dryomov 	if (!m->begin)
3949a02a946dSIlya Dryomov 		return -ENOMEM;
3950a02a946dSIlya Dryomov 
3951a02a946dSIlya Dryomov 	ret = decode_hoid(&p, end, m->begin);
3952a02a946dSIlya Dryomov 	if (ret) {
3953a02a946dSIlya Dryomov 		free_hoid(m->begin);
3954a02a946dSIlya Dryomov 		return ret;
3955a02a946dSIlya Dryomov 	}
3956a02a946dSIlya Dryomov 
3957a02a946dSIlya Dryomov 	m->end = kzalloc(sizeof(*m->end), GFP_NOIO);
3958a02a946dSIlya Dryomov 	if (!m->end) {
3959a02a946dSIlya Dryomov 		free_hoid(m->begin);
3960a02a946dSIlya Dryomov 		return -ENOMEM;
3961a02a946dSIlya Dryomov 	}
3962a02a946dSIlya Dryomov 
3963a02a946dSIlya Dryomov 	ret = decode_hoid(&p, end, m->end);
3964a02a946dSIlya Dryomov 	if (ret) {
3965a02a946dSIlya Dryomov 		free_hoid(m->begin);
3966a02a946dSIlya Dryomov 		free_hoid(m->end);
3967a02a946dSIlya Dryomov 		return ret;
3968a02a946dSIlya Dryomov 	}
3969a02a946dSIlya Dryomov 
3970a02a946dSIlya Dryomov 	return 0;
3971a02a946dSIlya Dryomov 
3972a02a946dSIlya Dryomov e_inval:
3973a02a946dSIlya Dryomov 	return -EINVAL;
3974a02a946dSIlya Dryomov }
3975a02a946dSIlya Dryomov 
3976a02a946dSIlya Dryomov static struct ceph_msg *create_backoff_message(
3977a02a946dSIlya Dryomov 				const struct ceph_osd_backoff *backoff,
3978a02a946dSIlya Dryomov 				u32 map_epoch)
3979a02a946dSIlya Dryomov {
3980a02a946dSIlya Dryomov 	struct ceph_msg *msg;
3981a02a946dSIlya Dryomov 	void *p, *end;
3982a02a946dSIlya Dryomov 	int msg_size;
3983a02a946dSIlya Dryomov 
3984a02a946dSIlya Dryomov 	msg_size = CEPH_ENCODING_START_BLK_LEN +
3985a02a946dSIlya Dryomov 			CEPH_PGID_ENCODING_LEN + 1; /* spgid */
3986a02a946dSIlya Dryomov 	msg_size += 4 + 1 + 8; /* map_epoch, op, id */
3987a02a946dSIlya Dryomov 	msg_size += CEPH_ENCODING_START_BLK_LEN +
3988a02a946dSIlya Dryomov 			hoid_encoding_size(backoff->begin);
3989a02a946dSIlya Dryomov 	msg_size += CEPH_ENCODING_START_BLK_LEN +
3990a02a946dSIlya Dryomov 			hoid_encoding_size(backoff->end);
3991a02a946dSIlya Dryomov 
3992a02a946dSIlya Dryomov 	msg = ceph_msg_new(CEPH_MSG_OSD_BACKOFF, msg_size, GFP_NOIO, true);
3993a02a946dSIlya Dryomov 	if (!msg)
3994a02a946dSIlya Dryomov 		return NULL;
3995a02a946dSIlya Dryomov 
3996a02a946dSIlya Dryomov 	p = msg->front.iov_base;
3997a02a946dSIlya Dryomov 	end = p + msg->front_alloc_len;
3998a02a946dSIlya Dryomov 
3999a02a946dSIlya Dryomov 	encode_spgid(&p, &backoff->spgid);
4000a02a946dSIlya Dryomov 	ceph_encode_32(&p, map_epoch);
4001a02a946dSIlya Dryomov 	ceph_encode_8(&p, CEPH_OSD_BACKOFF_OP_ACK_BLOCK);
4002a02a946dSIlya Dryomov 	ceph_encode_64(&p, backoff->id);
4003a02a946dSIlya Dryomov 	encode_hoid(&p, end, backoff->begin);
4004a02a946dSIlya Dryomov 	encode_hoid(&p, end, backoff->end);
4005a02a946dSIlya Dryomov 	BUG_ON(p != end);
4006a02a946dSIlya Dryomov 
4007a02a946dSIlya Dryomov 	msg->front.iov_len = p - msg->front.iov_base;
4008a02a946dSIlya Dryomov 	msg->hdr.version = cpu_to_le16(1); /* MOSDBackoff v1 */
4009a02a946dSIlya Dryomov 	msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
4010a02a946dSIlya Dryomov 
4011a02a946dSIlya Dryomov 	return msg;
4012a02a946dSIlya Dryomov }
4013a02a946dSIlya Dryomov 
4014a02a946dSIlya Dryomov static void handle_backoff_block(struct ceph_osd *osd, struct MOSDBackoff *m)
4015a02a946dSIlya Dryomov {
4016a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
4017a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
4018a02a946dSIlya Dryomov 	struct ceph_msg *msg;
4019a02a946dSIlya Dryomov 
4020a02a946dSIlya Dryomov 	dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4021a02a946dSIlya Dryomov 	     m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4022a02a946dSIlya Dryomov 
4023a02a946dSIlya Dryomov 	spg = lookup_spg_mapping(&osd->o_backoff_mappings, &m->spgid);
4024a02a946dSIlya Dryomov 	if (!spg) {
4025a02a946dSIlya Dryomov 		spg = alloc_spg_mapping();
4026a02a946dSIlya Dryomov 		if (!spg) {
4027a02a946dSIlya Dryomov 			pr_err("%s failed to allocate spg\n", __func__);
4028a02a946dSIlya Dryomov 			return;
4029a02a946dSIlya Dryomov 		}
4030a02a946dSIlya Dryomov 		spg->spgid = m->spgid; /* struct */
4031a02a946dSIlya Dryomov 		insert_spg_mapping(&osd->o_backoff_mappings, spg);
4032a02a946dSIlya Dryomov 	}
4033a02a946dSIlya Dryomov 
4034a02a946dSIlya Dryomov 	backoff = alloc_backoff();
4035a02a946dSIlya Dryomov 	if (!backoff) {
4036a02a946dSIlya Dryomov 		pr_err("%s failed to allocate backoff\n", __func__);
4037a02a946dSIlya Dryomov 		return;
4038a02a946dSIlya Dryomov 	}
4039a02a946dSIlya Dryomov 	backoff->spgid = m->spgid; /* struct */
4040a02a946dSIlya Dryomov 	backoff->id = m->id;
4041a02a946dSIlya Dryomov 	backoff->begin = m->begin;
4042a02a946dSIlya Dryomov 	m->begin = NULL; /* backoff now owns this */
4043a02a946dSIlya Dryomov 	backoff->end = m->end;
4044a02a946dSIlya Dryomov 	m->end = NULL;   /* ditto */
4045a02a946dSIlya Dryomov 
4046a02a946dSIlya Dryomov 	insert_backoff(&spg->backoffs, backoff);
4047a02a946dSIlya Dryomov 	insert_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4048a02a946dSIlya Dryomov 
4049a02a946dSIlya Dryomov 	/*
4050a02a946dSIlya Dryomov 	 * Ack with original backoff's epoch so that the OSD can
4051a02a946dSIlya Dryomov 	 * discard this if there was a PG split.
4052a02a946dSIlya Dryomov 	 */
4053a02a946dSIlya Dryomov 	msg = create_backoff_message(backoff, m->map_epoch);
4054a02a946dSIlya Dryomov 	if (!msg) {
4055a02a946dSIlya Dryomov 		pr_err("%s failed to allocate msg\n", __func__);
4056a02a946dSIlya Dryomov 		return;
4057a02a946dSIlya Dryomov 	}
4058a02a946dSIlya Dryomov 	ceph_con_send(&osd->o_con, msg);
4059a02a946dSIlya Dryomov }
4060a02a946dSIlya Dryomov 
4061a02a946dSIlya Dryomov static bool target_contained_by(const struct ceph_osd_request_target *t,
4062a02a946dSIlya Dryomov 				const struct ceph_hobject_id *begin,
4063a02a946dSIlya Dryomov 				const struct ceph_hobject_id *end)
4064a02a946dSIlya Dryomov {
4065a02a946dSIlya Dryomov 	struct ceph_hobject_id hoid;
4066a02a946dSIlya Dryomov 	int cmp;
4067a02a946dSIlya Dryomov 
4068a02a946dSIlya Dryomov 	hoid_fill_from_target(&hoid, t);
4069a02a946dSIlya Dryomov 	cmp = hoid_compare(&hoid, begin);
4070a02a946dSIlya Dryomov 	return !cmp || (cmp > 0 && hoid_compare(&hoid, end) < 0);
4071a02a946dSIlya Dryomov }
4072a02a946dSIlya Dryomov 
4073a02a946dSIlya Dryomov static void handle_backoff_unblock(struct ceph_osd *osd,
4074a02a946dSIlya Dryomov 				   const struct MOSDBackoff *m)
4075a02a946dSIlya Dryomov {
4076a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
4077a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
4078a02a946dSIlya Dryomov 	struct rb_node *n;
4079a02a946dSIlya Dryomov 
4080a02a946dSIlya Dryomov 	dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4081a02a946dSIlya Dryomov 	     m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4082a02a946dSIlya Dryomov 
4083a02a946dSIlya Dryomov 	backoff = lookup_backoff_by_id(&osd->o_backoffs_by_id, m->id);
4084a02a946dSIlya Dryomov 	if (!backoff) {
4085a02a946dSIlya Dryomov 		pr_err("%s osd%d spgid %llu.%xs%d id %llu backoff dne\n",
4086a02a946dSIlya Dryomov 		       __func__, osd->o_osd, m->spgid.pgid.pool,
4087a02a946dSIlya Dryomov 		       m->spgid.pgid.seed, m->spgid.shard, m->id);
4088a02a946dSIlya Dryomov 		return;
4089a02a946dSIlya Dryomov 	}
4090a02a946dSIlya Dryomov 
4091a02a946dSIlya Dryomov 	if (hoid_compare(backoff->begin, m->begin) &&
4092a02a946dSIlya Dryomov 	    hoid_compare(backoff->end, m->end)) {
4093a02a946dSIlya Dryomov 		pr_err("%s osd%d spgid %llu.%xs%d id %llu bad range?\n",
4094a02a946dSIlya Dryomov 		       __func__, osd->o_osd, m->spgid.pgid.pool,
4095a02a946dSIlya Dryomov 		       m->spgid.pgid.seed, m->spgid.shard, m->id);
4096a02a946dSIlya Dryomov 		/* unblock it anyway... */
4097a02a946dSIlya Dryomov 	}
4098a02a946dSIlya Dryomov 
4099a02a946dSIlya Dryomov 	spg = lookup_spg_mapping(&osd->o_backoff_mappings, &backoff->spgid);
4100a02a946dSIlya Dryomov 	BUG_ON(!spg);
4101a02a946dSIlya Dryomov 
4102a02a946dSIlya Dryomov 	erase_backoff(&spg->backoffs, backoff);
4103a02a946dSIlya Dryomov 	erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4104a02a946dSIlya Dryomov 	free_backoff(backoff);
4105a02a946dSIlya Dryomov 
4106a02a946dSIlya Dryomov 	if (RB_EMPTY_ROOT(&spg->backoffs)) {
4107a02a946dSIlya Dryomov 		erase_spg_mapping(&osd->o_backoff_mappings, spg);
4108a02a946dSIlya Dryomov 		free_spg_mapping(spg);
4109a02a946dSIlya Dryomov 	}
4110a02a946dSIlya Dryomov 
4111a02a946dSIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
4112a02a946dSIlya Dryomov 		struct ceph_osd_request *req =
4113a02a946dSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
4114a02a946dSIlya Dryomov 
4115a02a946dSIlya Dryomov 		if (!ceph_spg_compare(&req->r_t.spgid, &m->spgid)) {
4116a02a946dSIlya Dryomov 			/*
4117a02a946dSIlya Dryomov 			 * Match against @m, not @backoff -- the PG may
4118a02a946dSIlya Dryomov 			 * have split on the OSD.
4119a02a946dSIlya Dryomov 			 */
4120a02a946dSIlya Dryomov 			if (target_contained_by(&req->r_t, m->begin, m->end)) {
4121a02a946dSIlya Dryomov 				/*
4122a02a946dSIlya Dryomov 				 * If no other installed backoff applies,
4123a02a946dSIlya Dryomov 				 * resend.
4124a02a946dSIlya Dryomov 				 */
4125a02a946dSIlya Dryomov 				send_request(req);
4126a02a946dSIlya Dryomov 			}
4127a02a946dSIlya Dryomov 		}
4128a02a946dSIlya Dryomov 	}
4129a02a946dSIlya Dryomov }
4130a02a946dSIlya Dryomov 
4131a02a946dSIlya Dryomov static void handle_backoff(struct ceph_osd *osd, struct ceph_msg *msg)
4132a02a946dSIlya Dryomov {
4133a02a946dSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
4134a02a946dSIlya Dryomov 	struct MOSDBackoff m;
4135a02a946dSIlya Dryomov 	int ret;
4136a02a946dSIlya Dryomov 
4137a02a946dSIlya Dryomov 	down_read(&osdc->lock);
4138a02a946dSIlya Dryomov 	if (!osd_registered(osd)) {
4139a02a946dSIlya Dryomov 		dout("%s osd%d unknown\n", __func__, osd->o_osd);
4140a02a946dSIlya Dryomov 		up_read(&osdc->lock);
4141a02a946dSIlya Dryomov 		return;
4142a02a946dSIlya Dryomov 	}
4143a02a946dSIlya Dryomov 	WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
4144a02a946dSIlya Dryomov 
4145a02a946dSIlya Dryomov 	mutex_lock(&osd->lock);
4146a02a946dSIlya Dryomov 	ret = decode_MOSDBackoff(msg, &m);
4147a02a946dSIlya Dryomov 	if (ret) {
4148a02a946dSIlya Dryomov 		pr_err("failed to decode MOSDBackoff: %d\n", ret);
4149a02a946dSIlya Dryomov 		ceph_msg_dump(msg);
4150a02a946dSIlya Dryomov 		goto out_unlock;
4151a02a946dSIlya Dryomov 	}
4152a02a946dSIlya Dryomov 
4153a02a946dSIlya Dryomov 	switch (m.op) {
4154a02a946dSIlya Dryomov 	case CEPH_OSD_BACKOFF_OP_BLOCK:
4155a02a946dSIlya Dryomov 		handle_backoff_block(osd, &m);
4156a02a946dSIlya Dryomov 		break;
4157a02a946dSIlya Dryomov 	case CEPH_OSD_BACKOFF_OP_UNBLOCK:
4158a02a946dSIlya Dryomov 		handle_backoff_unblock(osd, &m);
4159a02a946dSIlya Dryomov 		break;
4160a02a946dSIlya Dryomov 	default:
4161a02a946dSIlya Dryomov 		pr_err("%s osd%d unknown op %d\n", __func__, osd->o_osd, m.op);
4162a02a946dSIlya Dryomov 	}
4163a02a946dSIlya Dryomov 
4164a02a946dSIlya Dryomov 	free_hoid(m.begin);
4165a02a946dSIlya Dryomov 	free_hoid(m.end);
4166a02a946dSIlya Dryomov 
4167a02a946dSIlya Dryomov out_unlock:
4168a02a946dSIlya Dryomov 	mutex_unlock(&osd->lock);
4169a02a946dSIlya Dryomov 	up_read(&osdc->lock);
4170a02a946dSIlya Dryomov }
4171a02a946dSIlya Dryomov 
41723d14c5d2SYehuda Sadeh /*
4173a40c4f10SYehuda Sadeh  * Process osd watch notifications
4174a40c4f10SYehuda Sadeh  */
41753c663bbdSAlex Elder static void handle_watch_notify(struct ceph_osd_client *osdc,
41763c663bbdSAlex Elder 				struct ceph_msg *msg)
4177a40c4f10SYehuda Sadeh {
4178922dab61SIlya Dryomov 	void *p = msg->front.iov_base;
4179922dab61SIlya Dryomov 	void *const end = p + msg->front.iov_len;
4180922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
4181922dab61SIlya Dryomov 	struct linger_work *lwork;
4182922dab61SIlya Dryomov 	u8 proto_ver, opcode;
4183922dab61SIlya Dryomov 	u64 cookie, notify_id;
4184922dab61SIlya Dryomov 	u64 notifier_id = 0;
418519079203SIlya Dryomov 	s32 return_code = 0;
4186922dab61SIlya Dryomov 	void *payload = NULL;
4187922dab61SIlya Dryomov 	u32 payload_len = 0;
4188a40c4f10SYehuda Sadeh 
4189a40c4f10SYehuda Sadeh 	ceph_decode_8_safe(&p, end, proto_ver, bad);
4190a40c4f10SYehuda Sadeh 	ceph_decode_8_safe(&p, end, opcode, bad);
4191a40c4f10SYehuda Sadeh 	ceph_decode_64_safe(&p, end, cookie, bad);
4192922dab61SIlya Dryomov 	p += 8; /* skip ver */
4193a40c4f10SYehuda Sadeh 	ceph_decode_64_safe(&p, end, notify_id, bad);
4194a40c4f10SYehuda Sadeh 
4195922dab61SIlya Dryomov 	if (proto_ver >= 1) {
4196922dab61SIlya Dryomov 		ceph_decode_32_safe(&p, end, payload_len, bad);
4197922dab61SIlya Dryomov 		ceph_decode_need(&p, end, payload_len, bad);
4198922dab61SIlya Dryomov 		payload = p;
4199922dab61SIlya Dryomov 		p += payload_len;
4200a40c4f10SYehuda Sadeh 	}
4201a40c4f10SYehuda Sadeh 
4202922dab61SIlya Dryomov 	if (le16_to_cpu(msg->hdr.version) >= 2)
420319079203SIlya Dryomov 		ceph_decode_32_safe(&p, end, return_code, bad);
4204922dab61SIlya Dryomov 
4205922dab61SIlya Dryomov 	if (le16_to_cpu(msg->hdr.version) >= 3)
4206922dab61SIlya Dryomov 		ceph_decode_64_safe(&p, end, notifier_id, bad);
4207922dab61SIlya Dryomov 
4208922dab61SIlya Dryomov 	down_read(&osdc->lock);
4209922dab61SIlya Dryomov 	lreq = lookup_linger_osdc(&osdc->linger_requests, cookie);
4210922dab61SIlya Dryomov 	if (!lreq) {
4211922dab61SIlya Dryomov 		dout("%s opcode %d cookie %llu dne\n", __func__, opcode,
4212922dab61SIlya Dryomov 		     cookie);
4213922dab61SIlya Dryomov 		goto out_unlock_osdc;
4214922dab61SIlya Dryomov 	}
4215922dab61SIlya Dryomov 
4216922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
421719079203SIlya Dryomov 	dout("%s opcode %d cookie %llu lreq %p is_watch %d\n", __func__,
421819079203SIlya Dryomov 	     opcode, cookie, lreq, lreq->is_watch);
4219922dab61SIlya Dryomov 	if (opcode == CEPH_WATCH_EVENT_DISCONNECT) {
4220922dab61SIlya Dryomov 		if (!lreq->last_error) {
4221922dab61SIlya Dryomov 			lreq->last_error = -ENOTCONN;
4222922dab61SIlya Dryomov 			queue_watch_error(lreq);
4223922dab61SIlya Dryomov 		}
422419079203SIlya Dryomov 	} else if (!lreq->is_watch) {
422519079203SIlya Dryomov 		/* CEPH_WATCH_EVENT_NOTIFY_COMPLETE */
422619079203SIlya Dryomov 		if (lreq->notify_id && lreq->notify_id != notify_id) {
422719079203SIlya Dryomov 			dout("lreq %p notify_id %llu != %llu, ignoring\n", lreq,
422819079203SIlya Dryomov 			     lreq->notify_id, notify_id);
422919079203SIlya Dryomov 		} else if (!completion_done(&lreq->notify_finish_wait)) {
423019079203SIlya Dryomov 			struct ceph_msg_data *data =
423119079203SIlya Dryomov 			    list_first_entry_or_null(&msg->data,
423219079203SIlya Dryomov 						     struct ceph_msg_data,
423319079203SIlya Dryomov 						     links);
423419079203SIlya Dryomov 
423519079203SIlya Dryomov 			if (data) {
423619079203SIlya Dryomov 				if (lreq->preply_pages) {
423719079203SIlya Dryomov 					WARN_ON(data->type !=
423819079203SIlya Dryomov 							CEPH_MSG_DATA_PAGES);
423919079203SIlya Dryomov 					*lreq->preply_pages = data->pages;
424019079203SIlya Dryomov 					*lreq->preply_len = data->length;
424119079203SIlya Dryomov 				} else {
424219079203SIlya Dryomov 					ceph_release_page_vector(data->pages,
424319079203SIlya Dryomov 					       calc_pages_for(0, data->length));
424419079203SIlya Dryomov 				}
424519079203SIlya Dryomov 			}
424619079203SIlya Dryomov 			lreq->notify_finish_error = return_code;
424719079203SIlya Dryomov 			complete_all(&lreq->notify_finish_wait);
424819079203SIlya Dryomov 		}
4249922dab61SIlya Dryomov 	} else {
4250922dab61SIlya Dryomov 		/* CEPH_WATCH_EVENT_NOTIFY */
4251922dab61SIlya Dryomov 		lwork = lwork_alloc(lreq, do_watch_notify);
4252922dab61SIlya Dryomov 		if (!lwork) {
4253922dab61SIlya Dryomov 			pr_err("failed to allocate notify-lwork\n");
4254922dab61SIlya Dryomov 			goto out_unlock_lreq;
4255922dab61SIlya Dryomov 		}
4256922dab61SIlya Dryomov 
4257922dab61SIlya Dryomov 		lwork->notify.notify_id = notify_id;
4258922dab61SIlya Dryomov 		lwork->notify.notifier_id = notifier_id;
4259922dab61SIlya Dryomov 		lwork->notify.payload = payload;
4260922dab61SIlya Dryomov 		lwork->notify.payload_len = payload_len;
4261922dab61SIlya Dryomov 		lwork->notify.msg = ceph_msg_get(msg);
4262922dab61SIlya Dryomov 		lwork_queue(lwork);
4263922dab61SIlya Dryomov 	}
4264922dab61SIlya Dryomov 
4265922dab61SIlya Dryomov out_unlock_lreq:
4266922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
4267922dab61SIlya Dryomov out_unlock_osdc:
4268922dab61SIlya Dryomov 	up_read(&osdc->lock);
4269a40c4f10SYehuda Sadeh 	return;
4270a40c4f10SYehuda Sadeh 
4271a40c4f10SYehuda Sadeh bad:
4272a40c4f10SYehuda Sadeh 	pr_err("osdc handle_watch_notify corrupt msg\n");
4273a40c4f10SYehuda Sadeh }
4274a40c4f10SYehuda Sadeh 
4275a40c4f10SYehuda Sadeh /*
42763d14c5d2SYehuda Sadeh  * Register request, send initial attempt.
42773d14c5d2SYehuda Sadeh  */
42783d14c5d2SYehuda Sadeh int ceph_osdc_start_request(struct ceph_osd_client *osdc,
42793d14c5d2SYehuda Sadeh 			    struct ceph_osd_request *req,
42803d14c5d2SYehuda Sadeh 			    bool nofail)
42813d14c5d2SYehuda Sadeh {
42825aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
42835aea3dcdSIlya Dryomov 	submit_request(req, false);
42845aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
42853d14c5d2SYehuda Sadeh 
42865aea3dcdSIlya Dryomov 	return 0;
42873d14c5d2SYehuda Sadeh }
42883d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_start_request);
42893d14c5d2SYehuda Sadeh 
42903d14c5d2SYehuda Sadeh /*
4291c297eb42SIlya Dryomov  * Unregister a registered request.  The request is not completed:
4292c297eb42SIlya Dryomov  * ->r_result isn't set and __complete_request() isn't called.
4293c9f9b93dSIlya Dryomov  */
4294c9f9b93dSIlya Dryomov void ceph_osdc_cancel_request(struct ceph_osd_request *req)
4295c9f9b93dSIlya Dryomov {
4296c9f9b93dSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
4297c9f9b93dSIlya Dryomov 
42985aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
42995aea3dcdSIlya Dryomov 	if (req->r_osd)
43005aea3dcdSIlya Dryomov 		cancel_request(req);
43015aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
4302c9f9b93dSIlya Dryomov }
4303c9f9b93dSIlya Dryomov EXPORT_SYMBOL(ceph_osdc_cancel_request);
4304c9f9b93dSIlya Dryomov 
4305c9f9b93dSIlya Dryomov /*
430642b06965SIlya Dryomov  * @timeout: in jiffies, 0 means "wait forever"
430742b06965SIlya Dryomov  */
430842b06965SIlya Dryomov static int wait_request_timeout(struct ceph_osd_request *req,
430942b06965SIlya Dryomov 				unsigned long timeout)
431042b06965SIlya Dryomov {
431142b06965SIlya Dryomov 	long left;
431242b06965SIlya Dryomov 
431342b06965SIlya Dryomov 	dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
43140e76abf2SYan, Zheng 	left = wait_for_completion_killable_timeout(&req->r_completion,
431542b06965SIlya Dryomov 						ceph_timeout_jiffies(timeout));
431642b06965SIlya Dryomov 	if (left <= 0) {
431742b06965SIlya Dryomov 		left = left ?: -ETIMEDOUT;
431842b06965SIlya Dryomov 		ceph_osdc_cancel_request(req);
431942b06965SIlya Dryomov 	} else {
432042b06965SIlya Dryomov 		left = req->r_result; /* completed */
432142b06965SIlya Dryomov 	}
432242b06965SIlya Dryomov 
432342b06965SIlya Dryomov 	return left;
432442b06965SIlya Dryomov }
432542b06965SIlya Dryomov 
432642b06965SIlya Dryomov /*
43273d14c5d2SYehuda Sadeh  * wait for a request to complete
43283d14c5d2SYehuda Sadeh  */
43293d14c5d2SYehuda Sadeh int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
43303d14c5d2SYehuda Sadeh 			   struct ceph_osd_request *req)
43313d14c5d2SYehuda Sadeh {
433242b06965SIlya Dryomov 	return wait_request_timeout(req, 0);
43333d14c5d2SYehuda Sadeh }
43343d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_wait_request);
43353d14c5d2SYehuda Sadeh 
43363d14c5d2SYehuda Sadeh /*
43373d14c5d2SYehuda Sadeh  * sync - wait for all in-flight requests to flush.  avoid starvation.
43383d14c5d2SYehuda Sadeh  */
43393d14c5d2SYehuda Sadeh void ceph_osdc_sync(struct ceph_osd_client *osdc)
43403d14c5d2SYehuda Sadeh {
43415aea3dcdSIlya Dryomov 	struct rb_node *n, *p;
43425aea3dcdSIlya Dryomov 	u64 last_tid = atomic64_read(&osdc->last_tid);
43433d14c5d2SYehuda Sadeh 
43445aea3dcdSIlya Dryomov again:
43455aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
43465aea3dcdSIlya Dryomov 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
43475aea3dcdSIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
43485aea3dcdSIlya Dryomov 
43495aea3dcdSIlya Dryomov 		mutex_lock(&osd->lock);
43505aea3dcdSIlya Dryomov 		for (p = rb_first(&osd->o_requests); p; p = rb_next(p)) {
43515aea3dcdSIlya Dryomov 			struct ceph_osd_request *req =
43525aea3dcdSIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
43535aea3dcdSIlya Dryomov 
43543d14c5d2SYehuda Sadeh 			if (req->r_tid > last_tid)
43553d14c5d2SYehuda Sadeh 				break;
43563d14c5d2SYehuda Sadeh 
43575aea3dcdSIlya Dryomov 			if (!(req->r_flags & CEPH_OSD_FLAG_WRITE))
43583d14c5d2SYehuda Sadeh 				continue;
43593d14c5d2SYehuda Sadeh 
43603d14c5d2SYehuda Sadeh 			ceph_osdc_get_request(req);
43615aea3dcdSIlya Dryomov 			mutex_unlock(&osd->lock);
43625aea3dcdSIlya Dryomov 			up_read(&osdc->lock);
43635aea3dcdSIlya Dryomov 			dout("%s waiting on req %p tid %llu last_tid %llu\n",
43645aea3dcdSIlya Dryomov 			     __func__, req, req->r_tid, last_tid);
4365b18b9550SIlya Dryomov 			wait_for_completion(&req->r_completion);
43663d14c5d2SYehuda Sadeh 			ceph_osdc_put_request(req);
43675aea3dcdSIlya Dryomov 			goto again;
43683d14c5d2SYehuda Sadeh 		}
43695aea3dcdSIlya Dryomov 
43705aea3dcdSIlya Dryomov 		mutex_unlock(&osd->lock);
43715aea3dcdSIlya Dryomov 	}
43725aea3dcdSIlya Dryomov 
43735aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
43745aea3dcdSIlya Dryomov 	dout("%s done last_tid %llu\n", __func__, last_tid);
43753d14c5d2SYehuda Sadeh }
43763d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_sync);
43773d14c5d2SYehuda Sadeh 
4378922dab61SIlya Dryomov static struct ceph_osd_request *
4379922dab61SIlya Dryomov alloc_linger_request(struct ceph_osd_linger_request *lreq)
4380922dab61SIlya Dryomov {
4381922dab61SIlya Dryomov 	struct ceph_osd_request *req;
4382922dab61SIlya Dryomov 
4383922dab61SIlya Dryomov 	req = ceph_osdc_alloc_request(lreq->osdc, NULL, 1, false, GFP_NOIO);
4384922dab61SIlya Dryomov 	if (!req)
4385922dab61SIlya Dryomov 		return NULL;
4386922dab61SIlya Dryomov 
4387922dab61SIlya Dryomov 	ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
4388922dab61SIlya Dryomov 	ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
4389922dab61SIlya Dryomov 
4390922dab61SIlya Dryomov 	if (ceph_osdc_alloc_messages(req, GFP_NOIO)) {
4391922dab61SIlya Dryomov 		ceph_osdc_put_request(req);
4392922dab61SIlya Dryomov 		return NULL;
4393922dab61SIlya Dryomov 	}
4394922dab61SIlya Dryomov 
4395922dab61SIlya Dryomov 	return req;
4396922dab61SIlya Dryomov }
4397922dab61SIlya Dryomov 
4398922dab61SIlya Dryomov /*
4399922dab61SIlya Dryomov  * Returns a handle, caller owns a ref.
4400922dab61SIlya Dryomov  */
4401922dab61SIlya Dryomov struct ceph_osd_linger_request *
4402922dab61SIlya Dryomov ceph_osdc_watch(struct ceph_osd_client *osdc,
4403922dab61SIlya Dryomov 		struct ceph_object_id *oid,
4404922dab61SIlya Dryomov 		struct ceph_object_locator *oloc,
4405922dab61SIlya Dryomov 		rados_watchcb2_t wcb,
4406922dab61SIlya Dryomov 		rados_watcherrcb_t errcb,
4407922dab61SIlya Dryomov 		void *data)
4408922dab61SIlya Dryomov {
4409922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
4410922dab61SIlya Dryomov 	int ret;
4411922dab61SIlya Dryomov 
4412922dab61SIlya Dryomov 	lreq = linger_alloc(osdc);
4413922dab61SIlya Dryomov 	if (!lreq)
4414922dab61SIlya Dryomov 		return ERR_PTR(-ENOMEM);
4415922dab61SIlya Dryomov 
441619079203SIlya Dryomov 	lreq->is_watch = true;
4417922dab61SIlya Dryomov 	lreq->wcb = wcb;
4418922dab61SIlya Dryomov 	lreq->errcb = errcb;
4419922dab61SIlya Dryomov 	lreq->data = data;
4420b07d3c4bSIlya Dryomov 	lreq->watch_valid_thru = jiffies;
4421922dab61SIlya Dryomov 
4422922dab61SIlya Dryomov 	ceph_oid_copy(&lreq->t.base_oid, oid);
4423922dab61SIlya Dryomov 	ceph_oloc_copy(&lreq->t.base_oloc, oloc);
442454ea0046SIlya Dryomov 	lreq->t.flags = CEPH_OSD_FLAG_WRITE;
44251134e091SDeepa Dinamani 	ktime_get_real_ts(&lreq->mtime);
4426922dab61SIlya Dryomov 
4427922dab61SIlya Dryomov 	lreq->reg_req = alloc_linger_request(lreq);
4428922dab61SIlya Dryomov 	if (!lreq->reg_req) {
4429922dab61SIlya Dryomov 		ret = -ENOMEM;
4430922dab61SIlya Dryomov 		goto err_put_lreq;
4431922dab61SIlya Dryomov 	}
4432922dab61SIlya Dryomov 
4433922dab61SIlya Dryomov 	lreq->ping_req = alloc_linger_request(lreq);
4434922dab61SIlya Dryomov 	if (!lreq->ping_req) {
4435922dab61SIlya Dryomov 		ret = -ENOMEM;
4436922dab61SIlya Dryomov 		goto err_put_lreq;
4437922dab61SIlya Dryomov 	}
4438922dab61SIlya Dryomov 
4439922dab61SIlya Dryomov 	down_write(&osdc->lock);
4440922dab61SIlya Dryomov 	linger_register(lreq); /* before osd_req_op_* */
4441922dab61SIlya Dryomov 	osd_req_op_watch_init(lreq->reg_req, 0, lreq->linger_id,
4442922dab61SIlya Dryomov 			      CEPH_OSD_WATCH_OP_WATCH);
4443922dab61SIlya Dryomov 	osd_req_op_watch_init(lreq->ping_req, 0, lreq->linger_id,
4444922dab61SIlya Dryomov 			      CEPH_OSD_WATCH_OP_PING);
4445922dab61SIlya Dryomov 	linger_submit(lreq);
4446922dab61SIlya Dryomov 	up_write(&osdc->lock);
4447922dab61SIlya Dryomov 
4448922dab61SIlya Dryomov 	ret = linger_reg_commit_wait(lreq);
4449922dab61SIlya Dryomov 	if (ret) {
4450922dab61SIlya Dryomov 		linger_cancel(lreq);
4451922dab61SIlya Dryomov 		goto err_put_lreq;
4452922dab61SIlya Dryomov 	}
4453922dab61SIlya Dryomov 
4454922dab61SIlya Dryomov 	return lreq;
4455922dab61SIlya Dryomov 
4456922dab61SIlya Dryomov err_put_lreq:
4457922dab61SIlya Dryomov 	linger_put(lreq);
4458922dab61SIlya Dryomov 	return ERR_PTR(ret);
4459922dab61SIlya Dryomov }
4460922dab61SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_watch);
4461922dab61SIlya Dryomov 
4462922dab61SIlya Dryomov /*
4463922dab61SIlya Dryomov  * Releases a ref.
4464922dab61SIlya Dryomov  *
4465922dab61SIlya Dryomov  * Times out after mount_timeout to preserve rbd unmap behaviour
4466922dab61SIlya Dryomov  * introduced in 2894e1d76974 ("rbd: timeout watch teardown on unmap
4467922dab61SIlya Dryomov  * with mount_timeout").
4468922dab61SIlya Dryomov  */
4469922dab61SIlya Dryomov int ceph_osdc_unwatch(struct ceph_osd_client *osdc,
4470922dab61SIlya Dryomov 		      struct ceph_osd_linger_request *lreq)
4471922dab61SIlya Dryomov {
4472922dab61SIlya Dryomov 	struct ceph_options *opts = osdc->client->options;
4473922dab61SIlya Dryomov 	struct ceph_osd_request *req;
4474922dab61SIlya Dryomov 	int ret;
4475922dab61SIlya Dryomov 
4476922dab61SIlya Dryomov 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4477922dab61SIlya Dryomov 	if (!req)
4478922dab61SIlya Dryomov 		return -ENOMEM;
4479922dab61SIlya Dryomov 
4480922dab61SIlya Dryomov 	ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
4481922dab61SIlya Dryomov 	ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
448254ea0046SIlya Dryomov 	req->r_flags = CEPH_OSD_FLAG_WRITE;
44831134e091SDeepa Dinamani 	ktime_get_real_ts(&req->r_mtime);
4484922dab61SIlya Dryomov 	osd_req_op_watch_init(req, 0, lreq->linger_id,
4485922dab61SIlya Dryomov 			      CEPH_OSD_WATCH_OP_UNWATCH);
4486922dab61SIlya Dryomov 
4487922dab61SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4488922dab61SIlya Dryomov 	if (ret)
4489922dab61SIlya Dryomov 		goto out_put_req;
4490922dab61SIlya Dryomov 
4491922dab61SIlya Dryomov 	ceph_osdc_start_request(osdc, req, false);
4492922dab61SIlya Dryomov 	linger_cancel(lreq);
4493922dab61SIlya Dryomov 	linger_put(lreq);
4494922dab61SIlya Dryomov 	ret = wait_request_timeout(req, opts->mount_timeout);
4495922dab61SIlya Dryomov 
4496922dab61SIlya Dryomov out_put_req:
4497922dab61SIlya Dryomov 	ceph_osdc_put_request(req);
4498922dab61SIlya Dryomov 	return ret;
4499922dab61SIlya Dryomov }
4500922dab61SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_unwatch);
4501922dab61SIlya Dryomov 
4502922dab61SIlya Dryomov static int osd_req_op_notify_ack_init(struct ceph_osd_request *req, int which,
4503922dab61SIlya Dryomov 				      u64 notify_id, u64 cookie, void *payload,
4504922dab61SIlya Dryomov 				      size_t payload_len)
4505922dab61SIlya Dryomov {
4506922dab61SIlya Dryomov 	struct ceph_osd_req_op *op;
4507922dab61SIlya Dryomov 	struct ceph_pagelist *pl;
4508922dab61SIlya Dryomov 	int ret;
4509922dab61SIlya Dryomov 
4510922dab61SIlya Dryomov 	op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY_ACK, 0);
4511922dab61SIlya Dryomov 
4512922dab61SIlya Dryomov 	pl = kmalloc(sizeof(*pl), GFP_NOIO);
4513922dab61SIlya Dryomov 	if (!pl)
4514922dab61SIlya Dryomov 		return -ENOMEM;
4515922dab61SIlya Dryomov 
4516922dab61SIlya Dryomov 	ceph_pagelist_init(pl);
4517922dab61SIlya Dryomov 	ret = ceph_pagelist_encode_64(pl, notify_id);
4518922dab61SIlya Dryomov 	ret |= ceph_pagelist_encode_64(pl, cookie);
4519922dab61SIlya Dryomov 	if (payload) {
4520922dab61SIlya Dryomov 		ret |= ceph_pagelist_encode_32(pl, payload_len);
4521922dab61SIlya Dryomov 		ret |= ceph_pagelist_append(pl, payload, payload_len);
4522922dab61SIlya Dryomov 	} else {
4523922dab61SIlya Dryomov 		ret |= ceph_pagelist_encode_32(pl, 0);
4524922dab61SIlya Dryomov 	}
4525922dab61SIlya Dryomov 	if (ret) {
4526922dab61SIlya Dryomov 		ceph_pagelist_release(pl);
4527922dab61SIlya Dryomov 		return -ENOMEM;
4528922dab61SIlya Dryomov 	}
4529922dab61SIlya Dryomov 
4530922dab61SIlya Dryomov 	ceph_osd_data_pagelist_init(&op->notify_ack.request_data, pl);
4531922dab61SIlya Dryomov 	op->indata_len = pl->length;
4532922dab61SIlya Dryomov 	return 0;
4533922dab61SIlya Dryomov }
4534922dab61SIlya Dryomov 
4535922dab61SIlya Dryomov int ceph_osdc_notify_ack(struct ceph_osd_client *osdc,
4536922dab61SIlya Dryomov 			 struct ceph_object_id *oid,
4537922dab61SIlya Dryomov 			 struct ceph_object_locator *oloc,
4538922dab61SIlya Dryomov 			 u64 notify_id,
4539922dab61SIlya Dryomov 			 u64 cookie,
4540922dab61SIlya Dryomov 			 void *payload,
4541922dab61SIlya Dryomov 			 size_t payload_len)
4542922dab61SIlya Dryomov {
4543922dab61SIlya Dryomov 	struct ceph_osd_request *req;
4544922dab61SIlya Dryomov 	int ret;
4545922dab61SIlya Dryomov 
4546922dab61SIlya Dryomov 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4547922dab61SIlya Dryomov 	if (!req)
4548922dab61SIlya Dryomov 		return -ENOMEM;
4549922dab61SIlya Dryomov 
4550922dab61SIlya Dryomov 	ceph_oid_copy(&req->r_base_oid, oid);
4551922dab61SIlya Dryomov 	ceph_oloc_copy(&req->r_base_oloc, oloc);
4552922dab61SIlya Dryomov 	req->r_flags = CEPH_OSD_FLAG_READ;
4553922dab61SIlya Dryomov 
4554922dab61SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4555922dab61SIlya Dryomov 	if (ret)
4556922dab61SIlya Dryomov 		goto out_put_req;
4557922dab61SIlya Dryomov 
4558922dab61SIlya Dryomov 	ret = osd_req_op_notify_ack_init(req, 0, notify_id, cookie, payload,
4559922dab61SIlya Dryomov 					 payload_len);
4560922dab61SIlya Dryomov 	if (ret)
4561922dab61SIlya Dryomov 		goto out_put_req;
4562922dab61SIlya Dryomov 
4563922dab61SIlya Dryomov 	ceph_osdc_start_request(osdc, req, false);
4564922dab61SIlya Dryomov 	ret = ceph_osdc_wait_request(osdc, req);
4565922dab61SIlya Dryomov 
4566922dab61SIlya Dryomov out_put_req:
4567922dab61SIlya Dryomov 	ceph_osdc_put_request(req);
4568922dab61SIlya Dryomov 	return ret;
4569922dab61SIlya Dryomov }
4570922dab61SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_notify_ack);
4571922dab61SIlya Dryomov 
457219079203SIlya Dryomov static int osd_req_op_notify_init(struct ceph_osd_request *req, int which,
457319079203SIlya Dryomov 				  u64 cookie, u32 prot_ver, u32 timeout,
457419079203SIlya Dryomov 				  void *payload, size_t payload_len)
457519079203SIlya Dryomov {
457619079203SIlya Dryomov 	struct ceph_osd_req_op *op;
457719079203SIlya Dryomov 	struct ceph_pagelist *pl;
457819079203SIlya Dryomov 	int ret;
457919079203SIlya Dryomov 
458019079203SIlya Dryomov 	op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY, 0);
458119079203SIlya Dryomov 	op->notify.cookie = cookie;
458219079203SIlya Dryomov 
458319079203SIlya Dryomov 	pl = kmalloc(sizeof(*pl), GFP_NOIO);
458419079203SIlya Dryomov 	if (!pl)
458519079203SIlya Dryomov 		return -ENOMEM;
458619079203SIlya Dryomov 
458719079203SIlya Dryomov 	ceph_pagelist_init(pl);
458819079203SIlya Dryomov 	ret = ceph_pagelist_encode_32(pl, 1); /* prot_ver */
458919079203SIlya Dryomov 	ret |= ceph_pagelist_encode_32(pl, timeout);
459019079203SIlya Dryomov 	ret |= ceph_pagelist_encode_32(pl, payload_len);
459119079203SIlya Dryomov 	ret |= ceph_pagelist_append(pl, payload, payload_len);
459219079203SIlya Dryomov 	if (ret) {
459319079203SIlya Dryomov 		ceph_pagelist_release(pl);
459419079203SIlya Dryomov 		return -ENOMEM;
459519079203SIlya Dryomov 	}
459619079203SIlya Dryomov 
459719079203SIlya Dryomov 	ceph_osd_data_pagelist_init(&op->notify.request_data, pl);
459819079203SIlya Dryomov 	op->indata_len = pl->length;
459919079203SIlya Dryomov 	return 0;
460019079203SIlya Dryomov }
460119079203SIlya Dryomov 
460219079203SIlya Dryomov /*
460319079203SIlya Dryomov  * @timeout: in seconds
460419079203SIlya Dryomov  *
460519079203SIlya Dryomov  * @preply_{pages,len} are initialized both on success and error.
460619079203SIlya Dryomov  * The caller is responsible for:
460719079203SIlya Dryomov  *
460819079203SIlya Dryomov  *     ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len))
460919079203SIlya Dryomov  */
461019079203SIlya Dryomov int ceph_osdc_notify(struct ceph_osd_client *osdc,
461119079203SIlya Dryomov 		     struct ceph_object_id *oid,
461219079203SIlya Dryomov 		     struct ceph_object_locator *oloc,
461319079203SIlya Dryomov 		     void *payload,
461419079203SIlya Dryomov 		     size_t payload_len,
461519079203SIlya Dryomov 		     u32 timeout,
461619079203SIlya Dryomov 		     struct page ***preply_pages,
461719079203SIlya Dryomov 		     size_t *preply_len)
461819079203SIlya Dryomov {
461919079203SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
462019079203SIlya Dryomov 	struct page **pages;
462119079203SIlya Dryomov 	int ret;
462219079203SIlya Dryomov 
462319079203SIlya Dryomov 	WARN_ON(!timeout);
462419079203SIlya Dryomov 	if (preply_pages) {
462519079203SIlya Dryomov 		*preply_pages = NULL;
462619079203SIlya Dryomov 		*preply_len = 0;
462719079203SIlya Dryomov 	}
462819079203SIlya Dryomov 
462919079203SIlya Dryomov 	lreq = linger_alloc(osdc);
463019079203SIlya Dryomov 	if (!lreq)
463119079203SIlya Dryomov 		return -ENOMEM;
463219079203SIlya Dryomov 
463319079203SIlya Dryomov 	lreq->preply_pages = preply_pages;
463419079203SIlya Dryomov 	lreq->preply_len = preply_len;
463519079203SIlya Dryomov 
463619079203SIlya Dryomov 	ceph_oid_copy(&lreq->t.base_oid, oid);
463719079203SIlya Dryomov 	ceph_oloc_copy(&lreq->t.base_oloc, oloc);
463819079203SIlya Dryomov 	lreq->t.flags = CEPH_OSD_FLAG_READ;
463919079203SIlya Dryomov 
464019079203SIlya Dryomov 	lreq->reg_req = alloc_linger_request(lreq);
464119079203SIlya Dryomov 	if (!lreq->reg_req) {
464219079203SIlya Dryomov 		ret = -ENOMEM;
464319079203SIlya Dryomov 		goto out_put_lreq;
464419079203SIlya Dryomov 	}
464519079203SIlya Dryomov 
464619079203SIlya Dryomov 	/* for notify_id */
464719079203SIlya Dryomov 	pages = ceph_alloc_page_vector(1, GFP_NOIO);
464819079203SIlya Dryomov 	if (IS_ERR(pages)) {
464919079203SIlya Dryomov 		ret = PTR_ERR(pages);
465019079203SIlya Dryomov 		goto out_put_lreq;
465119079203SIlya Dryomov 	}
465219079203SIlya Dryomov 
465319079203SIlya Dryomov 	down_write(&osdc->lock);
465419079203SIlya Dryomov 	linger_register(lreq); /* before osd_req_op_* */
465519079203SIlya Dryomov 	ret = osd_req_op_notify_init(lreq->reg_req, 0, lreq->linger_id, 1,
465619079203SIlya Dryomov 				     timeout, payload, payload_len);
465719079203SIlya Dryomov 	if (ret) {
465819079203SIlya Dryomov 		linger_unregister(lreq);
465919079203SIlya Dryomov 		up_write(&osdc->lock);
466019079203SIlya Dryomov 		ceph_release_page_vector(pages, 1);
466119079203SIlya Dryomov 		goto out_put_lreq;
466219079203SIlya Dryomov 	}
466319079203SIlya Dryomov 	ceph_osd_data_pages_init(osd_req_op_data(lreq->reg_req, 0, notify,
466419079203SIlya Dryomov 						 response_data),
466519079203SIlya Dryomov 				 pages, PAGE_SIZE, 0, false, true);
466619079203SIlya Dryomov 	linger_submit(lreq);
466719079203SIlya Dryomov 	up_write(&osdc->lock);
466819079203SIlya Dryomov 
466919079203SIlya Dryomov 	ret = linger_reg_commit_wait(lreq);
467019079203SIlya Dryomov 	if (!ret)
467119079203SIlya Dryomov 		ret = linger_notify_finish_wait(lreq);
467219079203SIlya Dryomov 	else
467319079203SIlya Dryomov 		dout("lreq %p failed to initiate notify %d\n", lreq, ret);
467419079203SIlya Dryomov 
467519079203SIlya Dryomov 	linger_cancel(lreq);
467619079203SIlya Dryomov out_put_lreq:
467719079203SIlya Dryomov 	linger_put(lreq);
467819079203SIlya Dryomov 	return ret;
467919079203SIlya Dryomov }
468019079203SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_notify);
468119079203SIlya Dryomov 
46823d14c5d2SYehuda Sadeh /*
4683b07d3c4bSIlya Dryomov  * Return the number of milliseconds since the watch was last
4684b07d3c4bSIlya Dryomov  * confirmed, or an error.  If there is an error, the watch is no
4685b07d3c4bSIlya Dryomov  * longer valid, and should be destroyed with ceph_osdc_unwatch().
4686b07d3c4bSIlya Dryomov  */
4687b07d3c4bSIlya Dryomov int ceph_osdc_watch_check(struct ceph_osd_client *osdc,
4688b07d3c4bSIlya Dryomov 			  struct ceph_osd_linger_request *lreq)
4689b07d3c4bSIlya Dryomov {
4690b07d3c4bSIlya Dryomov 	unsigned long stamp, age;
4691b07d3c4bSIlya Dryomov 	int ret;
4692b07d3c4bSIlya Dryomov 
4693b07d3c4bSIlya Dryomov 	down_read(&osdc->lock);
4694b07d3c4bSIlya Dryomov 	mutex_lock(&lreq->lock);
4695b07d3c4bSIlya Dryomov 	stamp = lreq->watch_valid_thru;
4696b07d3c4bSIlya Dryomov 	if (!list_empty(&lreq->pending_lworks)) {
4697b07d3c4bSIlya Dryomov 		struct linger_work *lwork =
4698b07d3c4bSIlya Dryomov 		    list_first_entry(&lreq->pending_lworks,
4699b07d3c4bSIlya Dryomov 				     struct linger_work,
4700b07d3c4bSIlya Dryomov 				     pending_item);
4701b07d3c4bSIlya Dryomov 
4702b07d3c4bSIlya Dryomov 		if (time_before(lwork->queued_stamp, stamp))
4703b07d3c4bSIlya Dryomov 			stamp = lwork->queued_stamp;
4704b07d3c4bSIlya Dryomov 	}
4705b07d3c4bSIlya Dryomov 	age = jiffies - stamp;
4706b07d3c4bSIlya Dryomov 	dout("%s lreq %p linger_id %llu age %lu last_error %d\n", __func__,
4707b07d3c4bSIlya Dryomov 	     lreq, lreq->linger_id, age, lreq->last_error);
4708b07d3c4bSIlya Dryomov 	/* we are truncating to msecs, so return a safe upper bound */
4709b07d3c4bSIlya Dryomov 	ret = lreq->last_error ?: 1 + jiffies_to_msecs(age);
4710b07d3c4bSIlya Dryomov 
4711b07d3c4bSIlya Dryomov 	mutex_unlock(&lreq->lock);
4712b07d3c4bSIlya Dryomov 	up_read(&osdc->lock);
4713b07d3c4bSIlya Dryomov 	return ret;
4714b07d3c4bSIlya Dryomov }
4715b07d3c4bSIlya Dryomov 
4716a4ed38d7SDouglas Fuller static int decode_watcher(void **p, void *end, struct ceph_watch_item *item)
4717a4ed38d7SDouglas Fuller {
4718a4ed38d7SDouglas Fuller 	u8 struct_v;
4719a4ed38d7SDouglas Fuller 	u32 struct_len;
4720a4ed38d7SDouglas Fuller 	int ret;
4721a4ed38d7SDouglas Fuller 
4722a4ed38d7SDouglas Fuller 	ret = ceph_start_decoding(p, end, 2, "watch_item_t",
4723a4ed38d7SDouglas Fuller 				  &struct_v, &struct_len);
4724a4ed38d7SDouglas Fuller 	if (ret)
4725a4ed38d7SDouglas Fuller 		return ret;
4726a4ed38d7SDouglas Fuller 
4727a4ed38d7SDouglas Fuller 	ceph_decode_copy(p, &item->name, sizeof(item->name));
4728a4ed38d7SDouglas Fuller 	item->cookie = ceph_decode_64(p);
4729a4ed38d7SDouglas Fuller 	*p += 4; /* skip timeout_seconds */
4730a4ed38d7SDouglas Fuller 	if (struct_v >= 2) {
4731a4ed38d7SDouglas Fuller 		ceph_decode_copy(p, &item->addr, sizeof(item->addr));
4732a4ed38d7SDouglas Fuller 		ceph_decode_addr(&item->addr);
4733a4ed38d7SDouglas Fuller 	}
4734a4ed38d7SDouglas Fuller 
4735a4ed38d7SDouglas Fuller 	dout("%s %s%llu cookie %llu addr %s\n", __func__,
4736a4ed38d7SDouglas Fuller 	     ENTITY_NAME(item->name), item->cookie,
4737a4ed38d7SDouglas Fuller 	     ceph_pr_addr(&item->addr.in_addr));
4738a4ed38d7SDouglas Fuller 	return 0;
4739a4ed38d7SDouglas Fuller }
4740a4ed38d7SDouglas Fuller 
4741a4ed38d7SDouglas Fuller static int decode_watchers(void **p, void *end,
4742a4ed38d7SDouglas Fuller 			   struct ceph_watch_item **watchers,
4743a4ed38d7SDouglas Fuller 			   u32 *num_watchers)
4744a4ed38d7SDouglas Fuller {
4745a4ed38d7SDouglas Fuller 	u8 struct_v;
4746a4ed38d7SDouglas Fuller 	u32 struct_len;
4747a4ed38d7SDouglas Fuller 	int i;
4748a4ed38d7SDouglas Fuller 	int ret;
4749a4ed38d7SDouglas Fuller 
4750a4ed38d7SDouglas Fuller 	ret = ceph_start_decoding(p, end, 1, "obj_list_watch_response_t",
4751a4ed38d7SDouglas Fuller 				  &struct_v, &struct_len);
4752a4ed38d7SDouglas Fuller 	if (ret)
4753a4ed38d7SDouglas Fuller 		return ret;
4754a4ed38d7SDouglas Fuller 
4755a4ed38d7SDouglas Fuller 	*num_watchers = ceph_decode_32(p);
4756a4ed38d7SDouglas Fuller 	*watchers = kcalloc(*num_watchers, sizeof(**watchers), GFP_NOIO);
4757a4ed38d7SDouglas Fuller 	if (!*watchers)
4758a4ed38d7SDouglas Fuller 		return -ENOMEM;
4759a4ed38d7SDouglas Fuller 
4760a4ed38d7SDouglas Fuller 	for (i = 0; i < *num_watchers; i++) {
4761a4ed38d7SDouglas Fuller 		ret = decode_watcher(p, end, *watchers + i);
4762a4ed38d7SDouglas Fuller 		if (ret) {
4763a4ed38d7SDouglas Fuller 			kfree(*watchers);
4764a4ed38d7SDouglas Fuller 			return ret;
4765a4ed38d7SDouglas Fuller 		}
4766a4ed38d7SDouglas Fuller 	}
4767a4ed38d7SDouglas Fuller 
4768a4ed38d7SDouglas Fuller 	return 0;
4769a4ed38d7SDouglas Fuller }
4770a4ed38d7SDouglas Fuller 
4771a4ed38d7SDouglas Fuller /*
4772a4ed38d7SDouglas Fuller  * On success, the caller is responsible for:
4773a4ed38d7SDouglas Fuller  *
4774a4ed38d7SDouglas Fuller  *     kfree(watchers);
4775a4ed38d7SDouglas Fuller  */
4776a4ed38d7SDouglas Fuller int ceph_osdc_list_watchers(struct ceph_osd_client *osdc,
4777a4ed38d7SDouglas Fuller 			    struct ceph_object_id *oid,
4778a4ed38d7SDouglas Fuller 			    struct ceph_object_locator *oloc,
4779a4ed38d7SDouglas Fuller 			    struct ceph_watch_item **watchers,
4780a4ed38d7SDouglas Fuller 			    u32 *num_watchers)
4781a4ed38d7SDouglas Fuller {
4782a4ed38d7SDouglas Fuller 	struct ceph_osd_request *req;
4783a4ed38d7SDouglas Fuller 	struct page **pages;
4784a4ed38d7SDouglas Fuller 	int ret;
4785a4ed38d7SDouglas Fuller 
4786a4ed38d7SDouglas Fuller 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4787a4ed38d7SDouglas Fuller 	if (!req)
4788a4ed38d7SDouglas Fuller 		return -ENOMEM;
4789a4ed38d7SDouglas Fuller 
4790a4ed38d7SDouglas Fuller 	ceph_oid_copy(&req->r_base_oid, oid);
4791a4ed38d7SDouglas Fuller 	ceph_oloc_copy(&req->r_base_oloc, oloc);
4792a4ed38d7SDouglas Fuller 	req->r_flags = CEPH_OSD_FLAG_READ;
4793a4ed38d7SDouglas Fuller 
4794a4ed38d7SDouglas Fuller 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4795a4ed38d7SDouglas Fuller 	if (ret)
4796a4ed38d7SDouglas Fuller 		goto out_put_req;
4797a4ed38d7SDouglas Fuller 
4798a4ed38d7SDouglas Fuller 	pages = ceph_alloc_page_vector(1, GFP_NOIO);
4799a4ed38d7SDouglas Fuller 	if (IS_ERR(pages)) {
4800a4ed38d7SDouglas Fuller 		ret = PTR_ERR(pages);
4801a4ed38d7SDouglas Fuller 		goto out_put_req;
4802a4ed38d7SDouglas Fuller 	}
4803a4ed38d7SDouglas Fuller 
4804a4ed38d7SDouglas Fuller 	osd_req_op_init(req, 0, CEPH_OSD_OP_LIST_WATCHERS, 0);
4805a4ed38d7SDouglas Fuller 	ceph_osd_data_pages_init(osd_req_op_data(req, 0, list_watchers,
4806a4ed38d7SDouglas Fuller 						 response_data),
4807a4ed38d7SDouglas Fuller 				 pages, PAGE_SIZE, 0, false, true);
4808a4ed38d7SDouglas Fuller 
4809a4ed38d7SDouglas Fuller 	ceph_osdc_start_request(osdc, req, false);
4810a4ed38d7SDouglas Fuller 	ret = ceph_osdc_wait_request(osdc, req);
4811a4ed38d7SDouglas Fuller 	if (ret >= 0) {
4812a4ed38d7SDouglas Fuller 		void *p = page_address(pages[0]);
4813a4ed38d7SDouglas Fuller 		void *const end = p + req->r_ops[0].outdata_len;
4814a4ed38d7SDouglas Fuller 
4815a4ed38d7SDouglas Fuller 		ret = decode_watchers(&p, end, watchers, num_watchers);
4816a4ed38d7SDouglas Fuller 	}
4817a4ed38d7SDouglas Fuller 
4818a4ed38d7SDouglas Fuller out_put_req:
4819a4ed38d7SDouglas Fuller 	ceph_osdc_put_request(req);
4820a4ed38d7SDouglas Fuller 	return ret;
4821a4ed38d7SDouglas Fuller }
4822a4ed38d7SDouglas Fuller EXPORT_SYMBOL(ceph_osdc_list_watchers);
4823a4ed38d7SDouglas Fuller 
4824b07d3c4bSIlya Dryomov /*
4825dd935f44SJosh Durgin  * Call all pending notify callbacks - for use after a watch is
4826dd935f44SJosh Durgin  * unregistered, to make sure no more callbacks for it will be invoked
4827dd935f44SJosh Durgin  */
4828f6479449Sstephen hemminger void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
4829dd935f44SJosh Durgin {
483099d16943SIlya Dryomov 	dout("%s osdc %p\n", __func__, osdc);
4831dd935f44SJosh Durgin 	flush_workqueue(osdc->notify_wq);
4832dd935f44SJosh Durgin }
4833dd935f44SJosh Durgin EXPORT_SYMBOL(ceph_osdc_flush_notifies);
4834dd935f44SJosh Durgin 
48357cca78c9SIlya Dryomov void ceph_osdc_maybe_request_map(struct ceph_osd_client *osdc)
48367cca78c9SIlya Dryomov {
48377cca78c9SIlya Dryomov 	down_read(&osdc->lock);
48387cca78c9SIlya Dryomov 	maybe_request_map(osdc);
48397cca78c9SIlya Dryomov 	up_read(&osdc->lock);
48407cca78c9SIlya Dryomov }
48417cca78c9SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_maybe_request_map);
4842dd935f44SJosh Durgin 
4843dd935f44SJosh Durgin /*
4844428a7158SDouglas Fuller  * Execute an OSD class method on an object.
4845428a7158SDouglas Fuller  *
4846428a7158SDouglas Fuller  * @flags: CEPH_OSD_FLAG_*
48472544a020SIlya Dryomov  * @resp_len: in/out param for reply length
4848428a7158SDouglas Fuller  */
4849428a7158SDouglas Fuller int ceph_osdc_call(struct ceph_osd_client *osdc,
4850428a7158SDouglas Fuller 		   struct ceph_object_id *oid,
4851428a7158SDouglas Fuller 		   struct ceph_object_locator *oloc,
4852428a7158SDouglas Fuller 		   const char *class, const char *method,
4853428a7158SDouglas Fuller 		   unsigned int flags,
4854428a7158SDouglas Fuller 		   struct page *req_page, size_t req_len,
4855428a7158SDouglas Fuller 		   struct page *resp_page, size_t *resp_len)
4856428a7158SDouglas Fuller {
4857428a7158SDouglas Fuller 	struct ceph_osd_request *req;
4858428a7158SDouglas Fuller 	int ret;
4859428a7158SDouglas Fuller 
48602544a020SIlya Dryomov 	if (req_len > PAGE_SIZE || (resp_page && *resp_len > PAGE_SIZE))
48612544a020SIlya Dryomov 		return -E2BIG;
48622544a020SIlya Dryomov 
4863428a7158SDouglas Fuller 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4864428a7158SDouglas Fuller 	if (!req)
4865428a7158SDouglas Fuller 		return -ENOMEM;
4866428a7158SDouglas Fuller 
4867428a7158SDouglas Fuller 	ceph_oid_copy(&req->r_base_oid, oid);
4868428a7158SDouglas Fuller 	ceph_oloc_copy(&req->r_base_oloc, oloc);
4869428a7158SDouglas Fuller 	req->r_flags = flags;
4870428a7158SDouglas Fuller 
4871428a7158SDouglas Fuller 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4872428a7158SDouglas Fuller 	if (ret)
4873428a7158SDouglas Fuller 		goto out_put_req;
4874428a7158SDouglas Fuller 
4875428a7158SDouglas Fuller 	osd_req_op_cls_init(req, 0, CEPH_OSD_OP_CALL, class, method);
4876428a7158SDouglas Fuller 	if (req_page)
4877428a7158SDouglas Fuller 		osd_req_op_cls_request_data_pages(req, 0, &req_page, req_len,
4878428a7158SDouglas Fuller 						  0, false, false);
4879428a7158SDouglas Fuller 	if (resp_page)
4880428a7158SDouglas Fuller 		osd_req_op_cls_response_data_pages(req, 0, &resp_page,
48812544a020SIlya Dryomov 						   *resp_len, 0, false, false);
4882428a7158SDouglas Fuller 
4883428a7158SDouglas Fuller 	ceph_osdc_start_request(osdc, req, false);
4884428a7158SDouglas Fuller 	ret = ceph_osdc_wait_request(osdc, req);
4885428a7158SDouglas Fuller 	if (ret >= 0) {
4886428a7158SDouglas Fuller 		ret = req->r_ops[0].rval;
4887428a7158SDouglas Fuller 		if (resp_page)
4888428a7158SDouglas Fuller 			*resp_len = req->r_ops[0].outdata_len;
4889428a7158SDouglas Fuller 	}
4890428a7158SDouglas Fuller 
4891428a7158SDouglas Fuller out_put_req:
4892428a7158SDouglas Fuller 	ceph_osdc_put_request(req);
4893428a7158SDouglas Fuller 	return ret;
4894428a7158SDouglas Fuller }
4895428a7158SDouglas Fuller EXPORT_SYMBOL(ceph_osdc_call);
4896428a7158SDouglas Fuller 
4897428a7158SDouglas Fuller /*
48983d14c5d2SYehuda Sadeh  * init, shutdown
48993d14c5d2SYehuda Sadeh  */
49003d14c5d2SYehuda Sadeh int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
49013d14c5d2SYehuda Sadeh {
49023d14c5d2SYehuda Sadeh 	int err;
49033d14c5d2SYehuda Sadeh 
49043d14c5d2SYehuda Sadeh 	dout("init\n");
49053d14c5d2SYehuda Sadeh 	osdc->client = client;
49065aea3dcdSIlya Dryomov 	init_rwsem(&osdc->lock);
49073d14c5d2SYehuda Sadeh 	osdc->osds = RB_ROOT;
49083d14c5d2SYehuda Sadeh 	INIT_LIST_HEAD(&osdc->osd_lru);
49099dd2845cSIlya Dryomov 	spin_lock_init(&osdc->osd_lru_lock);
49105aea3dcdSIlya Dryomov 	osd_init(&osdc->homeless_osd);
49115aea3dcdSIlya Dryomov 	osdc->homeless_osd.o_osdc = osdc;
49125aea3dcdSIlya Dryomov 	osdc->homeless_osd.o_osd = CEPH_HOMELESS_OSD;
4913264048afSIlya Dryomov 	osdc->last_linger_id = CEPH_LINGER_ID_START;
4914922dab61SIlya Dryomov 	osdc->linger_requests = RB_ROOT;
49154609245eSIlya Dryomov 	osdc->map_checks = RB_ROOT;
49164609245eSIlya Dryomov 	osdc->linger_map_checks = RB_ROOT;
49173d14c5d2SYehuda Sadeh 	INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
49183d14c5d2SYehuda Sadeh 	INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
49193d14c5d2SYehuda Sadeh 
49203d14c5d2SYehuda Sadeh 	err = -ENOMEM;
4921e5253a7bSIlya Dryomov 	osdc->osdmap = ceph_osdmap_alloc();
4922e5253a7bSIlya Dryomov 	if (!osdc->osdmap)
4923e5253a7bSIlya Dryomov 		goto out;
4924e5253a7bSIlya Dryomov 
49259e767adbSIlya Dryomov 	osdc->req_mempool = mempool_create_slab_pool(10,
49269e767adbSIlya Dryomov 						     ceph_osd_request_cache);
49273d14c5d2SYehuda Sadeh 	if (!osdc->req_mempool)
4928e5253a7bSIlya Dryomov 		goto out_map;
49293d14c5d2SYehuda Sadeh 
4930d50b409fSSage Weil 	err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
4931711da55dSIlya Dryomov 				PAGE_SIZE, 10, true, "osd_op");
49323d14c5d2SYehuda Sadeh 	if (err < 0)
49333d14c5d2SYehuda Sadeh 		goto out_mempool;
4934d50b409fSSage Weil 	err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
4935711da55dSIlya Dryomov 				PAGE_SIZE, 10, true, "osd_op_reply");
49363d14c5d2SYehuda Sadeh 	if (err < 0)
49373d14c5d2SYehuda Sadeh 		goto out_msgpool;
4938a40c4f10SYehuda Sadeh 
4939dbcae088SDan Carpenter 	err = -ENOMEM;
4940a40c4f10SYehuda Sadeh 	osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
4941dbcae088SDan Carpenter 	if (!osdc->notify_wq)
4942c172ec5cSIlya Dryomov 		goto out_msgpool_reply;
4943c172ec5cSIlya Dryomov 
4944fbca9635SIlya Dryomov 	schedule_delayed_work(&osdc->timeout_work,
4945fbca9635SIlya Dryomov 			      osdc->client->options->osd_keepalive_timeout);
4946b37ee1b9SIlya Dryomov 	schedule_delayed_work(&osdc->osds_timeout_work,
4947b37ee1b9SIlya Dryomov 	    round_jiffies_relative(osdc->client->options->osd_idle_ttl));
4948b37ee1b9SIlya Dryomov 
49493d14c5d2SYehuda Sadeh 	return 0;
49503d14c5d2SYehuda Sadeh 
4951c172ec5cSIlya Dryomov out_msgpool_reply:
4952c172ec5cSIlya Dryomov 	ceph_msgpool_destroy(&osdc->msgpool_op_reply);
49533d14c5d2SYehuda Sadeh out_msgpool:
49543d14c5d2SYehuda Sadeh 	ceph_msgpool_destroy(&osdc->msgpool_op);
49553d14c5d2SYehuda Sadeh out_mempool:
49563d14c5d2SYehuda Sadeh 	mempool_destroy(osdc->req_mempool);
4957e5253a7bSIlya Dryomov out_map:
4958e5253a7bSIlya Dryomov 	ceph_osdmap_destroy(osdc->osdmap);
49593d14c5d2SYehuda Sadeh out:
49603d14c5d2SYehuda Sadeh 	return err;
49613d14c5d2SYehuda Sadeh }
49623d14c5d2SYehuda Sadeh 
49633d14c5d2SYehuda Sadeh void ceph_osdc_stop(struct ceph_osd_client *osdc)
49643d14c5d2SYehuda Sadeh {
4965a40c4f10SYehuda Sadeh 	flush_workqueue(osdc->notify_wq);
4966a40c4f10SYehuda Sadeh 	destroy_workqueue(osdc->notify_wq);
49673d14c5d2SYehuda Sadeh 	cancel_delayed_work_sync(&osdc->timeout_work);
49683d14c5d2SYehuda Sadeh 	cancel_delayed_work_sync(&osdc->osds_timeout_work);
496942a2c09fSIlya Dryomov 
49705aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
497142a2c09fSIlya Dryomov 	while (!RB_EMPTY_ROOT(&osdc->osds)) {
497242a2c09fSIlya Dryomov 		struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
497342a2c09fSIlya Dryomov 						struct ceph_osd, o_node);
49745aea3dcdSIlya Dryomov 		close_osd(osd);
497542a2c09fSIlya Dryomov 	}
49765aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
497702113a0fSElena Reshetova 	WARN_ON(refcount_read(&osdc->homeless_osd.o_ref) != 1);
49785aea3dcdSIlya Dryomov 	osd_cleanup(&osdc->homeless_osd);
49795aea3dcdSIlya Dryomov 
49805aea3dcdSIlya Dryomov 	WARN_ON(!list_empty(&osdc->osd_lru));
4981922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_requests));
49824609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osdc->map_checks));
49834609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_map_checks));
49845aea3dcdSIlya Dryomov 	WARN_ON(atomic_read(&osdc->num_requests));
49855aea3dcdSIlya Dryomov 	WARN_ON(atomic_read(&osdc->num_homeless));
498642a2c09fSIlya Dryomov 
49873d14c5d2SYehuda Sadeh 	ceph_osdmap_destroy(osdc->osdmap);
49883d14c5d2SYehuda Sadeh 	mempool_destroy(osdc->req_mempool);
49893d14c5d2SYehuda Sadeh 	ceph_msgpool_destroy(&osdc->msgpool_op);
49903d14c5d2SYehuda Sadeh 	ceph_msgpool_destroy(&osdc->msgpool_op_reply);
49913d14c5d2SYehuda Sadeh }
49923d14c5d2SYehuda Sadeh 
49933d14c5d2SYehuda Sadeh /*
49943d14c5d2SYehuda Sadeh  * Read some contiguous pages.  If we cross a stripe boundary, shorten
49953d14c5d2SYehuda Sadeh  * *plen.  Return number of bytes read, or error.
49963d14c5d2SYehuda Sadeh  */
49973d14c5d2SYehuda Sadeh int ceph_osdc_readpages(struct ceph_osd_client *osdc,
49983d14c5d2SYehuda Sadeh 			struct ceph_vino vino, struct ceph_file_layout *layout,
49993d14c5d2SYehuda Sadeh 			u64 off, u64 *plen,
50003d14c5d2SYehuda Sadeh 			u32 truncate_seq, u64 truncate_size,
5001b7495fc2SSage Weil 			struct page **pages, int num_pages, int page_align)
50023d14c5d2SYehuda Sadeh {
50033d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
50043d14c5d2SYehuda Sadeh 	int rc = 0;
50053d14c5d2SYehuda Sadeh 
50063d14c5d2SYehuda Sadeh 	dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
50073d14c5d2SYehuda Sadeh 	     vino.snap, off, *plen);
5008715e4cd4SYan, Zheng 	req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
50093d14c5d2SYehuda Sadeh 				    CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
5010acead002SAlex Elder 				    NULL, truncate_seq, truncate_size,
5011153e5167SAlex Elder 				    false);
50126816282dSSage Weil 	if (IS_ERR(req))
50136816282dSSage Weil 		return PTR_ERR(req);
50143d14c5d2SYehuda Sadeh 
50153d14c5d2SYehuda Sadeh 	/* it may be a short read due to an object boundary */
5016406e2c9fSAlex Elder 	osd_req_op_extent_osd_data_pages(req, 0,
5017a4ce40a9SAlex Elder 				pages, *plen, page_align, false, false);
50183d14c5d2SYehuda Sadeh 
5019e0c59487SAlex Elder 	dout("readpages  final extent is %llu~%llu (%llu bytes align %d)\n",
502043bfe5deSAlex Elder 	     off, *plen, *plen, page_align);
50213d14c5d2SYehuda Sadeh 
50223d14c5d2SYehuda Sadeh 	rc = ceph_osdc_start_request(osdc, req, false);
50233d14c5d2SYehuda Sadeh 	if (!rc)
50243d14c5d2SYehuda Sadeh 		rc = ceph_osdc_wait_request(osdc, req);
50253d14c5d2SYehuda Sadeh 
50263d14c5d2SYehuda Sadeh 	ceph_osdc_put_request(req);
50273d14c5d2SYehuda Sadeh 	dout("readpages result %d\n", rc);
50283d14c5d2SYehuda Sadeh 	return rc;
50293d14c5d2SYehuda Sadeh }
50303d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_readpages);
50313d14c5d2SYehuda Sadeh 
50323d14c5d2SYehuda Sadeh /*
50333d14c5d2SYehuda Sadeh  * do a synchronous write on N pages
50343d14c5d2SYehuda Sadeh  */
50353d14c5d2SYehuda Sadeh int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
50363d14c5d2SYehuda Sadeh 			 struct ceph_file_layout *layout,
50373d14c5d2SYehuda Sadeh 			 struct ceph_snap_context *snapc,
50383d14c5d2SYehuda Sadeh 			 u64 off, u64 len,
50393d14c5d2SYehuda Sadeh 			 u32 truncate_seq, u64 truncate_size,
50403d14c5d2SYehuda Sadeh 			 struct timespec *mtime,
504124808826SAlex Elder 			 struct page **pages, int num_pages)
50423d14c5d2SYehuda Sadeh {
50433d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
50443d14c5d2SYehuda Sadeh 	int rc = 0;
5045b7495fc2SSage Weil 	int page_align = off & ~PAGE_MASK;
50463d14c5d2SYehuda Sadeh 
5047715e4cd4SYan, Zheng 	req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
504854ea0046SIlya Dryomov 				    CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
5049acead002SAlex Elder 				    snapc, truncate_seq, truncate_size,
5050153e5167SAlex Elder 				    true);
50516816282dSSage Weil 	if (IS_ERR(req))
50526816282dSSage Weil 		return PTR_ERR(req);
50533d14c5d2SYehuda Sadeh 
50543d14c5d2SYehuda Sadeh 	/* it may be a short write due to an object boundary */
5055406e2c9fSAlex Elder 	osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
505643bfe5deSAlex Elder 				false, false);
505743bfe5deSAlex Elder 	dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
50583d14c5d2SYehuda Sadeh 
5059bb873b53SIlya Dryomov 	req->r_mtime = *mtime;
506087f979d3SAlex Elder 	rc = ceph_osdc_start_request(osdc, req, true);
50613d14c5d2SYehuda Sadeh 	if (!rc)
50623d14c5d2SYehuda Sadeh 		rc = ceph_osdc_wait_request(osdc, req);
50633d14c5d2SYehuda Sadeh 
50643d14c5d2SYehuda Sadeh 	ceph_osdc_put_request(req);
50653d14c5d2SYehuda Sadeh 	if (rc == 0)
50663d14c5d2SYehuda Sadeh 		rc = len;
50673d14c5d2SYehuda Sadeh 	dout("writepages result %d\n", rc);
50683d14c5d2SYehuda Sadeh 	return rc;
50693d14c5d2SYehuda Sadeh }
50703d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_writepages);
50713d14c5d2SYehuda Sadeh 
50725522ae0bSAlex Elder int ceph_osdc_setup(void)
50735522ae0bSAlex Elder {
50743f1af42aSIlya Dryomov 	size_t size = sizeof(struct ceph_osd_request) +
50753f1af42aSIlya Dryomov 	    CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
50763f1af42aSIlya Dryomov 
50775522ae0bSAlex Elder 	BUG_ON(ceph_osd_request_cache);
50783f1af42aSIlya Dryomov 	ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
50793f1af42aSIlya Dryomov 						   0, 0, NULL);
50805522ae0bSAlex Elder 
50815522ae0bSAlex Elder 	return ceph_osd_request_cache ? 0 : -ENOMEM;
50825522ae0bSAlex Elder }
50835522ae0bSAlex Elder EXPORT_SYMBOL(ceph_osdc_setup);
50845522ae0bSAlex Elder 
50855522ae0bSAlex Elder void ceph_osdc_cleanup(void)
50865522ae0bSAlex Elder {
50875522ae0bSAlex Elder 	BUG_ON(!ceph_osd_request_cache);
50885522ae0bSAlex Elder 	kmem_cache_destroy(ceph_osd_request_cache);
50895522ae0bSAlex Elder 	ceph_osd_request_cache = NULL;
50905522ae0bSAlex Elder }
50915522ae0bSAlex Elder EXPORT_SYMBOL(ceph_osdc_cleanup);
50925522ae0bSAlex Elder 
50933d14c5d2SYehuda Sadeh /*
50943d14c5d2SYehuda Sadeh  * handle incoming message
50953d14c5d2SYehuda Sadeh  */
50963d14c5d2SYehuda Sadeh static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
50973d14c5d2SYehuda Sadeh {
50983d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
50995aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
51003d14c5d2SYehuda Sadeh 	int type = le16_to_cpu(msg->hdr.type);
51013d14c5d2SYehuda Sadeh 
51023d14c5d2SYehuda Sadeh 	switch (type) {
51033d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_MAP:
51043d14c5d2SYehuda Sadeh 		ceph_osdc_handle_map(osdc, msg);
51053d14c5d2SYehuda Sadeh 		break;
51063d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_OPREPLY:
51075aea3dcdSIlya Dryomov 		handle_reply(osd, msg);
51083d14c5d2SYehuda Sadeh 		break;
5109a02a946dSIlya Dryomov 	case CEPH_MSG_OSD_BACKOFF:
5110a02a946dSIlya Dryomov 		handle_backoff(osd, msg);
5111a02a946dSIlya Dryomov 		break;
5112a40c4f10SYehuda Sadeh 	case CEPH_MSG_WATCH_NOTIFY:
5113a40c4f10SYehuda Sadeh 		handle_watch_notify(osdc, msg);
5114a40c4f10SYehuda Sadeh 		break;
51153d14c5d2SYehuda Sadeh 
51163d14c5d2SYehuda Sadeh 	default:
51173d14c5d2SYehuda Sadeh 		pr_err("received unknown message type %d %s\n", type,
51183d14c5d2SYehuda Sadeh 		       ceph_msg_type_name(type));
51193d14c5d2SYehuda Sadeh 	}
51205aea3dcdSIlya Dryomov 
51213d14c5d2SYehuda Sadeh 	ceph_msg_put(msg);
51223d14c5d2SYehuda Sadeh }
51233d14c5d2SYehuda Sadeh 
51243d14c5d2SYehuda Sadeh /*
5125d15f9d69SIlya Dryomov  * Lookup and return message for incoming reply.  Don't try to do
5126d15f9d69SIlya Dryomov  * anything about a larger than preallocated data portion of the
5127d15f9d69SIlya Dryomov  * message at the moment - for now, just skip the message.
51283d14c5d2SYehuda Sadeh  */
51293d14c5d2SYehuda Sadeh static struct ceph_msg *get_reply(struct ceph_connection *con,
51303d14c5d2SYehuda Sadeh 				  struct ceph_msg_header *hdr,
51313d14c5d2SYehuda Sadeh 				  int *skip)
51323d14c5d2SYehuda Sadeh {
51333d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
51343d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = osd->o_osdc;
51355aea3dcdSIlya Dryomov 	struct ceph_msg *m = NULL;
51363d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
51373f0a4ac5SIlya Dryomov 	int front_len = le32_to_cpu(hdr->front_len);
51383d14c5d2SYehuda Sadeh 	int data_len = le32_to_cpu(hdr->data_len);
51395aea3dcdSIlya Dryomov 	u64 tid = le64_to_cpu(hdr->tid);
51403d14c5d2SYehuda Sadeh 
51415aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
51425aea3dcdSIlya Dryomov 	if (!osd_registered(osd)) {
51435aea3dcdSIlya Dryomov 		dout("%s osd%d unknown, skipping\n", __func__, osd->o_osd);
51445aea3dcdSIlya Dryomov 		*skip = 1;
51455aea3dcdSIlya Dryomov 		goto out_unlock_osdc;
51465aea3dcdSIlya Dryomov 	}
51475aea3dcdSIlya Dryomov 	WARN_ON(osd->o_osd != le64_to_cpu(hdr->src.num));
51485aea3dcdSIlya Dryomov 
51495aea3dcdSIlya Dryomov 	mutex_lock(&osd->lock);
51505aea3dcdSIlya Dryomov 	req = lookup_request(&osd->o_requests, tid);
51513d14c5d2SYehuda Sadeh 	if (!req) {
5152cd8140c6SIlya Dryomov 		dout("%s osd%d tid %llu unknown, skipping\n", __func__,
5153cd8140c6SIlya Dryomov 		     osd->o_osd, tid);
5154d15f9d69SIlya Dryomov 		*skip = 1;
51555aea3dcdSIlya Dryomov 		goto out_unlock_session;
51563d14c5d2SYehuda Sadeh 	}
51573d14c5d2SYehuda Sadeh 
51588921d114SAlex Elder 	ceph_msg_revoke_incoming(req->r_reply);
51593d14c5d2SYehuda Sadeh 
5160f2be82b0SIlya Dryomov 	if (front_len > req->r_reply->front_alloc_len) {
5161d15f9d69SIlya Dryomov 		pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
5162d15f9d69SIlya Dryomov 			__func__, osd->o_osd, req->r_tid, front_len,
5163d15f9d69SIlya Dryomov 			req->r_reply->front_alloc_len);
51643f0a4ac5SIlya Dryomov 		m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
51653f0a4ac5SIlya Dryomov 				 false);
51663d14c5d2SYehuda Sadeh 		if (!m)
51675aea3dcdSIlya Dryomov 			goto out_unlock_session;
51683d14c5d2SYehuda Sadeh 		ceph_msg_put(req->r_reply);
51693d14c5d2SYehuda Sadeh 		req->r_reply = m;
51703d14c5d2SYehuda Sadeh 	}
51713d14c5d2SYehuda Sadeh 
5172d15f9d69SIlya Dryomov 	if (data_len > req->r_reply->data_length) {
5173d15f9d69SIlya Dryomov 		pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
5174d15f9d69SIlya Dryomov 			__func__, osd->o_osd, req->r_tid, data_len,
5175d15f9d69SIlya Dryomov 			req->r_reply->data_length);
51763d14c5d2SYehuda Sadeh 		m = NULL;
5177d15f9d69SIlya Dryomov 		*skip = 1;
51785aea3dcdSIlya Dryomov 		goto out_unlock_session;
51793d14c5d2SYehuda Sadeh 	}
5180d15f9d69SIlya Dryomov 
5181d15f9d69SIlya Dryomov 	m = ceph_msg_get(req->r_reply);
51823d14c5d2SYehuda Sadeh 	dout("get_reply tid %lld %p\n", tid, m);
51833d14c5d2SYehuda Sadeh 
51845aea3dcdSIlya Dryomov out_unlock_session:
51855aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
51865aea3dcdSIlya Dryomov out_unlock_osdc:
51875aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
51883d14c5d2SYehuda Sadeh 	return m;
51893d14c5d2SYehuda Sadeh }
51903d14c5d2SYehuda Sadeh 
519119079203SIlya Dryomov /*
519219079203SIlya Dryomov  * TODO: switch to a msg-owned pagelist
519319079203SIlya Dryomov  */
519419079203SIlya Dryomov static struct ceph_msg *alloc_msg_with_page_vector(struct ceph_msg_header *hdr)
519519079203SIlya Dryomov {
519619079203SIlya Dryomov 	struct ceph_msg *m;
519719079203SIlya Dryomov 	int type = le16_to_cpu(hdr->type);
519819079203SIlya Dryomov 	u32 front_len = le32_to_cpu(hdr->front_len);
519919079203SIlya Dryomov 	u32 data_len = le32_to_cpu(hdr->data_len);
520019079203SIlya Dryomov 
520119079203SIlya Dryomov 	m = ceph_msg_new(type, front_len, GFP_NOIO, false);
520219079203SIlya Dryomov 	if (!m)
520319079203SIlya Dryomov 		return NULL;
520419079203SIlya Dryomov 
520519079203SIlya Dryomov 	if (data_len) {
520619079203SIlya Dryomov 		struct page **pages;
520719079203SIlya Dryomov 		struct ceph_osd_data osd_data;
520819079203SIlya Dryomov 
520919079203SIlya Dryomov 		pages = ceph_alloc_page_vector(calc_pages_for(0, data_len),
521019079203SIlya Dryomov 					       GFP_NOIO);
5211c22e853aSWei Yongjun 		if (IS_ERR(pages)) {
521219079203SIlya Dryomov 			ceph_msg_put(m);
521319079203SIlya Dryomov 			return NULL;
521419079203SIlya Dryomov 		}
521519079203SIlya Dryomov 
521619079203SIlya Dryomov 		ceph_osd_data_pages_init(&osd_data, pages, data_len, 0, false,
521719079203SIlya Dryomov 					 false);
521819079203SIlya Dryomov 		ceph_osdc_msg_data_add(m, &osd_data);
521919079203SIlya Dryomov 	}
522019079203SIlya Dryomov 
522119079203SIlya Dryomov 	return m;
522219079203SIlya Dryomov }
522319079203SIlya Dryomov 
52243d14c5d2SYehuda Sadeh static struct ceph_msg *alloc_msg(struct ceph_connection *con,
52253d14c5d2SYehuda Sadeh 				  struct ceph_msg_header *hdr,
52263d14c5d2SYehuda Sadeh 				  int *skip)
52273d14c5d2SYehuda Sadeh {
52283d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
52293d14c5d2SYehuda Sadeh 	int type = le16_to_cpu(hdr->type);
52303d14c5d2SYehuda Sadeh 
52311c20f2d2SAlex Elder 	*skip = 0;
52323d14c5d2SYehuda Sadeh 	switch (type) {
52333d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_MAP:
5234a02a946dSIlya Dryomov 	case CEPH_MSG_OSD_BACKOFF:
5235a40c4f10SYehuda Sadeh 	case CEPH_MSG_WATCH_NOTIFY:
523619079203SIlya Dryomov 		return alloc_msg_with_page_vector(hdr);
52373d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_OPREPLY:
52383d14c5d2SYehuda Sadeh 		return get_reply(con, hdr, skip);
52393d14c5d2SYehuda Sadeh 	default:
52405aea3dcdSIlya Dryomov 		pr_warn("%s osd%d unknown msg type %d, skipping\n", __func__,
52415aea3dcdSIlya Dryomov 			osd->o_osd, type);
52423d14c5d2SYehuda Sadeh 		*skip = 1;
52433d14c5d2SYehuda Sadeh 		return NULL;
52443d14c5d2SYehuda Sadeh 	}
52453d14c5d2SYehuda Sadeh }
52463d14c5d2SYehuda Sadeh 
52473d14c5d2SYehuda Sadeh /*
52483d14c5d2SYehuda Sadeh  * Wrappers to refcount containing ceph_osd struct
52493d14c5d2SYehuda Sadeh  */
52503d14c5d2SYehuda Sadeh static struct ceph_connection *get_osd_con(struct ceph_connection *con)
52513d14c5d2SYehuda Sadeh {
52523d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
52533d14c5d2SYehuda Sadeh 	if (get_osd(osd))
52543d14c5d2SYehuda Sadeh 		return con;
52553d14c5d2SYehuda Sadeh 	return NULL;
52563d14c5d2SYehuda Sadeh }
52573d14c5d2SYehuda Sadeh 
52583d14c5d2SYehuda Sadeh static void put_osd_con(struct ceph_connection *con)
52593d14c5d2SYehuda Sadeh {
52603d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
52613d14c5d2SYehuda Sadeh 	put_osd(osd);
52623d14c5d2SYehuda Sadeh }
52633d14c5d2SYehuda Sadeh 
52643d14c5d2SYehuda Sadeh /*
52653d14c5d2SYehuda Sadeh  * authentication
52663d14c5d2SYehuda Sadeh  */
5267a3530df3SAlex Elder /*
5268a3530df3SAlex Elder  * Note: returned pointer is the address of a structure that's
5269a3530df3SAlex Elder  * managed separately.  Caller must *not* attempt to free it.
5270a3530df3SAlex Elder  */
5271a3530df3SAlex Elder static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
52728f43fb53SAlex Elder 					int *proto, int force_new)
52733d14c5d2SYehuda Sadeh {
52743d14c5d2SYehuda Sadeh 	struct ceph_osd *o = con->private;
52753d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = o->o_osdc;
52763d14c5d2SYehuda Sadeh 	struct ceph_auth_client *ac = osdc->client->monc.auth;
527774f1869fSAlex Elder 	struct ceph_auth_handshake *auth = &o->o_auth;
52783d14c5d2SYehuda Sadeh 
527974f1869fSAlex Elder 	if (force_new && auth->authorizer) {
52806c1ea260SIlya Dryomov 		ceph_auth_destroy_authorizer(auth->authorizer);
528174f1869fSAlex Elder 		auth->authorizer = NULL;
52823d14c5d2SYehuda Sadeh 	}
528327859f97SSage Weil 	if (!auth->authorizer) {
528427859f97SSage Weil 		int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
5285a3530df3SAlex Elder 						      auth);
52863d14c5d2SYehuda Sadeh 		if (ret)
5287a3530df3SAlex Elder 			return ERR_PTR(ret);
528827859f97SSage Weil 	} else {
528927859f97SSage Weil 		int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
52900bed9b5cSSage Weil 						     auth);
52910bed9b5cSSage Weil 		if (ret)
52920bed9b5cSSage Weil 			return ERR_PTR(ret);
52933d14c5d2SYehuda Sadeh 	}
52943d14c5d2SYehuda Sadeh 	*proto = ac->protocol;
529574f1869fSAlex Elder 
5296a3530df3SAlex Elder 	return auth;
52973d14c5d2SYehuda Sadeh }
52983d14c5d2SYehuda Sadeh 
52993d14c5d2SYehuda Sadeh 
53000dde5848SIlya Dryomov static int verify_authorizer_reply(struct ceph_connection *con)
53013d14c5d2SYehuda Sadeh {
53023d14c5d2SYehuda Sadeh 	struct ceph_osd *o = con->private;
53033d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = o->o_osdc;
53043d14c5d2SYehuda Sadeh 	struct ceph_auth_client *ac = osdc->client->monc.auth;
53053d14c5d2SYehuda Sadeh 
53060dde5848SIlya Dryomov 	return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer);
53073d14c5d2SYehuda Sadeh }
53083d14c5d2SYehuda Sadeh 
53093d14c5d2SYehuda Sadeh static int invalidate_authorizer(struct ceph_connection *con)
53103d14c5d2SYehuda Sadeh {
53113d14c5d2SYehuda Sadeh 	struct ceph_osd *o = con->private;
53123d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = o->o_osdc;
53133d14c5d2SYehuda Sadeh 	struct ceph_auth_client *ac = osdc->client->monc.auth;
53143d14c5d2SYehuda Sadeh 
531527859f97SSage Weil 	ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
53163d14c5d2SYehuda Sadeh 	return ceph_monc_validate_auth(&osdc->client->monc);
53173d14c5d2SYehuda Sadeh }
53183d14c5d2SYehuda Sadeh 
53198cb441c0SIlya Dryomov static void osd_reencode_message(struct ceph_msg *msg)
53208cb441c0SIlya Dryomov {
5321914902afSIlya Dryomov 	int type = le16_to_cpu(msg->hdr.type);
5322914902afSIlya Dryomov 
5323914902afSIlya Dryomov 	if (type == CEPH_MSG_OSD_OP)
53248cb441c0SIlya Dryomov 		encode_request_finish(msg);
53258cb441c0SIlya Dryomov }
53268cb441c0SIlya Dryomov 
532779dbd1baSIlya Dryomov static int osd_sign_message(struct ceph_msg *msg)
532833d07337SYan, Zheng {
532979dbd1baSIlya Dryomov 	struct ceph_osd *o = msg->con->private;
533033d07337SYan, Zheng 	struct ceph_auth_handshake *auth = &o->o_auth;
533179dbd1baSIlya Dryomov 
533233d07337SYan, Zheng 	return ceph_auth_sign_message(auth, msg);
533333d07337SYan, Zheng }
533433d07337SYan, Zheng 
533579dbd1baSIlya Dryomov static int osd_check_message_signature(struct ceph_msg *msg)
533633d07337SYan, Zheng {
533779dbd1baSIlya Dryomov 	struct ceph_osd *o = msg->con->private;
533833d07337SYan, Zheng 	struct ceph_auth_handshake *auth = &o->o_auth;
533979dbd1baSIlya Dryomov 
534033d07337SYan, Zheng 	return ceph_auth_check_message_signature(auth, msg);
534133d07337SYan, Zheng }
534233d07337SYan, Zheng 
53433d14c5d2SYehuda Sadeh static const struct ceph_connection_operations osd_con_ops = {
53443d14c5d2SYehuda Sadeh 	.get = get_osd_con,
53453d14c5d2SYehuda Sadeh 	.put = put_osd_con,
53463d14c5d2SYehuda Sadeh 	.dispatch = dispatch,
53473d14c5d2SYehuda Sadeh 	.get_authorizer = get_authorizer,
53483d14c5d2SYehuda Sadeh 	.verify_authorizer_reply = verify_authorizer_reply,
53493d14c5d2SYehuda Sadeh 	.invalidate_authorizer = invalidate_authorizer,
53503d14c5d2SYehuda Sadeh 	.alloc_msg = alloc_msg,
53518cb441c0SIlya Dryomov 	.reencode_message = osd_reencode_message,
535279dbd1baSIlya Dryomov 	.sign_message = osd_sign_message,
535379dbd1baSIlya Dryomov 	.check_message_signature = osd_check_message_signature,
53545aea3dcdSIlya Dryomov 	.fault = osd_fault,
53553d14c5d2SYehuda Sadeh };
5356