xref: /openbmc/linux/net/ceph/osd_client.c (revision b2441318)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2a4ce40a9SAlex Elder 
33d14c5d2SYehuda Sadeh #include <linux/ceph/ceph_debug.h>
43d14c5d2SYehuda Sadeh 
53d14c5d2SYehuda Sadeh #include <linux/module.h>
63d14c5d2SYehuda Sadeh #include <linux/err.h>
73d14c5d2SYehuda Sadeh #include <linux/highmem.h>
83d14c5d2SYehuda Sadeh #include <linux/mm.h>
93d14c5d2SYehuda Sadeh #include <linux/pagemap.h>
103d14c5d2SYehuda Sadeh #include <linux/slab.h>
113d14c5d2SYehuda Sadeh #include <linux/uaccess.h>
123d14c5d2SYehuda Sadeh #ifdef CONFIG_BLOCK
133d14c5d2SYehuda Sadeh #include <linux/bio.h>
143d14c5d2SYehuda Sadeh #endif
153d14c5d2SYehuda Sadeh 
168cb441c0SIlya Dryomov #include <linux/ceph/ceph_features.h>
173d14c5d2SYehuda Sadeh #include <linux/ceph/libceph.h>
183d14c5d2SYehuda Sadeh #include <linux/ceph/osd_client.h>
193d14c5d2SYehuda Sadeh #include <linux/ceph/messenger.h>
203d14c5d2SYehuda Sadeh #include <linux/ceph/decode.h>
213d14c5d2SYehuda Sadeh #include <linux/ceph/auth.h>
223d14c5d2SYehuda Sadeh #include <linux/ceph/pagelist.h>
233d14c5d2SYehuda Sadeh 
243d14c5d2SYehuda Sadeh #define OSD_OPREPLY_FRONT_LEN	512
253d14c5d2SYehuda Sadeh 
265522ae0bSAlex Elder static struct kmem_cache	*ceph_osd_request_cache;
275522ae0bSAlex Elder 
283d14c5d2SYehuda Sadeh static const struct ceph_connection_operations osd_con_ops;
293d14c5d2SYehuda Sadeh 
303d14c5d2SYehuda Sadeh /*
313d14c5d2SYehuda Sadeh  * Implement client access to distributed object storage cluster.
323d14c5d2SYehuda Sadeh  *
333d14c5d2SYehuda Sadeh  * All data objects are stored within a cluster/cloud of OSDs, or
343d14c5d2SYehuda Sadeh  * "object storage devices."  (Note that Ceph OSDs have _nothing_ to
353d14c5d2SYehuda Sadeh  * do with the T10 OSD extensions to SCSI.)  Ceph OSDs are simply
363d14c5d2SYehuda Sadeh  * remote daemons serving up and coordinating consistent and safe
373d14c5d2SYehuda Sadeh  * access to storage.
383d14c5d2SYehuda Sadeh  *
393d14c5d2SYehuda Sadeh  * Cluster membership and the mapping of data objects onto storage devices
403d14c5d2SYehuda Sadeh  * are described by the osd map.
413d14c5d2SYehuda Sadeh  *
423d14c5d2SYehuda Sadeh  * We keep track of pending OSD requests (read, write), resubmit
433d14c5d2SYehuda Sadeh  * requests to different OSDs when the cluster topology/data layout
443d14c5d2SYehuda Sadeh  * change, or retry the affected requests when the communications
453d14c5d2SYehuda Sadeh  * channel with an OSD is reset.
463d14c5d2SYehuda Sadeh  */
473d14c5d2SYehuda Sadeh 
485aea3dcdSIlya Dryomov static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req);
495aea3dcdSIlya Dryomov static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req);
50922dab61SIlya Dryomov static void link_linger(struct ceph_osd *osd,
51922dab61SIlya Dryomov 			struct ceph_osd_linger_request *lreq);
52922dab61SIlya Dryomov static void unlink_linger(struct ceph_osd *osd,
53922dab61SIlya Dryomov 			  struct ceph_osd_linger_request *lreq);
54a02a946dSIlya Dryomov static void clear_backoffs(struct ceph_osd *osd);
555aea3dcdSIlya Dryomov 
565aea3dcdSIlya Dryomov #if 1
575aea3dcdSIlya Dryomov static inline bool rwsem_is_wrlocked(struct rw_semaphore *sem)
585aea3dcdSIlya Dryomov {
595aea3dcdSIlya Dryomov 	bool wrlocked = true;
605aea3dcdSIlya Dryomov 
615aea3dcdSIlya Dryomov 	if (unlikely(down_read_trylock(sem))) {
625aea3dcdSIlya Dryomov 		wrlocked = false;
635aea3dcdSIlya Dryomov 		up_read(sem);
645aea3dcdSIlya Dryomov 	}
655aea3dcdSIlya Dryomov 
665aea3dcdSIlya Dryomov 	return wrlocked;
675aea3dcdSIlya Dryomov }
685aea3dcdSIlya Dryomov static inline void verify_osdc_locked(struct ceph_osd_client *osdc)
695aea3dcdSIlya Dryomov {
705aea3dcdSIlya Dryomov 	WARN_ON(!rwsem_is_locked(&osdc->lock));
715aea3dcdSIlya Dryomov }
725aea3dcdSIlya Dryomov static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc)
735aea3dcdSIlya Dryomov {
745aea3dcdSIlya Dryomov 	WARN_ON(!rwsem_is_wrlocked(&osdc->lock));
755aea3dcdSIlya Dryomov }
765aea3dcdSIlya Dryomov static inline void verify_osd_locked(struct ceph_osd *osd)
775aea3dcdSIlya Dryomov {
785aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
795aea3dcdSIlya Dryomov 
805aea3dcdSIlya Dryomov 	WARN_ON(!(mutex_is_locked(&osd->lock) &&
815aea3dcdSIlya Dryomov 		  rwsem_is_locked(&osdc->lock)) &&
825aea3dcdSIlya Dryomov 		!rwsem_is_wrlocked(&osdc->lock));
835aea3dcdSIlya Dryomov }
84922dab61SIlya Dryomov static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq)
85922dab61SIlya Dryomov {
86922dab61SIlya Dryomov 	WARN_ON(!mutex_is_locked(&lreq->lock));
87922dab61SIlya Dryomov }
885aea3dcdSIlya Dryomov #else
895aea3dcdSIlya Dryomov static inline void verify_osdc_locked(struct ceph_osd_client *osdc) { }
905aea3dcdSIlya Dryomov static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc) { }
915aea3dcdSIlya Dryomov static inline void verify_osd_locked(struct ceph_osd *osd) { }
92922dab61SIlya Dryomov static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq) { }
935aea3dcdSIlya Dryomov #endif
945aea3dcdSIlya Dryomov 
953d14c5d2SYehuda Sadeh /*
963d14c5d2SYehuda Sadeh  * calculate the mapping of a file extent onto an object, and fill out the
973d14c5d2SYehuda Sadeh  * request accordingly.  shorten extent as necessary if it crosses an
983d14c5d2SYehuda Sadeh  * object boundary.
993d14c5d2SYehuda Sadeh  *
1003d14c5d2SYehuda Sadeh  * fill osd op in request message.
1013d14c5d2SYehuda Sadeh  */
102dbe0fc41SAlex Elder static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
103a19dadfbSAlex Elder 			u64 *objnum, u64 *objoff, u64 *objlen)
1043d14c5d2SYehuda Sadeh {
10560e56f13SAlex Elder 	u64 orig_len = *plen;
106d63b77f4SSage Weil 	int r;
1073d14c5d2SYehuda Sadeh 
10860e56f13SAlex Elder 	/* object extent? */
10975d1c941SAlex Elder 	r = ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
11075d1c941SAlex Elder 					  objoff, objlen);
111d63b77f4SSage Weil 	if (r < 0)
112d63b77f4SSage Weil 		return r;
11375d1c941SAlex Elder 	if (*objlen < orig_len) {
11475d1c941SAlex Elder 		*plen = *objlen;
11560e56f13SAlex Elder 		dout(" skipping last %llu, final file extent %llu~%llu\n",
11660e56f13SAlex Elder 		     orig_len - *plen, off, *plen);
11760e56f13SAlex Elder 	}
11860e56f13SAlex Elder 
11975d1c941SAlex Elder 	dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
1203d14c5d2SYehuda Sadeh 
1213ff5f385SAlex Elder 	return 0;
1223d14c5d2SYehuda Sadeh }
1233d14c5d2SYehuda Sadeh 
124c54d47bfSAlex Elder static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
125c54d47bfSAlex Elder {
126c54d47bfSAlex Elder 	memset(osd_data, 0, sizeof (*osd_data));
127c54d47bfSAlex Elder 	osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
128c54d47bfSAlex Elder }
129c54d47bfSAlex Elder 
130a4ce40a9SAlex Elder static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
13143bfe5deSAlex Elder 			struct page **pages, u64 length, u32 alignment,
13243bfe5deSAlex Elder 			bool pages_from_pool, bool own_pages)
13343bfe5deSAlex Elder {
13443bfe5deSAlex Elder 	osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
13543bfe5deSAlex Elder 	osd_data->pages = pages;
13643bfe5deSAlex Elder 	osd_data->length = length;
13743bfe5deSAlex Elder 	osd_data->alignment = alignment;
13843bfe5deSAlex Elder 	osd_data->pages_from_pool = pages_from_pool;
13943bfe5deSAlex Elder 	osd_data->own_pages = own_pages;
14043bfe5deSAlex Elder }
14143bfe5deSAlex Elder 
142a4ce40a9SAlex Elder static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
14343bfe5deSAlex Elder 			struct ceph_pagelist *pagelist)
14443bfe5deSAlex Elder {
14543bfe5deSAlex Elder 	osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
14643bfe5deSAlex Elder 	osd_data->pagelist = pagelist;
14743bfe5deSAlex Elder }
14843bfe5deSAlex Elder 
14943bfe5deSAlex Elder #ifdef CONFIG_BLOCK
150a4ce40a9SAlex Elder static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
15143bfe5deSAlex Elder 			struct bio *bio, size_t bio_length)
15243bfe5deSAlex Elder {
15343bfe5deSAlex Elder 	osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
15443bfe5deSAlex Elder 	osd_data->bio = bio;
15543bfe5deSAlex Elder 	osd_data->bio_length = bio_length;
15643bfe5deSAlex Elder }
15743bfe5deSAlex Elder #endif /* CONFIG_BLOCK */
15843bfe5deSAlex Elder 
159863c7eb5SAlex Elder #define osd_req_op_data(oreq, whch, typ, fld)				\
160863c7eb5SAlex Elder ({									\
1618a703a38SIoana Ciornei 	struct ceph_osd_request *__oreq = (oreq);			\
1628a703a38SIoana Ciornei 	unsigned int __whch = (whch);					\
1638a703a38SIoana Ciornei 	BUG_ON(__whch >= __oreq->r_num_ops);				\
1648a703a38SIoana Ciornei 	&__oreq->r_ops[__whch].typ.fld;					\
165863c7eb5SAlex Elder })
166863c7eb5SAlex Elder 
16749719778SAlex Elder static struct ceph_osd_data *
16849719778SAlex Elder osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
16949719778SAlex Elder {
17049719778SAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
17149719778SAlex Elder 
17249719778SAlex Elder 	return &osd_req->r_ops[which].raw_data_in;
17349719778SAlex Elder }
17449719778SAlex Elder 
175a4ce40a9SAlex Elder struct ceph_osd_data *
176a4ce40a9SAlex Elder osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
177406e2c9fSAlex Elder 			unsigned int which)
178a4ce40a9SAlex Elder {
179863c7eb5SAlex Elder 	return osd_req_op_data(osd_req, which, extent, osd_data);
180a4ce40a9SAlex Elder }
181a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_osd_data);
182a4ce40a9SAlex Elder 
18349719778SAlex Elder void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
18449719778SAlex Elder 			unsigned int which, struct page **pages,
18549719778SAlex Elder 			u64 length, u32 alignment,
18649719778SAlex Elder 			bool pages_from_pool, bool own_pages)
18749719778SAlex Elder {
18849719778SAlex Elder 	struct ceph_osd_data *osd_data;
18949719778SAlex Elder 
19049719778SAlex Elder 	osd_data = osd_req_op_raw_data_in(osd_req, which);
19149719778SAlex Elder 	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
19249719778SAlex Elder 				pages_from_pool, own_pages);
19349719778SAlex Elder }
19449719778SAlex Elder EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
19549719778SAlex Elder 
196a4ce40a9SAlex Elder void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
197406e2c9fSAlex Elder 			unsigned int which, struct page **pages,
198406e2c9fSAlex Elder 			u64 length, u32 alignment,
199a4ce40a9SAlex Elder 			bool pages_from_pool, bool own_pages)
200a4ce40a9SAlex Elder {
201a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
202a4ce40a9SAlex Elder 
203863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
204a4ce40a9SAlex Elder 	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
205a4ce40a9SAlex Elder 				pages_from_pool, own_pages);
206a4ce40a9SAlex Elder }
207a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
208a4ce40a9SAlex Elder 
209a4ce40a9SAlex Elder void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
210406e2c9fSAlex Elder 			unsigned int which, struct ceph_pagelist *pagelist)
211a4ce40a9SAlex Elder {
212a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
213a4ce40a9SAlex Elder 
214863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
215a4ce40a9SAlex Elder 	ceph_osd_data_pagelist_init(osd_data, pagelist);
216a4ce40a9SAlex Elder }
217a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
218a4ce40a9SAlex Elder 
219a4ce40a9SAlex Elder #ifdef CONFIG_BLOCK
220a4ce40a9SAlex Elder void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
221406e2c9fSAlex Elder 			unsigned int which, struct bio *bio, size_t bio_length)
222a4ce40a9SAlex Elder {
223a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
224863c7eb5SAlex Elder 
225863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
226a4ce40a9SAlex Elder 	ceph_osd_data_bio_init(osd_data, bio, bio_length);
227a4ce40a9SAlex Elder }
228a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
229a4ce40a9SAlex Elder #endif /* CONFIG_BLOCK */
230a4ce40a9SAlex Elder 
231a4ce40a9SAlex Elder static void osd_req_op_cls_request_info_pagelist(
232a4ce40a9SAlex Elder 			struct ceph_osd_request *osd_req,
233a4ce40a9SAlex Elder 			unsigned int which, struct ceph_pagelist *pagelist)
234a4ce40a9SAlex Elder {
235a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
236a4ce40a9SAlex Elder 
237863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, request_info);
238a4ce40a9SAlex Elder 	ceph_osd_data_pagelist_init(osd_data, pagelist);
239a4ce40a9SAlex Elder }
240a4ce40a9SAlex Elder 
24104017e29SAlex Elder void osd_req_op_cls_request_data_pagelist(
24204017e29SAlex Elder 			struct ceph_osd_request *osd_req,
24304017e29SAlex Elder 			unsigned int which, struct ceph_pagelist *pagelist)
24404017e29SAlex Elder {
24504017e29SAlex Elder 	struct ceph_osd_data *osd_data;
24604017e29SAlex Elder 
247863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, request_data);
24804017e29SAlex Elder 	ceph_osd_data_pagelist_init(osd_data, pagelist);
249bb873b53SIlya Dryomov 	osd_req->r_ops[which].cls.indata_len += pagelist->length;
250bb873b53SIlya Dryomov 	osd_req->r_ops[which].indata_len += pagelist->length;
25104017e29SAlex Elder }
25204017e29SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
25304017e29SAlex Elder 
2546c57b554SAlex Elder void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
2556c57b554SAlex Elder 			unsigned int which, struct page **pages, u64 length,
2566c57b554SAlex Elder 			u32 alignment, bool pages_from_pool, bool own_pages)
2576c57b554SAlex Elder {
2586c57b554SAlex Elder 	struct ceph_osd_data *osd_data;
2596c57b554SAlex Elder 
2606c57b554SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, request_data);
2616c57b554SAlex Elder 	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
2626c57b554SAlex Elder 				pages_from_pool, own_pages);
263bb873b53SIlya Dryomov 	osd_req->r_ops[which].cls.indata_len += length;
264bb873b53SIlya Dryomov 	osd_req->r_ops[which].indata_len += length;
2656c57b554SAlex Elder }
2666c57b554SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
2676c57b554SAlex Elder 
268a4ce40a9SAlex Elder void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
269a4ce40a9SAlex Elder 			unsigned int which, struct page **pages, u64 length,
270a4ce40a9SAlex Elder 			u32 alignment, bool pages_from_pool, bool own_pages)
271a4ce40a9SAlex Elder {
272a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
273a4ce40a9SAlex Elder 
274863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, response_data);
275a4ce40a9SAlex Elder 	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
276a4ce40a9SAlex Elder 				pages_from_pool, own_pages);
277a4ce40a9SAlex Elder }
278a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
279a4ce40a9SAlex Elder 
28023c08a9cSAlex Elder static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
28123c08a9cSAlex Elder {
28223c08a9cSAlex Elder 	switch (osd_data->type) {
28323c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_NONE:
28423c08a9cSAlex Elder 		return 0;
28523c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_PAGES:
28623c08a9cSAlex Elder 		return osd_data->length;
28723c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_PAGELIST:
28823c08a9cSAlex Elder 		return (u64)osd_data->pagelist->length;
28923c08a9cSAlex Elder #ifdef CONFIG_BLOCK
29023c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_BIO:
29123c08a9cSAlex Elder 		return (u64)osd_data->bio_length;
29223c08a9cSAlex Elder #endif /* CONFIG_BLOCK */
29323c08a9cSAlex Elder 	default:
29423c08a9cSAlex Elder 		WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
29523c08a9cSAlex Elder 		return 0;
29623c08a9cSAlex Elder 	}
29723c08a9cSAlex Elder }
29823c08a9cSAlex Elder 
299c54d47bfSAlex Elder static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
300c54d47bfSAlex Elder {
3015476492fSAlex Elder 	if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
302c54d47bfSAlex Elder 		int num_pages;
303c54d47bfSAlex Elder 
304c54d47bfSAlex Elder 		num_pages = calc_pages_for((u64)osd_data->alignment,
305c54d47bfSAlex Elder 						(u64)osd_data->length);
306c54d47bfSAlex Elder 		ceph_release_page_vector(osd_data->pages, num_pages);
307c54d47bfSAlex Elder 	}
3085476492fSAlex Elder 	ceph_osd_data_init(osd_data);
3095476492fSAlex Elder }
3105476492fSAlex Elder 
3115476492fSAlex Elder static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
3125476492fSAlex Elder 			unsigned int which)
3135476492fSAlex Elder {
3145476492fSAlex Elder 	struct ceph_osd_req_op *op;
3155476492fSAlex Elder 
3165476492fSAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
3175476492fSAlex Elder 	op = &osd_req->r_ops[which];
3185476492fSAlex Elder 
3195476492fSAlex Elder 	switch (op->op) {
3205476492fSAlex Elder 	case CEPH_OSD_OP_READ:
3215476492fSAlex Elder 	case CEPH_OSD_OP_WRITE:
322e30b7577SIlya Dryomov 	case CEPH_OSD_OP_WRITEFULL:
3235476492fSAlex Elder 		ceph_osd_data_release(&op->extent.osd_data);
3245476492fSAlex Elder 		break;
3255476492fSAlex Elder 	case CEPH_OSD_OP_CALL:
3265476492fSAlex Elder 		ceph_osd_data_release(&op->cls.request_info);
32704017e29SAlex Elder 		ceph_osd_data_release(&op->cls.request_data);
3285476492fSAlex Elder 		ceph_osd_data_release(&op->cls.response_data);
3295476492fSAlex Elder 		break;
330d74b50beSYan, Zheng 	case CEPH_OSD_OP_SETXATTR:
331d74b50beSYan, Zheng 	case CEPH_OSD_OP_CMPXATTR:
332d74b50beSYan, Zheng 		ceph_osd_data_release(&op->xattr.osd_data);
333d74b50beSYan, Zheng 		break;
33466ba609fSYan, Zheng 	case CEPH_OSD_OP_STAT:
33566ba609fSYan, Zheng 		ceph_osd_data_release(&op->raw_data_in);
33666ba609fSYan, Zheng 		break;
337922dab61SIlya Dryomov 	case CEPH_OSD_OP_NOTIFY_ACK:
338922dab61SIlya Dryomov 		ceph_osd_data_release(&op->notify_ack.request_data);
339922dab61SIlya Dryomov 		break;
34019079203SIlya Dryomov 	case CEPH_OSD_OP_NOTIFY:
34119079203SIlya Dryomov 		ceph_osd_data_release(&op->notify.request_data);
34219079203SIlya Dryomov 		ceph_osd_data_release(&op->notify.response_data);
34319079203SIlya Dryomov 		break;
344a4ed38d7SDouglas Fuller 	case CEPH_OSD_OP_LIST_WATCHERS:
345a4ed38d7SDouglas Fuller 		ceph_osd_data_release(&op->list_watchers.response_data);
346a4ed38d7SDouglas Fuller 		break;
3475476492fSAlex Elder 	default:
3485476492fSAlex Elder 		break;
3495476492fSAlex Elder 	}
350c54d47bfSAlex Elder }
351c54d47bfSAlex Elder 
3523d14c5d2SYehuda Sadeh /*
35363244fa1SIlya Dryomov  * Assumes @t is zero-initialized.
35463244fa1SIlya Dryomov  */
35563244fa1SIlya Dryomov static void target_init(struct ceph_osd_request_target *t)
35663244fa1SIlya Dryomov {
35763244fa1SIlya Dryomov 	ceph_oid_init(&t->base_oid);
35863244fa1SIlya Dryomov 	ceph_oloc_init(&t->base_oloc);
35963244fa1SIlya Dryomov 	ceph_oid_init(&t->target_oid);
36063244fa1SIlya Dryomov 	ceph_oloc_init(&t->target_oloc);
36163244fa1SIlya Dryomov 
36263244fa1SIlya Dryomov 	ceph_osds_init(&t->acting);
36363244fa1SIlya Dryomov 	ceph_osds_init(&t->up);
36463244fa1SIlya Dryomov 	t->size = -1;
36563244fa1SIlya Dryomov 	t->min_size = -1;
36663244fa1SIlya Dryomov 
36763244fa1SIlya Dryomov 	t->osd = CEPH_HOMELESS_OSD;
36863244fa1SIlya Dryomov }
36963244fa1SIlya Dryomov 
370922dab61SIlya Dryomov static void target_copy(struct ceph_osd_request_target *dest,
371922dab61SIlya Dryomov 			const struct ceph_osd_request_target *src)
372922dab61SIlya Dryomov {
373922dab61SIlya Dryomov 	ceph_oid_copy(&dest->base_oid, &src->base_oid);
374922dab61SIlya Dryomov 	ceph_oloc_copy(&dest->base_oloc, &src->base_oloc);
375922dab61SIlya Dryomov 	ceph_oid_copy(&dest->target_oid, &src->target_oid);
376922dab61SIlya Dryomov 	ceph_oloc_copy(&dest->target_oloc, &src->target_oloc);
377922dab61SIlya Dryomov 
378922dab61SIlya Dryomov 	dest->pgid = src->pgid; /* struct */
379dc98ff72SIlya Dryomov 	dest->spgid = src->spgid; /* struct */
380922dab61SIlya Dryomov 	dest->pg_num = src->pg_num;
381922dab61SIlya Dryomov 	dest->pg_num_mask = src->pg_num_mask;
382922dab61SIlya Dryomov 	ceph_osds_copy(&dest->acting, &src->acting);
383922dab61SIlya Dryomov 	ceph_osds_copy(&dest->up, &src->up);
384922dab61SIlya Dryomov 	dest->size = src->size;
385922dab61SIlya Dryomov 	dest->min_size = src->min_size;
386922dab61SIlya Dryomov 	dest->sort_bitwise = src->sort_bitwise;
387922dab61SIlya Dryomov 
388922dab61SIlya Dryomov 	dest->flags = src->flags;
389922dab61SIlya Dryomov 	dest->paused = src->paused;
390922dab61SIlya Dryomov 
39104c7d789SIlya Dryomov 	dest->epoch = src->epoch;
392dc93e0e2SIlya Dryomov 	dest->last_force_resend = src->last_force_resend;
393dc93e0e2SIlya Dryomov 
394922dab61SIlya Dryomov 	dest->osd = src->osd;
395922dab61SIlya Dryomov }
396922dab61SIlya Dryomov 
39763244fa1SIlya Dryomov static void target_destroy(struct ceph_osd_request_target *t)
39863244fa1SIlya Dryomov {
39963244fa1SIlya Dryomov 	ceph_oid_destroy(&t->base_oid);
40030c156d9SYan, Zheng 	ceph_oloc_destroy(&t->base_oloc);
40163244fa1SIlya Dryomov 	ceph_oid_destroy(&t->target_oid);
40230c156d9SYan, Zheng 	ceph_oloc_destroy(&t->target_oloc);
40363244fa1SIlya Dryomov }
40463244fa1SIlya Dryomov 
40563244fa1SIlya Dryomov /*
4063d14c5d2SYehuda Sadeh  * requests
4073d14c5d2SYehuda Sadeh  */
4083540bfdbSIlya Dryomov static void request_release_checks(struct ceph_osd_request *req)
4093540bfdbSIlya Dryomov {
4103540bfdbSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&req->r_node));
4114609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&req->r_mc_node));
4123540bfdbSIlya Dryomov 	WARN_ON(!list_empty(&req->r_unsafe_item));
4133540bfdbSIlya Dryomov 	WARN_ON(req->r_osd);
4143540bfdbSIlya Dryomov }
4153540bfdbSIlya Dryomov 
4169e94af20SIlya Dryomov static void ceph_osdc_release_request(struct kref *kref)
4173d14c5d2SYehuda Sadeh {
4189e94af20SIlya Dryomov 	struct ceph_osd_request *req = container_of(kref,
4199e94af20SIlya Dryomov 					    struct ceph_osd_request, r_kref);
4205476492fSAlex Elder 	unsigned int which;
4213d14c5d2SYehuda Sadeh 
4229e94af20SIlya Dryomov 	dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
4239e94af20SIlya Dryomov 	     req->r_request, req->r_reply);
4243540bfdbSIlya Dryomov 	request_release_checks(req);
4259e94af20SIlya Dryomov 
4263d14c5d2SYehuda Sadeh 	if (req->r_request)
4273d14c5d2SYehuda Sadeh 		ceph_msg_put(req->r_request);
4285aea3dcdSIlya Dryomov 	if (req->r_reply)
429ab8cb34aSAlex Elder 		ceph_msg_put(req->r_reply);
4300fff87ecSAlex Elder 
4315476492fSAlex Elder 	for (which = 0; which < req->r_num_ops; which++)
4325476492fSAlex Elder 		osd_req_op_data_release(req, which);
4330fff87ecSAlex Elder 
434a66dd383SIlya Dryomov 	target_destroy(&req->r_t);
4353d14c5d2SYehuda Sadeh 	ceph_put_snap_context(req->r_snapc);
436d30291b9SIlya Dryomov 
4373d14c5d2SYehuda Sadeh 	if (req->r_mempool)
4383d14c5d2SYehuda Sadeh 		mempool_free(req, req->r_osdc->req_mempool);
4393f1af42aSIlya Dryomov 	else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS)
4405522ae0bSAlex Elder 		kmem_cache_free(ceph_osd_request_cache, req);
4413f1af42aSIlya Dryomov 	else
4423f1af42aSIlya Dryomov 		kfree(req);
4433d14c5d2SYehuda Sadeh }
4449e94af20SIlya Dryomov 
4459e94af20SIlya Dryomov void ceph_osdc_get_request(struct ceph_osd_request *req)
4469e94af20SIlya Dryomov {
4479e94af20SIlya Dryomov 	dout("%s %p (was %d)\n", __func__, req,
4482c935bc5SPeter Zijlstra 	     kref_read(&req->r_kref));
4499e94af20SIlya Dryomov 	kref_get(&req->r_kref);
4509e94af20SIlya Dryomov }
4519e94af20SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_get_request);
4529e94af20SIlya Dryomov 
4539e94af20SIlya Dryomov void ceph_osdc_put_request(struct ceph_osd_request *req)
4549e94af20SIlya Dryomov {
4553ed97d63SIlya Dryomov 	if (req) {
4569e94af20SIlya Dryomov 		dout("%s %p (was %d)\n", __func__, req,
4572c935bc5SPeter Zijlstra 		     kref_read(&req->r_kref));
4589e94af20SIlya Dryomov 		kref_put(&req->r_kref, ceph_osdc_release_request);
4599e94af20SIlya Dryomov 	}
4603ed97d63SIlya Dryomov }
4619e94af20SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_put_request);
4623d14c5d2SYehuda Sadeh 
4633540bfdbSIlya Dryomov static void request_init(struct ceph_osd_request *req)
4643540bfdbSIlya Dryomov {
4653540bfdbSIlya Dryomov 	/* req only, each op is zeroed in _osd_req_op_init() */
4663540bfdbSIlya Dryomov 	memset(req, 0, sizeof(*req));
4673540bfdbSIlya Dryomov 
4683540bfdbSIlya Dryomov 	kref_init(&req->r_kref);
4693540bfdbSIlya Dryomov 	init_completion(&req->r_completion);
4703540bfdbSIlya Dryomov 	RB_CLEAR_NODE(&req->r_node);
4714609245eSIlya Dryomov 	RB_CLEAR_NODE(&req->r_mc_node);
4723540bfdbSIlya Dryomov 	INIT_LIST_HEAD(&req->r_unsafe_item);
4733540bfdbSIlya Dryomov 
4743540bfdbSIlya Dryomov 	target_init(&req->r_t);
4753540bfdbSIlya Dryomov }
4763540bfdbSIlya Dryomov 
477922dab61SIlya Dryomov /*
478922dab61SIlya Dryomov  * This is ugly, but it allows us to reuse linger registration and ping
479922dab61SIlya Dryomov  * requests, keeping the structure of the code around send_linger{_ping}()
480922dab61SIlya Dryomov  * reasonable.  Setting up a min_nr=2 mempool for each linger request
481922dab61SIlya Dryomov  * and dealing with copying ops (this blasts req only, watch op remains
482922dab61SIlya Dryomov  * intact) isn't any better.
483922dab61SIlya Dryomov  */
484922dab61SIlya Dryomov static void request_reinit(struct ceph_osd_request *req)
485922dab61SIlya Dryomov {
486922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
487922dab61SIlya Dryomov 	bool mempool = req->r_mempool;
488922dab61SIlya Dryomov 	unsigned int num_ops = req->r_num_ops;
489922dab61SIlya Dryomov 	u64 snapid = req->r_snapid;
490922dab61SIlya Dryomov 	struct ceph_snap_context *snapc = req->r_snapc;
491922dab61SIlya Dryomov 	bool linger = req->r_linger;
492922dab61SIlya Dryomov 	struct ceph_msg *request_msg = req->r_request;
493922dab61SIlya Dryomov 	struct ceph_msg *reply_msg = req->r_reply;
494922dab61SIlya Dryomov 
495922dab61SIlya Dryomov 	dout("%s req %p\n", __func__, req);
4962c935bc5SPeter Zijlstra 	WARN_ON(kref_read(&req->r_kref) != 1);
497922dab61SIlya Dryomov 	request_release_checks(req);
498922dab61SIlya Dryomov 
4992c935bc5SPeter Zijlstra 	WARN_ON(kref_read(&request_msg->kref) != 1);
5002c935bc5SPeter Zijlstra 	WARN_ON(kref_read(&reply_msg->kref) != 1);
501922dab61SIlya Dryomov 	target_destroy(&req->r_t);
502922dab61SIlya Dryomov 
503922dab61SIlya Dryomov 	request_init(req);
504922dab61SIlya Dryomov 	req->r_osdc = osdc;
505922dab61SIlya Dryomov 	req->r_mempool = mempool;
506922dab61SIlya Dryomov 	req->r_num_ops = num_ops;
507922dab61SIlya Dryomov 	req->r_snapid = snapid;
508922dab61SIlya Dryomov 	req->r_snapc = snapc;
509922dab61SIlya Dryomov 	req->r_linger = linger;
510922dab61SIlya Dryomov 	req->r_request = request_msg;
511922dab61SIlya Dryomov 	req->r_reply = reply_msg;
512922dab61SIlya Dryomov }
513922dab61SIlya Dryomov 
5143d14c5d2SYehuda Sadeh struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
5153d14c5d2SYehuda Sadeh 					       struct ceph_snap_context *snapc,
5161b83bef2SSage Weil 					       unsigned int num_ops,
5173d14c5d2SYehuda Sadeh 					       bool use_mempool,
51854a54007SAlex Elder 					       gfp_t gfp_flags)
5193d14c5d2SYehuda Sadeh {
5203d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
5213d14c5d2SYehuda Sadeh 
5223d14c5d2SYehuda Sadeh 	if (use_mempool) {
5233f1af42aSIlya Dryomov 		BUG_ON(num_ops > CEPH_OSD_SLAB_OPS);
5243d14c5d2SYehuda Sadeh 		req = mempool_alloc(osdc->req_mempool, gfp_flags);
5253f1af42aSIlya Dryomov 	} else if (num_ops <= CEPH_OSD_SLAB_OPS) {
5263f1af42aSIlya Dryomov 		req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
5273d14c5d2SYehuda Sadeh 	} else {
5283f1af42aSIlya Dryomov 		BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
5293f1af42aSIlya Dryomov 		req = kmalloc(sizeof(*req) + num_ops * sizeof(req->r_ops[0]),
5303f1af42aSIlya Dryomov 			      gfp_flags);
5313d14c5d2SYehuda Sadeh 	}
5323f1af42aSIlya Dryomov 	if (unlikely(!req))
5333d14c5d2SYehuda Sadeh 		return NULL;
5343d14c5d2SYehuda Sadeh 
5353540bfdbSIlya Dryomov 	request_init(req);
5363d14c5d2SYehuda Sadeh 	req->r_osdc = osdc;
5373d14c5d2SYehuda Sadeh 	req->r_mempool = use_mempool;
53879528734SAlex Elder 	req->r_num_ops = num_ops;
53984127282SIlya Dryomov 	req->r_snapid = CEPH_NOSNAP;
54084127282SIlya Dryomov 	req->r_snapc = ceph_get_snap_context(snapc);
5413d14c5d2SYehuda Sadeh 
54213d1ad16SIlya Dryomov 	dout("%s req %p\n", __func__, req);
54313d1ad16SIlya Dryomov 	return req;
5443f1af42aSIlya Dryomov }
54513d1ad16SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_alloc_request);
5463f1af42aSIlya Dryomov 
5472e59ffd1SIlya Dryomov static int ceph_oloc_encoding_size(const struct ceph_object_locator *oloc)
54830c156d9SYan, Zheng {
54930c156d9SYan, Zheng 	return 8 + 4 + 4 + 4 + (oloc->pool_ns ? oloc->pool_ns->len : 0);
55030c156d9SYan, Zheng }
55130c156d9SYan, Zheng 
55213d1ad16SIlya Dryomov int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp)
55313d1ad16SIlya Dryomov {
55413d1ad16SIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
55513d1ad16SIlya Dryomov 	struct ceph_msg *msg;
55613d1ad16SIlya Dryomov 	int msg_size;
5573d14c5d2SYehuda Sadeh 
558d30291b9SIlya Dryomov 	WARN_ON(ceph_oid_empty(&req->r_base_oid));
55930c156d9SYan, Zheng 	WARN_ON(ceph_oloc_empty(&req->r_base_oloc));
560d30291b9SIlya Dryomov 
56113d1ad16SIlya Dryomov 	/* create request message */
5628cb441c0SIlya Dryomov 	msg_size = CEPH_ENCODING_START_BLK_LEN +
5638cb441c0SIlya Dryomov 			CEPH_PGID_ENCODING_LEN + 1; /* spgid */
5648cb441c0SIlya Dryomov 	msg_size += 4 + 4 + 4; /* hash, osdmap_epoch, flags */
5658cb441c0SIlya Dryomov 	msg_size += CEPH_ENCODING_START_BLK_LEN +
5668cb441c0SIlya Dryomov 			sizeof(struct ceph_osd_reqid); /* reqid */
5678cb441c0SIlya Dryomov 	msg_size += sizeof(struct ceph_blkin_trace_info); /* trace */
5688cb441c0SIlya Dryomov 	msg_size += 4 + sizeof(struct ceph_timespec); /* client_inc, mtime */
56930c156d9SYan, Zheng 	msg_size += CEPH_ENCODING_START_BLK_LEN +
57030c156d9SYan, Zheng 			ceph_oloc_encoding_size(&req->r_base_oloc); /* oloc */
57113d1ad16SIlya Dryomov 	msg_size += 4 + req->r_base_oid.name_len; /* oid */
57213d1ad16SIlya Dryomov 	msg_size += 2 + req->r_num_ops * sizeof(struct ceph_osd_op);
573ae458f5aSIlya Dryomov 	msg_size += 8; /* snapid */
574ae458f5aSIlya Dryomov 	msg_size += 8; /* snap_seq */
57513d1ad16SIlya Dryomov 	msg_size += 4 + 8 * (req->r_snapc ? req->r_snapc->num_snaps : 0);
5768cb441c0SIlya Dryomov 	msg_size += 4 + 8; /* retry_attempt, features */
577ae458f5aSIlya Dryomov 
57813d1ad16SIlya Dryomov 	if (req->r_mempool)
5793d14c5d2SYehuda Sadeh 		msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
5803d14c5d2SYehuda Sadeh 	else
58113d1ad16SIlya Dryomov 		msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp, true);
58213d1ad16SIlya Dryomov 	if (!msg)
58313d1ad16SIlya Dryomov 		return -ENOMEM;
5843d14c5d2SYehuda Sadeh 
5853d14c5d2SYehuda Sadeh 	memset(msg->front.iov_base, 0, msg->front.iov_len);
5863d14c5d2SYehuda Sadeh 	req->r_request = msg;
5873d14c5d2SYehuda Sadeh 
58813d1ad16SIlya Dryomov 	/* create reply message */
58913d1ad16SIlya Dryomov 	msg_size = OSD_OPREPLY_FRONT_LEN;
590711da55dSIlya Dryomov 	msg_size += req->r_base_oid.name_len;
591711da55dSIlya Dryomov 	msg_size += req->r_num_ops * sizeof(struct ceph_osd_op);
59213d1ad16SIlya Dryomov 
59313d1ad16SIlya Dryomov 	if (req->r_mempool)
59413d1ad16SIlya Dryomov 		msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
59513d1ad16SIlya Dryomov 	else
59613d1ad16SIlya Dryomov 		msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, msg_size, gfp, true);
59713d1ad16SIlya Dryomov 	if (!msg)
59813d1ad16SIlya Dryomov 		return -ENOMEM;
59913d1ad16SIlya Dryomov 
60013d1ad16SIlya Dryomov 	req->r_reply = msg;
60113d1ad16SIlya Dryomov 
60213d1ad16SIlya Dryomov 	return 0;
60313d1ad16SIlya Dryomov }
60413d1ad16SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_alloc_messages);
6053d14c5d2SYehuda Sadeh 
606a8dd0a37SAlex Elder static bool osd_req_opcode_valid(u16 opcode)
607a8dd0a37SAlex Elder {
608a8dd0a37SAlex Elder 	switch (opcode) {
60970b5bfa3SIlya Dryomov #define GENERATE_CASE(op, opcode, str)	case CEPH_OSD_OP_##op: return true;
61070b5bfa3SIlya Dryomov __CEPH_FORALL_OSD_OPS(GENERATE_CASE)
61170b5bfa3SIlya Dryomov #undef GENERATE_CASE
612a8dd0a37SAlex Elder 	default:
613a8dd0a37SAlex Elder 		return false;
614a8dd0a37SAlex Elder 	}
615a8dd0a37SAlex Elder }
616a8dd0a37SAlex Elder 
61733803f33SAlex Elder /*
61833803f33SAlex Elder  * This is an osd op init function for opcodes that have no data or
61933803f33SAlex Elder  * other information associated with them.  It also serves as a
62033803f33SAlex Elder  * common init routine for all the other init functions, below.
62133803f33SAlex Elder  */
622c99d2d4aSAlex Elder static struct ceph_osd_req_op *
62349719778SAlex Elder _osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
624144cba14SYan, Zheng 		 u16 opcode, u32 flags)
62533803f33SAlex Elder {
626c99d2d4aSAlex Elder 	struct ceph_osd_req_op *op;
627c99d2d4aSAlex Elder 
628c99d2d4aSAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
62933803f33SAlex Elder 	BUG_ON(!osd_req_opcode_valid(opcode));
63033803f33SAlex Elder 
631c99d2d4aSAlex Elder 	op = &osd_req->r_ops[which];
63233803f33SAlex Elder 	memset(op, 0, sizeof (*op));
63333803f33SAlex Elder 	op->op = opcode;
634144cba14SYan, Zheng 	op->flags = flags;
635c99d2d4aSAlex Elder 
636c99d2d4aSAlex Elder 	return op;
63733803f33SAlex Elder }
63833803f33SAlex Elder 
63949719778SAlex Elder void osd_req_op_init(struct ceph_osd_request *osd_req,
640144cba14SYan, Zheng 		     unsigned int which, u16 opcode, u32 flags)
64149719778SAlex Elder {
642144cba14SYan, Zheng 	(void)_osd_req_op_init(osd_req, which, opcode, flags);
64349719778SAlex Elder }
64449719778SAlex Elder EXPORT_SYMBOL(osd_req_op_init);
64549719778SAlex Elder 
646c99d2d4aSAlex Elder void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
647c99d2d4aSAlex Elder 				unsigned int which, u16 opcode,
64833803f33SAlex Elder 				u64 offset, u64 length,
64933803f33SAlex Elder 				u64 truncate_size, u32 truncate_seq)
65033803f33SAlex Elder {
651144cba14SYan, Zheng 	struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
652144cba14SYan, Zheng 						      opcode, 0);
65333803f33SAlex Elder 	size_t payload_len = 0;
65433803f33SAlex Elder 
655ad7a60deSLi Wang 	BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
656e30b7577SIlya Dryomov 	       opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
657e30b7577SIlya Dryomov 	       opcode != CEPH_OSD_OP_TRUNCATE);
65833803f33SAlex Elder 
65933803f33SAlex Elder 	op->extent.offset = offset;
66033803f33SAlex Elder 	op->extent.length = length;
66133803f33SAlex Elder 	op->extent.truncate_size = truncate_size;
66233803f33SAlex Elder 	op->extent.truncate_seq = truncate_seq;
663e30b7577SIlya Dryomov 	if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
66433803f33SAlex Elder 		payload_len += length;
66533803f33SAlex Elder 
666de2aa102SIlya Dryomov 	op->indata_len = payload_len;
66733803f33SAlex Elder }
66833803f33SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_init);
66933803f33SAlex Elder 
670c99d2d4aSAlex Elder void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
671c99d2d4aSAlex Elder 				unsigned int which, u64 length)
672e5975c7cSAlex Elder {
673c99d2d4aSAlex Elder 	struct ceph_osd_req_op *op;
674c99d2d4aSAlex Elder 	u64 previous;
675c99d2d4aSAlex Elder 
676c99d2d4aSAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
677c99d2d4aSAlex Elder 	op = &osd_req->r_ops[which];
678c99d2d4aSAlex Elder 	previous = op->extent.length;
679e5975c7cSAlex Elder 
680e5975c7cSAlex Elder 	if (length == previous)
681e5975c7cSAlex Elder 		return;		/* Nothing to do */
682e5975c7cSAlex Elder 	BUG_ON(length > previous);
683e5975c7cSAlex Elder 
684e5975c7cSAlex Elder 	op->extent.length = length;
685d641df81SYan, Zheng 	if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
686de2aa102SIlya Dryomov 		op->indata_len -= previous - length;
687e5975c7cSAlex Elder }
688e5975c7cSAlex Elder EXPORT_SYMBOL(osd_req_op_extent_update);
689e5975c7cSAlex Elder 
6902c63f49aSYan, Zheng void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
6912c63f49aSYan, Zheng 				unsigned int which, u64 offset_inc)
6922c63f49aSYan, Zheng {
6932c63f49aSYan, Zheng 	struct ceph_osd_req_op *op, *prev_op;
6942c63f49aSYan, Zheng 
6952c63f49aSYan, Zheng 	BUG_ON(which + 1 >= osd_req->r_num_ops);
6962c63f49aSYan, Zheng 
6972c63f49aSYan, Zheng 	prev_op = &osd_req->r_ops[which];
6982c63f49aSYan, Zheng 	op = _osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags);
6992c63f49aSYan, Zheng 	/* dup previous one */
7002c63f49aSYan, Zheng 	op->indata_len = prev_op->indata_len;
7012c63f49aSYan, Zheng 	op->outdata_len = prev_op->outdata_len;
7022c63f49aSYan, Zheng 	op->extent = prev_op->extent;
7032c63f49aSYan, Zheng 	/* adjust offset */
7042c63f49aSYan, Zheng 	op->extent.offset += offset_inc;
7052c63f49aSYan, Zheng 	op->extent.length -= offset_inc;
7062c63f49aSYan, Zheng 
7072c63f49aSYan, Zheng 	if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
7082c63f49aSYan, Zheng 		op->indata_len -= offset_inc;
7092c63f49aSYan, Zheng }
7102c63f49aSYan, Zheng EXPORT_SYMBOL(osd_req_op_extent_dup_last);
7112c63f49aSYan, Zheng 
712c99d2d4aSAlex Elder void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
71304017e29SAlex Elder 			u16 opcode, const char *class, const char *method)
71433803f33SAlex Elder {
715144cba14SYan, Zheng 	struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
716144cba14SYan, Zheng 						      opcode, 0);
7175f562df5SAlex Elder 	struct ceph_pagelist *pagelist;
71833803f33SAlex Elder 	size_t payload_len = 0;
71933803f33SAlex Elder 	size_t size;
72033803f33SAlex Elder 
72133803f33SAlex Elder 	BUG_ON(opcode != CEPH_OSD_OP_CALL);
72233803f33SAlex Elder 
7235f562df5SAlex Elder 	pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
7245f562df5SAlex Elder 	BUG_ON(!pagelist);
7255f562df5SAlex Elder 	ceph_pagelist_init(pagelist);
7265f562df5SAlex Elder 
72733803f33SAlex Elder 	op->cls.class_name = class;
72833803f33SAlex Elder 	size = strlen(class);
72933803f33SAlex Elder 	BUG_ON(size > (size_t) U8_MAX);
73033803f33SAlex Elder 	op->cls.class_len = size;
7315f562df5SAlex Elder 	ceph_pagelist_append(pagelist, class, size);
73233803f33SAlex Elder 	payload_len += size;
73333803f33SAlex Elder 
73433803f33SAlex Elder 	op->cls.method_name = method;
73533803f33SAlex Elder 	size = strlen(method);
73633803f33SAlex Elder 	BUG_ON(size > (size_t) U8_MAX);
73733803f33SAlex Elder 	op->cls.method_len = size;
7385f562df5SAlex Elder 	ceph_pagelist_append(pagelist, method, size);
73933803f33SAlex Elder 	payload_len += size;
74033803f33SAlex Elder 
741a4ce40a9SAlex Elder 	osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
7425f562df5SAlex Elder 
743de2aa102SIlya Dryomov 	op->indata_len = payload_len;
74433803f33SAlex Elder }
74533803f33SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_init);
7468c042b0dSAlex Elder 
747d74b50beSYan, Zheng int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
748d74b50beSYan, Zheng 			  u16 opcode, const char *name, const void *value,
749d74b50beSYan, Zheng 			  size_t size, u8 cmp_op, u8 cmp_mode)
750d74b50beSYan, Zheng {
751144cba14SYan, Zheng 	struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
752144cba14SYan, Zheng 						      opcode, 0);
753d74b50beSYan, Zheng 	struct ceph_pagelist *pagelist;
754d74b50beSYan, Zheng 	size_t payload_len;
755d74b50beSYan, Zheng 
756d74b50beSYan, Zheng 	BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
757d74b50beSYan, Zheng 
758d74b50beSYan, Zheng 	pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
759d74b50beSYan, Zheng 	if (!pagelist)
760d74b50beSYan, Zheng 		return -ENOMEM;
761d74b50beSYan, Zheng 
762d74b50beSYan, Zheng 	ceph_pagelist_init(pagelist);
763d74b50beSYan, Zheng 
764d74b50beSYan, Zheng 	payload_len = strlen(name);
765d74b50beSYan, Zheng 	op->xattr.name_len = payload_len;
766d74b50beSYan, Zheng 	ceph_pagelist_append(pagelist, name, payload_len);
767d74b50beSYan, Zheng 
768d74b50beSYan, Zheng 	op->xattr.value_len = size;
769d74b50beSYan, Zheng 	ceph_pagelist_append(pagelist, value, size);
770d74b50beSYan, Zheng 	payload_len += size;
771d74b50beSYan, Zheng 
772d74b50beSYan, Zheng 	op->xattr.cmp_op = cmp_op;
773d74b50beSYan, Zheng 	op->xattr.cmp_mode = cmp_mode;
774d74b50beSYan, Zheng 
775d74b50beSYan, Zheng 	ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
776de2aa102SIlya Dryomov 	op->indata_len = payload_len;
777d74b50beSYan, Zheng 	return 0;
778d74b50beSYan, Zheng }
779d74b50beSYan, Zheng EXPORT_SYMBOL(osd_req_op_xattr_init);
780d74b50beSYan, Zheng 
781922dab61SIlya Dryomov /*
782922dab61SIlya Dryomov  * @watch_opcode: CEPH_OSD_WATCH_OP_*
783922dab61SIlya Dryomov  */
784922dab61SIlya Dryomov static void osd_req_op_watch_init(struct ceph_osd_request *req, int which,
785922dab61SIlya Dryomov 				  u64 cookie, u8 watch_opcode)
78633803f33SAlex Elder {
787922dab61SIlya Dryomov 	struct ceph_osd_req_op *op;
78833803f33SAlex Elder 
789922dab61SIlya Dryomov 	op = _osd_req_op_init(req, which, CEPH_OSD_OP_WATCH, 0);
79033803f33SAlex Elder 	op->watch.cookie = cookie;
791922dab61SIlya Dryomov 	op->watch.op = watch_opcode;
792922dab61SIlya Dryomov 	op->watch.gen = 0;
79333803f33SAlex Elder }
79433803f33SAlex Elder 
795c647b8a8SIlya Dryomov void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
796c647b8a8SIlya Dryomov 				unsigned int which,
797c647b8a8SIlya Dryomov 				u64 expected_object_size,
798c647b8a8SIlya Dryomov 				u64 expected_write_size)
799c647b8a8SIlya Dryomov {
800c647b8a8SIlya Dryomov 	struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
801144cba14SYan, Zheng 						      CEPH_OSD_OP_SETALLOCHINT,
802144cba14SYan, Zheng 						      0);
803c647b8a8SIlya Dryomov 
804c647b8a8SIlya Dryomov 	op->alloc_hint.expected_object_size = expected_object_size;
805c647b8a8SIlya Dryomov 	op->alloc_hint.expected_write_size = expected_write_size;
806c647b8a8SIlya Dryomov 
807c647b8a8SIlya Dryomov 	/*
808c647b8a8SIlya Dryomov 	 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
809c647b8a8SIlya Dryomov 	 * not worth a feature bit.  Set FAILOK per-op flag to make
810c647b8a8SIlya Dryomov 	 * sure older osds don't trip over an unsupported opcode.
811c647b8a8SIlya Dryomov 	 */
812c647b8a8SIlya Dryomov 	op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
813c647b8a8SIlya Dryomov }
814c647b8a8SIlya Dryomov EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
815c647b8a8SIlya Dryomov 
81690af3602SAlex Elder static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
817ec9123c5SAlex Elder 				struct ceph_osd_data *osd_data)
818ec9123c5SAlex Elder {
819ec9123c5SAlex Elder 	u64 length = ceph_osd_data_length(osd_data);
820ec9123c5SAlex Elder 
821ec9123c5SAlex Elder 	if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
822ec9123c5SAlex Elder 		BUG_ON(length > (u64) SIZE_MAX);
823ec9123c5SAlex Elder 		if (length)
82490af3602SAlex Elder 			ceph_msg_data_add_pages(msg, osd_data->pages,
825ec9123c5SAlex Elder 					length, osd_data->alignment);
826ec9123c5SAlex Elder 	} else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
827ec9123c5SAlex Elder 		BUG_ON(!length);
82890af3602SAlex Elder 		ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
829ec9123c5SAlex Elder #ifdef CONFIG_BLOCK
830ec9123c5SAlex Elder 	} else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
83190af3602SAlex Elder 		ceph_msg_data_add_bio(msg, osd_data->bio, length);
832ec9123c5SAlex Elder #endif
833ec9123c5SAlex Elder 	} else {
834ec9123c5SAlex Elder 		BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
835ec9123c5SAlex Elder 	}
836ec9123c5SAlex Elder }
837ec9123c5SAlex Elder 
838bb873b53SIlya Dryomov static u32 osd_req_encode_op(struct ceph_osd_op *dst,
839bb873b53SIlya Dryomov 			     const struct ceph_osd_req_op *src)
8403d14c5d2SYehuda Sadeh {
841a8dd0a37SAlex Elder 	if (WARN_ON(!osd_req_opcode_valid(src->op))) {
842a8dd0a37SAlex Elder 		pr_err("unrecognized osd opcode %d\n", src->op);
843a8dd0a37SAlex Elder 
844a8dd0a37SAlex Elder 		return 0;
845a8dd0a37SAlex Elder 	}
8463d14c5d2SYehuda Sadeh 
847065a68f9SAlex Elder 	switch (src->op) {
848fbfab539SAlex Elder 	case CEPH_OSD_OP_STAT:
849fbfab539SAlex Elder 		break;
8503d14c5d2SYehuda Sadeh 	case CEPH_OSD_OP_READ:
8513d14c5d2SYehuda Sadeh 	case CEPH_OSD_OP_WRITE:
852e30b7577SIlya Dryomov 	case CEPH_OSD_OP_WRITEFULL:
853ad7a60deSLi Wang 	case CEPH_OSD_OP_ZERO:
854ad7a60deSLi Wang 	case CEPH_OSD_OP_TRUNCATE:
855175face2SAlex Elder 		dst->extent.offset = cpu_to_le64(src->extent.offset);
856175face2SAlex Elder 		dst->extent.length = cpu_to_le64(src->extent.length);
8573d14c5d2SYehuda Sadeh 		dst->extent.truncate_size =
8583d14c5d2SYehuda Sadeh 			cpu_to_le64(src->extent.truncate_size);
8593d14c5d2SYehuda Sadeh 		dst->extent.truncate_seq =
8603d14c5d2SYehuda Sadeh 			cpu_to_le32(src->extent.truncate_seq);
8613d14c5d2SYehuda Sadeh 		break;
8623d14c5d2SYehuda Sadeh 	case CEPH_OSD_OP_CALL:
8633d14c5d2SYehuda Sadeh 		dst->cls.class_len = src->cls.class_len;
8643d14c5d2SYehuda Sadeh 		dst->cls.method_len = src->cls.method_len;
865bb873b53SIlya Dryomov 		dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
8663d14c5d2SYehuda Sadeh 		break;
867a40c4f10SYehuda Sadeh 	case CEPH_OSD_OP_WATCH:
868a40c4f10SYehuda Sadeh 		dst->watch.cookie = cpu_to_le64(src->watch.cookie);
869922dab61SIlya Dryomov 		dst->watch.ver = cpu_to_le64(0);
870922dab61SIlya Dryomov 		dst->watch.op = src->watch.op;
871922dab61SIlya Dryomov 		dst->watch.gen = cpu_to_le32(src->watch.gen);
872922dab61SIlya Dryomov 		break;
873922dab61SIlya Dryomov 	case CEPH_OSD_OP_NOTIFY_ACK:
874a40c4f10SYehuda Sadeh 		break;
87519079203SIlya Dryomov 	case CEPH_OSD_OP_NOTIFY:
87619079203SIlya Dryomov 		dst->notify.cookie = cpu_to_le64(src->notify.cookie);
87719079203SIlya Dryomov 		break;
878a4ed38d7SDouglas Fuller 	case CEPH_OSD_OP_LIST_WATCHERS:
879a4ed38d7SDouglas Fuller 		break;
880c647b8a8SIlya Dryomov 	case CEPH_OSD_OP_SETALLOCHINT:
881c647b8a8SIlya Dryomov 		dst->alloc_hint.expected_object_size =
882c647b8a8SIlya Dryomov 		    cpu_to_le64(src->alloc_hint.expected_object_size);
883c647b8a8SIlya Dryomov 		dst->alloc_hint.expected_write_size =
884c647b8a8SIlya Dryomov 		    cpu_to_le64(src->alloc_hint.expected_write_size);
885c647b8a8SIlya Dryomov 		break;
886d74b50beSYan, Zheng 	case CEPH_OSD_OP_SETXATTR:
887d74b50beSYan, Zheng 	case CEPH_OSD_OP_CMPXATTR:
888d74b50beSYan, Zheng 		dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
889d74b50beSYan, Zheng 		dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
890d74b50beSYan, Zheng 		dst->xattr.cmp_op = src->xattr.cmp_op;
891d74b50beSYan, Zheng 		dst->xattr.cmp_mode = src->xattr.cmp_mode;
892d74b50beSYan, Zheng 		break;
893864e9197SYan, Zheng 	case CEPH_OSD_OP_CREATE:
894864e9197SYan, Zheng 	case CEPH_OSD_OP_DELETE:
895864e9197SYan, Zheng 		break;
8963d14c5d2SYehuda Sadeh 	default:
8974c46459cSAlex Elder 		pr_err("unsupported osd opcode %s\n",
8988f63ca2dSAlex Elder 			ceph_osd_op_name(src->op));
8994c46459cSAlex Elder 		WARN_ON(1);
900a8dd0a37SAlex Elder 
901a8dd0a37SAlex Elder 		return 0;
9023d14c5d2SYehuda Sadeh 	}
9037b25bf5fSIlya Dryomov 
904a8dd0a37SAlex Elder 	dst->op = cpu_to_le16(src->op);
9057b25bf5fSIlya Dryomov 	dst->flags = cpu_to_le32(src->flags);
906de2aa102SIlya Dryomov 	dst->payload_len = cpu_to_le32(src->indata_len);
907175face2SAlex Elder 
908bb873b53SIlya Dryomov 	return src->indata_len;
9093d14c5d2SYehuda Sadeh }
9103d14c5d2SYehuda Sadeh 
9113d14c5d2SYehuda Sadeh /*
9123d14c5d2SYehuda Sadeh  * build new request AND message, calculate layout, and adjust file
9133d14c5d2SYehuda Sadeh  * extent as needed.
9143d14c5d2SYehuda Sadeh  *
9153d14c5d2SYehuda Sadeh  * if the file was recently truncated, we include information about its
9163d14c5d2SYehuda Sadeh  * old and new size so that the object can be updated appropriately.  (we
9173d14c5d2SYehuda Sadeh  * avoid synchronously deleting truncated objects because it's slow.)
9183d14c5d2SYehuda Sadeh  */
9193d14c5d2SYehuda Sadeh struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
9203d14c5d2SYehuda Sadeh 					       struct ceph_file_layout *layout,
9213d14c5d2SYehuda Sadeh 					       struct ceph_vino vino,
922715e4cd4SYan, Zheng 					       u64 off, u64 *plen,
923715e4cd4SYan, Zheng 					       unsigned int which, int num_ops,
9243d14c5d2SYehuda Sadeh 					       int opcode, int flags,
9253d14c5d2SYehuda Sadeh 					       struct ceph_snap_context *snapc,
9263d14c5d2SYehuda Sadeh 					       u32 truncate_seq,
9273d14c5d2SYehuda Sadeh 					       u64 truncate_size,
928153e5167SAlex Elder 					       bool use_mempool)
9293d14c5d2SYehuda Sadeh {
9303d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
93175d1c941SAlex Elder 	u64 objnum = 0;
93275d1c941SAlex Elder 	u64 objoff = 0;
93375d1c941SAlex Elder 	u64 objlen = 0;
9346816282dSSage Weil 	int r;
9353d14c5d2SYehuda Sadeh 
936ad7a60deSLi Wang 	BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
937864e9197SYan, Zheng 	       opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
938864e9197SYan, Zheng 	       opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
9393d14c5d2SYehuda Sadeh 
940acead002SAlex Elder 	req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
941ae7ca4a3SAlex Elder 					GFP_NOFS);
94213d1ad16SIlya Dryomov 	if (!req) {
94313d1ad16SIlya Dryomov 		r = -ENOMEM;
94413d1ad16SIlya Dryomov 		goto fail;
94513d1ad16SIlya Dryomov 	}
94679528734SAlex Elder 
9473d14c5d2SYehuda Sadeh 	/* calculate max write size */
948a19dadfbSAlex Elder 	r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
94913d1ad16SIlya Dryomov 	if (r)
95013d1ad16SIlya Dryomov 		goto fail;
951a19dadfbSAlex Elder 
952864e9197SYan, Zheng 	if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
953144cba14SYan, Zheng 		osd_req_op_init(req, which, opcode, 0);
954864e9197SYan, Zheng 	} else {
9557627151eSYan, Zheng 		u32 object_size = layout->object_size;
956864e9197SYan, Zheng 		u32 object_base = off - objoff;
957ccca4e37SYan, Zheng 		if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
958d18d1e28SAlex Elder 			if (truncate_size <= object_base) {
959d18d1e28SAlex Elder 				truncate_size = 0;
960d18d1e28SAlex Elder 			} else {
961d18d1e28SAlex Elder 				truncate_size -= object_base;
962d18d1e28SAlex Elder 				if (truncate_size > object_size)
963d18d1e28SAlex Elder 					truncate_size = object_size;
964d18d1e28SAlex Elder 			}
965ccca4e37SYan, Zheng 		}
966715e4cd4SYan, Zheng 		osd_req_op_extent_init(req, which, opcode, objoff, objlen,
967b0270324SAlex Elder 				       truncate_size, truncate_seq);
968864e9197SYan, Zheng 	}
969d18d1e28SAlex Elder 
970a1f4020aSJeff Layton 	req->r_abort_on_full = true;
971bb873b53SIlya Dryomov 	req->r_flags = flags;
9727627151eSYan, Zheng 	req->r_base_oloc.pool = layout->pool_id;
97330c156d9SYan, Zheng 	req->r_base_oloc.pool_ns = ceph_try_get_string(layout->pool_ns);
974d30291b9SIlya Dryomov 	ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum);
975dbe0fc41SAlex Elder 
976bb873b53SIlya Dryomov 	req->r_snapid = vino.snap;
977bb873b53SIlya Dryomov 	if (flags & CEPH_OSD_FLAG_WRITE)
978bb873b53SIlya Dryomov 		req->r_data_offset = off;
979bb873b53SIlya Dryomov 
98013d1ad16SIlya Dryomov 	r = ceph_osdc_alloc_messages(req, GFP_NOFS);
98113d1ad16SIlya Dryomov 	if (r)
98213d1ad16SIlya Dryomov 		goto fail;
98313d1ad16SIlya Dryomov 
9843d14c5d2SYehuda Sadeh 	return req;
98513d1ad16SIlya Dryomov 
98613d1ad16SIlya Dryomov fail:
98713d1ad16SIlya Dryomov 	ceph_osdc_put_request(req);
98813d1ad16SIlya Dryomov 	return ERR_PTR(r);
9893d14c5d2SYehuda Sadeh }
9903d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_new_request);
9913d14c5d2SYehuda Sadeh 
9923d14c5d2SYehuda Sadeh /*
9933d14c5d2SYehuda Sadeh  * We keep osd requests in an rbtree, sorted by ->r_tid.
9943d14c5d2SYehuda Sadeh  */
995fcd00b68SIlya Dryomov DEFINE_RB_FUNCS(request, struct ceph_osd_request, r_tid, r_node)
9964609245eSIlya Dryomov DEFINE_RB_FUNCS(request_mc, struct ceph_osd_request, r_tid, r_mc_node)
9973d14c5d2SYehuda Sadeh 
9980247a0cfSIlya Dryomov static bool osd_homeless(struct ceph_osd *osd)
9990247a0cfSIlya Dryomov {
10000247a0cfSIlya Dryomov 	return osd->o_osd == CEPH_HOMELESS_OSD;
10010247a0cfSIlya Dryomov }
10020247a0cfSIlya Dryomov 
10035aea3dcdSIlya Dryomov static bool osd_registered(struct ceph_osd *osd)
10043d14c5d2SYehuda Sadeh {
10055aea3dcdSIlya Dryomov 	verify_osdc_locked(osd->o_osdc);
10063d14c5d2SYehuda Sadeh 
10075aea3dcdSIlya Dryomov 	return !RB_EMPTY_NODE(&osd->o_node);
10083d14c5d2SYehuda Sadeh }
10093d14c5d2SYehuda Sadeh 
10103d14c5d2SYehuda Sadeh /*
10110247a0cfSIlya Dryomov  * Assumes @osd is zero-initialized.
10120247a0cfSIlya Dryomov  */
10130247a0cfSIlya Dryomov static void osd_init(struct ceph_osd *osd)
10140247a0cfSIlya Dryomov {
101502113a0fSElena Reshetova 	refcount_set(&osd->o_ref, 1);
10160247a0cfSIlya Dryomov 	RB_CLEAR_NODE(&osd->o_node);
10175aea3dcdSIlya Dryomov 	osd->o_requests = RB_ROOT;
1018922dab61SIlya Dryomov 	osd->o_linger_requests = RB_ROOT;
1019a02a946dSIlya Dryomov 	osd->o_backoff_mappings = RB_ROOT;
1020a02a946dSIlya Dryomov 	osd->o_backoffs_by_id = RB_ROOT;
10210247a0cfSIlya Dryomov 	INIT_LIST_HEAD(&osd->o_osd_lru);
10220247a0cfSIlya Dryomov 	INIT_LIST_HEAD(&osd->o_keepalive_item);
10230247a0cfSIlya Dryomov 	osd->o_incarnation = 1;
10245aea3dcdSIlya Dryomov 	mutex_init(&osd->lock);
10250247a0cfSIlya Dryomov }
10260247a0cfSIlya Dryomov 
10270247a0cfSIlya Dryomov static void osd_cleanup(struct ceph_osd *osd)
10280247a0cfSIlya Dryomov {
10290247a0cfSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&osd->o_node));
10305aea3dcdSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
1031922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
1032a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoff_mappings));
1033a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoffs_by_id));
10340247a0cfSIlya Dryomov 	WARN_ON(!list_empty(&osd->o_osd_lru));
10350247a0cfSIlya Dryomov 	WARN_ON(!list_empty(&osd->o_keepalive_item));
10360247a0cfSIlya Dryomov 
10370247a0cfSIlya Dryomov 	if (osd->o_auth.authorizer) {
10380247a0cfSIlya Dryomov 		WARN_ON(osd_homeless(osd));
10390247a0cfSIlya Dryomov 		ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
10400247a0cfSIlya Dryomov 	}
10410247a0cfSIlya Dryomov }
10420247a0cfSIlya Dryomov 
10430247a0cfSIlya Dryomov /*
10443d14c5d2SYehuda Sadeh  * Track open sessions with osds.
10453d14c5d2SYehuda Sadeh  */
1046e10006f8SAlex Elder static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
10473d14c5d2SYehuda Sadeh {
10483d14c5d2SYehuda Sadeh 	struct ceph_osd *osd;
10493d14c5d2SYehuda Sadeh 
10500247a0cfSIlya Dryomov 	WARN_ON(onum == CEPH_HOMELESS_OSD);
10510247a0cfSIlya Dryomov 
10527a28f59bSIlya Dryomov 	osd = kzalloc(sizeof(*osd), GFP_NOIO | __GFP_NOFAIL);
10530247a0cfSIlya Dryomov 	osd_init(osd);
10543d14c5d2SYehuda Sadeh 	osd->o_osdc = osdc;
1055e10006f8SAlex Elder 	osd->o_osd = onum;
10563d14c5d2SYehuda Sadeh 
1057b7a9e5ddSSage Weil 	ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
10583d14c5d2SYehuda Sadeh 
10593d14c5d2SYehuda Sadeh 	return osd;
10603d14c5d2SYehuda Sadeh }
10613d14c5d2SYehuda Sadeh 
10623d14c5d2SYehuda Sadeh static struct ceph_osd *get_osd(struct ceph_osd *osd)
10633d14c5d2SYehuda Sadeh {
106402113a0fSElena Reshetova 	if (refcount_inc_not_zero(&osd->o_ref)) {
106502113a0fSElena Reshetova 		dout("get_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref)-1,
106602113a0fSElena Reshetova 		     refcount_read(&osd->o_ref));
10673d14c5d2SYehuda Sadeh 		return osd;
10683d14c5d2SYehuda Sadeh 	} else {
10693d14c5d2SYehuda Sadeh 		dout("get_osd %p FAIL\n", osd);
10703d14c5d2SYehuda Sadeh 		return NULL;
10713d14c5d2SYehuda Sadeh 	}
10723d14c5d2SYehuda Sadeh }
10733d14c5d2SYehuda Sadeh 
10743d14c5d2SYehuda Sadeh static void put_osd(struct ceph_osd *osd)
10753d14c5d2SYehuda Sadeh {
107602113a0fSElena Reshetova 	dout("put_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref),
107702113a0fSElena Reshetova 	     refcount_read(&osd->o_ref) - 1);
107802113a0fSElena Reshetova 	if (refcount_dec_and_test(&osd->o_ref)) {
10790247a0cfSIlya Dryomov 		osd_cleanup(osd);
10803d14c5d2SYehuda Sadeh 		kfree(osd);
10813d14c5d2SYehuda Sadeh 	}
10823d14c5d2SYehuda Sadeh }
10833d14c5d2SYehuda Sadeh 
1084fcd00b68SIlya Dryomov DEFINE_RB_FUNCS(osd, struct ceph_osd, o_osd, o_node)
1085fcd00b68SIlya Dryomov 
10869dd2845cSIlya Dryomov static void __move_osd_to_lru(struct ceph_osd *osd)
10873d14c5d2SYehuda Sadeh {
10889dd2845cSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
10899dd2845cSIlya Dryomov 
10909dd2845cSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
10913d14c5d2SYehuda Sadeh 	BUG_ON(!list_empty(&osd->o_osd_lru));
1092bbf37ec3SIlya Dryomov 
10939dd2845cSIlya Dryomov 	spin_lock(&osdc->osd_lru_lock);
10943d14c5d2SYehuda Sadeh 	list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
10959dd2845cSIlya Dryomov 	spin_unlock(&osdc->osd_lru_lock);
10969dd2845cSIlya Dryomov 
1097a319bf56SIlya Dryomov 	osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
10983d14c5d2SYehuda Sadeh }
10993d14c5d2SYehuda Sadeh 
11009dd2845cSIlya Dryomov static void maybe_move_osd_to_lru(struct ceph_osd *osd)
1101bbf37ec3SIlya Dryomov {
11025aea3dcdSIlya Dryomov 	if (RB_EMPTY_ROOT(&osd->o_requests) &&
1103922dab61SIlya Dryomov 	    RB_EMPTY_ROOT(&osd->o_linger_requests))
11049dd2845cSIlya Dryomov 		__move_osd_to_lru(osd);
1105bbf37ec3SIlya Dryomov }
1106bbf37ec3SIlya Dryomov 
11073d14c5d2SYehuda Sadeh static void __remove_osd_from_lru(struct ceph_osd *osd)
11083d14c5d2SYehuda Sadeh {
11099dd2845cSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
11109dd2845cSIlya Dryomov 
11119dd2845cSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
11129dd2845cSIlya Dryomov 
11139dd2845cSIlya Dryomov 	spin_lock(&osdc->osd_lru_lock);
11143d14c5d2SYehuda Sadeh 	if (!list_empty(&osd->o_osd_lru))
11153d14c5d2SYehuda Sadeh 		list_del_init(&osd->o_osd_lru);
11169dd2845cSIlya Dryomov 	spin_unlock(&osdc->osd_lru_lock);
11173d14c5d2SYehuda Sadeh }
11183d14c5d2SYehuda Sadeh 
11193d14c5d2SYehuda Sadeh /*
11205aea3dcdSIlya Dryomov  * Close the connection and assign any leftover requests to the
11215aea3dcdSIlya Dryomov  * homeless session.
11225aea3dcdSIlya Dryomov  */
11235aea3dcdSIlya Dryomov static void close_osd(struct ceph_osd *osd)
11245aea3dcdSIlya Dryomov {
11255aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
11265aea3dcdSIlya Dryomov 	struct rb_node *n;
11275aea3dcdSIlya Dryomov 
11285aea3dcdSIlya Dryomov 	verify_osdc_wrlocked(osdc);
11295aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
11305aea3dcdSIlya Dryomov 
11315aea3dcdSIlya Dryomov 	ceph_con_close(&osd->o_con);
11325aea3dcdSIlya Dryomov 
11335aea3dcdSIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; ) {
11345aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
11355aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
11365aea3dcdSIlya Dryomov 
11375aea3dcdSIlya Dryomov 		n = rb_next(n); /* unlink_request() */
11385aea3dcdSIlya Dryomov 
11395aea3dcdSIlya Dryomov 		dout(" reassigning req %p tid %llu\n", req, req->r_tid);
11405aea3dcdSIlya Dryomov 		unlink_request(osd, req);
11415aea3dcdSIlya Dryomov 		link_request(&osdc->homeless_osd, req);
11425aea3dcdSIlya Dryomov 	}
1143922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_linger_requests); n; ) {
1144922dab61SIlya Dryomov 		struct ceph_osd_linger_request *lreq =
1145922dab61SIlya Dryomov 		    rb_entry(n, struct ceph_osd_linger_request, node);
1146922dab61SIlya Dryomov 
1147922dab61SIlya Dryomov 		n = rb_next(n); /* unlink_linger() */
1148922dab61SIlya Dryomov 
1149922dab61SIlya Dryomov 		dout(" reassigning lreq %p linger_id %llu\n", lreq,
1150922dab61SIlya Dryomov 		     lreq->linger_id);
1151922dab61SIlya Dryomov 		unlink_linger(osd, lreq);
1152922dab61SIlya Dryomov 		link_linger(&osdc->homeless_osd, lreq);
1153922dab61SIlya Dryomov 	}
1154a02a946dSIlya Dryomov 	clear_backoffs(osd);
11555aea3dcdSIlya Dryomov 
11565aea3dcdSIlya Dryomov 	__remove_osd_from_lru(osd);
11575aea3dcdSIlya Dryomov 	erase_osd(&osdc->osds, osd);
11585aea3dcdSIlya Dryomov 	put_osd(osd);
11595aea3dcdSIlya Dryomov }
11605aea3dcdSIlya Dryomov 
11615aea3dcdSIlya Dryomov /*
11623d14c5d2SYehuda Sadeh  * reset osd connect
11633d14c5d2SYehuda Sadeh  */
11645aea3dcdSIlya Dryomov static int reopen_osd(struct ceph_osd *osd)
11653d14c5d2SYehuda Sadeh {
1166c3acb181SAlex Elder 	struct ceph_entity_addr *peer_addr;
11673d14c5d2SYehuda Sadeh 
11685aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
11695aea3dcdSIlya Dryomov 
11705aea3dcdSIlya Dryomov 	if (RB_EMPTY_ROOT(&osd->o_requests) &&
1171922dab61SIlya Dryomov 	    RB_EMPTY_ROOT(&osd->o_linger_requests)) {
11725aea3dcdSIlya Dryomov 		close_osd(osd);
1173c3acb181SAlex Elder 		return -ENODEV;
1174c3acb181SAlex Elder 	}
1175c3acb181SAlex Elder 
11765aea3dcdSIlya Dryomov 	peer_addr = &osd->o_osdc->osdmap->osd_addr[osd->o_osd];
1177c3acb181SAlex Elder 	if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
11783d14c5d2SYehuda Sadeh 			!ceph_con_opened(&osd->o_con)) {
11795aea3dcdSIlya Dryomov 		struct rb_node *n;
1180c3acb181SAlex Elder 
11813d14c5d2SYehuda Sadeh 		dout("osd addr hasn't changed and connection never opened, "
11820b4af2e8SIlya Dryomov 		     "letting msgr retry\n");
11833d14c5d2SYehuda Sadeh 		/* touch each r_stamp for handle_timeout()'s benfit */
11845aea3dcdSIlya Dryomov 		for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
11855aea3dcdSIlya Dryomov 			struct ceph_osd_request *req =
11865aea3dcdSIlya Dryomov 			    rb_entry(n, struct ceph_osd_request, r_node);
11873d14c5d2SYehuda Sadeh 			req->r_stamp = jiffies;
11885aea3dcdSIlya Dryomov 		}
1189c3acb181SAlex Elder 
1190c3acb181SAlex Elder 		return -EAGAIN;
11913d14c5d2SYehuda Sadeh 	}
1192c3acb181SAlex Elder 
1193c3acb181SAlex Elder 	ceph_con_close(&osd->o_con);
1194c3acb181SAlex Elder 	ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1195c3acb181SAlex Elder 	osd->o_incarnation++;
1196c3acb181SAlex Elder 
1197c3acb181SAlex Elder 	return 0;
11983d14c5d2SYehuda Sadeh }
11993d14c5d2SYehuda Sadeh 
12005aea3dcdSIlya Dryomov static struct ceph_osd *lookup_create_osd(struct ceph_osd_client *osdc, int o,
12015aea3dcdSIlya Dryomov 					  bool wrlocked)
12023d14c5d2SYehuda Sadeh {
12035aea3dcdSIlya Dryomov 	struct ceph_osd *osd;
12045aea3dcdSIlya Dryomov 
12055aea3dcdSIlya Dryomov 	if (wrlocked)
12065aea3dcdSIlya Dryomov 		verify_osdc_wrlocked(osdc);
12075aea3dcdSIlya Dryomov 	else
12085aea3dcdSIlya Dryomov 		verify_osdc_locked(osdc);
12095aea3dcdSIlya Dryomov 
12105aea3dcdSIlya Dryomov 	if (o != CEPH_HOMELESS_OSD)
12115aea3dcdSIlya Dryomov 		osd = lookup_osd(&osdc->osds, o);
12125aea3dcdSIlya Dryomov 	else
12135aea3dcdSIlya Dryomov 		osd = &osdc->homeless_osd;
12145aea3dcdSIlya Dryomov 	if (!osd) {
12155aea3dcdSIlya Dryomov 		if (!wrlocked)
12165aea3dcdSIlya Dryomov 			return ERR_PTR(-EAGAIN);
12175aea3dcdSIlya Dryomov 
12185aea3dcdSIlya Dryomov 		osd = create_osd(osdc, o);
12195aea3dcdSIlya Dryomov 		insert_osd(&osdc->osds, osd);
12205aea3dcdSIlya Dryomov 		ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
12215aea3dcdSIlya Dryomov 			      &osdc->osdmap->osd_addr[osd->o_osd]);
12225aea3dcdSIlya Dryomov 	}
12235aea3dcdSIlya Dryomov 
12245aea3dcdSIlya Dryomov 	dout("%s osdc %p osd%d -> osd %p\n", __func__, osdc, o, osd);
12255aea3dcdSIlya Dryomov 	return osd;
1226a40c4f10SYehuda Sadeh }
1227a40c4f10SYehuda Sadeh 
12283d14c5d2SYehuda Sadeh /*
12295aea3dcdSIlya Dryomov  * Create request <-> OSD session relation.
12305aea3dcdSIlya Dryomov  *
12315aea3dcdSIlya Dryomov  * @req has to be assigned a tid, @osd may be homeless.
12323d14c5d2SYehuda Sadeh  */
12335aea3dcdSIlya Dryomov static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req)
12343d14c5d2SYehuda Sadeh {
12355aea3dcdSIlya Dryomov 	verify_osd_locked(osd);
12365aea3dcdSIlya Dryomov 	WARN_ON(!req->r_tid || req->r_osd);
12375aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
123835f9f8a0SSage Weil 	     req, req->r_tid);
12395aea3dcdSIlya Dryomov 
12405aea3dcdSIlya Dryomov 	if (!osd_homeless(osd))
12415aea3dcdSIlya Dryomov 		__remove_osd_from_lru(osd);
12425aea3dcdSIlya Dryomov 	else
12435aea3dcdSIlya Dryomov 		atomic_inc(&osd->o_osdc->num_homeless);
12445aea3dcdSIlya Dryomov 
12455aea3dcdSIlya Dryomov 	get_osd(osd);
12465aea3dcdSIlya Dryomov 	insert_request(&osd->o_requests, req);
12475aea3dcdSIlya Dryomov 	req->r_osd = osd;
124835f9f8a0SSage Weil }
124935f9f8a0SSage Weil 
12505aea3dcdSIlya Dryomov static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req)
12513d14c5d2SYehuda Sadeh {
12525aea3dcdSIlya Dryomov 	verify_osd_locked(osd);
12535aea3dcdSIlya Dryomov 	WARN_ON(req->r_osd != osd);
12545aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
12555aea3dcdSIlya Dryomov 	     req, req->r_tid);
12565aea3dcdSIlya Dryomov 
12575aea3dcdSIlya Dryomov 	req->r_osd = NULL;
12585aea3dcdSIlya Dryomov 	erase_request(&osd->o_requests, req);
12595aea3dcdSIlya Dryomov 	put_osd(osd);
12605aea3dcdSIlya Dryomov 
12615aea3dcdSIlya Dryomov 	if (!osd_homeless(osd))
12625aea3dcdSIlya Dryomov 		maybe_move_osd_to_lru(osd);
12635aea3dcdSIlya Dryomov 	else
12645aea3dcdSIlya Dryomov 		atomic_dec(&osd->o_osdc->num_homeless);
12653d14c5d2SYehuda Sadeh }
12663d14c5d2SYehuda Sadeh 
126763244fa1SIlya Dryomov static bool __pool_full(struct ceph_pg_pool_info *pi)
126863244fa1SIlya Dryomov {
126963244fa1SIlya Dryomov 	return pi->flags & CEPH_POOL_FLAG_FULL;
127063244fa1SIlya Dryomov }
127163244fa1SIlya Dryomov 
127242c1b124SIlya Dryomov static bool have_pool_full(struct ceph_osd_client *osdc)
127342c1b124SIlya Dryomov {
127442c1b124SIlya Dryomov 	struct rb_node *n;
127542c1b124SIlya Dryomov 
127642c1b124SIlya Dryomov 	for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
127742c1b124SIlya Dryomov 		struct ceph_pg_pool_info *pi =
127842c1b124SIlya Dryomov 		    rb_entry(n, struct ceph_pg_pool_info, node);
127942c1b124SIlya Dryomov 
128042c1b124SIlya Dryomov 		if (__pool_full(pi))
128142c1b124SIlya Dryomov 			return true;
128242c1b124SIlya Dryomov 	}
128342c1b124SIlya Dryomov 
128442c1b124SIlya Dryomov 	return false;
128542c1b124SIlya Dryomov }
128642c1b124SIlya Dryomov 
12875aea3dcdSIlya Dryomov static bool pool_full(struct ceph_osd_client *osdc, s64 pool_id)
12885aea3dcdSIlya Dryomov {
12895aea3dcdSIlya Dryomov 	struct ceph_pg_pool_info *pi;
12905aea3dcdSIlya Dryomov 
12915aea3dcdSIlya Dryomov 	pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
12925aea3dcdSIlya Dryomov 	if (!pi)
12935aea3dcdSIlya Dryomov 		return false;
12945aea3dcdSIlya Dryomov 
12955aea3dcdSIlya Dryomov 	return __pool_full(pi);
12965aea3dcdSIlya Dryomov }
12975aea3dcdSIlya Dryomov 
12983d14c5d2SYehuda Sadeh /*
1299d29adb34SJosh Durgin  * Returns whether a request should be blocked from being sent
1300d29adb34SJosh Durgin  * based on the current osdmap and osd_client settings.
1301d29adb34SJosh Durgin  */
130263244fa1SIlya Dryomov static bool target_should_be_paused(struct ceph_osd_client *osdc,
130363244fa1SIlya Dryomov 				    const struct ceph_osd_request_target *t,
130463244fa1SIlya Dryomov 				    struct ceph_pg_pool_info *pi)
130563244fa1SIlya Dryomov {
1306b7ec35b3SIlya Dryomov 	bool pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
1307b7ec35b3SIlya Dryomov 	bool pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
1308b7ec35b3SIlya Dryomov 		       ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
130963244fa1SIlya Dryomov 		       __pool_full(pi);
131063244fa1SIlya Dryomov 
13116d637a54SIlya Dryomov 	WARN_ON(pi->id != t->target_oloc.pool);
131258eb7932SJeff Layton 	return ((t->flags & CEPH_OSD_FLAG_READ) && pauserd) ||
131358eb7932SJeff Layton 	       ((t->flags & CEPH_OSD_FLAG_WRITE) && pausewr) ||
131458eb7932SJeff Layton 	       (osdc->osdmap->epoch < osdc->epoch_barrier);
131563244fa1SIlya Dryomov }
131663244fa1SIlya Dryomov 
131763244fa1SIlya Dryomov enum calc_target_result {
131863244fa1SIlya Dryomov 	CALC_TARGET_NO_ACTION = 0,
131963244fa1SIlya Dryomov 	CALC_TARGET_NEED_RESEND,
132063244fa1SIlya Dryomov 	CALC_TARGET_POOL_DNE,
132163244fa1SIlya Dryomov };
132263244fa1SIlya Dryomov 
132363244fa1SIlya Dryomov static enum calc_target_result calc_target(struct ceph_osd_client *osdc,
132463244fa1SIlya Dryomov 					   struct ceph_osd_request_target *t,
13257de030d6SIlya Dryomov 					   struct ceph_connection *con,
132663244fa1SIlya Dryomov 					   bool any_change)
132763244fa1SIlya Dryomov {
132863244fa1SIlya Dryomov 	struct ceph_pg_pool_info *pi;
132963244fa1SIlya Dryomov 	struct ceph_pg pgid, last_pgid;
133063244fa1SIlya Dryomov 	struct ceph_osds up, acting;
133163244fa1SIlya Dryomov 	bool force_resend = false;
133284ed45dfSIlya Dryomov 	bool unpaused = false;
133384ed45dfSIlya Dryomov 	bool legacy_change;
13347de030d6SIlya Dryomov 	bool split = false;
1335b7ec35b3SIlya Dryomov 	bool sort_bitwise = ceph_osdmap_flag(osdc, CEPH_OSDMAP_SORTBITWISE);
1336ae78dd81SIlya Dryomov 	bool recovery_deletes = ceph_osdmap_flag(osdc,
1337ae78dd81SIlya Dryomov 						 CEPH_OSDMAP_RECOVERY_DELETES);
133863244fa1SIlya Dryomov 	enum calc_target_result ct_res;
133963244fa1SIlya Dryomov 	int ret;
134063244fa1SIlya Dryomov 
134104c7d789SIlya Dryomov 	t->epoch = osdc->osdmap->epoch;
134263244fa1SIlya Dryomov 	pi = ceph_pg_pool_by_id(osdc->osdmap, t->base_oloc.pool);
134363244fa1SIlya Dryomov 	if (!pi) {
134463244fa1SIlya Dryomov 		t->osd = CEPH_HOMELESS_OSD;
134563244fa1SIlya Dryomov 		ct_res = CALC_TARGET_POOL_DNE;
134663244fa1SIlya Dryomov 		goto out;
134763244fa1SIlya Dryomov 	}
134863244fa1SIlya Dryomov 
134963244fa1SIlya Dryomov 	if (osdc->osdmap->epoch == pi->last_force_request_resend) {
1350dc93e0e2SIlya Dryomov 		if (t->last_force_resend < pi->last_force_request_resend) {
1351dc93e0e2SIlya Dryomov 			t->last_force_resend = pi->last_force_request_resend;
135263244fa1SIlya Dryomov 			force_resend = true;
1353dc93e0e2SIlya Dryomov 		} else if (t->last_force_resend == 0) {
135463244fa1SIlya Dryomov 			force_resend = true;
135563244fa1SIlya Dryomov 		}
135663244fa1SIlya Dryomov 	}
135763244fa1SIlya Dryomov 
1358db098ec4SIlya Dryomov 	/* apply tiering */
1359db098ec4SIlya Dryomov 	ceph_oid_copy(&t->target_oid, &t->base_oid);
1360db098ec4SIlya Dryomov 	ceph_oloc_copy(&t->target_oloc, &t->base_oloc);
1361db098ec4SIlya Dryomov 	if ((t->flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
136263244fa1SIlya Dryomov 		if (t->flags & CEPH_OSD_FLAG_READ && pi->read_tier >= 0)
136363244fa1SIlya Dryomov 			t->target_oloc.pool = pi->read_tier;
136463244fa1SIlya Dryomov 		if (t->flags & CEPH_OSD_FLAG_WRITE && pi->write_tier >= 0)
136563244fa1SIlya Dryomov 			t->target_oloc.pool = pi->write_tier;
13666d637a54SIlya Dryomov 
13676d637a54SIlya Dryomov 		pi = ceph_pg_pool_by_id(osdc->osdmap, t->target_oloc.pool);
13686d637a54SIlya Dryomov 		if (!pi) {
13696d637a54SIlya Dryomov 			t->osd = CEPH_HOMELESS_OSD;
13706d637a54SIlya Dryomov 			ct_res = CALC_TARGET_POOL_DNE;
13716d637a54SIlya Dryomov 			goto out;
13726d637a54SIlya Dryomov 		}
137363244fa1SIlya Dryomov 	}
137463244fa1SIlya Dryomov 
1375df28152dSIlya Dryomov 	ret = __ceph_object_locator_to_pg(pi, &t->target_oid, &t->target_oloc,
1376df28152dSIlya Dryomov 					  &pgid);
137763244fa1SIlya Dryomov 	if (ret) {
137863244fa1SIlya Dryomov 		WARN_ON(ret != -ENOENT);
137963244fa1SIlya Dryomov 		t->osd = CEPH_HOMELESS_OSD;
138063244fa1SIlya Dryomov 		ct_res = CALC_TARGET_POOL_DNE;
138163244fa1SIlya Dryomov 		goto out;
138263244fa1SIlya Dryomov 	}
138363244fa1SIlya Dryomov 	last_pgid.pool = pgid.pool;
138463244fa1SIlya Dryomov 	last_pgid.seed = ceph_stable_mod(pgid.seed, t->pg_num, t->pg_num_mask);
138563244fa1SIlya Dryomov 
1386df28152dSIlya Dryomov 	ceph_pg_to_up_acting_osds(osdc->osdmap, pi, &pgid, &up, &acting);
138763244fa1SIlya Dryomov 	if (any_change &&
138863244fa1SIlya Dryomov 	    ceph_is_new_interval(&t->acting,
138963244fa1SIlya Dryomov 				 &acting,
139063244fa1SIlya Dryomov 				 &t->up,
139163244fa1SIlya Dryomov 				 &up,
139263244fa1SIlya Dryomov 				 t->size,
139363244fa1SIlya Dryomov 				 pi->size,
139463244fa1SIlya Dryomov 				 t->min_size,
139563244fa1SIlya Dryomov 				 pi->min_size,
139663244fa1SIlya Dryomov 				 t->pg_num,
139763244fa1SIlya Dryomov 				 pi->pg_num,
139863244fa1SIlya Dryomov 				 t->sort_bitwise,
139963244fa1SIlya Dryomov 				 sort_bitwise,
1400ae78dd81SIlya Dryomov 				 t->recovery_deletes,
1401ae78dd81SIlya Dryomov 				 recovery_deletes,
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;
1424ae78dd81SIlya Dryomov 		t->recovery_deletes = recovery_deletes;
142563244fa1SIlya Dryomov 
142663244fa1SIlya Dryomov 		t->osd = acting.primary;
142763244fa1SIlya Dryomov 	}
142863244fa1SIlya Dryomov 
14297de030d6SIlya Dryomov 	if (unpaused || legacy_change || force_resend ||
14307de030d6SIlya Dryomov 	    (split && con && CEPH_HAVE_FEATURE(con->peer_features,
14317de030d6SIlya Dryomov 					       RESEND_ON_SPLIT)))
143284ed45dfSIlya Dryomov 		ct_res = CALC_TARGET_NEED_RESEND;
143384ed45dfSIlya Dryomov 	else
143484ed45dfSIlya Dryomov 		ct_res = CALC_TARGET_NO_ACTION;
143584ed45dfSIlya Dryomov 
143663244fa1SIlya Dryomov out:
143763244fa1SIlya Dryomov 	dout("%s t %p -> ct_res %d osd %d\n", __func__, t, ct_res, t->osd);
143863244fa1SIlya Dryomov 	return ct_res;
143963244fa1SIlya Dryomov }
144063244fa1SIlya Dryomov 
1441a02a946dSIlya Dryomov static struct ceph_spg_mapping *alloc_spg_mapping(void)
1442a02a946dSIlya Dryomov {
1443a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
1444a02a946dSIlya Dryomov 
1445a02a946dSIlya Dryomov 	spg = kmalloc(sizeof(*spg), GFP_NOIO);
1446a02a946dSIlya Dryomov 	if (!spg)
1447a02a946dSIlya Dryomov 		return NULL;
1448a02a946dSIlya Dryomov 
1449a02a946dSIlya Dryomov 	RB_CLEAR_NODE(&spg->node);
1450a02a946dSIlya Dryomov 	spg->backoffs = RB_ROOT;
1451a02a946dSIlya Dryomov 	return spg;
1452a02a946dSIlya Dryomov }
1453a02a946dSIlya Dryomov 
1454a02a946dSIlya Dryomov static void free_spg_mapping(struct ceph_spg_mapping *spg)
1455a02a946dSIlya Dryomov {
1456a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&spg->node));
1457a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&spg->backoffs));
1458a02a946dSIlya Dryomov 
1459a02a946dSIlya Dryomov 	kfree(spg);
1460a02a946dSIlya Dryomov }
1461a02a946dSIlya Dryomov 
1462a02a946dSIlya Dryomov /*
1463a02a946dSIlya Dryomov  * rbtree of ceph_spg_mapping for handling map<spg_t, ...>, similar to
1464a02a946dSIlya Dryomov  * ceph_pg_mapping.  Used to track OSD backoffs -- a backoff [range] is
1465a02a946dSIlya Dryomov  * defined only within a specific spgid; it does not pass anything to
1466a02a946dSIlya Dryomov  * children on split, or to another primary.
1467a02a946dSIlya Dryomov  */
1468a02a946dSIlya Dryomov DEFINE_RB_FUNCS2(spg_mapping, struct ceph_spg_mapping, spgid, ceph_spg_compare,
1469a02a946dSIlya Dryomov 		 RB_BYPTR, const struct ceph_spg *, node)
1470a02a946dSIlya Dryomov 
1471a02a946dSIlya Dryomov static u64 hoid_get_bitwise_key(const struct ceph_hobject_id *hoid)
1472a02a946dSIlya Dryomov {
1473a02a946dSIlya Dryomov 	return hoid->is_max ? 0x100000000ull : hoid->hash_reverse_bits;
1474a02a946dSIlya Dryomov }
1475a02a946dSIlya Dryomov 
1476a02a946dSIlya Dryomov static void hoid_get_effective_key(const struct ceph_hobject_id *hoid,
1477a02a946dSIlya Dryomov 				   void **pkey, size_t *pkey_len)
1478a02a946dSIlya Dryomov {
1479a02a946dSIlya Dryomov 	if (hoid->key_len) {
1480a02a946dSIlya Dryomov 		*pkey = hoid->key;
1481a02a946dSIlya Dryomov 		*pkey_len = hoid->key_len;
1482a02a946dSIlya Dryomov 	} else {
1483a02a946dSIlya Dryomov 		*pkey = hoid->oid;
1484a02a946dSIlya Dryomov 		*pkey_len = hoid->oid_len;
1485a02a946dSIlya Dryomov 	}
1486a02a946dSIlya Dryomov }
1487a02a946dSIlya Dryomov 
1488a02a946dSIlya Dryomov static int compare_names(const void *name1, size_t name1_len,
1489a02a946dSIlya Dryomov 			 const void *name2, size_t name2_len)
1490a02a946dSIlya Dryomov {
1491a02a946dSIlya Dryomov 	int ret;
1492a02a946dSIlya Dryomov 
1493a02a946dSIlya Dryomov 	ret = memcmp(name1, name2, min(name1_len, name2_len));
1494a02a946dSIlya Dryomov 	if (!ret) {
1495a02a946dSIlya Dryomov 		if (name1_len < name2_len)
1496a02a946dSIlya Dryomov 			ret = -1;
1497a02a946dSIlya Dryomov 		else if (name1_len > name2_len)
1498a02a946dSIlya Dryomov 			ret = 1;
1499a02a946dSIlya Dryomov 	}
1500a02a946dSIlya Dryomov 	return ret;
1501a02a946dSIlya Dryomov }
1502a02a946dSIlya Dryomov 
1503a02a946dSIlya Dryomov static int hoid_compare(const struct ceph_hobject_id *lhs,
1504a02a946dSIlya Dryomov 			const struct ceph_hobject_id *rhs)
1505a02a946dSIlya Dryomov {
1506a02a946dSIlya Dryomov 	void *effective_key1, *effective_key2;
1507a02a946dSIlya Dryomov 	size_t effective_key1_len, effective_key2_len;
1508a02a946dSIlya Dryomov 	int ret;
1509a02a946dSIlya Dryomov 
1510a02a946dSIlya Dryomov 	if (lhs->is_max < rhs->is_max)
1511a02a946dSIlya Dryomov 		return -1;
1512a02a946dSIlya Dryomov 	if (lhs->is_max > rhs->is_max)
1513a02a946dSIlya Dryomov 		return 1;
1514a02a946dSIlya Dryomov 
1515a02a946dSIlya Dryomov 	if (lhs->pool < rhs->pool)
1516a02a946dSIlya Dryomov 		return -1;
1517a02a946dSIlya Dryomov 	if (lhs->pool > rhs->pool)
1518a02a946dSIlya Dryomov 		return 1;
1519a02a946dSIlya Dryomov 
1520a02a946dSIlya Dryomov 	if (hoid_get_bitwise_key(lhs) < hoid_get_bitwise_key(rhs))
1521a02a946dSIlya Dryomov 		return -1;
1522a02a946dSIlya Dryomov 	if (hoid_get_bitwise_key(lhs) > hoid_get_bitwise_key(rhs))
1523a02a946dSIlya Dryomov 		return 1;
1524a02a946dSIlya Dryomov 
1525a02a946dSIlya Dryomov 	ret = compare_names(lhs->nspace, lhs->nspace_len,
1526a02a946dSIlya Dryomov 			    rhs->nspace, rhs->nspace_len);
1527a02a946dSIlya Dryomov 	if (ret)
1528a02a946dSIlya Dryomov 		return ret;
1529a02a946dSIlya Dryomov 
1530a02a946dSIlya Dryomov 	hoid_get_effective_key(lhs, &effective_key1, &effective_key1_len);
1531a02a946dSIlya Dryomov 	hoid_get_effective_key(rhs, &effective_key2, &effective_key2_len);
1532a02a946dSIlya Dryomov 	ret = compare_names(effective_key1, effective_key1_len,
1533a02a946dSIlya Dryomov 			    effective_key2, effective_key2_len);
1534a02a946dSIlya Dryomov 	if (ret)
1535a02a946dSIlya Dryomov 		return ret;
1536a02a946dSIlya Dryomov 
1537a02a946dSIlya Dryomov 	ret = compare_names(lhs->oid, lhs->oid_len, rhs->oid, rhs->oid_len);
1538a02a946dSIlya Dryomov 	if (ret)
1539a02a946dSIlya Dryomov 		return ret;
1540a02a946dSIlya Dryomov 
1541a02a946dSIlya Dryomov 	if (lhs->snapid < rhs->snapid)
1542a02a946dSIlya Dryomov 		return -1;
1543a02a946dSIlya Dryomov 	if (lhs->snapid > rhs->snapid)
1544a02a946dSIlya Dryomov 		return 1;
1545a02a946dSIlya Dryomov 
1546a02a946dSIlya Dryomov 	return 0;
1547a02a946dSIlya Dryomov }
1548a02a946dSIlya Dryomov 
1549a02a946dSIlya Dryomov /*
1550a02a946dSIlya Dryomov  * For decoding ->begin and ->end of MOSDBackoff only -- no MIN/MAX
1551a02a946dSIlya Dryomov  * compat stuff here.
1552a02a946dSIlya Dryomov  *
1553a02a946dSIlya Dryomov  * Assumes @hoid is zero-initialized.
1554a02a946dSIlya Dryomov  */
1555a02a946dSIlya Dryomov static int decode_hoid(void **p, void *end, struct ceph_hobject_id *hoid)
1556a02a946dSIlya Dryomov {
1557a02a946dSIlya Dryomov 	u8 struct_v;
1558a02a946dSIlya Dryomov 	u32 struct_len;
1559a02a946dSIlya Dryomov 	int ret;
1560a02a946dSIlya Dryomov 
1561a02a946dSIlya Dryomov 	ret = ceph_start_decoding(p, end, 4, "hobject_t", &struct_v,
1562a02a946dSIlya Dryomov 				  &struct_len);
1563a02a946dSIlya Dryomov 	if (ret)
1564a02a946dSIlya Dryomov 		return ret;
1565a02a946dSIlya Dryomov 
1566a02a946dSIlya Dryomov 	if (struct_v < 4) {
1567a02a946dSIlya Dryomov 		pr_err("got struct_v %d < 4 of hobject_t\n", struct_v);
1568a02a946dSIlya Dryomov 		goto e_inval;
1569a02a946dSIlya Dryomov 	}
1570a02a946dSIlya Dryomov 
1571a02a946dSIlya Dryomov 	hoid->key = ceph_extract_encoded_string(p, end, &hoid->key_len,
1572a02a946dSIlya Dryomov 						GFP_NOIO);
1573a02a946dSIlya Dryomov 	if (IS_ERR(hoid->key)) {
1574a02a946dSIlya Dryomov 		ret = PTR_ERR(hoid->key);
1575a02a946dSIlya Dryomov 		hoid->key = NULL;
1576a02a946dSIlya Dryomov 		return ret;
1577a02a946dSIlya Dryomov 	}
1578a02a946dSIlya Dryomov 
1579a02a946dSIlya Dryomov 	hoid->oid = ceph_extract_encoded_string(p, end, &hoid->oid_len,
1580a02a946dSIlya Dryomov 						GFP_NOIO);
1581a02a946dSIlya Dryomov 	if (IS_ERR(hoid->oid)) {
1582a02a946dSIlya Dryomov 		ret = PTR_ERR(hoid->oid);
1583a02a946dSIlya Dryomov 		hoid->oid = NULL;
1584a02a946dSIlya Dryomov 		return ret;
1585a02a946dSIlya Dryomov 	}
1586a02a946dSIlya Dryomov 
1587a02a946dSIlya Dryomov 	ceph_decode_64_safe(p, end, hoid->snapid, e_inval);
1588a02a946dSIlya Dryomov 	ceph_decode_32_safe(p, end, hoid->hash, e_inval);
1589a02a946dSIlya Dryomov 	ceph_decode_8_safe(p, end, hoid->is_max, e_inval);
1590a02a946dSIlya Dryomov 
1591a02a946dSIlya Dryomov 	hoid->nspace = ceph_extract_encoded_string(p, end, &hoid->nspace_len,
1592a02a946dSIlya Dryomov 						   GFP_NOIO);
1593a02a946dSIlya Dryomov 	if (IS_ERR(hoid->nspace)) {
1594a02a946dSIlya Dryomov 		ret = PTR_ERR(hoid->nspace);
1595a02a946dSIlya Dryomov 		hoid->nspace = NULL;
1596a02a946dSIlya Dryomov 		return ret;
1597a02a946dSIlya Dryomov 	}
1598a02a946dSIlya Dryomov 
1599a02a946dSIlya Dryomov 	ceph_decode_64_safe(p, end, hoid->pool, e_inval);
1600a02a946dSIlya Dryomov 
1601a02a946dSIlya Dryomov 	ceph_hoid_build_hash_cache(hoid);
1602a02a946dSIlya Dryomov 	return 0;
1603a02a946dSIlya Dryomov 
1604a02a946dSIlya Dryomov e_inval:
1605a02a946dSIlya Dryomov 	return -EINVAL;
1606a02a946dSIlya Dryomov }
1607a02a946dSIlya Dryomov 
1608a02a946dSIlya Dryomov static int hoid_encoding_size(const struct ceph_hobject_id *hoid)
1609a02a946dSIlya Dryomov {
1610a02a946dSIlya Dryomov 	return 8 + 4 + 1 + 8 + /* snapid, hash, is_max, pool */
1611a02a946dSIlya Dryomov 	       4 + hoid->key_len + 4 + hoid->oid_len + 4 + hoid->nspace_len;
1612a02a946dSIlya Dryomov }
1613a02a946dSIlya Dryomov 
1614a02a946dSIlya Dryomov static void encode_hoid(void **p, void *end, const struct ceph_hobject_id *hoid)
1615a02a946dSIlya Dryomov {
1616a02a946dSIlya Dryomov 	ceph_start_encoding(p, 4, 3, hoid_encoding_size(hoid));
1617a02a946dSIlya Dryomov 	ceph_encode_string(p, end, hoid->key, hoid->key_len);
1618a02a946dSIlya Dryomov 	ceph_encode_string(p, end, hoid->oid, hoid->oid_len);
1619a02a946dSIlya Dryomov 	ceph_encode_64(p, hoid->snapid);
1620a02a946dSIlya Dryomov 	ceph_encode_32(p, hoid->hash);
1621a02a946dSIlya Dryomov 	ceph_encode_8(p, hoid->is_max);
1622a02a946dSIlya Dryomov 	ceph_encode_string(p, end, hoid->nspace, hoid->nspace_len);
1623a02a946dSIlya Dryomov 	ceph_encode_64(p, hoid->pool);
1624a02a946dSIlya Dryomov }
1625a02a946dSIlya Dryomov 
1626a02a946dSIlya Dryomov static void free_hoid(struct ceph_hobject_id *hoid)
1627a02a946dSIlya Dryomov {
1628a02a946dSIlya Dryomov 	if (hoid) {
1629a02a946dSIlya Dryomov 		kfree(hoid->key);
1630a02a946dSIlya Dryomov 		kfree(hoid->oid);
1631a02a946dSIlya Dryomov 		kfree(hoid->nspace);
1632a02a946dSIlya Dryomov 		kfree(hoid);
1633a02a946dSIlya Dryomov 	}
1634a02a946dSIlya Dryomov }
1635a02a946dSIlya Dryomov 
1636a02a946dSIlya Dryomov static struct ceph_osd_backoff *alloc_backoff(void)
1637a02a946dSIlya Dryomov {
1638a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
1639a02a946dSIlya Dryomov 
1640a02a946dSIlya Dryomov 	backoff = kzalloc(sizeof(*backoff), GFP_NOIO);
1641a02a946dSIlya Dryomov 	if (!backoff)
1642a02a946dSIlya Dryomov 		return NULL;
1643a02a946dSIlya Dryomov 
1644a02a946dSIlya Dryomov 	RB_CLEAR_NODE(&backoff->spg_node);
1645a02a946dSIlya Dryomov 	RB_CLEAR_NODE(&backoff->id_node);
1646a02a946dSIlya Dryomov 	return backoff;
1647a02a946dSIlya Dryomov }
1648a02a946dSIlya Dryomov 
1649a02a946dSIlya Dryomov static void free_backoff(struct ceph_osd_backoff *backoff)
1650a02a946dSIlya Dryomov {
1651a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&backoff->spg_node));
1652a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&backoff->id_node));
1653a02a946dSIlya Dryomov 
1654a02a946dSIlya Dryomov 	free_hoid(backoff->begin);
1655a02a946dSIlya Dryomov 	free_hoid(backoff->end);
1656a02a946dSIlya Dryomov 	kfree(backoff);
1657a02a946dSIlya Dryomov }
1658a02a946dSIlya Dryomov 
1659a02a946dSIlya Dryomov /*
1660a02a946dSIlya Dryomov  * Within a specific spgid, backoffs are managed by ->begin hoid.
1661a02a946dSIlya Dryomov  */
1662a02a946dSIlya Dryomov DEFINE_RB_INSDEL_FUNCS2(backoff, struct ceph_osd_backoff, begin, hoid_compare,
1663a02a946dSIlya Dryomov 			RB_BYVAL, spg_node);
1664a02a946dSIlya Dryomov 
1665a02a946dSIlya Dryomov static struct ceph_osd_backoff *lookup_containing_backoff(struct rb_root *root,
1666a02a946dSIlya Dryomov 					    const struct ceph_hobject_id *hoid)
1667a02a946dSIlya Dryomov {
1668a02a946dSIlya Dryomov 	struct rb_node *n = root->rb_node;
1669a02a946dSIlya Dryomov 
1670a02a946dSIlya Dryomov 	while (n) {
1671a02a946dSIlya Dryomov 		struct ceph_osd_backoff *cur =
1672a02a946dSIlya Dryomov 		    rb_entry(n, struct ceph_osd_backoff, spg_node);
1673a02a946dSIlya Dryomov 		int cmp;
1674a02a946dSIlya Dryomov 
1675a02a946dSIlya Dryomov 		cmp = hoid_compare(hoid, cur->begin);
1676a02a946dSIlya Dryomov 		if (cmp < 0) {
1677a02a946dSIlya Dryomov 			n = n->rb_left;
1678a02a946dSIlya Dryomov 		} else if (cmp > 0) {
1679a02a946dSIlya Dryomov 			if (hoid_compare(hoid, cur->end) < 0)
1680a02a946dSIlya Dryomov 				return cur;
1681a02a946dSIlya Dryomov 
1682a02a946dSIlya Dryomov 			n = n->rb_right;
1683a02a946dSIlya Dryomov 		} else {
1684a02a946dSIlya Dryomov 			return cur;
1685a02a946dSIlya Dryomov 		}
1686a02a946dSIlya Dryomov 	}
1687a02a946dSIlya Dryomov 
1688a02a946dSIlya Dryomov 	return NULL;
1689a02a946dSIlya Dryomov }
1690a02a946dSIlya Dryomov 
1691a02a946dSIlya Dryomov /*
1692a02a946dSIlya Dryomov  * Each backoff has a unique id within its OSD session.
1693a02a946dSIlya Dryomov  */
1694a02a946dSIlya Dryomov DEFINE_RB_FUNCS(backoff_by_id, struct ceph_osd_backoff, id, id_node)
1695a02a946dSIlya Dryomov 
1696a02a946dSIlya Dryomov static void clear_backoffs(struct ceph_osd *osd)
1697a02a946dSIlya Dryomov {
1698a02a946dSIlya Dryomov 	while (!RB_EMPTY_ROOT(&osd->o_backoff_mappings)) {
1699a02a946dSIlya Dryomov 		struct ceph_spg_mapping *spg =
1700a02a946dSIlya Dryomov 		    rb_entry(rb_first(&osd->o_backoff_mappings),
1701a02a946dSIlya Dryomov 			     struct ceph_spg_mapping, node);
1702a02a946dSIlya Dryomov 
1703a02a946dSIlya Dryomov 		while (!RB_EMPTY_ROOT(&spg->backoffs)) {
1704a02a946dSIlya Dryomov 			struct ceph_osd_backoff *backoff =
1705a02a946dSIlya Dryomov 			    rb_entry(rb_first(&spg->backoffs),
1706a02a946dSIlya Dryomov 				     struct ceph_osd_backoff, spg_node);
1707a02a946dSIlya Dryomov 
1708a02a946dSIlya Dryomov 			erase_backoff(&spg->backoffs, backoff);
1709a02a946dSIlya Dryomov 			erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
1710a02a946dSIlya Dryomov 			free_backoff(backoff);
1711a02a946dSIlya Dryomov 		}
1712a02a946dSIlya Dryomov 		erase_spg_mapping(&osd->o_backoff_mappings, spg);
1713a02a946dSIlya Dryomov 		free_spg_mapping(spg);
1714a02a946dSIlya Dryomov 	}
1715a02a946dSIlya Dryomov }
1716a02a946dSIlya Dryomov 
1717a02a946dSIlya Dryomov /*
1718a02a946dSIlya Dryomov  * Set up a temporary, non-owning view into @t.
1719a02a946dSIlya Dryomov  */
1720a02a946dSIlya Dryomov static void hoid_fill_from_target(struct ceph_hobject_id *hoid,
1721a02a946dSIlya Dryomov 				  const struct ceph_osd_request_target *t)
1722a02a946dSIlya Dryomov {
1723a02a946dSIlya Dryomov 	hoid->key = NULL;
1724a02a946dSIlya Dryomov 	hoid->key_len = 0;
1725a02a946dSIlya Dryomov 	hoid->oid = t->target_oid.name;
1726a02a946dSIlya Dryomov 	hoid->oid_len = t->target_oid.name_len;
1727a02a946dSIlya Dryomov 	hoid->snapid = CEPH_NOSNAP;
1728a02a946dSIlya Dryomov 	hoid->hash = t->pgid.seed;
1729a02a946dSIlya Dryomov 	hoid->is_max = false;
1730a02a946dSIlya Dryomov 	if (t->target_oloc.pool_ns) {
1731a02a946dSIlya Dryomov 		hoid->nspace = t->target_oloc.pool_ns->str;
1732a02a946dSIlya Dryomov 		hoid->nspace_len = t->target_oloc.pool_ns->len;
1733a02a946dSIlya Dryomov 	} else {
1734a02a946dSIlya Dryomov 		hoid->nspace = NULL;
1735a02a946dSIlya Dryomov 		hoid->nspace_len = 0;
1736a02a946dSIlya Dryomov 	}
1737a02a946dSIlya Dryomov 	hoid->pool = t->target_oloc.pool;
1738a02a946dSIlya Dryomov 	ceph_hoid_build_hash_cache(hoid);
1739a02a946dSIlya Dryomov }
1740a02a946dSIlya Dryomov 
1741a02a946dSIlya Dryomov static bool should_plug_request(struct ceph_osd_request *req)
1742a02a946dSIlya Dryomov {
1743a02a946dSIlya Dryomov 	struct ceph_osd *osd = req->r_osd;
1744a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
1745a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
1746a02a946dSIlya Dryomov 	struct ceph_hobject_id hoid;
1747a02a946dSIlya Dryomov 
1748a02a946dSIlya Dryomov 	spg = lookup_spg_mapping(&osd->o_backoff_mappings, &req->r_t.spgid);
1749a02a946dSIlya Dryomov 	if (!spg)
1750a02a946dSIlya Dryomov 		return false;
1751a02a946dSIlya Dryomov 
1752a02a946dSIlya Dryomov 	hoid_fill_from_target(&hoid, &req->r_t);
1753a02a946dSIlya Dryomov 	backoff = lookup_containing_backoff(&spg->backoffs, &hoid);
1754a02a946dSIlya Dryomov 	if (!backoff)
1755a02a946dSIlya Dryomov 		return false;
1756a02a946dSIlya Dryomov 
1757a02a946dSIlya Dryomov 	dout("%s req %p tid %llu backoff osd%d spgid %llu.%xs%d id %llu\n",
1758a02a946dSIlya Dryomov 	     __func__, req, req->r_tid, osd->o_osd, backoff->spgid.pgid.pool,
1759a02a946dSIlya Dryomov 	     backoff->spgid.pgid.seed, backoff->spgid.shard, backoff->id);
1760a02a946dSIlya Dryomov 	return true;
1761a02a946dSIlya Dryomov }
1762a02a946dSIlya Dryomov 
1763bb873b53SIlya Dryomov static void setup_request_data(struct ceph_osd_request *req,
1764bb873b53SIlya Dryomov 			       struct ceph_msg *msg)
17653d14c5d2SYehuda Sadeh {
1766bb873b53SIlya Dryomov 	u32 data_len = 0;
1767bb873b53SIlya Dryomov 	int i;
17683d14c5d2SYehuda Sadeh 
1769bb873b53SIlya Dryomov 	if (!list_empty(&msg->data))
1770bb873b53SIlya Dryomov 		return;
17713d14c5d2SYehuda Sadeh 
1772bb873b53SIlya Dryomov 	WARN_ON(msg->data_length);
1773bb873b53SIlya Dryomov 	for (i = 0; i < req->r_num_ops; i++) {
1774bb873b53SIlya Dryomov 		struct ceph_osd_req_op *op = &req->r_ops[i];
1775bb873b53SIlya Dryomov 
1776bb873b53SIlya Dryomov 		switch (op->op) {
1777bb873b53SIlya Dryomov 		/* request */
1778bb873b53SIlya Dryomov 		case CEPH_OSD_OP_WRITE:
1779bb873b53SIlya Dryomov 		case CEPH_OSD_OP_WRITEFULL:
1780bb873b53SIlya Dryomov 			WARN_ON(op->indata_len != op->extent.length);
1781bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(msg, &op->extent.osd_data);
1782bb873b53SIlya Dryomov 			break;
1783bb873b53SIlya Dryomov 		case CEPH_OSD_OP_SETXATTR:
1784bb873b53SIlya Dryomov 		case CEPH_OSD_OP_CMPXATTR:
1785bb873b53SIlya Dryomov 			WARN_ON(op->indata_len != op->xattr.name_len +
1786bb873b53SIlya Dryomov 						  op->xattr.value_len);
1787bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(msg, &op->xattr.osd_data);
1788bb873b53SIlya Dryomov 			break;
1789922dab61SIlya Dryomov 		case CEPH_OSD_OP_NOTIFY_ACK:
1790922dab61SIlya Dryomov 			ceph_osdc_msg_data_add(msg,
1791922dab61SIlya Dryomov 					       &op->notify_ack.request_data);
1792922dab61SIlya Dryomov 			break;
1793bb873b53SIlya Dryomov 
1794bb873b53SIlya Dryomov 		/* reply */
1795bb873b53SIlya Dryomov 		case CEPH_OSD_OP_STAT:
1796bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(req->r_reply,
1797bb873b53SIlya Dryomov 					       &op->raw_data_in);
1798bb873b53SIlya Dryomov 			break;
1799bb873b53SIlya Dryomov 		case CEPH_OSD_OP_READ:
1800bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(req->r_reply,
1801bb873b53SIlya Dryomov 					       &op->extent.osd_data);
1802bb873b53SIlya Dryomov 			break;
1803a4ed38d7SDouglas Fuller 		case CEPH_OSD_OP_LIST_WATCHERS:
1804a4ed38d7SDouglas Fuller 			ceph_osdc_msg_data_add(req->r_reply,
1805a4ed38d7SDouglas Fuller 					       &op->list_watchers.response_data);
1806a4ed38d7SDouglas Fuller 			break;
1807bb873b53SIlya Dryomov 
1808bb873b53SIlya Dryomov 		/* both */
1809bb873b53SIlya Dryomov 		case CEPH_OSD_OP_CALL:
1810bb873b53SIlya Dryomov 			WARN_ON(op->indata_len != op->cls.class_len +
1811bb873b53SIlya Dryomov 						  op->cls.method_len +
1812bb873b53SIlya Dryomov 						  op->cls.indata_len);
1813bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(msg, &op->cls.request_info);
1814bb873b53SIlya Dryomov 			/* optional, can be NONE */
1815bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(msg, &op->cls.request_data);
1816bb873b53SIlya Dryomov 			/* optional, can be NONE */
1817bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(req->r_reply,
1818bb873b53SIlya Dryomov 					       &op->cls.response_data);
1819bb873b53SIlya Dryomov 			break;
182019079203SIlya Dryomov 		case CEPH_OSD_OP_NOTIFY:
182119079203SIlya Dryomov 			ceph_osdc_msg_data_add(msg,
182219079203SIlya Dryomov 					       &op->notify.request_data);
182319079203SIlya Dryomov 			ceph_osdc_msg_data_add(req->r_reply,
182419079203SIlya Dryomov 					       &op->notify.response_data);
182519079203SIlya Dryomov 			break;
1826bb873b53SIlya Dryomov 		}
1827bb873b53SIlya Dryomov 
1828bb873b53SIlya Dryomov 		data_len += op->indata_len;
1829bb873b53SIlya Dryomov 	}
1830bb873b53SIlya Dryomov 
1831bb873b53SIlya Dryomov 	WARN_ON(data_len != msg->data_length);
1832bb873b53SIlya Dryomov }
1833bb873b53SIlya Dryomov 
18342e59ffd1SIlya Dryomov static void encode_pgid(void **p, const struct ceph_pg *pgid)
18352e59ffd1SIlya Dryomov {
18362e59ffd1SIlya Dryomov 	ceph_encode_8(p, 1);
18372e59ffd1SIlya Dryomov 	ceph_encode_64(p, pgid->pool);
18382e59ffd1SIlya Dryomov 	ceph_encode_32(p, pgid->seed);
18392e59ffd1SIlya Dryomov 	ceph_encode_32(p, -1); /* preferred */
18402e59ffd1SIlya Dryomov }
18412e59ffd1SIlya Dryomov 
18428cb441c0SIlya Dryomov static void encode_spgid(void **p, const struct ceph_spg *spgid)
18438cb441c0SIlya Dryomov {
18448cb441c0SIlya Dryomov 	ceph_start_encoding(p, 1, 1, CEPH_PGID_ENCODING_LEN + 1);
18458cb441c0SIlya Dryomov 	encode_pgid(p, &spgid->pgid);
18468cb441c0SIlya Dryomov 	ceph_encode_8(p, spgid->shard);
18478cb441c0SIlya Dryomov }
18488cb441c0SIlya Dryomov 
18492e59ffd1SIlya Dryomov static void encode_oloc(void **p, void *end,
18502e59ffd1SIlya Dryomov 			const struct ceph_object_locator *oloc)
18512e59ffd1SIlya Dryomov {
18522e59ffd1SIlya Dryomov 	ceph_start_encoding(p, 5, 4, ceph_oloc_encoding_size(oloc));
18532e59ffd1SIlya Dryomov 	ceph_encode_64(p, oloc->pool);
18542e59ffd1SIlya Dryomov 	ceph_encode_32(p, -1); /* preferred */
18552e59ffd1SIlya Dryomov 	ceph_encode_32(p, 0);  /* key len */
18562e59ffd1SIlya Dryomov 	if (oloc->pool_ns)
18572e59ffd1SIlya Dryomov 		ceph_encode_string(p, end, oloc->pool_ns->str,
18582e59ffd1SIlya Dryomov 				   oloc->pool_ns->len);
18592e59ffd1SIlya Dryomov 	else
18602e59ffd1SIlya Dryomov 		ceph_encode_32(p, 0);
18612e59ffd1SIlya Dryomov }
18622e59ffd1SIlya Dryomov 
18638cb441c0SIlya Dryomov static void encode_request_partial(struct ceph_osd_request *req,
18648cb441c0SIlya Dryomov 				   struct ceph_msg *msg)
1865bb873b53SIlya Dryomov {
1866bb873b53SIlya Dryomov 	void *p = msg->front.iov_base;
1867bb873b53SIlya Dryomov 	void *const end = p + msg->front_alloc_len;
1868bb873b53SIlya Dryomov 	u32 data_len = 0;
1869bb873b53SIlya Dryomov 	int i;
1870bb873b53SIlya Dryomov 
1871bb873b53SIlya Dryomov 	if (req->r_flags & CEPH_OSD_FLAG_WRITE) {
1872bb873b53SIlya Dryomov 		/* snapshots aren't writeable */
1873bb873b53SIlya Dryomov 		WARN_ON(req->r_snapid != CEPH_NOSNAP);
1874bb873b53SIlya Dryomov 	} else {
1875bb873b53SIlya Dryomov 		WARN_ON(req->r_mtime.tv_sec || req->r_mtime.tv_nsec ||
1876bb873b53SIlya Dryomov 			req->r_data_offset || req->r_snapc);
1877bb873b53SIlya Dryomov 	}
1878bb873b53SIlya Dryomov 
1879bb873b53SIlya Dryomov 	setup_request_data(req, msg);
1880bb873b53SIlya Dryomov 
18818cb441c0SIlya Dryomov 	encode_spgid(&p, &req->r_t.spgid); /* actual spg */
18828cb441c0SIlya Dryomov 	ceph_encode_32(&p, req->r_t.pgid.seed); /* raw hash */
1883bb873b53SIlya Dryomov 	ceph_encode_32(&p, req->r_osdc->osdmap->epoch);
1884bb873b53SIlya Dryomov 	ceph_encode_32(&p, req->r_flags);
18858cb441c0SIlya Dryomov 
18868cb441c0SIlya Dryomov 	/* reqid */
18878cb441c0SIlya Dryomov 	ceph_start_encoding(&p, 2, 2, sizeof(struct ceph_osd_reqid));
18888cb441c0SIlya Dryomov 	memset(p, 0, sizeof(struct ceph_osd_reqid));
18898cb441c0SIlya Dryomov 	p += sizeof(struct ceph_osd_reqid);
18908cb441c0SIlya Dryomov 
18918cb441c0SIlya Dryomov 	/* trace */
18928cb441c0SIlya Dryomov 	memset(p, 0, sizeof(struct ceph_blkin_trace_info));
18938cb441c0SIlya Dryomov 	p += sizeof(struct ceph_blkin_trace_info);
18948cb441c0SIlya Dryomov 
18958cb441c0SIlya Dryomov 	ceph_encode_32(&p, 0); /* client_inc, always 0 */
1896bb873b53SIlya Dryomov 	ceph_encode_timespec(p, &req->r_mtime);
1897bb873b53SIlya Dryomov 	p += sizeof(struct ceph_timespec);
1898aa26d662SJeff Layton 
18992e59ffd1SIlya Dryomov 	encode_oloc(&p, end, &req->r_t.target_oloc);
19002e59ffd1SIlya Dryomov 	ceph_encode_string(&p, end, req->r_t.target_oid.name,
19012e59ffd1SIlya Dryomov 			   req->r_t.target_oid.name_len);
1902bb873b53SIlya Dryomov 
1903bb873b53SIlya Dryomov 	/* ops, can imply data */
1904bb873b53SIlya Dryomov 	ceph_encode_16(&p, req->r_num_ops);
1905bb873b53SIlya Dryomov 	for (i = 0; i < req->r_num_ops; i++) {
1906bb873b53SIlya Dryomov 		data_len += osd_req_encode_op(p, &req->r_ops[i]);
1907bb873b53SIlya Dryomov 		p += sizeof(struct ceph_osd_op);
1908bb873b53SIlya Dryomov 	}
1909bb873b53SIlya Dryomov 
1910bb873b53SIlya Dryomov 	ceph_encode_64(&p, req->r_snapid); /* snapid */
1911bb873b53SIlya Dryomov 	if (req->r_snapc) {
1912bb873b53SIlya Dryomov 		ceph_encode_64(&p, req->r_snapc->seq);
1913bb873b53SIlya Dryomov 		ceph_encode_32(&p, req->r_snapc->num_snaps);
1914bb873b53SIlya Dryomov 		for (i = 0; i < req->r_snapc->num_snaps; i++)
1915bb873b53SIlya Dryomov 			ceph_encode_64(&p, req->r_snapc->snaps[i]);
1916bb873b53SIlya Dryomov 	} else {
1917bb873b53SIlya Dryomov 		ceph_encode_64(&p, 0); /* snap_seq */
1918bb873b53SIlya Dryomov 		ceph_encode_32(&p, 0); /* snaps len */
1919bb873b53SIlya Dryomov 	}
1920bb873b53SIlya Dryomov 
1921bb873b53SIlya Dryomov 	ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
1922986e8989SIlya Dryomov 	BUG_ON(p > end - 8); /* space for features */
1923bb873b53SIlya Dryomov 
19248cb441c0SIlya Dryomov 	msg->hdr.version = cpu_to_le16(8); /* MOSDOp v8 */
19258cb441c0SIlya Dryomov 	/* front_len is finalized in encode_request_finish() */
1926986e8989SIlya Dryomov 	msg->front.iov_len = p - msg->front.iov_base;
1927986e8989SIlya Dryomov 	msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1928bb873b53SIlya Dryomov 	msg->hdr.data_len = cpu_to_le32(data_len);
1929bb873b53SIlya Dryomov 	/*
1930bb873b53SIlya Dryomov 	 * The header "data_off" is a hint to the receiver allowing it
1931bb873b53SIlya Dryomov 	 * to align received data into its buffers such that there's no
1932bb873b53SIlya Dryomov 	 * need to re-copy it before writing it to disk (direct I/O).
1933bb873b53SIlya Dryomov 	 */
1934bb873b53SIlya Dryomov 	msg->hdr.data_off = cpu_to_le16(req->r_data_offset);
1935bb873b53SIlya Dryomov 
19368cb441c0SIlya Dryomov 	dout("%s req %p msg %p oid %s oid_len %d\n", __func__, req, msg,
19378cb441c0SIlya Dryomov 	     req->r_t.target_oid.name, req->r_t.target_oid.name_len);
19388cb441c0SIlya Dryomov }
19398cb441c0SIlya Dryomov 
19408cb441c0SIlya Dryomov static void encode_request_finish(struct ceph_msg *msg)
19418cb441c0SIlya Dryomov {
19428cb441c0SIlya Dryomov 	void *p = msg->front.iov_base;
1943986e8989SIlya Dryomov 	void *const partial_end = p + msg->front.iov_len;
19448cb441c0SIlya Dryomov 	void *const end = p + msg->front_alloc_len;
19458cb441c0SIlya Dryomov 
19468cb441c0SIlya Dryomov 	if (CEPH_HAVE_FEATURE(msg->con->peer_features, RESEND_ON_SPLIT)) {
19478cb441c0SIlya Dryomov 		/* luminous OSD -- encode features and be done */
1948986e8989SIlya Dryomov 		p = partial_end;
19498cb441c0SIlya Dryomov 		ceph_encode_64(&p, msg->con->peer_features);
19508cb441c0SIlya Dryomov 	} else {
19518cb441c0SIlya Dryomov 		struct {
19528cb441c0SIlya Dryomov 			char spgid[CEPH_ENCODING_START_BLK_LEN +
19538cb441c0SIlya Dryomov 				   CEPH_PGID_ENCODING_LEN + 1];
19548cb441c0SIlya Dryomov 			__le32 hash;
19558cb441c0SIlya Dryomov 			__le32 epoch;
19568cb441c0SIlya Dryomov 			__le32 flags;
19578cb441c0SIlya Dryomov 			char reqid[CEPH_ENCODING_START_BLK_LEN +
19588cb441c0SIlya Dryomov 				   sizeof(struct ceph_osd_reqid)];
19598cb441c0SIlya Dryomov 			char trace[sizeof(struct ceph_blkin_trace_info)];
19608cb441c0SIlya Dryomov 			__le32 client_inc;
19618cb441c0SIlya Dryomov 			struct ceph_timespec mtime;
19628cb441c0SIlya Dryomov 		} __packed head;
19638cb441c0SIlya Dryomov 		struct ceph_pg pgid;
19648cb441c0SIlya Dryomov 		void *oloc, *oid, *tail;
19658cb441c0SIlya Dryomov 		int oloc_len, oid_len, tail_len;
19668cb441c0SIlya Dryomov 		int len;
19678cb441c0SIlya Dryomov 
19688cb441c0SIlya Dryomov 		/*
19698cb441c0SIlya Dryomov 		 * Pre-luminous OSD -- reencode v8 into v4 using @head
19708cb441c0SIlya Dryomov 		 * as a temporary buffer.  Encode the raw PG; the rest
19718cb441c0SIlya Dryomov 		 * is just a matter of moving oloc, oid and tail blobs
19728cb441c0SIlya Dryomov 		 * around.
19738cb441c0SIlya Dryomov 		 */
19748cb441c0SIlya Dryomov 		memcpy(&head, p, sizeof(head));
19758cb441c0SIlya Dryomov 		p += sizeof(head);
19768cb441c0SIlya Dryomov 
19778cb441c0SIlya Dryomov 		oloc = p;
19788cb441c0SIlya Dryomov 		p += CEPH_ENCODING_START_BLK_LEN;
19798cb441c0SIlya Dryomov 		pgid.pool = ceph_decode_64(&p);
19808cb441c0SIlya Dryomov 		p += 4 + 4; /* preferred, key len */
19818cb441c0SIlya Dryomov 		len = ceph_decode_32(&p);
19828cb441c0SIlya Dryomov 		p += len;   /* nspace */
19838cb441c0SIlya Dryomov 		oloc_len = p - oloc;
19848cb441c0SIlya Dryomov 
19858cb441c0SIlya Dryomov 		oid = p;
19868cb441c0SIlya Dryomov 		len = ceph_decode_32(&p);
19878cb441c0SIlya Dryomov 		p += len;
19888cb441c0SIlya Dryomov 		oid_len = p - oid;
19898cb441c0SIlya Dryomov 
19908cb441c0SIlya Dryomov 		tail = p;
1991986e8989SIlya Dryomov 		tail_len = partial_end - p;
19928cb441c0SIlya Dryomov 
19938cb441c0SIlya Dryomov 		p = msg->front.iov_base;
19948cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.client_inc, sizeof(head.client_inc));
19958cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.epoch, sizeof(head.epoch));
19968cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.flags, sizeof(head.flags));
19978cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.mtime, sizeof(head.mtime));
19988cb441c0SIlya Dryomov 
19998cb441c0SIlya Dryomov 		/* reassert_version */
20008cb441c0SIlya Dryomov 		memset(p, 0, sizeof(struct ceph_eversion));
20018cb441c0SIlya Dryomov 		p += sizeof(struct ceph_eversion);
20028cb441c0SIlya Dryomov 
20038cb441c0SIlya Dryomov 		BUG_ON(p >= oloc);
20048cb441c0SIlya Dryomov 		memmove(p, oloc, oloc_len);
20058cb441c0SIlya Dryomov 		p += oloc_len;
20068cb441c0SIlya Dryomov 
20078cb441c0SIlya Dryomov 		pgid.seed = le32_to_cpu(head.hash);
20088cb441c0SIlya Dryomov 		encode_pgid(&p, &pgid); /* raw pg */
20098cb441c0SIlya Dryomov 
20108cb441c0SIlya Dryomov 		BUG_ON(p >= oid);
20118cb441c0SIlya Dryomov 		memmove(p, oid, oid_len);
20128cb441c0SIlya Dryomov 		p += oid_len;
20138cb441c0SIlya Dryomov 
20148cb441c0SIlya Dryomov 		/* tail -- ops, snapid, snapc, retry_attempt */
20158cb441c0SIlya Dryomov 		BUG_ON(p >= tail);
20168cb441c0SIlya Dryomov 		memmove(p, tail, tail_len);
20178cb441c0SIlya Dryomov 		p += tail_len;
20188cb441c0SIlya Dryomov 
20198cb441c0SIlya Dryomov 		msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
20208cb441c0SIlya Dryomov 	}
20218cb441c0SIlya Dryomov 
20228cb441c0SIlya Dryomov 	BUG_ON(p > end);
20238cb441c0SIlya Dryomov 	msg->front.iov_len = p - msg->front.iov_base;
20248cb441c0SIlya Dryomov 	msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
20258cb441c0SIlya Dryomov 
20268cb441c0SIlya Dryomov 	dout("%s msg %p tid %llu %u+%u+%u v%d\n", __func__, msg,
20278cb441c0SIlya Dryomov 	     le64_to_cpu(msg->hdr.tid), le32_to_cpu(msg->hdr.front_len),
20288cb441c0SIlya Dryomov 	     le32_to_cpu(msg->hdr.middle_len), le32_to_cpu(msg->hdr.data_len),
20298cb441c0SIlya Dryomov 	     le16_to_cpu(msg->hdr.version));
2030bb873b53SIlya Dryomov }
2031bb873b53SIlya Dryomov 
2032bb873b53SIlya Dryomov /*
2033bb873b53SIlya Dryomov  * @req has to be assigned a tid and registered.
2034bb873b53SIlya Dryomov  */
2035bb873b53SIlya Dryomov static void send_request(struct ceph_osd_request *req)
2036bb873b53SIlya Dryomov {
2037bb873b53SIlya Dryomov 	struct ceph_osd *osd = req->r_osd;
2038bb873b53SIlya Dryomov 
20395aea3dcdSIlya Dryomov 	verify_osd_locked(osd);
2040bb873b53SIlya Dryomov 	WARN_ON(osd->o_osd != req->r_t.osd);
2041bb873b53SIlya Dryomov 
2042a02a946dSIlya Dryomov 	/* backoff? */
2043a02a946dSIlya Dryomov 	if (should_plug_request(req))
2044a02a946dSIlya Dryomov 		return;
2045a02a946dSIlya Dryomov 
20465aea3dcdSIlya Dryomov 	/*
20475aea3dcdSIlya Dryomov 	 * We may have a previously queued request message hanging
20485aea3dcdSIlya Dryomov 	 * around.  Cancel it to avoid corrupting the msgr.
20495aea3dcdSIlya Dryomov 	 */
20505aea3dcdSIlya Dryomov 	if (req->r_sent)
20515aea3dcdSIlya Dryomov 		ceph_msg_revoke(req->r_request);
20525aea3dcdSIlya Dryomov 
2053bb873b53SIlya Dryomov 	req->r_flags |= CEPH_OSD_FLAG_KNOWN_REDIR;
2054bb873b53SIlya Dryomov 	if (req->r_attempts)
2055bb873b53SIlya Dryomov 		req->r_flags |= CEPH_OSD_FLAG_RETRY;
2056bb873b53SIlya Dryomov 	else
2057bb873b53SIlya Dryomov 		WARN_ON(req->r_flags & CEPH_OSD_FLAG_RETRY);
2058bb873b53SIlya Dryomov 
20598cb441c0SIlya Dryomov 	encode_request_partial(req, req->r_request);
2060bb873b53SIlya Dryomov 
206104c7d789SIlya 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",
2062bb873b53SIlya Dryomov 	     __func__, req, req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed,
2063dc98ff72SIlya Dryomov 	     req->r_t.spgid.pgid.pool, req->r_t.spgid.pgid.seed,
206404c7d789SIlya Dryomov 	     req->r_t.spgid.shard, osd->o_osd, req->r_t.epoch, req->r_flags,
206504c7d789SIlya Dryomov 	     req->r_attempts);
2066bb873b53SIlya Dryomov 
2067bb873b53SIlya Dryomov 	req->r_t.paused = false;
20683d14c5d2SYehuda Sadeh 	req->r_stamp = jiffies;
2069bb873b53SIlya Dryomov 	req->r_attempts++;
20703d14c5d2SYehuda Sadeh 
2071bb873b53SIlya Dryomov 	req->r_sent = osd->o_incarnation;
2072bb873b53SIlya Dryomov 	req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
2073bb873b53SIlya Dryomov 	ceph_con_send(&osd->o_con, ceph_msg_get(req->r_request));
20743d14c5d2SYehuda Sadeh }
20753d14c5d2SYehuda Sadeh 
207642c1b124SIlya Dryomov static void maybe_request_map(struct ceph_osd_client *osdc)
207742c1b124SIlya Dryomov {
207842c1b124SIlya Dryomov 	bool continuous = false;
207942c1b124SIlya Dryomov 
20805aea3dcdSIlya Dryomov 	verify_osdc_locked(osdc);
208142c1b124SIlya Dryomov 	WARN_ON(!osdc->osdmap->epoch);
208242c1b124SIlya Dryomov 
2083b7ec35b3SIlya Dryomov 	if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2084b7ec35b3SIlya Dryomov 	    ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD) ||
2085b7ec35b3SIlya Dryomov 	    ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
208642c1b124SIlya Dryomov 		dout("%s osdc %p continuous\n", __func__, osdc);
208742c1b124SIlya Dryomov 		continuous = true;
208842c1b124SIlya Dryomov 	} else {
208942c1b124SIlya Dryomov 		dout("%s osdc %p onetime\n", __func__, osdc);
209042c1b124SIlya Dryomov 	}
209142c1b124SIlya Dryomov 
209242c1b124SIlya Dryomov 	if (ceph_monc_want_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
209342c1b124SIlya Dryomov 			       osdc->osdmap->epoch + 1, continuous))
209442c1b124SIlya Dryomov 		ceph_monc_renew_subs(&osdc->client->monc);
209542c1b124SIlya Dryomov }
209642c1b124SIlya Dryomov 
2097a1f4020aSJeff Layton static void complete_request(struct ceph_osd_request *req, int err);
20984609245eSIlya Dryomov static void send_map_check(struct ceph_osd_request *req);
20994609245eSIlya Dryomov 
21005aea3dcdSIlya Dryomov static void __submit_request(struct ceph_osd_request *req, bool wrlocked)
21010bbfdfe8SIlya Dryomov {
21025aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
21035aea3dcdSIlya Dryomov 	struct ceph_osd *osd;
21044609245eSIlya Dryomov 	enum calc_target_result ct_res;
21055aea3dcdSIlya Dryomov 	bool need_send = false;
21065aea3dcdSIlya Dryomov 	bool promoted = false;
2107a1f4020aSJeff Layton 	bool need_abort = false;
21080bbfdfe8SIlya Dryomov 
2109b18b9550SIlya Dryomov 	WARN_ON(req->r_tid);
21105aea3dcdSIlya Dryomov 	dout("%s req %p wrlocked %d\n", __func__, req, wrlocked);
21115aea3dcdSIlya Dryomov 
21125aea3dcdSIlya Dryomov again:
21137de030d6SIlya Dryomov 	ct_res = calc_target(osdc, &req->r_t, NULL, false);
21144609245eSIlya Dryomov 	if (ct_res == CALC_TARGET_POOL_DNE && !wrlocked)
21154609245eSIlya Dryomov 		goto promote;
21164609245eSIlya Dryomov 
21175aea3dcdSIlya Dryomov 	osd = lookup_create_osd(osdc, req->r_t.osd, wrlocked);
21185aea3dcdSIlya Dryomov 	if (IS_ERR(osd)) {
21195aea3dcdSIlya Dryomov 		WARN_ON(PTR_ERR(osd) != -EAGAIN || wrlocked);
21205aea3dcdSIlya Dryomov 		goto promote;
21215aea3dcdSIlya Dryomov 	}
21225aea3dcdSIlya Dryomov 
212358eb7932SJeff Layton 	if (osdc->osdmap->epoch < osdc->epoch_barrier) {
212458eb7932SJeff Layton 		dout("req %p epoch %u barrier %u\n", req, osdc->osdmap->epoch,
212558eb7932SJeff Layton 		     osdc->epoch_barrier);
212658eb7932SJeff Layton 		req->r_t.paused = true;
212758eb7932SJeff Layton 		maybe_request_map(osdc);
212858eb7932SJeff Layton 	} else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2129b7ec35b3SIlya Dryomov 		   ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
21305aea3dcdSIlya Dryomov 		dout("req %p pausewr\n", req);
21315aea3dcdSIlya Dryomov 		req->r_t.paused = true;
21325aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
21335aea3dcdSIlya Dryomov 	} else if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
2134b7ec35b3SIlya Dryomov 		   ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
21355aea3dcdSIlya Dryomov 		dout("req %p pauserd\n", req);
21365aea3dcdSIlya Dryomov 		req->r_t.paused = true;
21375aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
21385aea3dcdSIlya Dryomov 	} else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
21395aea3dcdSIlya Dryomov 		   !(req->r_flags & (CEPH_OSD_FLAG_FULL_TRY |
21405aea3dcdSIlya Dryomov 				     CEPH_OSD_FLAG_FULL_FORCE)) &&
2141b7ec35b3SIlya Dryomov 		   (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
21425aea3dcdSIlya Dryomov 		    pool_full(osdc, req->r_t.base_oloc.pool))) {
21435aea3dcdSIlya Dryomov 		dout("req %p full/pool_full\n", req);
21445aea3dcdSIlya Dryomov 		pr_warn_ratelimited("FULL or reached pool quota\n");
21455aea3dcdSIlya Dryomov 		req->r_t.paused = true;
21465aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
2147a1f4020aSJeff Layton 		if (req->r_abort_on_full)
2148a1f4020aSJeff Layton 			need_abort = true;
21495aea3dcdSIlya Dryomov 	} else if (!osd_homeless(osd)) {
21505aea3dcdSIlya Dryomov 		need_send = true;
21510bbfdfe8SIlya Dryomov 	} else {
21525aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
21530bbfdfe8SIlya Dryomov 	}
21540bbfdfe8SIlya Dryomov 
21555aea3dcdSIlya Dryomov 	mutex_lock(&osd->lock);
21565aea3dcdSIlya Dryomov 	/*
21575aea3dcdSIlya Dryomov 	 * Assign the tid atomically with send_request() to protect
21585aea3dcdSIlya Dryomov 	 * multiple writes to the same object from racing with each
21595aea3dcdSIlya Dryomov 	 * other, resulting in out of order ops on the OSDs.
21605aea3dcdSIlya Dryomov 	 */
21615aea3dcdSIlya Dryomov 	req->r_tid = atomic64_inc_return(&osdc->last_tid);
21625aea3dcdSIlya Dryomov 	link_request(osd, req);
21635aea3dcdSIlya Dryomov 	if (need_send)
21645aea3dcdSIlya Dryomov 		send_request(req);
2165a1f4020aSJeff Layton 	else if (need_abort)
2166a1f4020aSJeff Layton 		complete_request(req, -ENOSPC);
21675aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
21685aea3dcdSIlya Dryomov 
21694609245eSIlya Dryomov 	if (ct_res == CALC_TARGET_POOL_DNE)
21704609245eSIlya Dryomov 		send_map_check(req);
21714609245eSIlya Dryomov 
21725aea3dcdSIlya Dryomov 	if (promoted)
21735aea3dcdSIlya Dryomov 		downgrade_write(&osdc->lock);
21745aea3dcdSIlya Dryomov 	return;
21755aea3dcdSIlya Dryomov 
21765aea3dcdSIlya Dryomov promote:
21775aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
21785aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
21795aea3dcdSIlya Dryomov 	wrlocked = true;
21805aea3dcdSIlya Dryomov 	promoted = true;
21815aea3dcdSIlya Dryomov 	goto again;
21820bbfdfe8SIlya Dryomov }
21830bbfdfe8SIlya Dryomov 
21845aea3dcdSIlya Dryomov static void account_request(struct ceph_osd_request *req)
21855aea3dcdSIlya Dryomov {
218654ea0046SIlya Dryomov 	WARN_ON(req->r_flags & (CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK));
2187b18b9550SIlya Dryomov 	WARN_ON(!(req->r_flags & (CEPH_OSD_FLAG_READ | CEPH_OSD_FLAG_WRITE)));
21885aea3dcdSIlya Dryomov 
2189b18b9550SIlya Dryomov 	req->r_flags |= CEPH_OSD_FLAG_ONDISK;
21905aea3dcdSIlya Dryomov 	atomic_inc(&req->r_osdc->num_requests);
21917cc5e38fSIlya Dryomov 
21927cc5e38fSIlya Dryomov 	req->r_start_stamp = jiffies;
21935aea3dcdSIlya Dryomov }
21945aea3dcdSIlya Dryomov 
21955aea3dcdSIlya Dryomov static void submit_request(struct ceph_osd_request *req, bool wrlocked)
21965aea3dcdSIlya Dryomov {
21975aea3dcdSIlya Dryomov 	ceph_osdc_get_request(req);
21985aea3dcdSIlya Dryomov 	account_request(req);
21995aea3dcdSIlya Dryomov 	__submit_request(req, wrlocked);
22005aea3dcdSIlya Dryomov }
22015aea3dcdSIlya Dryomov 
220245ee2c1dSIlya Dryomov static void finish_request(struct ceph_osd_request *req)
22035aea3dcdSIlya Dryomov {
22045aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
22055aea3dcdSIlya Dryomov 
22064609245eSIlya Dryomov 	WARN_ON(lookup_request_mc(&osdc->map_checks, req->r_tid));
220704c7d789SIlya Dryomov 	dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
220804c7d789SIlya Dryomov 
220904c7d789SIlya Dryomov 	if (req->r_osd)
221004c7d789SIlya Dryomov 		unlink_request(req->r_osd, req);
22115aea3dcdSIlya Dryomov 	atomic_dec(&osdc->num_requests);
22125aea3dcdSIlya Dryomov 
22135aea3dcdSIlya Dryomov 	/*
22145aea3dcdSIlya Dryomov 	 * If an OSD has failed or returned and a request has been sent
22155aea3dcdSIlya Dryomov 	 * twice, it's possible to get a reply and end up here while the
22165aea3dcdSIlya Dryomov 	 * request message is queued for delivery.  We will ignore the
22175aea3dcdSIlya Dryomov 	 * reply, so not a big deal, but better to try and catch it.
22185aea3dcdSIlya Dryomov 	 */
22195aea3dcdSIlya Dryomov 	ceph_msg_revoke(req->r_request);
22205aea3dcdSIlya Dryomov 	ceph_msg_revoke_incoming(req->r_reply);
22215aea3dcdSIlya Dryomov }
22225aea3dcdSIlya Dryomov 
2223fe5da05eSIlya Dryomov static void __complete_request(struct ceph_osd_request *req)
2224fe5da05eSIlya Dryomov {
2225b18b9550SIlya Dryomov 	if (req->r_callback) {
2226b18b9550SIlya Dryomov 		dout("%s req %p tid %llu cb %pf result %d\n", __func__, req,
2227b18b9550SIlya Dryomov 		     req->r_tid, req->r_callback, req->r_result);
2228fe5da05eSIlya Dryomov 		req->r_callback(req);
2229b18b9550SIlya Dryomov 	}
2230fe5da05eSIlya Dryomov }
2231fe5da05eSIlya Dryomov 
22324609245eSIlya Dryomov /*
2233b18b9550SIlya Dryomov  * This is open-coded in handle_reply().
22344609245eSIlya Dryomov  */
22354609245eSIlya Dryomov static void complete_request(struct ceph_osd_request *req, int err)
22364609245eSIlya Dryomov {
22374609245eSIlya Dryomov 	dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
22384609245eSIlya Dryomov 
22394609245eSIlya Dryomov 	req->r_result = err;
224045ee2c1dSIlya Dryomov 	finish_request(req);
22414609245eSIlya Dryomov 	__complete_request(req);
2242b18b9550SIlya Dryomov 	complete_all(&req->r_completion);
22434609245eSIlya Dryomov 	ceph_osdc_put_request(req);
22444609245eSIlya Dryomov }
22454609245eSIlya Dryomov 
22464609245eSIlya Dryomov static void cancel_map_check(struct ceph_osd_request *req)
22474609245eSIlya Dryomov {
22484609245eSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
22494609245eSIlya Dryomov 	struct ceph_osd_request *lookup_req;
22504609245eSIlya Dryomov 
22514609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
22524609245eSIlya Dryomov 
22534609245eSIlya Dryomov 	lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
22544609245eSIlya Dryomov 	if (!lookup_req)
22554609245eSIlya Dryomov 		return;
22564609245eSIlya Dryomov 
22574609245eSIlya Dryomov 	WARN_ON(lookup_req != req);
22584609245eSIlya Dryomov 	erase_request_mc(&osdc->map_checks, req);
22594609245eSIlya Dryomov 	ceph_osdc_put_request(req);
22604609245eSIlya Dryomov }
22614609245eSIlya Dryomov 
22625aea3dcdSIlya Dryomov static void cancel_request(struct ceph_osd_request *req)
22635aea3dcdSIlya Dryomov {
22645aea3dcdSIlya Dryomov 	dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
22655aea3dcdSIlya Dryomov 
22664609245eSIlya Dryomov 	cancel_map_check(req);
226745ee2c1dSIlya Dryomov 	finish_request(req);
2268b18b9550SIlya Dryomov 	complete_all(&req->r_completion);
2269c297eb42SIlya Dryomov 	ceph_osdc_put_request(req);
22705aea3dcdSIlya Dryomov }
22715aea3dcdSIlya Dryomov 
22727cc5e38fSIlya Dryomov static void abort_request(struct ceph_osd_request *req, int err)
22737cc5e38fSIlya Dryomov {
22747cc5e38fSIlya Dryomov 	dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
22757cc5e38fSIlya Dryomov 
22767cc5e38fSIlya Dryomov 	cancel_map_check(req);
22777cc5e38fSIlya Dryomov 	complete_request(req, err);
22787cc5e38fSIlya Dryomov }
22797cc5e38fSIlya Dryomov 
228058eb7932SJeff Layton static void update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
228158eb7932SJeff Layton {
228258eb7932SJeff Layton 	if (likely(eb > osdc->epoch_barrier)) {
228358eb7932SJeff Layton 		dout("updating epoch_barrier from %u to %u\n",
228458eb7932SJeff Layton 				osdc->epoch_barrier, eb);
228558eb7932SJeff Layton 		osdc->epoch_barrier = eb;
228658eb7932SJeff Layton 		/* Request map if we're not to the barrier yet */
228758eb7932SJeff Layton 		if (eb > osdc->osdmap->epoch)
228858eb7932SJeff Layton 			maybe_request_map(osdc);
228958eb7932SJeff Layton 	}
229058eb7932SJeff Layton }
229158eb7932SJeff Layton 
229258eb7932SJeff Layton void ceph_osdc_update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
229358eb7932SJeff Layton {
229458eb7932SJeff Layton 	down_read(&osdc->lock);
229558eb7932SJeff Layton 	if (unlikely(eb > osdc->epoch_barrier)) {
229658eb7932SJeff Layton 		up_read(&osdc->lock);
229758eb7932SJeff Layton 		down_write(&osdc->lock);
229858eb7932SJeff Layton 		update_epoch_barrier(osdc, eb);
229958eb7932SJeff Layton 		up_write(&osdc->lock);
230058eb7932SJeff Layton 	} else {
230158eb7932SJeff Layton 		up_read(&osdc->lock);
230258eb7932SJeff Layton 	}
230358eb7932SJeff Layton }
230458eb7932SJeff Layton EXPORT_SYMBOL(ceph_osdc_update_epoch_barrier);
230558eb7932SJeff Layton 
2306fc36d0a4SJeff Layton /*
2307fc36d0a4SJeff Layton  * Drop all pending requests that are stalled waiting on a full condition to
230858eb7932SJeff Layton  * clear, and complete them with ENOSPC as the return code. Set the
230958eb7932SJeff Layton  * osdc->epoch_barrier to the latest map epoch that we've seen if any were
231058eb7932SJeff Layton  * cancelled.
2311fc36d0a4SJeff Layton  */
2312fc36d0a4SJeff Layton static void ceph_osdc_abort_on_full(struct ceph_osd_client *osdc)
2313fc36d0a4SJeff Layton {
2314fc36d0a4SJeff Layton 	struct rb_node *n;
231558eb7932SJeff Layton 	bool victims = false;
2316fc36d0a4SJeff Layton 
2317fc36d0a4SJeff Layton 	dout("enter abort_on_full\n");
2318fc36d0a4SJeff Layton 
2319fc36d0a4SJeff Layton 	if (!ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) && !have_pool_full(osdc))
2320fc36d0a4SJeff Layton 		goto out;
2321fc36d0a4SJeff Layton 
232258eb7932SJeff Layton 	/* Scan list and see if there is anything to abort */
232358eb7932SJeff Layton 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
232458eb7932SJeff Layton 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
232558eb7932SJeff Layton 		struct rb_node *m;
232658eb7932SJeff Layton 
232758eb7932SJeff Layton 		m = rb_first(&osd->o_requests);
232858eb7932SJeff Layton 		while (m) {
232958eb7932SJeff Layton 			struct ceph_osd_request *req = rb_entry(m,
233058eb7932SJeff Layton 					struct ceph_osd_request, r_node);
233158eb7932SJeff Layton 			m = rb_next(m);
233258eb7932SJeff Layton 
233358eb7932SJeff Layton 			if (req->r_abort_on_full) {
233458eb7932SJeff Layton 				victims = true;
233558eb7932SJeff Layton 				break;
233658eb7932SJeff Layton 			}
233758eb7932SJeff Layton 		}
233858eb7932SJeff Layton 		if (victims)
233958eb7932SJeff Layton 			break;
234058eb7932SJeff Layton 	}
234158eb7932SJeff Layton 
234258eb7932SJeff Layton 	if (!victims)
234358eb7932SJeff Layton 		goto out;
234458eb7932SJeff Layton 
234558eb7932SJeff Layton 	/*
234658eb7932SJeff Layton 	 * Update the barrier to current epoch if it's behind that point,
234758eb7932SJeff Layton 	 * since we know we have some calls to be aborted in the tree.
234858eb7932SJeff Layton 	 */
234958eb7932SJeff Layton 	update_epoch_barrier(osdc, osdc->osdmap->epoch);
235058eb7932SJeff Layton 
2351fc36d0a4SJeff Layton 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
2352fc36d0a4SJeff Layton 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
2353fc36d0a4SJeff Layton 		struct rb_node *m;
2354fc36d0a4SJeff Layton 
2355fc36d0a4SJeff Layton 		m = rb_first(&osd->o_requests);
2356fc36d0a4SJeff Layton 		while (m) {
2357fc36d0a4SJeff Layton 			struct ceph_osd_request *req = rb_entry(m,
2358fc36d0a4SJeff Layton 					struct ceph_osd_request, r_node);
2359fc36d0a4SJeff Layton 			m = rb_next(m);
2360fc36d0a4SJeff Layton 
2361fc36d0a4SJeff Layton 			if (req->r_abort_on_full &&
2362fc36d0a4SJeff Layton 			    (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2363fc36d0a4SJeff Layton 			     pool_full(osdc, req->r_t.target_oloc.pool)))
2364fc36d0a4SJeff Layton 				abort_request(req, -ENOSPC);
2365fc36d0a4SJeff Layton 		}
2366fc36d0a4SJeff Layton 	}
2367fc36d0a4SJeff Layton out:
236858eb7932SJeff Layton 	dout("return abort_on_full barrier=%u\n", osdc->epoch_barrier);
2369fc36d0a4SJeff Layton }
2370fc36d0a4SJeff Layton 
23714609245eSIlya Dryomov static void check_pool_dne(struct ceph_osd_request *req)
23724609245eSIlya Dryomov {
23734609245eSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
23744609245eSIlya Dryomov 	struct ceph_osdmap *map = osdc->osdmap;
23754609245eSIlya Dryomov 
23764609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
23774609245eSIlya Dryomov 	WARN_ON(!map->epoch);
23784609245eSIlya Dryomov 
23794609245eSIlya Dryomov 	if (req->r_attempts) {
23804609245eSIlya Dryomov 		/*
23814609245eSIlya Dryomov 		 * We sent a request earlier, which means that
23824609245eSIlya Dryomov 		 * previously the pool existed, and now it does not
23834609245eSIlya Dryomov 		 * (i.e., it was deleted).
23844609245eSIlya Dryomov 		 */
23854609245eSIlya Dryomov 		req->r_map_dne_bound = map->epoch;
23864609245eSIlya Dryomov 		dout("%s req %p tid %llu pool disappeared\n", __func__, req,
23874609245eSIlya Dryomov 		     req->r_tid);
23884609245eSIlya Dryomov 	} else {
23894609245eSIlya Dryomov 		dout("%s req %p tid %llu map_dne_bound %u have %u\n", __func__,
23904609245eSIlya Dryomov 		     req, req->r_tid, req->r_map_dne_bound, map->epoch);
23914609245eSIlya Dryomov 	}
23924609245eSIlya Dryomov 
23934609245eSIlya Dryomov 	if (req->r_map_dne_bound) {
23944609245eSIlya Dryomov 		if (map->epoch >= req->r_map_dne_bound) {
23954609245eSIlya Dryomov 			/* we had a new enough map */
23964609245eSIlya Dryomov 			pr_info_ratelimited("tid %llu pool does not exist\n",
23974609245eSIlya Dryomov 					    req->r_tid);
23984609245eSIlya Dryomov 			complete_request(req, -ENOENT);
23994609245eSIlya Dryomov 		}
24004609245eSIlya Dryomov 	} else {
24014609245eSIlya Dryomov 		send_map_check(req);
24024609245eSIlya Dryomov 	}
24034609245eSIlya Dryomov }
24044609245eSIlya Dryomov 
24054609245eSIlya Dryomov static void map_check_cb(struct ceph_mon_generic_request *greq)
24064609245eSIlya Dryomov {
24074609245eSIlya Dryomov 	struct ceph_osd_client *osdc = &greq->monc->client->osdc;
24084609245eSIlya Dryomov 	struct ceph_osd_request *req;
24094609245eSIlya Dryomov 	u64 tid = greq->private_data;
24104609245eSIlya Dryomov 
24114609245eSIlya Dryomov 	WARN_ON(greq->result || !greq->u.newest);
24124609245eSIlya Dryomov 
24134609245eSIlya Dryomov 	down_write(&osdc->lock);
24144609245eSIlya Dryomov 	req = lookup_request_mc(&osdc->map_checks, tid);
24154609245eSIlya Dryomov 	if (!req) {
24164609245eSIlya Dryomov 		dout("%s tid %llu dne\n", __func__, tid);
24174609245eSIlya Dryomov 		goto out_unlock;
24184609245eSIlya Dryomov 	}
24194609245eSIlya Dryomov 
24204609245eSIlya Dryomov 	dout("%s req %p tid %llu map_dne_bound %u newest %llu\n", __func__,
24214609245eSIlya Dryomov 	     req, req->r_tid, req->r_map_dne_bound, greq->u.newest);
24224609245eSIlya Dryomov 	if (!req->r_map_dne_bound)
24234609245eSIlya Dryomov 		req->r_map_dne_bound = greq->u.newest;
24244609245eSIlya Dryomov 	erase_request_mc(&osdc->map_checks, req);
24254609245eSIlya Dryomov 	check_pool_dne(req);
24264609245eSIlya Dryomov 
24274609245eSIlya Dryomov 	ceph_osdc_put_request(req);
24284609245eSIlya Dryomov out_unlock:
24294609245eSIlya Dryomov 	up_write(&osdc->lock);
24304609245eSIlya Dryomov }
24314609245eSIlya Dryomov 
24324609245eSIlya Dryomov static void send_map_check(struct ceph_osd_request *req)
24334609245eSIlya Dryomov {
24344609245eSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
24354609245eSIlya Dryomov 	struct ceph_osd_request *lookup_req;
24364609245eSIlya Dryomov 	int ret;
24374609245eSIlya Dryomov 
24384609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
24394609245eSIlya Dryomov 
24404609245eSIlya Dryomov 	lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
24414609245eSIlya Dryomov 	if (lookup_req) {
24424609245eSIlya Dryomov 		WARN_ON(lookup_req != req);
24434609245eSIlya Dryomov 		return;
24444609245eSIlya Dryomov 	}
24454609245eSIlya Dryomov 
24464609245eSIlya Dryomov 	ceph_osdc_get_request(req);
24474609245eSIlya Dryomov 	insert_request_mc(&osdc->map_checks, req);
24484609245eSIlya Dryomov 	ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
24494609245eSIlya Dryomov 					  map_check_cb, req->r_tid);
24504609245eSIlya Dryomov 	WARN_ON(ret);
24514609245eSIlya Dryomov }
24524609245eSIlya Dryomov 
24530bbfdfe8SIlya Dryomov /*
2454922dab61SIlya Dryomov  * lingering requests, watch/notify v2 infrastructure
2455922dab61SIlya Dryomov  */
2456922dab61SIlya Dryomov static void linger_release(struct kref *kref)
2457922dab61SIlya Dryomov {
2458922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq =
2459922dab61SIlya Dryomov 	    container_of(kref, struct ceph_osd_linger_request, kref);
2460922dab61SIlya Dryomov 
2461922dab61SIlya Dryomov 	dout("%s lreq %p reg_req %p ping_req %p\n", __func__, lreq,
2462922dab61SIlya Dryomov 	     lreq->reg_req, lreq->ping_req);
2463922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&lreq->node));
2464922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&lreq->osdc_node));
24654609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&lreq->mc_node));
2466922dab61SIlya Dryomov 	WARN_ON(!list_empty(&lreq->scan_item));
2467b07d3c4bSIlya Dryomov 	WARN_ON(!list_empty(&lreq->pending_lworks));
2468922dab61SIlya Dryomov 	WARN_ON(lreq->osd);
2469922dab61SIlya Dryomov 
2470922dab61SIlya Dryomov 	if (lreq->reg_req)
2471922dab61SIlya Dryomov 		ceph_osdc_put_request(lreq->reg_req);
2472922dab61SIlya Dryomov 	if (lreq->ping_req)
2473922dab61SIlya Dryomov 		ceph_osdc_put_request(lreq->ping_req);
2474922dab61SIlya Dryomov 	target_destroy(&lreq->t);
2475922dab61SIlya Dryomov 	kfree(lreq);
2476922dab61SIlya Dryomov }
2477922dab61SIlya Dryomov 
2478922dab61SIlya Dryomov static void linger_put(struct ceph_osd_linger_request *lreq)
2479922dab61SIlya Dryomov {
2480922dab61SIlya Dryomov 	if (lreq)
2481922dab61SIlya Dryomov 		kref_put(&lreq->kref, linger_release);
2482922dab61SIlya Dryomov }
2483922dab61SIlya Dryomov 
2484922dab61SIlya Dryomov static struct ceph_osd_linger_request *
2485922dab61SIlya Dryomov linger_get(struct ceph_osd_linger_request *lreq)
2486922dab61SIlya Dryomov {
2487922dab61SIlya Dryomov 	kref_get(&lreq->kref);
2488922dab61SIlya Dryomov 	return lreq;
2489922dab61SIlya Dryomov }
2490922dab61SIlya Dryomov 
2491922dab61SIlya Dryomov static struct ceph_osd_linger_request *
2492922dab61SIlya Dryomov linger_alloc(struct ceph_osd_client *osdc)
2493922dab61SIlya Dryomov {
2494922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
2495922dab61SIlya Dryomov 
2496922dab61SIlya Dryomov 	lreq = kzalloc(sizeof(*lreq), GFP_NOIO);
2497922dab61SIlya Dryomov 	if (!lreq)
2498922dab61SIlya Dryomov 		return NULL;
2499922dab61SIlya Dryomov 
2500922dab61SIlya Dryomov 	kref_init(&lreq->kref);
2501922dab61SIlya Dryomov 	mutex_init(&lreq->lock);
2502922dab61SIlya Dryomov 	RB_CLEAR_NODE(&lreq->node);
2503922dab61SIlya Dryomov 	RB_CLEAR_NODE(&lreq->osdc_node);
25044609245eSIlya Dryomov 	RB_CLEAR_NODE(&lreq->mc_node);
2505922dab61SIlya Dryomov 	INIT_LIST_HEAD(&lreq->scan_item);
2506b07d3c4bSIlya Dryomov 	INIT_LIST_HEAD(&lreq->pending_lworks);
2507922dab61SIlya Dryomov 	init_completion(&lreq->reg_commit_wait);
250819079203SIlya Dryomov 	init_completion(&lreq->notify_finish_wait);
2509922dab61SIlya Dryomov 
2510922dab61SIlya Dryomov 	lreq->osdc = osdc;
2511922dab61SIlya Dryomov 	target_init(&lreq->t);
2512922dab61SIlya Dryomov 
2513922dab61SIlya Dryomov 	dout("%s lreq %p\n", __func__, lreq);
2514922dab61SIlya Dryomov 	return lreq;
2515922dab61SIlya Dryomov }
2516922dab61SIlya Dryomov 
2517922dab61SIlya Dryomov DEFINE_RB_INSDEL_FUNCS(linger, struct ceph_osd_linger_request, linger_id, node)
2518922dab61SIlya Dryomov DEFINE_RB_FUNCS(linger_osdc, struct ceph_osd_linger_request, linger_id, osdc_node)
25194609245eSIlya Dryomov DEFINE_RB_FUNCS(linger_mc, struct ceph_osd_linger_request, linger_id, mc_node)
2520922dab61SIlya Dryomov 
2521922dab61SIlya Dryomov /*
2522922dab61SIlya Dryomov  * Create linger request <-> OSD session relation.
2523922dab61SIlya Dryomov  *
2524922dab61SIlya Dryomov  * @lreq has to be registered, @osd may be homeless.
2525922dab61SIlya Dryomov  */
2526922dab61SIlya Dryomov static void link_linger(struct ceph_osd *osd,
2527922dab61SIlya Dryomov 			struct ceph_osd_linger_request *lreq)
2528922dab61SIlya Dryomov {
2529922dab61SIlya Dryomov 	verify_osd_locked(osd);
2530922dab61SIlya Dryomov 	WARN_ON(!lreq->linger_id || lreq->osd);
2531922dab61SIlya Dryomov 	dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2532922dab61SIlya Dryomov 	     osd->o_osd, lreq, lreq->linger_id);
2533922dab61SIlya Dryomov 
2534922dab61SIlya Dryomov 	if (!osd_homeless(osd))
2535922dab61SIlya Dryomov 		__remove_osd_from_lru(osd);
2536922dab61SIlya Dryomov 	else
2537922dab61SIlya Dryomov 		atomic_inc(&osd->o_osdc->num_homeless);
2538922dab61SIlya Dryomov 
2539922dab61SIlya Dryomov 	get_osd(osd);
2540922dab61SIlya Dryomov 	insert_linger(&osd->o_linger_requests, lreq);
2541922dab61SIlya Dryomov 	lreq->osd = osd;
2542922dab61SIlya Dryomov }
2543922dab61SIlya Dryomov 
2544922dab61SIlya Dryomov static void unlink_linger(struct ceph_osd *osd,
2545922dab61SIlya Dryomov 			  struct ceph_osd_linger_request *lreq)
2546922dab61SIlya Dryomov {
2547922dab61SIlya Dryomov 	verify_osd_locked(osd);
2548922dab61SIlya Dryomov 	WARN_ON(lreq->osd != osd);
2549922dab61SIlya Dryomov 	dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2550922dab61SIlya Dryomov 	     osd->o_osd, lreq, lreq->linger_id);
2551922dab61SIlya Dryomov 
2552922dab61SIlya Dryomov 	lreq->osd = NULL;
2553922dab61SIlya Dryomov 	erase_linger(&osd->o_linger_requests, lreq);
2554922dab61SIlya Dryomov 	put_osd(osd);
2555922dab61SIlya Dryomov 
2556922dab61SIlya Dryomov 	if (!osd_homeless(osd))
2557922dab61SIlya Dryomov 		maybe_move_osd_to_lru(osd);
2558922dab61SIlya Dryomov 	else
2559922dab61SIlya Dryomov 		atomic_dec(&osd->o_osdc->num_homeless);
2560922dab61SIlya Dryomov }
2561922dab61SIlya Dryomov 
2562922dab61SIlya Dryomov static bool __linger_registered(struct ceph_osd_linger_request *lreq)
2563922dab61SIlya Dryomov {
2564922dab61SIlya Dryomov 	verify_osdc_locked(lreq->osdc);
2565922dab61SIlya Dryomov 
2566922dab61SIlya Dryomov 	return !RB_EMPTY_NODE(&lreq->osdc_node);
2567922dab61SIlya Dryomov }
2568922dab61SIlya Dryomov 
2569922dab61SIlya Dryomov static bool linger_registered(struct ceph_osd_linger_request *lreq)
2570922dab61SIlya Dryomov {
2571922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2572922dab61SIlya Dryomov 	bool registered;
2573922dab61SIlya Dryomov 
2574922dab61SIlya Dryomov 	down_read(&osdc->lock);
2575922dab61SIlya Dryomov 	registered = __linger_registered(lreq);
2576922dab61SIlya Dryomov 	up_read(&osdc->lock);
2577922dab61SIlya Dryomov 
2578922dab61SIlya Dryomov 	return registered;
2579922dab61SIlya Dryomov }
2580922dab61SIlya Dryomov 
2581922dab61SIlya Dryomov static void linger_register(struct ceph_osd_linger_request *lreq)
2582922dab61SIlya Dryomov {
2583922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2584922dab61SIlya Dryomov 
2585922dab61SIlya Dryomov 	verify_osdc_wrlocked(osdc);
2586922dab61SIlya Dryomov 	WARN_ON(lreq->linger_id);
2587922dab61SIlya Dryomov 
2588922dab61SIlya Dryomov 	linger_get(lreq);
2589922dab61SIlya Dryomov 	lreq->linger_id = ++osdc->last_linger_id;
2590922dab61SIlya Dryomov 	insert_linger_osdc(&osdc->linger_requests, lreq);
2591922dab61SIlya Dryomov }
2592922dab61SIlya Dryomov 
2593922dab61SIlya Dryomov static void linger_unregister(struct ceph_osd_linger_request *lreq)
2594922dab61SIlya Dryomov {
2595922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2596922dab61SIlya Dryomov 
2597922dab61SIlya Dryomov 	verify_osdc_wrlocked(osdc);
2598922dab61SIlya Dryomov 
2599922dab61SIlya Dryomov 	erase_linger_osdc(&osdc->linger_requests, lreq);
2600922dab61SIlya Dryomov 	linger_put(lreq);
2601922dab61SIlya Dryomov }
2602922dab61SIlya Dryomov 
2603922dab61SIlya Dryomov static void cancel_linger_request(struct ceph_osd_request *req)
2604922dab61SIlya Dryomov {
2605922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
2606922dab61SIlya Dryomov 
2607922dab61SIlya Dryomov 	WARN_ON(!req->r_linger);
2608922dab61SIlya Dryomov 	cancel_request(req);
2609922dab61SIlya Dryomov 	linger_put(lreq);
2610922dab61SIlya Dryomov }
2611922dab61SIlya Dryomov 
2612922dab61SIlya Dryomov struct linger_work {
2613922dab61SIlya Dryomov 	struct work_struct work;
2614922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
2615b07d3c4bSIlya Dryomov 	struct list_head pending_item;
2616b07d3c4bSIlya Dryomov 	unsigned long queued_stamp;
2617922dab61SIlya Dryomov 
2618922dab61SIlya Dryomov 	union {
2619922dab61SIlya Dryomov 		struct {
2620922dab61SIlya Dryomov 			u64 notify_id;
2621922dab61SIlya Dryomov 			u64 notifier_id;
2622922dab61SIlya Dryomov 			void *payload; /* points into @msg front */
2623922dab61SIlya Dryomov 			size_t payload_len;
2624922dab61SIlya Dryomov 
2625922dab61SIlya Dryomov 			struct ceph_msg *msg; /* for ceph_msg_put() */
2626922dab61SIlya Dryomov 		} notify;
2627922dab61SIlya Dryomov 		struct {
2628922dab61SIlya Dryomov 			int err;
2629922dab61SIlya Dryomov 		} error;
2630922dab61SIlya Dryomov 	};
2631922dab61SIlya Dryomov };
2632922dab61SIlya Dryomov 
2633922dab61SIlya Dryomov static struct linger_work *lwork_alloc(struct ceph_osd_linger_request *lreq,
2634922dab61SIlya Dryomov 				       work_func_t workfn)
2635922dab61SIlya Dryomov {
2636922dab61SIlya Dryomov 	struct linger_work *lwork;
2637922dab61SIlya Dryomov 
2638922dab61SIlya Dryomov 	lwork = kzalloc(sizeof(*lwork), GFP_NOIO);
2639922dab61SIlya Dryomov 	if (!lwork)
2640922dab61SIlya Dryomov 		return NULL;
2641922dab61SIlya Dryomov 
2642922dab61SIlya Dryomov 	INIT_WORK(&lwork->work, workfn);
2643b07d3c4bSIlya Dryomov 	INIT_LIST_HEAD(&lwork->pending_item);
2644922dab61SIlya Dryomov 	lwork->lreq = linger_get(lreq);
2645922dab61SIlya Dryomov 
2646922dab61SIlya Dryomov 	return lwork;
2647922dab61SIlya Dryomov }
2648922dab61SIlya Dryomov 
2649922dab61SIlya Dryomov static void lwork_free(struct linger_work *lwork)
2650922dab61SIlya Dryomov {
2651922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2652922dab61SIlya Dryomov 
2653b07d3c4bSIlya Dryomov 	mutex_lock(&lreq->lock);
2654b07d3c4bSIlya Dryomov 	list_del(&lwork->pending_item);
2655b07d3c4bSIlya Dryomov 	mutex_unlock(&lreq->lock);
2656b07d3c4bSIlya Dryomov 
2657922dab61SIlya Dryomov 	linger_put(lreq);
2658922dab61SIlya Dryomov 	kfree(lwork);
2659922dab61SIlya Dryomov }
2660922dab61SIlya Dryomov 
2661922dab61SIlya Dryomov static void lwork_queue(struct linger_work *lwork)
2662922dab61SIlya Dryomov {
2663922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2664922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2665922dab61SIlya Dryomov 
2666922dab61SIlya Dryomov 	verify_lreq_locked(lreq);
2667b07d3c4bSIlya Dryomov 	WARN_ON(!list_empty(&lwork->pending_item));
2668b07d3c4bSIlya Dryomov 
2669b07d3c4bSIlya Dryomov 	lwork->queued_stamp = jiffies;
2670b07d3c4bSIlya Dryomov 	list_add_tail(&lwork->pending_item, &lreq->pending_lworks);
2671922dab61SIlya Dryomov 	queue_work(osdc->notify_wq, &lwork->work);
2672922dab61SIlya Dryomov }
2673922dab61SIlya Dryomov 
2674922dab61SIlya Dryomov static void do_watch_notify(struct work_struct *w)
2675922dab61SIlya Dryomov {
2676922dab61SIlya Dryomov 	struct linger_work *lwork = container_of(w, struct linger_work, work);
2677922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2678922dab61SIlya Dryomov 
2679922dab61SIlya Dryomov 	if (!linger_registered(lreq)) {
2680922dab61SIlya Dryomov 		dout("%s lreq %p not registered\n", __func__, lreq);
2681922dab61SIlya Dryomov 		goto out;
2682922dab61SIlya Dryomov 	}
2683922dab61SIlya Dryomov 
268419079203SIlya Dryomov 	WARN_ON(!lreq->is_watch);
2685922dab61SIlya Dryomov 	dout("%s lreq %p notify_id %llu notifier_id %llu payload_len %zu\n",
2686922dab61SIlya Dryomov 	     __func__, lreq, lwork->notify.notify_id, lwork->notify.notifier_id,
2687922dab61SIlya Dryomov 	     lwork->notify.payload_len);
2688922dab61SIlya Dryomov 	lreq->wcb(lreq->data, lwork->notify.notify_id, lreq->linger_id,
2689922dab61SIlya Dryomov 		  lwork->notify.notifier_id, lwork->notify.payload,
2690922dab61SIlya Dryomov 		  lwork->notify.payload_len);
2691922dab61SIlya Dryomov 
2692922dab61SIlya Dryomov out:
2693922dab61SIlya Dryomov 	ceph_msg_put(lwork->notify.msg);
2694922dab61SIlya Dryomov 	lwork_free(lwork);
2695922dab61SIlya Dryomov }
2696922dab61SIlya Dryomov 
2697922dab61SIlya Dryomov static void do_watch_error(struct work_struct *w)
2698922dab61SIlya Dryomov {
2699922dab61SIlya Dryomov 	struct linger_work *lwork = container_of(w, struct linger_work, work);
2700922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2701922dab61SIlya Dryomov 
2702922dab61SIlya Dryomov 	if (!linger_registered(lreq)) {
2703922dab61SIlya Dryomov 		dout("%s lreq %p not registered\n", __func__, lreq);
2704922dab61SIlya Dryomov 		goto out;
2705922dab61SIlya Dryomov 	}
2706922dab61SIlya Dryomov 
2707922dab61SIlya Dryomov 	dout("%s lreq %p err %d\n", __func__, lreq, lwork->error.err);
2708922dab61SIlya Dryomov 	lreq->errcb(lreq->data, lreq->linger_id, lwork->error.err);
2709922dab61SIlya Dryomov 
2710922dab61SIlya Dryomov out:
2711922dab61SIlya Dryomov 	lwork_free(lwork);
2712922dab61SIlya Dryomov }
2713922dab61SIlya Dryomov 
2714922dab61SIlya Dryomov static void queue_watch_error(struct ceph_osd_linger_request *lreq)
2715922dab61SIlya Dryomov {
2716922dab61SIlya Dryomov 	struct linger_work *lwork;
2717922dab61SIlya Dryomov 
2718922dab61SIlya Dryomov 	lwork = lwork_alloc(lreq, do_watch_error);
2719922dab61SIlya Dryomov 	if (!lwork) {
2720922dab61SIlya Dryomov 		pr_err("failed to allocate error-lwork\n");
2721922dab61SIlya Dryomov 		return;
2722922dab61SIlya Dryomov 	}
2723922dab61SIlya Dryomov 
2724922dab61SIlya Dryomov 	lwork->error.err = lreq->last_error;
2725922dab61SIlya Dryomov 	lwork_queue(lwork);
2726922dab61SIlya Dryomov }
2727922dab61SIlya Dryomov 
2728922dab61SIlya Dryomov static void linger_reg_commit_complete(struct ceph_osd_linger_request *lreq,
2729922dab61SIlya Dryomov 				       int result)
2730922dab61SIlya Dryomov {
2731922dab61SIlya Dryomov 	if (!completion_done(&lreq->reg_commit_wait)) {
2732922dab61SIlya Dryomov 		lreq->reg_commit_error = (result <= 0 ? result : 0);
2733922dab61SIlya Dryomov 		complete_all(&lreq->reg_commit_wait);
2734922dab61SIlya Dryomov 	}
2735922dab61SIlya Dryomov }
2736922dab61SIlya Dryomov 
2737922dab61SIlya Dryomov static void linger_commit_cb(struct ceph_osd_request *req)
2738922dab61SIlya Dryomov {
2739922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
2740922dab61SIlya Dryomov 
2741922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
2742922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu result %d\n", __func__, lreq,
2743922dab61SIlya Dryomov 	     lreq->linger_id, req->r_result);
2744922dab61SIlya Dryomov 	linger_reg_commit_complete(lreq, req->r_result);
2745922dab61SIlya Dryomov 	lreq->committed = true;
2746922dab61SIlya Dryomov 
274719079203SIlya Dryomov 	if (!lreq->is_watch) {
274819079203SIlya Dryomov 		struct ceph_osd_data *osd_data =
274919079203SIlya Dryomov 		    osd_req_op_data(req, 0, notify, response_data);
275019079203SIlya Dryomov 		void *p = page_address(osd_data->pages[0]);
275119079203SIlya Dryomov 
275219079203SIlya Dryomov 		WARN_ON(req->r_ops[0].op != CEPH_OSD_OP_NOTIFY ||
275319079203SIlya Dryomov 			osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
275419079203SIlya Dryomov 
275519079203SIlya Dryomov 		/* make note of the notify_id */
275619079203SIlya Dryomov 		if (req->r_ops[0].outdata_len >= sizeof(u64)) {
275719079203SIlya Dryomov 			lreq->notify_id = ceph_decode_64(&p);
275819079203SIlya Dryomov 			dout("lreq %p notify_id %llu\n", lreq,
275919079203SIlya Dryomov 			     lreq->notify_id);
276019079203SIlya Dryomov 		} else {
276119079203SIlya Dryomov 			dout("lreq %p no notify_id\n", lreq);
276219079203SIlya Dryomov 		}
276319079203SIlya Dryomov 	}
276419079203SIlya Dryomov 
2765922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
2766922dab61SIlya Dryomov 	linger_put(lreq);
2767922dab61SIlya Dryomov }
2768922dab61SIlya Dryomov 
2769922dab61SIlya Dryomov static int normalize_watch_error(int err)
2770922dab61SIlya Dryomov {
2771922dab61SIlya Dryomov 	/*
2772922dab61SIlya Dryomov 	 * Translate ENOENT -> ENOTCONN so that a delete->disconnection
2773922dab61SIlya Dryomov 	 * notification and a failure to reconnect because we raced with
2774922dab61SIlya Dryomov 	 * the delete appear the same to the user.
2775922dab61SIlya Dryomov 	 */
2776922dab61SIlya Dryomov 	if (err == -ENOENT)
2777922dab61SIlya Dryomov 		err = -ENOTCONN;
2778922dab61SIlya Dryomov 
2779922dab61SIlya Dryomov 	return err;
2780922dab61SIlya Dryomov }
2781922dab61SIlya Dryomov 
2782922dab61SIlya Dryomov static void linger_reconnect_cb(struct ceph_osd_request *req)
2783922dab61SIlya Dryomov {
2784922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
2785922dab61SIlya Dryomov 
2786922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
2787922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu result %d last_error %d\n", __func__,
2788922dab61SIlya Dryomov 	     lreq, lreq->linger_id, req->r_result, lreq->last_error);
2789922dab61SIlya Dryomov 	if (req->r_result < 0) {
2790922dab61SIlya Dryomov 		if (!lreq->last_error) {
2791922dab61SIlya Dryomov 			lreq->last_error = normalize_watch_error(req->r_result);
2792922dab61SIlya Dryomov 			queue_watch_error(lreq);
2793922dab61SIlya Dryomov 		}
2794922dab61SIlya Dryomov 	}
2795922dab61SIlya Dryomov 
2796922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
2797922dab61SIlya Dryomov 	linger_put(lreq);
2798922dab61SIlya Dryomov }
2799922dab61SIlya Dryomov 
2800922dab61SIlya Dryomov static void send_linger(struct ceph_osd_linger_request *lreq)
2801922dab61SIlya Dryomov {
2802922dab61SIlya Dryomov 	struct ceph_osd_request *req = lreq->reg_req;
2803922dab61SIlya Dryomov 	struct ceph_osd_req_op *op = &req->r_ops[0];
2804922dab61SIlya Dryomov 
2805922dab61SIlya Dryomov 	verify_osdc_wrlocked(req->r_osdc);
2806922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2807922dab61SIlya Dryomov 
2808922dab61SIlya Dryomov 	if (req->r_osd)
2809922dab61SIlya Dryomov 		cancel_linger_request(req);
2810922dab61SIlya Dryomov 
2811922dab61SIlya Dryomov 	request_reinit(req);
2812922dab61SIlya Dryomov 	ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
2813922dab61SIlya Dryomov 	ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
2814922dab61SIlya Dryomov 	req->r_flags = lreq->t.flags;
2815922dab61SIlya Dryomov 	req->r_mtime = lreq->mtime;
2816922dab61SIlya Dryomov 
2817922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
281819079203SIlya Dryomov 	if (lreq->is_watch && lreq->committed) {
2819922dab61SIlya Dryomov 		WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2820922dab61SIlya Dryomov 			op->watch.cookie != lreq->linger_id);
2821922dab61SIlya Dryomov 		op->watch.op = CEPH_OSD_WATCH_OP_RECONNECT;
2822922dab61SIlya Dryomov 		op->watch.gen = ++lreq->register_gen;
2823922dab61SIlya Dryomov 		dout("lreq %p reconnect register_gen %u\n", lreq,
2824922dab61SIlya Dryomov 		     op->watch.gen);
2825922dab61SIlya Dryomov 		req->r_callback = linger_reconnect_cb;
2826922dab61SIlya Dryomov 	} else {
282719079203SIlya Dryomov 		if (!lreq->is_watch)
282819079203SIlya Dryomov 			lreq->notify_id = 0;
282919079203SIlya Dryomov 		else
2830922dab61SIlya Dryomov 			WARN_ON(op->watch.op != CEPH_OSD_WATCH_OP_WATCH);
2831922dab61SIlya Dryomov 		dout("lreq %p register\n", lreq);
2832922dab61SIlya Dryomov 		req->r_callback = linger_commit_cb;
2833922dab61SIlya Dryomov 	}
2834922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
2835922dab61SIlya Dryomov 
2836922dab61SIlya Dryomov 	req->r_priv = linger_get(lreq);
2837922dab61SIlya Dryomov 	req->r_linger = true;
2838922dab61SIlya Dryomov 
2839922dab61SIlya Dryomov 	submit_request(req, true);
2840922dab61SIlya Dryomov }
2841922dab61SIlya Dryomov 
2842922dab61SIlya Dryomov static void linger_ping_cb(struct ceph_osd_request *req)
2843922dab61SIlya Dryomov {
2844922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
2845922dab61SIlya Dryomov 
2846922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
2847922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu result %d ping_sent %lu last_error %d\n",
2848922dab61SIlya Dryomov 	     __func__, lreq, lreq->linger_id, req->r_result, lreq->ping_sent,
2849922dab61SIlya Dryomov 	     lreq->last_error);
2850922dab61SIlya Dryomov 	if (lreq->register_gen == req->r_ops[0].watch.gen) {
2851b07d3c4bSIlya Dryomov 		if (!req->r_result) {
2852b07d3c4bSIlya Dryomov 			lreq->watch_valid_thru = lreq->ping_sent;
2853b07d3c4bSIlya Dryomov 		} else if (!lreq->last_error) {
2854922dab61SIlya Dryomov 			lreq->last_error = normalize_watch_error(req->r_result);
2855922dab61SIlya Dryomov 			queue_watch_error(lreq);
2856922dab61SIlya Dryomov 		}
2857922dab61SIlya Dryomov 	} else {
2858922dab61SIlya Dryomov 		dout("lreq %p register_gen %u ignoring old pong %u\n", lreq,
2859922dab61SIlya Dryomov 		     lreq->register_gen, req->r_ops[0].watch.gen);
2860922dab61SIlya Dryomov 	}
2861922dab61SIlya Dryomov 
2862922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
2863922dab61SIlya Dryomov 	linger_put(lreq);
2864922dab61SIlya Dryomov }
2865922dab61SIlya Dryomov 
2866922dab61SIlya Dryomov static void send_linger_ping(struct ceph_osd_linger_request *lreq)
2867922dab61SIlya Dryomov {
2868922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2869922dab61SIlya Dryomov 	struct ceph_osd_request *req = lreq->ping_req;
2870922dab61SIlya Dryomov 	struct ceph_osd_req_op *op = &req->r_ops[0];
2871922dab61SIlya Dryomov 
2872b7ec35b3SIlya Dryomov 	if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
2873922dab61SIlya Dryomov 		dout("%s PAUSERD\n", __func__);
2874922dab61SIlya Dryomov 		return;
2875922dab61SIlya Dryomov 	}
2876922dab61SIlya Dryomov 
2877922dab61SIlya Dryomov 	lreq->ping_sent = jiffies;
2878922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu ping_sent %lu register_gen %u\n",
2879922dab61SIlya Dryomov 	     __func__, lreq, lreq->linger_id, lreq->ping_sent,
2880922dab61SIlya Dryomov 	     lreq->register_gen);
2881922dab61SIlya Dryomov 
2882922dab61SIlya Dryomov 	if (req->r_osd)
2883922dab61SIlya Dryomov 		cancel_linger_request(req);
2884922dab61SIlya Dryomov 
2885922dab61SIlya Dryomov 	request_reinit(req);
2886922dab61SIlya Dryomov 	target_copy(&req->r_t, &lreq->t);
2887922dab61SIlya Dryomov 
2888922dab61SIlya Dryomov 	WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2889922dab61SIlya Dryomov 		op->watch.cookie != lreq->linger_id ||
2890922dab61SIlya Dryomov 		op->watch.op != CEPH_OSD_WATCH_OP_PING);
2891922dab61SIlya Dryomov 	op->watch.gen = lreq->register_gen;
2892922dab61SIlya Dryomov 	req->r_callback = linger_ping_cb;
2893922dab61SIlya Dryomov 	req->r_priv = linger_get(lreq);
2894922dab61SIlya Dryomov 	req->r_linger = true;
2895922dab61SIlya Dryomov 
2896922dab61SIlya Dryomov 	ceph_osdc_get_request(req);
2897922dab61SIlya Dryomov 	account_request(req);
2898922dab61SIlya Dryomov 	req->r_tid = atomic64_inc_return(&osdc->last_tid);
2899922dab61SIlya Dryomov 	link_request(lreq->osd, req);
2900922dab61SIlya Dryomov 	send_request(req);
2901922dab61SIlya Dryomov }
2902922dab61SIlya Dryomov 
2903922dab61SIlya Dryomov static void linger_submit(struct ceph_osd_linger_request *lreq)
2904922dab61SIlya Dryomov {
2905922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2906922dab61SIlya Dryomov 	struct ceph_osd *osd;
2907922dab61SIlya Dryomov 
29087de030d6SIlya Dryomov 	calc_target(osdc, &lreq->t, NULL, false);
2909922dab61SIlya Dryomov 	osd = lookup_create_osd(osdc, lreq->t.osd, true);
2910922dab61SIlya Dryomov 	link_linger(osd, lreq);
2911922dab61SIlya Dryomov 
2912922dab61SIlya Dryomov 	send_linger(lreq);
2913922dab61SIlya Dryomov }
2914922dab61SIlya Dryomov 
29154609245eSIlya Dryomov static void cancel_linger_map_check(struct ceph_osd_linger_request *lreq)
29164609245eSIlya Dryomov {
29174609245eSIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
29184609245eSIlya Dryomov 	struct ceph_osd_linger_request *lookup_lreq;
29194609245eSIlya Dryomov 
29204609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
29214609245eSIlya Dryomov 
29224609245eSIlya Dryomov 	lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
29234609245eSIlya Dryomov 				       lreq->linger_id);
29244609245eSIlya Dryomov 	if (!lookup_lreq)
29254609245eSIlya Dryomov 		return;
29264609245eSIlya Dryomov 
29274609245eSIlya Dryomov 	WARN_ON(lookup_lreq != lreq);
29284609245eSIlya Dryomov 	erase_linger_mc(&osdc->linger_map_checks, lreq);
29294609245eSIlya Dryomov 	linger_put(lreq);
29304609245eSIlya Dryomov }
29314609245eSIlya Dryomov 
2932922dab61SIlya Dryomov /*
2933922dab61SIlya Dryomov  * @lreq has to be both registered and linked.
2934922dab61SIlya Dryomov  */
2935922dab61SIlya Dryomov static void __linger_cancel(struct ceph_osd_linger_request *lreq)
2936922dab61SIlya Dryomov {
293719079203SIlya Dryomov 	if (lreq->is_watch && lreq->ping_req->r_osd)
2938922dab61SIlya Dryomov 		cancel_linger_request(lreq->ping_req);
2939922dab61SIlya Dryomov 	if (lreq->reg_req->r_osd)
2940922dab61SIlya Dryomov 		cancel_linger_request(lreq->reg_req);
29414609245eSIlya Dryomov 	cancel_linger_map_check(lreq);
2942922dab61SIlya Dryomov 	unlink_linger(lreq->osd, lreq);
2943922dab61SIlya Dryomov 	linger_unregister(lreq);
2944922dab61SIlya Dryomov }
2945922dab61SIlya Dryomov 
2946922dab61SIlya Dryomov static void linger_cancel(struct ceph_osd_linger_request *lreq)
2947922dab61SIlya Dryomov {
2948922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2949922dab61SIlya Dryomov 
2950922dab61SIlya Dryomov 	down_write(&osdc->lock);
2951922dab61SIlya Dryomov 	if (__linger_registered(lreq))
2952922dab61SIlya Dryomov 		__linger_cancel(lreq);
2953922dab61SIlya Dryomov 	up_write(&osdc->lock);
2954922dab61SIlya Dryomov }
2955922dab61SIlya Dryomov 
29564609245eSIlya Dryomov static void send_linger_map_check(struct ceph_osd_linger_request *lreq);
29574609245eSIlya Dryomov 
29584609245eSIlya Dryomov static void check_linger_pool_dne(struct ceph_osd_linger_request *lreq)
29594609245eSIlya Dryomov {
29604609245eSIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
29614609245eSIlya Dryomov 	struct ceph_osdmap *map = osdc->osdmap;
29624609245eSIlya Dryomov 
29634609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
29644609245eSIlya Dryomov 	WARN_ON(!map->epoch);
29654609245eSIlya Dryomov 
29664609245eSIlya Dryomov 	if (lreq->register_gen) {
29674609245eSIlya Dryomov 		lreq->map_dne_bound = map->epoch;
29684609245eSIlya Dryomov 		dout("%s lreq %p linger_id %llu pool disappeared\n", __func__,
29694609245eSIlya Dryomov 		     lreq, lreq->linger_id);
29704609245eSIlya Dryomov 	} else {
29714609245eSIlya Dryomov 		dout("%s lreq %p linger_id %llu map_dne_bound %u have %u\n",
29724609245eSIlya Dryomov 		     __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
29734609245eSIlya Dryomov 		     map->epoch);
29744609245eSIlya Dryomov 	}
29754609245eSIlya Dryomov 
29764609245eSIlya Dryomov 	if (lreq->map_dne_bound) {
29774609245eSIlya Dryomov 		if (map->epoch >= lreq->map_dne_bound) {
29784609245eSIlya Dryomov 			/* we had a new enough map */
29794609245eSIlya Dryomov 			pr_info("linger_id %llu pool does not exist\n",
29804609245eSIlya Dryomov 				lreq->linger_id);
29814609245eSIlya Dryomov 			linger_reg_commit_complete(lreq, -ENOENT);
29824609245eSIlya Dryomov 			__linger_cancel(lreq);
29834609245eSIlya Dryomov 		}
29844609245eSIlya Dryomov 	} else {
29854609245eSIlya Dryomov 		send_linger_map_check(lreq);
29864609245eSIlya Dryomov 	}
29874609245eSIlya Dryomov }
29884609245eSIlya Dryomov 
29894609245eSIlya Dryomov static void linger_map_check_cb(struct ceph_mon_generic_request *greq)
29904609245eSIlya Dryomov {
29914609245eSIlya Dryomov 	struct ceph_osd_client *osdc = &greq->monc->client->osdc;
29924609245eSIlya Dryomov 	struct ceph_osd_linger_request *lreq;
29934609245eSIlya Dryomov 	u64 linger_id = greq->private_data;
29944609245eSIlya Dryomov 
29954609245eSIlya Dryomov 	WARN_ON(greq->result || !greq->u.newest);
29964609245eSIlya Dryomov 
29974609245eSIlya Dryomov 	down_write(&osdc->lock);
29984609245eSIlya Dryomov 	lreq = lookup_linger_mc(&osdc->linger_map_checks, linger_id);
29994609245eSIlya Dryomov 	if (!lreq) {
30004609245eSIlya Dryomov 		dout("%s linger_id %llu dne\n", __func__, linger_id);
30014609245eSIlya Dryomov 		goto out_unlock;
30024609245eSIlya Dryomov 	}
30034609245eSIlya Dryomov 
30044609245eSIlya Dryomov 	dout("%s lreq %p linger_id %llu map_dne_bound %u newest %llu\n",
30054609245eSIlya Dryomov 	     __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
30064609245eSIlya Dryomov 	     greq->u.newest);
30074609245eSIlya Dryomov 	if (!lreq->map_dne_bound)
30084609245eSIlya Dryomov 		lreq->map_dne_bound = greq->u.newest;
30094609245eSIlya Dryomov 	erase_linger_mc(&osdc->linger_map_checks, lreq);
30104609245eSIlya Dryomov 	check_linger_pool_dne(lreq);
30114609245eSIlya Dryomov 
30124609245eSIlya Dryomov 	linger_put(lreq);
30134609245eSIlya Dryomov out_unlock:
30144609245eSIlya Dryomov 	up_write(&osdc->lock);
30154609245eSIlya Dryomov }
30164609245eSIlya Dryomov 
30174609245eSIlya Dryomov static void send_linger_map_check(struct ceph_osd_linger_request *lreq)
30184609245eSIlya Dryomov {
30194609245eSIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
30204609245eSIlya Dryomov 	struct ceph_osd_linger_request *lookup_lreq;
30214609245eSIlya Dryomov 	int ret;
30224609245eSIlya Dryomov 
30234609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
30244609245eSIlya Dryomov 
30254609245eSIlya Dryomov 	lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
30264609245eSIlya Dryomov 				       lreq->linger_id);
30274609245eSIlya Dryomov 	if (lookup_lreq) {
30284609245eSIlya Dryomov 		WARN_ON(lookup_lreq != lreq);
30294609245eSIlya Dryomov 		return;
30304609245eSIlya Dryomov 	}
30314609245eSIlya Dryomov 
30324609245eSIlya Dryomov 	linger_get(lreq);
30334609245eSIlya Dryomov 	insert_linger_mc(&osdc->linger_map_checks, lreq);
30344609245eSIlya Dryomov 	ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
30354609245eSIlya Dryomov 					  linger_map_check_cb, lreq->linger_id);
30364609245eSIlya Dryomov 	WARN_ON(ret);
30374609245eSIlya Dryomov }
30384609245eSIlya Dryomov 
3039922dab61SIlya Dryomov static int linger_reg_commit_wait(struct ceph_osd_linger_request *lreq)
3040922dab61SIlya Dryomov {
3041922dab61SIlya Dryomov 	int ret;
3042922dab61SIlya Dryomov 
3043922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3044922dab61SIlya Dryomov 	ret = wait_for_completion_interruptible(&lreq->reg_commit_wait);
3045922dab61SIlya Dryomov 	return ret ?: lreq->reg_commit_error;
3046922dab61SIlya Dryomov }
3047922dab61SIlya Dryomov 
304819079203SIlya Dryomov static int linger_notify_finish_wait(struct ceph_osd_linger_request *lreq)
304919079203SIlya Dryomov {
305019079203SIlya Dryomov 	int ret;
305119079203SIlya Dryomov 
305219079203SIlya Dryomov 	dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
305319079203SIlya Dryomov 	ret = wait_for_completion_interruptible(&lreq->notify_finish_wait);
305419079203SIlya Dryomov 	return ret ?: lreq->notify_finish_error;
305519079203SIlya Dryomov }
305619079203SIlya Dryomov 
3057922dab61SIlya Dryomov /*
3058fbca9635SIlya Dryomov  * Timeout callback, called every N seconds.  When 1 or more OSD
3059fbca9635SIlya Dryomov  * requests has been active for more than N seconds, we send a keepalive
3060fbca9635SIlya Dryomov  * (tag + timestamp) to its OSD to ensure any communications channel
3061fbca9635SIlya Dryomov  * reset is detected.
30623d14c5d2SYehuda Sadeh  */
30633d14c5d2SYehuda Sadeh static void handle_timeout(struct work_struct *work)
30643d14c5d2SYehuda Sadeh {
30653d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc =
30663d14c5d2SYehuda Sadeh 		container_of(work, struct ceph_osd_client, timeout_work.work);
3067a319bf56SIlya Dryomov 	struct ceph_options *opts = osdc->client->options;
30685aea3dcdSIlya Dryomov 	unsigned long cutoff = jiffies - opts->osd_keepalive_timeout;
30697cc5e38fSIlya Dryomov 	unsigned long expiry_cutoff = jiffies - opts->osd_request_timeout;
30705aea3dcdSIlya Dryomov 	LIST_HEAD(slow_osds);
30715aea3dcdSIlya Dryomov 	struct rb_node *n, *p;
30723d14c5d2SYehuda Sadeh 
30735aea3dcdSIlya Dryomov 	dout("%s osdc %p\n", __func__, osdc);
30745aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
30753d14c5d2SYehuda Sadeh 
30763d14c5d2SYehuda Sadeh 	/*
30773d14c5d2SYehuda Sadeh 	 * ping osds that are a bit slow.  this ensures that if there
30783d14c5d2SYehuda Sadeh 	 * is a break in the TCP connection we will notice, and reopen
30793d14c5d2SYehuda Sadeh 	 * a connection with that osd (from the fault callback).
30803d14c5d2SYehuda Sadeh 	 */
30815aea3dcdSIlya Dryomov 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
30825aea3dcdSIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
30835aea3dcdSIlya Dryomov 		bool found = false;
30843d14c5d2SYehuda Sadeh 
30857cc5e38fSIlya Dryomov 		for (p = rb_first(&osd->o_requests); p; ) {
30865aea3dcdSIlya Dryomov 			struct ceph_osd_request *req =
30875aea3dcdSIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
30885aea3dcdSIlya Dryomov 
30897cc5e38fSIlya Dryomov 			p = rb_next(p); /* abort_request() */
30907cc5e38fSIlya Dryomov 
30915aea3dcdSIlya Dryomov 			if (time_before(req->r_stamp, cutoff)) {
30925aea3dcdSIlya Dryomov 				dout(" req %p tid %llu on osd%d is laggy\n",
30935aea3dcdSIlya Dryomov 				     req, req->r_tid, osd->o_osd);
30945aea3dcdSIlya Dryomov 				found = true;
30955aea3dcdSIlya Dryomov 			}
30967cc5e38fSIlya Dryomov 			if (opts->osd_request_timeout &&
30977cc5e38fSIlya Dryomov 			    time_before(req->r_start_stamp, expiry_cutoff)) {
30987cc5e38fSIlya Dryomov 				pr_err_ratelimited("tid %llu on osd%d timeout\n",
30997cc5e38fSIlya Dryomov 				       req->r_tid, osd->o_osd);
31007cc5e38fSIlya Dryomov 				abort_request(req, -ETIMEDOUT);
31017cc5e38fSIlya Dryomov 			}
31025aea3dcdSIlya Dryomov 		}
3103922dab61SIlya Dryomov 		for (p = rb_first(&osd->o_linger_requests); p; p = rb_next(p)) {
3104922dab61SIlya Dryomov 			struct ceph_osd_linger_request *lreq =
3105922dab61SIlya Dryomov 			    rb_entry(p, struct ceph_osd_linger_request, node);
3106922dab61SIlya Dryomov 
3107922dab61SIlya Dryomov 			dout(" lreq %p linger_id %llu is served by osd%d\n",
3108922dab61SIlya Dryomov 			     lreq, lreq->linger_id, osd->o_osd);
3109922dab61SIlya Dryomov 			found = true;
3110922dab61SIlya Dryomov 
3111922dab61SIlya Dryomov 			mutex_lock(&lreq->lock);
311219079203SIlya Dryomov 			if (lreq->is_watch && lreq->committed && !lreq->last_error)
3113922dab61SIlya Dryomov 				send_linger_ping(lreq);
3114922dab61SIlya Dryomov 			mutex_unlock(&lreq->lock);
3115922dab61SIlya Dryomov 		}
31165aea3dcdSIlya Dryomov 
31175aea3dcdSIlya Dryomov 		if (found)
31183d14c5d2SYehuda Sadeh 			list_move_tail(&osd->o_keepalive_item, &slow_osds);
31193d14c5d2SYehuda Sadeh 	}
31205aea3dcdSIlya Dryomov 
31217cc5e38fSIlya Dryomov 	if (opts->osd_request_timeout) {
31227cc5e38fSIlya Dryomov 		for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
31237cc5e38fSIlya Dryomov 			struct ceph_osd_request *req =
31247cc5e38fSIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
31257cc5e38fSIlya Dryomov 
31267cc5e38fSIlya Dryomov 			p = rb_next(p); /* abort_request() */
31277cc5e38fSIlya Dryomov 
31287cc5e38fSIlya Dryomov 			if (time_before(req->r_start_stamp, expiry_cutoff)) {
31297cc5e38fSIlya Dryomov 				pr_err_ratelimited("tid %llu on osd%d timeout\n",
31307cc5e38fSIlya Dryomov 				       req->r_tid, osdc->homeless_osd.o_osd);
31317cc5e38fSIlya Dryomov 				abort_request(req, -ETIMEDOUT);
31327cc5e38fSIlya Dryomov 			}
31337cc5e38fSIlya Dryomov 		}
31347cc5e38fSIlya Dryomov 	}
31357cc5e38fSIlya Dryomov 
31365aea3dcdSIlya Dryomov 	if (atomic_read(&osdc->num_homeless) || !list_empty(&slow_osds))
31375aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
31385aea3dcdSIlya Dryomov 
31393d14c5d2SYehuda Sadeh 	while (!list_empty(&slow_osds)) {
31405aea3dcdSIlya Dryomov 		struct ceph_osd *osd = list_first_entry(&slow_osds,
31415aea3dcdSIlya Dryomov 							struct ceph_osd,
31423d14c5d2SYehuda Sadeh 							o_keepalive_item);
31433d14c5d2SYehuda Sadeh 		list_del_init(&osd->o_keepalive_item);
31443d14c5d2SYehuda Sadeh 		ceph_con_keepalive(&osd->o_con);
31453d14c5d2SYehuda Sadeh 	}
31463d14c5d2SYehuda Sadeh 
31475aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
3148fbca9635SIlya Dryomov 	schedule_delayed_work(&osdc->timeout_work,
3149fbca9635SIlya Dryomov 			      osdc->client->options->osd_keepalive_timeout);
31503d14c5d2SYehuda Sadeh }
31513d14c5d2SYehuda Sadeh 
31523d14c5d2SYehuda Sadeh static void handle_osds_timeout(struct work_struct *work)
31533d14c5d2SYehuda Sadeh {
31543d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc =
31553d14c5d2SYehuda Sadeh 		container_of(work, struct ceph_osd_client,
31563d14c5d2SYehuda Sadeh 			     osds_timeout_work.work);
3157a319bf56SIlya Dryomov 	unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
315842a2c09fSIlya Dryomov 	struct ceph_osd *osd, *nosd;
31593d14c5d2SYehuda Sadeh 
316042a2c09fSIlya Dryomov 	dout("%s osdc %p\n", __func__, osdc);
31615aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
316242a2c09fSIlya Dryomov 	list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
316342a2c09fSIlya Dryomov 		if (time_before(jiffies, osd->lru_ttl))
316442a2c09fSIlya Dryomov 			break;
316542a2c09fSIlya Dryomov 
31665aea3dcdSIlya Dryomov 		WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
3167922dab61SIlya Dryomov 		WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
31685aea3dcdSIlya Dryomov 		close_osd(osd);
316942a2c09fSIlya Dryomov 	}
317042a2c09fSIlya Dryomov 
31715aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
31723d14c5d2SYehuda Sadeh 	schedule_delayed_work(&osdc->osds_timeout_work,
31733d14c5d2SYehuda Sadeh 			      round_jiffies_relative(delay));
31743d14c5d2SYehuda Sadeh }
31753d14c5d2SYehuda Sadeh 
3176205ee118SIlya Dryomov static int ceph_oloc_decode(void **p, void *end,
3177205ee118SIlya Dryomov 			    struct ceph_object_locator *oloc)
3178205ee118SIlya Dryomov {
3179205ee118SIlya Dryomov 	u8 struct_v, struct_cv;
3180205ee118SIlya Dryomov 	u32 len;
3181205ee118SIlya Dryomov 	void *struct_end;
3182205ee118SIlya Dryomov 	int ret = 0;
3183205ee118SIlya Dryomov 
3184205ee118SIlya Dryomov 	ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3185205ee118SIlya Dryomov 	struct_v = ceph_decode_8(p);
3186205ee118SIlya Dryomov 	struct_cv = ceph_decode_8(p);
3187205ee118SIlya Dryomov 	if (struct_v < 3) {
3188205ee118SIlya Dryomov 		pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
3189205ee118SIlya Dryomov 			struct_v, struct_cv);
3190205ee118SIlya Dryomov 		goto e_inval;
3191205ee118SIlya Dryomov 	}
3192205ee118SIlya Dryomov 	if (struct_cv > 6) {
3193205ee118SIlya Dryomov 		pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
3194205ee118SIlya Dryomov 			struct_v, struct_cv);
3195205ee118SIlya Dryomov 		goto e_inval;
3196205ee118SIlya Dryomov 	}
3197205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3198205ee118SIlya Dryomov 	ceph_decode_need(p, end, len, e_inval);
3199205ee118SIlya Dryomov 	struct_end = *p + len;
3200205ee118SIlya Dryomov 
3201205ee118SIlya Dryomov 	oloc->pool = ceph_decode_64(p);
3202205ee118SIlya Dryomov 	*p += 4; /* skip preferred */
3203205ee118SIlya Dryomov 
3204205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3205205ee118SIlya Dryomov 	if (len > 0) {
3206205ee118SIlya Dryomov 		pr_warn("ceph_object_locator::key is set\n");
3207205ee118SIlya Dryomov 		goto e_inval;
3208205ee118SIlya Dryomov 	}
3209205ee118SIlya Dryomov 
3210205ee118SIlya Dryomov 	if (struct_v >= 5) {
3211cd08e0a2SYan, Zheng 		bool changed = false;
3212cd08e0a2SYan, Zheng 
3213205ee118SIlya Dryomov 		len = ceph_decode_32(p);
3214205ee118SIlya Dryomov 		if (len > 0) {
321530c156d9SYan, Zheng 			ceph_decode_need(p, end, len, e_inval);
3216cd08e0a2SYan, Zheng 			if (!oloc->pool_ns ||
3217cd08e0a2SYan, Zheng 			    ceph_compare_string(oloc->pool_ns, *p, len))
3218cd08e0a2SYan, Zheng 				changed = true;
321930c156d9SYan, Zheng 			*p += len;
3220cd08e0a2SYan, Zheng 		} else {
3221cd08e0a2SYan, Zheng 			if (oloc->pool_ns)
3222cd08e0a2SYan, Zheng 				changed = true;
3223cd08e0a2SYan, Zheng 		}
3224cd08e0a2SYan, Zheng 		if (changed) {
3225cd08e0a2SYan, Zheng 			/* redirect changes namespace */
3226cd08e0a2SYan, Zheng 			pr_warn("ceph_object_locator::nspace is changed\n");
3227cd08e0a2SYan, Zheng 			goto e_inval;
3228205ee118SIlya Dryomov 		}
3229205ee118SIlya Dryomov 	}
3230205ee118SIlya Dryomov 
3231205ee118SIlya Dryomov 	if (struct_v >= 6) {
3232205ee118SIlya Dryomov 		s64 hash = ceph_decode_64(p);
3233205ee118SIlya Dryomov 		if (hash != -1) {
3234205ee118SIlya Dryomov 			pr_warn("ceph_object_locator::hash is set\n");
3235205ee118SIlya Dryomov 			goto e_inval;
3236205ee118SIlya Dryomov 		}
3237205ee118SIlya Dryomov 	}
3238205ee118SIlya Dryomov 
3239205ee118SIlya Dryomov 	/* skip the rest */
3240205ee118SIlya Dryomov 	*p = struct_end;
3241205ee118SIlya Dryomov out:
3242205ee118SIlya Dryomov 	return ret;
3243205ee118SIlya Dryomov 
3244205ee118SIlya Dryomov e_inval:
3245205ee118SIlya Dryomov 	ret = -EINVAL;
3246205ee118SIlya Dryomov 	goto out;
3247205ee118SIlya Dryomov }
3248205ee118SIlya Dryomov 
3249205ee118SIlya Dryomov static int ceph_redirect_decode(void **p, void *end,
3250205ee118SIlya Dryomov 				struct ceph_request_redirect *redir)
3251205ee118SIlya Dryomov {
3252205ee118SIlya Dryomov 	u8 struct_v, struct_cv;
3253205ee118SIlya Dryomov 	u32 len;
3254205ee118SIlya Dryomov 	void *struct_end;
3255205ee118SIlya Dryomov 	int ret;
3256205ee118SIlya Dryomov 
3257205ee118SIlya Dryomov 	ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3258205ee118SIlya Dryomov 	struct_v = ceph_decode_8(p);
3259205ee118SIlya Dryomov 	struct_cv = ceph_decode_8(p);
3260205ee118SIlya Dryomov 	if (struct_cv > 1) {
3261205ee118SIlya Dryomov 		pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
3262205ee118SIlya Dryomov 			struct_v, struct_cv);
3263205ee118SIlya Dryomov 		goto e_inval;
3264205ee118SIlya Dryomov 	}
3265205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3266205ee118SIlya Dryomov 	ceph_decode_need(p, end, len, e_inval);
3267205ee118SIlya Dryomov 	struct_end = *p + len;
3268205ee118SIlya Dryomov 
3269205ee118SIlya Dryomov 	ret = ceph_oloc_decode(p, end, &redir->oloc);
3270205ee118SIlya Dryomov 	if (ret)
3271205ee118SIlya Dryomov 		goto out;
3272205ee118SIlya Dryomov 
3273205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3274205ee118SIlya Dryomov 	if (len > 0) {
3275205ee118SIlya Dryomov 		pr_warn("ceph_request_redirect::object_name is set\n");
3276205ee118SIlya Dryomov 		goto e_inval;
3277205ee118SIlya Dryomov 	}
3278205ee118SIlya Dryomov 
3279205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3280205ee118SIlya Dryomov 	*p += len; /* skip osd_instructions */
3281205ee118SIlya Dryomov 
3282205ee118SIlya Dryomov 	/* skip the rest */
3283205ee118SIlya Dryomov 	*p = struct_end;
3284205ee118SIlya Dryomov out:
3285205ee118SIlya Dryomov 	return ret;
3286205ee118SIlya Dryomov 
3287205ee118SIlya Dryomov e_inval:
3288205ee118SIlya Dryomov 	ret = -EINVAL;
3289205ee118SIlya Dryomov 	goto out;
3290205ee118SIlya Dryomov }
3291205ee118SIlya Dryomov 
3292fe5da05eSIlya Dryomov struct MOSDOpReply {
3293fe5da05eSIlya Dryomov 	struct ceph_pg pgid;
3294fe5da05eSIlya Dryomov 	u64 flags;
3295fe5da05eSIlya Dryomov 	int result;
3296fe5da05eSIlya Dryomov 	u32 epoch;
3297fe5da05eSIlya Dryomov 	int num_ops;
3298fe5da05eSIlya Dryomov 	u32 outdata_len[CEPH_OSD_MAX_OPS];
3299fe5da05eSIlya Dryomov 	s32 rval[CEPH_OSD_MAX_OPS];
3300fe5da05eSIlya Dryomov 	int retry_attempt;
3301fe5da05eSIlya Dryomov 	struct ceph_eversion replay_version;
3302fe5da05eSIlya Dryomov 	u64 user_version;
3303fe5da05eSIlya Dryomov 	struct ceph_request_redirect redirect;
3304fe5da05eSIlya Dryomov };
330525845472SSage Weil 
3306fe5da05eSIlya Dryomov static int decode_MOSDOpReply(const struct ceph_msg *msg, struct MOSDOpReply *m)
33073d14c5d2SYehuda Sadeh {
3308fe5da05eSIlya Dryomov 	void *p = msg->front.iov_base;
3309fe5da05eSIlya Dryomov 	void *const end = p + msg->front.iov_len;
3310fe5da05eSIlya Dryomov 	u16 version = le16_to_cpu(msg->hdr.version);
3311fe5da05eSIlya Dryomov 	struct ceph_eversion bad_replay_version;
3312b0b31a8fSIlya Dryomov 	u8 decode_redir;
3313fe5da05eSIlya Dryomov 	u32 len;
3314fe5da05eSIlya Dryomov 	int ret;
3315fe5da05eSIlya Dryomov 	int i;
33163d14c5d2SYehuda Sadeh 
3317fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, len, e_inval);
3318fe5da05eSIlya Dryomov 	ceph_decode_need(&p, end, len, e_inval);
3319fe5da05eSIlya Dryomov 	p += len; /* skip oid */
33201b83bef2SSage Weil 
3321fe5da05eSIlya Dryomov 	ret = ceph_decode_pgid(&p, end, &m->pgid);
3322fe5da05eSIlya Dryomov 	if (ret)
3323fe5da05eSIlya Dryomov 		return ret;
33241b83bef2SSage Weil 
3325fe5da05eSIlya Dryomov 	ceph_decode_64_safe(&p, end, m->flags, e_inval);
3326fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->result, e_inval);
3327fe5da05eSIlya Dryomov 	ceph_decode_need(&p, end, sizeof(bad_replay_version), e_inval);
3328fe5da05eSIlya Dryomov 	memcpy(&bad_replay_version, p, sizeof(bad_replay_version));
3329fe5da05eSIlya Dryomov 	p += sizeof(bad_replay_version);
3330fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->epoch, e_inval);
33311b83bef2SSage Weil 
3332fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->num_ops, e_inval);
3333fe5da05eSIlya Dryomov 	if (m->num_ops > ARRAY_SIZE(m->outdata_len))
3334fe5da05eSIlya Dryomov 		goto e_inval;
33351b83bef2SSage Weil 
3336fe5da05eSIlya Dryomov 	ceph_decode_need(&p, end, m->num_ops * sizeof(struct ceph_osd_op),
3337fe5da05eSIlya Dryomov 			 e_inval);
3338fe5da05eSIlya Dryomov 	for (i = 0; i < m->num_ops; i++) {
33391b83bef2SSage Weil 		struct ceph_osd_op *op = p;
33401b83bef2SSage Weil 
3341fe5da05eSIlya Dryomov 		m->outdata_len[i] = le32_to_cpu(op->payload_len);
33421b83bef2SSage Weil 		p += sizeof(*op);
33431b83bef2SSage Weil 	}
3344fe5da05eSIlya Dryomov 
3345fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->retry_attempt, e_inval);
3346fe5da05eSIlya Dryomov 	for (i = 0; i < m->num_ops; i++)
3347fe5da05eSIlya Dryomov 		ceph_decode_32_safe(&p, end, m->rval[i], e_inval);
3348fe5da05eSIlya Dryomov 
3349fe5da05eSIlya Dryomov 	if (version >= 5) {
3350fe5da05eSIlya Dryomov 		ceph_decode_need(&p, end, sizeof(m->replay_version), e_inval);
3351fe5da05eSIlya Dryomov 		memcpy(&m->replay_version, p, sizeof(m->replay_version));
3352fe5da05eSIlya Dryomov 		p += sizeof(m->replay_version);
3353fe5da05eSIlya Dryomov 		ceph_decode_64_safe(&p, end, m->user_version, e_inval);
3354fe5da05eSIlya Dryomov 	} else {
3355fe5da05eSIlya Dryomov 		m->replay_version = bad_replay_version; /* struct */
3356fe5da05eSIlya Dryomov 		m->user_version = le64_to_cpu(m->replay_version.version);
33571b83bef2SSage Weil 	}
33581b83bef2SSage Weil 
3359fe5da05eSIlya Dryomov 	if (version >= 6) {
3360fe5da05eSIlya Dryomov 		if (version >= 7)
3361fe5da05eSIlya Dryomov 			ceph_decode_8_safe(&p, end, decode_redir, e_inval);
3362b0b31a8fSIlya Dryomov 		else
3363b0b31a8fSIlya Dryomov 			decode_redir = 1;
3364b0b31a8fSIlya Dryomov 	} else {
3365b0b31a8fSIlya Dryomov 		decode_redir = 0;
3366b0b31a8fSIlya Dryomov 	}
3367b0b31a8fSIlya Dryomov 
3368b0b31a8fSIlya Dryomov 	if (decode_redir) {
3369fe5da05eSIlya Dryomov 		ret = ceph_redirect_decode(&p, end, &m->redirect);
3370fe5da05eSIlya Dryomov 		if (ret)
3371fe5da05eSIlya Dryomov 			return ret;
3372205ee118SIlya Dryomov 	} else {
3373fe5da05eSIlya Dryomov 		ceph_oloc_init(&m->redirect.oloc);
3374205ee118SIlya Dryomov 	}
3375205ee118SIlya Dryomov 
3376fe5da05eSIlya Dryomov 	return 0;
3377205ee118SIlya Dryomov 
3378fe5da05eSIlya Dryomov e_inval:
3379fe5da05eSIlya Dryomov 	return -EINVAL;
3380fe5da05eSIlya Dryomov }
3381fe5da05eSIlya Dryomov 
3382fe5da05eSIlya Dryomov /*
3383b18b9550SIlya Dryomov  * Handle MOSDOpReply.  Set ->r_result and call the callback if it is
3384b18b9550SIlya Dryomov  * specified.
3385fe5da05eSIlya Dryomov  */
33865aea3dcdSIlya Dryomov static void handle_reply(struct ceph_osd *osd, struct ceph_msg *msg)
3387fe5da05eSIlya Dryomov {
33885aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
3389fe5da05eSIlya Dryomov 	struct ceph_osd_request *req;
3390fe5da05eSIlya Dryomov 	struct MOSDOpReply m;
3391fe5da05eSIlya Dryomov 	u64 tid = le64_to_cpu(msg->hdr.tid);
3392fe5da05eSIlya Dryomov 	u32 data_len = 0;
3393fe5da05eSIlya Dryomov 	int ret;
3394fe5da05eSIlya Dryomov 	int i;
3395fe5da05eSIlya Dryomov 
3396fe5da05eSIlya Dryomov 	dout("%s msg %p tid %llu\n", __func__, msg, tid);
3397fe5da05eSIlya Dryomov 
33985aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
33995aea3dcdSIlya Dryomov 	if (!osd_registered(osd)) {
34005aea3dcdSIlya Dryomov 		dout("%s osd%d unknown\n", __func__, osd->o_osd);
34015aea3dcdSIlya Dryomov 		goto out_unlock_osdc;
3402fe5da05eSIlya Dryomov 	}
34035aea3dcdSIlya Dryomov 	WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
34045aea3dcdSIlya Dryomov 
34055aea3dcdSIlya Dryomov 	mutex_lock(&osd->lock);
34065aea3dcdSIlya Dryomov 	req = lookup_request(&osd->o_requests, tid);
34075aea3dcdSIlya Dryomov 	if (!req) {
34085aea3dcdSIlya Dryomov 		dout("%s osd%d tid %llu unknown\n", __func__, osd->o_osd, tid);
34095aea3dcdSIlya Dryomov 		goto out_unlock_session;
34105aea3dcdSIlya Dryomov 	}
3411fe5da05eSIlya Dryomov 
3412cd08e0a2SYan, Zheng 	m.redirect.oloc.pool_ns = req->r_t.target_oloc.pool_ns;
3413fe5da05eSIlya Dryomov 	ret = decode_MOSDOpReply(msg, &m);
3414cd08e0a2SYan, Zheng 	m.redirect.oloc.pool_ns = NULL;
3415fe5da05eSIlya Dryomov 	if (ret) {
3416fe5da05eSIlya Dryomov 		pr_err("failed to decode MOSDOpReply for tid %llu: %d\n",
3417fe5da05eSIlya Dryomov 		       req->r_tid, ret);
3418fe5da05eSIlya Dryomov 		ceph_msg_dump(msg);
3419fe5da05eSIlya Dryomov 		goto fail_request;
3420fe5da05eSIlya Dryomov 	}
3421fe5da05eSIlya Dryomov 	dout("%s req %p tid %llu flags 0x%llx pgid %llu.%x epoch %u attempt %d v %u'%llu uv %llu\n",
3422fe5da05eSIlya Dryomov 	     __func__, req, req->r_tid, m.flags, m.pgid.pool, m.pgid.seed,
3423fe5da05eSIlya Dryomov 	     m.epoch, m.retry_attempt, le32_to_cpu(m.replay_version.epoch),
3424fe5da05eSIlya Dryomov 	     le64_to_cpu(m.replay_version.version), m.user_version);
3425fe5da05eSIlya Dryomov 
3426fe5da05eSIlya Dryomov 	if (m.retry_attempt >= 0) {
3427fe5da05eSIlya Dryomov 		if (m.retry_attempt != req->r_attempts - 1) {
3428fe5da05eSIlya Dryomov 			dout("req %p tid %llu retry_attempt %d != %d, ignoring\n",
3429fe5da05eSIlya Dryomov 			     req, req->r_tid, m.retry_attempt,
3430fe5da05eSIlya Dryomov 			     req->r_attempts - 1);
34315aea3dcdSIlya Dryomov 			goto out_unlock_session;
3432fe5da05eSIlya Dryomov 		}
3433fe5da05eSIlya Dryomov 	} else {
3434fe5da05eSIlya Dryomov 		WARN_ON(1); /* MOSDOpReply v4 is assumed */
3435fe5da05eSIlya Dryomov 	}
3436fe5da05eSIlya Dryomov 
3437fe5da05eSIlya Dryomov 	if (!ceph_oloc_empty(&m.redirect.oloc)) {
3438fe5da05eSIlya Dryomov 		dout("req %p tid %llu redirect pool %lld\n", req, req->r_tid,
3439fe5da05eSIlya Dryomov 		     m.redirect.oloc.pool);
34405aea3dcdSIlya Dryomov 		unlink_request(osd, req);
34415aea3dcdSIlya Dryomov 		mutex_unlock(&osd->lock);
3442205ee118SIlya Dryomov 
344330c156d9SYan, Zheng 		/*
344430c156d9SYan, Zheng 		 * Not ceph_oloc_copy() - changing pool_ns is not
344530c156d9SYan, Zheng 		 * supported.
344630c156d9SYan, Zheng 		 */
344730c156d9SYan, Zheng 		req->r_t.target_oloc.pool = m.redirect.oloc.pool;
34485aea3dcdSIlya Dryomov 		req->r_flags |= CEPH_OSD_FLAG_REDIRECTED;
34495aea3dcdSIlya Dryomov 		req->r_tid = 0;
34505aea3dcdSIlya Dryomov 		__submit_request(req, false);
34515aea3dcdSIlya Dryomov 		goto out_unlock_osdc;
3452205ee118SIlya Dryomov 	}
3453205ee118SIlya Dryomov 
3454fe5da05eSIlya Dryomov 	if (m.num_ops != req->r_num_ops) {
3455fe5da05eSIlya Dryomov 		pr_err("num_ops %d != %d for tid %llu\n", m.num_ops,
3456fe5da05eSIlya Dryomov 		       req->r_num_ops, req->r_tid);
3457fe5da05eSIlya Dryomov 		goto fail_request;
3458fe5da05eSIlya Dryomov 	}
3459fe5da05eSIlya Dryomov 	for (i = 0; i < req->r_num_ops; i++) {
3460fe5da05eSIlya Dryomov 		dout(" req %p tid %llu op %d rval %d len %u\n", req,
3461fe5da05eSIlya Dryomov 		     req->r_tid, i, m.rval[i], m.outdata_len[i]);
3462fe5da05eSIlya Dryomov 		req->r_ops[i].rval = m.rval[i];
3463fe5da05eSIlya Dryomov 		req->r_ops[i].outdata_len = m.outdata_len[i];
3464fe5da05eSIlya Dryomov 		data_len += m.outdata_len[i];
3465fe5da05eSIlya Dryomov 	}
3466fe5da05eSIlya Dryomov 	if (data_len != le32_to_cpu(msg->hdr.data_len)) {
3467fe5da05eSIlya Dryomov 		pr_err("sum of lens %u != %u for tid %llu\n", data_len,
3468fe5da05eSIlya Dryomov 		       le32_to_cpu(msg->hdr.data_len), req->r_tid);
3469fe5da05eSIlya Dryomov 		goto fail_request;
3470fe5da05eSIlya Dryomov 	}
3471b18b9550SIlya Dryomov 	dout("%s req %p tid %llu result %d data_len %u\n", __func__,
3472b18b9550SIlya Dryomov 	     req, req->r_tid, m.result, data_len);
34733d14c5d2SYehuda Sadeh 
3474b18b9550SIlya Dryomov 	/*
3475b18b9550SIlya Dryomov 	 * Since we only ever request ONDISK, we should only ever get
3476b18b9550SIlya Dryomov 	 * one (type of) reply back.
3477b18b9550SIlya Dryomov 	 */
3478b18b9550SIlya Dryomov 	WARN_ON(!(m.flags & CEPH_OSD_FLAG_ONDISK));
3479fe5da05eSIlya Dryomov 	req->r_result = m.result ?: data_len;
348045ee2c1dSIlya Dryomov 	finish_request(req);
34815aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
34825aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
34833d14c5d2SYehuda Sadeh 
3484fe5da05eSIlya Dryomov 	__complete_request(req);
3485b18b9550SIlya Dryomov 	complete_all(&req->r_completion);
3486dc045a91SIlya Dryomov 	ceph_osdc_put_request(req);
34873d14c5d2SYehuda Sadeh 	return;
3488fe5da05eSIlya Dryomov 
3489fe5da05eSIlya Dryomov fail_request:
34904609245eSIlya Dryomov 	complete_request(req, -EIO);
34915aea3dcdSIlya Dryomov out_unlock_session:
34925aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
34935aea3dcdSIlya Dryomov out_unlock_osdc:
34945aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
34953d14c5d2SYehuda Sadeh }
34963d14c5d2SYehuda Sadeh 
349742c1b124SIlya Dryomov static void set_pool_was_full(struct ceph_osd_client *osdc)
349842c1b124SIlya Dryomov {
349942c1b124SIlya Dryomov 	struct rb_node *n;
350042c1b124SIlya Dryomov 
350142c1b124SIlya Dryomov 	for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
350242c1b124SIlya Dryomov 		struct ceph_pg_pool_info *pi =
350342c1b124SIlya Dryomov 		    rb_entry(n, struct ceph_pg_pool_info, node);
350442c1b124SIlya Dryomov 
350542c1b124SIlya Dryomov 		pi->was_full = __pool_full(pi);
350642c1b124SIlya Dryomov 	}
350742c1b124SIlya Dryomov }
350842c1b124SIlya Dryomov 
35095aea3dcdSIlya Dryomov static bool pool_cleared_full(struct ceph_osd_client *osdc, s64 pool_id)
35103d14c5d2SYehuda Sadeh {
35115aea3dcdSIlya Dryomov 	struct ceph_pg_pool_info *pi;
35123d14c5d2SYehuda Sadeh 
35135aea3dcdSIlya Dryomov 	pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
35145aea3dcdSIlya Dryomov 	if (!pi)
35155aea3dcdSIlya Dryomov 		return false;
35163d14c5d2SYehuda Sadeh 
35175aea3dcdSIlya Dryomov 	return pi->was_full && !__pool_full(pi);
35183d14c5d2SYehuda Sadeh }
35193d14c5d2SYehuda Sadeh 
3520922dab61SIlya Dryomov static enum calc_target_result
3521922dab61SIlya Dryomov recalc_linger_target(struct ceph_osd_linger_request *lreq)
3522922dab61SIlya Dryomov {
3523922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
3524922dab61SIlya Dryomov 	enum calc_target_result ct_res;
3525922dab61SIlya Dryomov 
35267de030d6SIlya Dryomov 	ct_res = calc_target(osdc, &lreq->t, NULL, true);
3527922dab61SIlya Dryomov 	if (ct_res == CALC_TARGET_NEED_RESEND) {
3528922dab61SIlya Dryomov 		struct ceph_osd *osd;
3529922dab61SIlya Dryomov 
3530922dab61SIlya Dryomov 		osd = lookup_create_osd(osdc, lreq->t.osd, true);
3531922dab61SIlya Dryomov 		if (osd != lreq->osd) {
3532922dab61SIlya Dryomov 			unlink_linger(lreq->osd, lreq);
3533922dab61SIlya Dryomov 			link_linger(osd, lreq);
3534922dab61SIlya Dryomov 		}
3535922dab61SIlya Dryomov 	}
3536922dab61SIlya Dryomov 
3537922dab61SIlya Dryomov 	return ct_res;
3538922dab61SIlya Dryomov }
3539922dab61SIlya Dryomov 
35403d14c5d2SYehuda Sadeh /*
35415aea3dcdSIlya Dryomov  * Requeue requests whose mapping to an OSD has changed.
35423d14c5d2SYehuda Sadeh  */
35435aea3dcdSIlya Dryomov static void scan_requests(struct ceph_osd *osd,
35445aea3dcdSIlya Dryomov 			  bool force_resend,
35455aea3dcdSIlya Dryomov 			  bool cleared_full,
35465aea3dcdSIlya Dryomov 			  bool check_pool_cleared_full,
35475aea3dcdSIlya Dryomov 			  struct rb_root *need_resend,
35485aea3dcdSIlya Dryomov 			  struct list_head *need_resend_linger)
35493d14c5d2SYehuda Sadeh {
35505aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
35515aea3dcdSIlya Dryomov 	struct rb_node *n;
35525aea3dcdSIlya Dryomov 	bool force_resend_writes;
35533d14c5d2SYehuda Sadeh 
3554922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_linger_requests); n; ) {
3555922dab61SIlya Dryomov 		struct ceph_osd_linger_request *lreq =
3556922dab61SIlya Dryomov 		    rb_entry(n, struct ceph_osd_linger_request, node);
3557922dab61SIlya Dryomov 		enum calc_target_result ct_res;
3558922dab61SIlya Dryomov 
3559922dab61SIlya Dryomov 		n = rb_next(n); /* recalc_linger_target() */
3560922dab61SIlya Dryomov 
3561922dab61SIlya Dryomov 		dout("%s lreq %p linger_id %llu\n", __func__, lreq,
3562922dab61SIlya Dryomov 		     lreq->linger_id);
3563922dab61SIlya Dryomov 		ct_res = recalc_linger_target(lreq);
3564922dab61SIlya Dryomov 		switch (ct_res) {
3565922dab61SIlya Dryomov 		case CALC_TARGET_NO_ACTION:
3566922dab61SIlya Dryomov 			force_resend_writes = cleared_full ||
3567922dab61SIlya Dryomov 			    (check_pool_cleared_full &&
3568922dab61SIlya Dryomov 			     pool_cleared_full(osdc, lreq->t.base_oloc.pool));
3569922dab61SIlya Dryomov 			if (!force_resend && !force_resend_writes)
3570922dab61SIlya Dryomov 				break;
3571922dab61SIlya Dryomov 
3572922dab61SIlya Dryomov 			/* fall through */
3573922dab61SIlya Dryomov 		case CALC_TARGET_NEED_RESEND:
35744609245eSIlya Dryomov 			cancel_linger_map_check(lreq);
3575922dab61SIlya Dryomov 			/*
3576922dab61SIlya Dryomov 			 * scan_requests() for the previous epoch(s)
3577922dab61SIlya Dryomov 			 * may have already added it to the list, since
3578922dab61SIlya Dryomov 			 * it's not unlinked here.
3579922dab61SIlya Dryomov 			 */
3580922dab61SIlya Dryomov 			if (list_empty(&lreq->scan_item))
3581922dab61SIlya Dryomov 				list_add_tail(&lreq->scan_item, need_resend_linger);
3582922dab61SIlya Dryomov 			break;
3583922dab61SIlya Dryomov 		case CALC_TARGET_POOL_DNE:
3584a10bcb19SIlya Dryomov 			list_del_init(&lreq->scan_item);
35854609245eSIlya Dryomov 			check_linger_pool_dne(lreq);
3586922dab61SIlya Dryomov 			break;
3587922dab61SIlya Dryomov 		}
3588922dab61SIlya Dryomov 	}
3589922dab61SIlya Dryomov 
35905aea3dcdSIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; ) {
35915aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
35925aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
35935aea3dcdSIlya Dryomov 		enum calc_target_result ct_res;
3594ab60b16dSAlex Elder 
35954609245eSIlya Dryomov 		n = rb_next(n); /* unlink_request(), check_pool_dne() */
3596ab60b16dSAlex Elder 
35975aea3dcdSIlya Dryomov 		dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
35987de030d6SIlya Dryomov 		ct_res = calc_target(osdc, &req->r_t, &req->r_osd->o_con,
35997de030d6SIlya Dryomov 				     false);
36005aea3dcdSIlya Dryomov 		switch (ct_res) {
36015aea3dcdSIlya Dryomov 		case CALC_TARGET_NO_ACTION:
36025aea3dcdSIlya Dryomov 			force_resend_writes = cleared_full ||
36035aea3dcdSIlya Dryomov 			    (check_pool_cleared_full &&
36045aea3dcdSIlya Dryomov 			     pool_cleared_full(osdc, req->r_t.base_oloc.pool));
36055aea3dcdSIlya Dryomov 			if (!force_resend &&
36065aea3dcdSIlya Dryomov 			    (!(req->r_flags & CEPH_OSD_FLAG_WRITE) ||
36075aea3dcdSIlya Dryomov 			     !force_resend_writes))
36085aea3dcdSIlya Dryomov 				break;
3609a40c4f10SYehuda Sadeh 
36105aea3dcdSIlya Dryomov 			/* fall through */
36115aea3dcdSIlya Dryomov 		case CALC_TARGET_NEED_RESEND:
36124609245eSIlya Dryomov 			cancel_map_check(req);
36135aea3dcdSIlya Dryomov 			unlink_request(osd, req);
36145aea3dcdSIlya Dryomov 			insert_request(need_resend, req);
36155aea3dcdSIlya Dryomov 			break;
36165aea3dcdSIlya Dryomov 		case CALC_TARGET_POOL_DNE:
36174609245eSIlya Dryomov 			check_pool_dne(req);
36185aea3dcdSIlya Dryomov 			break;
3619a40c4f10SYehuda Sadeh 		}
36203d14c5d2SYehuda Sadeh 	}
36213d14c5d2SYehuda Sadeh }
36226f6c7006SSage Weil 
362342c1b124SIlya Dryomov static int handle_one_map(struct ceph_osd_client *osdc,
36245aea3dcdSIlya Dryomov 			  void *p, void *end, bool incremental,
36255aea3dcdSIlya Dryomov 			  struct rb_root *need_resend,
36265aea3dcdSIlya Dryomov 			  struct list_head *need_resend_linger)
362742c1b124SIlya Dryomov {
362842c1b124SIlya Dryomov 	struct ceph_osdmap *newmap;
362942c1b124SIlya Dryomov 	struct rb_node *n;
363042c1b124SIlya Dryomov 	bool skipped_map = false;
363142c1b124SIlya Dryomov 	bool was_full;
363242c1b124SIlya Dryomov 
3633b7ec35b3SIlya Dryomov 	was_full = ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
363442c1b124SIlya Dryomov 	set_pool_was_full(osdc);
363542c1b124SIlya Dryomov 
363642c1b124SIlya Dryomov 	if (incremental)
363742c1b124SIlya Dryomov 		newmap = osdmap_apply_incremental(&p, end, osdc->osdmap);
363842c1b124SIlya Dryomov 	else
363942c1b124SIlya Dryomov 		newmap = ceph_osdmap_decode(&p, end);
364042c1b124SIlya Dryomov 	if (IS_ERR(newmap))
364142c1b124SIlya Dryomov 		return PTR_ERR(newmap);
364242c1b124SIlya Dryomov 
364342c1b124SIlya Dryomov 	if (newmap != osdc->osdmap) {
364442c1b124SIlya Dryomov 		/*
364542c1b124SIlya Dryomov 		 * Preserve ->was_full before destroying the old map.
364642c1b124SIlya Dryomov 		 * For pools that weren't in the old map, ->was_full
364742c1b124SIlya Dryomov 		 * should be false.
364842c1b124SIlya Dryomov 		 */
364942c1b124SIlya Dryomov 		for (n = rb_first(&newmap->pg_pools); n; n = rb_next(n)) {
365042c1b124SIlya Dryomov 			struct ceph_pg_pool_info *pi =
365142c1b124SIlya Dryomov 			    rb_entry(n, struct ceph_pg_pool_info, node);
365242c1b124SIlya Dryomov 			struct ceph_pg_pool_info *old_pi;
365342c1b124SIlya Dryomov 
365442c1b124SIlya Dryomov 			old_pi = ceph_pg_pool_by_id(osdc->osdmap, pi->id);
365542c1b124SIlya Dryomov 			if (old_pi)
365642c1b124SIlya Dryomov 				pi->was_full = old_pi->was_full;
365742c1b124SIlya Dryomov 			else
365842c1b124SIlya Dryomov 				WARN_ON(pi->was_full);
365942c1b124SIlya Dryomov 		}
366042c1b124SIlya Dryomov 
366142c1b124SIlya Dryomov 		if (osdc->osdmap->epoch &&
366242c1b124SIlya Dryomov 		    osdc->osdmap->epoch + 1 < newmap->epoch) {
366342c1b124SIlya Dryomov 			WARN_ON(incremental);
366442c1b124SIlya Dryomov 			skipped_map = true;
366542c1b124SIlya Dryomov 		}
366642c1b124SIlya Dryomov 
366742c1b124SIlya Dryomov 		ceph_osdmap_destroy(osdc->osdmap);
366842c1b124SIlya Dryomov 		osdc->osdmap = newmap;
366942c1b124SIlya Dryomov 	}
367042c1b124SIlya Dryomov 
3671b7ec35b3SIlya Dryomov 	was_full &= !ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
36725aea3dcdSIlya Dryomov 	scan_requests(&osdc->homeless_osd, skipped_map, was_full, true,
36735aea3dcdSIlya Dryomov 		      need_resend, need_resend_linger);
36745aea3dcdSIlya Dryomov 
36755aea3dcdSIlya Dryomov 	for (n = rb_first(&osdc->osds); n; ) {
36765aea3dcdSIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
36775aea3dcdSIlya Dryomov 
36785aea3dcdSIlya Dryomov 		n = rb_next(n); /* close_osd() */
36795aea3dcdSIlya Dryomov 
36805aea3dcdSIlya Dryomov 		scan_requests(osd, skipped_map, was_full, true, need_resend,
36815aea3dcdSIlya Dryomov 			      need_resend_linger);
36825aea3dcdSIlya Dryomov 		if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
36835aea3dcdSIlya Dryomov 		    memcmp(&osd->o_con.peer_addr,
36845aea3dcdSIlya Dryomov 			   ceph_osd_addr(osdc->osdmap, osd->o_osd),
36855aea3dcdSIlya Dryomov 			   sizeof(struct ceph_entity_addr)))
36865aea3dcdSIlya Dryomov 			close_osd(osd);
36875aea3dcdSIlya Dryomov 	}
368842c1b124SIlya Dryomov 
368942c1b124SIlya Dryomov 	return 0;
369042c1b124SIlya Dryomov }
36916f6c7006SSage Weil 
36925aea3dcdSIlya Dryomov static void kick_requests(struct ceph_osd_client *osdc,
36935aea3dcdSIlya Dryomov 			  struct rb_root *need_resend,
36945aea3dcdSIlya Dryomov 			  struct list_head *need_resend_linger)
36955aea3dcdSIlya Dryomov {
3696922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq, *nlreq;
369704c7d789SIlya Dryomov 	enum calc_target_result ct_res;
36985aea3dcdSIlya Dryomov 	struct rb_node *n;
36995aea3dcdSIlya Dryomov 
370004c7d789SIlya Dryomov 	/* make sure need_resend targets reflect latest map */
370104c7d789SIlya Dryomov 	for (n = rb_first(need_resend); n; ) {
370204c7d789SIlya Dryomov 		struct ceph_osd_request *req =
370304c7d789SIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
370404c7d789SIlya Dryomov 
370504c7d789SIlya Dryomov 		n = rb_next(n);
370604c7d789SIlya Dryomov 
370704c7d789SIlya Dryomov 		if (req->r_t.epoch < osdc->osdmap->epoch) {
370804c7d789SIlya Dryomov 			ct_res = calc_target(osdc, &req->r_t, NULL, false);
370904c7d789SIlya Dryomov 			if (ct_res == CALC_TARGET_POOL_DNE) {
371004c7d789SIlya Dryomov 				erase_request(need_resend, req);
371104c7d789SIlya Dryomov 				check_pool_dne(req);
371204c7d789SIlya Dryomov 			}
371304c7d789SIlya Dryomov 		}
371404c7d789SIlya Dryomov 	}
371504c7d789SIlya Dryomov 
37165aea3dcdSIlya Dryomov 	for (n = rb_first(need_resend); n; ) {
37175aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
37185aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
37195aea3dcdSIlya Dryomov 		struct ceph_osd *osd;
37205aea3dcdSIlya Dryomov 
37215aea3dcdSIlya Dryomov 		n = rb_next(n);
37225aea3dcdSIlya Dryomov 		erase_request(need_resend, req); /* before link_request() */
37235aea3dcdSIlya Dryomov 
37245aea3dcdSIlya Dryomov 		osd = lookup_create_osd(osdc, req->r_t.osd, true);
37255aea3dcdSIlya Dryomov 		link_request(osd, req);
37265aea3dcdSIlya Dryomov 		if (!req->r_linger) {
37275aea3dcdSIlya Dryomov 			if (!osd_homeless(osd) && !req->r_t.paused)
37285aea3dcdSIlya Dryomov 				send_request(req);
3729922dab61SIlya Dryomov 		} else {
3730922dab61SIlya Dryomov 			cancel_linger_request(req);
37315aea3dcdSIlya Dryomov 		}
37325aea3dcdSIlya Dryomov 	}
3733922dab61SIlya Dryomov 
3734922dab61SIlya Dryomov 	list_for_each_entry_safe(lreq, nlreq, need_resend_linger, scan_item) {
3735922dab61SIlya Dryomov 		if (!osd_homeless(lreq->osd))
3736922dab61SIlya Dryomov 			send_linger(lreq);
3737922dab61SIlya Dryomov 
3738922dab61SIlya Dryomov 		list_del_init(&lreq->scan_item);
3739922dab61SIlya Dryomov 	}
37405aea3dcdSIlya Dryomov }
37415aea3dcdSIlya Dryomov 
37423d14c5d2SYehuda Sadeh /*
37433d14c5d2SYehuda Sadeh  * Process updated osd map.
37443d14c5d2SYehuda Sadeh  *
37453d14c5d2SYehuda Sadeh  * The message contains any number of incremental and full maps, normally
37463d14c5d2SYehuda Sadeh  * indicating some sort of topology change in the cluster.  Kick requests
37473d14c5d2SYehuda Sadeh  * off to different OSDs as needed.
37483d14c5d2SYehuda Sadeh  */
37493d14c5d2SYehuda Sadeh void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
37503d14c5d2SYehuda Sadeh {
375142c1b124SIlya Dryomov 	void *p = msg->front.iov_base;
375242c1b124SIlya Dryomov 	void *const end = p + msg->front.iov_len;
37533d14c5d2SYehuda Sadeh 	u32 nr_maps, maplen;
37543d14c5d2SYehuda Sadeh 	u32 epoch;
37553d14c5d2SYehuda Sadeh 	struct ceph_fsid fsid;
37565aea3dcdSIlya Dryomov 	struct rb_root need_resend = RB_ROOT;
37575aea3dcdSIlya Dryomov 	LIST_HEAD(need_resend_linger);
375842c1b124SIlya Dryomov 	bool handled_incremental = false;
375942c1b124SIlya Dryomov 	bool was_pauserd, was_pausewr;
376042c1b124SIlya Dryomov 	bool pauserd, pausewr;
376142c1b124SIlya Dryomov 	int err;
37623d14c5d2SYehuda Sadeh 
376342c1b124SIlya Dryomov 	dout("%s have %u\n", __func__, osdc->osdmap->epoch);
37645aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
37653d14c5d2SYehuda Sadeh 
37663d14c5d2SYehuda Sadeh 	/* verify fsid */
37673d14c5d2SYehuda Sadeh 	ceph_decode_need(&p, end, sizeof(fsid), bad);
37683d14c5d2SYehuda Sadeh 	ceph_decode_copy(&p, &fsid, sizeof(fsid));
37693d14c5d2SYehuda Sadeh 	if (ceph_check_fsid(osdc->client, &fsid) < 0)
377042c1b124SIlya Dryomov 		goto bad;
37713d14c5d2SYehuda Sadeh 
3772b7ec35b3SIlya Dryomov 	was_pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3773b7ec35b3SIlya Dryomov 	was_pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3774b7ec35b3SIlya Dryomov 		      ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
377542c1b124SIlya Dryomov 		      have_pool_full(osdc);
37769a1ea2dbSJosh Durgin 
37773d14c5d2SYehuda Sadeh 	/* incremental maps */
37783d14c5d2SYehuda Sadeh 	ceph_decode_32_safe(&p, end, nr_maps, bad);
37793d14c5d2SYehuda Sadeh 	dout(" %d inc maps\n", nr_maps);
37803d14c5d2SYehuda Sadeh 	while (nr_maps > 0) {
37813d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, 2*sizeof(u32), bad);
37823d14c5d2SYehuda Sadeh 		epoch = ceph_decode_32(&p);
37833d14c5d2SYehuda Sadeh 		maplen = ceph_decode_32(&p);
37843d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, maplen, bad);
378542c1b124SIlya Dryomov 		if (osdc->osdmap->epoch &&
378642c1b124SIlya Dryomov 		    osdc->osdmap->epoch + 1 == epoch) {
37873d14c5d2SYehuda Sadeh 			dout("applying incremental map %u len %d\n",
37883d14c5d2SYehuda Sadeh 			     epoch, maplen);
37895aea3dcdSIlya Dryomov 			err = handle_one_map(osdc, p, p + maplen, true,
37905aea3dcdSIlya Dryomov 					     &need_resend, &need_resend_linger);
379142c1b124SIlya Dryomov 			if (err)
37923d14c5d2SYehuda Sadeh 				goto bad;
379342c1b124SIlya Dryomov 			handled_incremental = true;
37943d14c5d2SYehuda Sadeh 		} else {
37953d14c5d2SYehuda Sadeh 			dout("ignoring incremental map %u len %d\n",
37963d14c5d2SYehuda Sadeh 			     epoch, maplen);
37973d14c5d2SYehuda Sadeh 		}
379842c1b124SIlya Dryomov 		p += maplen;
37993d14c5d2SYehuda Sadeh 		nr_maps--;
38003d14c5d2SYehuda Sadeh 	}
380142c1b124SIlya Dryomov 	if (handled_incremental)
38023d14c5d2SYehuda Sadeh 		goto done;
38033d14c5d2SYehuda Sadeh 
38043d14c5d2SYehuda Sadeh 	/* full maps */
38053d14c5d2SYehuda Sadeh 	ceph_decode_32_safe(&p, end, nr_maps, bad);
38063d14c5d2SYehuda Sadeh 	dout(" %d full maps\n", nr_maps);
38073d14c5d2SYehuda Sadeh 	while (nr_maps) {
38083d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, 2*sizeof(u32), bad);
38093d14c5d2SYehuda Sadeh 		epoch = ceph_decode_32(&p);
38103d14c5d2SYehuda Sadeh 		maplen = ceph_decode_32(&p);
38113d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, maplen, bad);
38123d14c5d2SYehuda Sadeh 		if (nr_maps > 1) {
38133d14c5d2SYehuda Sadeh 			dout("skipping non-latest full map %u len %d\n",
38143d14c5d2SYehuda Sadeh 			     epoch, maplen);
3815e5253a7bSIlya Dryomov 		} else if (osdc->osdmap->epoch >= epoch) {
38163d14c5d2SYehuda Sadeh 			dout("skipping full map %u len %d, "
38173d14c5d2SYehuda Sadeh 			     "older than our %u\n", epoch, maplen,
38183d14c5d2SYehuda Sadeh 			     osdc->osdmap->epoch);
38193d14c5d2SYehuda Sadeh 		} else {
38203d14c5d2SYehuda Sadeh 			dout("taking full map %u len %d\n", epoch, maplen);
38215aea3dcdSIlya Dryomov 			err = handle_one_map(osdc, p, p + maplen, false,
38225aea3dcdSIlya Dryomov 					     &need_resend, &need_resend_linger);
382342c1b124SIlya Dryomov 			if (err)
38243d14c5d2SYehuda Sadeh 				goto bad;
38253d14c5d2SYehuda Sadeh 		}
38263d14c5d2SYehuda Sadeh 		p += maplen;
38273d14c5d2SYehuda Sadeh 		nr_maps--;
38283d14c5d2SYehuda Sadeh 	}
38293d14c5d2SYehuda Sadeh 
38303d14c5d2SYehuda Sadeh done:
3831cd634fb6SSage Weil 	/*
3832cd634fb6SSage Weil 	 * subscribe to subsequent osdmap updates if full to ensure
3833cd634fb6SSage Weil 	 * we find out when we are no longer full and stop returning
3834cd634fb6SSage Weil 	 * ENOSPC.
3835cd634fb6SSage Weil 	 */
3836b7ec35b3SIlya Dryomov 	pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3837b7ec35b3SIlya Dryomov 	pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3838b7ec35b3SIlya Dryomov 		  ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
383942c1b124SIlya Dryomov 		  have_pool_full(osdc);
384058eb7932SJeff Layton 	if (was_pauserd || was_pausewr || pauserd || pausewr ||
384158eb7932SJeff Layton 	    osdc->osdmap->epoch < osdc->epoch_barrier)
384242c1b124SIlya Dryomov 		maybe_request_map(osdc);
3843cd634fb6SSage Weil 
38445aea3dcdSIlya Dryomov 	kick_requests(osdc, &need_resend, &need_resend_linger);
384542c1b124SIlya Dryomov 
3846fc36d0a4SJeff Layton 	ceph_osdc_abort_on_full(osdc);
384742c1b124SIlya Dryomov 	ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
384842c1b124SIlya Dryomov 			  osdc->osdmap->epoch);
38495aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
38503d14c5d2SYehuda Sadeh 	wake_up_all(&osdc->client->auth_wq);
38513d14c5d2SYehuda Sadeh 	return;
38523d14c5d2SYehuda Sadeh 
38533d14c5d2SYehuda Sadeh bad:
38543d14c5d2SYehuda Sadeh 	pr_err("osdc handle_map corrupt msg\n");
38553d14c5d2SYehuda Sadeh 	ceph_msg_dump(msg);
38565aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
38575aea3dcdSIlya Dryomov }
38585aea3dcdSIlya Dryomov 
38595aea3dcdSIlya Dryomov /*
38605aea3dcdSIlya Dryomov  * Resubmit requests pending on the given osd.
38615aea3dcdSIlya Dryomov  */
38625aea3dcdSIlya Dryomov static void kick_osd_requests(struct ceph_osd *osd)
38635aea3dcdSIlya Dryomov {
38645aea3dcdSIlya Dryomov 	struct rb_node *n;
38655aea3dcdSIlya Dryomov 
3866a02a946dSIlya Dryomov 	clear_backoffs(osd);
3867a02a946dSIlya Dryomov 
3868922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; ) {
38695aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
38705aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
38715aea3dcdSIlya Dryomov 
3872922dab61SIlya Dryomov 		n = rb_next(n); /* cancel_linger_request() */
3873922dab61SIlya Dryomov 
38745aea3dcdSIlya Dryomov 		if (!req->r_linger) {
38755aea3dcdSIlya Dryomov 			if (!req->r_t.paused)
38765aea3dcdSIlya Dryomov 				send_request(req);
3877922dab61SIlya Dryomov 		} else {
3878922dab61SIlya Dryomov 			cancel_linger_request(req);
38795aea3dcdSIlya Dryomov 		}
38805aea3dcdSIlya Dryomov 	}
3881922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_linger_requests); n; n = rb_next(n)) {
3882922dab61SIlya Dryomov 		struct ceph_osd_linger_request *lreq =
3883922dab61SIlya Dryomov 		    rb_entry(n, struct ceph_osd_linger_request, node);
3884922dab61SIlya Dryomov 
3885922dab61SIlya Dryomov 		send_linger(lreq);
3886922dab61SIlya Dryomov 	}
38875aea3dcdSIlya Dryomov }
38885aea3dcdSIlya Dryomov 
38895aea3dcdSIlya Dryomov /*
38905aea3dcdSIlya Dryomov  * If the osd connection drops, we need to resubmit all requests.
38915aea3dcdSIlya Dryomov  */
38925aea3dcdSIlya Dryomov static void osd_fault(struct ceph_connection *con)
38935aea3dcdSIlya Dryomov {
38945aea3dcdSIlya Dryomov 	struct ceph_osd *osd = con->private;
38955aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
38965aea3dcdSIlya Dryomov 
38975aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
38985aea3dcdSIlya Dryomov 
38995aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
39005aea3dcdSIlya Dryomov 	if (!osd_registered(osd)) {
39015aea3dcdSIlya Dryomov 		dout("%s osd%d unknown\n", __func__, osd->o_osd);
39025aea3dcdSIlya Dryomov 		goto out_unlock;
39035aea3dcdSIlya Dryomov 	}
39045aea3dcdSIlya Dryomov 
39055aea3dcdSIlya Dryomov 	if (!reopen_osd(osd))
39065aea3dcdSIlya Dryomov 		kick_osd_requests(osd);
39075aea3dcdSIlya Dryomov 	maybe_request_map(osdc);
39085aea3dcdSIlya Dryomov 
39095aea3dcdSIlya Dryomov out_unlock:
39105aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
39113d14c5d2SYehuda Sadeh }
39123d14c5d2SYehuda Sadeh 
3913a02a946dSIlya Dryomov struct MOSDBackoff {
3914a02a946dSIlya Dryomov 	struct ceph_spg spgid;
3915a02a946dSIlya Dryomov 	u32 map_epoch;
3916a02a946dSIlya Dryomov 	u8 op;
3917a02a946dSIlya Dryomov 	u64 id;
3918a02a946dSIlya Dryomov 	struct ceph_hobject_id *begin;
3919a02a946dSIlya Dryomov 	struct ceph_hobject_id *end;
3920a02a946dSIlya Dryomov };
3921a02a946dSIlya Dryomov 
3922a02a946dSIlya Dryomov static int decode_MOSDBackoff(const struct ceph_msg *msg, struct MOSDBackoff *m)
3923a02a946dSIlya Dryomov {
3924a02a946dSIlya Dryomov 	void *p = msg->front.iov_base;
3925a02a946dSIlya Dryomov 	void *const end = p + msg->front.iov_len;
3926a02a946dSIlya Dryomov 	u8 struct_v;
3927a02a946dSIlya Dryomov 	u32 struct_len;
3928a02a946dSIlya Dryomov 	int ret;
3929a02a946dSIlya Dryomov 
3930a02a946dSIlya Dryomov 	ret = ceph_start_decoding(&p, end, 1, "spg_t", &struct_v, &struct_len);
3931a02a946dSIlya Dryomov 	if (ret)
3932a02a946dSIlya Dryomov 		return ret;
3933a02a946dSIlya Dryomov 
3934a02a946dSIlya Dryomov 	ret = ceph_decode_pgid(&p, end, &m->spgid.pgid);
3935a02a946dSIlya Dryomov 	if (ret)
3936a02a946dSIlya Dryomov 		return ret;
3937a02a946dSIlya Dryomov 
3938a02a946dSIlya Dryomov 	ceph_decode_8_safe(&p, end, m->spgid.shard, e_inval);
3939a02a946dSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->map_epoch, e_inval);
3940a02a946dSIlya Dryomov 	ceph_decode_8_safe(&p, end, m->op, e_inval);
3941a02a946dSIlya Dryomov 	ceph_decode_64_safe(&p, end, m->id, e_inval);
3942a02a946dSIlya Dryomov 
3943a02a946dSIlya Dryomov 	m->begin = kzalloc(sizeof(*m->begin), GFP_NOIO);
3944a02a946dSIlya Dryomov 	if (!m->begin)
3945a02a946dSIlya Dryomov 		return -ENOMEM;
3946a02a946dSIlya Dryomov 
3947a02a946dSIlya Dryomov 	ret = decode_hoid(&p, end, m->begin);
3948a02a946dSIlya Dryomov 	if (ret) {
3949a02a946dSIlya Dryomov 		free_hoid(m->begin);
3950a02a946dSIlya Dryomov 		return ret;
3951a02a946dSIlya Dryomov 	}
3952a02a946dSIlya Dryomov 
3953a02a946dSIlya Dryomov 	m->end = kzalloc(sizeof(*m->end), GFP_NOIO);
3954a02a946dSIlya Dryomov 	if (!m->end) {
3955a02a946dSIlya Dryomov 		free_hoid(m->begin);
3956a02a946dSIlya Dryomov 		return -ENOMEM;
3957a02a946dSIlya Dryomov 	}
3958a02a946dSIlya Dryomov 
3959a02a946dSIlya Dryomov 	ret = decode_hoid(&p, end, m->end);
3960a02a946dSIlya Dryomov 	if (ret) {
3961a02a946dSIlya Dryomov 		free_hoid(m->begin);
3962a02a946dSIlya Dryomov 		free_hoid(m->end);
3963a02a946dSIlya Dryomov 		return ret;
3964a02a946dSIlya Dryomov 	}
3965a02a946dSIlya Dryomov 
3966a02a946dSIlya Dryomov 	return 0;
3967a02a946dSIlya Dryomov 
3968a02a946dSIlya Dryomov e_inval:
3969a02a946dSIlya Dryomov 	return -EINVAL;
3970a02a946dSIlya Dryomov }
3971a02a946dSIlya Dryomov 
3972a02a946dSIlya Dryomov static struct ceph_msg *create_backoff_message(
3973a02a946dSIlya Dryomov 				const struct ceph_osd_backoff *backoff,
3974a02a946dSIlya Dryomov 				u32 map_epoch)
3975a02a946dSIlya Dryomov {
3976a02a946dSIlya Dryomov 	struct ceph_msg *msg;
3977a02a946dSIlya Dryomov 	void *p, *end;
3978a02a946dSIlya Dryomov 	int msg_size;
3979a02a946dSIlya Dryomov 
3980a02a946dSIlya Dryomov 	msg_size = CEPH_ENCODING_START_BLK_LEN +
3981a02a946dSIlya Dryomov 			CEPH_PGID_ENCODING_LEN + 1; /* spgid */
3982a02a946dSIlya Dryomov 	msg_size += 4 + 1 + 8; /* map_epoch, op, id */
3983a02a946dSIlya Dryomov 	msg_size += CEPH_ENCODING_START_BLK_LEN +
3984a02a946dSIlya Dryomov 			hoid_encoding_size(backoff->begin);
3985a02a946dSIlya Dryomov 	msg_size += CEPH_ENCODING_START_BLK_LEN +
3986a02a946dSIlya Dryomov 			hoid_encoding_size(backoff->end);
3987a02a946dSIlya Dryomov 
3988a02a946dSIlya Dryomov 	msg = ceph_msg_new(CEPH_MSG_OSD_BACKOFF, msg_size, GFP_NOIO, true);
3989a02a946dSIlya Dryomov 	if (!msg)
3990a02a946dSIlya Dryomov 		return NULL;
3991a02a946dSIlya Dryomov 
3992a02a946dSIlya Dryomov 	p = msg->front.iov_base;
3993a02a946dSIlya Dryomov 	end = p + msg->front_alloc_len;
3994a02a946dSIlya Dryomov 
3995a02a946dSIlya Dryomov 	encode_spgid(&p, &backoff->spgid);
3996a02a946dSIlya Dryomov 	ceph_encode_32(&p, map_epoch);
3997a02a946dSIlya Dryomov 	ceph_encode_8(&p, CEPH_OSD_BACKOFF_OP_ACK_BLOCK);
3998a02a946dSIlya Dryomov 	ceph_encode_64(&p, backoff->id);
3999a02a946dSIlya Dryomov 	encode_hoid(&p, end, backoff->begin);
4000a02a946dSIlya Dryomov 	encode_hoid(&p, end, backoff->end);
4001a02a946dSIlya Dryomov 	BUG_ON(p != end);
4002a02a946dSIlya Dryomov 
4003a02a946dSIlya Dryomov 	msg->front.iov_len = p - msg->front.iov_base;
4004a02a946dSIlya Dryomov 	msg->hdr.version = cpu_to_le16(1); /* MOSDBackoff v1 */
4005a02a946dSIlya Dryomov 	msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
4006a02a946dSIlya Dryomov 
4007a02a946dSIlya Dryomov 	return msg;
4008a02a946dSIlya Dryomov }
4009a02a946dSIlya Dryomov 
4010a02a946dSIlya Dryomov static void handle_backoff_block(struct ceph_osd *osd, struct MOSDBackoff *m)
4011a02a946dSIlya Dryomov {
4012a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
4013a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
4014a02a946dSIlya Dryomov 	struct ceph_msg *msg;
4015a02a946dSIlya Dryomov 
4016a02a946dSIlya Dryomov 	dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4017a02a946dSIlya Dryomov 	     m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4018a02a946dSIlya Dryomov 
4019a02a946dSIlya Dryomov 	spg = lookup_spg_mapping(&osd->o_backoff_mappings, &m->spgid);
4020a02a946dSIlya Dryomov 	if (!spg) {
4021a02a946dSIlya Dryomov 		spg = alloc_spg_mapping();
4022a02a946dSIlya Dryomov 		if (!spg) {
4023a02a946dSIlya Dryomov 			pr_err("%s failed to allocate spg\n", __func__);
4024a02a946dSIlya Dryomov 			return;
4025a02a946dSIlya Dryomov 		}
4026a02a946dSIlya Dryomov 		spg->spgid = m->spgid; /* struct */
4027a02a946dSIlya Dryomov 		insert_spg_mapping(&osd->o_backoff_mappings, spg);
4028a02a946dSIlya Dryomov 	}
4029a02a946dSIlya Dryomov 
4030a02a946dSIlya Dryomov 	backoff = alloc_backoff();
4031a02a946dSIlya Dryomov 	if (!backoff) {
4032a02a946dSIlya Dryomov 		pr_err("%s failed to allocate backoff\n", __func__);
4033a02a946dSIlya Dryomov 		return;
4034a02a946dSIlya Dryomov 	}
4035a02a946dSIlya Dryomov 	backoff->spgid = m->spgid; /* struct */
4036a02a946dSIlya Dryomov 	backoff->id = m->id;
4037a02a946dSIlya Dryomov 	backoff->begin = m->begin;
4038a02a946dSIlya Dryomov 	m->begin = NULL; /* backoff now owns this */
4039a02a946dSIlya Dryomov 	backoff->end = m->end;
4040a02a946dSIlya Dryomov 	m->end = NULL;   /* ditto */
4041a02a946dSIlya Dryomov 
4042a02a946dSIlya Dryomov 	insert_backoff(&spg->backoffs, backoff);
4043a02a946dSIlya Dryomov 	insert_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4044a02a946dSIlya Dryomov 
4045a02a946dSIlya Dryomov 	/*
4046a02a946dSIlya Dryomov 	 * Ack with original backoff's epoch so that the OSD can
4047a02a946dSIlya Dryomov 	 * discard this if there was a PG split.
4048a02a946dSIlya Dryomov 	 */
4049a02a946dSIlya Dryomov 	msg = create_backoff_message(backoff, m->map_epoch);
4050a02a946dSIlya Dryomov 	if (!msg) {
4051a02a946dSIlya Dryomov 		pr_err("%s failed to allocate msg\n", __func__);
4052a02a946dSIlya Dryomov 		return;
4053a02a946dSIlya Dryomov 	}
4054a02a946dSIlya Dryomov 	ceph_con_send(&osd->o_con, msg);
4055a02a946dSIlya Dryomov }
4056a02a946dSIlya Dryomov 
4057a02a946dSIlya Dryomov static bool target_contained_by(const struct ceph_osd_request_target *t,
4058a02a946dSIlya Dryomov 				const struct ceph_hobject_id *begin,
4059a02a946dSIlya Dryomov 				const struct ceph_hobject_id *end)
4060a02a946dSIlya Dryomov {
4061a02a946dSIlya Dryomov 	struct ceph_hobject_id hoid;
4062a02a946dSIlya Dryomov 	int cmp;
4063a02a946dSIlya Dryomov 
4064a02a946dSIlya Dryomov 	hoid_fill_from_target(&hoid, t);
4065a02a946dSIlya Dryomov 	cmp = hoid_compare(&hoid, begin);
4066a02a946dSIlya Dryomov 	return !cmp || (cmp > 0 && hoid_compare(&hoid, end) < 0);
4067a02a946dSIlya Dryomov }
4068a02a946dSIlya Dryomov 
4069a02a946dSIlya Dryomov static void handle_backoff_unblock(struct ceph_osd *osd,
4070a02a946dSIlya Dryomov 				   const struct MOSDBackoff *m)
4071a02a946dSIlya Dryomov {
4072a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
4073a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
4074a02a946dSIlya Dryomov 	struct rb_node *n;
4075a02a946dSIlya Dryomov 
4076a02a946dSIlya Dryomov 	dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4077a02a946dSIlya Dryomov 	     m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4078a02a946dSIlya Dryomov 
4079a02a946dSIlya Dryomov 	backoff = lookup_backoff_by_id(&osd->o_backoffs_by_id, m->id);
4080a02a946dSIlya Dryomov 	if (!backoff) {
4081a02a946dSIlya Dryomov 		pr_err("%s osd%d spgid %llu.%xs%d id %llu backoff dne\n",
4082a02a946dSIlya Dryomov 		       __func__, osd->o_osd, m->spgid.pgid.pool,
4083a02a946dSIlya Dryomov 		       m->spgid.pgid.seed, m->spgid.shard, m->id);
4084a02a946dSIlya Dryomov 		return;
4085a02a946dSIlya Dryomov 	}
4086a02a946dSIlya Dryomov 
4087a02a946dSIlya Dryomov 	if (hoid_compare(backoff->begin, m->begin) &&
4088a02a946dSIlya Dryomov 	    hoid_compare(backoff->end, m->end)) {
4089a02a946dSIlya Dryomov 		pr_err("%s osd%d spgid %llu.%xs%d id %llu bad range?\n",
4090a02a946dSIlya Dryomov 		       __func__, osd->o_osd, m->spgid.pgid.pool,
4091a02a946dSIlya Dryomov 		       m->spgid.pgid.seed, m->spgid.shard, m->id);
4092a02a946dSIlya Dryomov 		/* unblock it anyway... */
4093a02a946dSIlya Dryomov 	}
4094a02a946dSIlya Dryomov 
4095a02a946dSIlya Dryomov 	spg = lookup_spg_mapping(&osd->o_backoff_mappings, &backoff->spgid);
4096a02a946dSIlya Dryomov 	BUG_ON(!spg);
4097a02a946dSIlya Dryomov 
4098a02a946dSIlya Dryomov 	erase_backoff(&spg->backoffs, backoff);
4099a02a946dSIlya Dryomov 	erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4100a02a946dSIlya Dryomov 	free_backoff(backoff);
4101a02a946dSIlya Dryomov 
4102a02a946dSIlya Dryomov 	if (RB_EMPTY_ROOT(&spg->backoffs)) {
4103a02a946dSIlya Dryomov 		erase_spg_mapping(&osd->o_backoff_mappings, spg);
4104a02a946dSIlya Dryomov 		free_spg_mapping(spg);
4105a02a946dSIlya Dryomov 	}
4106a02a946dSIlya Dryomov 
4107a02a946dSIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
4108a02a946dSIlya Dryomov 		struct ceph_osd_request *req =
4109a02a946dSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
4110a02a946dSIlya Dryomov 
4111a02a946dSIlya Dryomov 		if (!ceph_spg_compare(&req->r_t.spgid, &m->spgid)) {
4112a02a946dSIlya Dryomov 			/*
4113a02a946dSIlya Dryomov 			 * Match against @m, not @backoff -- the PG may
4114a02a946dSIlya Dryomov 			 * have split on the OSD.
4115a02a946dSIlya Dryomov 			 */
4116a02a946dSIlya Dryomov 			if (target_contained_by(&req->r_t, m->begin, m->end)) {
4117a02a946dSIlya Dryomov 				/*
4118a02a946dSIlya Dryomov 				 * If no other installed backoff applies,
4119a02a946dSIlya Dryomov 				 * resend.
4120a02a946dSIlya Dryomov 				 */
4121a02a946dSIlya Dryomov 				send_request(req);
4122a02a946dSIlya Dryomov 			}
4123a02a946dSIlya Dryomov 		}
4124a02a946dSIlya Dryomov 	}
4125a02a946dSIlya Dryomov }
4126a02a946dSIlya Dryomov 
4127a02a946dSIlya Dryomov static void handle_backoff(struct ceph_osd *osd, struct ceph_msg *msg)
4128a02a946dSIlya Dryomov {
4129a02a946dSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
4130a02a946dSIlya Dryomov 	struct MOSDBackoff m;
4131a02a946dSIlya Dryomov 	int ret;
4132a02a946dSIlya Dryomov 
4133a02a946dSIlya Dryomov 	down_read(&osdc->lock);
4134a02a946dSIlya Dryomov 	if (!osd_registered(osd)) {
4135a02a946dSIlya Dryomov 		dout("%s osd%d unknown\n", __func__, osd->o_osd);
4136a02a946dSIlya Dryomov 		up_read(&osdc->lock);
4137a02a946dSIlya Dryomov 		return;
4138a02a946dSIlya Dryomov 	}
4139a02a946dSIlya Dryomov 	WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
4140a02a946dSIlya Dryomov 
4141a02a946dSIlya Dryomov 	mutex_lock(&osd->lock);
4142a02a946dSIlya Dryomov 	ret = decode_MOSDBackoff(msg, &m);
4143a02a946dSIlya Dryomov 	if (ret) {
4144a02a946dSIlya Dryomov 		pr_err("failed to decode MOSDBackoff: %d\n", ret);
4145a02a946dSIlya Dryomov 		ceph_msg_dump(msg);
4146a02a946dSIlya Dryomov 		goto out_unlock;
4147a02a946dSIlya Dryomov 	}
4148a02a946dSIlya Dryomov 
4149a02a946dSIlya Dryomov 	switch (m.op) {
4150a02a946dSIlya Dryomov 	case CEPH_OSD_BACKOFF_OP_BLOCK:
4151a02a946dSIlya Dryomov 		handle_backoff_block(osd, &m);
4152a02a946dSIlya Dryomov 		break;
4153a02a946dSIlya Dryomov 	case CEPH_OSD_BACKOFF_OP_UNBLOCK:
4154a02a946dSIlya Dryomov 		handle_backoff_unblock(osd, &m);
4155a02a946dSIlya Dryomov 		break;
4156a02a946dSIlya Dryomov 	default:
4157a02a946dSIlya Dryomov 		pr_err("%s osd%d unknown op %d\n", __func__, osd->o_osd, m.op);
4158a02a946dSIlya Dryomov 	}
4159a02a946dSIlya Dryomov 
4160a02a946dSIlya Dryomov 	free_hoid(m.begin);
4161a02a946dSIlya Dryomov 	free_hoid(m.end);
4162a02a946dSIlya Dryomov 
4163a02a946dSIlya Dryomov out_unlock:
4164a02a946dSIlya Dryomov 	mutex_unlock(&osd->lock);
4165a02a946dSIlya Dryomov 	up_read(&osdc->lock);
4166a02a946dSIlya Dryomov }
4167a02a946dSIlya Dryomov 
41683d14c5d2SYehuda Sadeh /*
4169a40c4f10SYehuda Sadeh  * Process osd watch notifications
4170a40c4f10SYehuda Sadeh  */
41713c663bbdSAlex Elder static void handle_watch_notify(struct ceph_osd_client *osdc,
41723c663bbdSAlex Elder 				struct ceph_msg *msg)
4173a40c4f10SYehuda Sadeh {
4174922dab61SIlya Dryomov 	void *p = msg->front.iov_base;
4175922dab61SIlya Dryomov 	void *const end = p + msg->front.iov_len;
4176922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
4177922dab61SIlya Dryomov 	struct linger_work *lwork;
4178922dab61SIlya Dryomov 	u8 proto_ver, opcode;
4179922dab61SIlya Dryomov 	u64 cookie, notify_id;
4180922dab61SIlya Dryomov 	u64 notifier_id = 0;
418119079203SIlya Dryomov 	s32 return_code = 0;
4182922dab61SIlya Dryomov 	void *payload = NULL;
4183922dab61SIlya Dryomov 	u32 payload_len = 0;
4184a40c4f10SYehuda Sadeh 
4185a40c4f10SYehuda Sadeh 	ceph_decode_8_safe(&p, end, proto_ver, bad);
4186a40c4f10SYehuda Sadeh 	ceph_decode_8_safe(&p, end, opcode, bad);
4187a40c4f10SYehuda Sadeh 	ceph_decode_64_safe(&p, end, cookie, bad);
4188922dab61SIlya Dryomov 	p += 8; /* skip ver */
4189a40c4f10SYehuda Sadeh 	ceph_decode_64_safe(&p, end, notify_id, bad);
4190a40c4f10SYehuda Sadeh 
4191922dab61SIlya Dryomov 	if (proto_ver >= 1) {
4192922dab61SIlya Dryomov 		ceph_decode_32_safe(&p, end, payload_len, bad);
4193922dab61SIlya Dryomov 		ceph_decode_need(&p, end, payload_len, bad);
4194922dab61SIlya Dryomov 		payload = p;
4195922dab61SIlya Dryomov 		p += payload_len;
4196a40c4f10SYehuda Sadeh 	}
4197a40c4f10SYehuda Sadeh 
4198922dab61SIlya Dryomov 	if (le16_to_cpu(msg->hdr.version) >= 2)
419919079203SIlya Dryomov 		ceph_decode_32_safe(&p, end, return_code, bad);
4200922dab61SIlya Dryomov 
4201922dab61SIlya Dryomov 	if (le16_to_cpu(msg->hdr.version) >= 3)
4202922dab61SIlya Dryomov 		ceph_decode_64_safe(&p, end, notifier_id, bad);
4203922dab61SIlya Dryomov 
4204922dab61SIlya Dryomov 	down_read(&osdc->lock);
4205922dab61SIlya Dryomov 	lreq = lookup_linger_osdc(&osdc->linger_requests, cookie);
4206922dab61SIlya Dryomov 	if (!lreq) {
4207922dab61SIlya Dryomov 		dout("%s opcode %d cookie %llu dne\n", __func__, opcode,
4208922dab61SIlya Dryomov 		     cookie);
4209922dab61SIlya Dryomov 		goto out_unlock_osdc;
4210922dab61SIlya Dryomov 	}
4211922dab61SIlya Dryomov 
4212922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
421319079203SIlya Dryomov 	dout("%s opcode %d cookie %llu lreq %p is_watch %d\n", __func__,
421419079203SIlya Dryomov 	     opcode, cookie, lreq, lreq->is_watch);
4215922dab61SIlya Dryomov 	if (opcode == CEPH_WATCH_EVENT_DISCONNECT) {
4216922dab61SIlya Dryomov 		if (!lreq->last_error) {
4217922dab61SIlya Dryomov 			lreq->last_error = -ENOTCONN;
4218922dab61SIlya Dryomov 			queue_watch_error(lreq);
4219922dab61SIlya Dryomov 		}
422019079203SIlya Dryomov 	} else if (!lreq->is_watch) {
422119079203SIlya Dryomov 		/* CEPH_WATCH_EVENT_NOTIFY_COMPLETE */
422219079203SIlya Dryomov 		if (lreq->notify_id && lreq->notify_id != notify_id) {
422319079203SIlya Dryomov 			dout("lreq %p notify_id %llu != %llu, ignoring\n", lreq,
422419079203SIlya Dryomov 			     lreq->notify_id, notify_id);
422519079203SIlya Dryomov 		} else if (!completion_done(&lreq->notify_finish_wait)) {
422619079203SIlya Dryomov 			struct ceph_msg_data *data =
422719079203SIlya Dryomov 			    list_first_entry_or_null(&msg->data,
422819079203SIlya Dryomov 						     struct ceph_msg_data,
422919079203SIlya Dryomov 						     links);
423019079203SIlya Dryomov 
423119079203SIlya Dryomov 			if (data) {
423219079203SIlya Dryomov 				if (lreq->preply_pages) {
423319079203SIlya Dryomov 					WARN_ON(data->type !=
423419079203SIlya Dryomov 							CEPH_MSG_DATA_PAGES);
423519079203SIlya Dryomov 					*lreq->preply_pages = data->pages;
423619079203SIlya Dryomov 					*lreq->preply_len = data->length;
423719079203SIlya Dryomov 				} else {
423819079203SIlya Dryomov 					ceph_release_page_vector(data->pages,
423919079203SIlya Dryomov 					       calc_pages_for(0, data->length));
424019079203SIlya Dryomov 				}
424119079203SIlya Dryomov 			}
424219079203SIlya Dryomov 			lreq->notify_finish_error = return_code;
424319079203SIlya Dryomov 			complete_all(&lreq->notify_finish_wait);
424419079203SIlya Dryomov 		}
4245922dab61SIlya Dryomov 	} else {
4246922dab61SIlya Dryomov 		/* CEPH_WATCH_EVENT_NOTIFY */
4247922dab61SIlya Dryomov 		lwork = lwork_alloc(lreq, do_watch_notify);
4248922dab61SIlya Dryomov 		if (!lwork) {
4249922dab61SIlya Dryomov 			pr_err("failed to allocate notify-lwork\n");
4250922dab61SIlya Dryomov 			goto out_unlock_lreq;
4251922dab61SIlya Dryomov 		}
4252922dab61SIlya Dryomov 
4253922dab61SIlya Dryomov 		lwork->notify.notify_id = notify_id;
4254922dab61SIlya Dryomov 		lwork->notify.notifier_id = notifier_id;
4255922dab61SIlya Dryomov 		lwork->notify.payload = payload;
4256922dab61SIlya Dryomov 		lwork->notify.payload_len = payload_len;
4257922dab61SIlya Dryomov 		lwork->notify.msg = ceph_msg_get(msg);
4258922dab61SIlya Dryomov 		lwork_queue(lwork);
4259922dab61SIlya Dryomov 	}
4260922dab61SIlya Dryomov 
4261922dab61SIlya Dryomov out_unlock_lreq:
4262922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
4263922dab61SIlya Dryomov out_unlock_osdc:
4264922dab61SIlya Dryomov 	up_read(&osdc->lock);
4265a40c4f10SYehuda Sadeh 	return;
4266a40c4f10SYehuda Sadeh 
4267a40c4f10SYehuda Sadeh bad:
4268a40c4f10SYehuda Sadeh 	pr_err("osdc handle_watch_notify corrupt msg\n");
4269a40c4f10SYehuda Sadeh }
4270a40c4f10SYehuda Sadeh 
4271a40c4f10SYehuda Sadeh /*
42723d14c5d2SYehuda Sadeh  * Register request, send initial attempt.
42733d14c5d2SYehuda Sadeh  */
42743d14c5d2SYehuda Sadeh int ceph_osdc_start_request(struct ceph_osd_client *osdc,
42753d14c5d2SYehuda Sadeh 			    struct ceph_osd_request *req,
42763d14c5d2SYehuda Sadeh 			    bool nofail)
42773d14c5d2SYehuda Sadeh {
42785aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
42795aea3dcdSIlya Dryomov 	submit_request(req, false);
42805aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
42813d14c5d2SYehuda Sadeh 
42825aea3dcdSIlya Dryomov 	return 0;
42833d14c5d2SYehuda Sadeh }
42843d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_start_request);
42853d14c5d2SYehuda Sadeh 
42863d14c5d2SYehuda Sadeh /*
4287c297eb42SIlya Dryomov  * Unregister a registered request.  The request is not completed:
4288c297eb42SIlya Dryomov  * ->r_result isn't set and __complete_request() isn't called.
4289c9f9b93dSIlya Dryomov  */
4290c9f9b93dSIlya Dryomov void ceph_osdc_cancel_request(struct ceph_osd_request *req)
4291c9f9b93dSIlya Dryomov {
4292c9f9b93dSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
4293c9f9b93dSIlya Dryomov 
42945aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
42955aea3dcdSIlya Dryomov 	if (req->r_osd)
42965aea3dcdSIlya Dryomov 		cancel_request(req);
42975aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
4298c9f9b93dSIlya Dryomov }
4299c9f9b93dSIlya Dryomov EXPORT_SYMBOL(ceph_osdc_cancel_request);
4300c9f9b93dSIlya Dryomov 
4301c9f9b93dSIlya Dryomov /*
430242b06965SIlya Dryomov  * @timeout: in jiffies, 0 means "wait forever"
430342b06965SIlya Dryomov  */
430442b06965SIlya Dryomov static int wait_request_timeout(struct ceph_osd_request *req,
430542b06965SIlya Dryomov 				unsigned long timeout)
430642b06965SIlya Dryomov {
430742b06965SIlya Dryomov 	long left;
430842b06965SIlya Dryomov 
430942b06965SIlya Dryomov 	dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
43100e76abf2SYan, Zheng 	left = wait_for_completion_killable_timeout(&req->r_completion,
431142b06965SIlya Dryomov 						ceph_timeout_jiffies(timeout));
431242b06965SIlya Dryomov 	if (left <= 0) {
431342b06965SIlya Dryomov 		left = left ?: -ETIMEDOUT;
431442b06965SIlya Dryomov 		ceph_osdc_cancel_request(req);
431542b06965SIlya Dryomov 	} else {
431642b06965SIlya Dryomov 		left = req->r_result; /* completed */
431742b06965SIlya Dryomov 	}
431842b06965SIlya Dryomov 
431942b06965SIlya Dryomov 	return left;
432042b06965SIlya Dryomov }
432142b06965SIlya Dryomov 
432242b06965SIlya Dryomov /*
43233d14c5d2SYehuda Sadeh  * wait for a request to complete
43243d14c5d2SYehuda Sadeh  */
43253d14c5d2SYehuda Sadeh int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
43263d14c5d2SYehuda Sadeh 			   struct ceph_osd_request *req)
43273d14c5d2SYehuda Sadeh {
432842b06965SIlya Dryomov 	return wait_request_timeout(req, 0);
43293d14c5d2SYehuda Sadeh }
43303d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_wait_request);
43313d14c5d2SYehuda Sadeh 
43323d14c5d2SYehuda Sadeh /*
43333d14c5d2SYehuda Sadeh  * sync - wait for all in-flight requests to flush.  avoid starvation.
43343d14c5d2SYehuda Sadeh  */
43353d14c5d2SYehuda Sadeh void ceph_osdc_sync(struct ceph_osd_client *osdc)
43363d14c5d2SYehuda Sadeh {
43375aea3dcdSIlya Dryomov 	struct rb_node *n, *p;
43385aea3dcdSIlya Dryomov 	u64 last_tid = atomic64_read(&osdc->last_tid);
43393d14c5d2SYehuda Sadeh 
43405aea3dcdSIlya Dryomov again:
43415aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
43425aea3dcdSIlya Dryomov 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
43435aea3dcdSIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
43445aea3dcdSIlya Dryomov 
43455aea3dcdSIlya Dryomov 		mutex_lock(&osd->lock);
43465aea3dcdSIlya Dryomov 		for (p = rb_first(&osd->o_requests); p; p = rb_next(p)) {
43475aea3dcdSIlya Dryomov 			struct ceph_osd_request *req =
43485aea3dcdSIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
43495aea3dcdSIlya Dryomov 
43503d14c5d2SYehuda Sadeh 			if (req->r_tid > last_tid)
43513d14c5d2SYehuda Sadeh 				break;
43523d14c5d2SYehuda Sadeh 
43535aea3dcdSIlya Dryomov 			if (!(req->r_flags & CEPH_OSD_FLAG_WRITE))
43543d14c5d2SYehuda Sadeh 				continue;
43553d14c5d2SYehuda Sadeh 
43563d14c5d2SYehuda Sadeh 			ceph_osdc_get_request(req);
43575aea3dcdSIlya Dryomov 			mutex_unlock(&osd->lock);
43585aea3dcdSIlya Dryomov 			up_read(&osdc->lock);
43595aea3dcdSIlya Dryomov 			dout("%s waiting on req %p tid %llu last_tid %llu\n",
43605aea3dcdSIlya Dryomov 			     __func__, req, req->r_tid, last_tid);
4361b18b9550SIlya Dryomov 			wait_for_completion(&req->r_completion);
43623d14c5d2SYehuda Sadeh 			ceph_osdc_put_request(req);
43635aea3dcdSIlya Dryomov 			goto again;
43643d14c5d2SYehuda Sadeh 		}
43655aea3dcdSIlya Dryomov 
43665aea3dcdSIlya Dryomov 		mutex_unlock(&osd->lock);
43675aea3dcdSIlya Dryomov 	}
43685aea3dcdSIlya Dryomov 
43695aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
43705aea3dcdSIlya Dryomov 	dout("%s done last_tid %llu\n", __func__, last_tid);
43713d14c5d2SYehuda Sadeh }
43723d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_sync);
43733d14c5d2SYehuda Sadeh 
4374922dab61SIlya Dryomov static struct ceph_osd_request *
4375922dab61SIlya Dryomov alloc_linger_request(struct ceph_osd_linger_request *lreq)
4376922dab61SIlya Dryomov {
4377922dab61SIlya Dryomov 	struct ceph_osd_request *req;
4378922dab61SIlya Dryomov 
4379922dab61SIlya Dryomov 	req = ceph_osdc_alloc_request(lreq->osdc, NULL, 1, false, GFP_NOIO);
4380922dab61SIlya Dryomov 	if (!req)
4381922dab61SIlya Dryomov 		return NULL;
4382922dab61SIlya Dryomov 
4383922dab61SIlya Dryomov 	ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
4384922dab61SIlya Dryomov 	ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
4385922dab61SIlya Dryomov 
4386922dab61SIlya Dryomov 	if (ceph_osdc_alloc_messages(req, GFP_NOIO)) {
4387922dab61SIlya Dryomov 		ceph_osdc_put_request(req);
4388922dab61SIlya Dryomov 		return NULL;
4389922dab61SIlya Dryomov 	}
4390922dab61SIlya Dryomov 
4391922dab61SIlya Dryomov 	return req;
4392922dab61SIlya Dryomov }
4393922dab61SIlya Dryomov 
4394922dab61SIlya Dryomov /*
4395922dab61SIlya Dryomov  * Returns a handle, caller owns a ref.
4396922dab61SIlya Dryomov  */
4397922dab61SIlya Dryomov struct ceph_osd_linger_request *
4398922dab61SIlya Dryomov ceph_osdc_watch(struct ceph_osd_client *osdc,
4399922dab61SIlya Dryomov 		struct ceph_object_id *oid,
4400922dab61SIlya Dryomov 		struct ceph_object_locator *oloc,
4401922dab61SIlya Dryomov 		rados_watchcb2_t wcb,
4402922dab61SIlya Dryomov 		rados_watcherrcb_t errcb,
4403922dab61SIlya Dryomov 		void *data)
4404922dab61SIlya Dryomov {
4405922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
4406922dab61SIlya Dryomov 	int ret;
4407922dab61SIlya Dryomov 
4408922dab61SIlya Dryomov 	lreq = linger_alloc(osdc);
4409922dab61SIlya Dryomov 	if (!lreq)
4410922dab61SIlya Dryomov 		return ERR_PTR(-ENOMEM);
4411922dab61SIlya Dryomov 
441219079203SIlya Dryomov 	lreq->is_watch = true;
4413922dab61SIlya Dryomov 	lreq->wcb = wcb;
4414922dab61SIlya Dryomov 	lreq->errcb = errcb;
4415922dab61SIlya Dryomov 	lreq->data = data;
4416b07d3c4bSIlya Dryomov 	lreq->watch_valid_thru = jiffies;
4417922dab61SIlya Dryomov 
4418922dab61SIlya Dryomov 	ceph_oid_copy(&lreq->t.base_oid, oid);
4419922dab61SIlya Dryomov 	ceph_oloc_copy(&lreq->t.base_oloc, oloc);
442054ea0046SIlya Dryomov 	lreq->t.flags = CEPH_OSD_FLAG_WRITE;
44211134e091SDeepa Dinamani 	ktime_get_real_ts(&lreq->mtime);
4422922dab61SIlya Dryomov 
4423922dab61SIlya Dryomov 	lreq->reg_req = alloc_linger_request(lreq);
4424922dab61SIlya Dryomov 	if (!lreq->reg_req) {
4425922dab61SIlya Dryomov 		ret = -ENOMEM;
4426922dab61SIlya Dryomov 		goto err_put_lreq;
4427922dab61SIlya Dryomov 	}
4428922dab61SIlya Dryomov 
4429922dab61SIlya Dryomov 	lreq->ping_req = alloc_linger_request(lreq);
4430922dab61SIlya Dryomov 	if (!lreq->ping_req) {
4431922dab61SIlya Dryomov 		ret = -ENOMEM;
4432922dab61SIlya Dryomov 		goto err_put_lreq;
4433922dab61SIlya Dryomov 	}
4434922dab61SIlya Dryomov 
4435922dab61SIlya Dryomov 	down_write(&osdc->lock);
4436922dab61SIlya Dryomov 	linger_register(lreq); /* before osd_req_op_* */
4437922dab61SIlya Dryomov 	osd_req_op_watch_init(lreq->reg_req, 0, lreq->linger_id,
4438922dab61SIlya Dryomov 			      CEPH_OSD_WATCH_OP_WATCH);
4439922dab61SIlya Dryomov 	osd_req_op_watch_init(lreq->ping_req, 0, lreq->linger_id,
4440922dab61SIlya Dryomov 			      CEPH_OSD_WATCH_OP_PING);
4441922dab61SIlya Dryomov 	linger_submit(lreq);
4442922dab61SIlya Dryomov 	up_write(&osdc->lock);
4443922dab61SIlya Dryomov 
4444922dab61SIlya Dryomov 	ret = linger_reg_commit_wait(lreq);
4445922dab61SIlya Dryomov 	if (ret) {
4446922dab61SIlya Dryomov 		linger_cancel(lreq);
4447922dab61SIlya Dryomov 		goto err_put_lreq;
4448922dab61SIlya Dryomov 	}
4449922dab61SIlya Dryomov 
4450922dab61SIlya Dryomov 	return lreq;
4451922dab61SIlya Dryomov 
4452922dab61SIlya Dryomov err_put_lreq:
4453922dab61SIlya Dryomov 	linger_put(lreq);
4454922dab61SIlya Dryomov 	return ERR_PTR(ret);
4455922dab61SIlya Dryomov }
4456922dab61SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_watch);
4457922dab61SIlya Dryomov 
4458922dab61SIlya Dryomov /*
4459922dab61SIlya Dryomov  * Releases a ref.
4460922dab61SIlya Dryomov  *
4461922dab61SIlya Dryomov  * Times out after mount_timeout to preserve rbd unmap behaviour
4462922dab61SIlya Dryomov  * introduced in 2894e1d76974 ("rbd: timeout watch teardown on unmap
4463922dab61SIlya Dryomov  * with mount_timeout").
4464922dab61SIlya Dryomov  */
4465922dab61SIlya Dryomov int ceph_osdc_unwatch(struct ceph_osd_client *osdc,
4466922dab61SIlya Dryomov 		      struct ceph_osd_linger_request *lreq)
4467922dab61SIlya Dryomov {
4468922dab61SIlya Dryomov 	struct ceph_options *opts = osdc->client->options;
4469922dab61SIlya Dryomov 	struct ceph_osd_request *req;
4470922dab61SIlya Dryomov 	int ret;
4471922dab61SIlya Dryomov 
4472922dab61SIlya Dryomov 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4473922dab61SIlya Dryomov 	if (!req)
4474922dab61SIlya Dryomov 		return -ENOMEM;
4475922dab61SIlya Dryomov 
4476922dab61SIlya Dryomov 	ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
4477922dab61SIlya Dryomov 	ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
447854ea0046SIlya Dryomov 	req->r_flags = CEPH_OSD_FLAG_WRITE;
44791134e091SDeepa Dinamani 	ktime_get_real_ts(&req->r_mtime);
4480922dab61SIlya Dryomov 	osd_req_op_watch_init(req, 0, lreq->linger_id,
4481922dab61SIlya Dryomov 			      CEPH_OSD_WATCH_OP_UNWATCH);
4482922dab61SIlya Dryomov 
4483922dab61SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4484922dab61SIlya Dryomov 	if (ret)
4485922dab61SIlya Dryomov 		goto out_put_req;
4486922dab61SIlya Dryomov 
4487922dab61SIlya Dryomov 	ceph_osdc_start_request(osdc, req, false);
4488922dab61SIlya Dryomov 	linger_cancel(lreq);
4489922dab61SIlya Dryomov 	linger_put(lreq);
4490922dab61SIlya Dryomov 	ret = wait_request_timeout(req, opts->mount_timeout);
4491922dab61SIlya Dryomov 
4492922dab61SIlya Dryomov out_put_req:
4493922dab61SIlya Dryomov 	ceph_osdc_put_request(req);
4494922dab61SIlya Dryomov 	return ret;
4495922dab61SIlya Dryomov }
4496922dab61SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_unwatch);
4497922dab61SIlya Dryomov 
4498922dab61SIlya Dryomov static int osd_req_op_notify_ack_init(struct ceph_osd_request *req, int which,
4499922dab61SIlya Dryomov 				      u64 notify_id, u64 cookie, void *payload,
4500922dab61SIlya Dryomov 				      size_t payload_len)
4501922dab61SIlya Dryomov {
4502922dab61SIlya Dryomov 	struct ceph_osd_req_op *op;
4503922dab61SIlya Dryomov 	struct ceph_pagelist *pl;
4504922dab61SIlya Dryomov 	int ret;
4505922dab61SIlya Dryomov 
4506922dab61SIlya Dryomov 	op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY_ACK, 0);
4507922dab61SIlya Dryomov 
4508922dab61SIlya Dryomov 	pl = kmalloc(sizeof(*pl), GFP_NOIO);
4509922dab61SIlya Dryomov 	if (!pl)
4510922dab61SIlya Dryomov 		return -ENOMEM;
4511922dab61SIlya Dryomov 
4512922dab61SIlya Dryomov 	ceph_pagelist_init(pl);
4513922dab61SIlya Dryomov 	ret = ceph_pagelist_encode_64(pl, notify_id);
4514922dab61SIlya Dryomov 	ret |= ceph_pagelist_encode_64(pl, cookie);
4515922dab61SIlya Dryomov 	if (payload) {
4516922dab61SIlya Dryomov 		ret |= ceph_pagelist_encode_32(pl, payload_len);
4517922dab61SIlya Dryomov 		ret |= ceph_pagelist_append(pl, payload, payload_len);
4518922dab61SIlya Dryomov 	} else {
4519922dab61SIlya Dryomov 		ret |= ceph_pagelist_encode_32(pl, 0);
4520922dab61SIlya Dryomov 	}
4521922dab61SIlya Dryomov 	if (ret) {
4522922dab61SIlya Dryomov 		ceph_pagelist_release(pl);
4523922dab61SIlya Dryomov 		return -ENOMEM;
4524922dab61SIlya Dryomov 	}
4525922dab61SIlya Dryomov 
4526922dab61SIlya Dryomov 	ceph_osd_data_pagelist_init(&op->notify_ack.request_data, pl);
4527922dab61SIlya Dryomov 	op->indata_len = pl->length;
4528922dab61SIlya Dryomov 	return 0;
4529922dab61SIlya Dryomov }
4530922dab61SIlya Dryomov 
4531922dab61SIlya Dryomov int ceph_osdc_notify_ack(struct ceph_osd_client *osdc,
4532922dab61SIlya Dryomov 			 struct ceph_object_id *oid,
4533922dab61SIlya Dryomov 			 struct ceph_object_locator *oloc,
4534922dab61SIlya Dryomov 			 u64 notify_id,
4535922dab61SIlya Dryomov 			 u64 cookie,
4536922dab61SIlya Dryomov 			 void *payload,
4537922dab61SIlya Dryomov 			 size_t payload_len)
4538922dab61SIlya Dryomov {
4539922dab61SIlya Dryomov 	struct ceph_osd_request *req;
4540922dab61SIlya Dryomov 	int ret;
4541922dab61SIlya Dryomov 
4542922dab61SIlya Dryomov 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4543922dab61SIlya Dryomov 	if (!req)
4544922dab61SIlya Dryomov 		return -ENOMEM;
4545922dab61SIlya Dryomov 
4546922dab61SIlya Dryomov 	ceph_oid_copy(&req->r_base_oid, oid);
4547922dab61SIlya Dryomov 	ceph_oloc_copy(&req->r_base_oloc, oloc);
4548922dab61SIlya Dryomov 	req->r_flags = CEPH_OSD_FLAG_READ;
4549922dab61SIlya Dryomov 
4550922dab61SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4551922dab61SIlya Dryomov 	if (ret)
4552922dab61SIlya Dryomov 		goto out_put_req;
4553922dab61SIlya Dryomov 
4554922dab61SIlya Dryomov 	ret = osd_req_op_notify_ack_init(req, 0, notify_id, cookie, payload,
4555922dab61SIlya Dryomov 					 payload_len);
4556922dab61SIlya Dryomov 	if (ret)
4557922dab61SIlya Dryomov 		goto out_put_req;
4558922dab61SIlya Dryomov 
4559922dab61SIlya Dryomov 	ceph_osdc_start_request(osdc, req, false);
4560922dab61SIlya Dryomov 	ret = ceph_osdc_wait_request(osdc, req);
4561922dab61SIlya Dryomov 
4562922dab61SIlya Dryomov out_put_req:
4563922dab61SIlya Dryomov 	ceph_osdc_put_request(req);
4564922dab61SIlya Dryomov 	return ret;
4565922dab61SIlya Dryomov }
4566922dab61SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_notify_ack);
4567922dab61SIlya Dryomov 
456819079203SIlya Dryomov static int osd_req_op_notify_init(struct ceph_osd_request *req, int which,
456919079203SIlya Dryomov 				  u64 cookie, u32 prot_ver, u32 timeout,
457019079203SIlya Dryomov 				  void *payload, size_t payload_len)
457119079203SIlya Dryomov {
457219079203SIlya Dryomov 	struct ceph_osd_req_op *op;
457319079203SIlya Dryomov 	struct ceph_pagelist *pl;
457419079203SIlya Dryomov 	int ret;
457519079203SIlya Dryomov 
457619079203SIlya Dryomov 	op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY, 0);
457719079203SIlya Dryomov 	op->notify.cookie = cookie;
457819079203SIlya Dryomov 
457919079203SIlya Dryomov 	pl = kmalloc(sizeof(*pl), GFP_NOIO);
458019079203SIlya Dryomov 	if (!pl)
458119079203SIlya Dryomov 		return -ENOMEM;
458219079203SIlya Dryomov 
458319079203SIlya Dryomov 	ceph_pagelist_init(pl);
458419079203SIlya Dryomov 	ret = ceph_pagelist_encode_32(pl, 1); /* prot_ver */
458519079203SIlya Dryomov 	ret |= ceph_pagelist_encode_32(pl, timeout);
458619079203SIlya Dryomov 	ret |= ceph_pagelist_encode_32(pl, payload_len);
458719079203SIlya Dryomov 	ret |= ceph_pagelist_append(pl, payload, payload_len);
458819079203SIlya Dryomov 	if (ret) {
458919079203SIlya Dryomov 		ceph_pagelist_release(pl);
459019079203SIlya Dryomov 		return -ENOMEM;
459119079203SIlya Dryomov 	}
459219079203SIlya Dryomov 
459319079203SIlya Dryomov 	ceph_osd_data_pagelist_init(&op->notify.request_data, pl);
459419079203SIlya Dryomov 	op->indata_len = pl->length;
459519079203SIlya Dryomov 	return 0;
459619079203SIlya Dryomov }
459719079203SIlya Dryomov 
459819079203SIlya Dryomov /*
459919079203SIlya Dryomov  * @timeout: in seconds
460019079203SIlya Dryomov  *
460119079203SIlya Dryomov  * @preply_{pages,len} are initialized both on success and error.
460219079203SIlya Dryomov  * The caller is responsible for:
460319079203SIlya Dryomov  *
460419079203SIlya Dryomov  *     ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len))
460519079203SIlya Dryomov  */
460619079203SIlya Dryomov int ceph_osdc_notify(struct ceph_osd_client *osdc,
460719079203SIlya Dryomov 		     struct ceph_object_id *oid,
460819079203SIlya Dryomov 		     struct ceph_object_locator *oloc,
460919079203SIlya Dryomov 		     void *payload,
461019079203SIlya Dryomov 		     size_t payload_len,
461119079203SIlya Dryomov 		     u32 timeout,
461219079203SIlya Dryomov 		     struct page ***preply_pages,
461319079203SIlya Dryomov 		     size_t *preply_len)
461419079203SIlya Dryomov {
461519079203SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
461619079203SIlya Dryomov 	struct page **pages;
461719079203SIlya Dryomov 	int ret;
461819079203SIlya Dryomov 
461919079203SIlya Dryomov 	WARN_ON(!timeout);
462019079203SIlya Dryomov 	if (preply_pages) {
462119079203SIlya Dryomov 		*preply_pages = NULL;
462219079203SIlya Dryomov 		*preply_len = 0;
462319079203SIlya Dryomov 	}
462419079203SIlya Dryomov 
462519079203SIlya Dryomov 	lreq = linger_alloc(osdc);
462619079203SIlya Dryomov 	if (!lreq)
462719079203SIlya Dryomov 		return -ENOMEM;
462819079203SIlya Dryomov 
462919079203SIlya Dryomov 	lreq->preply_pages = preply_pages;
463019079203SIlya Dryomov 	lreq->preply_len = preply_len;
463119079203SIlya Dryomov 
463219079203SIlya Dryomov 	ceph_oid_copy(&lreq->t.base_oid, oid);
463319079203SIlya Dryomov 	ceph_oloc_copy(&lreq->t.base_oloc, oloc);
463419079203SIlya Dryomov 	lreq->t.flags = CEPH_OSD_FLAG_READ;
463519079203SIlya Dryomov 
463619079203SIlya Dryomov 	lreq->reg_req = alloc_linger_request(lreq);
463719079203SIlya Dryomov 	if (!lreq->reg_req) {
463819079203SIlya Dryomov 		ret = -ENOMEM;
463919079203SIlya Dryomov 		goto out_put_lreq;
464019079203SIlya Dryomov 	}
464119079203SIlya Dryomov 
464219079203SIlya Dryomov 	/* for notify_id */
464319079203SIlya Dryomov 	pages = ceph_alloc_page_vector(1, GFP_NOIO);
464419079203SIlya Dryomov 	if (IS_ERR(pages)) {
464519079203SIlya Dryomov 		ret = PTR_ERR(pages);
464619079203SIlya Dryomov 		goto out_put_lreq;
464719079203SIlya Dryomov 	}
464819079203SIlya Dryomov 
464919079203SIlya Dryomov 	down_write(&osdc->lock);
465019079203SIlya Dryomov 	linger_register(lreq); /* before osd_req_op_* */
465119079203SIlya Dryomov 	ret = osd_req_op_notify_init(lreq->reg_req, 0, lreq->linger_id, 1,
465219079203SIlya Dryomov 				     timeout, payload, payload_len);
465319079203SIlya Dryomov 	if (ret) {
465419079203SIlya Dryomov 		linger_unregister(lreq);
465519079203SIlya Dryomov 		up_write(&osdc->lock);
465619079203SIlya Dryomov 		ceph_release_page_vector(pages, 1);
465719079203SIlya Dryomov 		goto out_put_lreq;
465819079203SIlya Dryomov 	}
465919079203SIlya Dryomov 	ceph_osd_data_pages_init(osd_req_op_data(lreq->reg_req, 0, notify,
466019079203SIlya Dryomov 						 response_data),
466119079203SIlya Dryomov 				 pages, PAGE_SIZE, 0, false, true);
466219079203SIlya Dryomov 	linger_submit(lreq);
466319079203SIlya Dryomov 	up_write(&osdc->lock);
466419079203SIlya Dryomov 
466519079203SIlya Dryomov 	ret = linger_reg_commit_wait(lreq);
466619079203SIlya Dryomov 	if (!ret)
466719079203SIlya Dryomov 		ret = linger_notify_finish_wait(lreq);
466819079203SIlya Dryomov 	else
466919079203SIlya Dryomov 		dout("lreq %p failed to initiate notify %d\n", lreq, ret);
467019079203SIlya Dryomov 
467119079203SIlya Dryomov 	linger_cancel(lreq);
467219079203SIlya Dryomov out_put_lreq:
467319079203SIlya Dryomov 	linger_put(lreq);
467419079203SIlya Dryomov 	return ret;
467519079203SIlya Dryomov }
467619079203SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_notify);
467719079203SIlya Dryomov 
46783d14c5d2SYehuda Sadeh /*
4679b07d3c4bSIlya Dryomov  * Return the number of milliseconds since the watch was last
4680b07d3c4bSIlya Dryomov  * confirmed, or an error.  If there is an error, the watch is no
4681b07d3c4bSIlya Dryomov  * longer valid, and should be destroyed with ceph_osdc_unwatch().
4682b07d3c4bSIlya Dryomov  */
4683b07d3c4bSIlya Dryomov int ceph_osdc_watch_check(struct ceph_osd_client *osdc,
4684b07d3c4bSIlya Dryomov 			  struct ceph_osd_linger_request *lreq)
4685b07d3c4bSIlya Dryomov {
4686b07d3c4bSIlya Dryomov 	unsigned long stamp, age;
4687b07d3c4bSIlya Dryomov 	int ret;
4688b07d3c4bSIlya Dryomov 
4689b07d3c4bSIlya Dryomov 	down_read(&osdc->lock);
4690b07d3c4bSIlya Dryomov 	mutex_lock(&lreq->lock);
4691b07d3c4bSIlya Dryomov 	stamp = lreq->watch_valid_thru;
4692b07d3c4bSIlya Dryomov 	if (!list_empty(&lreq->pending_lworks)) {
4693b07d3c4bSIlya Dryomov 		struct linger_work *lwork =
4694b07d3c4bSIlya Dryomov 		    list_first_entry(&lreq->pending_lworks,
4695b07d3c4bSIlya Dryomov 				     struct linger_work,
4696b07d3c4bSIlya Dryomov 				     pending_item);
4697b07d3c4bSIlya Dryomov 
4698b07d3c4bSIlya Dryomov 		if (time_before(lwork->queued_stamp, stamp))
4699b07d3c4bSIlya Dryomov 			stamp = lwork->queued_stamp;
4700b07d3c4bSIlya Dryomov 	}
4701b07d3c4bSIlya Dryomov 	age = jiffies - stamp;
4702b07d3c4bSIlya Dryomov 	dout("%s lreq %p linger_id %llu age %lu last_error %d\n", __func__,
4703b07d3c4bSIlya Dryomov 	     lreq, lreq->linger_id, age, lreq->last_error);
4704b07d3c4bSIlya Dryomov 	/* we are truncating to msecs, so return a safe upper bound */
4705b07d3c4bSIlya Dryomov 	ret = lreq->last_error ?: 1 + jiffies_to_msecs(age);
4706b07d3c4bSIlya Dryomov 
4707b07d3c4bSIlya Dryomov 	mutex_unlock(&lreq->lock);
4708b07d3c4bSIlya Dryomov 	up_read(&osdc->lock);
4709b07d3c4bSIlya Dryomov 	return ret;
4710b07d3c4bSIlya Dryomov }
4711b07d3c4bSIlya Dryomov 
4712a4ed38d7SDouglas Fuller static int decode_watcher(void **p, void *end, struct ceph_watch_item *item)
4713a4ed38d7SDouglas Fuller {
4714a4ed38d7SDouglas Fuller 	u8 struct_v;
4715a4ed38d7SDouglas Fuller 	u32 struct_len;
4716a4ed38d7SDouglas Fuller 	int ret;
4717a4ed38d7SDouglas Fuller 
4718a4ed38d7SDouglas Fuller 	ret = ceph_start_decoding(p, end, 2, "watch_item_t",
4719a4ed38d7SDouglas Fuller 				  &struct_v, &struct_len);
4720a4ed38d7SDouglas Fuller 	if (ret)
4721a4ed38d7SDouglas Fuller 		return ret;
4722a4ed38d7SDouglas Fuller 
4723a4ed38d7SDouglas Fuller 	ceph_decode_copy(p, &item->name, sizeof(item->name));
4724a4ed38d7SDouglas Fuller 	item->cookie = ceph_decode_64(p);
4725a4ed38d7SDouglas Fuller 	*p += 4; /* skip timeout_seconds */
4726a4ed38d7SDouglas Fuller 	if (struct_v >= 2) {
4727a4ed38d7SDouglas Fuller 		ceph_decode_copy(p, &item->addr, sizeof(item->addr));
4728a4ed38d7SDouglas Fuller 		ceph_decode_addr(&item->addr);
4729a4ed38d7SDouglas Fuller 	}
4730a4ed38d7SDouglas Fuller 
4731a4ed38d7SDouglas Fuller 	dout("%s %s%llu cookie %llu addr %s\n", __func__,
4732a4ed38d7SDouglas Fuller 	     ENTITY_NAME(item->name), item->cookie,
4733a4ed38d7SDouglas Fuller 	     ceph_pr_addr(&item->addr.in_addr));
4734a4ed38d7SDouglas Fuller 	return 0;
4735a4ed38d7SDouglas Fuller }
4736a4ed38d7SDouglas Fuller 
4737a4ed38d7SDouglas Fuller static int decode_watchers(void **p, void *end,
4738a4ed38d7SDouglas Fuller 			   struct ceph_watch_item **watchers,
4739a4ed38d7SDouglas Fuller 			   u32 *num_watchers)
4740a4ed38d7SDouglas Fuller {
4741a4ed38d7SDouglas Fuller 	u8 struct_v;
4742a4ed38d7SDouglas Fuller 	u32 struct_len;
4743a4ed38d7SDouglas Fuller 	int i;
4744a4ed38d7SDouglas Fuller 	int ret;
4745a4ed38d7SDouglas Fuller 
4746a4ed38d7SDouglas Fuller 	ret = ceph_start_decoding(p, end, 1, "obj_list_watch_response_t",
4747a4ed38d7SDouglas Fuller 				  &struct_v, &struct_len);
4748a4ed38d7SDouglas Fuller 	if (ret)
4749a4ed38d7SDouglas Fuller 		return ret;
4750a4ed38d7SDouglas Fuller 
4751a4ed38d7SDouglas Fuller 	*num_watchers = ceph_decode_32(p);
4752a4ed38d7SDouglas Fuller 	*watchers = kcalloc(*num_watchers, sizeof(**watchers), GFP_NOIO);
4753a4ed38d7SDouglas Fuller 	if (!*watchers)
4754a4ed38d7SDouglas Fuller 		return -ENOMEM;
4755a4ed38d7SDouglas Fuller 
4756a4ed38d7SDouglas Fuller 	for (i = 0; i < *num_watchers; i++) {
4757a4ed38d7SDouglas Fuller 		ret = decode_watcher(p, end, *watchers + i);
4758a4ed38d7SDouglas Fuller 		if (ret) {
4759a4ed38d7SDouglas Fuller 			kfree(*watchers);
4760a4ed38d7SDouglas Fuller 			return ret;
4761a4ed38d7SDouglas Fuller 		}
4762a4ed38d7SDouglas Fuller 	}
4763a4ed38d7SDouglas Fuller 
4764a4ed38d7SDouglas Fuller 	return 0;
4765a4ed38d7SDouglas Fuller }
4766a4ed38d7SDouglas Fuller 
4767a4ed38d7SDouglas Fuller /*
4768a4ed38d7SDouglas Fuller  * On success, the caller is responsible for:
4769a4ed38d7SDouglas Fuller  *
4770a4ed38d7SDouglas Fuller  *     kfree(watchers);
4771a4ed38d7SDouglas Fuller  */
4772a4ed38d7SDouglas Fuller int ceph_osdc_list_watchers(struct ceph_osd_client *osdc,
4773a4ed38d7SDouglas Fuller 			    struct ceph_object_id *oid,
4774a4ed38d7SDouglas Fuller 			    struct ceph_object_locator *oloc,
4775a4ed38d7SDouglas Fuller 			    struct ceph_watch_item **watchers,
4776a4ed38d7SDouglas Fuller 			    u32 *num_watchers)
4777a4ed38d7SDouglas Fuller {
4778a4ed38d7SDouglas Fuller 	struct ceph_osd_request *req;
4779a4ed38d7SDouglas Fuller 	struct page **pages;
4780a4ed38d7SDouglas Fuller 	int ret;
4781a4ed38d7SDouglas Fuller 
4782a4ed38d7SDouglas Fuller 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4783a4ed38d7SDouglas Fuller 	if (!req)
4784a4ed38d7SDouglas Fuller 		return -ENOMEM;
4785a4ed38d7SDouglas Fuller 
4786a4ed38d7SDouglas Fuller 	ceph_oid_copy(&req->r_base_oid, oid);
4787a4ed38d7SDouglas Fuller 	ceph_oloc_copy(&req->r_base_oloc, oloc);
4788a4ed38d7SDouglas Fuller 	req->r_flags = CEPH_OSD_FLAG_READ;
4789a4ed38d7SDouglas Fuller 
4790a4ed38d7SDouglas Fuller 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4791a4ed38d7SDouglas Fuller 	if (ret)
4792a4ed38d7SDouglas Fuller 		goto out_put_req;
4793a4ed38d7SDouglas Fuller 
4794a4ed38d7SDouglas Fuller 	pages = ceph_alloc_page_vector(1, GFP_NOIO);
4795a4ed38d7SDouglas Fuller 	if (IS_ERR(pages)) {
4796a4ed38d7SDouglas Fuller 		ret = PTR_ERR(pages);
4797a4ed38d7SDouglas Fuller 		goto out_put_req;
4798a4ed38d7SDouglas Fuller 	}
4799a4ed38d7SDouglas Fuller 
4800a4ed38d7SDouglas Fuller 	osd_req_op_init(req, 0, CEPH_OSD_OP_LIST_WATCHERS, 0);
4801a4ed38d7SDouglas Fuller 	ceph_osd_data_pages_init(osd_req_op_data(req, 0, list_watchers,
4802a4ed38d7SDouglas Fuller 						 response_data),
4803a4ed38d7SDouglas Fuller 				 pages, PAGE_SIZE, 0, false, true);
4804a4ed38d7SDouglas Fuller 
4805a4ed38d7SDouglas Fuller 	ceph_osdc_start_request(osdc, req, false);
4806a4ed38d7SDouglas Fuller 	ret = ceph_osdc_wait_request(osdc, req);
4807a4ed38d7SDouglas Fuller 	if (ret >= 0) {
4808a4ed38d7SDouglas Fuller 		void *p = page_address(pages[0]);
4809a4ed38d7SDouglas Fuller 		void *const end = p + req->r_ops[0].outdata_len;
4810a4ed38d7SDouglas Fuller 
4811a4ed38d7SDouglas Fuller 		ret = decode_watchers(&p, end, watchers, num_watchers);
4812a4ed38d7SDouglas Fuller 	}
4813a4ed38d7SDouglas Fuller 
4814a4ed38d7SDouglas Fuller out_put_req:
4815a4ed38d7SDouglas Fuller 	ceph_osdc_put_request(req);
4816a4ed38d7SDouglas Fuller 	return ret;
4817a4ed38d7SDouglas Fuller }
4818a4ed38d7SDouglas Fuller EXPORT_SYMBOL(ceph_osdc_list_watchers);
4819a4ed38d7SDouglas Fuller 
4820b07d3c4bSIlya Dryomov /*
4821dd935f44SJosh Durgin  * Call all pending notify callbacks - for use after a watch is
4822dd935f44SJosh Durgin  * unregistered, to make sure no more callbacks for it will be invoked
4823dd935f44SJosh Durgin  */
4824f6479449Sstephen hemminger void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
4825dd935f44SJosh Durgin {
482699d16943SIlya Dryomov 	dout("%s osdc %p\n", __func__, osdc);
4827dd935f44SJosh Durgin 	flush_workqueue(osdc->notify_wq);
4828dd935f44SJosh Durgin }
4829dd935f44SJosh Durgin EXPORT_SYMBOL(ceph_osdc_flush_notifies);
4830dd935f44SJosh Durgin 
48317cca78c9SIlya Dryomov void ceph_osdc_maybe_request_map(struct ceph_osd_client *osdc)
48327cca78c9SIlya Dryomov {
48337cca78c9SIlya Dryomov 	down_read(&osdc->lock);
48347cca78c9SIlya Dryomov 	maybe_request_map(osdc);
48357cca78c9SIlya Dryomov 	up_read(&osdc->lock);
48367cca78c9SIlya Dryomov }
48377cca78c9SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_maybe_request_map);
4838dd935f44SJosh Durgin 
4839dd935f44SJosh Durgin /*
4840428a7158SDouglas Fuller  * Execute an OSD class method on an object.
4841428a7158SDouglas Fuller  *
4842428a7158SDouglas Fuller  * @flags: CEPH_OSD_FLAG_*
48432544a020SIlya Dryomov  * @resp_len: in/out param for reply length
4844428a7158SDouglas Fuller  */
4845428a7158SDouglas Fuller int ceph_osdc_call(struct ceph_osd_client *osdc,
4846428a7158SDouglas Fuller 		   struct ceph_object_id *oid,
4847428a7158SDouglas Fuller 		   struct ceph_object_locator *oloc,
4848428a7158SDouglas Fuller 		   const char *class, const char *method,
4849428a7158SDouglas Fuller 		   unsigned int flags,
4850428a7158SDouglas Fuller 		   struct page *req_page, size_t req_len,
4851428a7158SDouglas Fuller 		   struct page *resp_page, size_t *resp_len)
4852428a7158SDouglas Fuller {
4853428a7158SDouglas Fuller 	struct ceph_osd_request *req;
4854428a7158SDouglas Fuller 	int ret;
4855428a7158SDouglas Fuller 
48562544a020SIlya Dryomov 	if (req_len > PAGE_SIZE || (resp_page && *resp_len > PAGE_SIZE))
48572544a020SIlya Dryomov 		return -E2BIG;
48582544a020SIlya Dryomov 
4859428a7158SDouglas Fuller 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4860428a7158SDouglas Fuller 	if (!req)
4861428a7158SDouglas Fuller 		return -ENOMEM;
4862428a7158SDouglas Fuller 
4863428a7158SDouglas Fuller 	ceph_oid_copy(&req->r_base_oid, oid);
4864428a7158SDouglas Fuller 	ceph_oloc_copy(&req->r_base_oloc, oloc);
4865428a7158SDouglas Fuller 	req->r_flags = flags;
4866428a7158SDouglas Fuller 
4867428a7158SDouglas Fuller 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4868428a7158SDouglas Fuller 	if (ret)
4869428a7158SDouglas Fuller 		goto out_put_req;
4870428a7158SDouglas Fuller 
4871428a7158SDouglas Fuller 	osd_req_op_cls_init(req, 0, CEPH_OSD_OP_CALL, class, method);
4872428a7158SDouglas Fuller 	if (req_page)
4873428a7158SDouglas Fuller 		osd_req_op_cls_request_data_pages(req, 0, &req_page, req_len,
4874428a7158SDouglas Fuller 						  0, false, false);
4875428a7158SDouglas Fuller 	if (resp_page)
4876428a7158SDouglas Fuller 		osd_req_op_cls_response_data_pages(req, 0, &resp_page,
48772544a020SIlya Dryomov 						   *resp_len, 0, false, false);
4878428a7158SDouglas Fuller 
4879428a7158SDouglas Fuller 	ceph_osdc_start_request(osdc, req, false);
4880428a7158SDouglas Fuller 	ret = ceph_osdc_wait_request(osdc, req);
4881428a7158SDouglas Fuller 	if (ret >= 0) {
4882428a7158SDouglas Fuller 		ret = req->r_ops[0].rval;
4883428a7158SDouglas Fuller 		if (resp_page)
4884428a7158SDouglas Fuller 			*resp_len = req->r_ops[0].outdata_len;
4885428a7158SDouglas Fuller 	}
4886428a7158SDouglas Fuller 
4887428a7158SDouglas Fuller out_put_req:
4888428a7158SDouglas Fuller 	ceph_osdc_put_request(req);
4889428a7158SDouglas Fuller 	return ret;
4890428a7158SDouglas Fuller }
4891428a7158SDouglas Fuller EXPORT_SYMBOL(ceph_osdc_call);
4892428a7158SDouglas Fuller 
4893428a7158SDouglas Fuller /*
48943d14c5d2SYehuda Sadeh  * init, shutdown
48953d14c5d2SYehuda Sadeh  */
48963d14c5d2SYehuda Sadeh int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
48973d14c5d2SYehuda Sadeh {
48983d14c5d2SYehuda Sadeh 	int err;
48993d14c5d2SYehuda Sadeh 
49003d14c5d2SYehuda Sadeh 	dout("init\n");
49013d14c5d2SYehuda Sadeh 	osdc->client = client;
49025aea3dcdSIlya Dryomov 	init_rwsem(&osdc->lock);
49033d14c5d2SYehuda Sadeh 	osdc->osds = RB_ROOT;
49043d14c5d2SYehuda Sadeh 	INIT_LIST_HEAD(&osdc->osd_lru);
49059dd2845cSIlya Dryomov 	spin_lock_init(&osdc->osd_lru_lock);
49065aea3dcdSIlya Dryomov 	osd_init(&osdc->homeless_osd);
49075aea3dcdSIlya Dryomov 	osdc->homeless_osd.o_osdc = osdc;
49085aea3dcdSIlya Dryomov 	osdc->homeless_osd.o_osd = CEPH_HOMELESS_OSD;
4909264048afSIlya Dryomov 	osdc->last_linger_id = CEPH_LINGER_ID_START;
4910922dab61SIlya Dryomov 	osdc->linger_requests = RB_ROOT;
49114609245eSIlya Dryomov 	osdc->map_checks = RB_ROOT;
49124609245eSIlya Dryomov 	osdc->linger_map_checks = RB_ROOT;
49133d14c5d2SYehuda Sadeh 	INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
49143d14c5d2SYehuda Sadeh 	INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
49153d14c5d2SYehuda Sadeh 
49163d14c5d2SYehuda Sadeh 	err = -ENOMEM;
4917e5253a7bSIlya Dryomov 	osdc->osdmap = ceph_osdmap_alloc();
4918e5253a7bSIlya Dryomov 	if (!osdc->osdmap)
4919e5253a7bSIlya Dryomov 		goto out;
4920e5253a7bSIlya Dryomov 
49219e767adbSIlya Dryomov 	osdc->req_mempool = mempool_create_slab_pool(10,
49229e767adbSIlya Dryomov 						     ceph_osd_request_cache);
49233d14c5d2SYehuda Sadeh 	if (!osdc->req_mempool)
4924e5253a7bSIlya Dryomov 		goto out_map;
49253d14c5d2SYehuda Sadeh 
4926d50b409fSSage Weil 	err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
4927711da55dSIlya Dryomov 				PAGE_SIZE, 10, true, "osd_op");
49283d14c5d2SYehuda Sadeh 	if (err < 0)
49293d14c5d2SYehuda Sadeh 		goto out_mempool;
4930d50b409fSSage Weil 	err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
4931711da55dSIlya Dryomov 				PAGE_SIZE, 10, true, "osd_op_reply");
49323d14c5d2SYehuda Sadeh 	if (err < 0)
49333d14c5d2SYehuda Sadeh 		goto out_msgpool;
4934a40c4f10SYehuda Sadeh 
4935dbcae088SDan Carpenter 	err = -ENOMEM;
4936a40c4f10SYehuda Sadeh 	osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
4937dbcae088SDan Carpenter 	if (!osdc->notify_wq)
4938c172ec5cSIlya Dryomov 		goto out_msgpool_reply;
4939c172ec5cSIlya Dryomov 
4940fbca9635SIlya Dryomov 	schedule_delayed_work(&osdc->timeout_work,
4941fbca9635SIlya Dryomov 			      osdc->client->options->osd_keepalive_timeout);
4942b37ee1b9SIlya Dryomov 	schedule_delayed_work(&osdc->osds_timeout_work,
4943b37ee1b9SIlya Dryomov 	    round_jiffies_relative(osdc->client->options->osd_idle_ttl));
4944b37ee1b9SIlya Dryomov 
49453d14c5d2SYehuda Sadeh 	return 0;
49463d14c5d2SYehuda Sadeh 
4947c172ec5cSIlya Dryomov out_msgpool_reply:
4948c172ec5cSIlya Dryomov 	ceph_msgpool_destroy(&osdc->msgpool_op_reply);
49493d14c5d2SYehuda Sadeh out_msgpool:
49503d14c5d2SYehuda Sadeh 	ceph_msgpool_destroy(&osdc->msgpool_op);
49513d14c5d2SYehuda Sadeh out_mempool:
49523d14c5d2SYehuda Sadeh 	mempool_destroy(osdc->req_mempool);
4953e5253a7bSIlya Dryomov out_map:
4954e5253a7bSIlya Dryomov 	ceph_osdmap_destroy(osdc->osdmap);
49553d14c5d2SYehuda Sadeh out:
49563d14c5d2SYehuda Sadeh 	return err;
49573d14c5d2SYehuda Sadeh }
49583d14c5d2SYehuda Sadeh 
49593d14c5d2SYehuda Sadeh void ceph_osdc_stop(struct ceph_osd_client *osdc)
49603d14c5d2SYehuda Sadeh {
4961a40c4f10SYehuda Sadeh 	flush_workqueue(osdc->notify_wq);
4962a40c4f10SYehuda Sadeh 	destroy_workqueue(osdc->notify_wq);
49633d14c5d2SYehuda Sadeh 	cancel_delayed_work_sync(&osdc->timeout_work);
49643d14c5d2SYehuda Sadeh 	cancel_delayed_work_sync(&osdc->osds_timeout_work);
496542a2c09fSIlya Dryomov 
49665aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
496742a2c09fSIlya Dryomov 	while (!RB_EMPTY_ROOT(&osdc->osds)) {
496842a2c09fSIlya Dryomov 		struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
496942a2c09fSIlya Dryomov 						struct ceph_osd, o_node);
49705aea3dcdSIlya Dryomov 		close_osd(osd);
497142a2c09fSIlya Dryomov 	}
49725aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
497302113a0fSElena Reshetova 	WARN_ON(refcount_read(&osdc->homeless_osd.o_ref) != 1);
49745aea3dcdSIlya Dryomov 	osd_cleanup(&osdc->homeless_osd);
49755aea3dcdSIlya Dryomov 
49765aea3dcdSIlya Dryomov 	WARN_ON(!list_empty(&osdc->osd_lru));
4977922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_requests));
49784609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osdc->map_checks));
49794609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_map_checks));
49805aea3dcdSIlya Dryomov 	WARN_ON(atomic_read(&osdc->num_requests));
49815aea3dcdSIlya Dryomov 	WARN_ON(atomic_read(&osdc->num_homeless));
498242a2c09fSIlya Dryomov 
49833d14c5d2SYehuda Sadeh 	ceph_osdmap_destroy(osdc->osdmap);
49843d14c5d2SYehuda Sadeh 	mempool_destroy(osdc->req_mempool);
49853d14c5d2SYehuda Sadeh 	ceph_msgpool_destroy(&osdc->msgpool_op);
49863d14c5d2SYehuda Sadeh 	ceph_msgpool_destroy(&osdc->msgpool_op_reply);
49873d14c5d2SYehuda Sadeh }
49883d14c5d2SYehuda Sadeh 
49893d14c5d2SYehuda Sadeh /*
49903d14c5d2SYehuda Sadeh  * Read some contiguous pages.  If we cross a stripe boundary, shorten
49913d14c5d2SYehuda Sadeh  * *plen.  Return number of bytes read, or error.
49923d14c5d2SYehuda Sadeh  */
49933d14c5d2SYehuda Sadeh int ceph_osdc_readpages(struct ceph_osd_client *osdc,
49943d14c5d2SYehuda Sadeh 			struct ceph_vino vino, struct ceph_file_layout *layout,
49953d14c5d2SYehuda Sadeh 			u64 off, u64 *plen,
49963d14c5d2SYehuda Sadeh 			u32 truncate_seq, u64 truncate_size,
4997b7495fc2SSage Weil 			struct page **pages, int num_pages, int page_align)
49983d14c5d2SYehuda Sadeh {
49993d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
50003d14c5d2SYehuda Sadeh 	int rc = 0;
50013d14c5d2SYehuda Sadeh 
50023d14c5d2SYehuda Sadeh 	dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
50033d14c5d2SYehuda Sadeh 	     vino.snap, off, *plen);
5004715e4cd4SYan, Zheng 	req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
50053d14c5d2SYehuda Sadeh 				    CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
5006acead002SAlex Elder 				    NULL, truncate_seq, truncate_size,
5007153e5167SAlex Elder 				    false);
50086816282dSSage Weil 	if (IS_ERR(req))
50096816282dSSage Weil 		return PTR_ERR(req);
50103d14c5d2SYehuda Sadeh 
50113d14c5d2SYehuda Sadeh 	/* it may be a short read due to an object boundary */
5012406e2c9fSAlex Elder 	osd_req_op_extent_osd_data_pages(req, 0,
5013a4ce40a9SAlex Elder 				pages, *plen, page_align, false, false);
50143d14c5d2SYehuda Sadeh 
5015e0c59487SAlex Elder 	dout("readpages  final extent is %llu~%llu (%llu bytes align %d)\n",
501643bfe5deSAlex Elder 	     off, *plen, *plen, page_align);
50173d14c5d2SYehuda Sadeh 
50183d14c5d2SYehuda Sadeh 	rc = ceph_osdc_start_request(osdc, req, false);
50193d14c5d2SYehuda Sadeh 	if (!rc)
50203d14c5d2SYehuda Sadeh 		rc = ceph_osdc_wait_request(osdc, req);
50213d14c5d2SYehuda Sadeh 
50223d14c5d2SYehuda Sadeh 	ceph_osdc_put_request(req);
50233d14c5d2SYehuda Sadeh 	dout("readpages result %d\n", rc);
50243d14c5d2SYehuda Sadeh 	return rc;
50253d14c5d2SYehuda Sadeh }
50263d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_readpages);
50273d14c5d2SYehuda Sadeh 
50283d14c5d2SYehuda Sadeh /*
50293d14c5d2SYehuda Sadeh  * do a synchronous write on N pages
50303d14c5d2SYehuda Sadeh  */
50313d14c5d2SYehuda Sadeh int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
50323d14c5d2SYehuda Sadeh 			 struct ceph_file_layout *layout,
50333d14c5d2SYehuda Sadeh 			 struct ceph_snap_context *snapc,
50343d14c5d2SYehuda Sadeh 			 u64 off, u64 len,
50353d14c5d2SYehuda Sadeh 			 u32 truncate_seq, u64 truncate_size,
50363d14c5d2SYehuda Sadeh 			 struct timespec *mtime,
503724808826SAlex Elder 			 struct page **pages, int num_pages)
50383d14c5d2SYehuda Sadeh {
50393d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
50403d14c5d2SYehuda Sadeh 	int rc = 0;
5041b7495fc2SSage Weil 	int page_align = off & ~PAGE_MASK;
50423d14c5d2SYehuda Sadeh 
5043715e4cd4SYan, Zheng 	req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
504454ea0046SIlya Dryomov 				    CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
5045acead002SAlex Elder 				    snapc, truncate_seq, truncate_size,
5046153e5167SAlex Elder 				    true);
50476816282dSSage Weil 	if (IS_ERR(req))
50486816282dSSage Weil 		return PTR_ERR(req);
50493d14c5d2SYehuda Sadeh 
50503d14c5d2SYehuda Sadeh 	/* it may be a short write due to an object boundary */
5051406e2c9fSAlex Elder 	osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
505243bfe5deSAlex Elder 				false, false);
505343bfe5deSAlex Elder 	dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
50543d14c5d2SYehuda Sadeh 
5055bb873b53SIlya Dryomov 	req->r_mtime = *mtime;
505687f979d3SAlex Elder 	rc = ceph_osdc_start_request(osdc, req, true);
50573d14c5d2SYehuda Sadeh 	if (!rc)
50583d14c5d2SYehuda Sadeh 		rc = ceph_osdc_wait_request(osdc, req);
50593d14c5d2SYehuda Sadeh 
50603d14c5d2SYehuda Sadeh 	ceph_osdc_put_request(req);
50613d14c5d2SYehuda Sadeh 	if (rc == 0)
50623d14c5d2SYehuda Sadeh 		rc = len;
50633d14c5d2SYehuda Sadeh 	dout("writepages result %d\n", rc);
50643d14c5d2SYehuda Sadeh 	return rc;
50653d14c5d2SYehuda Sadeh }
50663d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_writepages);
50673d14c5d2SYehuda Sadeh 
50685522ae0bSAlex Elder int ceph_osdc_setup(void)
50695522ae0bSAlex Elder {
50703f1af42aSIlya Dryomov 	size_t size = sizeof(struct ceph_osd_request) +
50713f1af42aSIlya Dryomov 	    CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
50723f1af42aSIlya Dryomov 
50735522ae0bSAlex Elder 	BUG_ON(ceph_osd_request_cache);
50743f1af42aSIlya Dryomov 	ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
50753f1af42aSIlya Dryomov 						   0, 0, NULL);
50765522ae0bSAlex Elder 
50775522ae0bSAlex Elder 	return ceph_osd_request_cache ? 0 : -ENOMEM;
50785522ae0bSAlex Elder }
50795522ae0bSAlex Elder EXPORT_SYMBOL(ceph_osdc_setup);
50805522ae0bSAlex Elder 
50815522ae0bSAlex Elder void ceph_osdc_cleanup(void)
50825522ae0bSAlex Elder {
50835522ae0bSAlex Elder 	BUG_ON(!ceph_osd_request_cache);
50845522ae0bSAlex Elder 	kmem_cache_destroy(ceph_osd_request_cache);
50855522ae0bSAlex Elder 	ceph_osd_request_cache = NULL;
50865522ae0bSAlex Elder }
50875522ae0bSAlex Elder EXPORT_SYMBOL(ceph_osdc_cleanup);
50885522ae0bSAlex Elder 
50893d14c5d2SYehuda Sadeh /*
50903d14c5d2SYehuda Sadeh  * handle incoming message
50913d14c5d2SYehuda Sadeh  */
50923d14c5d2SYehuda Sadeh static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
50933d14c5d2SYehuda Sadeh {
50943d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
50955aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
50963d14c5d2SYehuda Sadeh 	int type = le16_to_cpu(msg->hdr.type);
50973d14c5d2SYehuda Sadeh 
50983d14c5d2SYehuda Sadeh 	switch (type) {
50993d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_MAP:
51003d14c5d2SYehuda Sadeh 		ceph_osdc_handle_map(osdc, msg);
51013d14c5d2SYehuda Sadeh 		break;
51023d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_OPREPLY:
51035aea3dcdSIlya Dryomov 		handle_reply(osd, msg);
51043d14c5d2SYehuda Sadeh 		break;
5105a02a946dSIlya Dryomov 	case CEPH_MSG_OSD_BACKOFF:
5106a02a946dSIlya Dryomov 		handle_backoff(osd, msg);
5107a02a946dSIlya Dryomov 		break;
5108a40c4f10SYehuda Sadeh 	case CEPH_MSG_WATCH_NOTIFY:
5109a40c4f10SYehuda Sadeh 		handle_watch_notify(osdc, msg);
5110a40c4f10SYehuda Sadeh 		break;
51113d14c5d2SYehuda Sadeh 
51123d14c5d2SYehuda Sadeh 	default:
51133d14c5d2SYehuda Sadeh 		pr_err("received unknown message type %d %s\n", type,
51143d14c5d2SYehuda Sadeh 		       ceph_msg_type_name(type));
51153d14c5d2SYehuda Sadeh 	}
51165aea3dcdSIlya Dryomov 
51173d14c5d2SYehuda Sadeh 	ceph_msg_put(msg);
51183d14c5d2SYehuda Sadeh }
51193d14c5d2SYehuda Sadeh 
51203d14c5d2SYehuda Sadeh /*
5121d15f9d69SIlya Dryomov  * Lookup and return message for incoming reply.  Don't try to do
5122d15f9d69SIlya Dryomov  * anything about a larger than preallocated data portion of the
5123d15f9d69SIlya Dryomov  * message at the moment - for now, just skip the message.
51243d14c5d2SYehuda Sadeh  */
51253d14c5d2SYehuda Sadeh static struct ceph_msg *get_reply(struct ceph_connection *con,
51263d14c5d2SYehuda Sadeh 				  struct ceph_msg_header *hdr,
51273d14c5d2SYehuda Sadeh 				  int *skip)
51283d14c5d2SYehuda Sadeh {
51293d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
51303d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = osd->o_osdc;
51315aea3dcdSIlya Dryomov 	struct ceph_msg *m = NULL;
51323d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
51333f0a4ac5SIlya Dryomov 	int front_len = le32_to_cpu(hdr->front_len);
51343d14c5d2SYehuda Sadeh 	int data_len = le32_to_cpu(hdr->data_len);
51355aea3dcdSIlya Dryomov 	u64 tid = le64_to_cpu(hdr->tid);
51363d14c5d2SYehuda Sadeh 
51375aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
51385aea3dcdSIlya Dryomov 	if (!osd_registered(osd)) {
51395aea3dcdSIlya Dryomov 		dout("%s osd%d unknown, skipping\n", __func__, osd->o_osd);
51405aea3dcdSIlya Dryomov 		*skip = 1;
51415aea3dcdSIlya Dryomov 		goto out_unlock_osdc;
51425aea3dcdSIlya Dryomov 	}
51435aea3dcdSIlya Dryomov 	WARN_ON(osd->o_osd != le64_to_cpu(hdr->src.num));
51445aea3dcdSIlya Dryomov 
51455aea3dcdSIlya Dryomov 	mutex_lock(&osd->lock);
51465aea3dcdSIlya Dryomov 	req = lookup_request(&osd->o_requests, tid);
51473d14c5d2SYehuda Sadeh 	if (!req) {
5148cd8140c6SIlya Dryomov 		dout("%s osd%d tid %llu unknown, skipping\n", __func__,
5149cd8140c6SIlya Dryomov 		     osd->o_osd, tid);
5150d15f9d69SIlya Dryomov 		*skip = 1;
51515aea3dcdSIlya Dryomov 		goto out_unlock_session;
51523d14c5d2SYehuda Sadeh 	}
51533d14c5d2SYehuda Sadeh 
51548921d114SAlex Elder 	ceph_msg_revoke_incoming(req->r_reply);
51553d14c5d2SYehuda Sadeh 
5156f2be82b0SIlya Dryomov 	if (front_len > req->r_reply->front_alloc_len) {
5157d15f9d69SIlya Dryomov 		pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
5158d15f9d69SIlya Dryomov 			__func__, osd->o_osd, req->r_tid, front_len,
5159d15f9d69SIlya Dryomov 			req->r_reply->front_alloc_len);
51603f0a4ac5SIlya Dryomov 		m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
51613f0a4ac5SIlya Dryomov 				 false);
51623d14c5d2SYehuda Sadeh 		if (!m)
51635aea3dcdSIlya Dryomov 			goto out_unlock_session;
51643d14c5d2SYehuda Sadeh 		ceph_msg_put(req->r_reply);
51653d14c5d2SYehuda Sadeh 		req->r_reply = m;
51663d14c5d2SYehuda Sadeh 	}
51673d14c5d2SYehuda Sadeh 
5168d15f9d69SIlya Dryomov 	if (data_len > req->r_reply->data_length) {
5169d15f9d69SIlya Dryomov 		pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
5170d15f9d69SIlya Dryomov 			__func__, osd->o_osd, req->r_tid, data_len,
5171d15f9d69SIlya Dryomov 			req->r_reply->data_length);
51723d14c5d2SYehuda Sadeh 		m = NULL;
5173d15f9d69SIlya Dryomov 		*skip = 1;
51745aea3dcdSIlya Dryomov 		goto out_unlock_session;
51753d14c5d2SYehuda Sadeh 	}
5176d15f9d69SIlya Dryomov 
5177d15f9d69SIlya Dryomov 	m = ceph_msg_get(req->r_reply);
51783d14c5d2SYehuda Sadeh 	dout("get_reply tid %lld %p\n", tid, m);
51793d14c5d2SYehuda Sadeh 
51805aea3dcdSIlya Dryomov out_unlock_session:
51815aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
51825aea3dcdSIlya Dryomov out_unlock_osdc:
51835aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
51843d14c5d2SYehuda Sadeh 	return m;
51853d14c5d2SYehuda Sadeh }
51863d14c5d2SYehuda Sadeh 
518719079203SIlya Dryomov /*
518819079203SIlya Dryomov  * TODO: switch to a msg-owned pagelist
518919079203SIlya Dryomov  */
519019079203SIlya Dryomov static struct ceph_msg *alloc_msg_with_page_vector(struct ceph_msg_header *hdr)
519119079203SIlya Dryomov {
519219079203SIlya Dryomov 	struct ceph_msg *m;
519319079203SIlya Dryomov 	int type = le16_to_cpu(hdr->type);
519419079203SIlya Dryomov 	u32 front_len = le32_to_cpu(hdr->front_len);
519519079203SIlya Dryomov 	u32 data_len = le32_to_cpu(hdr->data_len);
519619079203SIlya Dryomov 
519719079203SIlya Dryomov 	m = ceph_msg_new(type, front_len, GFP_NOIO, false);
519819079203SIlya Dryomov 	if (!m)
519919079203SIlya Dryomov 		return NULL;
520019079203SIlya Dryomov 
520119079203SIlya Dryomov 	if (data_len) {
520219079203SIlya Dryomov 		struct page **pages;
520319079203SIlya Dryomov 		struct ceph_osd_data osd_data;
520419079203SIlya Dryomov 
520519079203SIlya Dryomov 		pages = ceph_alloc_page_vector(calc_pages_for(0, data_len),
520619079203SIlya Dryomov 					       GFP_NOIO);
5207c22e853aSWei Yongjun 		if (IS_ERR(pages)) {
520819079203SIlya Dryomov 			ceph_msg_put(m);
520919079203SIlya Dryomov 			return NULL;
521019079203SIlya Dryomov 		}
521119079203SIlya Dryomov 
521219079203SIlya Dryomov 		ceph_osd_data_pages_init(&osd_data, pages, data_len, 0, false,
521319079203SIlya Dryomov 					 false);
521419079203SIlya Dryomov 		ceph_osdc_msg_data_add(m, &osd_data);
521519079203SIlya Dryomov 	}
521619079203SIlya Dryomov 
521719079203SIlya Dryomov 	return m;
521819079203SIlya Dryomov }
521919079203SIlya Dryomov 
52203d14c5d2SYehuda Sadeh static struct ceph_msg *alloc_msg(struct ceph_connection *con,
52213d14c5d2SYehuda Sadeh 				  struct ceph_msg_header *hdr,
52223d14c5d2SYehuda Sadeh 				  int *skip)
52233d14c5d2SYehuda Sadeh {
52243d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
52253d14c5d2SYehuda Sadeh 	int type = le16_to_cpu(hdr->type);
52263d14c5d2SYehuda Sadeh 
52271c20f2d2SAlex Elder 	*skip = 0;
52283d14c5d2SYehuda Sadeh 	switch (type) {
52293d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_MAP:
5230a02a946dSIlya Dryomov 	case CEPH_MSG_OSD_BACKOFF:
5231a40c4f10SYehuda Sadeh 	case CEPH_MSG_WATCH_NOTIFY:
523219079203SIlya Dryomov 		return alloc_msg_with_page_vector(hdr);
52333d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_OPREPLY:
52343d14c5d2SYehuda Sadeh 		return get_reply(con, hdr, skip);
52353d14c5d2SYehuda Sadeh 	default:
52365aea3dcdSIlya Dryomov 		pr_warn("%s osd%d unknown msg type %d, skipping\n", __func__,
52375aea3dcdSIlya Dryomov 			osd->o_osd, type);
52383d14c5d2SYehuda Sadeh 		*skip = 1;
52393d14c5d2SYehuda Sadeh 		return NULL;
52403d14c5d2SYehuda Sadeh 	}
52413d14c5d2SYehuda Sadeh }
52423d14c5d2SYehuda Sadeh 
52433d14c5d2SYehuda Sadeh /*
52443d14c5d2SYehuda Sadeh  * Wrappers to refcount containing ceph_osd struct
52453d14c5d2SYehuda Sadeh  */
52463d14c5d2SYehuda Sadeh static struct ceph_connection *get_osd_con(struct ceph_connection *con)
52473d14c5d2SYehuda Sadeh {
52483d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
52493d14c5d2SYehuda Sadeh 	if (get_osd(osd))
52503d14c5d2SYehuda Sadeh 		return con;
52513d14c5d2SYehuda Sadeh 	return NULL;
52523d14c5d2SYehuda Sadeh }
52533d14c5d2SYehuda Sadeh 
52543d14c5d2SYehuda Sadeh static void put_osd_con(struct ceph_connection *con)
52553d14c5d2SYehuda Sadeh {
52563d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
52573d14c5d2SYehuda Sadeh 	put_osd(osd);
52583d14c5d2SYehuda Sadeh }
52593d14c5d2SYehuda Sadeh 
52603d14c5d2SYehuda Sadeh /*
52613d14c5d2SYehuda Sadeh  * authentication
52623d14c5d2SYehuda Sadeh  */
5263a3530df3SAlex Elder /*
5264a3530df3SAlex Elder  * Note: returned pointer is the address of a structure that's
5265a3530df3SAlex Elder  * managed separately.  Caller must *not* attempt to free it.
5266a3530df3SAlex Elder  */
5267a3530df3SAlex Elder static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
52688f43fb53SAlex Elder 					int *proto, int force_new)
52693d14c5d2SYehuda Sadeh {
52703d14c5d2SYehuda Sadeh 	struct ceph_osd *o = con->private;
52713d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = o->o_osdc;
52723d14c5d2SYehuda Sadeh 	struct ceph_auth_client *ac = osdc->client->monc.auth;
527374f1869fSAlex Elder 	struct ceph_auth_handshake *auth = &o->o_auth;
52743d14c5d2SYehuda Sadeh 
527574f1869fSAlex Elder 	if (force_new && auth->authorizer) {
52766c1ea260SIlya Dryomov 		ceph_auth_destroy_authorizer(auth->authorizer);
527774f1869fSAlex Elder 		auth->authorizer = NULL;
52783d14c5d2SYehuda Sadeh 	}
527927859f97SSage Weil 	if (!auth->authorizer) {
528027859f97SSage Weil 		int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
5281a3530df3SAlex Elder 						      auth);
52823d14c5d2SYehuda Sadeh 		if (ret)
5283a3530df3SAlex Elder 			return ERR_PTR(ret);
528427859f97SSage Weil 	} else {
528527859f97SSage Weil 		int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
52860bed9b5cSSage Weil 						     auth);
52870bed9b5cSSage Weil 		if (ret)
52880bed9b5cSSage Weil 			return ERR_PTR(ret);
52893d14c5d2SYehuda Sadeh 	}
52903d14c5d2SYehuda Sadeh 	*proto = ac->protocol;
529174f1869fSAlex Elder 
5292a3530df3SAlex Elder 	return auth;
52933d14c5d2SYehuda Sadeh }
52943d14c5d2SYehuda Sadeh 
52953d14c5d2SYehuda Sadeh 
52960dde5848SIlya Dryomov static int verify_authorizer_reply(struct ceph_connection *con)
52973d14c5d2SYehuda Sadeh {
52983d14c5d2SYehuda Sadeh 	struct ceph_osd *o = con->private;
52993d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = o->o_osdc;
53003d14c5d2SYehuda Sadeh 	struct ceph_auth_client *ac = osdc->client->monc.auth;
53013d14c5d2SYehuda Sadeh 
53020dde5848SIlya Dryomov 	return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer);
53033d14c5d2SYehuda Sadeh }
53043d14c5d2SYehuda Sadeh 
53053d14c5d2SYehuda Sadeh static int invalidate_authorizer(struct ceph_connection *con)
53063d14c5d2SYehuda Sadeh {
53073d14c5d2SYehuda Sadeh 	struct ceph_osd *o = con->private;
53083d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = o->o_osdc;
53093d14c5d2SYehuda Sadeh 	struct ceph_auth_client *ac = osdc->client->monc.auth;
53103d14c5d2SYehuda Sadeh 
531127859f97SSage Weil 	ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
53123d14c5d2SYehuda Sadeh 	return ceph_monc_validate_auth(&osdc->client->monc);
53133d14c5d2SYehuda Sadeh }
53143d14c5d2SYehuda Sadeh 
53158cb441c0SIlya Dryomov static void osd_reencode_message(struct ceph_msg *msg)
53168cb441c0SIlya Dryomov {
5317914902afSIlya Dryomov 	int type = le16_to_cpu(msg->hdr.type);
5318914902afSIlya Dryomov 
5319914902afSIlya Dryomov 	if (type == CEPH_MSG_OSD_OP)
53208cb441c0SIlya Dryomov 		encode_request_finish(msg);
53218cb441c0SIlya Dryomov }
53228cb441c0SIlya Dryomov 
532379dbd1baSIlya Dryomov static int osd_sign_message(struct ceph_msg *msg)
532433d07337SYan, Zheng {
532579dbd1baSIlya Dryomov 	struct ceph_osd *o = msg->con->private;
532633d07337SYan, Zheng 	struct ceph_auth_handshake *auth = &o->o_auth;
532779dbd1baSIlya Dryomov 
532833d07337SYan, Zheng 	return ceph_auth_sign_message(auth, msg);
532933d07337SYan, Zheng }
533033d07337SYan, Zheng 
533179dbd1baSIlya Dryomov static int osd_check_message_signature(struct ceph_msg *msg)
533233d07337SYan, Zheng {
533379dbd1baSIlya Dryomov 	struct ceph_osd *o = msg->con->private;
533433d07337SYan, Zheng 	struct ceph_auth_handshake *auth = &o->o_auth;
533579dbd1baSIlya Dryomov 
533633d07337SYan, Zheng 	return ceph_auth_check_message_signature(auth, msg);
533733d07337SYan, Zheng }
533833d07337SYan, Zheng 
53393d14c5d2SYehuda Sadeh static const struct ceph_connection_operations osd_con_ops = {
53403d14c5d2SYehuda Sadeh 	.get = get_osd_con,
53413d14c5d2SYehuda Sadeh 	.put = put_osd_con,
53423d14c5d2SYehuda Sadeh 	.dispatch = dispatch,
53433d14c5d2SYehuda Sadeh 	.get_authorizer = get_authorizer,
53443d14c5d2SYehuda Sadeh 	.verify_authorizer_reply = verify_authorizer_reply,
53453d14c5d2SYehuda Sadeh 	.invalidate_authorizer = invalidate_authorizer,
53463d14c5d2SYehuda Sadeh 	.alloc_msg = alloc_msg,
53478cb441c0SIlya Dryomov 	.reencode_message = osd_reencode_message,
534879dbd1baSIlya Dryomov 	.sign_message = osd_sign_message,
534979dbd1baSIlya Dryomov 	.check_message_signature = osd_check_message_signature,
53505aea3dcdSIlya Dryomov 	.fault = osd_fault,
53513d14c5d2SYehuda Sadeh };
5352