xref: /openbmc/linux/net/ceph/osd_client.c (revision 914902af)
1a4ce40a9SAlex Elder 
23d14c5d2SYehuda Sadeh #include <linux/ceph/ceph_debug.h>
33d14c5d2SYehuda Sadeh 
43d14c5d2SYehuda Sadeh #include <linux/module.h>
53d14c5d2SYehuda Sadeh #include <linux/err.h>
63d14c5d2SYehuda Sadeh #include <linux/highmem.h>
73d14c5d2SYehuda Sadeh #include <linux/mm.h>
83d14c5d2SYehuda Sadeh #include <linux/pagemap.h>
93d14c5d2SYehuda Sadeh #include <linux/slab.h>
103d14c5d2SYehuda Sadeh #include <linux/uaccess.h>
113d14c5d2SYehuda Sadeh #ifdef CONFIG_BLOCK
123d14c5d2SYehuda Sadeh #include <linux/bio.h>
133d14c5d2SYehuda Sadeh #endif
143d14c5d2SYehuda Sadeh 
158cb441c0SIlya Dryomov #include <linux/ceph/ceph_features.h>
163d14c5d2SYehuda Sadeh #include <linux/ceph/libceph.h>
173d14c5d2SYehuda Sadeh #include <linux/ceph/osd_client.h>
183d14c5d2SYehuda Sadeh #include <linux/ceph/messenger.h>
193d14c5d2SYehuda Sadeh #include <linux/ceph/decode.h>
203d14c5d2SYehuda Sadeh #include <linux/ceph/auth.h>
213d14c5d2SYehuda Sadeh #include <linux/ceph/pagelist.h>
223d14c5d2SYehuda Sadeh 
233d14c5d2SYehuda Sadeh #define OSD_OPREPLY_FRONT_LEN	512
243d14c5d2SYehuda Sadeh 
255522ae0bSAlex Elder static struct kmem_cache	*ceph_osd_request_cache;
265522ae0bSAlex Elder 
273d14c5d2SYehuda Sadeh static const struct ceph_connection_operations osd_con_ops;
283d14c5d2SYehuda Sadeh 
293d14c5d2SYehuda Sadeh /*
303d14c5d2SYehuda Sadeh  * Implement client access to distributed object storage cluster.
313d14c5d2SYehuda Sadeh  *
323d14c5d2SYehuda Sadeh  * All data objects are stored within a cluster/cloud of OSDs, or
333d14c5d2SYehuda Sadeh  * "object storage devices."  (Note that Ceph OSDs have _nothing_ to
343d14c5d2SYehuda Sadeh  * do with the T10 OSD extensions to SCSI.)  Ceph OSDs are simply
353d14c5d2SYehuda Sadeh  * remote daemons serving up and coordinating consistent and safe
363d14c5d2SYehuda Sadeh  * access to storage.
373d14c5d2SYehuda Sadeh  *
383d14c5d2SYehuda Sadeh  * Cluster membership and the mapping of data objects onto storage devices
393d14c5d2SYehuda Sadeh  * are described by the osd map.
403d14c5d2SYehuda Sadeh  *
413d14c5d2SYehuda Sadeh  * We keep track of pending OSD requests (read, write), resubmit
423d14c5d2SYehuda Sadeh  * requests to different OSDs when the cluster topology/data layout
433d14c5d2SYehuda Sadeh  * change, or retry the affected requests when the communications
443d14c5d2SYehuda Sadeh  * channel with an OSD is reset.
453d14c5d2SYehuda Sadeh  */
463d14c5d2SYehuda Sadeh 
475aea3dcdSIlya Dryomov static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req);
485aea3dcdSIlya Dryomov static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req);
49922dab61SIlya Dryomov static void link_linger(struct ceph_osd *osd,
50922dab61SIlya Dryomov 			struct ceph_osd_linger_request *lreq);
51922dab61SIlya Dryomov static void unlink_linger(struct ceph_osd *osd,
52922dab61SIlya Dryomov 			  struct ceph_osd_linger_request *lreq);
53a02a946dSIlya Dryomov static void clear_backoffs(struct ceph_osd *osd);
545aea3dcdSIlya Dryomov 
555aea3dcdSIlya Dryomov #if 1
565aea3dcdSIlya Dryomov static inline bool rwsem_is_wrlocked(struct rw_semaphore *sem)
575aea3dcdSIlya Dryomov {
585aea3dcdSIlya Dryomov 	bool wrlocked = true;
595aea3dcdSIlya Dryomov 
605aea3dcdSIlya Dryomov 	if (unlikely(down_read_trylock(sem))) {
615aea3dcdSIlya Dryomov 		wrlocked = false;
625aea3dcdSIlya Dryomov 		up_read(sem);
635aea3dcdSIlya Dryomov 	}
645aea3dcdSIlya Dryomov 
655aea3dcdSIlya Dryomov 	return wrlocked;
665aea3dcdSIlya Dryomov }
675aea3dcdSIlya Dryomov static inline void verify_osdc_locked(struct ceph_osd_client *osdc)
685aea3dcdSIlya Dryomov {
695aea3dcdSIlya Dryomov 	WARN_ON(!rwsem_is_locked(&osdc->lock));
705aea3dcdSIlya Dryomov }
715aea3dcdSIlya Dryomov static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc)
725aea3dcdSIlya Dryomov {
735aea3dcdSIlya Dryomov 	WARN_ON(!rwsem_is_wrlocked(&osdc->lock));
745aea3dcdSIlya Dryomov }
755aea3dcdSIlya Dryomov static inline void verify_osd_locked(struct ceph_osd *osd)
765aea3dcdSIlya Dryomov {
775aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
785aea3dcdSIlya Dryomov 
795aea3dcdSIlya Dryomov 	WARN_ON(!(mutex_is_locked(&osd->lock) &&
805aea3dcdSIlya Dryomov 		  rwsem_is_locked(&osdc->lock)) &&
815aea3dcdSIlya Dryomov 		!rwsem_is_wrlocked(&osdc->lock));
825aea3dcdSIlya Dryomov }
83922dab61SIlya Dryomov static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq)
84922dab61SIlya Dryomov {
85922dab61SIlya Dryomov 	WARN_ON(!mutex_is_locked(&lreq->lock));
86922dab61SIlya Dryomov }
875aea3dcdSIlya Dryomov #else
885aea3dcdSIlya Dryomov static inline void verify_osdc_locked(struct ceph_osd_client *osdc) { }
895aea3dcdSIlya Dryomov static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc) { }
905aea3dcdSIlya Dryomov static inline void verify_osd_locked(struct ceph_osd *osd) { }
91922dab61SIlya Dryomov static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq) { }
925aea3dcdSIlya Dryomov #endif
935aea3dcdSIlya Dryomov 
943d14c5d2SYehuda Sadeh /*
953d14c5d2SYehuda Sadeh  * calculate the mapping of a file extent onto an object, and fill out the
963d14c5d2SYehuda Sadeh  * request accordingly.  shorten extent as necessary if it crosses an
973d14c5d2SYehuda Sadeh  * object boundary.
983d14c5d2SYehuda Sadeh  *
993d14c5d2SYehuda Sadeh  * fill osd op in request message.
1003d14c5d2SYehuda Sadeh  */
101dbe0fc41SAlex Elder static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
102a19dadfbSAlex Elder 			u64 *objnum, u64 *objoff, u64 *objlen)
1033d14c5d2SYehuda Sadeh {
10460e56f13SAlex Elder 	u64 orig_len = *plen;
105d63b77f4SSage Weil 	int r;
1063d14c5d2SYehuda Sadeh 
10760e56f13SAlex Elder 	/* object extent? */
10875d1c941SAlex Elder 	r = ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
10975d1c941SAlex Elder 					  objoff, objlen);
110d63b77f4SSage Weil 	if (r < 0)
111d63b77f4SSage Weil 		return r;
11275d1c941SAlex Elder 	if (*objlen < orig_len) {
11375d1c941SAlex Elder 		*plen = *objlen;
11460e56f13SAlex Elder 		dout(" skipping last %llu, final file extent %llu~%llu\n",
11560e56f13SAlex Elder 		     orig_len - *plen, off, *plen);
11660e56f13SAlex Elder 	}
11760e56f13SAlex Elder 
11875d1c941SAlex Elder 	dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
1193d14c5d2SYehuda Sadeh 
1203ff5f385SAlex Elder 	return 0;
1213d14c5d2SYehuda Sadeh }
1223d14c5d2SYehuda Sadeh 
123c54d47bfSAlex Elder static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
124c54d47bfSAlex Elder {
125c54d47bfSAlex Elder 	memset(osd_data, 0, sizeof (*osd_data));
126c54d47bfSAlex Elder 	osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
127c54d47bfSAlex Elder }
128c54d47bfSAlex Elder 
129a4ce40a9SAlex Elder static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
13043bfe5deSAlex Elder 			struct page **pages, u64 length, u32 alignment,
13143bfe5deSAlex Elder 			bool pages_from_pool, bool own_pages)
13243bfe5deSAlex Elder {
13343bfe5deSAlex Elder 	osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
13443bfe5deSAlex Elder 	osd_data->pages = pages;
13543bfe5deSAlex Elder 	osd_data->length = length;
13643bfe5deSAlex Elder 	osd_data->alignment = alignment;
13743bfe5deSAlex Elder 	osd_data->pages_from_pool = pages_from_pool;
13843bfe5deSAlex Elder 	osd_data->own_pages = own_pages;
13943bfe5deSAlex Elder }
14043bfe5deSAlex Elder 
141a4ce40a9SAlex Elder static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
14243bfe5deSAlex Elder 			struct ceph_pagelist *pagelist)
14343bfe5deSAlex Elder {
14443bfe5deSAlex Elder 	osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
14543bfe5deSAlex Elder 	osd_data->pagelist = pagelist;
14643bfe5deSAlex Elder }
14743bfe5deSAlex Elder 
14843bfe5deSAlex Elder #ifdef CONFIG_BLOCK
149a4ce40a9SAlex Elder static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
15043bfe5deSAlex Elder 			struct bio *bio, size_t bio_length)
15143bfe5deSAlex Elder {
15243bfe5deSAlex Elder 	osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
15343bfe5deSAlex Elder 	osd_data->bio = bio;
15443bfe5deSAlex Elder 	osd_data->bio_length = bio_length;
15543bfe5deSAlex Elder }
15643bfe5deSAlex Elder #endif /* CONFIG_BLOCK */
15743bfe5deSAlex Elder 
158863c7eb5SAlex Elder #define osd_req_op_data(oreq, whch, typ, fld)				\
159863c7eb5SAlex Elder ({									\
1608a703a38SIoana Ciornei 	struct ceph_osd_request *__oreq = (oreq);			\
1618a703a38SIoana Ciornei 	unsigned int __whch = (whch);					\
1628a703a38SIoana Ciornei 	BUG_ON(__whch >= __oreq->r_num_ops);				\
1638a703a38SIoana Ciornei 	&__oreq->r_ops[__whch].typ.fld;					\
164863c7eb5SAlex Elder })
165863c7eb5SAlex Elder 
16649719778SAlex Elder static struct ceph_osd_data *
16749719778SAlex Elder osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
16849719778SAlex Elder {
16949719778SAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
17049719778SAlex Elder 
17149719778SAlex Elder 	return &osd_req->r_ops[which].raw_data_in;
17249719778SAlex Elder }
17349719778SAlex Elder 
174a4ce40a9SAlex Elder struct ceph_osd_data *
175a4ce40a9SAlex Elder osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
176406e2c9fSAlex Elder 			unsigned int which)
177a4ce40a9SAlex Elder {
178863c7eb5SAlex Elder 	return osd_req_op_data(osd_req, which, extent, osd_data);
179a4ce40a9SAlex Elder }
180a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_osd_data);
181a4ce40a9SAlex Elder 
18249719778SAlex Elder void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
18349719778SAlex Elder 			unsigned int which, struct page **pages,
18449719778SAlex Elder 			u64 length, u32 alignment,
18549719778SAlex Elder 			bool pages_from_pool, bool own_pages)
18649719778SAlex Elder {
18749719778SAlex Elder 	struct ceph_osd_data *osd_data;
18849719778SAlex Elder 
18949719778SAlex Elder 	osd_data = osd_req_op_raw_data_in(osd_req, which);
19049719778SAlex Elder 	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
19149719778SAlex Elder 				pages_from_pool, own_pages);
19249719778SAlex Elder }
19349719778SAlex Elder EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
19449719778SAlex Elder 
195a4ce40a9SAlex Elder void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
196406e2c9fSAlex Elder 			unsigned int which, struct page **pages,
197406e2c9fSAlex Elder 			u64 length, u32 alignment,
198a4ce40a9SAlex Elder 			bool pages_from_pool, bool own_pages)
199a4ce40a9SAlex Elder {
200a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
201a4ce40a9SAlex Elder 
202863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
203a4ce40a9SAlex Elder 	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
204a4ce40a9SAlex Elder 				pages_from_pool, own_pages);
205a4ce40a9SAlex Elder }
206a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
207a4ce40a9SAlex Elder 
208a4ce40a9SAlex Elder void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
209406e2c9fSAlex Elder 			unsigned int which, struct ceph_pagelist *pagelist)
210a4ce40a9SAlex Elder {
211a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
212a4ce40a9SAlex Elder 
213863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
214a4ce40a9SAlex Elder 	ceph_osd_data_pagelist_init(osd_data, pagelist);
215a4ce40a9SAlex Elder }
216a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
217a4ce40a9SAlex Elder 
218a4ce40a9SAlex Elder #ifdef CONFIG_BLOCK
219a4ce40a9SAlex Elder void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
220406e2c9fSAlex Elder 			unsigned int which, struct bio *bio, size_t bio_length)
221a4ce40a9SAlex Elder {
222a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
223863c7eb5SAlex Elder 
224863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
225a4ce40a9SAlex Elder 	ceph_osd_data_bio_init(osd_data, bio, bio_length);
226a4ce40a9SAlex Elder }
227a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
228a4ce40a9SAlex Elder #endif /* CONFIG_BLOCK */
229a4ce40a9SAlex Elder 
230a4ce40a9SAlex Elder static void osd_req_op_cls_request_info_pagelist(
231a4ce40a9SAlex Elder 			struct ceph_osd_request *osd_req,
232a4ce40a9SAlex Elder 			unsigned int which, struct ceph_pagelist *pagelist)
233a4ce40a9SAlex Elder {
234a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
235a4ce40a9SAlex Elder 
236863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, request_info);
237a4ce40a9SAlex Elder 	ceph_osd_data_pagelist_init(osd_data, pagelist);
238a4ce40a9SAlex Elder }
239a4ce40a9SAlex Elder 
24004017e29SAlex Elder void osd_req_op_cls_request_data_pagelist(
24104017e29SAlex Elder 			struct ceph_osd_request *osd_req,
24204017e29SAlex Elder 			unsigned int which, struct ceph_pagelist *pagelist)
24304017e29SAlex Elder {
24404017e29SAlex Elder 	struct ceph_osd_data *osd_data;
24504017e29SAlex Elder 
246863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, request_data);
24704017e29SAlex Elder 	ceph_osd_data_pagelist_init(osd_data, pagelist);
248bb873b53SIlya Dryomov 	osd_req->r_ops[which].cls.indata_len += pagelist->length;
249bb873b53SIlya Dryomov 	osd_req->r_ops[which].indata_len += pagelist->length;
25004017e29SAlex Elder }
25104017e29SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
25204017e29SAlex Elder 
2536c57b554SAlex Elder void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
2546c57b554SAlex Elder 			unsigned int which, struct page **pages, u64 length,
2556c57b554SAlex Elder 			u32 alignment, bool pages_from_pool, bool own_pages)
2566c57b554SAlex Elder {
2576c57b554SAlex Elder 	struct ceph_osd_data *osd_data;
2586c57b554SAlex Elder 
2596c57b554SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, request_data);
2606c57b554SAlex Elder 	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
2616c57b554SAlex Elder 				pages_from_pool, own_pages);
262bb873b53SIlya Dryomov 	osd_req->r_ops[which].cls.indata_len += length;
263bb873b53SIlya Dryomov 	osd_req->r_ops[which].indata_len += length;
2646c57b554SAlex Elder }
2656c57b554SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
2666c57b554SAlex Elder 
267a4ce40a9SAlex Elder void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
268a4ce40a9SAlex Elder 			unsigned int which, struct page **pages, u64 length,
269a4ce40a9SAlex Elder 			u32 alignment, bool pages_from_pool, bool own_pages)
270a4ce40a9SAlex Elder {
271a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
272a4ce40a9SAlex Elder 
273863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, response_data);
274a4ce40a9SAlex Elder 	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
275a4ce40a9SAlex Elder 				pages_from_pool, own_pages);
276a4ce40a9SAlex Elder }
277a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
278a4ce40a9SAlex Elder 
27923c08a9cSAlex Elder static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
28023c08a9cSAlex Elder {
28123c08a9cSAlex Elder 	switch (osd_data->type) {
28223c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_NONE:
28323c08a9cSAlex Elder 		return 0;
28423c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_PAGES:
28523c08a9cSAlex Elder 		return osd_data->length;
28623c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_PAGELIST:
28723c08a9cSAlex Elder 		return (u64)osd_data->pagelist->length;
28823c08a9cSAlex Elder #ifdef CONFIG_BLOCK
28923c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_BIO:
29023c08a9cSAlex Elder 		return (u64)osd_data->bio_length;
29123c08a9cSAlex Elder #endif /* CONFIG_BLOCK */
29223c08a9cSAlex Elder 	default:
29323c08a9cSAlex Elder 		WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
29423c08a9cSAlex Elder 		return 0;
29523c08a9cSAlex Elder 	}
29623c08a9cSAlex Elder }
29723c08a9cSAlex Elder 
298c54d47bfSAlex Elder static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
299c54d47bfSAlex Elder {
3005476492fSAlex Elder 	if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
301c54d47bfSAlex Elder 		int num_pages;
302c54d47bfSAlex Elder 
303c54d47bfSAlex Elder 		num_pages = calc_pages_for((u64)osd_data->alignment,
304c54d47bfSAlex Elder 						(u64)osd_data->length);
305c54d47bfSAlex Elder 		ceph_release_page_vector(osd_data->pages, num_pages);
306c54d47bfSAlex Elder 	}
3075476492fSAlex Elder 	ceph_osd_data_init(osd_data);
3085476492fSAlex Elder }
3095476492fSAlex Elder 
3105476492fSAlex Elder static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
3115476492fSAlex Elder 			unsigned int which)
3125476492fSAlex Elder {
3135476492fSAlex Elder 	struct ceph_osd_req_op *op;
3145476492fSAlex Elder 
3155476492fSAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
3165476492fSAlex Elder 	op = &osd_req->r_ops[which];
3175476492fSAlex Elder 
3185476492fSAlex Elder 	switch (op->op) {
3195476492fSAlex Elder 	case CEPH_OSD_OP_READ:
3205476492fSAlex Elder 	case CEPH_OSD_OP_WRITE:
321e30b7577SIlya Dryomov 	case CEPH_OSD_OP_WRITEFULL:
3225476492fSAlex Elder 		ceph_osd_data_release(&op->extent.osd_data);
3235476492fSAlex Elder 		break;
3245476492fSAlex Elder 	case CEPH_OSD_OP_CALL:
3255476492fSAlex Elder 		ceph_osd_data_release(&op->cls.request_info);
32604017e29SAlex Elder 		ceph_osd_data_release(&op->cls.request_data);
3275476492fSAlex Elder 		ceph_osd_data_release(&op->cls.response_data);
3285476492fSAlex Elder 		break;
329d74b50beSYan, Zheng 	case CEPH_OSD_OP_SETXATTR:
330d74b50beSYan, Zheng 	case CEPH_OSD_OP_CMPXATTR:
331d74b50beSYan, Zheng 		ceph_osd_data_release(&op->xattr.osd_data);
332d74b50beSYan, Zheng 		break;
33366ba609fSYan, Zheng 	case CEPH_OSD_OP_STAT:
33466ba609fSYan, Zheng 		ceph_osd_data_release(&op->raw_data_in);
33566ba609fSYan, Zheng 		break;
336922dab61SIlya Dryomov 	case CEPH_OSD_OP_NOTIFY_ACK:
337922dab61SIlya Dryomov 		ceph_osd_data_release(&op->notify_ack.request_data);
338922dab61SIlya Dryomov 		break;
33919079203SIlya Dryomov 	case CEPH_OSD_OP_NOTIFY:
34019079203SIlya Dryomov 		ceph_osd_data_release(&op->notify.request_data);
34119079203SIlya Dryomov 		ceph_osd_data_release(&op->notify.response_data);
34219079203SIlya Dryomov 		break;
343a4ed38d7SDouglas Fuller 	case CEPH_OSD_OP_LIST_WATCHERS:
344a4ed38d7SDouglas Fuller 		ceph_osd_data_release(&op->list_watchers.response_data);
345a4ed38d7SDouglas Fuller 		break;
3465476492fSAlex Elder 	default:
3475476492fSAlex Elder 		break;
3485476492fSAlex Elder 	}
349c54d47bfSAlex Elder }
350c54d47bfSAlex Elder 
3513d14c5d2SYehuda Sadeh /*
35263244fa1SIlya Dryomov  * Assumes @t is zero-initialized.
35363244fa1SIlya Dryomov  */
35463244fa1SIlya Dryomov static void target_init(struct ceph_osd_request_target *t)
35563244fa1SIlya Dryomov {
35663244fa1SIlya Dryomov 	ceph_oid_init(&t->base_oid);
35763244fa1SIlya Dryomov 	ceph_oloc_init(&t->base_oloc);
35863244fa1SIlya Dryomov 	ceph_oid_init(&t->target_oid);
35963244fa1SIlya Dryomov 	ceph_oloc_init(&t->target_oloc);
36063244fa1SIlya Dryomov 
36163244fa1SIlya Dryomov 	ceph_osds_init(&t->acting);
36263244fa1SIlya Dryomov 	ceph_osds_init(&t->up);
36363244fa1SIlya Dryomov 	t->size = -1;
36463244fa1SIlya Dryomov 	t->min_size = -1;
36563244fa1SIlya Dryomov 
36663244fa1SIlya Dryomov 	t->osd = CEPH_HOMELESS_OSD;
36763244fa1SIlya Dryomov }
36863244fa1SIlya Dryomov 
369922dab61SIlya Dryomov static void target_copy(struct ceph_osd_request_target *dest,
370922dab61SIlya Dryomov 			const struct ceph_osd_request_target *src)
371922dab61SIlya Dryomov {
372922dab61SIlya Dryomov 	ceph_oid_copy(&dest->base_oid, &src->base_oid);
373922dab61SIlya Dryomov 	ceph_oloc_copy(&dest->base_oloc, &src->base_oloc);
374922dab61SIlya Dryomov 	ceph_oid_copy(&dest->target_oid, &src->target_oid);
375922dab61SIlya Dryomov 	ceph_oloc_copy(&dest->target_oloc, &src->target_oloc);
376922dab61SIlya Dryomov 
377922dab61SIlya Dryomov 	dest->pgid = src->pgid; /* struct */
378dc98ff72SIlya Dryomov 	dest->spgid = src->spgid; /* struct */
379922dab61SIlya Dryomov 	dest->pg_num = src->pg_num;
380922dab61SIlya Dryomov 	dest->pg_num_mask = src->pg_num_mask;
381922dab61SIlya Dryomov 	ceph_osds_copy(&dest->acting, &src->acting);
382922dab61SIlya Dryomov 	ceph_osds_copy(&dest->up, &src->up);
383922dab61SIlya Dryomov 	dest->size = src->size;
384922dab61SIlya Dryomov 	dest->min_size = src->min_size;
385922dab61SIlya Dryomov 	dest->sort_bitwise = src->sort_bitwise;
386922dab61SIlya Dryomov 
387922dab61SIlya Dryomov 	dest->flags = src->flags;
388922dab61SIlya Dryomov 	dest->paused = src->paused;
389922dab61SIlya Dryomov 
39004c7d789SIlya Dryomov 	dest->epoch = src->epoch;
391dc93e0e2SIlya Dryomov 	dest->last_force_resend = src->last_force_resend;
392dc93e0e2SIlya Dryomov 
393922dab61SIlya Dryomov 	dest->osd = src->osd;
394922dab61SIlya Dryomov }
395922dab61SIlya Dryomov 
39663244fa1SIlya Dryomov static void target_destroy(struct ceph_osd_request_target *t)
39763244fa1SIlya Dryomov {
39863244fa1SIlya Dryomov 	ceph_oid_destroy(&t->base_oid);
39930c156d9SYan, Zheng 	ceph_oloc_destroy(&t->base_oloc);
40063244fa1SIlya Dryomov 	ceph_oid_destroy(&t->target_oid);
40130c156d9SYan, Zheng 	ceph_oloc_destroy(&t->target_oloc);
40263244fa1SIlya Dryomov }
40363244fa1SIlya Dryomov 
40463244fa1SIlya Dryomov /*
4053d14c5d2SYehuda Sadeh  * requests
4063d14c5d2SYehuda Sadeh  */
4073540bfdbSIlya Dryomov static void request_release_checks(struct ceph_osd_request *req)
4083540bfdbSIlya Dryomov {
4093540bfdbSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&req->r_node));
4104609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&req->r_mc_node));
4113540bfdbSIlya Dryomov 	WARN_ON(!list_empty(&req->r_unsafe_item));
4123540bfdbSIlya Dryomov 	WARN_ON(req->r_osd);
4133540bfdbSIlya Dryomov }
4143540bfdbSIlya Dryomov 
4159e94af20SIlya Dryomov static void ceph_osdc_release_request(struct kref *kref)
4163d14c5d2SYehuda Sadeh {
4179e94af20SIlya Dryomov 	struct ceph_osd_request *req = container_of(kref,
4189e94af20SIlya Dryomov 					    struct ceph_osd_request, r_kref);
4195476492fSAlex Elder 	unsigned int which;
4203d14c5d2SYehuda Sadeh 
4219e94af20SIlya Dryomov 	dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
4229e94af20SIlya Dryomov 	     req->r_request, req->r_reply);
4233540bfdbSIlya Dryomov 	request_release_checks(req);
4249e94af20SIlya Dryomov 
4253d14c5d2SYehuda Sadeh 	if (req->r_request)
4263d14c5d2SYehuda Sadeh 		ceph_msg_put(req->r_request);
4275aea3dcdSIlya Dryomov 	if (req->r_reply)
428ab8cb34aSAlex Elder 		ceph_msg_put(req->r_reply);
4290fff87ecSAlex Elder 
4305476492fSAlex Elder 	for (which = 0; which < req->r_num_ops; which++)
4315476492fSAlex Elder 		osd_req_op_data_release(req, which);
4320fff87ecSAlex Elder 
433a66dd383SIlya Dryomov 	target_destroy(&req->r_t);
4343d14c5d2SYehuda Sadeh 	ceph_put_snap_context(req->r_snapc);
435d30291b9SIlya Dryomov 
4363d14c5d2SYehuda Sadeh 	if (req->r_mempool)
4373d14c5d2SYehuda Sadeh 		mempool_free(req, req->r_osdc->req_mempool);
4383f1af42aSIlya Dryomov 	else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS)
4395522ae0bSAlex Elder 		kmem_cache_free(ceph_osd_request_cache, req);
4403f1af42aSIlya Dryomov 	else
4413f1af42aSIlya Dryomov 		kfree(req);
4423d14c5d2SYehuda Sadeh }
4439e94af20SIlya Dryomov 
4449e94af20SIlya Dryomov void ceph_osdc_get_request(struct ceph_osd_request *req)
4459e94af20SIlya Dryomov {
4469e94af20SIlya Dryomov 	dout("%s %p (was %d)\n", __func__, req,
4472c935bc5SPeter Zijlstra 	     kref_read(&req->r_kref));
4489e94af20SIlya Dryomov 	kref_get(&req->r_kref);
4499e94af20SIlya Dryomov }
4509e94af20SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_get_request);
4519e94af20SIlya Dryomov 
4529e94af20SIlya Dryomov void ceph_osdc_put_request(struct ceph_osd_request *req)
4539e94af20SIlya Dryomov {
4543ed97d63SIlya Dryomov 	if (req) {
4559e94af20SIlya Dryomov 		dout("%s %p (was %d)\n", __func__, req,
4562c935bc5SPeter Zijlstra 		     kref_read(&req->r_kref));
4579e94af20SIlya Dryomov 		kref_put(&req->r_kref, ceph_osdc_release_request);
4589e94af20SIlya Dryomov 	}
4593ed97d63SIlya Dryomov }
4609e94af20SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_put_request);
4613d14c5d2SYehuda Sadeh 
4623540bfdbSIlya Dryomov static void request_init(struct ceph_osd_request *req)
4633540bfdbSIlya Dryomov {
4643540bfdbSIlya Dryomov 	/* req only, each op is zeroed in _osd_req_op_init() */
4653540bfdbSIlya Dryomov 	memset(req, 0, sizeof(*req));
4663540bfdbSIlya Dryomov 
4673540bfdbSIlya Dryomov 	kref_init(&req->r_kref);
4683540bfdbSIlya Dryomov 	init_completion(&req->r_completion);
4693540bfdbSIlya Dryomov 	RB_CLEAR_NODE(&req->r_node);
4704609245eSIlya Dryomov 	RB_CLEAR_NODE(&req->r_mc_node);
4713540bfdbSIlya Dryomov 	INIT_LIST_HEAD(&req->r_unsafe_item);
4723540bfdbSIlya Dryomov 
4733540bfdbSIlya Dryomov 	target_init(&req->r_t);
4743540bfdbSIlya Dryomov }
4753540bfdbSIlya Dryomov 
476922dab61SIlya Dryomov /*
477922dab61SIlya Dryomov  * This is ugly, but it allows us to reuse linger registration and ping
478922dab61SIlya Dryomov  * requests, keeping the structure of the code around send_linger{_ping}()
479922dab61SIlya Dryomov  * reasonable.  Setting up a min_nr=2 mempool for each linger request
480922dab61SIlya Dryomov  * and dealing with copying ops (this blasts req only, watch op remains
481922dab61SIlya Dryomov  * intact) isn't any better.
482922dab61SIlya Dryomov  */
483922dab61SIlya Dryomov static void request_reinit(struct ceph_osd_request *req)
484922dab61SIlya Dryomov {
485922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
486922dab61SIlya Dryomov 	bool mempool = req->r_mempool;
487922dab61SIlya Dryomov 	unsigned int num_ops = req->r_num_ops;
488922dab61SIlya Dryomov 	u64 snapid = req->r_snapid;
489922dab61SIlya Dryomov 	struct ceph_snap_context *snapc = req->r_snapc;
490922dab61SIlya Dryomov 	bool linger = req->r_linger;
491922dab61SIlya Dryomov 	struct ceph_msg *request_msg = req->r_request;
492922dab61SIlya Dryomov 	struct ceph_msg *reply_msg = req->r_reply;
493922dab61SIlya Dryomov 
494922dab61SIlya Dryomov 	dout("%s req %p\n", __func__, req);
4952c935bc5SPeter Zijlstra 	WARN_ON(kref_read(&req->r_kref) != 1);
496922dab61SIlya Dryomov 	request_release_checks(req);
497922dab61SIlya Dryomov 
4982c935bc5SPeter Zijlstra 	WARN_ON(kref_read(&request_msg->kref) != 1);
4992c935bc5SPeter Zijlstra 	WARN_ON(kref_read(&reply_msg->kref) != 1);
500922dab61SIlya Dryomov 	target_destroy(&req->r_t);
501922dab61SIlya Dryomov 
502922dab61SIlya Dryomov 	request_init(req);
503922dab61SIlya Dryomov 	req->r_osdc = osdc;
504922dab61SIlya Dryomov 	req->r_mempool = mempool;
505922dab61SIlya Dryomov 	req->r_num_ops = num_ops;
506922dab61SIlya Dryomov 	req->r_snapid = snapid;
507922dab61SIlya Dryomov 	req->r_snapc = snapc;
508922dab61SIlya Dryomov 	req->r_linger = linger;
509922dab61SIlya Dryomov 	req->r_request = request_msg;
510922dab61SIlya Dryomov 	req->r_reply = reply_msg;
511922dab61SIlya Dryomov }
512922dab61SIlya Dryomov 
5133d14c5d2SYehuda Sadeh struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
5143d14c5d2SYehuda Sadeh 					       struct ceph_snap_context *snapc,
5151b83bef2SSage Weil 					       unsigned int num_ops,
5163d14c5d2SYehuda Sadeh 					       bool use_mempool,
51754a54007SAlex Elder 					       gfp_t gfp_flags)
5183d14c5d2SYehuda Sadeh {
5193d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
5203d14c5d2SYehuda Sadeh 
5213d14c5d2SYehuda Sadeh 	if (use_mempool) {
5223f1af42aSIlya Dryomov 		BUG_ON(num_ops > CEPH_OSD_SLAB_OPS);
5233d14c5d2SYehuda Sadeh 		req = mempool_alloc(osdc->req_mempool, gfp_flags);
5243f1af42aSIlya Dryomov 	} else if (num_ops <= CEPH_OSD_SLAB_OPS) {
5253f1af42aSIlya Dryomov 		req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
5263d14c5d2SYehuda Sadeh 	} else {
5273f1af42aSIlya Dryomov 		BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
5283f1af42aSIlya Dryomov 		req = kmalloc(sizeof(*req) + num_ops * sizeof(req->r_ops[0]),
5293f1af42aSIlya Dryomov 			      gfp_flags);
5303d14c5d2SYehuda Sadeh 	}
5313f1af42aSIlya Dryomov 	if (unlikely(!req))
5323d14c5d2SYehuda Sadeh 		return NULL;
5333d14c5d2SYehuda Sadeh 
5343540bfdbSIlya Dryomov 	request_init(req);
5353d14c5d2SYehuda Sadeh 	req->r_osdc = osdc;
5363d14c5d2SYehuda Sadeh 	req->r_mempool = use_mempool;
53779528734SAlex Elder 	req->r_num_ops = num_ops;
53884127282SIlya Dryomov 	req->r_snapid = CEPH_NOSNAP;
53984127282SIlya Dryomov 	req->r_snapc = ceph_get_snap_context(snapc);
5403d14c5d2SYehuda Sadeh 
54113d1ad16SIlya Dryomov 	dout("%s req %p\n", __func__, req);
54213d1ad16SIlya Dryomov 	return req;
5433f1af42aSIlya Dryomov }
54413d1ad16SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_alloc_request);
5453f1af42aSIlya Dryomov 
5462e59ffd1SIlya Dryomov static int ceph_oloc_encoding_size(const struct ceph_object_locator *oloc)
54730c156d9SYan, Zheng {
54830c156d9SYan, Zheng 	return 8 + 4 + 4 + 4 + (oloc->pool_ns ? oloc->pool_ns->len : 0);
54930c156d9SYan, Zheng }
55030c156d9SYan, Zheng 
55113d1ad16SIlya Dryomov int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp)
55213d1ad16SIlya Dryomov {
55313d1ad16SIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
55413d1ad16SIlya Dryomov 	struct ceph_msg *msg;
55513d1ad16SIlya Dryomov 	int msg_size;
5563d14c5d2SYehuda Sadeh 
557d30291b9SIlya Dryomov 	WARN_ON(ceph_oid_empty(&req->r_base_oid));
55830c156d9SYan, Zheng 	WARN_ON(ceph_oloc_empty(&req->r_base_oloc));
559d30291b9SIlya Dryomov 
56013d1ad16SIlya Dryomov 	/* create request message */
5618cb441c0SIlya Dryomov 	msg_size = CEPH_ENCODING_START_BLK_LEN +
5628cb441c0SIlya Dryomov 			CEPH_PGID_ENCODING_LEN + 1; /* spgid */
5638cb441c0SIlya Dryomov 	msg_size += 4 + 4 + 4; /* hash, osdmap_epoch, flags */
5648cb441c0SIlya Dryomov 	msg_size += CEPH_ENCODING_START_BLK_LEN +
5658cb441c0SIlya Dryomov 			sizeof(struct ceph_osd_reqid); /* reqid */
5668cb441c0SIlya Dryomov 	msg_size += sizeof(struct ceph_blkin_trace_info); /* trace */
5678cb441c0SIlya Dryomov 	msg_size += 4 + sizeof(struct ceph_timespec); /* client_inc, mtime */
56830c156d9SYan, Zheng 	msg_size += CEPH_ENCODING_START_BLK_LEN +
56930c156d9SYan, Zheng 			ceph_oloc_encoding_size(&req->r_base_oloc); /* oloc */
57013d1ad16SIlya Dryomov 	msg_size += 4 + req->r_base_oid.name_len; /* oid */
57113d1ad16SIlya Dryomov 	msg_size += 2 + req->r_num_ops * sizeof(struct ceph_osd_op);
572ae458f5aSIlya Dryomov 	msg_size += 8; /* snapid */
573ae458f5aSIlya Dryomov 	msg_size += 8; /* snap_seq */
57413d1ad16SIlya Dryomov 	msg_size += 4 + 8 * (req->r_snapc ? req->r_snapc->num_snaps : 0);
5758cb441c0SIlya Dryomov 	msg_size += 4 + 8; /* retry_attempt, features */
576ae458f5aSIlya Dryomov 
57713d1ad16SIlya Dryomov 	if (req->r_mempool)
5783d14c5d2SYehuda Sadeh 		msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
5793d14c5d2SYehuda Sadeh 	else
58013d1ad16SIlya Dryomov 		msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp, true);
58113d1ad16SIlya Dryomov 	if (!msg)
58213d1ad16SIlya Dryomov 		return -ENOMEM;
5833d14c5d2SYehuda Sadeh 
5843d14c5d2SYehuda Sadeh 	memset(msg->front.iov_base, 0, msg->front.iov_len);
5853d14c5d2SYehuda Sadeh 	req->r_request = msg;
5863d14c5d2SYehuda Sadeh 
58713d1ad16SIlya Dryomov 	/* create reply message */
58813d1ad16SIlya Dryomov 	msg_size = OSD_OPREPLY_FRONT_LEN;
589711da55dSIlya Dryomov 	msg_size += req->r_base_oid.name_len;
590711da55dSIlya Dryomov 	msg_size += req->r_num_ops * sizeof(struct ceph_osd_op);
59113d1ad16SIlya Dryomov 
59213d1ad16SIlya Dryomov 	if (req->r_mempool)
59313d1ad16SIlya Dryomov 		msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
59413d1ad16SIlya Dryomov 	else
59513d1ad16SIlya Dryomov 		msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, msg_size, gfp, true);
59613d1ad16SIlya Dryomov 	if (!msg)
59713d1ad16SIlya Dryomov 		return -ENOMEM;
59813d1ad16SIlya Dryomov 
59913d1ad16SIlya Dryomov 	req->r_reply = msg;
60013d1ad16SIlya Dryomov 
60113d1ad16SIlya Dryomov 	return 0;
60213d1ad16SIlya Dryomov }
60313d1ad16SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_alloc_messages);
6043d14c5d2SYehuda Sadeh 
605a8dd0a37SAlex Elder static bool osd_req_opcode_valid(u16 opcode)
606a8dd0a37SAlex Elder {
607a8dd0a37SAlex Elder 	switch (opcode) {
60870b5bfa3SIlya Dryomov #define GENERATE_CASE(op, opcode, str)	case CEPH_OSD_OP_##op: return true;
60970b5bfa3SIlya Dryomov __CEPH_FORALL_OSD_OPS(GENERATE_CASE)
61070b5bfa3SIlya Dryomov #undef GENERATE_CASE
611a8dd0a37SAlex Elder 	default:
612a8dd0a37SAlex Elder 		return false;
613a8dd0a37SAlex Elder 	}
614a8dd0a37SAlex Elder }
615a8dd0a37SAlex Elder 
61633803f33SAlex Elder /*
61733803f33SAlex Elder  * This is an osd op init function for opcodes that have no data or
61833803f33SAlex Elder  * other information associated with them.  It also serves as a
61933803f33SAlex Elder  * common init routine for all the other init functions, below.
62033803f33SAlex Elder  */
621c99d2d4aSAlex Elder static struct ceph_osd_req_op *
62249719778SAlex Elder _osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
623144cba14SYan, Zheng 		 u16 opcode, u32 flags)
62433803f33SAlex Elder {
625c99d2d4aSAlex Elder 	struct ceph_osd_req_op *op;
626c99d2d4aSAlex Elder 
627c99d2d4aSAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
62833803f33SAlex Elder 	BUG_ON(!osd_req_opcode_valid(opcode));
62933803f33SAlex Elder 
630c99d2d4aSAlex Elder 	op = &osd_req->r_ops[which];
63133803f33SAlex Elder 	memset(op, 0, sizeof (*op));
63233803f33SAlex Elder 	op->op = opcode;
633144cba14SYan, Zheng 	op->flags = flags;
634c99d2d4aSAlex Elder 
635c99d2d4aSAlex Elder 	return op;
63633803f33SAlex Elder }
63733803f33SAlex Elder 
63849719778SAlex Elder void osd_req_op_init(struct ceph_osd_request *osd_req,
639144cba14SYan, Zheng 		     unsigned int which, u16 opcode, u32 flags)
64049719778SAlex Elder {
641144cba14SYan, Zheng 	(void)_osd_req_op_init(osd_req, which, opcode, flags);
64249719778SAlex Elder }
64349719778SAlex Elder EXPORT_SYMBOL(osd_req_op_init);
64449719778SAlex Elder 
645c99d2d4aSAlex Elder void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
646c99d2d4aSAlex Elder 				unsigned int which, u16 opcode,
64733803f33SAlex Elder 				u64 offset, u64 length,
64833803f33SAlex Elder 				u64 truncate_size, u32 truncate_seq)
64933803f33SAlex Elder {
650144cba14SYan, Zheng 	struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
651144cba14SYan, Zheng 						      opcode, 0);
65233803f33SAlex Elder 	size_t payload_len = 0;
65333803f33SAlex Elder 
654ad7a60deSLi Wang 	BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
655e30b7577SIlya Dryomov 	       opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
656e30b7577SIlya Dryomov 	       opcode != CEPH_OSD_OP_TRUNCATE);
65733803f33SAlex Elder 
65833803f33SAlex Elder 	op->extent.offset = offset;
65933803f33SAlex Elder 	op->extent.length = length;
66033803f33SAlex Elder 	op->extent.truncate_size = truncate_size;
66133803f33SAlex Elder 	op->extent.truncate_seq = truncate_seq;
662e30b7577SIlya Dryomov 	if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
66333803f33SAlex Elder 		payload_len += length;
66433803f33SAlex Elder 
665de2aa102SIlya Dryomov 	op->indata_len = payload_len;
66633803f33SAlex Elder }
66733803f33SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_init);
66833803f33SAlex Elder 
669c99d2d4aSAlex Elder void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
670c99d2d4aSAlex Elder 				unsigned int which, u64 length)
671e5975c7cSAlex Elder {
672c99d2d4aSAlex Elder 	struct ceph_osd_req_op *op;
673c99d2d4aSAlex Elder 	u64 previous;
674c99d2d4aSAlex Elder 
675c99d2d4aSAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
676c99d2d4aSAlex Elder 	op = &osd_req->r_ops[which];
677c99d2d4aSAlex Elder 	previous = op->extent.length;
678e5975c7cSAlex Elder 
679e5975c7cSAlex Elder 	if (length == previous)
680e5975c7cSAlex Elder 		return;		/* Nothing to do */
681e5975c7cSAlex Elder 	BUG_ON(length > previous);
682e5975c7cSAlex Elder 
683e5975c7cSAlex Elder 	op->extent.length = length;
684d641df81SYan, Zheng 	if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
685de2aa102SIlya Dryomov 		op->indata_len -= previous - length;
686e5975c7cSAlex Elder }
687e5975c7cSAlex Elder EXPORT_SYMBOL(osd_req_op_extent_update);
688e5975c7cSAlex Elder 
6892c63f49aSYan, Zheng void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
6902c63f49aSYan, Zheng 				unsigned int which, u64 offset_inc)
6912c63f49aSYan, Zheng {
6922c63f49aSYan, Zheng 	struct ceph_osd_req_op *op, *prev_op;
6932c63f49aSYan, Zheng 
6942c63f49aSYan, Zheng 	BUG_ON(which + 1 >= osd_req->r_num_ops);
6952c63f49aSYan, Zheng 
6962c63f49aSYan, Zheng 	prev_op = &osd_req->r_ops[which];
6972c63f49aSYan, Zheng 	op = _osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags);
6982c63f49aSYan, Zheng 	/* dup previous one */
6992c63f49aSYan, Zheng 	op->indata_len = prev_op->indata_len;
7002c63f49aSYan, Zheng 	op->outdata_len = prev_op->outdata_len;
7012c63f49aSYan, Zheng 	op->extent = prev_op->extent;
7022c63f49aSYan, Zheng 	/* adjust offset */
7032c63f49aSYan, Zheng 	op->extent.offset += offset_inc;
7042c63f49aSYan, Zheng 	op->extent.length -= offset_inc;
7052c63f49aSYan, Zheng 
7062c63f49aSYan, Zheng 	if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
7072c63f49aSYan, Zheng 		op->indata_len -= offset_inc;
7082c63f49aSYan, Zheng }
7092c63f49aSYan, Zheng EXPORT_SYMBOL(osd_req_op_extent_dup_last);
7102c63f49aSYan, Zheng 
711c99d2d4aSAlex Elder void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
71204017e29SAlex Elder 			u16 opcode, const char *class, const char *method)
71333803f33SAlex Elder {
714144cba14SYan, Zheng 	struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
715144cba14SYan, Zheng 						      opcode, 0);
7165f562df5SAlex Elder 	struct ceph_pagelist *pagelist;
71733803f33SAlex Elder 	size_t payload_len = 0;
71833803f33SAlex Elder 	size_t size;
71933803f33SAlex Elder 
72033803f33SAlex Elder 	BUG_ON(opcode != CEPH_OSD_OP_CALL);
72133803f33SAlex Elder 
7225f562df5SAlex Elder 	pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
7235f562df5SAlex Elder 	BUG_ON(!pagelist);
7245f562df5SAlex Elder 	ceph_pagelist_init(pagelist);
7255f562df5SAlex Elder 
72633803f33SAlex Elder 	op->cls.class_name = class;
72733803f33SAlex Elder 	size = strlen(class);
72833803f33SAlex Elder 	BUG_ON(size > (size_t) U8_MAX);
72933803f33SAlex Elder 	op->cls.class_len = size;
7305f562df5SAlex Elder 	ceph_pagelist_append(pagelist, class, size);
73133803f33SAlex Elder 	payload_len += size;
73233803f33SAlex Elder 
73333803f33SAlex Elder 	op->cls.method_name = method;
73433803f33SAlex Elder 	size = strlen(method);
73533803f33SAlex Elder 	BUG_ON(size > (size_t) U8_MAX);
73633803f33SAlex Elder 	op->cls.method_len = size;
7375f562df5SAlex Elder 	ceph_pagelist_append(pagelist, method, size);
73833803f33SAlex Elder 	payload_len += size;
73933803f33SAlex Elder 
740a4ce40a9SAlex Elder 	osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
7415f562df5SAlex Elder 
742de2aa102SIlya Dryomov 	op->indata_len = payload_len;
74333803f33SAlex Elder }
74433803f33SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_init);
7458c042b0dSAlex Elder 
746d74b50beSYan, Zheng int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
747d74b50beSYan, Zheng 			  u16 opcode, const char *name, const void *value,
748d74b50beSYan, Zheng 			  size_t size, u8 cmp_op, u8 cmp_mode)
749d74b50beSYan, Zheng {
750144cba14SYan, Zheng 	struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
751144cba14SYan, Zheng 						      opcode, 0);
752d74b50beSYan, Zheng 	struct ceph_pagelist *pagelist;
753d74b50beSYan, Zheng 	size_t payload_len;
754d74b50beSYan, Zheng 
755d74b50beSYan, Zheng 	BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
756d74b50beSYan, Zheng 
757d74b50beSYan, Zheng 	pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
758d74b50beSYan, Zheng 	if (!pagelist)
759d74b50beSYan, Zheng 		return -ENOMEM;
760d74b50beSYan, Zheng 
761d74b50beSYan, Zheng 	ceph_pagelist_init(pagelist);
762d74b50beSYan, Zheng 
763d74b50beSYan, Zheng 	payload_len = strlen(name);
764d74b50beSYan, Zheng 	op->xattr.name_len = payload_len;
765d74b50beSYan, Zheng 	ceph_pagelist_append(pagelist, name, payload_len);
766d74b50beSYan, Zheng 
767d74b50beSYan, Zheng 	op->xattr.value_len = size;
768d74b50beSYan, Zheng 	ceph_pagelist_append(pagelist, value, size);
769d74b50beSYan, Zheng 	payload_len += size;
770d74b50beSYan, Zheng 
771d74b50beSYan, Zheng 	op->xattr.cmp_op = cmp_op;
772d74b50beSYan, Zheng 	op->xattr.cmp_mode = cmp_mode;
773d74b50beSYan, Zheng 
774d74b50beSYan, Zheng 	ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
775de2aa102SIlya Dryomov 	op->indata_len = payload_len;
776d74b50beSYan, Zheng 	return 0;
777d74b50beSYan, Zheng }
778d74b50beSYan, Zheng EXPORT_SYMBOL(osd_req_op_xattr_init);
779d74b50beSYan, Zheng 
780922dab61SIlya Dryomov /*
781922dab61SIlya Dryomov  * @watch_opcode: CEPH_OSD_WATCH_OP_*
782922dab61SIlya Dryomov  */
783922dab61SIlya Dryomov static void osd_req_op_watch_init(struct ceph_osd_request *req, int which,
784922dab61SIlya Dryomov 				  u64 cookie, u8 watch_opcode)
78533803f33SAlex Elder {
786922dab61SIlya Dryomov 	struct ceph_osd_req_op *op;
78733803f33SAlex Elder 
788922dab61SIlya Dryomov 	op = _osd_req_op_init(req, which, CEPH_OSD_OP_WATCH, 0);
78933803f33SAlex Elder 	op->watch.cookie = cookie;
790922dab61SIlya Dryomov 	op->watch.op = watch_opcode;
791922dab61SIlya Dryomov 	op->watch.gen = 0;
79233803f33SAlex Elder }
79333803f33SAlex Elder 
794c647b8a8SIlya Dryomov void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
795c647b8a8SIlya Dryomov 				unsigned int which,
796c647b8a8SIlya Dryomov 				u64 expected_object_size,
797c647b8a8SIlya Dryomov 				u64 expected_write_size)
798c647b8a8SIlya Dryomov {
799c647b8a8SIlya Dryomov 	struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
800144cba14SYan, Zheng 						      CEPH_OSD_OP_SETALLOCHINT,
801144cba14SYan, Zheng 						      0);
802c647b8a8SIlya Dryomov 
803c647b8a8SIlya Dryomov 	op->alloc_hint.expected_object_size = expected_object_size;
804c647b8a8SIlya Dryomov 	op->alloc_hint.expected_write_size = expected_write_size;
805c647b8a8SIlya Dryomov 
806c647b8a8SIlya Dryomov 	/*
807c647b8a8SIlya Dryomov 	 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
808c647b8a8SIlya Dryomov 	 * not worth a feature bit.  Set FAILOK per-op flag to make
809c647b8a8SIlya Dryomov 	 * sure older osds don't trip over an unsupported opcode.
810c647b8a8SIlya Dryomov 	 */
811c647b8a8SIlya Dryomov 	op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
812c647b8a8SIlya Dryomov }
813c647b8a8SIlya Dryomov EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
814c647b8a8SIlya Dryomov 
81590af3602SAlex Elder static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
816ec9123c5SAlex Elder 				struct ceph_osd_data *osd_data)
817ec9123c5SAlex Elder {
818ec9123c5SAlex Elder 	u64 length = ceph_osd_data_length(osd_data);
819ec9123c5SAlex Elder 
820ec9123c5SAlex Elder 	if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
821ec9123c5SAlex Elder 		BUG_ON(length > (u64) SIZE_MAX);
822ec9123c5SAlex Elder 		if (length)
82390af3602SAlex Elder 			ceph_msg_data_add_pages(msg, osd_data->pages,
824ec9123c5SAlex Elder 					length, osd_data->alignment);
825ec9123c5SAlex Elder 	} else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
826ec9123c5SAlex Elder 		BUG_ON(!length);
82790af3602SAlex Elder 		ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
828ec9123c5SAlex Elder #ifdef CONFIG_BLOCK
829ec9123c5SAlex Elder 	} else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
83090af3602SAlex Elder 		ceph_msg_data_add_bio(msg, osd_data->bio, length);
831ec9123c5SAlex Elder #endif
832ec9123c5SAlex Elder 	} else {
833ec9123c5SAlex Elder 		BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
834ec9123c5SAlex Elder 	}
835ec9123c5SAlex Elder }
836ec9123c5SAlex Elder 
837bb873b53SIlya Dryomov static u32 osd_req_encode_op(struct ceph_osd_op *dst,
838bb873b53SIlya Dryomov 			     const struct ceph_osd_req_op *src)
8393d14c5d2SYehuda Sadeh {
840a8dd0a37SAlex Elder 	if (WARN_ON(!osd_req_opcode_valid(src->op))) {
841a8dd0a37SAlex Elder 		pr_err("unrecognized osd opcode %d\n", src->op);
842a8dd0a37SAlex Elder 
843a8dd0a37SAlex Elder 		return 0;
844a8dd0a37SAlex Elder 	}
8453d14c5d2SYehuda Sadeh 
846065a68f9SAlex Elder 	switch (src->op) {
847fbfab539SAlex Elder 	case CEPH_OSD_OP_STAT:
848fbfab539SAlex Elder 		break;
8493d14c5d2SYehuda Sadeh 	case CEPH_OSD_OP_READ:
8503d14c5d2SYehuda Sadeh 	case CEPH_OSD_OP_WRITE:
851e30b7577SIlya Dryomov 	case CEPH_OSD_OP_WRITEFULL:
852ad7a60deSLi Wang 	case CEPH_OSD_OP_ZERO:
853ad7a60deSLi Wang 	case CEPH_OSD_OP_TRUNCATE:
854175face2SAlex Elder 		dst->extent.offset = cpu_to_le64(src->extent.offset);
855175face2SAlex Elder 		dst->extent.length = cpu_to_le64(src->extent.length);
8563d14c5d2SYehuda Sadeh 		dst->extent.truncate_size =
8573d14c5d2SYehuda Sadeh 			cpu_to_le64(src->extent.truncate_size);
8583d14c5d2SYehuda Sadeh 		dst->extent.truncate_seq =
8593d14c5d2SYehuda Sadeh 			cpu_to_le32(src->extent.truncate_seq);
8603d14c5d2SYehuda Sadeh 		break;
8613d14c5d2SYehuda Sadeh 	case CEPH_OSD_OP_CALL:
8623d14c5d2SYehuda Sadeh 		dst->cls.class_len = src->cls.class_len;
8633d14c5d2SYehuda Sadeh 		dst->cls.method_len = src->cls.method_len;
864bb873b53SIlya Dryomov 		dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
8653d14c5d2SYehuda Sadeh 		break;
8663d14c5d2SYehuda Sadeh 	case CEPH_OSD_OP_STARTSYNC:
8673d14c5d2SYehuda Sadeh 		break;
868a40c4f10SYehuda Sadeh 	case CEPH_OSD_OP_WATCH:
869a40c4f10SYehuda Sadeh 		dst->watch.cookie = cpu_to_le64(src->watch.cookie);
870922dab61SIlya Dryomov 		dst->watch.ver = cpu_to_le64(0);
871922dab61SIlya Dryomov 		dst->watch.op = src->watch.op;
872922dab61SIlya Dryomov 		dst->watch.gen = cpu_to_le32(src->watch.gen);
873922dab61SIlya Dryomov 		break;
874922dab61SIlya Dryomov 	case CEPH_OSD_OP_NOTIFY_ACK:
875a40c4f10SYehuda Sadeh 		break;
87619079203SIlya Dryomov 	case CEPH_OSD_OP_NOTIFY:
87719079203SIlya Dryomov 		dst->notify.cookie = cpu_to_le64(src->notify.cookie);
87819079203SIlya Dryomov 		break;
879a4ed38d7SDouglas Fuller 	case CEPH_OSD_OP_LIST_WATCHERS:
880a4ed38d7SDouglas Fuller 		break;
881c647b8a8SIlya Dryomov 	case CEPH_OSD_OP_SETALLOCHINT:
882c647b8a8SIlya Dryomov 		dst->alloc_hint.expected_object_size =
883c647b8a8SIlya Dryomov 		    cpu_to_le64(src->alloc_hint.expected_object_size);
884c647b8a8SIlya Dryomov 		dst->alloc_hint.expected_write_size =
885c647b8a8SIlya Dryomov 		    cpu_to_le64(src->alloc_hint.expected_write_size);
886c647b8a8SIlya Dryomov 		break;
887d74b50beSYan, Zheng 	case CEPH_OSD_OP_SETXATTR:
888d74b50beSYan, Zheng 	case CEPH_OSD_OP_CMPXATTR:
889d74b50beSYan, Zheng 		dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
890d74b50beSYan, Zheng 		dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
891d74b50beSYan, Zheng 		dst->xattr.cmp_op = src->xattr.cmp_op;
892d74b50beSYan, Zheng 		dst->xattr.cmp_mode = src->xattr.cmp_mode;
893d74b50beSYan, Zheng 		break;
894864e9197SYan, Zheng 	case CEPH_OSD_OP_CREATE:
895864e9197SYan, Zheng 	case CEPH_OSD_OP_DELETE:
896864e9197SYan, Zheng 		break;
8973d14c5d2SYehuda Sadeh 	default:
8984c46459cSAlex Elder 		pr_err("unsupported osd opcode %s\n",
8998f63ca2dSAlex Elder 			ceph_osd_op_name(src->op));
9004c46459cSAlex Elder 		WARN_ON(1);
901a8dd0a37SAlex Elder 
902a8dd0a37SAlex Elder 		return 0;
9033d14c5d2SYehuda Sadeh 	}
9047b25bf5fSIlya Dryomov 
905a8dd0a37SAlex Elder 	dst->op = cpu_to_le16(src->op);
9067b25bf5fSIlya Dryomov 	dst->flags = cpu_to_le32(src->flags);
907de2aa102SIlya Dryomov 	dst->payload_len = cpu_to_le32(src->indata_len);
908175face2SAlex Elder 
909bb873b53SIlya Dryomov 	return src->indata_len;
9103d14c5d2SYehuda Sadeh }
9113d14c5d2SYehuda Sadeh 
9123d14c5d2SYehuda Sadeh /*
9133d14c5d2SYehuda Sadeh  * build new request AND message, calculate layout, and adjust file
9143d14c5d2SYehuda Sadeh  * extent as needed.
9153d14c5d2SYehuda Sadeh  *
9163d14c5d2SYehuda Sadeh  * if the file was recently truncated, we include information about its
9173d14c5d2SYehuda Sadeh  * old and new size so that the object can be updated appropriately.  (we
9183d14c5d2SYehuda Sadeh  * avoid synchronously deleting truncated objects because it's slow.)
9193d14c5d2SYehuda Sadeh  *
9203d14c5d2SYehuda Sadeh  * if @do_sync, include a 'startsync' command so that the osd will flush
9213d14c5d2SYehuda Sadeh  * data quickly.
9223d14c5d2SYehuda Sadeh  */
9233d14c5d2SYehuda Sadeh struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
9243d14c5d2SYehuda Sadeh 					       struct ceph_file_layout *layout,
9253d14c5d2SYehuda Sadeh 					       struct ceph_vino vino,
926715e4cd4SYan, Zheng 					       u64 off, u64 *plen,
927715e4cd4SYan, Zheng 					       unsigned int which, int num_ops,
9283d14c5d2SYehuda Sadeh 					       int opcode, int flags,
9293d14c5d2SYehuda Sadeh 					       struct ceph_snap_context *snapc,
9303d14c5d2SYehuda Sadeh 					       u32 truncate_seq,
9313d14c5d2SYehuda Sadeh 					       u64 truncate_size,
932153e5167SAlex Elder 					       bool use_mempool)
9333d14c5d2SYehuda Sadeh {
9343d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
93575d1c941SAlex Elder 	u64 objnum = 0;
93675d1c941SAlex Elder 	u64 objoff = 0;
93775d1c941SAlex Elder 	u64 objlen = 0;
9386816282dSSage Weil 	int r;
9393d14c5d2SYehuda Sadeh 
940ad7a60deSLi Wang 	BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
941864e9197SYan, Zheng 	       opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
942864e9197SYan, Zheng 	       opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
9433d14c5d2SYehuda Sadeh 
944acead002SAlex Elder 	req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
945ae7ca4a3SAlex Elder 					GFP_NOFS);
94613d1ad16SIlya Dryomov 	if (!req) {
94713d1ad16SIlya Dryomov 		r = -ENOMEM;
94813d1ad16SIlya Dryomov 		goto fail;
94913d1ad16SIlya Dryomov 	}
95079528734SAlex Elder 
9513d14c5d2SYehuda Sadeh 	/* calculate max write size */
952a19dadfbSAlex Elder 	r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
95313d1ad16SIlya Dryomov 	if (r)
95413d1ad16SIlya Dryomov 		goto fail;
955a19dadfbSAlex Elder 
956864e9197SYan, Zheng 	if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
957144cba14SYan, Zheng 		osd_req_op_init(req, which, opcode, 0);
958864e9197SYan, Zheng 	} else {
9597627151eSYan, Zheng 		u32 object_size = layout->object_size;
960864e9197SYan, Zheng 		u32 object_base = off - objoff;
961ccca4e37SYan, Zheng 		if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
962d18d1e28SAlex Elder 			if (truncate_size <= object_base) {
963d18d1e28SAlex Elder 				truncate_size = 0;
964d18d1e28SAlex Elder 			} else {
965d18d1e28SAlex Elder 				truncate_size -= object_base;
966d18d1e28SAlex Elder 				if (truncate_size > object_size)
967d18d1e28SAlex Elder 					truncate_size = object_size;
968d18d1e28SAlex Elder 			}
969ccca4e37SYan, Zheng 		}
970715e4cd4SYan, Zheng 		osd_req_op_extent_init(req, which, opcode, objoff, objlen,
971b0270324SAlex Elder 				       truncate_size, truncate_seq);
972864e9197SYan, Zheng 	}
973d18d1e28SAlex Elder 
974a1f4020aSJeff Layton 	req->r_abort_on_full = true;
975bb873b53SIlya Dryomov 	req->r_flags = flags;
9767627151eSYan, Zheng 	req->r_base_oloc.pool = layout->pool_id;
97730c156d9SYan, Zheng 	req->r_base_oloc.pool_ns = ceph_try_get_string(layout->pool_ns);
978d30291b9SIlya Dryomov 	ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum);
979dbe0fc41SAlex Elder 
980bb873b53SIlya Dryomov 	req->r_snapid = vino.snap;
981bb873b53SIlya Dryomov 	if (flags & CEPH_OSD_FLAG_WRITE)
982bb873b53SIlya Dryomov 		req->r_data_offset = off;
983bb873b53SIlya Dryomov 
98413d1ad16SIlya Dryomov 	r = ceph_osdc_alloc_messages(req, GFP_NOFS);
98513d1ad16SIlya Dryomov 	if (r)
98613d1ad16SIlya Dryomov 		goto fail;
98713d1ad16SIlya Dryomov 
9883d14c5d2SYehuda Sadeh 	return req;
98913d1ad16SIlya Dryomov 
99013d1ad16SIlya Dryomov fail:
99113d1ad16SIlya Dryomov 	ceph_osdc_put_request(req);
99213d1ad16SIlya Dryomov 	return ERR_PTR(r);
9933d14c5d2SYehuda Sadeh }
9943d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_new_request);
9953d14c5d2SYehuda Sadeh 
9963d14c5d2SYehuda Sadeh /*
9973d14c5d2SYehuda Sadeh  * We keep osd requests in an rbtree, sorted by ->r_tid.
9983d14c5d2SYehuda Sadeh  */
999fcd00b68SIlya Dryomov DEFINE_RB_FUNCS(request, struct ceph_osd_request, r_tid, r_node)
10004609245eSIlya Dryomov DEFINE_RB_FUNCS(request_mc, struct ceph_osd_request, r_tid, r_mc_node)
10013d14c5d2SYehuda Sadeh 
10020247a0cfSIlya Dryomov static bool osd_homeless(struct ceph_osd *osd)
10030247a0cfSIlya Dryomov {
10040247a0cfSIlya Dryomov 	return osd->o_osd == CEPH_HOMELESS_OSD;
10050247a0cfSIlya Dryomov }
10060247a0cfSIlya Dryomov 
10075aea3dcdSIlya Dryomov static bool osd_registered(struct ceph_osd *osd)
10083d14c5d2SYehuda Sadeh {
10095aea3dcdSIlya Dryomov 	verify_osdc_locked(osd->o_osdc);
10103d14c5d2SYehuda Sadeh 
10115aea3dcdSIlya Dryomov 	return !RB_EMPTY_NODE(&osd->o_node);
10123d14c5d2SYehuda Sadeh }
10133d14c5d2SYehuda Sadeh 
10143d14c5d2SYehuda Sadeh /*
10150247a0cfSIlya Dryomov  * Assumes @osd is zero-initialized.
10160247a0cfSIlya Dryomov  */
10170247a0cfSIlya Dryomov static void osd_init(struct ceph_osd *osd)
10180247a0cfSIlya Dryomov {
101902113a0fSElena Reshetova 	refcount_set(&osd->o_ref, 1);
10200247a0cfSIlya Dryomov 	RB_CLEAR_NODE(&osd->o_node);
10215aea3dcdSIlya Dryomov 	osd->o_requests = RB_ROOT;
1022922dab61SIlya Dryomov 	osd->o_linger_requests = RB_ROOT;
1023a02a946dSIlya Dryomov 	osd->o_backoff_mappings = RB_ROOT;
1024a02a946dSIlya Dryomov 	osd->o_backoffs_by_id = RB_ROOT;
10250247a0cfSIlya Dryomov 	INIT_LIST_HEAD(&osd->o_osd_lru);
10260247a0cfSIlya Dryomov 	INIT_LIST_HEAD(&osd->o_keepalive_item);
10270247a0cfSIlya Dryomov 	osd->o_incarnation = 1;
10285aea3dcdSIlya Dryomov 	mutex_init(&osd->lock);
10290247a0cfSIlya Dryomov }
10300247a0cfSIlya Dryomov 
10310247a0cfSIlya Dryomov static void osd_cleanup(struct ceph_osd *osd)
10320247a0cfSIlya Dryomov {
10330247a0cfSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&osd->o_node));
10345aea3dcdSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
1035922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
1036a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoff_mappings));
1037a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoffs_by_id));
10380247a0cfSIlya Dryomov 	WARN_ON(!list_empty(&osd->o_osd_lru));
10390247a0cfSIlya Dryomov 	WARN_ON(!list_empty(&osd->o_keepalive_item));
10400247a0cfSIlya Dryomov 
10410247a0cfSIlya Dryomov 	if (osd->o_auth.authorizer) {
10420247a0cfSIlya Dryomov 		WARN_ON(osd_homeless(osd));
10430247a0cfSIlya Dryomov 		ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
10440247a0cfSIlya Dryomov 	}
10450247a0cfSIlya Dryomov }
10460247a0cfSIlya Dryomov 
10470247a0cfSIlya Dryomov /*
10483d14c5d2SYehuda Sadeh  * Track open sessions with osds.
10493d14c5d2SYehuda Sadeh  */
1050e10006f8SAlex Elder static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
10513d14c5d2SYehuda Sadeh {
10523d14c5d2SYehuda Sadeh 	struct ceph_osd *osd;
10533d14c5d2SYehuda Sadeh 
10540247a0cfSIlya Dryomov 	WARN_ON(onum == CEPH_HOMELESS_OSD);
10550247a0cfSIlya Dryomov 
10567a28f59bSIlya Dryomov 	osd = kzalloc(sizeof(*osd), GFP_NOIO | __GFP_NOFAIL);
10570247a0cfSIlya Dryomov 	osd_init(osd);
10583d14c5d2SYehuda Sadeh 	osd->o_osdc = osdc;
1059e10006f8SAlex Elder 	osd->o_osd = onum;
10603d14c5d2SYehuda Sadeh 
1061b7a9e5ddSSage Weil 	ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
10623d14c5d2SYehuda Sadeh 
10633d14c5d2SYehuda Sadeh 	return osd;
10643d14c5d2SYehuda Sadeh }
10653d14c5d2SYehuda Sadeh 
10663d14c5d2SYehuda Sadeh static struct ceph_osd *get_osd(struct ceph_osd *osd)
10673d14c5d2SYehuda Sadeh {
106802113a0fSElena Reshetova 	if (refcount_inc_not_zero(&osd->o_ref)) {
106902113a0fSElena Reshetova 		dout("get_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref)-1,
107002113a0fSElena Reshetova 		     refcount_read(&osd->o_ref));
10713d14c5d2SYehuda Sadeh 		return osd;
10723d14c5d2SYehuda Sadeh 	} else {
10733d14c5d2SYehuda Sadeh 		dout("get_osd %p FAIL\n", osd);
10743d14c5d2SYehuda Sadeh 		return NULL;
10753d14c5d2SYehuda Sadeh 	}
10763d14c5d2SYehuda Sadeh }
10773d14c5d2SYehuda Sadeh 
10783d14c5d2SYehuda Sadeh static void put_osd(struct ceph_osd *osd)
10793d14c5d2SYehuda Sadeh {
108002113a0fSElena Reshetova 	dout("put_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref),
108102113a0fSElena Reshetova 	     refcount_read(&osd->o_ref) - 1);
108202113a0fSElena Reshetova 	if (refcount_dec_and_test(&osd->o_ref)) {
10830247a0cfSIlya Dryomov 		osd_cleanup(osd);
10843d14c5d2SYehuda Sadeh 		kfree(osd);
10853d14c5d2SYehuda Sadeh 	}
10863d14c5d2SYehuda Sadeh }
10873d14c5d2SYehuda Sadeh 
1088fcd00b68SIlya Dryomov DEFINE_RB_FUNCS(osd, struct ceph_osd, o_osd, o_node)
1089fcd00b68SIlya Dryomov 
10909dd2845cSIlya Dryomov static void __move_osd_to_lru(struct ceph_osd *osd)
10913d14c5d2SYehuda Sadeh {
10929dd2845cSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
10939dd2845cSIlya Dryomov 
10949dd2845cSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
10953d14c5d2SYehuda Sadeh 	BUG_ON(!list_empty(&osd->o_osd_lru));
1096bbf37ec3SIlya Dryomov 
10979dd2845cSIlya Dryomov 	spin_lock(&osdc->osd_lru_lock);
10983d14c5d2SYehuda Sadeh 	list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
10999dd2845cSIlya Dryomov 	spin_unlock(&osdc->osd_lru_lock);
11009dd2845cSIlya Dryomov 
1101a319bf56SIlya Dryomov 	osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
11023d14c5d2SYehuda Sadeh }
11033d14c5d2SYehuda Sadeh 
11049dd2845cSIlya Dryomov static void maybe_move_osd_to_lru(struct ceph_osd *osd)
1105bbf37ec3SIlya Dryomov {
11065aea3dcdSIlya Dryomov 	if (RB_EMPTY_ROOT(&osd->o_requests) &&
1107922dab61SIlya Dryomov 	    RB_EMPTY_ROOT(&osd->o_linger_requests))
11089dd2845cSIlya Dryomov 		__move_osd_to_lru(osd);
1109bbf37ec3SIlya Dryomov }
1110bbf37ec3SIlya Dryomov 
11113d14c5d2SYehuda Sadeh static void __remove_osd_from_lru(struct ceph_osd *osd)
11123d14c5d2SYehuda Sadeh {
11139dd2845cSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
11149dd2845cSIlya Dryomov 
11159dd2845cSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
11169dd2845cSIlya Dryomov 
11179dd2845cSIlya Dryomov 	spin_lock(&osdc->osd_lru_lock);
11183d14c5d2SYehuda Sadeh 	if (!list_empty(&osd->o_osd_lru))
11193d14c5d2SYehuda Sadeh 		list_del_init(&osd->o_osd_lru);
11209dd2845cSIlya Dryomov 	spin_unlock(&osdc->osd_lru_lock);
11213d14c5d2SYehuda Sadeh }
11223d14c5d2SYehuda Sadeh 
11233d14c5d2SYehuda Sadeh /*
11245aea3dcdSIlya Dryomov  * Close the connection and assign any leftover requests to the
11255aea3dcdSIlya Dryomov  * homeless session.
11265aea3dcdSIlya Dryomov  */
11275aea3dcdSIlya Dryomov static void close_osd(struct ceph_osd *osd)
11285aea3dcdSIlya Dryomov {
11295aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
11305aea3dcdSIlya Dryomov 	struct rb_node *n;
11315aea3dcdSIlya Dryomov 
11325aea3dcdSIlya Dryomov 	verify_osdc_wrlocked(osdc);
11335aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
11345aea3dcdSIlya Dryomov 
11355aea3dcdSIlya Dryomov 	ceph_con_close(&osd->o_con);
11365aea3dcdSIlya Dryomov 
11375aea3dcdSIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; ) {
11385aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
11395aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
11405aea3dcdSIlya Dryomov 
11415aea3dcdSIlya Dryomov 		n = rb_next(n); /* unlink_request() */
11425aea3dcdSIlya Dryomov 
11435aea3dcdSIlya Dryomov 		dout(" reassigning req %p tid %llu\n", req, req->r_tid);
11445aea3dcdSIlya Dryomov 		unlink_request(osd, req);
11455aea3dcdSIlya Dryomov 		link_request(&osdc->homeless_osd, req);
11465aea3dcdSIlya Dryomov 	}
1147922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_linger_requests); n; ) {
1148922dab61SIlya Dryomov 		struct ceph_osd_linger_request *lreq =
1149922dab61SIlya Dryomov 		    rb_entry(n, struct ceph_osd_linger_request, node);
1150922dab61SIlya Dryomov 
1151922dab61SIlya Dryomov 		n = rb_next(n); /* unlink_linger() */
1152922dab61SIlya Dryomov 
1153922dab61SIlya Dryomov 		dout(" reassigning lreq %p linger_id %llu\n", lreq,
1154922dab61SIlya Dryomov 		     lreq->linger_id);
1155922dab61SIlya Dryomov 		unlink_linger(osd, lreq);
1156922dab61SIlya Dryomov 		link_linger(&osdc->homeless_osd, lreq);
1157922dab61SIlya Dryomov 	}
1158a02a946dSIlya Dryomov 	clear_backoffs(osd);
11595aea3dcdSIlya Dryomov 
11605aea3dcdSIlya Dryomov 	__remove_osd_from_lru(osd);
11615aea3dcdSIlya Dryomov 	erase_osd(&osdc->osds, osd);
11625aea3dcdSIlya Dryomov 	put_osd(osd);
11635aea3dcdSIlya Dryomov }
11645aea3dcdSIlya Dryomov 
11655aea3dcdSIlya Dryomov /*
11663d14c5d2SYehuda Sadeh  * reset osd connect
11673d14c5d2SYehuda Sadeh  */
11685aea3dcdSIlya Dryomov static int reopen_osd(struct ceph_osd *osd)
11693d14c5d2SYehuda Sadeh {
1170c3acb181SAlex Elder 	struct ceph_entity_addr *peer_addr;
11713d14c5d2SYehuda Sadeh 
11725aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
11735aea3dcdSIlya Dryomov 
11745aea3dcdSIlya Dryomov 	if (RB_EMPTY_ROOT(&osd->o_requests) &&
1175922dab61SIlya Dryomov 	    RB_EMPTY_ROOT(&osd->o_linger_requests)) {
11765aea3dcdSIlya Dryomov 		close_osd(osd);
1177c3acb181SAlex Elder 		return -ENODEV;
1178c3acb181SAlex Elder 	}
1179c3acb181SAlex Elder 
11805aea3dcdSIlya Dryomov 	peer_addr = &osd->o_osdc->osdmap->osd_addr[osd->o_osd];
1181c3acb181SAlex Elder 	if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
11823d14c5d2SYehuda Sadeh 			!ceph_con_opened(&osd->o_con)) {
11835aea3dcdSIlya Dryomov 		struct rb_node *n;
1184c3acb181SAlex Elder 
11853d14c5d2SYehuda Sadeh 		dout("osd addr hasn't changed and connection never opened, "
11860b4af2e8SIlya Dryomov 		     "letting msgr retry\n");
11873d14c5d2SYehuda Sadeh 		/* touch each r_stamp for handle_timeout()'s benfit */
11885aea3dcdSIlya Dryomov 		for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
11895aea3dcdSIlya Dryomov 			struct ceph_osd_request *req =
11905aea3dcdSIlya Dryomov 			    rb_entry(n, struct ceph_osd_request, r_node);
11913d14c5d2SYehuda Sadeh 			req->r_stamp = jiffies;
11925aea3dcdSIlya Dryomov 		}
1193c3acb181SAlex Elder 
1194c3acb181SAlex Elder 		return -EAGAIN;
11953d14c5d2SYehuda Sadeh 	}
1196c3acb181SAlex Elder 
1197c3acb181SAlex Elder 	ceph_con_close(&osd->o_con);
1198c3acb181SAlex Elder 	ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1199c3acb181SAlex Elder 	osd->o_incarnation++;
1200c3acb181SAlex Elder 
1201c3acb181SAlex Elder 	return 0;
12023d14c5d2SYehuda Sadeh }
12033d14c5d2SYehuda Sadeh 
12045aea3dcdSIlya Dryomov static struct ceph_osd *lookup_create_osd(struct ceph_osd_client *osdc, int o,
12055aea3dcdSIlya Dryomov 					  bool wrlocked)
12063d14c5d2SYehuda Sadeh {
12075aea3dcdSIlya Dryomov 	struct ceph_osd *osd;
12085aea3dcdSIlya Dryomov 
12095aea3dcdSIlya Dryomov 	if (wrlocked)
12105aea3dcdSIlya Dryomov 		verify_osdc_wrlocked(osdc);
12115aea3dcdSIlya Dryomov 	else
12125aea3dcdSIlya Dryomov 		verify_osdc_locked(osdc);
12135aea3dcdSIlya Dryomov 
12145aea3dcdSIlya Dryomov 	if (o != CEPH_HOMELESS_OSD)
12155aea3dcdSIlya Dryomov 		osd = lookup_osd(&osdc->osds, o);
12165aea3dcdSIlya Dryomov 	else
12175aea3dcdSIlya Dryomov 		osd = &osdc->homeless_osd;
12185aea3dcdSIlya Dryomov 	if (!osd) {
12195aea3dcdSIlya Dryomov 		if (!wrlocked)
12205aea3dcdSIlya Dryomov 			return ERR_PTR(-EAGAIN);
12215aea3dcdSIlya Dryomov 
12225aea3dcdSIlya Dryomov 		osd = create_osd(osdc, o);
12235aea3dcdSIlya Dryomov 		insert_osd(&osdc->osds, osd);
12245aea3dcdSIlya Dryomov 		ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
12255aea3dcdSIlya Dryomov 			      &osdc->osdmap->osd_addr[osd->o_osd]);
12265aea3dcdSIlya Dryomov 	}
12275aea3dcdSIlya Dryomov 
12285aea3dcdSIlya Dryomov 	dout("%s osdc %p osd%d -> osd %p\n", __func__, osdc, o, osd);
12295aea3dcdSIlya Dryomov 	return osd;
1230a40c4f10SYehuda Sadeh }
1231a40c4f10SYehuda Sadeh 
12323d14c5d2SYehuda Sadeh /*
12335aea3dcdSIlya Dryomov  * Create request <-> OSD session relation.
12345aea3dcdSIlya Dryomov  *
12355aea3dcdSIlya Dryomov  * @req has to be assigned a tid, @osd may be homeless.
12363d14c5d2SYehuda Sadeh  */
12375aea3dcdSIlya Dryomov static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req)
12383d14c5d2SYehuda Sadeh {
12395aea3dcdSIlya Dryomov 	verify_osd_locked(osd);
12405aea3dcdSIlya Dryomov 	WARN_ON(!req->r_tid || req->r_osd);
12415aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
124235f9f8a0SSage Weil 	     req, req->r_tid);
12435aea3dcdSIlya Dryomov 
12445aea3dcdSIlya Dryomov 	if (!osd_homeless(osd))
12455aea3dcdSIlya Dryomov 		__remove_osd_from_lru(osd);
12465aea3dcdSIlya Dryomov 	else
12475aea3dcdSIlya Dryomov 		atomic_inc(&osd->o_osdc->num_homeless);
12485aea3dcdSIlya Dryomov 
12495aea3dcdSIlya Dryomov 	get_osd(osd);
12505aea3dcdSIlya Dryomov 	insert_request(&osd->o_requests, req);
12515aea3dcdSIlya Dryomov 	req->r_osd = osd;
125235f9f8a0SSage Weil }
125335f9f8a0SSage Weil 
12545aea3dcdSIlya Dryomov static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req)
12553d14c5d2SYehuda Sadeh {
12565aea3dcdSIlya Dryomov 	verify_osd_locked(osd);
12575aea3dcdSIlya Dryomov 	WARN_ON(req->r_osd != osd);
12585aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
12595aea3dcdSIlya Dryomov 	     req, req->r_tid);
12605aea3dcdSIlya Dryomov 
12615aea3dcdSIlya Dryomov 	req->r_osd = NULL;
12625aea3dcdSIlya Dryomov 	erase_request(&osd->o_requests, req);
12635aea3dcdSIlya Dryomov 	put_osd(osd);
12645aea3dcdSIlya Dryomov 
12655aea3dcdSIlya Dryomov 	if (!osd_homeless(osd))
12665aea3dcdSIlya Dryomov 		maybe_move_osd_to_lru(osd);
12675aea3dcdSIlya Dryomov 	else
12685aea3dcdSIlya Dryomov 		atomic_dec(&osd->o_osdc->num_homeless);
12693d14c5d2SYehuda Sadeh }
12703d14c5d2SYehuda Sadeh 
127163244fa1SIlya Dryomov static bool __pool_full(struct ceph_pg_pool_info *pi)
127263244fa1SIlya Dryomov {
127363244fa1SIlya Dryomov 	return pi->flags & CEPH_POOL_FLAG_FULL;
127463244fa1SIlya Dryomov }
127563244fa1SIlya Dryomov 
127642c1b124SIlya Dryomov static bool have_pool_full(struct ceph_osd_client *osdc)
127742c1b124SIlya Dryomov {
127842c1b124SIlya Dryomov 	struct rb_node *n;
127942c1b124SIlya Dryomov 
128042c1b124SIlya Dryomov 	for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
128142c1b124SIlya Dryomov 		struct ceph_pg_pool_info *pi =
128242c1b124SIlya Dryomov 		    rb_entry(n, struct ceph_pg_pool_info, node);
128342c1b124SIlya Dryomov 
128442c1b124SIlya Dryomov 		if (__pool_full(pi))
128542c1b124SIlya Dryomov 			return true;
128642c1b124SIlya Dryomov 	}
128742c1b124SIlya Dryomov 
128842c1b124SIlya Dryomov 	return false;
128942c1b124SIlya Dryomov }
129042c1b124SIlya Dryomov 
12915aea3dcdSIlya Dryomov static bool pool_full(struct ceph_osd_client *osdc, s64 pool_id)
12925aea3dcdSIlya Dryomov {
12935aea3dcdSIlya Dryomov 	struct ceph_pg_pool_info *pi;
12945aea3dcdSIlya Dryomov 
12955aea3dcdSIlya Dryomov 	pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
12965aea3dcdSIlya Dryomov 	if (!pi)
12975aea3dcdSIlya Dryomov 		return false;
12985aea3dcdSIlya Dryomov 
12995aea3dcdSIlya Dryomov 	return __pool_full(pi);
13005aea3dcdSIlya Dryomov }
13015aea3dcdSIlya Dryomov 
13023d14c5d2SYehuda Sadeh /*
1303d29adb34SJosh Durgin  * Returns whether a request should be blocked from being sent
1304d29adb34SJosh Durgin  * based on the current osdmap and osd_client settings.
1305d29adb34SJosh Durgin  */
130663244fa1SIlya Dryomov static bool target_should_be_paused(struct ceph_osd_client *osdc,
130763244fa1SIlya Dryomov 				    const struct ceph_osd_request_target *t,
130863244fa1SIlya Dryomov 				    struct ceph_pg_pool_info *pi)
130963244fa1SIlya Dryomov {
1310b7ec35b3SIlya Dryomov 	bool pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
1311b7ec35b3SIlya Dryomov 	bool pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
1312b7ec35b3SIlya Dryomov 		       ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
131363244fa1SIlya Dryomov 		       __pool_full(pi);
131463244fa1SIlya Dryomov 
13156d637a54SIlya Dryomov 	WARN_ON(pi->id != t->target_oloc.pool);
131658eb7932SJeff Layton 	return ((t->flags & CEPH_OSD_FLAG_READ) && pauserd) ||
131758eb7932SJeff Layton 	       ((t->flags & CEPH_OSD_FLAG_WRITE) && pausewr) ||
131858eb7932SJeff Layton 	       (osdc->osdmap->epoch < osdc->epoch_barrier);
131963244fa1SIlya Dryomov }
132063244fa1SIlya Dryomov 
132163244fa1SIlya Dryomov enum calc_target_result {
132263244fa1SIlya Dryomov 	CALC_TARGET_NO_ACTION = 0,
132363244fa1SIlya Dryomov 	CALC_TARGET_NEED_RESEND,
132463244fa1SIlya Dryomov 	CALC_TARGET_POOL_DNE,
132563244fa1SIlya Dryomov };
132663244fa1SIlya Dryomov 
132763244fa1SIlya Dryomov static enum calc_target_result calc_target(struct ceph_osd_client *osdc,
132863244fa1SIlya Dryomov 					   struct ceph_osd_request_target *t,
13297de030d6SIlya Dryomov 					   struct ceph_connection *con,
133063244fa1SIlya Dryomov 					   bool any_change)
133163244fa1SIlya Dryomov {
133263244fa1SIlya Dryomov 	struct ceph_pg_pool_info *pi;
133363244fa1SIlya Dryomov 	struct ceph_pg pgid, last_pgid;
133463244fa1SIlya Dryomov 	struct ceph_osds up, acting;
133563244fa1SIlya Dryomov 	bool force_resend = false;
133684ed45dfSIlya Dryomov 	bool unpaused = false;
133784ed45dfSIlya Dryomov 	bool legacy_change;
13387de030d6SIlya Dryomov 	bool split = false;
1339b7ec35b3SIlya Dryomov 	bool sort_bitwise = ceph_osdmap_flag(osdc, CEPH_OSDMAP_SORTBITWISE);
134063244fa1SIlya Dryomov 	enum calc_target_result ct_res;
134163244fa1SIlya Dryomov 	int ret;
134263244fa1SIlya Dryomov 
134304c7d789SIlya Dryomov 	t->epoch = osdc->osdmap->epoch;
134463244fa1SIlya Dryomov 	pi = ceph_pg_pool_by_id(osdc->osdmap, t->base_oloc.pool);
134563244fa1SIlya Dryomov 	if (!pi) {
134663244fa1SIlya Dryomov 		t->osd = CEPH_HOMELESS_OSD;
134763244fa1SIlya Dryomov 		ct_res = CALC_TARGET_POOL_DNE;
134863244fa1SIlya Dryomov 		goto out;
134963244fa1SIlya Dryomov 	}
135063244fa1SIlya Dryomov 
135163244fa1SIlya Dryomov 	if (osdc->osdmap->epoch == pi->last_force_request_resend) {
1352dc93e0e2SIlya Dryomov 		if (t->last_force_resend < pi->last_force_request_resend) {
1353dc93e0e2SIlya Dryomov 			t->last_force_resend = pi->last_force_request_resend;
135463244fa1SIlya Dryomov 			force_resend = true;
1355dc93e0e2SIlya Dryomov 		} else if (t->last_force_resend == 0) {
135663244fa1SIlya Dryomov 			force_resend = true;
135763244fa1SIlya Dryomov 		}
135863244fa1SIlya Dryomov 	}
135963244fa1SIlya Dryomov 
1360db098ec4SIlya Dryomov 	/* apply tiering */
1361db098ec4SIlya Dryomov 	ceph_oid_copy(&t->target_oid, &t->base_oid);
1362db098ec4SIlya Dryomov 	ceph_oloc_copy(&t->target_oloc, &t->base_oloc);
1363db098ec4SIlya Dryomov 	if ((t->flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
136463244fa1SIlya Dryomov 		if (t->flags & CEPH_OSD_FLAG_READ && pi->read_tier >= 0)
136563244fa1SIlya Dryomov 			t->target_oloc.pool = pi->read_tier;
136663244fa1SIlya Dryomov 		if (t->flags & CEPH_OSD_FLAG_WRITE && pi->write_tier >= 0)
136763244fa1SIlya Dryomov 			t->target_oloc.pool = pi->write_tier;
13686d637a54SIlya Dryomov 
13696d637a54SIlya Dryomov 		pi = ceph_pg_pool_by_id(osdc->osdmap, t->target_oloc.pool);
13706d637a54SIlya Dryomov 		if (!pi) {
13716d637a54SIlya Dryomov 			t->osd = CEPH_HOMELESS_OSD;
13726d637a54SIlya Dryomov 			ct_res = CALC_TARGET_POOL_DNE;
13736d637a54SIlya Dryomov 			goto out;
13746d637a54SIlya Dryomov 		}
137563244fa1SIlya Dryomov 	}
137663244fa1SIlya Dryomov 
1377df28152dSIlya Dryomov 	ret = __ceph_object_locator_to_pg(pi, &t->target_oid, &t->target_oloc,
1378df28152dSIlya Dryomov 					  &pgid);
137963244fa1SIlya Dryomov 	if (ret) {
138063244fa1SIlya Dryomov 		WARN_ON(ret != -ENOENT);
138163244fa1SIlya Dryomov 		t->osd = CEPH_HOMELESS_OSD;
138263244fa1SIlya Dryomov 		ct_res = CALC_TARGET_POOL_DNE;
138363244fa1SIlya Dryomov 		goto out;
138463244fa1SIlya Dryomov 	}
138563244fa1SIlya Dryomov 	last_pgid.pool = pgid.pool;
138663244fa1SIlya Dryomov 	last_pgid.seed = ceph_stable_mod(pgid.seed, t->pg_num, t->pg_num_mask);
138763244fa1SIlya Dryomov 
1388df28152dSIlya Dryomov 	ceph_pg_to_up_acting_osds(osdc->osdmap, pi, &pgid, &up, &acting);
138963244fa1SIlya Dryomov 	if (any_change &&
139063244fa1SIlya Dryomov 	    ceph_is_new_interval(&t->acting,
139163244fa1SIlya Dryomov 				 &acting,
139263244fa1SIlya Dryomov 				 &t->up,
139363244fa1SIlya Dryomov 				 &up,
139463244fa1SIlya Dryomov 				 t->size,
139563244fa1SIlya Dryomov 				 pi->size,
139663244fa1SIlya Dryomov 				 t->min_size,
139763244fa1SIlya Dryomov 				 pi->min_size,
139863244fa1SIlya Dryomov 				 t->pg_num,
139963244fa1SIlya Dryomov 				 pi->pg_num,
140063244fa1SIlya Dryomov 				 t->sort_bitwise,
140163244fa1SIlya Dryomov 				 sort_bitwise,
140263244fa1SIlya Dryomov 				 &last_pgid))
140363244fa1SIlya Dryomov 		force_resend = true;
140463244fa1SIlya Dryomov 
140563244fa1SIlya Dryomov 	if (t->paused && !target_should_be_paused(osdc, t, pi)) {
140663244fa1SIlya Dryomov 		t->paused = false;
140784ed45dfSIlya Dryomov 		unpaused = true;
140863244fa1SIlya Dryomov 	}
140984ed45dfSIlya Dryomov 	legacy_change = ceph_pg_compare(&t->pgid, &pgid) ||
141084ed45dfSIlya Dryomov 			ceph_osds_changed(&t->acting, &acting, any_change);
14117de030d6SIlya Dryomov 	if (t->pg_num)
14127de030d6SIlya Dryomov 		split = ceph_pg_is_split(&last_pgid, t->pg_num, pi->pg_num);
141363244fa1SIlya Dryomov 
14147de030d6SIlya Dryomov 	if (legacy_change || force_resend || split) {
141563244fa1SIlya Dryomov 		t->pgid = pgid; /* struct */
1416df28152dSIlya Dryomov 		ceph_pg_to_primary_shard(osdc->osdmap, pi, &pgid, &t->spgid);
141763244fa1SIlya Dryomov 		ceph_osds_copy(&t->acting, &acting);
141863244fa1SIlya Dryomov 		ceph_osds_copy(&t->up, &up);
141963244fa1SIlya Dryomov 		t->size = pi->size;
142063244fa1SIlya Dryomov 		t->min_size = pi->min_size;
142163244fa1SIlya Dryomov 		t->pg_num = pi->pg_num;
142263244fa1SIlya Dryomov 		t->pg_num_mask = pi->pg_num_mask;
142363244fa1SIlya Dryomov 		t->sort_bitwise = sort_bitwise;
142463244fa1SIlya Dryomov 
142563244fa1SIlya Dryomov 		t->osd = acting.primary;
142663244fa1SIlya Dryomov 	}
142763244fa1SIlya Dryomov 
14287de030d6SIlya Dryomov 	if (unpaused || legacy_change || force_resend ||
14297de030d6SIlya Dryomov 	    (split && con && CEPH_HAVE_FEATURE(con->peer_features,
14307de030d6SIlya Dryomov 					       RESEND_ON_SPLIT)))
143184ed45dfSIlya Dryomov 		ct_res = CALC_TARGET_NEED_RESEND;
143284ed45dfSIlya Dryomov 	else
143384ed45dfSIlya Dryomov 		ct_res = CALC_TARGET_NO_ACTION;
143484ed45dfSIlya Dryomov 
143563244fa1SIlya Dryomov out:
143663244fa1SIlya Dryomov 	dout("%s t %p -> ct_res %d osd %d\n", __func__, t, ct_res, t->osd);
143763244fa1SIlya Dryomov 	return ct_res;
143863244fa1SIlya Dryomov }
143963244fa1SIlya Dryomov 
1440a02a946dSIlya Dryomov static struct ceph_spg_mapping *alloc_spg_mapping(void)
1441a02a946dSIlya Dryomov {
1442a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
1443a02a946dSIlya Dryomov 
1444a02a946dSIlya Dryomov 	spg = kmalloc(sizeof(*spg), GFP_NOIO);
1445a02a946dSIlya Dryomov 	if (!spg)
1446a02a946dSIlya Dryomov 		return NULL;
1447a02a946dSIlya Dryomov 
1448a02a946dSIlya Dryomov 	RB_CLEAR_NODE(&spg->node);
1449a02a946dSIlya Dryomov 	spg->backoffs = RB_ROOT;
1450a02a946dSIlya Dryomov 	return spg;
1451a02a946dSIlya Dryomov }
1452a02a946dSIlya Dryomov 
1453a02a946dSIlya Dryomov static void free_spg_mapping(struct ceph_spg_mapping *spg)
1454a02a946dSIlya Dryomov {
1455a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&spg->node));
1456a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&spg->backoffs));
1457a02a946dSIlya Dryomov 
1458a02a946dSIlya Dryomov 	kfree(spg);
1459a02a946dSIlya Dryomov }
1460a02a946dSIlya Dryomov 
1461a02a946dSIlya Dryomov /*
1462a02a946dSIlya Dryomov  * rbtree of ceph_spg_mapping for handling map<spg_t, ...>, similar to
1463a02a946dSIlya Dryomov  * ceph_pg_mapping.  Used to track OSD backoffs -- a backoff [range] is
1464a02a946dSIlya Dryomov  * defined only within a specific spgid; it does not pass anything to
1465a02a946dSIlya Dryomov  * children on split, or to another primary.
1466a02a946dSIlya Dryomov  */
1467a02a946dSIlya Dryomov DEFINE_RB_FUNCS2(spg_mapping, struct ceph_spg_mapping, spgid, ceph_spg_compare,
1468a02a946dSIlya Dryomov 		 RB_BYPTR, const struct ceph_spg *, node)
1469a02a946dSIlya Dryomov 
1470a02a946dSIlya Dryomov static u64 hoid_get_bitwise_key(const struct ceph_hobject_id *hoid)
1471a02a946dSIlya Dryomov {
1472a02a946dSIlya Dryomov 	return hoid->is_max ? 0x100000000ull : hoid->hash_reverse_bits;
1473a02a946dSIlya Dryomov }
1474a02a946dSIlya Dryomov 
1475a02a946dSIlya Dryomov static void hoid_get_effective_key(const struct ceph_hobject_id *hoid,
1476a02a946dSIlya Dryomov 				   void **pkey, size_t *pkey_len)
1477a02a946dSIlya Dryomov {
1478a02a946dSIlya Dryomov 	if (hoid->key_len) {
1479a02a946dSIlya Dryomov 		*pkey = hoid->key;
1480a02a946dSIlya Dryomov 		*pkey_len = hoid->key_len;
1481a02a946dSIlya Dryomov 	} else {
1482a02a946dSIlya Dryomov 		*pkey = hoid->oid;
1483a02a946dSIlya Dryomov 		*pkey_len = hoid->oid_len;
1484a02a946dSIlya Dryomov 	}
1485a02a946dSIlya Dryomov }
1486a02a946dSIlya Dryomov 
1487a02a946dSIlya Dryomov static int compare_names(const void *name1, size_t name1_len,
1488a02a946dSIlya Dryomov 			 const void *name2, size_t name2_len)
1489a02a946dSIlya Dryomov {
1490a02a946dSIlya Dryomov 	int ret;
1491a02a946dSIlya Dryomov 
1492a02a946dSIlya Dryomov 	ret = memcmp(name1, name2, min(name1_len, name2_len));
1493a02a946dSIlya Dryomov 	if (!ret) {
1494a02a946dSIlya Dryomov 		if (name1_len < name2_len)
1495a02a946dSIlya Dryomov 			ret = -1;
1496a02a946dSIlya Dryomov 		else if (name1_len > name2_len)
1497a02a946dSIlya Dryomov 			ret = 1;
1498a02a946dSIlya Dryomov 	}
1499a02a946dSIlya Dryomov 	return ret;
1500a02a946dSIlya Dryomov }
1501a02a946dSIlya Dryomov 
1502a02a946dSIlya Dryomov static int hoid_compare(const struct ceph_hobject_id *lhs,
1503a02a946dSIlya Dryomov 			const struct ceph_hobject_id *rhs)
1504a02a946dSIlya Dryomov {
1505a02a946dSIlya Dryomov 	void *effective_key1, *effective_key2;
1506a02a946dSIlya Dryomov 	size_t effective_key1_len, effective_key2_len;
1507a02a946dSIlya Dryomov 	int ret;
1508a02a946dSIlya Dryomov 
1509a02a946dSIlya Dryomov 	if (lhs->is_max < rhs->is_max)
1510a02a946dSIlya Dryomov 		return -1;
1511a02a946dSIlya Dryomov 	if (lhs->is_max > rhs->is_max)
1512a02a946dSIlya Dryomov 		return 1;
1513a02a946dSIlya Dryomov 
1514a02a946dSIlya Dryomov 	if (lhs->pool < rhs->pool)
1515a02a946dSIlya Dryomov 		return -1;
1516a02a946dSIlya Dryomov 	if (lhs->pool > rhs->pool)
1517a02a946dSIlya Dryomov 		return 1;
1518a02a946dSIlya Dryomov 
1519a02a946dSIlya Dryomov 	if (hoid_get_bitwise_key(lhs) < hoid_get_bitwise_key(rhs))
1520a02a946dSIlya Dryomov 		return -1;
1521a02a946dSIlya Dryomov 	if (hoid_get_bitwise_key(lhs) > hoid_get_bitwise_key(rhs))
1522a02a946dSIlya Dryomov 		return 1;
1523a02a946dSIlya Dryomov 
1524a02a946dSIlya Dryomov 	ret = compare_names(lhs->nspace, lhs->nspace_len,
1525a02a946dSIlya Dryomov 			    rhs->nspace, rhs->nspace_len);
1526a02a946dSIlya Dryomov 	if (ret)
1527a02a946dSIlya Dryomov 		return ret;
1528a02a946dSIlya Dryomov 
1529a02a946dSIlya Dryomov 	hoid_get_effective_key(lhs, &effective_key1, &effective_key1_len);
1530a02a946dSIlya Dryomov 	hoid_get_effective_key(rhs, &effective_key2, &effective_key2_len);
1531a02a946dSIlya Dryomov 	ret = compare_names(effective_key1, effective_key1_len,
1532a02a946dSIlya Dryomov 			    effective_key2, effective_key2_len);
1533a02a946dSIlya Dryomov 	if (ret)
1534a02a946dSIlya Dryomov 		return ret;
1535a02a946dSIlya Dryomov 
1536a02a946dSIlya Dryomov 	ret = compare_names(lhs->oid, lhs->oid_len, rhs->oid, rhs->oid_len);
1537a02a946dSIlya Dryomov 	if (ret)
1538a02a946dSIlya Dryomov 		return ret;
1539a02a946dSIlya Dryomov 
1540a02a946dSIlya Dryomov 	if (lhs->snapid < rhs->snapid)
1541a02a946dSIlya Dryomov 		return -1;
1542a02a946dSIlya Dryomov 	if (lhs->snapid > rhs->snapid)
1543a02a946dSIlya Dryomov 		return 1;
1544a02a946dSIlya Dryomov 
1545a02a946dSIlya Dryomov 	return 0;
1546a02a946dSIlya Dryomov }
1547a02a946dSIlya Dryomov 
1548a02a946dSIlya Dryomov /*
1549a02a946dSIlya Dryomov  * For decoding ->begin and ->end of MOSDBackoff only -- no MIN/MAX
1550a02a946dSIlya Dryomov  * compat stuff here.
1551a02a946dSIlya Dryomov  *
1552a02a946dSIlya Dryomov  * Assumes @hoid is zero-initialized.
1553a02a946dSIlya Dryomov  */
1554a02a946dSIlya Dryomov static int decode_hoid(void **p, void *end, struct ceph_hobject_id *hoid)
1555a02a946dSIlya Dryomov {
1556a02a946dSIlya Dryomov 	u8 struct_v;
1557a02a946dSIlya Dryomov 	u32 struct_len;
1558a02a946dSIlya Dryomov 	int ret;
1559a02a946dSIlya Dryomov 
1560a02a946dSIlya Dryomov 	ret = ceph_start_decoding(p, end, 4, "hobject_t", &struct_v,
1561a02a946dSIlya Dryomov 				  &struct_len);
1562a02a946dSIlya Dryomov 	if (ret)
1563a02a946dSIlya Dryomov 		return ret;
1564a02a946dSIlya Dryomov 
1565a02a946dSIlya Dryomov 	if (struct_v < 4) {
1566a02a946dSIlya Dryomov 		pr_err("got struct_v %d < 4 of hobject_t\n", struct_v);
1567a02a946dSIlya Dryomov 		goto e_inval;
1568a02a946dSIlya Dryomov 	}
1569a02a946dSIlya Dryomov 
1570a02a946dSIlya Dryomov 	hoid->key = ceph_extract_encoded_string(p, end, &hoid->key_len,
1571a02a946dSIlya Dryomov 						GFP_NOIO);
1572a02a946dSIlya Dryomov 	if (IS_ERR(hoid->key)) {
1573a02a946dSIlya Dryomov 		ret = PTR_ERR(hoid->key);
1574a02a946dSIlya Dryomov 		hoid->key = NULL;
1575a02a946dSIlya Dryomov 		return ret;
1576a02a946dSIlya Dryomov 	}
1577a02a946dSIlya Dryomov 
1578a02a946dSIlya Dryomov 	hoid->oid = ceph_extract_encoded_string(p, end, &hoid->oid_len,
1579a02a946dSIlya Dryomov 						GFP_NOIO);
1580a02a946dSIlya Dryomov 	if (IS_ERR(hoid->oid)) {
1581a02a946dSIlya Dryomov 		ret = PTR_ERR(hoid->oid);
1582a02a946dSIlya Dryomov 		hoid->oid = NULL;
1583a02a946dSIlya Dryomov 		return ret;
1584a02a946dSIlya Dryomov 	}
1585a02a946dSIlya Dryomov 
1586a02a946dSIlya Dryomov 	ceph_decode_64_safe(p, end, hoid->snapid, e_inval);
1587a02a946dSIlya Dryomov 	ceph_decode_32_safe(p, end, hoid->hash, e_inval);
1588a02a946dSIlya Dryomov 	ceph_decode_8_safe(p, end, hoid->is_max, e_inval);
1589a02a946dSIlya Dryomov 
1590a02a946dSIlya Dryomov 	hoid->nspace = ceph_extract_encoded_string(p, end, &hoid->nspace_len,
1591a02a946dSIlya Dryomov 						   GFP_NOIO);
1592a02a946dSIlya Dryomov 	if (IS_ERR(hoid->nspace)) {
1593a02a946dSIlya Dryomov 		ret = PTR_ERR(hoid->nspace);
1594a02a946dSIlya Dryomov 		hoid->nspace = NULL;
1595a02a946dSIlya Dryomov 		return ret;
1596a02a946dSIlya Dryomov 	}
1597a02a946dSIlya Dryomov 
1598a02a946dSIlya Dryomov 	ceph_decode_64_safe(p, end, hoid->pool, e_inval);
1599a02a946dSIlya Dryomov 
1600a02a946dSIlya Dryomov 	ceph_hoid_build_hash_cache(hoid);
1601a02a946dSIlya Dryomov 	return 0;
1602a02a946dSIlya Dryomov 
1603a02a946dSIlya Dryomov e_inval:
1604a02a946dSIlya Dryomov 	return -EINVAL;
1605a02a946dSIlya Dryomov }
1606a02a946dSIlya Dryomov 
1607a02a946dSIlya Dryomov static int hoid_encoding_size(const struct ceph_hobject_id *hoid)
1608a02a946dSIlya Dryomov {
1609a02a946dSIlya Dryomov 	return 8 + 4 + 1 + 8 + /* snapid, hash, is_max, pool */
1610a02a946dSIlya Dryomov 	       4 + hoid->key_len + 4 + hoid->oid_len + 4 + hoid->nspace_len;
1611a02a946dSIlya Dryomov }
1612a02a946dSIlya Dryomov 
1613a02a946dSIlya Dryomov static void encode_hoid(void **p, void *end, const struct ceph_hobject_id *hoid)
1614a02a946dSIlya Dryomov {
1615a02a946dSIlya Dryomov 	ceph_start_encoding(p, 4, 3, hoid_encoding_size(hoid));
1616a02a946dSIlya Dryomov 	ceph_encode_string(p, end, hoid->key, hoid->key_len);
1617a02a946dSIlya Dryomov 	ceph_encode_string(p, end, hoid->oid, hoid->oid_len);
1618a02a946dSIlya Dryomov 	ceph_encode_64(p, hoid->snapid);
1619a02a946dSIlya Dryomov 	ceph_encode_32(p, hoid->hash);
1620a02a946dSIlya Dryomov 	ceph_encode_8(p, hoid->is_max);
1621a02a946dSIlya Dryomov 	ceph_encode_string(p, end, hoid->nspace, hoid->nspace_len);
1622a02a946dSIlya Dryomov 	ceph_encode_64(p, hoid->pool);
1623a02a946dSIlya Dryomov }
1624a02a946dSIlya Dryomov 
1625a02a946dSIlya Dryomov static void free_hoid(struct ceph_hobject_id *hoid)
1626a02a946dSIlya Dryomov {
1627a02a946dSIlya Dryomov 	if (hoid) {
1628a02a946dSIlya Dryomov 		kfree(hoid->key);
1629a02a946dSIlya Dryomov 		kfree(hoid->oid);
1630a02a946dSIlya Dryomov 		kfree(hoid->nspace);
1631a02a946dSIlya Dryomov 		kfree(hoid);
1632a02a946dSIlya Dryomov 	}
1633a02a946dSIlya Dryomov }
1634a02a946dSIlya Dryomov 
1635a02a946dSIlya Dryomov static struct ceph_osd_backoff *alloc_backoff(void)
1636a02a946dSIlya Dryomov {
1637a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
1638a02a946dSIlya Dryomov 
1639a02a946dSIlya Dryomov 	backoff = kzalloc(sizeof(*backoff), GFP_NOIO);
1640a02a946dSIlya Dryomov 	if (!backoff)
1641a02a946dSIlya Dryomov 		return NULL;
1642a02a946dSIlya Dryomov 
1643a02a946dSIlya Dryomov 	RB_CLEAR_NODE(&backoff->spg_node);
1644a02a946dSIlya Dryomov 	RB_CLEAR_NODE(&backoff->id_node);
1645a02a946dSIlya Dryomov 	return backoff;
1646a02a946dSIlya Dryomov }
1647a02a946dSIlya Dryomov 
1648a02a946dSIlya Dryomov static void free_backoff(struct ceph_osd_backoff *backoff)
1649a02a946dSIlya Dryomov {
1650a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&backoff->spg_node));
1651a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&backoff->id_node));
1652a02a946dSIlya Dryomov 
1653a02a946dSIlya Dryomov 	free_hoid(backoff->begin);
1654a02a946dSIlya Dryomov 	free_hoid(backoff->end);
1655a02a946dSIlya Dryomov 	kfree(backoff);
1656a02a946dSIlya Dryomov }
1657a02a946dSIlya Dryomov 
1658a02a946dSIlya Dryomov /*
1659a02a946dSIlya Dryomov  * Within a specific spgid, backoffs are managed by ->begin hoid.
1660a02a946dSIlya Dryomov  */
1661a02a946dSIlya Dryomov DEFINE_RB_INSDEL_FUNCS2(backoff, struct ceph_osd_backoff, begin, hoid_compare,
1662a02a946dSIlya Dryomov 			RB_BYVAL, spg_node);
1663a02a946dSIlya Dryomov 
1664a02a946dSIlya Dryomov static struct ceph_osd_backoff *lookup_containing_backoff(struct rb_root *root,
1665a02a946dSIlya Dryomov 					    const struct ceph_hobject_id *hoid)
1666a02a946dSIlya Dryomov {
1667a02a946dSIlya Dryomov 	struct rb_node *n = root->rb_node;
1668a02a946dSIlya Dryomov 
1669a02a946dSIlya Dryomov 	while (n) {
1670a02a946dSIlya Dryomov 		struct ceph_osd_backoff *cur =
1671a02a946dSIlya Dryomov 		    rb_entry(n, struct ceph_osd_backoff, spg_node);
1672a02a946dSIlya Dryomov 		int cmp;
1673a02a946dSIlya Dryomov 
1674a02a946dSIlya Dryomov 		cmp = hoid_compare(hoid, cur->begin);
1675a02a946dSIlya Dryomov 		if (cmp < 0) {
1676a02a946dSIlya Dryomov 			n = n->rb_left;
1677a02a946dSIlya Dryomov 		} else if (cmp > 0) {
1678a02a946dSIlya Dryomov 			if (hoid_compare(hoid, cur->end) < 0)
1679a02a946dSIlya Dryomov 				return cur;
1680a02a946dSIlya Dryomov 
1681a02a946dSIlya Dryomov 			n = n->rb_right;
1682a02a946dSIlya Dryomov 		} else {
1683a02a946dSIlya Dryomov 			return cur;
1684a02a946dSIlya Dryomov 		}
1685a02a946dSIlya Dryomov 	}
1686a02a946dSIlya Dryomov 
1687a02a946dSIlya Dryomov 	return NULL;
1688a02a946dSIlya Dryomov }
1689a02a946dSIlya Dryomov 
1690a02a946dSIlya Dryomov /*
1691a02a946dSIlya Dryomov  * Each backoff has a unique id within its OSD session.
1692a02a946dSIlya Dryomov  */
1693a02a946dSIlya Dryomov DEFINE_RB_FUNCS(backoff_by_id, struct ceph_osd_backoff, id, id_node)
1694a02a946dSIlya Dryomov 
1695a02a946dSIlya Dryomov static void clear_backoffs(struct ceph_osd *osd)
1696a02a946dSIlya Dryomov {
1697a02a946dSIlya Dryomov 	while (!RB_EMPTY_ROOT(&osd->o_backoff_mappings)) {
1698a02a946dSIlya Dryomov 		struct ceph_spg_mapping *spg =
1699a02a946dSIlya Dryomov 		    rb_entry(rb_first(&osd->o_backoff_mappings),
1700a02a946dSIlya Dryomov 			     struct ceph_spg_mapping, node);
1701a02a946dSIlya Dryomov 
1702a02a946dSIlya Dryomov 		while (!RB_EMPTY_ROOT(&spg->backoffs)) {
1703a02a946dSIlya Dryomov 			struct ceph_osd_backoff *backoff =
1704a02a946dSIlya Dryomov 			    rb_entry(rb_first(&spg->backoffs),
1705a02a946dSIlya Dryomov 				     struct ceph_osd_backoff, spg_node);
1706a02a946dSIlya Dryomov 
1707a02a946dSIlya Dryomov 			erase_backoff(&spg->backoffs, backoff);
1708a02a946dSIlya Dryomov 			erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
1709a02a946dSIlya Dryomov 			free_backoff(backoff);
1710a02a946dSIlya Dryomov 		}
1711a02a946dSIlya Dryomov 		erase_spg_mapping(&osd->o_backoff_mappings, spg);
1712a02a946dSIlya Dryomov 		free_spg_mapping(spg);
1713a02a946dSIlya Dryomov 	}
1714a02a946dSIlya Dryomov }
1715a02a946dSIlya Dryomov 
1716a02a946dSIlya Dryomov /*
1717a02a946dSIlya Dryomov  * Set up a temporary, non-owning view into @t.
1718a02a946dSIlya Dryomov  */
1719a02a946dSIlya Dryomov static void hoid_fill_from_target(struct ceph_hobject_id *hoid,
1720a02a946dSIlya Dryomov 				  const struct ceph_osd_request_target *t)
1721a02a946dSIlya Dryomov {
1722a02a946dSIlya Dryomov 	hoid->key = NULL;
1723a02a946dSIlya Dryomov 	hoid->key_len = 0;
1724a02a946dSIlya Dryomov 	hoid->oid = t->target_oid.name;
1725a02a946dSIlya Dryomov 	hoid->oid_len = t->target_oid.name_len;
1726a02a946dSIlya Dryomov 	hoid->snapid = CEPH_NOSNAP;
1727a02a946dSIlya Dryomov 	hoid->hash = t->pgid.seed;
1728a02a946dSIlya Dryomov 	hoid->is_max = false;
1729a02a946dSIlya Dryomov 	if (t->target_oloc.pool_ns) {
1730a02a946dSIlya Dryomov 		hoid->nspace = t->target_oloc.pool_ns->str;
1731a02a946dSIlya Dryomov 		hoid->nspace_len = t->target_oloc.pool_ns->len;
1732a02a946dSIlya Dryomov 	} else {
1733a02a946dSIlya Dryomov 		hoid->nspace = NULL;
1734a02a946dSIlya Dryomov 		hoid->nspace_len = 0;
1735a02a946dSIlya Dryomov 	}
1736a02a946dSIlya Dryomov 	hoid->pool = t->target_oloc.pool;
1737a02a946dSIlya Dryomov 	ceph_hoid_build_hash_cache(hoid);
1738a02a946dSIlya Dryomov }
1739a02a946dSIlya Dryomov 
1740a02a946dSIlya Dryomov static bool should_plug_request(struct ceph_osd_request *req)
1741a02a946dSIlya Dryomov {
1742a02a946dSIlya Dryomov 	struct ceph_osd *osd = req->r_osd;
1743a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
1744a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
1745a02a946dSIlya Dryomov 	struct ceph_hobject_id hoid;
1746a02a946dSIlya Dryomov 
1747a02a946dSIlya Dryomov 	spg = lookup_spg_mapping(&osd->o_backoff_mappings, &req->r_t.spgid);
1748a02a946dSIlya Dryomov 	if (!spg)
1749a02a946dSIlya Dryomov 		return false;
1750a02a946dSIlya Dryomov 
1751a02a946dSIlya Dryomov 	hoid_fill_from_target(&hoid, &req->r_t);
1752a02a946dSIlya Dryomov 	backoff = lookup_containing_backoff(&spg->backoffs, &hoid);
1753a02a946dSIlya Dryomov 	if (!backoff)
1754a02a946dSIlya Dryomov 		return false;
1755a02a946dSIlya Dryomov 
1756a02a946dSIlya Dryomov 	dout("%s req %p tid %llu backoff osd%d spgid %llu.%xs%d id %llu\n",
1757a02a946dSIlya Dryomov 	     __func__, req, req->r_tid, osd->o_osd, backoff->spgid.pgid.pool,
1758a02a946dSIlya Dryomov 	     backoff->spgid.pgid.seed, backoff->spgid.shard, backoff->id);
1759a02a946dSIlya Dryomov 	return true;
1760a02a946dSIlya Dryomov }
1761a02a946dSIlya Dryomov 
1762bb873b53SIlya Dryomov static void setup_request_data(struct ceph_osd_request *req,
1763bb873b53SIlya Dryomov 			       struct ceph_msg *msg)
17643d14c5d2SYehuda Sadeh {
1765bb873b53SIlya Dryomov 	u32 data_len = 0;
1766bb873b53SIlya Dryomov 	int i;
17673d14c5d2SYehuda Sadeh 
1768bb873b53SIlya Dryomov 	if (!list_empty(&msg->data))
1769bb873b53SIlya Dryomov 		return;
17703d14c5d2SYehuda Sadeh 
1771bb873b53SIlya Dryomov 	WARN_ON(msg->data_length);
1772bb873b53SIlya Dryomov 	for (i = 0; i < req->r_num_ops; i++) {
1773bb873b53SIlya Dryomov 		struct ceph_osd_req_op *op = &req->r_ops[i];
1774bb873b53SIlya Dryomov 
1775bb873b53SIlya Dryomov 		switch (op->op) {
1776bb873b53SIlya Dryomov 		/* request */
1777bb873b53SIlya Dryomov 		case CEPH_OSD_OP_WRITE:
1778bb873b53SIlya Dryomov 		case CEPH_OSD_OP_WRITEFULL:
1779bb873b53SIlya Dryomov 			WARN_ON(op->indata_len != op->extent.length);
1780bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(msg, &op->extent.osd_data);
1781bb873b53SIlya Dryomov 			break;
1782bb873b53SIlya Dryomov 		case CEPH_OSD_OP_SETXATTR:
1783bb873b53SIlya Dryomov 		case CEPH_OSD_OP_CMPXATTR:
1784bb873b53SIlya Dryomov 			WARN_ON(op->indata_len != op->xattr.name_len +
1785bb873b53SIlya Dryomov 						  op->xattr.value_len);
1786bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(msg, &op->xattr.osd_data);
1787bb873b53SIlya Dryomov 			break;
1788922dab61SIlya Dryomov 		case CEPH_OSD_OP_NOTIFY_ACK:
1789922dab61SIlya Dryomov 			ceph_osdc_msg_data_add(msg,
1790922dab61SIlya Dryomov 					       &op->notify_ack.request_data);
1791922dab61SIlya Dryomov 			break;
1792bb873b53SIlya Dryomov 
1793bb873b53SIlya Dryomov 		/* reply */
1794bb873b53SIlya Dryomov 		case CEPH_OSD_OP_STAT:
1795bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(req->r_reply,
1796bb873b53SIlya Dryomov 					       &op->raw_data_in);
1797bb873b53SIlya Dryomov 			break;
1798bb873b53SIlya Dryomov 		case CEPH_OSD_OP_READ:
1799bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(req->r_reply,
1800bb873b53SIlya Dryomov 					       &op->extent.osd_data);
1801bb873b53SIlya Dryomov 			break;
1802a4ed38d7SDouglas Fuller 		case CEPH_OSD_OP_LIST_WATCHERS:
1803a4ed38d7SDouglas Fuller 			ceph_osdc_msg_data_add(req->r_reply,
1804a4ed38d7SDouglas Fuller 					       &op->list_watchers.response_data);
1805a4ed38d7SDouglas Fuller 			break;
1806bb873b53SIlya Dryomov 
1807bb873b53SIlya Dryomov 		/* both */
1808bb873b53SIlya Dryomov 		case CEPH_OSD_OP_CALL:
1809bb873b53SIlya Dryomov 			WARN_ON(op->indata_len != op->cls.class_len +
1810bb873b53SIlya Dryomov 						  op->cls.method_len +
1811bb873b53SIlya Dryomov 						  op->cls.indata_len);
1812bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(msg, &op->cls.request_info);
1813bb873b53SIlya Dryomov 			/* optional, can be NONE */
1814bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(msg, &op->cls.request_data);
1815bb873b53SIlya Dryomov 			/* optional, can be NONE */
1816bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(req->r_reply,
1817bb873b53SIlya Dryomov 					       &op->cls.response_data);
1818bb873b53SIlya Dryomov 			break;
181919079203SIlya Dryomov 		case CEPH_OSD_OP_NOTIFY:
182019079203SIlya Dryomov 			ceph_osdc_msg_data_add(msg,
182119079203SIlya Dryomov 					       &op->notify.request_data);
182219079203SIlya Dryomov 			ceph_osdc_msg_data_add(req->r_reply,
182319079203SIlya Dryomov 					       &op->notify.response_data);
182419079203SIlya Dryomov 			break;
1825bb873b53SIlya Dryomov 		}
1826bb873b53SIlya Dryomov 
1827bb873b53SIlya Dryomov 		data_len += op->indata_len;
1828bb873b53SIlya Dryomov 	}
1829bb873b53SIlya Dryomov 
1830bb873b53SIlya Dryomov 	WARN_ON(data_len != msg->data_length);
1831bb873b53SIlya Dryomov }
1832bb873b53SIlya Dryomov 
18332e59ffd1SIlya Dryomov static void encode_pgid(void **p, const struct ceph_pg *pgid)
18342e59ffd1SIlya Dryomov {
18352e59ffd1SIlya Dryomov 	ceph_encode_8(p, 1);
18362e59ffd1SIlya Dryomov 	ceph_encode_64(p, pgid->pool);
18372e59ffd1SIlya Dryomov 	ceph_encode_32(p, pgid->seed);
18382e59ffd1SIlya Dryomov 	ceph_encode_32(p, -1); /* preferred */
18392e59ffd1SIlya Dryomov }
18402e59ffd1SIlya Dryomov 
18418cb441c0SIlya Dryomov static void encode_spgid(void **p, const struct ceph_spg *spgid)
18428cb441c0SIlya Dryomov {
18438cb441c0SIlya Dryomov 	ceph_start_encoding(p, 1, 1, CEPH_PGID_ENCODING_LEN + 1);
18448cb441c0SIlya Dryomov 	encode_pgid(p, &spgid->pgid);
18458cb441c0SIlya Dryomov 	ceph_encode_8(p, spgid->shard);
18468cb441c0SIlya Dryomov }
18478cb441c0SIlya Dryomov 
18482e59ffd1SIlya Dryomov static void encode_oloc(void **p, void *end,
18492e59ffd1SIlya Dryomov 			const struct ceph_object_locator *oloc)
18502e59ffd1SIlya Dryomov {
18512e59ffd1SIlya Dryomov 	ceph_start_encoding(p, 5, 4, ceph_oloc_encoding_size(oloc));
18522e59ffd1SIlya Dryomov 	ceph_encode_64(p, oloc->pool);
18532e59ffd1SIlya Dryomov 	ceph_encode_32(p, -1); /* preferred */
18542e59ffd1SIlya Dryomov 	ceph_encode_32(p, 0);  /* key len */
18552e59ffd1SIlya Dryomov 	if (oloc->pool_ns)
18562e59ffd1SIlya Dryomov 		ceph_encode_string(p, end, oloc->pool_ns->str,
18572e59ffd1SIlya Dryomov 				   oloc->pool_ns->len);
18582e59ffd1SIlya Dryomov 	else
18592e59ffd1SIlya Dryomov 		ceph_encode_32(p, 0);
18602e59ffd1SIlya Dryomov }
18612e59ffd1SIlya Dryomov 
18628cb441c0SIlya Dryomov static void encode_request_partial(struct ceph_osd_request *req,
18638cb441c0SIlya Dryomov 				   struct ceph_msg *msg)
1864bb873b53SIlya Dryomov {
1865bb873b53SIlya Dryomov 	void *p = msg->front.iov_base;
1866bb873b53SIlya Dryomov 	void *const end = p + msg->front_alloc_len;
1867bb873b53SIlya Dryomov 	u32 data_len = 0;
1868bb873b53SIlya Dryomov 	int i;
1869bb873b53SIlya Dryomov 
1870bb873b53SIlya Dryomov 	if (req->r_flags & CEPH_OSD_FLAG_WRITE) {
1871bb873b53SIlya Dryomov 		/* snapshots aren't writeable */
1872bb873b53SIlya Dryomov 		WARN_ON(req->r_snapid != CEPH_NOSNAP);
1873bb873b53SIlya Dryomov 	} else {
1874bb873b53SIlya Dryomov 		WARN_ON(req->r_mtime.tv_sec || req->r_mtime.tv_nsec ||
1875bb873b53SIlya Dryomov 			req->r_data_offset || req->r_snapc);
1876bb873b53SIlya Dryomov 	}
1877bb873b53SIlya Dryomov 
1878bb873b53SIlya Dryomov 	setup_request_data(req, msg);
1879bb873b53SIlya Dryomov 
18808cb441c0SIlya Dryomov 	encode_spgid(&p, &req->r_t.spgid); /* actual spg */
18818cb441c0SIlya Dryomov 	ceph_encode_32(&p, req->r_t.pgid.seed); /* raw hash */
1882bb873b53SIlya Dryomov 	ceph_encode_32(&p, req->r_osdc->osdmap->epoch);
1883bb873b53SIlya Dryomov 	ceph_encode_32(&p, req->r_flags);
18848cb441c0SIlya Dryomov 
18858cb441c0SIlya Dryomov 	/* reqid */
18868cb441c0SIlya Dryomov 	ceph_start_encoding(&p, 2, 2, sizeof(struct ceph_osd_reqid));
18878cb441c0SIlya Dryomov 	memset(p, 0, sizeof(struct ceph_osd_reqid));
18888cb441c0SIlya Dryomov 	p += sizeof(struct ceph_osd_reqid);
18898cb441c0SIlya Dryomov 
18908cb441c0SIlya Dryomov 	/* trace */
18918cb441c0SIlya Dryomov 	memset(p, 0, sizeof(struct ceph_blkin_trace_info));
18928cb441c0SIlya Dryomov 	p += sizeof(struct ceph_blkin_trace_info);
18938cb441c0SIlya Dryomov 
18948cb441c0SIlya Dryomov 	ceph_encode_32(&p, 0); /* client_inc, always 0 */
1895bb873b53SIlya Dryomov 	ceph_encode_timespec(p, &req->r_mtime);
1896bb873b53SIlya Dryomov 	p += sizeof(struct ceph_timespec);
1897aa26d662SJeff Layton 
18982e59ffd1SIlya Dryomov 	encode_oloc(&p, end, &req->r_t.target_oloc);
18992e59ffd1SIlya Dryomov 	ceph_encode_string(&p, end, req->r_t.target_oid.name,
19002e59ffd1SIlya Dryomov 			   req->r_t.target_oid.name_len);
1901bb873b53SIlya Dryomov 
1902bb873b53SIlya Dryomov 	/* ops, can imply data */
1903bb873b53SIlya Dryomov 	ceph_encode_16(&p, req->r_num_ops);
1904bb873b53SIlya Dryomov 	for (i = 0; i < req->r_num_ops; i++) {
1905bb873b53SIlya Dryomov 		data_len += osd_req_encode_op(p, &req->r_ops[i]);
1906bb873b53SIlya Dryomov 		p += sizeof(struct ceph_osd_op);
1907bb873b53SIlya Dryomov 	}
1908bb873b53SIlya Dryomov 
1909bb873b53SIlya Dryomov 	ceph_encode_64(&p, req->r_snapid); /* snapid */
1910bb873b53SIlya Dryomov 	if (req->r_snapc) {
1911bb873b53SIlya Dryomov 		ceph_encode_64(&p, req->r_snapc->seq);
1912bb873b53SIlya Dryomov 		ceph_encode_32(&p, req->r_snapc->num_snaps);
1913bb873b53SIlya Dryomov 		for (i = 0; i < req->r_snapc->num_snaps; i++)
1914bb873b53SIlya Dryomov 			ceph_encode_64(&p, req->r_snapc->snaps[i]);
1915bb873b53SIlya Dryomov 	} else {
1916bb873b53SIlya Dryomov 		ceph_encode_64(&p, 0); /* snap_seq */
1917bb873b53SIlya Dryomov 		ceph_encode_32(&p, 0); /* snaps len */
1918bb873b53SIlya Dryomov 	}
1919bb873b53SIlya Dryomov 
1920bb873b53SIlya Dryomov 	ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
19218cb441c0SIlya Dryomov 	BUG_ON(p != end - 8); /* space for features */
1922bb873b53SIlya Dryomov 
19238cb441c0SIlya Dryomov 	msg->hdr.version = cpu_to_le16(8); /* MOSDOp v8 */
19248cb441c0SIlya Dryomov 	/* front_len is finalized in encode_request_finish() */
1925bb873b53SIlya Dryomov 	msg->hdr.data_len = cpu_to_le32(data_len);
1926bb873b53SIlya Dryomov 	/*
1927bb873b53SIlya Dryomov 	 * The header "data_off" is a hint to the receiver allowing it
1928bb873b53SIlya Dryomov 	 * to align received data into its buffers such that there's no
1929bb873b53SIlya Dryomov 	 * need to re-copy it before writing it to disk (direct I/O).
1930bb873b53SIlya Dryomov 	 */
1931bb873b53SIlya Dryomov 	msg->hdr.data_off = cpu_to_le16(req->r_data_offset);
1932bb873b53SIlya Dryomov 
19338cb441c0SIlya Dryomov 	dout("%s req %p msg %p oid %s oid_len %d\n", __func__, req, msg,
19348cb441c0SIlya Dryomov 	     req->r_t.target_oid.name, req->r_t.target_oid.name_len);
19358cb441c0SIlya Dryomov }
19368cb441c0SIlya Dryomov 
19378cb441c0SIlya Dryomov static void encode_request_finish(struct ceph_msg *msg)
19388cb441c0SIlya Dryomov {
19398cb441c0SIlya Dryomov 	void *p = msg->front.iov_base;
19408cb441c0SIlya Dryomov 	void *const end = p + msg->front_alloc_len;
19418cb441c0SIlya Dryomov 
19428cb441c0SIlya Dryomov 	if (CEPH_HAVE_FEATURE(msg->con->peer_features, RESEND_ON_SPLIT)) {
19438cb441c0SIlya Dryomov 		/* luminous OSD -- encode features and be done */
19448cb441c0SIlya Dryomov 		p = end - 8;
19458cb441c0SIlya Dryomov 		ceph_encode_64(&p, msg->con->peer_features);
19468cb441c0SIlya Dryomov 	} else {
19478cb441c0SIlya Dryomov 		struct {
19488cb441c0SIlya Dryomov 			char spgid[CEPH_ENCODING_START_BLK_LEN +
19498cb441c0SIlya Dryomov 				   CEPH_PGID_ENCODING_LEN + 1];
19508cb441c0SIlya Dryomov 			__le32 hash;
19518cb441c0SIlya Dryomov 			__le32 epoch;
19528cb441c0SIlya Dryomov 			__le32 flags;
19538cb441c0SIlya Dryomov 			char reqid[CEPH_ENCODING_START_BLK_LEN +
19548cb441c0SIlya Dryomov 				   sizeof(struct ceph_osd_reqid)];
19558cb441c0SIlya Dryomov 			char trace[sizeof(struct ceph_blkin_trace_info)];
19568cb441c0SIlya Dryomov 			__le32 client_inc;
19578cb441c0SIlya Dryomov 			struct ceph_timespec mtime;
19588cb441c0SIlya Dryomov 		} __packed head;
19598cb441c0SIlya Dryomov 		struct ceph_pg pgid;
19608cb441c0SIlya Dryomov 		void *oloc, *oid, *tail;
19618cb441c0SIlya Dryomov 		int oloc_len, oid_len, tail_len;
19628cb441c0SIlya Dryomov 		int len;
19638cb441c0SIlya Dryomov 
19648cb441c0SIlya Dryomov 		/*
19658cb441c0SIlya Dryomov 		 * Pre-luminous OSD -- reencode v8 into v4 using @head
19668cb441c0SIlya Dryomov 		 * as a temporary buffer.  Encode the raw PG; the rest
19678cb441c0SIlya Dryomov 		 * is just a matter of moving oloc, oid and tail blobs
19688cb441c0SIlya Dryomov 		 * around.
19698cb441c0SIlya Dryomov 		 */
19708cb441c0SIlya Dryomov 		memcpy(&head, p, sizeof(head));
19718cb441c0SIlya Dryomov 		p += sizeof(head);
19728cb441c0SIlya Dryomov 
19738cb441c0SIlya Dryomov 		oloc = p;
19748cb441c0SIlya Dryomov 		p += CEPH_ENCODING_START_BLK_LEN;
19758cb441c0SIlya Dryomov 		pgid.pool = ceph_decode_64(&p);
19768cb441c0SIlya Dryomov 		p += 4 + 4; /* preferred, key len */
19778cb441c0SIlya Dryomov 		len = ceph_decode_32(&p);
19788cb441c0SIlya Dryomov 		p += len;   /* nspace */
19798cb441c0SIlya Dryomov 		oloc_len = p - oloc;
19808cb441c0SIlya Dryomov 
19818cb441c0SIlya Dryomov 		oid = p;
19828cb441c0SIlya Dryomov 		len = ceph_decode_32(&p);
19838cb441c0SIlya Dryomov 		p += len;
19848cb441c0SIlya Dryomov 		oid_len = p - oid;
19858cb441c0SIlya Dryomov 
19868cb441c0SIlya Dryomov 		tail = p;
19878cb441c0SIlya Dryomov 		tail_len = (end - p) - 8;
19888cb441c0SIlya Dryomov 
19898cb441c0SIlya Dryomov 		p = msg->front.iov_base;
19908cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.client_inc, sizeof(head.client_inc));
19918cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.epoch, sizeof(head.epoch));
19928cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.flags, sizeof(head.flags));
19938cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.mtime, sizeof(head.mtime));
19948cb441c0SIlya Dryomov 
19958cb441c0SIlya Dryomov 		/* reassert_version */
19968cb441c0SIlya Dryomov 		memset(p, 0, sizeof(struct ceph_eversion));
19978cb441c0SIlya Dryomov 		p += sizeof(struct ceph_eversion);
19988cb441c0SIlya Dryomov 
19998cb441c0SIlya Dryomov 		BUG_ON(p >= oloc);
20008cb441c0SIlya Dryomov 		memmove(p, oloc, oloc_len);
20018cb441c0SIlya Dryomov 		p += oloc_len;
20028cb441c0SIlya Dryomov 
20038cb441c0SIlya Dryomov 		pgid.seed = le32_to_cpu(head.hash);
20048cb441c0SIlya Dryomov 		encode_pgid(&p, &pgid); /* raw pg */
20058cb441c0SIlya Dryomov 
20068cb441c0SIlya Dryomov 		BUG_ON(p >= oid);
20078cb441c0SIlya Dryomov 		memmove(p, oid, oid_len);
20088cb441c0SIlya Dryomov 		p += oid_len;
20098cb441c0SIlya Dryomov 
20108cb441c0SIlya Dryomov 		/* tail -- ops, snapid, snapc, retry_attempt */
20118cb441c0SIlya Dryomov 		BUG_ON(p >= tail);
20128cb441c0SIlya Dryomov 		memmove(p, tail, tail_len);
20138cb441c0SIlya Dryomov 		p += tail_len;
20148cb441c0SIlya Dryomov 
20158cb441c0SIlya Dryomov 		msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
20168cb441c0SIlya Dryomov 	}
20178cb441c0SIlya Dryomov 
20188cb441c0SIlya Dryomov 	BUG_ON(p > end);
20198cb441c0SIlya Dryomov 	msg->front.iov_len = p - msg->front.iov_base;
20208cb441c0SIlya Dryomov 	msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
20218cb441c0SIlya Dryomov 
20228cb441c0SIlya Dryomov 	dout("%s msg %p tid %llu %u+%u+%u v%d\n", __func__, msg,
20238cb441c0SIlya Dryomov 	     le64_to_cpu(msg->hdr.tid), le32_to_cpu(msg->hdr.front_len),
20248cb441c0SIlya Dryomov 	     le32_to_cpu(msg->hdr.middle_len), le32_to_cpu(msg->hdr.data_len),
20258cb441c0SIlya Dryomov 	     le16_to_cpu(msg->hdr.version));
2026bb873b53SIlya Dryomov }
2027bb873b53SIlya Dryomov 
2028bb873b53SIlya Dryomov /*
2029bb873b53SIlya Dryomov  * @req has to be assigned a tid and registered.
2030bb873b53SIlya Dryomov  */
2031bb873b53SIlya Dryomov static void send_request(struct ceph_osd_request *req)
2032bb873b53SIlya Dryomov {
2033bb873b53SIlya Dryomov 	struct ceph_osd *osd = req->r_osd;
2034bb873b53SIlya Dryomov 
20355aea3dcdSIlya Dryomov 	verify_osd_locked(osd);
2036bb873b53SIlya Dryomov 	WARN_ON(osd->o_osd != req->r_t.osd);
2037bb873b53SIlya Dryomov 
2038a02a946dSIlya Dryomov 	/* backoff? */
2039a02a946dSIlya Dryomov 	if (should_plug_request(req))
2040a02a946dSIlya Dryomov 		return;
2041a02a946dSIlya Dryomov 
20425aea3dcdSIlya Dryomov 	/*
20435aea3dcdSIlya Dryomov 	 * We may have a previously queued request message hanging
20445aea3dcdSIlya Dryomov 	 * around.  Cancel it to avoid corrupting the msgr.
20455aea3dcdSIlya Dryomov 	 */
20465aea3dcdSIlya Dryomov 	if (req->r_sent)
20475aea3dcdSIlya Dryomov 		ceph_msg_revoke(req->r_request);
20485aea3dcdSIlya Dryomov 
2049bb873b53SIlya Dryomov 	req->r_flags |= CEPH_OSD_FLAG_KNOWN_REDIR;
2050bb873b53SIlya Dryomov 	if (req->r_attempts)
2051bb873b53SIlya Dryomov 		req->r_flags |= CEPH_OSD_FLAG_RETRY;
2052bb873b53SIlya Dryomov 	else
2053bb873b53SIlya Dryomov 		WARN_ON(req->r_flags & CEPH_OSD_FLAG_RETRY);
2054bb873b53SIlya Dryomov 
20558cb441c0SIlya Dryomov 	encode_request_partial(req, req->r_request);
2056bb873b53SIlya Dryomov 
205704c7d789SIlya 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",
2058bb873b53SIlya Dryomov 	     __func__, req, req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed,
2059dc98ff72SIlya Dryomov 	     req->r_t.spgid.pgid.pool, req->r_t.spgid.pgid.seed,
206004c7d789SIlya Dryomov 	     req->r_t.spgid.shard, osd->o_osd, req->r_t.epoch, req->r_flags,
206104c7d789SIlya Dryomov 	     req->r_attempts);
2062bb873b53SIlya Dryomov 
2063bb873b53SIlya Dryomov 	req->r_t.paused = false;
20643d14c5d2SYehuda Sadeh 	req->r_stamp = jiffies;
2065bb873b53SIlya Dryomov 	req->r_attempts++;
20663d14c5d2SYehuda Sadeh 
2067bb873b53SIlya Dryomov 	req->r_sent = osd->o_incarnation;
2068bb873b53SIlya Dryomov 	req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
2069bb873b53SIlya Dryomov 	ceph_con_send(&osd->o_con, ceph_msg_get(req->r_request));
20703d14c5d2SYehuda Sadeh }
20713d14c5d2SYehuda Sadeh 
207242c1b124SIlya Dryomov static void maybe_request_map(struct ceph_osd_client *osdc)
207342c1b124SIlya Dryomov {
207442c1b124SIlya Dryomov 	bool continuous = false;
207542c1b124SIlya Dryomov 
20765aea3dcdSIlya Dryomov 	verify_osdc_locked(osdc);
207742c1b124SIlya Dryomov 	WARN_ON(!osdc->osdmap->epoch);
207842c1b124SIlya Dryomov 
2079b7ec35b3SIlya Dryomov 	if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2080b7ec35b3SIlya Dryomov 	    ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD) ||
2081b7ec35b3SIlya Dryomov 	    ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
208242c1b124SIlya Dryomov 		dout("%s osdc %p continuous\n", __func__, osdc);
208342c1b124SIlya Dryomov 		continuous = true;
208442c1b124SIlya Dryomov 	} else {
208542c1b124SIlya Dryomov 		dout("%s osdc %p onetime\n", __func__, osdc);
208642c1b124SIlya Dryomov 	}
208742c1b124SIlya Dryomov 
208842c1b124SIlya Dryomov 	if (ceph_monc_want_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
208942c1b124SIlya Dryomov 			       osdc->osdmap->epoch + 1, continuous))
209042c1b124SIlya Dryomov 		ceph_monc_renew_subs(&osdc->client->monc);
209142c1b124SIlya Dryomov }
209242c1b124SIlya Dryomov 
2093a1f4020aSJeff Layton static void complete_request(struct ceph_osd_request *req, int err);
20944609245eSIlya Dryomov static void send_map_check(struct ceph_osd_request *req);
20954609245eSIlya Dryomov 
20965aea3dcdSIlya Dryomov static void __submit_request(struct ceph_osd_request *req, bool wrlocked)
20970bbfdfe8SIlya Dryomov {
20985aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
20995aea3dcdSIlya Dryomov 	struct ceph_osd *osd;
21004609245eSIlya Dryomov 	enum calc_target_result ct_res;
21015aea3dcdSIlya Dryomov 	bool need_send = false;
21025aea3dcdSIlya Dryomov 	bool promoted = false;
2103a1f4020aSJeff Layton 	bool need_abort = false;
21040bbfdfe8SIlya Dryomov 
2105b18b9550SIlya Dryomov 	WARN_ON(req->r_tid);
21065aea3dcdSIlya Dryomov 	dout("%s req %p wrlocked %d\n", __func__, req, wrlocked);
21075aea3dcdSIlya Dryomov 
21085aea3dcdSIlya Dryomov again:
21097de030d6SIlya Dryomov 	ct_res = calc_target(osdc, &req->r_t, NULL, false);
21104609245eSIlya Dryomov 	if (ct_res == CALC_TARGET_POOL_DNE && !wrlocked)
21114609245eSIlya Dryomov 		goto promote;
21124609245eSIlya Dryomov 
21135aea3dcdSIlya Dryomov 	osd = lookup_create_osd(osdc, req->r_t.osd, wrlocked);
21145aea3dcdSIlya Dryomov 	if (IS_ERR(osd)) {
21155aea3dcdSIlya Dryomov 		WARN_ON(PTR_ERR(osd) != -EAGAIN || wrlocked);
21165aea3dcdSIlya Dryomov 		goto promote;
21175aea3dcdSIlya Dryomov 	}
21185aea3dcdSIlya Dryomov 
211958eb7932SJeff Layton 	if (osdc->osdmap->epoch < osdc->epoch_barrier) {
212058eb7932SJeff Layton 		dout("req %p epoch %u barrier %u\n", req, osdc->osdmap->epoch,
212158eb7932SJeff Layton 		     osdc->epoch_barrier);
212258eb7932SJeff Layton 		req->r_t.paused = true;
212358eb7932SJeff Layton 		maybe_request_map(osdc);
212458eb7932SJeff Layton 	} else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2125b7ec35b3SIlya Dryomov 		   ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
21265aea3dcdSIlya Dryomov 		dout("req %p pausewr\n", req);
21275aea3dcdSIlya Dryomov 		req->r_t.paused = true;
21285aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
21295aea3dcdSIlya Dryomov 	} else if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
2130b7ec35b3SIlya Dryomov 		   ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
21315aea3dcdSIlya Dryomov 		dout("req %p pauserd\n", req);
21325aea3dcdSIlya Dryomov 		req->r_t.paused = true;
21335aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
21345aea3dcdSIlya Dryomov 	} else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
21355aea3dcdSIlya Dryomov 		   !(req->r_flags & (CEPH_OSD_FLAG_FULL_TRY |
21365aea3dcdSIlya Dryomov 				     CEPH_OSD_FLAG_FULL_FORCE)) &&
2137b7ec35b3SIlya Dryomov 		   (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
21385aea3dcdSIlya Dryomov 		    pool_full(osdc, req->r_t.base_oloc.pool))) {
21395aea3dcdSIlya Dryomov 		dout("req %p full/pool_full\n", req);
21405aea3dcdSIlya Dryomov 		pr_warn_ratelimited("FULL or reached pool quota\n");
21415aea3dcdSIlya Dryomov 		req->r_t.paused = true;
21425aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
2143a1f4020aSJeff Layton 		if (req->r_abort_on_full)
2144a1f4020aSJeff Layton 			need_abort = true;
21455aea3dcdSIlya Dryomov 	} else if (!osd_homeless(osd)) {
21465aea3dcdSIlya Dryomov 		need_send = true;
21470bbfdfe8SIlya Dryomov 	} else {
21485aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
21490bbfdfe8SIlya Dryomov 	}
21500bbfdfe8SIlya Dryomov 
21515aea3dcdSIlya Dryomov 	mutex_lock(&osd->lock);
21525aea3dcdSIlya Dryomov 	/*
21535aea3dcdSIlya Dryomov 	 * Assign the tid atomically with send_request() to protect
21545aea3dcdSIlya Dryomov 	 * multiple writes to the same object from racing with each
21555aea3dcdSIlya Dryomov 	 * other, resulting in out of order ops on the OSDs.
21565aea3dcdSIlya Dryomov 	 */
21575aea3dcdSIlya Dryomov 	req->r_tid = atomic64_inc_return(&osdc->last_tid);
21585aea3dcdSIlya Dryomov 	link_request(osd, req);
21595aea3dcdSIlya Dryomov 	if (need_send)
21605aea3dcdSIlya Dryomov 		send_request(req);
2161a1f4020aSJeff Layton 	else if (need_abort)
2162a1f4020aSJeff Layton 		complete_request(req, -ENOSPC);
21635aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
21645aea3dcdSIlya Dryomov 
21654609245eSIlya Dryomov 	if (ct_res == CALC_TARGET_POOL_DNE)
21664609245eSIlya Dryomov 		send_map_check(req);
21674609245eSIlya Dryomov 
21685aea3dcdSIlya Dryomov 	if (promoted)
21695aea3dcdSIlya Dryomov 		downgrade_write(&osdc->lock);
21705aea3dcdSIlya Dryomov 	return;
21715aea3dcdSIlya Dryomov 
21725aea3dcdSIlya Dryomov promote:
21735aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
21745aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
21755aea3dcdSIlya Dryomov 	wrlocked = true;
21765aea3dcdSIlya Dryomov 	promoted = true;
21775aea3dcdSIlya Dryomov 	goto again;
21780bbfdfe8SIlya Dryomov }
21790bbfdfe8SIlya Dryomov 
21805aea3dcdSIlya Dryomov static void account_request(struct ceph_osd_request *req)
21815aea3dcdSIlya Dryomov {
218254ea0046SIlya Dryomov 	WARN_ON(req->r_flags & (CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK));
2183b18b9550SIlya Dryomov 	WARN_ON(!(req->r_flags & (CEPH_OSD_FLAG_READ | CEPH_OSD_FLAG_WRITE)));
21845aea3dcdSIlya Dryomov 
2185b18b9550SIlya Dryomov 	req->r_flags |= CEPH_OSD_FLAG_ONDISK;
21865aea3dcdSIlya Dryomov 	atomic_inc(&req->r_osdc->num_requests);
21877cc5e38fSIlya Dryomov 
21887cc5e38fSIlya Dryomov 	req->r_start_stamp = jiffies;
21895aea3dcdSIlya Dryomov }
21905aea3dcdSIlya Dryomov 
21915aea3dcdSIlya Dryomov static void submit_request(struct ceph_osd_request *req, bool wrlocked)
21925aea3dcdSIlya Dryomov {
21935aea3dcdSIlya Dryomov 	ceph_osdc_get_request(req);
21945aea3dcdSIlya Dryomov 	account_request(req);
21955aea3dcdSIlya Dryomov 	__submit_request(req, wrlocked);
21965aea3dcdSIlya Dryomov }
21975aea3dcdSIlya Dryomov 
219845ee2c1dSIlya Dryomov static void finish_request(struct ceph_osd_request *req)
21995aea3dcdSIlya Dryomov {
22005aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
22015aea3dcdSIlya Dryomov 
22024609245eSIlya Dryomov 	WARN_ON(lookup_request_mc(&osdc->map_checks, req->r_tid));
220304c7d789SIlya Dryomov 	dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
220404c7d789SIlya Dryomov 
220504c7d789SIlya Dryomov 	if (req->r_osd)
220604c7d789SIlya Dryomov 		unlink_request(req->r_osd, req);
22075aea3dcdSIlya Dryomov 	atomic_dec(&osdc->num_requests);
22085aea3dcdSIlya Dryomov 
22095aea3dcdSIlya Dryomov 	/*
22105aea3dcdSIlya Dryomov 	 * If an OSD has failed or returned and a request has been sent
22115aea3dcdSIlya Dryomov 	 * twice, it's possible to get a reply and end up here while the
22125aea3dcdSIlya Dryomov 	 * request message is queued for delivery.  We will ignore the
22135aea3dcdSIlya Dryomov 	 * reply, so not a big deal, but better to try and catch it.
22145aea3dcdSIlya Dryomov 	 */
22155aea3dcdSIlya Dryomov 	ceph_msg_revoke(req->r_request);
22165aea3dcdSIlya Dryomov 	ceph_msg_revoke_incoming(req->r_reply);
22175aea3dcdSIlya Dryomov }
22185aea3dcdSIlya Dryomov 
2219fe5da05eSIlya Dryomov static void __complete_request(struct ceph_osd_request *req)
2220fe5da05eSIlya Dryomov {
2221b18b9550SIlya Dryomov 	if (req->r_callback) {
2222b18b9550SIlya Dryomov 		dout("%s req %p tid %llu cb %pf result %d\n", __func__, req,
2223b18b9550SIlya Dryomov 		     req->r_tid, req->r_callback, req->r_result);
2224fe5da05eSIlya Dryomov 		req->r_callback(req);
2225b18b9550SIlya Dryomov 	}
2226fe5da05eSIlya Dryomov }
2227fe5da05eSIlya Dryomov 
22284609245eSIlya Dryomov /*
2229b18b9550SIlya Dryomov  * This is open-coded in handle_reply().
22304609245eSIlya Dryomov  */
22314609245eSIlya Dryomov static void complete_request(struct ceph_osd_request *req, int err)
22324609245eSIlya Dryomov {
22334609245eSIlya Dryomov 	dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
22344609245eSIlya Dryomov 
22354609245eSIlya Dryomov 	req->r_result = err;
223645ee2c1dSIlya Dryomov 	finish_request(req);
22374609245eSIlya Dryomov 	__complete_request(req);
2238b18b9550SIlya Dryomov 	complete_all(&req->r_completion);
22394609245eSIlya Dryomov 	ceph_osdc_put_request(req);
22404609245eSIlya Dryomov }
22414609245eSIlya Dryomov 
22424609245eSIlya Dryomov static void cancel_map_check(struct ceph_osd_request *req)
22434609245eSIlya Dryomov {
22444609245eSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
22454609245eSIlya Dryomov 	struct ceph_osd_request *lookup_req;
22464609245eSIlya Dryomov 
22474609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
22484609245eSIlya Dryomov 
22494609245eSIlya Dryomov 	lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
22504609245eSIlya Dryomov 	if (!lookup_req)
22514609245eSIlya Dryomov 		return;
22524609245eSIlya Dryomov 
22534609245eSIlya Dryomov 	WARN_ON(lookup_req != req);
22544609245eSIlya Dryomov 	erase_request_mc(&osdc->map_checks, req);
22554609245eSIlya Dryomov 	ceph_osdc_put_request(req);
22564609245eSIlya Dryomov }
22574609245eSIlya Dryomov 
22585aea3dcdSIlya Dryomov static void cancel_request(struct ceph_osd_request *req)
22595aea3dcdSIlya Dryomov {
22605aea3dcdSIlya Dryomov 	dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
22615aea3dcdSIlya Dryomov 
22624609245eSIlya Dryomov 	cancel_map_check(req);
226345ee2c1dSIlya Dryomov 	finish_request(req);
2264b18b9550SIlya Dryomov 	complete_all(&req->r_completion);
2265c297eb42SIlya Dryomov 	ceph_osdc_put_request(req);
22665aea3dcdSIlya Dryomov }
22675aea3dcdSIlya Dryomov 
22687cc5e38fSIlya Dryomov static void abort_request(struct ceph_osd_request *req, int err)
22697cc5e38fSIlya Dryomov {
22707cc5e38fSIlya Dryomov 	dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
22717cc5e38fSIlya Dryomov 
22727cc5e38fSIlya Dryomov 	cancel_map_check(req);
22737cc5e38fSIlya Dryomov 	complete_request(req, err);
22747cc5e38fSIlya Dryomov }
22757cc5e38fSIlya Dryomov 
227658eb7932SJeff Layton static void update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
227758eb7932SJeff Layton {
227858eb7932SJeff Layton 	if (likely(eb > osdc->epoch_barrier)) {
227958eb7932SJeff Layton 		dout("updating epoch_barrier from %u to %u\n",
228058eb7932SJeff Layton 				osdc->epoch_barrier, eb);
228158eb7932SJeff Layton 		osdc->epoch_barrier = eb;
228258eb7932SJeff Layton 		/* Request map if we're not to the barrier yet */
228358eb7932SJeff Layton 		if (eb > osdc->osdmap->epoch)
228458eb7932SJeff Layton 			maybe_request_map(osdc);
228558eb7932SJeff Layton 	}
228658eb7932SJeff Layton }
228758eb7932SJeff Layton 
228858eb7932SJeff Layton void ceph_osdc_update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
228958eb7932SJeff Layton {
229058eb7932SJeff Layton 	down_read(&osdc->lock);
229158eb7932SJeff Layton 	if (unlikely(eb > osdc->epoch_barrier)) {
229258eb7932SJeff Layton 		up_read(&osdc->lock);
229358eb7932SJeff Layton 		down_write(&osdc->lock);
229458eb7932SJeff Layton 		update_epoch_barrier(osdc, eb);
229558eb7932SJeff Layton 		up_write(&osdc->lock);
229658eb7932SJeff Layton 	} else {
229758eb7932SJeff Layton 		up_read(&osdc->lock);
229858eb7932SJeff Layton 	}
229958eb7932SJeff Layton }
230058eb7932SJeff Layton EXPORT_SYMBOL(ceph_osdc_update_epoch_barrier);
230158eb7932SJeff Layton 
2302fc36d0a4SJeff Layton /*
2303fc36d0a4SJeff Layton  * Drop all pending requests that are stalled waiting on a full condition to
230458eb7932SJeff Layton  * clear, and complete them with ENOSPC as the return code. Set the
230558eb7932SJeff Layton  * osdc->epoch_barrier to the latest map epoch that we've seen if any were
230658eb7932SJeff Layton  * cancelled.
2307fc36d0a4SJeff Layton  */
2308fc36d0a4SJeff Layton static void ceph_osdc_abort_on_full(struct ceph_osd_client *osdc)
2309fc36d0a4SJeff Layton {
2310fc36d0a4SJeff Layton 	struct rb_node *n;
231158eb7932SJeff Layton 	bool victims = false;
2312fc36d0a4SJeff Layton 
2313fc36d0a4SJeff Layton 	dout("enter abort_on_full\n");
2314fc36d0a4SJeff Layton 
2315fc36d0a4SJeff Layton 	if (!ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) && !have_pool_full(osdc))
2316fc36d0a4SJeff Layton 		goto out;
2317fc36d0a4SJeff Layton 
231858eb7932SJeff Layton 	/* Scan list and see if there is anything to abort */
231958eb7932SJeff Layton 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
232058eb7932SJeff Layton 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
232158eb7932SJeff Layton 		struct rb_node *m;
232258eb7932SJeff Layton 
232358eb7932SJeff Layton 		m = rb_first(&osd->o_requests);
232458eb7932SJeff Layton 		while (m) {
232558eb7932SJeff Layton 			struct ceph_osd_request *req = rb_entry(m,
232658eb7932SJeff Layton 					struct ceph_osd_request, r_node);
232758eb7932SJeff Layton 			m = rb_next(m);
232858eb7932SJeff Layton 
232958eb7932SJeff Layton 			if (req->r_abort_on_full) {
233058eb7932SJeff Layton 				victims = true;
233158eb7932SJeff Layton 				break;
233258eb7932SJeff Layton 			}
233358eb7932SJeff Layton 		}
233458eb7932SJeff Layton 		if (victims)
233558eb7932SJeff Layton 			break;
233658eb7932SJeff Layton 	}
233758eb7932SJeff Layton 
233858eb7932SJeff Layton 	if (!victims)
233958eb7932SJeff Layton 		goto out;
234058eb7932SJeff Layton 
234158eb7932SJeff Layton 	/*
234258eb7932SJeff Layton 	 * Update the barrier to current epoch if it's behind that point,
234358eb7932SJeff Layton 	 * since we know we have some calls to be aborted in the tree.
234458eb7932SJeff Layton 	 */
234558eb7932SJeff Layton 	update_epoch_barrier(osdc, osdc->osdmap->epoch);
234658eb7932SJeff Layton 
2347fc36d0a4SJeff Layton 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
2348fc36d0a4SJeff Layton 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
2349fc36d0a4SJeff Layton 		struct rb_node *m;
2350fc36d0a4SJeff Layton 
2351fc36d0a4SJeff Layton 		m = rb_first(&osd->o_requests);
2352fc36d0a4SJeff Layton 		while (m) {
2353fc36d0a4SJeff Layton 			struct ceph_osd_request *req = rb_entry(m,
2354fc36d0a4SJeff Layton 					struct ceph_osd_request, r_node);
2355fc36d0a4SJeff Layton 			m = rb_next(m);
2356fc36d0a4SJeff Layton 
2357fc36d0a4SJeff Layton 			if (req->r_abort_on_full &&
2358fc36d0a4SJeff Layton 			    (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2359fc36d0a4SJeff Layton 			     pool_full(osdc, req->r_t.target_oloc.pool)))
2360fc36d0a4SJeff Layton 				abort_request(req, -ENOSPC);
2361fc36d0a4SJeff Layton 		}
2362fc36d0a4SJeff Layton 	}
2363fc36d0a4SJeff Layton out:
236458eb7932SJeff Layton 	dout("return abort_on_full barrier=%u\n", osdc->epoch_barrier);
2365fc36d0a4SJeff Layton }
2366fc36d0a4SJeff Layton 
23674609245eSIlya Dryomov static void check_pool_dne(struct ceph_osd_request *req)
23684609245eSIlya Dryomov {
23694609245eSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
23704609245eSIlya Dryomov 	struct ceph_osdmap *map = osdc->osdmap;
23714609245eSIlya Dryomov 
23724609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
23734609245eSIlya Dryomov 	WARN_ON(!map->epoch);
23744609245eSIlya Dryomov 
23754609245eSIlya Dryomov 	if (req->r_attempts) {
23764609245eSIlya Dryomov 		/*
23774609245eSIlya Dryomov 		 * We sent a request earlier, which means that
23784609245eSIlya Dryomov 		 * previously the pool existed, and now it does not
23794609245eSIlya Dryomov 		 * (i.e., it was deleted).
23804609245eSIlya Dryomov 		 */
23814609245eSIlya Dryomov 		req->r_map_dne_bound = map->epoch;
23824609245eSIlya Dryomov 		dout("%s req %p tid %llu pool disappeared\n", __func__, req,
23834609245eSIlya Dryomov 		     req->r_tid);
23844609245eSIlya Dryomov 	} else {
23854609245eSIlya Dryomov 		dout("%s req %p tid %llu map_dne_bound %u have %u\n", __func__,
23864609245eSIlya Dryomov 		     req, req->r_tid, req->r_map_dne_bound, map->epoch);
23874609245eSIlya Dryomov 	}
23884609245eSIlya Dryomov 
23894609245eSIlya Dryomov 	if (req->r_map_dne_bound) {
23904609245eSIlya Dryomov 		if (map->epoch >= req->r_map_dne_bound) {
23914609245eSIlya Dryomov 			/* we had a new enough map */
23924609245eSIlya Dryomov 			pr_info_ratelimited("tid %llu pool does not exist\n",
23934609245eSIlya Dryomov 					    req->r_tid);
23944609245eSIlya Dryomov 			complete_request(req, -ENOENT);
23954609245eSIlya Dryomov 		}
23964609245eSIlya Dryomov 	} else {
23974609245eSIlya Dryomov 		send_map_check(req);
23984609245eSIlya Dryomov 	}
23994609245eSIlya Dryomov }
24004609245eSIlya Dryomov 
24014609245eSIlya Dryomov static void map_check_cb(struct ceph_mon_generic_request *greq)
24024609245eSIlya Dryomov {
24034609245eSIlya Dryomov 	struct ceph_osd_client *osdc = &greq->monc->client->osdc;
24044609245eSIlya Dryomov 	struct ceph_osd_request *req;
24054609245eSIlya Dryomov 	u64 tid = greq->private_data;
24064609245eSIlya Dryomov 
24074609245eSIlya Dryomov 	WARN_ON(greq->result || !greq->u.newest);
24084609245eSIlya Dryomov 
24094609245eSIlya Dryomov 	down_write(&osdc->lock);
24104609245eSIlya Dryomov 	req = lookup_request_mc(&osdc->map_checks, tid);
24114609245eSIlya Dryomov 	if (!req) {
24124609245eSIlya Dryomov 		dout("%s tid %llu dne\n", __func__, tid);
24134609245eSIlya Dryomov 		goto out_unlock;
24144609245eSIlya Dryomov 	}
24154609245eSIlya Dryomov 
24164609245eSIlya Dryomov 	dout("%s req %p tid %llu map_dne_bound %u newest %llu\n", __func__,
24174609245eSIlya Dryomov 	     req, req->r_tid, req->r_map_dne_bound, greq->u.newest);
24184609245eSIlya Dryomov 	if (!req->r_map_dne_bound)
24194609245eSIlya Dryomov 		req->r_map_dne_bound = greq->u.newest;
24204609245eSIlya Dryomov 	erase_request_mc(&osdc->map_checks, req);
24214609245eSIlya Dryomov 	check_pool_dne(req);
24224609245eSIlya Dryomov 
24234609245eSIlya Dryomov 	ceph_osdc_put_request(req);
24244609245eSIlya Dryomov out_unlock:
24254609245eSIlya Dryomov 	up_write(&osdc->lock);
24264609245eSIlya Dryomov }
24274609245eSIlya Dryomov 
24284609245eSIlya Dryomov static void send_map_check(struct ceph_osd_request *req)
24294609245eSIlya Dryomov {
24304609245eSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
24314609245eSIlya Dryomov 	struct ceph_osd_request *lookup_req;
24324609245eSIlya Dryomov 	int ret;
24334609245eSIlya Dryomov 
24344609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
24354609245eSIlya Dryomov 
24364609245eSIlya Dryomov 	lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
24374609245eSIlya Dryomov 	if (lookup_req) {
24384609245eSIlya Dryomov 		WARN_ON(lookup_req != req);
24394609245eSIlya Dryomov 		return;
24404609245eSIlya Dryomov 	}
24414609245eSIlya Dryomov 
24424609245eSIlya Dryomov 	ceph_osdc_get_request(req);
24434609245eSIlya Dryomov 	insert_request_mc(&osdc->map_checks, req);
24444609245eSIlya Dryomov 	ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
24454609245eSIlya Dryomov 					  map_check_cb, req->r_tid);
24464609245eSIlya Dryomov 	WARN_ON(ret);
24474609245eSIlya Dryomov }
24484609245eSIlya Dryomov 
24490bbfdfe8SIlya Dryomov /*
2450922dab61SIlya Dryomov  * lingering requests, watch/notify v2 infrastructure
2451922dab61SIlya Dryomov  */
2452922dab61SIlya Dryomov static void linger_release(struct kref *kref)
2453922dab61SIlya Dryomov {
2454922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq =
2455922dab61SIlya Dryomov 	    container_of(kref, struct ceph_osd_linger_request, kref);
2456922dab61SIlya Dryomov 
2457922dab61SIlya Dryomov 	dout("%s lreq %p reg_req %p ping_req %p\n", __func__, lreq,
2458922dab61SIlya Dryomov 	     lreq->reg_req, lreq->ping_req);
2459922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&lreq->node));
2460922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&lreq->osdc_node));
24614609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&lreq->mc_node));
2462922dab61SIlya Dryomov 	WARN_ON(!list_empty(&lreq->scan_item));
2463b07d3c4bSIlya Dryomov 	WARN_ON(!list_empty(&lreq->pending_lworks));
2464922dab61SIlya Dryomov 	WARN_ON(lreq->osd);
2465922dab61SIlya Dryomov 
2466922dab61SIlya Dryomov 	if (lreq->reg_req)
2467922dab61SIlya Dryomov 		ceph_osdc_put_request(lreq->reg_req);
2468922dab61SIlya Dryomov 	if (lreq->ping_req)
2469922dab61SIlya Dryomov 		ceph_osdc_put_request(lreq->ping_req);
2470922dab61SIlya Dryomov 	target_destroy(&lreq->t);
2471922dab61SIlya Dryomov 	kfree(lreq);
2472922dab61SIlya Dryomov }
2473922dab61SIlya Dryomov 
2474922dab61SIlya Dryomov static void linger_put(struct ceph_osd_linger_request *lreq)
2475922dab61SIlya Dryomov {
2476922dab61SIlya Dryomov 	if (lreq)
2477922dab61SIlya Dryomov 		kref_put(&lreq->kref, linger_release);
2478922dab61SIlya Dryomov }
2479922dab61SIlya Dryomov 
2480922dab61SIlya Dryomov static struct ceph_osd_linger_request *
2481922dab61SIlya Dryomov linger_get(struct ceph_osd_linger_request *lreq)
2482922dab61SIlya Dryomov {
2483922dab61SIlya Dryomov 	kref_get(&lreq->kref);
2484922dab61SIlya Dryomov 	return lreq;
2485922dab61SIlya Dryomov }
2486922dab61SIlya Dryomov 
2487922dab61SIlya Dryomov static struct ceph_osd_linger_request *
2488922dab61SIlya Dryomov linger_alloc(struct ceph_osd_client *osdc)
2489922dab61SIlya Dryomov {
2490922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
2491922dab61SIlya Dryomov 
2492922dab61SIlya Dryomov 	lreq = kzalloc(sizeof(*lreq), GFP_NOIO);
2493922dab61SIlya Dryomov 	if (!lreq)
2494922dab61SIlya Dryomov 		return NULL;
2495922dab61SIlya Dryomov 
2496922dab61SIlya Dryomov 	kref_init(&lreq->kref);
2497922dab61SIlya Dryomov 	mutex_init(&lreq->lock);
2498922dab61SIlya Dryomov 	RB_CLEAR_NODE(&lreq->node);
2499922dab61SIlya Dryomov 	RB_CLEAR_NODE(&lreq->osdc_node);
25004609245eSIlya Dryomov 	RB_CLEAR_NODE(&lreq->mc_node);
2501922dab61SIlya Dryomov 	INIT_LIST_HEAD(&lreq->scan_item);
2502b07d3c4bSIlya Dryomov 	INIT_LIST_HEAD(&lreq->pending_lworks);
2503922dab61SIlya Dryomov 	init_completion(&lreq->reg_commit_wait);
250419079203SIlya Dryomov 	init_completion(&lreq->notify_finish_wait);
2505922dab61SIlya Dryomov 
2506922dab61SIlya Dryomov 	lreq->osdc = osdc;
2507922dab61SIlya Dryomov 	target_init(&lreq->t);
2508922dab61SIlya Dryomov 
2509922dab61SIlya Dryomov 	dout("%s lreq %p\n", __func__, lreq);
2510922dab61SIlya Dryomov 	return lreq;
2511922dab61SIlya Dryomov }
2512922dab61SIlya Dryomov 
2513922dab61SIlya Dryomov DEFINE_RB_INSDEL_FUNCS(linger, struct ceph_osd_linger_request, linger_id, node)
2514922dab61SIlya Dryomov DEFINE_RB_FUNCS(linger_osdc, struct ceph_osd_linger_request, linger_id, osdc_node)
25154609245eSIlya Dryomov DEFINE_RB_FUNCS(linger_mc, struct ceph_osd_linger_request, linger_id, mc_node)
2516922dab61SIlya Dryomov 
2517922dab61SIlya Dryomov /*
2518922dab61SIlya Dryomov  * Create linger request <-> OSD session relation.
2519922dab61SIlya Dryomov  *
2520922dab61SIlya Dryomov  * @lreq has to be registered, @osd may be homeless.
2521922dab61SIlya Dryomov  */
2522922dab61SIlya Dryomov static void link_linger(struct ceph_osd *osd,
2523922dab61SIlya Dryomov 			struct ceph_osd_linger_request *lreq)
2524922dab61SIlya Dryomov {
2525922dab61SIlya Dryomov 	verify_osd_locked(osd);
2526922dab61SIlya Dryomov 	WARN_ON(!lreq->linger_id || lreq->osd);
2527922dab61SIlya Dryomov 	dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2528922dab61SIlya Dryomov 	     osd->o_osd, lreq, lreq->linger_id);
2529922dab61SIlya Dryomov 
2530922dab61SIlya Dryomov 	if (!osd_homeless(osd))
2531922dab61SIlya Dryomov 		__remove_osd_from_lru(osd);
2532922dab61SIlya Dryomov 	else
2533922dab61SIlya Dryomov 		atomic_inc(&osd->o_osdc->num_homeless);
2534922dab61SIlya Dryomov 
2535922dab61SIlya Dryomov 	get_osd(osd);
2536922dab61SIlya Dryomov 	insert_linger(&osd->o_linger_requests, lreq);
2537922dab61SIlya Dryomov 	lreq->osd = osd;
2538922dab61SIlya Dryomov }
2539922dab61SIlya Dryomov 
2540922dab61SIlya Dryomov static void unlink_linger(struct ceph_osd *osd,
2541922dab61SIlya Dryomov 			  struct ceph_osd_linger_request *lreq)
2542922dab61SIlya Dryomov {
2543922dab61SIlya Dryomov 	verify_osd_locked(osd);
2544922dab61SIlya Dryomov 	WARN_ON(lreq->osd != osd);
2545922dab61SIlya Dryomov 	dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2546922dab61SIlya Dryomov 	     osd->o_osd, lreq, lreq->linger_id);
2547922dab61SIlya Dryomov 
2548922dab61SIlya Dryomov 	lreq->osd = NULL;
2549922dab61SIlya Dryomov 	erase_linger(&osd->o_linger_requests, lreq);
2550922dab61SIlya Dryomov 	put_osd(osd);
2551922dab61SIlya Dryomov 
2552922dab61SIlya Dryomov 	if (!osd_homeless(osd))
2553922dab61SIlya Dryomov 		maybe_move_osd_to_lru(osd);
2554922dab61SIlya Dryomov 	else
2555922dab61SIlya Dryomov 		atomic_dec(&osd->o_osdc->num_homeless);
2556922dab61SIlya Dryomov }
2557922dab61SIlya Dryomov 
2558922dab61SIlya Dryomov static bool __linger_registered(struct ceph_osd_linger_request *lreq)
2559922dab61SIlya Dryomov {
2560922dab61SIlya Dryomov 	verify_osdc_locked(lreq->osdc);
2561922dab61SIlya Dryomov 
2562922dab61SIlya Dryomov 	return !RB_EMPTY_NODE(&lreq->osdc_node);
2563922dab61SIlya Dryomov }
2564922dab61SIlya Dryomov 
2565922dab61SIlya Dryomov static bool linger_registered(struct ceph_osd_linger_request *lreq)
2566922dab61SIlya Dryomov {
2567922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2568922dab61SIlya Dryomov 	bool registered;
2569922dab61SIlya Dryomov 
2570922dab61SIlya Dryomov 	down_read(&osdc->lock);
2571922dab61SIlya Dryomov 	registered = __linger_registered(lreq);
2572922dab61SIlya Dryomov 	up_read(&osdc->lock);
2573922dab61SIlya Dryomov 
2574922dab61SIlya Dryomov 	return registered;
2575922dab61SIlya Dryomov }
2576922dab61SIlya Dryomov 
2577922dab61SIlya Dryomov static void linger_register(struct ceph_osd_linger_request *lreq)
2578922dab61SIlya Dryomov {
2579922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2580922dab61SIlya Dryomov 
2581922dab61SIlya Dryomov 	verify_osdc_wrlocked(osdc);
2582922dab61SIlya Dryomov 	WARN_ON(lreq->linger_id);
2583922dab61SIlya Dryomov 
2584922dab61SIlya Dryomov 	linger_get(lreq);
2585922dab61SIlya Dryomov 	lreq->linger_id = ++osdc->last_linger_id;
2586922dab61SIlya Dryomov 	insert_linger_osdc(&osdc->linger_requests, lreq);
2587922dab61SIlya Dryomov }
2588922dab61SIlya Dryomov 
2589922dab61SIlya Dryomov static void linger_unregister(struct ceph_osd_linger_request *lreq)
2590922dab61SIlya Dryomov {
2591922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2592922dab61SIlya Dryomov 
2593922dab61SIlya Dryomov 	verify_osdc_wrlocked(osdc);
2594922dab61SIlya Dryomov 
2595922dab61SIlya Dryomov 	erase_linger_osdc(&osdc->linger_requests, lreq);
2596922dab61SIlya Dryomov 	linger_put(lreq);
2597922dab61SIlya Dryomov }
2598922dab61SIlya Dryomov 
2599922dab61SIlya Dryomov static void cancel_linger_request(struct ceph_osd_request *req)
2600922dab61SIlya Dryomov {
2601922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
2602922dab61SIlya Dryomov 
2603922dab61SIlya Dryomov 	WARN_ON(!req->r_linger);
2604922dab61SIlya Dryomov 	cancel_request(req);
2605922dab61SIlya Dryomov 	linger_put(lreq);
2606922dab61SIlya Dryomov }
2607922dab61SIlya Dryomov 
2608922dab61SIlya Dryomov struct linger_work {
2609922dab61SIlya Dryomov 	struct work_struct work;
2610922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
2611b07d3c4bSIlya Dryomov 	struct list_head pending_item;
2612b07d3c4bSIlya Dryomov 	unsigned long queued_stamp;
2613922dab61SIlya Dryomov 
2614922dab61SIlya Dryomov 	union {
2615922dab61SIlya Dryomov 		struct {
2616922dab61SIlya Dryomov 			u64 notify_id;
2617922dab61SIlya Dryomov 			u64 notifier_id;
2618922dab61SIlya Dryomov 			void *payload; /* points into @msg front */
2619922dab61SIlya Dryomov 			size_t payload_len;
2620922dab61SIlya Dryomov 
2621922dab61SIlya Dryomov 			struct ceph_msg *msg; /* for ceph_msg_put() */
2622922dab61SIlya Dryomov 		} notify;
2623922dab61SIlya Dryomov 		struct {
2624922dab61SIlya Dryomov 			int err;
2625922dab61SIlya Dryomov 		} error;
2626922dab61SIlya Dryomov 	};
2627922dab61SIlya Dryomov };
2628922dab61SIlya Dryomov 
2629922dab61SIlya Dryomov static struct linger_work *lwork_alloc(struct ceph_osd_linger_request *lreq,
2630922dab61SIlya Dryomov 				       work_func_t workfn)
2631922dab61SIlya Dryomov {
2632922dab61SIlya Dryomov 	struct linger_work *lwork;
2633922dab61SIlya Dryomov 
2634922dab61SIlya Dryomov 	lwork = kzalloc(sizeof(*lwork), GFP_NOIO);
2635922dab61SIlya Dryomov 	if (!lwork)
2636922dab61SIlya Dryomov 		return NULL;
2637922dab61SIlya Dryomov 
2638922dab61SIlya Dryomov 	INIT_WORK(&lwork->work, workfn);
2639b07d3c4bSIlya Dryomov 	INIT_LIST_HEAD(&lwork->pending_item);
2640922dab61SIlya Dryomov 	lwork->lreq = linger_get(lreq);
2641922dab61SIlya Dryomov 
2642922dab61SIlya Dryomov 	return lwork;
2643922dab61SIlya Dryomov }
2644922dab61SIlya Dryomov 
2645922dab61SIlya Dryomov static void lwork_free(struct linger_work *lwork)
2646922dab61SIlya Dryomov {
2647922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2648922dab61SIlya Dryomov 
2649b07d3c4bSIlya Dryomov 	mutex_lock(&lreq->lock);
2650b07d3c4bSIlya Dryomov 	list_del(&lwork->pending_item);
2651b07d3c4bSIlya Dryomov 	mutex_unlock(&lreq->lock);
2652b07d3c4bSIlya Dryomov 
2653922dab61SIlya Dryomov 	linger_put(lreq);
2654922dab61SIlya Dryomov 	kfree(lwork);
2655922dab61SIlya Dryomov }
2656922dab61SIlya Dryomov 
2657922dab61SIlya Dryomov static void lwork_queue(struct linger_work *lwork)
2658922dab61SIlya Dryomov {
2659922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2660922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2661922dab61SIlya Dryomov 
2662922dab61SIlya Dryomov 	verify_lreq_locked(lreq);
2663b07d3c4bSIlya Dryomov 	WARN_ON(!list_empty(&lwork->pending_item));
2664b07d3c4bSIlya Dryomov 
2665b07d3c4bSIlya Dryomov 	lwork->queued_stamp = jiffies;
2666b07d3c4bSIlya Dryomov 	list_add_tail(&lwork->pending_item, &lreq->pending_lworks);
2667922dab61SIlya Dryomov 	queue_work(osdc->notify_wq, &lwork->work);
2668922dab61SIlya Dryomov }
2669922dab61SIlya Dryomov 
2670922dab61SIlya Dryomov static void do_watch_notify(struct work_struct *w)
2671922dab61SIlya Dryomov {
2672922dab61SIlya Dryomov 	struct linger_work *lwork = container_of(w, struct linger_work, work);
2673922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2674922dab61SIlya Dryomov 
2675922dab61SIlya Dryomov 	if (!linger_registered(lreq)) {
2676922dab61SIlya Dryomov 		dout("%s lreq %p not registered\n", __func__, lreq);
2677922dab61SIlya Dryomov 		goto out;
2678922dab61SIlya Dryomov 	}
2679922dab61SIlya Dryomov 
268019079203SIlya Dryomov 	WARN_ON(!lreq->is_watch);
2681922dab61SIlya Dryomov 	dout("%s lreq %p notify_id %llu notifier_id %llu payload_len %zu\n",
2682922dab61SIlya Dryomov 	     __func__, lreq, lwork->notify.notify_id, lwork->notify.notifier_id,
2683922dab61SIlya Dryomov 	     lwork->notify.payload_len);
2684922dab61SIlya Dryomov 	lreq->wcb(lreq->data, lwork->notify.notify_id, lreq->linger_id,
2685922dab61SIlya Dryomov 		  lwork->notify.notifier_id, lwork->notify.payload,
2686922dab61SIlya Dryomov 		  lwork->notify.payload_len);
2687922dab61SIlya Dryomov 
2688922dab61SIlya Dryomov out:
2689922dab61SIlya Dryomov 	ceph_msg_put(lwork->notify.msg);
2690922dab61SIlya Dryomov 	lwork_free(lwork);
2691922dab61SIlya Dryomov }
2692922dab61SIlya Dryomov 
2693922dab61SIlya Dryomov static void do_watch_error(struct work_struct *w)
2694922dab61SIlya Dryomov {
2695922dab61SIlya Dryomov 	struct linger_work *lwork = container_of(w, struct linger_work, work);
2696922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2697922dab61SIlya Dryomov 
2698922dab61SIlya Dryomov 	if (!linger_registered(lreq)) {
2699922dab61SIlya Dryomov 		dout("%s lreq %p not registered\n", __func__, lreq);
2700922dab61SIlya Dryomov 		goto out;
2701922dab61SIlya Dryomov 	}
2702922dab61SIlya Dryomov 
2703922dab61SIlya Dryomov 	dout("%s lreq %p err %d\n", __func__, lreq, lwork->error.err);
2704922dab61SIlya Dryomov 	lreq->errcb(lreq->data, lreq->linger_id, lwork->error.err);
2705922dab61SIlya Dryomov 
2706922dab61SIlya Dryomov out:
2707922dab61SIlya Dryomov 	lwork_free(lwork);
2708922dab61SIlya Dryomov }
2709922dab61SIlya Dryomov 
2710922dab61SIlya Dryomov static void queue_watch_error(struct ceph_osd_linger_request *lreq)
2711922dab61SIlya Dryomov {
2712922dab61SIlya Dryomov 	struct linger_work *lwork;
2713922dab61SIlya Dryomov 
2714922dab61SIlya Dryomov 	lwork = lwork_alloc(lreq, do_watch_error);
2715922dab61SIlya Dryomov 	if (!lwork) {
2716922dab61SIlya Dryomov 		pr_err("failed to allocate error-lwork\n");
2717922dab61SIlya Dryomov 		return;
2718922dab61SIlya Dryomov 	}
2719922dab61SIlya Dryomov 
2720922dab61SIlya Dryomov 	lwork->error.err = lreq->last_error;
2721922dab61SIlya Dryomov 	lwork_queue(lwork);
2722922dab61SIlya Dryomov }
2723922dab61SIlya Dryomov 
2724922dab61SIlya Dryomov static void linger_reg_commit_complete(struct ceph_osd_linger_request *lreq,
2725922dab61SIlya Dryomov 				       int result)
2726922dab61SIlya Dryomov {
2727922dab61SIlya Dryomov 	if (!completion_done(&lreq->reg_commit_wait)) {
2728922dab61SIlya Dryomov 		lreq->reg_commit_error = (result <= 0 ? result : 0);
2729922dab61SIlya Dryomov 		complete_all(&lreq->reg_commit_wait);
2730922dab61SIlya Dryomov 	}
2731922dab61SIlya Dryomov }
2732922dab61SIlya Dryomov 
2733922dab61SIlya Dryomov static void linger_commit_cb(struct ceph_osd_request *req)
2734922dab61SIlya Dryomov {
2735922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
2736922dab61SIlya Dryomov 
2737922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
2738922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu result %d\n", __func__, lreq,
2739922dab61SIlya Dryomov 	     lreq->linger_id, req->r_result);
2740922dab61SIlya Dryomov 	linger_reg_commit_complete(lreq, req->r_result);
2741922dab61SIlya Dryomov 	lreq->committed = true;
2742922dab61SIlya Dryomov 
274319079203SIlya Dryomov 	if (!lreq->is_watch) {
274419079203SIlya Dryomov 		struct ceph_osd_data *osd_data =
274519079203SIlya Dryomov 		    osd_req_op_data(req, 0, notify, response_data);
274619079203SIlya Dryomov 		void *p = page_address(osd_data->pages[0]);
274719079203SIlya Dryomov 
274819079203SIlya Dryomov 		WARN_ON(req->r_ops[0].op != CEPH_OSD_OP_NOTIFY ||
274919079203SIlya Dryomov 			osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
275019079203SIlya Dryomov 
275119079203SIlya Dryomov 		/* make note of the notify_id */
275219079203SIlya Dryomov 		if (req->r_ops[0].outdata_len >= sizeof(u64)) {
275319079203SIlya Dryomov 			lreq->notify_id = ceph_decode_64(&p);
275419079203SIlya Dryomov 			dout("lreq %p notify_id %llu\n", lreq,
275519079203SIlya Dryomov 			     lreq->notify_id);
275619079203SIlya Dryomov 		} else {
275719079203SIlya Dryomov 			dout("lreq %p no notify_id\n", lreq);
275819079203SIlya Dryomov 		}
275919079203SIlya Dryomov 	}
276019079203SIlya Dryomov 
2761922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
2762922dab61SIlya Dryomov 	linger_put(lreq);
2763922dab61SIlya Dryomov }
2764922dab61SIlya Dryomov 
2765922dab61SIlya Dryomov static int normalize_watch_error(int err)
2766922dab61SIlya Dryomov {
2767922dab61SIlya Dryomov 	/*
2768922dab61SIlya Dryomov 	 * Translate ENOENT -> ENOTCONN so that a delete->disconnection
2769922dab61SIlya Dryomov 	 * notification and a failure to reconnect because we raced with
2770922dab61SIlya Dryomov 	 * the delete appear the same to the user.
2771922dab61SIlya Dryomov 	 */
2772922dab61SIlya Dryomov 	if (err == -ENOENT)
2773922dab61SIlya Dryomov 		err = -ENOTCONN;
2774922dab61SIlya Dryomov 
2775922dab61SIlya Dryomov 	return err;
2776922dab61SIlya Dryomov }
2777922dab61SIlya Dryomov 
2778922dab61SIlya Dryomov static void linger_reconnect_cb(struct ceph_osd_request *req)
2779922dab61SIlya Dryomov {
2780922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
2781922dab61SIlya Dryomov 
2782922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
2783922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu result %d last_error %d\n", __func__,
2784922dab61SIlya Dryomov 	     lreq, lreq->linger_id, req->r_result, lreq->last_error);
2785922dab61SIlya Dryomov 	if (req->r_result < 0) {
2786922dab61SIlya Dryomov 		if (!lreq->last_error) {
2787922dab61SIlya Dryomov 			lreq->last_error = normalize_watch_error(req->r_result);
2788922dab61SIlya Dryomov 			queue_watch_error(lreq);
2789922dab61SIlya Dryomov 		}
2790922dab61SIlya Dryomov 	}
2791922dab61SIlya Dryomov 
2792922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
2793922dab61SIlya Dryomov 	linger_put(lreq);
2794922dab61SIlya Dryomov }
2795922dab61SIlya Dryomov 
2796922dab61SIlya Dryomov static void send_linger(struct ceph_osd_linger_request *lreq)
2797922dab61SIlya Dryomov {
2798922dab61SIlya Dryomov 	struct ceph_osd_request *req = lreq->reg_req;
2799922dab61SIlya Dryomov 	struct ceph_osd_req_op *op = &req->r_ops[0];
2800922dab61SIlya Dryomov 
2801922dab61SIlya Dryomov 	verify_osdc_wrlocked(req->r_osdc);
2802922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2803922dab61SIlya Dryomov 
2804922dab61SIlya Dryomov 	if (req->r_osd)
2805922dab61SIlya Dryomov 		cancel_linger_request(req);
2806922dab61SIlya Dryomov 
2807922dab61SIlya Dryomov 	request_reinit(req);
2808922dab61SIlya Dryomov 	ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
2809922dab61SIlya Dryomov 	ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
2810922dab61SIlya Dryomov 	req->r_flags = lreq->t.flags;
2811922dab61SIlya Dryomov 	req->r_mtime = lreq->mtime;
2812922dab61SIlya Dryomov 
2813922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
281419079203SIlya Dryomov 	if (lreq->is_watch && lreq->committed) {
2815922dab61SIlya Dryomov 		WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2816922dab61SIlya Dryomov 			op->watch.cookie != lreq->linger_id);
2817922dab61SIlya Dryomov 		op->watch.op = CEPH_OSD_WATCH_OP_RECONNECT;
2818922dab61SIlya Dryomov 		op->watch.gen = ++lreq->register_gen;
2819922dab61SIlya Dryomov 		dout("lreq %p reconnect register_gen %u\n", lreq,
2820922dab61SIlya Dryomov 		     op->watch.gen);
2821922dab61SIlya Dryomov 		req->r_callback = linger_reconnect_cb;
2822922dab61SIlya Dryomov 	} else {
282319079203SIlya Dryomov 		if (!lreq->is_watch)
282419079203SIlya Dryomov 			lreq->notify_id = 0;
282519079203SIlya Dryomov 		else
2826922dab61SIlya Dryomov 			WARN_ON(op->watch.op != CEPH_OSD_WATCH_OP_WATCH);
2827922dab61SIlya Dryomov 		dout("lreq %p register\n", lreq);
2828922dab61SIlya Dryomov 		req->r_callback = linger_commit_cb;
2829922dab61SIlya Dryomov 	}
2830922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
2831922dab61SIlya Dryomov 
2832922dab61SIlya Dryomov 	req->r_priv = linger_get(lreq);
2833922dab61SIlya Dryomov 	req->r_linger = true;
2834922dab61SIlya Dryomov 
2835922dab61SIlya Dryomov 	submit_request(req, true);
2836922dab61SIlya Dryomov }
2837922dab61SIlya Dryomov 
2838922dab61SIlya Dryomov static void linger_ping_cb(struct ceph_osd_request *req)
2839922dab61SIlya Dryomov {
2840922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
2841922dab61SIlya Dryomov 
2842922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
2843922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu result %d ping_sent %lu last_error %d\n",
2844922dab61SIlya Dryomov 	     __func__, lreq, lreq->linger_id, req->r_result, lreq->ping_sent,
2845922dab61SIlya Dryomov 	     lreq->last_error);
2846922dab61SIlya Dryomov 	if (lreq->register_gen == req->r_ops[0].watch.gen) {
2847b07d3c4bSIlya Dryomov 		if (!req->r_result) {
2848b07d3c4bSIlya Dryomov 			lreq->watch_valid_thru = lreq->ping_sent;
2849b07d3c4bSIlya Dryomov 		} else if (!lreq->last_error) {
2850922dab61SIlya Dryomov 			lreq->last_error = normalize_watch_error(req->r_result);
2851922dab61SIlya Dryomov 			queue_watch_error(lreq);
2852922dab61SIlya Dryomov 		}
2853922dab61SIlya Dryomov 	} else {
2854922dab61SIlya Dryomov 		dout("lreq %p register_gen %u ignoring old pong %u\n", lreq,
2855922dab61SIlya Dryomov 		     lreq->register_gen, req->r_ops[0].watch.gen);
2856922dab61SIlya Dryomov 	}
2857922dab61SIlya Dryomov 
2858922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
2859922dab61SIlya Dryomov 	linger_put(lreq);
2860922dab61SIlya Dryomov }
2861922dab61SIlya Dryomov 
2862922dab61SIlya Dryomov static void send_linger_ping(struct ceph_osd_linger_request *lreq)
2863922dab61SIlya Dryomov {
2864922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2865922dab61SIlya Dryomov 	struct ceph_osd_request *req = lreq->ping_req;
2866922dab61SIlya Dryomov 	struct ceph_osd_req_op *op = &req->r_ops[0];
2867922dab61SIlya Dryomov 
2868b7ec35b3SIlya Dryomov 	if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
2869922dab61SIlya Dryomov 		dout("%s PAUSERD\n", __func__);
2870922dab61SIlya Dryomov 		return;
2871922dab61SIlya Dryomov 	}
2872922dab61SIlya Dryomov 
2873922dab61SIlya Dryomov 	lreq->ping_sent = jiffies;
2874922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu ping_sent %lu register_gen %u\n",
2875922dab61SIlya Dryomov 	     __func__, lreq, lreq->linger_id, lreq->ping_sent,
2876922dab61SIlya Dryomov 	     lreq->register_gen);
2877922dab61SIlya Dryomov 
2878922dab61SIlya Dryomov 	if (req->r_osd)
2879922dab61SIlya Dryomov 		cancel_linger_request(req);
2880922dab61SIlya Dryomov 
2881922dab61SIlya Dryomov 	request_reinit(req);
2882922dab61SIlya Dryomov 	target_copy(&req->r_t, &lreq->t);
2883922dab61SIlya Dryomov 
2884922dab61SIlya Dryomov 	WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2885922dab61SIlya Dryomov 		op->watch.cookie != lreq->linger_id ||
2886922dab61SIlya Dryomov 		op->watch.op != CEPH_OSD_WATCH_OP_PING);
2887922dab61SIlya Dryomov 	op->watch.gen = lreq->register_gen;
2888922dab61SIlya Dryomov 	req->r_callback = linger_ping_cb;
2889922dab61SIlya Dryomov 	req->r_priv = linger_get(lreq);
2890922dab61SIlya Dryomov 	req->r_linger = true;
2891922dab61SIlya Dryomov 
2892922dab61SIlya Dryomov 	ceph_osdc_get_request(req);
2893922dab61SIlya Dryomov 	account_request(req);
2894922dab61SIlya Dryomov 	req->r_tid = atomic64_inc_return(&osdc->last_tid);
2895922dab61SIlya Dryomov 	link_request(lreq->osd, req);
2896922dab61SIlya Dryomov 	send_request(req);
2897922dab61SIlya Dryomov }
2898922dab61SIlya Dryomov 
2899922dab61SIlya Dryomov static void linger_submit(struct ceph_osd_linger_request *lreq)
2900922dab61SIlya Dryomov {
2901922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2902922dab61SIlya Dryomov 	struct ceph_osd *osd;
2903922dab61SIlya Dryomov 
29047de030d6SIlya Dryomov 	calc_target(osdc, &lreq->t, NULL, false);
2905922dab61SIlya Dryomov 	osd = lookup_create_osd(osdc, lreq->t.osd, true);
2906922dab61SIlya Dryomov 	link_linger(osd, lreq);
2907922dab61SIlya Dryomov 
2908922dab61SIlya Dryomov 	send_linger(lreq);
2909922dab61SIlya Dryomov }
2910922dab61SIlya Dryomov 
29114609245eSIlya Dryomov static void cancel_linger_map_check(struct ceph_osd_linger_request *lreq)
29124609245eSIlya Dryomov {
29134609245eSIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
29144609245eSIlya Dryomov 	struct ceph_osd_linger_request *lookup_lreq;
29154609245eSIlya Dryomov 
29164609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
29174609245eSIlya Dryomov 
29184609245eSIlya Dryomov 	lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
29194609245eSIlya Dryomov 				       lreq->linger_id);
29204609245eSIlya Dryomov 	if (!lookup_lreq)
29214609245eSIlya Dryomov 		return;
29224609245eSIlya Dryomov 
29234609245eSIlya Dryomov 	WARN_ON(lookup_lreq != lreq);
29244609245eSIlya Dryomov 	erase_linger_mc(&osdc->linger_map_checks, lreq);
29254609245eSIlya Dryomov 	linger_put(lreq);
29264609245eSIlya Dryomov }
29274609245eSIlya Dryomov 
2928922dab61SIlya Dryomov /*
2929922dab61SIlya Dryomov  * @lreq has to be both registered and linked.
2930922dab61SIlya Dryomov  */
2931922dab61SIlya Dryomov static void __linger_cancel(struct ceph_osd_linger_request *lreq)
2932922dab61SIlya Dryomov {
293319079203SIlya Dryomov 	if (lreq->is_watch && lreq->ping_req->r_osd)
2934922dab61SIlya Dryomov 		cancel_linger_request(lreq->ping_req);
2935922dab61SIlya Dryomov 	if (lreq->reg_req->r_osd)
2936922dab61SIlya Dryomov 		cancel_linger_request(lreq->reg_req);
29374609245eSIlya Dryomov 	cancel_linger_map_check(lreq);
2938922dab61SIlya Dryomov 	unlink_linger(lreq->osd, lreq);
2939922dab61SIlya Dryomov 	linger_unregister(lreq);
2940922dab61SIlya Dryomov }
2941922dab61SIlya Dryomov 
2942922dab61SIlya Dryomov static void linger_cancel(struct ceph_osd_linger_request *lreq)
2943922dab61SIlya Dryomov {
2944922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2945922dab61SIlya Dryomov 
2946922dab61SIlya Dryomov 	down_write(&osdc->lock);
2947922dab61SIlya Dryomov 	if (__linger_registered(lreq))
2948922dab61SIlya Dryomov 		__linger_cancel(lreq);
2949922dab61SIlya Dryomov 	up_write(&osdc->lock);
2950922dab61SIlya Dryomov }
2951922dab61SIlya Dryomov 
29524609245eSIlya Dryomov static void send_linger_map_check(struct ceph_osd_linger_request *lreq);
29534609245eSIlya Dryomov 
29544609245eSIlya Dryomov static void check_linger_pool_dne(struct ceph_osd_linger_request *lreq)
29554609245eSIlya Dryomov {
29564609245eSIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
29574609245eSIlya Dryomov 	struct ceph_osdmap *map = osdc->osdmap;
29584609245eSIlya Dryomov 
29594609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
29604609245eSIlya Dryomov 	WARN_ON(!map->epoch);
29614609245eSIlya Dryomov 
29624609245eSIlya Dryomov 	if (lreq->register_gen) {
29634609245eSIlya Dryomov 		lreq->map_dne_bound = map->epoch;
29644609245eSIlya Dryomov 		dout("%s lreq %p linger_id %llu pool disappeared\n", __func__,
29654609245eSIlya Dryomov 		     lreq, lreq->linger_id);
29664609245eSIlya Dryomov 	} else {
29674609245eSIlya Dryomov 		dout("%s lreq %p linger_id %llu map_dne_bound %u have %u\n",
29684609245eSIlya Dryomov 		     __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
29694609245eSIlya Dryomov 		     map->epoch);
29704609245eSIlya Dryomov 	}
29714609245eSIlya Dryomov 
29724609245eSIlya Dryomov 	if (lreq->map_dne_bound) {
29734609245eSIlya Dryomov 		if (map->epoch >= lreq->map_dne_bound) {
29744609245eSIlya Dryomov 			/* we had a new enough map */
29754609245eSIlya Dryomov 			pr_info("linger_id %llu pool does not exist\n",
29764609245eSIlya Dryomov 				lreq->linger_id);
29774609245eSIlya Dryomov 			linger_reg_commit_complete(lreq, -ENOENT);
29784609245eSIlya Dryomov 			__linger_cancel(lreq);
29794609245eSIlya Dryomov 		}
29804609245eSIlya Dryomov 	} else {
29814609245eSIlya Dryomov 		send_linger_map_check(lreq);
29824609245eSIlya Dryomov 	}
29834609245eSIlya Dryomov }
29844609245eSIlya Dryomov 
29854609245eSIlya Dryomov static void linger_map_check_cb(struct ceph_mon_generic_request *greq)
29864609245eSIlya Dryomov {
29874609245eSIlya Dryomov 	struct ceph_osd_client *osdc = &greq->monc->client->osdc;
29884609245eSIlya Dryomov 	struct ceph_osd_linger_request *lreq;
29894609245eSIlya Dryomov 	u64 linger_id = greq->private_data;
29904609245eSIlya Dryomov 
29914609245eSIlya Dryomov 	WARN_ON(greq->result || !greq->u.newest);
29924609245eSIlya Dryomov 
29934609245eSIlya Dryomov 	down_write(&osdc->lock);
29944609245eSIlya Dryomov 	lreq = lookup_linger_mc(&osdc->linger_map_checks, linger_id);
29954609245eSIlya Dryomov 	if (!lreq) {
29964609245eSIlya Dryomov 		dout("%s linger_id %llu dne\n", __func__, linger_id);
29974609245eSIlya Dryomov 		goto out_unlock;
29984609245eSIlya Dryomov 	}
29994609245eSIlya Dryomov 
30004609245eSIlya Dryomov 	dout("%s lreq %p linger_id %llu map_dne_bound %u newest %llu\n",
30014609245eSIlya Dryomov 	     __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
30024609245eSIlya Dryomov 	     greq->u.newest);
30034609245eSIlya Dryomov 	if (!lreq->map_dne_bound)
30044609245eSIlya Dryomov 		lreq->map_dne_bound = greq->u.newest;
30054609245eSIlya Dryomov 	erase_linger_mc(&osdc->linger_map_checks, lreq);
30064609245eSIlya Dryomov 	check_linger_pool_dne(lreq);
30074609245eSIlya Dryomov 
30084609245eSIlya Dryomov 	linger_put(lreq);
30094609245eSIlya Dryomov out_unlock:
30104609245eSIlya Dryomov 	up_write(&osdc->lock);
30114609245eSIlya Dryomov }
30124609245eSIlya Dryomov 
30134609245eSIlya Dryomov static void send_linger_map_check(struct ceph_osd_linger_request *lreq)
30144609245eSIlya Dryomov {
30154609245eSIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
30164609245eSIlya Dryomov 	struct ceph_osd_linger_request *lookup_lreq;
30174609245eSIlya Dryomov 	int ret;
30184609245eSIlya Dryomov 
30194609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
30204609245eSIlya Dryomov 
30214609245eSIlya Dryomov 	lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
30224609245eSIlya Dryomov 				       lreq->linger_id);
30234609245eSIlya Dryomov 	if (lookup_lreq) {
30244609245eSIlya Dryomov 		WARN_ON(lookup_lreq != lreq);
30254609245eSIlya Dryomov 		return;
30264609245eSIlya Dryomov 	}
30274609245eSIlya Dryomov 
30284609245eSIlya Dryomov 	linger_get(lreq);
30294609245eSIlya Dryomov 	insert_linger_mc(&osdc->linger_map_checks, lreq);
30304609245eSIlya Dryomov 	ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
30314609245eSIlya Dryomov 					  linger_map_check_cb, lreq->linger_id);
30324609245eSIlya Dryomov 	WARN_ON(ret);
30334609245eSIlya Dryomov }
30344609245eSIlya Dryomov 
3035922dab61SIlya Dryomov static int linger_reg_commit_wait(struct ceph_osd_linger_request *lreq)
3036922dab61SIlya Dryomov {
3037922dab61SIlya Dryomov 	int ret;
3038922dab61SIlya Dryomov 
3039922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3040922dab61SIlya Dryomov 	ret = wait_for_completion_interruptible(&lreq->reg_commit_wait);
3041922dab61SIlya Dryomov 	return ret ?: lreq->reg_commit_error;
3042922dab61SIlya Dryomov }
3043922dab61SIlya Dryomov 
304419079203SIlya Dryomov static int linger_notify_finish_wait(struct ceph_osd_linger_request *lreq)
304519079203SIlya Dryomov {
304619079203SIlya Dryomov 	int ret;
304719079203SIlya Dryomov 
304819079203SIlya Dryomov 	dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
304919079203SIlya Dryomov 	ret = wait_for_completion_interruptible(&lreq->notify_finish_wait);
305019079203SIlya Dryomov 	return ret ?: lreq->notify_finish_error;
305119079203SIlya Dryomov }
305219079203SIlya Dryomov 
3053922dab61SIlya Dryomov /*
3054fbca9635SIlya Dryomov  * Timeout callback, called every N seconds.  When 1 or more OSD
3055fbca9635SIlya Dryomov  * requests has been active for more than N seconds, we send a keepalive
3056fbca9635SIlya Dryomov  * (tag + timestamp) to its OSD to ensure any communications channel
3057fbca9635SIlya Dryomov  * reset is detected.
30583d14c5d2SYehuda Sadeh  */
30593d14c5d2SYehuda Sadeh static void handle_timeout(struct work_struct *work)
30603d14c5d2SYehuda Sadeh {
30613d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc =
30623d14c5d2SYehuda Sadeh 		container_of(work, struct ceph_osd_client, timeout_work.work);
3063a319bf56SIlya Dryomov 	struct ceph_options *opts = osdc->client->options;
30645aea3dcdSIlya Dryomov 	unsigned long cutoff = jiffies - opts->osd_keepalive_timeout;
30657cc5e38fSIlya Dryomov 	unsigned long expiry_cutoff = jiffies - opts->osd_request_timeout;
30665aea3dcdSIlya Dryomov 	LIST_HEAD(slow_osds);
30675aea3dcdSIlya Dryomov 	struct rb_node *n, *p;
30683d14c5d2SYehuda Sadeh 
30695aea3dcdSIlya Dryomov 	dout("%s osdc %p\n", __func__, osdc);
30705aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
30713d14c5d2SYehuda Sadeh 
30723d14c5d2SYehuda Sadeh 	/*
30733d14c5d2SYehuda Sadeh 	 * ping osds that are a bit slow.  this ensures that if there
30743d14c5d2SYehuda Sadeh 	 * is a break in the TCP connection we will notice, and reopen
30753d14c5d2SYehuda Sadeh 	 * a connection with that osd (from the fault callback).
30763d14c5d2SYehuda Sadeh 	 */
30775aea3dcdSIlya Dryomov 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
30785aea3dcdSIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
30795aea3dcdSIlya Dryomov 		bool found = false;
30803d14c5d2SYehuda Sadeh 
30817cc5e38fSIlya Dryomov 		for (p = rb_first(&osd->o_requests); p; ) {
30825aea3dcdSIlya Dryomov 			struct ceph_osd_request *req =
30835aea3dcdSIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
30845aea3dcdSIlya Dryomov 
30857cc5e38fSIlya Dryomov 			p = rb_next(p); /* abort_request() */
30867cc5e38fSIlya Dryomov 
30875aea3dcdSIlya Dryomov 			if (time_before(req->r_stamp, cutoff)) {
30885aea3dcdSIlya Dryomov 				dout(" req %p tid %llu on osd%d is laggy\n",
30895aea3dcdSIlya Dryomov 				     req, req->r_tid, osd->o_osd);
30905aea3dcdSIlya Dryomov 				found = true;
30915aea3dcdSIlya Dryomov 			}
30927cc5e38fSIlya Dryomov 			if (opts->osd_request_timeout &&
30937cc5e38fSIlya Dryomov 			    time_before(req->r_start_stamp, expiry_cutoff)) {
30947cc5e38fSIlya Dryomov 				pr_err_ratelimited("tid %llu on osd%d timeout\n",
30957cc5e38fSIlya Dryomov 				       req->r_tid, osd->o_osd);
30967cc5e38fSIlya Dryomov 				abort_request(req, -ETIMEDOUT);
30977cc5e38fSIlya Dryomov 			}
30985aea3dcdSIlya Dryomov 		}
3099922dab61SIlya Dryomov 		for (p = rb_first(&osd->o_linger_requests); p; p = rb_next(p)) {
3100922dab61SIlya Dryomov 			struct ceph_osd_linger_request *lreq =
3101922dab61SIlya Dryomov 			    rb_entry(p, struct ceph_osd_linger_request, node);
3102922dab61SIlya Dryomov 
3103922dab61SIlya Dryomov 			dout(" lreq %p linger_id %llu is served by osd%d\n",
3104922dab61SIlya Dryomov 			     lreq, lreq->linger_id, osd->o_osd);
3105922dab61SIlya Dryomov 			found = true;
3106922dab61SIlya Dryomov 
3107922dab61SIlya Dryomov 			mutex_lock(&lreq->lock);
310819079203SIlya Dryomov 			if (lreq->is_watch && lreq->committed && !lreq->last_error)
3109922dab61SIlya Dryomov 				send_linger_ping(lreq);
3110922dab61SIlya Dryomov 			mutex_unlock(&lreq->lock);
3111922dab61SIlya Dryomov 		}
31125aea3dcdSIlya Dryomov 
31135aea3dcdSIlya Dryomov 		if (found)
31143d14c5d2SYehuda Sadeh 			list_move_tail(&osd->o_keepalive_item, &slow_osds);
31153d14c5d2SYehuda Sadeh 	}
31165aea3dcdSIlya Dryomov 
31177cc5e38fSIlya Dryomov 	if (opts->osd_request_timeout) {
31187cc5e38fSIlya Dryomov 		for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
31197cc5e38fSIlya Dryomov 			struct ceph_osd_request *req =
31207cc5e38fSIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
31217cc5e38fSIlya Dryomov 
31227cc5e38fSIlya Dryomov 			p = rb_next(p); /* abort_request() */
31237cc5e38fSIlya Dryomov 
31247cc5e38fSIlya Dryomov 			if (time_before(req->r_start_stamp, expiry_cutoff)) {
31257cc5e38fSIlya Dryomov 				pr_err_ratelimited("tid %llu on osd%d timeout\n",
31267cc5e38fSIlya Dryomov 				       req->r_tid, osdc->homeless_osd.o_osd);
31277cc5e38fSIlya Dryomov 				abort_request(req, -ETIMEDOUT);
31287cc5e38fSIlya Dryomov 			}
31297cc5e38fSIlya Dryomov 		}
31307cc5e38fSIlya Dryomov 	}
31317cc5e38fSIlya Dryomov 
31325aea3dcdSIlya Dryomov 	if (atomic_read(&osdc->num_homeless) || !list_empty(&slow_osds))
31335aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
31345aea3dcdSIlya Dryomov 
31353d14c5d2SYehuda Sadeh 	while (!list_empty(&slow_osds)) {
31365aea3dcdSIlya Dryomov 		struct ceph_osd *osd = list_first_entry(&slow_osds,
31375aea3dcdSIlya Dryomov 							struct ceph_osd,
31383d14c5d2SYehuda Sadeh 							o_keepalive_item);
31393d14c5d2SYehuda Sadeh 		list_del_init(&osd->o_keepalive_item);
31403d14c5d2SYehuda Sadeh 		ceph_con_keepalive(&osd->o_con);
31413d14c5d2SYehuda Sadeh 	}
31423d14c5d2SYehuda Sadeh 
31435aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
3144fbca9635SIlya Dryomov 	schedule_delayed_work(&osdc->timeout_work,
3145fbca9635SIlya Dryomov 			      osdc->client->options->osd_keepalive_timeout);
31463d14c5d2SYehuda Sadeh }
31473d14c5d2SYehuda Sadeh 
31483d14c5d2SYehuda Sadeh static void handle_osds_timeout(struct work_struct *work)
31493d14c5d2SYehuda Sadeh {
31503d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc =
31513d14c5d2SYehuda Sadeh 		container_of(work, struct ceph_osd_client,
31523d14c5d2SYehuda Sadeh 			     osds_timeout_work.work);
3153a319bf56SIlya Dryomov 	unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
315442a2c09fSIlya Dryomov 	struct ceph_osd *osd, *nosd;
31553d14c5d2SYehuda Sadeh 
315642a2c09fSIlya Dryomov 	dout("%s osdc %p\n", __func__, osdc);
31575aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
315842a2c09fSIlya Dryomov 	list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
315942a2c09fSIlya Dryomov 		if (time_before(jiffies, osd->lru_ttl))
316042a2c09fSIlya Dryomov 			break;
316142a2c09fSIlya Dryomov 
31625aea3dcdSIlya Dryomov 		WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
3163922dab61SIlya Dryomov 		WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
31645aea3dcdSIlya Dryomov 		close_osd(osd);
316542a2c09fSIlya Dryomov 	}
316642a2c09fSIlya Dryomov 
31675aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
31683d14c5d2SYehuda Sadeh 	schedule_delayed_work(&osdc->osds_timeout_work,
31693d14c5d2SYehuda Sadeh 			      round_jiffies_relative(delay));
31703d14c5d2SYehuda Sadeh }
31713d14c5d2SYehuda Sadeh 
3172205ee118SIlya Dryomov static int ceph_oloc_decode(void **p, void *end,
3173205ee118SIlya Dryomov 			    struct ceph_object_locator *oloc)
3174205ee118SIlya Dryomov {
3175205ee118SIlya Dryomov 	u8 struct_v, struct_cv;
3176205ee118SIlya Dryomov 	u32 len;
3177205ee118SIlya Dryomov 	void *struct_end;
3178205ee118SIlya Dryomov 	int ret = 0;
3179205ee118SIlya Dryomov 
3180205ee118SIlya Dryomov 	ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3181205ee118SIlya Dryomov 	struct_v = ceph_decode_8(p);
3182205ee118SIlya Dryomov 	struct_cv = ceph_decode_8(p);
3183205ee118SIlya Dryomov 	if (struct_v < 3) {
3184205ee118SIlya Dryomov 		pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
3185205ee118SIlya Dryomov 			struct_v, struct_cv);
3186205ee118SIlya Dryomov 		goto e_inval;
3187205ee118SIlya Dryomov 	}
3188205ee118SIlya Dryomov 	if (struct_cv > 6) {
3189205ee118SIlya Dryomov 		pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
3190205ee118SIlya Dryomov 			struct_v, struct_cv);
3191205ee118SIlya Dryomov 		goto e_inval;
3192205ee118SIlya Dryomov 	}
3193205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3194205ee118SIlya Dryomov 	ceph_decode_need(p, end, len, e_inval);
3195205ee118SIlya Dryomov 	struct_end = *p + len;
3196205ee118SIlya Dryomov 
3197205ee118SIlya Dryomov 	oloc->pool = ceph_decode_64(p);
3198205ee118SIlya Dryomov 	*p += 4; /* skip preferred */
3199205ee118SIlya Dryomov 
3200205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3201205ee118SIlya Dryomov 	if (len > 0) {
3202205ee118SIlya Dryomov 		pr_warn("ceph_object_locator::key is set\n");
3203205ee118SIlya Dryomov 		goto e_inval;
3204205ee118SIlya Dryomov 	}
3205205ee118SIlya Dryomov 
3206205ee118SIlya Dryomov 	if (struct_v >= 5) {
3207cd08e0a2SYan, Zheng 		bool changed = false;
3208cd08e0a2SYan, Zheng 
3209205ee118SIlya Dryomov 		len = ceph_decode_32(p);
3210205ee118SIlya Dryomov 		if (len > 0) {
321130c156d9SYan, Zheng 			ceph_decode_need(p, end, len, e_inval);
3212cd08e0a2SYan, Zheng 			if (!oloc->pool_ns ||
3213cd08e0a2SYan, Zheng 			    ceph_compare_string(oloc->pool_ns, *p, len))
3214cd08e0a2SYan, Zheng 				changed = true;
321530c156d9SYan, Zheng 			*p += len;
3216cd08e0a2SYan, Zheng 		} else {
3217cd08e0a2SYan, Zheng 			if (oloc->pool_ns)
3218cd08e0a2SYan, Zheng 				changed = true;
3219cd08e0a2SYan, Zheng 		}
3220cd08e0a2SYan, Zheng 		if (changed) {
3221cd08e0a2SYan, Zheng 			/* redirect changes namespace */
3222cd08e0a2SYan, Zheng 			pr_warn("ceph_object_locator::nspace is changed\n");
3223cd08e0a2SYan, Zheng 			goto e_inval;
3224205ee118SIlya Dryomov 		}
3225205ee118SIlya Dryomov 	}
3226205ee118SIlya Dryomov 
3227205ee118SIlya Dryomov 	if (struct_v >= 6) {
3228205ee118SIlya Dryomov 		s64 hash = ceph_decode_64(p);
3229205ee118SIlya Dryomov 		if (hash != -1) {
3230205ee118SIlya Dryomov 			pr_warn("ceph_object_locator::hash is set\n");
3231205ee118SIlya Dryomov 			goto e_inval;
3232205ee118SIlya Dryomov 		}
3233205ee118SIlya Dryomov 	}
3234205ee118SIlya Dryomov 
3235205ee118SIlya Dryomov 	/* skip the rest */
3236205ee118SIlya Dryomov 	*p = struct_end;
3237205ee118SIlya Dryomov out:
3238205ee118SIlya Dryomov 	return ret;
3239205ee118SIlya Dryomov 
3240205ee118SIlya Dryomov e_inval:
3241205ee118SIlya Dryomov 	ret = -EINVAL;
3242205ee118SIlya Dryomov 	goto out;
3243205ee118SIlya Dryomov }
3244205ee118SIlya Dryomov 
3245205ee118SIlya Dryomov static int ceph_redirect_decode(void **p, void *end,
3246205ee118SIlya Dryomov 				struct ceph_request_redirect *redir)
3247205ee118SIlya Dryomov {
3248205ee118SIlya Dryomov 	u8 struct_v, struct_cv;
3249205ee118SIlya Dryomov 	u32 len;
3250205ee118SIlya Dryomov 	void *struct_end;
3251205ee118SIlya Dryomov 	int ret;
3252205ee118SIlya Dryomov 
3253205ee118SIlya Dryomov 	ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3254205ee118SIlya Dryomov 	struct_v = ceph_decode_8(p);
3255205ee118SIlya Dryomov 	struct_cv = ceph_decode_8(p);
3256205ee118SIlya Dryomov 	if (struct_cv > 1) {
3257205ee118SIlya Dryomov 		pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
3258205ee118SIlya Dryomov 			struct_v, struct_cv);
3259205ee118SIlya Dryomov 		goto e_inval;
3260205ee118SIlya Dryomov 	}
3261205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3262205ee118SIlya Dryomov 	ceph_decode_need(p, end, len, e_inval);
3263205ee118SIlya Dryomov 	struct_end = *p + len;
3264205ee118SIlya Dryomov 
3265205ee118SIlya Dryomov 	ret = ceph_oloc_decode(p, end, &redir->oloc);
3266205ee118SIlya Dryomov 	if (ret)
3267205ee118SIlya Dryomov 		goto out;
3268205ee118SIlya Dryomov 
3269205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3270205ee118SIlya Dryomov 	if (len > 0) {
3271205ee118SIlya Dryomov 		pr_warn("ceph_request_redirect::object_name is set\n");
3272205ee118SIlya Dryomov 		goto e_inval;
3273205ee118SIlya Dryomov 	}
3274205ee118SIlya Dryomov 
3275205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3276205ee118SIlya Dryomov 	*p += len; /* skip osd_instructions */
3277205ee118SIlya Dryomov 
3278205ee118SIlya Dryomov 	/* skip the rest */
3279205ee118SIlya Dryomov 	*p = struct_end;
3280205ee118SIlya Dryomov out:
3281205ee118SIlya Dryomov 	return ret;
3282205ee118SIlya Dryomov 
3283205ee118SIlya Dryomov e_inval:
3284205ee118SIlya Dryomov 	ret = -EINVAL;
3285205ee118SIlya Dryomov 	goto out;
3286205ee118SIlya Dryomov }
3287205ee118SIlya Dryomov 
3288fe5da05eSIlya Dryomov struct MOSDOpReply {
3289fe5da05eSIlya Dryomov 	struct ceph_pg pgid;
3290fe5da05eSIlya Dryomov 	u64 flags;
3291fe5da05eSIlya Dryomov 	int result;
3292fe5da05eSIlya Dryomov 	u32 epoch;
3293fe5da05eSIlya Dryomov 	int num_ops;
3294fe5da05eSIlya Dryomov 	u32 outdata_len[CEPH_OSD_MAX_OPS];
3295fe5da05eSIlya Dryomov 	s32 rval[CEPH_OSD_MAX_OPS];
3296fe5da05eSIlya Dryomov 	int retry_attempt;
3297fe5da05eSIlya Dryomov 	struct ceph_eversion replay_version;
3298fe5da05eSIlya Dryomov 	u64 user_version;
3299fe5da05eSIlya Dryomov 	struct ceph_request_redirect redirect;
3300fe5da05eSIlya Dryomov };
330125845472SSage Weil 
3302fe5da05eSIlya Dryomov static int decode_MOSDOpReply(const struct ceph_msg *msg, struct MOSDOpReply *m)
33033d14c5d2SYehuda Sadeh {
3304fe5da05eSIlya Dryomov 	void *p = msg->front.iov_base;
3305fe5da05eSIlya Dryomov 	void *const end = p + msg->front.iov_len;
3306fe5da05eSIlya Dryomov 	u16 version = le16_to_cpu(msg->hdr.version);
3307fe5da05eSIlya Dryomov 	struct ceph_eversion bad_replay_version;
3308b0b31a8fSIlya Dryomov 	u8 decode_redir;
3309fe5da05eSIlya Dryomov 	u32 len;
3310fe5da05eSIlya Dryomov 	int ret;
3311fe5da05eSIlya Dryomov 	int i;
33123d14c5d2SYehuda Sadeh 
3313fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, len, e_inval);
3314fe5da05eSIlya Dryomov 	ceph_decode_need(&p, end, len, e_inval);
3315fe5da05eSIlya Dryomov 	p += len; /* skip oid */
33161b83bef2SSage Weil 
3317fe5da05eSIlya Dryomov 	ret = ceph_decode_pgid(&p, end, &m->pgid);
3318fe5da05eSIlya Dryomov 	if (ret)
3319fe5da05eSIlya Dryomov 		return ret;
33201b83bef2SSage Weil 
3321fe5da05eSIlya Dryomov 	ceph_decode_64_safe(&p, end, m->flags, e_inval);
3322fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->result, e_inval);
3323fe5da05eSIlya Dryomov 	ceph_decode_need(&p, end, sizeof(bad_replay_version), e_inval);
3324fe5da05eSIlya Dryomov 	memcpy(&bad_replay_version, p, sizeof(bad_replay_version));
3325fe5da05eSIlya Dryomov 	p += sizeof(bad_replay_version);
3326fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->epoch, e_inval);
33271b83bef2SSage Weil 
3328fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->num_ops, e_inval);
3329fe5da05eSIlya Dryomov 	if (m->num_ops > ARRAY_SIZE(m->outdata_len))
3330fe5da05eSIlya Dryomov 		goto e_inval;
33311b83bef2SSage Weil 
3332fe5da05eSIlya Dryomov 	ceph_decode_need(&p, end, m->num_ops * sizeof(struct ceph_osd_op),
3333fe5da05eSIlya Dryomov 			 e_inval);
3334fe5da05eSIlya Dryomov 	for (i = 0; i < m->num_ops; i++) {
33351b83bef2SSage Weil 		struct ceph_osd_op *op = p;
33361b83bef2SSage Weil 
3337fe5da05eSIlya Dryomov 		m->outdata_len[i] = le32_to_cpu(op->payload_len);
33381b83bef2SSage Weil 		p += sizeof(*op);
33391b83bef2SSage Weil 	}
3340fe5da05eSIlya Dryomov 
3341fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->retry_attempt, e_inval);
3342fe5da05eSIlya Dryomov 	for (i = 0; i < m->num_ops; i++)
3343fe5da05eSIlya Dryomov 		ceph_decode_32_safe(&p, end, m->rval[i], e_inval);
3344fe5da05eSIlya Dryomov 
3345fe5da05eSIlya Dryomov 	if (version >= 5) {
3346fe5da05eSIlya Dryomov 		ceph_decode_need(&p, end, sizeof(m->replay_version), e_inval);
3347fe5da05eSIlya Dryomov 		memcpy(&m->replay_version, p, sizeof(m->replay_version));
3348fe5da05eSIlya Dryomov 		p += sizeof(m->replay_version);
3349fe5da05eSIlya Dryomov 		ceph_decode_64_safe(&p, end, m->user_version, e_inval);
3350fe5da05eSIlya Dryomov 	} else {
3351fe5da05eSIlya Dryomov 		m->replay_version = bad_replay_version; /* struct */
3352fe5da05eSIlya Dryomov 		m->user_version = le64_to_cpu(m->replay_version.version);
33531b83bef2SSage Weil 	}
33541b83bef2SSage Weil 
3355fe5da05eSIlya Dryomov 	if (version >= 6) {
3356fe5da05eSIlya Dryomov 		if (version >= 7)
3357fe5da05eSIlya Dryomov 			ceph_decode_8_safe(&p, end, decode_redir, e_inval);
3358b0b31a8fSIlya Dryomov 		else
3359b0b31a8fSIlya Dryomov 			decode_redir = 1;
3360b0b31a8fSIlya Dryomov 	} else {
3361b0b31a8fSIlya Dryomov 		decode_redir = 0;
3362b0b31a8fSIlya Dryomov 	}
3363b0b31a8fSIlya Dryomov 
3364b0b31a8fSIlya Dryomov 	if (decode_redir) {
3365fe5da05eSIlya Dryomov 		ret = ceph_redirect_decode(&p, end, &m->redirect);
3366fe5da05eSIlya Dryomov 		if (ret)
3367fe5da05eSIlya Dryomov 			return ret;
3368205ee118SIlya Dryomov 	} else {
3369fe5da05eSIlya Dryomov 		ceph_oloc_init(&m->redirect.oloc);
3370205ee118SIlya Dryomov 	}
3371205ee118SIlya Dryomov 
3372fe5da05eSIlya Dryomov 	return 0;
3373205ee118SIlya Dryomov 
3374fe5da05eSIlya Dryomov e_inval:
3375fe5da05eSIlya Dryomov 	return -EINVAL;
3376fe5da05eSIlya Dryomov }
3377fe5da05eSIlya Dryomov 
3378fe5da05eSIlya Dryomov /*
3379b18b9550SIlya Dryomov  * Handle MOSDOpReply.  Set ->r_result and call the callback if it is
3380b18b9550SIlya Dryomov  * specified.
3381fe5da05eSIlya Dryomov  */
33825aea3dcdSIlya Dryomov static void handle_reply(struct ceph_osd *osd, struct ceph_msg *msg)
3383fe5da05eSIlya Dryomov {
33845aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
3385fe5da05eSIlya Dryomov 	struct ceph_osd_request *req;
3386fe5da05eSIlya Dryomov 	struct MOSDOpReply m;
3387fe5da05eSIlya Dryomov 	u64 tid = le64_to_cpu(msg->hdr.tid);
3388fe5da05eSIlya Dryomov 	u32 data_len = 0;
3389fe5da05eSIlya Dryomov 	int ret;
3390fe5da05eSIlya Dryomov 	int i;
3391fe5da05eSIlya Dryomov 
3392fe5da05eSIlya Dryomov 	dout("%s msg %p tid %llu\n", __func__, msg, tid);
3393fe5da05eSIlya Dryomov 
33945aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
33955aea3dcdSIlya Dryomov 	if (!osd_registered(osd)) {
33965aea3dcdSIlya Dryomov 		dout("%s osd%d unknown\n", __func__, osd->o_osd);
33975aea3dcdSIlya Dryomov 		goto out_unlock_osdc;
3398fe5da05eSIlya Dryomov 	}
33995aea3dcdSIlya Dryomov 	WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
34005aea3dcdSIlya Dryomov 
34015aea3dcdSIlya Dryomov 	mutex_lock(&osd->lock);
34025aea3dcdSIlya Dryomov 	req = lookup_request(&osd->o_requests, tid);
34035aea3dcdSIlya Dryomov 	if (!req) {
34045aea3dcdSIlya Dryomov 		dout("%s osd%d tid %llu unknown\n", __func__, osd->o_osd, tid);
34055aea3dcdSIlya Dryomov 		goto out_unlock_session;
34065aea3dcdSIlya Dryomov 	}
3407fe5da05eSIlya Dryomov 
3408cd08e0a2SYan, Zheng 	m.redirect.oloc.pool_ns = req->r_t.target_oloc.pool_ns;
3409fe5da05eSIlya Dryomov 	ret = decode_MOSDOpReply(msg, &m);
3410cd08e0a2SYan, Zheng 	m.redirect.oloc.pool_ns = NULL;
3411fe5da05eSIlya Dryomov 	if (ret) {
3412fe5da05eSIlya Dryomov 		pr_err("failed to decode MOSDOpReply for tid %llu: %d\n",
3413fe5da05eSIlya Dryomov 		       req->r_tid, ret);
3414fe5da05eSIlya Dryomov 		ceph_msg_dump(msg);
3415fe5da05eSIlya Dryomov 		goto fail_request;
3416fe5da05eSIlya Dryomov 	}
3417fe5da05eSIlya Dryomov 	dout("%s req %p tid %llu flags 0x%llx pgid %llu.%x epoch %u attempt %d v %u'%llu uv %llu\n",
3418fe5da05eSIlya Dryomov 	     __func__, req, req->r_tid, m.flags, m.pgid.pool, m.pgid.seed,
3419fe5da05eSIlya Dryomov 	     m.epoch, m.retry_attempt, le32_to_cpu(m.replay_version.epoch),
3420fe5da05eSIlya Dryomov 	     le64_to_cpu(m.replay_version.version), m.user_version);
3421fe5da05eSIlya Dryomov 
3422fe5da05eSIlya Dryomov 	if (m.retry_attempt >= 0) {
3423fe5da05eSIlya Dryomov 		if (m.retry_attempt != req->r_attempts - 1) {
3424fe5da05eSIlya Dryomov 			dout("req %p tid %llu retry_attempt %d != %d, ignoring\n",
3425fe5da05eSIlya Dryomov 			     req, req->r_tid, m.retry_attempt,
3426fe5da05eSIlya Dryomov 			     req->r_attempts - 1);
34275aea3dcdSIlya Dryomov 			goto out_unlock_session;
3428fe5da05eSIlya Dryomov 		}
3429fe5da05eSIlya Dryomov 	} else {
3430fe5da05eSIlya Dryomov 		WARN_ON(1); /* MOSDOpReply v4 is assumed */
3431fe5da05eSIlya Dryomov 	}
3432fe5da05eSIlya Dryomov 
3433fe5da05eSIlya Dryomov 	if (!ceph_oloc_empty(&m.redirect.oloc)) {
3434fe5da05eSIlya Dryomov 		dout("req %p tid %llu redirect pool %lld\n", req, req->r_tid,
3435fe5da05eSIlya Dryomov 		     m.redirect.oloc.pool);
34365aea3dcdSIlya Dryomov 		unlink_request(osd, req);
34375aea3dcdSIlya Dryomov 		mutex_unlock(&osd->lock);
3438205ee118SIlya Dryomov 
343930c156d9SYan, Zheng 		/*
344030c156d9SYan, Zheng 		 * Not ceph_oloc_copy() - changing pool_ns is not
344130c156d9SYan, Zheng 		 * supported.
344230c156d9SYan, Zheng 		 */
344330c156d9SYan, Zheng 		req->r_t.target_oloc.pool = m.redirect.oloc.pool;
34445aea3dcdSIlya Dryomov 		req->r_flags |= CEPH_OSD_FLAG_REDIRECTED;
34455aea3dcdSIlya Dryomov 		req->r_tid = 0;
34465aea3dcdSIlya Dryomov 		__submit_request(req, false);
34475aea3dcdSIlya Dryomov 		goto out_unlock_osdc;
3448205ee118SIlya Dryomov 	}
3449205ee118SIlya Dryomov 
3450fe5da05eSIlya Dryomov 	if (m.num_ops != req->r_num_ops) {
3451fe5da05eSIlya Dryomov 		pr_err("num_ops %d != %d for tid %llu\n", m.num_ops,
3452fe5da05eSIlya Dryomov 		       req->r_num_ops, req->r_tid);
3453fe5da05eSIlya Dryomov 		goto fail_request;
3454fe5da05eSIlya Dryomov 	}
3455fe5da05eSIlya Dryomov 	for (i = 0; i < req->r_num_ops; i++) {
3456fe5da05eSIlya Dryomov 		dout(" req %p tid %llu op %d rval %d len %u\n", req,
3457fe5da05eSIlya Dryomov 		     req->r_tid, i, m.rval[i], m.outdata_len[i]);
3458fe5da05eSIlya Dryomov 		req->r_ops[i].rval = m.rval[i];
3459fe5da05eSIlya Dryomov 		req->r_ops[i].outdata_len = m.outdata_len[i];
3460fe5da05eSIlya Dryomov 		data_len += m.outdata_len[i];
3461fe5da05eSIlya Dryomov 	}
3462fe5da05eSIlya Dryomov 	if (data_len != le32_to_cpu(msg->hdr.data_len)) {
3463fe5da05eSIlya Dryomov 		pr_err("sum of lens %u != %u for tid %llu\n", data_len,
3464fe5da05eSIlya Dryomov 		       le32_to_cpu(msg->hdr.data_len), req->r_tid);
3465fe5da05eSIlya Dryomov 		goto fail_request;
3466fe5da05eSIlya Dryomov 	}
3467b18b9550SIlya Dryomov 	dout("%s req %p tid %llu result %d data_len %u\n", __func__,
3468b18b9550SIlya Dryomov 	     req, req->r_tid, m.result, data_len);
34693d14c5d2SYehuda Sadeh 
3470b18b9550SIlya Dryomov 	/*
3471b18b9550SIlya Dryomov 	 * Since we only ever request ONDISK, we should only ever get
3472b18b9550SIlya Dryomov 	 * one (type of) reply back.
3473b18b9550SIlya Dryomov 	 */
3474b18b9550SIlya Dryomov 	WARN_ON(!(m.flags & CEPH_OSD_FLAG_ONDISK));
3475fe5da05eSIlya Dryomov 	req->r_result = m.result ?: data_len;
347645ee2c1dSIlya Dryomov 	finish_request(req);
34775aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
34785aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
34793d14c5d2SYehuda Sadeh 
3480fe5da05eSIlya Dryomov 	__complete_request(req);
3481b18b9550SIlya Dryomov 	complete_all(&req->r_completion);
3482dc045a91SIlya Dryomov 	ceph_osdc_put_request(req);
34833d14c5d2SYehuda Sadeh 	return;
3484fe5da05eSIlya Dryomov 
3485fe5da05eSIlya Dryomov fail_request:
34864609245eSIlya Dryomov 	complete_request(req, -EIO);
34875aea3dcdSIlya Dryomov out_unlock_session:
34885aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
34895aea3dcdSIlya Dryomov out_unlock_osdc:
34905aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
34913d14c5d2SYehuda Sadeh }
34923d14c5d2SYehuda Sadeh 
349342c1b124SIlya Dryomov static void set_pool_was_full(struct ceph_osd_client *osdc)
349442c1b124SIlya Dryomov {
349542c1b124SIlya Dryomov 	struct rb_node *n;
349642c1b124SIlya Dryomov 
349742c1b124SIlya Dryomov 	for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
349842c1b124SIlya Dryomov 		struct ceph_pg_pool_info *pi =
349942c1b124SIlya Dryomov 		    rb_entry(n, struct ceph_pg_pool_info, node);
350042c1b124SIlya Dryomov 
350142c1b124SIlya Dryomov 		pi->was_full = __pool_full(pi);
350242c1b124SIlya Dryomov 	}
350342c1b124SIlya Dryomov }
350442c1b124SIlya Dryomov 
35055aea3dcdSIlya Dryomov static bool pool_cleared_full(struct ceph_osd_client *osdc, s64 pool_id)
35063d14c5d2SYehuda Sadeh {
35075aea3dcdSIlya Dryomov 	struct ceph_pg_pool_info *pi;
35083d14c5d2SYehuda Sadeh 
35095aea3dcdSIlya Dryomov 	pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
35105aea3dcdSIlya Dryomov 	if (!pi)
35115aea3dcdSIlya Dryomov 		return false;
35123d14c5d2SYehuda Sadeh 
35135aea3dcdSIlya Dryomov 	return pi->was_full && !__pool_full(pi);
35143d14c5d2SYehuda Sadeh }
35153d14c5d2SYehuda Sadeh 
3516922dab61SIlya Dryomov static enum calc_target_result
3517922dab61SIlya Dryomov recalc_linger_target(struct ceph_osd_linger_request *lreq)
3518922dab61SIlya Dryomov {
3519922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
3520922dab61SIlya Dryomov 	enum calc_target_result ct_res;
3521922dab61SIlya Dryomov 
35227de030d6SIlya Dryomov 	ct_res = calc_target(osdc, &lreq->t, NULL, true);
3523922dab61SIlya Dryomov 	if (ct_res == CALC_TARGET_NEED_RESEND) {
3524922dab61SIlya Dryomov 		struct ceph_osd *osd;
3525922dab61SIlya Dryomov 
3526922dab61SIlya Dryomov 		osd = lookup_create_osd(osdc, lreq->t.osd, true);
3527922dab61SIlya Dryomov 		if (osd != lreq->osd) {
3528922dab61SIlya Dryomov 			unlink_linger(lreq->osd, lreq);
3529922dab61SIlya Dryomov 			link_linger(osd, lreq);
3530922dab61SIlya Dryomov 		}
3531922dab61SIlya Dryomov 	}
3532922dab61SIlya Dryomov 
3533922dab61SIlya Dryomov 	return ct_res;
3534922dab61SIlya Dryomov }
3535922dab61SIlya Dryomov 
35363d14c5d2SYehuda Sadeh /*
35375aea3dcdSIlya Dryomov  * Requeue requests whose mapping to an OSD has changed.
35383d14c5d2SYehuda Sadeh  */
35395aea3dcdSIlya Dryomov static void scan_requests(struct ceph_osd *osd,
35405aea3dcdSIlya Dryomov 			  bool force_resend,
35415aea3dcdSIlya Dryomov 			  bool cleared_full,
35425aea3dcdSIlya Dryomov 			  bool check_pool_cleared_full,
35435aea3dcdSIlya Dryomov 			  struct rb_root *need_resend,
35445aea3dcdSIlya Dryomov 			  struct list_head *need_resend_linger)
35453d14c5d2SYehuda Sadeh {
35465aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
35475aea3dcdSIlya Dryomov 	struct rb_node *n;
35485aea3dcdSIlya Dryomov 	bool force_resend_writes;
35493d14c5d2SYehuda Sadeh 
3550922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_linger_requests); n; ) {
3551922dab61SIlya Dryomov 		struct ceph_osd_linger_request *lreq =
3552922dab61SIlya Dryomov 		    rb_entry(n, struct ceph_osd_linger_request, node);
3553922dab61SIlya Dryomov 		enum calc_target_result ct_res;
3554922dab61SIlya Dryomov 
3555922dab61SIlya Dryomov 		n = rb_next(n); /* recalc_linger_target() */
3556922dab61SIlya Dryomov 
3557922dab61SIlya Dryomov 		dout("%s lreq %p linger_id %llu\n", __func__, lreq,
3558922dab61SIlya Dryomov 		     lreq->linger_id);
3559922dab61SIlya Dryomov 		ct_res = recalc_linger_target(lreq);
3560922dab61SIlya Dryomov 		switch (ct_res) {
3561922dab61SIlya Dryomov 		case CALC_TARGET_NO_ACTION:
3562922dab61SIlya Dryomov 			force_resend_writes = cleared_full ||
3563922dab61SIlya Dryomov 			    (check_pool_cleared_full &&
3564922dab61SIlya Dryomov 			     pool_cleared_full(osdc, lreq->t.base_oloc.pool));
3565922dab61SIlya Dryomov 			if (!force_resend && !force_resend_writes)
3566922dab61SIlya Dryomov 				break;
3567922dab61SIlya Dryomov 
3568922dab61SIlya Dryomov 			/* fall through */
3569922dab61SIlya Dryomov 		case CALC_TARGET_NEED_RESEND:
35704609245eSIlya Dryomov 			cancel_linger_map_check(lreq);
3571922dab61SIlya Dryomov 			/*
3572922dab61SIlya Dryomov 			 * scan_requests() for the previous epoch(s)
3573922dab61SIlya Dryomov 			 * may have already added it to the list, since
3574922dab61SIlya Dryomov 			 * it's not unlinked here.
3575922dab61SIlya Dryomov 			 */
3576922dab61SIlya Dryomov 			if (list_empty(&lreq->scan_item))
3577922dab61SIlya Dryomov 				list_add_tail(&lreq->scan_item, need_resend_linger);
3578922dab61SIlya Dryomov 			break;
3579922dab61SIlya Dryomov 		case CALC_TARGET_POOL_DNE:
3580a10bcb19SIlya Dryomov 			list_del_init(&lreq->scan_item);
35814609245eSIlya Dryomov 			check_linger_pool_dne(lreq);
3582922dab61SIlya Dryomov 			break;
3583922dab61SIlya Dryomov 		}
3584922dab61SIlya Dryomov 	}
3585922dab61SIlya Dryomov 
35865aea3dcdSIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; ) {
35875aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
35885aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
35895aea3dcdSIlya Dryomov 		enum calc_target_result ct_res;
3590ab60b16dSAlex Elder 
35914609245eSIlya Dryomov 		n = rb_next(n); /* unlink_request(), check_pool_dne() */
3592ab60b16dSAlex Elder 
35935aea3dcdSIlya Dryomov 		dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
35947de030d6SIlya Dryomov 		ct_res = calc_target(osdc, &req->r_t, &req->r_osd->o_con,
35957de030d6SIlya Dryomov 				     false);
35965aea3dcdSIlya Dryomov 		switch (ct_res) {
35975aea3dcdSIlya Dryomov 		case CALC_TARGET_NO_ACTION:
35985aea3dcdSIlya Dryomov 			force_resend_writes = cleared_full ||
35995aea3dcdSIlya Dryomov 			    (check_pool_cleared_full &&
36005aea3dcdSIlya Dryomov 			     pool_cleared_full(osdc, req->r_t.base_oloc.pool));
36015aea3dcdSIlya Dryomov 			if (!force_resend &&
36025aea3dcdSIlya Dryomov 			    (!(req->r_flags & CEPH_OSD_FLAG_WRITE) ||
36035aea3dcdSIlya Dryomov 			     !force_resend_writes))
36045aea3dcdSIlya Dryomov 				break;
3605a40c4f10SYehuda Sadeh 
36065aea3dcdSIlya Dryomov 			/* fall through */
36075aea3dcdSIlya Dryomov 		case CALC_TARGET_NEED_RESEND:
36084609245eSIlya Dryomov 			cancel_map_check(req);
36095aea3dcdSIlya Dryomov 			unlink_request(osd, req);
36105aea3dcdSIlya Dryomov 			insert_request(need_resend, req);
36115aea3dcdSIlya Dryomov 			break;
36125aea3dcdSIlya Dryomov 		case CALC_TARGET_POOL_DNE:
36134609245eSIlya Dryomov 			check_pool_dne(req);
36145aea3dcdSIlya Dryomov 			break;
3615a40c4f10SYehuda Sadeh 		}
36163d14c5d2SYehuda Sadeh 	}
36173d14c5d2SYehuda Sadeh }
36186f6c7006SSage Weil 
361942c1b124SIlya Dryomov static int handle_one_map(struct ceph_osd_client *osdc,
36205aea3dcdSIlya Dryomov 			  void *p, void *end, bool incremental,
36215aea3dcdSIlya Dryomov 			  struct rb_root *need_resend,
36225aea3dcdSIlya Dryomov 			  struct list_head *need_resend_linger)
362342c1b124SIlya Dryomov {
362442c1b124SIlya Dryomov 	struct ceph_osdmap *newmap;
362542c1b124SIlya Dryomov 	struct rb_node *n;
362642c1b124SIlya Dryomov 	bool skipped_map = false;
362742c1b124SIlya Dryomov 	bool was_full;
362842c1b124SIlya Dryomov 
3629b7ec35b3SIlya Dryomov 	was_full = ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
363042c1b124SIlya Dryomov 	set_pool_was_full(osdc);
363142c1b124SIlya Dryomov 
363242c1b124SIlya Dryomov 	if (incremental)
363342c1b124SIlya Dryomov 		newmap = osdmap_apply_incremental(&p, end, osdc->osdmap);
363442c1b124SIlya Dryomov 	else
363542c1b124SIlya Dryomov 		newmap = ceph_osdmap_decode(&p, end);
363642c1b124SIlya Dryomov 	if (IS_ERR(newmap))
363742c1b124SIlya Dryomov 		return PTR_ERR(newmap);
363842c1b124SIlya Dryomov 
363942c1b124SIlya Dryomov 	if (newmap != osdc->osdmap) {
364042c1b124SIlya Dryomov 		/*
364142c1b124SIlya Dryomov 		 * Preserve ->was_full before destroying the old map.
364242c1b124SIlya Dryomov 		 * For pools that weren't in the old map, ->was_full
364342c1b124SIlya Dryomov 		 * should be false.
364442c1b124SIlya Dryomov 		 */
364542c1b124SIlya Dryomov 		for (n = rb_first(&newmap->pg_pools); n; n = rb_next(n)) {
364642c1b124SIlya Dryomov 			struct ceph_pg_pool_info *pi =
364742c1b124SIlya Dryomov 			    rb_entry(n, struct ceph_pg_pool_info, node);
364842c1b124SIlya Dryomov 			struct ceph_pg_pool_info *old_pi;
364942c1b124SIlya Dryomov 
365042c1b124SIlya Dryomov 			old_pi = ceph_pg_pool_by_id(osdc->osdmap, pi->id);
365142c1b124SIlya Dryomov 			if (old_pi)
365242c1b124SIlya Dryomov 				pi->was_full = old_pi->was_full;
365342c1b124SIlya Dryomov 			else
365442c1b124SIlya Dryomov 				WARN_ON(pi->was_full);
365542c1b124SIlya Dryomov 		}
365642c1b124SIlya Dryomov 
365742c1b124SIlya Dryomov 		if (osdc->osdmap->epoch &&
365842c1b124SIlya Dryomov 		    osdc->osdmap->epoch + 1 < newmap->epoch) {
365942c1b124SIlya Dryomov 			WARN_ON(incremental);
366042c1b124SIlya Dryomov 			skipped_map = true;
366142c1b124SIlya Dryomov 		}
366242c1b124SIlya Dryomov 
366342c1b124SIlya Dryomov 		ceph_osdmap_destroy(osdc->osdmap);
366442c1b124SIlya Dryomov 		osdc->osdmap = newmap;
366542c1b124SIlya Dryomov 	}
366642c1b124SIlya Dryomov 
3667b7ec35b3SIlya Dryomov 	was_full &= !ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
36685aea3dcdSIlya Dryomov 	scan_requests(&osdc->homeless_osd, skipped_map, was_full, true,
36695aea3dcdSIlya Dryomov 		      need_resend, need_resend_linger);
36705aea3dcdSIlya Dryomov 
36715aea3dcdSIlya Dryomov 	for (n = rb_first(&osdc->osds); n; ) {
36725aea3dcdSIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
36735aea3dcdSIlya Dryomov 
36745aea3dcdSIlya Dryomov 		n = rb_next(n); /* close_osd() */
36755aea3dcdSIlya Dryomov 
36765aea3dcdSIlya Dryomov 		scan_requests(osd, skipped_map, was_full, true, need_resend,
36775aea3dcdSIlya Dryomov 			      need_resend_linger);
36785aea3dcdSIlya Dryomov 		if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
36795aea3dcdSIlya Dryomov 		    memcmp(&osd->o_con.peer_addr,
36805aea3dcdSIlya Dryomov 			   ceph_osd_addr(osdc->osdmap, osd->o_osd),
36815aea3dcdSIlya Dryomov 			   sizeof(struct ceph_entity_addr)))
36825aea3dcdSIlya Dryomov 			close_osd(osd);
36835aea3dcdSIlya Dryomov 	}
368442c1b124SIlya Dryomov 
368542c1b124SIlya Dryomov 	return 0;
368642c1b124SIlya Dryomov }
36876f6c7006SSage Weil 
36885aea3dcdSIlya Dryomov static void kick_requests(struct ceph_osd_client *osdc,
36895aea3dcdSIlya Dryomov 			  struct rb_root *need_resend,
36905aea3dcdSIlya Dryomov 			  struct list_head *need_resend_linger)
36915aea3dcdSIlya Dryomov {
3692922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq, *nlreq;
369304c7d789SIlya Dryomov 	enum calc_target_result ct_res;
36945aea3dcdSIlya Dryomov 	struct rb_node *n;
36955aea3dcdSIlya Dryomov 
369604c7d789SIlya Dryomov 	/* make sure need_resend targets reflect latest map */
369704c7d789SIlya Dryomov 	for (n = rb_first(need_resend); n; ) {
369804c7d789SIlya Dryomov 		struct ceph_osd_request *req =
369904c7d789SIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
370004c7d789SIlya Dryomov 
370104c7d789SIlya Dryomov 		n = rb_next(n);
370204c7d789SIlya Dryomov 
370304c7d789SIlya Dryomov 		if (req->r_t.epoch < osdc->osdmap->epoch) {
370404c7d789SIlya Dryomov 			ct_res = calc_target(osdc, &req->r_t, NULL, false);
370504c7d789SIlya Dryomov 			if (ct_res == CALC_TARGET_POOL_DNE) {
370604c7d789SIlya Dryomov 				erase_request(need_resend, req);
370704c7d789SIlya Dryomov 				check_pool_dne(req);
370804c7d789SIlya Dryomov 			}
370904c7d789SIlya Dryomov 		}
371004c7d789SIlya Dryomov 	}
371104c7d789SIlya Dryomov 
37125aea3dcdSIlya Dryomov 	for (n = rb_first(need_resend); n; ) {
37135aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
37145aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
37155aea3dcdSIlya Dryomov 		struct ceph_osd *osd;
37165aea3dcdSIlya Dryomov 
37175aea3dcdSIlya Dryomov 		n = rb_next(n);
37185aea3dcdSIlya Dryomov 		erase_request(need_resend, req); /* before link_request() */
37195aea3dcdSIlya Dryomov 
37205aea3dcdSIlya Dryomov 		osd = lookup_create_osd(osdc, req->r_t.osd, true);
37215aea3dcdSIlya Dryomov 		link_request(osd, req);
37225aea3dcdSIlya Dryomov 		if (!req->r_linger) {
37235aea3dcdSIlya Dryomov 			if (!osd_homeless(osd) && !req->r_t.paused)
37245aea3dcdSIlya Dryomov 				send_request(req);
3725922dab61SIlya Dryomov 		} else {
3726922dab61SIlya Dryomov 			cancel_linger_request(req);
37275aea3dcdSIlya Dryomov 		}
37285aea3dcdSIlya Dryomov 	}
3729922dab61SIlya Dryomov 
3730922dab61SIlya Dryomov 	list_for_each_entry_safe(lreq, nlreq, need_resend_linger, scan_item) {
3731922dab61SIlya Dryomov 		if (!osd_homeless(lreq->osd))
3732922dab61SIlya Dryomov 			send_linger(lreq);
3733922dab61SIlya Dryomov 
3734922dab61SIlya Dryomov 		list_del_init(&lreq->scan_item);
3735922dab61SIlya Dryomov 	}
37365aea3dcdSIlya Dryomov }
37375aea3dcdSIlya Dryomov 
37383d14c5d2SYehuda Sadeh /*
37393d14c5d2SYehuda Sadeh  * Process updated osd map.
37403d14c5d2SYehuda Sadeh  *
37413d14c5d2SYehuda Sadeh  * The message contains any number of incremental and full maps, normally
37423d14c5d2SYehuda Sadeh  * indicating some sort of topology change in the cluster.  Kick requests
37433d14c5d2SYehuda Sadeh  * off to different OSDs as needed.
37443d14c5d2SYehuda Sadeh  */
37453d14c5d2SYehuda Sadeh void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
37463d14c5d2SYehuda Sadeh {
374742c1b124SIlya Dryomov 	void *p = msg->front.iov_base;
374842c1b124SIlya Dryomov 	void *const end = p + msg->front.iov_len;
37493d14c5d2SYehuda Sadeh 	u32 nr_maps, maplen;
37503d14c5d2SYehuda Sadeh 	u32 epoch;
37513d14c5d2SYehuda Sadeh 	struct ceph_fsid fsid;
37525aea3dcdSIlya Dryomov 	struct rb_root need_resend = RB_ROOT;
37535aea3dcdSIlya Dryomov 	LIST_HEAD(need_resend_linger);
375442c1b124SIlya Dryomov 	bool handled_incremental = false;
375542c1b124SIlya Dryomov 	bool was_pauserd, was_pausewr;
375642c1b124SIlya Dryomov 	bool pauserd, pausewr;
375742c1b124SIlya Dryomov 	int err;
37583d14c5d2SYehuda Sadeh 
375942c1b124SIlya Dryomov 	dout("%s have %u\n", __func__, osdc->osdmap->epoch);
37605aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
37613d14c5d2SYehuda Sadeh 
37623d14c5d2SYehuda Sadeh 	/* verify fsid */
37633d14c5d2SYehuda Sadeh 	ceph_decode_need(&p, end, sizeof(fsid), bad);
37643d14c5d2SYehuda Sadeh 	ceph_decode_copy(&p, &fsid, sizeof(fsid));
37653d14c5d2SYehuda Sadeh 	if (ceph_check_fsid(osdc->client, &fsid) < 0)
376642c1b124SIlya Dryomov 		goto bad;
37673d14c5d2SYehuda Sadeh 
3768b7ec35b3SIlya Dryomov 	was_pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3769b7ec35b3SIlya Dryomov 	was_pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3770b7ec35b3SIlya Dryomov 		      ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
377142c1b124SIlya Dryomov 		      have_pool_full(osdc);
37729a1ea2dbSJosh Durgin 
37733d14c5d2SYehuda Sadeh 	/* incremental maps */
37743d14c5d2SYehuda Sadeh 	ceph_decode_32_safe(&p, end, nr_maps, bad);
37753d14c5d2SYehuda Sadeh 	dout(" %d inc maps\n", nr_maps);
37763d14c5d2SYehuda Sadeh 	while (nr_maps > 0) {
37773d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, 2*sizeof(u32), bad);
37783d14c5d2SYehuda Sadeh 		epoch = ceph_decode_32(&p);
37793d14c5d2SYehuda Sadeh 		maplen = ceph_decode_32(&p);
37803d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, maplen, bad);
378142c1b124SIlya Dryomov 		if (osdc->osdmap->epoch &&
378242c1b124SIlya Dryomov 		    osdc->osdmap->epoch + 1 == epoch) {
37833d14c5d2SYehuda Sadeh 			dout("applying incremental map %u len %d\n",
37843d14c5d2SYehuda Sadeh 			     epoch, maplen);
37855aea3dcdSIlya Dryomov 			err = handle_one_map(osdc, p, p + maplen, true,
37865aea3dcdSIlya Dryomov 					     &need_resend, &need_resend_linger);
378742c1b124SIlya Dryomov 			if (err)
37883d14c5d2SYehuda Sadeh 				goto bad;
378942c1b124SIlya Dryomov 			handled_incremental = true;
37903d14c5d2SYehuda Sadeh 		} else {
37913d14c5d2SYehuda Sadeh 			dout("ignoring incremental map %u len %d\n",
37923d14c5d2SYehuda Sadeh 			     epoch, maplen);
37933d14c5d2SYehuda Sadeh 		}
379442c1b124SIlya Dryomov 		p += maplen;
37953d14c5d2SYehuda Sadeh 		nr_maps--;
37963d14c5d2SYehuda Sadeh 	}
379742c1b124SIlya Dryomov 	if (handled_incremental)
37983d14c5d2SYehuda Sadeh 		goto done;
37993d14c5d2SYehuda Sadeh 
38003d14c5d2SYehuda Sadeh 	/* full maps */
38013d14c5d2SYehuda Sadeh 	ceph_decode_32_safe(&p, end, nr_maps, bad);
38023d14c5d2SYehuda Sadeh 	dout(" %d full maps\n", nr_maps);
38033d14c5d2SYehuda Sadeh 	while (nr_maps) {
38043d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, 2*sizeof(u32), bad);
38053d14c5d2SYehuda Sadeh 		epoch = ceph_decode_32(&p);
38063d14c5d2SYehuda Sadeh 		maplen = ceph_decode_32(&p);
38073d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, maplen, bad);
38083d14c5d2SYehuda Sadeh 		if (nr_maps > 1) {
38093d14c5d2SYehuda Sadeh 			dout("skipping non-latest full map %u len %d\n",
38103d14c5d2SYehuda Sadeh 			     epoch, maplen);
3811e5253a7bSIlya Dryomov 		} else if (osdc->osdmap->epoch >= epoch) {
38123d14c5d2SYehuda Sadeh 			dout("skipping full map %u len %d, "
38133d14c5d2SYehuda Sadeh 			     "older than our %u\n", epoch, maplen,
38143d14c5d2SYehuda Sadeh 			     osdc->osdmap->epoch);
38153d14c5d2SYehuda Sadeh 		} else {
38163d14c5d2SYehuda Sadeh 			dout("taking full map %u len %d\n", epoch, maplen);
38175aea3dcdSIlya Dryomov 			err = handle_one_map(osdc, p, p + maplen, false,
38185aea3dcdSIlya Dryomov 					     &need_resend, &need_resend_linger);
381942c1b124SIlya Dryomov 			if (err)
38203d14c5d2SYehuda Sadeh 				goto bad;
38213d14c5d2SYehuda Sadeh 		}
38223d14c5d2SYehuda Sadeh 		p += maplen;
38233d14c5d2SYehuda Sadeh 		nr_maps--;
38243d14c5d2SYehuda Sadeh 	}
38253d14c5d2SYehuda Sadeh 
38263d14c5d2SYehuda Sadeh done:
3827cd634fb6SSage Weil 	/*
3828cd634fb6SSage Weil 	 * subscribe to subsequent osdmap updates if full to ensure
3829cd634fb6SSage Weil 	 * we find out when we are no longer full and stop returning
3830cd634fb6SSage Weil 	 * ENOSPC.
3831cd634fb6SSage Weil 	 */
3832b7ec35b3SIlya Dryomov 	pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3833b7ec35b3SIlya Dryomov 	pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3834b7ec35b3SIlya Dryomov 		  ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
383542c1b124SIlya Dryomov 		  have_pool_full(osdc);
383658eb7932SJeff Layton 	if (was_pauserd || was_pausewr || pauserd || pausewr ||
383758eb7932SJeff Layton 	    osdc->osdmap->epoch < osdc->epoch_barrier)
383842c1b124SIlya Dryomov 		maybe_request_map(osdc);
3839cd634fb6SSage Weil 
38405aea3dcdSIlya Dryomov 	kick_requests(osdc, &need_resend, &need_resend_linger);
384142c1b124SIlya Dryomov 
3842fc36d0a4SJeff Layton 	ceph_osdc_abort_on_full(osdc);
384342c1b124SIlya Dryomov 	ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
384442c1b124SIlya Dryomov 			  osdc->osdmap->epoch);
38455aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
38463d14c5d2SYehuda Sadeh 	wake_up_all(&osdc->client->auth_wq);
38473d14c5d2SYehuda Sadeh 	return;
38483d14c5d2SYehuda Sadeh 
38493d14c5d2SYehuda Sadeh bad:
38503d14c5d2SYehuda Sadeh 	pr_err("osdc handle_map corrupt msg\n");
38513d14c5d2SYehuda Sadeh 	ceph_msg_dump(msg);
38525aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
38535aea3dcdSIlya Dryomov }
38545aea3dcdSIlya Dryomov 
38555aea3dcdSIlya Dryomov /*
38565aea3dcdSIlya Dryomov  * Resubmit requests pending on the given osd.
38575aea3dcdSIlya Dryomov  */
38585aea3dcdSIlya Dryomov static void kick_osd_requests(struct ceph_osd *osd)
38595aea3dcdSIlya Dryomov {
38605aea3dcdSIlya Dryomov 	struct rb_node *n;
38615aea3dcdSIlya Dryomov 
3862a02a946dSIlya Dryomov 	clear_backoffs(osd);
3863a02a946dSIlya Dryomov 
3864922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; ) {
38655aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
38665aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
38675aea3dcdSIlya Dryomov 
3868922dab61SIlya Dryomov 		n = rb_next(n); /* cancel_linger_request() */
3869922dab61SIlya Dryomov 
38705aea3dcdSIlya Dryomov 		if (!req->r_linger) {
38715aea3dcdSIlya Dryomov 			if (!req->r_t.paused)
38725aea3dcdSIlya Dryomov 				send_request(req);
3873922dab61SIlya Dryomov 		} else {
3874922dab61SIlya Dryomov 			cancel_linger_request(req);
38755aea3dcdSIlya Dryomov 		}
38765aea3dcdSIlya Dryomov 	}
3877922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_linger_requests); n; n = rb_next(n)) {
3878922dab61SIlya Dryomov 		struct ceph_osd_linger_request *lreq =
3879922dab61SIlya Dryomov 		    rb_entry(n, struct ceph_osd_linger_request, node);
3880922dab61SIlya Dryomov 
3881922dab61SIlya Dryomov 		send_linger(lreq);
3882922dab61SIlya Dryomov 	}
38835aea3dcdSIlya Dryomov }
38845aea3dcdSIlya Dryomov 
38855aea3dcdSIlya Dryomov /*
38865aea3dcdSIlya Dryomov  * If the osd connection drops, we need to resubmit all requests.
38875aea3dcdSIlya Dryomov  */
38885aea3dcdSIlya Dryomov static void osd_fault(struct ceph_connection *con)
38895aea3dcdSIlya Dryomov {
38905aea3dcdSIlya Dryomov 	struct ceph_osd *osd = con->private;
38915aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
38925aea3dcdSIlya Dryomov 
38935aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
38945aea3dcdSIlya Dryomov 
38955aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
38965aea3dcdSIlya Dryomov 	if (!osd_registered(osd)) {
38975aea3dcdSIlya Dryomov 		dout("%s osd%d unknown\n", __func__, osd->o_osd);
38985aea3dcdSIlya Dryomov 		goto out_unlock;
38995aea3dcdSIlya Dryomov 	}
39005aea3dcdSIlya Dryomov 
39015aea3dcdSIlya Dryomov 	if (!reopen_osd(osd))
39025aea3dcdSIlya Dryomov 		kick_osd_requests(osd);
39035aea3dcdSIlya Dryomov 	maybe_request_map(osdc);
39045aea3dcdSIlya Dryomov 
39055aea3dcdSIlya Dryomov out_unlock:
39065aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
39073d14c5d2SYehuda Sadeh }
39083d14c5d2SYehuda Sadeh 
3909a02a946dSIlya Dryomov struct MOSDBackoff {
3910a02a946dSIlya Dryomov 	struct ceph_spg spgid;
3911a02a946dSIlya Dryomov 	u32 map_epoch;
3912a02a946dSIlya Dryomov 	u8 op;
3913a02a946dSIlya Dryomov 	u64 id;
3914a02a946dSIlya Dryomov 	struct ceph_hobject_id *begin;
3915a02a946dSIlya Dryomov 	struct ceph_hobject_id *end;
3916a02a946dSIlya Dryomov };
3917a02a946dSIlya Dryomov 
3918a02a946dSIlya Dryomov static int decode_MOSDBackoff(const struct ceph_msg *msg, struct MOSDBackoff *m)
3919a02a946dSIlya Dryomov {
3920a02a946dSIlya Dryomov 	void *p = msg->front.iov_base;
3921a02a946dSIlya Dryomov 	void *const end = p + msg->front.iov_len;
3922a02a946dSIlya Dryomov 	u8 struct_v;
3923a02a946dSIlya Dryomov 	u32 struct_len;
3924a02a946dSIlya Dryomov 	int ret;
3925a02a946dSIlya Dryomov 
3926a02a946dSIlya Dryomov 	ret = ceph_start_decoding(&p, end, 1, "spg_t", &struct_v, &struct_len);
3927a02a946dSIlya Dryomov 	if (ret)
3928a02a946dSIlya Dryomov 		return ret;
3929a02a946dSIlya Dryomov 
3930a02a946dSIlya Dryomov 	ret = ceph_decode_pgid(&p, end, &m->spgid.pgid);
3931a02a946dSIlya Dryomov 	if (ret)
3932a02a946dSIlya Dryomov 		return ret;
3933a02a946dSIlya Dryomov 
3934a02a946dSIlya Dryomov 	ceph_decode_8_safe(&p, end, m->spgid.shard, e_inval);
3935a02a946dSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->map_epoch, e_inval);
3936a02a946dSIlya Dryomov 	ceph_decode_8_safe(&p, end, m->op, e_inval);
3937a02a946dSIlya Dryomov 	ceph_decode_64_safe(&p, end, m->id, e_inval);
3938a02a946dSIlya Dryomov 
3939a02a946dSIlya Dryomov 	m->begin = kzalloc(sizeof(*m->begin), GFP_NOIO);
3940a02a946dSIlya Dryomov 	if (!m->begin)
3941a02a946dSIlya Dryomov 		return -ENOMEM;
3942a02a946dSIlya Dryomov 
3943a02a946dSIlya Dryomov 	ret = decode_hoid(&p, end, m->begin);
3944a02a946dSIlya Dryomov 	if (ret) {
3945a02a946dSIlya Dryomov 		free_hoid(m->begin);
3946a02a946dSIlya Dryomov 		return ret;
3947a02a946dSIlya Dryomov 	}
3948a02a946dSIlya Dryomov 
3949a02a946dSIlya Dryomov 	m->end = kzalloc(sizeof(*m->end), GFP_NOIO);
3950a02a946dSIlya Dryomov 	if (!m->end) {
3951a02a946dSIlya Dryomov 		free_hoid(m->begin);
3952a02a946dSIlya Dryomov 		return -ENOMEM;
3953a02a946dSIlya Dryomov 	}
3954a02a946dSIlya Dryomov 
3955a02a946dSIlya Dryomov 	ret = decode_hoid(&p, end, m->end);
3956a02a946dSIlya Dryomov 	if (ret) {
3957a02a946dSIlya Dryomov 		free_hoid(m->begin);
3958a02a946dSIlya Dryomov 		free_hoid(m->end);
3959a02a946dSIlya Dryomov 		return ret;
3960a02a946dSIlya Dryomov 	}
3961a02a946dSIlya Dryomov 
3962a02a946dSIlya Dryomov 	return 0;
3963a02a946dSIlya Dryomov 
3964a02a946dSIlya Dryomov e_inval:
3965a02a946dSIlya Dryomov 	return -EINVAL;
3966a02a946dSIlya Dryomov }
3967a02a946dSIlya Dryomov 
3968a02a946dSIlya Dryomov static struct ceph_msg *create_backoff_message(
3969a02a946dSIlya Dryomov 				const struct ceph_osd_backoff *backoff,
3970a02a946dSIlya Dryomov 				u32 map_epoch)
3971a02a946dSIlya Dryomov {
3972a02a946dSIlya Dryomov 	struct ceph_msg *msg;
3973a02a946dSIlya Dryomov 	void *p, *end;
3974a02a946dSIlya Dryomov 	int msg_size;
3975a02a946dSIlya Dryomov 
3976a02a946dSIlya Dryomov 	msg_size = CEPH_ENCODING_START_BLK_LEN +
3977a02a946dSIlya Dryomov 			CEPH_PGID_ENCODING_LEN + 1; /* spgid */
3978a02a946dSIlya Dryomov 	msg_size += 4 + 1 + 8; /* map_epoch, op, id */
3979a02a946dSIlya Dryomov 	msg_size += CEPH_ENCODING_START_BLK_LEN +
3980a02a946dSIlya Dryomov 			hoid_encoding_size(backoff->begin);
3981a02a946dSIlya Dryomov 	msg_size += CEPH_ENCODING_START_BLK_LEN +
3982a02a946dSIlya Dryomov 			hoid_encoding_size(backoff->end);
3983a02a946dSIlya Dryomov 
3984a02a946dSIlya Dryomov 	msg = ceph_msg_new(CEPH_MSG_OSD_BACKOFF, msg_size, GFP_NOIO, true);
3985a02a946dSIlya Dryomov 	if (!msg)
3986a02a946dSIlya Dryomov 		return NULL;
3987a02a946dSIlya Dryomov 
3988a02a946dSIlya Dryomov 	p = msg->front.iov_base;
3989a02a946dSIlya Dryomov 	end = p + msg->front_alloc_len;
3990a02a946dSIlya Dryomov 
3991a02a946dSIlya Dryomov 	encode_spgid(&p, &backoff->spgid);
3992a02a946dSIlya Dryomov 	ceph_encode_32(&p, map_epoch);
3993a02a946dSIlya Dryomov 	ceph_encode_8(&p, CEPH_OSD_BACKOFF_OP_ACK_BLOCK);
3994a02a946dSIlya Dryomov 	ceph_encode_64(&p, backoff->id);
3995a02a946dSIlya Dryomov 	encode_hoid(&p, end, backoff->begin);
3996a02a946dSIlya Dryomov 	encode_hoid(&p, end, backoff->end);
3997a02a946dSIlya Dryomov 	BUG_ON(p != end);
3998a02a946dSIlya Dryomov 
3999a02a946dSIlya Dryomov 	msg->front.iov_len = p - msg->front.iov_base;
4000a02a946dSIlya Dryomov 	msg->hdr.version = cpu_to_le16(1); /* MOSDBackoff v1 */
4001a02a946dSIlya Dryomov 	msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
4002a02a946dSIlya Dryomov 
4003a02a946dSIlya Dryomov 	return msg;
4004a02a946dSIlya Dryomov }
4005a02a946dSIlya Dryomov 
4006a02a946dSIlya Dryomov static void handle_backoff_block(struct ceph_osd *osd, struct MOSDBackoff *m)
4007a02a946dSIlya Dryomov {
4008a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
4009a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
4010a02a946dSIlya Dryomov 	struct ceph_msg *msg;
4011a02a946dSIlya Dryomov 
4012a02a946dSIlya Dryomov 	dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4013a02a946dSIlya Dryomov 	     m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4014a02a946dSIlya Dryomov 
4015a02a946dSIlya Dryomov 	spg = lookup_spg_mapping(&osd->o_backoff_mappings, &m->spgid);
4016a02a946dSIlya Dryomov 	if (!spg) {
4017a02a946dSIlya Dryomov 		spg = alloc_spg_mapping();
4018a02a946dSIlya Dryomov 		if (!spg) {
4019a02a946dSIlya Dryomov 			pr_err("%s failed to allocate spg\n", __func__);
4020a02a946dSIlya Dryomov 			return;
4021a02a946dSIlya Dryomov 		}
4022a02a946dSIlya Dryomov 		spg->spgid = m->spgid; /* struct */
4023a02a946dSIlya Dryomov 		insert_spg_mapping(&osd->o_backoff_mappings, spg);
4024a02a946dSIlya Dryomov 	}
4025a02a946dSIlya Dryomov 
4026a02a946dSIlya Dryomov 	backoff = alloc_backoff();
4027a02a946dSIlya Dryomov 	if (!backoff) {
4028a02a946dSIlya Dryomov 		pr_err("%s failed to allocate backoff\n", __func__);
4029a02a946dSIlya Dryomov 		return;
4030a02a946dSIlya Dryomov 	}
4031a02a946dSIlya Dryomov 	backoff->spgid = m->spgid; /* struct */
4032a02a946dSIlya Dryomov 	backoff->id = m->id;
4033a02a946dSIlya Dryomov 	backoff->begin = m->begin;
4034a02a946dSIlya Dryomov 	m->begin = NULL; /* backoff now owns this */
4035a02a946dSIlya Dryomov 	backoff->end = m->end;
4036a02a946dSIlya Dryomov 	m->end = NULL;   /* ditto */
4037a02a946dSIlya Dryomov 
4038a02a946dSIlya Dryomov 	insert_backoff(&spg->backoffs, backoff);
4039a02a946dSIlya Dryomov 	insert_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4040a02a946dSIlya Dryomov 
4041a02a946dSIlya Dryomov 	/*
4042a02a946dSIlya Dryomov 	 * Ack with original backoff's epoch so that the OSD can
4043a02a946dSIlya Dryomov 	 * discard this if there was a PG split.
4044a02a946dSIlya Dryomov 	 */
4045a02a946dSIlya Dryomov 	msg = create_backoff_message(backoff, m->map_epoch);
4046a02a946dSIlya Dryomov 	if (!msg) {
4047a02a946dSIlya Dryomov 		pr_err("%s failed to allocate msg\n", __func__);
4048a02a946dSIlya Dryomov 		return;
4049a02a946dSIlya Dryomov 	}
4050a02a946dSIlya Dryomov 	ceph_con_send(&osd->o_con, msg);
4051a02a946dSIlya Dryomov }
4052a02a946dSIlya Dryomov 
4053a02a946dSIlya Dryomov static bool target_contained_by(const struct ceph_osd_request_target *t,
4054a02a946dSIlya Dryomov 				const struct ceph_hobject_id *begin,
4055a02a946dSIlya Dryomov 				const struct ceph_hobject_id *end)
4056a02a946dSIlya Dryomov {
4057a02a946dSIlya Dryomov 	struct ceph_hobject_id hoid;
4058a02a946dSIlya Dryomov 	int cmp;
4059a02a946dSIlya Dryomov 
4060a02a946dSIlya Dryomov 	hoid_fill_from_target(&hoid, t);
4061a02a946dSIlya Dryomov 	cmp = hoid_compare(&hoid, begin);
4062a02a946dSIlya Dryomov 	return !cmp || (cmp > 0 && hoid_compare(&hoid, end) < 0);
4063a02a946dSIlya Dryomov }
4064a02a946dSIlya Dryomov 
4065a02a946dSIlya Dryomov static void handle_backoff_unblock(struct ceph_osd *osd,
4066a02a946dSIlya Dryomov 				   const struct MOSDBackoff *m)
4067a02a946dSIlya Dryomov {
4068a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
4069a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
4070a02a946dSIlya Dryomov 	struct rb_node *n;
4071a02a946dSIlya Dryomov 
4072a02a946dSIlya Dryomov 	dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4073a02a946dSIlya Dryomov 	     m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4074a02a946dSIlya Dryomov 
4075a02a946dSIlya Dryomov 	backoff = lookup_backoff_by_id(&osd->o_backoffs_by_id, m->id);
4076a02a946dSIlya Dryomov 	if (!backoff) {
4077a02a946dSIlya Dryomov 		pr_err("%s osd%d spgid %llu.%xs%d id %llu backoff dne\n",
4078a02a946dSIlya Dryomov 		       __func__, osd->o_osd, m->spgid.pgid.pool,
4079a02a946dSIlya Dryomov 		       m->spgid.pgid.seed, m->spgid.shard, m->id);
4080a02a946dSIlya Dryomov 		return;
4081a02a946dSIlya Dryomov 	}
4082a02a946dSIlya Dryomov 
4083a02a946dSIlya Dryomov 	if (hoid_compare(backoff->begin, m->begin) &&
4084a02a946dSIlya Dryomov 	    hoid_compare(backoff->end, m->end)) {
4085a02a946dSIlya Dryomov 		pr_err("%s osd%d spgid %llu.%xs%d id %llu bad range?\n",
4086a02a946dSIlya Dryomov 		       __func__, osd->o_osd, m->spgid.pgid.pool,
4087a02a946dSIlya Dryomov 		       m->spgid.pgid.seed, m->spgid.shard, m->id);
4088a02a946dSIlya Dryomov 		/* unblock it anyway... */
4089a02a946dSIlya Dryomov 	}
4090a02a946dSIlya Dryomov 
4091a02a946dSIlya Dryomov 	spg = lookup_spg_mapping(&osd->o_backoff_mappings, &backoff->spgid);
4092a02a946dSIlya Dryomov 	BUG_ON(!spg);
4093a02a946dSIlya Dryomov 
4094a02a946dSIlya Dryomov 	erase_backoff(&spg->backoffs, backoff);
4095a02a946dSIlya Dryomov 	erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4096a02a946dSIlya Dryomov 	free_backoff(backoff);
4097a02a946dSIlya Dryomov 
4098a02a946dSIlya Dryomov 	if (RB_EMPTY_ROOT(&spg->backoffs)) {
4099a02a946dSIlya Dryomov 		erase_spg_mapping(&osd->o_backoff_mappings, spg);
4100a02a946dSIlya Dryomov 		free_spg_mapping(spg);
4101a02a946dSIlya Dryomov 	}
4102a02a946dSIlya Dryomov 
4103a02a946dSIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
4104a02a946dSIlya Dryomov 		struct ceph_osd_request *req =
4105a02a946dSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
4106a02a946dSIlya Dryomov 
4107a02a946dSIlya Dryomov 		if (!ceph_spg_compare(&req->r_t.spgid, &m->spgid)) {
4108a02a946dSIlya Dryomov 			/*
4109a02a946dSIlya Dryomov 			 * Match against @m, not @backoff -- the PG may
4110a02a946dSIlya Dryomov 			 * have split on the OSD.
4111a02a946dSIlya Dryomov 			 */
4112a02a946dSIlya Dryomov 			if (target_contained_by(&req->r_t, m->begin, m->end)) {
4113a02a946dSIlya Dryomov 				/*
4114a02a946dSIlya Dryomov 				 * If no other installed backoff applies,
4115a02a946dSIlya Dryomov 				 * resend.
4116a02a946dSIlya Dryomov 				 */
4117a02a946dSIlya Dryomov 				send_request(req);
4118a02a946dSIlya Dryomov 			}
4119a02a946dSIlya Dryomov 		}
4120a02a946dSIlya Dryomov 	}
4121a02a946dSIlya Dryomov }
4122a02a946dSIlya Dryomov 
4123a02a946dSIlya Dryomov static void handle_backoff(struct ceph_osd *osd, struct ceph_msg *msg)
4124a02a946dSIlya Dryomov {
4125a02a946dSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
4126a02a946dSIlya Dryomov 	struct MOSDBackoff m;
4127a02a946dSIlya Dryomov 	int ret;
4128a02a946dSIlya Dryomov 
4129a02a946dSIlya Dryomov 	down_read(&osdc->lock);
4130a02a946dSIlya Dryomov 	if (!osd_registered(osd)) {
4131a02a946dSIlya Dryomov 		dout("%s osd%d unknown\n", __func__, osd->o_osd);
4132a02a946dSIlya Dryomov 		up_read(&osdc->lock);
4133a02a946dSIlya Dryomov 		return;
4134a02a946dSIlya Dryomov 	}
4135a02a946dSIlya Dryomov 	WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
4136a02a946dSIlya Dryomov 
4137a02a946dSIlya Dryomov 	mutex_lock(&osd->lock);
4138a02a946dSIlya Dryomov 	ret = decode_MOSDBackoff(msg, &m);
4139a02a946dSIlya Dryomov 	if (ret) {
4140a02a946dSIlya Dryomov 		pr_err("failed to decode MOSDBackoff: %d\n", ret);
4141a02a946dSIlya Dryomov 		ceph_msg_dump(msg);
4142a02a946dSIlya Dryomov 		goto out_unlock;
4143a02a946dSIlya Dryomov 	}
4144a02a946dSIlya Dryomov 
4145a02a946dSIlya Dryomov 	switch (m.op) {
4146a02a946dSIlya Dryomov 	case CEPH_OSD_BACKOFF_OP_BLOCK:
4147a02a946dSIlya Dryomov 		handle_backoff_block(osd, &m);
4148a02a946dSIlya Dryomov 		break;
4149a02a946dSIlya Dryomov 	case CEPH_OSD_BACKOFF_OP_UNBLOCK:
4150a02a946dSIlya Dryomov 		handle_backoff_unblock(osd, &m);
4151a02a946dSIlya Dryomov 		break;
4152a02a946dSIlya Dryomov 	default:
4153a02a946dSIlya Dryomov 		pr_err("%s osd%d unknown op %d\n", __func__, osd->o_osd, m.op);
4154a02a946dSIlya Dryomov 	}
4155a02a946dSIlya Dryomov 
4156a02a946dSIlya Dryomov 	free_hoid(m.begin);
4157a02a946dSIlya Dryomov 	free_hoid(m.end);
4158a02a946dSIlya Dryomov 
4159a02a946dSIlya Dryomov out_unlock:
4160a02a946dSIlya Dryomov 	mutex_unlock(&osd->lock);
4161a02a946dSIlya Dryomov 	up_read(&osdc->lock);
4162a02a946dSIlya Dryomov }
4163a02a946dSIlya Dryomov 
41643d14c5d2SYehuda Sadeh /*
4165a40c4f10SYehuda Sadeh  * Process osd watch notifications
4166a40c4f10SYehuda Sadeh  */
41673c663bbdSAlex Elder static void handle_watch_notify(struct ceph_osd_client *osdc,
41683c663bbdSAlex Elder 				struct ceph_msg *msg)
4169a40c4f10SYehuda Sadeh {
4170922dab61SIlya Dryomov 	void *p = msg->front.iov_base;
4171922dab61SIlya Dryomov 	void *const end = p + msg->front.iov_len;
4172922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
4173922dab61SIlya Dryomov 	struct linger_work *lwork;
4174922dab61SIlya Dryomov 	u8 proto_ver, opcode;
4175922dab61SIlya Dryomov 	u64 cookie, notify_id;
4176922dab61SIlya Dryomov 	u64 notifier_id = 0;
417719079203SIlya Dryomov 	s32 return_code = 0;
4178922dab61SIlya Dryomov 	void *payload = NULL;
4179922dab61SIlya Dryomov 	u32 payload_len = 0;
4180a40c4f10SYehuda Sadeh 
4181a40c4f10SYehuda Sadeh 	ceph_decode_8_safe(&p, end, proto_ver, bad);
4182a40c4f10SYehuda Sadeh 	ceph_decode_8_safe(&p, end, opcode, bad);
4183a40c4f10SYehuda Sadeh 	ceph_decode_64_safe(&p, end, cookie, bad);
4184922dab61SIlya Dryomov 	p += 8; /* skip ver */
4185a40c4f10SYehuda Sadeh 	ceph_decode_64_safe(&p, end, notify_id, bad);
4186a40c4f10SYehuda Sadeh 
4187922dab61SIlya Dryomov 	if (proto_ver >= 1) {
4188922dab61SIlya Dryomov 		ceph_decode_32_safe(&p, end, payload_len, bad);
4189922dab61SIlya Dryomov 		ceph_decode_need(&p, end, payload_len, bad);
4190922dab61SIlya Dryomov 		payload = p;
4191922dab61SIlya Dryomov 		p += payload_len;
4192a40c4f10SYehuda Sadeh 	}
4193a40c4f10SYehuda Sadeh 
4194922dab61SIlya Dryomov 	if (le16_to_cpu(msg->hdr.version) >= 2)
419519079203SIlya Dryomov 		ceph_decode_32_safe(&p, end, return_code, bad);
4196922dab61SIlya Dryomov 
4197922dab61SIlya Dryomov 	if (le16_to_cpu(msg->hdr.version) >= 3)
4198922dab61SIlya Dryomov 		ceph_decode_64_safe(&p, end, notifier_id, bad);
4199922dab61SIlya Dryomov 
4200922dab61SIlya Dryomov 	down_read(&osdc->lock);
4201922dab61SIlya Dryomov 	lreq = lookup_linger_osdc(&osdc->linger_requests, cookie);
4202922dab61SIlya Dryomov 	if (!lreq) {
4203922dab61SIlya Dryomov 		dout("%s opcode %d cookie %llu dne\n", __func__, opcode,
4204922dab61SIlya Dryomov 		     cookie);
4205922dab61SIlya Dryomov 		goto out_unlock_osdc;
4206922dab61SIlya Dryomov 	}
4207922dab61SIlya Dryomov 
4208922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
420919079203SIlya Dryomov 	dout("%s opcode %d cookie %llu lreq %p is_watch %d\n", __func__,
421019079203SIlya Dryomov 	     opcode, cookie, lreq, lreq->is_watch);
4211922dab61SIlya Dryomov 	if (opcode == CEPH_WATCH_EVENT_DISCONNECT) {
4212922dab61SIlya Dryomov 		if (!lreq->last_error) {
4213922dab61SIlya Dryomov 			lreq->last_error = -ENOTCONN;
4214922dab61SIlya Dryomov 			queue_watch_error(lreq);
4215922dab61SIlya Dryomov 		}
421619079203SIlya Dryomov 	} else if (!lreq->is_watch) {
421719079203SIlya Dryomov 		/* CEPH_WATCH_EVENT_NOTIFY_COMPLETE */
421819079203SIlya Dryomov 		if (lreq->notify_id && lreq->notify_id != notify_id) {
421919079203SIlya Dryomov 			dout("lreq %p notify_id %llu != %llu, ignoring\n", lreq,
422019079203SIlya Dryomov 			     lreq->notify_id, notify_id);
422119079203SIlya Dryomov 		} else if (!completion_done(&lreq->notify_finish_wait)) {
422219079203SIlya Dryomov 			struct ceph_msg_data *data =
422319079203SIlya Dryomov 			    list_first_entry_or_null(&msg->data,
422419079203SIlya Dryomov 						     struct ceph_msg_data,
422519079203SIlya Dryomov 						     links);
422619079203SIlya Dryomov 
422719079203SIlya Dryomov 			if (data) {
422819079203SIlya Dryomov 				if (lreq->preply_pages) {
422919079203SIlya Dryomov 					WARN_ON(data->type !=
423019079203SIlya Dryomov 							CEPH_MSG_DATA_PAGES);
423119079203SIlya Dryomov 					*lreq->preply_pages = data->pages;
423219079203SIlya Dryomov 					*lreq->preply_len = data->length;
423319079203SIlya Dryomov 				} else {
423419079203SIlya Dryomov 					ceph_release_page_vector(data->pages,
423519079203SIlya Dryomov 					       calc_pages_for(0, data->length));
423619079203SIlya Dryomov 				}
423719079203SIlya Dryomov 			}
423819079203SIlya Dryomov 			lreq->notify_finish_error = return_code;
423919079203SIlya Dryomov 			complete_all(&lreq->notify_finish_wait);
424019079203SIlya Dryomov 		}
4241922dab61SIlya Dryomov 	} else {
4242922dab61SIlya Dryomov 		/* CEPH_WATCH_EVENT_NOTIFY */
4243922dab61SIlya Dryomov 		lwork = lwork_alloc(lreq, do_watch_notify);
4244922dab61SIlya Dryomov 		if (!lwork) {
4245922dab61SIlya Dryomov 			pr_err("failed to allocate notify-lwork\n");
4246922dab61SIlya Dryomov 			goto out_unlock_lreq;
4247922dab61SIlya Dryomov 		}
4248922dab61SIlya Dryomov 
4249922dab61SIlya Dryomov 		lwork->notify.notify_id = notify_id;
4250922dab61SIlya Dryomov 		lwork->notify.notifier_id = notifier_id;
4251922dab61SIlya Dryomov 		lwork->notify.payload = payload;
4252922dab61SIlya Dryomov 		lwork->notify.payload_len = payload_len;
4253922dab61SIlya Dryomov 		lwork->notify.msg = ceph_msg_get(msg);
4254922dab61SIlya Dryomov 		lwork_queue(lwork);
4255922dab61SIlya Dryomov 	}
4256922dab61SIlya Dryomov 
4257922dab61SIlya Dryomov out_unlock_lreq:
4258922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
4259922dab61SIlya Dryomov out_unlock_osdc:
4260922dab61SIlya Dryomov 	up_read(&osdc->lock);
4261a40c4f10SYehuda Sadeh 	return;
4262a40c4f10SYehuda Sadeh 
4263a40c4f10SYehuda Sadeh bad:
4264a40c4f10SYehuda Sadeh 	pr_err("osdc handle_watch_notify corrupt msg\n");
4265a40c4f10SYehuda Sadeh }
4266a40c4f10SYehuda Sadeh 
4267a40c4f10SYehuda Sadeh /*
42683d14c5d2SYehuda Sadeh  * Register request, send initial attempt.
42693d14c5d2SYehuda Sadeh  */
42703d14c5d2SYehuda Sadeh int ceph_osdc_start_request(struct ceph_osd_client *osdc,
42713d14c5d2SYehuda Sadeh 			    struct ceph_osd_request *req,
42723d14c5d2SYehuda Sadeh 			    bool nofail)
42733d14c5d2SYehuda Sadeh {
42745aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
42755aea3dcdSIlya Dryomov 	submit_request(req, false);
42765aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
42773d14c5d2SYehuda Sadeh 
42785aea3dcdSIlya Dryomov 	return 0;
42793d14c5d2SYehuda Sadeh }
42803d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_start_request);
42813d14c5d2SYehuda Sadeh 
42823d14c5d2SYehuda Sadeh /*
4283c297eb42SIlya Dryomov  * Unregister a registered request.  The request is not completed:
4284c297eb42SIlya Dryomov  * ->r_result isn't set and __complete_request() isn't called.
4285c9f9b93dSIlya Dryomov  */
4286c9f9b93dSIlya Dryomov void ceph_osdc_cancel_request(struct ceph_osd_request *req)
4287c9f9b93dSIlya Dryomov {
4288c9f9b93dSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
4289c9f9b93dSIlya Dryomov 
42905aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
42915aea3dcdSIlya Dryomov 	if (req->r_osd)
42925aea3dcdSIlya Dryomov 		cancel_request(req);
42935aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
4294c9f9b93dSIlya Dryomov }
4295c9f9b93dSIlya Dryomov EXPORT_SYMBOL(ceph_osdc_cancel_request);
4296c9f9b93dSIlya Dryomov 
4297c9f9b93dSIlya Dryomov /*
429842b06965SIlya Dryomov  * @timeout: in jiffies, 0 means "wait forever"
429942b06965SIlya Dryomov  */
430042b06965SIlya Dryomov static int wait_request_timeout(struct ceph_osd_request *req,
430142b06965SIlya Dryomov 				unsigned long timeout)
430242b06965SIlya Dryomov {
430342b06965SIlya Dryomov 	long left;
430442b06965SIlya Dryomov 
430542b06965SIlya Dryomov 	dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
43060e76abf2SYan, Zheng 	left = wait_for_completion_killable_timeout(&req->r_completion,
430742b06965SIlya Dryomov 						ceph_timeout_jiffies(timeout));
430842b06965SIlya Dryomov 	if (left <= 0) {
430942b06965SIlya Dryomov 		left = left ?: -ETIMEDOUT;
431042b06965SIlya Dryomov 		ceph_osdc_cancel_request(req);
431142b06965SIlya Dryomov 	} else {
431242b06965SIlya Dryomov 		left = req->r_result; /* completed */
431342b06965SIlya Dryomov 	}
431442b06965SIlya Dryomov 
431542b06965SIlya Dryomov 	return left;
431642b06965SIlya Dryomov }
431742b06965SIlya Dryomov 
431842b06965SIlya Dryomov /*
43193d14c5d2SYehuda Sadeh  * wait for a request to complete
43203d14c5d2SYehuda Sadeh  */
43213d14c5d2SYehuda Sadeh int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
43223d14c5d2SYehuda Sadeh 			   struct ceph_osd_request *req)
43233d14c5d2SYehuda Sadeh {
432442b06965SIlya Dryomov 	return wait_request_timeout(req, 0);
43253d14c5d2SYehuda Sadeh }
43263d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_wait_request);
43273d14c5d2SYehuda Sadeh 
43283d14c5d2SYehuda Sadeh /*
43293d14c5d2SYehuda Sadeh  * sync - wait for all in-flight requests to flush.  avoid starvation.
43303d14c5d2SYehuda Sadeh  */
43313d14c5d2SYehuda Sadeh void ceph_osdc_sync(struct ceph_osd_client *osdc)
43323d14c5d2SYehuda Sadeh {
43335aea3dcdSIlya Dryomov 	struct rb_node *n, *p;
43345aea3dcdSIlya Dryomov 	u64 last_tid = atomic64_read(&osdc->last_tid);
43353d14c5d2SYehuda Sadeh 
43365aea3dcdSIlya Dryomov again:
43375aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
43385aea3dcdSIlya Dryomov 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
43395aea3dcdSIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
43405aea3dcdSIlya Dryomov 
43415aea3dcdSIlya Dryomov 		mutex_lock(&osd->lock);
43425aea3dcdSIlya Dryomov 		for (p = rb_first(&osd->o_requests); p; p = rb_next(p)) {
43435aea3dcdSIlya Dryomov 			struct ceph_osd_request *req =
43445aea3dcdSIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
43455aea3dcdSIlya Dryomov 
43463d14c5d2SYehuda Sadeh 			if (req->r_tid > last_tid)
43473d14c5d2SYehuda Sadeh 				break;
43483d14c5d2SYehuda Sadeh 
43495aea3dcdSIlya Dryomov 			if (!(req->r_flags & CEPH_OSD_FLAG_WRITE))
43503d14c5d2SYehuda Sadeh 				continue;
43513d14c5d2SYehuda Sadeh 
43523d14c5d2SYehuda Sadeh 			ceph_osdc_get_request(req);
43535aea3dcdSIlya Dryomov 			mutex_unlock(&osd->lock);
43545aea3dcdSIlya Dryomov 			up_read(&osdc->lock);
43555aea3dcdSIlya Dryomov 			dout("%s waiting on req %p tid %llu last_tid %llu\n",
43565aea3dcdSIlya Dryomov 			     __func__, req, req->r_tid, last_tid);
4357b18b9550SIlya Dryomov 			wait_for_completion(&req->r_completion);
43583d14c5d2SYehuda Sadeh 			ceph_osdc_put_request(req);
43595aea3dcdSIlya Dryomov 			goto again;
43603d14c5d2SYehuda Sadeh 		}
43615aea3dcdSIlya Dryomov 
43625aea3dcdSIlya Dryomov 		mutex_unlock(&osd->lock);
43635aea3dcdSIlya Dryomov 	}
43645aea3dcdSIlya Dryomov 
43655aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
43665aea3dcdSIlya Dryomov 	dout("%s done last_tid %llu\n", __func__, last_tid);
43673d14c5d2SYehuda Sadeh }
43683d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_sync);
43693d14c5d2SYehuda Sadeh 
4370922dab61SIlya Dryomov static struct ceph_osd_request *
4371922dab61SIlya Dryomov alloc_linger_request(struct ceph_osd_linger_request *lreq)
4372922dab61SIlya Dryomov {
4373922dab61SIlya Dryomov 	struct ceph_osd_request *req;
4374922dab61SIlya Dryomov 
4375922dab61SIlya Dryomov 	req = ceph_osdc_alloc_request(lreq->osdc, NULL, 1, false, GFP_NOIO);
4376922dab61SIlya Dryomov 	if (!req)
4377922dab61SIlya Dryomov 		return NULL;
4378922dab61SIlya Dryomov 
4379922dab61SIlya Dryomov 	ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
4380922dab61SIlya Dryomov 	ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
4381922dab61SIlya Dryomov 
4382922dab61SIlya Dryomov 	if (ceph_osdc_alloc_messages(req, GFP_NOIO)) {
4383922dab61SIlya Dryomov 		ceph_osdc_put_request(req);
4384922dab61SIlya Dryomov 		return NULL;
4385922dab61SIlya Dryomov 	}
4386922dab61SIlya Dryomov 
4387922dab61SIlya Dryomov 	return req;
4388922dab61SIlya Dryomov }
4389922dab61SIlya Dryomov 
4390922dab61SIlya Dryomov /*
4391922dab61SIlya Dryomov  * Returns a handle, caller owns a ref.
4392922dab61SIlya Dryomov  */
4393922dab61SIlya Dryomov struct ceph_osd_linger_request *
4394922dab61SIlya Dryomov ceph_osdc_watch(struct ceph_osd_client *osdc,
4395922dab61SIlya Dryomov 		struct ceph_object_id *oid,
4396922dab61SIlya Dryomov 		struct ceph_object_locator *oloc,
4397922dab61SIlya Dryomov 		rados_watchcb2_t wcb,
4398922dab61SIlya Dryomov 		rados_watcherrcb_t errcb,
4399922dab61SIlya Dryomov 		void *data)
4400922dab61SIlya Dryomov {
4401922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
4402922dab61SIlya Dryomov 	int ret;
4403922dab61SIlya Dryomov 
4404922dab61SIlya Dryomov 	lreq = linger_alloc(osdc);
4405922dab61SIlya Dryomov 	if (!lreq)
4406922dab61SIlya Dryomov 		return ERR_PTR(-ENOMEM);
4407922dab61SIlya Dryomov 
440819079203SIlya Dryomov 	lreq->is_watch = true;
4409922dab61SIlya Dryomov 	lreq->wcb = wcb;
4410922dab61SIlya Dryomov 	lreq->errcb = errcb;
4411922dab61SIlya Dryomov 	lreq->data = data;
4412b07d3c4bSIlya Dryomov 	lreq->watch_valid_thru = jiffies;
4413922dab61SIlya Dryomov 
4414922dab61SIlya Dryomov 	ceph_oid_copy(&lreq->t.base_oid, oid);
4415922dab61SIlya Dryomov 	ceph_oloc_copy(&lreq->t.base_oloc, oloc);
441654ea0046SIlya Dryomov 	lreq->t.flags = CEPH_OSD_FLAG_WRITE;
44171134e091SDeepa Dinamani 	ktime_get_real_ts(&lreq->mtime);
4418922dab61SIlya Dryomov 
4419922dab61SIlya Dryomov 	lreq->reg_req = alloc_linger_request(lreq);
4420922dab61SIlya Dryomov 	if (!lreq->reg_req) {
4421922dab61SIlya Dryomov 		ret = -ENOMEM;
4422922dab61SIlya Dryomov 		goto err_put_lreq;
4423922dab61SIlya Dryomov 	}
4424922dab61SIlya Dryomov 
4425922dab61SIlya Dryomov 	lreq->ping_req = alloc_linger_request(lreq);
4426922dab61SIlya Dryomov 	if (!lreq->ping_req) {
4427922dab61SIlya Dryomov 		ret = -ENOMEM;
4428922dab61SIlya Dryomov 		goto err_put_lreq;
4429922dab61SIlya Dryomov 	}
4430922dab61SIlya Dryomov 
4431922dab61SIlya Dryomov 	down_write(&osdc->lock);
4432922dab61SIlya Dryomov 	linger_register(lreq); /* before osd_req_op_* */
4433922dab61SIlya Dryomov 	osd_req_op_watch_init(lreq->reg_req, 0, lreq->linger_id,
4434922dab61SIlya Dryomov 			      CEPH_OSD_WATCH_OP_WATCH);
4435922dab61SIlya Dryomov 	osd_req_op_watch_init(lreq->ping_req, 0, lreq->linger_id,
4436922dab61SIlya Dryomov 			      CEPH_OSD_WATCH_OP_PING);
4437922dab61SIlya Dryomov 	linger_submit(lreq);
4438922dab61SIlya Dryomov 	up_write(&osdc->lock);
4439922dab61SIlya Dryomov 
4440922dab61SIlya Dryomov 	ret = linger_reg_commit_wait(lreq);
4441922dab61SIlya Dryomov 	if (ret) {
4442922dab61SIlya Dryomov 		linger_cancel(lreq);
4443922dab61SIlya Dryomov 		goto err_put_lreq;
4444922dab61SIlya Dryomov 	}
4445922dab61SIlya Dryomov 
4446922dab61SIlya Dryomov 	return lreq;
4447922dab61SIlya Dryomov 
4448922dab61SIlya Dryomov err_put_lreq:
4449922dab61SIlya Dryomov 	linger_put(lreq);
4450922dab61SIlya Dryomov 	return ERR_PTR(ret);
4451922dab61SIlya Dryomov }
4452922dab61SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_watch);
4453922dab61SIlya Dryomov 
4454922dab61SIlya Dryomov /*
4455922dab61SIlya Dryomov  * Releases a ref.
4456922dab61SIlya Dryomov  *
4457922dab61SIlya Dryomov  * Times out after mount_timeout to preserve rbd unmap behaviour
4458922dab61SIlya Dryomov  * introduced in 2894e1d76974 ("rbd: timeout watch teardown on unmap
4459922dab61SIlya Dryomov  * with mount_timeout").
4460922dab61SIlya Dryomov  */
4461922dab61SIlya Dryomov int ceph_osdc_unwatch(struct ceph_osd_client *osdc,
4462922dab61SIlya Dryomov 		      struct ceph_osd_linger_request *lreq)
4463922dab61SIlya Dryomov {
4464922dab61SIlya Dryomov 	struct ceph_options *opts = osdc->client->options;
4465922dab61SIlya Dryomov 	struct ceph_osd_request *req;
4466922dab61SIlya Dryomov 	int ret;
4467922dab61SIlya Dryomov 
4468922dab61SIlya Dryomov 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4469922dab61SIlya Dryomov 	if (!req)
4470922dab61SIlya Dryomov 		return -ENOMEM;
4471922dab61SIlya Dryomov 
4472922dab61SIlya Dryomov 	ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
4473922dab61SIlya Dryomov 	ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
447454ea0046SIlya Dryomov 	req->r_flags = CEPH_OSD_FLAG_WRITE;
44751134e091SDeepa Dinamani 	ktime_get_real_ts(&req->r_mtime);
4476922dab61SIlya Dryomov 	osd_req_op_watch_init(req, 0, lreq->linger_id,
4477922dab61SIlya Dryomov 			      CEPH_OSD_WATCH_OP_UNWATCH);
4478922dab61SIlya Dryomov 
4479922dab61SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4480922dab61SIlya Dryomov 	if (ret)
4481922dab61SIlya Dryomov 		goto out_put_req;
4482922dab61SIlya Dryomov 
4483922dab61SIlya Dryomov 	ceph_osdc_start_request(osdc, req, false);
4484922dab61SIlya Dryomov 	linger_cancel(lreq);
4485922dab61SIlya Dryomov 	linger_put(lreq);
4486922dab61SIlya Dryomov 	ret = wait_request_timeout(req, opts->mount_timeout);
4487922dab61SIlya Dryomov 
4488922dab61SIlya Dryomov out_put_req:
4489922dab61SIlya Dryomov 	ceph_osdc_put_request(req);
4490922dab61SIlya Dryomov 	return ret;
4491922dab61SIlya Dryomov }
4492922dab61SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_unwatch);
4493922dab61SIlya Dryomov 
4494922dab61SIlya Dryomov static int osd_req_op_notify_ack_init(struct ceph_osd_request *req, int which,
4495922dab61SIlya Dryomov 				      u64 notify_id, u64 cookie, void *payload,
4496922dab61SIlya Dryomov 				      size_t payload_len)
4497922dab61SIlya Dryomov {
4498922dab61SIlya Dryomov 	struct ceph_osd_req_op *op;
4499922dab61SIlya Dryomov 	struct ceph_pagelist *pl;
4500922dab61SIlya Dryomov 	int ret;
4501922dab61SIlya Dryomov 
4502922dab61SIlya Dryomov 	op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY_ACK, 0);
4503922dab61SIlya Dryomov 
4504922dab61SIlya Dryomov 	pl = kmalloc(sizeof(*pl), GFP_NOIO);
4505922dab61SIlya Dryomov 	if (!pl)
4506922dab61SIlya Dryomov 		return -ENOMEM;
4507922dab61SIlya Dryomov 
4508922dab61SIlya Dryomov 	ceph_pagelist_init(pl);
4509922dab61SIlya Dryomov 	ret = ceph_pagelist_encode_64(pl, notify_id);
4510922dab61SIlya Dryomov 	ret |= ceph_pagelist_encode_64(pl, cookie);
4511922dab61SIlya Dryomov 	if (payload) {
4512922dab61SIlya Dryomov 		ret |= ceph_pagelist_encode_32(pl, payload_len);
4513922dab61SIlya Dryomov 		ret |= ceph_pagelist_append(pl, payload, payload_len);
4514922dab61SIlya Dryomov 	} else {
4515922dab61SIlya Dryomov 		ret |= ceph_pagelist_encode_32(pl, 0);
4516922dab61SIlya Dryomov 	}
4517922dab61SIlya Dryomov 	if (ret) {
4518922dab61SIlya Dryomov 		ceph_pagelist_release(pl);
4519922dab61SIlya Dryomov 		return -ENOMEM;
4520922dab61SIlya Dryomov 	}
4521922dab61SIlya Dryomov 
4522922dab61SIlya Dryomov 	ceph_osd_data_pagelist_init(&op->notify_ack.request_data, pl);
4523922dab61SIlya Dryomov 	op->indata_len = pl->length;
4524922dab61SIlya Dryomov 	return 0;
4525922dab61SIlya Dryomov }
4526922dab61SIlya Dryomov 
4527922dab61SIlya Dryomov int ceph_osdc_notify_ack(struct ceph_osd_client *osdc,
4528922dab61SIlya Dryomov 			 struct ceph_object_id *oid,
4529922dab61SIlya Dryomov 			 struct ceph_object_locator *oloc,
4530922dab61SIlya Dryomov 			 u64 notify_id,
4531922dab61SIlya Dryomov 			 u64 cookie,
4532922dab61SIlya Dryomov 			 void *payload,
4533922dab61SIlya Dryomov 			 size_t payload_len)
4534922dab61SIlya Dryomov {
4535922dab61SIlya Dryomov 	struct ceph_osd_request *req;
4536922dab61SIlya Dryomov 	int ret;
4537922dab61SIlya Dryomov 
4538922dab61SIlya Dryomov 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4539922dab61SIlya Dryomov 	if (!req)
4540922dab61SIlya Dryomov 		return -ENOMEM;
4541922dab61SIlya Dryomov 
4542922dab61SIlya Dryomov 	ceph_oid_copy(&req->r_base_oid, oid);
4543922dab61SIlya Dryomov 	ceph_oloc_copy(&req->r_base_oloc, oloc);
4544922dab61SIlya Dryomov 	req->r_flags = CEPH_OSD_FLAG_READ;
4545922dab61SIlya Dryomov 
4546922dab61SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4547922dab61SIlya Dryomov 	if (ret)
4548922dab61SIlya Dryomov 		goto out_put_req;
4549922dab61SIlya Dryomov 
4550922dab61SIlya Dryomov 	ret = osd_req_op_notify_ack_init(req, 0, notify_id, cookie, payload,
4551922dab61SIlya Dryomov 					 payload_len);
4552922dab61SIlya Dryomov 	if (ret)
4553922dab61SIlya Dryomov 		goto out_put_req;
4554922dab61SIlya Dryomov 
4555922dab61SIlya Dryomov 	ceph_osdc_start_request(osdc, req, false);
4556922dab61SIlya Dryomov 	ret = ceph_osdc_wait_request(osdc, req);
4557922dab61SIlya Dryomov 
4558922dab61SIlya Dryomov out_put_req:
4559922dab61SIlya Dryomov 	ceph_osdc_put_request(req);
4560922dab61SIlya Dryomov 	return ret;
4561922dab61SIlya Dryomov }
4562922dab61SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_notify_ack);
4563922dab61SIlya Dryomov 
456419079203SIlya Dryomov static int osd_req_op_notify_init(struct ceph_osd_request *req, int which,
456519079203SIlya Dryomov 				  u64 cookie, u32 prot_ver, u32 timeout,
456619079203SIlya Dryomov 				  void *payload, size_t payload_len)
456719079203SIlya Dryomov {
456819079203SIlya Dryomov 	struct ceph_osd_req_op *op;
456919079203SIlya Dryomov 	struct ceph_pagelist *pl;
457019079203SIlya Dryomov 	int ret;
457119079203SIlya Dryomov 
457219079203SIlya Dryomov 	op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY, 0);
457319079203SIlya Dryomov 	op->notify.cookie = cookie;
457419079203SIlya Dryomov 
457519079203SIlya Dryomov 	pl = kmalloc(sizeof(*pl), GFP_NOIO);
457619079203SIlya Dryomov 	if (!pl)
457719079203SIlya Dryomov 		return -ENOMEM;
457819079203SIlya Dryomov 
457919079203SIlya Dryomov 	ceph_pagelist_init(pl);
458019079203SIlya Dryomov 	ret = ceph_pagelist_encode_32(pl, 1); /* prot_ver */
458119079203SIlya Dryomov 	ret |= ceph_pagelist_encode_32(pl, timeout);
458219079203SIlya Dryomov 	ret |= ceph_pagelist_encode_32(pl, payload_len);
458319079203SIlya Dryomov 	ret |= ceph_pagelist_append(pl, payload, payload_len);
458419079203SIlya Dryomov 	if (ret) {
458519079203SIlya Dryomov 		ceph_pagelist_release(pl);
458619079203SIlya Dryomov 		return -ENOMEM;
458719079203SIlya Dryomov 	}
458819079203SIlya Dryomov 
458919079203SIlya Dryomov 	ceph_osd_data_pagelist_init(&op->notify.request_data, pl);
459019079203SIlya Dryomov 	op->indata_len = pl->length;
459119079203SIlya Dryomov 	return 0;
459219079203SIlya Dryomov }
459319079203SIlya Dryomov 
459419079203SIlya Dryomov /*
459519079203SIlya Dryomov  * @timeout: in seconds
459619079203SIlya Dryomov  *
459719079203SIlya Dryomov  * @preply_{pages,len} are initialized both on success and error.
459819079203SIlya Dryomov  * The caller is responsible for:
459919079203SIlya Dryomov  *
460019079203SIlya Dryomov  *     ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len))
460119079203SIlya Dryomov  */
460219079203SIlya Dryomov int ceph_osdc_notify(struct ceph_osd_client *osdc,
460319079203SIlya Dryomov 		     struct ceph_object_id *oid,
460419079203SIlya Dryomov 		     struct ceph_object_locator *oloc,
460519079203SIlya Dryomov 		     void *payload,
460619079203SIlya Dryomov 		     size_t payload_len,
460719079203SIlya Dryomov 		     u32 timeout,
460819079203SIlya Dryomov 		     struct page ***preply_pages,
460919079203SIlya Dryomov 		     size_t *preply_len)
461019079203SIlya Dryomov {
461119079203SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
461219079203SIlya Dryomov 	struct page **pages;
461319079203SIlya Dryomov 	int ret;
461419079203SIlya Dryomov 
461519079203SIlya Dryomov 	WARN_ON(!timeout);
461619079203SIlya Dryomov 	if (preply_pages) {
461719079203SIlya Dryomov 		*preply_pages = NULL;
461819079203SIlya Dryomov 		*preply_len = 0;
461919079203SIlya Dryomov 	}
462019079203SIlya Dryomov 
462119079203SIlya Dryomov 	lreq = linger_alloc(osdc);
462219079203SIlya Dryomov 	if (!lreq)
462319079203SIlya Dryomov 		return -ENOMEM;
462419079203SIlya Dryomov 
462519079203SIlya Dryomov 	lreq->preply_pages = preply_pages;
462619079203SIlya Dryomov 	lreq->preply_len = preply_len;
462719079203SIlya Dryomov 
462819079203SIlya Dryomov 	ceph_oid_copy(&lreq->t.base_oid, oid);
462919079203SIlya Dryomov 	ceph_oloc_copy(&lreq->t.base_oloc, oloc);
463019079203SIlya Dryomov 	lreq->t.flags = CEPH_OSD_FLAG_READ;
463119079203SIlya Dryomov 
463219079203SIlya Dryomov 	lreq->reg_req = alloc_linger_request(lreq);
463319079203SIlya Dryomov 	if (!lreq->reg_req) {
463419079203SIlya Dryomov 		ret = -ENOMEM;
463519079203SIlya Dryomov 		goto out_put_lreq;
463619079203SIlya Dryomov 	}
463719079203SIlya Dryomov 
463819079203SIlya Dryomov 	/* for notify_id */
463919079203SIlya Dryomov 	pages = ceph_alloc_page_vector(1, GFP_NOIO);
464019079203SIlya Dryomov 	if (IS_ERR(pages)) {
464119079203SIlya Dryomov 		ret = PTR_ERR(pages);
464219079203SIlya Dryomov 		goto out_put_lreq;
464319079203SIlya Dryomov 	}
464419079203SIlya Dryomov 
464519079203SIlya Dryomov 	down_write(&osdc->lock);
464619079203SIlya Dryomov 	linger_register(lreq); /* before osd_req_op_* */
464719079203SIlya Dryomov 	ret = osd_req_op_notify_init(lreq->reg_req, 0, lreq->linger_id, 1,
464819079203SIlya Dryomov 				     timeout, payload, payload_len);
464919079203SIlya Dryomov 	if (ret) {
465019079203SIlya Dryomov 		linger_unregister(lreq);
465119079203SIlya Dryomov 		up_write(&osdc->lock);
465219079203SIlya Dryomov 		ceph_release_page_vector(pages, 1);
465319079203SIlya Dryomov 		goto out_put_lreq;
465419079203SIlya Dryomov 	}
465519079203SIlya Dryomov 	ceph_osd_data_pages_init(osd_req_op_data(lreq->reg_req, 0, notify,
465619079203SIlya Dryomov 						 response_data),
465719079203SIlya Dryomov 				 pages, PAGE_SIZE, 0, false, true);
465819079203SIlya Dryomov 	linger_submit(lreq);
465919079203SIlya Dryomov 	up_write(&osdc->lock);
466019079203SIlya Dryomov 
466119079203SIlya Dryomov 	ret = linger_reg_commit_wait(lreq);
466219079203SIlya Dryomov 	if (!ret)
466319079203SIlya Dryomov 		ret = linger_notify_finish_wait(lreq);
466419079203SIlya Dryomov 	else
466519079203SIlya Dryomov 		dout("lreq %p failed to initiate notify %d\n", lreq, ret);
466619079203SIlya Dryomov 
466719079203SIlya Dryomov 	linger_cancel(lreq);
466819079203SIlya Dryomov out_put_lreq:
466919079203SIlya Dryomov 	linger_put(lreq);
467019079203SIlya Dryomov 	return ret;
467119079203SIlya Dryomov }
467219079203SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_notify);
467319079203SIlya Dryomov 
46743d14c5d2SYehuda Sadeh /*
4675b07d3c4bSIlya Dryomov  * Return the number of milliseconds since the watch was last
4676b07d3c4bSIlya Dryomov  * confirmed, or an error.  If there is an error, the watch is no
4677b07d3c4bSIlya Dryomov  * longer valid, and should be destroyed with ceph_osdc_unwatch().
4678b07d3c4bSIlya Dryomov  */
4679b07d3c4bSIlya Dryomov int ceph_osdc_watch_check(struct ceph_osd_client *osdc,
4680b07d3c4bSIlya Dryomov 			  struct ceph_osd_linger_request *lreq)
4681b07d3c4bSIlya Dryomov {
4682b07d3c4bSIlya Dryomov 	unsigned long stamp, age;
4683b07d3c4bSIlya Dryomov 	int ret;
4684b07d3c4bSIlya Dryomov 
4685b07d3c4bSIlya Dryomov 	down_read(&osdc->lock);
4686b07d3c4bSIlya Dryomov 	mutex_lock(&lreq->lock);
4687b07d3c4bSIlya Dryomov 	stamp = lreq->watch_valid_thru;
4688b07d3c4bSIlya Dryomov 	if (!list_empty(&lreq->pending_lworks)) {
4689b07d3c4bSIlya Dryomov 		struct linger_work *lwork =
4690b07d3c4bSIlya Dryomov 		    list_first_entry(&lreq->pending_lworks,
4691b07d3c4bSIlya Dryomov 				     struct linger_work,
4692b07d3c4bSIlya Dryomov 				     pending_item);
4693b07d3c4bSIlya Dryomov 
4694b07d3c4bSIlya Dryomov 		if (time_before(lwork->queued_stamp, stamp))
4695b07d3c4bSIlya Dryomov 			stamp = lwork->queued_stamp;
4696b07d3c4bSIlya Dryomov 	}
4697b07d3c4bSIlya Dryomov 	age = jiffies - stamp;
4698b07d3c4bSIlya Dryomov 	dout("%s lreq %p linger_id %llu age %lu last_error %d\n", __func__,
4699b07d3c4bSIlya Dryomov 	     lreq, lreq->linger_id, age, lreq->last_error);
4700b07d3c4bSIlya Dryomov 	/* we are truncating to msecs, so return a safe upper bound */
4701b07d3c4bSIlya Dryomov 	ret = lreq->last_error ?: 1 + jiffies_to_msecs(age);
4702b07d3c4bSIlya Dryomov 
4703b07d3c4bSIlya Dryomov 	mutex_unlock(&lreq->lock);
4704b07d3c4bSIlya Dryomov 	up_read(&osdc->lock);
4705b07d3c4bSIlya Dryomov 	return ret;
4706b07d3c4bSIlya Dryomov }
4707b07d3c4bSIlya Dryomov 
4708a4ed38d7SDouglas Fuller static int decode_watcher(void **p, void *end, struct ceph_watch_item *item)
4709a4ed38d7SDouglas Fuller {
4710a4ed38d7SDouglas Fuller 	u8 struct_v;
4711a4ed38d7SDouglas Fuller 	u32 struct_len;
4712a4ed38d7SDouglas Fuller 	int ret;
4713a4ed38d7SDouglas Fuller 
4714a4ed38d7SDouglas Fuller 	ret = ceph_start_decoding(p, end, 2, "watch_item_t",
4715a4ed38d7SDouglas Fuller 				  &struct_v, &struct_len);
4716a4ed38d7SDouglas Fuller 	if (ret)
4717a4ed38d7SDouglas Fuller 		return ret;
4718a4ed38d7SDouglas Fuller 
4719a4ed38d7SDouglas Fuller 	ceph_decode_copy(p, &item->name, sizeof(item->name));
4720a4ed38d7SDouglas Fuller 	item->cookie = ceph_decode_64(p);
4721a4ed38d7SDouglas Fuller 	*p += 4; /* skip timeout_seconds */
4722a4ed38d7SDouglas Fuller 	if (struct_v >= 2) {
4723a4ed38d7SDouglas Fuller 		ceph_decode_copy(p, &item->addr, sizeof(item->addr));
4724a4ed38d7SDouglas Fuller 		ceph_decode_addr(&item->addr);
4725a4ed38d7SDouglas Fuller 	}
4726a4ed38d7SDouglas Fuller 
4727a4ed38d7SDouglas Fuller 	dout("%s %s%llu cookie %llu addr %s\n", __func__,
4728a4ed38d7SDouglas Fuller 	     ENTITY_NAME(item->name), item->cookie,
4729a4ed38d7SDouglas Fuller 	     ceph_pr_addr(&item->addr.in_addr));
4730a4ed38d7SDouglas Fuller 	return 0;
4731a4ed38d7SDouglas Fuller }
4732a4ed38d7SDouglas Fuller 
4733a4ed38d7SDouglas Fuller static int decode_watchers(void **p, void *end,
4734a4ed38d7SDouglas Fuller 			   struct ceph_watch_item **watchers,
4735a4ed38d7SDouglas Fuller 			   u32 *num_watchers)
4736a4ed38d7SDouglas Fuller {
4737a4ed38d7SDouglas Fuller 	u8 struct_v;
4738a4ed38d7SDouglas Fuller 	u32 struct_len;
4739a4ed38d7SDouglas Fuller 	int i;
4740a4ed38d7SDouglas Fuller 	int ret;
4741a4ed38d7SDouglas Fuller 
4742a4ed38d7SDouglas Fuller 	ret = ceph_start_decoding(p, end, 1, "obj_list_watch_response_t",
4743a4ed38d7SDouglas Fuller 				  &struct_v, &struct_len);
4744a4ed38d7SDouglas Fuller 	if (ret)
4745a4ed38d7SDouglas Fuller 		return ret;
4746a4ed38d7SDouglas Fuller 
4747a4ed38d7SDouglas Fuller 	*num_watchers = ceph_decode_32(p);
4748a4ed38d7SDouglas Fuller 	*watchers = kcalloc(*num_watchers, sizeof(**watchers), GFP_NOIO);
4749a4ed38d7SDouglas Fuller 	if (!*watchers)
4750a4ed38d7SDouglas Fuller 		return -ENOMEM;
4751a4ed38d7SDouglas Fuller 
4752a4ed38d7SDouglas Fuller 	for (i = 0; i < *num_watchers; i++) {
4753a4ed38d7SDouglas Fuller 		ret = decode_watcher(p, end, *watchers + i);
4754a4ed38d7SDouglas Fuller 		if (ret) {
4755a4ed38d7SDouglas Fuller 			kfree(*watchers);
4756a4ed38d7SDouglas Fuller 			return ret;
4757a4ed38d7SDouglas Fuller 		}
4758a4ed38d7SDouglas Fuller 	}
4759a4ed38d7SDouglas Fuller 
4760a4ed38d7SDouglas Fuller 	return 0;
4761a4ed38d7SDouglas Fuller }
4762a4ed38d7SDouglas Fuller 
4763a4ed38d7SDouglas Fuller /*
4764a4ed38d7SDouglas Fuller  * On success, the caller is responsible for:
4765a4ed38d7SDouglas Fuller  *
4766a4ed38d7SDouglas Fuller  *     kfree(watchers);
4767a4ed38d7SDouglas Fuller  */
4768a4ed38d7SDouglas Fuller int ceph_osdc_list_watchers(struct ceph_osd_client *osdc,
4769a4ed38d7SDouglas Fuller 			    struct ceph_object_id *oid,
4770a4ed38d7SDouglas Fuller 			    struct ceph_object_locator *oloc,
4771a4ed38d7SDouglas Fuller 			    struct ceph_watch_item **watchers,
4772a4ed38d7SDouglas Fuller 			    u32 *num_watchers)
4773a4ed38d7SDouglas Fuller {
4774a4ed38d7SDouglas Fuller 	struct ceph_osd_request *req;
4775a4ed38d7SDouglas Fuller 	struct page **pages;
4776a4ed38d7SDouglas Fuller 	int ret;
4777a4ed38d7SDouglas Fuller 
4778a4ed38d7SDouglas Fuller 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4779a4ed38d7SDouglas Fuller 	if (!req)
4780a4ed38d7SDouglas Fuller 		return -ENOMEM;
4781a4ed38d7SDouglas Fuller 
4782a4ed38d7SDouglas Fuller 	ceph_oid_copy(&req->r_base_oid, oid);
4783a4ed38d7SDouglas Fuller 	ceph_oloc_copy(&req->r_base_oloc, oloc);
4784a4ed38d7SDouglas Fuller 	req->r_flags = CEPH_OSD_FLAG_READ;
4785a4ed38d7SDouglas Fuller 
4786a4ed38d7SDouglas Fuller 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4787a4ed38d7SDouglas Fuller 	if (ret)
4788a4ed38d7SDouglas Fuller 		goto out_put_req;
4789a4ed38d7SDouglas Fuller 
4790a4ed38d7SDouglas Fuller 	pages = ceph_alloc_page_vector(1, GFP_NOIO);
4791a4ed38d7SDouglas Fuller 	if (IS_ERR(pages)) {
4792a4ed38d7SDouglas Fuller 		ret = PTR_ERR(pages);
4793a4ed38d7SDouglas Fuller 		goto out_put_req;
4794a4ed38d7SDouglas Fuller 	}
4795a4ed38d7SDouglas Fuller 
4796a4ed38d7SDouglas Fuller 	osd_req_op_init(req, 0, CEPH_OSD_OP_LIST_WATCHERS, 0);
4797a4ed38d7SDouglas Fuller 	ceph_osd_data_pages_init(osd_req_op_data(req, 0, list_watchers,
4798a4ed38d7SDouglas Fuller 						 response_data),
4799a4ed38d7SDouglas Fuller 				 pages, PAGE_SIZE, 0, false, true);
4800a4ed38d7SDouglas Fuller 
4801a4ed38d7SDouglas Fuller 	ceph_osdc_start_request(osdc, req, false);
4802a4ed38d7SDouglas Fuller 	ret = ceph_osdc_wait_request(osdc, req);
4803a4ed38d7SDouglas Fuller 	if (ret >= 0) {
4804a4ed38d7SDouglas Fuller 		void *p = page_address(pages[0]);
4805a4ed38d7SDouglas Fuller 		void *const end = p + req->r_ops[0].outdata_len;
4806a4ed38d7SDouglas Fuller 
4807a4ed38d7SDouglas Fuller 		ret = decode_watchers(&p, end, watchers, num_watchers);
4808a4ed38d7SDouglas Fuller 	}
4809a4ed38d7SDouglas Fuller 
4810a4ed38d7SDouglas Fuller out_put_req:
4811a4ed38d7SDouglas Fuller 	ceph_osdc_put_request(req);
4812a4ed38d7SDouglas Fuller 	return ret;
4813a4ed38d7SDouglas Fuller }
4814a4ed38d7SDouglas Fuller EXPORT_SYMBOL(ceph_osdc_list_watchers);
4815a4ed38d7SDouglas Fuller 
4816b07d3c4bSIlya Dryomov /*
4817dd935f44SJosh Durgin  * Call all pending notify callbacks - for use after a watch is
4818dd935f44SJosh Durgin  * unregistered, to make sure no more callbacks for it will be invoked
4819dd935f44SJosh Durgin  */
4820f6479449Sstephen hemminger void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
4821dd935f44SJosh Durgin {
482299d16943SIlya Dryomov 	dout("%s osdc %p\n", __func__, osdc);
4823dd935f44SJosh Durgin 	flush_workqueue(osdc->notify_wq);
4824dd935f44SJosh Durgin }
4825dd935f44SJosh Durgin EXPORT_SYMBOL(ceph_osdc_flush_notifies);
4826dd935f44SJosh Durgin 
48277cca78c9SIlya Dryomov void ceph_osdc_maybe_request_map(struct ceph_osd_client *osdc)
48287cca78c9SIlya Dryomov {
48297cca78c9SIlya Dryomov 	down_read(&osdc->lock);
48307cca78c9SIlya Dryomov 	maybe_request_map(osdc);
48317cca78c9SIlya Dryomov 	up_read(&osdc->lock);
48327cca78c9SIlya Dryomov }
48337cca78c9SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_maybe_request_map);
4834dd935f44SJosh Durgin 
4835dd935f44SJosh Durgin /*
4836428a7158SDouglas Fuller  * Execute an OSD class method on an object.
4837428a7158SDouglas Fuller  *
4838428a7158SDouglas Fuller  * @flags: CEPH_OSD_FLAG_*
48392544a020SIlya Dryomov  * @resp_len: in/out param for reply length
4840428a7158SDouglas Fuller  */
4841428a7158SDouglas Fuller int ceph_osdc_call(struct ceph_osd_client *osdc,
4842428a7158SDouglas Fuller 		   struct ceph_object_id *oid,
4843428a7158SDouglas Fuller 		   struct ceph_object_locator *oloc,
4844428a7158SDouglas Fuller 		   const char *class, const char *method,
4845428a7158SDouglas Fuller 		   unsigned int flags,
4846428a7158SDouglas Fuller 		   struct page *req_page, size_t req_len,
4847428a7158SDouglas Fuller 		   struct page *resp_page, size_t *resp_len)
4848428a7158SDouglas Fuller {
4849428a7158SDouglas Fuller 	struct ceph_osd_request *req;
4850428a7158SDouglas Fuller 	int ret;
4851428a7158SDouglas Fuller 
48522544a020SIlya Dryomov 	if (req_len > PAGE_SIZE || (resp_page && *resp_len > PAGE_SIZE))
48532544a020SIlya Dryomov 		return -E2BIG;
48542544a020SIlya Dryomov 
4855428a7158SDouglas Fuller 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4856428a7158SDouglas Fuller 	if (!req)
4857428a7158SDouglas Fuller 		return -ENOMEM;
4858428a7158SDouglas Fuller 
4859428a7158SDouglas Fuller 	ceph_oid_copy(&req->r_base_oid, oid);
4860428a7158SDouglas Fuller 	ceph_oloc_copy(&req->r_base_oloc, oloc);
4861428a7158SDouglas Fuller 	req->r_flags = flags;
4862428a7158SDouglas Fuller 
4863428a7158SDouglas Fuller 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4864428a7158SDouglas Fuller 	if (ret)
4865428a7158SDouglas Fuller 		goto out_put_req;
4866428a7158SDouglas Fuller 
4867428a7158SDouglas Fuller 	osd_req_op_cls_init(req, 0, CEPH_OSD_OP_CALL, class, method);
4868428a7158SDouglas Fuller 	if (req_page)
4869428a7158SDouglas Fuller 		osd_req_op_cls_request_data_pages(req, 0, &req_page, req_len,
4870428a7158SDouglas Fuller 						  0, false, false);
4871428a7158SDouglas Fuller 	if (resp_page)
4872428a7158SDouglas Fuller 		osd_req_op_cls_response_data_pages(req, 0, &resp_page,
48732544a020SIlya Dryomov 						   *resp_len, 0, false, false);
4874428a7158SDouglas Fuller 
4875428a7158SDouglas Fuller 	ceph_osdc_start_request(osdc, req, false);
4876428a7158SDouglas Fuller 	ret = ceph_osdc_wait_request(osdc, req);
4877428a7158SDouglas Fuller 	if (ret >= 0) {
4878428a7158SDouglas Fuller 		ret = req->r_ops[0].rval;
4879428a7158SDouglas Fuller 		if (resp_page)
4880428a7158SDouglas Fuller 			*resp_len = req->r_ops[0].outdata_len;
4881428a7158SDouglas Fuller 	}
4882428a7158SDouglas Fuller 
4883428a7158SDouglas Fuller out_put_req:
4884428a7158SDouglas Fuller 	ceph_osdc_put_request(req);
4885428a7158SDouglas Fuller 	return ret;
4886428a7158SDouglas Fuller }
4887428a7158SDouglas Fuller EXPORT_SYMBOL(ceph_osdc_call);
4888428a7158SDouglas Fuller 
4889428a7158SDouglas Fuller /*
48903d14c5d2SYehuda Sadeh  * init, shutdown
48913d14c5d2SYehuda Sadeh  */
48923d14c5d2SYehuda Sadeh int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
48933d14c5d2SYehuda Sadeh {
48943d14c5d2SYehuda Sadeh 	int err;
48953d14c5d2SYehuda Sadeh 
48963d14c5d2SYehuda Sadeh 	dout("init\n");
48973d14c5d2SYehuda Sadeh 	osdc->client = client;
48985aea3dcdSIlya Dryomov 	init_rwsem(&osdc->lock);
48993d14c5d2SYehuda Sadeh 	osdc->osds = RB_ROOT;
49003d14c5d2SYehuda Sadeh 	INIT_LIST_HEAD(&osdc->osd_lru);
49019dd2845cSIlya Dryomov 	spin_lock_init(&osdc->osd_lru_lock);
49025aea3dcdSIlya Dryomov 	osd_init(&osdc->homeless_osd);
49035aea3dcdSIlya Dryomov 	osdc->homeless_osd.o_osdc = osdc;
49045aea3dcdSIlya Dryomov 	osdc->homeless_osd.o_osd = CEPH_HOMELESS_OSD;
4905264048afSIlya Dryomov 	osdc->last_linger_id = CEPH_LINGER_ID_START;
4906922dab61SIlya Dryomov 	osdc->linger_requests = RB_ROOT;
49074609245eSIlya Dryomov 	osdc->map_checks = RB_ROOT;
49084609245eSIlya Dryomov 	osdc->linger_map_checks = RB_ROOT;
49093d14c5d2SYehuda Sadeh 	INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
49103d14c5d2SYehuda Sadeh 	INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
49113d14c5d2SYehuda Sadeh 
49123d14c5d2SYehuda Sadeh 	err = -ENOMEM;
4913e5253a7bSIlya Dryomov 	osdc->osdmap = ceph_osdmap_alloc();
4914e5253a7bSIlya Dryomov 	if (!osdc->osdmap)
4915e5253a7bSIlya Dryomov 		goto out;
4916e5253a7bSIlya Dryomov 
49179e767adbSIlya Dryomov 	osdc->req_mempool = mempool_create_slab_pool(10,
49189e767adbSIlya Dryomov 						     ceph_osd_request_cache);
49193d14c5d2SYehuda Sadeh 	if (!osdc->req_mempool)
4920e5253a7bSIlya Dryomov 		goto out_map;
49213d14c5d2SYehuda Sadeh 
4922d50b409fSSage Weil 	err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
4923711da55dSIlya Dryomov 				PAGE_SIZE, 10, true, "osd_op");
49243d14c5d2SYehuda Sadeh 	if (err < 0)
49253d14c5d2SYehuda Sadeh 		goto out_mempool;
4926d50b409fSSage Weil 	err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
4927711da55dSIlya Dryomov 				PAGE_SIZE, 10, true, "osd_op_reply");
49283d14c5d2SYehuda Sadeh 	if (err < 0)
49293d14c5d2SYehuda Sadeh 		goto out_msgpool;
4930a40c4f10SYehuda Sadeh 
4931dbcae088SDan Carpenter 	err = -ENOMEM;
4932a40c4f10SYehuda Sadeh 	osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
4933dbcae088SDan Carpenter 	if (!osdc->notify_wq)
4934c172ec5cSIlya Dryomov 		goto out_msgpool_reply;
4935c172ec5cSIlya Dryomov 
4936fbca9635SIlya Dryomov 	schedule_delayed_work(&osdc->timeout_work,
4937fbca9635SIlya Dryomov 			      osdc->client->options->osd_keepalive_timeout);
4938b37ee1b9SIlya Dryomov 	schedule_delayed_work(&osdc->osds_timeout_work,
4939b37ee1b9SIlya Dryomov 	    round_jiffies_relative(osdc->client->options->osd_idle_ttl));
4940b37ee1b9SIlya Dryomov 
49413d14c5d2SYehuda Sadeh 	return 0;
49423d14c5d2SYehuda Sadeh 
4943c172ec5cSIlya Dryomov out_msgpool_reply:
4944c172ec5cSIlya Dryomov 	ceph_msgpool_destroy(&osdc->msgpool_op_reply);
49453d14c5d2SYehuda Sadeh out_msgpool:
49463d14c5d2SYehuda Sadeh 	ceph_msgpool_destroy(&osdc->msgpool_op);
49473d14c5d2SYehuda Sadeh out_mempool:
49483d14c5d2SYehuda Sadeh 	mempool_destroy(osdc->req_mempool);
4949e5253a7bSIlya Dryomov out_map:
4950e5253a7bSIlya Dryomov 	ceph_osdmap_destroy(osdc->osdmap);
49513d14c5d2SYehuda Sadeh out:
49523d14c5d2SYehuda Sadeh 	return err;
49533d14c5d2SYehuda Sadeh }
49543d14c5d2SYehuda Sadeh 
49553d14c5d2SYehuda Sadeh void ceph_osdc_stop(struct ceph_osd_client *osdc)
49563d14c5d2SYehuda Sadeh {
4957a40c4f10SYehuda Sadeh 	flush_workqueue(osdc->notify_wq);
4958a40c4f10SYehuda Sadeh 	destroy_workqueue(osdc->notify_wq);
49593d14c5d2SYehuda Sadeh 	cancel_delayed_work_sync(&osdc->timeout_work);
49603d14c5d2SYehuda Sadeh 	cancel_delayed_work_sync(&osdc->osds_timeout_work);
496142a2c09fSIlya Dryomov 
49625aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
496342a2c09fSIlya Dryomov 	while (!RB_EMPTY_ROOT(&osdc->osds)) {
496442a2c09fSIlya Dryomov 		struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
496542a2c09fSIlya Dryomov 						struct ceph_osd, o_node);
49665aea3dcdSIlya Dryomov 		close_osd(osd);
496742a2c09fSIlya Dryomov 	}
49685aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
496902113a0fSElena Reshetova 	WARN_ON(refcount_read(&osdc->homeless_osd.o_ref) != 1);
49705aea3dcdSIlya Dryomov 	osd_cleanup(&osdc->homeless_osd);
49715aea3dcdSIlya Dryomov 
49725aea3dcdSIlya Dryomov 	WARN_ON(!list_empty(&osdc->osd_lru));
4973922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_requests));
49744609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osdc->map_checks));
49754609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_map_checks));
49765aea3dcdSIlya Dryomov 	WARN_ON(atomic_read(&osdc->num_requests));
49775aea3dcdSIlya Dryomov 	WARN_ON(atomic_read(&osdc->num_homeless));
497842a2c09fSIlya Dryomov 
49793d14c5d2SYehuda Sadeh 	ceph_osdmap_destroy(osdc->osdmap);
49803d14c5d2SYehuda Sadeh 	mempool_destroy(osdc->req_mempool);
49813d14c5d2SYehuda Sadeh 	ceph_msgpool_destroy(&osdc->msgpool_op);
49823d14c5d2SYehuda Sadeh 	ceph_msgpool_destroy(&osdc->msgpool_op_reply);
49833d14c5d2SYehuda Sadeh }
49843d14c5d2SYehuda Sadeh 
49853d14c5d2SYehuda Sadeh /*
49863d14c5d2SYehuda Sadeh  * Read some contiguous pages.  If we cross a stripe boundary, shorten
49873d14c5d2SYehuda Sadeh  * *plen.  Return number of bytes read, or error.
49883d14c5d2SYehuda Sadeh  */
49893d14c5d2SYehuda Sadeh int ceph_osdc_readpages(struct ceph_osd_client *osdc,
49903d14c5d2SYehuda Sadeh 			struct ceph_vino vino, struct ceph_file_layout *layout,
49913d14c5d2SYehuda Sadeh 			u64 off, u64 *plen,
49923d14c5d2SYehuda Sadeh 			u32 truncate_seq, u64 truncate_size,
4993b7495fc2SSage Weil 			struct page **pages, int num_pages, int page_align)
49943d14c5d2SYehuda Sadeh {
49953d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
49963d14c5d2SYehuda Sadeh 	int rc = 0;
49973d14c5d2SYehuda Sadeh 
49983d14c5d2SYehuda Sadeh 	dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
49993d14c5d2SYehuda Sadeh 	     vino.snap, off, *plen);
5000715e4cd4SYan, Zheng 	req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
50013d14c5d2SYehuda Sadeh 				    CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
5002acead002SAlex Elder 				    NULL, truncate_seq, truncate_size,
5003153e5167SAlex Elder 				    false);
50046816282dSSage Weil 	if (IS_ERR(req))
50056816282dSSage Weil 		return PTR_ERR(req);
50063d14c5d2SYehuda Sadeh 
50073d14c5d2SYehuda Sadeh 	/* it may be a short read due to an object boundary */
5008406e2c9fSAlex Elder 	osd_req_op_extent_osd_data_pages(req, 0,
5009a4ce40a9SAlex Elder 				pages, *plen, page_align, false, false);
50103d14c5d2SYehuda Sadeh 
5011e0c59487SAlex Elder 	dout("readpages  final extent is %llu~%llu (%llu bytes align %d)\n",
501243bfe5deSAlex Elder 	     off, *plen, *plen, page_align);
50133d14c5d2SYehuda Sadeh 
50143d14c5d2SYehuda Sadeh 	rc = ceph_osdc_start_request(osdc, req, false);
50153d14c5d2SYehuda Sadeh 	if (!rc)
50163d14c5d2SYehuda Sadeh 		rc = ceph_osdc_wait_request(osdc, req);
50173d14c5d2SYehuda Sadeh 
50183d14c5d2SYehuda Sadeh 	ceph_osdc_put_request(req);
50193d14c5d2SYehuda Sadeh 	dout("readpages result %d\n", rc);
50203d14c5d2SYehuda Sadeh 	return rc;
50213d14c5d2SYehuda Sadeh }
50223d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_readpages);
50233d14c5d2SYehuda Sadeh 
50243d14c5d2SYehuda Sadeh /*
50253d14c5d2SYehuda Sadeh  * do a synchronous write on N pages
50263d14c5d2SYehuda Sadeh  */
50273d14c5d2SYehuda Sadeh int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
50283d14c5d2SYehuda Sadeh 			 struct ceph_file_layout *layout,
50293d14c5d2SYehuda Sadeh 			 struct ceph_snap_context *snapc,
50303d14c5d2SYehuda Sadeh 			 u64 off, u64 len,
50313d14c5d2SYehuda Sadeh 			 u32 truncate_seq, u64 truncate_size,
50323d14c5d2SYehuda Sadeh 			 struct timespec *mtime,
503324808826SAlex Elder 			 struct page **pages, int num_pages)
50343d14c5d2SYehuda Sadeh {
50353d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
50363d14c5d2SYehuda Sadeh 	int rc = 0;
5037b7495fc2SSage Weil 	int page_align = off & ~PAGE_MASK;
50383d14c5d2SYehuda Sadeh 
5039715e4cd4SYan, Zheng 	req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
504054ea0046SIlya Dryomov 				    CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
5041acead002SAlex Elder 				    snapc, truncate_seq, truncate_size,
5042153e5167SAlex Elder 				    true);
50436816282dSSage Weil 	if (IS_ERR(req))
50446816282dSSage Weil 		return PTR_ERR(req);
50453d14c5d2SYehuda Sadeh 
50463d14c5d2SYehuda Sadeh 	/* it may be a short write due to an object boundary */
5047406e2c9fSAlex Elder 	osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
504843bfe5deSAlex Elder 				false, false);
504943bfe5deSAlex Elder 	dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
50503d14c5d2SYehuda Sadeh 
5051bb873b53SIlya Dryomov 	req->r_mtime = *mtime;
505287f979d3SAlex Elder 	rc = ceph_osdc_start_request(osdc, req, true);
50533d14c5d2SYehuda Sadeh 	if (!rc)
50543d14c5d2SYehuda Sadeh 		rc = ceph_osdc_wait_request(osdc, req);
50553d14c5d2SYehuda Sadeh 
50563d14c5d2SYehuda Sadeh 	ceph_osdc_put_request(req);
50573d14c5d2SYehuda Sadeh 	if (rc == 0)
50583d14c5d2SYehuda Sadeh 		rc = len;
50593d14c5d2SYehuda Sadeh 	dout("writepages result %d\n", rc);
50603d14c5d2SYehuda Sadeh 	return rc;
50613d14c5d2SYehuda Sadeh }
50623d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_writepages);
50633d14c5d2SYehuda Sadeh 
50645522ae0bSAlex Elder int ceph_osdc_setup(void)
50655522ae0bSAlex Elder {
50663f1af42aSIlya Dryomov 	size_t size = sizeof(struct ceph_osd_request) +
50673f1af42aSIlya Dryomov 	    CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
50683f1af42aSIlya Dryomov 
50695522ae0bSAlex Elder 	BUG_ON(ceph_osd_request_cache);
50703f1af42aSIlya Dryomov 	ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
50713f1af42aSIlya Dryomov 						   0, 0, NULL);
50725522ae0bSAlex Elder 
50735522ae0bSAlex Elder 	return ceph_osd_request_cache ? 0 : -ENOMEM;
50745522ae0bSAlex Elder }
50755522ae0bSAlex Elder EXPORT_SYMBOL(ceph_osdc_setup);
50765522ae0bSAlex Elder 
50775522ae0bSAlex Elder void ceph_osdc_cleanup(void)
50785522ae0bSAlex Elder {
50795522ae0bSAlex Elder 	BUG_ON(!ceph_osd_request_cache);
50805522ae0bSAlex Elder 	kmem_cache_destroy(ceph_osd_request_cache);
50815522ae0bSAlex Elder 	ceph_osd_request_cache = NULL;
50825522ae0bSAlex Elder }
50835522ae0bSAlex Elder EXPORT_SYMBOL(ceph_osdc_cleanup);
50845522ae0bSAlex Elder 
50853d14c5d2SYehuda Sadeh /*
50863d14c5d2SYehuda Sadeh  * handle incoming message
50873d14c5d2SYehuda Sadeh  */
50883d14c5d2SYehuda Sadeh static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
50893d14c5d2SYehuda Sadeh {
50903d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
50915aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
50923d14c5d2SYehuda Sadeh 	int type = le16_to_cpu(msg->hdr.type);
50933d14c5d2SYehuda Sadeh 
50943d14c5d2SYehuda Sadeh 	switch (type) {
50953d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_MAP:
50963d14c5d2SYehuda Sadeh 		ceph_osdc_handle_map(osdc, msg);
50973d14c5d2SYehuda Sadeh 		break;
50983d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_OPREPLY:
50995aea3dcdSIlya Dryomov 		handle_reply(osd, msg);
51003d14c5d2SYehuda Sadeh 		break;
5101a02a946dSIlya Dryomov 	case CEPH_MSG_OSD_BACKOFF:
5102a02a946dSIlya Dryomov 		handle_backoff(osd, msg);
5103a02a946dSIlya Dryomov 		break;
5104a40c4f10SYehuda Sadeh 	case CEPH_MSG_WATCH_NOTIFY:
5105a40c4f10SYehuda Sadeh 		handle_watch_notify(osdc, msg);
5106a40c4f10SYehuda Sadeh 		break;
51073d14c5d2SYehuda Sadeh 
51083d14c5d2SYehuda Sadeh 	default:
51093d14c5d2SYehuda Sadeh 		pr_err("received unknown message type %d %s\n", type,
51103d14c5d2SYehuda Sadeh 		       ceph_msg_type_name(type));
51113d14c5d2SYehuda Sadeh 	}
51125aea3dcdSIlya Dryomov 
51133d14c5d2SYehuda Sadeh 	ceph_msg_put(msg);
51143d14c5d2SYehuda Sadeh }
51153d14c5d2SYehuda Sadeh 
51163d14c5d2SYehuda Sadeh /*
5117d15f9d69SIlya Dryomov  * Lookup and return message for incoming reply.  Don't try to do
5118d15f9d69SIlya Dryomov  * anything about a larger than preallocated data portion of the
5119d15f9d69SIlya Dryomov  * message at the moment - for now, just skip the message.
51203d14c5d2SYehuda Sadeh  */
51213d14c5d2SYehuda Sadeh static struct ceph_msg *get_reply(struct ceph_connection *con,
51223d14c5d2SYehuda Sadeh 				  struct ceph_msg_header *hdr,
51233d14c5d2SYehuda Sadeh 				  int *skip)
51243d14c5d2SYehuda Sadeh {
51253d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
51263d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = osd->o_osdc;
51275aea3dcdSIlya Dryomov 	struct ceph_msg *m = NULL;
51283d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
51293f0a4ac5SIlya Dryomov 	int front_len = le32_to_cpu(hdr->front_len);
51303d14c5d2SYehuda Sadeh 	int data_len = le32_to_cpu(hdr->data_len);
51315aea3dcdSIlya Dryomov 	u64 tid = le64_to_cpu(hdr->tid);
51323d14c5d2SYehuda Sadeh 
51335aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
51345aea3dcdSIlya Dryomov 	if (!osd_registered(osd)) {
51355aea3dcdSIlya Dryomov 		dout("%s osd%d unknown, skipping\n", __func__, osd->o_osd);
51365aea3dcdSIlya Dryomov 		*skip = 1;
51375aea3dcdSIlya Dryomov 		goto out_unlock_osdc;
51385aea3dcdSIlya Dryomov 	}
51395aea3dcdSIlya Dryomov 	WARN_ON(osd->o_osd != le64_to_cpu(hdr->src.num));
51405aea3dcdSIlya Dryomov 
51415aea3dcdSIlya Dryomov 	mutex_lock(&osd->lock);
51425aea3dcdSIlya Dryomov 	req = lookup_request(&osd->o_requests, tid);
51433d14c5d2SYehuda Sadeh 	if (!req) {
5144cd8140c6SIlya Dryomov 		dout("%s osd%d tid %llu unknown, skipping\n", __func__,
5145cd8140c6SIlya Dryomov 		     osd->o_osd, tid);
5146d15f9d69SIlya Dryomov 		*skip = 1;
51475aea3dcdSIlya Dryomov 		goto out_unlock_session;
51483d14c5d2SYehuda Sadeh 	}
51493d14c5d2SYehuda Sadeh 
51508921d114SAlex Elder 	ceph_msg_revoke_incoming(req->r_reply);
51513d14c5d2SYehuda Sadeh 
5152f2be82b0SIlya Dryomov 	if (front_len > req->r_reply->front_alloc_len) {
5153d15f9d69SIlya Dryomov 		pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
5154d15f9d69SIlya Dryomov 			__func__, osd->o_osd, req->r_tid, front_len,
5155d15f9d69SIlya Dryomov 			req->r_reply->front_alloc_len);
51563f0a4ac5SIlya Dryomov 		m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
51573f0a4ac5SIlya Dryomov 				 false);
51583d14c5d2SYehuda Sadeh 		if (!m)
51595aea3dcdSIlya Dryomov 			goto out_unlock_session;
51603d14c5d2SYehuda Sadeh 		ceph_msg_put(req->r_reply);
51613d14c5d2SYehuda Sadeh 		req->r_reply = m;
51623d14c5d2SYehuda Sadeh 	}
51633d14c5d2SYehuda Sadeh 
5164d15f9d69SIlya Dryomov 	if (data_len > req->r_reply->data_length) {
5165d15f9d69SIlya Dryomov 		pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
5166d15f9d69SIlya Dryomov 			__func__, osd->o_osd, req->r_tid, data_len,
5167d15f9d69SIlya Dryomov 			req->r_reply->data_length);
51683d14c5d2SYehuda Sadeh 		m = NULL;
5169d15f9d69SIlya Dryomov 		*skip = 1;
51705aea3dcdSIlya Dryomov 		goto out_unlock_session;
51713d14c5d2SYehuda Sadeh 	}
5172d15f9d69SIlya Dryomov 
5173d15f9d69SIlya Dryomov 	m = ceph_msg_get(req->r_reply);
51743d14c5d2SYehuda Sadeh 	dout("get_reply tid %lld %p\n", tid, m);
51753d14c5d2SYehuda Sadeh 
51765aea3dcdSIlya Dryomov out_unlock_session:
51775aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
51785aea3dcdSIlya Dryomov out_unlock_osdc:
51795aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
51803d14c5d2SYehuda Sadeh 	return m;
51813d14c5d2SYehuda Sadeh }
51823d14c5d2SYehuda Sadeh 
518319079203SIlya Dryomov /*
518419079203SIlya Dryomov  * TODO: switch to a msg-owned pagelist
518519079203SIlya Dryomov  */
518619079203SIlya Dryomov static struct ceph_msg *alloc_msg_with_page_vector(struct ceph_msg_header *hdr)
518719079203SIlya Dryomov {
518819079203SIlya Dryomov 	struct ceph_msg *m;
518919079203SIlya Dryomov 	int type = le16_to_cpu(hdr->type);
519019079203SIlya Dryomov 	u32 front_len = le32_to_cpu(hdr->front_len);
519119079203SIlya Dryomov 	u32 data_len = le32_to_cpu(hdr->data_len);
519219079203SIlya Dryomov 
519319079203SIlya Dryomov 	m = ceph_msg_new(type, front_len, GFP_NOIO, false);
519419079203SIlya Dryomov 	if (!m)
519519079203SIlya Dryomov 		return NULL;
519619079203SIlya Dryomov 
519719079203SIlya Dryomov 	if (data_len) {
519819079203SIlya Dryomov 		struct page **pages;
519919079203SIlya Dryomov 		struct ceph_osd_data osd_data;
520019079203SIlya Dryomov 
520119079203SIlya Dryomov 		pages = ceph_alloc_page_vector(calc_pages_for(0, data_len),
520219079203SIlya Dryomov 					       GFP_NOIO);
5203c22e853aSWei Yongjun 		if (IS_ERR(pages)) {
520419079203SIlya Dryomov 			ceph_msg_put(m);
520519079203SIlya Dryomov 			return NULL;
520619079203SIlya Dryomov 		}
520719079203SIlya Dryomov 
520819079203SIlya Dryomov 		ceph_osd_data_pages_init(&osd_data, pages, data_len, 0, false,
520919079203SIlya Dryomov 					 false);
521019079203SIlya Dryomov 		ceph_osdc_msg_data_add(m, &osd_data);
521119079203SIlya Dryomov 	}
521219079203SIlya Dryomov 
521319079203SIlya Dryomov 	return m;
521419079203SIlya Dryomov }
521519079203SIlya Dryomov 
52163d14c5d2SYehuda Sadeh static struct ceph_msg *alloc_msg(struct ceph_connection *con,
52173d14c5d2SYehuda Sadeh 				  struct ceph_msg_header *hdr,
52183d14c5d2SYehuda Sadeh 				  int *skip)
52193d14c5d2SYehuda Sadeh {
52203d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
52213d14c5d2SYehuda Sadeh 	int type = le16_to_cpu(hdr->type);
52223d14c5d2SYehuda Sadeh 
52231c20f2d2SAlex Elder 	*skip = 0;
52243d14c5d2SYehuda Sadeh 	switch (type) {
52253d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_MAP:
5226a02a946dSIlya Dryomov 	case CEPH_MSG_OSD_BACKOFF:
5227a40c4f10SYehuda Sadeh 	case CEPH_MSG_WATCH_NOTIFY:
522819079203SIlya Dryomov 		return alloc_msg_with_page_vector(hdr);
52293d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_OPREPLY:
52303d14c5d2SYehuda Sadeh 		return get_reply(con, hdr, skip);
52313d14c5d2SYehuda Sadeh 	default:
52325aea3dcdSIlya Dryomov 		pr_warn("%s osd%d unknown msg type %d, skipping\n", __func__,
52335aea3dcdSIlya Dryomov 			osd->o_osd, type);
52343d14c5d2SYehuda Sadeh 		*skip = 1;
52353d14c5d2SYehuda Sadeh 		return NULL;
52363d14c5d2SYehuda Sadeh 	}
52373d14c5d2SYehuda Sadeh }
52383d14c5d2SYehuda Sadeh 
52393d14c5d2SYehuda Sadeh /*
52403d14c5d2SYehuda Sadeh  * Wrappers to refcount containing ceph_osd struct
52413d14c5d2SYehuda Sadeh  */
52423d14c5d2SYehuda Sadeh static struct ceph_connection *get_osd_con(struct ceph_connection *con)
52433d14c5d2SYehuda Sadeh {
52443d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
52453d14c5d2SYehuda Sadeh 	if (get_osd(osd))
52463d14c5d2SYehuda Sadeh 		return con;
52473d14c5d2SYehuda Sadeh 	return NULL;
52483d14c5d2SYehuda Sadeh }
52493d14c5d2SYehuda Sadeh 
52503d14c5d2SYehuda Sadeh static void put_osd_con(struct ceph_connection *con)
52513d14c5d2SYehuda Sadeh {
52523d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
52533d14c5d2SYehuda Sadeh 	put_osd(osd);
52543d14c5d2SYehuda Sadeh }
52553d14c5d2SYehuda Sadeh 
52563d14c5d2SYehuda Sadeh /*
52573d14c5d2SYehuda Sadeh  * authentication
52583d14c5d2SYehuda Sadeh  */
5259a3530df3SAlex Elder /*
5260a3530df3SAlex Elder  * Note: returned pointer is the address of a structure that's
5261a3530df3SAlex Elder  * managed separately.  Caller must *not* attempt to free it.
5262a3530df3SAlex Elder  */
5263a3530df3SAlex Elder static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
52648f43fb53SAlex Elder 					int *proto, int force_new)
52653d14c5d2SYehuda Sadeh {
52663d14c5d2SYehuda Sadeh 	struct ceph_osd *o = con->private;
52673d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = o->o_osdc;
52683d14c5d2SYehuda Sadeh 	struct ceph_auth_client *ac = osdc->client->monc.auth;
526974f1869fSAlex Elder 	struct ceph_auth_handshake *auth = &o->o_auth;
52703d14c5d2SYehuda Sadeh 
527174f1869fSAlex Elder 	if (force_new && auth->authorizer) {
52726c1ea260SIlya Dryomov 		ceph_auth_destroy_authorizer(auth->authorizer);
527374f1869fSAlex Elder 		auth->authorizer = NULL;
52743d14c5d2SYehuda Sadeh 	}
527527859f97SSage Weil 	if (!auth->authorizer) {
527627859f97SSage Weil 		int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
5277a3530df3SAlex Elder 						      auth);
52783d14c5d2SYehuda Sadeh 		if (ret)
5279a3530df3SAlex Elder 			return ERR_PTR(ret);
528027859f97SSage Weil 	} else {
528127859f97SSage Weil 		int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
52820bed9b5cSSage Weil 						     auth);
52830bed9b5cSSage Weil 		if (ret)
52840bed9b5cSSage Weil 			return ERR_PTR(ret);
52853d14c5d2SYehuda Sadeh 	}
52863d14c5d2SYehuda Sadeh 	*proto = ac->protocol;
528774f1869fSAlex Elder 
5288a3530df3SAlex Elder 	return auth;
52893d14c5d2SYehuda Sadeh }
52903d14c5d2SYehuda Sadeh 
52913d14c5d2SYehuda Sadeh 
52920dde5848SIlya Dryomov static int verify_authorizer_reply(struct ceph_connection *con)
52933d14c5d2SYehuda Sadeh {
52943d14c5d2SYehuda Sadeh 	struct ceph_osd *o = con->private;
52953d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = o->o_osdc;
52963d14c5d2SYehuda Sadeh 	struct ceph_auth_client *ac = osdc->client->monc.auth;
52973d14c5d2SYehuda Sadeh 
52980dde5848SIlya Dryomov 	return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer);
52993d14c5d2SYehuda Sadeh }
53003d14c5d2SYehuda Sadeh 
53013d14c5d2SYehuda Sadeh static int invalidate_authorizer(struct ceph_connection *con)
53023d14c5d2SYehuda Sadeh {
53033d14c5d2SYehuda Sadeh 	struct ceph_osd *o = con->private;
53043d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = o->o_osdc;
53053d14c5d2SYehuda Sadeh 	struct ceph_auth_client *ac = osdc->client->monc.auth;
53063d14c5d2SYehuda Sadeh 
530727859f97SSage Weil 	ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
53083d14c5d2SYehuda Sadeh 	return ceph_monc_validate_auth(&osdc->client->monc);
53093d14c5d2SYehuda Sadeh }
53103d14c5d2SYehuda Sadeh 
53118cb441c0SIlya Dryomov static void osd_reencode_message(struct ceph_msg *msg)
53128cb441c0SIlya Dryomov {
5313914902afSIlya Dryomov 	int type = le16_to_cpu(msg->hdr.type);
5314914902afSIlya Dryomov 
5315914902afSIlya Dryomov 	if (type == CEPH_MSG_OSD_OP)
53168cb441c0SIlya Dryomov 		encode_request_finish(msg);
53178cb441c0SIlya Dryomov }
53188cb441c0SIlya Dryomov 
531979dbd1baSIlya Dryomov static int osd_sign_message(struct ceph_msg *msg)
532033d07337SYan, Zheng {
532179dbd1baSIlya Dryomov 	struct ceph_osd *o = msg->con->private;
532233d07337SYan, Zheng 	struct ceph_auth_handshake *auth = &o->o_auth;
532379dbd1baSIlya Dryomov 
532433d07337SYan, Zheng 	return ceph_auth_sign_message(auth, msg);
532533d07337SYan, Zheng }
532633d07337SYan, Zheng 
532779dbd1baSIlya Dryomov static int osd_check_message_signature(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_check_message_signature(auth, msg);
533333d07337SYan, Zheng }
533433d07337SYan, Zheng 
53353d14c5d2SYehuda Sadeh static const struct ceph_connection_operations osd_con_ops = {
53363d14c5d2SYehuda Sadeh 	.get = get_osd_con,
53373d14c5d2SYehuda Sadeh 	.put = put_osd_con,
53383d14c5d2SYehuda Sadeh 	.dispatch = dispatch,
53393d14c5d2SYehuda Sadeh 	.get_authorizer = get_authorizer,
53403d14c5d2SYehuda Sadeh 	.verify_authorizer_reply = verify_authorizer_reply,
53413d14c5d2SYehuda Sadeh 	.invalidate_authorizer = invalidate_authorizer,
53423d14c5d2SYehuda Sadeh 	.alloc_msg = alloc_msg,
53438cb441c0SIlya Dryomov 	.reencode_message = osd_reencode_message,
534479dbd1baSIlya Dryomov 	.sign_message = osd_sign_message,
534579dbd1baSIlya Dryomov 	.check_message_signature = osd_check_message_signature,
53465aea3dcdSIlya Dryomov 	.fault = osd_fault,
53473d14c5d2SYehuda Sadeh };
5348