xref: /openbmc/linux/net/ceph/osd_client.c (revision 88bc1922)
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>
2308c1ac50SIlya Dryomov #include <linux/ceph/striper.h>
243d14c5d2SYehuda Sadeh 
253d14c5d2SYehuda Sadeh #define OSD_OPREPLY_FRONT_LEN	512
263d14c5d2SYehuda Sadeh 
275522ae0bSAlex Elder static struct kmem_cache	*ceph_osd_request_cache;
285522ae0bSAlex Elder 
293d14c5d2SYehuda Sadeh static const struct ceph_connection_operations osd_con_ops;
303d14c5d2SYehuda Sadeh 
313d14c5d2SYehuda Sadeh /*
323d14c5d2SYehuda Sadeh  * Implement client access to distributed object storage cluster.
333d14c5d2SYehuda Sadeh  *
343d14c5d2SYehuda Sadeh  * All data objects are stored within a cluster/cloud of OSDs, or
353d14c5d2SYehuda Sadeh  * "object storage devices."  (Note that Ceph OSDs have _nothing_ to
363d14c5d2SYehuda Sadeh  * do with the T10 OSD extensions to SCSI.)  Ceph OSDs are simply
373d14c5d2SYehuda Sadeh  * remote daemons serving up and coordinating consistent and safe
383d14c5d2SYehuda Sadeh  * access to storage.
393d14c5d2SYehuda Sadeh  *
403d14c5d2SYehuda Sadeh  * Cluster membership and the mapping of data objects onto storage devices
413d14c5d2SYehuda Sadeh  * are described by the osd map.
423d14c5d2SYehuda Sadeh  *
433d14c5d2SYehuda Sadeh  * We keep track of pending OSD requests (read, write), resubmit
443d14c5d2SYehuda Sadeh  * requests to different OSDs when the cluster topology/data layout
453d14c5d2SYehuda Sadeh  * change, or retry the affected requests when the communications
463d14c5d2SYehuda Sadeh  * channel with an OSD is reset.
473d14c5d2SYehuda Sadeh  */
483d14c5d2SYehuda Sadeh 
495aea3dcdSIlya Dryomov static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req);
505aea3dcdSIlya Dryomov static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req);
51922dab61SIlya Dryomov static void link_linger(struct ceph_osd *osd,
52922dab61SIlya Dryomov 			struct ceph_osd_linger_request *lreq);
53922dab61SIlya Dryomov static void unlink_linger(struct ceph_osd *osd,
54922dab61SIlya Dryomov 			  struct ceph_osd_linger_request *lreq);
55a02a946dSIlya Dryomov static void clear_backoffs(struct ceph_osd *osd);
565aea3dcdSIlya Dryomov 
575aea3dcdSIlya Dryomov #if 1
585aea3dcdSIlya Dryomov static inline bool rwsem_is_wrlocked(struct rw_semaphore *sem)
595aea3dcdSIlya Dryomov {
605aea3dcdSIlya Dryomov 	bool wrlocked = true;
615aea3dcdSIlya Dryomov 
625aea3dcdSIlya Dryomov 	if (unlikely(down_read_trylock(sem))) {
635aea3dcdSIlya Dryomov 		wrlocked = false;
645aea3dcdSIlya Dryomov 		up_read(sem);
655aea3dcdSIlya Dryomov 	}
665aea3dcdSIlya Dryomov 
675aea3dcdSIlya Dryomov 	return wrlocked;
685aea3dcdSIlya Dryomov }
695aea3dcdSIlya Dryomov static inline void verify_osdc_locked(struct ceph_osd_client *osdc)
705aea3dcdSIlya Dryomov {
715aea3dcdSIlya Dryomov 	WARN_ON(!rwsem_is_locked(&osdc->lock));
725aea3dcdSIlya Dryomov }
735aea3dcdSIlya Dryomov static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc)
745aea3dcdSIlya Dryomov {
755aea3dcdSIlya Dryomov 	WARN_ON(!rwsem_is_wrlocked(&osdc->lock));
765aea3dcdSIlya Dryomov }
775aea3dcdSIlya Dryomov static inline void verify_osd_locked(struct ceph_osd *osd)
785aea3dcdSIlya Dryomov {
795aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
805aea3dcdSIlya Dryomov 
815aea3dcdSIlya Dryomov 	WARN_ON(!(mutex_is_locked(&osd->lock) &&
825aea3dcdSIlya Dryomov 		  rwsem_is_locked(&osdc->lock)) &&
835aea3dcdSIlya Dryomov 		!rwsem_is_wrlocked(&osdc->lock));
845aea3dcdSIlya Dryomov }
85922dab61SIlya Dryomov static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq)
86922dab61SIlya Dryomov {
87922dab61SIlya Dryomov 	WARN_ON(!mutex_is_locked(&lreq->lock));
88922dab61SIlya Dryomov }
895aea3dcdSIlya Dryomov #else
905aea3dcdSIlya Dryomov static inline void verify_osdc_locked(struct ceph_osd_client *osdc) { }
915aea3dcdSIlya Dryomov static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc) { }
925aea3dcdSIlya Dryomov static inline void verify_osd_locked(struct ceph_osd *osd) { }
93922dab61SIlya Dryomov static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq) { }
945aea3dcdSIlya Dryomov #endif
955aea3dcdSIlya Dryomov 
963d14c5d2SYehuda Sadeh /*
973d14c5d2SYehuda Sadeh  * calculate the mapping of a file extent onto an object, and fill out the
983d14c5d2SYehuda Sadeh  * request accordingly.  shorten extent as necessary if it crosses an
993d14c5d2SYehuda Sadeh  * object boundary.
1003d14c5d2SYehuda Sadeh  *
1013d14c5d2SYehuda Sadeh  * fill osd op in request message.
1023d14c5d2SYehuda Sadeh  */
103dbe0fc41SAlex Elder static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
104a19dadfbSAlex Elder 			u64 *objnum, u64 *objoff, u64 *objlen)
1053d14c5d2SYehuda Sadeh {
10660e56f13SAlex Elder 	u64 orig_len = *plen;
107dccbf080SIlya Dryomov 	u32 xlen;
1083d14c5d2SYehuda Sadeh 
10960e56f13SAlex Elder 	/* object extent? */
110dccbf080SIlya Dryomov 	ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
111dccbf080SIlya Dryomov 					  objoff, &xlen);
112dccbf080SIlya Dryomov 	*objlen = xlen;
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);
1203ff5f385SAlex Elder 	return 0;
1213d14c5d2SYehuda Sadeh }
1223d14c5d2SYehuda Sadeh 
123c54d47bfSAlex Elder static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
124c54d47bfSAlex Elder {
125c54d47bfSAlex Elder 	memset(osd_data, 0, sizeof (*osd_data));
126c54d47bfSAlex Elder 	osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
127c54d47bfSAlex Elder }
128c54d47bfSAlex Elder 
129a4ce40a9SAlex Elder static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
13043bfe5deSAlex Elder 			struct page **pages, u64 length, u32 alignment,
13143bfe5deSAlex Elder 			bool pages_from_pool, bool own_pages)
13243bfe5deSAlex Elder {
13343bfe5deSAlex Elder 	osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
13443bfe5deSAlex Elder 	osd_data->pages = pages;
13543bfe5deSAlex Elder 	osd_data->length = length;
13643bfe5deSAlex Elder 	osd_data->alignment = alignment;
13743bfe5deSAlex Elder 	osd_data->pages_from_pool = pages_from_pool;
13843bfe5deSAlex Elder 	osd_data->own_pages = own_pages;
13943bfe5deSAlex Elder }
14043bfe5deSAlex Elder 
141a4ce40a9SAlex Elder static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
14243bfe5deSAlex Elder 			struct ceph_pagelist *pagelist)
14343bfe5deSAlex Elder {
14443bfe5deSAlex Elder 	osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
14543bfe5deSAlex Elder 	osd_data->pagelist = pagelist;
14643bfe5deSAlex Elder }
14743bfe5deSAlex Elder 
14843bfe5deSAlex Elder #ifdef CONFIG_BLOCK
149a4ce40a9SAlex Elder static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
1505359a17dSIlya Dryomov 				   struct ceph_bio_iter *bio_pos,
1515359a17dSIlya Dryomov 				   u32 bio_length)
15243bfe5deSAlex Elder {
15343bfe5deSAlex Elder 	osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
1545359a17dSIlya Dryomov 	osd_data->bio_pos = *bio_pos;
15543bfe5deSAlex Elder 	osd_data->bio_length = bio_length;
15643bfe5deSAlex Elder }
15743bfe5deSAlex Elder #endif /* CONFIG_BLOCK */
15843bfe5deSAlex Elder 
159b9e281c2SIlya Dryomov static void ceph_osd_data_bvecs_init(struct ceph_osd_data *osd_data,
1600010f705SIlya Dryomov 				     struct ceph_bvec_iter *bvec_pos,
1610010f705SIlya Dryomov 				     u32 num_bvecs)
162b9e281c2SIlya Dryomov {
163b9e281c2SIlya Dryomov 	osd_data->type = CEPH_OSD_DATA_TYPE_BVECS;
164b9e281c2SIlya Dryomov 	osd_data->bvec_pos = *bvec_pos;
1650010f705SIlya Dryomov 	osd_data->num_bvecs = num_bvecs;
166b9e281c2SIlya Dryomov }
167b9e281c2SIlya Dryomov 
168863c7eb5SAlex Elder #define osd_req_op_data(oreq, whch, typ, fld)				\
169863c7eb5SAlex Elder ({									\
1708a703a38SIoana Ciornei 	struct ceph_osd_request *__oreq = (oreq);			\
1718a703a38SIoana Ciornei 	unsigned int __whch = (whch);					\
1728a703a38SIoana Ciornei 	BUG_ON(__whch >= __oreq->r_num_ops);				\
1738a703a38SIoana Ciornei 	&__oreq->r_ops[__whch].typ.fld;					\
174863c7eb5SAlex Elder })
175863c7eb5SAlex Elder 
17649719778SAlex Elder static struct ceph_osd_data *
17749719778SAlex Elder osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
17849719778SAlex Elder {
17949719778SAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
18049719778SAlex Elder 
18149719778SAlex Elder 	return &osd_req->r_ops[which].raw_data_in;
18249719778SAlex Elder }
18349719778SAlex Elder 
184a4ce40a9SAlex Elder struct ceph_osd_data *
185a4ce40a9SAlex Elder osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
186406e2c9fSAlex Elder 			unsigned int which)
187a4ce40a9SAlex Elder {
188863c7eb5SAlex Elder 	return osd_req_op_data(osd_req, which, extent, osd_data);
189a4ce40a9SAlex Elder }
190a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_osd_data);
191a4ce40a9SAlex Elder 
19249719778SAlex Elder void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
19349719778SAlex Elder 			unsigned int which, struct page **pages,
19449719778SAlex Elder 			u64 length, u32 alignment,
19549719778SAlex Elder 			bool pages_from_pool, bool own_pages)
19649719778SAlex Elder {
19749719778SAlex Elder 	struct ceph_osd_data *osd_data;
19849719778SAlex Elder 
19949719778SAlex Elder 	osd_data = osd_req_op_raw_data_in(osd_req, which);
20049719778SAlex Elder 	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
20149719778SAlex Elder 				pages_from_pool, own_pages);
20249719778SAlex Elder }
20349719778SAlex Elder EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
20449719778SAlex Elder 
205a4ce40a9SAlex Elder void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
206406e2c9fSAlex Elder 			unsigned int which, struct page **pages,
207406e2c9fSAlex Elder 			u64 length, u32 alignment,
208a4ce40a9SAlex Elder 			bool pages_from_pool, bool own_pages)
209a4ce40a9SAlex Elder {
210a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
211a4ce40a9SAlex Elder 
212863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
213a4ce40a9SAlex Elder 	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
214a4ce40a9SAlex Elder 				pages_from_pool, own_pages);
215a4ce40a9SAlex Elder }
216a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
217a4ce40a9SAlex Elder 
218a4ce40a9SAlex Elder void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
219406e2c9fSAlex Elder 			unsigned int which, struct ceph_pagelist *pagelist)
220a4ce40a9SAlex Elder {
221a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
222a4ce40a9SAlex Elder 
223863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
224a4ce40a9SAlex Elder 	ceph_osd_data_pagelist_init(osd_data, pagelist);
225a4ce40a9SAlex Elder }
226a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
227a4ce40a9SAlex Elder 
228a4ce40a9SAlex Elder #ifdef CONFIG_BLOCK
229a4ce40a9SAlex Elder void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
2305359a17dSIlya Dryomov 				    unsigned int which,
2315359a17dSIlya Dryomov 				    struct ceph_bio_iter *bio_pos,
2325359a17dSIlya Dryomov 				    u32 bio_length)
233a4ce40a9SAlex Elder {
234a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
235863c7eb5SAlex Elder 
236863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
2375359a17dSIlya Dryomov 	ceph_osd_data_bio_init(osd_data, bio_pos, bio_length);
238a4ce40a9SAlex Elder }
239a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
240a4ce40a9SAlex Elder #endif /* CONFIG_BLOCK */
241a4ce40a9SAlex Elder 
2420010f705SIlya Dryomov void osd_req_op_extent_osd_data_bvecs(struct ceph_osd_request *osd_req,
2430010f705SIlya Dryomov 				      unsigned int which,
2440010f705SIlya Dryomov 				      struct bio_vec *bvecs, u32 num_bvecs,
2450010f705SIlya Dryomov 				      u32 bytes)
2460010f705SIlya Dryomov {
2470010f705SIlya Dryomov 	struct ceph_osd_data *osd_data;
2480010f705SIlya Dryomov 	struct ceph_bvec_iter it = {
2490010f705SIlya Dryomov 		.bvecs = bvecs,
2500010f705SIlya Dryomov 		.iter = { .bi_size = bytes },
2510010f705SIlya Dryomov 	};
2520010f705SIlya Dryomov 
2530010f705SIlya Dryomov 	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
2540010f705SIlya Dryomov 	ceph_osd_data_bvecs_init(osd_data, &it, num_bvecs);
2550010f705SIlya Dryomov }
2560010f705SIlya Dryomov EXPORT_SYMBOL(osd_req_op_extent_osd_data_bvecs);
2570010f705SIlya Dryomov 
258b9e281c2SIlya Dryomov void osd_req_op_extent_osd_data_bvec_pos(struct ceph_osd_request *osd_req,
259b9e281c2SIlya Dryomov 					 unsigned int which,
260b9e281c2SIlya Dryomov 					 struct ceph_bvec_iter *bvec_pos)
261b9e281c2SIlya Dryomov {
262b9e281c2SIlya Dryomov 	struct ceph_osd_data *osd_data;
263b9e281c2SIlya Dryomov 
264b9e281c2SIlya Dryomov 	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
2650010f705SIlya Dryomov 	ceph_osd_data_bvecs_init(osd_data, bvec_pos, 0);
266b9e281c2SIlya Dryomov }
267b9e281c2SIlya Dryomov EXPORT_SYMBOL(osd_req_op_extent_osd_data_bvec_pos);
268b9e281c2SIlya Dryomov 
269a4ce40a9SAlex Elder static void osd_req_op_cls_request_info_pagelist(
270a4ce40a9SAlex Elder 			struct ceph_osd_request *osd_req,
271a4ce40a9SAlex Elder 			unsigned int which, struct ceph_pagelist *pagelist)
272a4ce40a9SAlex Elder {
273a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
274a4ce40a9SAlex Elder 
275863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, request_info);
276a4ce40a9SAlex Elder 	ceph_osd_data_pagelist_init(osd_data, pagelist);
277a4ce40a9SAlex Elder }
278a4ce40a9SAlex Elder 
27904017e29SAlex Elder void osd_req_op_cls_request_data_pagelist(
28004017e29SAlex Elder 			struct ceph_osd_request *osd_req,
28104017e29SAlex Elder 			unsigned int which, struct ceph_pagelist *pagelist)
28204017e29SAlex Elder {
28304017e29SAlex Elder 	struct ceph_osd_data *osd_data;
28404017e29SAlex Elder 
285863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, request_data);
28604017e29SAlex Elder 	ceph_osd_data_pagelist_init(osd_data, pagelist);
287bb873b53SIlya Dryomov 	osd_req->r_ops[which].cls.indata_len += pagelist->length;
288bb873b53SIlya Dryomov 	osd_req->r_ops[which].indata_len += pagelist->length;
28904017e29SAlex Elder }
29004017e29SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
29104017e29SAlex Elder 
2926c57b554SAlex Elder void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
2936c57b554SAlex Elder 			unsigned int which, struct page **pages, u64 length,
2946c57b554SAlex Elder 			u32 alignment, bool pages_from_pool, bool own_pages)
2956c57b554SAlex Elder {
2966c57b554SAlex Elder 	struct ceph_osd_data *osd_data;
2976c57b554SAlex Elder 
2986c57b554SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, request_data);
2996c57b554SAlex Elder 	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
3006c57b554SAlex Elder 				pages_from_pool, own_pages);
301bb873b53SIlya Dryomov 	osd_req->r_ops[which].cls.indata_len += length;
302bb873b53SIlya Dryomov 	osd_req->r_ops[which].indata_len += length;
3036c57b554SAlex Elder }
3046c57b554SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
3056c57b554SAlex Elder 
306b9e281c2SIlya Dryomov void osd_req_op_cls_request_data_bvecs(struct ceph_osd_request *osd_req,
307b9e281c2SIlya Dryomov 				       unsigned int which,
3080010f705SIlya Dryomov 				       struct bio_vec *bvecs, u32 num_bvecs,
3090010f705SIlya Dryomov 				       u32 bytes)
310b9e281c2SIlya Dryomov {
311b9e281c2SIlya Dryomov 	struct ceph_osd_data *osd_data;
312b9e281c2SIlya Dryomov 	struct ceph_bvec_iter it = {
313b9e281c2SIlya Dryomov 		.bvecs = bvecs,
314b9e281c2SIlya Dryomov 		.iter = { .bi_size = bytes },
315b9e281c2SIlya Dryomov 	};
316b9e281c2SIlya Dryomov 
317b9e281c2SIlya Dryomov 	osd_data = osd_req_op_data(osd_req, which, cls, request_data);
3180010f705SIlya Dryomov 	ceph_osd_data_bvecs_init(osd_data, &it, num_bvecs);
319b9e281c2SIlya Dryomov 	osd_req->r_ops[which].cls.indata_len += bytes;
320b9e281c2SIlya Dryomov 	osd_req->r_ops[which].indata_len += bytes;
321b9e281c2SIlya Dryomov }
322b9e281c2SIlya Dryomov EXPORT_SYMBOL(osd_req_op_cls_request_data_bvecs);
323b9e281c2SIlya Dryomov 
324a4ce40a9SAlex Elder void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
325a4ce40a9SAlex Elder 			unsigned int which, struct page **pages, u64 length,
326a4ce40a9SAlex Elder 			u32 alignment, bool pages_from_pool, bool own_pages)
327a4ce40a9SAlex Elder {
328a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
329a4ce40a9SAlex Elder 
330863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, response_data);
331a4ce40a9SAlex Elder 	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
332a4ce40a9SAlex Elder 				pages_from_pool, own_pages);
333a4ce40a9SAlex Elder }
334a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
335a4ce40a9SAlex Elder 
33623c08a9cSAlex Elder static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
33723c08a9cSAlex Elder {
33823c08a9cSAlex Elder 	switch (osd_data->type) {
33923c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_NONE:
34023c08a9cSAlex Elder 		return 0;
34123c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_PAGES:
34223c08a9cSAlex Elder 		return osd_data->length;
34323c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_PAGELIST:
34423c08a9cSAlex Elder 		return (u64)osd_data->pagelist->length;
34523c08a9cSAlex Elder #ifdef CONFIG_BLOCK
34623c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_BIO:
34723c08a9cSAlex Elder 		return (u64)osd_data->bio_length;
34823c08a9cSAlex Elder #endif /* CONFIG_BLOCK */
349b9e281c2SIlya Dryomov 	case CEPH_OSD_DATA_TYPE_BVECS:
350b9e281c2SIlya Dryomov 		return osd_data->bvec_pos.iter.bi_size;
35123c08a9cSAlex Elder 	default:
35223c08a9cSAlex Elder 		WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
35323c08a9cSAlex Elder 		return 0;
35423c08a9cSAlex Elder 	}
35523c08a9cSAlex Elder }
35623c08a9cSAlex Elder 
357c54d47bfSAlex Elder static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
358c54d47bfSAlex Elder {
3595476492fSAlex Elder 	if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
360c54d47bfSAlex Elder 		int num_pages;
361c54d47bfSAlex Elder 
362c54d47bfSAlex Elder 		num_pages = calc_pages_for((u64)osd_data->alignment,
363c54d47bfSAlex Elder 						(u64)osd_data->length);
364c54d47bfSAlex Elder 		ceph_release_page_vector(osd_data->pages, num_pages);
365c54d47bfSAlex Elder 	}
3665476492fSAlex Elder 	ceph_osd_data_init(osd_data);
3675476492fSAlex Elder }
3685476492fSAlex Elder 
3695476492fSAlex Elder static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
3705476492fSAlex Elder 			unsigned int which)
3715476492fSAlex Elder {
3725476492fSAlex Elder 	struct ceph_osd_req_op *op;
3735476492fSAlex Elder 
3745476492fSAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
3755476492fSAlex Elder 	op = &osd_req->r_ops[which];
3765476492fSAlex Elder 
3775476492fSAlex Elder 	switch (op->op) {
3785476492fSAlex Elder 	case CEPH_OSD_OP_READ:
3795476492fSAlex Elder 	case CEPH_OSD_OP_WRITE:
380e30b7577SIlya Dryomov 	case CEPH_OSD_OP_WRITEFULL:
3815476492fSAlex Elder 		ceph_osd_data_release(&op->extent.osd_data);
3825476492fSAlex Elder 		break;
3835476492fSAlex Elder 	case CEPH_OSD_OP_CALL:
3845476492fSAlex Elder 		ceph_osd_data_release(&op->cls.request_info);
38504017e29SAlex Elder 		ceph_osd_data_release(&op->cls.request_data);
3865476492fSAlex Elder 		ceph_osd_data_release(&op->cls.response_data);
3875476492fSAlex Elder 		break;
388d74b50beSYan, Zheng 	case CEPH_OSD_OP_SETXATTR:
389d74b50beSYan, Zheng 	case CEPH_OSD_OP_CMPXATTR:
390d74b50beSYan, Zheng 		ceph_osd_data_release(&op->xattr.osd_data);
391d74b50beSYan, Zheng 		break;
39266ba609fSYan, Zheng 	case CEPH_OSD_OP_STAT:
39366ba609fSYan, Zheng 		ceph_osd_data_release(&op->raw_data_in);
39466ba609fSYan, Zheng 		break;
395922dab61SIlya Dryomov 	case CEPH_OSD_OP_NOTIFY_ACK:
396922dab61SIlya Dryomov 		ceph_osd_data_release(&op->notify_ack.request_data);
397922dab61SIlya Dryomov 		break;
39819079203SIlya Dryomov 	case CEPH_OSD_OP_NOTIFY:
39919079203SIlya Dryomov 		ceph_osd_data_release(&op->notify.request_data);
40019079203SIlya Dryomov 		ceph_osd_data_release(&op->notify.response_data);
40119079203SIlya Dryomov 		break;
402a4ed38d7SDouglas Fuller 	case CEPH_OSD_OP_LIST_WATCHERS:
403a4ed38d7SDouglas Fuller 		ceph_osd_data_release(&op->list_watchers.response_data);
404a4ed38d7SDouglas Fuller 		break;
4055476492fSAlex Elder 	default:
4065476492fSAlex Elder 		break;
4075476492fSAlex Elder 	}
408c54d47bfSAlex Elder }
409c54d47bfSAlex Elder 
4103d14c5d2SYehuda Sadeh /*
41163244fa1SIlya Dryomov  * Assumes @t is zero-initialized.
41263244fa1SIlya Dryomov  */
41363244fa1SIlya Dryomov static void target_init(struct ceph_osd_request_target *t)
41463244fa1SIlya Dryomov {
41563244fa1SIlya Dryomov 	ceph_oid_init(&t->base_oid);
41663244fa1SIlya Dryomov 	ceph_oloc_init(&t->base_oloc);
41763244fa1SIlya Dryomov 	ceph_oid_init(&t->target_oid);
41863244fa1SIlya Dryomov 	ceph_oloc_init(&t->target_oloc);
41963244fa1SIlya Dryomov 
42063244fa1SIlya Dryomov 	ceph_osds_init(&t->acting);
42163244fa1SIlya Dryomov 	ceph_osds_init(&t->up);
42263244fa1SIlya Dryomov 	t->size = -1;
42363244fa1SIlya Dryomov 	t->min_size = -1;
42463244fa1SIlya Dryomov 
42563244fa1SIlya Dryomov 	t->osd = CEPH_HOMELESS_OSD;
42663244fa1SIlya Dryomov }
42763244fa1SIlya Dryomov 
428922dab61SIlya Dryomov static void target_copy(struct ceph_osd_request_target *dest,
429922dab61SIlya Dryomov 			const struct ceph_osd_request_target *src)
430922dab61SIlya Dryomov {
431922dab61SIlya Dryomov 	ceph_oid_copy(&dest->base_oid, &src->base_oid);
432922dab61SIlya Dryomov 	ceph_oloc_copy(&dest->base_oloc, &src->base_oloc);
433922dab61SIlya Dryomov 	ceph_oid_copy(&dest->target_oid, &src->target_oid);
434922dab61SIlya Dryomov 	ceph_oloc_copy(&dest->target_oloc, &src->target_oloc);
435922dab61SIlya Dryomov 
436922dab61SIlya Dryomov 	dest->pgid = src->pgid; /* struct */
437dc98ff72SIlya Dryomov 	dest->spgid = src->spgid; /* struct */
438922dab61SIlya Dryomov 	dest->pg_num = src->pg_num;
439922dab61SIlya Dryomov 	dest->pg_num_mask = src->pg_num_mask;
440922dab61SIlya Dryomov 	ceph_osds_copy(&dest->acting, &src->acting);
441922dab61SIlya Dryomov 	ceph_osds_copy(&dest->up, &src->up);
442922dab61SIlya Dryomov 	dest->size = src->size;
443922dab61SIlya Dryomov 	dest->min_size = src->min_size;
444922dab61SIlya Dryomov 	dest->sort_bitwise = src->sort_bitwise;
445922dab61SIlya Dryomov 
446922dab61SIlya Dryomov 	dest->flags = src->flags;
447922dab61SIlya Dryomov 	dest->paused = src->paused;
448922dab61SIlya Dryomov 
44904c7d789SIlya Dryomov 	dest->epoch = src->epoch;
450dc93e0e2SIlya Dryomov 	dest->last_force_resend = src->last_force_resend;
451dc93e0e2SIlya Dryomov 
452922dab61SIlya Dryomov 	dest->osd = src->osd;
453922dab61SIlya Dryomov }
454922dab61SIlya Dryomov 
45563244fa1SIlya Dryomov static void target_destroy(struct ceph_osd_request_target *t)
45663244fa1SIlya Dryomov {
45763244fa1SIlya Dryomov 	ceph_oid_destroy(&t->base_oid);
45830c156d9SYan, Zheng 	ceph_oloc_destroy(&t->base_oloc);
45963244fa1SIlya Dryomov 	ceph_oid_destroy(&t->target_oid);
46030c156d9SYan, Zheng 	ceph_oloc_destroy(&t->target_oloc);
46163244fa1SIlya Dryomov }
46263244fa1SIlya Dryomov 
46363244fa1SIlya Dryomov /*
4643d14c5d2SYehuda Sadeh  * requests
4653d14c5d2SYehuda Sadeh  */
4663540bfdbSIlya Dryomov static void request_release_checks(struct ceph_osd_request *req)
4673540bfdbSIlya Dryomov {
4683540bfdbSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&req->r_node));
4694609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&req->r_mc_node));
4703540bfdbSIlya Dryomov 	WARN_ON(!list_empty(&req->r_unsafe_item));
4713540bfdbSIlya Dryomov 	WARN_ON(req->r_osd);
4723540bfdbSIlya Dryomov }
4733540bfdbSIlya Dryomov 
4749e94af20SIlya Dryomov static void ceph_osdc_release_request(struct kref *kref)
4753d14c5d2SYehuda Sadeh {
4769e94af20SIlya Dryomov 	struct ceph_osd_request *req = container_of(kref,
4779e94af20SIlya Dryomov 					    struct ceph_osd_request, r_kref);
4785476492fSAlex Elder 	unsigned int which;
4793d14c5d2SYehuda Sadeh 
4809e94af20SIlya Dryomov 	dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
4819e94af20SIlya Dryomov 	     req->r_request, req->r_reply);
4823540bfdbSIlya Dryomov 	request_release_checks(req);
4839e94af20SIlya Dryomov 
4843d14c5d2SYehuda Sadeh 	if (req->r_request)
4853d14c5d2SYehuda Sadeh 		ceph_msg_put(req->r_request);
4865aea3dcdSIlya Dryomov 	if (req->r_reply)
487ab8cb34aSAlex Elder 		ceph_msg_put(req->r_reply);
4880fff87ecSAlex Elder 
4895476492fSAlex Elder 	for (which = 0; which < req->r_num_ops; which++)
4905476492fSAlex Elder 		osd_req_op_data_release(req, which);
4910fff87ecSAlex Elder 
492a66dd383SIlya Dryomov 	target_destroy(&req->r_t);
4933d14c5d2SYehuda Sadeh 	ceph_put_snap_context(req->r_snapc);
494d30291b9SIlya Dryomov 
4953d14c5d2SYehuda Sadeh 	if (req->r_mempool)
4963d14c5d2SYehuda Sadeh 		mempool_free(req, req->r_osdc->req_mempool);
4973f1af42aSIlya Dryomov 	else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS)
4985522ae0bSAlex Elder 		kmem_cache_free(ceph_osd_request_cache, req);
4993f1af42aSIlya Dryomov 	else
5003f1af42aSIlya Dryomov 		kfree(req);
5013d14c5d2SYehuda Sadeh }
5029e94af20SIlya Dryomov 
5039e94af20SIlya Dryomov void ceph_osdc_get_request(struct ceph_osd_request *req)
5049e94af20SIlya Dryomov {
5059e94af20SIlya Dryomov 	dout("%s %p (was %d)\n", __func__, req,
5062c935bc5SPeter Zijlstra 	     kref_read(&req->r_kref));
5079e94af20SIlya Dryomov 	kref_get(&req->r_kref);
5089e94af20SIlya Dryomov }
5099e94af20SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_get_request);
5109e94af20SIlya Dryomov 
5119e94af20SIlya Dryomov void ceph_osdc_put_request(struct ceph_osd_request *req)
5129e94af20SIlya Dryomov {
5133ed97d63SIlya Dryomov 	if (req) {
5149e94af20SIlya Dryomov 		dout("%s %p (was %d)\n", __func__, req,
5152c935bc5SPeter Zijlstra 		     kref_read(&req->r_kref));
5169e94af20SIlya Dryomov 		kref_put(&req->r_kref, ceph_osdc_release_request);
5179e94af20SIlya Dryomov 	}
5183ed97d63SIlya Dryomov }
5199e94af20SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_put_request);
5203d14c5d2SYehuda Sadeh 
5213540bfdbSIlya Dryomov static void request_init(struct ceph_osd_request *req)
5223540bfdbSIlya Dryomov {
5233540bfdbSIlya Dryomov 	/* req only, each op is zeroed in _osd_req_op_init() */
5243540bfdbSIlya Dryomov 	memset(req, 0, sizeof(*req));
5253540bfdbSIlya Dryomov 
5263540bfdbSIlya Dryomov 	kref_init(&req->r_kref);
5273540bfdbSIlya Dryomov 	init_completion(&req->r_completion);
5283540bfdbSIlya Dryomov 	RB_CLEAR_NODE(&req->r_node);
5294609245eSIlya Dryomov 	RB_CLEAR_NODE(&req->r_mc_node);
5303540bfdbSIlya Dryomov 	INIT_LIST_HEAD(&req->r_unsafe_item);
5313540bfdbSIlya Dryomov 
5323540bfdbSIlya Dryomov 	target_init(&req->r_t);
5333540bfdbSIlya Dryomov }
5343540bfdbSIlya Dryomov 
535922dab61SIlya Dryomov /*
536922dab61SIlya Dryomov  * This is ugly, but it allows us to reuse linger registration and ping
537922dab61SIlya Dryomov  * requests, keeping the structure of the code around send_linger{_ping}()
538922dab61SIlya Dryomov  * reasonable.  Setting up a min_nr=2 mempool for each linger request
539922dab61SIlya Dryomov  * and dealing with copying ops (this blasts req only, watch op remains
540922dab61SIlya Dryomov  * intact) isn't any better.
541922dab61SIlya Dryomov  */
542922dab61SIlya Dryomov static void request_reinit(struct ceph_osd_request *req)
543922dab61SIlya Dryomov {
544922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
545922dab61SIlya Dryomov 	bool mempool = req->r_mempool;
546922dab61SIlya Dryomov 	unsigned int num_ops = req->r_num_ops;
547922dab61SIlya Dryomov 	u64 snapid = req->r_snapid;
548922dab61SIlya Dryomov 	struct ceph_snap_context *snapc = req->r_snapc;
549922dab61SIlya Dryomov 	bool linger = req->r_linger;
550922dab61SIlya Dryomov 	struct ceph_msg *request_msg = req->r_request;
551922dab61SIlya Dryomov 	struct ceph_msg *reply_msg = req->r_reply;
552922dab61SIlya Dryomov 
553922dab61SIlya Dryomov 	dout("%s req %p\n", __func__, req);
5542c935bc5SPeter Zijlstra 	WARN_ON(kref_read(&req->r_kref) != 1);
555922dab61SIlya Dryomov 	request_release_checks(req);
556922dab61SIlya Dryomov 
5572c935bc5SPeter Zijlstra 	WARN_ON(kref_read(&request_msg->kref) != 1);
5582c935bc5SPeter Zijlstra 	WARN_ON(kref_read(&reply_msg->kref) != 1);
559922dab61SIlya Dryomov 	target_destroy(&req->r_t);
560922dab61SIlya Dryomov 
561922dab61SIlya Dryomov 	request_init(req);
562922dab61SIlya Dryomov 	req->r_osdc = osdc;
563922dab61SIlya Dryomov 	req->r_mempool = mempool;
564922dab61SIlya Dryomov 	req->r_num_ops = num_ops;
565922dab61SIlya Dryomov 	req->r_snapid = snapid;
566922dab61SIlya Dryomov 	req->r_snapc = snapc;
567922dab61SIlya Dryomov 	req->r_linger = linger;
568922dab61SIlya Dryomov 	req->r_request = request_msg;
569922dab61SIlya Dryomov 	req->r_reply = reply_msg;
570922dab61SIlya Dryomov }
571922dab61SIlya Dryomov 
5723d14c5d2SYehuda Sadeh struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
5733d14c5d2SYehuda Sadeh 					       struct ceph_snap_context *snapc,
5741b83bef2SSage Weil 					       unsigned int num_ops,
5753d14c5d2SYehuda Sadeh 					       bool use_mempool,
57654a54007SAlex Elder 					       gfp_t gfp_flags)
5773d14c5d2SYehuda Sadeh {
5783d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
5793d14c5d2SYehuda Sadeh 
5803d14c5d2SYehuda Sadeh 	if (use_mempool) {
5813f1af42aSIlya Dryomov 		BUG_ON(num_ops > CEPH_OSD_SLAB_OPS);
5823d14c5d2SYehuda Sadeh 		req = mempool_alloc(osdc->req_mempool, gfp_flags);
5833f1af42aSIlya Dryomov 	} else if (num_ops <= CEPH_OSD_SLAB_OPS) {
5843f1af42aSIlya Dryomov 		req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
5853d14c5d2SYehuda Sadeh 	} else {
5863f1af42aSIlya Dryomov 		BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
5873f1af42aSIlya Dryomov 		req = kmalloc(sizeof(*req) + num_ops * sizeof(req->r_ops[0]),
5883f1af42aSIlya Dryomov 			      gfp_flags);
5893d14c5d2SYehuda Sadeh 	}
5903f1af42aSIlya Dryomov 	if (unlikely(!req))
5913d14c5d2SYehuda Sadeh 		return NULL;
5923d14c5d2SYehuda Sadeh 
5933540bfdbSIlya Dryomov 	request_init(req);
5943d14c5d2SYehuda Sadeh 	req->r_osdc = osdc;
5953d14c5d2SYehuda Sadeh 	req->r_mempool = use_mempool;
59679528734SAlex Elder 	req->r_num_ops = num_ops;
59784127282SIlya Dryomov 	req->r_snapid = CEPH_NOSNAP;
59884127282SIlya Dryomov 	req->r_snapc = ceph_get_snap_context(snapc);
5993d14c5d2SYehuda Sadeh 
60013d1ad16SIlya Dryomov 	dout("%s req %p\n", __func__, req);
60113d1ad16SIlya Dryomov 	return req;
6023f1af42aSIlya Dryomov }
60313d1ad16SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_alloc_request);
6043f1af42aSIlya Dryomov 
6052e59ffd1SIlya Dryomov static int ceph_oloc_encoding_size(const struct ceph_object_locator *oloc)
60630c156d9SYan, Zheng {
60730c156d9SYan, Zheng 	return 8 + 4 + 4 + 4 + (oloc->pool_ns ? oloc->pool_ns->len : 0);
60830c156d9SYan, Zheng }
60930c156d9SYan, Zheng 
61013d1ad16SIlya Dryomov int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp)
61113d1ad16SIlya Dryomov {
61213d1ad16SIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
61313d1ad16SIlya Dryomov 	struct ceph_msg *msg;
61413d1ad16SIlya Dryomov 	int msg_size;
6153d14c5d2SYehuda Sadeh 
616d30291b9SIlya Dryomov 	WARN_ON(ceph_oid_empty(&req->r_base_oid));
61730c156d9SYan, Zheng 	WARN_ON(ceph_oloc_empty(&req->r_base_oloc));
618d30291b9SIlya Dryomov 
61913d1ad16SIlya Dryomov 	/* create request message */
6208cb441c0SIlya Dryomov 	msg_size = CEPH_ENCODING_START_BLK_LEN +
6218cb441c0SIlya Dryomov 			CEPH_PGID_ENCODING_LEN + 1; /* spgid */
6228cb441c0SIlya Dryomov 	msg_size += 4 + 4 + 4; /* hash, osdmap_epoch, flags */
6238cb441c0SIlya Dryomov 	msg_size += CEPH_ENCODING_START_BLK_LEN +
6248cb441c0SIlya Dryomov 			sizeof(struct ceph_osd_reqid); /* reqid */
6258cb441c0SIlya Dryomov 	msg_size += sizeof(struct ceph_blkin_trace_info); /* trace */
6268cb441c0SIlya Dryomov 	msg_size += 4 + sizeof(struct ceph_timespec); /* client_inc, mtime */
62730c156d9SYan, Zheng 	msg_size += CEPH_ENCODING_START_BLK_LEN +
62830c156d9SYan, Zheng 			ceph_oloc_encoding_size(&req->r_base_oloc); /* oloc */
62913d1ad16SIlya Dryomov 	msg_size += 4 + req->r_base_oid.name_len; /* oid */
63013d1ad16SIlya Dryomov 	msg_size += 2 + req->r_num_ops * sizeof(struct ceph_osd_op);
631ae458f5aSIlya Dryomov 	msg_size += 8; /* snapid */
632ae458f5aSIlya Dryomov 	msg_size += 8; /* snap_seq */
63313d1ad16SIlya Dryomov 	msg_size += 4 + 8 * (req->r_snapc ? req->r_snapc->num_snaps : 0);
6348cb441c0SIlya Dryomov 	msg_size += 4 + 8; /* retry_attempt, features */
635ae458f5aSIlya Dryomov 
63613d1ad16SIlya Dryomov 	if (req->r_mempool)
6373d14c5d2SYehuda Sadeh 		msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
6383d14c5d2SYehuda Sadeh 	else
63913d1ad16SIlya Dryomov 		msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp, true);
64013d1ad16SIlya Dryomov 	if (!msg)
64113d1ad16SIlya Dryomov 		return -ENOMEM;
6423d14c5d2SYehuda Sadeh 
6433d14c5d2SYehuda Sadeh 	memset(msg->front.iov_base, 0, msg->front.iov_len);
6443d14c5d2SYehuda Sadeh 	req->r_request = msg;
6453d14c5d2SYehuda Sadeh 
64613d1ad16SIlya Dryomov 	/* create reply message */
64713d1ad16SIlya Dryomov 	msg_size = OSD_OPREPLY_FRONT_LEN;
648711da55dSIlya Dryomov 	msg_size += req->r_base_oid.name_len;
649711da55dSIlya Dryomov 	msg_size += req->r_num_ops * sizeof(struct ceph_osd_op);
65013d1ad16SIlya Dryomov 
65113d1ad16SIlya Dryomov 	if (req->r_mempool)
65213d1ad16SIlya Dryomov 		msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
65313d1ad16SIlya Dryomov 	else
65413d1ad16SIlya Dryomov 		msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, msg_size, gfp, true);
65513d1ad16SIlya Dryomov 	if (!msg)
65613d1ad16SIlya Dryomov 		return -ENOMEM;
65713d1ad16SIlya Dryomov 
65813d1ad16SIlya Dryomov 	req->r_reply = msg;
65913d1ad16SIlya Dryomov 
66013d1ad16SIlya Dryomov 	return 0;
66113d1ad16SIlya Dryomov }
66213d1ad16SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_alloc_messages);
6633d14c5d2SYehuda Sadeh 
664a8dd0a37SAlex Elder static bool osd_req_opcode_valid(u16 opcode)
665a8dd0a37SAlex Elder {
666a8dd0a37SAlex Elder 	switch (opcode) {
66770b5bfa3SIlya Dryomov #define GENERATE_CASE(op, opcode, str)	case CEPH_OSD_OP_##op: return true;
66870b5bfa3SIlya Dryomov __CEPH_FORALL_OSD_OPS(GENERATE_CASE)
66970b5bfa3SIlya Dryomov #undef GENERATE_CASE
670a8dd0a37SAlex Elder 	default:
671a8dd0a37SAlex Elder 		return false;
672a8dd0a37SAlex Elder 	}
673a8dd0a37SAlex Elder }
674a8dd0a37SAlex Elder 
67533803f33SAlex Elder /*
67633803f33SAlex Elder  * This is an osd op init function for opcodes that have no data or
67733803f33SAlex Elder  * other information associated with them.  It also serves as a
67833803f33SAlex Elder  * common init routine for all the other init functions, below.
67933803f33SAlex Elder  */
680c99d2d4aSAlex Elder static struct ceph_osd_req_op *
68149719778SAlex Elder _osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
682144cba14SYan, Zheng 		 u16 opcode, u32 flags)
68333803f33SAlex Elder {
684c99d2d4aSAlex Elder 	struct ceph_osd_req_op *op;
685c99d2d4aSAlex Elder 
686c99d2d4aSAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
68733803f33SAlex Elder 	BUG_ON(!osd_req_opcode_valid(opcode));
68833803f33SAlex Elder 
689c99d2d4aSAlex Elder 	op = &osd_req->r_ops[which];
69033803f33SAlex Elder 	memset(op, 0, sizeof (*op));
69133803f33SAlex Elder 	op->op = opcode;
692144cba14SYan, Zheng 	op->flags = flags;
693c99d2d4aSAlex Elder 
694c99d2d4aSAlex Elder 	return op;
69533803f33SAlex Elder }
69633803f33SAlex Elder 
69749719778SAlex Elder void osd_req_op_init(struct ceph_osd_request *osd_req,
698144cba14SYan, Zheng 		     unsigned int which, u16 opcode, u32 flags)
69949719778SAlex Elder {
700144cba14SYan, Zheng 	(void)_osd_req_op_init(osd_req, which, opcode, flags);
70149719778SAlex Elder }
70249719778SAlex Elder EXPORT_SYMBOL(osd_req_op_init);
70349719778SAlex Elder 
704c99d2d4aSAlex Elder void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
705c99d2d4aSAlex Elder 				unsigned int which, u16 opcode,
70633803f33SAlex Elder 				u64 offset, u64 length,
70733803f33SAlex Elder 				u64 truncate_size, u32 truncate_seq)
70833803f33SAlex Elder {
709144cba14SYan, Zheng 	struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
710144cba14SYan, Zheng 						      opcode, 0);
71133803f33SAlex Elder 	size_t payload_len = 0;
71233803f33SAlex Elder 
713ad7a60deSLi Wang 	BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
714e30b7577SIlya Dryomov 	       opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
715e30b7577SIlya Dryomov 	       opcode != CEPH_OSD_OP_TRUNCATE);
71633803f33SAlex Elder 
71733803f33SAlex Elder 	op->extent.offset = offset;
71833803f33SAlex Elder 	op->extent.length = length;
71933803f33SAlex Elder 	op->extent.truncate_size = truncate_size;
72033803f33SAlex Elder 	op->extent.truncate_seq = truncate_seq;
721e30b7577SIlya Dryomov 	if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
72233803f33SAlex Elder 		payload_len += length;
72333803f33SAlex Elder 
724de2aa102SIlya Dryomov 	op->indata_len = payload_len;
72533803f33SAlex Elder }
72633803f33SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_init);
72733803f33SAlex Elder 
728c99d2d4aSAlex Elder void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
729c99d2d4aSAlex Elder 				unsigned int which, u64 length)
730e5975c7cSAlex Elder {
731c99d2d4aSAlex Elder 	struct ceph_osd_req_op *op;
732c99d2d4aSAlex Elder 	u64 previous;
733c99d2d4aSAlex Elder 
734c99d2d4aSAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
735c99d2d4aSAlex Elder 	op = &osd_req->r_ops[which];
736c99d2d4aSAlex Elder 	previous = op->extent.length;
737e5975c7cSAlex Elder 
738e5975c7cSAlex Elder 	if (length == previous)
739e5975c7cSAlex Elder 		return;		/* Nothing to do */
740e5975c7cSAlex Elder 	BUG_ON(length > previous);
741e5975c7cSAlex Elder 
742e5975c7cSAlex Elder 	op->extent.length = length;
743d641df81SYan, Zheng 	if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
744de2aa102SIlya Dryomov 		op->indata_len -= previous - length;
745e5975c7cSAlex Elder }
746e5975c7cSAlex Elder EXPORT_SYMBOL(osd_req_op_extent_update);
747e5975c7cSAlex Elder 
7482c63f49aSYan, Zheng void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
7492c63f49aSYan, Zheng 				unsigned int which, u64 offset_inc)
7502c63f49aSYan, Zheng {
7512c63f49aSYan, Zheng 	struct ceph_osd_req_op *op, *prev_op;
7522c63f49aSYan, Zheng 
7532c63f49aSYan, Zheng 	BUG_ON(which + 1 >= osd_req->r_num_ops);
7542c63f49aSYan, Zheng 
7552c63f49aSYan, Zheng 	prev_op = &osd_req->r_ops[which];
7562c63f49aSYan, Zheng 	op = _osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags);
7572c63f49aSYan, Zheng 	/* dup previous one */
7582c63f49aSYan, Zheng 	op->indata_len = prev_op->indata_len;
7592c63f49aSYan, Zheng 	op->outdata_len = prev_op->outdata_len;
7602c63f49aSYan, Zheng 	op->extent = prev_op->extent;
7612c63f49aSYan, Zheng 	/* adjust offset */
7622c63f49aSYan, Zheng 	op->extent.offset += offset_inc;
7632c63f49aSYan, Zheng 	op->extent.length -= offset_inc;
7642c63f49aSYan, Zheng 
7652c63f49aSYan, Zheng 	if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
7662c63f49aSYan, Zheng 		op->indata_len -= offset_inc;
7672c63f49aSYan, Zheng }
7682c63f49aSYan, Zheng EXPORT_SYMBOL(osd_req_op_extent_dup_last);
7692c63f49aSYan, Zheng 
770fe943d50SChengguang Xu int osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
77104017e29SAlex Elder 			u16 opcode, const char *class, const char *method)
77233803f33SAlex Elder {
773144cba14SYan, Zheng 	struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
774144cba14SYan, Zheng 						      opcode, 0);
7755f562df5SAlex Elder 	struct ceph_pagelist *pagelist;
77633803f33SAlex Elder 	size_t payload_len = 0;
77733803f33SAlex Elder 	size_t size;
77833803f33SAlex Elder 
77933803f33SAlex Elder 	BUG_ON(opcode != CEPH_OSD_OP_CALL);
78033803f33SAlex Elder 
7815f562df5SAlex Elder 	pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
782fe943d50SChengguang Xu 	if (!pagelist)
783fe943d50SChengguang Xu 		return -ENOMEM;
784fe943d50SChengguang Xu 
7855f562df5SAlex Elder 	ceph_pagelist_init(pagelist);
7865f562df5SAlex Elder 
78733803f33SAlex Elder 	op->cls.class_name = class;
78833803f33SAlex Elder 	size = strlen(class);
78933803f33SAlex Elder 	BUG_ON(size > (size_t) U8_MAX);
79033803f33SAlex Elder 	op->cls.class_len = size;
7915f562df5SAlex Elder 	ceph_pagelist_append(pagelist, class, size);
79233803f33SAlex Elder 	payload_len += size;
79333803f33SAlex Elder 
79433803f33SAlex Elder 	op->cls.method_name = method;
79533803f33SAlex Elder 	size = strlen(method);
79633803f33SAlex Elder 	BUG_ON(size > (size_t) U8_MAX);
79733803f33SAlex Elder 	op->cls.method_len = size;
7985f562df5SAlex Elder 	ceph_pagelist_append(pagelist, method, size);
79933803f33SAlex Elder 	payload_len += size;
80033803f33SAlex Elder 
801a4ce40a9SAlex Elder 	osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
8025f562df5SAlex Elder 
803de2aa102SIlya Dryomov 	op->indata_len = payload_len;
804fe943d50SChengguang Xu 	return 0;
80533803f33SAlex Elder }
80633803f33SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_init);
8078c042b0dSAlex Elder 
808d74b50beSYan, Zheng int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
809d74b50beSYan, Zheng 			  u16 opcode, const char *name, const void *value,
810d74b50beSYan, Zheng 			  size_t size, u8 cmp_op, u8 cmp_mode)
811d74b50beSYan, Zheng {
812144cba14SYan, Zheng 	struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
813144cba14SYan, Zheng 						      opcode, 0);
814d74b50beSYan, Zheng 	struct ceph_pagelist *pagelist;
815d74b50beSYan, Zheng 	size_t payload_len;
816d74b50beSYan, Zheng 
817d74b50beSYan, Zheng 	BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
818d74b50beSYan, Zheng 
819d74b50beSYan, Zheng 	pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
820d74b50beSYan, Zheng 	if (!pagelist)
821d74b50beSYan, Zheng 		return -ENOMEM;
822d74b50beSYan, Zheng 
823d74b50beSYan, Zheng 	ceph_pagelist_init(pagelist);
824d74b50beSYan, Zheng 
825d74b50beSYan, Zheng 	payload_len = strlen(name);
826d74b50beSYan, Zheng 	op->xattr.name_len = payload_len;
827d74b50beSYan, Zheng 	ceph_pagelist_append(pagelist, name, payload_len);
828d74b50beSYan, Zheng 
829d74b50beSYan, Zheng 	op->xattr.value_len = size;
830d74b50beSYan, Zheng 	ceph_pagelist_append(pagelist, value, size);
831d74b50beSYan, Zheng 	payload_len += size;
832d74b50beSYan, Zheng 
833d74b50beSYan, Zheng 	op->xattr.cmp_op = cmp_op;
834d74b50beSYan, Zheng 	op->xattr.cmp_mode = cmp_mode;
835d74b50beSYan, Zheng 
836d74b50beSYan, Zheng 	ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
837de2aa102SIlya Dryomov 	op->indata_len = payload_len;
838d74b50beSYan, Zheng 	return 0;
839d74b50beSYan, Zheng }
840d74b50beSYan, Zheng EXPORT_SYMBOL(osd_req_op_xattr_init);
841d74b50beSYan, Zheng 
842922dab61SIlya Dryomov /*
843922dab61SIlya Dryomov  * @watch_opcode: CEPH_OSD_WATCH_OP_*
844922dab61SIlya Dryomov  */
845922dab61SIlya Dryomov static void osd_req_op_watch_init(struct ceph_osd_request *req, int which,
846922dab61SIlya Dryomov 				  u64 cookie, u8 watch_opcode)
84733803f33SAlex Elder {
848922dab61SIlya Dryomov 	struct ceph_osd_req_op *op;
84933803f33SAlex Elder 
850922dab61SIlya Dryomov 	op = _osd_req_op_init(req, which, CEPH_OSD_OP_WATCH, 0);
85133803f33SAlex Elder 	op->watch.cookie = cookie;
852922dab61SIlya Dryomov 	op->watch.op = watch_opcode;
853922dab61SIlya Dryomov 	op->watch.gen = 0;
85433803f33SAlex Elder }
85533803f33SAlex Elder 
856c647b8a8SIlya Dryomov void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
857c647b8a8SIlya Dryomov 				unsigned int which,
858c647b8a8SIlya Dryomov 				u64 expected_object_size,
859c647b8a8SIlya Dryomov 				u64 expected_write_size)
860c647b8a8SIlya Dryomov {
861c647b8a8SIlya Dryomov 	struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
862144cba14SYan, Zheng 						      CEPH_OSD_OP_SETALLOCHINT,
863144cba14SYan, Zheng 						      0);
864c647b8a8SIlya Dryomov 
865c647b8a8SIlya Dryomov 	op->alloc_hint.expected_object_size = expected_object_size;
866c647b8a8SIlya Dryomov 	op->alloc_hint.expected_write_size = expected_write_size;
867c647b8a8SIlya Dryomov 
868c647b8a8SIlya Dryomov 	/*
869c647b8a8SIlya Dryomov 	 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
870c647b8a8SIlya Dryomov 	 * not worth a feature bit.  Set FAILOK per-op flag to make
871c647b8a8SIlya Dryomov 	 * sure older osds don't trip over an unsupported opcode.
872c647b8a8SIlya Dryomov 	 */
873c647b8a8SIlya Dryomov 	op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
874c647b8a8SIlya Dryomov }
875c647b8a8SIlya Dryomov EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
876c647b8a8SIlya Dryomov 
87790af3602SAlex Elder static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
878ec9123c5SAlex Elder 				struct ceph_osd_data *osd_data)
879ec9123c5SAlex Elder {
880ec9123c5SAlex Elder 	u64 length = ceph_osd_data_length(osd_data);
881ec9123c5SAlex Elder 
882ec9123c5SAlex Elder 	if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
883ec9123c5SAlex Elder 		BUG_ON(length > (u64) SIZE_MAX);
884ec9123c5SAlex Elder 		if (length)
88590af3602SAlex Elder 			ceph_msg_data_add_pages(msg, osd_data->pages,
886ec9123c5SAlex Elder 					length, osd_data->alignment);
887ec9123c5SAlex Elder 	} else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
888ec9123c5SAlex Elder 		BUG_ON(!length);
88990af3602SAlex Elder 		ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
890ec9123c5SAlex Elder #ifdef CONFIG_BLOCK
891ec9123c5SAlex Elder 	} else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
8925359a17dSIlya Dryomov 		ceph_msg_data_add_bio(msg, &osd_data->bio_pos, length);
893ec9123c5SAlex Elder #endif
894b9e281c2SIlya Dryomov 	} else if (osd_data->type == CEPH_OSD_DATA_TYPE_BVECS) {
895b9e281c2SIlya Dryomov 		ceph_msg_data_add_bvecs(msg, &osd_data->bvec_pos);
896ec9123c5SAlex Elder 	} else {
897ec9123c5SAlex Elder 		BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
898ec9123c5SAlex Elder 	}
899ec9123c5SAlex Elder }
900ec9123c5SAlex Elder 
901bb873b53SIlya Dryomov static u32 osd_req_encode_op(struct ceph_osd_op *dst,
902bb873b53SIlya Dryomov 			     const struct ceph_osd_req_op *src)
9033d14c5d2SYehuda Sadeh {
904a8dd0a37SAlex Elder 	if (WARN_ON(!osd_req_opcode_valid(src->op))) {
905a8dd0a37SAlex Elder 		pr_err("unrecognized osd opcode %d\n", src->op);
906a8dd0a37SAlex Elder 
907a8dd0a37SAlex Elder 		return 0;
908a8dd0a37SAlex Elder 	}
9093d14c5d2SYehuda Sadeh 
910065a68f9SAlex Elder 	switch (src->op) {
911fbfab539SAlex Elder 	case CEPH_OSD_OP_STAT:
912fbfab539SAlex Elder 		break;
9133d14c5d2SYehuda Sadeh 	case CEPH_OSD_OP_READ:
9143d14c5d2SYehuda Sadeh 	case CEPH_OSD_OP_WRITE:
915e30b7577SIlya Dryomov 	case CEPH_OSD_OP_WRITEFULL:
916ad7a60deSLi Wang 	case CEPH_OSD_OP_ZERO:
917ad7a60deSLi Wang 	case CEPH_OSD_OP_TRUNCATE:
918175face2SAlex Elder 		dst->extent.offset = cpu_to_le64(src->extent.offset);
919175face2SAlex Elder 		dst->extent.length = cpu_to_le64(src->extent.length);
9203d14c5d2SYehuda Sadeh 		dst->extent.truncate_size =
9213d14c5d2SYehuda Sadeh 			cpu_to_le64(src->extent.truncate_size);
9223d14c5d2SYehuda Sadeh 		dst->extent.truncate_seq =
9233d14c5d2SYehuda Sadeh 			cpu_to_le32(src->extent.truncate_seq);
9243d14c5d2SYehuda Sadeh 		break;
9253d14c5d2SYehuda Sadeh 	case CEPH_OSD_OP_CALL:
9263d14c5d2SYehuda Sadeh 		dst->cls.class_len = src->cls.class_len;
9273d14c5d2SYehuda Sadeh 		dst->cls.method_len = src->cls.method_len;
928bb873b53SIlya Dryomov 		dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
9293d14c5d2SYehuda Sadeh 		break;
930a40c4f10SYehuda Sadeh 	case CEPH_OSD_OP_WATCH:
931a40c4f10SYehuda Sadeh 		dst->watch.cookie = cpu_to_le64(src->watch.cookie);
932922dab61SIlya Dryomov 		dst->watch.ver = cpu_to_le64(0);
933922dab61SIlya Dryomov 		dst->watch.op = src->watch.op;
934922dab61SIlya Dryomov 		dst->watch.gen = cpu_to_le32(src->watch.gen);
935922dab61SIlya Dryomov 		break;
936922dab61SIlya Dryomov 	case CEPH_OSD_OP_NOTIFY_ACK:
937a40c4f10SYehuda Sadeh 		break;
93819079203SIlya Dryomov 	case CEPH_OSD_OP_NOTIFY:
93919079203SIlya Dryomov 		dst->notify.cookie = cpu_to_le64(src->notify.cookie);
94019079203SIlya Dryomov 		break;
941a4ed38d7SDouglas Fuller 	case CEPH_OSD_OP_LIST_WATCHERS:
942a4ed38d7SDouglas Fuller 		break;
943c647b8a8SIlya Dryomov 	case CEPH_OSD_OP_SETALLOCHINT:
944c647b8a8SIlya Dryomov 		dst->alloc_hint.expected_object_size =
945c647b8a8SIlya Dryomov 		    cpu_to_le64(src->alloc_hint.expected_object_size);
946c647b8a8SIlya Dryomov 		dst->alloc_hint.expected_write_size =
947c647b8a8SIlya Dryomov 		    cpu_to_le64(src->alloc_hint.expected_write_size);
948c647b8a8SIlya Dryomov 		break;
949d74b50beSYan, Zheng 	case CEPH_OSD_OP_SETXATTR:
950d74b50beSYan, Zheng 	case CEPH_OSD_OP_CMPXATTR:
951d74b50beSYan, Zheng 		dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
952d74b50beSYan, Zheng 		dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
953d74b50beSYan, Zheng 		dst->xattr.cmp_op = src->xattr.cmp_op;
954d74b50beSYan, Zheng 		dst->xattr.cmp_mode = src->xattr.cmp_mode;
955d74b50beSYan, Zheng 		break;
956864e9197SYan, Zheng 	case CEPH_OSD_OP_CREATE:
957864e9197SYan, Zheng 	case CEPH_OSD_OP_DELETE:
958864e9197SYan, Zheng 		break;
9593d14c5d2SYehuda Sadeh 	default:
9604c46459cSAlex Elder 		pr_err("unsupported osd opcode %s\n",
9618f63ca2dSAlex Elder 			ceph_osd_op_name(src->op));
9624c46459cSAlex Elder 		WARN_ON(1);
963a8dd0a37SAlex Elder 
964a8dd0a37SAlex Elder 		return 0;
9653d14c5d2SYehuda Sadeh 	}
9667b25bf5fSIlya Dryomov 
967a8dd0a37SAlex Elder 	dst->op = cpu_to_le16(src->op);
9687b25bf5fSIlya Dryomov 	dst->flags = cpu_to_le32(src->flags);
969de2aa102SIlya Dryomov 	dst->payload_len = cpu_to_le32(src->indata_len);
970175face2SAlex Elder 
971bb873b53SIlya Dryomov 	return src->indata_len;
9723d14c5d2SYehuda Sadeh }
9733d14c5d2SYehuda Sadeh 
9743d14c5d2SYehuda Sadeh /*
9753d14c5d2SYehuda Sadeh  * build new request AND message, calculate layout, and adjust file
9763d14c5d2SYehuda Sadeh  * extent as needed.
9773d14c5d2SYehuda Sadeh  *
9783d14c5d2SYehuda Sadeh  * if the file was recently truncated, we include information about its
9793d14c5d2SYehuda Sadeh  * old and new size so that the object can be updated appropriately.  (we
9803d14c5d2SYehuda Sadeh  * avoid synchronously deleting truncated objects because it's slow.)
9813d14c5d2SYehuda Sadeh  */
9823d14c5d2SYehuda Sadeh struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
9833d14c5d2SYehuda Sadeh 					       struct ceph_file_layout *layout,
9843d14c5d2SYehuda Sadeh 					       struct ceph_vino vino,
985715e4cd4SYan, Zheng 					       u64 off, u64 *plen,
986715e4cd4SYan, Zheng 					       unsigned int which, int num_ops,
9873d14c5d2SYehuda Sadeh 					       int opcode, int flags,
9883d14c5d2SYehuda Sadeh 					       struct ceph_snap_context *snapc,
9893d14c5d2SYehuda Sadeh 					       u32 truncate_seq,
9903d14c5d2SYehuda Sadeh 					       u64 truncate_size,
991153e5167SAlex Elder 					       bool use_mempool)
9923d14c5d2SYehuda Sadeh {
9933d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
99475d1c941SAlex Elder 	u64 objnum = 0;
99575d1c941SAlex Elder 	u64 objoff = 0;
99675d1c941SAlex Elder 	u64 objlen = 0;
9976816282dSSage Weil 	int r;
9983d14c5d2SYehuda Sadeh 
999ad7a60deSLi Wang 	BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
1000864e9197SYan, Zheng 	       opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
1001864e9197SYan, Zheng 	       opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
10023d14c5d2SYehuda Sadeh 
1003acead002SAlex Elder 	req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
1004ae7ca4a3SAlex Elder 					GFP_NOFS);
100513d1ad16SIlya Dryomov 	if (!req) {
100613d1ad16SIlya Dryomov 		r = -ENOMEM;
100713d1ad16SIlya Dryomov 		goto fail;
100813d1ad16SIlya Dryomov 	}
100979528734SAlex Elder 
10103d14c5d2SYehuda Sadeh 	/* calculate max write size */
1011a19dadfbSAlex Elder 	r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
101213d1ad16SIlya Dryomov 	if (r)
101313d1ad16SIlya Dryomov 		goto fail;
1014a19dadfbSAlex Elder 
1015864e9197SYan, Zheng 	if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
1016144cba14SYan, Zheng 		osd_req_op_init(req, which, opcode, 0);
1017864e9197SYan, Zheng 	} else {
10187627151eSYan, Zheng 		u32 object_size = layout->object_size;
1019864e9197SYan, Zheng 		u32 object_base = off - objoff;
1020ccca4e37SYan, Zheng 		if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
1021d18d1e28SAlex Elder 			if (truncate_size <= object_base) {
1022d18d1e28SAlex Elder 				truncate_size = 0;
1023d18d1e28SAlex Elder 			} else {
1024d18d1e28SAlex Elder 				truncate_size -= object_base;
1025d18d1e28SAlex Elder 				if (truncate_size > object_size)
1026d18d1e28SAlex Elder 					truncate_size = object_size;
1027d18d1e28SAlex Elder 			}
1028ccca4e37SYan, Zheng 		}
1029715e4cd4SYan, Zheng 		osd_req_op_extent_init(req, which, opcode, objoff, objlen,
1030b0270324SAlex Elder 				       truncate_size, truncate_seq);
1031864e9197SYan, Zheng 	}
1032d18d1e28SAlex Elder 
1033a1f4020aSJeff Layton 	req->r_abort_on_full = true;
1034bb873b53SIlya Dryomov 	req->r_flags = flags;
10357627151eSYan, Zheng 	req->r_base_oloc.pool = layout->pool_id;
103630c156d9SYan, Zheng 	req->r_base_oloc.pool_ns = ceph_try_get_string(layout->pool_ns);
1037d30291b9SIlya Dryomov 	ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum);
1038dbe0fc41SAlex Elder 
1039bb873b53SIlya Dryomov 	req->r_snapid = vino.snap;
1040bb873b53SIlya Dryomov 	if (flags & CEPH_OSD_FLAG_WRITE)
1041bb873b53SIlya Dryomov 		req->r_data_offset = off;
1042bb873b53SIlya Dryomov 
104313d1ad16SIlya Dryomov 	r = ceph_osdc_alloc_messages(req, GFP_NOFS);
104413d1ad16SIlya Dryomov 	if (r)
104513d1ad16SIlya Dryomov 		goto fail;
104613d1ad16SIlya Dryomov 
10473d14c5d2SYehuda Sadeh 	return req;
104813d1ad16SIlya Dryomov 
104913d1ad16SIlya Dryomov fail:
105013d1ad16SIlya Dryomov 	ceph_osdc_put_request(req);
105113d1ad16SIlya Dryomov 	return ERR_PTR(r);
10523d14c5d2SYehuda Sadeh }
10533d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_new_request);
10543d14c5d2SYehuda Sadeh 
10553d14c5d2SYehuda Sadeh /*
10563d14c5d2SYehuda Sadeh  * We keep osd requests in an rbtree, sorted by ->r_tid.
10573d14c5d2SYehuda Sadeh  */
1058fcd00b68SIlya Dryomov DEFINE_RB_FUNCS(request, struct ceph_osd_request, r_tid, r_node)
10594609245eSIlya Dryomov DEFINE_RB_FUNCS(request_mc, struct ceph_osd_request, r_tid, r_mc_node)
10603d14c5d2SYehuda Sadeh 
106166850df5SIlya Dryomov /*
106266850df5SIlya Dryomov  * Call @fn on each OSD request as long as @fn returns 0.
106366850df5SIlya Dryomov  */
106466850df5SIlya Dryomov static void for_each_request(struct ceph_osd_client *osdc,
106566850df5SIlya Dryomov 			int (*fn)(struct ceph_osd_request *req, void *arg),
106666850df5SIlya Dryomov 			void *arg)
106766850df5SIlya Dryomov {
106866850df5SIlya Dryomov 	struct rb_node *n, *p;
106966850df5SIlya Dryomov 
107066850df5SIlya Dryomov 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
107166850df5SIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
107266850df5SIlya Dryomov 
107366850df5SIlya Dryomov 		for (p = rb_first(&osd->o_requests); p; ) {
107466850df5SIlya Dryomov 			struct ceph_osd_request *req =
107566850df5SIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
107666850df5SIlya Dryomov 
107766850df5SIlya Dryomov 			p = rb_next(p);
107866850df5SIlya Dryomov 			if (fn(req, arg))
107966850df5SIlya Dryomov 				return;
108066850df5SIlya Dryomov 		}
108166850df5SIlya Dryomov 	}
108266850df5SIlya Dryomov 
108366850df5SIlya Dryomov 	for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
108466850df5SIlya Dryomov 		struct ceph_osd_request *req =
108566850df5SIlya Dryomov 		    rb_entry(p, struct ceph_osd_request, r_node);
108666850df5SIlya Dryomov 
108766850df5SIlya Dryomov 		p = rb_next(p);
108866850df5SIlya Dryomov 		if (fn(req, arg))
108966850df5SIlya Dryomov 			return;
109066850df5SIlya Dryomov 	}
109166850df5SIlya Dryomov }
109266850df5SIlya Dryomov 
10930247a0cfSIlya Dryomov static bool osd_homeless(struct ceph_osd *osd)
10940247a0cfSIlya Dryomov {
10950247a0cfSIlya Dryomov 	return osd->o_osd == CEPH_HOMELESS_OSD;
10960247a0cfSIlya Dryomov }
10970247a0cfSIlya Dryomov 
10985aea3dcdSIlya Dryomov static bool osd_registered(struct ceph_osd *osd)
10993d14c5d2SYehuda Sadeh {
11005aea3dcdSIlya Dryomov 	verify_osdc_locked(osd->o_osdc);
11013d14c5d2SYehuda Sadeh 
11025aea3dcdSIlya Dryomov 	return !RB_EMPTY_NODE(&osd->o_node);
11033d14c5d2SYehuda Sadeh }
11043d14c5d2SYehuda Sadeh 
11053d14c5d2SYehuda Sadeh /*
11060247a0cfSIlya Dryomov  * Assumes @osd is zero-initialized.
11070247a0cfSIlya Dryomov  */
11080247a0cfSIlya Dryomov static void osd_init(struct ceph_osd *osd)
11090247a0cfSIlya Dryomov {
111002113a0fSElena Reshetova 	refcount_set(&osd->o_ref, 1);
11110247a0cfSIlya Dryomov 	RB_CLEAR_NODE(&osd->o_node);
11125aea3dcdSIlya Dryomov 	osd->o_requests = RB_ROOT;
1113922dab61SIlya Dryomov 	osd->o_linger_requests = RB_ROOT;
1114a02a946dSIlya Dryomov 	osd->o_backoff_mappings = RB_ROOT;
1115a02a946dSIlya Dryomov 	osd->o_backoffs_by_id = RB_ROOT;
11160247a0cfSIlya Dryomov 	INIT_LIST_HEAD(&osd->o_osd_lru);
11170247a0cfSIlya Dryomov 	INIT_LIST_HEAD(&osd->o_keepalive_item);
11180247a0cfSIlya Dryomov 	osd->o_incarnation = 1;
11195aea3dcdSIlya Dryomov 	mutex_init(&osd->lock);
11200247a0cfSIlya Dryomov }
11210247a0cfSIlya Dryomov 
11220247a0cfSIlya Dryomov static void osd_cleanup(struct ceph_osd *osd)
11230247a0cfSIlya Dryomov {
11240247a0cfSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&osd->o_node));
11255aea3dcdSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
1126922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
1127a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoff_mappings));
1128a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoffs_by_id));
11290247a0cfSIlya Dryomov 	WARN_ON(!list_empty(&osd->o_osd_lru));
11300247a0cfSIlya Dryomov 	WARN_ON(!list_empty(&osd->o_keepalive_item));
11310247a0cfSIlya Dryomov 
11320247a0cfSIlya Dryomov 	if (osd->o_auth.authorizer) {
11330247a0cfSIlya Dryomov 		WARN_ON(osd_homeless(osd));
11340247a0cfSIlya Dryomov 		ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
11350247a0cfSIlya Dryomov 	}
11360247a0cfSIlya Dryomov }
11370247a0cfSIlya Dryomov 
11380247a0cfSIlya Dryomov /*
11393d14c5d2SYehuda Sadeh  * Track open sessions with osds.
11403d14c5d2SYehuda Sadeh  */
1141e10006f8SAlex Elder static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
11423d14c5d2SYehuda Sadeh {
11433d14c5d2SYehuda Sadeh 	struct ceph_osd *osd;
11443d14c5d2SYehuda Sadeh 
11450247a0cfSIlya Dryomov 	WARN_ON(onum == CEPH_HOMELESS_OSD);
11460247a0cfSIlya Dryomov 
11477a28f59bSIlya Dryomov 	osd = kzalloc(sizeof(*osd), GFP_NOIO | __GFP_NOFAIL);
11480247a0cfSIlya Dryomov 	osd_init(osd);
11493d14c5d2SYehuda Sadeh 	osd->o_osdc = osdc;
1150e10006f8SAlex Elder 	osd->o_osd = onum;
11513d14c5d2SYehuda Sadeh 
1152b7a9e5ddSSage Weil 	ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
11533d14c5d2SYehuda Sadeh 
11543d14c5d2SYehuda Sadeh 	return osd;
11553d14c5d2SYehuda Sadeh }
11563d14c5d2SYehuda Sadeh 
11573d14c5d2SYehuda Sadeh static struct ceph_osd *get_osd(struct ceph_osd *osd)
11583d14c5d2SYehuda Sadeh {
115902113a0fSElena Reshetova 	if (refcount_inc_not_zero(&osd->o_ref)) {
116002113a0fSElena Reshetova 		dout("get_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref)-1,
116102113a0fSElena Reshetova 		     refcount_read(&osd->o_ref));
11623d14c5d2SYehuda Sadeh 		return osd;
11633d14c5d2SYehuda Sadeh 	} else {
11643d14c5d2SYehuda Sadeh 		dout("get_osd %p FAIL\n", osd);
11653d14c5d2SYehuda Sadeh 		return NULL;
11663d14c5d2SYehuda Sadeh 	}
11673d14c5d2SYehuda Sadeh }
11683d14c5d2SYehuda Sadeh 
11693d14c5d2SYehuda Sadeh static void put_osd(struct ceph_osd *osd)
11703d14c5d2SYehuda Sadeh {
117102113a0fSElena Reshetova 	dout("put_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref),
117202113a0fSElena Reshetova 	     refcount_read(&osd->o_ref) - 1);
117302113a0fSElena Reshetova 	if (refcount_dec_and_test(&osd->o_ref)) {
11740247a0cfSIlya Dryomov 		osd_cleanup(osd);
11753d14c5d2SYehuda Sadeh 		kfree(osd);
11763d14c5d2SYehuda Sadeh 	}
11773d14c5d2SYehuda Sadeh }
11783d14c5d2SYehuda Sadeh 
1179fcd00b68SIlya Dryomov DEFINE_RB_FUNCS(osd, struct ceph_osd, o_osd, o_node)
1180fcd00b68SIlya Dryomov 
11819dd2845cSIlya Dryomov static void __move_osd_to_lru(struct ceph_osd *osd)
11823d14c5d2SYehuda Sadeh {
11839dd2845cSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
11849dd2845cSIlya Dryomov 
11859dd2845cSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
11863d14c5d2SYehuda Sadeh 	BUG_ON(!list_empty(&osd->o_osd_lru));
1187bbf37ec3SIlya Dryomov 
11889dd2845cSIlya Dryomov 	spin_lock(&osdc->osd_lru_lock);
11893d14c5d2SYehuda Sadeh 	list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
11909dd2845cSIlya Dryomov 	spin_unlock(&osdc->osd_lru_lock);
11919dd2845cSIlya Dryomov 
1192a319bf56SIlya Dryomov 	osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
11933d14c5d2SYehuda Sadeh }
11943d14c5d2SYehuda Sadeh 
11959dd2845cSIlya Dryomov static void maybe_move_osd_to_lru(struct ceph_osd *osd)
1196bbf37ec3SIlya Dryomov {
11975aea3dcdSIlya Dryomov 	if (RB_EMPTY_ROOT(&osd->o_requests) &&
1198922dab61SIlya Dryomov 	    RB_EMPTY_ROOT(&osd->o_linger_requests))
11999dd2845cSIlya Dryomov 		__move_osd_to_lru(osd);
1200bbf37ec3SIlya Dryomov }
1201bbf37ec3SIlya Dryomov 
12023d14c5d2SYehuda Sadeh static void __remove_osd_from_lru(struct ceph_osd *osd)
12033d14c5d2SYehuda Sadeh {
12049dd2845cSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
12059dd2845cSIlya Dryomov 
12069dd2845cSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
12079dd2845cSIlya Dryomov 
12089dd2845cSIlya Dryomov 	spin_lock(&osdc->osd_lru_lock);
12093d14c5d2SYehuda Sadeh 	if (!list_empty(&osd->o_osd_lru))
12103d14c5d2SYehuda Sadeh 		list_del_init(&osd->o_osd_lru);
12119dd2845cSIlya Dryomov 	spin_unlock(&osdc->osd_lru_lock);
12123d14c5d2SYehuda Sadeh }
12133d14c5d2SYehuda Sadeh 
12143d14c5d2SYehuda Sadeh /*
12155aea3dcdSIlya Dryomov  * Close the connection and assign any leftover requests to the
12165aea3dcdSIlya Dryomov  * homeless session.
12175aea3dcdSIlya Dryomov  */
12185aea3dcdSIlya Dryomov static void close_osd(struct ceph_osd *osd)
12195aea3dcdSIlya Dryomov {
12205aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
12215aea3dcdSIlya Dryomov 	struct rb_node *n;
12225aea3dcdSIlya Dryomov 
12235aea3dcdSIlya Dryomov 	verify_osdc_wrlocked(osdc);
12245aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
12255aea3dcdSIlya Dryomov 
12265aea3dcdSIlya Dryomov 	ceph_con_close(&osd->o_con);
12275aea3dcdSIlya Dryomov 
12285aea3dcdSIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; ) {
12295aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
12305aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
12315aea3dcdSIlya Dryomov 
12325aea3dcdSIlya Dryomov 		n = rb_next(n); /* unlink_request() */
12335aea3dcdSIlya Dryomov 
12345aea3dcdSIlya Dryomov 		dout(" reassigning req %p tid %llu\n", req, req->r_tid);
12355aea3dcdSIlya Dryomov 		unlink_request(osd, req);
12365aea3dcdSIlya Dryomov 		link_request(&osdc->homeless_osd, req);
12375aea3dcdSIlya Dryomov 	}
1238922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_linger_requests); n; ) {
1239922dab61SIlya Dryomov 		struct ceph_osd_linger_request *lreq =
1240922dab61SIlya Dryomov 		    rb_entry(n, struct ceph_osd_linger_request, node);
1241922dab61SIlya Dryomov 
1242922dab61SIlya Dryomov 		n = rb_next(n); /* unlink_linger() */
1243922dab61SIlya Dryomov 
1244922dab61SIlya Dryomov 		dout(" reassigning lreq %p linger_id %llu\n", lreq,
1245922dab61SIlya Dryomov 		     lreq->linger_id);
1246922dab61SIlya Dryomov 		unlink_linger(osd, lreq);
1247922dab61SIlya Dryomov 		link_linger(&osdc->homeless_osd, lreq);
1248922dab61SIlya Dryomov 	}
1249a02a946dSIlya Dryomov 	clear_backoffs(osd);
12505aea3dcdSIlya Dryomov 
12515aea3dcdSIlya Dryomov 	__remove_osd_from_lru(osd);
12525aea3dcdSIlya Dryomov 	erase_osd(&osdc->osds, osd);
12535aea3dcdSIlya Dryomov 	put_osd(osd);
12545aea3dcdSIlya Dryomov }
12555aea3dcdSIlya Dryomov 
12565aea3dcdSIlya Dryomov /*
12573d14c5d2SYehuda Sadeh  * reset osd connect
12583d14c5d2SYehuda Sadeh  */
12595aea3dcdSIlya Dryomov static int reopen_osd(struct ceph_osd *osd)
12603d14c5d2SYehuda Sadeh {
1261c3acb181SAlex Elder 	struct ceph_entity_addr *peer_addr;
12623d14c5d2SYehuda Sadeh 
12635aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
12645aea3dcdSIlya Dryomov 
12655aea3dcdSIlya Dryomov 	if (RB_EMPTY_ROOT(&osd->o_requests) &&
1266922dab61SIlya Dryomov 	    RB_EMPTY_ROOT(&osd->o_linger_requests)) {
12675aea3dcdSIlya Dryomov 		close_osd(osd);
1268c3acb181SAlex Elder 		return -ENODEV;
1269c3acb181SAlex Elder 	}
1270c3acb181SAlex Elder 
12715aea3dcdSIlya Dryomov 	peer_addr = &osd->o_osdc->osdmap->osd_addr[osd->o_osd];
1272c3acb181SAlex Elder 	if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
12733d14c5d2SYehuda Sadeh 			!ceph_con_opened(&osd->o_con)) {
12745aea3dcdSIlya Dryomov 		struct rb_node *n;
1275c3acb181SAlex Elder 
12763d14c5d2SYehuda Sadeh 		dout("osd addr hasn't changed and connection never opened, "
12770b4af2e8SIlya Dryomov 		     "letting msgr retry\n");
12783d14c5d2SYehuda Sadeh 		/* touch each r_stamp for handle_timeout()'s benfit */
12795aea3dcdSIlya Dryomov 		for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
12805aea3dcdSIlya Dryomov 			struct ceph_osd_request *req =
12815aea3dcdSIlya Dryomov 			    rb_entry(n, struct ceph_osd_request, r_node);
12823d14c5d2SYehuda Sadeh 			req->r_stamp = jiffies;
12835aea3dcdSIlya Dryomov 		}
1284c3acb181SAlex Elder 
1285c3acb181SAlex Elder 		return -EAGAIN;
12863d14c5d2SYehuda Sadeh 	}
1287c3acb181SAlex Elder 
1288c3acb181SAlex Elder 	ceph_con_close(&osd->o_con);
1289c3acb181SAlex Elder 	ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1290c3acb181SAlex Elder 	osd->o_incarnation++;
1291c3acb181SAlex Elder 
1292c3acb181SAlex Elder 	return 0;
12933d14c5d2SYehuda Sadeh }
12943d14c5d2SYehuda Sadeh 
12955aea3dcdSIlya Dryomov static struct ceph_osd *lookup_create_osd(struct ceph_osd_client *osdc, int o,
12965aea3dcdSIlya Dryomov 					  bool wrlocked)
12973d14c5d2SYehuda Sadeh {
12985aea3dcdSIlya Dryomov 	struct ceph_osd *osd;
12995aea3dcdSIlya Dryomov 
13005aea3dcdSIlya Dryomov 	if (wrlocked)
13015aea3dcdSIlya Dryomov 		verify_osdc_wrlocked(osdc);
13025aea3dcdSIlya Dryomov 	else
13035aea3dcdSIlya Dryomov 		verify_osdc_locked(osdc);
13045aea3dcdSIlya Dryomov 
13055aea3dcdSIlya Dryomov 	if (o != CEPH_HOMELESS_OSD)
13065aea3dcdSIlya Dryomov 		osd = lookup_osd(&osdc->osds, o);
13075aea3dcdSIlya Dryomov 	else
13085aea3dcdSIlya Dryomov 		osd = &osdc->homeless_osd;
13095aea3dcdSIlya Dryomov 	if (!osd) {
13105aea3dcdSIlya Dryomov 		if (!wrlocked)
13115aea3dcdSIlya Dryomov 			return ERR_PTR(-EAGAIN);
13125aea3dcdSIlya Dryomov 
13135aea3dcdSIlya Dryomov 		osd = create_osd(osdc, o);
13145aea3dcdSIlya Dryomov 		insert_osd(&osdc->osds, osd);
13155aea3dcdSIlya Dryomov 		ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
13165aea3dcdSIlya Dryomov 			      &osdc->osdmap->osd_addr[osd->o_osd]);
13175aea3dcdSIlya Dryomov 	}
13185aea3dcdSIlya Dryomov 
13195aea3dcdSIlya Dryomov 	dout("%s osdc %p osd%d -> osd %p\n", __func__, osdc, o, osd);
13205aea3dcdSIlya Dryomov 	return osd;
1321a40c4f10SYehuda Sadeh }
1322a40c4f10SYehuda Sadeh 
13233d14c5d2SYehuda Sadeh /*
13245aea3dcdSIlya Dryomov  * Create request <-> OSD session relation.
13255aea3dcdSIlya Dryomov  *
13265aea3dcdSIlya Dryomov  * @req has to be assigned a tid, @osd may be homeless.
13273d14c5d2SYehuda Sadeh  */
13285aea3dcdSIlya Dryomov static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req)
13293d14c5d2SYehuda Sadeh {
13305aea3dcdSIlya Dryomov 	verify_osd_locked(osd);
13315aea3dcdSIlya Dryomov 	WARN_ON(!req->r_tid || req->r_osd);
13325aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
133335f9f8a0SSage Weil 	     req, req->r_tid);
13345aea3dcdSIlya Dryomov 
13355aea3dcdSIlya Dryomov 	if (!osd_homeless(osd))
13365aea3dcdSIlya Dryomov 		__remove_osd_from_lru(osd);
13375aea3dcdSIlya Dryomov 	else
13385aea3dcdSIlya Dryomov 		atomic_inc(&osd->o_osdc->num_homeless);
13395aea3dcdSIlya Dryomov 
13405aea3dcdSIlya Dryomov 	get_osd(osd);
13415aea3dcdSIlya Dryomov 	insert_request(&osd->o_requests, req);
13425aea3dcdSIlya Dryomov 	req->r_osd = osd;
134335f9f8a0SSage Weil }
134435f9f8a0SSage Weil 
13455aea3dcdSIlya Dryomov static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req)
13463d14c5d2SYehuda Sadeh {
13475aea3dcdSIlya Dryomov 	verify_osd_locked(osd);
13485aea3dcdSIlya Dryomov 	WARN_ON(req->r_osd != osd);
13495aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
13505aea3dcdSIlya Dryomov 	     req, req->r_tid);
13515aea3dcdSIlya Dryomov 
13525aea3dcdSIlya Dryomov 	req->r_osd = NULL;
13535aea3dcdSIlya Dryomov 	erase_request(&osd->o_requests, req);
13545aea3dcdSIlya Dryomov 	put_osd(osd);
13555aea3dcdSIlya Dryomov 
13565aea3dcdSIlya Dryomov 	if (!osd_homeless(osd))
13575aea3dcdSIlya Dryomov 		maybe_move_osd_to_lru(osd);
13585aea3dcdSIlya Dryomov 	else
13595aea3dcdSIlya Dryomov 		atomic_dec(&osd->o_osdc->num_homeless);
13603d14c5d2SYehuda Sadeh }
13613d14c5d2SYehuda Sadeh 
136263244fa1SIlya Dryomov static bool __pool_full(struct ceph_pg_pool_info *pi)
136363244fa1SIlya Dryomov {
136463244fa1SIlya Dryomov 	return pi->flags & CEPH_POOL_FLAG_FULL;
136563244fa1SIlya Dryomov }
136663244fa1SIlya Dryomov 
136742c1b124SIlya Dryomov static bool have_pool_full(struct ceph_osd_client *osdc)
136842c1b124SIlya Dryomov {
136942c1b124SIlya Dryomov 	struct rb_node *n;
137042c1b124SIlya Dryomov 
137142c1b124SIlya Dryomov 	for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
137242c1b124SIlya Dryomov 		struct ceph_pg_pool_info *pi =
137342c1b124SIlya Dryomov 		    rb_entry(n, struct ceph_pg_pool_info, node);
137442c1b124SIlya Dryomov 
137542c1b124SIlya Dryomov 		if (__pool_full(pi))
137642c1b124SIlya Dryomov 			return true;
137742c1b124SIlya Dryomov 	}
137842c1b124SIlya Dryomov 
137942c1b124SIlya Dryomov 	return false;
138042c1b124SIlya Dryomov }
138142c1b124SIlya Dryomov 
13825aea3dcdSIlya Dryomov static bool pool_full(struct ceph_osd_client *osdc, s64 pool_id)
13835aea3dcdSIlya Dryomov {
13845aea3dcdSIlya Dryomov 	struct ceph_pg_pool_info *pi;
13855aea3dcdSIlya Dryomov 
13865aea3dcdSIlya Dryomov 	pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
13875aea3dcdSIlya Dryomov 	if (!pi)
13885aea3dcdSIlya Dryomov 		return false;
13895aea3dcdSIlya Dryomov 
13905aea3dcdSIlya Dryomov 	return __pool_full(pi);
13915aea3dcdSIlya Dryomov }
13925aea3dcdSIlya Dryomov 
13933d14c5d2SYehuda Sadeh /*
1394d29adb34SJosh Durgin  * Returns whether a request should be blocked from being sent
1395d29adb34SJosh Durgin  * based on the current osdmap and osd_client settings.
1396d29adb34SJosh Durgin  */
139763244fa1SIlya Dryomov static bool target_should_be_paused(struct ceph_osd_client *osdc,
139863244fa1SIlya Dryomov 				    const struct ceph_osd_request_target *t,
139963244fa1SIlya Dryomov 				    struct ceph_pg_pool_info *pi)
140063244fa1SIlya Dryomov {
1401b7ec35b3SIlya Dryomov 	bool pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
1402b7ec35b3SIlya Dryomov 	bool pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
1403b7ec35b3SIlya Dryomov 		       ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
140463244fa1SIlya Dryomov 		       __pool_full(pi);
140563244fa1SIlya Dryomov 
14066d637a54SIlya Dryomov 	WARN_ON(pi->id != t->target_oloc.pool);
140758eb7932SJeff Layton 	return ((t->flags & CEPH_OSD_FLAG_READ) && pauserd) ||
140858eb7932SJeff Layton 	       ((t->flags & CEPH_OSD_FLAG_WRITE) && pausewr) ||
140958eb7932SJeff Layton 	       (osdc->osdmap->epoch < osdc->epoch_barrier);
141063244fa1SIlya Dryomov }
141163244fa1SIlya Dryomov 
141263244fa1SIlya Dryomov enum calc_target_result {
141363244fa1SIlya Dryomov 	CALC_TARGET_NO_ACTION = 0,
141463244fa1SIlya Dryomov 	CALC_TARGET_NEED_RESEND,
141563244fa1SIlya Dryomov 	CALC_TARGET_POOL_DNE,
141663244fa1SIlya Dryomov };
141763244fa1SIlya Dryomov 
141863244fa1SIlya Dryomov static enum calc_target_result calc_target(struct ceph_osd_client *osdc,
141963244fa1SIlya Dryomov 					   struct ceph_osd_request_target *t,
14207de030d6SIlya Dryomov 					   struct ceph_connection *con,
142163244fa1SIlya Dryomov 					   bool any_change)
142263244fa1SIlya Dryomov {
142363244fa1SIlya Dryomov 	struct ceph_pg_pool_info *pi;
142463244fa1SIlya Dryomov 	struct ceph_pg pgid, last_pgid;
142563244fa1SIlya Dryomov 	struct ceph_osds up, acting;
142663244fa1SIlya Dryomov 	bool force_resend = false;
142784ed45dfSIlya Dryomov 	bool unpaused = false;
142884ed45dfSIlya Dryomov 	bool legacy_change;
14297de030d6SIlya Dryomov 	bool split = false;
1430b7ec35b3SIlya Dryomov 	bool sort_bitwise = ceph_osdmap_flag(osdc, CEPH_OSDMAP_SORTBITWISE);
1431ae78dd81SIlya Dryomov 	bool recovery_deletes = ceph_osdmap_flag(osdc,
1432ae78dd81SIlya Dryomov 						 CEPH_OSDMAP_RECOVERY_DELETES);
143363244fa1SIlya Dryomov 	enum calc_target_result ct_res;
143463244fa1SIlya Dryomov 	int ret;
143563244fa1SIlya Dryomov 
143604c7d789SIlya Dryomov 	t->epoch = osdc->osdmap->epoch;
143763244fa1SIlya Dryomov 	pi = ceph_pg_pool_by_id(osdc->osdmap, t->base_oloc.pool);
143863244fa1SIlya Dryomov 	if (!pi) {
143963244fa1SIlya Dryomov 		t->osd = CEPH_HOMELESS_OSD;
144063244fa1SIlya Dryomov 		ct_res = CALC_TARGET_POOL_DNE;
144163244fa1SIlya Dryomov 		goto out;
144263244fa1SIlya Dryomov 	}
144363244fa1SIlya Dryomov 
144463244fa1SIlya Dryomov 	if (osdc->osdmap->epoch == pi->last_force_request_resend) {
1445dc93e0e2SIlya Dryomov 		if (t->last_force_resend < pi->last_force_request_resend) {
1446dc93e0e2SIlya Dryomov 			t->last_force_resend = pi->last_force_request_resend;
144763244fa1SIlya Dryomov 			force_resend = true;
1448dc93e0e2SIlya Dryomov 		} else if (t->last_force_resend == 0) {
144963244fa1SIlya Dryomov 			force_resend = true;
145063244fa1SIlya Dryomov 		}
145163244fa1SIlya Dryomov 	}
145263244fa1SIlya Dryomov 
1453db098ec4SIlya Dryomov 	/* apply tiering */
1454db098ec4SIlya Dryomov 	ceph_oid_copy(&t->target_oid, &t->base_oid);
1455db098ec4SIlya Dryomov 	ceph_oloc_copy(&t->target_oloc, &t->base_oloc);
1456db098ec4SIlya Dryomov 	if ((t->flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
145763244fa1SIlya Dryomov 		if (t->flags & CEPH_OSD_FLAG_READ && pi->read_tier >= 0)
145863244fa1SIlya Dryomov 			t->target_oloc.pool = pi->read_tier;
145963244fa1SIlya Dryomov 		if (t->flags & CEPH_OSD_FLAG_WRITE && pi->write_tier >= 0)
146063244fa1SIlya Dryomov 			t->target_oloc.pool = pi->write_tier;
14616d637a54SIlya Dryomov 
14626d637a54SIlya Dryomov 		pi = ceph_pg_pool_by_id(osdc->osdmap, t->target_oloc.pool);
14636d637a54SIlya Dryomov 		if (!pi) {
14646d637a54SIlya Dryomov 			t->osd = CEPH_HOMELESS_OSD;
14656d637a54SIlya Dryomov 			ct_res = CALC_TARGET_POOL_DNE;
14666d637a54SIlya Dryomov 			goto out;
14676d637a54SIlya Dryomov 		}
146863244fa1SIlya Dryomov 	}
146963244fa1SIlya Dryomov 
1470df28152dSIlya Dryomov 	ret = __ceph_object_locator_to_pg(pi, &t->target_oid, &t->target_oloc,
1471df28152dSIlya Dryomov 					  &pgid);
147263244fa1SIlya Dryomov 	if (ret) {
147363244fa1SIlya Dryomov 		WARN_ON(ret != -ENOENT);
147463244fa1SIlya Dryomov 		t->osd = CEPH_HOMELESS_OSD;
147563244fa1SIlya Dryomov 		ct_res = CALC_TARGET_POOL_DNE;
147663244fa1SIlya Dryomov 		goto out;
147763244fa1SIlya Dryomov 	}
147863244fa1SIlya Dryomov 	last_pgid.pool = pgid.pool;
147963244fa1SIlya Dryomov 	last_pgid.seed = ceph_stable_mod(pgid.seed, t->pg_num, t->pg_num_mask);
148063244fa1SIlya Dryomov 
1481df28152dSIlya Dryomov 	ceph_pg_to_up_acting_osds(osdc->osdmap, pi, &pgid, &up, &acting);
148263244fa1SIlya Dryomov 	if (any_change &&
148363244fa1SIlya Dryomov 	    ceph_is_new_interval(&t->acting,
148463244fa1SIlya Dryomov 				 &acting,
148563244fa1SIlya Dryomov 				 &t->up,
148663244fa1SIlya Dryomov 				 &up,
148763244fa1SIlya Dryomov 				 t->size,
148863244fa1SIlya Dryomov 				 pi->size,
148963244fa1SIlya Dryomov 				 t->min_size,
149063244fa1SIlya Dryomov 				 pi->min_size,
149163244fa1SIlya Dryomov 				 t->pg_num,
149263244fa1SIlya Dryomov 				 pi->pg_num,
149363244fa1SIlya Dryomov 				 t->sort_bitwise,
149463244fa1SIlya Dryomov 				 sort_bitwise,
1495ae78dd81SIlya Dryomov 				 t->recovery_deletes,
1496ae78dd81SIlya Dryomov 				 recovery_deletes,
149763244fa1SIlya Dryomov 				 &last_pgid))
149863244fa1SIlya Dryomov 		force_resend = true;
149963244fa1SIlya Dryomov 
150063244fa1SIlya Dryomov 	if (t->paused && !target_should_be_paused(osdc, t, pi)) {
150163244fa1SIlya Dryomov 		t->paused = false;
150284ed45dfSIlya Dryomov 		unpaused = true;
150363244fa1SIlya Dryomov 	}
150484ed45dfSIlya Dryomov 	legacy_change = ceph_pg_compare(&t->pgid, &pgid) ||
150584ed45dfSIlya Dryomov 			ceph_osds_changed(&t->acting, &acting, any_change);
15067de030d6SIlya Dryomov 	if (t->pg_num)
15077de030d6SIlya Dryomov 		split = ceph_pg_is_split(&last_pgid, t->pg_num, pi->pg_num);
150863244fa1SIlya Dryomov 
15097de030d6SIlya Dryomov 	if (legacy_change || force_resend || split) {
151063244fa1SIlya Dryomov 		t->pgid = pgid; /* struct */
1511df28152dSIlya Dryomov 		ceph_pg_to_primary_shard(osdc->osdmap, pi, &pgid, &t->spgid);
151263244fa1SIlya Dryomov 		ceph_osds_copy(&t->acting, &acting);
151363244fa1SIlya Dryomov 		ceph_osds_copy(&t->up, &up);
151463244fa1SIlya Dryomov 		t->size = pi->size;
151563244fa1SIlya Dryomov 		t->min_size = pi->min_size;
151663244fa1SIlya Dryomov 		t->pg_num = pi->pg_num;
151763244fa1SIlya Dryomov 		t->pg_num_mask = pi->pg_num_mask;
151863244fa1SIlya Dryomov 		t->sort_bitwise = sort_bitwise;
1519ae78dd81SIlya Dryomov 		t->recovery_deletes = recovery_deletes;
152063244fa1SIlya Dryomov 
152163244fa1SIlya Dryomov 		t->osd = acting.primary;
152263244fa1SIlya Dryomov 	}
152363244fa1SIlya Dryomov 
15247de030d6SIlya Dryomov 	if (unpaused || legacy_change || force_resend ||
15257de030d6SIlya Dryomov 	    (split && con && CEPH_HAVE_FEATURE(con->peer_features,
15267de030d6SIlya Dryomov 					       RESEND_ON_SPLIT)))
152784ed45dfSIlya Dryomov 		ct_res = CALC_TARGET_NEED_RESEND;
152884ed45dfSIlya Dryomov 	else
152984ed45dfSIlya Dryomov 		ct_res = CALC_TARGET_NO_ACTION;
153084ed45dfSIlya Dryomov 
153163244fa1SIlya Dryomov out:
153263244fa1SIlya Dryomov 	dout("%s t %p -> ct_res %d osd %d\n", __func__, t, ct_res, t->osd);
153363244fa1SIlya Dryomov 	return ct_res;
153463244fa1SIlya Dryomov }
153563244fa1SIlya Dryomov 
1536a02a946dSIlya Dryomov static struct ceph_spg_mapping *alloc_spg_mapping(void)
1537a02a946dSIlya Dryomov {
1538a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
1539a02a946dSIlya Dryomov 
1540a02a946dSIlya Dryomov 	spg = kmalloc(sizeof(*spg), GFP_NOIO);
1541a02a946dSIlya Dryomov 	if (!spg)
1542a02a946dSIlya Dryomov 		return NULL;
1543a02a946dSIlya Dryomov 
1544a02a946dSIlya Dryomov 	RB_CLEAR_NODE(&spg->node);
1545a02a946dSIlya Dryomov 	spg->backoffs = RB_ROOT;
1546a02a946dSIlya Dryomov 	return spg;
1547a02a946dSIlya Dryomov }
1548a02a946dSIlya Dryomov 
1549a02a946dSIlya Dryomov static void free_spg_mapping(struct ceph_spg_mapping *spg)
1550a02a946dSIlya Dryomov {
1551a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&spg->node));
1552a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&spg->backoffs));
1553a02a946dSIlya Dryomov 
1554a02a946dSIlya Dryomov 	kfree(spg);
1555a02a946dSIlya Dryomov }
1556a02a946dSIlya Dryomov 
1557a02a946dSIlya Dryomov /*
1558a02a946dSIlya Dryomov  * rbtree of ceph_spg_mapping for handling map<spg_t, ...>, similar to
1559a02a946dSIlya Dryomov  * ceph_pg_mapping.  Used to track OSD backoffs -- a backoff [range] is
1560a02a946dSIlya Dryomov  * defined only within a specific spgid; it does not pass anything to
1561a02a946dSIlya Dryomov  * children on split, or to another primary.
1562a02a946dSIlya Dryomov  */
1563a02a946dSIlya Dryomov DEFINE_RB_FUNCS2(spg_mapping, struct ceph_spg_mapping, spgid, ceph_spg_compare,
1564a02a946dSIlya Dryomov 		 RB_BYPTR, const struct ceph_spg *, node)
1565a02a946dSIlya Dryomov 
1566a02a946dSIlya Dryomov static u64 hoid_get_bitwise_key(const struct ceph_hobject_id *hoid)
1567a02a946dSIlya Dryomov {
1568a02a946dSIlya Dryomov 	return hoid->is_max ? 0x100000000ull : hoid->hash_reverse_bits;
1569a02a946dSIlya Dryomov }
1570a02a946dSIlya Dryomov 
1571a02a946dSIlya Dryomov static void hoid_get_effective_key(const struct ceph_hobject_id *hoid,
1572a02a946dSIlya Dryomov 				   void **pkey, size_t *pkey_len)
1573a02a946dSIlya Dryomov {
1574a02a946dSIlya Dryomov 	if (hoid->key_len) {
1575a02a946dSIlya Dryomov 		*pkey = hoid->key;
1576a02a946dSIlya Dryomov 		*pkey_len = hoid->key_len;
1577a02a946dSIlya Dryomov 	} else {
1578a02a946dSIlya Dryomov 		*pkey = hoid->oid;
1579a02a946dSIlya Dryomov 		*pkey_len = hoid->oid_len;
1580a02a946dSIlya Dryomov 	}
1581a02a946dSIlya Dryomov }
1582a02a946dSIlya Dryomov 
1583a02a946dSIlya Dryomov static int compare_names(const void *name1, size_t name1_len,
1584a02a946dSIlya Dryomov 			 const void *name2, size_t name2_len)
1585a02a946dSIlya Dryomov {
1586a02a946dSIlya Dryomov 	int ret;
1587a02a946dSIlya Dryomov 
1588a02a946dSIlya Dryomov 	ret = memcmp(name1, name2, min(name1_len, name2_len));
1589a02a946dSIlya Dryomov 	if (!ret) {
1590a02a946dSIlya Dryomov 		if (name1_len < name2_len)
1591a02a946dSIlya Dryomov 			ret = -1;
1592a02a946dSIlya Dryomov 		else if (name1_len > name2_len)
1593a02a946dSIlya Dryomov 			ret = 1;
1594a02a946dSIlya Dryomov 	}
1595a02a946dSIlya Dryomov 	return ret;
1596a02a946dSIlya Dryomov }
1597a02a946dSIlya Dryomov 
1598a02a946dSIlya Dryomov static int hoid_compare(const struct ceph_hobject_id *lhs,
1599a02a946dSIlya Dryomov 			const struct ceph_hobject_id *rhs)
1600a02a946dSIlya Dryomov {
1601a02a946dSIlya Dryomov 	void *effective_key1, *effective_key2;
1602a02a946dSIlya Dryomov 	size_t effective_key1_len, effective_key2_len;
1603a02a946dSIlya Dryomov 	int ret;
1604a02a946dSIlya Dryomov 
1605a02a946dSIlya Dryomov 	if (lhs->is_max < rhs->is_max)
1606a02a946dSIlya Dryomov 		return -1;
1607a02a946dSIlya Dryomov 	if (lhs->is_max > rhs->is_max)
1608a02a946dSIlya Dryomov 		return 1;
1609a02a946dSIlya Dryomov 
1610a02a946dSIlya Dryomov 	if (lhs->pool < rhs->pool)
1611a02a946dSIlya Dryomov 		return -1;
1612a02a946dSIlya Dryomov 	if (lhs->pool > rhs->pool)
1613a02a946dSIlya Dryomov 		return 1;
1614a02a946dSIlya Dryomov 
1615a02a946dSIlya Dryomov 	if (hoid_get_bitwise_key(lhs) < hoid_get_bitwise_key(rhs))
1616a02a946dSIlya Dryomov 		return -1;
1617a02a946dSIlya Dryomov 	if (hoid_get_bitwise_key(lhs) > hoid_get_bitwise_key(rhs))
1618a02a946dSIlya Dryomov 		return 1;
1619a02a946dSIlya Dryomov 
1620a02a946dSIlya Dryomov 	ret = compare_names(lhs->nspace, lhs->nspace_len,
1621a02a946dSIlya Dryomov 			    rhs->nspace, rhs->nspace_len);
1622a02a946dSIlya Dryomov 	if (ret)
1623a02a946dSIlya Dryomov 		return ret;
1624a02a946dSIlya Dryomov 
1625a02a946dSIlya Dryomov 	hoid_get_effective_key(lhs, &effective_key1, &effective_key1_len);
1626a02a946dSIlya Dryomov 	hoid_get_effective_key(rhs, &effective_key2, &effective_key2_len);
1627a02a946dSIlya Dryomov 	ret = compare_names(effective_key1, effective_key1_len,
1628a02a946dSIlya Dryomov 			    effective_key2, effective_key2_len);
1629a02a946dSIlya Dryomov 	if (ret)
1630a02a946dSIlya Dryomov 		return ret;
1631a02a946dSIlya Dryomov 
1632a02a946dSIlya Dryomov 	ret = compare_names(lhs->oid, lhs->oid_len, rhs->oid, rhs->oid_len);
1633a02a946dSIlya Dryomov 	if (ret)
1634a02a946dSIlya Dryomov 		return ret;
1635a02a946dSIlya Dryomov 
1636a02a946dSIlya Dryomov 	if (lhs->snapid < rhs->snapid)
1637a02a946dSIlya Dryomov 		return -1;
1638a02a946dSIlya Dryomov 	if (lhs->snapid > rhs->snapid)
1639a02a946dSIlya Dryomov 		return 1;
1640a02a946dSIlya Dryomov 
1641a02a946dSIlya Dryomov 	return 0;
1642a02a946dSIlya Dryomov }
1643a02a946dSIlya Dryomov 
1644a02a946dSIlya Dryomov /*
1645a02a946dSIlya Dryomov  * For decoding ->begin and ->end of MOSDBackoff only -- no MIN/MAX
1646a02a946dSIlya Dryomov  * compat stuff here.
1647a02a946dSIlya Dryomov  *
1648a02a946dSIlya Dryomov  * Assumes @hoid is zero-initialized.
1649a02a946dSIlya Dryomov  */
1650a02a946dSIlya Dryomov static int decode_hoid(void **p, void *end, struct ceph_hobject_id *hoid)
1651a02a946dSIlya Dryomov {
1652a02a946dSIlya Dryomov 	u8 struct_v;
1653a02a946dSIlya Dryomov 	u32 struct_len;
1654a02a946dSIlya Dryomov 	int ret;
1655a02a946dSIlya Dryomov 
1656a02a946dSIlya Dryomov 	ret = ceph_start_decoding(p, end, 4, "hobject_t", &struct_v,
1657a02a946dSIlya Dryomov 				  &struct_len);
1658a02a946dSIlya Dryomov 	if (ret)
1659a02a946dSIlya Dryomov 		return ret;
1660a02a946dSIlya Dryomov 
1661a02a946dSIlya Dryomov 	if (struct_v < 4) {
1662a02a946dSIlya Dryomov 		pr_err("got struct_v %d < 4 of hobject_t\n", struct_v);
1663a02a946dSIlya Dryomov 		goto e_inval;
1664a02a946dSIlya Dryomov 	}
1665a02a946dSIlya Dryomov 
1666a02a946dSIlya Dryomov 	hoid->key = ceph_extract_encoded_string(p, end, &hoid->key_len,
1667a02a946dSIlya Dryomov 						GFP_NOIO);
1668a02a946dSIlya Dryomov 	if (IS_ERR(hoid->key)) {
1669a02a946dSIlya Dryomov 		ret = PTR_ERR(hoid->key);
1670a02a946dSIlya Dryomov 		hoid->key = NULL;
1671a02a946dSIlya Dryomov 		return ret;
1672a02a946dSIlya Dryomov 	}
1673a02a946dSIlya Dryomov 
1674a02a946dSIlya Dryomov 	hoid->oid = ceph_extract_encoded_string(p, end, &hoid->oid_len,
1675a02a946dSIlya Dryomov 						GFP_NOIO);
1676a02a946dSIlya Dryomov 	if (IS_ERR(hoid->oid)) {
1677a02a946dSIlya Dryomov 		ret = PTR_ERR(hoid->oid);
1678a02a946dSIlya Dryomov 		hoid->oid = NULL;
1679a02a946dSIlya Dryomov 		return ret;
1680a02a946dSIlya Dryomov 	}
1681a02a946dSIlya Dryomov 
1682a02a946dSIlya Dryomov 	ceph_decode_64_safe(p, end, hoid->snapid, e_inval);
1683a02a946dSIlya Dryomov 	ceph_decode_32_safe(p, end, hoid->hash, e_inval);
1684a02a946dSIlya Dryomov 	ceph_decode_8_safe(p, end, hoid->is_max, e_inval);
1685a02a946dSIlya Dryomov 
1686a02a946dSIlya Dryomov 	hoid->nspace = ceph_extract_encoded_string(p, end, &hoid->nspace_len,
1687a02a946dSIlya Dryomov 						   GFP_NOIO);
1688a02a946dSIlya Dryomov 	if (IS_ERR(hoid->nspace)) {
1689a02a946dSIlya Dryomov 		ret = PTR_ERR(hoid->nspace);
1690a02a946dSIlya Dryomov 		hoid->nspace = NULL;
1691a02a946dSIlya Dryomov 		return ret;
1692a02a946dSIlya Dryomov 	}
1693a02a946dSIlya Dryomov 
1694a02a946dSIlya Dryomov 	ceph_decode_64_safe(p, end, hoid->pool, e_inval);
1695a02a946dSIlya Dryomov 
1696a02a946dSIlya Dryomov 	ceph_hoid_build_hash_cache(hoid);
1697a02a946dSIlya Dryomov 	return 0;
1698a02a946dSIlya Dryomov 
1699a02a946dSIlya Dryomov e_inval:
1700a02a946dSIlya Dryomov 	return -EINVAL;
1701a02a946dSIlya Dryomov }
1702a02a946dSIlya Dryomov 
1703a02a946dSIlya Dryomov static int hoid_encoding_size(const struct ceph_hobject_id *hoid)
1704a02a946dSIlya Dryomov {
1705a02a946dSIlya Dryomov 	return 8 + 4 + 1 + 8 + /* snapid, hash, is_max, pool */
1706a02a946dSIlya Dryomov 	       4 + hoid->key_len + 4 + hoid->oid_len + 4 + hoid->nspace_len;
1707a02a946dSIlya Dryomov }
1708a02a946dSIlya Dryomov 
1709a02a946dSIlya Dryomov static void encode_hoid(void **p, void *end, const struct ceph_hobject_id *hoid)
1710a02a946dSIlya Dryomov {
1711a02a946dSIlya Dryomov 	ceph_start_encoding(p, 4, 3, hoid_encoding_size(hoid));
1712a02a946dSIlya Dryomov 	ceph_encode_string(p, end, hoid->key, hoid->key_len);
1713a02a946dSIlya Dryomov 	ceph_encode_string(p, end, hoid->oid, hoid->oid_len);
1714a02a946dSIlya Dryomov 	ceph_encode_64(p, hoid->snapid);
1715a02a946dSIlya Dryomov 	ceph_encode_32(p, hoid->hash);
1716a02a946dSIlya Dryomov 	ceph_encode_8(p, hoid->is_max);
1717a02a946dSIlya Dryomov 	ceph_encode_string(p, end, hoid->nspace, hoid->nspace_len);
1718a02a946dSIlya Dryomov 	ceph_encode_64(p, hoid->pool);
1719a02a946dSIlya Dryomov }
1720a02a946dSIlya Dryomov 
1721a02a946dSIlya Dryomov static void free_hoid(struct ceph_hobject_id *hoid)
1722a02a946dSIlya Dryomov {
1723a02a946dSIlya Dryomov 	if (hoid) {
1724a02a946dSIlya Dryomov 		kfree(hoid->key);
1725a02a946dSIlya Dryomov 		kfree(hoid->oid);
1726a02a946dSIlya Dryomov 		kfree(hoid->nspace);
1727a02a946dSIlya Dryomov 		kfree(hoid);
1728a02a946dSIlya Dryomov 	}
1729a02a946dSIlya Dryomov }
1730a02a946dSIlya Dryomov 
1731a02a946dSIlya Dryomov static struct ceph_osd_backoff *alloc_backoff(void)
1732a02a946dSIlya Dryomov {
1733a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
1734a02a946dSIlya Dryomov 
1735a02a946dSIlya Dryomov 	backoff = kzalloc(sizeof(*backoff), GFP_NOIO);
1736a02a946dSIlya Dryomov 	if (!backoff)
1737a02a946dSIlya Dryomov 		return NULL;
1738a02a946dSIlya Dryomov 
1739a02a946dSIlya Dryomov 	RB_CLEAR_NODE(&backoff->spg_node);
1740a02a946dSIlya Dryomov 	RB_CLEAR_NODE(&backoff->id_node);
1741a02a946dSIlya Dryomov 	return backoff;
1742a02a946dSIlya Dryomov }
1743a02a946dSIlya Dryomov 
1744a02a946dSIlya Dryomov static void free_backoff(struct ceph_osd_backoff *backoff)
1745a02a946dSIlya Dryomov {
1746a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&backoff->spg_node));
1747a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&backoff->id_node));
1748a02a946dSIlya Dryomov 
1749a02a946dSIlya Dryomov 	free_hoid(backoff->begin);
1750a02a946dSIlya Dryomov 	free_hoid(backoff->end);
1751a02a946dSIlya Dryomov 	kfree(backoff);
1752a02a946dSIlya Dryomov }
1753a02a946dSIlya Dryomov 
1754a02a946dSIlya Dryomov /*
1755a02a946dSIlya Dryomov  * Within a specific spgid, backoffs are managed by ->begin hoid.
1756a02a946dSIlya Dryomov  */
1757a02a946dSIlya Dryomov DEFINE_RB_INSDEL_FUNCS2(backoff, struct ceph_osd_backoff, begin, hoid_compare,
1758a02a946dSIlya Dryomov 			RB_BYVAL, spg_node);
1759a02a946dSIlya Dryomov 
1760a02a946dSIlya Dryomov static struct ceph_osd_backoff *lookup_containing_backoff(struct rb_root *root,
1761a02a946dSIlya Dryomov 					    const struct ceph_hobject_id *hoid)
1762a02a946dSIlya Dryomov {
1763a02a946dSIlya Dryomov 	struct rb_node *n = root->rb_node;
1764a02a946dSIlya Dryomov 
1765a02a946dSIlya Dryomov 	while (n) {
1766a02a946dSIlya Dryomov 		struct ceph_osd_backoff *cur =
1767a02a946dSIlya Dryomov 		    rb_entry(n, struct ceph_osd_backoff, spg_node);
1768a02a946dSIlya Dryomov 		int cmp;
1769a02a946dSIlya Dryomov 
1770a02a946dSIlya Dryomov 		cmp = hoid_compare(hoid, cur->begin);
1771a02a946dSIlya Dryomov 		if (cmp < 0) {
1772a02a946dSIlya Dryomov 			n = n->rb_left;
1773a02a946dSIlya Dryomov 		} else if (cmp > 0) {
1774a02a946dSIlya Dryomov 			if (hoid_compare(hoid, cur->end) < 0)
1775a02a946dSIlya Dryomov 				return cur;
1776a02a946dSIlya Dryomov 
1777a02a946dSIlya Dryomov 			n = n->rb_right;
1778a02a946dSIlya Dryomov 		} else {
1779a02a946dSIlya Dryomov 			return cur;
1780a02a946dSIlya Dryomov 		}
1781a02a946dSIlya Dryomov 	}
1782a02a946dSIlya Dryomov 
1783a02a946dSIlya Dryomov 	return NULL;
1784a02a946dSIlya Dryomov }
1785a02a946dSIlya Dryomov 
1786a02a946dSIlya Dryomov /*
1787a02a946dSIlya Dryomov  * Each backoff has a unique id within its OSD session.
1788a02a946dSIlya Dryomov  */
1789a02a946dSIlya Dryomov DEFINE_RB_FUNCS(backoff_by_id, struct ceph_osd_backoff, id, id_node)
1790a02a946dSIlya Dryomov 
1791a02a946dSIlya Dryomov static void clear_backoffs(struct ceph_osd *osd)
1792a02a946dSIlya Dryomov {
1793a02a946dSIlya Dryomov 	while (!RB_EMPTY_ROOT(&osd->o_backoff_mappings)) {
1794a02a946dSIlya Dryomov 		struct ceph_spg_mapping *spg =
1795a02a946dSIlya Dryomov 		    rb_entry(rb_first(&osd->o_backoff_mappings),
1796a02a946dSIlya Dryomov 			     struct ceph_spg_mapping, node);
1797a02a946dSIlya Dryomov 
1798a02a946dSIlya Dryomov 		while (!RB_EMPTY_ROOT(&spg->backoffs)) {
1799a02a946dSIlya Dryomov 			struct ceph_osd_backoff *backoff =
1800a02a946dSIlya Dryomov 			    rb_entry(rb_first(&spg->backoffs),
1801a02a946dSIlya Dryomov 				     struct ceph_osd_backoff, spg_node);
1802a02a946dSIlya Dryomov 
1803a02a946dSIlya Dryomov 			erase_backoff(&spg->backoffs, backoff);
1804a02a946dSIlya Dryomov 			erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
1805a02a946dSIlya Dryomov 			free_backoff(backoff);
1806a02a946dSIlya Dryomov 		}
1807a02a946dSIlya Dryomov 		erase_spg_mapping(&osd->o_backoff_mappings, spg);
1808a02a946dSIlya Dryomov 		free_spg_mapping(spg);
1809a02a946dSIlya Dryomov 	}
1810a02a946dSIlya Dryomov }
1811a02a946dSIlya Dryomov 
1812a02a946dSIlya Dryomov /*
1813a02a946dSIlya Dryomov  * Set up a temporary, non-owning view into @t.
1814a02a946dSIlya Dryomov  */
1815a02a946dSIlya Dryomov static void hoid_fill_from_target(struct ceph_hobject_id *hoid,
1816a02a946dSIlya Dryomov 				  const struct ceph_osd_request_target *t)
1817a02a946dSIlya Dryomov {
1818a02a946dSIlya Dryomov 	hoid->key = NULL;
1819a02a946dSIlya Dryomov 	hoid->key_len = 0;
1820a02a946dSIlya Dryomov 	hoid->oid = t->target_oid.name;
1821a02a946dSIlya Dryomov 	hoid->oid_len = t->target_oid.name_len;
1822a02a946dSIlya Dryomov 	hoid->snapid = CEPH_NOSNAP;
1823a02a946dSIlya Dryomov 	hoid->hash = t->pgid.seed;
1824a02a946dSIlya Dryomov 	hoid->is_max = false;
1825a02a946dSIlya Dryomov 	if (t->target_oloc.pool_ns) {
1826a02a946dSIlya Dryomov 		hoid->nspace = t->target_oloc.pool_ns->str;
1827a02a946dSIlya Dryomov 		hoid->nspace_len = t->target_oloc.pool_ns->len;
1828a02a946dSIlya Dryomov 	} else {
1829a02a946dSIlya Dryomov 		hoid->nspace = NULL;
1830a02a946dSIlya Dryomov 		hoid->nspace_len = 0;
1831a02a946dSIlya Dryomov 	}
1832a02a946dSIlya Dryomov 	hoid->pool = t->target_oloc.pool;
1833a02a946dSIlya Dryomov 	ceph_hoid_build_hash_cache(hoid);
1834a02a946dSIlya Dryomov }
1835a02a946dSIlya Dryomov 
1836a02a946dSIlya Dryomov static bool should_plug_request(struct ceph_osd_request *req)
1837a02a946dSIlya Dryomov {
1838a02a946dSIlya Dryomov 	struct ceph_osd *osd = req->r_osd;
1839a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
1840a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
1841a02a946dSIlya Dryomov 	struct ceph_hobject_id hoid;
1842a02a946dSIlya Dryomov 
1843a02a946dSIlya Dryomov 	spg = lookup_spg_mapping(&osd->o_backoff_mappings, &req->r_t.spgid);
1844a02a946dSIlya Dryomov 	if (!spg)
1845a02a946dSIlya Dryomov 		return false;
1846a02a946dSIlya Dryomov 
1847a02a946dSIlya Dryomov 	hoid_fill_from_target(&hoid, &req->r_t);
1848a02a946dSIlya Dryomov 	backoff = lookup_containing_backoff(&spg->backoffs, &hoid);
1849a02a946dSIlya Dryomov 	if (!backoff)
1850a02a946dSIlya Dryomov 		return false;
1851a02a946dSIlya Dryomov 
1852a02a946dSIlya Dryomov 	dout("%s req %p tid %llu backoff osd%d spgid %llu.%xs%d id %llu\n",
1853a02a946dSIlya Dryomov 	     __func__, req, req->r_tid, osd->o_osd, backoff->spgid.pgid.pool,
1854a02a946dSIlya Dryomov 	     backoff->spgid.pgid.seed, backoff->spgid.shard, backoff->id);
1855a02a946dSIlya Dryomov 	return true;
1856a02a946dSIlya Dryomov }
1857a02a946dSIlya Dryomov 
1858bb873b53SIlya Dryomov static void setup_request_data(struct ceph_osd_request *req,
1859bb873b53SIlya Dryomov 			       struct ceph_msg *msg)
18603d14c5d2SYehuda Sadeh {
1861bb873b53SIlya Dryomov 	u32 data_len = 0;
1862bb873b53SIlya Dryomov 	int i;
18633d14c5d2SYehuda Sadeh 
1864bb873b53SIlya Dryomov 	if (!list_empty(&msg->data))
1865bb873b53SIlya Dryomov 		return;
18663d14c5d2SYehuda Sadeh 
1867bb873b53SIlya Dryomov 	WARN_ON(msg->data_length);
1868bb873b53SIlya Dryomov 	for (i = 0; i < req->r_num_ops; i++) {
1869bb873b53SIlya Dryomov 		struct ceph_osd_req_op *op = &req->r_ops[i];
1870bb873b53SIlya Dryomov 
1871bb873b53SIlya Dryomov 		switch (op->op) {
1872bb873b53SIlya Dryomov 		/* request */
1873bb873b53SIlya Dryomov 		case CEPH_OSD_OP_WRITE:
1874bb873b53SIlya Dryomov 		case CEPH_OSD_OP_WRITEFULL:
1875bb873b53SIlya Dryomov 			WARN_ON(op->indata_len != op->extent.length);
1876bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(msg, &op->extent.osd_data);
1877bb873b53SIlya Dryomov 			break;
1878bb873b53SIlya Dryomov 		case CEPH_OSD_OP_SETXATTR:
1879bb873b53SIlya Dryomov 		case CEPH_OSD_OP_CMPXATTR:
1880bb873b53SIlya Dryomov 			WARN_ON(op->indata_len != op->xattr.name_len +
1881bb873b53SIlya Dryomov 						  op->xattr.value_len);
1882bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(msg, &op->xattr.osd_data);
1883bb873b53SIlya Dryomov 			break;
1884922dab61SIlya Dryomov 		case CEPH_OSD_OP_NOTIFY_ACK:
1885922dab61SIlya Dryomov 			ceph_osdc_msg_data_add(msg,
1886922dab61SIlya Dryomov 					       &op->notify_ack.request_data);
1887922dab61SIlya Dryomov 			break;
1888bb873b53SIlya Dryomov 
1889bb873b53SIlya Dryomov 		/* reply */
1890bb873b53SIlya Dryomov 		case CEPH_OSD_OP_STAT:
1891bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(req->r_reply,
1892bb873b53SIlya Dryomov 					       &op->raw_data_in);
1893bb873b53SIlya Dryomov 			break;
1894bb873b53SIlya Dryomov 		case CEPH_OSD_OP_READ:
1895bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(req->r_reply,
1896bb873b53SIlya Dryomov 					       &op->extent.osd_data);
1897bb873b53SIlya Dryomov 			break;
1898a4ed38d7SDouglas Fuller 		case CEPH_OSD_OP_LIST_WATCHERS:
1899a4ed38d7SDouglas Fuller 			ceph_osdc_msg_data_add(req->r_reply,
1900a4ed38d7SDouglas Fuller 					       &op->list_watchers.response_data);
1901a4ed38d7SDouglas Fuller 			break;
1902bb873b53SIlya Dryomov 
1903bb873b53SIlya Dryomov 		/* both */
1904bb873b53SIlya Dryomov 		case CEPH_OSD_OP_CALL:
1905bb873b53SIlya Dryomov 			WARN_ON(op->indata_len != op->cls.class_len +
1906bb873b53SIlya Dryomov 						  op->cls.method_len +
1907bb873b53SIlya Dryomov 						  op->cls.indata_len);
1908bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(msg, &op->cls.request_info);
1909bb873b53SIlya Dryomov 			/* optional, can be NONE */
1910bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(msg, &op->cls.request_data);
1911bb873b53SIlya Dryomov 			/* optional, can be NONE */
1912bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(req->r_reply,
1913bb873b53SIlya Dryomov 					       &op->cls.response_data);
1914bb873b53SIlya Dryomov 			break;
191519079203SIlya Dryomov 		case CEPH_OSD_OP_NOTIFY:
191619079203SIlya Dryomov 			ceph_osdc_msg_data_add(msg,
191719079203SIlya Dryomov 					       &op->notify.request_data);
191819079203SIlya Dryomov 			ceph_osdc_msg_data_add(req->r_reply,
191919079203SIlya Dryomov 					       &op->notify.response_data);
192019079203SIlya Dryomov 			break;
1921bb873b53SIlya Dryomov 		}
1922bb873b53SIlya Dryomov 
1923bb873b53SIlya Dryomov 		data_len += op->indata_len;
1924bb873b53SIlya Dryomov 	}
1925bb873b53SIlya Dryomov 
1926bb873b53SIlya Dryomov 	WARN_ON(data_len != msg->data_length);
1927bb873b53SIlya Dryomov }
1928bb873b53SIlya Dryomov 
19292e59ffd1SIlya Dryomov static void encode_pgid(void **p, const struct ceph_pg *pgid)
19302e59ffd1SIlya Dryomov {
19312e59ffd1SIlya Dryomov 	ceph_encode_8(p, 1);
19322e59ffd1SIlya Dryomov 	ceph_encode_64(p, pgid->pool);
19332e59ffd1SIlya Dryomov 	ceph_encode_32(p, pgid->seed);
19342e59ffd1SIlya Dryomov 	ceph_encode_32(p, -1); /* preferred */
19352e59ffd1SIlya Dryomov }
19362e59ffd1SIlya Dryomov 
19378cb441c0SIlya Dryomov static void encode_spgid(void **p, const struct ceph_spg *spgid)
19388cb441c0SIlya Dryomov {
19398cb441c0SIlya Dryomov 	ceph_start_encoding(p, 1, 1, CEPH_PGID_ENCODING_LEN + 1);
19408cb441c0SIlya Dryomov 	encode_pgid(p, &spgid->pgid);
19418cb441c0SIlya Dryomov 	ceph_encode_8(p, spgid->shard);
19428cb441c0SIlya Dryomov }
19438cb441c0SIlya Dryomov 
19442e59ffd1SIlya Dryomov static void encode_oloc(void **p, void *end,
19452e59ffd1SIlya Dryomov 			const struct ceph_object_locator *oloc)
19462e59ffd1SIlya Dryomov {
19472e59ffd1SIlya Dryomov 	ceph_start_encoding(p, 5, 4, ceph_oloc_encoding_size(oloc));
19482e59ffd1SIlya Dryomov 	ceph_encode_64(p, oloc->pool);
19492e59ffd1SIlya Dryomov 	ceph_encode_32(p, -1); /* preferred */
19502e59ffd1SIlya Dryomov 	ceph_encode_32(p, 0);  /* key len */
19512e59ffd1SIlya Dryomov 	if (oloc->pool_ns)
19522e59ffd1SIlya Dryomov 		ceph_encode_string(p, end, oloc->pool_ns->str,
19532e59ffd1SIlya Dryomov 				   oloc->pool_ns->len);
19542e59ffd1SIlya Dryomov 	else
19552e59ffd1SIlya Dryomov 		ceph_encode_32(p, 0);
19562e59ffd1SIlya Dryomov }
19572e59ffd1SIlya Dryomov 
19588cb441c0SIlya Dryomov static void encode_request_partial(struct ceph_osd_request *req,
19598cb441c0SIlya Dryomov 				   struct ceph_msg *msg)
1960bb873b53SIlya Dryomov {
1961bb873b53SIlya Dryomov 	void *p = msg->front.iov_base;
1962bb873b53SIlya Dryomov 	void *const end = p + msg->front_alloc_len;
1963bb873b53SIlya Dryomov 	u32 data_len = 0;
1964bb873b53SIlya Dryomov 	int i;
1965bb873b53SIlya Dryomov 
1966bb873b53SIlya Dryomov 	if (req->r_flags & CEPH_OSD_FLAG_WRITE) {
1967bb873b53SIlya Dryomov 		/* snapshots aren't writeable */
1968bb873b53SIlya Dryomov 		WARN_ON(req->r_snapid != CEPH_NOSNAP);
1969bb873b53SIlya Dryomov 	} else {
1970bb873b53SIlya Dryomov 		WARN_ON(req->r_mtime.tv_sec || req->r_mtime.tv_nsec ||
1971bb873b53SIlya Dryomov 			req->r_data_offset || req->r_snapc);
1972bb873b53SIlya Dryomov 	}
1973bb873b53SIlya Dryomov 
1974bb873b53SIlya Dryomov 	setup_request_data(req, msg);
1975bb873b53SIlya Dryomov 
19768cb441c0SIlya Dryomov 	encode_spgid(&p, &req->r_t.spgid); /* actual spg */
19778cb441c0SIlya Dryomov 	ceph_encode_32(&p, req->r_t.pgid.seed); /* raw hash */
1978bb873b53SIlya Dryomov 	ceph_encode_32(&p, req->r_osdc->osdmap->epoch);
1979bb873b53SIlya Dryomov 	ceph_encode_32(&p, req->r_flags);
19808cb441c0SIlya Dryomov 
19818cb441c0SIlya Dryomov 	/* reqid */
19828cb441c0SIlya Dryomov 	ceph_start_encoding(&p, 2, 2, sizeof(struct ceph_osd_reqid));
19838cb441c0SIlya Dryomov 	memset(p, 0, sizeof(struct ceph_osd_reqid));
19848cb441c0SIlya Dryomov 	p += sizeof(struct ceph_osd_reqid);
19858cb441c0SIlya Dryomov 
19868cb441c0SIlya Dryomov 	/* trace */
19878cb441c0SIlya Dryomov 	memset(p, 0, sizeof(struct ceph_blkin_trace_info));
19888cb441c0SIlya Dryomov 	p += sizeof(struct ceph_blkin_trace_info);
19898cb441c0SIlya Dryomov 
19908cb441c0SIlya Dryomov 	ceph_encode_32(&p, 0); /* client_inc, always 0 */
1991bb873b53SIlya Dryomov 	ceph_encode_timespec(p, &req->r_mtime);
1992bb873b53SIlya Dryomov 	p += sizeof(struct ceph_timespec);
1993aa26d662SJeff Layton 
19942e59ffd1SIlya Dryomov 	encode_oloc(&p, end, &req->r_t.target_oloc);
19952e59ffd1SIlya Dryomov 	ceph_encode_string(&p, end, req->r_t.target_oid.name,
19962e59ffd1SIlya Dryomov 			   req->r_t.target_oid.name_len);
1997bb873b53SIlya Dryomov 
1998bb873b53SIlya Dryomov 	/* ops, can imply data */
1999bb873b53SIlya Dryomov 	ceph_encode_16(&p, req->r_num_ops);
2000bb873b53SIlya Dryomov 	for (i = 0; i < req->r_num_ops; i++) {
2001bb873b53SIlya Dryomov 		data_len += osd_req_encode_op(p, &req->r_ops[i]);
2002bb873b53SIlya Dryomov 		p += sizeof(struct ceph_osd_op);
2003bb873b53SIlya Dryomov 	}
2004bb873b53SIlya Dryomov 
2005bb873b53SIlya Dryomov 	ceph_encode_64(&p, req->r_snapid); /* snapid */
2006bb873b53SIlya Dryomov 	if (req->r_snapc) {
2007bb873b53SIlya Dryomov 		ceph_encode_64(&p, req->r_snapc->seq);
2008bb873b53SIlya Dryomov 		ceph_encode_32(&p, req->r_snapc->num_snaps);
2009bb873b53SIlya Dryomov 		for (i = 0; i < req->r_snapc->num_snaps; i++)
2010bb873b53SIlya Dryomov 			ceph_encode_64(&p, req->r_snapc->snaps[i]);
2011bb873b53SIlya Dryomov 	} else {
2012bb873b53SIlya Dryomov 		ceph_encode_64(&p, 0); /* snap_seq */
2013bb873b53SIlya Dryomov 		ceph_encode_32(&p, 0); /* snaps len */
2014bb873b53SIlya Dryomov 	}
2015bb873b53SIlya Dryomov 
2016bb873b53SIlya Dryomov 	ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
2017986e8989SIlya Dryomov 	BUG_ON(p > end - 8); /* space for features */
2018bb873b53SIlya Dryomov 
20198cb441c0SIlya Dryomov 	msg->hdr.version = cpu_to_le16(8); /* MOSDOp v8 */
20208cb441c0SIlya Dryomov 	/* front_len is finalized in encode_request_finish() */
2021986e8989SIlya Dryomov 	msg->front.iov_len = p - msg->front.iov_base;
2022986e8989SIlya Dryomov 	msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2023bb873b53SIlya Dryomov 	msg->hdr.data_len = cpu_to_le32(data_len);
2024bb873b53SIlya Dryomov 	/*
2025bb873b53SIlya Dryomov 	 * The header "data_off" is a hint to the receiver allowing it
2026bb873b53SIlya Dryomov 	 * to align received data into its buffers such that there's no
2027bb873b53SIlya Dryomov 	 * need to re-copy it before writing it to disk (direct I/O).
2028bb873b53SIlya Dryomov 	 */
2029bb873b53SIlya Dryomov 	msg->hdr.data_off = cpu_to_le16(req->r_data_offset);
2030bb873b53SIlya Dryomov 
20318cb441c0SIlya Dryomov 	dout("%s req %p msg %p oid %s oid_len %d\n", __func__, req, msg,
20328cb441c0SIlya Dryomov 	     req->r_t.target_oid.name, req->r_t.target_oid.name_len);
20338cb441c0SIlya Dryomov }
20348cb441c0SIlya Dryomov 
20358cb441c0SIlya Dryomov static void encode_request_finish(struct ceph_msg *msg)
20368cb441c0SIlya Dryomov {
20378cb441c0SIlya Dryomov 	void *p = msg->front.iov_base;
2038986e8989SIlya Dryomov 	void *const partial_end = p + msg->front.iov_len;
20398cb441c0SIlya Dryomov 	void *const end = p + msg->front_alloc_len;
20408cb441c0SIlya Dryomov 
20418cb441c0SIlya Dryomov 	if (CEPH_HAVE_FEATURE(msg->con->peer_features, RESEND_ON_SPLIT)) {
20428cb441c0SIlya Dryomov 		/* luminous OSD -- encode features and be done */
2043986e8989SIlya Dryomov 		p = partial_end;
20448cb441c0SIlya Dryomov 		ceph_encode_64(&p, msg->con->peer_features);
20458cb441c0SIlya Dryomov 	} else {
20468cb441c0SIlya Dryomov 		struct {
20478cb441c0SIlya Dryomov 			char spgid[CEPH_ENCODING_START_BLK_LEN +
20488cb441c0SIlya Dryomov 				   CEPH_PGID_ENCODING_LEN + 1];
20498cb441c0SIlya Dryomov 			__le32 hash;
20508cb441c0SIlya Dryomov 			__le32 epoch;
20518cb441c0SIlya Dryomov 			__le32 flags;
20528cb441c0SIlya Dryomov 			char reqid[CEPH_ENCODING_START_BLK_LEN +
20538cb441c0SIlya Dryomov 				   sizeof(struct ceph_osd_reqid)];
20548cb441c0SIlya Dryomov 			char trace[sizeof(struct ceph_blkin_trace_info)];
20558cb441c0SIlya Dryomov 			__le32 client_inc;
20568cb441c0SIlya Dryomov 			struct ceph_timespec mtime;
20578cb441c0SIlya Dryomov 		} __packed head;
20588cb441c0SIlya Dryomov 		struct ceph_pg pgid;
20598cb441c0SIlya Dryomov 		void *oloc, *oid, *tail;
20608cb441c0SIlya Dryomov 		int oloc_len, oid_len, tail_len;
20618cb441c0SIlya Dryomov 		int len;
20628cb441c0SIlya Dryomov 
20638cb441c0SIlya Dryomov 		/*
20648cb441c0SIlya Dryomov 		 * Pre-luminous OSD -- reencode v8 into v4 using @head
20658cb441c0SIlya Dryomov 		 * as a temporary buffer.  Encode the raw PG; the rest
20668cb441c0SIlya Dryomov 		 * is just a matter of moving oloc, oid and tail blobs
20678cb441c0SIlya Dryomov 		 * around.
20688cb441c0SIlya Dryomov 		 */
20698cb441c0SIlya Dryomov 		memcpy(&head, p, sizeof(head));
20708cb441c0SIlya Dryomov 		p += sizeof(head);
20718cb441c0SIlya Dryomov 
20728cb441c0SIlya Dryomov 		oloc = p;
20738cb441c0SIlya Dryomov 		p += CEPH_ENCODING_START_BLK_LEN;
20748cb441c0SIlya Dryomov 		pgid.pool = ceph_decode_64(&p);
20758cb441c0SIlya Dryomov 		p += 4 + 4; /* preferred, key len */
20768cb441c0SIlya Dryomov 		len = ceph_decode_32(&p);
20778cb441c0SIlya Dryomov 		p += len;   /* nspace */
20788cb441c0SIlya Dryomov 		oloc_len = p - oloc;
20798cb441c0SIlya Dryomov 
20808cb441c0SIlya Dryomov 		oid = p;
20818cb441c0SIlya Dryomov 		len = ceph_decode_32(&p);
20828cb441c0SIlya Dryomov 		p += len;
20838cb441c0SIlya Dryomov 		oid_len = p - oid;
20848cb441c0SIlya Dryomov 
20858cb441c0SIlya Dryomov 		tail = p;
2086986e8989SIlya Dryomov 		tail_len = partial_end - p;
20878cb441c0SIlya Dryomov 
20888cb441c0SIlya Dryomov 		p = msg->front.iov_base;
20898cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.client_inc, sizeof(head.client_inc));
20908cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.epoch, sizeof(head.epoch));
20918cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.flags, sizeof(head.flags));
20928cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.mtime, sizeof(head.mtime));
20938cb441c0SIlya Dryomov 
20948cb441c0SIlya Dryomov 		/* reassert_version */
20958cb441c0SIlya Dryomov 		memset(p, 0, sizeof(struct ceph_eversion));
20968cb441c0SIlya Dryomov 		p += sizeof(struct ceph_eversion);
20978cb441c0SIlya Dryomov 
20988cb441c0SIlya Dryomov 		BUG_ON(p >= oloc);
20998cb441c0SIlya Dryomov 		memmove(p, oloc, oloc_len);
21008cb441c0SIlya Dryomov 		p += oloc_len;
21018cb441c0SIlya Dryomov 
21028cb441c0SIlya Dryomov 		pgid.seed = le32_to_cpu(head.hash);
21038cb441c0SIlya Dryomov 		encode_pgid(&p, &pgid); /* raw pg */
21048cb441c0SIlya Dryomov 
21058cb441c0SIlya Dryomov 		BUG_ON(p >= oid);
21068cb441c0SIlya Dryomov 		memmove(p, oid, oid_len);
21078cb441c0SIlya Dryomov 		p += oid_len;
21088cb441c0SIlya Dryomov 
21098cb441c0SIlya Dryomov 		/* tail -- ops, snapid, snapc, retry_attempt */
21108cb441c0SIlya Dryomov 		BUG_ON(p >= tail);
21118cb441c0SIlya Dryomov 		memmove(p, tail, tail_len);
21128cb441c0SIlya Dryomov 		p += tail_len;
21138cb441c0SIlya Dryomov 
21148cb441c0SIlya Dryomov 		msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
21158cb441c0SIlya Dryomov 	}
21168cb441c0SIlya Dryomov 
21178cb441c0SIlya Dryomov 	BUG_ON(p > end);
21188cb441c0SIlya Dryomov 	msg->front.iov_len = p - msg->front.iov_base;
21198cb441c0SIlya Dryomov 	msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
21208cb441c0SIlya Dryomov 
21218cb441c0SIlya Dryomov 	dout("%s msg %p tid %llu %u+%u+%u v%d\n", __func__, msg,
21228cb441c0SIlya Dryomov 	     le64_to_cpu(msg->hdr.tid), le32_to_cpu(msg->hdr.front_len),
21238cb441c0SIlya Dryomov 	     le32_to_cpu(msg->hdr.middle_len), le32_to_cpu(msg->hdr.data_len),
21248cb441c0SIlya Dryomov 	     le16_to_cpu(msg->hdr.version));
2125bb873b53SIlya Dryomov }
2126bb873b53SIlya Dryomov 
2127bb873b53SIlya Dryomov /*
2128bb873b53SIlya Dryomov  * @req has to be assigned a tid and registered.
2129bb873b53SIlya Dryomov  */
2130bb873b53SIlya Dryomov static void send_request(struct ceph_osd_request *req)
2131bb873b53SIlya Dryomov {
2132bb873b53SIlya Dryomov 	struct ceph_osd *osd = req->r_osd;
2133bb873b53SIlya Dryomov 
21345aea3dcdSIlya Dryomov 	verify_osd_locked(osd);
2135bb873b53SIlya Dryomov 	WARN_ON(osd->o_osd != req->r_t.osd);
2136bb873b53SIlya Dryomov 
2137a02a946dSIlya Dryomov 	/* backoff? */
2138a02a946dSIlya Dryomov 	if (should_plug_request(req))
2139a02a946dSIlya Dryomov 		return;
2140a02a946dSIlya Dryomov 
21415aea3dcdSIlya Dryomov 	/*
21425aea3dcdSIlya Dryomov 	 * We may have a previously queued request message hanging
21435aea3dcdSIlya Dryomov 	 * around.  Cancel it to avoid corrupting the msgr.
21445aea3dcdSIlya Dryomov 	 */
21455aea3dcdSIlya Dryomov 	if (req->r_sent)
21465aea3dcdSIlya Dryomov 		ceph_msg_revoke(req->r_request);
21475aea3dcdSIlya Dryomov 
2148bb873b53SIlya Dryomov 	req->r_flags |= CEPH_OSD_FLAG_KNOWN_REDIR;
2149bb873b53SIlya Dryomov 	if (req->r_attempts)
2150bb873b53SIlya Dryomov 		req->r_flags |= CEPH_OSD_FLAG_RETRY;
2151bb873b53SIlya Dryomov 	else
2152bb873b53SIlya Dryomov 		WARN_ON(req->r_flags & CEPH_OSD_FLAG_RETRY);
2153bb873b53SIlya Dryomov 
21548cb441c0SIlya Dryomov 	encode_request_partial(req, req->r_request);
2155bb873b53SIlya Dryomov 
215604c7d789SIlya 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",
2157bb873b53SIlya Dryomov 	     __func__, req, req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed,
2158dc98ff72SIlya Dryomov 	     req->r_t.spgid.pgid.pool, req->r_t.spgid.pgid.seed,
215904c7d789SIlya Dryomov 	     req->r_t.spgid.shard, osd->o_osd, req->r_t.epoch, req->r_flags,
216004c7d789SIlya Dryomov 	     req->r_attempts);
2161bb873b53SIlya Dryomov 
2162bb873b53SIlya Dryomov 	req->r_t.paused = false;
21633d14c5d2SYehuda Sadeh 	req->r_stamp = jiffies;
2164bb873b53SIlya Dryomov 	req->r_attempts++;
21653d14c5d2SYehuda Sadeh 
2166bb873b53SIlya Dryomov 	req->r_sent = osd->o_incarnation;
2167bb873b53SIlya Dryomov 	req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
2168bb873b53SIlya Dryomov 	ceph_con_send(&osd->o_con, ceph_msg_get(req->r_request));
21693d14c5d2SYehuda Sadeh }
21703d14c5d2SYehuda Sadeh 
217142c1b124SIlya Dryomov static void maybe_request_map(struct ceph_osd_client *osdc)
217242c1b124SIlya Dryomov {
217342c1b124SIlya Dryomov 	bool continuous = false;
217442c1b124SIlya Dryomov 
21755aea3dcdSIlya Dryomov 	verify_osdc_locked(osdc);
217642c1b124SIlya Dryomov 	WARN_ON(!osdc->osdmap->epoch);
217742c1b124SIlya Dryomov 
2178b7ec35b3SIlya Dryomov 	if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2179b7ec35b3SIlya Dryomov 	    ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD) ||
2180b7ec35b3SIlya Dryomov 	    ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
218142c1b124SIlya Dryomov 		dout("%s osdc %p continuous\n", __func__, osdc);
218242c1b124SIlya Dryomov 		continuous = true;
218342c1b124SIlya Dryomov 	} else {
218442c1b124SIlya Dryomov 		dout("%s osdc %p onetime\n", __func__, osdc);
218542c1b124SIlya Dryomov 	}
218642c1b124SIlya Dryomov 
218742c1b124SIlya Dryomov 	if (ceph_monc_want_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
218842c1b124SIlya Dryomov 			       osdc->osdmap->epoch + 1, continuous))
218942c1b124SIlya Dryomov 		ceph_monc_renew_subs(&osdc->client->monc);
219042c1b124SIlya Dryomov }
219142c1b124SIlya Dryomov 
2192a1f4020aSJeff Layton static void complete_request(struct ceph_osd_request *req, int err);
21934609245eSIlya Dryomov static void send_map_check(struct ceph_osd_request *req);
21944609245eSIlya Dryomov 
21955aea3dcdSIlya Dryomov static void __submit_request(struct ceph_osd_request *req, bool wrlocked)
21960bbfdfe8SIlya Dryomov {
21975aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
21985aea3dcdSIlya Dryomov 	struct ceph_osd *osd;
21994609245eSIlya Dryomov 	enum calc_target_result ct_res;
220066850df5SIlya Dryomov 	int err = 0;
22015aea3dcdSIlya Dryomov 	bool need_send = false;
22025aea3dcdSIlya Dryomov 	bool promoted = false;
22030bbfdfe8SIlya Dryomov 
2204b18b9550SIlya Dryomov 	WARN_ON(req->r_tid);
22055aea3dcdSIlya Dryomov 	dout("%s req %p wrlocked %d\n", __func__, req, wrlocked);
22065aea3dcdSIlya Dryomov 
22075aea3dcdSIlya Dryomov again:
22087de030d6SIlya Dryomov 	ct_res = calc_target(osdc, &req->r_t, NULL, false);
22094609245eSIlya Dryomov 	if (ct_res == CALC_TARGET_POOL_DNE && !wrlocked)
22104609245eSIlya Dryomov 		goto promote;
22114609245eSIlya Dryomov 
22125aea3dcdSIlya Dryomov 	osd = lookup_create_osd(osdc, req->r_t.osd, wrlocked);
22135aea3dcdSIlya Dryomov 	if (IS_ERR(osd)) {
22145aea3dcdSIlya Dryomov 		WARN_ON(PTR_ERR(osd) != -EAGAIN || wrlocked);
22155aea3dcdSIlya Dryomov 		goto promote;
22165aea3dcdSIlya Dryomov 	}
22175aea3dcdSIlya Dryomov 
221866850df5SIlya Dryomov 	if (osdc->abort_err) {
221966850df5SIlya Dryomov 		dout("req %p abort_err %d\n", req, osdc->abort_err);
222066850df5SIlya Dryomov 		err = osdc->abort_err;
222166850df5SIlya Dryomov 	} else if (osdc->osdmap->epoch < osdc->epoch_barrier) {
222258eb7932SJeff Layton 		dout("req %p epoch %u barrier %u\n", req, osdc->osdmap->epoch,
222358eb7932SJeff Layton 		     osdc->epoch_barrier);
222458eb7932SJeff Layton 		req->r_t.paused = true;
222558eb7932SJeff Layton 		maybe_request_map(osdc);
222658eb7932SJeff Layton 	} else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2227b7ec35b3SIlya Dryomov 		   ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
22285aea3dcdSIlya Dryomov 		dout("req %p pausewr\n", req);
22295aea3dcdSIlya Dryomov 		req->r_t.paused = true;
22305aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
22315aea3dcdSIlya Dryomov 	} else if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
2232b7ec35b3SIlya Dryomov 		   ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
22335aea3dcdSIlya Dryomov 		dout("req %p pauserd\n", req);
22345aea3dcdSIlya Dryomov 		req->r_t.paused = true;
22355aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
22365aea3dcdSIlya Dryomov 	} else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
22375aea3dcdSIlya Dryomov 		   !(req->r_flags & (CEPH_OSD_FLAG_FULL_TRY |
22385aea3dcdSIlya Dryomov 				     CEPH_OSD_FLAG_FULL_FORCE)) &&
2239b7ec35b3SIlya Dryomov 		   (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
22405aea3dcdSIlya Dryomov 		    pool_full(osdc, req->r_t.base_oloc.pool))) {
22415aea3dcdSIlya Dryomov 		dout("req %p full/pool_full\n", req);
22425aea3dcdSIlya Dryomov 		pr_warn_ratelimited("FULL or reached pool quota\n");
22435aea3dcdSIlya Dryomov 		req->r_t.paused = true;
22445aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
2245a1f4020aSJeff Layton 		if (req->r_abort_on_full)
224666850df5SIlya Dryomov 			err = -ENOSPC;
22475aea3dcdSIlya Dryomov 	} else if (!osd_homeless(osd)) {
22485aea3dcdSIlya Dryomov 		need_send = true;
22490bbfdfe8SIlya Dryomov 	} else {
22505aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
22510bbfdfe8SIlya Dryomov 	}
22520bbfdfe8SIlya Dryomov 
22535aea3dcdSIlya Dryomov 	mutex_lock(&osd->lock);
22545aea3dcdSIlya Dryomov 	/*
22555aea3dcdSIlya Dryomov 	 * Assign the tid atomically with send_request() to protect
22565aea3dcdSIlya Dryomov 	 * multiple writes to the same object from racing with each
22575aea3dcdSIlya Dryomov 	 * other, resulting in out of order ops on the OSDs.
22585aea3dcdSIlya Dryomov 	 */
22595aea3dcdSIlya Dryomov 	req->r_tid = atomic64_inc_return(&osdc->last_tid);
22605aea3dcdSIlya Dryomov 	link_request(osd, req);
22615aea3dcdSIlya Dryomov 	if (need_send)
22625aea3dcdSIlya Dryomov 		send_request(req);
226366850df5SIlya Dryomov 	else if (err)
226466850df5SIlya Dryomov 		complete_request(req, err);
22655aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
22665aea3dcdSIlya Dryomov 
22674609245eSIlya Dryomov 	if (ct_res == CALC_TARGET_POOL_DNE)
22684609245eSIlya Dryomov 		send_map_check(req);
22694609245eSIlya Dryomov 
22705aea3dcdSIlya Dryomov 	if (promoted)
22715aea3dcdSIlya Dryomov 		downgrade_write(&osdc->lock);
22725aea3dcdSIlya Dryomov 	return;
22735aea3dcdSIlya Dryomov 
22745aea3dcdSIlya Dryomov promote:
22755aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
22765aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
22775aea3dcdSIlya Dryomov 	wrlocked = true;
22785aea3dcdSIlya Dryomov 	promoted = true;
22795aea3dcdSIlya Dryomov 	goto again;
22800bbfdfe8SIlya Dryomov }
22810bbfdfe8SIlya Dryomov 
22825aea3dcdSIlya Dryomov static void account_request(struct ceph_osd_request *req)
22835aea3dcdSIlya Dryomov {
228454ea0046SIlya Dryomov 	WARN_ON(req->r_flags & (CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK));
2285b18b9550SIlya Dryomov 	WARN_ON(!(req->r_flags & (CEPH_OSD_FLAG_READ | CEPH_OSD_FLAG_WRITE)));
22865aea3dcdSIlya Dryomov 
2287b18b9550SIlya Dryomov 	req->r_flags |= CEPH_OSD_FLAG_ONDISK;
22885aea3dcdSIlya Dryomov 	atomic_inc(&req->r_osdc->num_requests);
22897cc5e38fSIlya Dryomov 
22907cc5e38fSIlya Dryomov 	req->r_start_stamp = jiffies;
22915aea3dcdSIlya Dryomov }
22925aea3dcdSIlya Dryomov 
22935aea3dcdSIlya Dryomov static void submit_request(struct ceph_osd_request *req, bool wrlocked)
22945aea3dcdSIlya Dryomov {
22955aea3dcdSIlya Dryomov 	ceph_osdc_get_request(req);
22965aea3dcdSIlya Dryomov 	account_request(req);
22975aea3dcdSIlya Dryomov 	__submit_request(req, wrlocked);
22985aea3dcdSIlya Dryomov }
22995aea3dcdSIlya Dryomov 
230045ee2c1dSIlya Dryomov static void finish_request(struct ceph_osd_request *req)
23015aea3dcdSIlya Dryomov {
23025aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
23035aea3dcdSIlya Dryomov 
23044609245eSIlya Dryomov 	WARN_ON(lookup_request_mc(&osdc->map_checks, req->r_tid));
230504c7d789SIlya Dryomov 	dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
230604c7d789SIlya Dryomov 
230704c7d789SIlya Dryomov 	if (req->r_osd)
230804c7d789SIlya Dryomov 		unlink_request(req->r_osd, req);
23095aea3dcdSIlya Dryomov 	atomic_dec(&osdc->num_requests);
23105aea3dcdSIlya Dryomov 
23115aea3dcdSIlya Dryomov 	/*
23125aea3dcdSIlya Dryomov 	 * If an OSD has failed or returned and a request has been sent
23135aea3dcdSIlya Dryomov 	 * twice, it's possible to get a reply and end up here while the
23145aea3dcdSIlya Dryomov 	 * request message is queued for delivery.  We will ignore the
23155aea3dcdSIlya Dryomov 	 * reply, so not a big deal, but better to try and catch it.
23165aea3dcdSIlya Dryomov 	 */
23175aea3dcdSIlya Dryomov 	ceph_msg_revoke(req->r_request);
23185aea3dcdSIlya Dryomov 	ceph_msg_revoke_incoming(req->r_reply);
23195aea3dcdSIlya Dryomov }
23205aea3dcdSIlya Dryomov 
2321fe5da05eSIlya Dryomov static void __complete_request(struct ceph_osd_request *req)
2322fe5da05eSIlya Dryomov {
2323b18b9550SIlya Dryomov 	dout("%s req %p tid %llu cb %pf result %d\n", __func__, req,
2324b18b9550SIlya Dryomov 	     req->r_tid, req->r_callback, req->r_result);
232526df726bSIlya Dryomov 
232626df726bSIlya Dryomov 	if (req->r_callback)
2327fe5da05eSIlya Dryomov 		req->r_callback(req);
232826df726bSIlya Dryomov 	complete_all(&req->r_completion);
232926df726bSIlya Dryomov 	ceph_osdc_put_request(req);
2330fe5da05eSIlya Dryomov }
2331fe5da05eSIlya Dryomov 
233288bc1922SIlya Dryomov static void complete_request_workfn(struct work_struct *work)
233388bc1922SIlya Dryomov {
233488bc1922SIlya Dryomov 	struct ceph_osd_request *req =
233588bc1922SIlya Dryomov 	    container_of(work, struct ceph_osd_request, r_complete_work);
233688bc1922SIlya Dryomov 
233788bc1922SIlya Dryomov 	__complete_request(req);
233888bc1922SIlya Dryomov }
233988bc1922SIlya Dryomov 
23404609245eSIlya Dryomov /*
2341b18b9550SIlya Dryomov  * This is open-coded in handle_reply().
23424609245eSIlya Dryomov  */
23434609245eSIlya Dryomov static void complete_request(struct ceph_osd_request *req, int err)
23444609245eSIlya Dryomov {
23454609245eSIlya Dryomov 	dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
23464609245eSIlya Dryomov 
23474609245eSIlya Dryomov 	req->r_result = err;
234845ee2c1dSIlya Dryomov 	finish_request(req);
234988bc1922SIlya Dryomov 
235088bc1922SIlya Dryomov 	INIT_WORK(&req->r_complete_work, complete_request_workfn);
235188bc1922SIlya Dryomov 	queue_work(req->r_osdc->completion_wq, &req->r_complete_work);
23524609245eSIlya Dryomov }
23534609245eSIlya Dryomov 
23544609245eSIlya Dryomov static void cancel_map_check(struct ceph_osd_request *req)
23554609245eSIlya Dryomov {
23564609245eSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
23574609245eSIlya Dryomov 	struct ceph_osd_request *lookup_req;
23584609245eSIlya Dryomov 
23594609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
23604609245eSIlya Dryomov 
23614609245eSIlya Dryomov 	lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
23624609245eSIlya Dryomov 	if (!lookup_req)
23634609245eSIlya Dryomov 		return;
23644609245eSIlya Dryomov 
23654609245eSIlya Dryomov 	WARN_ON(lookup_req != req);
23664609245eSIlya Dryomov 	erase_request_mc(&osdc->map_checks, req);
23674609245eSIlya Dryomov 	ceph_osdc_put_request(req);
23684609245eSIlya Dryomov }
23694609245eSIlya Dryomov 
23705aea3dcdSIlya Dryomov static void cancel_request(struct ceph_osd_request *req)
23715aea3dcdSIlya Dryomov {
23725aea3dcdSIlya Dryomov 	dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
23735aea3dcdSIlya Dryomov 
23744609245eSIlya Dryomov 	cancel_map_check(req);
237545ee2c1dSIlya Dryomov 	finish_request(req);
2376b18b9550SIlya Dryomov 	complete_all(&req->r_completion);
2377c297eb42SIlya Dryomov 	ceph_osdc_put_request(req);
23785aea3dcdSIlya Dryomov }
23795aea3dcdSIlya Dryomov 
23807cc5e38fSIlya Dryomov static void abort_request(struct ceph_osd_request *req, int err)
23817cc5e38fSIlya Dryomov {
23827cc5e38fSIlya Dryomov 	dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
23837cc5e38fSIlya Dryomov 
23847cc5e38fSIlya Dryomov 	cancel_map_check(req);
23857cc5e38fSIlya Dryomov 	complete_request(req, err);
23867cc5e38fSIlya Dryomov }
23877cc5e38fSIlya Dryomov 
238866850df5SIlya Dryomov static int abort_fn(struct ceph_osd_request *req, void *arg)
238966850df5SIlya Dryomov {
239066850df5SIlya Dryomov 	int err = *(int *)arg;
239166850df5SIlya Dryomov 
239266850df5SIlya Dryomov 	abort_request(req, err);
239366850df5SIlya Dryomov 	return 0; /* continue iteration */
239466850df5SIlya Dryomov }
239566850df5SIlya Dryomov 
239666850df5SIlya Dryomov /*
239766850df5SIlya Dryomov  * Abort all in-flight requests with @err and arrange for all future
239866850df5SIlya Dryomov  * requests to be failed immediately.
239966850df5SIlya Dryomov  */
240066850df5SIlya Dryomov void ceph_osdc_abort_requests(struct ceph_osd_client *osdc, int err)
240166850df5SIlya Dryomov {
240266850df5SIlya Dryomov 	dout("%s osdc %p err %d\n", __func__, osdc, err);
240366850df5SIlya Dryomov 	down_write(&osdc->lock);
240466850df5SIlya Dryomov 	for_each_request(osdc, abort_fn, &err);
240566850df5SIlya Dryomov 	osdc->abort_err = err;
240666850df5SIlya Dryomov 	up_write(&osdc->lock);
240766850df5SIlya Dryomov }
240866850df5SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_abort_requests);
240966850df5SIlya Dryomov 
241058eb7932SJeff Layton static void update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
241158eb7932SJeff Layton {
241258eb7932SJeff Layton 	if (likely(eb > osdc->epoch_barrier)) {
241358eb7932SJeff Layton 		dout("updating epoch_barrier from %u to %u\n",
241458eb7932SJeff Layton 				osdc->epoch_barrier, eb);
241558eb7932SJeff Layton 		osdc->epoch_barrier = eb;
241658eb7932SJeff Layton 		/* Request map if we're not to the barrier yet */
241758eb7932SJeff Layton 		if (eb > osdc->osdmap->epoch)
241858eb7932SJeff Layton 			maybe_request_map(osdc);
241958eb7932SJeff Layton 	}
242058eb7932SJeff Layton }
242158eb7932SJeff Layton 
242258eb7932SJeff Layton void ceph_osdc_update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
242358eb7932SJeff Layton {
242458eb7932SJeff Layton 	down_read(&osdc->lock);
242558eb7932SJeff Layton 	if (unlikely(eb > osdc->epoch_barrier)) {
242658eb7932SJeff Layton 		up_read(&osdc->lock);
242758eb7932SJeff Layton 		down_write(&osdc->lock);
242858eb7932SJeff Layton 		update_epoch_barrier(osdc, eb);
242958eb7932SJeff Layton 		up_write(&osdc->lock);
243058eb7932SJeff Layton 	} else {
243158eb7932SJeff Layton 		up_read(&osdc->lock);
243258eb7932SJeff Layton 	}
243358eb7932SJeff Layton }
243458eb7932SJeff Layton EXPORT_SYMBOL(ceph_osdc_update_epoch_barrier);
243558eb7932SJeff Layton 
2436fc36d0a4SJeff Layton /*
2437fc36d0a4SJeff Layton  * Drop all pending requests that are stalled waiting on a full condition to
243858eb7932SJeff Layton  * clear, and complete them with ENOSPC as the return code. Set the
243958eb7932SJeff Layton  * osdc->epoch_barrier to the latest map epoch that we've seen if any were
244058eb7932SJeff Layton  * cancelled.
2441fc36d0a4SJeff Layton  */
2442fc36d0a4SJeff Layton static void ceph_osdc_abort_on_full(struct ceph_osd_client *osdc)
2443fc36d0a4SJeff Layton {
2444fc36d0a4SJeff Layton 	struct rb_node *n;
244558eb7932SJeff Layton 	bool victims = false;
2446fc36d0a4SJeff Layton 
2447fc36d0a4SJeff Layton 	dout("enter abort_on_full\n");
2448fc36d0a4SJeff Layton 
2449fc36d0a4SJeff Layton 	if (!ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) && !have_pool_full(osdc))
2450fc36d0a4SJeff Layton 		goto out;
2451fc36d0a4SJeff Layton 
245258eb7932SJeff Layton 	/* Scan list and see if there is anything to abort */
245358eb7932SJeff Layton 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
245458eb7932SJeff Layton 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
245558eb7932SJeff Layton 		struct rb_node *m;
245658eb7932SJeff Layton 
245758eb7932SJeff Layton 		m = rb_first(&osd->o_requests);
245858eb7932SJeff Layton 		while (m) {
245958eb7932SJeff Layton 			struct ceph_osd_request *req = rb_entry(m,
246058eb7932SJeff Layton 					struct ceph_osd_request, r_node);
246158eb7932SJeff Layton 			m = rb_next(m);
246258eb7932SJeff Layton 
246358eb7932SJeff Layton 			if (req->r_abort_on_full) {
246458eb7932SJeff Layton 				victims = true;
246558eb7932SJeff Layton 				break;
246658eb7932SJeff Layton 			}
246758eb7932SJeff Layton 		}
246858eb7932SJeff Layton 		if (victims)
246958eb7932SJeff Layton 			break;
247058eb7932SJeff Layton 	}
247158eb7932SJeff Layton 
247258eb7932SJeff Layton 	if (!victims)
247358eb7932SJeff Layton 		goto out;
247458eb7932SJeff Layton 
247558eb7932SJeff Layton 	/*
247658eb7932SJeff Layton 	 * Update the barrier to current epoch if it's behind that point,
247758eb7932SJeff Layton 	 * since we know we have some calls to be aborted in the tree.
247858eb7932SJeff Layton 	 */
247958eb7932SJeff Layton 	update_epoch_barrier(osdc, osdc->osdmap->epoch);
248058eb7932SJeff Layton 
2481fc36d0a4SJeff Layton 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
2482fc36d0a4SJeff Layton 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
2483fc36d0a4SJeff Layton 		struct rb_node *m;
2484fc36d0a4SJeff Layton 
2485fc36d0a4SJeff Layton 		m = rb_first(&osd->o_requests);
2486fc36d0a4SJeff Layton 		while (m) {
2487fc36d0a4SJeff Layton 			struct ceph_osd_request *req = rb_entry(m,
2488fc36d0a4SJeff Layton 					struct ceph_osd_request, r_node);
2489fc36d0a4SJeff Layton 			m = rb_next(m);
2490fc36d0a4SJeff Layton 
2491fc36d0a4SJeff Layton 			if (req->r_abort_on_full &&
2492fc36d0a4SJeff Layton 			    (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2493fc36d0a4SJeff Layton 			     pool_full(osdc, req->r_t.target_oloc.pool)))
2494fc36d0a4SJeff Layton 				abort_request(req, -ENOSPC);
2495fc36d0a4SJeff Layton 		}
2496fc36d0a4SJeff Layton 	}
2497fc36d0a4SJeff Layton out:
249858eb7932SJeff Layton 	dout("return abort_on_full barrier=%u\n", osdc->epoch_barrier);
2499fc36d0a4SJeff Layton }
2500fc36d0a4SJeff Layton 
25014609245eSIlya Dryomov static void check_pool_dne(struct ceph_osd_request *req)
25024609245eSIlya Dryomov {
25034609245eSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
25044609245eSIlya Dryomov 	struct ceph_osdmap *map = osdc->osdmap;
25054609245eSIlya Dryomov 
25064609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
25074609245eSIlya Dryomov 	WARN_ON(!map->epoch);
25084609245eSIlya Dryomov 
25094609245eSIlya Dryomov 	if (req->r_attempts) {
25104609245eSIlya Dryomov 		/*
25114609245eSIlya Dryomov 		 * We sent a request earlier, which means that
25124609245eSIlya Dryomov 		 * previously the pool existed, and now it does not
25134609245eSIlya Dryomov 		 * (i.e., it was deleted).
25144609245eSIlya Dryomov 		 */
25154609245eSIlya Dryomov 		req->r_map_dne_bound = map->epoch;
25164609245eSIlya Dryomov 		dout("%s req %p tid %llu pool disappeared\n", __func__, req,
25174609245eSIlya Dryomov 		     req->r_tid);
25184609245eSIlya Dryomov 	} else {
25194609245eSIlya Dryomov 		dout("%s req %p tid %llu map_dne_bound %u have %u\n", __func__,
25204609245eSIlya Dryomov 		     req, req->r_tid, req->r_map_dne_bound, map->epoch);
25214609245eSIlya Dryomov 	}
25224609245eSIlya Dryomov 
25234609245eSIlya Dryomov 	if (req->r_map_dne_bound) {
25244609245eSIlya Dryomov 		if (map->epoch >= req->r_map_dne_bound) {
25254609245eSIlya Dryomov 			/* we had a new enough map */
25264609245eSIlya Dryomov 			pr_info_ratelimited("tid %llu pool does not exist\n",
25274609245eSIlya Dryomov 					    req->r_tid);
25284609245eSIlya Dryomov 			complete_request(req, -ENOENT);
25294609245eSIlya Dryomov 		}
25304609245eSIlya Dryomov 	} else {
25314609245eSIlya Dryomov 		send_map_check(req);
25324609245eSIlya Dryomov 	}
25334609245eSIlya Dryomov }
25344609245eSIlya Dryomov 
25354609245eSIlya Dryomov static void map_check_cb(struct ceph_mon_generic_request *greq)
25364609245eSIlya Dryomov {
25374609245eSIlya Dryomov 	struct ceph_osd_client *osdc = &greq->monc->client->osdc;
25384609245eSIlya Dryomov 	struct ceph_osd_request *req;
25394609245eSIlya Dryomov 	u64 tid = greq->private_data;
25404609245eSIlya Dryomov 
25414609245eSIlya Dryomov 	WARN_ON(greq->result || !greq->u.newest);
25424609245eSIlya Dryomov 
25434609245eSIlya Dryomov 	down_write(&osdc->lock);
25444609245eSIlya Dryomov 	req = lookup_request_mc(&osdc->map_checks, tid);
25454609245eSIlya Dryomov 	if (!req) {
25464609245eSIlya Dryomov 		dout("%s tid %llu dne\n", __func__, tid);
25474609245eSIlya Dryomov 		goto out_unlock;
25484609245eSIlya Dryomov 	}
25494609245eSIlya Dryomov 
25504609245eSIlya Dryomov 	dout("%s req %p tid %llu map_dne_bound %u newest %llu\n", __func__,
25514609245eSIlya Dryomov 	     req, req->r_tid, req->r_map_dne_bound, greq->u.newest);
25524609245eSIlya Dryomov 	if (!req->r_map_dne_bound)
25534609245eSIlya Dryomov 		req->r_map_dne_bound = greq->u.newest;
25544609245eSIlya Dryomov 	erase_request_mc(&osdc->map_checks, req);
25554609245eSIlya Dryomov 	check_pool_dne(req);
25564609245eSIlya Dryomov 
25574609245eSIlya Dryomov 	ceph_osdc_put_request(req);
25584609245eSIlya Dryomov out_unlock:
25594609245eSIlya Dryomov 	up_write(&osdc->lock);
25604609245eSIlya Dryomov }
25614609245eSIlya Dryomov 
25624609245eSIlya Dryomov static void send_map_check(struct ceph_osd_request *req)
25634609245eSIlya Dryomov {
25644609245eSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
25654609245eSIlya Dryomov 	struct ceph_osd_request *lookup_req;
25664609245eSIlya Dryomov 	int ret;
25674609245eSIlya Dryomov 
25684609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
25694609245eSIlya Dryomov 
25704609245eSIlya Dryomov 	lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
25714609245eSIlya Dryomov 	if (lookup_req) {
25724609245eSIlya Dryomov 		WARN_ON(lookup_req != req);
25734609245eSIlya Dryomov 		return;
25744609245eSIlya Dryomov 	}
25754609245eSIlya Dryomov 
25764609245eSIlya Dryomov 	ceph_osdc_get_request(req);
25774609245eSIlya Dryomov 	insert_request_mc(&osdc->map_checks, req);
25784609245eSIlya Dryomov 	ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
25794609245eSIlya Dryomov 					  map_check_cb, req->r_tid);
25804609245eSIlya Dryomov 	WARN_ON(ret);
25814609245eSIlya Dryomov }
25824609245eSIlya Dryomov 
25830bbfdfe8SIlya Dryomov /*
2584922dab61SIlya Dryomov  * lingering requests, watch/notify v2 infrastructure
2585922dab61SIlya Dryomov  */
2586922dab61SIlya Dryomov static void linger_release(struct kref *kref)
2587922dab61SIlya Dryomov {
2588922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq =
2589922dab61SIlya Dryomov 	    container_of(kref, struct ceph_osd_linger_request, kref);
2590922dab61SIlya Dryomov 
2591922dab61SIlya Dryomov 	dout("%s lreq %p reg_req %p ping_req %p\n", __func__, lreq,
2592922dab61SIlya Dryomov 	     lreq->reg_req, lreq->ping_req);
2593922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&lreq->node));
2594922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&lreq->osdc_node));
25954609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&lreq->mc_node));
2596922dab61SIlya Dryomov 	WARN_ON(!list_empty(&lreq->scan_item));
2597b07d3c4bSIlya Dryomov 	WARN_ON(!list_empty(&lreq->pending_lworks));
2598922dab61SIlya Dryomov 	WARN_ON(lreq->osd);
2599922dab61SIlya Dryomov 
2600922dab61SIlya Dryomov 	if (lreq->reg_req)
2601922dab61SIlya Dryomov 		ceph_osdc_put_request(lreq->reg_req);
2602922dab61SIlya Dryomov 	if (lreq->ping_req)
2603922dab61SIlya Dryomov 		ceph_osdc_put_request(lreq->ping_req);
2604922dab61SIlya Dryomov 	target_destroy(&lreq->t);
2605922dab61SIlya Dryomov 	kfree(lreq);
2606922dab61SIlya Dryomov }
2607922dab61SIlya Dryomov 
2608922dab61SIlya Dryomov static void linger_put(struct ceph_osd_linger_request *lreq)
2609922dab61SIlya Dryomov {
2610922dab61SIlya Dryomov 	if (lreq)
2611922dab61SIlya Dryomov 		kref_put(&lreq->kref, linger_release);
2612922dab61SIlya Dryomov }
2613922dab61SIlya Dryomov 
2614922dab61SIlya Dryomov static struct ceph_osd_linger_request *
2615922dab61SIlya Dryomov linger_get(struct ceph_osd_linger_request *lreq)
2616922dab61SIlya Dryomov {
2617922dab61SIlya Dryomov 	kref_get(&lreq->kref);
2618922dab61SIlya Dryomov 	return lreq;
2619922dab61SIlya Dryomov }
2620922dab61SIlya Dryomov 
2621922dab61SIlya Dryomov static struct ceph_osd_linger_request *
2622922dab61SIlya Dryomov linger_alloc(struct ceph_osd_client *osdc)
2623922dab61SIlya Dryomov {
2624922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
2625922dab61SIlya Dryomov 
2626922dab61SIlya Dryomov 	lreq = kzalloc(sizeof(*lreq), GFP_NOIO);
2627922dab61SIlya Dryomov 	if (!lreq)
2628922dab61SIlya Dryomov 		return NULL;
2629922dab61SIlya Dryomov 
2630922dab61SIlya Dryomov 	kref_init(&lreq->kref);
2631922dab61SIlya Dryomov 	mutex_init(&lreq->lock);
2632922dab61SIlya Dryomov 	RB_CLEAR_NODE(&lreq->node);
2633922dab61SIlya Dryomov 	RB_CLEAR_NODE(&lreq->osdc_node);
26344609245eSIlya Dryomov 	RB_CLEAR_NODE(&lreq->mc_node);
2635922dab61SIlya Dryomov 	INIT_LIST_HEAD(&lreq->scan_item);
2636b07d3c4bSIlya Dryomov 	INIT_LIST_HEAD(&lreq->pending_lworks);
2637922dab61SIlya Dryomov 	init_completion(&lreq->reg_commit_wait);
263819079203SIlya Dryomov 	init_completion(&lreq->notify_finish_wait);
2639922dab61SIlya Dryomov 
2640922dab61SIlya Dryomov 	lreq->osdc = osdc;
2641922dab61SIlya Dryomov 	target_init(&lreq->t);
2642922dab61SIlya Dryomov 
2643922dab61SIlya Dryomov 	dout("%s lreq %p\n", __func__, lreq);
2644922dab61SIlya Dryomov 	return lreq;
2645922dab61SIlya Dryomov }
2646922dab61SIlya Dryomov 
2647922dab61SIlya Dryomov DEFINE_RB_INSDEL_FUNCS(linger, struct ceph_osd_linger_request, linger_id, node)
2648922dab61SIlya Dryomov DEFINE_RB_FUNCS(linger_osdc, struct ceph_osd_linger_request, linger_id, osdc_node)
26494609245eSIlya Dryomov DEFINE_RB_FUNCS(linger_mc, struct ceph_osd_linger_request, linger_id, mc_node)
2650922dab61SIlya Dryomov 
2651922dab61SIlya Dryomov /*
2652922dab61SIlya Dryomov  * Create linger request <-> OSD session relation.
2653922dab61SIlya Dryomov  *
2654922dab61SIlya Dryomov  * @lreq has to be registered, @osd may be homeless.
2655922dab61SIlya Dryomov  */
2656922dab61SIlya Dryomov static void link_linger(struct ceph_osd *osd,
2657922dab61SIlya Dryomov 			struct ceph_osd_linger_request *lreq)
2658922dab61SIlya Dryomov {
2659922dab61SIlya Dryomov 	verify_osd_locked(osd);
2660922dab61SIlya Dryomov 	WARN_ON(!lreq->linger_id || lreq->osd);
2661922dab61SIlya Dryomov 	dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2662922dab61SIlya Dryomov 	     osd->o_osd, lreq, lreq->linger_id);
2663922dab61SIlya Dryomov 
2664922dab61SIlya Dryomov 	if (!osd_homeless(osd))
2665922dab61SIlya Dryomov 		__remove_osd_from_lru(osd);
2666922dab61SIlya Dryomov 	else
2667922dab61SIlya Dryomov 		atomic_inc(&osd->o_osdc->num_homeless);
2668922dab61SIlya Dryomov 
2669922dab61SIlya Dryomov 	get_osd(osd);
2670922dab61SIlya Dryomov 	insert_linger(&osd->o_linger_requests, lreq);
2671922dab61SIlya Dryomov 	lreq->osd = osd;
2672922dab61SIlya Dryomov }
2673922dab61SIlya Dryomov 
2674922dab61SIlya Dryomov static void unlink_linger(struct ceph_osd *osd,
2675922dab61SIlya Dryomov 			  struct ceph_osd_linger_request *lreq)
2676922dab61SIlya Dryomov {
2677922dab61SIlya Dryomov 	verify_osd_locked(osd);
2678922dab61SIlya Dryomov 	WARN_ON(lreq->osd != osd);
2679922dab61SIlya Dryomov 	dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2680922dab61SIlya Dryomov 	     osd->o_osd, lreq, lreq->linger_id);
2681922dab61SIlya Dryomov 
2682922dab61SIlya Dryomov 	lreq->osd = NULL;
2683922dab61SIlya Dryomov 	erase_linger(&osd->o_linger_requests, lreq);
2684922dab61SIlya Dryomov 	put_osd(osd);
2685922dab61SIlya Dryomov 
2686922dab61SIlya Dryomov 	if (!osd_homeless(osd))
2687922dab61SIlya Dryomov 		maybe_move_osd_to_lru(osd);
2688922dab61SIlya Dryomov 	else
2689922dab61SIlya Dryomov 		atomic_dec(&osd->o_osdc->num_homeless);
2690922dab61SIlya Dryomov }
2691922dab61SIlya Dryomov 
2692922dab61SIlya Dryomov static bool __linger_registered(struct ceph_osd_linger_request *lreq)
2693922dab61SIlya Dryomov {
2694922dab61SIlya Dryomov 	verify_osdc_locked(lreq->osdc);
2695922dab61SIlya Dryomov 
2696922dab61SIlya Dryomov 	return !RB_EMPTY_NODE(&lreq->osdc_node);
2697922dab61SIlya Dryomov }
2698922dab61SIlya Dryomov 
2699922dab61SIlya Dryomov static bool linger_registered(struct ceph_osd_linger_request *lreq)
2700922dab61SIlya Dryomov {
2701922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2702922dab61SIlya Dryomov 	bool registered;
2703922dab61SIlya Dryomov 
2704922dab61SIlya Dryomov 	down_read(&osdc->lock);
2705922dab61SIlya Dryomov 	registered = __linger_registered(lreq);
2706922dab61SIlya Dryomov 	up_read(&osdc->lock);
2707922dab61SIlya Dryomov 
2708922dab61SIlya Dryomov 	return registered;
2709922dab61SIlya Dryomov }
2710922dab61SIlya Dryomov 
2711922dab61SIlya Dryomov static void linger_register(struct ceph_osd_linger_request *lreq)
2712922dab61SIlya Dryomov {
2713922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2714922dab61SIlya Dryomov 
2715922dab61SIlya Dryomov 	verify_osdc_wrlocked(osdc);
2716922dab61SIlya Dryomov 	WARN_ON(lreq->linger_id);
2717922dab61SIlya Dryomov 
2718922dab61SIlya Dryomov 	linger_get(lreq);
2719922dab61SIlya Dryomov 	lreq->linger_id = ++osdc->last_linger_id;
2720922dab61SIlya Dryomov 	insert_linger_osdc(&osdc->linger_requests, lreq);
2721922dab61SIlya Dryomov }
2722922dab61SIlya Dryomov 
2723922dab61SIlya Dryomov static void linger_unregister(struct ceph_osd_linger_request *lreq)
2724922dab61SIlya Dryomov {
2725922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2726922dab61SIlya Dryomov 
2727922dab61SIlya Dryomov 	verify_osdc_wrlocked(osdc);
2728922dab61SIlya Dryomov 
2729922dab61SIlya Dryomov 	erase_linger_osdc(&osdc->linger_requests, lreq);
2730922dab61SIlya Dryomov 	linger_put(lreq);
2731922dab61SIlya Dryomov }
2732922dab61SIlya Dryomov 
2733922dab61SIlya Dryomov static void cancel_linger_request(struct ceph_osd_request *req)
2734922dab61SIlya Dryomov {
2735922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
2736922dab61SIlya Dryomov 
2737922dab61SIlya Dryomov 	WARN_ON(!req->r_linger);
2738922dab61SIlya Dryomov 	cancel_request(req);
2739922dab61SIlya Dryomov 	linger_put(lreq);
2740922dab61SIlya Dryomov }
2741922dab61SIlya Dryomov 
2742922dab61SIlya Dryomov struct linger_work {
2743922dab61SIlya Dryomov 	struct work_struct work;
2744922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
2745b07d3c4bSIlya Dryomov 	struct list_head pending_item;
2746b07d3c4bSIlya Dryomov 	unsigned long queued_stamp;
2747922dab61SIlya Dryomov 
2748922dab61SIlya Dryomov 	union {
2749922dab61SIlya Dryomov 		struct {
2750922dab61SIlya Dryomov 			u64 notify_id;
2751922dab61SIlya Dryomov 			u64 notifier_id;
2752922dab61SIlya Dryomov 			void *payload; /* points into @msg front */
2753922dab61SIlya Dryomov 			size_t payload_len;
2754922dab61SIlya Dryomov 
2755922dab61SIlya Dryomov 			struct ceph_msg *msg; /* for ceph_msg_put() */
2756922dab61SIlya Dryomov 		} notify;
2757922dab61SIlya Dryomov 		struct {
2758922dab61SIlya Dryomov 			int err;
2759922dab61SIlya Dryomov 		} error;
2760922dab61SIlya Dryomov 	};
2761922dab61SIlya Dryomov };
2762922dab61SIlya Dryomov 
2763922dab61SIlya Dryomov static struct linger_work *lwork_alloc(struct ceph_osd_linger_request *lreq,
2764922dab61SIlya Dryomov 				       work_func_t workfn)
2765922dab61SIlya Dryomov {
2766922dab61SIlya Dryomov 	struct linger_work *lwork;
2767922dab61SIlya Dryomov 
2768922dab61SIlya Dryomov 	lwork = kzalloc(sizeof(*lwork), GFP_NOIO);
2769922dab61SIlya Dryomov 	if (!lwork)
2770922dab61SIlya Dryomov 		return NULL;
2771922dab61SIlya Dryomov 
2772922dab61SIlya Dryomov 	INIT_WORK(&lwork->work, workfn);
2773b07d3c4bSIlya Dryomov 	INIT_LIST_HEAD(&lwork->pending_item);
2774922dab61SIlya Dryomov 	lwork->lreq = linger_get(lreq);
2775922dab61SIlya Dryomov 
2776922dab61SIlya Dryomov 	return lwork;
2777922dab61SIlya Dryomov }
2778922dab61SIlya Dryomov 
2779922dab61SIlya Dryomov static void lwork_free(struct linger_work *lwork)
2780922dab61SIlya Dryomov {
2781922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2782922dab61SIlya Dryomov 
2783b07d3c4bSIlya Dryomov 	mutex_lock(&lreq->lock);
2784b07d3c4bSIlya Dryomov 	list_del(&lwork->pending_item);
2785b07d3c4bSIlya Dryomov 	mutex_unlock(&lreq->lock);
2786b07d3c4bSIlya Dryomov 
2787922dab61SIlya Dryomov 	linger_put(lreq);
2788922dab61SIlya Dryomov 	kfree(lwork);
2789922dab61SIlya Dryomov }
2790922dab61SIlya Dryomov 
2791922dab61SIlya Dryomov static void lwork_queue(struct linger_work *lwork)
2792922dab61SIlya Dryomov {
2793922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2794922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2795922dab61SIlya Dryomov 
2796922dab61SIlya Dryomov 	verify_lreq_locked(lreq);
2797b07d3c4bSIlya Dryomov 	WARN_ON(!list_empty(&lwork->pending_item));
2798b07d3c4bSIlya Dryomov 
2799b07d3c4bSIlya Dryomov 	lwork->queued_stamp = jiffies;
2800b07d3c4bSIlya Dryomov 	list_add_tail(&lwork->pending_item, &lreq->pending_lworks);
2801922dab61SIlya Dryomov 	queue_work(osdc->notify_wq, &lwork->work);
2802922dab61SIlya Dryomov }
2803922dab61SIlya Dryomov 
2804922dab61SIlya Dryomov static void do_watch_notify(struct work_struct *w)
2805922dab61SIlya Dryomov {
2806922dab61SIlya Dryomov 	struct linger_work *lwork = container_of(w, struct linger_work, work);
2807922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2808922dab61SIlya Dryomov 
2809922dab61SIlya Dryomov 	if (!linger_registered(lreq)) {
2810922dab61SIlya Dryomov 		dout("%s lreq %p not registered\n", __func__, lreq);
2811922dab61SIlya Dryomov 		goto out;
2812922dab61SIlya Dryomov 	}
2813922dab61SIlya Dryomov 
281419079203SIlya Dryomov 	WARN_ON(!lreq->is_watch);
2815922dab61SIlya Dryomov 	dout("%s lreq %p notify_id %llu notifier_id %llu payload_len %zu\n",
2816922dab61SIlya Dryomov 	     __func__, lreq, lwork->notify.notify_id, lwork->notify.notifier_id,
2817922dab61SIlya Dryomov 	     lwork->notify.payload_len);
2818922dab61SIlya Dryomov 	lreq->wcb(lreq->data, lwork->notify.notify_id, lreq->linger_id,
2819922dab61SIlya Dryomov 		  lwork->notify.notifier_id, lwork->notify.payload,
2820922dab61SIlya Dryomov 		  lwork->notify.payload_len);
2821922dab61SIlya Dryomov 
2822922dab61SIlya Dryomov out:
2823922dab61SIlya Dryomov 	ceph_msg_put(lwork->notify.msg);
2824922dab61SIlya Dryomov 	lwork_free(lwork);
2825922dab61SIlya Dryomov }
2826922dab61SIlya Dryomov 
2827922dab61SIlya Dryomov static void do_watch_error(struct work_struct *w)
2828922dab61SIlya Dryomov {
2829922dab61SIlya Dryomov 	struct linger_work *lwork = container_of(w, struct linger_work, work);
2830922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2831922dab61SIlya Dryomov 
2832922dab61SIlya Dryomov 	if (!linger_registered(lreq)) {
2833922dab61SIlya Dryomov 		dout("%s lreq %p not registered\n", __func__, lreq);
2834922dab61SIlya Dryomov 		goto out;
2835922dab61SIlya Dryomov 	}
2836922dab61SIlya Dryomov 
2837922dab61SIlya Dryomov 	dout("%s lreq %p err %d\n", __func__, lreq, lwork->error.err);
2838922dab61SIlya Dryomov 	lreq->errcb(lreq->data, lreq->linger_id, lwork->error.err);
2839922dab61SIlya Dryomov 
2840922dab61SIlya Dryomov out:
2841922dab61SIlya Dryomov 	lwork_free(lwork);
2842922dab61SIlya Dryomov }
2843922dab61SIlya Dryomov 
2844922dab61SIlya Dryomov static void queue_watch_error(struct ceph_osd_linger_request *lreq)
2845922dab61SIlya Dryomov {
2846922dab61SIlya Dryomov 	struct linger_work *lwork;
2847922dab61SIlya Dryomov 
2848922dab61SIlya Dryomov 	lwork = lwork_alloc(lreq, do_watch_error);
2849922dab61SIlya Dryomov 	if (!lwork) {
2850922dab61SIlya Dryomov 		pr_err("failed to allocate error-lwork\n");
2851922dab61SIlya Dryomov 		return;
2852922dab61SIlya Dryomov 	}
2853922dab61SIlya Dryomov 
2854922dab61SIlya Dryomov 	lwork->error.err = lreq->last_error;
2855922dab61SIlya Dryomov 	lwork_queue(lwork);
2856922dab61SIlya Dryomov }
2857922dab61SIlya Dryomov 
2858922dab61SIlya Dryomov static void linger_reg_commit_complete(struct ceph_osd_linger_request *lreq,
2859922dab61SIlya Dryomov 				       int result)
2860922dab61SIlya Dryomov {
2861922dab61SIlya Dryomov 	if (!completion_done(&lreq->reg_commit_wait)) {
2862922dab61SIlya Dryomov 		lreq->reg_commit_error = (result <= 0 ? result : 0);
2863922dab61SIlya Dryomov 		complete_all(&lreq->reg_commit_wait);
2864922dab61SIlya Dryomov 	}
2865922dab61SIlya Dryomov }
2866922dab61SIlya Dryomov 
2867922dab61SIlya Dryomov static void linger_commit_cb(struct ceph_osd_request *req)
2868922dab61SIlya Dryomov {
2869922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
2870922dab61SIlya Dryomov 
2871922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
2872922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu result %d\n", __func__, lreq,
2873922dab61SIlya Dryomov 	     lreq->linger_id, req->r_result);
2874922dab61SIlya Dryomov 	linger_reg_commit_complete(lreq, req->r_result);
2875922dab61SIlya Dryomov 	lreq->committed = true;
2876922dab61SIlya Dryomov 
287719079203SIlya Dryomov 	if (!lreq->is_watch) {
287819079203SIlya Dryomov 		struct ceph_osd_data *osd_data =
287919079203SIlya Dryomov 		    osd_req_op_data(req, 0, notify, response_data);
288019079203SIlya Dryomov 		void *p = page_address(osd_data->pages[0]);
288119079203SIlya Dryomov 
288219079203SIlya Dryomov 		WARN_ON(req->r_ops[0].op != CEPH_OSD_OP_NOTIFY ||
288319079203SIlya Dryomov 			osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
288419079203SIlya Dryomov 
288519079203SIlya Dryomov 		/* make note of the notify_id */
288619079203SIlya Dryomov 		if (req->r_ops[0].outdata_len >= sizeof(u64)) {
288719079203SIlya Dryomov 			lreq->notify_id = ceph_decode_64(&p);
288819079203SIlya Dryomov 			dout("lreq %p notify_id %llu\n", lreq,
288919079203SIlya Dryomov 			     lreq->notify_id);
289019079203SIlya Dryomov 		} else {
289119079203SIlya Dryomov 			dout("lreq %p no notify_id\n", lreq);
289219079203SIlya Dryomov 		}
289319079203SIlya Dryomov 	}
289419079203SIlya Dryomov 
2895922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
2896922dab61SIlya Dryomov 	linger_put(lreq);
2897922dab61SIlya Dryomov }
2898922dab61SIlya Dryomov 
2899922dab61SIlya Dryomov static int normalize_watch_error(int err)
2900922dab61SIlya Dryomov {
2901922dab61SIlya Dryomov 	/*
2902922dab61SIlya Dryomov 	 * Translate ENOENT -> ENOTCONN so that a delete->disconnection
2903922dab61SIlya Dryomov 	 * notification and a failure to reconnect because we raced with
2904922dab61SIlya Dryomov 	 * the delete appear the same to the user.
2905922dab61SIlya Dryomov 	 */
2906922dab61SIlya Dryomov 	if (err == -ENOENT)
2907922dab61SIlya Dryomov 		err = -ENOTCONN;
2908922dab61SIlya Dryomov 
2909922dab61SIlya Dryomov 	return err;
2910922dab61SIlya Dryomov }
2911922dab61SIlya Dryomov 
2912922dab61SIlya Dryomov static void linger_reconnect_cb(struct ceph_osd_request *req)
2913922dab61SIlya Dryomov {
2914922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
2915922dab61SIlya Dryomov 
2916922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
2917922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu result %d last_error %d\n", __func__,
2918922dab61SIlya Dryomov 	     lreq, lreq->linger_id, req->r_result, lreq->last_error);
2919922dab61SIlya Dryomov 	if (req->r_result < 0) {
2920922dab61SIlya Dryomov 		if (!lreq->last_error) {
2921922dab61SIlya Dryomov 			lreq->last_error = normalize_watch_error(req->r_result);
2922922dab61SIlya Dryomov 			queue_watch_error(lreq);
2923922dab61SIlya Dryomov 		}
2924922dab61SIlya Dryomov 	}
2925922dab61SIlya Dryomov 
2926922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
2927922dab61SIlya Dryomov 	linger_put(lreq);
2928922dab61SIlya Dryomov }
2929922dab61SIlya Dryomov 
2930922dab61SIlya Dryomov static void send_linger(struct ceph_osd_linger_request *lreq)
2931922dab61SIlya Dryomov {
2932922dab61SIlya Dryomov 	struct ceph_osd_request *req = lreq->reg_req;
2933922dab61SIlya Dryomov 	struct ceph_osd_req_op *op = &req->r_ops[0];
2934922dab61SIlya Dryomov 
2935922dab61SIlya Dryomov 	verify_osdc_wrlocked(req->r_osdc);
2936922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2937922dab61SIlya Dryomov 
2938922dab61SIlya Dryomov 	if (req->r_osd)
2939922dab61SIlya Dryomov 		cancel_linger_request(req);
2940922dab61SIlya Dryomov 
2941922dab61SIlya Dryomov 	request_reinit(req);
2942922dab61SIlya Dryomov 	ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
2943922dab61SIlya Dryomov 	ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
2944922dab61SIlya Dryomov 	req->r_flags = lreq->t.flags;
2945922dab61SIlya Dryomov 	req->r_mtime = lreq->mtime;
2946922dab61SIlya Dryomov 
2947922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
294819079203SIlya Dryomov 	if (lreq->is_watch && lreq->committed) {
2949922dab61SIlya Dryomov 		WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2950922dab61SIlya Dryomov 			op->watch.cookie != lreq->linger_id);
2951922dab61SIlya Dryomov 		op->watch.op = CEPH_OSD_WATCH_OP_RECONNECT;
2952922dab61SIlya Dryomov 		op->watch.gen = ++lreq->register_gen;
2953922dab61SIlya Dryomov 		dout("lreq %p reconnect register_gen %u\n", lreq,
2954922dab61SIlya Dryomov 		     op->watch.gen);
2955922dab61SIlya Dryomov 		req->r_callback = linger_reconnect_cb;
2956922dab61SIlya Dryomov 	} else {
295719079203SIlya Dryomov 		if (!lreq->is_watch)
295819079203SIlya Dryomov 			lreq->notify_id = 0;
295919079203SIlya Dryomov 		else
2960922dab61SIlya Dryomov 			WARN_ON(op->watch.op != CEPH_OSD_WATCH_OP_WATCH);
2961922dab61SIlya Dryomov 		dout("lreq %p register\n", lreq);
2962922dab61SIlya Dryomov 		req->r_callback = linger_commit_cb;
2963922dab61SIlya Dryomov 	}
2964922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
2965922dab61SIlya Dryomov 
2966922dab61SIlya Dryomov 	req->r_priv = linger_get(lreq);
2967922dab61SIlya Dryomov 	req->r_linger = true;
2968922dab61SIlya Dryomov 
2969922dab61SIlya Dryomov 	submit_request(req, true);
2970922dab61SIlya Dryomov }
2971922dab61SIlya Dryomov 
2972922dab61SIlya Dryomov static void linger_ping_cb(struct ceph_osd_request *req)
2973922dab61SIlya Dryomov {
2974922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
2975922dab61SIlya Dryomov 
2976922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
2977922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu result %d ping_sent %lu last_error %d\n",
2978922dab61SIlya Dryomov 	     __func__, lreq, lreq->linger_id, req->r_result, lreq->ping_sent,
2979922dab61SIlya Dryomov 	     lreq->last_error);
2980922dab61SIlya Dryomov 	if (lreq->register_gen == req->r_ops[0].watch.gen) {
2981b07d3c4bSIlya Dryomov 		if (!req->r_result) {
2982b07d3c4bSIlya Dryomov 			lreq->watch_valid_thru = lreq->ping_sent;
2983b07d3c4bSIlya Dryomov 		} else if (!lreq->last_error) {
2984922dab61SIlya Dryomov 			lreq->last_error = normalize_watch_error(req->r_result);
2985922dab61SIlya Dryomov 			queue_watch_error(lreq);
2986922dab61SIlya Dryomov 		}
2987922dab61SIlya Dryomov 	} else {
2988922dab61SIlya Dryomov 		dout("lreq %p register_gen %u ignoring old pong %u\n", lreq,
2989922dab61SIlya Dryomov 		     lreq->register_gen, req->r_ops[0].watch.gen);
2990922dab61SIlya Dryomov 	}
2991922dab61SIlya Dryomov 
2992922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
2993922dab61SIlya Dryomov 	linger_put(lreq);
2994922dab61SIlya Dryomov }
2995922dab61SIlya Dryomov 
2996922dab61SIlya Dryomov static void send_linger_ping(struct ceph_osd_linger_request *lreq)
2997922dab61SIlya Dryomov {
2998922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2999922dab61SIlya Dryomov 	struct ceph_osd_request *req = lreq->ping_req;
3000922dab61SIlya Dryomov 	struct ceph_osd_req_op *op = &req->r_ops[0];
3001922dab61SIlya Dryomov 
3002b7ec35b3SIlya Dryomov 	if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
3003922dab61SIlya Dryomov 		dout("%s PAUSERD\n", __func__);
3004922dab61SIlya Dryomov 		return;
3005922dab61SIlya Dryomov 	}
3006922dab61SIlya Dryomov 
3007922dab61SIlya Dryomov 	lreq->ping_sent = jiffies;
3008922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu ping_sent %lu register_gen %u\n",
3009922dab61SIlya Dryomov 	     __func__, lreq, lreq->linger_id, lreq->ping_sent,
3010922dab61SIlya Dryomov 	     lreq->register_gen);
3011922dab61SIlya Dryomov 
3012922dab61SIlya Dryomov 	if (req->r_osd)
3013922dab61SIlya Dryomov 		cancel_linger_request(req);
3014922dab61SIlya Dryomov 
3015922dab61SIlya Dryomov 	request_reinit(req);
3016922dab61SIlya Dryomov 	target_copy(&req->r_t, &lreq->t);
3017922dab61SIlya Dryomov 
3018922dab61SIlya Dryomov 	WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
3019922dab61SIlya Dryomov 		op->watch.cookie != lreq->linger_id ||
3020922dab61SIlya Dryomov 		op->watch.op != CEPH_OSD_WATCH_OP_PING);
3021922dab61SIlya Dryomov 	op->watch.gen = lreq->register_gen;
3022922dab61SIlya Dryomov 	req->r_callback = linger_ping_cb;
3023922dab61SIlya Dryomov 	req->r_priv = linger_get(lreq);
3024922dab61SIlya Dryomov 	req->r_linger = true;
3025922dab61SIlya Dryomov 
3026922dab61SIlya Dryomov 	ceph_osdc_get_request(req);
3027922dab61SIlya Dryomov 	account_request(req);
3028922dab61SIlya Dryomov 	req->r_tid = atomic64_inc_return(&osdc->last_tid);
3029922dab61SIlya Dryomov 	link_request(lreq->osd, req);
3030922dab61SIlya Dryomov 	send_request(req);
3031922dab61SIlya Dryomov }
3032922dab61SIlya Dryomov 
3033922dab61SIlya Dryomov static void linger_submit(struct ceph_osd_linger_request *lreq)
3034922dab61SIlya Dryomov {
3035922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
3036922dab61SIlya Dryomov 	struct ceph_osd *osd;
3037922dab61SIlya Dryomov 
30387de030d6SIlya Dryomov 	calc_target(osdc, &lreq->t, NULL, false);
3039922dab61SIlya Dryomov 	osd = lookup_create_osd(osdc, lreq->t.osd, true);
3040922dab61SIlya Dryomov 	link_linger(osd, lreq);
3041922dab61SIlya Dryomov 
3042922dab61SIlya Dryomov 	send_linger(lreq);
3043922dab61SIlya Dryomov }
3044922dab61SIlya Dryomov 
30454609245eSIlya Dryomov static void cancel_linger_map_check(struct ceph_osd_linger_request *lreq)
30464609245eSIlya Dryomov {
30474609245eSIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
30484609245eSIlya Dryomov 	struct ceph_osd_linger_request *lookup_lreq;
30494609245eSIlya Dryomov 
30504609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
30514609245eSIlya Dryomov 
30524609245eSIlya Dryomov 	lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
30534609245eSIlya Dryomov 				       lreq->linger_id);
30544609245eSIlya Dryomov 	if (!lookup_lreq)
30554609245eSIlya Dryomov 		return;
30564609245eSIlya Dryomov 
30574609245eSIlya Dryomov 	WARN_ON(lookup_lreq != lreq);
30584609245eSIlya Dryomov 	erase_linger_mc(&osdc->linger_map_checks, lreq);
30594609245eSIlya Dryomov 	linger_put(lreq);
30604609245eSIlya Dryomov }
30614609245eSIlya Dryomov 
3062922dab61SIlya Dryomov /*
3063922dab61SIlya Dryomov  * @lreq has to be both registered and linked.
3064922dab61SIlya Dryomov  */
3065922dab61SIlya Dryomov static void __linger_cancel(struct ceph_osd_linger_request *lreq)
3066922dab61SIlya Dryomov {
306719079203SIlya Dryomov 	if (lreq->is_watch && lreq->ping_req->r_osd)
3068922dab61SIlya Dryomov 		cancel_linger_request(lreq->ping_req);
3069922dab61SIlya Dryomov 	if (lreq->reg_req->r_osd)
3070922dab61SIlya Dryomov 		cancel_linger_request(lreq->reg_req);
30714609245eSIlya Dryomov 	cancel_linger_map_check(lreq);
3072922dab61SIlya Dryomov 	unlink_linger(lreq->osd, lreq);
3073922dab61SIlya Dryomov 	linger_unregister(lreq);
3074922dab61SIlya Dryomov }
3075922dab61SIlya Dryomov 
3076922dab61SIlya Dryomov static void linger_cancel(struct ceph_osd_linger_request *lreq)
3077922dab61SIlya Dryomov {
3078922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
3079922dab61SIlya Dryomov 
3080922dab61SIlya Dryomov 	down_write(&osdc->lock);
3081922dab61SIlya Dryomov 	if (__linger_registered(lreq))
3082922dab61SIlya Dryomov 		__linger_cancel(lreq);
3083922dab61SIlya Dryomov 	up_write(&osdc->lock);
3084922dab61SIlya Dryomov }
3085922dab61SIlya Dryomov 
30864609245eSIlya Dryomov static void send_linger_map_check(struct ceph_osd_linger_request *lreq);
30874609245eSIlya Dryomov 
30884609245eSIlya Dryomov static void check_linger_pool_dne(struct ceph_osd_linger_request *lreq)
30894609245eSIlya Dryomov {
30904609245eSIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
30914609245eSIlya Dryomov 	struct ceph_osdmap *map = osdc->osdmap;
30924609245eSIlya Dryomov 
30934609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
30944609245eSIlya Dryomov 	WARN_ON(!map->epoch);
30954609245eSIlya Dryomov 
30964609245eSIlya Dryomov 	if (lreq->register_gen) {
30974609245eSIlya Dryomov 		lreq->map_dne_bound = map->epoch;
30984609245eSIlya Dryomov 		dout("%s lreq %p linger_id %llu pool disappeared\n", __func__,
30994609245eSIlya Dryomov 		     lreq, lreq->linger_id);
31004609245eSIlya Dryomov 	} else {
31014609245eSIlya Dryomov 		dout("%s lreq %p linger_id %llu map_dne_bound %u have %u\n",
31024609245eSIlya Dryomov 		     __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
31034609245eSIlya Dryomov 		     map->epoch);
31044609245eSIlya Dryomov 	}
31054609245eSIlya Dryomov 
31064609245eSIlya Dryomov 	if (lreq->map_dne_bound) {
31074609245eSIlya Dryomov 		if (map->epoch >= lreq->map_dne_bound) {
31084609245eSIlya Dryomov 			/* we had a new enough map */
31094609245eSIlya Dryomov 			pr_info("linger_id %llu pool does not exist\n",
31104609245eSIlya Dryomov 				lreq->linger_id);
31114609245eSIlya Dryomov 			linger_reg_commit_complete(lreq, -ENOENT);
31124609245eSIlya Dryomov 			__linger_cancel(lreq);
31134609245eSIlya Dryomov 		}
31144609245eSIlya Dryomov 	} else {
31154609245eSIlya Dryomov 		send_linger_map_check(lreq);
31164609245eSIlya Dryomov 	}
31174609245eSIlya Dryomov }
31184609245eSIlya Dryomov 
31194609245eSIlya Dryomov static void linger_map_check_cb(struct ceph_mon_generic_request *greq)
31204609245eSIlya Dryomov {
31214609245eSIlya Dryomov 	struct ceph_osd_client *osdc = &greq->monc->client->osdc;
31224609245eSIlya Dryomov 	struct ceph_osd_linger_request *lreq;
31234609245eSIlya Dryomov 	u64 linger_id = greq->private_data;
31244609245eSIlya Dryomov 
31254609245eSIlya Dryomov 	WARN_ON(greq->result || !greq->u.newest);
31264609245eSIlya Dryomov 
31274609245eSIlya Dryomov 	down_write(&osdc->lock);
31284609245eSIlya Dryomov 	lreq = lookup_linger_mc(&osdc->linger_map_checks, linger_id);
31294609245eSIlya Dryomov 	if (!lreq) {
31304609245eSIlya Dryomov 		dout("%s linger_id %llu dne\n", __func__, linger_id);
31314609245eSIlya Dryomov 		goto out_unlock;
31324609245eSIlya Dryomov 	}
31334609245eSIlya Dryomov 
31344609245eSIlya Dryomov 	dout("%s lreq %p linger_id %llu map_dne_bound %u newest %llu\n",
31354609245eSIlya Dryomov 	     __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
31364609245eSIlya Dryomov 	     greq->u.newest);
31374609245eSIlya Dryomov 	if (!lreq->map_dne_bound)
31384609245eSIlya Dryomov 		lreq->map_dne_bound = greq->u.newest;
31394609245eSIlya Dryomov 	erase_linger_mc(&osdc->linger_map_checks, lreq);
31404609245eSIlya Dryomov 	check_linger_pool_dne(lreq);
31414609245eSIlya Dryomov 
31424609245eSIlya Dryomov 	linger_put(lreq);
31434609245eSIlya Dryomov out_unlock:
31444609245eSIlya Dryomov 	up_write(&osdc->lock);
31454609245eSIlya Dryomov }
31464609245eSIlya Dryomov 
31474609245eSIlya Dryomov static void send_linger_map_check(struct ceph_osd_linger_request *lreq)
31484609245eSIlya Dryomov {
31494609245eSIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
31504609245eSIlya Dryomov 	struct ceph_osd_linger_request *lookup_lreq;
31514609245eSIlya Dryomov 	int ret;
31524609245eSIlya Dryomov 
31534609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
31544609245eSIlya Dryomov 
31554609245eSIlya Dryomov 	lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
31564609245eSIlya Dryomov 				       lreq->linger_id);
31574609245eSIlya Dryomov 	if (lookup_lreq) {
31584609245eSIlya Dryomov 		WARN_ON(lookup_lreq != lreq);
31594609245eSIlya Dryomov 		return;
31604609245eSIlya Dryomov 	}
31614609245eSIlya Dryomov 
31624609245eSIlya Dryomov 	linger_get(lreq);
31634609245eSIlya Dryomov 	insert_linger_mc(&osdc->linger_map_checks, lreq);
31644609245eSIlya Dryomov 	ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
31654609245eSIlya Dryomov 					  linger_map_check_cb, lreq->linger_id);
31664609245eSIlya Dryomov 	WARN_ON(ret);
31674609245eSIlya Dryomov }
31684609245eSIlya Dryomov 
3169922dab61SIlya Dryomov static int linger_reg_commit_wait(struct ceph_osd_linger_request *lreq)
3170922dab61SIlya Dryomov {
3171922dab61SIlya Dryomov 	int ret;
3172922dab61SIlya Dryomov 
3173922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3174922dab61SIlya Dryomov 	ret = wait_for_completion_interruptible(&lreq->reg_commit_wait);
3175922dab61SIlya Dryomov 	return ret ?: lreq->reg_commit_error;
3176922dab61SIlya Dryomov }
3177922dab61SIlya Dryomov 
317819079203SIlya Dryomov static int linger_notify_finish_wait(struct ceph_osd_linger_request *lreq)
317919079203SIlya Dryomov {
318019079203SIlya Dryomov 	int ret;
318119079203SIlya Dryomov 
318219079203SIlya Dryomov 	dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
318319079203SIlya Dryomov 	ret = wait_for_completion_interruptible(&lreq->notify_finish_wait);
318419079203SIlya Dryomov 	return ret ?: lreq->notify_finish_error;
318519079203SIlya Dryomov }
318619079203SIlya Dryomov 
3187922dab61SIlya Dryomov /*
3188fbca9635SIlya Dryomov  * Timeout callback, called every N seconds.  When 1 or more OSD
3189fbca9635SIlya Dryomov  * requests has been active for more than N seconds, we send a keepalive
3190fbca9635SIlya Dryomov  * (tag + timestamp) to its OSD to ensure any communications channel
3191fbca9635SIlya Dryomov  * reset is detected.
31923d14c5d2SYehuda Sadeh  */
31933d14c5d2SYehuda Sadeh static void handle_timeout(struct work_struct *work)
31943d14c5d2SYehuda Sadeh {
31953d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc =
31963d14c5d2SYehuda Sadeh 		container_of(work, struct ceph_osd_client, timeout_work.work);
3197a319bf56SIlya Dryomov 	struct ceph_options *opts = osdc->client->options;
31985aea3dcdSIlya Dryomov 	unsigned long cutoff = jiffies - opts->osd_keepalive_timeout;
31997cc5e38fSIlya Dryomov 	unsigned long expiry_cutoff = jiffies - opts->osd_request_timeout;
32005aea3dcdSIlya Dryomov 	LIST_HEAD(slow_osds);
32015aea3dcdSIlya Dryomov 	struct rb_node *n, *p;
32023d14c5d2SYehuda Sadeh 
32035aea3dcdSIlya Dryomov 	dout("%s osdc %p\n", __func__, osdc);
32045aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
32053d14c5d2SYehuda Sadeh 
32063d14c5d2SYehuda Sadeh 	/*
32073d14c5d2SYehuda Sadeh 	 * ping osds that are a bit slow.  this ensures that if there
32083d14c5d2SYehuda Sadeh 	 * is a break in the TCP connection we will notice, and reopen
32093d14c5d2SYehuda Sadeh 	 * a connection with that osd (from the fault callback).
32103d14c5d2SYehuda Sadeh 	 */
32115aea3dcdSIlya Dryomov 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
32125aea3dcdSIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
32135aea3dcdSIlya Dryomov 		bool found = false;
32143d14c5d2SYehuda Sadeh 
32157cc5e38fSIlya Dryomov 		for (p = rb_first(&osd->o_requests); p; ) {
32165aea3dcdSIlya Dryomov 			struct ceph_osd_request *req =
32175aea3dcdSIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
32185aea3dcdSIlya Dryomov 
32197cc5e38fSIlya Dryomov 			p = rb_next(p); /* abort_request() */
32207cc5e38fSIlya Dryomov 
32215aea3dcdSIlya Dryomov 			if (time_before(req->r_stamp, cutoff)) {
32225aea3dcdSIlya Dryomov 				dout(" req %p tid %llu on osd%d is laggy\n",
32235aea3dcdSIlya Dryomov 				     req, req->r_tid, osd->o_osd);
32245aea3dcdSIlya Dryomov 				found = true;
32255aea3dcdSIlya Dryomov 			}
32267cc5e38fSIlya Dryomov 			if (opts->osd_request_timeout &&
32277cc5e38fSIlya Dryomov 			    time_before(req->r_start_stamp, expiry_cutoff)) {
32287cc5e38fSIlya Dryomov 				pr_err_ratelimited("tid %llu on osd%d timeout\n",
32297cc5e38fSIlya Dryomov 				       req->r_tid, osd->o_osd);
32307cc5e38fSIlya Dryomov 				abort_request(req, -ETIMEDOUT);
32317cc5e38fSIlya Dryomov 			}
32325aea3dcdSIlya Dryomov 		}
3233922dab61SIlya Dryomov 		for (p = rb_first(&osd->o_linger_requests); p; p = rb_next(p)) {
3234922dab61SIlya Dryomov 			struct ceph_osd_linger_request *lreq =
3235922dab61SIlya Dryomov 			    rb_entry(p, struct ceph_osd_linger_request, node);
3236922dab61SIlya Dryomov 
3237922dab61SIlya Dryomov 			dout(" lreq %p linger_id %llu is served by osd%d\n",
3238922dab61SIlya Dryomov 			     lreq, lreq->linger_id, osd->o_osd);
3239922dab61SIlya Dryomov 			found = true;
3240922dab61SIlya Dryomov 
3241922dab61SIlya Dryomov 			mutex_lock(&lreq->lock);
324219079203SIlya Dryomov 			if (lreq->is_watch && lreq->committed && !lreq->last_error)
3243922dab61SIlya Dryomov 				send_linger_ping(lreq);
3244922dab61SIlya Dryomov 			mutex_unlock(&lreq->lock);
3245922dab61SIlya Dryomov 		}
32465aea3dcdSIlya Dryomov 
32475aea3dcdSIlya Dryomov 		if (found)
32483d14c5d2SYehuda Sadeh 			list_move_tail(&osd->o_keepalive_item, &slow_osds);
32493d14c5d2SYehuda Sadeh 	}
32505aea3dcdSIlya Dryomov 
32517cc5e38fSIlya Dryomov 	if (opts->osd_request_timeout) {
32527cc5e38fSIlya Dryomov 		for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
32537cc5e38fSIlya Dryomov 			struct ceph_osd_request *req =
32547cc5e38fSIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
32557cc5e38fSIlya Dryomov 
32567cc5e38fSIlya Dryomov 			p = rb_next(p); /* abort_request() */
32577cc5e38fSIlya Dryomov 
32587cc5e38fSIlya Dryomov 			if (time_before(req->r_start_stamp, expiry_cutoff)) {
32597cc5e38fSIlya Dryomov 				pr_err_ratelimited("tid %llu on osd%d timeout\n",
32607cc5e38fSIlya Dryomov 				       req->r_tid, osdc->homeless_osd.o_osd);
32617cc5e38fSIlya Dryomov 				abort_request(req, -ETIMEDOUT);
32627cc5e38fSIlya Dryomov 			}
32637cc5e38fSIlya Dryomov 		}
32647cc5e38fSIlya Dryomov 	}
32657cc5e38fSIlya Dryomov 
32665aea3dcdSIlya Dryomov 	if (atomic_read(&osdc->num_homeless) || !list_empty(&slow_osds))
32675aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
32685aea3dcdSIlya Dryomov 
32693d14c5d2SYehuda Sadeh 	while (!list_empty(&slow_osds)) {
32705aea3dcdSIlya Dryomov 		struct ceph_osd *osd = list_first_entry(&slow_osds,
32715aea3dcdSIlya Dryomov 							struct ceph_osd,
32723d14c5d2SYehuda Sadeh 							o_keepalive_item);
32733d14c5d2SYehuda Sadeh 		list_del_init(&osd->o_keepalive_item);
32743d14c5d2SYehuda Sadeh 		ceph_con_keepalive(&osd->o_con);
32753d14c5d2SYehuda Sadeh 	}
32763d14c5d2SYehuda Sadeh 
32775aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
3278fbca9635SIlya Dryomov 	schedule_delayed_work(&osdc->timeout_work,
3279fbca9635SIlya Dryomov 			      osdc->client->options->osd_keepalive_timeout);
32803d14c5d2SYehuda Sadeh }
32813d14c5d2SYehuda Sadeh 
32823d14c5d2SYehuda Sadeh static void handle_osds_timeout(struct work_struct *work)
32833d14c5d2SYehuda Sadeh {
32843d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc =
32853d14c5d2SYehuda Sadeh 		container_of(work, struct ceph_osd_client,
32863d14c5d2SYehuda Sadeh 			     osds_timeout_work.work);
3287a319bf56SIlya Dryomov 	unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
328842a2c09fSIlya Dryomov 	struct ceph_osd *osd, *nosd;
32893d14c5d2SYehuda Sadeh 
329042a2c09fSIlya Dryomov 	dout("%s osdc %p\n", __func__, osdc);
32915aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
329242a2c09fSIlya Dryomov 	list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
329342a2c09fSIlya Dryomov 		if (time_before(jiffies, osd->lru_ttl))
329442a2c09fSIlya Dryomov 			break;
329542a2c09fSIlya Dryomov 
32965aea3dcdSIlya Dryomov 		WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
3297922dab61SIlya Dryomov 		WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
32985aea3dcdSIlya Dryomov 		close_osd(osd);
329942a2c09fSIlya Dryomov 	}
330042a2c09fSIlya Dryomov 
33015aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
33023d14c5d2SYehuda Sadeh 	schedule_delayed_work(&osdc->osds_timeout_work,
33033d14c5d2SYehuda Sadeh 			      round_jiffies_relative(delay));
33043d14c5d2SYehuda Sadeh }
33053d14c5d2SYehuda Sadeh 
3306205ee118SIlya Dryomov static int ceph_oloc_decode(void **p, void *end,
3307205ee118SIlya Dryomov 			    struct ceph_object_locator *oloc)
3308205ee118SIlya Dryomov {
3309205ee118SIlya Dryomov 	u8 struct_v, struct_cv;
3310205ee118SIlya Dryomov 	u32 len;
3311205ee118SIlya Dryomov 	void *struct_end;
3312205ee118SIlya Dryomov 	int ret = 0;
3313205ee118SIlya Dryomov 
3314205ee118SIlya Dryomov 	ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3315205ee118SIlya Dryomov 	struct_v = ceph_decode_8(p);
3316205ee118SIlya Dryomov 	struct_cv = ceph_decode_8(p);
3317205ee118SIlya Dryomov 	if (struct_v < 3) {
3318205ee118SIlya Dryomov 		pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
3319205ee118SIlya Dryomov 			struct_v, struct_cv);
3320205ee118SIlya Dryomov 		goto e_inval;
3321205ee118SIlya Dryomov 	}
3322205ee118SIlya Dryomov 	if (struct_cv > 6) {
3323205ee118SIlya Dryomov 		pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
3324205ee118SIlya Dryomov 			struct_v, struct_cv);
3325205ee118SIlya Dryomov 		goto e_inval;
3326205ee118SIlya Dryomov 	}
3327205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3328205ee118SIlya Dryomov 	ceph_decode_need(p, end, len, e_inval);
3329205ee118SIlya Dryomov 	struct_end = *p + len;
3330205ee118SIlya Dryomov 
3331205ee118SIlya Dryomov 	oloc->pool = ceph_decode_64(p);
3332205ee118SIlya Dryomov 	*p += 4; /* skip preferred */
3333205ee118SIlya Dryomov 
3334205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3335205ee118SIlya Dryomov 	if (len > 0) {
3336205ee118SIlya Dryomov 		pr_warn("ceph_object_locator::key is set\n");
3337205ee118SIlya Dryomov 		goto e_inval;
3338205ee118SIlya Dryomov 	}
3339205ee118SIlya Dryomov 
3340205ee118SIlya Dryomov 	if (struct_v >= 5) {
3341cd08e0a2SYan, Zheng 		bool changed = false;
3342cd08e0a2SYan, Zheng 
3343205ee118SIlya Dryomov 		len = ceph_decode_32(p);
3344205ee118SIlya Dryomov 		if (len > 0) {
334530c156d9SYan, Zheng 			ceph_decode_need(p, end, len, e_inval);
3346cd08e0a2SYan, Zheng 			if (!oloc->pool_ns ||
3347cd08e0a2SYan, Zheng 			    ceph_compare_string(oloc->pool_ns, *p, len))
3348cd08e0a2SYan, Zheng 				changed = true;
334930c156d9SYan, Zheng 			*p += len;
3350cd08e0a2SYan, Zheng 		} else {
3351cd08e0a2SYan, Zheng 			if (oloc->pool_ns)
3352cd08e0a2SYan, Zheng 				changed = true;
3353cd08e0a2SYan, Zheng 		}
3354cd08e0a2SYan, Zheng 		if (changed) {
3355cd08e0a2SYan, Zheng 			/* redirect changes namespace */
3356cd08e0a2SYan, Zheng 			pr_warn("ceph_object_locator::nspace is changed\n");
3357cd08e0a2SYan, Zheng 			goto e_inval;
3358205ee118SIlya Dryomov 		}
3359205ee118SIlya Dryomov 	}
3360205ee118SIlya Dryomov 
3361205ee118SIlya Dryomov 	if (struct_v >= 6) {
3362205ee118SIlya Dryomov 		s64 hash = ceph_decode_64(p);
3363205ee118SIlya Dryomov 		if (hash != -1) {
3364205ee118SIlya Dryomov 			pr_warn("ceph_object_locator::hash is set\n");
3365205ee118SIlya Dryomov 			goto e_inval;
3366205ee118SIlya Dryomov 		}
3367205ee118SIlya Dryomov 	}
3368205ee118SIlya Dryomov 
3369205ee118SIlya Dryomov 	/* skip the rest */
3370205ee118SIlya Dryomov 	*p = struct_end;
3371205ee118SIlya Dryomov out:
3372205ee118SIlya Dryomov 	return ret;
3373205ee118SIlya Dryomov 
3374205ee118SIlya Dryomov e_inval:
3375205ee118SIlya Dryomov 	ret = -EINVAL;
3376205ee118SIlya Dryomov 	goto out;
3377205ee118SIlya Dryomov }
3378205ee118SIlya Dryomov 
3379205ee118SIlya Dryomov static int ceph_redirect_decode(void **p, void *end,
3380205ee118SIlya Dryomov 				struct ceph_request_redirect *redir)
3381205ee118SIlya Dryomov {
3382205ee118SIlya Dryomov 	u8 struct_v, struct_cv;
3383205ee118SIlya Dryomov 	u32 len;
3384205ee118SIlya Dryomov 	void *struct_end;
3385205ee118SIlya Dryomov 	int ret;
3386205ee118SIlya Dryomov 
3387205ee118SIlya Dryomov 	ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3388205ee118SIlya Dryomov 	struct_v = ceph_decode_8(p);
3389205ee118SIlya Dryomov 	struct_cv = ceph_decode_8(p);
3390205ee118SIlya Dryomov 	if (struct_cv > 1) {
3391205ee118SIlya Dryomov 		pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
3392205ee118SIlya Dryomov 			struct_v, struct_cv);
3393205ee118SIlya Dryomov 		goto e_inval;
3394205ee118SIlya Dryomov 	}
3395205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3396205ee118SIlya Dryomov 	ceph_decode_need(p, end, len, e_inval);
3397205ee118SIlya Dryomov 	struct_end = *p + len;
3398205ee118SIlya Dryomov 
3399205ee118SIlya Dryomov 	ret = ceph_oloc_decode(p, end, &redir->oloc);
3400205ee118SIlya Dryomov 	if (ret)
3401205ee118SIlya Dryomov 		goto out;
3402205ee118SIlya Dryomov 
3403205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3404205ee118SIlya Dryomov 	if (len > 0) {
3405205ee118SIlya Dryomov 		pr_warn("ceph_request_redirect::object_name is set\n");
3406205ee118SIlya Dryomov 		goto e_inval;
3407205ee118SIlya Dryomov 	}
3408205ee118SIlya Dryomov 
3409205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3410205ee118SIlya Dryomov 	*p += len; /* skip osd_instructions */
3411205ee118SIlya Dryomov 
3412205ee118SIlya Dryomov 	/* skip the rest */
3413205ee118SIlya Dryomov 	*p = struct_end;
3414205ee118SIlya Dryomov out:
3415205ee118SIlya Dryomov 	return ret;
3416205ee118SIlya Dryomov 
3417205ee118SIlya Dryomov e_inval:
3418205ee118SIlya Dryomov 	ret = -EINVAL;
3419205ee118SIlya Dryomov 	goto out;
3420205ee118SIlya Dryomov }
3421205ee118SIlya Dryomov 
3422fe5da05eSIlya Dryomov struct MOSDOpReply {
3423fe5da05eSIlya Dryomov 	struct ceph_pg pgid;
3424fe5da05eSIlya Dryomov 	u64 flags;
3425fe5da05eSIlya Dryomov 	int result;
3426fe5da05eSIlya Dryomov 	u32 epoch;
3427fe5da05eSIlya Dryomov 	int num_ops;
3428fe5da05eSIlya Dryomov 	u32 outdata_len[CEPH_OSD_MAX_OPS];
3429fe5da05eSIlya Dryomov 	s32 rval[CEPH_OSD_MAX_OPS];
3430fe5da05eSIlya Dryomov 	int retry_attempt;
3431fe5da05eSIlya Dryomov 	struct ceph_eversion replay_version;
3432fe5da05eSIlya Dryomov 	u64 user_version;
3433fe5da05eSIlya Dryomov 	struct ceph_request_redirect redirect;
3434fe5da05eSIlya Dryomov };
343525845472SSage Weil 
3436fe5da05eSIlya Dryomov static int decode_MOSDOpReply(const struct ceph_msg *msg, struct MOSDOpReply *m)
34373d14c5d2SYehuda Sadeh {
3438fe5da05eSIlya Dryomov 	void *p = msg->front.iov_base;
3439fe5da05eSIlya Dryomov 	void *const end = p + msg->front.iov_len;
3440fe5da05eSIlya Dryomov 	u16 version = le16_to_cpu(msg->hdr.version);
3441fe5da05eSIlya Dryomov 	struct ceph_eversion bad_replay_version;
3442b0b31a8fSIlya Dryomov 	u8 decode_redir;
3443fe5da05eSIlya Dryomov 	u32 len;
3444fe5da05eSIlya Dryomov 	int ret;
3445fe5da05eSIlya Dryomov 	int i;
34463d14c5d2SYehuda Sadeh 
3447fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, len, e_inval);
3448fe5da05eSIlya Dryomov 	ceph_decode_need(&p, end, len, e_inval);
3449fe5da05eSIlya Dryomov 	p += len; /* skip oid */
34501b83bef2SSage Weil 
3451fe5da05eSIlya Dryomov 	ret = ceph_decode_pgid(&p, end, &m->pgid);
3452fe5da05eSIlya Dryomov 	if (ret)
3453fe5da05eSIlya Dryomov 		return ret;
34541b83bef2SSage Weil 
3455fe5da05eSIlya Dryomov 	ceph_decode_64_safe(&p, end, m->flags, e_inval);
3456fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->result, e_inval);
3457fe5da05eSIlya Dryomov 	ceph_decode_need(&p, end, sizeof(bad_replay_version), e_inval);
3458fe5da05eSIlya Dryomov 	memcpy(&bad_replay_version, p, sizeof(bad_replay_version));
3459fe5da05eSIlya Dryomov 	p += sizeof(bad_replay_version);
3460fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->epoch, e_inval);
34611b83bef2SSage Weil 
3462fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->num_ops, e_inval);
3463fe5da05eSIlya Dryomov 	if (m->num_ops > ARRAY_SIZE(m->outdata_len))
3464fe5da05eSIlya Dryomov 		goto e_inval;
34651b83bef2SSage Weil 
3466fe5da05eSIlya Dryomov 	ceph_decode_need(&p, end, m->num_ops * sizeof(struct ceph_osd_op),
3467fe5da05eSIlya Dryomov 			 e_inval);
3468fe5da05eSIlya Dryomov 	for (i = 0; i < m->num_ops; i++) {
34691b83bef2SSage Weil 		struct ceph_osd_op *op = p;
34701b83bef2SSage Weil 
3471fe5da05eSIlya Dryomov 		m->outdata_len[i] = le32_to_cpu(op->payload_len);
34721b83bef2SSage Weil 		p += sizeof(*op);
34731b83bef2SSage Weil 	}
3474fe5da05eSIlya Dryomov 
3475fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->retry_attempt, e_inval);
3476fe5da05eSIlya Dryomov 	for (i = 0; i < m->num_ops; i++)
3477fe5da05eSIlya Dryomov 		ceph_decode_32_safe(&p, end, m->rval[i], e_inval);
3478fe5da05eSIlya Dryomov 
3479fe5da05eSIlya Dryomov 	if (version >= 5) {
3480fe5da05eSIlya Dryomov 		ceph_decode_need(&p, end, sizeof(m->replay_version), e_inval);
3481fe5da05eSIlya Dryomov 		memcpy(&m->replay_version, p, sizeof(m->replay_version));
3482fe5da05eSIlya Dryomov 		p += sizeof(m->replay_version);
3483fe5da05eSIlya Dryomov 		ceph_decode_64_safe(&p, end, m->user_version, e_inval);
3484fe5da05eSIlya Dryomov 	} else {
3485fe5da05eSIlya Dryomov 		m->replay_version = bad_replay_version; /* struct */
3486fe5da05eSIlya Dryomov 		m->user_version = le64_to_cpu(m->replay_version.version);
34871b83bef2SSage Weil 	}
34881b83bef2SSage Weil 
3489fe5da05eSIlya Dryomov 	if (version >= 6) {
3490fe5da05eSIlya Dryomov 		if (version >= 7)
3491fe5da05eSIlya Dryomov 			ceph_decode_8_safe(&p, end, decode_redir, e_inval);
3492b0b31a8fSIlya Dryomov 		else
3493b0b31a8fSIlya Dryomov 			decode_redir = 1;
3494b0b31a8fSIlya Dryomov 	} else {
3495b0b31a8fSIlya Dryomov 		decode_redir = 0;
3496b0b31a8fSIlya Dryomov 	}
3497b0b31a8fSIlya Dryomov 
3498b0b31a8fSIlya Dryomov 	if (decode_redir) {
3499fe5da05eSIlya Dryomov 		ret = ceph_redirect_decode(&p, end, &m->redirect);
3500fe5da05eSIlya Dryomov 		if (ret)
3501fe5da05eSIlya Dryomov 			return ret;
3502205ee118SIlya Dryomov 	} else {
3503fe5da05eSIlya Dryomov 		ceph_oloc_init(&m->redirect.oloc);
3504205ee118SIlya Dryomov 	}
3505205ee118SIlya Dryomov 
3506fe5da05eSIlya Dryomov 	return 0;
3507205ee118SIlya Dryomov 
3508fe5da05eSIlya Dryomov e_inval:
3509fe5da05eSIlya Dryomov 	return -EINVAL;
3510fe5da05eSIlya Dryomov }
3511fe5da05eSIlya Dryomov 
3512fe5da05eSIlya Dryomov /*
3513b18b9550SIlya Dryomov  * Handle MOSDOpReply.  Set ->r_result and call the callback if it is
3514b18b9550SIlya Dryomov  * specified.
3515fe5da05eSIlya Dryomov  */
35165aea3dcdSIlya Dryomov static void handle_reply(struct ceph_osd *osd, struct ceph_msg *msg)
3517fe5da05eSIlya Dryomov {
35185aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
3519fe5da05eSIlya Dryomov 	struct ceph_osd_request *req;
3520fe5da05eSIlya Dryomov 	struct MOSDOpReply m;
3521fe5da05eSIlya Dryomov 	u64 tid = le64_to_cpu(msg->hdr.tid);
3522fe5da05eSIlya Dryomov 	u32 data_len = 0;
3523fe5da05eSIlya Dryomov 	int ret;
3524fe5da05eSIlya Dryomov 	int i;
3525fe5da05eSIlya Dryomov 
3526fe5da05eSIlya Dryomov 	dout("%s msg %p tid %llu\n", __func__, msg, tid);
3527fe5da05eSIlya Dryomov 
35285aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
35295aea3dcdSIlya Dryomov 	if (!osd_registered(osd)) {
35305aea3dcdSIlya Dryomov 		dout("%s osd%d unknown\n", __func__, osd->o_osd);
35315aea3dcdSIlya Dryomov 		goto out_unlock_osdc;
3532fe5da05eSIlya Dryomov 	}
35335aea3dcdSIlya Dryomov 	WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
35345aea3dcdSIlya Dryomov 
35355aea3dcdSIlya Dryomov 	mutex_lock(&osd->lock);
35365aea3dcdSIlya Dryomov 	req = lookup_request(&osd->o_requests, tid);
35375aea3dcdSIlya Dryomov 	if (!req) {
35385aea3dcdSIlya Dryomov 		dout("%s osd%d tid %llu unknown\n", __func__, osd->o_osd, tid);
35395aea3dcdSIlya Dryomov 		goto out_unlock_session;
35405aea3dcdSIlya Dryomov 	}
3541fe5da05eSIlya Dryomov 
3542cd08e0a2SYan, Zheng 	m.redirect.oloc.pool_ns = req->r_t.target_oloc.pool_ns;
3543fe5da05eSIlya Dryomov 	ret = decode_MOSDOpReply(msg, &m);
3544cd08e0a2SYan, Zheng 	m.redirect.oloc.pool_ns = NULL;
3545fe5da05eSIlya Dryomov 	if (ret) {
3546fe5da05eSIlya Dryomov 		pr_err("failed to decode MOSDOpReply for tid %llu: %d\n",
3547fe5da05eSIlya Dryomov 		       req->r_tid, ret);
3548fe5da05eSIlya Dryomov 		ceph_msg_dump(msg);
3549fe5da05eSIlya Dryomov 		goto fail_request;
3550fe5da05eSIlya Dryomov 	}
3551fe5da05eSIlya Dryomov 	dout("%s req %p tid %llu flags 0x%llx pgid %llu.%x epoch %u attempt %d v %u'%llu uv %llu\n",
3552fe5da05eSIlya Dryomov 	     __func__, req, req->r_tid, m.flags, m.pgid.pool, m.pgid.seed,
3553fe5da05eSIlya Dryomov 	     m.epoch, m.retry_attempt, le32_to_cpu(m.replay_version.epoch),
3554fe5da05eSIlya Dryomov 	     le64_to_cpu(m.replay_version.version), m.user_version);
3555fe5da05eSIlya Dryomov 
3556fe5da05eSIlya Dryomov 	if (m.retry_attempt >= 0) {
3557fe5da05eSIlya Dryomov 		if (m.retry_attempt != req->r_attempts - 1) {
3558fe5da05eSIlya Dryomov 			dout("req %p tid %llu retry_attempt %d != %d, ignoring\n",
3559fe5da05eSIlya Dryomov 			     req, req->r_tid, m.retry_attempt,
3560fe5da05eSIlya Dryomov 			     req->r_attempts - 1);
35615aea3dcdSIlya Dryomov 			goto out_unlock_session;
3562fe5da05eSIlya Dryomov 		}
3563fe5da05eSIlya Dryomov 	} else {
3564fe5da05eSIlya Dryomov 		WARN_ON(1); /* MOSDOpReply v4 is assumed */
3565fe5da05eSIlya Dryomov 	}
3566fe5da05eSIlya Dryomov 
3567fe5da05eSIlya Dryomov 	if (!ceph_oloc_empty(&m.redirect.oloc)) {
3568fe5da05eSIlya Dryomov 		dout("req %p tid %llu redirect pool %lld\n", req, req->r_tid,
3569fe5da05eSIlya Dryomov 		     m.redirect.oloc.pool);
35705aea3dcdSIlya Dryomov 		unlink_request(osd, req);
35715aea3dcdSIlya Dryomov 		mutex_unlock(&osd->lock);
3572205ee118SIlya Dryomov 
357330c156d9SYan, Zheng 		/*
357430c156d9SYan, Zheng 		 * Not ceph_oloc_copy() - changing pool_ns is not
357530c156d9SYan, Zheng 		 * supported.
357630c156d9SYan, Zheng 		 */
357730c156d9SYan, Zheng 		req->r_t.target_oloc.pool = m.redirect.oloc.pool;
35785aea3dcdSIlya Dryomov 		req->r_flags |= CEPH_OSD_FLAG_REDIRECTED;
35795aea3dcdSIlya Dryomov 		req->r_tid = 0;
35805aea3dcdSIlya Dryomov 		__submit_request(req, false);
35815aea3dcdSIlya Dryomov 		goto out_unlock_osdc;
3582205ee118SIlya Dryomov 	}
3583205ee118SIlya Dryomov 
3584fe5da05eSIlya Dryomov 	if (m.num_ops != req->r_num_ops) {
3585fe5da05eSIlya Dryomov 		pr_err("num_ops %d != %d for tid %llu\n", m.num_ops,
3586fe5da05eSIlya Dryomov 		       req->r_num_ops, req->r_tid);
3587fe5da05eSIlya Dryomov 		goto fail_request;
3588fe5da05eSIlya Dryomov 	}
3589fe5da05eSIlya Dryomov 	for (i = 0; i < req->r_num_ops; i++) {
3590fe5da05eSIlya Dryomov 		dout(" req %p tid %llu op %d rval %d len %u\n", req,
3591fe5da05eSIlya Dryomov 		     req->r_tid, i, m.rval[i], m.outdata_len[i]);
3592fe5da05eSIlya Dryomov 		req->r_ops[i].rval = m.rval[i];
3593fe5da05eSIlya Dryomov 		req->r_ops[i].outdata_len = m.outdata_len[i];
3594fe5da05eSIlya Dryomov 		data_len += m.outdata_len[i];
3595fe5da05eSIlya Dryomov 	}
3596fe5da05eSIlya Dryomov 	if (data_len != le32_to_cpu(msg->hdr.data_len)) {
3597fe5da05eSIlya Dryomov 		pr_err("sum of lens %u != %u for tid %llu\n", data_len,
3598fe5da05eSIlya Dryomov 		       le32_to_cpu(msg->hdr.data_len), req->r_tid);
3599fe5da05eSIlya Dryomov 		goto fail_request;
3600fe5da05eSIlya Dryomov 	}
3601b18b9550SIlya Dryomov 	dout("%s req %p tid %llu result %d data_len %u\n", __func__,
3602b18b9550SIlya Dryomov 	     req, req->r_tid, m.result, data_len);
36033d14c5d2SYehuda Sadeh 
3604b18b9550SIlya Dryomov 	/*
3605b18b9550SIlya Dryomov 	 * Since we only ever request ONDISK, we should only ever get
3606b18b9550SIlya Dryomov 	 * one (type of) reply back.
3607b18b9550SIlya Dryomov 	 */
3608b18b9550SIlya Dryomov 	WARN_ON(!(m.flags & CEPH_OSD_FLAG_ONDISK));
3609fe5da05eSIlya Dryomov 	req->r_result = m.result ?: data_len;
361045ee2c1dSIlya Dryomov 	finish_request(req);
36115aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
36125aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
36133d14c5d2SYehuda Sadeh 
3614fe5da05eSIlya Dryomov 	__complete_request(req);
36153d14c5d2SYehuda Sadeh 	return;
3616fe5da05eSIlya Dryomov 
3617fe5da05eSIlya Dryomov fail_request:
36184609245eSIlya Dryomov 	complete_request(req, -EIO);
36195aea3dcdSIlya Dryomov out_unlock_session:
36205aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
36215aea3dcdSIlya Dryomov out_unlock_osdc:
36225aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
36233d14c5d2SYehuda Sadeh }
36243d14c5d2SYehuda Sadeh 
362542c1b124SIlya Dryomov static void set_pool_was_full(struct ceph_osd_client *osdc)
362642c1b124SIlya Dryomov {
362742c1b124SIlya Dryomov 	struct rb_node *n;
362842c1b124SIlya Dryomov 
362942c1b124SIlya Dryomov 	for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
363042c1b124SIlya Dryomov 		struct ceph_pg_pool_info *pi =
363142c1b124SIlya Dryomov 		    rb_entry(n, struct ceph_pg_pool_info, node);
363242c1b124SIlya Dryomov 
363342c1b124SIlya Dryomov 		pi->was_full = __pool_full(pi);
363442c1b124SIlya Dryomov 	}
363542c1b124SIlya Dryomov }
363642c1b124SIlya Dryomov 
36375aea3dcdSIlya Dryomov static bool pool_cleared_full(struct ceph_osd_client *osdc, s64 pool_id)
36383d14c5d2SYehuda Sadeh {
36395aea3dcdSIlya Dryomov 	struct ceph_pg_pool_info *pi;
36403d14c5d2SYehuda Sadeh 
36415aea3dcdSIlya Dryomov 	pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
36425aea3dcdSIlya Dryomov 	if (!pi)
36435aea3dcdSIlya Dryomov 		return false;
36443d14c5d2SYehuda Sadeh 
36455aea3dcdSIlya Dryomov 	return pi->was_full && !__pool_full(pi);
36463d14c5d2SYehuda Sadeh }
36473d14c5d2SYehuda Sadeh 
3648922dab61SIlya Dryomov static enum calc_target_result
3649922dab61SIlya Dryomov recalc_linger_target(struct ceph_osd_linger_request *lreq)
3650922dab61SIlya Dryomov {
3651922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
3652922dab61SIlya Dryomov 	enum calc_target_result ct_res;
3653922dab61SIlya Dryomov 
36547de030d6SIlya Dryomov 	ct_res = calc_target(osdc, &lreq->t, NULL, true);
3655922dab61SIlya Dryomov 	if (ct_res == CALC_TARGET_NEED_RESEND) {
3656922dab61SIlya Dryomov 		struct ceph_osd *osd;
3657922dab61SIlya Dryomov 
3658922dab61SIlya Dryomov 		osd = lookup_create_osd(osdc, lreq->t.osd, true);
3659922dab61SIlya Dryomov 		if (osd != lreq->osd) {
3660922dab61SIlya Dryomov 			unlink_linger(lreq->osd, lreq);
3661922dab61SIlya Dryomov 			link_linger(osd, lreq);
3662922dab61SIlya Dryomov 		}
3663922dab61SIlya Dryomov 	}
3664922dab61SIlya Dryomov 
3665922dab61SIlya Dryomov 	return ct_res;
3666922dab61SIlya Dryomov }
3667922dab61SIlya Dryomov 
36683d14c5d2SYehuda Sadeh /*
36695aea3dcdSIlya Dryomov  * Requeue requests whose mapping to an OSD has changed.
36703d14c5d2SYehuda Sadeh  */
36715aea3dcdSIlya Dryomov static void scan_requests(struct ceph_osd *osd,
36725aea3dcdSIlya Dryomov 			  bool force_resend,
36735aea3dcdSIlya Dryomov 			  bool cleared_full,
36745aea3dcdSIlya Dryomov 			  bool check_pool_cleared_full,
36755aea3dcdSIlya Dryomov 			  struct rb_root *need_resend,
36765aea3dcdSIlya Dryomov 			  struct list_head *need_resend_linger)
36773d14c5d2SYehuda Sadeh {
36785aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
36795aea3dcdSIlya Dryomov 	struct rb_node *n;
36805aea3dcdSIlya Dryomov 	bool force_resend_writes;
36813d14c5d2SYehuda Sadeh 
3682922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_linger_requests); n; ) {
3683922dab61SIlya Dryomov 		struct ceph_osd_linger_request *lreq =
3684922dab61SIlya Dryomov 		    rb_entry(n, struct ceph_osd_linger_request, node);
3685922dab61SIlya Dryomov 		enum calc_target_result ct_res;
3686922dab61SIlya Dryomov 
3687922dab61SIlya Dryomov 		n = rb_next(n); /* recalc_linger_target() */
3688922dab61SIlya Dryomov 
3689922dab61SIlya Dryomov 		dout("%s lreq %p linger_id %llu\n", __func__, lreq,
3690922dab61SIlya Dryomov 		     lreq->linger_id);
3691922dab61SIlya Dryomov 		ct_res = recalc_linger_target(lreq);
3692922dab61SIlya Dryomov 		switch (ct_res) {
3693922dab61SIlya Dryomov 		case CALC_TARGET_NO_ACTION:
3694922dab61SIlya Dryomov 			force_resend_writes = cleared_full ||
3695922dab61SIlya Dryomov 			    (check_pool_cleared_full &&
3696922dab61SIlya Dryomov 			     pool_cleared_full(osdc, lreq->t.base_oloc.pool));
3697922dab61SIlya Dryomov 			if (!force_resend && !force_resend_writes)
3698922dab61SIlya Dryomov 				break;
3699922dab61SIlya Dryomov 
3700922dab61SIlya Dryomov 			/* fall through */
3701922dab61SIlya Dryomov 		case CALC_TARGET_NEED_RESEND:
37024609245eSIlya Dryomov 			cancel_linger_map_check(lreq);
3703922dab61SIlya Dryomov 			/*
3704922dab61SIlya Dryomov 			 * scan_requests() for the previous epoch(s)
3705922dab61SIlya Dryomov 			 * may have already added it to the list, since
3706922dab61SIlya Dryomov 			 * it's not unlinked here.
3707922dab61SIlya Dryomov 			 */
3708922dab61SIlya Dryomov 			if (list_empty(&lreq->scan_item))
3709922dab61SIlya Dryomov 				list_add_tail(&lreq->scan_item, need_resend_linger);
3710922dab61SIlya Dryomov 			break;
3711922dab61SIlya Dryomov 		case CALC_TARGET_POOL_DNE:
3712a10bcb19SIlya Dryomov 			list_del_init(&lreq->scan_item);
37134609245eSIlya Dryomov 			check_linger_pool_dne(lreq);
3714922dab61SIlya Dryomov 			break;
3715922dab61SIlya Dryomov 		}
3716922dab61SIlya Dryomov 	}
3717922dab61SIlya Dryomov 
37185aea3dcdSIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; ) {
37195aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
37205aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
37215aea3dcdSIlya Dryomov 		enum calc_target_result ct_res;
3722ab60b16dSAlex Elder 
37234609245eSIlya Dryomov 		n = rb_next(n); /* unlink_request(), check_pool_dne() */
3724ab60b16dSAlex Elder 
37255aea3dcdSIlya Dryomov 		dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
37267de030d6SIlya Dryomov 		ct_res = calc_target(osdc, &req->r_t, &req->r_osd->o_con,
37277de030d6SIlya Dryomov 				     false);
37285aea3dcdSIlya Dryomov 		switch (ct_res) {
37295aea3dcdSIlya Dryomov 		case CALC_TARGET_NO_ACTION:
37305aea3dcdSIlya Dryomov 			force_resend_writes = cleared_full ||
37315aea3dcdSIlya Dryomov 			    (check_pool_cleared_full &&
37325aea3dcdSIlya Dryomov 			     pool_cleared_full(osdc, req->r_t.base_oloc.pool));
37335aea3dcdSIlya Dryomov 			if (!force_resend &&
37345aea3dcdSIlya Dryomov 			    (!(req->r_flags & CEPH_OSD_FLAG_WRITE) ||
37355aea3dcdSIlya Dryomov 			     !force_resend_writes))
37365aea3dcdSIlya Dryomov 				break;
3737a40c4f10SYehuda Sadeh 
37385aea3dcdSIlya Dryomov 			/* fall through */
37395aea3dcdSIlya Dryomov 		case CALC_TARGET_NEED_RESEND:
37404609245eSIlya Dryomov 			cancel_map_check(req);
37415aea3dcdSIlya Dryomov 			unlink_request(osd, req);
37425aea3dcdSIlya Dryomov 			insert_request(need_resend, req);
37435aea3dcdSIlya Dryomov 			break;
37445aea3dcdSIlya Dryomov 		case CALC_TARGET_POOL_DNE:
37454609245eSIlya Dryomov 			check_pool_dne(req);
37465aea3dcdSIlya Dryomov 			break;
3747a40c4f10SYehuda Sadeh 		}
37483d14c5d2SYehuda Sadeh 	}
37493d14c5d2SYehuda Sadeh }
37506f6c7006SSage Weil 
375142c1b124SIlya Dryomov static int handle_one_map(struct ceph_osd_client *osdc,
37525aea3dcdSIlya Dryomov 			  void *p, void *end, bool incremental,
37535aea3dcdSIlya Dryomov 			  struct rb_root *need_resend,
37545aea3dcdSIlya Dryomov 			  struct list_head *need_resend_linger)
375542c1b124SIlya Dryomov {
375642c1b124SIlya Dryomov 	struct ceph_osdmap *newmap;
375742c1b124SIlya Dryomov 	struct rb_node *n;
375842c1b124SIlya Dryomov 	bool skipped_map = false;
375942c1b124SIlya Dryomov 	bool was_full;
376042c1b124SIlya Dryomov 
3761b7ec35b3SIlya Dryomov 	was_full = ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
376242c1b124SIlya Dryomov 	set_pool_was_full(osdc);
376342c1b124SIlya Dryomov 
376442c1b124SIlya Dryomov 	if (incremental)
376542c1b124SIlya Dryomov 		newmap = osdmap_apply_incremental(&p, end, osdc->osdmap);
376642c1b124SIlya Dryomov 	else
376742c1b124SIlya Dryomov 		newmap = ceph_osdmap_decode(&p, end);
376842c1b124SIlya Dryomov 	if (IS_ERR(newmap))
376942c1b124SIlya Dryomov 		return PTR_ERR(newmap);
377042c1b124SIlya Dryomov 
377142c1b124SIlya Dryomov 	if (newmap != osdc->osdmap) {
377242c1b124SIlya Dryomov 		/*
377342c1b124SIlya Dryomov 		 * Preserve ->was_full before destroying the old map.
377442c1b124SIlya Dryomov 		 * For pools that weren't in the old map, ->was_full
377542c1b124SIlya Dryomov 		 * should be false.
377642c1b124SIlya Dryomov 		 */
377742c1b124SIlya Dryomov 		for (n = rb_first(&newmap->pg_pools); n; n = rb_next(n)) {
377842c1b124SIlya Dryomov 			struct ceph_pg_pool_info *pi =
377942c1b124SIlya Dryomov 			    rb_entry(n, struct ceph_pg_pool_info, node);
378042c1b124SIlya Dryomov 			struct ceph_pg_pool_info *old_pi;
378142c1b124SIlya Dryomov 
378242c1b124SIlya Dryomov 			old_pi = ceph_pg_pool_by_id(osdc->osdmap, pi->id);
378342c1b124SIlya Dryomov 			if (old_pi)
378442c1b124SIlya Dryomov 				pi->was_full = old_pi->was_full;
378542c1b124SIlya Dryomov 			else
378642c1b124SIlya Dryomov 				WARN_ON(pi->was_full);
378742c1b124SIlya Dryomov 		}
378842c1b124SIlya Dryomov 
378942c1b124SIlya Dryomov 		if (osdc->osdmap->epoch &&
379042c1b124SIlya Dryomov 		    osdc->osdmap->epoch + 1 < newmap->epoch) {
379142c1b124SIlya Dryomov 			WARN_ON(incremental);
379242c1b124SIlya Dryomov 			skipped_map = true;
379342c1b124SIlya Dryomov 		}
379442c1b124SIlya Dryomov 
379542c1b124SIlya Dryomov 		ceph_osdmap_destroy(osdc->osdmap);
379642c1b124SIlya Dryomov 		osdc->osdmap = newmap;
379742c1b124SIlya Dryomov 	}
379842c1b124SIlya Dryomov 
3799b7ec35b3SIlya Dryomov 	was_full &= !ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
38005aea3dcdSIlya Dryomov 	scan_requests(&osdc->homeless_osd, skipped_map, was_full, true,
38015aea3dcdSIlya Dryomov 		      need_resend, need_resend_linger);
38025aea3dcdSIlya Dryomov 
38035aea3dcdSIlya Dryomov 	for (n = rb_first(&osdc->osds); n; ) {
38045aea3dcdSIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
38055aea3dcdSIlya Dryomov 
38065aea3dcdSIlya Dryomov 		n = rb_next(n); /* close_osd() */
38075aea3dcdSIlya Dryomov 
38085aea3dcdSIlya Dryomov 		scan_requests(osd, skipped_map, was_full, true, need_resend,
38095aea3dcdSIlya Dryomov 			      need_resend_linger);
38105aea3dcdSIlya Dryomov 		if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
38115aea3dcdSIlya Dryomov 		    memcmp(&osd->o_con.peer_addr,
38125aea3dcdSIlya Dryomov 			   ceph_osd_addr(osdc->osdmap, osd->o_osd),
38135aea3dcdSIlya Dryomov 			   sizeof(struct ceph_entity_addr)))
38145aea3dcdSIlya Dryomov 			close_osd(osd);
38155aea3dcdSIlya Dryomov 	}
381642c1b124SIlya Dryomov 
381742c1b124SIlya Dryomov 	return 0;
381842c1b124SIlya Dryomov }
38196f6c7006SSage Weil 
38205aea3dcdSIlya Dryomov static void kick_requests(struct ceph_osd_client *osdc,
38215aea3dcdSIlya Dryomov 			  struct rb_root *need_resend,
38225aea3dcdSIlya Dryomov 			  struct list_head *need_resend_linger)
38235aea3dcdSIlya Dryomov {
3824922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq, *nlreq;
382504c7d789SIlya Dryomov 	enum calc_target_result ct_res;
38265aea3dcdSIlya Dryomov 	struct rb_node *n;
38275aea3dcdSIlya Dryomov 
382804c7d789SIlya Dryomov 	/* make sure need_resend targets reflect latest map */
382904c7d789SIlya Dryomov 	for (n = rb_first(need_resend); n; ) {
383004c7d789SIlya Dryomov 		struct ceph_osd_request *req =
383104c7d789SIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
383204c7d789SIlya Dryomov 
383304c7d789SIlya Dryomov 		n = rb_next(n);
383404c7d789SIlya Dryomov 
383504c7d789SIlya Dryomov 		if (req->r_t.epoch < osdc->osdmap->epoch) {
383604c7d789SIlya Dryomov 			ct_res = calc_target(osdc, &req->r_t, NULL, false);
383704c7d789SIlya Dryomov 			if (ct_res == CALC_TARGET_POOL_DNE) {
383804c7d789SIlya Dryomov 				erase_request(need_resend, req);
383904c7d789SIlya Dryomov 				check_pool_dne(req);
384004c7d789SIlya Dryomov 			}
384104c7d789SIlya Dryomov 		}
384204c7d789SIlya Dryomov 	}
384304c7d789SIlya Dryomov 
38445aea3dcdSIlya Dryomov 	for (n = rb_first(need_resend); n; ) {
38455aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
38465aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
38475aea3dcdSIlya Dryomov 		struct ceph_osd *osd;
38485aea3dcdSIlya Dryomov 
38495aea3dcdSIlya Dryomov 		n = rb_next(n);
38505aea3dcdSIlya Dryomov 		erase_request(need_resend, req); /* before link_request() */
38515aea3dcdSIlya Dryomov 
38525aea3dcdSIlya Dryomov 		osd = lookup_create_osd(osdc, req->r_t.osd, true);
38535aea3dcdSIlya Dryomov 		link_request(osd, req);
38545aea3dcdSIlya Dryomov 		if (!req->r_linger) {
38555aea3dcdSIlya Dryomov 			if (!osd_homeless(osd) && !req->r_t.paused)
38565aea3dcdSIlya Dryomov 				send_request(req);
3857922dab61SIlya Dryomov 		} else {
3858922dab61SIlya Dryomov 			cancel_linger_request(req);
38595aea3dcdSIlya Dryomov 		}
38605aea3dcdSIlya Dryomov 	}
3861922dab61SIlya Dryomov 
3862922dab61SIlya Dryomov 	list_for_each_entry_safe(lreq, nlreq, need_resend_linger, scan_item) {
3863922dab61SIlya Dryomov 		if (!osd_homeless(lreq->osd))
3864922dab61SIlya Dryomov 			send_linger(lreq);
3865922dab61SIlya Dryomov 
3866922dab61SIlya Dryomov 		list_del_init(&lreq->scan_item);
3867922dab61SIlya Dryomov 	}
38685aea3dcdSIlya Dryomov }
38695aea3dcdSIlya Dryomov 
38703d14c5d2SYehuda Sadeh /*
38713d14c5d2SYehuda Sadeh  * Process updated osd map.
38723d14c5d2SYehuda Sadeh  *
38733d14c5d2SYehuda Sadeh  * The message contains any number of incremental and full maps, normally
38743d14c5d2SYehuda Sadeh  * indicating some sort of topology change in the cluster.  Kick requests
38753d14c5d2SYehuda Sadeh  * off to different OSDs as needed.
38763d14c5d2SYehuda Sadeh  */
38773d14c5d2SYehuda Sadeh void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
38783d14c5d2SYehuda Sadeh {
387942c1b124SIlya Dryomov 	void *p = msg->front.iov_base;
388042c1b124SIlya Dryomov 	void *const end = p + msg->front.iov_len;
38813d14c5d2SYehuda Sadeh 	u32 nr_maps, maplen;
38823d14c5d2SYehuda Sadeh 	u32 epoch;
38833d14c5d2SYehuda Sadeh 	struct ceph_fsid fsid;
38845aea3dcdSIlya Dryomov 	struct rb_root need_resend = RB_ROOT;
38855aea3dcdSIlya Dryomov 	LIST_HEAD(need_resend_linger);
388642c1b124SIlya Dryomov 	bool handled_incremental = false;
388742c1b124SIlya Dryomov 	bool was_pauserd, was_pausewr;
388842c1b124SIlya Dryomov 	bool pauserd, pausewr;
388942c1b124SIlya Dryomov 	int err;
38903d14c5d2SYehuda Sadeh 
389142c1b124SIlya Dryomov 	dout("%s have %u\n", __func__, osdc->osdmap->epoch);
38925aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
38933d14c5d2SYehuda Sadeh 
38943d14c5d2SYehuda Sadeh 	/* verify fsid */
38953d14c5d2SYehuda Sadeh 	ceph_decode_need(&p, end, sizeof(fsid), bad);
38963d14c5d2SYehuda Sadeh 	ceph_decode_copy(&p, &fsid, sizeof(fsid));
38973d14c5d2SYehuda Sadeh 	if (ceph_check_fsid(osdc->client, &fsid) < 0)
389842c1b124SIlya Dryomov 		goto bad;
38993d14c5d2SYehuda Sadeh 
3900b7ec35b3SIlya Dryomov 	was_pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3901b7ec35b3SIlya Dryomov 	was_pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3902b7ec35b3SIlya Dryomov 		      ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
390342c1b124SIlya Dryomov 		      have_pool_full(osdc);
39049a1ea2dbSJosh Durgin 
39053d14c5d2SYehuda Sadeh 	/* incremental maps */
39063d14c5d2SYehuda Sadeh 	ceph_decode_32_safe(&p, end, nr_maps, bad);
39073d14c5d2SYehuda Sadeh 	dout(" %d inc maps\n", nr_maps);
39083d14c5d2SYehuda Sadeh 	while (nr_maps > 0) {
39093d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, 2*sizeof(u32), bad);
39103d14c5d2SYehuda Sadeh 		epoch = ceph_decode_32(&p);
39113d14c5d2SYehuda Sadeh 		maplen = ceph_decode_32(&p);
39123d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, maplen, bad);
391342c1b124SIlya Dryomov 		if (osdc->osdmap->epoch &&
391442c1b124SIlya Dryomov 		    osdc->osdmap->epoch + 1 == epoch) {
39153d14c5d2SYehuda Sadeh 			dout("applying incremental map %u len %d\n",
39163d14c5d2SYehuda Sadeh 			     epoch, maplen);
39175aea3dcdSIlya Dryomov 			err = handle_one_map(osdc, p, p + maplen, true,
39185aea3dcdSIlya Dryomov 					     &need_resend, &need_resend_linger);
391942c1b124SIlya Dryomov 			if (err)
39203d14c5d2SYehuda Sadeh 				goto bad;
392142c1b124SIlya Dryomov 			handled_incremental = true;
39223d14c5d2SYehuda Sadeh 		} else {
39233d14c5d2SYehuda Sadeh 			dout("ignoring incremental map %u len %d\n",
39243d14c5d2SYehuda Sadeh 			     epoch, maplen);
39253d14c5d2SYehuda Sadeh 		}
392642c1b124SIlya Dryomov 		p += maplen;
39273d14c5d2SYehuda Sadeh 		nr_maps--;
39283d14c5d2SYehuda Sadeh 	}
392942c1b124SIlya Dryomov 	if (handled_incremental)
39303d14c5d2SYehuda Sadeh 		goto done;
39313d14c5d2SYehuda Sadeh 
39323d14c5d2SYehuda Sadeh 	/* full maps */
39333d14c5d2SYehuda Sadeh 	ceph_decode_32_safe(&p, end, nr_maps, bad);
39343d14c5d2SYehuda Sadeh 	dout(" %d full maps\n", nr_maps);
39353d14c5d2SYehuda Sadeh 	while (nr_maps) {
39363d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, 2*sizeof(u32), bad);
39373d14c5d2SYehuda Sadeh 		epoch = ceph_decode_32(&p);
39383d14c5d2SYehuda Sadeh 		maplen = ceph_decode_32(&p);
39393d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, maplen, bad);
39403d14c5d2SYehuda Sadeh 		if (nr_maps > 1) {
39413d14c5d2SYehuda Sadeh 			dout("skipping non-latest full map %u len %d\n",
39423d14c5d2SYehuda Sadeh 			     epoch, maplen);
3943e5253a7bSIlya Dryomov 		} else if (osdc->osdmap->epoch >= epoch) {
39443d14c5d2SYehuda Sadeh 			dout("skipping full map %u len %d, "
39453d14c5d2SYehuda Sadeh 			     "older than our %u\n", epoch, maplen,
39463d14c5d2SYehuda Sadeh 			     osdc->osdmap->epoch);
39473d14c5d2SYehuda Sadeh 		} else {
39483d14c5d2SYehuda Sadeh 			dout("taking full map %u len %d\n", epoch, maplen);
39495aea3dcdSIlya Dryomov 			err = handle_one_map(osdc, p, p + maplen, false,
39505aea3dcdSIlya Dryomov 					     &need_resend, &need_resend_linger);
395142c1b124SIlya Dryomov 			if (err)
39523d14c5d2SYehuda Sadeh 				goto bad;
39533d14c5d2SYehuda Sadeh 		}
39543d14c5d2SYehuda Sadeh 		p += maplen;
39553d14c5d2SYehuda Sadeh 		nr_maps--;
39563d14c5d2SYehuda Sadeh 	}
39573d14c5d2SYehuda Sadeh 
39583d14c5d2SYehuda Sadeh done:
3959cd634fb6SSage Weil 	/*
3960cd634fb6SSage Weil 	 * subscribe to subsequent osdmap updates if full to ensure
3961cd634fb6SSage Weil 	 * we find out when we are no longer full and stop returning
3962cd634fb6SSage Weil 	 * ENOSPC.
3963cd634fb6SSage Weil 	 */
3964b7ec35b3SIlya Dryomov 	pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3965b7ec35b3SIlya Dryomov 	pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3966b7ec35b3SIlya Dryomov 		  ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
396742c1b124SIlya Dryomov 		  have_pool_full(osdc);
396858eb7932SJeff Layton 	if (was_pauserd || was_pausewr || pauserd || pausewr ||
396958eb7932SJeff Layton 	    osdc->osdmap->epoch < osdc->epoch_barrier)
397042c1b124SIlya Dryomov 		maybe_request_map(osdc);
3971cd634fb6SSage Weil 
39725aea3dcdSIlya Dryomov 	kick_requests(osdc, &need_resend, &need_resend_linger);
397342c1b124SIlya Dryomov 
3974fc36d0a4SJeff Layton 	ceph_osdc_abort_on_full(osdc);
397542c1b124SIlya Dryomov 	ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
397642c1b124SIlya Dryomov 			  osdc->osdmap->epoch);
39775aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
39783d14c5d2SYehuda Sadeh 	wake_up_all(&osdc->client->auth_wq);
39793d14c5d2SYehuda Sadeh 	return;
39803d14c5d2SYehuda Sadeh 
39813d14c5d2SYehuda Sadeh bad:
39823d14c5d2SYehuda Sadeh 	pr_err("osdc handle_map corrupt msg\n");
39833d14c5d2SYehuda Sadeh 	ceph_msg_dump(msg);
39845aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
39855aea3dcdSIlya Dryomov }
39865aea3dcdSIlya Dryomov 
39875aea3dcdSIlya Dryomov /*
39885aea3dcdSIlya Dryomov  * Resubmit requests pending on the given osd.
39895aea3dcdSIlya Dryomov  */
39905aea3dcdSIlya Dryomov static void kick_osd_requests(struct ceph_osd *osd)
39915aea3dcdSIlya Dryomov {
39925aea3dcdSIlya Dryomov 	struct rb_node *n;
39935aea3dcdSIlya Dryomov 
3994a02a946dSIlya Dryomov 	clear_backoffs(osd);
3995a02a946dSIlya Dryomov 
3996922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; ) {
39975aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
39985aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
39995aea3dcdSIlya Dryomov 
4000922dab61SIlya Dryomov 		n = rb_next(n); /* cancel_linger_request() */
4001922dab61SIlya Dryomov 
40025aea3dcdSIlya Dryomov 		if (!req->r_linger) {
40035aea3dcdSIlya Dryomov 			if (!req->r_t.paused)
40045aea3dcdSIlya Dryomov 				send_request(req);
4005922dab61SIlya Dryomov 		} else {
4006922dab61SIlya Dryomov 			cancel_linger_request(req);
40075aea3dcdSIlya Dryomov 		}
40085aea3dcdSIlya Dryomov 	}
4009922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_linger_requests); n; n = rb_next(n)) {
4010922dab61SIlya Dryomov 		struct ceph_osd_linger_request *lreq =
4011922dab61SIlya Dryomov 		    rb_entry(n, struct ceph_osd_linger_request, node);
4012922dab61SIlya Dryomov 
4013922dab61SIlya Dryomov 		send_linger(lreq);
4014922dab61SIlya Dryomov 	}
40155aea3dcdSIlya Dryomov }
40165aea3dcdSIlya Dryomov 
40175aea3dcdSIlya Dryomov /*
40185aea3dcdSIlya Dryomov  * If the osd connection drops, we need to resubmit all requests.
40195aea3dcdSIlya Dryomov  */
40205aea3dcdSIlya Dryomov static void osd_fault(struct ceph_connection *con)
40215aea3dcdSIlya Dryomov {
40225aea3dcdSIlya Dryomov 	struct ceph_osd *osd = con->private;
40235aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
40245aea3dcdSIlya Dryomov 
40255aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
40265aea3dcdSIlya Dryomov 
40275aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
40285aea3dcdSIlya Dryomov 	if (!osd_registered(osd)) {
40295aea3dcdSIlya Dryomov 		dout("%s osd%d unknown\n", __func__, osd->o_osd);
40305aea3dcdSIlya Dryomov 		goto out_unlock;
40315aea3dcdSIlya Dryomov 	}
40325aea3dcdSIlya Dryomov 
40335aea3dcdSIlya Dryomov 	if (!reopen_osd(osd))
40345aea3dcdSIlya Dryomov 		kick_osd_requests(osd);
40355aea3dcdSIlya Dryomov 	maybe_request_map(osdc);
40365aea3dcdSIlya Dryomov 
40375aea3dcdSIlya Dryomov out_unlock:
40385aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
40393d14c5d2SYehuda Sadeh }
40403d14c5d2SYehuda Sadeh 
4041a02a946dSIlya Dryomov struct MOSDBackoff {
4042a02a946dSIlya Dryomov 	struct ceph_spg spgid;
4043a02a946dSIlya Dryomov 	u32 map_epoch;
4044a02a946dSIlya Dryomov 	u8 op;
4045a02a946dSIlya Dryomov 	u64 id;
4046a02a946dSIlya Dryomov 	struct ceph_hobject_id *begin;
4047a02a946dSIlya Dryomov 	struct ceph_hobject_id *end;
4048a02a946dSIlya Dryomov };
4049a02a946dSIlya Dryomov 
4050a02a946dSIlya Dryomov static int decode_MOSDBackoff(const struct ceph_msg *msg, struct MOSDBackoff *m)
4051a02a946dSIlya Dryomov {
4052a02a946dSIlya Dryomov 	void *p = msg->front.iov_base;
4053a02a946dSIlya Dryomov 	void *const end = p + msg->front.iov_len;
4054a02a946dSIlya Dryomov 	u8 struct_v;
4055a02a946dSIlya Dryomov 	u32 struct_len;
4056a02a946dSIlya Dryomov 	int ret;
4057a02a946dSIlya Dryomov 
4058a02a946dSIlya Dryomov 	ret = ceph_start_decoding(&p, end, 1, "spg_t", &struct_v, &struct_len);
4059a02a946dSIlya Dryomov 	if (ret)
4060a02a946dSIlya Dryomov 		return ret;
4061a02a946dSIlya Dryomov 
4062a02a946dSIlya Dryomov 	ret = ceph_decode_pgid(&p, end, &m->spgid.pgid);
4063a02a946dSIlya Dryomov 	if (ret)
4064a02a946dSIlya Dryomov 		return ret;
4065a02a946dSIlya Dryomov 
4066a02a946dSIlya Dryomov 	ceph_decode_8_safe(&p, end, m->spgid.shard, e_inval);
4067a02a946dSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->map_epoch, e_inval);
4068a02a946dSIlya Dryomov 	ceph_decode_8_safe(&p, end, m->op, e_inval);
4069a02a946dSIlya Dryomov 	ceph_decode_64_safe(&p, end, m->id, e_inval);
4070a02a946dSIlya Dryomov 
4071a02a946dSIlya Dryomov 	m->begin = kzalloc(sizeof(*m->begin), GFP_NOIO);
4072a02a946dSIlya Dryomov 	if (!m->begin)
4073a02a946dSIlya Dryomov 		return -ENOMEM;
4074a02a946dSIlya Dryomov 
4075a02a946dSIlya Dryomov 	ret = decode_hoid(&p, end, m->begin);
4076a02a946dSIlya Dryomov 	if (ret) {
4077a02a946dSIlya Dryomov 		free_hoid(m->begin);
4078a02a946dSIlya Dryomov 		return ret;
4079a02a946dSIlya Dryomov 	}
4080a02a946dSIlya Dryomov 
4081a02a946dSIlya Dryomov 	m->end = kzalloc(sizeof(*m->end), GFP_NOIO);
4082a02a946dSIlya Dryomov 	if (!m->end) {
4083a02a946dSIlya Dryomov 		free_hoid(m->begin);
4084a02a946dSIlya Dryomov 		return -ENOMEM;
4085a02a946dSIlya Dryomov 	}
4086a02a946dSIlya Dryomov 
4087a02a946dSIlya Dryomov 	ret = decode_hoid(&p, end, m->end);
4088a02a946dSIlya Dryomov 	if (ret) {
4089a02a946dSIlya Dryomov 		free_hoid(m->begin);
4090a02a946dSIlya Dryomov 		free_hoid(m->end);
4091a02a946dSIlya Dryomov 		return ret;
4092a02a946dSIlya Dryomov 	}
4093a02a946dSIlya Dryomov 
4094a02a946dSIlya Dryomov 	return 0;
4095a02a946dSIlya Dryomov 
4096a02a946dSIlya Dryomov e_inval:
4097a02a946dSIlya Dryomov 	return -EINVAL;
4098a02a946dSIlya Dryomov }
4099a02a946dSIlya Dryomov 
4100a02a946dSIlya Dryomov static struct ceph_msg *create_backoff_message(
4101a02a946dSIlya Dryomov 				const struct ceph_osd_backoff *backoff,
4102a02a946dSIlya Dryomov 				u32 map_epoch)
4103a02a946dSIlya Dryomov {
4104a02a946dSIlya Dryomov 	struct ceph_msg *msg;
4105a02a946dSIlya Dryomov 	void *p, *end;
4106a02a946dSIlya Dryomov 	int msg_size;
4107a02a946dSIlya Dryomov 
4108a02a946dSIlya Dryomov 	msg_size = CEPH_ENCODING_START_BLK_LEN +
4109a02a946dSIlya Dryomov 			CEPH_PGID_ENCODING_LEN + 1; /* spgid */
4110a02a946dSIlya Dryomov 	msg_size += 4 + 1 + 8; /* map_epoch, op, id */
4111a02a946dSIlya Dryomov 	msg_size += CEPH_ENCODING_START_BLK_LEN +
4112a02a946dSIlya Dryomov 			hoid_encoding_size(backoff->begin);
4113a02a946dSIlya Dryomov 	msg_size += CEPH_ENCODING_START_BLK_LEN +
4114a02a946dSIlya Dryomov 			hoid_encoding_size(backoff->end);
4115a02a946dSIlya Dryomov 
4116a02a946dSIlya Dryomov 	msg = ceph_msg_new(CEPH_MSG_OSD_BACKOFF, msg_size, GFP_NOIO, true);
4117a02a946dSIlya Dryomov 	if (!msg)
4118a02a946dSIlya Dryomov 		return NULL;
4119a02a946dSIlya Dryomov 
4120a02a946dSIlya Dryomov 	p = msg->front.iov_base;
4121a02a946dSIlya Dryomov 	end = p + msg->front_alloc_len;
4122a02a946dSIlya Dryomov 
4123a02a946dSIlya Dryomov 	encode_spgid(&p, &backoff->spgid);
4124a02a946dSIlya Dryomov 	ceph_encode_32(&p, map_epoch);
4125a02a946dSIlya Dryomov 	ceph_encode_8(&p, CEPH_OSD_BACKOFF_OP_ACK_BLOCK);
4126a02a946dSIlya Dryomov 	ceph_encode_64(&p, backoff->id);
4127a02a946dSIlya Dryomov 	encode_hoid(&p, end, backoff->begin);
4128a02a946dSIlya Dryomov 	encode_hoid(&p, end, backoff->end);
4129a02a946dSIlya Dryomov 	BUG_ON(p != end);
4130a02a946dSIlya Dryomov 
4131a02a946dSIlya Dryomov 	msg->front.iov_len = p - msg->front.iov_base;
4132a02a946dSIlya Dryomov 	msg->hdr.version = cpu_to_le16(1); /* MOSDBackoff v1 */
4133a02a946dSIlya Dryomov 	msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
4134a02a946dSIlya Dryomov 
4135a02a946dSIlya Dryomov 	return msg;
4136a02a946dSIlya Dryomov }
4137a02a946dSIlya Dryomov 
4138a02a946dSIlya Dryomov static void handle_backoff_block(struct ceph_osd *osd, struct MOSDBackoff *m)
4139a02a946dSIlya Dryomov {
4140a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
4141a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
4142a02a946dSIlya Dryomov 	struct ceph_msg *msg;
4143a02a946dSIlya Dryomov 
4144a02a946dSIlya Dryomov 	dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4145a02a946dSIlya Dryomov 	     m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4146a02a946dSIlya Dryomov 
4147a02a946dSIlya Dryomov 	spg = lookup_spg_mapping(&osd->o_backoff_mappings, &m->spgid);
4148a02a946dSIlya Dryomov 	if (!spg) {
4149a02a946dSIlya Dryomov 		spg = alloc_spg_mapping();
4150a02a946dSIlya Dryomov 		if (!spg) {
4151a02a946dSIlya Dryomov 			pr_err("%s failed to allocate spg\n", __func__);
4152a02a946dSIlya Dryomov 			return;
4153a02a946dSIlya Dryomov 		}
4154a02a946dSIlya Dryomov 		spg->spgid = m->spgid; /* struct */
4155a02a946dSIlya Dryomov 		insert_spg_mapping(&osd->o_backoff_mappings, spg);
4156a02a946dSIlya Dryomov 	}
4157a02a946dSIlya Dryomov 
4158a02a946dSIlya Dryomov 	backoff = alloc_backoff();
4159a02a946dSIlya Dryomov 	if (!backoff) {
4160a02a946dSIlya Dryomov 		pr_err("%s failed to allocate backoff\n", __func__);
4161a02a946dSIlya Dryomov 		return;
4162a02a946dSIlya Dryomov 	}
4163a02a946dSIlya Dryomov 	backoff->spgid = m->spgid; /* struct */
4164a02a946dSIlya Dryomov 	backoff->id = m->id;
4165a02a946dSIlya Dryomov 	backoff->begin = m->begin;
4166a02a946dSIlya Dryomov 	m->begin = NULL; /* backoff now owns this */
4167a02a946dSIlya Dryomov 	backoff->end = m->end;
4168a02a946dSIlya Dryomov 	m->end = NULL;   /* ditto */
4169a02a946dSIlya Dryomov 
4170a02a946dSIlya Dryomov 	insert_backoff(&spg->backoffs, backoff);
4171a02a946dSIlya Dryomov 	insert_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4172a02a946dSIlya Dryomov 
4173a02a946dSIlya Dryomov 	/*
4174a02a946dSIlya Dryomov 	 * Ack with original backoff's epoch so that the OSD can
4175a02a946dSIlya Dryomov 	 * discard this if there was a PG split.
4176a02a946dSIlya Dryomov 	 */
4177a02a946dSIlya Dryomov 	msg = create_backoff_message(backoff, m->map_epoch);
4178a02a946dSIlya Dryomov 	if (!msg) {
4179a02a946dSIlya Dryomov 		pr_err("%s failed to allocate msg\n", __func__);
4180a02a946dSIlya Dryomov 		return;
4181a02a946dSIlya Dryomov 	}
4182a02a946dSIlya Dryomov 	ceph_con_send(&osd->o_con, msg);
4183a02a946dSIlya Dryomov }
4184a02a946dSIlya Dryomov 
4185a02a946dSIlya Dryomov static bool target_contained_by(const struct ceph_osd_request_target *t,
4186a02a946dSIlya Dryomov 				const struct ceph_hobject_id *begin,
4187a02a946dSIlya Dryomov 				const struct ceph_hobject_id *end)
4188a02a946dSIlya Dryomov {
4189a02a946dSIlya Dryomov 	struct ceph_hobject_id hoid;
4190a02a946dSIlya Dryomov 	int cmp;
4191a02a946dSIlya Dryomov 
4192a02a946dSIlya Dryomov 	hoid_fill_from_target(&hoid, t);
4193a02a946dSIlya Dryomov 	cmp = hoid_compare(&hoid, begin);
4194a02a946dSIlya Dryomov 	return !cmp || (cmp > 0 && hoid_compare(&hoid, end) < 0);
4195a02a946dSIlya Dryomov }
4196a02a946dSIlya Dryomov 
4197a02a946dSIlya Dryomov static void handle_backoff_unblock(struct ceph_osd *osd,
4198a02a946dSIlya Dryomov 				   const struct MOSDBackoff *m)
4199a02a946dSIlya Dryomov {
4200a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
4201a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
4202a02a946dSIlya Dryomov 	struct rb_node *n;
4203a02a946dSIlya Dryomov 
4204a02a946dSIlya Dryomov 	dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4205a02a946dSIlya Dryomov 	     m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4206a02a946dSIlya Dryomov 
4207a02a946dSIlya Dryomov 	backoff = lookup_backoff_by_id(&osd->o_backoffs_by_id, m->id);
4208a02a946dSIlya Dryomov 	if (!backoff) {
4209a02a946dSIlya Dryomov 		pr_err("%s osd%d spgid %llu.%xs%d id %llu backoff dne\n",
4210a02a946dSIlya Dryomov 		       __func__, osd->o_osd, m->spgid.pgid.pool,
4211a02a946dSIlya Dryomov 		       m->spgid.pgid.seed, m->spgid.shard, m->id);
4212a02a946dSIlya Dryomov 		return;
4213a02a946dSIlya Dryomov 	}
4214a02a946dSIlya Dryomov 
4215a02a946dSIlya Dryomov 	if (hoid_compare(backoff->begin, m->begin) &&
4216a02a946dSIlya Dryomov 	    hoid_compare(backoff->end, m->end)) {
4217a02a946dSIlya Dryomov 		pr_err("%s osd%d spgid %llu.%xs%d id %llu bad range?\n",
4218a02a946dSIlya Dryomov 		       __func__, osd->o_osd, m->spgid.pgid.pool,
4219a02a946dSIlya Dryomov 		       m->spgid.pgid.seed, m->spgid.shard, m->id);
4220a02a946dSIlya Dryomov 		/* unblock it anyway... */
4221a02a946dSIlya Dryomov 	}
4222a02a946dSIlya Dryomov 
4223a02a946dSIlya Dryomov 	spg = lookup_spg_mapping(&osd->o_backoff_mappings, &backoff->spgid);
4224a02a946dSIlya Dryomov 	BUG_ON(!spg);
4225a02a946dSIlya Dryomov 
4226a02a946dSIlya Dryomov 	erase_backoff(&spg->backoffs, backoff);
4227a02a946dSIlya Dryomov 	erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4228a02a946dSIlya Dryomov 	free_backoff(backoff);
4229a02a946dSIlya Dryomov 
4230a02a946dSIlya Dryomov 	if (RB_EMPTY_ROOT(&spg->backoffs)) {
4231a02a946dSIlya Dryomov 		erase_spg_mapping(&osd->o_backoff_mappings, spg);
4232a02a946dSIlya Dryomov 		free_spg_mapping(spg);
4233a02a946dSIlya Dryomov 	}
4234a02a946dSIlya Dryomov 
4235a02a946dSIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
4236a02a946dSIlya Dryomov 		struct ceph_osd_request *req =
4237a02a946dSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
4238a02a946dSIlya Dryomov 
4239a02a946dSIlya Dryomov 		if (!ceph_spg_compare(&req->r_t.spgid, &m->spgid)) {
4240a02a946dSIlya Dryomov 			/*
4241a02a946dSIlya Dryomov 			 * Match against @m, not @backoff -- the PG may
4242a02a946dSIlya Dryomov 			 * have split on the OSD.
4243a02a946dSIlya Dryomov 			 */
4244a02a946dSIlya Dryomov 			if (target_contained_by(&req->r_t, m->begin, m->end)) {
4245a02a946dSIlya Dryomov 				/*
4246a02a946dSIlya Dryomov 				 * If no other installed backoff applies,
4247a02a946dSIlya Dryomov 				 * resend.
4248a02a946dSIlya Dryomov 				 */
4249a02a946dSIlya Dryomov 				send_request(req);
4250a02a946dSIlya Dryomov 			}
4251a02a946dSIlya Dryomov 		}
4252a02a946dSIlya Dryomov 	}
4253a02a946dSIlya Dryomov }
4254a02a946dSIlya Dryomov 
4255a02a946dSIlya Dryomov static void handle_backoff(struct ceph_osd *osd, struct ceph_msg *msg)
4256a02a946dSIlya Dryomov {
4257a02a946dSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
4258a02a946dSIlya Dryomov 	struct MOSDBackoff m;
4259a02a946dSIlya Dryomov 	int ret;
4260a02a946dSIlya Dryomov 
4261a02a946dSIlya Dryomov 	down_read(&osdc->lock);
4262a02a946dSIlya Dryomov 	if (!osd_registered(osd)) {
4263a02a946dSIlya Dryomov 		dout("%s osd%d unknown\n", __func__, osd->o_osd);
4264a02a946dSIlya Dryomov 		up_read(&osdc->lock);
4265a02a946dSIlya Dryomov 		return;
4266a02a946dSIlya Dryomov 	}
4267a02a946dSIlya Dryomov 	WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
4268a02a946dSIlya Dryomov 
4269a02a946dSIlya Dryomov 	mutex_lock(&osd->lock);
4270a02a946dSIlya Dryomov 	ret = decode_MOSDBackoff(msg, &m);
4271a02a946dSIlya Dryomov 	if (ret) {
4272a02a946dSIlya Dryomov 		pr_err("failed to decode MOSDBackoff: %d\n", ret);
4273a02a946dSIlya Dryomov 		ceph_msg_dump(msg);
4274a02a946dSIlya Dryomov 		goto out_unlock;
4275a02a946dSIlya Dryomov 	}
4276a02a946dSIlya Dryomov 
4277a02a946dSIlya Dryomov 	switch (m.op) {
4278a02a946dSIlya Dryomov 	case CEPH_OSD_BACKOFF_OP_BLOCK:
4279a02a946dSIlya Dryomov 		handle_backoff_block(osd, &m);
4280a02a946dSIlya Dryomov 		break;
4281a02a946dSIlya Dryomov 	case CEPH_OSD_BACKOFF_OP_UNBLOCK:
4282a02a946dSIlya Dryomov 		handle_backoff_unblock(osd, &m);
4283a02a946dSIlya Dryomov 		break;
4284a02a946dSIlya Dryomov 	default:
4285a02a946dSIlya Dryomov 		pr_err("%s osd%d unknown op %d\n", __func__, osd->o_osd, m.op);
4286a02a946dSIlya Dryomov 	}
4287a02a946dSIlya Dryomov 
4288a02a946dSIlya Dryomov 	free_hoid(m.begin);
4289a02a946dSIlya Dryomov 	free_hoid(m.end);
4290a02a946dSIlya Dryomov 
4291a02a946dSIlya Dryomov out_unlock:
4292a02a946dSIlya Dryomov 	mutex_unlock(&osd->lock);
4293a02a946dSIlya Dryomov 	up_read(&osdc->lock);
4294a02a946dSIlya Dryomov }
4295a02a946dSIlya Dryomov 
42963d14c5d2SYehuda Sadeh /*
4297a40c4f10SYehuda Sadeh  * Process osd watch notifications
4298a40c4f10SYehuda Sadeh  */
42993c663bbdSAlex Elder static void handle_watch_notify(struct ceph_osd_client *osdc,
43003c663bbdSAlex Elder 				struct ceph_msg *msg)
4301a40c4f10SYehuda Sadeh {
4302922dab61SIlya Dryomov 	void *p = msg->front.iov_base;
4303922dab61SIlya Dryomov 	void *const end = p + msg->front.iov_len;
4304922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
4305922dab61SIlya Dryomov 	struct linger_work *lwork;
4306922dab61SIlya Dryomov 	u8 proto_ver, opcode;
4307922dab61SIlya Dryomov 	u64 cookie, notify_id;
4308922dab61SIlya Dryomov 	u64 notifier_id = 0;
430919079203SIlya Dryomov 	s32 return_code = 0;
4310922dab61SIlya Dryomov 	void *payload = NULL;
4311922dab61SIlya Dryomov 	u32 payload_len = 0;
4312a40c4f10SYehuda Sadeh 
4313a40c4f10SYehuda Sadeh 	ceph_decode_8_safe(&p, end, proto_ver, bad);
4314a40c4f10SYehuda Sadeh 	ceph_decode_8_safe(&p, end, opcode, bad);
4315a40c4f10SYehuda Sadeh 	ceph_decode_64_safe(&p, end, cookie, bad);
4316922dab61SIlya Dryomov 	p += 8; /* skip ver */
4317a40c4f10SYehuda Sadeh 	ceph_decode_64_safe(&p, end, notify_id, bad);
4318a40c4f10SYehuda Sadeh 
4319922dab61SIlya Dryomov 	if (proto_ver >= 1) {
4320922dab61SIlya Dryomov 		ceph_decode_32_safe(&p, end, payload_len, bad);
4321922dab61SIlya Dryomov 		ceph_decode_need(&p, end, payload_len, bad);
4322922dab61SIlya Dryomov 		payload = p;
4323922dab61SIlya Dryomov 		p += payload_len;
4324a40c4f10SYehuda Sadeh 	}
4325a40c4f10SYehuda Sadeh 
4326922dab61SIlya Dryomov 	if (le16_to_cpu(msg->hdr.version) >= 2)
432719079203SIlya Dryomov 		ceph_decode_32_safe(&p, end, return_code, bad);
4328922dab61SIlya Dryomov 
4329922dab61SIlya Dryomov 	if (le16_to_cpu(msg->hdr.version) >= 3)
4330922dab61SIlya Dryomov 		ceph_decode_64_safe(&p, end, notifier_id, bad);
4331922dab61SIlya Dryomov 
4332922dab61SIlya Dryomov 	down_read(&osdc->lock);
4333922dab61SIlya Dryomov 	lreq = lookup_linger_osdc(&osdc->linger_requests, cookie);
4334922dab61SIlya Dryomov 	if (!lreq) {
4335922dab61SIlya Dryomov 		dout("%s opcode %d cookie %llu dne\n", __func__, opcode,
4336922dab61SIlya Dryomov 		     cookie);
4337922dab61SIlya Dryomov 		goto out_unlock_osdc;
4338922dab61SIlya Dryomov 	}
4339922dab61SIlya Dryomov 
4340922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
434119079203SIlya Dryomov 	dout("%s opcode %d cookie %llu lreq %p is_watch %d\n", __func__,
434219079203SIlya Dryomov 	     opcode, cookie, lreq, lreq->is_watch);
4343922dab61SIlya Dryomov 	if (opcode == CEPH_WATCH_EVENT_DISCONNECT) {
4344922dab61SIlya Dryomov 		if (!lreq->last_error) {
4345922dab61SIlya Dryomov 			lreq->last_error = -ENOTCONN;
4346922dab61SIlya Dryomov 			queue_watch_error(lreq);
4347922dab61SIlya Dryomov 		}
434819079203SIlya Dryomov 	} else if (!lreq->is_watch) {
434919079203SIlya Dryomov 		/* CEPH_WATCH_EVENT_NOTIFY_COMPLETE */
435019079203SIlya Dryomov 		if (lreq->notify_id && lreq->notify_id != notify_id) {
435119079203SIlya Dryomov 			dout("lreq %p notify_id %llu != %llu, ignoring\n", lreq,
435219079203SIlya Dryomov 			     lreq->notify_id, notify_id);
435319079203SIlya Dryomov 		} else if (!completion_done(&lreq->notify_finish_wait)) {
435419079203SIlya Dryomov 			struct ceph_msg_data *data =
435519079203SIlya Dryomov 			    list_first_entry_or_null(&msg->data,
435619079203SIlya Dryomov 						     struct ceph_msg_data,
435719079203SIlya Dryomov 						     links);
435819079203SIlya Dryomov 
435919079203SIlya Dryomov 			if (data) {
436019079203SIlya Dryomov 				if (lreq->preply_pages) {
436119079203SIlya Dryomov 					WARN_ON(data->type !=
436219079203SIlya Dryomov 							CEPH_MSG_DATA_PAGES);
436319079203SIlya Dryomov 					*lreq->preply_pages = data->pages;
436419079203SIlya Dryomov 					*lreq->preply_len = data->length;
436519079203SIlya Dryomov 				} else {
436619079203SIlya Dryomov 					ceph_release_page_vector(data->pages,
436719079203SIlya Dryomov 					       calc_pages_for(0, data->length));
436819079203SIlya Dryomov 				}
436919079203SIlya Dryomov 			}
437019079203SIlya Dryomov 			lreq->notify_finish_error = return_code;
437119079203SIlya Dryomov 			complete_all(&lreq->notify_finish_wait);
437219079203SIlya Dryomov 		}
4373922dab61SIlya Dryomov 	} else {
4374922dab61SIlya Dryomov 		/* CEPH_WATCH_EVENT_NOTIFY */
4375922dab61SIlya Dryomov 		lwork = lwork_alloc(lreq, do_watch_notify);
4376922dab61SIlya Dryomov 		if (!lwork) {
4377922dab61SIlya Dryomov 			pr_err("failed to allocate notify-lwork\n");
4378922dab61SIlya Dryomov 			goto out_unlock_lreq;
4379922dab61SIlya Dryomov 		}
4380922dab61SIlya Dryomov 
4381922dab61SIlya Dryomov 		lwork->notify.notify_id = notify_id;
4382922dab61SIlya Dryomov 		lwork->notify.notifier_id = notifier_id;
4383922dab61SIlya Dryomov 		lwork->notify.payload = payload;
4384922dab61SIlya Dryomov 		lwork->notify.payload_len = payload_len;
4385922dab61SIlya Dryomov 		lwork->notify.msg = ceph_msg_get(msg);
4386922dab61SIlya Dryomov 		lwork_queue(lwork);
4387922dab61SIlya Dryomov 	}
4388922dab61SIlya Dryomov 
4389922dab61SIlya Dryomov out_unlock_lreq:
4390922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
4391922dab61SIlya Dryomov out_unlock_osdc:
4392922dab61SIlya Dryomov 	up_read(&osdc->lock);
4393a40c4f10SYehuda Sadeh 	return;
4394a40c4f10SYehuda Sadeh 
4395a40c4f10SYehuda Sadeh bad:
4396a40c4f10SYehuda Sadeh 	pr_err("osdc handle_watch_notify corrupt msg\n");
4397a40c4f10SYehuda Sadeh }
4398a40c4f10SYehuda Sadeh 
4399a40c4f10SYehuda Sadeh /*
44003d14c5d2SYehuda Sadeh  * Register request, send initial attempt.
44013d14c5d2SYehuda Sadeh  */
44023d14c5d2SYehuda Sadeh int ceph_osdc_start_request(struct ceph_osd_client *osdc,
44033d14c5d2SYehuda Sadeh 			    struct ceph_osd_request *req,
44043d14c5d2SYehuda Sadeh 			    bool nofail)
44053d14c5d2SYehuda Sadeh {
44065aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
44075aea3dcdSIlya Dryomov 	submit_request(req, false);
44085aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
44093d14c5d2SYehuda Sadeh 
44105aea3dcdSIlya Dryomov 	return 0;
44113d14c5d2SYehuda Sadeh }
44123d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_start_request);
44133d14c5d2SYehuda Sadeh 
44143d14c5d2SYehuda Sadeh /*
4415c297eb42SIlya Dryomov  * Unregister a registered request.  The request is not completed:
4416c297eb42SIlya Dryomov  * ->r_result isn't set and __complete_request() isn't called.
4417c9f9b93dSIlya Dryomov  */
4418c9f9b93dSIlya Dryomov void ceph_osdc_cancel_request(struct ceph_osd_request *req)
4419c9f9b93dSIlya Dryomov {
4420c9f9b93dSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
4421c9f9b93dSIlya Dryomov 
44225aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
44235aea3dcdSIlya Dryomov 	if (req->r_osd)
44245aea3dcdSIlya Dryomov 		cancel_request(req);
44255aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
4426c9f9b93dSIlya Dryomov }
4427c9f9b93dSIlya Dryomov EXPORT_SYMBOL(ceph_osdc_cancel_request);
4428c9f9b93dSIlya Dryomov 
4429c9f9b93dSIlya Dryomov /*
443042b06965SIlya Dryomov  * @timeout: in jiffies, 0 means "wait forever"
443142b06965SIlya Dryomov  */
443242b06965SIlya Dryomov static int wait_request_timeout(struct ceph_osd_request *req,
443342b06965SIlya Dryomov 				unsigned long timeout)
443442b06965SIlya Dryomov {
443542b06965SIlya Dryomov 	long left;
443642b06965SIlya Dryomov 
443742b06965SIlya Dryomov 	dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
44380e76abf2SYan, Zheng 	left = wait_for_completion_killable_timeout(&req->r_completion,
443942b06965SIlya Dryomov 						ceph_timeout_jiffies(timeout));
444042b06965SIlya Dryomov 	if (left <= 0) {
444142b06965SIlya Dryomov 		left = left ?: -ETIMEDOUT;
444242b06965SIlya Dryomov 		ceph_osdc_cancel_request(req);
444342b06965SIlya Dryomov 	} else {
444442b06965SIlya Dryomov 		left = req->r_result; /* completed */
444542b06965SIlya Dryomov 	}
444642b06965SIlya Dryomov 
444742b06965SIlya Dryomov 	return left;
444842b06965SIlya Dryomov }
444942b06965SIlya Dryomov 
445042b06965SIlya Dryomov /*
44513d14c5d2SYehuda Sadeh  * wait for a request to complete
44523d14c5d2SYehuda Sadeh  */
44533d14c5d2SYehuda Sadeh int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
44543d14c5d2SYehuda Sadeh 			   struct ceph_osd_request *req)
44553d14c5d2SYehuda Sadeh {
445642b06965SIlya Dryomov 	return wait_request_timeout(req, 0);
44573d14c5d2SYehuda Sadeh }
44583d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_wait_request);
44593d14c5d2SYehuda Sadeh 
44603d14c5d2SYehuda Sadeh /*
44613d14c5d2SYehuda Sadeh  * sync - wait for all in-flight requests to flush.  avoid starvation.
44623d14c5d2SYehuda Sadeh  */
44633d14c5d2SYehuda Sadeh void ceph_osdc_sync(struct ceph_osd_client *osdc)
44643d14c5d2SYehuda Sadeh {
44655aea3dcdSIlya Dryomov 	struct rb_node *n, *p;
44665aea3dcdSIlya Dryomov 	u64 last_tid = atomic64_read(&osdc->last_tid);
44673d14c5d2SYehuda Sadeh 
44685aea3dcdSIlya Dryomov again:
44695aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
44705aea3dcdSIlya Dryomov 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
44715aea3dcdSIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
44725aea3dcdSIlya Dryomov 
44735aea3dcdSIlya Dryomov 		mutex_lock(&osd->lock);
44745aea3dcdSIlya Dryomov 		for (p = rb_first(&osd->o_requests); p; p = rb_next(p)) {
44755aea3dcdSIlya Dryomov 			struct ceph_osd_request *req =
44765aea3dcdSIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
44775aea3dcdSIlya Dryomov 
44783d14c5d2SYehuda Sadeh 			if (req->r_tid > last_tid)
44793d14c5d2SYehuda Sadeh 				break;
44803d14c5d2SYehuda Sadeh 
44815aea3dcdSIlya Dryomov 			if (!(req->r_flags & CEPH_OSD_FLAG_WRITE))
44823d14c5d2SYehuda Sadeh 				continue;
44833d14c5d2SYehuda Sadeh 
44843d14c5d2SYehuda Sadeh 			ceph_osdc_get_request(req);
44855aea3dcdSIlya Dryomov 			mutex_unlock(&osd->lock);
44865aea3dcdSIlya Dryomov 			up_read(&osdc->lock);
44875aea3dcdSIlya Dryomov 			dout("%s waiting on req %p tid %llu last_tid %llu\n",
44885aea3dcdSIlya Dryomov 			     __func__, req, req->r_tid, last_tid);
4489b18b9550SIlya Dryomov 			wait_for_completion(&req->r_completion);
44903d14c5d2SYehuda Sadeh 			ceph_osdc_put_request(req);
44915aea3dcdSIlya Dryomov 			goto again;
44923d14c5d2SYehuda Sadeh 		}
44935aea3dcdSIlya Dryomov 
44945aea3dcdSIlya Dryomov 		mutex_unlock(&osd->lock);
44955aea3dcdSIlya Dryomov 	}
44965aea3dcdSIlya Dryomov 
44975aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
44985aea3dcdSIlya Dryomov 	dout("%s done last_tid %llu\n", __func__, last_tid);
44993d14c5d2SYehuda Sadeh }
45003d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_sync);
45013d14c5d2SYehuda Sadeh 
4502922dab61SIlya Dryomov static struct ceph_osd_request *
4503922dab61SIlya Dryomov alloc_linger_request(struct ceph_osd_linger_request *lreq)
4504922dab61SIlya Dryomov {
4505922dab61SIlya Dryomov 	struct ceph_osd_request *req;
4506922dab61SIlya Dryomov 
4507922dab61SIlya Dryomov 	req = ceph_osdc_alloc_request(lreq->osdc, NULL, 1, false, GFP_NOIO);
4508922dab61SIlya Dryomov 	if (!req)
4509922dab61SIlya Dryomov 		return NULL;
4510922dab61SIlya Dryomov 
4511922dab61SIlya Dryomov 	ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
4512922dab61SIlya Dryomov 	ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
4513922dab61SIlya Dryomov 
4514922dab61SIlya Dryomov 	if (ceph_osdc_alloc_messages(req, GFP_NOIO)) {
4515922dab61SIlya Dryomov 		ceph_osdc_put_request(req);
4516922dab61SIlya Dryomov 		return NULL;
4517922dab61SIlya Dryomov 	}
4518922dab61SIlya Dryomov 
4519922dab61SIlya Dryomov 	return req;
4520922dab61SIlya Dryomov }
4521922dab61SIlya Dryomov 
4522922dab61SIlya Dryomov /*
4523922dab61SIlya Dryomov  * Returns a handle, caller owns a ref.
4524922dab61SIlya Dryomov  */
4525922dab61SIlya Dryomov struct ceph_osd_linger_request *
4526922dab61SIlya Dryomov ceph_osdc_watch(struct ceph_osd_client *osdc,
4527922dab61SIlya Dryomov 		struct ceph_object_id *oid,
4528922dab61SIlya Dryomov 		struct ceph_object_locator *oloc,
4529922dab61SIlya Dryomov 		rados_watchcb2_t wcb,
4530922dab61SIlya Dryomov 		rados_watcherrcb_t errcb,
4531922dab61SIlya Dryomov 		void *data)
4532922dab61SIlya Dryomov {
4533922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
4534922dab61SIlya Dryomov 	int ret;
4535922dab61SIlya Dryomov 
4536922dab61SIlya Dryomov 	lreq = linger_alloc(osdc);
4537922dab61SIlya Dryomov 	if (!lreq)
4538922dab61SIlya Dryomov 		return ERR_PTR(-ENOMEM);
4539922dab61SIlya Dryomov 
454019079203SIlya Dryomov 	lreq->is_watch = true;
4541922dab61SIlya Dryomov 	lreq->wcb = wcb;
4542922dab61SIlya Dryomov 	lreq->errcb = errcb;
4543922dab61SIlya Dryomov 	lreq->data = data;
4544b07d3c4bSIlya Dryomov 	lreq->watch_valid_thru = jiffies;
4545922dab61SIlya Dryomov 
4546922dab61SIlya Dryomov 	ceph_oid_copy(&lreq->t.base_oid, oid);
4547922dab61SIlya Dryomov 	ceph_oloc_copy(&lreq->t.base_oloc, oloc);
454854ea0046SIlya Dryomov 	lreq->t.flags = CEPH_OSD_FLAG_WRITE;
45491134e091SDeepa Dinamani 	ktime_get_real_ts(&lreq->mtime);
4550922dab61SIlya Dryomov 
4551922dab61SIlya Dryomov 	lreq->reg_req = alloc_linger_request(lreq);
4552922dab61SIlya Dryomov 	if (!lreq->reg_req) {
4553922dab61SIlya Dryomov 		ret = -ENOMEM;
4554922dab61SIlya Dryomov 		goto err_put_lreq;
4555922dab61SIlya Dryomov 	}
4556922dab61SIlya Dryomov 
4557922dab61SIlya Dryomov 	lreq->ping_req = alloc_linger_request(lreq);
4558922dab61SIlya Dryomov 	if (!lreq->ping_req) {
4559922dab61SIlya Dryomov 		ret = -ENOMEM;
4560922dab61SIlya Dryomov 		goto err_put_lreq;
4561922dab61SIlya Dryomov 	}
4562922dab61SIlya Dryomov 
4563922dab61SIlya Dryomov 	down_write(&osdc->lock);
4564922dab61SIlya Dryomov 	linger_register(lreq); /* before osd_req_op_* */
4565922dab61SIlya Dryomov 	osd_req_op_watch_init(lreq->reg_req, 0, lreq->linger_id,
4566922dab61SIlya Dryomov 			      CEPH_OSD_WATCH_OP_WATCH);
4567922dab61SIlya Dryomov 	osd_req_op_watch_init(lreq->ping_req, 0, lreq->linger_id,
4568922dab61SIlya Dryomov 			      CEPH_OSD_WATCH_OP_PING);
4569922dab61SIlya Dryomov 	linger_submit(lreq);
4570922dab61SIlya Dryomov 	up_write(&osdc->lock);
4571922dab61SIlya Dryomov 
4572922dab61SIlya Dryomov 	ret = linger_reg_commit_wait(lreq);
4573922dab61SIlya Dryomov 	if (ret) {
4574922dab61SIlya Dryomov 		linger_cancel(lreq);
4575922dab61SIlya Dryomov 		goto err_put_lreq;
4576922dab61SIlya Dryomov 	}
4577922dab61SIlya Dryomov 
4578922dab61SIlya Dryomov 	return lreq;
4579922dab61SIlya Dryomov 
4580922dab61SIlya Dryomov err_put_lreq:
4581922dab61SIlya Dryomov 	linger_put(lreq);
4582922dab61SIlya Dryomov 	return ERR_PTR(ret);
4583922dab61SIlya Dryomov }
4584922dab61SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_watch);
4585922dab61SIlya Dryomov 
4586922dab61SIlya Dryomov /*
4587922dab61SIlya Dryomov  * Releases a ref.
4588922dab61SIlya Dryomov  *
4589922dab61SIlya Dryomov  * Times out after mount_timeout to preserve rbd unmap behaviour
4590922dab61SIlya Dryomov  * introduced in 2894e1d76974 ("rbd: timeout watch teardown on unmap
4591922dab61SIlya Dryomov  * with mount_timeout").
4592922dab61SIlya Dryomov  */
4593922dab61SIlya Dryomov int ceph_osdc_unwatch(struct ceph_osd_client *osdc,
4594922dab61SIlya Dryomov 		      struct ceph_osd_linger_request *lreq)
4595922dab61SIlya Dryomov {
4596922dab61SIlya Dryomov 	struct ceph_options *opts = osdc->client->options;
4597922dab61SIlya Dryomov 	struct ceph_osd_request *req;
4598922dab61SIlya Dryomov 	int ret;
4599922dab61SIlya Dryomov 
4600922dab61SIlya Dryomov 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4601922dab61SIlya Dryomov 	if (!req)
4602922dab61SIlya Dryomov 		return -ENOMEM;
4603922dab61SIlya Dryomov 
4604922dab61SIlya Dryomov 	ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
4605922dab61SIlya Dryomov 	ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
460654ea0046SIlya Dryomov 	req->r_flags = CEPH_OSD_FLAG_WRITE;
46071134e091SDeepa Dinamani 	ktime_get_real_ts(&req->r_mtime);
4608922dab61SIlya Dryomov 	osd_req_op_watch_init(req, 0, lreq->linger_id,
4609922dab61SIlya Dryomov 			      CEPH_OSD_WATCH_OP_UNWATCH);
4610922dab61SIlya Dryomov 
4611922dab61SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4612922dab61SIlya Dryomov 	if (ret)
4613922dab61SIlya Dryomov 		goto out_put_req;
4614922dab61SIlya Dryomov 
4615922dab61SIlya Dryomov 	ceph_osdc_start_request(osdc, req, false);
4616922dab61SIlya Dryomov 	linger_cancel(lreq);
4617922dab61SIlya Dryomov 	linger_put(lreq);
4618922dab61SIlya Dryomov 	ret = wait_request_timeout(req, opts->mount_timeout);
4619922dab61SIlya Dryomov 
4620922dab61SIlya Dryomov out_put_req:
4621922dab61SIlya Dryomov 	ceph_osdc_put_request(req);
4622922dab61SIlya Dryomov 	return ret;
4623922dab61SIlya Dryomov }
4624922dab61SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_unwatch);
4625922dab61SIlya Dryomov 
4626922dab61SIlya Dryomov static int osd_req_op_notify_ack_init(struct ceph_osd_request *req, int which,
4627922dab61SIlya Dryomov 				      u64 notify_id, u64 cookie, void *payload,
4628922dab61SIlya Dryomov 				      size_t payload_len)
4629922dab61SIlya Dryomov {
4630922dab61SIlya Dryomov 	struct ceph_osd_req_op *op;
4631922dab61SIlya Dryomov 	struct ceph_pagelist *pl;
4632922dab61SIlya Dryomov 	int ret;
4633922dab61SIlya Dryomov 
4634922dab61SIlya Dryomov 	op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY_ACK, 0);
4635922dab61SIlya Dryomov 
4636922dab61SIlya Dryomov 	pl = kmalloc(sizeof(*pl), GFP_NOIO);
4637922dab61SIlya Dryomov 	if (!pl)
4638922dab61SIlya Dryomov 		return -ENOMEM;
4639922dab61SIlya Dryomov 
4640922dab61SIlya Dryomov 	ceph_pagelist_init(pl);
4641922dab61SIlya Dryomov 	ret = ceph_pagelist_encode_64(pl, notify_id);
4642922dab61SIlya Dryomov 	ret |= ceph_pagelist_encode_64(pl, cookie);
4643922dab61SIlya Dryomov 	if (payload) {
4644922dab61SIlya Dryomov 		ret |= ceph_pagelist_encode_32(pl, payload_len);
4645922dab61SIlya Dryomov 		ret |= ceph_pagelist_append(pl, payload, payload_len);
4646922dab61SIlya Dryomov 	} else {
4647922dab61SIlya Dryomov 		ret |= ceph_pagelist_encode_32(pl, 0);
4648922dab61SIlya Dryomov 	}
4649922dab61SIlya Dryomov 	if (ret) {
4650922dab61SIlya Dryomov 		ceph_pagelist_release(pl);
4651922dab61SIlya Dryomov 		return -ENOMEM;
4652922dab61SIlya Dryomov 	}
4653922dab61SIlya Dryomov 
4654922dab61SIlya Dryomov 	ceph_osd_data_pagelist_init(&op->notify_ack.request_data, pl);
4655922dab61SIlya Dryomov 	op->indata_len = pl->length;
4656922dab61SIlya Dryomov 	return 0;
4657922dab61SIlya Dryomov }
4658922dab61SIlya Dryomov 
4659922dab61SIlya Dryomov int ceph_osdc_notify_ack(struct ceph_osd_client *osdc,
4660922dab61SIlya Dryomov 			 struct ceph_object_id *oid,
4661922dab61SIlya Dryomov 			 struct ceph_object_locator *oloc,
4662922dab61SIlya Dryomov 			 u64 notify_id,
4663922dab61SIlya Dryomov 			 u64 cookie,
4664922dab61SIlya Dryomov 			 void *payload,
4665922dab61SIlya Dryomov 			 size_t payload_len)
4666922dab61SIlya Dryomov {
4667922dab61SIlya Dryomov 	struct ceph_osd_request *req;
4668922dab61SIlya Dryomov 	int ret;
4669922dab61SIlya Dryomov 
4670922dab61SIlya Dryomov 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4671922dab61SIlya Dryomov 	if (!req)
4672922dab61SIlya Dryomov 		return -ENOMEM;
4673922dab61SIlya Dryomov 
4674922dab61SIlya Dryomov 	ceph_oid_copy(&req->r_base_oid, oid);
4675922dab61SIlya Dryomov 	ceph_oloc_copy(&req->r_base_oloc, oloc);
4676922dab61SIlya Dryomov 	req->r_flags = CEPH_OSD_FLAG_READ;
4677922dab61SIlya Dryomov 
4678922dab61SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4679922dab61SIlya Dryomov 	if (ret)
4680922dab61SIlya Dryomov 		goto out_put_req;
4681922dab61SIlya Dryomov 
4682922dab61SIlya Dryomov 	ret = osd_req_op_notify_ack_init(req, 0, notify_id, cookie, payload,
4683922dab61SIlya Dryomov 					 payload_len);
4684922dab61SIlya Dryomov 	if (ret)
4685922dab61SIlya Dryomov 		goto out_put_req;
4686922dab61SIlya Dryomov 
4687922dab61SIlya Dryomov 	ceph_osdc_start_request(osdc, req, false);
4688922dab61SIlya Dryomov 	ret = ceph_osdc_wait_request(osdc, req);
4689922dab61SIlya Dryomov 
4690922dab61SIlya Dryomov out_put_req:
4691922dab61SIlya Dryomov 	ceph_osdc_put_request(req);
4692922dab61SIlya Dryomov 	return ret;
4693922dab61SIlya Dryomov }
4694922dab61SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_notify_ack);
4695922dab61SIlya Dryomov 
469619079203SIlya Dryomov static int osd_req_op_notify_init(struct ceph_osd_request *req, int which,
469719079203SIlya Dryomov 				  u64 cookie, u32 prot_ver, u32 timeout,
469819079203SIlya Dryomov 				  void *payload, size_t payload_len)
469919079203SIlya Dryomov {
470019079203SIlya Dryomov 	struct ceph_osd_req_op *op;
470119079203SIlya Dryomov 	struct ceph_pagelist *pl;
470219079203SIlya Dryomov 	int ret;
470319079203SIlya Dryomov 
470419079203SIlya Dryomov 	op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY, 0);
470519079203SIlya Dryomov 	op->notify.cookie = cookie;
470619079203SIlya Dryomov 
470719079203SIlya Dryomov 	pl = kmalloc(sizeof(*pl), GFP_NOIO);
470819079203SIlya Dryomov 	if (!pl)
470919079203SIlya Dryomov 		return -ENOMEM;
471019079203SIlya Dryomov 
471119079203SIlya Dryomov 	ceph_pagelist_init(pl);
471219079203SIlya Dryomov 	ret = ceph_pagelist_encode_32(pl, 1); /* prot_ver */
471319079203SIlya Dryomov 	ret |= ceph_pagelist_encode_32(pl, timeout);
471419079203SIlya Dryomov 	ret |= ceph_pagelist_encode_32(pl, payload_len);
471519079203SIlya Dryomov 	ret |= ceph_pagelist_append(pl, payload, payload_len);
471619079203SIlya Dryomov 	if (ret) {
471719079203SIlya Dryomov 		ceph_pagelist_release(pl);
471819079203SIlya Dryomov 		return -ENOMEM;
471919079203SIlya Dryomov 	}
472019079203SIlya Dryomov 
472119079203SIlya Dryomov 	ceph_osd_data_pagelist_init(&op->notify.request_data, pl);
472219079203SIlya Dryomov 	op->indata_len = pl->length;
472319079203SIlya Dryomov 	return 0;
472419079203SIlya Dryomov }
472519079203SIlya Dryomov 
472619079203SIlya Dryomov /*
472719079203SIlya Dryomov  * @timeout: in seconds
472819079203SIlya Dryomov  *
472919079203SIlya Dryomov  * @preply_{pages,len} are initialized both on success and error.
473019079203SIlya Dryomov  * The caller is responsible for:
473119079203SIlya Dryomov  *
473219079203SIlya Dryomov  *     ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len))
473319079203SIlya Dryomov  */
473419079203SIlya Dryomov int ceph_osdc_notify(struct ceph_osd_client *osdc,
473519079203SIlya Dryomov 		     struct ceph_object_id *oid,
473619079203SIlya Dryomov 		     struct ceph_object_locator *oloc,
473719079203SIlya Dryomov 		     void *payload,
473819079203SIlya Dryomov 		     size_t payload_len,
473919079203SIlya Dryomov 		     u32 timeout,
474019079203SIlya Dryomov 		     struct page ***preply_pages,
474119079203SIlya Dryomov 		     size_t *preply_len)
474219079203SIlya Dryomov {
474319079203SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
474419079203SIlya Dryomov 	struct page **pages;
474519079203SIlya Dryomov 	int ret;
474619079203SIlya Dryomov 
474719079203SIlya Dryomov 	WARN_ON(!timeout);
474819079203SIlya Dryomov 	if (preply_pages) {
474919079203SIlya Dryomov 		*preply_pages = NULL;
475019079203SIlya Dryomov 		*preply_len = 0;
475119079203SIlya Dryomov 	}
475219079203SIlya Dryomov 
475319079203SIlya Dryomov 	lreq = linger_alloc(osdc);
475419079203SIlya Dryomov 	if (!lreq)
475519079203SIlya Dryomov 		return -ENOMEM;
475619079203SIlya Dryomov 
475719079203SIlya Dryomov 	lreq->preply_pages = preply_pages;
475819079203SIlya Dryomov 	lreq->preply_len = preply_len;
475919079203SIlya Dryomov 
476019079203SIlya Dryomov 	ceph_oid_copy(&lreq->t.base_oid, oid);
476119079203SIlya Dryomov 	ceph_oloc_copy(&lreq->t.base_oloc, oloc);
476219079203SIlya Dryomov 	lreq->t.flags = CEPH_OSD_FLAG_READ;
476319079203SIlya Dryomov 
476419079203SIlya Dryomov 	lreq->reg_req = alloc_linger_request(lreq);
476519079203SIlya Dryomov 	if (!lreq->reg_req) {
476619079203SIlya Dryomov 		ret = -ENOMEM;
476719079203SIlya Dryomov 		goto out_put_lreq;
476819079203SIlya Dryomov 	}
476919079203SIlya Dryomov 
477019079203SIlya Dryomov 	/* for notify_id */
477119079203SIlya Dryomov 	pages = ceph_alloc_page_vector(1, GFP_NOIO);
477219079203SIlya Dryomov 	if (IS_ERR(pages)) {
477319079203SIlya Dryomov 		ret = PTR_ERR(pages);
477419079203SIlya Dryomov 		goto out_put_lreq;
477519079203SIlya Dryomov 	}
477619079203SIlya Dryomov 
477719079203SIlya Dryomov 	down_write(&osdc->lock);
477819079203SIlya Dryomov 	linger_register(lreq); /* before osd_req_op_* */
477919079203SIlya Dryomov 	ret = osd_req_op_notify_init(lreq->reg_req, 0, lreq->linger_id, 1,
478019079203SIlya Dryomov 				     timeout, payload, payload_len);
478119079203SIlya Dryomov 	if (ret) {
478219079203SIlya Dryomov 		linger_unregister(lreq);
478319079203SIlya Dryomov 		up_write(&osdc->lock);
478419079203SIlya Dryomov 		ceph_release_page_vector(pages, 1);
478519079203SIlya Dryomov 		goto out_put_lreq;
478619079203SIlya Dryomov 	}
478719079203SIlya Dryomov 	ceph_osd_data_pages_init(osd_req_op_data(lreq->reg_req, 0, notify,
478819079203SIlya Dryomov 						 response_data),
478919079203SIlya Dryomov 				 pages, PAGE_SIZE, 0, false, true);
479019079203SIlya Dryomov 	linger_submit(lreq);
479119079203SIlya Dryomov 	up_write(&osdc->lock);
479219079203SIlya Dryomov 
479319079203SIlya Dryomov 	ret = linger_reg_commit_wait(lreq);
479419079203SIlya Dryomov 	if (!ret)
479519079203SIlya Dryomov 		ret = linger_notify_finish_wait(lreq);
479619079203SIlya Dryomov 	else
479719079203SIlya Dryomov 		dout("lreq %p failed to initiate notify %d\n", lreq, ret);
479819079203SIlya Dryomov 
479919079203SIlya Dryomov 	linger_cancel(lreq);
480019079203SIlya Dryomov out_put_lreq:
480119079203SIlya Dryomov 	linger_put(lreq);
480219079203SIlya Dryomov 	return ret;
480319079203SIlya Dryomov }
480419079203SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_notify);
480519079203SIlya Dryomov 
48063d14c5d2SYehuda Sadeh /*
4807b07d3c4bSIlya Dryomov  * Return the number of milliseconds since the watch was last
4808b07d3c4bSIlya Dryomov  * confirmed, or an error.  If there is an error, the watch is no
4809b07d3c4bSIlya Dryomov  * longer valid, and should be destroyed with ceph_osdc_unwatch().
4810b07d3c4bSIlya Dryomov  */
4811b07d3c4bSIlya Dryomov int ceph_osdc_watch_check(struct ceph_osd_client *osdc,
4812b07d3c4bSIlya Dryomov 			  struct ceph_osd_linger_request *lreq)
4813b07d3c4bSIlya Dryomov {
4814b07d3c4bSIlya Dryomov 	unsigned long stamp, age;
4815b07d3c4bSIlya Dryomov 	int ret;
4816b07d3c4bSIlya Dryomov 
4817b07d3c4bSIlya Dryomov 	down_read(&osdc->lock);
4818b07d3c4bSIlya Dryomov 	mutex_lock(&lreq->lock);
4819b07d3c4bSIlya Dryomov 	stamp = lreq->watch_valid_thru;
4820b07d3c4bSIlya Dryomov 	if (!list_empty(&lreq->pending_lworks)) {
4821b07d3c4bSIlya Dryomov 		struct linger_work *lwork =
4822b07d3c4bSIlya Dryomov 		    list_first_entry(&lreq->pending_lworks,
4823b07d3c4bSIlya Dryomov 				     struct linger_work,
4824b07d3c4bSIlya Dryomov 				     pending_item);
4825b07d3c4bSIlya Dryomov 
4826b07d3c4bSIlya Dryomov 		if (time_before(lwork->queued_stamp, stamp))
4827b07d3c4bSIlya Dryomov 			stamp = lwork->queued_stamp;
4828b07d3c4bSIlya Dryomov 	}
4829b07d3c4bSIlya Dryomov 	age = jiffies - stamp;
4830b07d3c4bSIlya Dryomov 	dout("%s lreq %p linger_id %llu age %lu last_error %d\n", __func__,
4831b07d3c4bSIlya Dryomov 	     lreq, lreq->linger_id, age, lreq->last_error);
4832b07d3c4bSIlya Dryomov 	/* we are truncating to msecs, so return a safe upper bound */
4833b07d3c4bSIlya Dryomov 	ret = lreq->last_error ?: 1 + jiffies_to_msecs(age);
4834b07d3c4bSIlya Dryomov 
4835b07d3c4bSIlya Dryomov 	mutex_unlock(&lreq->lock);
4836b07d3c4bSIlya Dryomov 	up_read(&osdc->lock);
4837b07d3c4bSIlya Dryomov 	return ret;
4838b07d3c4bSIlya Dryomov }
4839b07d3c4bSIlya Dryomov 
4840a4ed38d7SDouglas Fuller static int decode_watcher(void **p, void *end, struct ceph_watch_item *item)
4841a4ed38d7SDouglas Fuller {
4842a4ed38d7SDouglas Fuller 	u8 struct_v;
4843a4ed38d7SDouglas Fuller 	u32 struct_len;
4844a4ed38d7SDouglas Fuller 	int ret;
4845a4ed38d7SDouglas Fuller 
4846a4ed38d7SDouglas Fuller 	ret = ceph_start_decoding(p, end, 2, "watch_item_t",
4847a4ed38d7SDouglas Fuller 				  &struct_v, &struct_len);
4848a4ed38d7SDouglas Fuller 	if (ret)
4849a4ed38d7SDouglas Fuller 		return ret;
4850a4ed38d7SDouglas Fuller 
4851a4ed38d7SDouglas Fuller 	ceph_decode_copy(p, &item->name, sizeof(item->name));
4852a4ed38d7SDouglas Fuller 	item->cookie = ceph_decode_64(p);
4853a4ed38d7SDouglas Fuller 	*p += 4; /* skip timeout_seconds */
4854a4ed38d7SDouglas Fuller 	if (struct_v >= 2) {
4855a4ed38d7SDouglas Fuller 		ceph_decode_copy(p, &item->addr, sizeof(item->addr));
4856a4ed38d7SDouglas Fuller 		ceph_decode_addr(&item->addr);
4857a4ed38d7SDouglas Fuller 	}
4858a4ed38d7SDouglas Fuller 
4859a4ed38d7SDouglas Fuller 	dout("%s %s%llu cookie %llu addr %s\n", __func__,
4860a4ed38d7SDouglas Fuller 	     ENTITY_NAME(item->name), item->cookie,
4861a4ed38d7SDouglas Fuller 	     ceph_pr_addr(&item->addr.in_addr));
4862a4ed38d7SDouglas Fuller 	return 0;
4863a4ed38d7SDouglas Fuller }
4864a4ed38d7SDouglas Fuller 
4865a4ed38d7SDouglas Fuller static int decode_watchers(void **p, void *end,
4866a4ed38d7SDouglas Fuller 			   struct ceph_watch_item **watchers,
4867a4ed38d7SDouglas Fuller 			   u32 *num_watchers)
4868a4ed38d7SDouglas Fuller {
4869a4ed38d7SDouglas Fuller 	u8 struct_v;
4870a4ed38d7SDouglas Fuller 	u32 struct_len;
4871a4ed38d7SDouglas Fuller 	int i;
4872a4ed38d7SDouglas Fuller 	int ret;
4873a4ed38d7SDouglas Fuller 
4874a4ed38d7SDouglas Fuller 	ret = ceph_start_decoding(p, end, 1, "obj_list_watch_response_t",
4875a4ed38d7SDouglas Fuller 				  &struct_v, &struct_len);
4876a4ed38d7SDouglas Fuller 	if (ret)
4877a4ed38d7SDouglas Fuller 		return ret;
4878a4ed38d7SDouglas Fuller 
4879a4ed38d7SDouglas Fuller 	*num_watchers = ceph_decode_32(p);
4880a4ed38d7SDouglas Fuller 	*watchers = kcalloc(*num_watchers, sizeof(**watchers), GFP_NOIO);
4881a4ed38d7SDouglas Fuller 	if (!*watchers)
4882a4ed38d7SDouglas Fuller 		return -ENOMEM;
4883a4ed38d7SDouglas Fuller 
4884a4ed38d7SDouglas Fuller 	for (i = 0; i < *num_watchers; i++) {
4885a4ed38d7SDouglas Fuller 		ret = decode_watcher(p, end, *watchers + i);
4886a4ed38d7SDouglas Fuller 		if (ret) {
4887a4ed38d7SDouglas Fuller 			kfree(*watchers);
4888a4ed38d7SDouglas Fuller 			return ret;
4889a4ed38d7SDouglas Fuller 		}
4890a4ed38d7SDouglas Fuller 	}
4891a4ed38d7SDouglas Fuller 
4892a4ed38d7SDouglas Fuller 	return 0;
4893a4ed38d7SDouglas Fuller }
4894a4ed38d7SDouglas Fuller 
4895a4ed38d7SDouglas Fuller /*
4896a4ed38d7SDouglas Fuller  * On success, the caller is responsible for:
4897a4ed38d7SDouglas Fuller  *
4898a4ed38d7SDouglas Fuller  *     kfree(watchers);
4899a4ed38d7SDouglas Fuller  */
4900a4ed38d7SDouglas Fuller int ceph_osdc_list_watchers(struct ceph_osd_client *osdc,
4901a4ed38d7SDouglas Fuller 			    struct ceph_object_id *oid,
4902a4ed38d7SDouglas Fuller 			    struct ceph_object_locator *oloc,
4903a4ed38d7SDouglas Fuller 			    struct ceph_watch_item **watchers,
4904a4ed38d7SDouglas Fuller 			    u32 *num_watchers)
4905a4ed38d7SDouglas Fuller {
4906a4ed38d7SDouglas Fuller 	struct ceph_osd_request *req;
4907a4ed38d7SDouglas Fuller 	struct page **pages;
4908a4ed38d7SDouglas Fuller 	int ret;
4909a4ed38d7SDouglas Fuller 
4910a4ed38d7SDouglas Fuller 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4911a4ed38d7SDouglas Fuller 	if (!req)
4912a4ed38d7SDouglas Fuller 		return -ENOMEM;
4913a4ed38d7SDouglas Fuller 
4914a4ed38d7SDouglas Fuller 	ceph_oid_copy(&req->r_base_oid, oid);
4915a4ed38d7SDouglas Fuller 	ceph_oloc_copy(&req->r_base_oloc, oloc);
4916a4ed38d7SDouglas Fuller 	req->r_flags = CEPH_OSD_FLAG_READ;
4917a4ed38d7SDouglas Fuller 
4918a4ed38d7SDouglas Fuller 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4919a4ed38d7SDouglas Fuller 	if (ret)
4920a4ed38d7SDouglas Fuller 		goto out_put_req;
4921a4ed38d7SDouglas Fuller 
4922a4ed38d7SDouglas Fuller 	pages = ceph_alloc_page_vector(1, GFP_NOIO);
4923a4ed38d7SDouglas Fuller 	if (IS_ERR(pages)) {
4924a4ed38d7SDouglas Fuller 		ret = PTR_ERR(pages);
4925a4ed38d7SDouglas Fuller 		goto out_put_req;
4926a4ed38d7SDouglas Fuller 	}
4927a4ed38d7SDouglas Fuller 
4928a4ed38d7SDouglas Fuller 	osd_req_op_init(req, 0, CEPH_OSD_OP_LIST_WATCHERS, 0);
4929a4ed38d7SDouglas Fuller 	ceph_osd_data_pages_init(osd_req_op_data(req, 0, list_watchers,
4930a4ed38d7SDouglas Fuller 						 response_data),
4931a4ed38d7SDouglas Fuller 				 pages, PAGE_SIZE, 0, false, true);
4932a4ed38d7SDouglas Fuller 
4933a4ed38d7SDouglas Fuller 	ceph_osdc_start_request(osdc, req, false);
4934a4ed38d7SDouglas Fuller 	ret = ceph_osdc_wait_request(osdc, req);
4935a4ed38d7SDouglas Fuller 	if (ret >= 0) {
4936a4ed38d7SDouglas Fuller 		void *p = page_address(pages[0]);
4937a4ed38d7SDouglas Fuller 		void *const end = p + req->r_ops[0].outdata_len;
4938a4ed38d7SDouglas Fuller 
4939a4ed38d7SDouglas Fuller 		ret = decode_watchers(&p, end, watchers, num_watchers);
4940a4ed38d7SDouglas Fuller 	}
4941a4ed38d7SDouglas Fuller 
4942a4ed38d7SDouglas Fuller out_put_req:
4943a4ed38d7SDouglas Fuller 	ceph_osdc_put_request(req);
4944a4ed38d7SDouglas Fuller 	return ret;
4945a4ed38d7SDouglas Fuller }
4946a4ed38d7SDouglas Fuller EXPORT_SYMBOL(ceph_osdc_list_watchers);
4947a4ed38d7SDouglas Fuller 
4948b07d3c4bSIlya Dryomov /*
4949dd935f44SJosh Durgin  * Call all pending notify callbacks - for use after a watch is
4950dd935f44SJosh Durgin  * unregistered, to make sure no more callbacks for it will be invoked
4951dd935f44SJosh Durgin  */
4952f6479449Sstephen hemminger void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
4953dd935f44SJosh Durgin {
495499d16943SIlya Dryomov 	dout("%s osdc %p\n", __func__, osdc);
4955dd935f44SJosh Durgin 	flush_workqueue(osdc->notify_wq);
4956dd935f44SJosh Durgin }
4957dd935f44SJosh Durgin EXPORT_SYMBOL(ceph_osdc_flush_notifies);
4958dd935f44SJosh Durgin 
49597cca78c9SIlya Dryomov void ceph_osdc_maybe_request_map(struct ceph_osd_client *osdc)
49607cca78c9SIlya Dryomov {
49617cca78c9SIlya Dryomov 	down_read(&osdc->lock);
49627cca78c9SIlya Dryomov 	maybe_request_map(osdc);
49637cca78c9SIlya Dryomov 	up_read(&osdc->lock);
49647cca78c9SIlya Dryomov }
49657cca78c9SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_maybe_request_map);
4966dd935f44SJosh Durgin 
4967dd935f44SJosh Durgin /*
4968428a7158SDouglas Fuller  * Execute an OSD class method on an object.
4969428a7158SDouglas Fuller  *
4970428a7158SDouglas Fuller  * @flags: CEPH_OSD_FLAG_*
49712544a020SIlya Dryomov  * @resp_len: in/out param for reply length
4972428a7158SDouglas Fuller  */
4973428a7158SDouglas Fuller int ceph_osdc_call(struct ceph_osd_client *osdc,
4974428a7158SDouglas Fuller 		   struct ceph_object_id *oid,
4975428a7158SDouglas Fuller 		   struct ceph_object_locator *oloc,
4976428a7158SDouglas Fuller 		   const char *class, const char *method,
4977428a7158SDouglas Fuller 		   unsigned int flags,
4978428a7158SDouglas Fuller 		   struct page *req_page, size_t req_len,
4979428a7158SDouglas Fuller 		   struct page *resp_page, size_t *resp_len)
4980428a7158SDouglas Fuller {
4981428a7158SDouglas Fuller 	struct ceph_osd_request *req;
4982428a7158SDouglas Fuller 	int ret;
4983428a7158SDouglas Fuller 
49842544a020SIlya Dryomov 	if (req_len > PAGE_SIZE || (resp_page && *resp_len > PAGE_SIZE))
49852544a020SIlya Dryomov 		return -E2BIG;
49862544a020SIlya Dryomov 
4987428a7158SDouglas Fuller 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4988428a7158SDouglas Fuller 	if (!req)
4989428a7158SDouglas Fuller 		return -ENOMEM;
4990428a7158SDouglas Fuller 
4991428a7158SDouglas Fuller 	ceph_oid_copy(&req->r_base_oid, oid);
4992428a7158SDouglas Fuller 	ceph_oloc_copy(&req->r_base_oloc, oloc);
4993428a7158SDouglas Fuller 	req->r_flags = flags;
4994428a7158SDouglas Fuller 
4995428a7158SDouglas Fuller 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4996428a7158SDouglas Fuller 	if (ret)
4997428a7158SDouglas Fuller 		goto out_put_req;
4998428a7158SDouglas Fuller 
4999fe943d50SChengguang Xu 	ret = osd_req_op_cls_init(req, 0, CEPH_OSD_OP_CALL, class, method);
5000fe943d50SChengguang Xu 	if (ret)
5001fe943d50SChengguang Xu 		goto out_put_req;
5002fe943d50SChengguang Xu 
5003428a7158SDouglas Fuller 	if (req_page)
5004428a7158SDouglas Fuller 		osd_req_op_cls_request_data_pages(req, 0, &req_page, req_len,
5005428a7158SDouglas Fuller 						  0, false, false);
5006428a7158SDouglas Fuller 	if (resp_page)
5007428a7158SDouglas Fuller 		osd_req_op_cls_response_data_pages(req, 0, &resp_page,
50082544a020SIlya Dryomov 						   *resp_len, 0, false, false);
5009428a7158SDouglas Fuller 
5010428a7158SDouglas Fuller 	ceph_osdc_start_request(osdc, req, false);
5011428a7158SDouglas Fuller 	ret = ceph_osdc_wait_request(osdc, req);
5012428a7158SDouglas Fuller 	if (ret >= 0) {
5013428a7158SDouglas Fuller 		ret = req->r_ops[0].rval;
5014428a7158SDouglas Fuller 		if (resp_page)
5015428a7158SDouglas Fuller 			*resp_len = req->r_ops[0].outdata_len;
5016428a7158SDouglas Fuller 	}
5017428a7158SDouglas Fuller 
5018428a7158SDouglas Fuller out_put_req:
5019428a7158SDouglas Fuller 	ceph_osdc_put_request(req);
5020428a7158SDouglas Fuller 	return ret;
5021428a7158SDouglas Fuller }
5022428a7158SDouglas Fuller EXPORT_SYMBOL(ceph_osdc_call);
5023428a7158SDouglas Fuller 
5024428a7158SDouglas Fuller /*
50253d14c5d2SYehuda Sadeh  * init, shutdown
50263d14c5d2SYehuda Sadeh  */
50273d14c5d2SYehuda Sadeh int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
50283d14c5d2SYehuda Sadeh {
50293d14c5d2SYehuda Sadeh 	int err;
50303d14c5d2SYehuda Sadeh 
50313d14c5d2SYehuda Sadeh 	dout("init\n");
50323d14c5d2SYehuda Sadeh 	osdc->client = client;
50335aea3dcdSIlya Dryomov 	init_rwsem(&osdc->lock);
50343d14c5d2SYehuda Sadeh 	osdc->osds = RB_ROOT;
50353d14c5d2SYehuda Sadeh 	INIT_LIST_HEAD(&osdc->osd_lru);
50369dd2845cSIlya Dryomov 	spin_lock_init(&osdc->osd_lru_lock);
50375aea3dcdSIlya Dryomov 	osd_init(&osdc->homeless_osd);
50385aea3dcdSIlya Dryomov 	osdc->homeless_osd.o_osdc = osdc;
50395aea3dcdSIlya Dryomov 	osdc->homeless_osd.o_osd = CEPH_HOMELESS_OSD;
5040264048afSIlya Dryomov 	osdc->last_linger_id = CEPH_LINGER_ID_START;
5041922dab61SIlya Dryomov 	osdc->linger_requests = RB_ROOT;
50424609245eSIlya Dryomov 	osdc->map_checks = RB_ROOT;
50434609245eSIlya Dryomov 	osdc->linger_map_checks = RB_ROOT;
50443d14c5d2SYehuda Sadeh 	INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
50453d14c5d2SYehuda Sadeh 	INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
50463d14c5d2SYehuda Sadeh 
50473d14c5d2SYehuda Sadeh 	err = -ENOMEM;
5048e5253a7bSIlya Dryomov 	osdc->osdmap = ceph_osdmap_alloc();
5049e5253a7bSIlya Dryomov 	if (!osdc->osdmap)
5050e5253a7bSIlya Dryomov 		goto out;
5051e5253a7bSIlya Dryomov 
50529e767adbSIlya Dryomov 	osdc->req_mempool = mempool_create_slab_pool(10,
50539e767adbSIlya Dryomov 						     ceph_osd_request_cache);
50543d14c5d2SYehuda Sadeh 	if (!osdc->req_mempool)
5055e5253a7bSIlya Dryomov 		goto out_map;
50563d14c5d2SYehuda Sadeh 
5057d50b409fSSage Weil 	err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
5058711da55dSIlya Dryomov 				PAGE_SIZE, 10, true, "osd_op");
50593d14c5d2SYehuda Sadeh 	if (err < 0)
50603d14c5d2SYehuda Sadeh 		goto out_mempool;
5061d50b409fSSage Weil 	err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
5062711da55dSIlya Dryomov 				PAGE_SIZE, 10, true, "osd_op_reply");
50633d14c5d2SYehuda Sadeh 	if (err < 0)
50643d14c5d2SYehuda Sadeh 		goto out_msgpool;
5065a40c4f10SYehuda Sadeh 
5066dbcae088SDan Carpenter 	err = -ENOMEM;
5067a40c4f10SYehuda Sadeh 	osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
5068dbcae088SDan Carpenter 	if (!osdc->notify_wq)
5069c172ec5cSIlya Dryomov 		goto out_msgpool_reply;
5070c172ec5cSIlya Dryomov 
507188bc1922SIlya Dryomov 	osdc->completion_wq = create_singlethread_workqueue("ceph-completion");
507288bc1922SIlya Dryomov 	if (!osdc->completion_wq)
507388bc1922SIlya Dryomov 		goto out_notify_wq;
507488bc1922SIlya Dryomov 
5075fbca9635SIlya Dryomov 	schedule_delayed_work(&osdc->timeout_work,
5076fbca9635SIlya Dryomov 			      osdc->client->options->osd_keepalive_timeout);
5077b37ee1b9SIlya Dryomov 	schedule_delayed_work(&osdc->osds_timeout_work,
5078b37ee1b9SIlya Dryomov 	    round_jiffies_relative(osdc->client->options->osd_idle_ttl));
5079b37ee1b9SIlya Dryomov 
50803d14c5d2SYehuda Sadeh 	return 0;
50813d14c5d2SYehuda Sadeh 
508288bc1922SIlya Dryomov out_notify_wq:
508388bc1922SIlya Dryomov 	destroy_workqueue(osdc->notify_wq);
5084c172ec5cSIlya Dryomov out_msgpool_reply:
5085c172ec5cSIlya Dryomov 	ceph_msgpool_destroy(&osdc->msgpool_op_reply);
50863d14c5d2SYehuda Sadeh out_msgpool:
50873d14c5d2SYehuda Sadeh 	ceph_msgpool_destroy(&osdc->msgpool_op);
50883d14c5d2SYehuda Sadeh out_mempool:
50893d14c5d2SYehuda Sadeh 	mempool_destroy(osdc->req_mempool);
5090e5253a7bSIlya Dryomov out_map:
5091e5253a7bSIlya Dryomov 	ceph_osdmap_destroy(osdc->osdmap);
50923d14c5d2SYehuda Sadeh out:
50933d14c5d2SYehuda Sadeh 	return err;
50943d14c5d2SYehuda Sadeh }
50953d14c5d2SYehuda Sadeh 
50963d14c5d2SYehuda Sadeh void ceph_osdc_stop(struct ceph_osd_client *osdc)
50973d14c5d2SYehuda Sadeh {
509888bc1922SIlya Dryomov 	destroy_workqueue(osdc->completion_wq);
5099a40c4f10SYehuda Sadeh 	destroy_workqueue(osdc->notify_wq);
51003d14c5d2SYehuda Sadeh 	cancel_delayed_work_sync(&osdc->timeout_work);
51013d14c5d2SYehuda Sadeh 	cancel_delayed_work_sync(&osdc->osds_timeout_work);
510242a2c09fSIlya Dryomov 
51035aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
510442a2c09fSIlya Dryomov 	while (!RB_EMPTY_ROOT(&osdc->osds)) {
510542a2c09fSIlya Dryomov 		struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
510642a2c09fSIlya Dryomov 						struct ceph_osd, o_node);
51075aea3dcdSIlya Dryomov 		close_osd(osd);
510842a2c09fSIlya Dryomov 	}
51095aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
511002113a0fSElena Reshetova 	WARN_ON(refcount_read(&osdc->homeless_osd.o_ref) != 1);
51115aea3dcdSIlya Dryomov 	osd_cleanup(&osdc->homeless_osd);
51125aea3dcdSIlya Dryomov 
51135aea3dcdSIlya Dryomov 	WARN_ON(!list_empty(&osdc->osd_lru));
5114922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_requests));
51154609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osdc->map_checks));
51164609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_map_checks));
51175aea3dcdSIlya Dryomov 	WARN_ON(atomic_read(&osdc->num_requests));
51185aea3dcdSIlya Dryomov 	WARN_ON(atomic_read(&osdc->num_homeless));
511942a2c09fSIlya Dryomov 
51203d14c5d2SYehuda Sadeh 	ceph_osdmap_destroy(osdc->osdmap);
51213d14c5d2SYehuda Sadeh 	mempool_destroy(osdc->req_mempool);
51223d14c5d2SYehuda Sadeh 	ceph_msgpool_destroy(&osdc->msgpool_op);
51233d14c5d2SYehuda Sadeh 	ceph_msgpool_destroy(&osdc->msgpool_op_reply);
51243d14c5d2SYehuda Sadeh }
51253d14c5d2SYehuda Sadeh 
51263d14c5d2SYehuda Sadeh /*
51273d14c5d2SYehuda Sadeh  * Read some contiguous pages.  If we cross a stripe boundary, shorten
51283d14c5d2SYehuda Sadeh  * *plen.  Return number of bytes read, or error.
51293d14c5d2SYehuda Sadeh  */
51303d14c5d2SYehuda Sadeh int ceph_osdc_readpages(struct ceph_osd_client *osdc,
51313d14c5d2SYehuda Sadeh 			struct ceph_vino vino, struct ceph_file_layout *layout,
51323d14c5d2SYehuda Sadeh 			u64 off, u64 *plen,
51333d14c5d2SYehuda Sadeh 			u32 truncate_seq, u64 truncate_size,
5134b7495fc2SSage Weil 			struct page **pages, int num_pages, int page_align)
51353d14c5d2SYehuda Sadeh {
51363d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
51373d14c5d2SYehuda Sadeh 	int rc = 0;
51383d14c5d2SYehuda Sadeh 
51393d14c5d2SYehuda Sadeh 	dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
51403d14c5d2SYehuda Sadeh 	     vino.snap, off, *plen);
5141715e4cd4SYan, Zheng 	req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
51423d14c5d2SYehuda Sadeh 				    CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
5143acead002SAlex Elder 				    NULL, truncate_seq, truncate_size,
5144153e5167SAlex Elder 				    false);
51456816282dSSage Weil 	if (IS_ERR(req))
51466816282dSSage Weil 		return PTR_ERR(req);
51473d14c5d2SYehuda Sadeh 
51483d14c5d2SYehuda Sadeh 	/* it may be a short read due to an object boundary */
5149406e2c9fSAlex Elder 	osd_req_op_extent_osd_data_pages(req, 0,
5150a4ce40a9SAlex Elder 				pages, *plen, page_align, false, false);
51513d14c5d2SYehuda Sadeh 
5152e0c59487SAlex Elder 	dout("readpages  final extent is %llu~%llu (%llu bytes align %d)\n",
515343bfe5deSAlex Elder 	     off, *plen, *plen, page_align);
51543d14c5d2SYehuda Sadeh 
51553d14c5d2SYehuda Sadeh 	rc = ceph_osdc_start_request(osdc, req, false);
51563d14c5d2SYehuda Sadeh 	if (!rc)
51573d14c5d2SYehuda Sadeh 		rc = ceph_osdc_wait_request(osdc, req);
51583d14c5d2SYehuda Sadeh 
51593d14c5d2SYehuda Sadeh 	ceph_osdc_put_request(req);
51603d14c5d2SYehuda Sadeh 	dout("readpages result %d\n", rc);
51613d14c5d2SYehuda Sadeh 	return rc;
51623d14c5d2SYehuda Sadeh }
51633d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_readpages);
51643d14c5d2SYehuda Sadeh 
51653d14c5d2SYehuda Sadeh /*
51663d14c5d2SYehuda Sadeh  * do a synchronous write on N pages
51673d14c5d2SYehuda Sadeh  */
51683d14c5d2SYehuda Sadeh int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
51693d14c5d2SYehuda Sadeh 			 struct ceph_file_layout *layout,
51703d14c5d2SYehuda Sadeh 			 struct ceph_snap_context *snapc,
51713d14c5d2SYehuda Sadeh 			 u64 off, u64 len,
51723d14c5d2SYehuda Sadeh 			 u32 truncate_seq, u64 truncate_size,
51733d14c5d2SYehuda Sadeh 			 struct timespec *mtime,
517424808826SAlex Elder 			 struct page **pages, int num_pages)
51753d14c5d2SYehuda Sadeh {
51763d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
51773d14c5d2SYehuda Sadeh 	int rc = 0;
5178b7495fc2SSage Weil 	int page_align = off & ~PAGE_MASK;
51793d14c5d2SYehuda Sadeh 
5180715e4cd4SYan, Zheng 	req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
518154ea0046SIlya Dryomov 				    CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
5182acead002SAlex Elder 				    snapc, truncate_seq, truncate_size,
5183153e5167SAlex Elder 				    true);
51846816282dSSage Weil 	if (IS_ERR(req))
51856816282dSSage Weil 		return PTR_ERR(req);
51863d14c5d2SYehuda Sadeh 
51873d14c5d2SYehuda Sadeh 	/* it may be a short write due to an object boundary */
5188406e2c9fSAlex Elder 	osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
518943bfe5deSAlex Elder 				false, false);
519043bfe5deSAlex Elder 	dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
51913d14c5d2SYehuda Sadeh 
5192bb873b53SIlya Dryomov 	req->r_mtime = *mtime;
519387f979d3SAlex Elder 	rc = ceph_osdc_start_request(osdc, req, true);
51943d14c5d2SYehuda Sadeh 	if (!rc)
51953d14c5d2SYehuda Sadeh 		rc = ceph_osdc_wait_request(osdc, req);
51963d14c5d2SYehuda Sadeh 
51973d14c5d2SYehuda Sadeh 	ceph_osdc_put_request(req);
51983d14c5d2SYehuda Sadeh 	if (rc == 0)
51993d14c5d2SYehuda Sadeh 		rc = len;
52003d14c5d2SYehuda Sadeh 	dout("writepages result %d\n", rc);
52013d14c5d2SYehuda Sadeh 	return rc;
52023d14c5d2SYehuda Sadeh }
52033d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_writepages);
52043d14c5d2SYehuda Sadeh 
520557a35dfbSChengguang Xu int __init ceph_osdc_setup(void)
52065522ae0bSAlex Elder {
52073f1af42aSIlya Dryomov 	size_t size = sizeof(struct ceph_osd_request) +
52083f1af42aSIlya Dryomov 	    CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
52093f1af42aSIlya Dryomov 
52105522ae0bSAlex Elder 	BUG_ON(ceph_osd_request_cache);
52113f1af42aSIlya Dryomov 	ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
52123f1af42aSIlya Dryomov 						   0, 0, NULL);
52135522ae0bSAlex Elder 
52145522ae0bSAlex Elder 	return ceph_osd_request_cache ? 0 : -ENOMEM;
52155522ae0bSAlex Elder }
52165522ae0bSAlex Elder 
52175522ae0bSAlex Elder void ceph_osdc_cleanup(void)
52185522ae0bSAlex Elder {
52195522ae0bSAlex Elder 	BUG_ON(!ceph_osd_request_cache);
52205522ae0bSAlex Elder 	kmem_cache_destroy(ceph_osd_request_cache);
52215522ae0bSAlex Elder 	ceph_osd_request_cache = NULL;
52225522ae0bSAlex Elder }
52235522ae0bSAlex Elder 
52243d14c5d2SYehuda Sadeh /*
52253d14c5d2SYehuda Sadeh  * handle incoming message
52263d14c5d2SYehuda Sadeh  */
52273d14c5d2SYehuda Sadeh static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
52283d14c5d2SYehuda Sadeh {
52293d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
52305aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
52313d14c5d2SYehuda Sadeh 	int type = le16_to_cpu(msg->hdr.type);
52323d14c5d2SYehuda Sadeh 
52333d14c5d2SYehuda Sadeh 	switch (type) {
52343d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_MAP:
52353d14c5d2SYehuda Sadeh 		ceph_osdc_handle_map(osdc, msg);
52363d14c5d2SYehuda Sadeh 		break;
52373d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_OPREPLY:
52385aea3dcdSIlya Dryomov 		handle_reply(osd, msg);
52393d14c5d2SYehuda Sadeh 		break;
5240a02a946dSIlya Dryomov 	case CEPH_MSG_OSD_BACKOFF:
5241a02a946dSIlya Dryomov 		handle_backoff(osd, msg);
5242a02a946dSIlya Dryomov 		break;
5243a40c4f10SYehuda Sadeh 	case CEPH_MSG_WATCH_NOTIFY:
5244a40c4f10SYehuda Sadeh 		handle_watch_notify(osdc, msg);
5245a40c4f10SYehuda Sadeh 		break;
52463d14c5d2SYehuda Sadeh 
52473d14c5d2SYehuda Sadeh 	default:
52483d14c5d2SYehuda Sadeh 		pr_err("received unknown message type %d %s\n", type,
52493d14c5d2SYehuda Sadeh 		       ceph_msg_type_name(type));
52503d14c5d2SYehuda Sadeh 	}
52515aea3dcdSIlya Dryomov 
52523d14c5d2SYehuda Sadeh 	ceph_msg_put(msg);
52533d14c5d2SYehuda Sadeh }
52543d14c5d2SYehuda Sadeh 
52553d14c5d2SYehuda Sadeh /*
5256d15f9d69SIlya Dryomov  * Lookup and return message for incoming reply.  Don't try to do
5257d15f9d69SIlya Dryomov  * anything about a larger than preallocated data portion of the
5258d15f9d69SIlya Dryomov  * message at the moment - for now, just skip the message.
52593d14c5d2SYehuda Sadeh  */
52603d14c5d2SYehuda Sadeh static struct ceph_msg *get_reply(struct ceph_connection *con,
52613d14c5d2SYehuda Sadeh 				  struct ceph_msg_header *hdr,
52623d14c5d2SYehuda Sadeh 				  int *skip)
52633d14c5d2SYehuda Sadeh {
52643d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
52653d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = osd->o_osdc;
52665aea3dcdSIlya Dryomov 	struct ceph_msg *m = NULL;
52673d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
52683f0a4ac5SIlya Dryomov 	int front_len = le32_to_cpu(hdr->front_len);
52693d14c5d2SYehuda Sadeh 	int data_len = le32_to_cpu(hdr->data_len);
52705aea3dcdSIlya Dryomov 	u64 tid = le64_to_cpu(hdr->tid);
52713d14c5d2SYehuda Sadeh 
52725aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
52735aea3dcdSIlya Dryomov 	if (!osd_registered(osd)) {
52745aea3dcdSIlya Dryomov 		dout("%s osd%d unknown, skipping\n", __func__, osd->o_osd);
52755aea3dcdSIlya Dryomov 		*skip = 1;
52765aea3dcdSIlya Dryomov 		goto out_unlock_osdc;
52775aea3dcdSIlya Dryomov 	}
52785aea3dcdSIlya Dryomov 	WARN_ON(osd->o_osd != le64_to_cpu(hdr->src.num));
52795aea3dcdSIlya Dryomov 
52805aea3dcdSIlya Dryomov 	mutex_lock(&osd->lock);
52815aea3dcdSIlya Dryomov 	req = lookup_request(&osd->o_requests, tid);
52823d14c5d2SYehuda Sadeh 	if (!req) {
5283cd8140c6SIlya Dryomov 		dout("%s osd%d tid %llu unknown, skipping\n", __func__,
5284cd8140c6SIlya Dryomov 		     osd->o_osd, tid);
5285d15f9d69SIlya Dryomov 		*skip = 1;
52865aea3dcdSIlya Dryomov 		goto out_unlock_session;
52873d14c5d2SYehuda Sadeh 	}
52883d14c5d2SYehuda Sadeh 
52898921d114SAlex Elder 	ceph_msg_revoke_incoming(req->r_reply);
52903d14c5d2SYehuda Sadeh 
5291f2be82b0SIlya Dryomov 	if (front_len > req->r_reply->front_alloc_len) {
5292d15f9d69SIlya Dryomov 		pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
5293d15f9d69SIlya Dryomov 			__func__, osd->o_osd, req->r_tid, front_len,
5294d15f9d69SIlya Dryomov 			req->r_reply->front_alloc_len);
52953f0a4ac5SIlya Dryomov 		m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
52963f0a4ac5SIlya Dryomov 				 false);
52973d14c5d2SYehuda Sadeh 		if (!m)
52985aea3dcdSIlya Dryomov 			goto out_unlock_session;
52993d14c5d2SYehuda Sadeh 		ceph_msg_put(req->r_reply);
53003d14c5d2SYehuda Sadeh 		req->r_reply = m;
53013d14c5d2SYehuda Sadeh 	}
53023d14c5d2SYehuda Sadeh 
5303d15f9d69SIlya Dryomov 	if (data_len > req->r_reply->data_length) {
5304d15f9d69SIlya Dryomov 		pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
5305d15f9d69SIlya Dryomov 			__func__, osd->o_osd, req->r_tid, data_len,
5306d15f9d69SIlya Dryomov 			req->r_reply->data_length);
53073d14c5d2SYehuda Sadeh 		m = NULL;
5308d15f9d69SIlya Dryomov 		*skip = 1;
53095aea3dcdSIlya Dryomov 		goto out_unlock_session;
53103d14c5d2SYehuda Sadeh 	}
5311d15f9d69SIlya Dryomov 
5312d15f9d69SIlya Dryomov 	m = ceph_msg_get(req->r_reply);
53133d14c5d2SYehuda Sadeh 	dout("get_reply tid %lld %p\n", tid, m);
53143d14c5d2SYehuda Sadeh 
53155aea3dcdSIlya Dryomov out_unlock_session:
53165aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
53175aea3dcdSIlya Dryomov out_unlock_osdc:
53185aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
53193d14c5d2SYehuda Sadeh 	return m;
53203d14c5d2SYehuda Sadeh }
53213d14c5d2SYehuda Sadeh 
532219079203SIlya Dryomov /*
532319079203SIlya Dryomov  * TODO: switch to a msg-owned pagelist
532419079203SIlya Dryomov  */
532519079203SIlya Dryomov static struct ceph_msg *alloc_msg_with_page_vector(struct ceph_msg_header *hdr)
532619079203SIlya Dryomov {
532719079203SIlya Dryomov 	struct ceph_msg *m;
532819079203SIlya Dryomov 	int type = le16_to_cpu(hdr->type);
532919079203SIlya Dryomov 	u32 front_len = le32_to_cpu(hdr->front_len);
533019079203SIlya Dryomov 	u32 data_len = le32_to_cpu(hdr->data_len);
533119079203SIlya Dryomov 
533219079203SIlya Dryomov 	m = ceph_msg_new(type, front_len, GFP_NOIO, false);
533319079203SIlya Dryomov 	if (!m)
533419079203SIlya Dryomov 		return NULL;
533519079203SIlya Dryomov 
533619079203SIlya Dryomov 	if (data_len) {
533719079203SIlya Dryomov 		struct page **pages;
533819079203SIlya Dryomov 		struct ceph_osd_data osd_data;
533919079203SIlya Dryomov 
534019079203SIlya Dryomov 		pages = ceph_alloc_page_vector(calc_pages_for(0, data_len),
534119079203SIlya Dryomov 					       GFP_NOIO);
5342c22e853aSWei Yongjun 		if (IS_ERR(pages)) {
534319079203SIlya Dryomov 			ceph_msg_put(m);
534419079203SIlya Dryomov 			return NULL;
534519079203SIlya Dryomov 		}
534619079203SIlya Dryomov 
534719079203SIlya Dryomov 		ceph_osd_data_pages_init(&osd_data, pages, data_len, 0, false,
534819079203SIlya Dryomov 					 false);
534919079203SIlya Dryomov 		ceph_osdc_msg_data_add(m, &osd_data);
535019079203SIlya Dryomov 	}
535119079203SIlya Dryomov 
535219079203SIlya Dryomov 	return m;
535319079203SIlya Dryomov }
535419079203SIlya Dryomov 
53553d14c5d2SYehuda Sadeh static struct ceph_msg *alloc_msg(struct ceph_connection *con,
53563d14c5d2SYehuda Sadeh 				  struct ceph_msg_header *hdr,
53573d14c5d2SYehuda Sadeh 				  int *skip)
53583d14c5d2SYehuda Sadeh {
53593d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
53603d14c5d2SYehuda Sadeh 	int type = le16_to_cpu(hdr->type);
53613d14c5d2SYehuda Sadeh 
53621c20f2d2SAlex Elder 	*skip = 0;
53633d14c5d2SYehuda Sadeh 	switch (type) {
53643d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_MAP:
5365a02a946dSIlya Dryomov 	case CEPH_MSG_OSD_BACKOFF:
5366a40c4f10SYehuda Sadeh 	case CEPH_MSG_WATCH_NOTIFY:
536719079203SIlya Dryomov 		return alloc_msg_with_page_vector(hdr);
53683d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_OPREPLY:
53693d14c5d2SYehuda Sadeh 		return get_reply(con, hdr, skip);
53703d14c5d2SYehuda Sadeh 	default:
53715aea3dcdSIlya Dryomov 		pr_warn("%s osd%d unknown msg type %d, skipping\n", __func__,
53725aea3dcdSIlya Dryomov 			osd->o_osd, type);
53733d14c5d2SYehuda Sadeh 		*skip = 1;
53743d14c5d2SYehuda Sadeh 		return NULL;
53753d14c5d2SYehuda Sadeh 	}
53763d14c5d2SYehuda Sadeh }
53773d14c5d2SYehuda Sadeh 
53783d14c5d2SYehuda Sadeh /*
53793d14c5d2SYehuda Sadeh  * Wrappers to refcount containing ceph_osd struct
53803d14c5d2SYehuda Sadeh  */
53813d14c5d2SYehuda Sadeh static struct ceph_connection *get_osd_con(struct ceph_connection *con)
53823d14c5d2SYehuda Sadeh {
53833d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
53843d14c5d2SYehuda Sadeh 	if (get_osd(osd))
53853d14c5d2SYehuda Sadeh 		return con;
53863d14c5d2SYehuda Sadeh 	return NULL;
53873d14c5d2SYehuda Sadeh }
53883d14c5d2SYehuda Sadeh 
53893d14c5d2SYehuda Sadeh static void put_osd_con(struct ceph_connection *con)
53903d14c5d2SYehuda Sadeh {
53913d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
53923d14c5d2SYehuda Sadeh 	put_osd(osd);
53933d14c5d2SYehuda Sadeh }
53943d14c5d2SYehuda Sadeh 
53953d14c5d2SYehuda Sadeh /*
53963d14c5d2SYehuda Sadeh  * authentication
53973d14c5d2SYehuda Sadeh  */
5398a3530df3SAlex Elder /*
5399a3530df3SAlex Elder  * Note: returned pointer is the address of a structure that's
5400a3530df3SAlex Elder  * managed separately.  Caller must *not* attempt to free it.
5401a3530df3SAlex Elder  */
5402a3530df3SAlex Elder static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
54038f43fb53SAlex Elder 					int *proto, int force_new)
54043d14c5d2SYehuda Sadeh {
54053d14c5d2SYehuda Sadeh 	struct ceph_osd *o = con->private;
54063d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = o->o_osdc;
54073d14c5d2SYehuda Sadeh 	struct ceph_auth_client *ac = osdc->client->monc.auth;
540874f1869fSAlex Elder 	struct ceph_auth_handshake *auth = &o->o_auth;
54093d14c5d2SYehuda Sadeh 
541074f1869fSAlex Elder 	if (force_new && auth->authorizer) {
54116c1ea260SIlya Dryomov 		ceph_auth_destroy_authorizer(auth->authorizer);
541274f1869fSAlex Elder 		auth->authorizer = NULL;
54133d14c5d2SYehuda Sadeh 	}
541427859f97SSage Weil 	if (!auth->authorizer) {
541527859f97SSage Weil 		int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
5416a3530df3SAlex Elder 						      auth);
54173d14c5d2SYehuda Sadeh 		if (ret)
5418a3530df3SAlex Elder 			return ERR_PTR(ret);
541927859f97SSage Weil 	} else {
542027859f97SSage Weil 		int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
54210bed9b5cSSage Weil 						     auth);
54220bed9b5cSSage Weil 		if (ret)
54230bed9b5cSSage Weil 			return ERR_PTR(ret);
54243d14c5d2SYehuda Sadeh 	}
54253d14c5d2SYehuda Sadeh 	*proto = ac->protocol;
542674f1869fSAlex Elder 
5427a3530df3SAlex Elder 	return auth;
54283d14c5d2SYehuda Sadeh }
54293d14c5d2SYehuda Sadeh 
54303d14c5d2SYehuda Sadeh 
54310dde5848SIlya Dryomov static int verify_authorizer_reply(struct ceph_connection *con)
54323d14c5d2SYehuda Sadeh {
54333d14c5d2SYehuda Sadeh 	struct ceph_osd *o = con->private;
54343d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = o->o_osdc;
54353d14c5d2SYehuda Sadeh 	struct ceph_auth_client *ac = osdc->client->monc.auth;
54363d14c5d2SYehuda Sadeh 
54370dde5848SIlya Dryomov 	return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer);
54383d14c5d2SYehuda Sadeh }
54393d14c5d2SYehuda Sadeh 
54403d14c5d2SYehuda Sadeh static int invalidate_authorizer(struct ceph_connection *con)
54413d14c5d2SYehuda Sadeh {
54423d14c5d2SYehuda Sadeh 	struct ceph_osd *o = con->private;
54433d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = o->o_osdc;
54443d14c5d2SYehuda Sadeh 	struct ceph_auth_client *ac = osdc->client->monc.auth;
54453d14c5d2SYehuda Sadeh 
544627859f97SSage Weil 	ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
54473d14c5d2SYehuda Sadeh 	return ceph_monc_validate_auth(&osdc->client->monc);
54483d14c5d2SYehuda Sadeh }
54493d14c5d2SYehuda Sadeh 
54508cb441c0SIlya Dryomov static void osd_reencode_message(struct ceph_msg *msg)
54518cb441c0SIlya Dryomov {
5452914902afSIlya Dryomov 	int type = le16_to_cpu(msg->hdr.type);
5453914902afSIlya Dryomov 
5454914902afSIlya Dryomov 	if (type == CEPH_MSG_OSD_OP)
54558cb441c0SIlya Dryomov 		encode_request_finish(msg);
54568cb441c0SIlya Dryomov }
54578cb441c0SIlya Dryomov 
545879dbd1baSIlya Dryomov static int osd_sign_message(struct ceph_msg *msg)
545933d07337SYan, Zheng {
546079dbd1baSIlya Dryomov 	struct ceph_osd *o = msg->con->private;
546133d07337SYan, Zheng 	struct ceph_auth_handshake *auth = &o->o_auth;
546279dbd1baSIlya Dryomov 
546333d07337SYan, Zheng 	return ceph_auth_sign_message(auth, msg);
546433d07337SYan, Zheng }
546533d07337SYan, Zheng 
546679dbd1baSIlya Dryomov static int osd_check_message_signature(struct ceph_msg *msg)
546733d07337SYan, Zheng {
546879dbd1baSIlya Dryomov 	struct ceph_osd *o = msg->con->private;
546933d07337SYan, Zheng 	struct ceph_auth_handshake *auth = &o->o_auth;
547079dbd1baSIlya Dryomov 
547133d07337SYan, Zheng 	return ceph_auth_check_message_signature(auth, msg);
547233d07337SYan, Zheng }
547333d07337SYan, Zheng 
54743d14c5d2SYehuda Sadeh static const struct ceph_connection_operations osd_con_ops = {
54753d14c5d2SYehuda Sadeh 	.get = get_osd_con,
54763d14c5d2SYehuda Sadeh 	.put = put_osd_con,
54773d14c5d2SYehuda Sadeh 	.dispatch = dispatch,
54783d14c5d2SYehuda Sadeh 	.get_authorizer = get_authorizer,
54793d14c5d2SYehuda Sadeh 	.verify_authorizer_reply = verify_authorizer_reply,
54803d14c5d2SYehuda Sadeh 	.invalidate_authorizer = invalidate_authorizer,
54813d14c5d2SYehuda Sadeh 	.alloc_msg = alloc_msg,
54828cb441c0SIlya Dryomov 	.reencode_message = osd_reencode_message,
548379dbd1baSIlya Dryomov 	.sign_message = osd_sign_message,
548479dbd1baSIlya Dryomov 	.check_message_signature = osd_check_message_signature,
54855aea3dcdSIlya Dryomov 	.fault = osd_fault,
54863d14c5d2SYehuda Sadeh };
5487