xref: /openbmc/linux/net/ceph/osd_client.c (revision 7d7046a6)
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
rwsem_is_wrlocked(struct rw_semaphore * sem)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 }
verify_osdc_locked(struct ceph_osd_client * osdc)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 }
verify_osdc_wrlocked(struct ceph_osd_client * osdc)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 }
verify_osd_locked(struct ceph_osd * osd)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 }
verify_lreq_locked(struct ceph_osd_linger_request * lreq)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
verify_osdc_locked(struct ceph_osd_client * osdc)905aea3dcdSIlya Dryomov static inline void verify_osdc_locked(struct ceph_osd_client *osdc) { }
verify_osdc_wrlocked(struct ceph_osd_client * osdc)915aea3dcdSIlya Dryomov static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc) { }
verify_osd_locked(struct ceph_osd * osd)925aea3dcdSIlya Dryomov static inline void verify_osd_locked(struct ceph_osd *osd) { }
verify_lreq_locked(struct ceph_osd_linger_request * lreq)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  */
calc_layout(struct ceph_file_layout * layout,u64 off,u64 * plen,u64 * objnum,u64 * objoff,u64 * objlen)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 
ceph_osd_data_init(struct ceph_osd_data * osd_data)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 
12989486833SIlya Dryomov /*
13089486833SIlya Dryomov  * Consumes @pages if @own_pages is true.
13189486833SIlya Dryomov  */
ceph_osd_data_pages_init(struct ceph_osd_data * osd_data,struct page ** pages,u64 length,u32 alignment,bool pages_from_pool,bool own_pages)132a4ce40a9SAlex Elder static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
13343bfe5deSAlex Elder 			struct page **pages, u64 length, u32 alignment,
13443bfe5deSAlex Elder 			bool pages_from_pool, bool own_pages)
13543bfe5deSAlex Elder {
13643bfe5deSAlex Elder 	osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
13743bfe5deSAlex Elder 	osd_data->pages = pages;
13843bfe5deSAlex Elder 	osd_data->length = length;
13943bfe5deSAlex Elder 	osd_data->alignment = alignment;
14043bfe5deSAlex Elder 	osd_data->pages_from_pool = pages_from_pool;
14143bfe5deSAlex Elder 	osd_data->own_pages = own_pages;
14243bfe5deSAlex Elder }
14343bfe5deSAlex Elder 
14489486833SIlya Dryomov /*
14589486833SIlya Dryomov  * Consumes a ref on @pagelist.
14689486833SIlya Dryomov  */
ceph_osd_data_pagelist_init(struct ceph_osd_data * osd_data,struct ceph_pagelist * pagelist)147a4ce40a9SAlex Elder static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
14843bfe5deSAlex Elder 			struct ceph_pagelist *pagelist)
14943bfe5deSAlex Elder {
15043bfe5deSAlex Elder 	osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
15143bfe5deSAlex Elder 	osd_data->pagelist = pagelist;
15243bfe5deSAlex Elder }
15343bfe5deSAlex Elder 
15443bfe5deSAlex Elder #ifdef CONFIG_BLOCK
ceph_osd_data_bio_init(struct ceph_osd_data * osd_data,struct ceph_bio_iter * bio_pos,u32 bio_length)155a4ce40a9SAlex Elder static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
1565359a17dSIlya Dryomov 				   struct ceph_bio_iter *bio_pos,
1575359a17dSIlya Dryomov 				   u32 bio_length)
15843bfe5deSAlex Elder {
15943bfe5deSAlex Elder 	osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
1605359a17dSIlya Dryomov 	osd_data->bio_pos = *bio_pos;
16143bfe5deSAlex Elder 	osd_data->bio_length = bio_length;
16243bfe5deSAlex Elder }
16343bfe5deSAlex Elder #endif /* CONFIG_BLOCK */
16443bfe5deSAlex Elder 
ceph_osd_data_bvecs_init(struct ceph_osd_data * osd_data,struct ceph_bvec_iter * bvec_pos,u32 num_bvecs)165b9e281c2SIlya Dryomov static void ceph_osd_data_bvecs_init(struct ceph_osd_data *osd_data,
1660010f705SIlya Dryomov 				     struct ceph_bvec_iter *bvec_pos,
1670010f705SIlya Dryomov 				     u32 num_bvecs)
168b9e281c2SIlya Dryomov {
169b9e281c2SIlya Dryomov 	osd_data->type = CEPH_OSD_DATA_TYPE_BVECS;
170b9e281c2SIlya Dryomov 	osd_data->bvec_pos = *bvec_pos;
1710010f705SIlya Dryomov 	osd_data->num_bvecs = num_bvecs;
172b9e281c2SIlya Dryomov }
173b9e281c2SIlya Dryomov 
ceph_osd_iter_init(struct ceph_osd_data * osd_data,struct iov_iter * iter)174dee0c5f8SJeff Layton static void ceph_osd_iter_init(struct ceph_osd_data *osd_data,
175dee0c5f8SJeff Layton 			       struct iov_iter *iter)
176dee0c5f8SJeff Layton {
177dee0c5f8SJeff Layton 	osd_data->type = CEPH_OSD_DATA_TYPE_ITER;
178dee0c5f8SJeff Layton 	osd_data->iter = *iter;
179dee0c5f8SJeff Layton }
180dee0c5f8SJeff Layton 
18149719778SAlex Elder static struct ceph_osd_data *
osd_req_op_raw_data_in(struct ceph_osd_request * osd_req,unsigned int which)18249719778SAlex Elder osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
18349719778SAlex Elder {
18449719778SAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
18549719778SAlex Elder 
18649719778SAlex Elder 	return &osd_req->r_ops[which].raw_data_in;
18749719778SAlex Elder }
18849719778SAlex Elder 
189a4ce40a9SAlex Elder struct ceph_osd_data *
osd_req_op_extent_osd_data(struct ceph_osd_request * osd_req,unsigned int which)190a4ce40a9SAlex Elder osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
191406e2c9fSAlex Elder 			unsigned int which)
192a4ce40a9SAlex Elder {
193863c7eb5SAlex Elder 	return osd_req_op_data(osd_req, which, extent, osd_data);
194a4ce40a9SAlex Elder }
195a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_osd_data);
196a4ce40a9SAlex Elder 
osd_req_op_raw_data_in_pages(struct ceph_osd_request * osd_req,unsigned int which,struct page ** pages,u64 length,u32 alignment,bool pages_from_pool,bool own_pages)19749719778SAlex Elder void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
19849719778SAlex Elder 			unsigned int which, struct page **pages,
19949719778SAlex Elder 			u64 length, u32 alignment,
20049719778SAlex Elder 			bool pages_from_pool, bool own_pages)
20149719778SAlex Elder {
20249719778SAlex Elder 	struct ceph_osd_data *osd_data;
20349719778SAlex Elder 
20449719778SAlex Elder 	osd_data = osd_req_op_raw_data_in(osd_req, which);
20549719778SAlex Elder 	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
20649719778SAlex Elder 				pages_from_pool, own_pages);
20749719778SAlex Elder }
20849719778SAlex Elder EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
20949719778SAlex Elder 
osd_req_op_extent_osd_data_pages(struct ceph_osd_request * osd_req,unsigned int which,struct page ** pages,u64 length,u32 alignment,bool pages_from_pool,bool own_pages)210a4ce40a9SAlex Elder void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
211406e2c9fSAlex Elder 			unsigned int which, struct page **pages,
212406e2c9fSAlex Elder 			u64 length, u32 alignment,
213a4ce40a9SAlex Elder 			bool pages_from_pool, bool own_pages)
214a4ce40a9SAlex Elder {
215a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
216a4ce40a9SAlex Elder 
217863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
218a4ce40a9SAlex Elder 	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
219a4ce40a9SAlex Elder 				pages_from_pool, own_pages);
220a4ce40a9SAlex Elder }
221a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
222a4ce40a9SAlex Elder 
osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request * osd_req,unsigned int which,struct ceph_pagelist * pagelist)223a4ce40a9SAlex Elder void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
224406e2c9fSAlex Elder 			unsigned int which, struct ceph_pagelist *pagelist)
225a4ce40a9SAlex Elder {
226a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
227a4ce40a9SAlex Elder 
228863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
229a4ce40a9SAlex Elder 	ceph_osd_data_pagelist_init(osd_data, pagelist);
230a4ce40a9SAlex Elder }
231a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
232a4ce40a9SAlex Elder 
233a4ce40a9SAlex Elder #ifdef CONFIG_BLOCK
osd_req_op_extent_osd_data_bio(struct ceph_osd_request * osd_req,unsigned int which,struct ceph_bio_iter * bio_pos,u32 bio_length)234a4ce40a9SAlex Elder void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
2355359a17dSIlya Dryomov 				    unsigned int which,
2365359a17dSIlya Dryomov 				    struct ceph_bio_iter *bio_pos,
2375359a17dSIlya Dryomov 				    u32 bio_length)
238a4ce40a9SAlex Elder {
239a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
240863c7eb5SAlex Elder 
241863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
2425359a17dSIlya Dryomov 	ceph_osd_data_bio_init(osd_data, bio_pos, bio_length);
243a4ce40a9SAlex Elder }
244a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
245a4ce40a9SAlex Elder #endif /* CONFIG_BLOCK */
246a4ce40a9SAlex Elder 
osd_req_op_extent_osd_data_bvecs(struct ceph_osd_request * osd_req,unsigned int which,struct bio_vec * bvecs,u32 num_bvecs,u32 bytes)2470010f705SIlya Dryomov void osd_req_op_extent_osd_data_bvecs(struct ceph_osd_request *osd_req,
2480010f705SIlya Dryomov 				      unsigned int which,
2490010f705SIlya Dryomov 				      struct bio_vec *bvecs, u32 num_bvecs,
2500010f705SIlya Dryomov 				      u32 bytes)
2510010f705SIlya Dryomov {
2520010f705SIlya Dryomov 	struct ceph_osd_data *osd_data;
2530010f705SIlya Dryomov 	struct ceph_bvec_iter it = {
2540010f705SIlya Dryomov 		.bvecs = bvecs,
2550010f705SIlya Dryomov 		.iter = { .bi_size = bytes },
2560010f705SIlya Dryomov 	};
2570010f705SIlya Dryomov 
2580010f705SIlya Dryomov 	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
2590010f705SIlya Dryomov 	ceph_osd_data_bvecs_init(osd_data, &it, num_bvecs);
2600010f705SIlya Dryomov }
2610010f705SIlya Dryomov EXPORT_SYMBOL(osd_req_op_extent_osd_data_bvecs);
2620010f705SIlya Dryomov 
osd_req_op_extent_osd_data_bvec_pos(struct ceph_osd_request * osd_req,unsigned int which,struct ceph_bvec_iter * bvec_pos)263b9e281c2SIlya Dryomov void osd_req_op_extent_osd_data_bvec_pos(struct ceph_osd_request *osd_req,
264b9e281c2SIlya Dryomov 					 unsigned int which,
265b9e281c2SIlya Dryomov 					 struct ceph_bvec_iter *bvec_pos)
266b9e281c2SIlya Dryomov {
267b9e281c2SIlya Dryomov 	struct ceph_osd_data *osd_data;
268b9e281c2SIlya Dryomov 
269b9e281c2SIlya Dryomov 	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
2700010f705SIlya Dryomov 	ceph_osd_data_bvecs_init(osd_data, bvec_pos, 0);
271b9e281c2SIlya Dryomov }
272b9e281c2SIlya Dryomov EXPORT_SYMBOL(osd_req_op_extent_osd_data_bvec_pos);
273b9e281c2SIlya Dryomov 
274dee0c5f8SJeff Layton /**
275dee0c5f8SJeff Layton  * osd_req_op_extent_osd_iter - Set up an operation with an iterator buffer
276dee0c5f8SJeff Layton  * @osd_req: The request to set up
277dee0c5f8SJeff Layton  * @which: Index of the operation in which to set the iter
278dee0c5f8SJeff Layton  * @iter: The buffer iterator
279dee0c5f8SJeff Layton  */
osd_req_op_extent_osd_iter(struct ceph_osd_request * osd_req,unsigned int which,struct iov_iter * iter)280dee0c5f8SJeff Layton void osd_req_op_extent_osd_iter(struct ceph_osd_request *osd_req,
281dee0c5f8SJeff Layton 				unsigned int which, struct iov_iter *iter)
282dee0c5f8SJeff Layton {
283dee0c5f8SJeff Layton 	struct ceph_osd_data *osd_data;
284dee0c5f8SJeff Layton 
285dee0c5f8SJeff Layton 	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
286dee0c5f8SJeff Layton 	ceph_osd_iter_init(osd_data, iter);
287dee0c5f8SJeff Layton }
288dee0c5f8SJeff Layton EXPORT_SYMBOL(osd_req_op_extent_osd_iter);
289dee0c5f8SJeff Layton 
osd_req_op_cls_request_info_pagelist(struct ceph_osd_request * osd_req,unsigned int which,struct ceph_pagelist * pagelist)290a4ce40a9SAlex Elder static void osd_req_op_cls_request_info_pagelist(
291a4ce40a9SAlex Elder 			struct ceph_osd_request *osd_req,
292a4ce40a9SAlex Elder 			unsigned int which, struct ceph_pagelist *pagelist)
293a4ce40a9SAlex Elder {
294a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
295a4ce40a9SAlex Elder 
296863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, request_info);
297a4ce40a9SAlex Elder 	ceph_osd_data_pagelist_init(osd_data, pagelist);
298a4ce40a9SAlex Elder }
299a4ce40a9SAlex Elder 
osd_req_op_cls_request_data_pagelist(struct ceph_osd_request * osd_req,unsigned int which,struct ceph_pagelist * pagelist)30004017e29SAlex Elder void osd_req_op_cls_request_data_pagelist(
30104017e29SAlex Elder 			struct ceph_osd_request *osd_req,
30204017e29SAlex Elder 			unsigned int which, struct ceph_pagelist *pagelist)
30304017e29SAlex Elder {
30404017e29SAlex Elder 	struct ceph_osd_data *osd_data;
30504017e29SAlex Elder 
306863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, request_data);
30704017e29SAlex Elder 	ceph_osd_data_pagelist_init(osd_data, pagelist);
308bb873b53SIlya Dryomov 	osd_req->r_ops[which].cls.indata_len += pagelist->length;
309bb873b53SIlya Dryomov 	osd_req->r_ops[which].indata_len += pagelist->length;
31004017e29SAlex Elder }
31104017e29SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
31204017e29SAlex Elder 
osd_req_op_cls_request_data_pages(struct ceph_osd_request * osd_req,unsigned int which,struct page ** pages,u64 length,u32 alignment,bool pages_from_pool,bool own_pages)3136c57b554SAlex Elder void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
3146c57b554SAlex Elder 			unsigned int which, struct page **pages, u64 length,
3156c57b554SAlex Elder 			u32 alignment, bool pages_from_pool, bool own_pages)
3166c57b554SAlex Elder {
3176c57b554SAlex Elder 	struct ceph_osd_data *osd_data;
3186c57b554SAlex Elder 
3196c57b554SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, request_data);
3206c57b554SAlex Elder 	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
3216c57b554SAlex Elder 				pages_from_pool, own_pages);
322bb873b53SIlya Dryomov 	osd_req->r_ops[which].cls.indata_len += length;
323bb873b53SIlya Dryomov 	osd_req->r_ops[which].indata_len += length;
3246c57b554SAlex Elder }
3256c57b554SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
3266c57b554SAlex Elder 
osd_req_op_cls_request_data_bvecs(struct ceph_osd_request * osd_req,unsigned int which,struct bio_vec * bvecs,u32 num_bvecs,u32 bytes)327b9e281c2SIlya Dryomov void osd_req_op_cls_request_data_bvecs(struct ceph_osd_request *osd_req,
328b9e281c2SIlya Dryomov 				       unsigned int which,
3290010f705SIlya Dryomov 				       struct bio_vec *bvecs, u32 num_bvecs,
3300010f705SIlya Dryomov 				       u32 bytes)
331b9e281c2SIlya Dryomov {
332b9e281c2SIlya Dryomov 	struct ceph_osd_data *osd_data;
333b9e281c2SIlya Dryomov 	struct ceph_bvec_iter it = {
334b9e281c2SIlya Dryomov 		.bvecs = bvecs,
335b9e281c2SIlya Dryomov 		.iter = { .bi_size = bytes },
336b9e281c2SIlya Dryomov 	};
337b9e281c2SIlya Dryomov 
338b9e281c2SIlya Dryomov 	osd_data = osd_req_op_data(osd_req, which, cls, request_data);
3390010f705SIlya Dryomov 	ceph_osd_data_bvecs_init(osd_data, &it, num_bvecs);
340b9e281c2SIlya Dryomov 	osd_req->r_ops[which].cls.indata_len += bytes;
341b9e281c2SIlya Dryomov 	osd_req->r_ops[which].indata_len += bytes;
342b9e281c2SIlya Dryomov }
343b9e281c2SIlya Dryomov EXPORT_SYMBOL(osd_req_op_cls_request_data_bvecs);
344b9e281c2SIlya Dryomov 
osd_req_op_cls_response_data_pages(struct ceph_osd_request * osd_req,unsigned int which,struct page ** pages,u64 length,u32 alignment,bool pages_from_pool,bool own_pages)345a4ce40a9SAlex Elder void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
346a4ce40a9SAlex Elder 			unsigned int which, struct page **pages, u64 length,
347a4ce40a9SAlex Elder 			u32 alignment, bool pages_from_pool, bool own_pages)
348a4ce40a9SAlex Elder {
349a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
350a4ce40a9SAlex Elder 
351863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, response_data);
352a4ce40a9SAlex Elder 	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
353a4ce40a9SAlex Elder 				pages_from_pool, own_pages);
354a4ce40a9SAlex Elder }
355a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
356a4ce40a9SAlex Elder 
ceph_osd_data_length(struct ceph_osd_data * osd_data)35723c08a9cSAlex Elder static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
35823c08a9cSAlex Elder {
35923c08a9cSAlex Elder 	switch (osd_data->type) {
36023c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_NONE:
36123c08a9cSAlex Elder 		return 0;
36223c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_PAGES:
36323c08a9cSAlex Elder 		return osd_data->length;
36423c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_PAGELIST:
36523c08a9cSAlex Elder 		return (u64)osd_data->pagelist->length;
36623c08a9cSAlex Elder #ifdef CONFIG_BLOCK
36723c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_BIO:
36823c08a9cSAlex Elder 		return (u64)osd_data->bio_length;
36923c08a9cSAlex Elder #endif /* CONFIG_BLOCK */
370b9e281c2SIlya Dryomov 	case CEPH_OSD_DATA_TYPE_BVECS:
371b9e281c2SIlya Dryomov 		return osd_data->bvec_pos.iter.bi_size;
372dee0c5f8SJeff Layton 	case CEPH_OSD_DATA_TYPE_ITER:
373dee0c5f8SJeff Layton 		return iov_iter_count(&osd_data->iter);
37423c08a9cSAlex Elder 	default:
37523c08a9cSAlex Elder 		WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
37623c08a9cSAlex Elder 		return 0;
37723c08a9cSAlex Elder 	}
37823c08a9cSAlex Elder }
37923c08a9cSAlex Elder 
ceph_osd_data_release(struct ceph_osd_data * osd_data)380c54d47bfSAlex Elder static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
381c54d47bfSAlex Elder {
3825476492fSAlex Elder 	if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
383c54d47bfSAlex Elder 		int num_pages;
384c54d47bfSAlex Elder 
385c54d47bfSAlex Elder 		num_pages = calc_pages_for((u64)osd_data->alignment,
386c54d47bfSAlex Elder 						(u64)osd_data->length);
387c54d47bfSAlex Elder 		ceph_release_page_vector(osd_data->pages, num_pages);
38889486833SIlya Dryomov 	} else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
38989486833SIlya Dryomov 		ceph_pagelist_release(osd_data->pagelist);
390c54d47bfSAlex Elder 	}
3915476492fSAlex Elder 	ceph_osd_data_init(osd_data);
3925476492fSAlex Elder }
3935476492fSAlex Elder 
osd_req_op_data_release(struct ceph_osd_request * osd_req,unsigned int which)3945476492fSAlex Elder static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
3955476492fSAlex Elder 			unsigned int which)
3965476492fSAlex Elder {
3975476492fSAlex Elder 	struct ceph_osd_req_op *op;
3985476492fSAlex Elder 
3995476492fSAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
4005476492fSAlex Elder 	op = &osd_req->r_ops[which];
4015476492fSAlex Elder 
4025476492fSAlex Elder 	switch (op->op) {
4035476492fSAlex Elder 	case CEPH_OSD_OP_READ:
404f628d799SJeff Layton 	case CEPH_OSD_OP_SPARSE_READ:
4055476492fSAlex Elder 	case CEPH_OSD_OP_WRITE:
406e30b7577SIlya Dryomov 	case CEPH_OSD_OP_WRITEFULL:
407a679e50fSJeff Layton 		kfree(op->extent.sparse_ext);
4085476492fSAlex Elder 		ceph_osd_data_release(&op->extent.osd_data);
4095476492fSAlex Elder 		break;
4105476492fSAlex Elder 	case CEPH_OSD_OP_CALL:
4115476492fSAlex Elder 		ceph_osd_data_release(&op->cls.request_info);
41204017e29SAlex Elder 		ceph_osd_data_release(&op->cls.request_data);
4135476492fSAlex Elder 		ceph_osd_data_release(&op->cls.response_data);
4145476492fSAlex Elder 		break;
415d74b50beSYan, Zheng 	case CEPH_OSD_OP_SETXATTR:
416d74b50beSYan, Zheng 	case CEPH_OSD_OP_CMPXATTR:
417d74b50beSYan, Zheng 		ceph_osd_data_release(&op->xattr.osd_data);
418d74b50beSYan, Zheng 		break;
41966ba609fSYan, Zheng 	case CEPH_OSD_OP_STAT:
42066ba609fSYan, Zheng 		ceph_osd_data_release(&op->raw_data_in);
42166ba609fSYan, Zheng 		break;
422922dab61SIlya Dryomov 	case CEPH_OSD_OP_NOTIFY_ACK:
423922dab61SIlya Dryomov 		ceph_osd_data_release(&op->notify_ack.request_data);
424922dab61SIlya Dryomov 		break;
42519079203SIlya Dryomov 	case CEPH_OSD_OP_NOTIFY:
42619079203SIlya Dryomov 		ceph_osd_data_release(&op->notify.request_data);
42719079203SIlya Dryomov 		ceph_osd_data_release(&op->notify.response_data);
42819079203SIlya Dryomov 		break;
429a4ed38d7SDouglas Fuller 	case CEPH_OSD_OP_LIST_WATCHERS:
430a4ed38d7SDouglas Fuller 		ceph_osd_data_release(&op->list_watchers.response_data);
431a4ed38d7SDouglas Fuller 		break;
43278beb0ffSLuis Henriques 	case CEPH_OSD_OP_COPY_FROM2:
43323ddf9beSLuis Henriques 		ceph_osd_data_release(&op->copy_from.osd_data);
43423ddf9beSLuis Henriques 		break;
4355476492fSAlex Elder 	default:
4365476492fSAlex Elder 		break;
4375476492fSAlex Elder 	}
438c54d47bfSAlex Elder }
439c54d47bfSAlex Elder 
4403d14c5d2SYehuda Sadeh /*
44163244fa1SIlya Dryomov  * Assumes @t is zero-initialized.
44263244fa1SIlya Dryomov  */
target_init(struct ceph_osd_request_target * t)44363244fa1SIlya Dryomov static void target_init(struct ceph_osd_request_target *t)
44463244fa1SIlya Dryomov {
44563244fa1SIlya Dryomov 	ceph_oid_init(&t->base_oid);
44663244fa1SIlya Dryomov 	ceph_oloc_init(&t->base_oloc);
44763244fa1SIlya Dryomov 	ceph_oid_init(&t->target_oid);
44863244fa1SIlya Dryomov 	ceph_oloc_init(&t->target_oloc);
44963244fa1SIlya Dryomov 
45063244fa1SIlya Dryomov 	ceph_osds_init(&t->acting);
45163244fa1SIlya Dryomov 	ceph_osds_init(&t->up);
45263244fa1SIlya Dryomov 	t->size = -1;
45363244fa1SIlya Dryomov 	t->min_size = -1;
45463244fa1SIlya Dryomov 
45563244fa1SIlya Dryomov 	t->osd = CEPH_HOMELESS_OSD;
45663244fa1SIlya Dryomov }
45763244fa1SIlya Dryomov 
target_copy(struct ceph_osd_request_target * dest,const struct ceph_osd_request_target * src)458922dab61SIlya Dryomov static void target_copy(struct ceph_osd_request_target *dest,
459922dab61SIlya Dryomov 			const struct ceph_osd_request_target *src)
460922dab61SIlya Dryomov {
461922dab61SIlya Dryomov 	ceph_oid_copy(&dest->base_oid, &src->base_oid);
462922dab61SIlya Dryomov 	ceph_oloc_copy(&dest->base_oloc, &src->base_oloc);
463922dab61SIlya Dryomov 	ceph_oid_copy(&dest->target_oid, &src->target_oid);
464922dab61SIlya Dryomov 	ceph_oloc_copy(&dest->target_oloc, &src->target_oloc);
465922dab61SIlya Dryomov 
466922dab61SIlya Dryomov 	dest->pgid = src->pgid; /* struct */
467dc98ff72SIlya Dryomov 	dest->spgid = src->spgid; /* struct */
468922dab61SIlya Dryomov 	dest->pg_num = src->pg_num;
469922dab61SIlya Dryomov 	dest->pg_num_mask = src->pg_num_mask;
470922dab61SIlya Dryomov 	ceph_osds_copy(&dest->acting, &src->acting);
471922dab61SIlya Dryomov 	ceph_osds_copy(&dest->up, &src->up);
472922dab61SIlya Dryomov 	dest->size = src->size;
473922dab61SIlya Dryomov 	dest->min_size = src->min_size;
474922dab61SIlya Dryomov 	dest->sort_bitwise = src->sort_bitwise;
4752f3fead6SIlya Dryomov 	dest->recovery_deletes = src->recovery_deletes;
476922dab61SIlya Dryomov 
477922dab61SIlya Dryomov 	dest->flags = src->flags;
4787ed286f3SIlya Dryomov 	dest->used_replica = src->used_replica;
479922dab61SIlya Dryomov 	dest->paused = src->paused;
480922dab61SIlya Dryomov 
48104c7d789SIlya Dryomov 	dest->epoch = src->epoch;
482dc93e0e2SIlya Dryomov 	dest->last_force_resend = src->last_force_resend;
483dc93e0e2SIlya Dryomov 
484922dab61SIlya Dryomov 	dest->osd = src->osd;
485922dab61SIlya Dryomov }
486922dab61SIlya Dryomov 
target_destroy(struct ceph_osd_request_target * t)48763244fa1SIlya Dryomov static void target_destroy(struct ceph_osd_request_target *t)
48863244fa1SIlya Dryomov {
48963244fa1SIlya Dryomov 	ceph_oid_destroy(&t->base_oid);
49030c156d9SYan, Zheng 	ceph_oloc_destroy(&t->base_oloc);
49163244fa1SIlya Dryomov 	ceph_oid_destroy(&t->target_oid);
49230c156d9SYan, Zheng 	ceph_oloc_destroy(&t->target_oloc);
49363244fa1SIlya Dryomov }
49463244fa1SIlya Dryomov 
49563244fa1SIlya Dryomov /*
4963d14c5d2SYehuda Sadeh  * requests
4973d14c5d2SYehuda Sadeh  */
request_release_checks(struct ceph_osd_request * req)4983540bfdbSIlya Dryomov static void request_release_checks(struct ceph_osd_request *req)
4993540bfdbSIlya Dryomov {
5003540bfdbSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&req->r_node));
5014609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&req->r_mc_node));
50294e85771SIlya Dryomov 	WARN_ON(!list_empty(&req->r_private_item));
5033540bfdbSIlya Dryomov 	WARN_ON(req->r_osd);
5043540bfdbSIlya Dryomov }
5053540bfdbSIlya Dryomov 
ceph_osdc_release_request(struct kref * kref)5069e94af20SIlya Dryomov static void ceph_osdc_release_request(struct kref *kref)
5073d14c5d2SYehuda Sadeh {
5089e94af20SIlya Dryomov 	struct ceph_osd_request *req = container_of(kref,
5099e94af20SIlya Dryomov 					    struct ceph_osd_request, r_kref);
5105476492fSAlex Elder 	unsigned int which;
5113d14c5d2SYehuda Sadeh 
5129e94af20SIlya Dryomov 	dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
5139e94af20SIlya Dryomov 	     req->r_request, req->r_reply);
5143540bfdbSIlya Dryomov 	request_release_checks(req);
5159e94af20SIlya Dryomov 
5163d14c5d2SYehuda Sadeh 	if (req->r_request)
5173d14c5d2SYehuda Sadeh 		ceph_msg_put(req->r_request);
5185aea3dcdSIlya Dryomov 	if (req->r_reply)
519ab8cb34aSAlex Elder 		ceph_msg_put(req->r_reply);
5200fff87ecSAlex Elder 
5215476492fSAlex Elder 	for (which = 0; which < req->r_num_ops; which++)
5225476492fSAlex Elder 		osd_req_op_data_release(req, which);
5230fff87ecSAlex Elder 
524a66dd383SIlya Dryomov 	target_destroy(&req->r_t);
5253d14c5d2SYehuda Sadeh 	ceph_put_snap_context(req->r_snapc);
526d30291b9SIlya Dryomov 
5273d14c5d2SYehuda Sadeh 	if (req->r_mempool)
5283d14c5d2SYehuda Sadeh 		mempool_free(req, req->r_osdc->req_mempool);
5293f1af42aSIlya Dryomov 	else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS)
5305522ae0bSAlex Elder 		kmem_cache_free(ceph_osd_request_cache, req);
5313f1af42aSIlya Dryomov 	else
5323f1af42aSIlya Dryomov 		kfree(req);
5333d14c5d2SYehuda Sadeh }
5349e94af20SIlya Dryomov 
ceph_osdc_get_request(struct ceph_osd_request * req)5359e94af20SIlya Dryomov void ceph_osdc_get_request(struct ceph_osd_request *req)
5369e94af20SIlya Dryomov {
5379e94af20SIlya Dryomov 	dout("%s %p (was %d)\n", __func__, req,
5382c935bc5SPeter Zijlstra 	     kref_read(&req->r_kref));
5399e94af20SIlya Dryomov 	kref_get(&req->r_kref);
5409e94af20SIlya Dryomov }
5419e94af20SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_get_request);
5429e94af20SIlya Dryomov 
ceph_osdc_put_request(struct ceph_osd_request * req)5439e94af20SIlya Dryomov void ceph_osdc_put_request(struct ceph_osd_request *req)
5449e94af20SIlya Dryomov {
5453ed97d63SIlya Dryomov 	if (req) {
5469e94af20SIlya Dryomov 		dout("%s %p (was %d)\n", __func__, req,
5472c935bc5SPeter Zijlstra 		     kref_read(&req->r_kref));
5489e94af20SIlya Dryomov 		kref_put(&req->r_kref, ceph_osdc_release_request);
5499e94af20SIlya Dryomov 	}
5503ed97d63SIlya Dryomov }
5519e94af20SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_put_request);
5523d14c5d2SYehuda Sadeh 
request_init(struct ceph_osd_request * req)5533540bfdbSIlya Dryomov static void request_init(struct ceph_osd_request *req)
5543540bfdbSIlya Dryomov {
555042f6498SJeff Layton 	/* req only, each op is zeroed in osd_req_op_init() */
5563540bfdbSIlya Dryomov 	memset(req, 0, sizeof(*req));
5573540bfdbSIlya Dryomov 
5583540bfdbSIlya Dryomov 	kref_init(&req->r_kref);
5593540bfdbSIlya Dryomov 	init_completion(&req->r_completion);
5603540bfdbSIlya Dryomov 	RB_CLEAR_NODE(&req->r_node);
5614609245eSIlya Dryomov 	RB_CLEAR_NODE(&req->r_mc_node);
56294e85771SIlya Dryomov 	INIT_LIST_HEAD(&req->r_private_item);
5633540bfdbSIlya Dryomov 
5643540bfdbSIlya Dryomov 	target_init(&req->r_t);
5653540bfdbSIlya Dryomov }
5663540bfdbSIlya Dryomov 
ceph_osdc_alloc_request(struct ceph_osd_client * osdc,struct ceph_snap_context * snapc,unsigned int num_ops,bool use_mempool,gfp_t gfp_flags)5673d14c5d2SYehuda Sadeh struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
5683d14c5d2SYehuda Sadeh 					       struct ceph_snap_context *snapc,
5691b83bef2SSage Weil 					       unsigned int num_ops,
5703d14c5d2SYehuda Sadeh 					       bool use_mempool,
57154a54007SAlex Elder 					       gfp_t gfp_flags)
5723d14c5d2SYehuda Sadeh {
5733d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
5743d14c5d2SYehuda Sadeh 
5753d14c5d2SYehuda Sadeh 	if (use_mempool) {
5763f1af42aSIlya Dryomov 		BUG_ON(num_ops > CEPH_OSD_SLAB_OPS);
5773d14c5d2SYehuda Sadeh 		req = mempool_alloc(osdc->req_mempool, gfp_flags);
5783f1af42aSIlya Dryomov 	} else if (num_ops <= CEPH_OSD_SLAB_OPS) {
5793f1af42aSIlya Dryomov 		req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
5803d14c5d2SYehuda Sadeh 	} else {
5813f1af42aSIlya Dryomov 		BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
582acafe7e3SKees Cook 		req = kmalloc(struct_size(req, r_ops, num_ops), gfp_flags);
5833d14c5d2SYehuda Sadeh 	}
5843f1af42aSIlya Dryomov 	if (unlikely(!req))
5853d14c5d2SYehuda Sadeh 		return NULL;
5863d14c5d2SYehuda Sadeh 
5873540bfdbSIlya Dryomov 	request_init(req);
5883d14c5d2SYehuda Sadeh 	req->r_osdc = osdc;
5893d14c5d2SYehuda Sadeh 	req->r_mempool = use_mempool;
59079528734SAlex Elder 	req->r_num_ops = num_ops;
59184127282SIlya Dryomov 	req->r_snapid = CEPH_NOSNAP;
59284127282SIlya Dryomov 	req->r_snapc = ceph_get_snap_context(snapc);
5933d14c5d2SYehuda Sadeh 
59413d1ad16SIlya Dryomov 	dout("%s req %p\n", __func__, req);
59513d1ad16SIlya Dryomov 	return req;
5963f1af42aSIlya Dryomov }
59713d1ad16SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_alloc_request);
5983f1af42aSIlya Dryomov 
ceph_oloc_encoding_size(const struct ceph_object_locator * oloc)5992e59ffd1SIlya Dryomov static int ceph_oloc_encoding_size(const struct ceph_object_locator *oloc)
60030c156d9SYan, Zheng {
60130c156d9SYan, Zheng 	return 8 + 4 + 4 + 4 + (oloc->pool_ns ? oloc->pool_ns->len : 0);
60230c156d9SYan, Zheng }
60330c156d9SYan, Zheng 
__ceph_osdc_alloc_messages(struct ceph_osd_request * req,gfp_t gfp,int num_request_data_items,int num_reply_data_items)6040d9c1ab3SIlya Dryomov static int __ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp,
6050d9c1ab3SIlya Dryomov 				      int num_request_data_items,
6060d9c1ab3SIlya Dryomov 				      int num_reply_data_items)
60713d1ad16SIlya Dryomov {
60813d1ad16SIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
60913d1ad16SIlya Dryomov 	struct ceph_msg *msg;
61013d1ad16SIlya Dryomov 	int msg_size;
6113d14c5d2SYehuda Sadeh 
6120d9c1ab3SIlya Dryomov 	WARN_ON(req->r_request || req->r_reply);
613d30291b9SIlya Dryomov 	WARN_ON(ceph_oid_empty(&req->r_base_oid));
61430c156d9SYan, Zheng 	WARN_ON(ceph_oloc_empty(&req->r_base_oloc));
615d30291b9SIlya Dryomov 
61613d1ad16SIlya Dryomov 	/* create request message */
6178cb441c0SIlya Dryomov 	msg_size = CEPH_ENCODING_START_BLK_LEN +
6188cb441c0SIlya Dryomov 			CEPH_PGID_ENCODING_LEN + 1; /* spgid */
6198cb441c0SIlya Dryomov 	msg_size += 4 + 4 + 4; /* hash, osdmap_epoch, flags */
6208cb441c0SIlya Dryomov 	msg_size += CEPH_ENCODING_START_BLK_LEN +
6218cb441c0SIlya Dryomov 			sizeof(struct ceph_osd_reqid); /* reqid */
6228cb441c0SIlya Dryomov 	msg_size += sizeof(struct ceph_blkin_trace_info); /* trace */
6238cb441c0SIlya Dryomov 	msg_size += 4 + sizeof(struct ceph_timespec); /* client_inc, mtime */
62430c156d9SYan, Zheng 	msg_size += CEPH_ENCODING_START_BLK_LEN +
62530c156d9SYan, Zheng 			ceph_oloc_encoding_size(&req->r_base_oloc); /* oloc */
62613d1ad16SIlya Dryomov 	msg_size += 4 + req->r_base_oid.name_len; /* oid */
62713d1ad16SIlya Dryomov 	msg_size += 2 + req->r_num_ops * sizeof(struct ceph_osd_op);
628ae458f5aSIlya Dryomov 	msg_size += 8; /* snapid */
629ae458f5aSIlya Dryomov 	msg_size += 8; /* snap_seq */
63013d1ad16SIlya Dryomov 	msg_size += 4 + 8 * (req->r_snapc ? req->r_snapc->num_snaps : 0);
6318cb441c0SIlya Dryomov 	msg_size += 4 + 8; /* retry_attempt, features */
632ae458f5aSIlya Dryomov 
63313d1ad16SIlya Dryomov 	if (req->r_mempool)
6340d9c1ab3SIlya Dryomov 		msg = ceph_msgpool_get(&osdc->msgpool_op, msg_size,
6350d9c1ab3SIlya Dryomov 				       num_request_data_items);
6363d14c5d2SYehuda Sadeh 	else
6370d9c1ab3SIlya Dryomov 		msg = ceph_msg_new2(CEPH_MSG_OSD_OP, msg_size,
6380d9c1ab3SIlya Dryomov 				    num_request_data_items, gfp, true);
63913d1ad16SIlya Dryomov 	if (!msg)
64013d1ad16SIlya Dryomov 		return -ENOMEM;
6413d14c5d2SYehuda Sadeh 
6423d14c5d2SYehuda Sadeh 	memset(msg->front.iov_base, 0, msg->front.iov_len);
6433d14c5d2SYehuda Sadeh 	req->r_request = msg;
6443d14c5d2SYehuda Sadeh 
64513d1ad16SIlya Dryomov 	/* create reply message */
64613d1ad16SIlya Dryomov 	msg_size = OSD_OPREPLY_FRONT_LEN;
647711da55dSIlya Dryomov 	msg_size += req->r_base_oid.name_len;
648711da55dSIlya Dryomov 	msg_size += req->r_num_ops * sizeof(struct ceph_osd_op);
64913d1ad16SIlya Dryomov 
65013d1ad16SIlya Dryomov 	if (req->r_mempool)
6510d9c1ab3SIlya Dryomov 		msg = ceph_msgpool_get(&osdc->msgpool_op_reply, msg_size,
6520d9c1ab3SIlya Dryomov 				       num_reply_data_items);
65313d1ad16SIlya Dryomov 	else
6540d9c1ab3SIlya Dryomov 		msg = ceph_msg_new2(CEPH_MSG_OSD_OPREPLY, msg_size,
6550d9c1ab3SIlya Dryomov 				    num_reply_data_items, gfp, true);
65613d1ad16SIlya Dryomov 	if (!msg)
65713d1ad16SIlya Dryomov 		return -ENOMEM;
65813d1ad16SIlya Dryomov 
65913d1ad16SIlya Dryomov 	req->r_reply = msg;
66013d1ad16SIlya Dryomov 
66113d1ad16SIlya Dryomov 	return 0;
66213d1ad16SIlya Dryomov }
6633d14c5d2SYehuda Sadeh 
osd_req_opcode_valid(u16 opcode)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 
get_num_data_items(struct ceph_osd_request * req,int * num_request_data_items,int * num_reply_data_items)6750d9c1ab3SIlya Dryomov static void get_num_data_items(struct ceph_osd_request *req,
6760d9c1ab3SIlya Dryomov 			       int *num_request_data_items,
6770d9c1ab3SIlya Dryomov 			       int *num_reply_data_items)
6780d9c1ab3SIlya Dryomov {
6790d9c1ab3SIlya Dryomov 	struct ceph_osd_req_op *op;
6800d9c1ab3SIlya Dryomov 
6810d9c1ab3SIlya Dryomov 	*num_request_data_items = 0;
6820d9c1ab3SIlya Dryomov 	*num_reply_data_items = 0;
6830d9c1ab3SIlya Dryomov 
6840d9c1ab3SIlya Dryomov 	for (op = req->r_ops; op != &req->r_ops[req->r_num_ops]; op++) {
6850d9c1ab3SIlya Dryomov 		switch (op->op) {
6860d9c1ab3SIlya Dryomov 		/* request */
6870d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_WRITE:
6880d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_WRITEFULL:
6890d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_SETXATTR:
6900d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_CMPXATTR:
6910d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_NOTIFY_ACK:
69278beb0ffSLuis Henriques 		case CEPH_OSD_OP_COPY_FROM2:
6930d9c1ab3SIlya Dryomov 			*num_request_data_items += 1;
6940d9c1ab3SIlya Dryomov 			break;
6950d9c1ab3SIlya Dryomov 
6960d9c1ab3SIlya Dryomov 		/* reply */
6970d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_STAT:
6980d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_READ:
699f628d799SJeff Layton 		case CEPH_OSD_OP_SPARSE_READ:
7000d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_LIST_WATCHERS:
7010d9c1ab3SIlya Dryomov 			*num_reply_data_items += 1;
7020d9c1ab3SIlya Dryomov 			break;
7030d9c1ab3SIlya Dryomov 
7040d9c1ab3SIlya Dryomov 		/* both */
7050d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_NOTIFY:
7060d9c1ab3SIlya Dryomov 			*num_request_data_items += 1;
7070d9c1ab3SIlya Dryomov 			*num_reply_data_items += 1;
7080d9c1ab3SIlya Dryomov 			break;
7090d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_CALL:
7100d9c1ab3SIlya Dryomov 			*num_request_data_items += 2;
7110d9c1ab3SIlya Dryomov 			*num_reply_data_items += 1;
7120d9c1ab3SIlya Dryomov 			break;
7130d9c1ab3SIlya Dryomov 
7140d9c1ab3SIlya Dryomov 		default:
7150d9c1ab3SIlya Dryomov 			WARN_ON(!osd_req_opcode_valid(op->op));
7160d9c1ab3SIlya Dryomov 			break;
7170d9c1ab3SIlya Dryomov 		}
7180d9c1ab3SIlya Dryomov 	}
7190d9c1ab3SIlya Dryomov }
7200d9c1ab3SIlya Dryomov 
7210d9c1ab3SIlya Dryomov /*
7220d9c1ab3SIlya Dryomov  * oid, oloc and OSD op opcode(s) must be filled in before this function
7230d9c1ab3SIlya Dryomov  * is called.
7240d9c1ab3SIlya Dryomov  */
ceph_osdc_alloc_messages(struct ceph_osd_request * req,gfp_t gfp)7250d9c1ab3SIlya Dryomov int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp)
7260d9c1ab3SIlya Dryomov {
7270d9c1ab3SIlya Dryomov 	int num_request_data_items, num_reply_data_items;
7280d9c1ab3SIlya Dryomov 
7290d9c1ab3SIlya Dryomov 	get_num_data_items(req, &num_request_data_items, &num_reply_data_items);
7300d9c1ab3SIlya Dryomov 	return __ceph_osdc_alloc_messages(req, gfp, num_request_data_items,
7310d9c1ab3SIlya Dryomov 					  num_reply_data_items);
7320d9c1ab3SIlya Dryomov }
7330d9c1ab3SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_alloc_messages);
7340d9c1ab3SIlya Dryomov 
73533803f33SAlex Elder /*
73633803f33SAlex Elder  * This is an osd op init function for opcodes that have no data or
73733803f33SAlex Elder  * other information associated with them.  It also serves as a
73833803f33SAlex Elder  * common init routine for all the other init functions, below.
73933803f33SAlex Elder  */
740042f6498SJeff Layton struct ceph_osd_req_op *
osd_req_op_init(struct ceph_osd_request * osd_req,unsigned int which,u16 opcode,u32 flags)741042f6498SJeff Layton osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
742144cba14SYan, Zheng 		 u16 opcode, u32 flags)
74333803f33SAlex Elder {
744c99d2d4aSAlex Elder 	struct ceph_osd_req_op *op;
745c99d2d4aSAlex Elder 
746c99d2d4aSAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
74733803f33SAlex Elder 	BUG_ON(!osd_req_opcode_valid(opcode));
74833803f33SAlex Elder 
749c99d2d4aSAlex Elder 	op = &osd_req->r_ops[which];
75033803f33SAlex Elder 	memset(op, 0, sizeof (*op));
75133803f33SAlex Elder 	op->op = opcode;
752144cba14SYan, Zheng 	op->flags = flags;
753c99d2d4aSAlex Elder 
754c99d2d4aSAlex Elder 	return op;
75533803f33SAlex Elder }
75649719778SAlex Elder EXPORT_SYMBOL(osd_req_op_init);
75749719778SAlex Elder 
osd_req_op_extent_init(struct ceph_osd_request * osd_req,unsigned int which,u16 opcode,u64 offset,u64 length,u64 truncate_size,u32 truncate_seq)758c99d2d4aSAlex Elder void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
759c99d2d4aSAlex Elder 				unsigned int which, u16 opcode,
76033803f33SAlex Elder 				u64 offset, u64 length,
76133803f33SAlex Elder 				u64 truncate_size, u32 truncate_seq)
76233803f33SAlex Elder {
763042f6498SJeff Layton 	struct ceph_osd_req_op *op = osd_req_op_init(osd_req, which,
764144cba14SYan, Zheng 						     opcode, 0);
76533803f33SAlex Elder 	size_t payload_len = 0;
76633803f33SAlex Elder 
767ad7a60deSLi Wang 	BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
768e30b7577SIlya Dryomov 	       opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
769f628d799SJeff Layton 	       opcode != CEPH_OSD_OP_TRUNCATE && opcode != CEPH_OSD_OP_SPARSE_READ);
77033803f33SAlex Elder 
77133803f33SAlex Elder 	op->extent.offset = offset;
77233803f33SAlex Elder 	op->extent.length = length;
77333803f33SAlex Elder 	op->extent.truncate_size = truncate_size;
77433803f33SAlex Elder 	op->extent.truncate_seq = truncate_seq;
775e30b7577SIlya Dryomov 	if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
77633803f33SAlex Elder 		payload_len += length;
77733803f33SAlex Elder 
778de2aa102SIlya Dryomov 	op->indata_len = payload_len;
77933803f33SAlex Elder }
78033803f33SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_init);
78133803f33SAlex Elder 
osd_req_op_extent_update(struct ceph_osd_request * osd_req,unsigned int which,u64 length)782c99d2d4aSAlex Elder void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
783c99d2d4aSAlex Elder 				unsigned int which, u64 length)
784e5975c7cSAlex Elder {
785c99d2d4aSAlex Elder 	struct ceph_osd_req_op *op;
786c99d2d4aSAlex Elder 	u64 previous;
787c99d2d4aSAlex Elder 
788c99d2d4aSAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
789c99d2d4aSAlex Elder 	op = &osd_req->r_ops[which];
790c99d2d4aSAlex Elder 	previous = op->extent.length;
791e5975c7cSAlex Elder 
792e5975c7cSAlex Elder 	if (length == previous)
793e5975c7cSAlex Elder 		return;		/* Nothing to do */
794e5975c7cSAlex Elder 	BUG_ON(length > previous);
795e5975c7cSAlex Elder 
796e5975c7cSAlex Elder 	op->extent.length = length;
797d641df81SYan, Zheng 	if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
798de2aa102SIlya Dryomov 		op->indata_len -= previous - length;
799e5975c7cSAlex Elder }
800e5975c7cSAlex Elder EXPORT_SYMBOL(osd_req_op_extent_update);
801e5975c7cSAlex Elder 
osd_req_op_extent_dup_last(struct ceph_osd_request * osd_req,unsigned int which,u64 offset_inc)8022c63f49aSYan, Zheng void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
8032c63f49aSYan, Zheng 				unsigned int which, u64 offset_inc)
8042c63f49aSYan, Zheng {
8052c63f49aSYan, Zheng 	struct ceph_osd_req_op *op, *prev_op;
8062c63f49aSYan, Zheng 
8072c63f49aSYan, Zheng 	BUG_ON(which + 1 >= osd_req->r_num_ops);
8082c63f49aSYan, Zheng 
8092c63f49aSYan, Zheng 	prev_op = &osd_req->r_ops[which];
810042f6498SJeff Layton 	op = osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags);
8112c63f49aSYan, Zheng 	/* dup previous one */
8122c63f49aSYan, Zheng 	op->indata_len = prev_op->indata_len;
8132c63f49aSYan, Zheng 	op->outdata_len = prev_op->outdata_len;
8142c63f49aSYan, Zheng 	op->extent = prev_op->extent;
8152c63f49aSYan, Zheng 	/* adjust offset */
8162c63f49aSYan, Zheng 	op->extent.offset += offset_inc;
8172c63f49aSYan, Zheng 	op->extent.length -= offset_inc;
8182c63f49aSYan, Zheng 
8192c63f49aSYan, Zheng 	if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
8202c63f49aSYan, Zheng 		op->indata_len -= offset_inc;
8212c63f49aSYan, Zheng }
8222c63f49aSYan, Zheng EXPORT_SYMBOL(osd_req_op_extent_dup_last);
8232c63f49aSYan, Zheng 
osd_req_op_cls_init(struct ceph_osd_request * osd_req,unsigned int which,const char * class,const char * method)824fe943d50SChengguang Xu int osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
82524639ce5SIlya Dryomov 			const char *class, const char *method)
82633803f33SAlex Elder {
82724639ce5SIlya Dryomov 	struct ceph_osd_req_op *op;
8285f562df5SAlex Elder 	struct ceph_pagelist *pagelist;
82933803f33SAlex Elder 	size_t payload_len = 0;
83033803f33SAlex Elder 	size_t size;
8314766815bSDavid Disseldorp 	int ret;
83233803f33SAlex Elder 
833042f6498SJeff Layton 	op = osd_req_op_init(osd_req, which, CEPH_OSD_OP_CALL, 0);
83433803f33SAlex Elder 
83533165d47SIlya Dryomov 	pagelist = ceph_pagelist_alloc(GFP_NOFS);
836fe943d50SChengguang Xu 	if (!pagelist)
837fe943d50SChengguang Xu 		return -ENOMEM;
838fe943d50SChengguang Xu 
83933803f33SAlex Elder 	op->cls.class_name = class;
84033803f33SAlex Elder 	size = strlen(class);
84133803f33SAlex Elder 	BUG_ON(size > (size_t) U8_MAX);
84233803f33SAlex Elder 	op->cls.class_len = size;
8434766815bSDavid Disseldorp 	ret = ceph_pagelist_append(pagelist, class, size);
8444766815bSDavid Disseldorp 	if (ret)
8454766815bSDavid Disseldorp 		goto err_pagelist_free;
84633803f33SAlex Elder 	payload_len += size;
84733803f33SAlex Elder 
84833803f33SAlex Elder 	op->cls.method_name = method;
84933803f33SAlex Elder 	size = strlen(method);
85033803f33SAlex Elder 	BUG_ON(size > (size_t) U8_MAX);
85133803f33SAlex Elder 	op->cls.method_len = size;
8524766815bSDavid Disseldorp 	ret = ceph_pagelist_append(pagelist, method, size);
8534766815bSDavid Disseldorp 	if (ret)
8544766815bSDavid Disseldorp 		goto err_pagelist_free;
85533803f33SAlex Elder 	payload_len += size;
85633803f33SAlex Elder 
857a4ce40a9SAlex Elder 	osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
858de2aa102SIlya Dryomov 	op->indata_len = payload_len;
859fe943d50SChengguang Xu 	return 0;
8604766815bSDavid Disseldorp 
8614766815bSDavid Disseldorp err_pagelist_free:
8624766815bSDavid Disseldorp 	ceph_pagelist_release(pagelist);
8634766815bSDavid Disseldorp 	return ret;
86433803f33SAlex Elder }
86533803f33SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_init);
8668c042b0dSAlex Elder 
osd_req_op_xattr_init(struct ceph_osd_request * osd_req,unsigned int which,u16 opcode,const char * name,const void * value,size_t size,u8 cmp_op,u8 cmp_mode)867d74b50beSYan, Zheng int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
868d74b50beSYan, Zheng 			  u16 opcode, const char *name, const void *value,
869d74b50beSYan, Zheng 			  size_t size, u8 cmp_op, u8 cmp_mode)
870d74b50beSYan, Zheng {
871042f6498SJeff Layton 	struct ceph_osd_req_op *op = osd_req_op_init(osd_req, which,
872144cba14SYan, Zheng 						     opcode, 0);
873d74b50beSYan, Zheng 	struct ceph_pagelist *pagelist;
874d74b50beSYan, Zheng 	size_t payload_len;
8754766815bSDavid Disseldorp 	int ret;
876d74b50beSYan, Zheng 
877d74b50beSYan, Zheng 	BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
878d74b50beSYan, Zheng 
87933165d47SIlya Dryomov 	pagelist = ceph_pagelist_alloc(GFP_NOFS);
880d74b50beSYan, Zheng 	if (!pagelist)
881d74b50beSYan, Zheng 		return -ENOMEM;
882d74b50beSYan, Zheng 
883d74b50beSYan, Zheng 	payload_len = strlen(name);
884d74b50beSYan, Zheng 	op->xattr.name_len = payload_len;
8854766815bSDavid Disseldorp 	ret = ceph_pagelist_append(pagelist, name, payload_len);
8864766815bSDavid Disseldorp 	if (ret)
8874766815bSDavid Disseldorp 		goto err_pagelist_free;
888d74b50beSYan, Zheng 
889d74b50beSYan, Zheng 	op->xattr.value_len = size;
8904766815bSDavid Disseldorp 	ret = ceph_pagelist_append(pagelist, value, size);
8914766815bSDavid Disseldorp 	if (ret)
8924766815bSDavid Disseldorp 		goto err_pagelist_free;
893d74b50beSYan, Zheng 	payload_len += size;
894d74b50beSYan, Zheng 
895d74b50beSYan, Zheng 	op->xattr.cmp_op = cmp_op;
896d74b50beSYan, Zheng 	op->xattr.cmp_mode = cmp_mode;
897d74b50beSYan, Zheng 
898d74b50beSYan, Zheng 	ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
899de2aa102SIlya Dryomov 	op->indata_len = payload_len;
900d74b50beSYan, Zheng 	return 0;
9014766815bSDavid Disseldorp 
9024766815bSDavid Disseldorp err_pagelist_free:
9034766815bSDavid Disseldorp 	ceph_pagelist_release(pagelist);
9044766815bSDavid Disseldorp 	return ret;
905d74b50beSYan, Zheng }
906d74b50beSYan, Zheng EXPORT_SYMBOL(osd_req_op_xattr_init);
907d74b50beSYan, Zheng 
908922dab61SIlya Dryomov /*
909922dab61SIlya Dryomov  * @watch_opcode: CEPH_OSD_WATCH_OP_*
910922dab61SIlya Dryomov  */
osd_req_op_watch_init(struct ceph_osd_request * req,int which,u8 watch_opcode,u64 cookie,u32 gen)911922dab61SIlya Dryomov static void osd_req_op_watch_init(struct ceph_osd_request *req, int which,
91275dbb685SIlya Dryomov 				  u8 watch_opcode, u64 cookie, u32 gen)
91333803f33SAlex Elder {
914922dab61SIlya Dryomov 	struct ceph_osd_req_op *op;
91533803f33SAlex Elder 
916042f6498SJeff Layton 	op = osd_req_op_init(req, which, CEPH_OSD_OP_WATCH, 0);
91733803f33SAlex Elder 	op->watch.cookie = cookie;
918922dab61SIlya Dryomov 	op->watch.op = watch_opcode;
91975dbb685SIlya Dryomov 	op->watch.gen = gen;
92075dbb685SIlya Dryomov }
92175dbb685SIlya Dryomov 
92275dbb685SIlya Dryomov /*
92375dbb685SIlya Dryomov  * prot_ver, timeout and notify payload (may be empty) should already be
92475dbb685SIlya Dryomov  * encoded in @request_pl
92575dbb685SIlya Dryomov  */
osd_req_op_notify_init(struct ceph_osd_request * req,int which,u64 cookie,struct ceph_pagelist * request_pl)92675dbb685SIlya Dryomov static void osd_req_op_notify_init(struct ceph_osd_request *req, int which,
92775dbb685SIlya Dryomov 				   u64 cookie, struct ceph_pagelist *request_pl)
92875dbb685SIlya Dryomov {
92975dbb685SIlya Dryomov 	struct ceph_osd_req_op *op;
93075dbb685SIlya Dryomov 
93175dbb685SIlya Dryomov 	op = osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY, 0);
93275dbb685SIlya Dryomov 	op->notify.cookie = cookie;
93375dbb685SIlya Dryomov 
93475dbb685SIlya Dryomov 	ceph_osd_data_pagelist_init(&op->notify.request_data, request_pl);
93575dbb685SIlya Dryomov 	op->indata_len = request_pl->length;
93633803f33SAlex Elder }
93733803f33SAlex Elder 
938d3798accSIlya Dryomov /*
939d3798accSIlya Dryomov  * @flags: CEPH_OSD_OP_ALLOC_HINT_FLAG_*
940d3798accSIlya Dryomov  */
osd_req_op_alloc_hint_init(struct ceph_osd_request * osd_req,unsigned int which,u64 expected_object_size,u64 expected_write_size,u32 flags)941c647b8a8SIlya Dryomov void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
942c647b8a8SIlya Dryomov 				unsigned int which,
943c647b8a8SIlya Dryomov 				u64 expected_object_size,
944d3798accSIlya Dryomov 				u64 expected_write_size,
945d3798accSIlya Dryomov 				u32 flags)
946c647b8a8SIlya Dryomov {
947042f6498SJeff Layton 	struct ceph_osd_req_op *op;
948c647b8a8SIlya Dryomov 
949042f6498SJeff Layton 	op = osd_req_op_init(osd_req, which, CEPH_OSD_OP_SETALLOCHINT, 0);
950c647b8a8SIlya Dryomov 	op->alloc_hint.expected_object_size = expected_object_size;
951c647b8a8SIlya Dryomov 	op->alloc_hint.expected_write_size = expected_write_size;
952d3798accSIlya Dryomov 	op->alloc_hint.flags = flags;
953c647b8a8SIlya Dryomov 
954c647b8a8SIlya Dryomov 	/*
955c647b8a8SIlya Dryomov 	 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
956c647b8a8SIlya Dryomov 	 * not worth a feature bit.  Set FAILOK per-op flag to make
957c647b8a8SIlya Dryomov 	 * sure older osds don't trip over an unsupported opcode.
958c647b8a8SIlya Dryomov 	 */
959c647b8a8SIlya Dryomov 	op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
960c647b8a8SIlya Dryomov }
961c647b8a8SIlya Dryomov EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
962c647b8a8SIlya Dryomov 
ceph_osdc_msg_data_add(struct ceph_msg * msg,struct ceph_osd_data * osd_data)96390af3602SAlex Elder static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
964ec9123c5SAlex Elder 				struct ceph_osd_data *osd_data)
965ec9123c5SAlex Elder {
966ec9123c5SAlex Elder 	u64 length = ceph_osd_data_length(osd_data);
967ec9123c5SAlex Elder 
968ec9123c5SAlex Elder 	if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
969ec9123c5SAlex Elder 		BUG_ON(length > (u64) SIZE_MAX);
970ec9123c5SAlex Elder 		if (length)
97190af3602SAlex Elder 			ceph_msg_data_add_pages(msg, osd_data->pages,
972e8862740SIlya Dryomov 					length, osd_data->alignment, false);
973ec9123c5SAlex Elder 	} else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
974ec9123c5SAlex Elder 		BUG_ON(!length);
97590af3602SAlex Elder 		ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
976ec9123c5SAlex Elder #ifdef CONFIG_BLOCK
977ec9123c5SAlex Elder 	} else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
9785359a17dSIlya Dryomov 		ceph_msg_data_add_bio(msg, &osd_data->bio_pos, length);
979ec9123c5SAlex Elder #endif
980b9e281c2SIlya Dryomov 	} else if (osd_data->type == CEPH_OSD_DATA_TYPE_BVECS) {
981b9e281c2SIlya Dryomov 		ceph_msg_data_add_bvecs(msg, &osd_data->bvec_pos);
982dee0c5f8SJeff Layton 	} else if (osd_data->type == CEPH_OSD_DATA_TYPE_ITER) {
983dee0c5f8SJeff Layton 		ceph_msg_data_add_iter(msg, &osd_data->iter);
984ec9123c5SAlex Elder 	} else {
985ec9123c5SAlex Elder 		BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
986ec9123c5SAlex Elder 	}
987ec9123c5SAlex Elder }
988ec9123c5SAlex Elder 
osd_req_encode_op(struct ceph_osd_op * dst,const struct ceph_osd_req_op * src)989bb873b53SIlya Dryomov static u32 osd_req_encode_op(struct ceph_osd_op *dst,
990bb873b53SIlya Dryomov 			     const struct ceph_osd_req_op *src)
9913d14c5d2SYehuda Sadeh {
992065a68f9SAlex Elder 	switch (src->op) {
993fbfab539SAlex Elder 	case CEPH_OSD_OP_STAT:
994fbfab539SAlex Elder 		break;
9953d14c5d2SYehuda Sadeh 	case CEPH_OSD_OP_READ:
996f628d799SJeff Layton 	case CEPH_OSD_OP_SPARSE_READ:
9973d14c5d2SYehuda Sadeh 	case CEPH_OSD_OP_WRITE:
998e30b7577SIlya Dryomov 	case CEPH_OSD_OP_WRITEFULL:
999ad7a60deSLi Wang 	case CEPH_OSD_OP_ZERO:
1000ad7a60deSLi Wang 	case CEPH_OSD_OP_TRUNCATE:
1001175face2SAlex Elder 		dst->extent.offset = cpu_to_le64(src->extent.offset);
1002175face2SAlex Elder 		dst->extent.length = cpu_to_le64(src->extent.length);
10033d14c5d2SYehuda Sadeh 		dst->extent.truncate_size =
10043d14c5d2SYehuda Sadeh 			cpu_to_le64(src->extent.truncate_size);
10053d14c5d2SYehuda Sadeh 		dst->extent.truncate_seq =
10063d14c5d2SYehuda Sadeh 			cpu_to_le32(src->extent.truncate_seq);
10073d14c5d2SYehuda Sadeh 		break;
10083d14c5d2SYehuda Sadeh 	case CEPH_OSD_OP_CALL:
10093d14c5d2SYehuda Sadeh 		dst->cls.class_len = src->cls.class_len;
10103d14c5d2SYehuda Sadeh 		dst->cls.method_len = src->cls.method_len;
1011bb873b53SIlya Dryomov 		dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
10123d14c5d2SYehuda Sadeh 		break;
1013a40c4f10SYehuda Sadeh 	case CEPH_OSD_OP_WATCH:
1014a40c4f10SYehuda Sadeh 		dst->watch.cookie = cpu_to_le64(src->watch.cookie);
1015922dab61SIlya Dryomov 		dst->watch.ver = cpu_to_le64(0);
1016922dab61SIlya Dryomov 		dst->watch.op = src->watch.op;
1017922dab61SIlya Dryomov 		dst->watch.gen = cpu_to_le32(src->watch.gen);
1018922dab61SIlya Dryomov 		break;
1019922dab61SIlya Dryomov 	case CEPH_OSD_OP_NOTIFY_ACK:
1020a40c4f10SYehuda Sadeh 		break;
102119079203SIlya Dryomov 	case CEPH_OSD_OP_NOTIFY:
102219079203SIlya Dryomov 		dst->notify.cookie = cpu_to_le64(src->notify.cookie);
102319079203SIlya Dryomov 		break;
1024a4ed38d7SDouglas Fuller 	case CEPH_OSD_OP_LIST_WATCHERS:
1025a4ed38d7SDouglas Fuller 		break;
1026c647b8a8SIlya Dryomov 	case CEPH_OSD_OP_SETALLOCHINT:
1027c647b8a8SIlya Dryomov 		dst->alloc_hint.expected_object_size =
1028c647b8a8SIlya Dryomov 		    cpu_to_le64(src->alloc_hint.expected_object_size);
1029c647b8a8SIlya Dryomov 		dst->alloc_hint.expected_write_size =
1030c647b8a8SIlya Dryomov 		    cpu_to_le64(src->alloc_hint.expected_write_size);
1031d3798accSIlya Dryomov 		dst->alloc_hint.flags = cpu_to_le32(src->alloc_hint.flags);
1032c647b8a8SIlya Dryomov 		break;
1033d74b50beSYan, Zheng 	case CEPH_OSD_OP_SETXATTR:
1034d74b50beSYan, Zheng 	case CEPH_OSD_OP_CMPXATTR:
1035d74b50beSYan, Zheng 		dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
1036d74b50beSYan, Zheng 		dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
1037d74b50beSYan, Zheng 		dst->xattr.cmp_op = src->xattr.cmp_op;
1038d74b50beSYan, Zheng 		dst->xattr.cmp_mode = src->xattr.cmp_mode;
1039d74b50beSYan, Zheng 		break;
1040864e9197SYan, Zheng 	case CEPH_OSD_OP_CREATE:
1041864e9197SYan, Zheng 	case CEPH_OSD_OP_DELETE:
1042864e9197SYan, Zheng 		break;
104378beb0ffSLuis Henriques 	case CEPH_OSD_OP_COPY_FROM2:
104423ddf9beSLuis Henriques 		dst->copy_from.snapid = cpu_to_le64(src->copy_from.snapid);
104523ddf9beSLuis Henriques 		dst->copy_from.src_version =
104623ddf9beSLuis Henriques 			cpu_to_le64(src->copy_from.src_version);
104723ddf9beSLuis Henriques 		dst->copy_from.flags = src->copy_from.flags;
104823ddf9beSLuis Henriques 		dst->copy_from.src_fadvise_flags =
104923ddf9beSLuis Henriques 			cpu_to_le32(src->copy_from.src_fadvise_flags);
105023ddf9beSLuis Henriques 		break;
105169dd3b39SJeff Layton 	case CEPH_OSD_OP_ASSERT_VER:
105269dd3b39SJeff Layton 		dst->assert_ver.unused = cpu_to_le64(0);
105369dd3b39SJeff Layton 		dst->assert_ver.ver = cpu_to_le64(src->assert_ver.ver);
105469dd3b39SJeff Layton 		break;
10553d14c5d2SYehuda Sadeh 	default:
10564c46459cSAlex Elder 		pr_err("unsupported osd opcode %s\n",
10578f63ca2dSAlex Elder 			ceph_osd_op_name(src->op));
10584c46459cSAlex Elder 		WARN_ON(1);
1059a8dd0a37SAlex Elder 
1060a8dd0a37SAlex Elder 		return 0;
10613d14c5d2SYehuda Sadeh 	}
10627b25bf5fSIlya Dryomov 
1063a8dd0a37SAlex Elder 	dst->op = cpu_to_le16(src->op);
10647b25bf5fSIlya Dryomov 	dst->flags = cpu_to_le32(src->flags);
1065de2aa102SIlya Dryomov 	dst->payload_len = cpu_to_le32(src->indata_len);
1066175face2SAlex Elder 
1067bb873b53SIlya Dryomov 	return src->indata_len;
10683d14c5d2SYehuda Sadeh }
10693d14c5d2SYehuda Sadeh 
10703d14c5d2SYehuda Sadeh /*
10713d14c5d2SYehuda Sadeh  * build new request AND message, calculate layout, and adjust file
10723d14c5d2SYehuda Sadeh  * extent as needed.
10733d14c5d2SYehuda Sadeh  *
10743d14c5d2SYehuda Sadeh  * if the file was recently truncated, we include information about its
10753d14c5d2SYehuda Sadeh  * old and new size so that the object can be updated appropriately.  (we
10763d14c5d2SYehuda Sadeh  * avoid synchronously deleting truncated objects because it's slow.)
10773d14c5d2SYehuda Sadeh  */
ceph_osdc_new_request(struct ceph_osd_client * osdc,struct ceph_file_layout * layout,struct ceph_vino vino,u64 off,u64 * plen,unsigned int which,int num_ops,int opcode,int flags,struct ceph_snap_context * snapc,u32 truncate_seq,u64 truncate_size,bool use_mempool)10783d14c5d2SYehuda Sadeh struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
10793d14c5d2SYehuda Sadeh 					       struct ceph_file_layout *layout,
10803d14c5d2SYehuda Sadeh 					       struct ceph_vino vino,
1081715e4cd4SYan, Zheng 					       u64 off, u64 *plen,
1082715e4cd4SYan, Zheng 					       unsigned int which, int num_ops,
10833d14c5d2SYehuda Sadeh 					       int opcode, int flags,
10843d14c5d2SYehuda Sadeh 					       struct ceph_snap_context *snapc,
10853d14c5d2SYehuda Sadeh 					       u32 truncate_seq,
10863d14c5d2SYehuda Sadeh 					       u64 truncate_size,
1087153e5167SAlex Elder 					       bool use_mempool)
10883d14c5d2SYehuda Sadeh {
10893d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
109075d1c941SAlex Elder 	u64 objnum = 0;
109175d1c941SAlex Elder 	u64 objoff = 0;
109275d1c941SAlex Elder 	u64 objlen = 0;
10936816282dSSage Weil 	int r;
10943d14c5d2SYehuda Sadeh 
1095ad7a60deSLi Wang 	BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
1096864e9197SYan, Zheng 	       opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
1097f628d799SJeff Layton 	       opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE &&
1098f628d799SJeff Layton 	       opcode != CEPH_OSD_OP_SPARSE_READ);
10993d14c5d2SYehuda Sadeh 
1100acead002SAlex Elder 	req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
1101ae7ca4a3SAlex Elder 					GFP_NOFS);
110213d1ad16SIlya Dryomov 	if (!req) {
110313d1ad16SIlya Dryomov 		r = -ENOMEM;
110413d1ad16SIlya Dryomov 		goto fail;
110513d1ad16SIlya Dryomov 	}
110679528734SAlex Elder 
11073d14c5d2SYehuda Sadeh 	/* calculate max write size */
1108a19dadfbSAlex Elder 	r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
110913d1ad16SIlya Dryomov 	if (r)
111013d1ad16SIlya Dryomov 		goto fail;
1111a19dadfbSAlex Elder 
1112864e9197SYan, Zheng 	if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
1113144cba14SYan, Zheng 		osd_req_op_init(req, which, opcode, 0);
1114864e9197SYan, Zheng 	} else {
11157627151eSYan, Zheng 		u32 object_size = layout->object_size;
1116864e9197SYan, Zheng 		u32 object_base = off - objoff;
1117ccca4e37SYan, Zheng 		if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
1118d18d1e28SAlex Elder 			if (truncate_size <= object_base) {
1119d18d1e28SAlex Elder 				truncate_size = 0;
1120d18d1e28SAlex Elder 			} else {
1121d18d1e28SAlex Elder 				truncate_size -= object_base;
1122d18d1e28SAlex Elder 				if (truncate_size > object_size)
1123d18d1e28SAlex Elder 					truncate_size = object_size;
1124d18d1e28SAlex Elder 			}
1125ccca4e37SYan, Zheng 		}
1126715e4cd4SYan, Zheng 		osd_req_op_extent_init(req, which, opcode, objoff, objlen,
1127b0270324SAlex Elder 				       truncate_size, truncate_seq);
1128864e9197SYan, Zheng 	}
1129d18d1e28SAlex Elder 
11307627151eSYan, Zheng 	req->r_base_oloc.pool = layout->pool_id;
113130c156d9SYan, Zheng 	req->r_base_oloc.pool_ns = ceph_try_get_string(layout->pool_ns);
1132d30291b9SIlya Dryomov 	ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum);
113322d2cfdfSIlya Dryomov 	req->r_flags = flags | osdc->client->options->read_from_replica;
1134dbe0fc41SAlex Elder 
1135bb873b53SIlya Dryomov 	req->r_snapid = vino.snap;
1136bb873b53SIlya Dryomov 	if (flags & CEPH_OSD_FLAG_WRITE)
1137bb873b53SIlya Dryomov 		req->r_data_offset = off;
1138bb873b53SIlya Dryomov 
11394e8c4c23SJeff Layton 	if (num_ops > 1) {
11404e8c4c23SJeff Layton 		int num_req_ops, num_rep_ops;
11414e8c4c23SJeff Layton 
11420d9c1ab3SIlya Dryomov 		/*
11434e8c4c23SJeff Layton 		 * If this is a multi-op write request, assume that we'll need
11444e8c4c23SJeff Layton 		 * request ops. If it's a multi-op read then assume we'll need
11454e8c4c23SJeff Layton 		 * reply ops. Anything else and call it -EINVAL.
11460d9c1ab3SIlya Dryomov 		 */
11474e8c4c23SJeff Layton 		if (flags & CEPH_OSD_FLAG_WRITE) {
11484e8c4c23SJeff Layton 			num_req_ops = num_ops;
11494e8c4c23SJeff Layton 			num_rep_ops = 0;
11504e8c4c23SJeff Layton 		} else if (flags & CEPH_OSD_FLAG_READ) {
11514e8c4c23SJeff Layton 			num_req_ops = 0;
11524e8c4c23SJeff Layton 			num_rep_ops = num_ops;
11534e8c4c23SJeff Layton 		} else {
11544e8c4c23SJeff Layton 			r = -EINVAL;
11554e8c4c23SJeff Layton 			goto fail;
11564e8c4c23SJeff Layton 		}
11574e8c4c23SJeff Layton 
11584e8c4c23SJeff Layton 		r = __ceph_osdc_alloc_messages(req, GFP_NOFS, num_req_ops,
11594e8c4c23SJeff Layton 					       num_rep_ops);
11604e8c4c23SJeff Layton 	} else {
116113d1ad16SIlya Dryomov 		r = ceph_osdc_alloc_messages(req, GFP_NOFS);
11624e8c4c23SJeff Layton 	}
116313d1ad16SIlya Dryomov 	if (r)
116413d1ad16SIlya Dryomov 		goto fail;
116513d1ad16SIlya Dryomov 
11663d14c5d2SYehuda Sadeh 	return req;
116713d1ad16SIlya Dryomov 
116813d1ad16SIlya Dryomov fail:
116913d1ad16SIlya Dryomov 	ceph_osdc_put_request(req);
117013d1ad16SIlya Dryomov 	return ERR_PTR(r);
11713d14c5d2SYehuda Sadeh }
11723d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_new_request);
11733d14c5d2SYehuda Sadeh 
__ceph_alloc_sparse_ext_map(struct ceph_osd_req_op * op,int cnt)1174a679e50fSJeff Layton int __ceph_alloc_sparse_ext_map(struct ceph_osd_req_op *op, int cnt)
1175a679e50fSJeff Layton {
1176a679e50fSJeff Layton 	op->extent.sparse_ext_cnt = cnt;
1177a679e50fSJeff Layton 	op->extent.sparse_ext = kmalloc_array(cnt,
1178a679e50fSJeff Layton 					      sizeof(*op->extent.sparse_ext),
1179a679e50fSJeff Layton 					      GFP_NOFS);
1180a679e50fSJeff Layton 	if (!op->extent.sparse_ext)
1181a679e50fSJeff Layton 		return -ENOMEM;
1182a679e50fSJeff Layton 	return 0;
1183a679e50fSJeff Layton }
1184a679e50fSJeff Layton EXPORT_SYMBOL(__ceph_alloc_sparse_ext_map);
1185a679e50fSJeff Layton 
11863d14c5d2SYehuda Sadeh /*
11873d14c5d2SYehuda Sadeh  * We keep osd requests in an rbtree, sorted by ->r_tid.
11883d14c5d2SYehuda Sadeh  */
DEFINE_RB_FUNCS(request,struct ceph_osd_request,r_tid,r_node)1189fcd00b68SIlya Dryomov DEFINE_RB_FUNCS(request, struct ceph_osd_request, r_tid, r_node)
11904609245eSIlya Dryomov DEFINE_RB_FUNCS(request_mc, struct ceph_osd_request, r_tid, r_mc_node)
11913d14c5d2SYehuda Sadeh 
119266850df5SIlya Dryomov /*
119366850df5SIlya Dryomov  * Call @fn on each OSD request as long as @fn returns 0.
119466850df5SIlya Dryomov  */
119566850df5SIlya Dryomov static void for_each_request(struct ceph_osd_client *osdc,
119666850df5SIlya Dryomov 			int (*fn)(struct ceph_osd_request *req, void *arg),
119766850df5SIlya Dryomov 			void *arg)
119866850df5SIlya Dryomov {
119966850df5SIlya Dryomov 	struct rb_node *n, *p;
120066850df5SIlya Dryomov 
120166850df5SIlya Dryomov 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
120266850df5SIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
120366850df5SIlya Dryomov 
120466850df5SIlya Dryomov 		for (p = rb_first(&osd->o_requests); p; ) {
120566850df5SIlya Dryomov 			struct ceph_osd_request *req =
120666850df5SIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
120766850df5SIlya Dryomov 
120866850df5SIlya Dryomov 			p = rb_next(p);
120966850df5SIlya Dryomov 			if (fn(req, arg))
121066850df5SIlya Dryomov 				return;
121166850df5SIlya Dryomov 		}
121266850df5SIlya Dryomov 	}
121366850df5SIlya Dryomov 
121466850df5SIlya Dryomov 	for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
121566850df5SIlya Dryomov 		struct ceph_osd_request *req =
121666850df5SIlya Dryomov 		    rb_entry(p, struct ceph_osd_request, r_node);
121766850df5SIlya Dryomov 
121866850df5SIlya Dryomov 		p = rb_next(p);
121966850df5SIlya Dryomov 		if (fn(req, arg))
122066850df5SIlya Dryomov 			return;
122166850df5SIlya Dryomov 	}
122266850df5SIlya Dryomov }
122366850df5SIlya Dryomov 
osd_homeless(struct ceph_osd * osd)12240247a0cfSIlya Dryomov static bool osd_homeless(struct ceph_osd *osd)
12250247a0cfSIlya Dryomov {
12260247a0cfSIlya Dryomov 	return osd->o_osd == CEPH_HOMELESS_OSD;
12270247a0cfSIlya Dryomov }
12280247a0cfSIlya Dryomov 
osd_registered(struct ceph_osd * osd)12295aea3dcdSIlya Dryomov static bool osd_registered(struct ceph_osd *osd)
12303d14c5d2SYehuda Sadeh {
12315aea3dcdSIlya Dryomov 	verify_osdc_locked(osd->o_osdc);
12323d14c5d2SYehuda Sadeh 
12335aea3dcdSIlya Dryomov 	return !RB_EMPTY_NODE(&osd->o_node);
12343d14c5d2SYehuda Sadeh }
12353d14c5d2SYehuda Sadeh 
12363d14c5d2SYehuda Sadeh /*
12370247a0cfSIlya Dryomov  * Assumes @osd is zero-initialized.
12380247a0cfSIlya Dryomov  */
osd_init(struct ceph_osd * osd)12390247a0cfSIlya Dryomov static void osd_init(struct ceph_osd *osd)
12400247a0cfSIlya Dryomov {
124102113a0fSElena Reshetova 	refcount_set(&osd->o_ref, 1);
12420247a0cfSIlya Dryomov 	RB_CLEAR_NODE(&osd->o_node);
124308b8a044SJeff Layton 	spin_lock_init(&osd->o_requests_lock);
12445aea3dcdSIlya Dryomov 	osd->o_requests = RB_ROOT;
1245922dab61SIlya Dryomov 	osd->o_linger_requests = RB_ROOT;
1246a02a946dSIlya Dryomov 	osd->o_backoff_mappings = RB_ROOT;
1247a02a946dSIlya Dryomov 	osd->o_backoffs_by_id = RB_ROOT;
12480247a0cfSIlya Dryomov 	INIT_LIST_HEAD(&osd->o_osd_lru);
12490247a0cfSIlya Dryomov 	INIT_LIST_HEAD(&osd->o_keepalive_item);
12500247a0cfSIlya Dryomov 	osd->o_incarnation = 1;
12515aea3dcdSIlya Dryomov 	mutex_init(&osd->lock);
12520247a0cfSIlya Dryomov }
12530247a0cfSIlya Dryomov 
ceph_init_sparse_read(struct ceph_sparse_read * sr)1254f628d799SJeff Layton static void ceph_init_sparse_read(struct ceph_sparse_read *sr)
1255f628d799SJeff Layton {
1256f628d799SJeff Layton 	kfree(sr->sr_extent);
1257f628d799SJeff Layton 	memset(sr, '\0', sizeof(*sr));
1258f628d799SJeff Layton 	sr->sr_state = CEPH_SPARSE_READ_HDR;
1259f628d799SJeff Layton }
1260f628d799SJeff Layton 
osd_cleanup(struct ceph_osd * osd)12610247a0cfSIlya Dryomov static void osd_cleanup(struct ceph_osd *osd)
12620247a0cfSIlya Dryomov {
12630247a0cfSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&osd->o_node));
12645aea3dcdSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
1265922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
1266a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoff_mappings));
1267a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoffs_by_id));
12680247a0cfSIlya Dryomov 	WARN_ON(!list_empty(&osd->o_osd_lru));
12690247a0cfSIlya Dryomov 	WARN_ON(!list_empty(&osd->o_keepalive_item));
12700247a0cfSIlya Dryomov 
1271f628d799SJeff Layton 	ceph_init_sparse_read(&osd->o_sparse_read);
1272f628d799SJeff Layton 
12730247a0cfSIlya Dryomov 	if (osd->o_auth.authorizer) {
12740247a0cfSIlya Dryomov 		WARN_ON(osd_homeless(osd));
12750247a0cfSIlya Dryomov 		ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
12760247a0cfSIlya Dryomov 	}
12770247a0cfSIlya Dryomov }
12780247a0cfSIlya Dryomov 
12790247a0cfSIlya Dryomov /*
12803d14c5d2SYehuda Sadeh  * Track open sessions with osds.
12813d14c5d2SYehuda Sadeh  */
create_osd(struct ceph_osd_client * osdc,int onum)1282e10006f8SAlex Elder static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
12833d14c5d2SYehuda Sadeh {
12843d14c5d2SYehuda Sadeh 	struct ceph_osd *osd;
12853d14c5d2SYehuda Sadeh 
12860247a0cfSIlya Dryomov 	WARN_ON(onum == CEPH_HOMELESS_OSD);
12870247a0cfSIlya Dryomov 
12887a28f59bSIlya Dryomov 	osd = kzalloc(sizeof(*osd), GFP_NOIO | __GFP_NOFAIL);
12890247a0cfSIlya Dryomov 	osd_init(osd);
12903d14c5d2SYehuda Sadeh 	osd->o_osdc = osdc;
1291e10006f8SAlex Elder 	osd->o_osd = onum;
1292f628d799SJeff Layton 	osd->o_sparse_op_idx = -1;
1293f628d799SJeff Layton 
1294f628d799SJeff Layton 	ceph_init_sparse_read(&osd->o_sparse_read);
12953d14c5d2SYehuda Sadeh 
1296b7a9e5ddSSage Weil 	ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
12973d14c5d2SYehuda Sadeh 
12983d14c5d2SYehuda Sadeh 	return osd;
12993d14c5d2SYehuda Sadeh }
13003d14c5d2SYehuda Sadeh 
get_osd(struct ceph_osd * osd)13013d14c5d2SYehuda Sadeh static struct ceph_osd *get_osd(struct ceph_osd *osd)
13023d14c5d2SYehuda Sadeh {
130302113a0fSElena Reshetova 	if (refcount_inc_not_zero(&osd->o_ref)) {
130402113a0fSElena Reshetova 		dout("get_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref)-1,
130502113a0fSElena Reshetova 		     refcount_read(&osd->o_ref));
13063d14c5d2SYehuda Sadeh 		return osd;
13073d14c5d2SYehuda Sadeh 	} else {
13083d14c5d2SYehuda Sadeh 		dout("get_osd %p FAIL\n", osd);
13093d14c5d2SYehuda Sadeh 		return NULL;
13103d14c5d2SYehuda Sadeh 	}
13113d14c5d2SYehuda Sadeh }
13123d14c5d2SYehuda Sadeh 
put_osd(struct ceph_osd * osd)13133d14c5d2SYehuda Sadeh static void put_osd(struct ceph_osd *osd)
13143d14c5d2SYehuda Sadeh {
131502113a0fSElena Reshetova 	dout("put_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref),
131602113a0fSElena Reshetova 	     refcount_read(&osd->o_ref) - 1);
131702113a0fSElena Reshetova 	if (refcount_dec_and_test(&osd->o_ref)) {
13180247a0cfSIlya Dryomov 		osd_cleanup(osd);
13193d14c5d2SYehuda Sadeh 		kfree(osd);
13203d14c5d2SYehuda Sadeh 	}
13213d14c5d2SYehuda Sadeh }
13223d14c5d2SYehuda Sadeh 
DEFINE_RB_FUNCS(osd,struct ceph_osd,o_osd,o_node)1323fcd00b68SIlya Dryomov DEFINE_RB_FUNCS(osd, struct ceph_osd, o_osd, o_node)
1324fcd00b68SIlya Dryomov 
13259dd2845cSIlya Dryomov static void __move_osd_to_lru(struct ceph_osd *osd)
13263d14c5d2SYehuda Sadeh {
13279dd2845cSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
13289dd2845cSIlya Dryomov 
13299dd2845cSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
13303d14c5d2SYehuda Sadeh 	BUG_ON(!list_empty(&osd->o_osd_lru));
1331bbf37ec3SIlya Dryomov 
13329dd2845cSIlya Dryomov 	spin_lock(&osdc->osd_lru_lock);
13333d14c5d2SYehuda Sadeh 	list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
13349dd2845cSIlya Dryomov 	spin_unlock(&osdc->osd_lru_lock);
13359dd2845cSIlya Dryomov 
1336a319bf56SIlya Dryomov 	osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
13373d14c5d2SYehuda Sadeh }
13383d14c5d2SYehuda Sadeh 
maybe_move_osd_to_lru(struct ceph_osd * osd)13399dd2845cSIlya Dryomov static void maybe_move_osd_to_lru(struct ceph_osd *osd)
1340bbf37ec3SIlya Dryomov {
13415aea3dcdSIlya Dryomov 	if (RB_EMPTY_ROOT(&osd->o_requests) &&
1342922dab61SIlya Dryomov 	    RB_EMPTY_ROOT(&osd->o_linger_requests))
13439dd2845cSIlya Dryomov 		__move_osd_to_lru(osd);
1344bbf37ec3SIlya Dryomov }
1345bbf37ec3SIlya Dryomov 
__remove_osd_from_lru(struct ceph_osd * osd)13463d14c5d2SYehuda Sadeh static void __remove_osd_from_lru(struct ceph_osd *osd)
13473d14c5d2SYehuda Sadeh {
13489dd2845cSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
13499dd2845cSIlya Dryomov 
13509dd2845cSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
13519dd2845cSIlya Dryomov 
13529dd2845cSIlya Dryomov 	spin_lock(&osdc->osd_lru_lock);
13533d14c5d2SYehuda Sadeh 	if (!list_empty(&osd->o_osd_lru))
13543d14c5d2SYehuda Sadeh 		list_del_init(&osd->o_osd_lru);
13559dd2845cSIlya Dryomov 	spin_unlock(&osdc->osd_lru_lock);
13563d14c5d2SYehuda Sadeh }
13573d14c5d2SYehuda Sadeh 
13583d14c5d2SYehuda Sadeh /*
13595aea3dcdSIlya Dryomov  * Close the connection and assign any leftover requests to the
13605aea3dcdSIlya Dryomov  * homeless session.
13615aea3dcdSIlya Dryomov  */
close_osd(struct ceph_osd * osd)13625aea3dcdSIlya Dryomov static void close_osd(struct ceph_osd *osd)
13635aea3dcdSIlya Dryomov {
13645aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
13655aea3dcdSIlya Dryomov 	struct rb_node *n;
13665aea3dcdSIlya Dryomov 
13675aea3dcdSIlya Dryomov 	verify_osdc_wrlocked(osdc);
13685aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
13695aea3dcdSIlya Dryomov 
13705aea3dcdSIlya Dryomov 	ceph_con_close(&osd->o_con);
13715aea3dcdSIlya Dryomov 
13725aea3dcdSIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; ) {
13735aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
13745aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
13755aea3dcdSIlya Dryomov 
13765aea3dcdSIlya Dryomov 		n = rb_next(n); /* unlink_request() */
13775aea3dcdSIlya Dryomov 
13785aea3dcdSIlya Dryomov 		dout(" reassigning req %p tid %llu\n", req, req->r_tid);
13795aea3dcdSIlya Dryomov 		unlink_request(osd, req);
13805aea3dcdSIlya Dryomov 		link_request(&osdc->homeless_osd, req);
13815aea3dcdSIlya Dryomov 	}
1382922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_linger_requests); n; ) {
1383922dab61SIlya Dryomov 		struct ceph_osd_linger_request *lreq =
1384922dab61SIlya Dryomov 		    rb_entry(n, struct ceph_osd_linger_request, node);
1385922dab61SIlya Dryomov 
1386922dab61SIlya Dryomov 		n = rb_next(n); /* unlink_linger() */
1387922dab61SIlya Dryomov 
1388922dab61SIlya Dryomov 		dout(" reassigning lreq %p linger_id %llu\n", lreq,
1389922dab61SIlya Dryomov 		     lreq->linger_id);
1390922dab61SIlya Dryomov 		unlink_linger(osd, lreq);
1391922dab61SIlya Dryomov 		link_linger(&osdc->homeless_osd, lreq);
1392922dab61SIlya Dryomov 	}
1393a02a946dSIlya Dryomov 	clear_backoffs(osd);
13945aea3dcdSIlya Dryomov 
13955aea3dcdSIlya Dryomov 	__remove_osd_from_lru(osd);
13965aea3dcdSIlya Dryomov 	erase_osd(&osdc->osds, osd);
13975aea3dcdSIlya Dryomov 	put_osd(osd);
13985aea3dcdSIlya Dryomov }
13995aea3dcdSIlya Dryomov 
14005aea3dcdSIlya Dryomov /*
14013d14c5d2SYehuda Sadeh  * reset osd connect
14023d14c5d2SYehuda Sadeh  */
reopen_osd(struct ceph_osd * osd)14035aea3dcdSIlya Dryomov static int reopen_osd(struct ceph_osd *osd)
14043d14c5d2SYehuda Sadeh {
1405c3acb181SAlex Elder 	struct ceph_entity_addr *peer_addr;
14063d14c5d2SYehuda Sadeh 
14075aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
14085aea3dcdSIlya Dryomov 
14095aea3dcdSIlya Dryomov 	if (RB_EMPTY_ROOT(&osd->o_requests) &&
1410922dab61SIlya Dryomov 	    RB_EMPTY_ROOT(&osd->o_linger_requests)) {
14115aea3dcdSIlya Dryomov 		close_osd(osd);
1412c3acb181SAlex Elder 		return -ENODEV;
1413c3acb181SAlex Elder 	}
1414c3acb181SAlex Elder 
14155aea3dcdSIlya Dryomov 	peer_addr = &osd->o_osdc->osdmap->osd_addr[osd->o_osd];
1416c3acb181SAlex Elder 	if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
14173d14c5d2SYehuda Sadeh 			!ceph_con_opened(&osd->o_con)) {
14185aea3dcdSIlya Dryomov 		struct rb_node *n;
1419c3acb181SAlex Elder 
14203d14c5d2SYehuda Sadeh 		dout("osd addr hasn't changed and connection never opened, "
14210b4af2e8SIlya Dryomov 		     "letting msgr retry\n");
14223d14c5d2SYehuda Sadeh 		/* touch each r_stamp for handle_timeout()'s benfit */
14235aea3dcdSIlya Dryomov 		for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
14245aea3dcdSIlya Dryomov 			struct ceph_osd_request *req =
14255aea3dcdSIlya Dryomov 			    rb_entry(n, struct ceph_osd_request, r_node);
14263d14c5d2SYehuda Sadeh 			req->r_stamp = jiffies;
14275aea3dcdSIlya Dryomov 		}
1428c3acb181SAlex Elder 
1429c3acb181SAlex Elder 		return -EAGAIN;
14303d14c5d2SYehuda Sadeh 	}
1431c3acb181SAlex Elder 
1432c3acb181SAlex Elder 	ceph_con_close(&osd->o_con);
1433c3acb181SAlex Elder 	ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1434c3acb181SAlex Elder 	osd->o_incarnation++;
1435c3acb181SAlex Elder 
1436c3acb181SAlex Elder 	return 0;
14373d14c5d2SYehuda Sadeh }
14383d14c5d2SYehuda Sadeh 
lookup_create_osd(struct ceph_osd_client * osdc,int o,bool wrlocked)14395aea3dcdSIlya Dryomov static struct ceph_osd *lookup_create_osd(struct ceph_osd_client *osdc, int o,
14405aea3dcdSIlya Dryomov 					  bool wrlocked)
14413d14c5d2SYehuda Sadeh {
14425aea3dcdSIlya Dryomov 	struct ceph_osd *osd;
14435aea3dcdSIlya Dryomov 
14445aea3dcdSIlya Dryomov 	if (wrlocked)
14455aea3dcdSIlya Dryomov 		verify_osdc_wrlocked(osdc);
14465aea3dcdSIlya Dryomov 	else
14475aea3dcdSIlya Dryomov 		verify_osdc_locked(osdc);
14485aea3dcdSIlya Dryomov 
14495aea3dcdSIlya Dryomov 	if (o != CEPH_HOMELESS_OSD)
14505aea3dcdSIlya Dryomov 		osd = lookup_osd(&osdc->osds, o);
14515aea3dcdSIlya Dryomov 	else
14525aea3dcdSIlya Dryomov 		osd = &osdc->homeless_osd;
14535aea3dcdSIlya Dryomov 	if (!osd) {
14545aea3dcdSIlya Dryomov 		if (!wrlocked)
14555aea3dcdSIlya Dryomov 			return ERR_PTR(-EAGAIN);
14565aea3dcdSIlya Dryomov 
14575aea3dcdSIlya Dryomov 		osd = create_osd(osdc, o);
14585aea3dcdSIlya Dryomov 		insert_osd(&osdc->osds, osd);
14595aea3dcdSIlya Dryomov 		ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
14605aea3dcdSIlya Dryomov 			      &osdc->osdmap->osd_addr[osd->o_osd]);
14615aea3dcdSIlya Dryomov 	}
14625aea3dcdSIlya Dryomov 
14635aea3dcdSIlya Dryomov 	dout("%s osdc %p osd%d -> osd %p\n", __func__, osdc, o, osd);
14645aea3dcdSIlya Dryomov 	return osd;
1465a40c4f10SYehuda Sadeh }
1466a40c4f10SYehuda Sadeh 
14673d14c5d2SYehuda Sadeh /*
14685aea3dcdSIlya Dryomov  * Create request <-> OSD session relation.
14695aea3dcdSIlya Dryomov  *
14705aea3dcdSIlya Dryomov  * @req has to be assigned a tid, @osd may be homeless.
14713d14c5d2SYehuda Sadeh  */
link_request(struct ceph_osd * osd,struct ceph_osd_request * req)14725aea3dcdSIlya Dryomov static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req)
14733d14c5d2SYehuda Sadeh {
14745aea3dcdSIlya Dryomov 	verify_osd_locked(osd);
14755aea3dcdSIlya Dryomov 	WARN_ON(!req->r_tid || req->r_osd);
14765aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
147735f9f8a0SSage Weil 	     req, req->r_tid);
14785aea3dcdSIlya Dryomov 
14795aea3dcdSIlya Dryomov 	if (!osd_homeless(osd))
14805aea3dcdSIlya Dryomov 		__remove_osd_from_lru(osd);
14815aea3dcdSIlya Dryomov 	else
14825aea3dcdSIlya Dryomov 		atomic_inc(&osd->o_osdc->num_homeless);
14835aea3dcdSIlya Dryomov 
14845aea3dcdSIlya Dryomov 	get_osd(osd);
148508b8a044SJeff Layton 	spin_lock(&osd->o_requests_lock);
14865aea3dcdSIlya Dryomov 	insert_request(&osd->o_requests, req);
148708b8a044SJeff Layton 	spin_unlock(&osd->o_requests_lock);
14885aea3dcdSIlya Dryomov 	req->r_osd = osd;
148935f9f8a0SSage Weil }
149035f9f8a0SSage Weil 
unlink_request(struct ceph_osd * osd,struct ceph_osd_request * req)14915aea3dcdSIlya Dryomov static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req)
14923d14c5d2SYehuda Sadeh {
14935aea3dcdSIlya Dryomov 	verify_osd_locked(osd);
14945aea3dcdSIlya Dryomov 	WARN_ON(req->r_osd != osd);
14955aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
14965aea3dcdSIlya Dryomov 	     req, req->r_tid);
14975aea3dcdSIlya Dryomov 
14985aea3dcdSIlya Dryomov 	req->r_osd = NULL;
149908b8a044SJeff Layton 	spin_lock(&osd->o_requests_lock);
15005aea3dcdSIlya Dryomov 	erase_request(&osd->o_requests, req);
150108b8a044SJeff Layton 	spin_unlock(&osd->o_requests_lock);
15025aea3dcdSIlya Dryomov 	put_osd(osd);
15035aea3dcdSIlya Dryomov 
15045aea3dcdSIlya Dryomov 	if (!osd_homeless(osd))
15055aea3dcdSIlya Dryomov 		maybe_move_osd_to_lru(osd);
15065aea3dcdSIlya Dryomov 	else
15075aea3dcdSIlya Dryomov 		atomic_dec(&osd->o_osdc->num_homeless);
15083d14c5d2SYehuda Sadeh }
15093d14c5d2SYehuda Sadeh 
__pool_full(struct ceph_pg_pool_info * pi)151063244fa1SIlya Dryomov static bool __pool_full(struct ceph_pg_pool_info *pi)
151163244fa1SIlya Dryomov {
151263244fa1SIlya Dryomov 	return pi->flags & CEPH_POOL_FLAG_FULL;
151363244fa1SIlya Dryomov }
151463244fa1SIlya Dryomov 
have_pool_full(struct ceph_osd_client * osdc)151542c1b124SIlya Dryomov static bool have_pool_full(struct ceph_osd_client *osdc)
151642c1b124SIlya Dryomov {
151742c1b124SIlya Dryomov 	struct rb_node *n;
151842c1b124SIlya Dryomov 
151942c1b124SIlya Dryomov 	for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
152042c1b124SIlya Dryomov 		struct ceph_pg_pool_info *pi =
152142c1b124SIlya Dryomov 		    rb_entry(n, struct ceph_pg_pool_info, node);
152242c1b124SIlya Dryomov 
152342c1b124SIlya Dryomov 		if (__pool_full(pi))
152442c1b124SIlya Dryomov 			return true;
152542c1b124SIlya Dryomov 	}
152642c1b124SIlya Dryomov 
152742c1b124SIlya Dryomov 	return false;
152842c1b124SIlya Dryomov }
152942c1b124SIlya Dryomov 
pool_full(struct ceph_osd_client * osdc,s64 pool_id)15305aea3dcdSIlya Dryomov static bool pool_full(struct ceph_osd_client *osdc, s64 pool_id)
15315aea3dcdSIlya Dryomov {
15325aea3dcdSIlya Dryomov 	struct ceph_pg_pool_info *pi;
15335aea3dcdSIlya Dryomov 
15345aea3dcdSIlya Dryomov 	pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
15355aea3dcdSIlya Dryomov 	if (!pi)
15365aea3dcdSIlya Dryomov 		return false;
15375aea3dcdSIlya Dryomov 
15385aea3dcdSIlya Dryomov 	return __pool_full(pi);
15395aea3dcdSIlya Dryomov }
15405aea3dcdSIlya Dryomov 
15413d14c5d2SYehuda Sadeh /*
1542d29adb34SJosh Durgin  * Returns whether a request should be blocked from being sent
1543d29adb34SJosh Durgin  * based on the current osdmap and osd_client settings.
1544d29adb34SJosh Durgin  */
target_should_be_paused(struct ceph_osd_client * osdc,const struct ceph_osd_request_target * t,struct ceph_pg_pool_info * pi)154563244fa1SIlya Dryomov static bool target_should_be_paused(struct ceph_osd_client *osdc,
154663244fa1SIlya Dryomov 				    const struct ceph_osd_request_target *t,
154763244fa1SIlya Dryomov 				    struct ceph_pg_pool_info *pi)
154863244fa1SIlya Dryomov {
1549b7ec35b3SIlya Dryomov 	bool pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
1550b7ec35b3SIlya Dryomov 	bool pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
1551b7ec35b3SIlya Dryomov 		       ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
155263244fa1SIlya Dryomov 		       __pool_full(pi);
155363244fa1SIlya Dryomov 
15546d637a54SIlya Dryomov 	WARN_ON(pi->id != t->target_oloc.pool);
155558eb7932SJeff Layton 	return ((t->flags & CEPH_OSD_FLAG_READ) && pauserd) ||
155658eb7932SJeff Layton 	       ((t->flags & CEPH_OSD_FLAG_WRITE) && pausewr) ||
155758eb7932SJeff Layton 	       (osdc->osdmap->epoch < osdc->epoch_barrier);
155863244fa1SIlya Dryomov }
155963244fa1SIlya Dryomov 
pick_random_replica(const struct ceph_osds * acting)1560117d96a0SIlya Dryomov static int pick_random_replica(const struct ceph_osds *acting)
1561117d96a0SIlya Dryomov {
15628032bf12SJason A. Donenfeld 	int i = get_random_u32_below(acting->size);
1563117d96a0SIlya Dryomov 
1564117d96a0SIlya Dryomov 	dout("%s picked osd%d, primary osd%d\n", __func__,
1565117d96a0SIlya Dryomov 	     acting->osds[i], acting->primary);
1566117d96a0SIlya Dryomov 	return i;
1567117d96a0SIlya Dryomov }
1568117d96a0SIlya Dryomov 
1569117d96a0SIlya Dryomov /*
1570117d96a0SIlya Dryomov  * Picks the closest replica based on client's location given by
1571117d96a0SIlya Dryomov  * crush_location option.  Prefers the primary if the locality is
1572117d96a0SIlya Dryomov  * the same.
1573117d96a0SIlya Dryomov  */
pick_closest_replica(struct ceph_osd_client * osdc,const struct ceph_osds * acting)1574117d96a0SIlya Dryomov static int pick_closest_replica(struct ceph_osd_client *osdc,
1575117d96a0SIlya Dryomov 				const struct ceph_osds *acting)
1576117d96a0SIlya Dryomov {
1577117d96a0SIlya Dryomov 	struct ceph_options *opt = osdc->client->options;
1578117d96a0SIlya Dryomov 	int best_i, best_locality;
1579117d96a0SIlya Dryomov 	int i = 0, locality;
1580117d96a0SIlya Dryomov 
1581117d96a0SIlya Dryomov 	do {
1582117d96a0SIlya Dryomov 		locality = ceph_get_crush_locality(osdc->osdmap,
1583117d96a0SIlya Dryomov 						   acting->osds[i],
1584117d96a0SIlya Dryomov 						   &opt->crush_locs);
1585117d96a0SIlya Dryomov 		if (i == 0 ||
1586117d96a0SIlya Dryomov 		    (locality >= 0 && best_locality < 0) ||
1587117d96a0SIlya Dryomov 		    (locality >= 0 && best_locality >= 0 &&
1588117d96a0SIlya Dryomov 		     locality < best_locality)) {
1589117d96a0SIlya Dryomov 			best_i = i;
1590117d96a0SIlya Dryomov 			best_locality = locality;
1591117d96a0SIlya Dryomov 		}
1592117d96a0SIlya Dryomov 	} while (++i < acting->size);
1593117d96a0SIlya Dryomov 
1594117d96a0SIlya Dryomov 	dout("%s picked osd%d with locality %d, primary osd%d\n", __func__,
1595117d96a0SIlya Dryomov 	     acting->osds[best_i], best_locality, acting->primary);
1596117d96a0SIlya Dryomov 	return best_i;
1597117d96a0SIlya Dryomov }
1598117d96a0SIlya Dryomov 
159963244fa1SIlya Dryomov enum calc_target_result {
160063244fa1SIlya Dryomov 	CALC_TARGET_NO_ACTION = 0,
160163244fa1SIlya Dryomov 	CALC_TARGET_NEED_RESEND,
160263244fa1SIlya Dryomov 	CALC_TARGET_POOL_DNE,
160363244fa1SIlya Dryomov };
160463244fa1SIlya Dryomov 
calc_target(struct ceph_osd_client * osdc,struct ceph_osd_request_target * t,bool any_change)160563244fa1SIlya Dryomov static enum calc_target_result calc_target(struct ceph_osd_client *osdc,
160663244fa1SIlya Dryomov 					   struct ceph_osd_request_target *t,
160763244fa1SIlya Dryomov 					   bool any_change)
160863244fa1SIlya Dryomov {
160963244fa1SIlya Dryomov 	struct ceph_pg_pool_info *pi;
161063244fa1SIlya Dryomov 	struct ceph_pg pgid, last_pgid;
161163244fa1SIlya Dryomov 	struct ceph_osds up, acting;
1612117d96a0SIlya Dryomov 	bool is_read = t->flags & CEPH_OSD_FLAG_READ;
1613117d96a0SIlya Dryomov 	bool is_write = t->flags & CEPH_OSD_FLAG_WRITE;
161463244fa1SIlya Dryomov 	bool force_resend = false;
161584ed45dfSIlya Dryomov 	bool unpaused = false;
1616a5613724SIlya Dryomov 	bool legacy_change = false;
16177de030d6SIlya Dryomov 	bool split = false;
1618b7ec35b3SIlya Dryomov 	bool sort_bitwise = ceph_osdmap_flag(osdc, CEPH_OSDMAP_SORTBITWISE);
1619ae78dd81SIlya Dryomov 	bool recovery_deletes = ceph_osdmap_flag(osdc,
1620ae78dd81SIlya Dryomov 						 CEPH_OSDMAP_RECOVERY_DELETES);
162163244fa1SIlya Dryomov 	enum calc_target_result ct_res;
162263244fa1SIlya Dryomov 
162304c7d789SIlya Dryomov 	t->epoch = osdc->osdmap->epoch;
162463244fa1SIlya Dryomov 	pi = ceph_pg_pool_by_id(osdc->osdmap, t->base_oloc.pool);
162563244fa1SIlya Dryomov 	if (!pi) {
162663244fa1SIlya Dryomov 		t->osd = CEPH_HOMELESS_OSD;
162763244fa1SIlya Dryomov 		ct_res = CALC_TARGET_POOL_DNE;
162863244fa1SIlya Dryomov 		goto out;
162963244fa1SIlya Dryomov 	}
163063244fa1SIlya Dryomov 
163163244fa1SIlya Dryomov 	if (osdc->osdmap->epoch == pi->last_force_request_resend) {
1632dc93e0e2SIlya Dryomov 		if (t->last_force_resend < pi->last_force_request_resend) {
1633dc93e0e2SIlya Dryomov 			t->last_force_resend = pi->last_force_request_resend;
163463244fa1SIlya Dryomov 			force_resend = true;
1635dc93e0e2SIlya Dryomov 		} else if (t->last_force_resend == 0) {
163663244fa1SIlya Dryomov 			force_resend = true;
163763244fa1SIlya Dryomov 		}
163863244fa1SIlya Dryomov 	}
163963244fa1SIlya Dryomov 
1640db098ec4SIlya Dryomov 	/* apply tiering */
1641db098ec4SIlya Dryomov 	ceph_oid_copy(&t->target_oid, &t->base_oid);
1642db098ec4SIlya Dryomov 	ceph_oloc_copy(&t->target_oloc, &t->base_oloc);
1643db098ec4SIlya Dryomov 	if ((t->flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
1644117d96a0SIlya Dryomov 		if (is_read && pi->read_tier >= 0)
164563244fa1SIlya Dryomov 			t->target_oloc.pool = pi->read_tier;
1646117d96a0SIlya Dryomov 		if (is_write && pi->write_tier >= 0)
164763244fa1SIlya Dryomov 			t->target_oloc.pool = pi->write_tier;
16486d637a54SIlya Dryomov 
16496d637a54SIlya Dryomov 		pi = ceph_pg_pool_by_id(osdc->osdmap, t->target_oloc.pool);
16506d637a54SIlya Dryomov 		if (!pi) {
16516d637a54SIlya Dryomov 			t->osd = CEPH_HOMELESS_OSD;
16526d637a54SIlya Dryomov 			ct_res = CALC_TARGET_POOL_DNE;
16536d637a54SIlya Dryomov 			goto out;
16546d637a54SIlya Dryomov 		}
165563244fa1SIlya Dryomov 	}
165663244fa1SIlya Dryomov 
1657a86f009fSIlya Dryomov 	__ceph_object_locator_to_pg(pi, &t->target_oid, &t->target_oloc, &pgid);
165863244fa1SIlya Dryomov 	last_pgid.pool = pgid.pool;
165963244fa1SIlya Dryomov 	last_pgid.seed = ceph_stable_mod(pgid.seed, t->pg_num, t->pg_num_mask);
166063244fa1SIlya Dryomov 
1661df28152dSIlya Dryomov 	ceph_pg_to_up_acting_osds(osdc->osdmap, pi, &pgid, &up, &acting);
166263244fa1SIlya Dryomov 	if (any_change &&
166363244fa1SIlya Dryomov 	    ceph_is_new_interval(&t->acting,
166463244fa1SIlya Dryomov 				 &acting,
166563244fa1SIlya Dryomov 				 &t->up,
166663244fa1SIlya Dryomov 				 &up,
166763244fa1SIlya Dryomov 				 t->size,
166863244fa1SIlya Dryomov 				 pi->size,
166963244fa1SIlya Dryomov 				 t->min_size,
167063244fa1SIlya Dryomov 				 pi->min_size,
167163244fa1SIlya Dryomov 				 t->pg_num,
167263244fa1SIlya Dryomov 				 pi->pg_num,
167363244fa1SIlya Dryomov 				 t->sort_bitwise,
167463244fa1SIlya Dryomov 				 sort_bitwise,
1675ae78dd81SIlya Dryomov 				 t->recovery_deletes,
1676ae78dd81SIlya Dryomov 				 recovery_deletes,
167763244fa1SIlya Dryomov 				 &last_pgid))
167863244fa1SIlya Dryomov 		force_resend = true;
167963244fa1SIlya Dryomov 
168063244fa1SIlya Dryomov 	if (t->paused && !target_should_be_paused(osdc, t, pi)) {
168163244fa1SIlya Dryomov 		t->paused = false;
168284ed45dfSIlya Dryomov 		unpaused = true;
168363244fa1SIlya Dryomov 	}
168484ed45dfSIlya Dryomov 	legacy_change = ceph_pg_compare(&t->pgid, &pgid) ||
1685117d96a0SIlya Dryomov 			ceph_osds_changed(&t->acting, &acting,
1686117d96a0SIlya Dryomov 					  t->used_replica || any_change);
16877de030d6SIlya Dryomov 	if (t->pg_num)
16887de030d6SIlya Dryomov 		split = ceph_pg_is_split(&last_pgid, t->pg_num, pi->pg_num);
168963244fa1SIlya Dryomov 
16907de030d6SIlya Dryomov 	if (legacy_change || force_resend || split) {
169163244fa1SIlya Dryomov 		t->pgid = pgid; /* struct */
1692df28152dSIlya Dryomov 		ceph_pg_to_primary_shard(osdc->osdmap, pi, &pgid, &t->spgid);
169363244fa1SIlya Dryomov 		ceph_osds_copy(&t->acting, &acting);
169463244fa1SIlya Dryomov 		ceph_osds_copy(&t->up, &up);
169563244fa1SIlya Dryomov 		t->size = pi->size;
169663244fa1SIlya Dryomov 		t->min_size = pi->min_size;
169763244fa1SIlya Dryomov 		t->pg_num = pi->pg_num;
169863244fa1SIlya Dryomov 		t->pg_num_mask = pi->pg_num_mask;
169963244fa1SIlya Dryomov 		t->sort_bitwise = sort_bitwise;
1700ae78dd81SIlya Dryomov 		t->recovery_deletes = recovery_deletes;
170163244fa1SIlya Dryomov 
1702117d96a0SIlya Dryomov 		if ((t->flags & (CEPH_OSD_FLAG_BALANCE_READS |
1703117d96a0SIlya Dryomov 				 CEPH_OSD_FLAG_LOCALIZE_READS)) &&
1704117d96a0SIlya Dryomov 		    !is_write && pi->type == CEPH_POOL_TYPE_REP &&
1705117d96a0SIlya Dryomov 		    acting.size > 1) {
1706117d96a0SIlya Dryomov 			int pos;
1707117d96a0SIlya Dryomov 
1708117d96a0SIlya Dryomov 			WARN_ON(!is_read || acting.osds[0] != acting.primary);
1709117d96a0SIlya Dryomov 			if (t->flags & CEPH_OSD_FLAG_BALANCE_READS) {
1710117d96a0SIlya Dryomov 				pos = pick_random_replica(&acting);
1711117d96a0SIlya Dryomov 			} else {
1712117d96a0SIlya Dryomov 				pos = pick_closest_replica(osdc, &acting);
1713117d96a0SIlya Dryomov 			}
1714117d96a0SIlya Dryomov 			t->osd = acting.osds[pos];
1715117d96a0SIlya Dryomov 			t->used_replica = pos > 0;
1716117d96a0SIlya Dryomov 		} else {
171763244fa1SIlya Dryomov 			t->osd = acting.primary;
1718117d96a0SIlya Dryomov 			t->used_replica = false;
1719117d96a0SIlya Dryomov 		}
172063244fa1SIlya Dryomov 	}
172163244fa1SIlya Dryomov 
1722a5613724SIlya Dryomov 	if (unpaused || legacy_change || force_resend || split)
172384ed45dfSIlya Dryomov 		ct_res = CALC_TARGET_NEED_RESEND;
172484ed45dfSIlya Dryomov 	else
172584ed45dfSIlya Dryomov 		ct_res = CALC_TARGET_NO_ACTION;
172684ed45dfSIlya Dryomov 
172763244fa1SIlya Dryomov out:
1728a5613724SIlya Dryomov 	dout("%s t %p -> %d%d%d%d ct_res %d osd%d\n", __func__, t, unpaused,
1729a5613724SIlya Dryomov 	     legacy_change, force_resend, split, ct_res, t->osd);
173063244fa1SIlya Dryomov 	return ct_res;
173163244fa1SIlya Dryomov }
173263244fa1SIlya Dryomov 
alloc_spg_mapping(void)1733a02a946dSIlya Dryomov static struct ceph_spg_mapping *alloc_spg_mapping(void)
1734a02a946dSIlya Dryomov {
1735a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
1736a02a946dSIlya Dryomov 
1737a02a946dSIlya Dryomov 	spg = kmalloc(sizeof(*spg), GFP_NOIO);
1738a02a946dSIlya Dryomov 	if (!spg)
1739a02a946dSIlya Dryomov 		return NULL;
1740a02a946dSIlya Dryomov 
1741a02a946dSIlya Dryomov 	RB_CLEAR_NODE(&spg->node);
1742a02a946dSIlya Dryomov 	spg->backoffs = RB_ROOT;
1743a02a946dSIlya Dryomov 	return spg;
1744a02a946dSIlya Dryomov }
1745a02a946dSIlya Dryomov 
free_spg_mapping(struct ceph_spg_mapping * spg)1746a02a946dSIlya Dryomov static void free_spg_mapping(struct ceph_spg_mapping *spg)
1747a02a946dSIlya Dryomov {
1748a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&spg->node));
1749a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&spg->backoffs));
1750a02a946dSIlya Dryomov 
1751a02a946dSIlya Dryomov 	kfree(spg);
1752a02a946dSIlya Dryomov }
1753a02a946dSIlya Dryomov 
1754a02a946dSIlya Dryomov /*
1755a02a946dSIlya Dryomov  * rbtree of ceph_spg_mapping for handling map<spg_t, ...>, similar to
1756a02a946dSIlya Dryomov  * ceph_pg_mapping.  Used to track OSD backoffs -- a backoff [range] is
1757a02a946dSIlya Dryomov  * defined only within a specific spgid; it does not pass anything to
1758a02a946dSIlya Dryomov  * children on split, or to another primary.
1759a02a946dSIlya Dryomov  */
DEFINE_RB_FUNCS2(spg_mapping,struct ceph_spg_mapping,spgid,ceph_spg_compare,RB_BYPTR,const struct ceph_spg *,node)1760a02a946dSIlya Dryomov DEFINE_RB_FUNCS2(spg_mapping, struct ceph_spg_mapping, spgid, ceph_spg_compare,
1761a02a946dSIlya Dryomov 		 RB_BYPTR, const struct ceph_spg *, node)
1762a02a946dSIlya Dryomov 
1763a02a946dSIlya Dryomov static u64 hoid_get_bitwise_key(const struct ceph_hobject_id *hoid)
1764a02a946dSIlya Dryomov {
1765a02a946dSIlya Dryomov 	return hoid->is_max ? 0x100000000ull : hoid->hash_reverse_bits;
1766a02a946dSIlya Dryomov }
1767a02a946dSIlya Dryomov 
hoid_get_effective_key(const struct ceph_hobject_id * hoid,void ** pkey,size_t * pkey_len)1768a02a946dSIlya Dryomov static void hoid_get_effective_key(const struct ceph_hobject_id *hoid,
1769a02a946dSIlya Dryomov 				   void **pkey, size_t *pkey_len)
1770a02a946dSIlya Dryomov {
1771a02a946dSIlya Dryomov 	if (hoid->key_len) {
1772a02a946dSIlya Dryomov 		*pkey = hoid->key;
1773a02a946dSIlya Dryomov 		*pkey_len = hoid->key_len;
1774a02a946dSIlya Dryomov 	} else {
1775a02a946dSIlya Dryomov 		*pkey = hoid->oid;
1776a02a946dSIlya Dryomov 		*pkey_len = hoid->oid_len;
1777a02a946dSIlya Dryomov 	}
1778a02a946dSIlya Dryomov }
1779a02a946dSIlya Dryomov 
compare_names(const void * name1,size_t name1_len,const void * name2,size_t name2_len)1780a02a946dSIlya Dryomov static int compare_names(const void *name1, size_t name1_len,
1781a02a946dSIlya Dryomov 			 const void *name2, size_t name2_len)
1782a02a946dSIlya Dryomov {
1783a02a946dSIlya Dryomov 	int ret;
1784a02a946dSIlya Dryomov 
1785a02a946dSIlya Dryomov 	ret = memcmp(name1, name2, min(name1_len, name2_len));
1786a02a946dSIlya Dryomov 	if (!ret) {
1787a02a946dSIlya Dryomov 		if (name1_len < name2_len)
1788a02a946dSIlya Dryomov 			ret = -1;
1789a02a946dSIlya Dryomov 		else if (name1_len > name2_len)
1790a02a946dSIlya Dryomov 			ret = 1;
1791a02a946dSIlya Dryomov 	}
1792a02a946dSIlya Dryomov 	return ret;
1793a02a946dSIlya Dryomov }
1794a02a946dSIlya Dryomov 
hoid_compare(const struct ceph_hobject_id * lhs,const struct ceph_hobject_id * rhs)1795a02a946dSIlya Dryomov static int hoid_compare(const struct ceph_hobject_id *lhs,
1796a02a946dSIlya Dryomov 			const struct ceph_hobject_id *rhs)
1797a02a946dSIlya Dryomov {
1798a02a946dSIlya Dryomov 	void *effective_key1, *effective_key2;
1799a02a946dSIlya Dryomov 	size_t effective_key1_len, effective_key2_len;
1800a02a946dSIlya Dryomov 	int ret;
1801a02a946dSIlya Dryomov 
1802a02a946dSIlya Dryomov 	if (lhs->is_max < rhs->is_max)
1803a02a946dSIlya Dryomov 		return -1;
1804a02a946dSIlya Dryomov 	if (lhs->is_max > rhs->is_max)
1805a02a946dSIlya Dryomov 		return 1;
1806a02a946dSIlya Dryomov 
1807a02a946dSIlya Dryomov 	if (lhs->pool < rhs->pool)
1808a02a946dSIlya Dryomov 		return -1;
1809a02a946dSIlya Dryomov 	if (lhs->pool > rhs->pool)
1810a02a946dSIlya Dryomov 		return 1;
1811a02a946dSIlya Dryomov 
1812a02a946dSIlya Dryomov 	if (hoid_get_bitwise_key(lhs) < hoid_get_bitwise_key(rhs))
1813a02a946dSIlya Dryomov 		return -1;
1814a02a946dSIlya Dryomov 	if (hoid_get_bitwise_key(lhs) > hoid_get_bitwise_key(rhs))
1815a02a946dSIlya Dryomov 		return 1;
1816a02a946dSIlya Dryomov 
1817a02a946dSIlya Dryomov 	ret = compare_names(lhs->nspace, lhs->nspace_len,
1818a02a946dSIlya Dryomov 			    rhs->nspace, rhs->nspace_len);
1819a02a946dSIlya Dryomov 	if (ret)
1820a02a946dSIlya Dryomov 		return ret;
1821a02a946dSIlya Dryomov 
1822a02a946dSIlya Dryomov 	hoid_get_effective_key(lhs, &effective_key1, &effective_key1_len);
1823a02a946dSIlya Dryomov 	hoid_get_effective_key(rhs, &effective_key2, &effective_key2_len);
1824a02a946dSIlya Dryomov 	ret = compare_names(effective_key1, effective_key1_len,
1825a02a946dSIlya Dryomov 			    effective_key2, effective_key2_len);
1826a02a946dSIlya Dryomov 	if (ret)
1827a02a946dSIlya Dryomov 		return ret;
1828a02a946dSIlya Dryomov 
1829a02a946dSIlya Dryomov 	ret = compare_names(lhs->oid, lhs->oid_len, rhs->oid, rhs->oid_len);
1830a02a946dSIlya Dryomov 	if (ret)
1831a02a946dSIlya Dryomov 		return ret;
1832a02a946dSIlya Dryomov 
1833a02a946dSIlya Dryomov 	if (lhs->snapid < rhs->snapid)
1834a02a946dSIlya Dryomov 		return -1;
1835a02a946dSIlya Dryomov 	if (lhs->snapid > rhs->snapid)
1836a02a946dSIlya Dryomov 		return 1;
1837a02a946dSIlya Dryomov 
1838a02a946dSIlya Dryomov 	return 0;
1839a02a946dSIlya Dryomov }
1840a02a946dSIlya Dryomov 
1841a02a946dSIlya Dryomov /*
1842a02a946dSIlya Dryomov  * For decoding ->begin and ->end of MOSDBackoff only -- no MIN/MAX
1843a02a946dSIlya Dryomov  * compat stuff here.
1844a02a946dSIlya Dryomov  *
1845a02a946dSIlya Dryomov  * Assumes @hoid is zero-initialized.
1846a02a946dSIlya Dryomov  */
decode_hoid(void ** p,void * end,struct ceph_hobject_id * hoid)1847a02a946dSIlya Dryomov static int decode_hoid(void **p, void *end, struct ceph_hobject_id *hoid)
1848a02a946dSIlya Dryomov {
1849a02a946dSIlya Dryomov 	u8 struct_v;
1850a02a946dSIlya Dryomov 	u32 struct_len;
1851a02a946dSIlya Dryomov 	int ret;
1852a02a946dSIlya Dryomov 
1853a02a946dSIlya Dryomov 	ret = ceph_start_decoding(p, end, 4, "hobject_t", &struct_v,
1854a02a946dSIlya Dryomov 				  &struct_len);
1855a02a946dSIlya Dryomov 	if (ret)
1856a02a946dSIlya Dryomov 		return ret;
1857a02a946dSIlya Dryomov 
1858a02a946dSIlya Dryomov 	if (struct_v < 4) {
1859a02a946dSIlya Dryomov 		pr_err("got struct_v %d < 4 of hobject_t\n", struct_v);
1860a02a946dSIlya Dryomov 		goto e_inval;
1861a02a946dSIlya Dryomov 	}
1862a02a946dSIlya Dryomov 
1863a02a946dSIlya Dryomov 	hoid->key = ceph_extract_encoded_string(p, end, &hoid->key_len,
1864a02a946dSIlya Dryomov 						GFP_NOIO);
1865a02a946dSIlya Dryomov 	if (IS_ERR(hoid->key)) {
1866a02a946dSIlya Dryomov 		ret = PTR_ERR(hoid->key);
1867a02a946dSIlya Dryomov 		hoid->key = NULL;
1868a02a946dSIlya Dryomov 		return ret;
1869a02a946dSIlya Dryomov 	}
1870a02a946dSIlya Dryomov 
1871a02a946dSIlya Dryomov 	hoid->oid = ceph_extract_encoded_string(p, end, &hoid->oid_len,
1872a02a946dSIlya Dryomov 						GFP_NOIO);
1873a02a946dSIlya Dryomov 	if (IS_ERR(hoid->oid)) {
1874a02a946dSIlya Dryomov 		ret = PTR_ERR(hoid->oid);
1875a02a946dSIlya Dryomov 		hoid->oid = NULL;
1876a02a946dSIlya Dryomov 		return ret;
1877a02a946dSIlya Dryomov 	}
1878a02a946dSIlya Dryomov 
1879a02a946dSIlya Dryomov 	ceph_decode_64_safe(p, end, hoid->snapid, e_inval);
1880a02a946dSIlya Dryomov 	ceph_decode_32_safe(p, end, hoid->hash, e_inval);
1881a02a946dSIlya Dryomov 	ceph_decode_8_safe(p, end, hoid->is_max, e_inval);
1882a02a946dSIlya Dryomov 
1883a02a946dSIlya Dryomov 	hoid->nspace = ceph_extract_encoded_string(p, end, &hoid->nspace_len,
1884a02a946dSIlya Dryomov 						   GFP_NOIO);
1885a02a946dSIlya Dryomov 	if (IS_ERR(hoid->nspace)) {
1886a02a946dSIlya Dryomov 		ret = PTR_ERR(hoid->nspace);
1887a02a946dSIlya Dryomov 		hoid->nspace = NULL;
1888a02a946dSIlya Dryomov 		return ret;
1889a02a946dSIlya Dryomov 	}
1890a02a946dSIlya Dryomov 
1891a02a946dSIlya Dryomov 	ceph_decode_64_safe(p, end, hoid->pool, e_inval);
1892a02a946dSIlya Dryomov 
1893a02a946dSIlya Dryomov 	ceph_hoid_build_hash_cache(hoid);
1894a02a946dSIlya Dryomov 	return 0;
1895a02a946dSIlya Dryomov 
1896a02a946dSIlya Dryomov e_inval:
1897a02a946dSIlya Dryomov 	return -EINVAL;
1898a02a946dSIlya Dryomov }
1899a02a946dSIlya Dryomov 
hoid_encoding_size(const struct ceph_hobject_id * hoid)1900a02a946dSIlya Dryomov static int hoid_encoding_size(const struct ceph_hobject_id *hoid)
1901a02a946dSIlya Dryomov {
1902a02a946dSIlya Dryomov 	return 8 + 4 + 1 + 8 + /* snapid, hash, is_max, pool */
1903a02a946dSIlya Dryomov 	       4 + hoid->key_len + 4 + hoid->oid_len + 4 + hoid->nspace_len;
1904a02a946dSIlya Dryomov }
1905a02a946dSIlya Dryomov 
encode_hoid(void ** p,void * end,const struct ceph_hobject_id * hoid)1906a02a946dSIlya Dryomov static void encode_hoid(void **p, void *end, const struct ceph_hobject_id *hoid)
1907a02a946dSIlya Dryomov {
1908a02a946dSIlya Dryomov 	ceph_start_encoding(p, 4, 3, hoid_encoding_size(hoid));
1909a02a946dSIlya Dryomov 	ceph_encode_string(p, end, hoid->key, hoid->key_len);
1910a02a946dSIlya Dryomov 	ceph_encode_string(p, end, hoid->oid, hoid->oid_len);
1911a02a946dSIlya Dryomov 	ceph_encode_64(p, hoid->snapid);
1912a02a946dSIlya Dryomov 	ceph_encode_32(p, hoid->hash);
1913a02a946dSIlya Dryomov 	ceph_encode_8(p, hoid->is_max);
1914a02a946dSIlya Dryomov 	ceph_encode_string(p, end, hoid->nspace, hoid->nspace_len);
1915a02a946dSIlya Dryomov 	ceph_encode_64(p, hoid->pool);
1916a02a946dSIlya Dryomov }
1917a02a946dSIlya Dryomov 
free_hoid(struct ceph_hobject_id * hoid)1918a02a946dSIlya Dryomov static void free_hoid(struct ceph_hobject_id *hoid)
1919a02a946dSIlya Dryomov {
1920a02a946dSIlya Dryomov 	if (hoid) {
1921a02a946dSIlya Dryomov 		kfree(hoid->key);
1922a02a946dSIlya Dryomov 		kfree(hoid->oid);
1923a02a946dSIlya Dryomov 		kfree(hoid->nspace);
1924a02a946dSIlya Dryomov 		kfree(hoid);
1925a02a946dSIlya Dryomov 	}
1926a02a946dSIlya Dryomov }
1927a02a946dSIlya Dryomov 
alloc_backoff(void)1928a02a946dSIlya Dryomov static struct ceph_osd_backoff *alloc_backoff(void)
1929a02a946dSIlya Dryomov {
1930a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
1931a02a946dSIlya Dryomov 
1932a02a946dSIlya Dryomov 	backoff = kzalloc(sizeof(*backoff), GFP_NOIO);
1933a02a946dSIlya Dryomov 	if (!backoff)
1934a02a946dSIlya Dryomov 		return NULL;
1935a02a946dSIlya Dryomov 
1936a02a946dSIlya Dryomov 	RB_CLEAR_NODE(&backoff->spg_node);
1937a02a946dSIlya Dryomov 	RB_CLEAR_NODE(&backoff->id_node);
1938a02a946dSIlya Dryomov 	return backoff;
1939a02a946dSIlya Dryomov }
1940a02a946dSIlya Dryomov 
free_backoff(struct ceph_osd_backoff * backoff)1941a02a946dSIlya Dryomov static void free_backoff(struct ceph_osd_backoff *backoff)
1942a02a946dSIlya Dryomov {
1943a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&backoff->spg_node));
1944a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&backoff->id_node));
1945a02a946dSIlya Dryomov 
1946a02a946dSIlya Dryomov 	free_hoid(backoff->begin);
1947a02a946dSIlya Dryomov 	free_hoid(backoff->end);
1948a02a946dSIlya Dryomov 	kfree(backoff);
1949a02a946dSIlya Dryomov }
1950a02a946dSIlya Dryomov 
1951a02a946dSIlya Dryomov /*
1952a02a946dSIlya Dryomov  * Within a specific spgid, backoffs are managed by ->begin hoid.
1953a02a946dSIlya Dryomov  */
1954a02a946dSIlya Dryomov DEFINE_RB_INSDEL_FUNCS2(backoff, struct ceph_osd_backoff, begin, hoid_compare,
1955a02a946dSIlya Dryomov 			RB_BYVAL, spg_node);
1956a02a946dSIlya Dryomov 
lookup_containing_backoff(struct rb_root * root,const struct ceph_hobject_id * hoid)1957a02a946dSIlya Dryomov static struct ceph_osd_backoff *lookup_containing_backoff(struct rb_root *root,
1958a02a946dSIlya Dryomov 					    const struct ceph_hobject_id *hoid)
1959a02a946dSIlya Dryomov {
1960a02a946dSIlya Dryomov 	struct rb_node *n = root->rb_node;
1961a02a946dSIlya Dryomov 
1962a02a946dSIlya Dryomov 	while (n) {
1963a02a946dSIlya Dryomov 		struct ceph_osd_backoff *cur =
1964a02a946dSIlya Dryomov 		    rb_entry(n, struct ceph_osd_backoff, spg_node);
1965a02a946dSIlya Dryomov 		int cmp;
1966a02a946dSIlya Dryomov 
1967a02a946dSIlya Dryomov 		cmp = hoid_compare(hoid, cur->begin);
1968a02a946dSIlya Dryomov 		if (cmp < 0) {
1969a02a946dSIlya Dryomov 			n = n->rb_left;
1970a02a946dSIlya Dryomov 		} else if (cmp > 0) {
1971a02a946dSIlya Dryomov 			if (hoid_compare(hoid, cur->end) < 0)
1972a02a946dSIlya Dryomov 				return cur;
1973a02a946dSIlya Dryomov 
1974a02a946dSIlya Dryomov 			n = n->rb_right;
1975a02a946dSIlya Dryomov 		} else {
1976a02a946dSIlya Dryomov 			return cur;
1977a02a946dSIlya Dryomov 		}
1978a02a946dSIlya Dryomov 	}
1979a02a946dSIlya Dryomov 
1980a02a946dSIlya Dryomov 	return NULL;
1981a02a946dSIlya Dryomov }
1982a02a946dSIlya Dryomov 
1983a02a946dSIlya Dryomov /*
1984a02a946dSIlya Dryomov  * Each backoff has a unique id within its OSD session.
1985a02a946dSIlya Dryomov  */
DEFINE_RB_FUNCS(backoff_by_id,struct ceph_osd_backoff,id,id_node)1986a02a946dSIlya Dryomov DEFINE_RB_FUNCS(backoff_by_id, struct ceph_osd_backoff, id, id_node)
1987a02a946dSIlya Dryomov 
1988a02a946dSIlya Dryomov static void clear_backoffs(struct ceph_osd *osd)
1989a02a946dSIlya Dryomov {
1990a02a946dSIlya Dryomov 	while (!RB_EMPTY_ROOT(&osd->o_backoff_mappings)) {
1991a02a946dSIlya Dryomov 		struct ceph_spg_mapping *spg =
1992a02a946dSIlya Dryomov 		    rb_entry(rb_first(&osd->o_backoff_mappings),
1993a02a946dSIlya Dryomov 			     struct ceph_spg_mapping, node);
1994a02a946dSIlya Dryomov 
1995a02a946dSIlya Dryomov 		while (!RB_EMPTY_ROOT(&spg->backoffs)) {
1996a02a946dSIlya Dryomov 			struct ceph_osd_backoff *backoff =
1997a02a946dSIlya Dryomov 			    rb_entry(rb_first(&spg->backoffs),
1998a02a946dSIlya Dryomov 				     struct ceph_osd_backoff, spg_node);
1999a02a946dSIlya Dryomov 
2000a02a946dSIlya Dryomov 			erase_backoff(&spg->backoffs, backoff);
2001a02a946dSIlya Dryomov 			erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
2002a02a946dSIlya Dryomov 			free_backoff(backoff);
2003a02a946dSIlya Dryomov 		}
2004a02a946dSIlya Dryomov 		erase_spg_mapping(&osd->o_backoff_mappings, spg);
2005a02a946dSIlya Dryomov 		free_spg_mapping(spg);
2006a02a946dSIlya Dryomov 	}
2007a02a946dSIlya Dryomov }
2008a02a946dSIlya Dryomov 
2009a02a946dSIlya Dryomov /*
2010a02a946dSIlya Dryomov  * Set up a temporary, non-owning view into @t.
2011a02a946dSIlya Dryomov  */
hoid_fill_from_target(struct ceph_hobject_id * hoid,const struct ceph_osd_request_target * t)2012a02a946dSIlya Dryomov static void hoid_fill_from_target(struct ceph_hobject_id *hoid,
2013a02a946dSIlya Dryomov 				  const struct ceph_osd_request_target *t)
2014a02a946dSIlya Dryomov {
2015a02a946dSIlya Dryomov 	hoid->key = NULL;
2016a02a946dSIlya Dryomov 	hoid->key_len = 0;
2017a02a946dSIlya Dryomov 	hoid->oid = t->target_oid.name;
2018a02a946dSIlya Dryomov 	hoid->oid_len = t->target_oid.name_len;
2019a02a946dSIlya Dryomov 	hoid->snapid = CEPH_NOSNAP;
2020a02a946dSIlya Dryomov 	hoid->hash = t->pgid.seed;
2021a02a946dSIlya Dryomov 	hoid->is_max = false;
2022a02a946dSIlya Dryomov 	if (t->target_oloc.pool_ns) {
2023a02a946dSIlya Dryomov 		hoid->nspace = t->target_oloc.pool_ns->str;
2024a02a946dSIlya Dryomov 		hoid->nspace_len = t->target_oloc.pool_ns->len;
2025a02a946dSIlya Dryomov 	} else {
2026a02a946dSIlya Dryomov 		hoid->nspace = NULL;
2027a02a946dSIlya Dryomov 		hoid->nspace_len = 0;
2028a02a946dSIlya Dryomov 	}
2029a02a946dSIlya Dryomov 	hoid->pool = t->target_oloc.pool;
2030a02a946dSIlya Dryomov 	ceph_hoid_build_hash_cache(hoid);
2031a02a946dSIlya Dryomov }
2032a02a946dSIlya Dryomov 
should_plug_request(struct ceph_osd_request * req)2033a02a946dSIlya Dryomov static bool should_plug_request(struct ceph_osd_request *req)
2034a02a946dSIlya Dryomov {
2035a02a946dSIlya Dryomov 	struct ceph_osd *osd = req->r_osd;
2036a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
2037a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
2038a02a946dSIlya Dryomov 	struct ceph_hobject_id hoid;
2039a02a946dSIlya Dryomov 
2040a02a946dSIlya Dryomov 	spg = lookup_spg_mapping(&osd->o_backoff_mappings, &req->r_t.spgid);
2041a02a946dSIlya Dryomov 	if (!spg)
2042a02a946dSIlya Dryomov 		return false;
2043a02a946dSIlya Dryomov 
2044a02a946dSIlya Dryomov 	hoid_fill_from_target(&hoid, &req->r_t);
2045a02a946dSIlya Dryomov 	backoff = lookup_containing_backoff(&spg->backoffs, &hoid);
2046a02a946dSIlya Dryomov 	if (!backoff)
2047a02a946dSIlya Dryomov 		return false;
2048a02a946dSIlya Dryomov 
2049a02a946dSIlya Dryomov 	dout("%s req %p tid %llu backoff osd%d spgid %llu.%xs%d id %llu\n",
2050a02a946dSIlya Dryomov 	     __func__, req, req->r_tid, osd->o_osd, backoff->spgid.pgid.pool,
2051a02a946dSIlya Dryomov 	     backoff->spgid.pgid.seed, backoff->spgid.shard, backoff->id);
2052a02a946dSIlya Dryomov 	return true;
2053a02a946dSIlya Dryomov }
2054a02a946dSIlya Dryomov 
20550d9c1ab3SIlya Dryomov /*
20560d9c1ab3SIlya Dryomov  * Keep get_num_data_items() in sync with this function.
20570d9c1ab3SIlya Dryomov  */
setup_request_data(struct ceph_osd_request * req)205898c4bfe9SIlya Dryomov static void setup_request_data(struct ceph_osd_request *req)
20593d14c5d2SYehuda Sadeh {
206098c4bfe9SIlya Dryomov 	struct ceph_msg *request_msg = req->r_request;
206198c4bfe9SIlya Dryomov 	struct ceph_msg *reply_msg = req->r_reply;
206298c4bfe9SIlya Dryomov 	struct ceph_osd_req_op *op;
20633d14c5d2SYehuda Sadeh 
206498c4bfe9SIlya Dryomov 	if (req->r_request->num_data_items || req->r_reply->num_data_items)
2065bb873b53SIlya Dryomov 		return;
20663d14c5d2SYehuda Sadeh 
206798c4bfe9SIlya Dryomov 	WARN_ON(request_msg->data_length || reply_msg->data_length);
206898c4bfe9SIlya Dryomov 	for (op = req->r_ops; op != &req->r_ops[req->r_num_ops]; op++) {
2069bb873b53SIlya Dryomov 		switch (op->op) {
2070bb873b53SIlya Dryomov 		/* request */
2071bb873b53SIlya Dryomov 		case CEPH_OSD_OP_WRITE:
2072bb873b53SIlya Dryomov 		case CEPH_OSD_OP_WRITEFULL:
2073bb873b53SIlya Dryomov 			WARN_ON(op->indata_len != op->extent.length);
207498c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(request_msg,
207598c4bfe9SIlya Dryomov 					       &op->extent.osd_data);
2076bb873b53SIlya Dryomov 			break;
2077bb873b53SIlya Dryomov 		case CEPH_OSD_OP_SETXATTR:
2078bb873b53SIlya Dryomov 		case CEPH_OSD_OP_CMPXATTR:
2079bb873b53SIlya Dryomov 			WARN_ON(op->indata_len != op->xattr.name_len +
2080bb873b53SIlya Dryomov 						  op->xattr.value_len);
208198c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(request_msg,
208298c4bfe9SIlya Dryomov 					       &op->xattr.osd_data);
2083bb873b53SIlya Dryomov 			break;
2084922dab61SIlya Dryomov 		case CEPH_OSD_OP_NOTIFY_ACK:
208598c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(request_msg,
2086922dab61SIlya Dryomov 					       &op->notify_ack.request_data);
2087922dab61SIlya Dryomov 			break;
208878beb0ffSLuis Henriques 		case CEPH_OSD_OP_COPY_FROM2:
208923ddf9beSLuis Henriques 			ceph_osdc_msg_data_add(request_msg,
209023ddf9beSLuis Henriques 					       &op->copy_from.osd_data);
209123ddf9beSLuis Henriques 			break;
2092bb873b53SIlya Dryomov 
2093bb873b53SIlya Dryomov 		/* reply */
2094bb873b53SIlya Dryomov 		case CEPH_OSD_OP_STAT:
209598c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(reply_msg,
2096bb873b53SIlya Dryomov 					       &op->raw_data_in);
2097bb873b53SIlya Dryomov 			break;
2098bb873b53SIlya Dryomov 		case CEPH_OSD_OP_READ:
2099f628d799SJeff Layton 		case CEPH_OSD_OP_SPARSE_READ:
210098c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(reply_msg,
2101bb873b53SIlya Dryomov 					       &op->extent.osd_data);
2102bb873b53SIlya Dryomov 			break;
2103a4ed38d7SDouglas Fuller 		case CEPH_OSD_OP_LIST_WATCHERS:
210498c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(reply_msg,
2105a4ed38d7SDouglas Fuller 					       &op->list_watchers.response_data);
2106a4ed38d7SDouglas Fuller 			break;
2107bb873b53SIlya Dryomov 
2108bb873b53SIlya Dryomov 		/* both */
2109bb873b53SIlya Dryomov 		case CEPH_OSD_OP_CALL:
2110bb873b53SIlya Dryomov 			WARN_ON(op->indata_len != op->cls.class_len +
2111bb873b53SIlya Dryomov 						  op->cls.method_len +
2112bb873b53SIlya Dryomov 						  op->cls.indata_len);
211398c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(request_msg,
211498c4bfe9SIlya Dryomov 					       &op->cls.request_info);
2115bb873b53SIlya Dryomov 			/* optional, can be NONE */
211698c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(request_msg,
211798c4bfe9SIlya Dryomov 					       &op->cls.request_data);
2118bb873b53SIlya Dryomov 			/* optional, can be NONE */
211998c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(reply_msg,
2120bb873b53SIlya Dryomov 					       &op->cls.response_data);
2121bb873b53SIlya Dryomov 			break;
212219079203SIlya Dryomov 		case CEPH_OSD_OP_NOTIFY:
212398c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(request_msg,
212419079203SIlya Dryomov 					       &op->notify.request_data);
212598c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(reply_msg,
212619079203SIlya Dryomov 					       &op->notify.response_data);
212719079203SIlya Dryomov 			break;
2128bb873b53SIlya Dryomov 		}
2129bb873b53SIlya Dryomov 	}
2130bb873b53SIlya Dryomov }
2131bb873b53SIlya Dryomov 
encode_pgid(void ** p,const struct ceph_pg * pgid)21322e59ffd1SIlya Dryomov static void encode_pgid(void **p, const struct ceph_pg *pgid)
21332e59ffd1SIlya Dryomov {
21342e59ffd1SIlya Dryomov 	ceph_encode_8(p, 1);
21352e59ffd1SIlya Dryomov 	ceph_encode_64(p, pgid->pool);
21362e59ffd1SIlya Dryomov 	ceph_encode_32(p, pgid->seed);
21372e59ffd1SIlya Dryomov 	ceph_encode_32(p, -1); /* preferred */
21382e59ffd1SIlya Dryomov }
21392e59ffd1SIlya Dryomov 
encode_spgid(void ** p,const struct ceph_spg * spgid)21408cb441c0SIlya Dryomov static void encode_spgid(void **p, const struct ceph_spg *spgid)
21418cb441c0SIlya Dryomov {
21428cb441c0SIlya Dryomov 	ceph_start_encoding(p, 1, 1, CEPH_PGID_ENCODING_LEN + 1);
21438cb441c0SIlya Dryomov 	encode_pgid(p, &spgid->pgid);
21448cb441c0SIlya Dryomov 	ceph_encode_8(p, spgid->shard);
21458cb441c0SIlya Dryomov }
21468cb441c0SIlya Dryomov 
encode_oloc(void ** p,void * end,const struct ceph_object_locator * oloc)21472e59ffd1SIlya Dryomov static void encode_oloc(void **p, void *end,
21482e59ffd1SIlya Dryomov 			const struct ceph_object_locator *oloc)
21492e59ffd1SIlya Dryomov {
21502e59ffd1SIlya Dryomov 	ceph_start_encoding(p, 5, 4, ceph_oloc_encoding_size(oloc));
21512e59ffd1SIlya Dryomov 	ceph_encode_64(p, oloc->pool);
21522e59ffd1SIlya Dryomov 	ceph_encode_32(p, -1); /* preferred */
21532e59ffd1SIlya Dryomov 	ceph_encode_32(p, 0);  /* key len */
21542e59ffd1SIlya Dryomov 	if (oloc->pool_ns)
21552e59ffd1SIlya Dryomov 		ceph_encode_string(p, end, oloc->pool_ns->str,
21562e59ffd1SIlya Dryomov 				   oloc->pool_ns->len);
21572e59ffd1SIlya Dryomov 	else
21582e59ffd1SIlya Dryomov 		ceph_encode_32(p, 0);
21592e59ffd1SIlya Dryomov }
21602e59ffd1SIlya Dryomov 
encode_request_partial(struct ceph_osd_request * req,struct ceph_msg * msg)21618cb441c0SIlya Dryomov static void encode_request_partial(struct ceph_osd_request *req,
21628cb441c0SIlya Dryomov 				   struct ceph_msg *msg)
2163bb873b53SIlya Dryomov {
2164bb873b53SIlya Dryomov 	void *p = msg->front.iov_base;
2165bb873b53SIlya Dryomov 	void *const end = p + msg->front_alloc_len;
2166bb873b53SIlya Dryomov 	u32 data_len = 0;
2167bb873b53SIlya Dryomov 	int i;
2168bb873b53SIlya Dryomov 
2169bb873b53SIlya Dryomov 	if (req->r_flags & CEPH_OSD_FLAG_WRITE) {
2170bb873b53SIlya Dryomov 		/* snapshots aren't writeable */
2171bb873b53SIlya Dryomov 		WARN_ON(req->r_snapid != CEPH_NOSNAP);
2172bb873b53SIlya Dryomov 	} else {
2173bb873b53SIlya Dryomov 		WARN_ON(req->r_mtime.tv_sec || req->r_mtime.tv_nsec ||
2174bb873b53SIlya Dryomov 			req->r_data_offset || req->r_snapc);
2175bb873b53SIlya Dryomov 	}
2176bb873b53SIlya Dryomov 
217798c4bfe9SIlya Dryomov 	setup_request_data(req);
2178bb873b53SIlya Dryomov 
21798cb441c0SIlya Dryomov 	encode_spgid(&p, &req->r_t.spgid); /* actual spg */
21808cb441c0SIlya Dryomov 	ceph_encode_32(&p, req->r_t.pgid.seed); /* raw hash */
2181bb873b53SIlya Dryomov 	ceph_encode_32(&p, req->r_osdc->osdmap->epoch);
2182bb873b53SIlya Dryomov 	ceph_encode_32(&p, req->r_flags);
21838cb441c0SIlya Dryomov 
21848cb441c0SIlya Dryomov 	/* reqid */
21858cb441c0SIlya Dryomov 	ceph_start_encoding(&p, 2, 2, sizeof(struct ceph_osd_reqid));
21868cb441c0SIlya Dryomov 	memset(p, 0, sizeof(struct ceph_osd_reqid));
21878cb441c0SIlya Dryomov 	p += sizeof(struct ceph_osd_reqid);
21888cb441c0SIlya Dryomov 
21898cb441c0SIlya Dryomov 	/* trace */
21908cb441c0SIlya Dryomov 	memset(p, 0, sizeof(struct ceph_blkin_trace_info));
21918cb441c0SIlya Dryomov 	p += sizeof(struct ceph_blkin_trace_info);
21928cb441c0SIlya Dryomov 
21938cb441c0SIlya Dryomov 	ceph_encode_32(&p, 0); /* client_inc, always 0 */
2194fac02ddfSArnd Bergmann 	ceph_encode_timespec64(p, &req->r_mtime);
2195bb873b53SIlya Dryomov 	p += sizeof(struct ceph_timespec);
2196aa26d662SJeff Layton 
21972e59ffd1SIlya Dryomov 	encode_oloc(&p, end, &req->r_t.target_oloc);
21982e59ffd1SIlya Dryomov 	ceph_encode_string(&p, end, req->r_t.target_oid.name,
21992e59ffd1SIlya Dryomov 			   req->r_t.target_oid.name_len);
2200bb873b53SIlya Dryomov 
2201bb873b53SIlya Dryomov 	/* ops, can imply data */
2202bb873b53SIlya Dryomov 	ceph_encode_16(&p, req->r_num_ops);
2203bb873b53SIlya Dryomov 	for (i = 0; i < req->r_num_ops; i++) {
2204bb873b53SIlya Dryomov 		data_len += osd_req_encode_op(p, &req->r_ops[i]);
2205bb873b53SIlya Dryomov 		p += sizeof(struct ceph_osd_op);
2206bb873b53SIlya Dryomov 	}
2207bb873b53SIlya Dryomov 
2208bb873b53SIlya Dryomov 	ceph_encode_64(&p, req->r_snapid); /* snapid */
2209bb873b53SIlya Dryomov 	if (req->r_snapc) {
2210bb873b53SIlya Dryomov 		ceph_encode_64(&p, req->r_snapc->seq);
2211bb873b53SIlya Dryomov 		ceph_encode_32(&p, req->r_snapc->num_snaps);
2212bb873b53SIlya Dryomov 		for (i = 0; i < req->r_snapc->num_snaps; i++)
2213bb873b53SIlya Dryomov 			ceph_encode_64(&p, req->r_snapc->snaps[i]);
2214bb873b53SIlya Dryomov 	} else {
2215bb873b53SIlya Dryomov 		ceph_encode_64(&p, 0); /* snap_seq */
2216bb873b53SIlya Dryomov 		ceph_encode_32(&p, 0); /* snaps len */
2217bb873b53SIlya Dryomov 	}
2218bb873b53SIlya Dryomov 
2219bb873b53SIlya Dryomov 	ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
2220986e8989SIlya Dryomov 	BUG_ON(p > end - 8); /* space for features */
2221bb873b53SIlya Dryomov 
22228cb441c0SIlya Dryomov 	msg->hdr.version = cpu_to_le16(8); /* MOSDOp v8 */
22238cb441c0SIlya Dryomov 	/* front_len is finalized in encode_request_finish() */
2224986e8989SIlya Dryomov 	msg->front.iov_len = p - msg->front.iov_base;
2225986e8989SIlya Dryomov 	msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2226bb873b53SIlya Dryomov 	msg->hdr.data_len = cpu_to_le32(data_len);
2227bb873b53SIlya Dryomov 	/*
2228bb873b53SIlya Dryomov 	 * The header "data_off" is a hint to the receiver allowing it
2229bb873b53SIlya Dryomov 	 * to align received data into its buffers such that there's no
2230bb873b53SIlya Dryomov 	 * need to re-copy it before writing it to disk (direct I/O).
2231bb873b53SIlya Dryomov 	 */
2232bb873b53SIlya Dryomov 	msg->hdr.data_off = cpu_to_le16(req->r_data_offset);
2233bb873b53SIlya Dryomov 
22348cb441c0SIlya Dryomov 	dout("%s req %p msg %p oid %s oid_len %d\n", __func__, req, msg,
22358cb441c0SIlya Dryomov 	     req->r_t.target_oid.name, req->r_t.target_oid.name_len);
22368cb441c0SIlya Dryomov }
22378cb441c0SIlya Dryomov 
encode_request_finish(struct ceph_msg * msg)22388cb441c0SIlya Dryomov static void encode_request_finish(struct ceph_msg *msg)
22398cb441c0SIlya Dryomov {
22408cb441c0SIlya Dryomov 	void *p = msg->front.iov_base;
2241986e8989SIlya Dryomov 	void *const partial_end = p + msg->front.iov_len;
22428cb441c0SIlya Dryomov 	void *const end = p + msg->front_alloc_len;
22438cb441c0SIlya Dryomov 
22448cb441c0SIlya Dryomov 	if (CEPH_HAVE_FEATURE(msg->con->peer_features, RESEND_ON_SPLIT)) {
22458cb441c0SIlya Dryomov 		/* luminous OSD -- encode features and be done */
2246986e8989SIlya Dryomov 		p = partial_end;
22478cb441c0SIlya Dryomov 		ceph_encode_64(&p, msg->con->peer_features);
22488cb441c0SIlya Dryomov 	} else {
22498cb441c0SIlya Dryomov 		struct {
22508cb441c0SIlya Dryomov 			char spgid[CEPH_ENCODING_START_BLK_LEN +
22518cb441c0SIlya Dryomov 				   CEPH_PGID_ENCODING_LEN + 1];
22528cb441c0SIlya Dryomov 			__le32 hash;
22538cb441c0SIlya Dryomov 			__le32 epoch;
22548cb441c0SIlya Dryomov 			__le32 flags;
22558cb441c0SIlya Dryomov 			char reqid[CEPH_ENCODING_START_BLK_LEN +
22568cb441c0SIlya Dryomov 				   sizeof(struct ceph_osd_reqid)];
22578cb441c0SIlya Dryomov 			char trace[sizeof(struct ceph_blkin_trace_info)];
22588cb441c0SIlya Dryomov 			__le32 client_inc;
22598cb441c0SIlya Dryomov 			struct ceph_timespec mtime;
22608cb441c0SIlya Dryomov 		} __packed head;
22618cb441c0SIlya Dryomov 		struct ceph_pg pgid;
22628cb441c0SIlya Dryomov 		void *oloc, *oid, *tail;
22638cb441c0SIlya Dryomov 		int oloc_len, oid_len, tail_len;
22648cb441c0SIlya Dryomov 		int len;
22658cb441c0SIlya Dryomov 
22668cb441c0SIlya Dryomov 		/*
22678cb441c0SIlya Dryomov 		 * Pre-luminous OSD -- reencode v8 into v4 using @head
22688cb441c0SIlya Dryomov 		 * as a temporary buffer.  Encode the raw PG; the rest
22698cb441c0SIlya Dryomov 		 * is just a matter of moving oloc, oid and tail blobs
22708cb441c0SIlya Dryomov 		 * around.
22718cb441c0SIlya Dryomov 		 */
22728cb441c0SIlya Dryomov 		memcpy(&head, p, sizeof(head));
22738cb441c0SIlya Dryomov 		p += sizeof(head);
22748cb441c0SIlya Dryomov 
22758cb441c0SIlya Dryomov 		oloc = p;
22768cb441c0SIlya Dryomov 		p += CEPH_ENCODING_START_BLK_LEN;
22778cb441c0SIlya Dryomov 		pgid.pool = ceph_decode_64(&p);
22788cb441c0SIlya Dryomov 		p += 4 + 4; /* preferred, key len */
22798cb441c0SIlya Dryomov 		len = ceph_decode_32(&p);
22808cb441c0SIlya Dryomov 		p += len;   /* nspace */
22818cb441c0SIlya Dryomov 		oloc_len = p - oloc;
22828cb441c0SIlya Dryomov 
22838cb441c0SIlya Dryomov 		oid = p;
22848cb441c0SIlya Dryomov 		len = ceph_decode_32(&p);
22858cb441c0SIlya Dryomov 		p += len;
22868cb441c0SIlya Dryomov 		oid_len = p - oid;
22878cb441c0SIlya Dryomov 
22888cb441c0SIlya Dryomov 		tail = p;
2289986e8989SIlya Dryomov 		tail_len = partial_end - p;
22908cb441c0SIlya Dryomov 
22918cb441c0SIlya Dryomov 		p = msg->front.iov_base;
22928cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.client_inc, sizeof(head.client_inc));
22938cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.epoch, sizeof(head.epoch));
22948cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.flags, sizeof(head.flags));
22958cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.mtime, sizeof(head.mtime));
22968cb441c0SIlya Dryomov 
22978cb441c0SIlya Dryomov 		/* reassert_version */
22988cb441c0SIlya Dryomov 		memset(p, 0, sizeof(struct ceph_eversion));
22998cb441c0SIlya Dryomov 		p += sizeof(struct ceph_eversion);
23008cb441c0SIlya Dryomov 
23018cb441c0SIlya Dryomov 		BUG_ON(p >= oloc);
23028cb441c0SIlya Dryomov 		memmove(p, oloc, oloc_len);
23038cb441c0SIlya Dryomov 		p += oloc_len;
23048cb441c0SIlya Dryomov 
23058cb441c0SIlya Dryomov 		pgid.seed = le32_to_cpu(head.hash);
23068cb441c0SIlya Dryomov 		encode_pgid(&p, &pgid); /* raw pg */
23078cb441c0SIlya Dryomov 
23088cb441c0SIlya Dryomov 		BUG_ON(p >= oid);
23098cb441c0SIlya Dryomov 		memmove(p, oid, oid_len);
23108cb441c0SIlya Dryomov 		p += oid_len;
23118cb441c0SIlya Dryomov 
23128cb441c0SIlya Dryomov 		/* tail -- ops, snapid, snapc, retry_attempt */
23138cb441c0SIlya Dryomov 		BUG_ON(p >= tail);
23148cb441c0SIlya Dryomov 		memmove(p, tail, tail_len);
23158cb441c0SIlya Dryomov 		p += tail_len;
23168cb441c0SIlya Dryomov 
23178cb441c0SIlya Dryomov 		msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
23188cb441c0SIlya Dryomov 	}
23198cb441c0SIlya Dryomov 
23208cb441c0SIlya Dryomov 	BUG_ON(p > end);
23218cb441c0SIlya Dryomov 	msg->front.iov_len = p - msg->front.iov_base;
23228cb441c0SIlya Dryomov 	msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
23238cb441c0SIlya Dryomov 
23248cb441c0SIlya Dryomov 	dout("%s msg %p tid %llu %u+%u+%u v%d\n", __func__, msg,
23258cb441c0SIlya Dryomov 	     le64_to_cpu(msg->hdr.tid), le32_to_cpu(msg->hdr.front_len),
23268cb441c0SIlya Dryomov 	     le32_to_cpu(msg->hdr.middle_len), le32_to_cpu(msg->hdr.data_len),
23278cb441c0SIlya Dryomov 	     le16_to_cpu(msg->hdr.version));
2328bb873b53SIlya Dryomov }
2329bb873b53SIlya Dryomov 
2330bb873b53SIlya Dryomov /*
2331bb873b53SIlya Dryomov  * @req has to be assigned a tid and registered.
2332bb873b53SIlya Dryomov  */
send_request(struct ceph_osd_request * req)2333bb873b53SIlya Dryomov static void send_request(struct ceph_osd_request *req)
2334bb873b53SIlya Dryomov {
2335bb873b53SIlya Dryomov 	struct ceph_osd *osd = req->r_osd;
2336bb873b53SIlya Dryomov 
23375aea3dcdSIlya Dryomov 	verify_osd_locked(osd);
2338bb873b53SIlya Dryomov 	WARN_ON(osd->o_osd != req->r_t.osd);
2339bb873b53SIlya Dryomov 
2340a02a946dSIlya Dryomov 	/* backoff? */
2341a02a946dSIlya Dryomov 	if (should_plug_request(req))
2342a02a946dSIlya Dryomov 		return;
2343a02a946dSIlya Dryomov 
23445aea3dcdSIlya Dryomov 	/*
23455aea3dcdSIlya Dryomov 	 * We may have a previously queued request message hanging
23465aea3dcdSIlya Dryomov 	 * around.  Cancel it to avoid corrupting the msgr.
23475aea3dcdSIlya Dryomov 	 */
23485aea3dcdSIlya Dryomov 	if (req->r_sent)
23495aea3dcdSIlya Dryomov 		ceph_msg_revoke(req->r_request);
23505aea3dcdSIlya Dryomov 
2351bb873b53SIlya Dryomov 	req->r_flags |= CEPH_OSD_FLAG_KNOWN_REDIR;
2352bb873b53SIlya Dryomov 	if (req->r_attempts)
2353bb873b53SIlya Dryomov 		req->r_flags |= CEPH_OSD_FLAG_RETRY;
2354bb873b53SIlya Dryomov 	else
2355bb873b53SIlya Dryomov 		WARN_ON(req->r_flags & CEPH_OSD_FLAG_RETRY);
2356bb873b53SIlya Dryomov 
23578cb441c0SIlya Dryomov 	encode_request_partial(req, req->r_request);
2358bb873b53SIlya Dryomov 
235904c7d789SIlya 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",
2360bb873b53SIlya Dryomov 	     __func__, req, req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed,
2361dc98ff72SIlya Dryomov 	     req->r_t.spgid.pgid.pool, req->r_t.spgid.pgid.seed,
236204c7d789SIlya Dryomov 	     req->r_t.spgid.shard, osd->o_osd, req->r_t.epoch, req->r_flags,
236304c7d789SIlya Dryomov 	     req->r_attempts);
2364bb873b53SIlya Dryomov 
2365bb873b53SIlya Dryomov 	req->r_t.paused = false;
23663d14c5d2SYehuda Sadeh 	req->r_stamp = jiffies;
2367bb873b53SIlya Dryomov 	req->r_attempts++;
23683d14c5d2SYehuda Sadeh 
2369bb873b53SIlya Dryomov 	req->r_sent = osd->o_incarnation;
2370bb873b53SIlya Dryomov 	req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
2371bb873b53SIlya Dryomov 	ceph_con_send(&osd->o_con, ceph_msg_get(req->r_request));
23723d14c5d2SYehuda Sadeh }
23733d14c5d2SYehuda Sadeh 
maybe_request_map(struct ceph_osd_client * osdc)237442c1b124SIlya Dryomov static void maybe_request_map(struct ceph_osd_client *osdc)
237542c1b124SIlya Dryomov {
237642c1b124SIlya Dryomov 	bool continuous = false;
237742c1b124SIlya Dryomov 
23785aea3dcdSIlya Dryomov 	verify_osdc_locked(osdc);
237942c1b124SIlya Dryomov 	WARN_ON(!osdc->osdmap->epoch);
238042c1b124SIlya Dryomov 
2381b7ec35b3SIlya Dryomov 	if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2382b7ec35b3SIlya Dryomov 	    ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD) ||
2383b7ec35b3SIlya Dryomov 	    ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
238442c1b124SIlya Dryomov 		dout("%s osdc %p continuous\n", __func__, osdc);
238542c1b124SIlya Dryomov 		continuous = true;
238642c1b124SIlya Dryomov 	} else {
238742c1b124SIlya Dryomov 		dout("%s osdc %p onetime\n", __func__, osdc);
238842c1b124SIlya Dryomov 	}
238942c1b124SIlya Dryomov 
239042c1b124SIlya Dryomov 	if (ceph_monc_want_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
239142c1b124SIlya Dryomov 			       osdc->osdmap->epoch + 1, continuous))
239242c1b124SIlya Dryomov 		ceph_monc_renew_subs(&osdc->client->monc);
239342c1b124SIlya Dryomov }
239442c1b124SIlya Dryomov 
2395a1f4020aSJeff Layton static void complete_request(struct ceph_osd_request *req, int err);
23964609245eSIlya Dryomov static void send_map_check(struct ceph_osd_request *req);
23974609245eSIlya Dryomov 
__submit_request(struct ceph_osd_request * req,bool wrlocked)23985aea3dcdSIlya Dryomov static void __submit_request(struct ceph_osd_request *req, bool wrlocked)
23990bbfdfe8SIlya Dryomov {
24005aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
24015aea3dcdSIlya Dryomov 	struct ceph_osd *osd;
24024609245eSIlya Dryomov 	enum calc_target_result ct_res;
240366850df5SIlya Dryomov 	int err = 0;
24045aea3dcdSIlya Dryomov 	bool need_send = false;
24055aea3dcdSIlya Dryomov 	bool promoted = false;
24060bbfdfe8SIlya Dryomov 
2407b18b9550SIlya Dryomov 	WARN_ON(req->r_tid);
24085aea3dcdSIlya Dryomov 	dout("%s req %p wrlocked %d\n", __func__, req, wrlocked);
24095aea3dcdSIlya Dryomov 
24105aea3dcdSIlya Dryomov again:
24118edf84baSIlya Dryomov 	ct_res = calc_target(osdc, &req->r_t, false);
24124609245eSIlya Dryomov 	if (ct_res == CALC_TARGET_POOL_DNE && !wrlocked)
24134609245eSIlya Dryomov 		goto promote;
24144609245eSIlya Dryomov 
24155aea3dcdSIlya Dryomov 	osd = lookup_create_osd(osdc, req->r_t.osd, wrlocked);
24165aea3dcdSIlya Dryomov 	if (IS_ERR(osd)) {
24175aea3dcdSIlya Dryomov 		WARN_ON(PTR_ERR(osd) != -EAGAIN || wrlocked);
24185aea3dcdSIlya Dryomov 		goto promote;
24195aea3dcdSIlya Dryomov 	}
24205aea3dcdSIlya Dryomov 
242166850df5SIlya Dryomov 	if (osdc->abort_err) {
242266850df5SIlya Dryomov 		dout("req %p abort_err %d\n", req, osdc->abort_err);
242366850df5SIlya Dryomov 		err = osdc->abort_err;
242466850df5SIlya Dryomov 	} else if (osdc->osdmap->epoch < osdc->epoch_barrier) {
242558eb7932SJeff Layton 		dout("req %p epoch %u barrier %u\n", req, osdc->osdmap->epoch,
242658eb7932SJeff Layton 		     osdc->epoch_barrier);
242758eb7932SJeff Layton 		req->r_t.paused = true;
242858eb7932SJeff Layton 		maybe_request_map(osdc);
242958eb7932SJeff Layton 	} else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2430b7ec35b3SIlya Dryomov 		   ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
24315aea3dcdSIlya Dryomov 		dout("req %p pausewr\n", req);
24325aea3dcdSIlya Dryomov 		req->r_t.paused = true;
24335aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
24345aea3dcdSIlya Dryomov 	} else if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
2435b7ec35b3SIlya Dryomov 		   ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
24365aea3dcdSIlya Dryomov 		dout("req %p pauserd\n", req);
24375aea3dcdSIlya Dryomov 		req->r_t.paused = true;
24385aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
24395aea3dcdSIlya Dryomov 	} else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
24405aea3dcdSIlya Dryomov 		   !(req->r_flags & (CEPH_OSD_FLAG_FULL_TRY |
24415aea3dcdSIlya Dryomov 				     CEPH_OSD_FLAG_FULL_FORCE)) &&
2442b7ec35b3SIlya Dryomov 		   (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
24435aea3dcdSIlya Dryomov 		    pool_full(osdc, req->r_t.base_oloc.pool))) {
24445aea3dcdSIlya Dryomov 		dout("req %p full/pool_full\n", req);
244502b2f549SDongsheng Yang 		if (ceph_test_opt(osdc->client, ABORT_ON_FULL)) {
244629e87820SIlya Dryomov 			err = -ENOSPC;
244729e87820SIlya Dryomov 		} else {
2448dc9b0dc4SIlya Dryomov 			if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL))
2449dc9b0dc4SIlya Dryomov 				pr_warn_ratelimited("cluster is full (osdmap FULL)\n");
2450dc9b0dc4SIlya Dryomov 			else
2451dc9b0dc4SIlya Dryomov 				pr_warn_ratelimited("pool %lld is full or reached quota\n",
2452dc9b0dc4SIlya Dryomov 						    req->r_t.base_oloc.pool);
24535aea3dcdSIlya Dryomov 			req->r_t.paused = true;
24545aea3dcdSIlya Dryomov 			maybe_request_map(osdc);
245529e87820SIlya Dryomov 		}
24565aea3dcdSIlya Dryomov 	} else if (!osd_homeless(osd)) {
24575aea3dcdSIlya Dryomov 		need_send = true;
24580bbfdfe8SIlya Dryomov 	} else {
24595aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
24600bbfdfe8SIlya Dryomov 	}
24610bbfdfe8SIlya Dryomov 
24625aea3dcdSIlya Dryomov 	mutex_lock(&osd->lock);
24635aea3dcdSIlya Dryomov 	/*
24645aea3dcdSIlya Dryomov 	 * Assign the tid atomically with send_request() to protect
24655aea3dcdSIlya Dryomov 	 * multiple writes to the same object from racing with each
24665aea3dcdSIlya Dryomov 	 * other, resulting in out of order ops on the OSDs.
24675aea3dcdSIlya Dryomov 	 */
24685aea3dcdSIlya Dryomov 	req->r_tid = atomic64_inc_return(&osdc->last_tid);
24695aea3dcdSIlya Dryomov 	link_request(osd, req);
24705aea3dcdSIlya Dryomov 	if (need_send)
24715aea3dcdSIlya Dryomov 		send_request(req);
247266850df5SIlya Dryomov 	else if (err)
247366850df5SIlya Dryomov 		complete_request(req, err);
24745aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
24755aea3dcdSIlya Dryomov 
24766001567cSIlya Dryomov 	if (!err && ct_res == CALC_TARGET_POOL_DNE)
24774609245eSIlya Dryomov 		send_map_check(req);
24784609245eSIlya Dryomov 
24795aea3dcdSIlya Dryomov 	if (promoted)
24805aea3dcdSIlya Dryomov 		downgrade_write(&osdc->lock);
24815aea3dcdSIlya Dryomov 	return;
24825aea3dcdSIlya Dryomov 
24835aea3dcdSIlya Dryomov promote:
24845aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
24855aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
24865aea3dcdSIlya Dryomov 	wrlocked = true;
24875aea3dcdSIlya Dryomov 	promoted = true;
24885aea3dcdSIlya Dryomov 	goto again;
24890bbfdfe8SIlya Dryomov }
24900bbfdfe8SIlya Dryomov 
account_request(struct ceph_osd_request * req)24915aea3dcdSIlya Dryomov static void account_request(struct ceph_osd_request *req)
24925aea3dcdSIlya Dryomov {
249354ea0046SIlya Dryomov 	WARN_ON(req->r_flags & (CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK));
2494b18b9550SIlya Dryomov 	WARN_ON(!(req->r_flags & (CEPH_OSD_FLAG_READ | CEPH_OSD_FLAG_WRITE)));
24955aea3dcdSIlya Dryomov 
2496b18b9550SIlya Dryomov 	req->r_flags |= CEPH_OSD_FLAG_ONDISK;
249722d2cfdfSIlya Dryomov 	atomic_inc(&req->r_osdc->num_requests);
24987cc5e38fSIlya Dryomov 
24997cc5e38fSIlya Dryomov 	req->r_start_stamp = jiffies;
250097e27aaaSXiubo Li 	req->r_start_latency = ktime_get();
25015aea3dcdSIlya Dryomov }
25025aea3dcdSIlya Dryomov 
submit_request(struct ceph_osd_request * req,bool wrlocked)25035aea3dcdSIlya Dryomov static void submit_request(struct ceph_osd_request *req, bool wrlocked)
25045aea3dcdSIlya Dryomov {
25055aea3dcdSIlya Dryomov 	ceph_osdc_get_request(req);
25065aea3dcdSIlya Dryomov 	account_request(req);
25075aea3dcdSIlya Dryomov 	__submit_request(req, wrlocked);
25085aea3dcdSIlya Dryomov }
25095aea3dcdSIlya Dryomov 
finish_request(struct ceph_osd_request * req)251045ee2c1dSIlya Dryomov static void finish_request(struct ceph_osd_request *req)
25115aea3dcdSIlya Dryomov {
25125aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
25135aea3dcdSIlya Dryomov 
25144609245eSIlya Dryomov 	WARN_ON(lookup_request_mc(&osdc->map_checks, req->r_tid));
251504c7d789SIlya Dryomov 	dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
251604c7d789SIlya Dryomov 
251797e27aaaSXiubo Li 	req->r_end_latency = ktime_get();
251897e27aaaSXiubo Li 
2519f628d799SJeff Layton 	if (req->r_osd) {
2520f628d799SJeff Layton 		ceph_init_sparse_read(&req->r_osd->o_sparse_read);
252104c7d789SIlya Dryomov 		unlink_request(req->r_osd, req);
2522f628d799SJeff Layton 	}
25235aea3dcdSIlya Dryomov 	atomic_dec(&osdc->num_requests);
25245aea3dcdSIlya Dryomov 
25255aea3dcdSIlya Dryomov 	/*
25265aea3dcdSIlya Dryomov 	 * If an OSD has failed or returned and a request has been sent
25275aea3dcdSIlya Dryomov 	 * twice, it's possible to get a reply and end up here while the
25285aea3dcdSIlya Dryomov 	 * request message is queued for delivery.  We will ignore the
25295aea3dcdSIlya Dryomov 	 * reply, so not a big deal, but better to try and catch it.
25305aea3dcdSIlya Dryomov 	 */
25315aea3dcdSIlya Dryomov 	ceph_msg_revoke(req->r_request);
25325aea3dcdSIlya Dryomov 	ceph_msg_revoke_incoming(req->r_reply);
25335aea3dcdSIlya Dryomov }
25345aea3dcdSIlya Dryomov 
__complete_request(struct ceph_osd_request * req)2535fe5da05eSIlya Dryomov static void __complete_request(struct ceph_osd_request *req)
2536fe5da05eSIlya Dryomov {
2537d75f773cSSakari Ailus 	dout("%s req %p tid %llu cb %ps result %d\n", __func__, req,
2538b18b9550SIlya Dryomov 	     req->r_tid, req->r_callback, req->r_result);
253926df726bSIlya Dryomov 
254026df726bSIlya Dryomov 	if (req->r_callback)
2541fe5da05eSIlya Dryomov 		req->r_callback(req);
254226df726bSIlya Dryomov 	complete_all(&req->r_completion);
254326df726bSIlya Dryomov 	ceph_osdc_put_request(req);
2544b18b9550SIlya Dryomov }
2545fe5da05eSIlya Dryomov 
complete_request_workfn(struct work_struct * work)254688bc1922SIlya Dryomov static void complete_request_workfn(struct work_struct *work)
254788bc1922SIlya Dryomov {
254888bc1922SIlya Dryomov 	struct ceph_osd_request *req =
254988bc1922SIlya Dryomov 	    container_of(work, struct ceph_osd_request, r_complete_work);
255088bc1922SIlya Dryomov 
255188bc1922SIlya Dryomov 	__complete_request(req);
25520bbfdfe8SIlya Dryomov }
25530bbfdfe8SIlya Dryomov 
25544609245eSIlya Dryomov /*
2555b18b9550SIlya Dryomov  * This is open-coded in handle_reply().
25564609245eSIlya Dryomov  */
complete_request(struct ceph_osd_request * req,int err)25574609245eSIlya Dryomov static void complete_request(struct ceph_osd_request *req, int err)
25584609245eSIlya Dryomov {
25594609245eSIlya Dryomov 	dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
25604609245eSIlya Dryomov 
25614609245eSIlya Dryomov 	req->r_result = err;
256245ee2c1dSIlya Dryomov 	finish_request(req);
256388bc1922SIlya Dryomov 
256488bc1922SIlya Dryomov 	INIT_WORK(&req->r_complete_work, complete_request_workfn);
256588bc1922SIlya Dryomov 	queue_work(req->r_osdc->completion_wq, &req->r_complete_work);
25664609245eSIlya Dryomov }
25674609245eSIlya Dryomov 
cancel_map_check(struct ceph_osd_request * req)25684609245eSIlya Dryomov static void cancel_map_check(struct ceph_osd_request *req)
25694609245eSIlya Dryomov {
25704609245eSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
25714609245eSIlya Dryomov 	struct ceph_osd_request *lookup_req;
25724609245eSIlya Dryomov 
25734609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
25744609245eSIlya Dryomov 
25754609245eSIlya Dryomov 	lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
25764609245eSIlya Dryomov 	if (!lookup_req)
25774609245eSIlya Dryomov 		return;
25784609245eSIlya Dryomov 
25794609245eSIlya Dryomov 	WARN_ON(lookup_req != req);
25804609245eSIlya Dryomov 	erase_request_mc(&osdc->map_checks, req);
25814609245eSIlya Dryomov 	ceph_osdc_put_request(req);
25824609245eSIlya Dryomov }
25834609245eSIlya Dryomov 
cancel_request(struct ceph_osd_request * req)25845aea3dcdSIlya Dryomov static void cancel_request(struct ceph_osd_request *req)
25855aea3dcdSIlya Dryomov {
25865aea3dcdSIlya Dryomov 	dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
25875aea3dcdSIlya Dryomov 
25884609245eSIlya Dryomov 	cancel_map_check(req);
258945ee2c1dSIlya Dryomov 	finish_request(req);
2590b18b9550SIlya Dryomov 	complete_all(&req->r_completion);
2591c297eb42SIlya Dryomov 	ceph_osdc_put_request(req);
25925aea3dcdSIlya Dryomov }
25935aea3dcdSIlya Dryomov 
abort_request(struct ceph_osd_request * req,int err)25947cc5e38fSIlya Dryomov static void abort_request(struct ceph_osd_request *req, int err)
25957cc5e38fSIlya Dryomov {
25967cc5e38fSIlya Dryomov 	dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
25977cc5e38fSIlya Dryomov 
25987cc5e38fSIlya Dryomov 	cancel_map_check(req);
25997cc5e38fSIlya Dryomov 	complete_request(req, err);
26007cc5e38fSIlya Dryomov }
26017cc5e38fSIlya Dryomov 
abort_fn(struct ceph_osd_request * req,void * arg)260266850df5SIlya Dryomov static int abort_fn(struct ceph_osd_request *req, void *arg)
260366850df5SIlya Dryomov {
260466850df5SIlya Dryomov 	int err = *(int *)arg;
260566850df5SIlya Dryomov 
260666850df5SIlya Dryomov 	abort_request(req, err);
260766850df5SIlya Dryomov 	return 0; /* continue iteration */
260866850df5SIlya Dryomov }
260966850df5SIlya Dryomov 
261066850df5SIlya Dryomov /*
261166850df5SIlya Dryomov  * Abort all in-flight requests with @err and arrange for all future
261266850df5SIlya Dryomov  * requests to be failed immediately.
261366850df5SIlya Dryomov  */
ceph_osdc_abort_requests(struct ceph_osd_client * osdc,int err)261466850df5SIlya Dryomov void ceph_osdc_abort_requests(struct ceph_osd_client *osdc, int err)
261566850df5SIlya Dryomov {
261666850df5SIlya Dryomov 	dout("%s osdc %p err %d\n", __func__, osdc, err);
261766850df5SIlya Dryomov 	down_write(&osdc->lock);
261866850df5SIlya Dryomov 	for_each_request(osdc, abort_fn, &err);
261966850df5SIlya Dryomov 	osdc->abort_err = err;
262066850df5SIlya Dryomov 	up_write(&osdc->lock);
262166850df5SIlya Dryomov }
262266850df5SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_abort_requests);
262366850df5SIlya Dryomov 
ceph_osdc_clear_abort_err(struct ceph_osd_client * osdc)26242cef0ba8SYan, Zheng void ceph_osdc_clear_abort_err(struct ceph_osd_client *osdc)
26252cef0ba8SYan, Zheng {
26262cef0ba8SYan, Zheng 	down_write(&osdc->lock);
26272cef0ba8SYan, Zheng 	osdc->abort_err = 0;
26282cef0ba8SYan, Zheng 	up_write(&osdc->lock);
26292cef0ba8SYan, Zheng }
26302cef0ba8SYan, Zheng EXPORT_SYMBOL(ceph_osdc_clear_abort_err);
26312cef0ba8SYan, Zheng 
update_epoch_barrier(struct ceph_osd_client * osdc,u32 eb)263258eb7932SJeff Layton static void update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
263358eb7932SJeff Layton {
263458eb7932SJeff Layton 	if (likely(eb > osdc->epoch_barrier)) {
263558eb7932SJeff Layton 		dout("updating epoch_barrier from %u to %u\n",
263658eb7932SJeff Layton 				osdc->epoch_barrier, eb);
263758eb7932SJeff Layton 		osdc->epoch_barrier = eb;
263858eb7932SJeff Layton 		/* Request map if we're not to the barrier yet */
263958eb7932SJeff Layton 		if (eb > osdc->osdmap->epoch)
264058eb7932SJeff Layton 			maybe_request_map(osdc);
264158eb7932SJeff Layton 	}
264258eb7932SJeff Layton }
264358eb7932SJeff Layton 
ceph_osdc_update_epoch_barrier(struct ceph_osd_client * osdc,u32 eb)264458eb7932SJeff Layton void ceph_osdc_update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
264558eb7932SJeff Layton {
264658eb7932SJeff Layton 	down_read(&osdc->lock);
264758eb7932SJeff Layton 	if (unlikely(eb > osdc->epoch_barrier)) {
264858eb7932SJeff Layton 		up_read(&osdc->lock);
264958eb7932SJeff Layton 		down_write(&osdc->lock);
265058eb7932SJeff Layton 		update_epoch_barrier(osdc, eb);
265158eb7932SJeff Layton 		up_write(&osdc->lock);
265258eb7932SJeff Layton 	} else {
265358eb7932SJeff Layton 		up_read(&osdc->lock);
265458eb7932SJeff Layton 	}
265558eb7932SJeff Layton }
265658eb7932SJeff Layton EXPORT_SYMBOL(ceph_osdc_update_epoch_barrier);
265758eb7932SJeff Layton 
2658fc36d0a4SJeff Layton /*
26594eea0fefSIlya Dryomov  * We can end up releasing caps as a result of abort_request().
26604eea0fefSIlya Dryomov  * In that case, we probably want to ensure that the cap release message
26614eea0fefSIlya Dryomov  * has an updated epoch barrier in it, so set the epoch barrier prior to
26624eea0fefSIlya Dryomov  * aborting the first request.
26634eea0fefSIlya Dryomov  */
abort_on_full_fn(struct ceph_osd_request * req,void * arg)26644eea0fefSIlya Dryomov static int abort_on_full_fn(struct ceph_osd_request *req, void *arg)
26654eea0fefSIlya Dryomov {
26664eea0fefSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
26674eea0fefSIlya Dryomov 	bool *victims = arg;
26684eea0fefSIlya Dryomov 
2669c843d13cSIlya Dryomov 	if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
26704eea0fefSIlya Dryomov 	    (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2671690f951dSIlya Dryomov 	     pool_full(osdc, req->r_t.base_oloc.pool))) {
26724eea0fefSIlya Dryomov 		if (!*victims) {
26734eea0fefSIlya Dryomov 			update_epoch_barrier(osdc, osdc->osdmap->epoch);
26744eea0fefSIlya Dryomov 			*victims = true;
26754eea0fefSIlya Dryomov 		}
26764eea0fefSIlya Dryomov 		abort_request(req, -ENOSPC);
26774eea0fefSIlya Dryomov 	}
26784eea0fefSIlya Dryomov 
26794eea0fefSIlya Dryomov 	return 0; /* continue iteration */
26804eea0fefSIlya Dryomov }
26814eea0fefSIlya Dryomov 
26824eea0fefSIlya Dryomov /*
2683fc36d0a4SJeff Layton  * Drop all pending requests that are stalled waiting on a full condition to
268458eb7932SJeff Layton  * clear, and complete them with ENOSPC as the return code. Set the
268558eb7932SJeff Layton  * osdc->epoch_barrier to the latest map epoch that we've seen if any were
268658eb7932SJeff Layton  * cancelled.
2687fc36d0a4SJeff Layton  */
ceph_osdc_abort_on_full(struct ceph_osd_client * osdc)2688fc36d0a4SJeff Layton static void ceph_osdc_abort_on_full(struct ceph_osd_client *osdc)
2689fc36d0a4SJeff Layton {
269058eb7932SJeff Layton 	bool victims = false;
2691fc36d0a4SJeff Layton 
269202b2f549SDongsheng Yang 	if (ceph_test_opt(osdc->client, ABORT_ON_FULL) &&
2693c843d13cSIlya Dryomov 	    (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) || have_pool_full(osdc)))
26944eea0fefSIlya Dryomov 		for_each_request(osdc, abort_on_full_fn, &victims);
2695fc36d0a4SJeff Layton }
2696fc36d0a4SJeff Layton 
check_pool_dne(struct ceph_osd_request * req)26974609245eSIlya Dryomov static void check_pool_dne(struct ceph_osd_request *req)
26984609245eSIlya Dryomov {
26994609245eSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
27004609245eSIlya Dryomov 	struct ceph_osdmap *map = osdc->osdmap;
27014609245eSIlya Dryomov 
27024609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
27034609245eSIlya Dryomov 	WARN_ON(!map->epoch);
27044609245eSIlya Dryomov 
27054609245eSIlya Dryomov 	if (req->r_attempts) {
27064609245eSIlya Dryomov 		/*
27074609245eSIlya Dryomov 		 * We sent a request earlier, which means that
27084609245eSIlya Dryomov 		 * previously the pool existed, and now it does not
27094609245eSIlya Dryomov 		 * (i.e., it was deleted).
27104609245eSIlya Dryomov 		 */
27114609245eSIlya Dryomov 		req->r_map_dne_bound = map->epoch;
27124609245eSIlya Dryomov 		dout("%s req %p tid %llu pool disappeared\n", __func__, req,
27134609245eSIlya Dryomov 		     req->r_tid);
27144609245eSIlya Dryomov 	} else {
27154609245eSIlya Dryomov 		dout("%s req %p tid %llu map_dne_bound %u have %u\n", __func__,
27164609245eSIlya Dryomov 		     req, req->r_tid, req->r_map_dne_bound, map->epoch);
27174609245eSIlya Dryomov 	}
27184609245eSIlya Dryomov 
27194609245eSIlya Dryomov 	if (req->r_map_dne_bound) {
27204609245eSIlya Dryomov 		if (map->epoch >= req->r_map_dne_bound) {
27214609245eSIlya Dryomov 			/* we had a new enough map */
27224609245eSIlya Dryomov 			pr_info_ratelimited("tid %llu pool does not exist\n",
27234609245eSIlya Dryomov 					    req->r_tid);
27244609245eSIlya Dryomov 			complete_request(req, -ENOENT);
27254609245eSIlya Dryomov 		}
27264609245eSIlya Dryomov 	} else {
27274609245eSIlya Dryomov 		send_map_check(req);
27284609245eSIlya Dryomov 	}
27294609245eSIlya Dryomov }
27304609245eSIlya Dryomov 
map_check_cb(struct ceph_mon_generic_request * greq)27314609245eSIlya Dryomov static void map_check_cb(struct ceph_mon_generic_request *greq)
27324609245eSIlya Dryomov {
27334609245eSIlya Dryomov 	struct ceph_osd_client *osdc = &greq->monc->client->osdc;
27344609245eSIlya Dryomov 	struct ceph_osd_request *req;
27354609245eSIlya Dryomov 	u64 tid = greq->private_data;
27364609245eSIlya Dryomov 
27374609245eSIlya Dryomov 	WARN_ON(greq->result || !greq->u.newest);
27384609245eSIlya Dryomov 
27394609245eSIlya Dryomov 	down_write(&osdc->lock);
27404609245eSIlya Dryomov 	req = lookup_request_mc(&osdc->map_checks, tid);
27414609245eSIlya Dryomov 	if (!req) {
27424609245eSIlya Dryomov 		dout("%s tid %llu dne\n", __func__, tid);
27434609245eSIlya Dryomov 		goto out_unlock;
27444609245eSIlya Dryomov 	}
27454609245eSIlya Dryomov 
27464609245eSIlya Dryomov 	dout("%s req %p tid %llu map_dne_bound %u newest %llu\n", __func__,
27474609245eSIlya Dryomov 	     req, req->r_tid, req->r_map_dne_bound, greq->u.newest);
27484609245eSIlya Dryomov 	if (!req->r_map_dne_bound)
27494609245eSIlya Dryomov 		req->r_map_dne_bound = greq->u.newest;
27504609245eSIlya Dryomov 	erase_request_mc(&osdc->map_checks, req);
27514609245eSIlya Dryomov 	check_pool_dne(req);
27524609245eSIlya Dryomov 
27534609245eSIlya Dryomov 	ceph_osdc_put_request(req);
27544609245eSIlya Dryomov out_unlock:
27554609245eSIlya Dryomov 	up_write(&osdc->lock);
27564609245eSIlya Dryomov }
27574609245eSIlya Dryomov 
send_map_check(struct ceph_osd_request * req)27584609245eSIlya Dryomov static void send_map_check(struct ceph_osd_request *req)
27594609245eSIlya Dryomov {
27604609245eSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
27614609245eSIlya Dryomov 	struct ceph_osd_request *lookup_req;
27624609245eSIlya Dryomov 	int ret;
27634609245eSIlya Dryomov 
27644609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
27654609245eSIlya Dryomov 
27664609245eSIlya Dryomov 	lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
27674609245eSIlya Dryomov 	if (lookup_req) {
27684609245eSIlya Dryomov 		WARN_ON(lookup_req != req);
27694609245eSIlya Dryomov 		return;
27704609245eSIlya Dryomov 	}
27714609245eSIlya Dryomov 
27724609245eSIlya Dryomov 	ceph_osdc_get_request(req);
27734609245eSIlya Dryomov 	insert_request_mc(&osdc->map_checks, req);
27744609245eSIlya Dryomov 	ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
27754609245eSIlya Dryomov 					  map_check_cb, req->r_tid);
27764609245eSIlya Dryomov 	WARN_ON(ret);
27774609245eSIlya Dryomov }
27784609245eSIlya Dryomov 
27790bbfdfe8SIlya Dryomov /*
2780922dab61SIlya Dryomov  * lingering requests, watch/notify v2 infrastructure
2781922dab61SIlya Dryomov  */
linger_release(struct kref * kref)2782922dab61SIlya Dryomov static void linger_release(struct kref *kref)
2783922dab61SIlya Dryomov {
2784922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq =
2785922dab61SIlya Dryomov 	    container_of(kref, struct ceph_osd_linger_request, kref);
2786922dab61SIlya Dryomov 
2787922dab61SIlya Dryomov 	dout("%s lreq %p reg_req %p ping_req %p\n", __func__, lreq,
2788922dab61SIlya Dryomov 	     lreq->reg_req, lreq->ping_req);
2789922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&lreq->node));
2790922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&lreq->osdc_node));
27914609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&lreq->mc_node));
2792922dab61SIlya Dryomov 	WARN_ON(!list_empty(&lreq->scan_item));
2793b07d3c4bSIlya Dryomov 	WARN_ON(!list_empty(&lreq->pending_lworks));
2794922dab61SIlya Dryomov 	WARN_ON(lreq->osd);
2795922dab61SIlya Dryomov 
279675dbb685SIlya Dryomov 	if (lreq->request_pl)
279775dbb685SIlya Dryomov 		ceph_pagelist_release(lreq->request_pl);
279875dbb685SIlya Dryomov 	if (lreq->notify_id_pages)
279975dbb685SIlya Dryomov 		ceph_release_page_vector(lreq->notify_id_pages, 1);
280075dbb685SIlya Dryomov 
2801922dab61SIlya Dryomov 	ceph_osdc_put_request(lreq->reg_req);
2802922dab61SIlya Dryomov 	ceph_osdc_put_request(lreq->ping_req);
2803922dab61SIlya Dryomov 	target_destroy(&lreq->t);
2804922dab61SIlya Dryomov 	kfree(lreq);
2805922dab61SIlya Dryomov }
2806922dab61SIlya Dryomov 
linger_put(struct ceph_osd_linger_request * lreq)2807922dab61SIlya Dryomov static void linger_put(struct ceph_osd_linger_request *lreq)
2808922dab61SIlya Dryomov {
2809922dab61SIlya Dryomov 	if (lreq)
2810922dab61SIlya Dryomov 		kref_put(&lreq->kref, linger_release);
2811922dab61SIlya Dryomov }
2812922dab61SIlya Dryomov 
2813922dab61SIlya Dryomov static struct ceph_osd_linger_request *
linger_get(struct ceph_osd_linger_request * lreq)2814922dab61SIlya Dryomov linger_get(struct ceph_osd_linger_request *lreq)
2815922dab61SIlya Dryomov {
2816922dab61SIlya Dryomov 	kref_get(&lreq->kref);
2817922dab61SIlya Dryomov 	return lreq;
2818922dab61SIlya Dryomov }
2819922dab61SIlya Dryomov 
2820922dab61SIlya Dryomov static struct ceph_osd_linger_request *
linger_alloc(struct ceph_osd_client * osdc)2821922dab61SIlya Dryomov linger_alloc(struct ceph_osd_client *osdc)
2822922dab61SIlya Dryomov {
2823922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
2824922dab61SIlya Dryomov 
2825922dab61SIlya Dryomov 	lreq = kzalloc(sizeof(*lreq), GFP_NOIO);
2826922dab61SIlya Dryomov 	if (!lreq)
2827922dab61SIlya Dryomov 		return NULL;
2828922dab61SIlya Dryomov 
2829922dab61SIlya Dryomov 	kref_init(&lreq->kref);
2830922dab61SIlya Dryomov 	mutex_init(&lreq->lock);
2831922dab61SIlya Dryomov 	RB_CLEAR_NODE(&lreq->node);
2832922dab61SIlya Dryomov 	RB_CLEAR_NODE(&lreq->osdc_node);
28334609245eSIlya Dryomov 	RB_CLEAR_NODE(&lreq->mc_node);
2834922dab61SIlya Dryomov 	INIT_LIST_HEAD(&lreq->scan_item);
2835b07d3c4bSIlya Dryomov 	INIT_LIST_HEAD(&lreq->pending_lworks);
2836922dab61SIlya Dryomov 	init_completion(&lreq->reg_commit_wait);
283719079203SIlya Dryomov 	init_completion(&lreq->notify_finish_wait);
2838922dab61SIlya Dryomov 
2839922dab61SIlya Dryomov 	lreq->osdc = osdc;
2840922dab61SIlya Dryomov 	target_init(&lreq->t);
2841922dab61SIlya Dryomov 
2842922dab61SIlya Dryomov 	dout("%s lreq %p\n", __func__, lreq);
2843922dab61SIlya Dryomov 	return lreq;
2844922dab61SIlya Dryomov }
2845922dab61SIlya Dryomov 
DEFINE_RB_INSDEL_FUNCS(linger,struct ceph_osd_linger_request,linger_id,node)2846922dab61SIlya Dryomov DEFINE_RB_INSDEL_FUNCS(linger, struct ceph_osd_linger_request, linger_id, node)
2847922dab61SIlya Dryomov DEFINE_RB_FUNCS(linger_osdc, struct ceph_osd_linger_request, linger_id, osdc_node)
28484609245eSIlya Dryomov DEFINE_RB_FUNCS(linger_mc, struct ceph_osd_linger_request, linger_id, mc_node)
2849922dab61SIlya Dryomov 
2850922dab61SIlya Dryomov /*
2851922dab61SIlya Dryomov  * Create linger request <-> OSD session relation.
2852922dab61SIlya Dryomov  *
2853922dab61SIlya Dryomov  * @lreq has to be registered, @osd may be homeless.
2854922dab61SIlya Dryomov  */
2855922dab61SIlya Dryomov static void link_linger(struct ceph_osd *osd,
2856922dab61SIlya Dryomov 			struct ceph_osd_linger_request *lreq)
2857922dab61SIlya Dryomov {
2858922dab61SIlya Dryomov 	verify_osd_locked(osd);
2859922dab61SIlya Dryomov 	WARN_ON(!lreq->linger_id || lreq->osd);
2860922dab61SIlya Dryomov 	dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2861922dab61SIlya Dryomov 	     osd->o_osd, lreq, lreq->linger_id);
2862922dab61SIlya Dryomov 
2863922dab61SIlya Dryomov 	if (!osd_homeless(osd))
2864922dab61SIlya Dryomov 		__remove_osd_from_lru(osd);
2865922dab61SIlya Dryomov 	else
2866922dab61SIlya Dryomov 		atomic_inc(&osd->o_osdc->num_homeless);
2867922dab61SIlya Dryomov 
2868922dab61SIlya Dryomov 	get_osd(osd);
2869922dab61SIlya Dryomov 	insert_linger(&osd->o_linger_requests, lreq);
2870922dab61SIlya Dryomov 	lreq->osd = osd;
2871922dab61SIlya Dryomov }
2872922dab61SIlya Dryomov 
unlink_linger(struct ceph_osd * osd,struct ceph_osd_linger_request * lreq)2873922dab61SIlya Dryomov static void unlink_linger(struct ceph_osd *osd,
2874922dab61SIlya Dryomov 			  struct ceph_osd_linger_request *lreq)
2875922dab61SIlya Dryomov {
2876922dab61SIlya Dryomov 	verify_osd_locked(osd);
2877922dab61SIlya Dryomov 	WARN_ON(lreq->osd != osd);
2878922dab61SIlya Dryomov 	dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2879922dab61SIlya Dryomov 	     osd->o_osd, lreq, lreq->linger_id);
2880922dab61SIlya Dryomov 
2881922dab61SIlya Dryomov 	lreq->osd = NULL;
2882922dab61SIlya Dryomov 	erase_linger(&osd->o_linger_requests, lreq);
2883922dab61SIlya Dryomov 	put_osd(osd);
2884922dab61SIlya Dryomov 
2885922dab61SIlya Dryomov 	if (!osd_homeless(osd))
2886922dab61SIlya Dryomov 		maybe_move_osd_to_lru(osd);
2887922dab61SIlya Dryomov 	else
2888922dab61SIlya Dryomov 		atomic_dec(&osd->o_osdc->num_homeless);
2889922dab61SIlya Dryomov }
2890922dab61SIlya Dryomov 
__linger_registered(struct ceph_osd_linger_request * lreq)2891922dab61SIlya Dryomov static bool __linger_registered(struct ceph_osd_linger_request *lreq)
2892922dab61SIlya Dryomov {
2893922dab61SIlya Dryomov 	verify_osdc_locked(lreq->osdc);
2894922dab61SIlya Dryomov 
2895922dab61SIlya Dryomov 	return !RB_EMPTY_NODE(&lreq->osdc_node);
2896922dab61SIlya Dryomov }
2897922dab61SIlya Dryomov 
linger_registered(struct ceph_osd_linger_request * lreq)2898922dab61SIlya Dryomov static bool linger_registered(struct ceph_osd_linger_request *lreq)
2899922dab61SIlya Dryomov {
2900922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2901922dab61SIlya Dryomov 	bool registered;
2902922dab61SIlya Dryomov 
2903922dab61SIlya Dryomov 	down_read(&osdc->lock);
2904922dab61SIlya Dryomov 	registered = __linger_registered(lreq);
2905922dab61SIlya Dryomov 	up_read(&osdc->lock);
2906922dab61SIlya Dryomov 
2907922dab61SIlya Dryomov 	return registered;
2908922dab61SIlya Dryomov }
2909922dab61SIlya Dryomov 
linger_register(struct ceph_osd_linger_request * lreq)2910922dab61SIlya Dryomov static void linger_register(struct ceph_osd_linger_request *lreq)
2911922dab61SIlya Dryomov {
2912922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2913922dab61SIlya Dryomov 
2914922dab61SIlya Dryomov 	verify_osdc_wrlocked(osdc);
2915922dab61SIlya Dryomov 	WARN_ON(lreq->linger_id);
2916922dab61SIlya Dryomov 
2917922dab61SIlya Dryomov 	linger_get(lreq);
2918922dab61SIlya Dryomov 	lreq->linger_id = ++osdc->last_linger_id;
2919922dab61SIlya Dryomov 	insert_linger_osdc(&osdc->linger_requests, lreq);
2920922dab61SIlya Dryomov }
2921922dab61SIlya Dryomov 
linger_unregister(struct ceph_osd_linger_request * lreq)2922922dab61SIlya Dryomov static void linger_unregister(struct ceph_osd_linger_request *lreq)
2923922dab61SIlya Dryomov {
2924922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2925922dab61SIlya Dryomov 
2926922dab61SIlya Dryomov 	verify_osdc_wrlocked(osdc);
2927922dab61SIlya Dryomov 
2928922dab61SIlya Dryomov 	erase_linger_osdc(&osdc->linger_requests, lreq);
2929922dab61SIlya Dryomov 	linger_put(lreq);
2930922dab61SIlya Dryomov }
2931922dab61SIlya Dryomov 
cancel_linger_request(struct ceph_osd_request * req)2932922dab61SIlya Dryomov static void cancel_linger_request(struct ceph_osd_request *req)
2933922dab61SIlya Dryomov {
2934922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
2935922dab61SIlya Dryomov 
2936922dab61SIlya Dryomov 	WARN_ON(!req->r_linger);
2937922dab61SIlya Dryomov 	cancel_request(req);
2938922dab61SIlya Dryomov 	linger_put(lreq);
2939922dab61SIlya Dryomov }
2940922dab61SIlya Dryomov 
2941922dab61SIlya Dryomov struct linger_work {
2942922dab61SIlya Dryomov 	struct work_struct work;
2943922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
2944b07d3c4bSIlya Dryomov 	struct list_head pending_item;
2945b07d3c4bSIlya Dryomov 	unsigned long queued_stamp;
2946922dab61SIlya Dryomov 
2947922dab61SIlya Dryomov 	union {
2948922dab61SIlya Dryomov 		struct {
2949922dab61SIlya Dryomov 			u64 notify_id;
2950922dab61SIlya Dryomov 			u64 notifier_id;
2951922dab61SIlya Dryomov 			void *payload; /* points into @msg front */
2952922dab61SIlya Dryomov 			size_t payload_len;
2953922dab61SIlya Dryomov 
2954922dab61SIlya Dryomov 			struct ceph_msg *msg; /* for ceph_msg_put() */
2955922dab61SIlya Dryomov 		} notify;
2956922dab61SIlya Dryomov 		struct {
2957922dab61SIlya Dryomov 			int err;
2958922dab61SIlya Dryomov 		} error;
2959922dab61SIlya Dryomov 	};
2960922dab61SIlya Dryomov };
2961922dab61SIlya Dryomov 
lwork_alloc(struct ceph_osd_linger_request * lreq,work_func_t workfn)2962922dab61SIlya Dryomov static struct linger_work *lwork_alloc(struct ceph_osd_linger_request *lreq,
2963922dab61SIlya Dryomov 				       work_func_t workfn)
2964922dab61SIlya Dryomov {
2965922dab61SIlya Dryomov 	struct linger_work *lwork;
2966922dab61SIlya Dryomov 
2967922dab61SIlya Dryomov 	lwork = kzalloc(sizeof(*lwork), GFP_NOIO);
2968922dab61SIlya Dryomov 	if (!lwork)
2969922dab61SIlya Dryomov 		return NULL;
2970922dab61SIlya Dryomov 
2971922dab61SIlya Dryomov 	INIT_WORK(&lwork->work, workfn);
2972b07d3c4bSIlya Dryomov 	INIT_LIST_HEAD(&lwork->pending_item);
2973922dab61SIlya Dryomov 	lwork->lreq = linger_get(lreq);
2974922dab61SIlya Dryomov 
2975922dab61SIlya Dryomov 	return lwork;
2976922dab61SIlya Dryomov }
2977922dab61SIlya Dryomov 
lwork_free(struct linger_work * lwork)2978922dab61SIlya Dryomov static void lwork_free(struct linger_work *lwork)
2979922dab61SIlya Dryomov {
2980922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2981922dab61SIlya Dryomov 
2982b07d3c4bSIlya Dryomov 	mutex_lock(&lreq->lock);
2983b07d3c4bSIlya Dryomov 	list_del(&lwork->pending_item);
2984b07d3c4bSIlya Dryomov 	mutex_unlock(&lreq->lock);
2985b07d3c4bSIlya Dryomov 
2986922dab61SIlya Dryomov 	linger_put(lreq);
2987922dab61SIlya Dryomov 	kfree(lwork);
2988922dab61SIlya Dryomov }
2989922dab61SIlya Dryomov 
lwork_queue(struct linger_work * lwork)2990922dab61SIlya Dryomov static void lwork_queue(struct linger_work *lwork)
2991922dab61SIlya Dryomov {
2992922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2993922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2994922dab61SIlya Dryomov 
2995922dab61SIlya Dryomov 	verify_lreq_locked(lreq);
2996b07d3c4bSIlya Dryomov 	WARN_ON(!list_empty(&lwork->pending_item));
2997b07d3c4bSIlya Dryomov 
2998b07d3c4bSIlya Dryomov 	lwork->queued_stamp = jiffies;
2999b07d3c4bSIlya Dryomov 	list_add_tail(&lwork->pending_item, &lreq->pending_lworks);
3000922dab61SIlya Dryomov 	queue_work(osdc->notify_wq, &lwork->work);
3001922dab61SIlya Dryomov }
3002922dab61SIlya Dryomov 
do_watch_notify(struct work_struct * w)3003922dab61SIlya Dryomov static void do_watch_notify(struct work_struct *w)
3004922dab61SIlya Dryomov {
3005922dab61SIlya Dryomov 	struct linger_work *lwork = container_of(w, struct linger_work, work);
3006922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
3007922dab61SIlya Dryomov 
3008922dab61SIlya Dryomov 	if (!linger_registered(lreq)) {
3009922dab61SIlya Dryomov 		dout("%s lreq %p not registered\n", __func__, lreq);
3010922dab61SIlya Dryomov 		goto out;
3011922dab61SIlya Dryomov 	}
3012922dab61SIlya Dryomov 
301319079203SIlya Dryomov 	WARN_ON(!lreq->is_watch);
3014922dab61SIlya Dryomov 	dout("%s lreq %p notify_id %llu notifier_id %llu payload_len %zu\n",
3015922dab61SIlya Dryomov 	     __func__, lreq, lwork->notify.notify_id, lwork->notify.notifier_id,
3016922dab61SIlya Dryomov 	     lwork->notify.payload_len);
3017922dab61SIlya Dryomov 	lreq->wcb(lreq->data, lwork->notify.notify_id, lreq->linger_id,
3018922dab61SIlya Dryomov 		  lwork->notify.notifier_id, lwork->notify.payload,
3019922dab61SIlya Dryomov 		  lwork->notify.payload_len);
3020922dab61SIlya Dryomov 
3021922dab61SIlya Dryomov out:
3022922dab61SIlya Dryomov 	ceph_msg_put(lwork->notify.msg);
3023922dab61SIlya Dryomov 	lwork_free(lwork);
3024922dab61SIlya Dryomov }
3025922dab61SIlya Dryomov 
do_watch_error(struct work_struct * w)3026922dab61SIlya Dryomov static void do_watch_error(struct work_struct *w)
3027922dab61SIlya Dryomov {
3028922dab61SIlya Dryomov 	struct linger_work *lwork = container_of(w, struct linger_work, work);
3029922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
3030922dab61SIlya Dryomov 
3031922dab61SIlya Dryomov 	if (!linger_registered(lreq)) {
3032922dab61SIlya Dryomov 		dout("%s lreq %p not registered\n", __func__, lreq);
3033922dab61SIlya Dryomov 		goto out;
3034922dab61SIlya Dryomov 	}
3035922dab61SIlya Dryomov 
3036922dab61SIlya Dryomov 	dout("%s lreq %p err %d\n", __func__, lreq, lwork->error.err);
3037922dab61SIlya Dryomov 	lreq->errcb(lreq->data, lreq->linger_id, lwork->error.err);
3038922dab61SIlya Dryomov 
3039922dab61SIlya Dryomov out:
3040922dab61SIlya Dryomov 	lwork_free(lwork);
3041922dab61SIlya Dryomov }
3042922dab61SIlya Dryomov 
queue_watch_error(struct ceph_osd_linger_request * lreq)3043922dab61SIlya Dryomov static void queue_watch_error(struct ceph_osd_linger_request *lreq)
3044922dab61SIlya Dryomov {
3045922dab61SIlya Dryomov 	struct linger_work *lwork;
3046922dab61SIlya Dryomov 
3047922dab61SIlya Dryomov 	lwork = lwork_alloc(lreq, do_watch_error);
3048922dab61SIlya Dryomov 	if (!lwork) {
3049922dab61SIlya Dryomov 		pr_err("failed to allocate error-lwork\n");
3050922dab61SIlya Dryomov 		return;
3051922dab61SIlya Dryomov 	}
3052922dab61SIlya Dryomov 
3053922dab61SIlya Dryomov 	lwork->error.err = lreq->last_error;
3054922dab61SIlya Dryomov 	lwork_queue(lwork);
3055922dab61SIlya Dryomov }
3056922dab61SIlya Dryomov 
linger_reg_commit_complete(struct ceph_osd_linger_request * lreq,int result)3057922dab61SIlya Dryomov static void linger_reg_commit_complete(struct ceph_osd_linger_request *lreq,
3058922dab61SIlya Dryomov 				       int result)
3059922dab61SIlya Dryomov {
3060922dab61SIlya Dryomov 	if (!completion_done(&lreq->reg_commit_wait)) {
3061922dab61SIlya Dryomov 		lreq->reg_commit_error = (result <= 0 ? result : 0);
3062922dab61SIlya Dryomov 		complete_all(&lreq->reg_commit_wait);
3063922dab61SIlya Dryomov 	}
3064922dab61SIlya Dryomov }
3065922dab61SIlya Dryomov 
linger_commit_cb(struct ceph_osd_request * req)3066922dab61SIlya Dryomov static void linger_commit_cb(struct ceph_osd_request *req)
3067922dab61SIlya Dryomov {
3068922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
3069922dab61SIlya Dryomov 
3070922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
307175dbb685SIlya Dryomov 	if (req != lreq->reg_req) {
307275dbb685SIlya Dryomov 		dout("%s lreq %p linger_id %llu unknown req (%p != %p)\n",
307375dbb685SIlya Dryomov 		     __func__, lreq, lreq->linger_id, req, lreq->reg_req);
307475dbb685SIlya Dryomov 		goto out;
307575dbb685SIlya Dryomov 	}
307675dbb685SIlya Dryomov 
3077922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu result %d\n", __func__, lreq,
3078922dab61SIlya Dryomov 	     lreq->linger_id, req->r_result);
3079922dab61SIlya Dryomov 	linger_reg_commit_complete(lreq, req->r_result);
3080922dab61SIlya Dryomov 	lreq->committed = true;
3081922dab61SIlya Dryomov 
308219079203SIlya Dryomov 	if (!lreq->is_watch) {
308319079203SIlya Dryomov 		struct ceph_osd_data *osd_data =
308419079203SIlya Dryomov 		    osd_req_op_data(req, 0, notify, response_data);
308519079203SIlya Dryomov 		void *p = page_address(osd_data->pages[0]);
308619079203SIlya Dryomov 
308719079203SIlya Dryomov 		WARN_ON(req->r_ops[0].op != CEPH_OSD_OP_NOTIFY ||
308819079203SIlya Dryomov 			osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
308919079203SIlya Dryomov 
309019079203SIlya Dryomov 		/* make note of the notify_id */
309119079203SIlya Dryomov 		if (req->r_ops[0].outdata_len >= sizeof(u64)) {
309219079203SIlya Dryomov 			lreq->notify_id = ceph_decode_64(&p);
309319079203SIlya Dryomov 			dout("lreq %p notify_id %llu\n", lreq,
309419079203SIlya Dryomov 			     lreq->notify_id);
309519079203SIlya Dryomov 		} else {
309619079203SIlya Dryomov 			dout("lreq %p no notify_id\n", lreq);
309719079203SIlya Dryomov 		}
309819079203SIlya Dryomov 	}
309919079203SIlya Dryomov 
310075dbb685SIlya Dryomov out:
3101922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
3102922dab61SIlya Dryomov 	linger_put(lreq);
3103922dab61SIlya Dryomov }
3104922dab61SIlya Dryomov 
normalize_watch_error(int err)3105922dab61SIlya Dryomov static int normalize_watch_error(int err)
3106922dab61SIlya Dryomov {
3107922dab61SIlya Dryomov 	/*
3108922dab61SIlya Dryomov 	 * Translate ENOENT -> ENOTCONN so that a delete->disconnection
3109922dab61SIlya Dryomov 	 * notification and a failure to reconnect because we raced with
3110922dab61SIlya Dryomov 	 * the delete appear the same to the user.
3111922dab61SIlya Dryomov 	 */
3112922dab61SIlya Dryomov 	if (err == -ENOENT)
3113922dab61SIlya Dryomov 		err = -ENOTCONN;
3114922dab61SIlya Dryomov 
3115922dab61SIlya Dryomov 	return err;
3116922dab61SIlya Dryomov }
3117922dab61SIlya Dryomov 
linger_reconnect_cb(struct ceph_osd_request * req)3118922dab61SIlya Dryomov static void linger_reconnect_cb(struct ceph_osd_request *req)
3119922dab61SIlya Dryomov {
3120922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
3121922dab61SIlya Dryomov 
3122922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
312375dbb685SIlya Dryomov 	if (req != lreq->reg_req) {
312475dbb685SIlya Dryomov 		dout("%s lreq %p linger_id %llu unknown req (%p != %p)\n",
312575dbb685SIlya Dryomov 		     __func__, lreq, lreq->linger_id, req, lreq->reg_req);
312675dbb685SIlya Dryomov 		goto out;
312775dbb685SIlya Dryomov 	}
312875dbb685SIlya Dryomov 
3129922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu result %d last_error %d\n", __func__,
3130922dab61SIlya Dryomov 	     lreq, lreq->linger_id, req->r_result, lreq->last_error);
3131922dab61SIlya Dryomov 	if (req->r_result < 0) {
3132922dab61SIlya Dryomov 		if (!lreq->last_error) {
3133922dab61SIlya Dryomov 			lreq->last_error = normalize_watch_error(req->r_result);
3134922dab61SIlya Dryomov 			queue_watch_error(lreq);
3135922dab61SIlya Dryomov 		}
3136922dab61SIlya Dryomov 	}
3137922dab61SIlya Dryomov 
313875dbb685SIlya Dryomov out:
3139922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
3140922dab61SIlya Dryomov 	linger_put(lreq);
3141922dab61SIlya Dryomov }
3142922dab61SIlya Dryomov 
send_linger(struct ceph_osd_linger_request * lreq)3143922dab61SIlya Dryomov static void send_linger(struct ceph_osd_linger_request *lreq)
3144922dab61SIlya Dryomov {
314575dbb685SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
314675dbb685SIlya Dryomov 	struct ceph_osd_request *req;
314775dbb685SIlya Dryomov 	int ret;
3148922dab61SIlya Dryomov 
314975dbb685SIlya Dryomov 	verify_osdc_wrlocked(osdc);
315075dbb685SIlya Dryomov 	mutex_lock(&lreq->lock);
3151922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3152922dab61SIlya Dryomov 
315375dbb685SIlya Dryomov 	if (lreq->reg_req) {
315475dbb685SIlya Dryomov 		if (lreq->reg_req->r_osd)
315575dbb685SIlya Dryomov 			cancel_linger_request(lreq->reg_req);
315675dbb685SIlya Dryomov 		ceph_osdc_put_request(lreq->reg_req);
315775dbb685SIlya Dryomov 	}
3158922dab61SIlya Dryomov 
315975dbb685SIlya Dryomov 	req = ceph_osdc_alloc_request(osdc, NULL, 1, true, GFP_NOIO);
316075dbb685SIlya Dryomov 	BUG_ON(!req);
316175dbb685SIlya Dryomov 
31625133ba8fSIlya Dryomov 	target_copy(&req->r_t, &lreq->t);
3163922dab61SIlya Dryomov 	req->r_mtime = lreq->mtime;
3164922dab61SIlya Dryomov 
316519079203SIlya Dryomov 	if (lreq->is_watch && lreq->committed) {
316675dbb685SIlya Dryomov 		osd_req_op_watch_init(req, 0, CEPH_OSD_WATCH_OP_RECONNECT,
316775dbb685SIlya Dryomov 				      lreq->linger_id, ++lreq->register_gen);
3168922dab61SIlya Dryomov 		dout("lreq %p reconnect register_gen %u\n", lreq,
316975dbb685SIlya Dryomov 		     req->r_ops[0].watch.gen);
3170922dab61SIlya Dryomov 		req->r_callback = linger_reconnect_cb;
3171922dab61SIlya Dryomov 	} else {
317275dbb685SIlya Dryomov 		if (lreq->is_watch) {
317375dbb685SIlya Dryomov 			osd_req_op_watch_init(req, 0, CEPH_OSD_WATCH_OP_WATCH,
317475dbb685SIlya Dryomov 					      lreq->linger_id, 0);
317575dbb685SIlya Dryomov 		} else {
317619079203SIlya Dryomov 			lreq->notify_id = 0;
317775dbb685SIlya Dryomov 
317875dbb685SIlya Dryomov 			refcount_inc(&lreq->request_pl->refcnt);
317975dbb685SIlya Dryomov 			osd_req_op_notify_init(req, 0, lreq->linger_id,
318075dbb685SIlya Dryomov 					       lreq->request_pl);
318175dbb685SIlya Dryomov 			ceph_osd_data_pages_init(
318275dbb685SIlya Dryomov 			    osd_req_op_data(req, 0, notify, response_data),
318375dbb685SIlya Dryomov 			    lreq->notify_id_pages, PAGE_SIZE, 0, false, false);
318475dbb685SIlya Dryomov 		}
3185922dab61SIlya Dryomov 		dout("lreq %p register\n", lreq);
3186922dab61SIlya Dryomov 		req->r_callback = linger_commit_cb;
3187922dab61SIlya Dryomov 	}
318875dbb685SIlya Dryomov 
318975dbb685SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
319075dbb685SIlya Dryomov 	BUG_ON(ret);
3191922dab61SIlya Dryomov 
3192922dab61SIlya Dryomov 	req->r_priv = linger_get(lreq);
3193922dab61SIlya Dryomov 	req->r_linger = true;
319475dbb685SIlya Dryomov 	lreq->reg_req = req;
319575dbb685SIlya Dryomov 	mutex_unlock(&lreq->lock);
3196922dab61SIlya Dryomov 
3197922dab61SIlya Dryomov 	submit_request(req, true);
3198922dab61SIlya Dryomov }
3199922dab61SIlya Dryomov 
linger_ping_cb(struct ceph_osd_request * req)3200922dab61SIlya Dryomov static void linger_ping_cb(struct ceph_osd_request *req)
3201922dab61SIlya Dryomov {
3202922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
3203922dab61SIlya Dryomov 
3204922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
320575dbb685SIlya Dryomov 	if (req != lreq->ping_req) {
320675dbb685SIlya Dryomov 		dout("%s lreq %p linger_id %llu unknown req (%p != %p)\n",
320775dbb685SIlya Dryomov 		     __func__, lreq, lreq->linger_id, req, lreq->ping_req);
320875dbb685SIlya Dryomov 		goto out;
320975dbb685SIlya Dryomov 	}
321075dbb685SIlya Dryomov 
3211922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu result %d ping_sent %lu last_error %d\n",
3212922dab61SIlya Dryomov 	     __func__, lreq, lreq->linger_id, req->r_result, lreq->ping_sent,
3213922dab61SIlya Dryomov 	     lreq->last_error);
3214922dab61SIlya Dryomov 	if (lreq->register_gen == req->r_ops[0].watch.gen) {
3215b07d3c4bSIlya Dryomov 		if (!req->r_result) {
3216b07d3c4bSIlya Dryomov 			lreq->watch_valid_thru = lreq->ping_sent;
3217b07d3c4bSIlya Dryomov 		} else if (!lreq->last_error) {
3218922dab61SIlya Dryomov 			lreq->last_error = normalize_watch_error(req->r_result);
3219922dab61SIlya Dryomov 			queue_watch_error(lreq);
3220922dab61SIlya Dryomov 		}
3221922dab61SIlya Dryomov 	} else {
3222922dab61SIlya Dryomov 		dout("lreq %p register_gen %u ignoring old pong %u\n", lreq,
3223922dab61SIlya Dryomov 		     lreq->register_gen, req->r_ops[0].watch.gen);
3224922dab61SIlya Dryomov 	}
3225922dab61SIlya Dryomov 
322675dbb685SIlya Dryomov out:
3227922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
3228922dab61SIlya Dryomov 	linger_put(lreq);
3229922dab61SIlya Dryomov }
3230922dab61SIlya Dryomov 
send_linger_ping(struct ceph_osd_linger_request * lreq)3231922dab61SIlya Dryomov static void send_linger_ping(struct ceph_osd_linger_request *lreq)
3232922dab61SIlya Dryomov {
3233922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
323475dbb685SIlya Dryomov 	struct ceph_osd_request *req;
323575dbb685SIlya Dryomov 	int ret;
3236922dab61SIlya Dryomov 
3237b7ec35b3SIlya Dryomov 	if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
3238922dab61SIlya Dryomov 		dout("%s PAUSERD\n", __func__);
3239922dab61SIlya Dryomov 		return;
3240922dab61SIlya Dryomov 	}
3241922dab61SIlya Dryomov 
3242922dab61SIlya Dryomov 	lreq->ping_sent = jiffies;
3243922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu ping_sent %lu register_gen %u\n",
3244922dab61SIlya Dryomov 	     __func__, lreq, lreq->linger_id, lreq->ping_sent,
3245922dab61SIlya Dryomov 	     lreq->register_gen);
3246922dab61SIlya Dryomov 
324775dbb685SIlya Dryomov 	if (lreq->ping_req) {
324875dbb685SIlya Dryomov 		if (lreq->ping_req->r_osd)
324975dbb685SIlya Dryomov 			cancel_linger_request(lreq->ping_req);
325075dbb685SIlya Dryomov 		ceph_osdc_put_request(lreq->ping_req);
325175dbb685SIlya Dryomov 	}
3252922dab61SIlya Dryomov 
325375dbb685SIlya Dryomov 	req = ceph_osdc_alloc_request(osdc, NULL, 1, true, GFP_NOIO);
325475dbb685SIlya Dryomov 	BUG_ON(!req);
325575dbb685SIlya Dryomov 
3256922dab61SIlya Dryomov 	target_copy(&req->r_t, &lreq->t);
325775dbb685SIlya Dryomov 	osd_req_op_watch_init(req, 0, CEPH_OSD_WATCH_OP_PING, lreq->linger_id,
325875dbb685SIlya Dryomov 			      lreq->register_gen);
3259922dab61SIlya Dryomov 	req->r_callback = linger_ping_cb;
326075dbb685SIlya Dryomov 
326175dbb685SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
326275dbb685SIlya Dryomov 	BUG_ON(ret);
326375dbb685SIlya Dryomov 
3264922dab61SIlya Dryomov 	req->r_priv = linger_get(lreq);
3265922dab61SIlya Dryomov 	req->r_linger = true;
326675dbb685SIlya Dryomov 	lreq->ping_req = req;
3267922dab61SIlya Dryomov 
3268922dab61SIlya Dryomov 	ceph_osdc_get_request(req);
3269922dab61SIlya Dryomov 	account_request(req);
3270922dab61SIlya Dryomov 	req->r_tid = atomic64_inc_return(&osdc->last_tid);
3271922dab61SIlya Dryomov 	link_request(lreq->osd, req);
3272922dab61SIlya Dryomov 	send_request(req);
3273922dab61SIlya Dryomov }
3274922dab61SIlya Dryomov 
linger_submit(struct ceph_osd_linger_request * lreq)3275922dab61SIlya Dryomov static void linger_submit(struct ceph_osd_linger_request *lreq)
3276922dab61SIlya Dryomov {
3277922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
3278922dab61SIlya Dryomov 	struct ceph_osd *osd;
3279922dab61SIlya Dryomov 
328081c65213SIlya Dryomov 	down_write(&osdc->lock);
328181c65213SIlya Dryomov 	linger_register(lreq);
328281c65213SIlya Dryomov 
32838edf84baSIlya Dryomov 	calc_target(osdc, &lreq->t, false);
3284922dab61SIlya Dryomov 	osd = lookup_create_osd(osdc, lreq->t.osd, true);
3285922dab61SIlya Dryomov 	link_linger(osd, lreq);
3286922dab61SIlya Dryomov 
3287922dab61SIlya Dryomov 	send_linger(lreq);
328881c65213SIlya Dryomov 	up_write(&osdc->lock);
3289922dab61SIlya Dryomov }
3290922dab61SIlya Dryomov 
cancel_linger_map_check(struct ceph_osd_linger_request * lreq)32914609245eSIlya Dryomov static void cancel_linger_map_check(struct ceph_osd_linger_request *lreq)
32924609245eSIlya Dryomov {
32934609245eSIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
32944609245eSIlya Dryomov 	struct ceph_osd_linger_request *lookup_lreq;
32954609245eSIlya Dryomov 
32964609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
32974609245eSIlya Dryomov 
32984609245eSIlya Dryomov 	lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
32994609245eSIlya Dryomov 				       lreq->linger_id);
33004609245eSIlya Dryomov 	if (!lookup_lreq)
33014609245eSIlya Dryomov 		return;
33024609245eSIlya Dryomov 
33034609245eSIlya Dryomov 	WARN_ON(lookup_lreq != lreq);
33044609245eSIlya Dryomov 	erase_linger_mc(&osdc->linger_map_checks, lreq);
33054609245eSIlya Dryomov 	linger_put(lreq);
33064609245eSIlya Dryomov }
33074609245eSIlya Dryomov 
3308922dab61SIlya Dryomov /*
3309922dab61SIlya Dryomov  * @lreq has to be both registered and linked.
3310922dab61SIlya Dryomov  */
__linger_cancel(struct ceph_osd_linger_request * lreq)3311922dab61SIlya Dryomov static void __linger_cancel(struct ceph_osd_linger_request *lreq)
3312922dab61SIlya Dryomov {
331375dbb685SIlya Dryomov 	if (lreq->ping_req && lreq->ping_req->r_osd)
3314922dab61SIlya Dryomov 		cancel_linger_request(lreq->ping_req);
331575dbb685SIlya Dryomov 	if (lreq->reg_req && lreq->reg_req->r_osd)
3316922dab61SIlya Dryomov 		cancel_linger_request(lreq->reg_req);
33174609245eSIlya Dryomov 	cancel_linger_map_check(lreq);
3318922dab61SIlya Dryomov 	unlink_linger(lreq->osd, lreq);
3319922dab61SIlya Dryomov 	linger_unregister(lreq);
3320922dab61SIlya Dryomov }
3321922dab61SIlya Dryomov 
linger_cancel(struct ceph_osd_linger_request * lreq)3322922dab61SIlya Dryomov static void linger_cancel(struct ceph_osd_linger_request *lreq)
3323922dab61SIlya Dryomov {
3324922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
3325922dab61SIlya Dryomov 
3326922dab61SIlya Dryomov 	down_write(&osdc->lock);
3327922dab61SIlya Dryomov 	if (__linger_registered(lreq))
3328922dab61SIlya Dryomov 		__linger_cancel(lreq);
3329922dab61SIlya Dryomov 	up_write(&osdc->lock);
3330922dab61SIlya Dryomov }
3331922dab61SIlya Dryomov 
33324609245eSIlya Dryomov static void send_linger_map_check(struct ceph_osd_linger_request *lreq);
33334609245eSIlya Dryomov 
check_linger_pool_dne(struct ceph_osd_linger_request * lreq)33344609245eSIlya Dryomov static void check_linger_pool_dne(struct ceph_osd_linger_request *lreq)
33354609245eSIlya Dryomov {
33364609245eSIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
33374609245eSIlya Dryomov 	struct ceph_osdmap *map = osdc->osdmap;
33384609245eSIlya Dryomov 
33394609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
33404609245eSIlya Dryomov 	WARN_ON(!map->epoch);
33414609245eSIlya Dryomov 
33424609245eSIlya Dryomov 	if (lreq->register_gen) {
33434609245eSIlya Dryomov 		lreq->map_dne_bound = map->epoch;
33444609245eSIlya Dryomov 		dout("%s lreq %p linger_id %llu pool disappeared\n", __func__,
33454609245eSIlya Dryomov 		     lreq, lreq->linger_id);
33464609245eSIlya Dryomov 	} else {
33474609245eSIlya Dryomov 		dout("%s lreq %p linger_id %llu map_dne_bound %u have %u\n",
33484609245eSIlya Dryomov 		     __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
33494609245eSIlya Dryomov 		     map->epoch);
33504609245eSIlya Dryomov 	}
33514609245eSIlya Dryomov 
33524609245eSIlya Dryomov 	if (lreq->map_dne_bound) {
33534609245eSIlya Dryomov 		if (map->epoch >= lreq->map_dne_bound) {
33544609245eSIlya Dryomov 			/* we had a new enough map */
33554609245eSIlya Dryomov 			pr_info("linger_id %llu pool does not exist\n",
33564609245eSIlya Dryomov 				lreq->linger_id);
33574609245eSIlya Dryomov 			linger_reg_commit_complete(lreq, -ENOENT);
33584609245eSIlya Dryomov 			__linger_cancel(lreq);
33594609245eSIlya Dryomov 		}
33604609245eSIlya Dryomov 	} else {
33614609245eSIlya Dryomov 		send_linger_map_check(lreq);
33624609245eSIlya Dryomov 	}
33634609245eSIlya Dryomov }
33644609245eSIlya Dryomov 
linger_map_check_cb(struct ceph_mon_generic_request * greq)33654609245eSIlya Dryomov static void linger_map_check_cb(struct ceph_mon_generic_request *greq)
33664609245eSIlya Dryomov {
33674609245eSIlya Dryomov 	struct ceph_osd_client *osdc = &greq->monc->client->osdc;
33684609245eSIlya Dryomov 	struct ceph_osd_linger_request *lreq;
33694609245eSIlya Dryomov 	u64 linger_id = greq->private_data;
33704609245eSIlya Dryomov 
33714609245eSIlya Dryomov 	WARN_ON(greq->result || !greq->u.newest);
33724609245eSIlya Dryomov 
33734609245eSIlya Dryomov 	down_write(&osdc->lock);
33744609245eSIlya Dryomov 	lreq = lookup_linger_mc(&osdc->linger_map_checks, linger_id);
33754609245eSIlya Dryomov 	if (!lreq) {
33764609245eSIlya Dryomov 		dout("%s linger_id %llu dne\n", __func__, linger_id);
33774609245eSIlya Dryomov 		goto out_unlock;
33784609245eSIlya Dryomov 	}
33794609245eSIlya Dryomov 
33804609245eSIlya Dryomov 	dout("%s lreq %p linger_id %llu map_dne_bound %u newest %llu\n",
33814609245eSIlya Dryomov 	     __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
33824609245eSIlya Dryomov 	     greq->u.newest);
33834609245eSIlya Dryomov 	if (!lreq->map_dne_bound)
33844609245eSIlya Dryomov 		lreq->map_dne_bound = greq->u.newest;
33854609245eSIlya Dryomov 	erase_linger_mc(&osdc->linger_map_checks, lreq);
33864609245eSIlya Dryomov 	check_linger_pool_dne(lreq);
33874609245eSIlya Dryomov 
33884609245eSIlya Dryomov 	linger_put(lreq);
33894609245eSIlya Dryomov out_unlock:
33904609245eSIlya Dryomov 	up_write(&osdc->lock);
33914609245eSIlya Dryomov }
33924609245eSIlya Dryomov 
send_linger_map_check(struct ceph_osd_linger_request * lreq)33934609245eSIlya Dryomov static void send_linger_map_check(struct ceph_osd_linger_request *lreq)
33944609245eSIlya Dryomov {
33954609245eSIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
33964609245eSIlya Dryomov 	struct ceph_osd_linger_request *lookup_lreq;
33974609245eSIlya Dryomov 	int ret;
33984609245eSIlya Dryomov 
33994609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
34004609245eSIlya Dryomov 
34014609245eSIlya Dryomov 	lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
34024609245eSIlya Dryomov 				       lreq->linger_id);
34034609245eSIlya Dryomov 	if (lookup_lreq) {
34044609245eSIlya Dryomov 		WARN_ON(lookup_lreq != lreq);
34054609245eSIlya Dryomov 		return;
34064609245eSIlya Dryomov 	}
34074609245eSIlya Dryomov 
34084609245eSIlya Dryomov 	linger_get(lreq);
34094609245eSIlya Dryomov 	insert_linger_mc(&osdc->linger_map_checks, lreq);
34104609245eSIlya Dryomov 	ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
34114609245eSIlya Dryomov 					  linger_map_check_cb, lreq->linger_id);
34124609245eSIlya Dryomov 	WARN_ON(ret);
34134609245eSIlya Dryomov }
34144609245eSIlya Dryomov 
linger_reg_commit_wait(struct ceph_osd_linger_request * lreq)3415922dab61SIlya Dryomov static int linger_reg_commit_wait(struct ceph_osd_linger_request *lreq)
3416922dab61SIlya Dryomov {
3417922dab61SIlya Dryomov 	int ret;
3418922dab61SIlya Dryomov 
3419922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3420e6e28432SIlya Dryomov 	ret = wait_for_completion_killable(&lreq->reg_commit_wait);
3421922dab61SIlya Dryomov 	return ret ?: lreq->reg_commit_error;
3422922dab61SIlya Dryomov }
3423922dab61SIlya Dryomov 
linger_notify_finish_wait(struct ceph_osd_linger_request * lreq,unsigned long timeout)3424e6e28432SIlya Dryomov static int linger_notify_finish_wait(struct ceph_osd_linger_request *lreq,
3425e6e28432SIlya Dryomov 				     unsigned long timeout)
342619079203SIlya Dryomov {
3427e6e28432SIlya Dryomov 	long left;
342819079203SIlya Dryomov 
342919079203SIlya Dryomov 	dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3430e6e28432SIlya Dryomov 	left = wait_for_completion_killable_timeout(&lreq->notify_finish_wait,
3431e6e28432SIlya Dryomov 						ceph_timeout_jiffies(timeout));
3432e6e28432SIlya Dryomov 	if (left <= 0)
3433e6e28432SIlya Dryomov 		left = left ?: -ETIMEDOUT;
3434e6e28432SIlya Dryomov 	else
3435e6e28432SIlya Dryomov 		left = lreq->notify_finish_error; /* completed */
3436e6e28432SIlya Dryomov 
3437e6e28432SIlya Dryomov 	return left;
343819079203SIlya Dryomov }
343919079203SIlya Dryomov 
3440922dab61SIlya Dryomov /*
3441fbca9635SIlya Dryomov  * Timeout callback, called every N seconds.  When 1 or more OSD
3442fbca9635SIlya Dryomov  * requests has been active for more than N seconds, we send a keepalive
3443fbca9635SIlya Dryomov  * (tag + timestamp) to its OSD to ensure any communications channel
3444fbca9635SIlya Dryomov  * reset is detected.
34453d14c5d2SYehuda Sadeh  */
handle_timeout(struct work_struct * work)34463d14c5d2SYehuda Sadeh static void handle_timeout(struct work_struct *work)
34473d14c5d2SYehuda Sadeh {
34483d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc =
34493d14c5d2SYehuda Sadeh 		container_of(work, struct ceph_osd_client, timeout_work.work);
3450a319bf56SIlya Dryomov 	struct ceph_options *opts = osdc->client->options;
34515aea3dcdSIlya Dryomov 	unsigned long cutoff = jiffies - opts->osd_keepalive_timeout;
34527cc5e38fSIlya Dryomov 	unsigned long expiry_cutoff = jiffies - opts->osd_request_timeout;
34535aea3dcdSIlya Dryomov 	LIST_HEAD(slow_osds);
34545aea3dcdSIlya Dryomov 	struct rb_node *n, *p;
34553d14c5d2SYehuda Sadeh 
34565aea3dcdSIlya Dryomov 	dout("%s osdc %p\n", __func__, osdc);
34575aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
34583d14c5d2SYehuda Sadeh 
34593d14c5d2SYehuda Sadeh 	/*
34603d14c5d2SYehuda Sadeh 	 * ping osds that are a bit slow.  this ensures that if there
34613d14c5d2SYehuda Sadeh 	 * is a break in the TCP connection we will notice, and reopen
34623d14c5d2SYehuda Sadeh 	 * a connection with that osd (from the fault callback).
34633d14c5d2SYehuda Sadeh 	 */
34645aea3dcdSIlya Dryomov 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
34655aea3dcdSIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
34665aea3dcdSIlya Dryomov 		bool found = false;
34673d14c5d2SYehuda Sadeh 
34687cc5e38fSIlya Dryomov 		for (p = rb_first(&osd->o_requests); p; ) {
34695aea3dcdSIlya Dryomov 			struct ceph_osd_request *req =
34705aea3dcdSIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
34715aea3dcdSIlya Dryomov 
34727cc5e38fSIlya Dryomov 			p = rb_next(p); /* abort_request() */
34737cc5e38fSIlya Dryomov 
34745aea3dcdSIlya Dryomov 			if (time_before(req->r_stamp, cutoff)) {
34755aea3dcdSIlya Dryomov 				dout(" req %p tid %llu on osd%d is laggy\n",
34765aea3dcdSIlya Dryomov 				     req, req->r_tid, osd->o_osd);
34775aea3dcdSIlya Dryomov 				found = true;
34785aea3dcdSIlya Dryomov 			}
34797cc5e38fSIlya Dryomov 			if (opts->osd_request_timeout &&
34807cc5e38fSIlya Dryomov 			    time_before(req->r_start_stamp, expiry_cutoff)) {
34817cc5e38fSIlya Dryomov 				pr_err_ratelimited("tid %llu on osd%d timeout\n",
34827cc5e38fSIlya Dryomov 				       req->r_tid, osd->o_osd);
34837cc5e38fSIlya Dryomov 				abort_request(req, -ETIMEDOUT);
34847cc5e38fSIlya Dryomov 			}
34855aea3dcdSIlya Dryomov 		}
3486922dab61SIlya Dryomov 		for (p = rb_first(&osd->o_linger_requests); p; p = rb_next(p)) {
3487922dab61SIlya Dryomov 			struct ceph_osd_linger_request *lreq =
3488922dab61SIlya Dryomov 			    rb_entry(p, struct ceph_osd_linger_request, node);
3489922dab61SIlya Dryomov 
3490922dab61SIlya Dryomov 			dout(" lreq %p linger_id %llu is served by osd%d\n",
3491922dab61SIlya Dryomov 			     lreq, lreq->linger_id, osd->o_osd);
3492922dab61SIlya Dryomov 			found = true;
3493922dab61SIlya Dryomov 
3494922dab61SIlya Dryomov 			mutex_lock(&lreq->lock);
349519079203SIlya Dryomov 			if (lreq->is_watch && lreq->committed && !lreq->last_error)
3496922dab61SIlya Dryomov 				send_linger_ping(lreq);
3497922dab61SIlya Dryomov 			mutex_unlock(&lreq->lock);
3498922dab61SIlya Dryomov 		}
34995aea3dcdSIlya Dryomov 
35005aea3dcdSIlya Dryomov 		if (found)
35013d14c5d2SYehuda Sadeh 			list_move_tail(&osd->o_keepalive_item, &slow_osds);
35023d14c5d2SYehuda Sadeh 	}
35035aea3dcdSIlya Dryomov 
35047cc5e38fSIlya Dryomov 	if (opts->osd_request_timeout) {
35057cc5e38fSIlya Dryomov 		for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
35067cc5e38fSIlya Dryomov 			struct ceph_osd_request *req =
35077cc5e38fSIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
35087cc5e38fSIlya Dryomov 
35097cc5e38fSIlya Dryomov 			p = rb_next(p); /* abort_request() */
35107cc5e38fSIlya Dryomov 
35117cc5e38fSIlya Dryomov 			if (time_before(req->r_start_stamp, expiry_cutoff)) {
35127cc5e38fSIlya Dryomov 				pr_err_ratelimited("tid %llu on osd%d timeout\n",
35137cc5e38fSIlya Dryomov 				       req->r_tid, osdc->homeless_osd.o_osd);
35147cc5e38fSIlya Dryomov 				abort_request(req, -ETIMEDOUT);
35157cc5e38fSIlya Dryomov 			}
35167cc5e38fSIlya Dryomov 		}
35177cc5e38fSIlya Dryomov 	}
35187cc5e38fSIlya Dryomov 
35195aea3dcdSIlya Dryomov 	if (atomic_read(&osdc->num_homeless) || !list_empty(&slow_osds))
35205aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
35215aea3dcdSIlya Dryomov 
35223d14c5d2SYehuda Sadeh 	while (!list_empty(&slow_osds)) {
35235aea3dcdSIlya Dryomov 		struct ceph_osd *osd = list_first_entry(&slow_osds,
35245aea3dcdSIlya Dryomov 							struct ceph_osd,
35253d14c5d2SYehuda Sadeh 							o_keepalive_item);
35263d14c5d2SYehuda Sadeh 		list_del_init(&osd->o_keepalive_item);
35273d14c5d2SYehuda Sadeh 		ceph_con_keepalive(&osd->o_con);
35283d14c5d2SYehuda Sadeh 	}
35293d14c5d2SYehuda Sadeh 
35305aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
3531fbca9635SIlya Dryomov 	schedule_delayed_work(&osdc->timeout_work,
3532fbca9635SIlya Dryomov 			      osdc->client->options->osd_keepalive_timeout);
35333d14c5d2SYehuda Sadeh }
35343d14c5d2SYehuda Sadeh 
handle_osds_timeout(struct work_struct * work)35353d14c5d2SYehuda Sadeh static void handle_osds_timeout(struct work_struct *work)
35363d14c5d2SYehuda Sadeh {
35373d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc =
35383d14c5d2SYehuda Sadeh 		container_of(work, struct ceph_osd_client,
35393d14c5d2SYehuda Sadeh 			     osds_timeout_work.work);
3540a319bf56SIlya Dryomov 	unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
354142a2c09fSIlya Dryomov 	struct ceph_osd *osd, *nosd;
35423d14c5d2SYehuda Sadeh 
354342a2c09fSIlya Dryomov 	dout("%s osdc %p\n", __func__, osdc);
35445aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
354542a2c09fSIlya Dryomov 	list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
354642a2c09fSIlya Dryomov 		if (time_before(jiffies, osd->lru_ttl))
354742a2c09fSIlya Dryomov 			break;
354842a2c09fSIlya Dryomov 
35495aea3dcdSIlya Dryomov 		WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
3550922dab61SIlya Dryomov 		WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
35515aea3dcdSIlya Dryomov 		close_osd(osd);
355242a2c09fSIlya Dryomov 	}
355342a2c09fSIlya Dryomov 
35545aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
35553d14c5d2SYehuda Sadeh 	schedule_delayed_work(&osdc->osds_timeout_work,
35563d14c5d2SYehuda Sadeh 			      round_jiffies_relative(delay));
35573d14c5d2SYehuda Sadeh }
35583d14c5d2SYehuda Sadeh 
ceph_oloc_decode(void ** p,void * end,struct ceph_object_locator * oloc)3559205ee118SIlya Dryomov static int ceph_oloc_decode(void **p, void *end,
3560205ee118SIlya Dryomov 			    struct ceph_object_locator *oloc)
3561205ee118SIlya Dryomov {
3562205ee118SIlya Dryomov 	u8 struct_v, struct_cv;
3563205ee118SIlya Dryomov 	u32 len;
3564205ee118SIlya Dryomov 	void *struct_end;
3565205ee118SIlya Dryomov 	int ret = 0;
3566205ee118SIlya Dryomov 
3567205ee118SIlya Dryomov 	ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3568205ee118SIlya Dryomov 	struct_v = ceph_decode_8(p);
3569205ee118SIlya Dryomov 	struct_cv = ceph_decode_8(p);
3570205ee118SIlya Dryomov 	if (struct_v < 3) {
3571205ee118SIlya Dryomov 		pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
3572205ee118SIlya Dryomov 			struct_v, struct_cv);
3573205ee118SIlya Dryomov 		goto e_inval;
3574205ee118SIlya Dryomov 	}
3575205ee118SIlya Dryomov 	if (struct_cv > 6) {
3576205ee118SIlya Dryomov 		pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
3577205ee118SIlya Dryomov 			struct_v, struct_cv);
3578205ee118SIlya Dryomov 		goto e_inval;
3579205ee118SIlya Dryomov 	}
3580205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3581205ee118SIlya Dryomov 	ceph_decode_need(p, end, len, e_inval);
3582205ee118SIlya Dryomov 	struct_end = *p + len;
3583205ee118SIlya Dryomov 
3584205ee118SIlya Dryomov 	oloc->pool = ceph_decode_64(p);
3585205ee118SIlya Dryomov 	*p += 4; /* skip preferred */
3586205ee118SIlya Dryomov 
3587205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3588205ee118SIlya Dryomov 	if (len > 0) {
3589205ee118SIlya Dryomov 		pr_warn("ceph_object_locator::key is set\n");
3590205ee118SIlya Dryomov 		goto e_inval;
3591205ee118SIlya Dryomov 	}
3592205ee118SIlya Dryomov 
3593205ee118SIlya Dryomov 	if (struct_v >= 5) {
3594cd08e0a2SYan, Zheng 		bool changed = false;
3595cd08e0a2SYan, Zheng 
3596205ee118SIlya Dryomov 		len = ceph_decode_32(p);
3597205ee118SIlya Dryomov 		if (len > 0) {
359830c156d9SYan, Zheng 			ceph_decode_need(p, end, len, e_inval);
3599cd08e0a2SYan, Zheng 			if (!oloc->pool_ns ||
3600cd08e0a2SYan, Zheng 			    ceph_compare_string(oloc->pool_ns, *p, len))
3601cd08e0a2SYan, Zheng 				changed = true;
360230c156d9SYan, Zheng 			*p += len;
3603cd08e0a2SYan, Zheng 		} else {
3604cd08e0a2SYan, Zheng 			if (oloc->pool_ns)
3605cd08e0a2SYan, Zheng 				changed = true;
3606cd08e0a2SYan, Zheng 		}
3607cd08e0a2SYan, Zheng 		if (changed) {
3608cd08e0a2SYan, Zheng 			/* redirect changes namespace */
3609cd08e0a2SYan, Zheng 			pr_warn("ceph_object_locator::nspace is changed\n");
3610cd08e0a2SYan, Zheng 			goto e_inval;
3611205ee118SIlya Dryomov 		}
3612205ee118SIlya Dryomov 	}
3613205ee118SIlya Dryomov 
3614205ee118SIlya Dryomov 	if (struct_v >= 6) {
3615205ee118SIlya Dryomov 		s64 hash = ceph_decode_64(p);
3616205ee118SIlya Dryomov 		if (hash != -1) {
3617205ee118SIlya Dryomov 			pr_warn("ceph_object_locator::hash is set\n");
3618205ee118SIlya Dryomov 			goto e_inval;
3619205ee118SIlya Dryomov 		}
3620205ee118SIlya Dryomov 	}
3621205ee118SIlya Dryomov 
3622205ee118SIlya Dryomov 	/* skip the rest */
3623205ee118SIlya Dryomov 	*p = struct_end;
3624205ee118SIlya Dryomov out:
3625205ee118SIlya Dryomov 	return ret;
3626205ee118SIlya Dryomov 
3627205ee118SIlya Dryomov e_inval:
3628205ee118SIlya Dryomov 	ret = -EINVAL;
3629205ee118SIlya Dryomov 	goto out;
3630205ee118SIlya Dryomov }
3631205ee118SIlya Dryomov 
ceph_redirect_decode(void ** p,void * end,struct ceph_request_redirect * redir)3632205ee118SIlya Dryomov static int ceph_redirect_decode(void **p, void *end,
3633205ee118SIlya Dryomov 				struct ceph_request_redirect *redir)
3634205ee118SIlya Dryomov {
3635205ee118SIlya Dryomov 	u8 struct_v, struct_cv;
3636205ee118SIlya Dryomov 	u32 len;
3637205ee118SIlya Dryomov 	void *struct_end;
3638205ee118SIlya Dryomov 	int ret;
3639205ee118SIlya Dryomov 
3640205ee118SIlya Dryomov 	ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3641205ee118SIlya Dryomov 	struct_v = ceph_decode_8(p);
3642205ee118SIlya Dryomov 	struct_cv = ceph_decode_8(p);
3643205ee118SIlya Dryomov 	if (struct_cv > 1) {
3644205ee118SIlya Dryomov 		pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
3645205ee118SIlya Dryomov 			struct_v, struct_cv);
3646205ee118SIlya Dryomov 		goto e_inval;
3647205ee118SIlya Dryomov 	}
3648205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3649205ee118SIlya Dryomov 	ceph_decode_need(p, end, len, e_inval);
3650205ee118SIlya Dryomov 	struct_end = *p + len;
3651205ee118SIlya Dryomov 
3652205ee118SIlya Dryomov 	ret = ceph_oloc_decode(p, end, &redir->oloc);
3653205ee118SIlya Dryomov 	if (ret)
3654205ee118SIlya Dryomov 		goto out;
3655205ee118SIlya Dryomov 
3656205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3657205ee118SIlya Dryomov 	if (len > 0) {
3658205ee118SIlya Dryomov 		pr_warn("ceph_request_redirect::object_name is set\n");
3659205ee118SIlya Dryomov 		goto e_inval;
3660205ee118SIlya Dryomov 	}
3661205ee118SIlya Dryomov 
3662205ee118SIlya Dryomov 	/* skip the rest */
3663205ee118SIlya Dryomov 	*p = struct_end;
3664205ee118SIlya Dryomov out:
3665205ee118SIlya Dryomov 	return ret;
3666205ee118SIlya Dryomov 
3667205ee118SIlya Dryomov e_inval:
3668205ee118SIlya Dryomov 	ret = -EINVAL;
3669205ee118SIlya Dryomov 	goto out;
3670205ee118SIlya Dryomov }
3671205ee118SIlya Dryomov 
3672fe5da05eSIlya Dryomov struct MOSDOpReply {
3673fe5da05eSIlya Dryomov 	struct ceph_pg pgid;
3674fe5da05eSIlya Dryomov 	u64 flags;
3675fe5da05eSIlya Dryomov 	int result;
3676fe5da05eSIlya Dryomov 	u32 epoch;
3677fe5da05eSIlya Dryomov 	int num_ops;
3678fe5da05eSIlya Dryomov 	u32 outdata_len[CEPH_OSD_MAX_OPS];
3679fe5da05eSIlya Dryomov 	s32 rval[CEPH_OSD_MAX_OPS];
3680fe5da05eSIlya Dryomov 	int retry_attempt;
3681fe5da05eSIlya Dryomov 	struct ceph_eversion replay_version;
3682fe5da05eSIlya Dryomov 	u64 user_version;
3683fe5da05eSIlya Dryomov 	struct ceph_request_redirect redirect;
3684fe5da05eSIlya Dryomov };
368525845472SSage Weil 
decode_MOSDOpReply(const struct ceph_msg * msg,struct MOSDOpReply * m)3686fe5da05eSIlya Dryomov static int decode_MOSDOpReply(const struct ceph_msg *msg, struct MOSDOpReply *m)
36873d14c5d2SYehuda Sadeh {
3688fe5da05eSIlya Dryomov 	void *p = msg->front.iov_base;
3689fe5da05eSIlya Dryomov 	void *const end = p + msg->front.iov_len;
3690fe5da05eSIlya Dryomov 	u16 version = le16_to_cpu(msg->hdr.version);
3691fe5da05eSIlya Dryomov 	struct ceph_eversion bad_replay_version;
3692b0b31a8fSIlya Dryomov 	u8 decode_redir;
3693fe5da05eSIlya Dryomov 	u32 len;
3694fe5da05eSIlya Dryomov 	int ret;
3695fe5da05eSIlya Dryomov 	int i;
36963d14c5d2SYehuda Sadeh 
3697fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, len, e_inval);
3698fe5da05eSIlya Dryomov 	ceph_decode_need(&p, end, len, e_inval);
3699fe5da05eSIlya Dryomov 	p += len; /* skip oid */
37001b83bef2SSage Weil 
3701fe5da05eSIlya Dryomov 	ret = ceph_decode_pgid(&p, end, &m->pgid);
3702fe5da05eSIlya Dryomov 	if (ret)
3703fe5da05eSIlya Dryomov 		return ret;
37041b83bef2SSage Weil 
3705fe5da05eSIlya Dryomov 	ceph_decode_64_safe(&p, end, m->flags, e_inval);
3706fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->result, e_inval);
3707fe5da05eSIlya Dryomov 	ceph_decode_need(&p, end, sizeof(bad_replay_version), e_inval);
3708fe5da05eSIlya Dryomov 	memcpy(&bad_replay_version, p, sizeof(bad_replay_version));
3709fe5da05eSIlya Dryomov 	p += sizeof(bad_replay_version);
3710fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->epoch, e_inval);
37111b83bef2SSage Weil 
3712fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->num_ops, e_inval);
3713fe5da05eSIlya Dryomov 	if (m->num_ops > ARRAY_SIZE(m->outdata_len))
3714fe5da05eSIlya Dryomov 		goto e_inval;
37151b83bef2SSage Weil 
3716fe5da05eSIlya Dryomov 	ceph_decode_need(&p, end, m->num_ops * sizeof(struct ceph_osd_op),
3717fe5da05eSIlya Dryomov 			 e_inval);
3718fe5da05eSIlya Dryomov 	for (i = 0; i < m->num_ops; i++) {
37191b83bef2SSage Weil 		struct ceph_osd_op *op = p;
37201b83bef2SSage Weil 
3721fe5da05eSIlya Dryomov 		m->outdata_len[i] = le32_to_cpu(op->payload_len);
37221b83bef2SSage Weil 		p += sizeof(*op);
37231b83bef2SSage Weil 	}
3724fe5da05eSIlya Dryomov 
3725fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->retry_attempt, e_inval);
3726fe5da05eSIlya Dryomov 	for (i = 0; i < m->num_ops; i++)
3727fe5da05eSIlya Dryomov 		ceph_decode_32_safe(&p, end, m->rval[i], e_inval);
3728fe5da05eSIlya Dryomov 
3729fe5da05eSIlya Dryomov 	if (version >= 5) {
3730fe5da05eSIlya Dryomov 		ceph_decode_need(&p, end, sizeof(m->replay_version), e_inval);
3731fe5da05eSIlya Dryomov 		memcpy(&m->replay_version, p, sizeof(m->replay_version));
3732fe5da05eSIlya Dryomov 		p += sizeof(m->replay_version);
3733fe5da05eSIlya Dryomov 		ceph_decode_64_safe(&p, end, m->user_version, e_inval);
3734fe5da05eSIlya Dryomov 	} else {
3735fe5da05eSIlya Dryomov 		m->replay_version = bad_replay_version; /* struct */
3736fe5da05eSIlya Dryomov 		m->user_version = le64_to_cpu(m->replay_version.version);
37371b83bef2SSage Weil 	}
37381b83bef2SSage Weil 
3739fe5da05eSIlya Dryomov 	if (version >= 6) {
3740fe5da05eSIlya Dryomov 		if (version >= 7)
3741fe5da05eSIlya Dryomov 			ceph_decode_8_safe(&p, end, decode_redir, e_inval);
3742b0b31a8fSIlya Dryomov 		else
3743b0b31a8fSIlya Dryomov 			decode_redir = 1;
3744b0b31a8fSIlya Dryomov 	} else {
3745b0b31a8fSIlya Dryomov 		decode_redir = 0;
3746b0b31a8fSIlya Dryomov 	}
3747b0b31a8fSIlya Dryomov 
3748b0b31a8fSIlya Dryomov 	if (decode_redir) {
3749fe5da05eSIlya Dryomov 		ret = ceph_redirect_decode(&p, end, &m->redirect);
3750fe5da05eSIlya Dryomov 		if (ret)
3751fe5da05eSIlya Dryomov 			return ret;
3752205ee118SIlya Dryomov 	} else {
3753fe5da05eSIlya Dryomov 		ceph_oloc_init(&m->redirect.oloc);
3754205ee118SIlya Dryomov 	}
3755205ee118SIlya Dryomov 
3756fe5da05eSIlya Dryomov 	return 0;
3757205ee118SIlya Dryomov 
3758fe5da05eSIlya Dryomov e_inval:
3759fe5da05eSIlya Dryomov 	return -EINVAL;
3760fe5da05eSIlya Dryomov }
3761fe5da05eSIlya Dryomov 
3762fe5da05eSIlya Dryomov /*
3763b18b9550SIlya Dryomov  * Handle MOSDOpReply.  Set ->r_result and call the callback if it is
3764b18b9550SIlya Dryomov  * specified.
3765fe5da05eSIlya Dryomov  */
handle_reply(struct ceph_osd * osd,struct ceph_msg * msg)37665aea3dcdSIlya Dryomov static void handle_reply(struct ceph_osd *osd, struct ceph_msg *msg)
3767fe5da05eSIlya Dryomov {
37685aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
3769fe5da05eSIlya Dryomov 	struct ceph_osd_request *req;
3770fe5da05eSIlya Dryomov 	struct MOSDOpReply m;
3771fe5da05eSIlya Dryomov 	u64 tid = le64_to_cpu(msg->hdr.tid);
3772fe5da05eSIlya Dryomov 	u32 data_len = 0;
3773fe5da05eSIlya Dryomov 	int ret;
3774fe5da05eSIlya Dryomov 	int i;
3775fe5da05eSIlya Dryomov 
3776fe5da05eSIlya Dryomov 	dout("%s msg %p tid %llu\n", __func__, msg, tid);
3777fe5da05eSIlya Dryomov 
37785aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
37795aea3dcdSIlya Dryomov 	if (!osd_registered(osd)) {
37805aea3dcdSIlya Dryomov 		dout("%s osd%d unknown\n", __func__, osd->o_osd);
37815aea3dcdSIlya Dryomov 		goto out_unlock_osdc;
3782fe5da05eSIlya Dryomov 	}
37835aea3dcdSIlya Dryomov 	WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
37845aea3dcdSIlya Dryomov 
37855aea3dcdSIlya Dryomov 	mutex_lock(&osd->lock);
37865aea3dcdSIlya Dryomov 	req = lookup_request(&osd->o_requests, tid);
37875aea3dcdSIlya Dryomov 	if (!req) {
37885aea3dcdSIlya Dryomov 		dout("%s osd%d tid %llu unknown\n", __func__, osd->o_osd, tid);
37895aea3dcdSIlya Dryomov 		goto out_unlock_session;
37905aea3dcdSIlya Dryomov 	}
3791fe5da05eSIlya Dryomov 
3792cd08e0a2SYan, Zheng 	m.redirect.oloc.pool_ns = req->r_t.target_oloc.pool_ns;
3793fe5da05eSIlya Dryomov 	ret = decode_MOSDOpReply(msg, &m);
3794cd08e0a2SYan, Zheng 	m.redirect.oloc.pool_ns = NULL;
3795fe5da05eSIlya Dryomov 	if (ret) {
3796fe5da05eSIlya Dryomov 		pr_err("failed to decode MOSDOpReply for tid %llu: %d\n",
3797fe5da05eSIlya Dryomov 		       req->r_tid, ret);
3798fe5da05eSIlya Dryomov 		ceph_msg_dump(msg);
3799fe5da05eSIlya Dryomov 		goto fail_request;
3800fe5da05eSIlya Dryomov 	}
3801fe5da05eSIlya Dryomov 	dout("%s req %p tid %llu flags 0x%llx pgid %llu.%x epoch %u attempt %d v %u'%llu uv %llu\n",
3802fe5da05eSIlya Dryomov 	     __func__, req, req->r_tid, m.flags, m.pgid.pool, m.pgid.seed,
3803fe5da05eSIlya Dryomov 	     m.epoch, m.retry_attempt, le32_to_cpu(m.replay_version.epoch),
3804fe5da05eSIlya Dryomov 	     le64_to_cpu(m.replay_version.version), m.user_version);
3805fe5da05eSIlya Dryomov 
3806fe5da05eSIlya Dryomov 	if (m.retry_attempt >= 0) {
3807fe5da05eSIlya Dryomov 		if (m.retry_attempt != req->r_attempts - 1) {
3808fe5da05eSIlya Dryomov 			dout("req %p tid %llu retry_attempt %d != %d, ignoring\n",
3809fe5da05eSIlya Dryomov 			     req, req->r_tid, m.retry_attempt,
3810fe5da05eSIlya Dryomov 			     req->r_attempts - 1);
38115aea3dcdSIlya Dryomov 			goto out_unlock_session;
3812fe5da05eSIlya Dryomov 		}
3813fe5da05eSIlya Dryomov 	} else {
3814fe5da05eSIlya Dryomov 		WARN_ON(1); /* MOSDOpReply v4 is assumed */
3815fe5da05eSIlya Dryomov 	}
3816fe5da05eSIlya Dryomov 
3817fe5da05eSIlya Dryomov 	if (!ceph_oloc_empty(&m.redirect.oloc)) {
3818fe5da05eSIlya Dryomov 		dout("req %p tid %llu redirect pool %lld\n", req, req->r_tid,
3819fe5da05eSIlya Dryomov 		     m.redirect.oloc.pool);
38205aea3dcdSIlya Dryomov 		unlink_request(osd, req);
38215aea3dcdSIlya Dryomov 		mutex_unlock(&osd->lock);
3822205ee118SIlya Dryomov 
382330c156d9SYan, Zheng 		/*
382430c156d9SYan, Zheng 		 * Not ceph_oloc_copy() - changing pool_ns is not
382530c156d9SYan, Zheng 		 * supported.
382630c156d9SYan, Zheng 		 */
382730c156d9SYan, Zheng 		req->r_t.target_oloc.pool = m.redirect.oloc.pool;
3828890bd0f8SJerry Lee 		req->r_flags |= CEPH_OSD_FLAG_REDIRECTED |
3829890bd0f8SJerry Lee 				CEPH_OSD_FLAG_IGNORE_OVERLAY |
3830890bd0f8SJerry Lee 				CEPH_OSD_FLAG_IGNORE_CACHE;
38315aea3dcdSIlya Dryomov 		req->r_tid = 0;
38325aea3dcdSIlya Dryomov 		__submit_request(req, false);
38335aea3dcdSIlya Dryomov 		goto out_unlock_osdc;
3834205ee118SIlya Dryomov 	}
3835205ee118SIlya Dryomov 
3836117d96a0SIlya Dryomov 	if (m.result == -EAGAIN) {
3837117d96a0SIlya Dryomov 		dout("req %p tid %llu EAGAIN\n", req, req->r_tid);
3838117d96a0SIlya Dryomov 		unlink_request(osd, req);
3839117d96a0SIlya Dryomov 		mutex_unlock(&osd->lock);
3840117d96a0SIlya Dryomov 
3841117d96a0SIlya Dryomov 		/*
3842117d96a0SIlya Dryomov 		 * The object is missing on the replica or not (yet)
3843117d96a0SIlya Dryomov 		 * readable.  Clear pgid to force a resend to the primary
3844117d96a0SIlya Dryomov 		 * via legacy_change.
3845117d96a0SIlya Dryomov 		 */
3846117d96a0SIlya Dryomov 		req->r_t.pgid.pool = 0;
3847117d96a0SIlya Dryomov 		req->r_t.pgid.seed = 0;
3848117d96a0SIlya Dryomov 		WARN_ON(!req->r_t.used_replica);
3849117d96a0SIlya Dryomov 		req->r_flags &= ~(CEPH_OSD_FLAG_BALANCE_READS |
3850117d96a0SIlya Dryomov 				  CEPH_OSD_FLAG_LOCALIZE_READS);
3851117d96a0SIlya Dryomov 		req->r_tid = 0;
3852117d96a0SIlya Dryomov 		__submit_request(req, false);
3853117d96a0SIlya Dryomov 		goto out_unlock_osdc;
3854117d96a0SIlya Dryomov 	}
3855117d96a0SIlya Dryomov 
3856fe5da05eSIlya Dryomov 	if (m.num_ops != req->r_num_ops) {
3857fe5da05eSIlya Dryomov 		pr_err("num_ops %d != %d for tid %llu\n", m.num_ops,
3858fe5da05eSIlya Dryomov 		       req->r_num_ops, req->r_tid);
3859fe5da05eSIlya Dryomov 		goto fail_request;
3860fe5da05eSIlya Dryomov 	}
3861fe5da05eSIlya Dryomov 	for (i = 0; i < req->r_num_ops; i++) {
3862fe5da05eSIlya Dryomov 		dout(" req %p tid %llu op %d rval %d len %u\n", req,
3863fe5da05eSIlya Dryomov 		     req->r_tid, i, m.rval[i], m.outdata_len[i]);
3864fe5da05eSIlya Dryomov 		req->r_ops[i].rval = m.rval[i];
3865fe5da05eSIlya Dryomov 		req->r_ops[i].outdata_len = m.outdata_len[i];
3866fe5da05eSIlya Dryomov 		data_len += m.outdata_len[i];
3867fe5da05eSIlya Dryomov 	}
3868fe5da05eSIlya Dryomov 	if (data_len != le32_to_cpu(msg->hdr.data_len)) {
3869fe5da05eSIlya Dryomov 		pr_err("sum of lens %u != %u for tid %llu\n", data_len,
3870fe5da05eSIlya Dryomov 		       le32_to_cpu(msg->hdr.data_len), req->r_tid);
3871fe5da05eSIlya Dryomov 		goto fail_request;
3872fe5da05eSIlya Dryomov 	}
3873b18b9550SIlya Dryomov 	dout("%s req %p tid %llu result %d data_len %u\n", __func__,
3874b18b9550SIlya Dryomov 	     req, req->r_tid, m.result, data_len);
38753d14c5d2SYehuda Sadeh 
3876b18b9550SIlya Dryomov 	/*
3877b18b9550SIlya Dryomov 	 * Since we only ever request ONDISK, we should only ever get
3878b18b9550SIlya Dryomov 	 * one (type of) reply back.
3879b18b9550SIlya Dryomov 	 */
3880b18b9550SIlya Dryomov 	WARN_ON(!(m.flags & CEPH_OSD_FLAG_ONDISK));
388169dd3b39SJeff Layton 	req->r_version = m.user_version;
3882fe5da05eSIlya Dryomov 	req->r_result = m.result ?: data_len;
388345ee2c1dSIlya Dryomov 	finish_request(req);
38845aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
38855aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
38863d14c5d2SYehuda Sadeh 
3887fe5da05eSIlya Dryomov 	__complete_request(req);
38883d14c5d2SYehuda Sadeh 	return;
3889fe5da05eSIlya Dryomov 
3890fe5da05eSIlya Dryomov fail_request:
38914609245eSIlya Dryomov 	complete_request(req, -EIO);
38925aea3dcdSIlya Dryomov out_unlock_session:
38935aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
38945aea3dcdSIlya Dryomov out_unlock_osdc:
38955aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
38963d14c5d2SYehuda Sadeh }
38973d14c5d2SYehuda Sadeh 
set_pool_was_full(struct ceph_osd_client * osdc)389842c1b124SIlya Dryomov static void set_pool_was_full(struct ceph_osd_client *osdc)
389942c1b124SIlya Dryomov {
390042c1b124SIlya Dryomov 	struct rb_node *n;
390142c1b124SIlya Dryomov 
390242c1b124SIlya Dryomov 	for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
390342c1b124SIlya Dryomov 		struct ceph_pg_pool_info *pi =
390442c1b124SIlya Dryomov 		    rb_entry(n, struct ceph_pg_pool_info, node);
390542c1b124SIlya Dryomov 
390642c1b124SIlya Dryomov 		pi->was_full = __pool_full(pi);
390742c1b124SIlya Dryomov 	}
390842c1b124SIlya Dryomov }
390942c1b124SIlya Dryomov 
pool_cleared_full(struct ceph_osd_client * osdc,s64 pool_id)39105aea3dcdSIlya Dryomov static bool pool_cleared_full(struct ceph_osd_client *osdc, s64 pool_id)
39113d14c5d2SYehuda Sadeh {
39125aea3dcdSIlya Dryomov 	struct ceph_pg_pool_info *pi;
39133d14c5d2SYehuda Sadeh 
39145aea3dcdSIlya Dryomov 	pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
39155aea3dcdSIlya Dryomov 	if (!pi)
39165aea3dcdSIlya Dryomov 		return false;
39173d14c5d2SYehuda Sadeh 
39185aea3dcdSIlya Dryomov 	return pi->was_full && !__pool_full(pi);
39193d14c5d2SYehuda Sadeh }
39203d14c5d2SYehuda Sadeh 
3921922dab61SIlya Dryomov static enum calc_target_result
recalc_linger_target(struct ceph_osd_linger_request * lreq)3922922dab61SIlya Dryomov recalc_linger_target(struct ceph_osd_linger_request *lreq)
3923922dab61SIlya Dryomov {
3924922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
3925922dab61SIlya Dryomov 	enum calc_target_result ct_res;
3926922dab61SIlya Dryomov 
39278edf84baSIlya Dryomov 	ct_res = calc_target(osdc, &lreq->t, true);
3928922dab61SIlya Dryomov 	if (ct_res == CALC_TARGET_NEED_RESEND) {
3929922dab61SIlya Dryomov 		struct ceph_osd *osd;
3930922dab61SIlya Dryomov 
3931922dab61SIlya Dryomov 		osd = lookup_create_osd(osdc, lreq->t.osd, true);
3932922dab61SIlya Dryomov 		if (osd != lreq->osd) {
3933922dab61SIlya Dryomov 			unlink_linger(lreq->osd, lreq);
3934922dab61SIlya Dryomov 			link_linger(osd, lreq);
3935922dab61SIlya Dryomov 		}
3936922dab61SIlya Dryomov 	}
3937922dab61SIlya Dryomov 
3938922dab61SIlya Dryomov 	return ct_res;
3939922dab61SIlya Dryomov }
3940922dab61SIlya Dryomov 
39413d14c5d2SYehuda Sadeh /*
39425aea3dcdSIlya Dryomov  * Requeue requests whose mapping to an OSD has changed.
39433d14c5d2SYehuda Sadeh  */
scan_requests(struct ceph_osd * osd,bool force_resend,bool cleared_full,bool check_pool_cleared_full,struct rb_root * need_resend,struct list_head * need_resend_linger)39445aea3dcdSIlya Dryomov static void scan_requests(struct ceph_osd *osd,
39455aea3dcdSIlya Dryomov 			  bool force_resend,
39465aea3dcdSIlya Dryomov 			  bool cleared_full,
39475aea3dcdSIlya Dryomov 			  bool check_pool_cleared_full,
39485aea3dcdSIlya Dryomov 			  struct rb_root *need_resend,
39495aea3dcdSIlya Dryomov 			  struct list_head *need_resend_linger)
39503d14c5d2SYehuda Sadeh {
39515aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
39525aea3dcdSIlya Dryomov 	struct rb_node *n;
39535aea3dcdSIlya Dryomov 	bool force_resend_writes;
39543d14c5d2SYehuda Sadeh 
3955922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_linger_requests); n; ) {
3956922dab61SIlya Dryomov 		struct ceph_osd_linger_request *lreq =
3957922dab61SIlya Dryomov 		    rb_entry(n, struct ceph_osd_linger_request, node);
3958922dab61SIlya Dryomov 		enum calc_target_result ct_res;
3959922dab61SIlya Dryomov 
3960922dab61SIlya Dryomov 		n = rb_next(n); /* recalc_linger_target() */
3961922dab61SIlya Dryomov 
3962922dab61SIlya Dryomov 		dout("%s lreq %p linger_id %llu\n", __func__, lreq,
3963922dab61SIlya Dryomov 		     lreq->linger_id);
3964922dab61SIlya Dryomov 		ct_res = recalc_linger_target(lreq);
3965922dab61SIlya Dryomov 		switch (ct_res) {
3966922dab61SIlya Dryomov 		case CALC_TARGET_NO_ACTION:
3967922dab61SIlya Dryomov 			force_resend_writes = cleared_full ||
3968922dab61SIlya Dryomov 			    (check_pool_cleared_full &&
3969922dab61SIlya Dryomov 			     pool_cleared_full(osdc, lreq->t.base_oloc.pool));
3970922dab61SIlya Dryomov 			if (!force_resend && !force_resend_writes)
3971922dab61SIlya Dryomov 				break;
3972922dab61SIlya Dryomov 
3973df561f66SGustavo A. R. Silva 			fallthrough;
3974922dab61SIlya Dryomov 		case CALC_TARGET_NEED_RESEND:
39754609245eSIlya Dryomov 			cancel_linger_map_check(lreq);
3976922dab61SIlya Dryomov 			/*
3977922dab61SIlya Dryomov 			 * scan_requests() for the previous epoch(s)
3978922dab61SIlya Dryomov 			 * may have already added it to the list, since
3979922dab61SIlya Dryomov 			 * it's not unlinked here.
3980922dab61SIlya Dryomov 			 */
3981922dab61SIlya Dryomov 			if (list_empty(&lreq->scan_item))
3982922dab61SIlya Dryomov 				list_add_tail(&lreq->scan_item, need_resend_linger);
3983922dab61SIlya Dryomov 			break;
3984922dab61SIlya Dryomov 		case CALC_TARGET_POOL_DNE:
3985a10bcb19SIlya Dryomov 			list_del_init(&lreq->scan_item);
39864609245eSIlya Dryomov 			check_linger_pool_dne(lreq);
3987922dab61SIlya Dryomov 			break;
3988922dab61SIlya Dryomov 		}
3989922dab61SIlya Dryomov 	}
3990922dab61SIlya Dryomov 
39915aea3dcdSIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; ) {
39925aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
39935aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
39945aea3dcdSIlya Dryomov 		enum calc_target_result ct_res;
3995ab60b16dSAlex Elder 
39964609245eSIlya Dryomov 		n = rb_next(n); /* unlink_request(), check_pool_dne() */
3997ab60b16dSAlex Elder 
39985aea3dcdSIlya Dryomov 		dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
39998edf84baSIlya Dryomov 		ct_res = calc_target(osdc, &req->r_t, false);
40005aea3dcdSIlya Dryomov 		switch (ct_res) {
40015aea3dcdSIlya Dryomov 		case CALC_TARGET_NO_ACTION:
40025aea3dcdSIlya Dryomov 			force_resend_writes = cleared_full ||
40035aea3dcdSIlya Dryomov 			    (check_pool_cleared_full &&
40045aea3dcdSIlya Dryomov 			     pool_cleared_full(osdc, req->r_t.base_oloc.pool));
40055aea3dcdSIlya Dryomov 			if (!force_resend &&
40065aea3dcdSIlya Dryomov 			    (!(req->r_flags & CEPH_OSD_FLAG_WRITE) ||
40075aea3dcdSIlya Dryomov 			     !force_resend_writes))
40085aea3dcdSIlya Dryomov 				break;
4009a40c4f10SYehuda Sadeh 
4010df561f66SGustavo A. R. Silva 			fallthrough;
40115aea3dcdSIlya Dryomov 		case CALC_TARGET_NEED_RESEND:
40124609245eSIlya Dryomov 			cancel_map_check(req);
40135aea3dcdSIlya Dryomov 			unlink_request(osd, req);
40145aea3dcdSIlya Dryomov 			insert_request(need_resend, req);
40155aea3dcdSIlya Dryomov 			break;
40165aea3dcdSIlya Dryomov 		case CALC_TARGET_POOL_DNE:
40174609245eSIlya Dryomov 			check_pool_dne(req);
40185aea3dcdSIlya Dryomov 			break;
4019a40c4f10SYehuda Sadeh 		}
40203d14c5d2SYehuda Sadeh 	}
40213d14c5d2SYehuda Sadeh }
40226f6c7006SSage Weil 
handle_one_map(struct ceph_osd_client * osdc,void * p,void * end,bool incremental,struct rb_root * need_resend,struct list_head * need_resend_linger)402342c1b124SIlya Dryomov static int handle_one_map(struct ceph_osd_client *osdc,
40245aea3dcdSIlya Dryomov 			  void *p, void *end, bool incremental,
40255aea3dcdSIlya Dryomov 			  struct rb_root *need_resend,
40265aea3dcdSIlya Dryomov 			  struct list_head *need_resend_linger)
402742c1b124SIlya Dryomov {
402842c1b124SIlya Dryomov 	struct ceph_osdmap *newmap;
402942c1b124SIlya Dryomov 	struct rb_node *n;
403042c1b124SIlya Dryomov 	bool skipped_map = false;
403142c1b124SIlya Dryomov 	bool was_full;
403242c1b124SIlya Dryomov 
4033b7ec35b3SIlya Dryomov 	was_full = ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
403442c1b124SIlya Dryomov 	set_pool_was_full(osdc);
403542c1b124SIlya Dryomov 
403642c1b124SIlya Dryomov 	if (incremental)
4037cd1a677cSIlya Dryomov 		newmap = osdmap_apply_incremental(&p, end,
4038cd1a677cSIlya Dryomov 						  ceph_msgr2(osdc->client),
4039cd1a677cSIlya Dryomov 						  osdc->osdmap);
404042c1b124SIlya Dryomov 	else
4041cd1a677cSIlya Dryomov 		newmap = ceph_osdmap_decode(&p, end, ceph_msgr2(osdc->client));
404242c1b124SIlya Dryomov 	if (IS_ERR(newmap))
404342c1b124SIlya Dryomov 		return PTR_ERR(newmap);
404442c1b124SIlya Dryomov 
404542c1b124SIlya Dryomov 	if (newmap != osdc->osdmap) {
404642c1b124SIlya Dryomov 		/*
404742c1b124SIlya Dryomov 		 * Preserve ->was_full before destroying the old map.
404842c1b124SIlya Dryomov 		 * For pools that weren't in the old map, ->was_full
404942c1b124SIlya Dryomov 		 * should be false.
405042c1b124SIlya Dryomov 		 */
405142c1b124SIlya Dryomov 		for (n = rb_first(&newmap->pg_pools); n; n = rb_next(n)) {
405242c1b124SIlya Dryomov 			struct ceph_pg_pool_info *pi =
405342c1b124SIlya Dryomov 			    rb_entry(n, struct ceph_pg_pool_info, node);
405442c1b124SIlya Dryomov 			struct ceph_pg_pool_info *old_pi;
405542c1b124SIlya Dryomov 
405642c1b124SIlya Dryomov 			old_pi = ceph_pg_pool_by_id(osdc->osdmap, pi->id);
405742c1b124SIlya Dryomov 			if (old_pi)
405842c1b124SIlya Dryomov 				pi->was_full = old_pi->was_full;
405942c1b124SIlya Dryomov 			else
406042c1b124SIlya Dryomov 				WARN_ON(pi->was_full);
406142c1b124SIlya Dryomov 		}
406242c1b124SIlya Dryomov 
406342c1b124SIlya Dryomov 		if (osdc->osdmap->epoch &&
406442c1b124SIlya Dryomov 		    osdc->osdmap->epoch + 1 < newmap->epoch) {
406542c1b124SIlya Dryomov 			WARN_ON(incremental);
406642c1b124SIlya Dryomov 			skipped_map = true;
406742c1b124SIlya Dryomov 		}
406842c1b124SIlya Dryomov 
406942c1b124SIlya Dryomov 		ceph_osdmap_destroy(osdc->osdmap);
407042c1b124SIlya Dryomov 		osdc->osdmap = newmap;
407142c1b124SIlya Dryomov 	}
407242c1b124SIlya Dryomov 
4073b7ec35b3SIlya Dryomov 	was_full &= !ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
40745aea3dcdSIlya Dryomov 	scan_requests(&osdc->homeless_osd, skipped_map, was_full, true,
40755aea3dcdSIlya Dryomov 		      need_resend, need_resend_linger);
40765aea3dcdSIlya Dryomov 
40775aea3dcdSIlya Dryomov 	for (n = rb_first(&osdc->osds); n; ) {
40785aea3dcdSIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
40795aea3dcdSIlya Dryomov 
40805aea3dcdSIlya Dryomov 		n = rb_next(n); /* close_osd() */
40815aea3dcdSIlya Dryomov 
40825aea3dcdSIlya Dryomov 		scan_requests(osd, skipped_map, was_full, true, need_resend,
40835aea3dcdSIlya Dryomov 			      need_resend_linger);
40845aea3dcdSIlya Dryomov 		if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
40855aea3dcdSIlya Dryomov 		    memcmp(&osd->o_con.peer_addr,
40865aea3dcdSIlya Dryomov 			   ceph_osd_addr(osdc->osdmap, osd->o_osd),
40875aea3dcdSIlya Dryomov 			   sizeof(struct ceph_entity_addr)))
40885aea3dcdSIlya Dryomov 			close_osd(osd);
40895aea3dcdSIlya Dryomov 	}
409042c1b124SIlya Dryomov 
409142c1b124SIlya Dryomov 	return 0;
409242c1b124SIlya Dryomov }
40936f6c7006SSage Weil 
kick_requests(struct ceph_osd_client * osdc,struct rb_root * need_resend,struct list_head * need_resend_linger)40945aea3dcdSIlya Dryomov static void kick_requests(struct ceph_osd_client *osdc,
40955aea3dcdSIlya Dryomov 			  struct rb_root *need_resend,
40965aea3dcdSIlya Dryomov 			  struct list_head *need_resend_linger)
40975aea3dcdSIlya Dryomov {
4098922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq, *nlreq;
409904c7d789SIlya Dryomov 	enum calc_target_result ct_res;
41005aea3dcdSIlya Dryomov 	struct rb_node *n;
41015aea3dcdSIlya Dryomov 
410204c7d789SIlya Dryomov 	/* make sure need_resend targets reflect latest map */
410304c7d789SIlya Dryomov 	for (n = rb_first(need_resend); n; ) {
410404c7d789SIlya Dryomov 		struct ceph_osd_request *req =
410504c7d789SIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
410604c7d789SIlya Dryomov 
410704c7d789SIlya Dryomov 		n = rb_next(n);
410804c7d789SIlya Dryomov 
410904c7d789SIlya Dryomov 		if (req->r_t.epoch < osdc->osdmap->epoch) {
41108edf84baSIlya Dryomov 			ct_res = calc_target(osdc, &req->r_t, false);
411104c7d789SIlya Dryomov 			if (ct_res == CALC_TARGET_POOL_DNE) {
411204c7d789SIlya Dryomov 				erase_request(need_resend, req);
411304c7d789SIlya Dryomov 				check_pool_dne(req);
411404c7d789SIlya Dryomov 			}
411504c7d789SIlya Dryomov 		}
411604c7d789SIlya Dryomov 	}
411704c7d789SIlya Dryomov 
41185aea3dcdSIlya Dryomov 	for (n = rb_first(need_resend); n; ) {
41195aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
41205aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
41215aea3dcdSIlya Dryomov 		struct ceph_osd *osd;
41225aea3dcdSIlya Dryomov 
41235aea3dcdSIlya Dryomov 		n = rb_next(n);
41245aea3dcdSIlya Dryomov 		erase_request(need_resend, req); /* before link_request() */
41255aea3dcdSIlya Dryomov 
41265aea3dcdSIlya Dryomov 		osd = lookup_create_osd(osdc, req->r_t.osd, true);
41275aea3dcdSIlya Dryomov 		link_request(osd, req);
41285aea3dcdSIlya Dryomov 		if (!req->r_linger) {
41295aea3dcdSIlya Dryomov 			if (!osd_homeless(osd) && !req->r_t.paused)
41305aea3dcdSIlya Dryomov 				send_request(req);
4131922dab61SIlya Dryomov 		} else {
4132922dab61SIlya Dryomov 			cancel_linger_request(req);
41335aea3dcdSIlya Dryomov 		}
41345aea3dcdSIlya Dryomov 	}
4135922dab61SIlya Dryomov 
4136922dab61SIlya Dryomov 	list_for_each_entry_safe(lreq, nlreq, need_resend_linger, scan_item) {
4137922dab61SIlya Dryomov 		if (!osd_homeless(lreq->osd))
4138922dab61SIlya Dryomov 			send_linger(lreq);
4139922dab61SIlya Dryomov 
4140922dab61SIlya Dryomov 		list_del_init(&lreq->scan_item);
4141922dab61SIlya Dryomov 	}
41425aea3dcdSIlya Dryomov }
41435aea3dcdSIlya Dryomov 
41443d14c5d2SYehuda Sadeh /*
41453d14c5d2SYehuda Sadeh  * Process updated osd map.
41463d14c5d2SYehuda Sadeh  *
41473d14c5d2SYehuda Sadeh  * The message contains any number of incremental and full maps, normally
41483d14c5d2SYehuda Sadeh  * indicating some sort of topology change in the cluster.  Kick requests
41493d14c5d2SYehuda Sadeh  * off to different OSDs as needed.
41503d14c5d2SYehuda Sadeh  */
ceph_osdc_handle_map(struct ceph_osd_client * osdc,struct ceph_msg * msg)41513d14c5d2SYehuda Sadeh void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
41523d14c5d2SYehuda Sadeh {
415342c1b124SIlya Dryomov 	void *p = msg->front.iov_base;
415442c1b124SIlya Dryomov 	void *const end = p + msg->front.iov_len;
41553d14c5d2SYehuda Sadeh 	u32 nr_maps, maplen;
41563d14c5d2SYehuda Sadeh 	u32 epoch;
41573d14c5d2SYehuda Sadeh 	struct ceph_fsid fsid;
41585aea3dcdSIlya Dryomov 	struct rb_root need_resend = RB_ROOT;
41595aea3dcdSIlya Dryomov 	LIST_HEAD(need_resend_linger);
416042c1b124SIlya Dryomov 	bool handled_incremental = false;
416142c1b124SIlya Dryomov 	bool was_pauserd, was_pausewr;
416242c1b124SIlya Dryomov 	bool pauserd, pausewr;
416342c1b124SIlya Dryomov 	int err;
41643d14c5d2SYehuda Sadeh 
416542c1b124SIlya Dryomov 	dout("%s have %u\n", __func__, osdc->osdmap->epoch);
41665aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
41673d14c5d2SYehuda Sadeh 
41683d14c5d2SYehuda Sadeh 	/* verify fsid */
41693d14c5d2SYehuda Sadeh 	ceph_decode_need(&p, end, sizeof(fsid), bad);
41703d14c5d2SYehuda Sadeh 	ceph_decode_copy(&p, &fsid, sizeof(fsid));
41713d14c5d2SYehuda Sadeh 	if (ceph_check_fsid(osdc->client, &fsid) < 0)
417242c1b124SIlya Dryomov 		goto bad;
41733d14c5d2SYehuda Sadeh 
4174b7ec35b3SIlya Dryomov 	was_pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
4175b7ec35b3SIlya Dryomov 	was_pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
4176b7ec35b3SIlya Dryomov 		      ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
417742c1b124SIlya Dryomov 		      have_pool_full(osdc);
41789a1ea2dbSJosh Durgin 
41793d14c5d2SYehuda Sadeh 	/* incremental maps */
41803d14c5d2SYehuda Sadeh 	ceph_decode_32_safe(&p, end, nr_maps, bad);
41813d14c5d2SYehuda Sadeh 	dout(" %d inc maps\n", nr_maps);
41823d14c5d2SYehuda Sadeh 	while (nr_maps > 0) {
41833d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, 2*sizeof(u32), bad);
41843d14c5d2SYehuda Sadeh 		epoch = ceph_decode_32(&p);
41853d14c5d2SYehuda Sadeh 		maplen = ceph_decode_32(&p);
41863d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, maplen, bad);
418742c1b124SIlya Dryomov 		if (osdc->osdmap->epoch &&
418842c1b124SIlya Dryomov 		    osdc->osdmap->epoch + 1 == epoch) {
41893d14c5d2SYehuda Sadeh 			dout("applying incremental map %u len %d\n",
41903d14c5d2SYehuda Sadeh 			     epoch, maplen);
41915aea3dcdSIlya Dryomov 			err = handle_one_map(osdc, p, p + maplen, true,
41925aea3dcdSIlya Dryomov 					     &need_resend, &need_resend_linger);
419342c1b124SIlya Dryomov 			if (err)
41943d14c5d2SYehuda Sadeh 				goto bad;
419542c1b124SIlya Dryomov 			handled_incremental = true;
41963d14c5d2SYehuda Sadeh 		} else {
41973d14c5d2SYehuda Sadeh 			dout("ignoring incremental map %u len %d\n",
41983d14c5d2SYehuda Sadeh 			     epoch, maplen);
41993d14c5d2SYehuda Sadeh 		}
420042c1b124SIlya Dryomov 		p += maplen;
42013d14c5d2SYehuda Sadeh 		nr_maps--;
42023d14c5d2SYehuda Sadeh 	}
420342c1b124SIlya Dryomov 	if (handled_incremental)
42043d14c5d2SYehuda Sadeh 		goto done;
42053d14c5d2SYehuda Sadeh 
42063d14c5d2SYehuda Sadeh 	/* full maps */
42073d14c5d2SYehuda Sadeh 	ceph_decode_32_safe(&p, end, nr_maps, bad);
42083d14c5d2SYehuda Sadeh 	dout(" %d full maps\n", nr_maps);
42093d14c5d2SYehuda Sadeh 	while (nr_maps) {
42103d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, 2*sizeof(u32), bad);
42113d14c5d2SYehuda Sadeh 		epoch = ceph_decode_32(&p);
42123d14c5d2SYehuda Sadeh 		maplen = ceph_decode_32(&p);
42133d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, maplen, bad);
42143d14c5d2SYehuda Sadeh 		if (nr_maps > 1) {
42153d14c5d2SYehuda Sadeh 			dout("skipping non-latest full map %u len %d\n",
42163d14c5d2SYehuda Sadeh 			     epoch, maplen);
4217e5253a7bSIlya Dryomov 		} else if (osdc->osdmap->epoch >= epoch) {
42183d14c5d2SYehuda Sadeh 			dout("skipping full map %u len %d, "
42193d14c5d2SYehuda Sadeh 			     "older than our %u\n", epoch, maplen,
42203d14c5d2SYehuda Sadeh 			     osdc->osdmap->epoch);
42213d14c5d2SYehuda Sadeh 		} else {
42223d14c5d2SYehuda Sadeh 			dout("taking full map %u len %d\n", epoch, maplen);
42235aea3dcdSIlya Dryomov 			err = handle_one_map(osdc, p, p + maplen, false,
42245aea3dcdSIlya Dryomov 					     &need_resend, &need_resend_linger);
422542c1b124SIlya Dryomov 			if (err)
42263d14c5d2SYehuda Sadeh 				goto bad;
42273d14c5d2SYehuda Sadeh 		}
42283d14c5d2SYehuda Sadeh 		p += maplen;
42293d14c5d2SYehuda Sadeh 		nr_maps--;
42303d14c5d2SYehuda Sadeh 	}
42313d14c5d2SYehuda Sadeh 
42323d14c5d2SYehuda Sadeh done:
4233cd634fb6SSage Weil 	/*
4234cd634fb6SSage Weil 	 * subscribe to subsequent osdmap updates if full to ensure
4235cd634fb6SSage Weil 	 * we find out when we are no longer full and stop returning
4236cd634fb6SSage Weil 	 * ENOSPC.
4237cd634fb6SSage Weil 	 */
4238b7ec35b3SIlya Dryomov 	pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
4239b7ec35b3SIlya Dryomov 	pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
4240b7ec35b3SIlya Dryomov 		  ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
424142c1b124SIlya Dryomov 		  have_pool_full(osdc);
424258eb7932SJeff Layton 	if (was_pauserd || was_pausewr || pauserd || pausewr ||
424358eb7932SJeff Layton 	    osdc->osdmap->epoch < osdc->epoch_barrier)
424442c1b124SIlya Dryomov 		maybe_request_map(osdc);
4245cd634fb6SSage Weil 
42465aea3dcdSIlya Dryomov 	kick_requests(osdc, &need_resend, &need_resend_linger);
424742c1b124SIlya Dryomov 
4248fc36d0a4SJeff Layton 	ceph_osdc_abort_on_full(osdc);
424942c1b124SIlya Dryomov 	ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
425042c1b124SIlya Dryomov 			  osdc->osdmap->epoch);
42515aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
42523d14c5d2SYehuda Sadeh 	wake_up_all(&osdc->client->auth_wq);
42533d14c5d2SYehuda Sadeh 	return;
42543d14c5d2SYehuda Sadeh 
42553d14c5d2SYehuda Sadeh bad:
42563d14c5d2SYehuda Sadeh 	pr_err("osdc handle_map corrupt msg\n");
42573d14c5d2SYehuda Sadeh 	ceph_msg_dump(msg);
42585aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
42595aea3dcdSIlya Dryomov }
42605aea3dcdSIlya Dryomov 
42615aea3dcdSIlya Dryomov /*
42625aea3dcdSIlya Dryomov  * Resubmit requests pending on the given osd.
42635aea3dcdSIlya Dryomov  */
kick_osd_requests(struct ceph_osd * osd)42645aea3dcdSIlya Dryomov static void kick_osd_requests(struct ceph_osd *osd)
42655aea3dcdSIlya Dryomov {
42665aea3dcdSIlya Dryomov 	struct rb_node *n;
42675aea3dcdSIlya Dryomov 
4268a02a946dSIlya Dryomov 	clear_backoffs(osd);
4269a02a946dSIlya Dryomov 
4270922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; ) {
42715aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
42725aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
42735aea3dcdSIlya Dryomov 
4274922dab61SIlya Dryomov 		n = rb_next(n); /* cancel_linger_request() */
4275922dab61SIlya Dryomov 
42765aea3dcdSIlya Dryomov 		if (!req->r_linger) {
42775aea3dcdSIlya Dryomov 			if (!req->r_t.paused)
42785aea3dcdSIlya Dryomov 				send_request(req);
4279922dab61SIlya Dryomov 		} else {
4280922dab61SIlya Dryomov 			cancel_linger_request(req);
42815aea3dcdSIlya Dryomov 		}
42825aea3dcdSIlya Dryomov 	}
4283922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_linger_requests); n; n = rb_next(n)) {
4284922dab61SIlya Dryomov 		struct ceph_osd_linger_request *lreq =
4285922dab61SIlya Dryomov 		    rb_entry(n, struct ceph_osd_linger_request, node);
4286922dab61SIlya Dryomov 
4287922dab61SIlya Dryomov 		send_linger(lreq);
4288922dab61SIlya Dryomov 	}
42895aea3dcdSIlya Dryomov }
42905aea3dcdSIlya Dryomov 
42915aea3dcdSIlya Dryomov /*
42925aea3dcdSIlya Dryomov  * If the osd connection drops, we need to resubmit all requests.
42935aea3dcdSIlya Dryomov  */
osd_fault(struct ceph_connection * con)42945aea3dcdSIlya Dryomov static void osd_fault(struct ceph_connection *con)
42955aea3dcdSIlya Dryomov {
42965aea3dcdSIlya Dryomov 	struct ceph_osd *osd = con->private;
42975aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
42985aea3dcdSIlya Dryomov 
42995aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
43005aea3dcdSIlya Dryomov 
43015aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
43025aea3dcdSIlya Dryomov 	if (!osd_registered(osd)) {
43035aea3dcdSIlya Dryomov 		dout("%s osd%d unknown\n", __func__, osd->o_osd);
43045aea3dcdSIlya Dryomov 		goto out_unlock;
43055aea3dcdSIlya Dryomov 	}
43065aea3dcdSIlya Dryomov 
43075aea3dcdSIlya Dryomov 	if (!reopen_osd(osd))
43085aea3dcdSIlya Dryomov 		kick_osd_requests(osd);
43095aea3dcdSIlya Dryomov 	maybe_request_map(osdc);
43105aea3dcdSIlya Dryomov 
43115aea3dcdSIlya Dryomov out_unlock:
43125aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
43133d14c5d2SYehuda Sadeh }
43143d14c5d2SYehuda Sadeh 
4315a02a946dSIlya Dryomov struct MOSDBackoff {
4316a02a946dSIlya Dryomov 	struct ceph_spg spgid;
4317a02a946dSIlya Dryomov 	u32 map_epoch;
4318a02a946dSIlya Dryomov 	u8 op;
4319a02a946dSIlya Dryomov 	u64 id;
4320a02a946dSIlya Dryomov 	struct ceph_hobject_id *begin;
4321a02a946dSIlya Dryomov 	struct ceph_hobject_id *end;
4322a02a946dSIlya Dryomov };
4323a02a946dSIlya Dryomov 
decode_MOSDBackoff(const struct ceph_msg * msg,struct MOSDBackoff * m)4324a02a946dSIlya Dryomov static int decode_MOSDBackoff(const struct ceph_msg *msg, struct MOSDBackoff *m)
4325a02a946dSIlya Dryomov {
4326a02a946dSIlya Dryomov 	void *p = msg->front.iov_base;
4327a02a946dSIlya Dryomov 	void *const end = p + msg->front.iov_len;
4328a02a946dSIlya Dryomov 	u8 struct_v;
4329a02a946dSIlya Dryomov 	u32 struct_len;
4330a02a946dSIlya Dryomov 	int ret;
4331a02a946dSIlya Dryomov 
4332a02a946dSIlya Dryomov 	ret = ceph_start_decoding(&p, end, 1, "spg_t", &struct_v, &struct_len);
4333a02a946dSIlya Dryomov 	if (ret)
4334a02a946dSIlya Dryomov 		return ret;
4335a02a946dSIlya Dryomov 
4336a02a946dSIlya Dryomov 	ret = ceph_decode_pgid(&p, end, &m->spgid.pgid);
4337a02a946dSIlya Dryomov 	if (ret)
4338a02a946dSIlya Dryomov 		return ret;
4339a02a946dSIlya Dryomov 
4340a02a946dSIlya Dryomov 	ceph_decode_8_safe(&p, end, m->spgid.shard, e_inval);
4341a02a946dSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->map_epoch, e_inval);
4342a02a946dSIlya Dryomov 	ceph_decode_8_safe(&p, end, m->op, e_inval);
4343a02a946dSIlya Dryomov 	ceph_decode_64_safe(&p, end, m->id, e_inval);
4344a02a946dSIlya Dryomov 
4345a02a946dSIlya Dryomov 	m->begin = kzalloc(sizeof(*m->begin), GFP_NOIO);
4346a02a946dSIlya Dryomov 	if (!m->begin)
4347a02a946dSIlya Dryomov 		return -ENOMEM;
4348a02a946dSIlya Dryomov 
4349a02a946dSIlya Dryomov 	ret = decode_hoid(&p, end, m->begin);
4350a02a946dSIlya Dryomov 	if (ret) {
4351a02a946dSIlya Dryomov 		free_hoid(m->begin);
4352a02a946dSIlya Dryomov 		return ret;
4353a02a946dSIlya Dryomov 	}
4354a02a946dSIlya Dryomov 
4355a02a946dSIlya Dryomov 	m->end = kzalloc(sizeof(*m->end), GFP_NOIO);
4356a02a946dSIlya Dryomov 	if (!m->end) {
4357a02a946dSIlya Dryomov 		free_hoid(m->begin);
4358a02a946dSIlya Dryomov 		return -ENOMEM;
4359a02a946dSIlya Dryomov 	}
4360a02a946dSIlya Dryomov 
4361a02a946dSIlya Dryomov 	ret = decode_hoid(&p, end, m->end);
4362a02a946dSIlya Dryomov 	if (ret) {
4363a02a946dSIlya Dryomov 		free_hoid(m->begin);
4364a02a946dSIlya Dryomov 		free_hoid(m->end);
4365a02a946dSIlya Dryomov 		return ret;
4366a02a946dSIlya Dryomov 	}
4367a02a946dSIlya Dryomov 
4368a02a946dSIlya Dryomov 	return 0;
4369a02a946dSIlya Dryomov 
4370a02a946dSIlya Dryomov e_inval:
4371a02a946dSIlya Dryomov 	return -EINVAL;
4372a02a946dSIlya Dryomov }
4373a02a946dSIlya Dryomov 
create_backoff_message(const struct ceph_osd_backoff * backoff,u32 map_epoch)4374a02a946dSIlya Dryomov static struct ceph_msg *create_backoff_message(
4375a02a946dSIlya Dryomov 				const struct ceph_osd_backoff *backoff,
4376a02a946dSIlya Dryomov 				u32 map_epoch)
4377a02a946dSIlya Dryomov {
4378a02a946dSIlya Dryomov 	struct ceph_msg *msg;
4379a02a946dSIlya Dryomov 	void *p, *end;
4380a02a946dSIlya Dryomov 	int msg_size;
4381a02a946dSIlya Dryomov 
4382a02a946dSIlya Dryomov 	msg_size = CEPH_ENCODING_START_BLK_LEN +
4383a02a946dSIlya Dryomov 			CEPH_PGID_ENCODING_LEN + 1; /* spgid */
4384a02a946dSIlya Dryomov 	msg_size += 4 + 1 + 8; /* map_epoch, op, id */
4385a02a946dSIlya Dryomov 	msg_size += CEPH_ENCODING_START_BLK_LEN +
4386a02a946dSIlya Dryomov 			hoid_encoding_size(backoff->begin);
4387a02a946dSIlya Dryomov 	msg_size += CEPH_ENCODING_START_BLK_LEN +
4388a02a946dSIlya Dryomov 			hoid_encoding_size(backoff->end);
4389a02a946dSIlya Dryomov 
4390a02a946dSIlya Dryomov 	msg = ceph_msg_new(CEPH_MSG_OSD_BACKOFF, msg_size, GFP_NOIO, true);
4391a02a946dSIlya Dryomov 	if (!msg)
4392a02a946dSIlya Dryomov 		return NULL;
4393a02a946dSIlya Dryomov 
4394a02a946dSIlya Dryomov 	p = msg->front.iov_base;
4395a02a946dSIlya Dryomov 	end = p + msg->front_alloc_len;
4396a02a946dSIlya Dryomov 
4397a02a946dSIlya Dryomov 	encode_spgid(&p, &backoff->spgid);
4398a02a946dSIlya Dryomov 	ceph_encode_32(&p, map_epoch);
4399a02a946dSIlya Dryomov 	ceph_encode_8(&p, CEPH_OSD_BACKOFF_OP_ACK_BLOCK);
4400a02a946dSIlya Dryomov 	ceph_encode_64(&p, backoff->id);
4401a02a946dSIlya Dryomov 	encode_hoid(&p, end, backoff->begin);
4402a02a946dSIlya Dryomov 	encode_hoid(&p, end, backoff->end);
4403a02a946dSIlya Dryomov 	BUG_ON(p != end);
4404a02a946dSIlya Dryomov 
4405a02a946dSIlya Dryomov 	msg->front.iov_len = p - msg->front.iov_base;
4406a02a946dSIlya Dryomov 	msg->hdr.version = cpu_to_le16(1); /* MOSDBackoff v1 */
4407a02a946dSIlya Dryomov 	msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
4408a02a946dSIlya Dryomov 
4409a02a946dSIlya Dryomov 	return msg;
4410a02a946dSIlya Dryomov }
4411a02a946dSIlya Dryomov 
handle_backoff_block(struct ceph_osd * osd,struct MOSDBackoff * m)4412a02a946dSIlya Dryomov static void handle_backoff_block(struct ceph_osd *osd, struct MOSDBackoff *m)
4413a02a946dSIlya Dryomov {
4414a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
4415a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
4416a02a946dSIlya Dryomov 	struct ceph_msg *msg;
4417a02a946dSIlya Dryomov 
4418a02a946dSIlya Dryomov 	dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4419a02a946dSIlya Dryomov 	     m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4420a02a946dSIlya Dryomov 
4421a02a946dSIlya Dryomov 	spg = lookup_spg_mapping(&osd->o_backoff_mappings, &m->spgid);
4422a02a946dSIlya Dryomov 	if (!spg) {
4423a02a946dSIlya Dryomov 		spg = alloc_spg_mapping();
4424a02a946dSIlya Dryomov 		if (!spg) {
4425a02a946dSIlya Dryomov 			pr_err("%s failed to allocate spg\n", __func__);
4426a02a946dSIlya Dryomov 			return;
4427a02a946dSIlya Dryomov 		}
4428a02a946dSIlya Dryomov 		spg->spgid = m->spgid; /* struct */
4429a02a946dSIlya Dryomov 		insert_spg_mapping(&osd->o_backoff_mappings, spg);
4430a02a946dSIlya Dryomov 	}
4431a02a946dSIlya Dryomov 
4432a02a946dSIlya Dryomov 	backoff = alloc_backoff();
4433a02a946dSIlya Dryomov 	if (!backoff) {
4434a02a946dSIlya Dryomov 		pr_err("%s failed to allocate backoff\n", __func__);
4435a02a946dSIlya Dryomov 		return;
4436a02a946dSIlya Dryomov 	}
4437a02a946dSIlya Dryomov 	backoff->spgid = m->spgid; /* struct */
4438a02a946dSIlya Dryomov 	backoff->id = m->id;
4439a02a946dSIlya Dryomov 	backoff->begin = m->begin;
4440a02a946dSIlya Dryomov 	m->begin = NULL; /* backoff now owns this */
4441a02a946dSIlya Dryomov 	backoff->end = m->end;
4442a02a946dSIlya Dryomov 	m->end = NULL;   /* ditto */
4443a02a946dSIlya Dryomov 
4444a02a946dSIlya Dryomov 	insert_backoff(&spg->backoffs, backoff);
4445a02a946dSIlya Dryomov 	insert_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4446a02a946dSIlya Dryomov 
4447a02a946dSIlya Dryomov 	/*
4448a02a946dSIlya Dryomov 	 * Ack with original backoff's epoch so that the OSD can
4449a02a946dSIlya Dryomov 	 * discard this if there was a PG split.
4450a02a946dSIlya Dryomov 	 */
4451a02a946dSIlya Dryomov 	msg = create_backoff_message(backoff, m->map_epoch);
4452a02a946dSIlya Dryomov 	if (!msg) {
4453a02a946dSIlya Dryomov 		pr_err("%s failed to allocate msg\n", __func__);
4454a02a946dSIlya Dryomov 		return;
4455a02a946dSIlya Dryomov 	}
4456a02a946dSIlya Dryomov 	ceph_con_send(&osd->o_con, msg);
4457a02a946dSIlya Dryomov }
4458a02a946dSIlya Dryomov 
target_contained_by(const struct ceph_osd_request_target * t,const struct ceph_hobject_id * begin,const struct ceph_hobject_id * end)4459a02a946dSIlya Dryomov static bool target_contained_by(const struct ceph_osd_request_target *t,
4460a02a946dSIlya Dryomov 				const struct ceph_hobject_id *begin,
4461a02a946dSIlya Dryomov 				const struct ceph_hobject_id *end)
4462a02a946dSIlya Dryomov {
4463a02a946dSIlya Dryomov 	struct ceph_hobject_id hoid;
4464a02a946dSIlya Dryomov 	int cmp;
4465a02a946dSIlya Dryomov 
4466a02a946dSIlya Dryomov 	hoid_fill_from_target(&hoid, t);
4467a02a946dSIlya Dryomov 	cmp = hoid_compare(&hoid, begin);
4468a02a946dSIlya Dryomov 	return !cmp || (cmp > 0 && hoid_compare(&hoid, end) < 0);
4469a02a946dSIlya Dryomov }
4470a02a946dSIlya Dryomov 
handle_backoff_unblock(struct ceph_osd * osd,const struct MOSDBackoff * m)4471a02a946dSIlya Dryomov static void handle_backoff_unblock(struct ceph_osd *osd,
4472a02a946dSIlya Dryomov 				   const struct MOSDBackoff *m)
4473a02a946dSIlya Dryomov {
4474a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
4475a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
4476a02a946dSIlya Dryomov 	struct rb_node *n;
4477a02a946dSIlya Dryomov 
4478a02a946dSIlya Dryomov 	dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4479a02a946dSIlya Dryomov 	     m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4480a02a946dSIlya Dryomov 
4481a02a946dSIlya Dryomov 	backoff = lookup_backoff_by_id(&osd->o_backoffs_by_id, m->id);
4482a02a946dSIlya Dryomov 	if (!backoff) {
4483a02a946dSIlya Dryomov 		pr_err("%s osd%d spgid %llu.%xs%d id %llu backoff dne\n",
4484a02a946dSIlya Dryomov 		       __func__, osd->o_osd, m->spgid.pgid.pool,
4485a02a946dSIlya Dryomov 		       m->spgid.pgid.seed, m->spgid.shard, m->id);
4486a02a946dSIlya Dryomov 		return;
4487a02a946dSIlya Dryomov 	}
4488a02a946dSIlya Dryomov 
4489a02a946dSIlya Dryomov 	if (hoid_compare(backoff->begin, m->begin) &&
4490a02a946dSIlya Dryomov 	    hoid_compare(backoff->end, m->end)) {
4491a02a946dSIlya Dryomov 		pr_err("%s osd%d spgid %llu.%xs%d id %llu bad range?\n",
4492a02a946dSIlya Dryomov 		       __func__, osd->o_osd, m->spgid.pgid.pool,
4493a02a946dSIlya Dryomov 		       m->spgid.pgid.seed, m->spgid.shard, m->id);
4494a02a946dSIlya Dryomov 		/* unblock it anyway... */
4495a02a946dSIlya Dryomov 	}
4496a02a946dSIlya Dryomov 
4497a02a946dSIlya Dryomov 	spg = lookup_spg_mapping(&osd->o_backoff_mappings, &backoff->spgid);
4498a02a946dSIlya Dryomov 	BUG_ON(!spg);
4499a02a946dSIlya Dryomov 
4500a02a946dSIlya Dryomov 	erase_backoff(&spg->backoffs, backoff);
4501a02a946dSIlya Dryomov 	erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4502a02a946dSIlya Dryomov 	free_backoff(backoff);
4503a02a946dSIlya Dryomov 
4504a02a946dSIlya Dryomov 	if (RB_EMPTY_ROOT(&spg->backoffs)) {
4505a02a946dSIlya Dryomov 		erase_spg_mapping(&osd->o_backoff_mappings, spg);
4506a02a946dSIlya Dryomov 		free_spg_mapping(spg);
4507a02a946dSIlya Dryomov 	}
4508a02a946dSIlya Dryomov 
4509a02a946dSIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
4510a02a946dSIlya Dryomov 		struct ceph_osd_request *req =
4511a02a946dSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
4512a02a946dSIlya Dryomov 
4513a02a946dSIlya Dryomov 		if (!ceph_spg_compare(&req->r_t.spgid, &m->spgid)) {
4514a02a946dSIlya Dryomov 			/*
4515a02a946dSIlya Dryomov 			 * Match against @m, not @backoff -- the PG may
4516a02a946dSIlya Dryomov 			 * have split on the OSD.
4517a02a946dSIlya Dryomov 			 */
4518a02a946dSIlya Dryomov 			if (target_contained_by(&req->r_t, m->begin, m->end)) {
4519a02a946dSIlya Dryomov 				/*
4520a02a946dSIlya Dryomov 				 * If no other installed backoff applies,
4521a02a946dSIlya Dryomov 				 * resend.
4522a02a946dSIlya Dryomov 				 */
4523a02a946dSIlya Dryomov 				send_request(req);
4524a02a946dSIlya Dryomov 			}
4525a02a946dSIlya Dryomov 		}
4526a02a946dSIlya Dryomov 	}
4527a02a946dSIlya Dryomov }
4528a02a946dSIlya Dryomov 
handle_backoff(struct ceph_osd * osd,struct ceph_msg * msg)4529a02a946dSIlya Dryomov static void handle_backoff(struct ceph_osd *osd, struct ceph_msg *msg)
4530a02a946dSIlya Dryomov {
4531a02a946dSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
4532a02a946dSIlya Dryomov 	struct MOSDBackoff m;
4533a02a946dSIlya Dryomov 	int ret;
4534a02a946dSIlya Dryomov 
4535a02a946dSIlya Dryomov 	down_read(&osdc->lock);
4536a02a946dSIlya Dryomov 	if (!osd_registered(osd)) {
4537a02a946dSIlya Dryomov 		dout("%s osd%d unknown\n", __func__, osd->o_osd);
4538a02a946dSIlya Dryomov 		up_read(&osdc->lock);
4539a02a946dSIlya Dryomov 		return;
4540a02a946dSIlya Dryomov 	}
4541a02a946dSIlya Dryomov 	WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
4542a02a946dSIlya Dryomov 
4543a02a946dSIlya Dryomov 	mutex_lock(&osd->lock);
4544a02a946dSIlya Dryomov 	ret = decode_MOSDBackoff(msg, &m);
4545a02a946dSIlya Dryomov 	if (ret) {
4546a02a946dSIlya Dryomov 		pr_err("failed to decode MOSDBackoff: %d\n", ret);
4547a02a946dSIlya Dryomov 		ceph_msg_dump(msg);
4548a02a946dSIlya Dryomov 		goto out_unlock;
4549a02a946dSIlya Dryomov 	}
4550a02a946dSIlya Dryomov 
4551a02a946dSIlya Dryomov 	switch (m.op) {
4552a02a946dSIlya Dryomov 	case CEPH_OSD_BACKOFF_OP_BLOCK:
4553a02a946dSIlya Dryomov 		handle_backoff_block(osd, &m);
4554a02a946dSIlya Dryomov 		break;
4555a02a946dSIlya Dryomov 	case CEPH_OSD_BACKOFF_OP_UNBLOCK:
4556a02a946dSIlya Dryomov 		handle_backoff_unblock(osd, &m);
4557a02a946dSIlya Dryomov 		break;
4558a02a946dSIlya Dryomov 	default:
4559a02a946dSIlya Dryomov 		pr_err("%s osd%d unknown op %d\n", __func__, osd->o_osd, m.op);
4560a02a946dSIlya Dryomov 	}
4561a02a946dSIlya Dryomov 
4562a02a946dSIlya Dryomov 	free_hoid(m.begin);
4563a02a946dSIlya Dryomov 	free_hoid(m.end);
4564a02a946dSIlya Dryomov 
4565a02a946dSIlya Dryomov out_unlock:
4566a02a946dSIlya Dryomov 	mutex_unlock(&osd->lock);
4567a02a946dSIlya Dryomov 	up_read(&osdc->lock);
4568a02a946dSIlya Dryomov }
4569a02a946dSIlya Dryomov 
45703d14c5d2SYehuda Sadeh /*
4571a40c4f10SYehuda Sadeh  * Process osd watch notifications
4572a40c4f10SYehuda Sadeh  */
handle_watch_notify(struct ceph_osd_client * osdc,struct ceph_msg * msg)45733c663bbdSAlex Elder static void handle_watch_notify(struct ceph_osd_client *osdc,
45743c663bbdSAlex Elder 				struct ceph_msg *msg)
4575a40c4f10SYehuda Sadeh {
4576922dab61SIlya Dryomov 	void *p = msg->front.iov_base;
4577922dab61SIlya Dryomov 	void *const end = p + msg->front.iov_len;
4578922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
4579922dab61SIlya Dryomov 	struct linger_work *lwork;
4580922dab61SIlya Dryomov 	u8 proto_ver, opcode;
4581922dab61SIlya Dryomov 	u64 cookie, notify_id;
4582922dab61SIlya Dryomov 	u64 notifier_id = 0;
458319079203SIlya Dryomov 	s32 return_code = 0;
4584922dab61SIlya Dryomov 	void *payload = NULL;
4585922dab61SIlya Dryomov 	u32 payload_len = 0;
4586a40c4f10SYehuda Sadeh 
4587a40c4f10SYehuda Sadeh 	ceph_decode_8_safe(&p, end, proto_ver, bad);
4588a40c4f10SYehuda Sadeh 	ceph_decode_8_safe(&p, end, opcode, bad);
4589a40c4f10SYehuda Sadeh 	ceph_decode_64_safe(&p, end, cookie, bad);
4590922dab61SIlya Dryomov 	p += 8; /* skip ver */
4591a40c4f10SYehuda Sadeh 	ceph_decode_64_safe(&p, end, notify_id, bad);
4592a40c4f10SYehuda Sadeh 
4593922dab61SIlya Dryomov 	if (proto_ver >= 1) {
4594922dab61SIlya Dryomov 		ceph_decode_32_safe(&p, end, payload_len, bad);
4595922dab61SIlya Dryomov 		ceph_decode_need(&p, end, payload_len, bad);
4596922dab61SIlya Dryomov 		payload = p;
4597922dab61SIlya Dryomov 		p += payload_len;
4598a40c4f10SYehuda Sadeh 	}
4599a40c4f10SYehuda Sadeh 
4600922dab61SIlya Dryomov 	if (le16_to_cpu(msg->hdr.version) >= 2)
460119079203SIlya Dryomov 		ceph_decode_32_safe(&p, end, return_code, bad);
4602922dab61SIlya Dryomov 
4603922dab61SIlya Dryomov 	if (le16_to_cpu(msg->hdr.version) >= 3)
4604922dab61SIlya Dryomov 		ceph_decode_64_safe(&p, end, notifier_id, bad);
4605922dab61SIlya Dryomov 
4606922dab61SIlya Dryomov 	down_read(&osdc->lock);
4607922dab61SIlya Dryomov 	lreq = lookup_linger_osdc(&osdc->linger_requests, cookie);
4608922dab61SIlya Dryomov 	if (!lreq) {
4609922dab61SIlya Dryomov 		dout("%s opcode %d cookie %llu dne\n", __func__, opcode,
4610922dab61SIlya Dryomov 		     cookie);
4611922dab61SIlya Dryomov 		goto out_unlock_osdc;
4612922dab61SIlya Dryomov 	}
4613922dab61SIlya Dryomov 
4614922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
461519079203SIlya Dryomov 	dout("%s opcode %d cookie %llu lreq %p is_watch %d\n", __func__,
461619079203SIlya Dryomov 	     opcode, cookie, lreq, lreq->is_watch);
4617922dab61SIlya Dryomov 	if (opcode == CEPH_WATCH_EVENT_DISCONNECT) {
4618922dab61SIlya Dryomov 		if (!lreq->last_error) {
4619922dab61SIlya Dryomov 			lreq->last_error = -ENOTCONN;
4620922dab61SIlya Dryomov 			queue_watch_error(lreq);
4621922dab61SIlya Dryomov 		}
462219079203SIlya Dryomov 	} else if (!lreq->is_watch) {
462319079203SIlya Dryomov 		/* CEPH_WATCH_EVENT_NOTIFY_COMPLETE */
462419079203SIlya Dryomov 		if (lreq->notify_id && lreq->notify_id != notify_id) {
462519079203SIlya Dryomov 			dout("lreq %p notify_id %llu != %llu, ignoring\n", lreq,
462619079203SIlya Dryomov 			     lreq->notify_id, notify_id);
462719079203SIlya Dryomov 		} else if (!completion_done(&lreq->notify_finish_wait)) {
462819079203SIlya Dryomov 			struct ceph_msg_data *data =
46290d9c1ab3SIlya Dryomov 			    msg->num_data_items ? &msg->data[0] : NULL;
463019079203SIlya Dryomov 
463119079203SIlya Dryomov 			if (data) {
463219079203SIlya Dryomov 				if (lreq->preply_pages) {
463319079203SIlya Dryomov 					WARN_ON(data->type !=
463419079203SIlya Dryomov 							CEPH_MSG_DATA_PAGES);
463519079203SIlya Dryomov 					*lreq->preply_pages = data->pages;
463619079203SIlya Dryomov 					*lreq->preply_len = data->length;
4637e8862740SIlya Dryomov 					data->own_pages = false;
463819079203SIlya Dryomov 				}
463919079203SIlya Dryomov 			}
464019079203SIlya Dryomov 			lreq->notify_finish_error = return_code;
464119079203SIlya Dryomov 			complete_all(&lreq->notify_finish_wait);
464219079203SIlya Dryomov 		}
4643922dab61SIlya Dryomov 	} else {
4644922dab61SIlya Dryomov 		/* CEPH_WATCH_EVENT_NOTIFY */
4645922dab61SIlya Dryomov 		lwork = lwork_alloc(lreq, do_watch_notify);
4646922dab61SIlya Dryomov 		if (!lwork) {
4647922dab61SIlya Dryomov 			pr_err("failed to allocate notify-lwork\n");
4648922dab61SIlya Dryomov 			goto out_unlock_lreq;
4649922dab61SIlya Dryomov 		}
4650922dab61SIlya Dryomov 
4651922dab61SIlya Dryomov 		lwork->notify.notify_id = notify_id;
4652922dab61SIlya Dryomov 		lwork->notify.notifier_id = notifier_id;
4653922dab61SIlya Dryomov 		lwork->notify.payload = payload;
4654922dab61SIlya Dryomov 		lwork->notify.payload_len = payload_len;
4655922dab61SIlya Dryomov 		lwork->notify.msg = ceph_msg_get(msg);
4656922dab61SIlya Dryomov 		lwork_queue(lwork);
4657922dab61SIlya Dryomov 	}
4658922dab61SIlya Dryomov 
4659922dab61SIlya Dryomov out_unlock_lreq:
4660922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
4661922dab61SIlya Dryomov out_unlock_osdc:
4662922dab61SIlya Dryomov 	up_read(&osdc->lock);
4663a40c4f10SYehuda Sadeh 	return;
4664a40c4f10SYehuda Sadeh 
4665a40c4f10SYehuda Sadeh bad:
4666a40c4f10SYehuda Sadeh 	pr_err("osdc handle_watch_notify corrupt msg\n");
4667a40c4f10SYehuda Sadeh }
4668a40c4f10SYehuda Sadeh 
4669a40c4f10SYehuda Sadeh /*
46703d14c5d2SYehuda Sadeh  * Register request, send initial attempt.
46713d14c5d2SYehuda Sadeh  */
ceph_osdc_start_request(struct ceph_osd_client * osdc,struct ceph_osd_request * req)4672a8af0d68SJeff Layton void ceph_osdc_start_request(struct ceph_osd_client *osdc,
4673a8af0d68SJeff Layton 			     struct ceph_osd_request *req)
46743d14c5d2SYehuda Sadeh {
46755aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
46765aea3dcdSIlya Dryomov 	submit_request(req, false);
46775aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
46783d14c5d2SYehuda Sadeh }
46793d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_start_request);
46803d14c5d2SYehuda Sadeh 
46813d14c5d2SYehuda Sadeh /*
4682d0bb883cSIlya Dryomov  * Unregister request.  If @req was registered, it isn't completed:
4683d0bb883cSIlya Dryomov  * r_result isn't set and __complete_request() isn't invoked.
4684d0bb883cSIlya Dryomov  *
4685d0bb883cSIlya Dryomov  * If @req wasn't registered, this call may have raced with
4686d0bb883cSIlya Dryomov  * handle_reply(), in which case r_result would already be set and
4687d0bb883cSIlya Dryomov  * __complete_request() would be getting invoked, possibly even
4688d0bb883cSIlya Dryomov  * concurrently with this call.
4689c9f9b93dSIlya Dryomov  */
ceph_osdc_cancel_request(struct ceph_osd_request * req)4690c9f9b93dSIlya Dryomov void ceph_osdc_cancel_request(struct ceph_osd_request *req)
4691c9f9b93dSIlya Dryomov {
4692c9f9b93dSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
4693c9f9b93dSIlya Dryomov 
46945aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
46955aea3dcdSIlya Dryomov 	if (req->r_osd)
46965aea3dcdSIlya Dryomov 		cancel_request(req);
46975aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
4698c9f9b93dSIlya Dryomov }
4699c9f9b93dSIlya Dryomov EXPORT_SYMBOL(ceph_osdc_cancel_request);
4700c9f9b93dSIlya Dryomov 
4701c9f9b93dSIlya Dryomov /*
470242b06965SIlya Dryomov  * @timeout: in jiffies, 0 means "wait forever"
470342b06965SIlya Dryomov  */
wait_request_timeout(struct ceph_osd_request * req,unsigned long timeout)470442b06965SIlya Dryomov static int wait_request_timeout(struct ceph_osd_request *req,
470542b06965SIlya Dryomov 				unsigned long timeout)
470642b06965SIlya Dryomov {
470742b06965SIlya Dryomov 	long left;
470842b06965SIlya Dryomov 
470942b06965SIlya Dryomov 	dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
47100e76abf2SYan, Zheng 	left = wait_for_completion_killable_timeout(&req->r_completion,
471142b06965SIlya Dryomov 						ceph_timeout_jiffies(timeout));
471242b06965SIlya Dryomov 	if (left <= 0) {
471342b06965SIlya Dryomov 		left = left ?: -ETIMEDOUT;
471442b06965SIlya Dryomov 		ceph_osdc_cancel_request(req);
471542b06965SIlya Dryomov 	} else {
471642b06965SIlya Dryomov 		left = req->r_result; /* completed */
471742b06965SIlya Dryomov 	}
471842b06965SIlya Dryomov 
471942b06965SIlya Dryomov 	return left;
472042b06965SIlya Dryomov }
472142b06965SIlya Dryomov 
472242b06965SIlya Dryomov /*
47233d14c5d2SYehuda Sadeh  * wait for a request to complete
47243d14c5d2SYehuda Sadeh  */
ceph_osdc_wait_request(struct ceph_osd_client * osdc,struct ceph_osd_request * req)47253d14c5d2SYehuda Sadeh int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
47263d14c5d2SYehuda Sadeh 			   struct ceph_osd_request *req)
47273d14c5d2SYehuda Sadeh {
472842b06965SIlya Dryomov 	return wait_request_timeout(req, 0);
47293d14c5d2SYehuda Sadeh }
47303d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_wait_request);
47313d14c5d2SYehuda Sadeh 
47323d14c5d2SYehuda Sadeh /*
47333d14c5d2SYehuda Sadeh  * sync - wait for all in-flight requests to flush.  avoid starvation.
47343d14c5d2SYehuda Sadeh  */
ceph_osdc_sync(struct ceph_osd_client * osdc)47353d14c5d2SYehuda Sadeh void ceph_osdc_sync(struct ceph_osd_client *osdc)
47363d14c5d2SYehuda Sadeh {
47375aea3dcdSIlya Dryomov 	struct rb_node *n, *p;
47385aea3dcdSIlya Dryomov 	u64 last_tid = atomic64_read(&osdc->last_tid);
47393d14c5d2SYehuda Sadeh 
47405aea3dcdSIlya Dryomov again:
47415aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
47425aea3dcdSIlya Dryomov 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
47435aea3dcdSIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
47445aea3dcdSIlya Dryomov 
47455aea3dcdSIlya Dryomov 		mutex_lock(&osd->lock);
47465aea3dcdSIlya Dryomov 		for (p = rb_first(&osd->o_requests); p; p = rb_next(p)) {
47475aea3dcdSIlya Dryomov 			struct ceph_osd_request *req =
47485aea3dcdSIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
47495aea3dcdSIlya Dryomov 
47503d14c5d2SYehuda Sadeh 			if (req->r_tid > last_tid)
47513d14c5d2SYehuda Sadeh 				break;
47523d14c5d2SYehuda Sadeh 
47535aea3dcdSIlya Dryomov 			if (!(req->r_flags & CEPH_OSD_FLAG_WRITE))
47543d14c5d2SYehuda Sadeh 				continue;
47553d14c5d2SYehuda Sadeh 
47563d14c5d2SYehuda Sadeh 			ceph_osdc_get_request(req);
47575aea3dcdSIlya Dryomov 			mutex_unlock(&osd->lock);
47585aea3dcdSIlya Dryomov 			up_read(&osdc->lock);
47595aea3dcdSIlya Dryomov 			dout("%s waiting on req %p tid %llu last_tid %llu\n",
47605aea3dcdSIlya Dryomov 			     __func__, req, req->r_tid, last_tid);
4761b18b9550SIlya Dryomov 			wait_for_completion(&req->r_completion);
47623d14c5d2SYehuda Sadeh 			ceph_osdc_put_request(req);
47635aea3dcdSIlya Dryomov 			goto again;
47643d14c5d2SYehuda Sadeh 		}
47655aea3dcdSIlya Dryomov 
47665aea3dcdSIlya Dryomov 		mutex_unlock(&osd->lock);
47675aea3dcdSIlya Dryomov 	}
47685aea3dcdSIlya Dryomov 
47695aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
47705aea3dcdSIlya Dryomov 	dout("%s done last_tid %llu\n", __func__, last_tid);
47713d14c5d2SYehuda Sadeh }
47723d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_sync);
47733d14c5d2SYehuda Sadeh 
4774922dab61SIlya Dryomov /*
4775922dab61SIlya Dryomov  * Returns a handle, caller owns a ref.
4776922dab61SIlya Dryomov  */
4777922dab61SIlya Dryomov struct ceph_osd_linger_request *
ceph_osdc_watch(struct ceph_osd_client * osdc,struct ceph_object_id * oid,struct ceph_object_locator * oloc,rados_watchcb2_t wcb,rados_watcherrcb_t errcb,void * data)4778922dab61SIlya Dryomov ceph_osdc_watch(struct ceph_osd_client *osdc,
4779922dab61SIlya Dryomov 		struct ceph_object_id *oid,
4780922dab61SIlya Dryomov 		struct ceph_object_locator *oloc,
4781922dab61SIlya Dryomov 		rados_watchcb2_t wcb,
4782922dab61SIlya Dryomov 		rados_watcherrcb_t errcb,
4783922dab61SIlya Dryomov 		void *data)
4784922dab61SIlya Dryomov {
4785922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
4786922dab61SIlya Dryomov 	int ret;
4787922dab61SIlya Dryomov 
4788922dab61SIlya Dryomov 	lreq = linger_alloc(osdc);
4789922dab61SIlya Dryomov 	if (!lreq)
4790922dab61SIlya Dryomov 		return ERR_PTR(-ENOMEM);
4791922dab61SIlya Dryomov 
479219079203SIlya Dryomov 	lreq->is_watch = true;
4793922dab61SIlya Dryomov 	lreq->wcb = wcb;
4794922dab61SIlya Dryomov 	lreq->errcb = errcb;
4795922dab61SIlya Dryomov 	lreq->data = data;
4796b07d3c4bSIlya Dryomov 	lreq->watch_valid_thru = jiffies;
4797922dab61SIlya Dryomov 
4798922dab61SIlya Dryomov 	ceph_oid_copy(&lreq->t.base_oid, oid);
4799922dab61SIlya Dryomov 	ceph_oloc_copy(&lreq->t.base_oloc, oloc);
480054ea0046SIlya Dryomov 	lreq->t.flags = CEPH_OSD_FLAG_WRITE;
4801fac02ddfSArnd Bergmann 	ktime_get_real_ts64(&lreq->mtime);
4802922dab61SIlya Dryomov 
480381c65213SIlya Dryomov 	linger_submit(lreq);
4804922dab61SIlya Dryomov 	ret = linger_reg_commit_wait(lreq);
4805922dab61SIlya Dryomov 	if (ret) {
4806922dab61SIlya Dryomov 		linger_cancel(lreq);
4807922dab61SIlya Dryomov 		goto err_put_lreq;
4808922dab61SIlya Dryomov 	}
4809922dab61SIlya Dryomov 
4810922dab61SIlya Dryomov 	return lreq;
4811922dab61SIlya Dryomov 
4812922dab61SIlya Dryomov err_put_lreq:
4813922dab61SIlya Dryomov 	linger_put(lreq);
4814922dab61SIlya Dryomov 	return ERR_PTR(ret);
4815922dab61SIlya Dryomov }
4816922dab61SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_watch);
4817922dab61SIlya Dryomov 
4818922dab61SIlya Dryomov /*
4819922dab61SIlya Dryomov  * Releases a ref.
4820922dab61SIlya Dryomov  *
4821922dab61SIlya Dryomov  * Times out after mount_timeout to preserve rbd unmap behaviour
4822922dab61SIlya Dryomov  * introduced in 2894e1d76974 ("rbd: timeout watch teardown on unmap
4823922dab61SIlya Dryomov  * with mount_timeout").
4824922dab61SIlya Dryomov  */
ceph_osdc_unwatch(struct ceph_osd_client * osdc,struct ceph_osd_linger_request * lreq)4825922dab61SIlya Dryomov int ceph_osdc_unwatch(struct ceph_osd_client *osdc,
4826922dab61SIlya Dryomov 		      struct ceph_osd_linger_request *lreq)
4827922dab61SIlya Dryomov {
4828922dab61SIlya Dryomov 	struct ceph_options *opts = osdc->client->options;
4829922dab61SIlya Dryomov 	struct ceph_osd_request *req;
4830922dab61SIlya Dryomov 	int ret;
4831922dab61SIlya Dryomov 
4832922dab61SIlya Dryomov 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4833922dab61SIlya Dryomov 	if (!req)
4834922dab61SIlya Dryomov 		return -ENOMEM;
4835922dab61SIlya Dryomov 
4836922dab61SIlya Dryomov 	ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
4837922dab61SIlya Dryomov 	ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
483854ea0046SIlya Dryomov 	req->r_flags = CEPH_OSD_FLAG_WRITE;
4839fac02ddfSArnd Bergmann 	ktime_get_real_ts64(&req->r_mtime);
484075dbb685SIlya Dryomov 	osd_req_op_watch_init(req, 0, CEPH_OSD_WATCH_OP_UNWATCH,
484175dbb685SIlya Dryomov 			      lreq->linger_id, 0);
4842922dab61SIlya Dryomov 
4843922dab61SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4844922dab61SIlya Dryomov 	if (ret)
4845922dab61SIlya Dryomov 		goto out_put_req;
4846922dab61SIlya Dryomov 
4847a8af0d68SJeff Layton 	ceph_osdc_start_request(osdc, req);
4848922dab61SIlya Dryomov 	linger_cancel(lreq);
4849922dab61SIlya Dryomov 	linger_put(lreq);
4850922dab61SIlya Dryomov 	ret = wait_request_timeout(req, opts->mount_timeout);
4851922dab61SIlya Dryomov 
4852922dab61SIlya Dryomov out_put_req:
4853922dab61SIlya Dryomov 	ceph_osdc_put_request(req);
4854922dab61SIlya Dryomov 	return ret;
4855922dab61SIlya Dryomov }
4856922dab61SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_unwatch);
4857922dab61SIlya Dryomov 
osd_req_op_notify_ack_init(struct ceph_osd_request * req,int which,u64 notify_id,u64 cookie,void * payload,u32 payload_len)4858922dab61SIlya Dryomov static int osd_req_op_notify_ack_init(struct ceph_osd_request *req, int which,
4859922dab61SIlya Dryomov 				      u64 notify_id, u64 cookie, void *payload,
48606d54228fSIlya Dryomov 				      u32 payload_len)
4861922dab61SIlya Dryomov {
4862922dab61SIlya Dryomov 	struct ceph_osd_req_op *op;
4863922dab61SIlya Dryomov 	struct ceph_pagelist *pl;
4864922dab61SIlya Dryomov 	int ret;
4865922dab61SIlya Dryomov 
4866042f6498SJeff Layton 	op = osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY_ACK, 0);
4867922dab61SIlya Dryomov 
486833165d47SIlya Dryomov 	pl = ceph_pagelist_alloc(GFP_NOIO);
4869922dab61SIlya Dryomov 	if (!pl)
4870922dab61SIlya Dryomov 		return -ENOMEM;
4871922dab61SIlya Dryomov 
4872922dab61SIlya Dryomov 	ret = ceph_pagelist_encode_64(pl, notify_id);
4873922dab61SIlya Dryomov 	ret |= ceph_pagelist_encode_64(pl, cookie);
4874922dab61SIlya Dryomov 	if (payload) {
4875922dab61SIlya Dryomov 		ret |= ceph_pagelist_encode_32(pl, payload_len);
4876922dab61SIlya Dryomov 		ret |= ceph_pagelist_append(pl, payload, payload_len);
4877922dab61SIlya Dryomov 	} else {
4878922dab61SIlya Dryomov 		ret |= ceph_pagelist_encode_32(pl, 0);
4879922dab61SIlya Dryomov 	}
4880922dab61SIlya Dryomov 	if (ret) {
4881922dab61SIlya Dryomov 		ceph_pagelist_release(pl);
4882922dab61SIlya Dryomov 		return -ENOMEM;
4883922dab61SIlya Dryomov 	}
4884922dab61SIlya Dryomov 
4885922dab61SIlya Dryomov 	ceph_osd_data_pagelist_init(&op->notify_ack.request_data, pl);
4886922dab61SIlya Dryomov 	op->indata_len = pl->length;
4887922dab61SIlya Dryomov 	return 0;
4888922dab61SIlya Dryomov }
4889922dab61SIlya Dryomov 
ceph_osdc_notify_ack(struct ceph_osd_client * osdc,struct ceph_object_id * oid,struct ceph_object_locator * oloc,u64 notify_id,u64 cookie,void * payload,u32 payload_len)4890922dab61SIlya Dryomov int ceph_osdc_notify_ack(struct ceph_osd_client *osdc,
4891922dab61SIlya Dryomov 			 struct ceph_object_id *oid,
4892922dab61SIlya Dryomov 			 struct ceph_object_locator *oloc,
4893922dab61SIlya Dryomov 			 u64 notify_id,
4894922dab61SIlya Dryomov 			 u64 cookie,
4895922dab61SIlya Dryomov 			 void *payload,
48966d54228fSIlya Dryomov 			 u32 payload_len)
4897922dab61SIlya Dryomov {
4898922dab61SIlya Dryomov 	struct ceph_osd_request *req;
4899922dab61SIlya Dryomov 	int ret;
4900922dab61SIlya Dryomov 
4901922dab61SIlya Dryomov 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4902922dab61SIlya Dryomov 	if (!req)
4903922dab61SIlya Dryomov 		return -ENOMEM;
4904922dab61SIlya Dryomov 
4905922dab61SIlya Dryomov 	ceph_oid_copy(&req->r_base_oid, oid);
4906922dab61SIlya Dryomov 	ceph_oloc_copy(&req->r_base_oloc, oloc);
4907922dab61SIlya Dryomov 	req->r_flags = CEPH_OSD_FLAG_READ;
4908922dab61SIlya Dryomov 
490926f887e0SIlya Dryomov 	ret = osd_req_op_notify_ack_init(req, 0, notify_id, cookie, payload,
491026f887e0SIlya Dryomov 					 payload_len);
4911922dab61SIlya Dryomov 	if (ret)
4912922dab61SIlya Dryomov 		goto out_put_req;
4913922dab61SIlya Dryomov 
491426f887e0SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4915922dab61SIlya Dryomov 	if (ret)
4916922dab61SIlya Dryomov 		goto out_put_req;
4917922dab61SIlya Dryomov 
4918a8af0d68SJeff Layton 	ceph_osdc_start_request(osdc, req);
4919922dab61SIlya Dryomov 	ret = ceph_osdc_wait_request(osdc, req);
4920922dab61SIlya Dryomov 
4921922dab61SIlya Dryomov out_put_req:
4922922dab61SIlya Dryomov 	ceph_osdc_put_request(req);
4923922dab61SIlya Dryomov 	return ret;
4924922dab61SIlya Dryomov }
4925922dab61SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_notify_ack);
4926922dab61SIlya Dryomov 
492719079203SIlya Dryomov /*
492819079203SIlya Dryomov  * @timeout: in seconds
492919079203SIlya Dryomov  *
493019079203SIlya Dryomov  * @preply_{pages,len} are initialized both on success and error.
493119079203SIlya Dryomov  * The caller is responsible for:
493219079203SIlya Dryomov  *
493319079203SIlya Dryomov  *     ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len))
493419079203SIlya Dryomov  */
ceph_osdc_notify(struct ceph_osd_client * osdc,struct ceph_object_id * oid,struct ceph_object_locator * oloc,void * payload,u32 payload_len,u32 timeout,struct page *** preply_pages,size_t * preply_len)493519079203SIlya Dryomov int ceph_osdc_notify(struct ceph_osd_client *osdc,
493619079203SIlya Dryomov 		     struct ceph_object_id *oid,
493719079203SIlya Dryomov 		     struct ceph_object_locator *oloc,
493819079203SIlya Dryomov 		     void *payload,
49396d54228fSIlya Dryomov 		     u32 payload_len,
494019079203SIlya Dryomov 		     u32 timeout,
494119079203SIlya Dryomov 		     struct page ***preply_pages,
494219079203SIlya Dryomov 		     size_t *preply_len)
494319079203SIlya Dryomov {
494419079203SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
494519079203SIlya Dryomov 	int ret;
494619079203SIlya Dryomov 
494719079203SIlya Dryomov 	WARN_ON(!timeout);
494819079203SIlya Dryomov 	if (preply_pages) {
494919079203SIlya Dryomov 		*preply_pages = NULL;
495019079203SIlya Dryomov 		*preply_len = 0;
495119079203SIlya Dryomov 	}
495219079203SIlya Dryomov 
495319079203SIlya Dryomov 	lreq = linger_alloc(osdc);
495419079203SIlya Dryomov 	if (!lreq)
495519079203SIlya Dryomov 		return -ENOMEM;
495619079203SIlya Dryomov 
495775dbb685SIlya Dryomov 	lreq->request_pl = ceph_pagelist_alloc(GFP_NOIO);
495875dbb685SIlya Dryomov 	if (!lreq->request_pl) {
495975dbb685SIlya Dryomov 		ret = -ENOMEM;
496075dbb685SIlya Dryomov 		goto out_put_lreq;
496175dbb685SIlya Dryomov 	}
496275dbb685SIlya Dryomov 
496375dbb685SIlya Dryomov 	ret = ceph_pagelist_encode_32(lreq->request_pl, 1); /* prot_ver */
496475dbb685SIlya Dryomov 	ret |= ceph_pagelist_encode_32(lreq->request_pl, timeout);
496575dbb685SIlya Dryomov 	ret |= ceph_pagelist_encode_32(lreq->request_pl, payload_len);
496675dbb685SIlya Dryomov 	ret |= ceph_pagelist_append(lreq->request_pl, payload, payload_len);
496775dbb685SIlya Dryomov 	if (ret) {
496875dbb685SIlya Dryomov 		ret = -ENOMEM;
496975dbb685SIlya Dryomov 		goto out_put_lreq;
497075dbb685SIlya Dryomov 	}
497175dbb685SIlya Dryomov 
497275dbb685SIlya Dryomov 	/* for notify_id */
497375dbb685SIlya Dryomov 	lreq->notify_id_pages = ceph_alloc_page_vector(1, GFP_NOIO);
497475dbb685SIlya Dryomov 	if (IS_ERR(lreq->notify_id_pages)) {
497575dbb685SIlya Dryomov 		ret = PTR_ERR(lreq->notify_id_pages);
497675dbb685SIlya Dryomov 		lreq->notify_id_pages = NULL;
497775dbb685SIlya Dryomov 		goto out_put_lreq;
497875dbb685SIlya Dryomov 	}
497975dbb685SIlya Dryomov 
498019079203SIlya Dryomov 	lreq->preply_pages = preply_pages;
498119079203SIlya Dryomov 	lreq->preply_len = preply_len;
498219079203SIlya Dryomov 
498319079203SIlya Dryomov 	ceph_oid_copy(&lreq->t.base_oid, oid);
498419079203SIlya Dryomov 	ceph_oloc_copy(&lreq->t.base_oloc, oloc);
498519079203SIlya Dryomov 	lreq->t.flags = CEPH_OSD_FLAG_READ;
498619079203SIlya Dryomov 
498781c65213SIlya Dryomov 	linger_submit(lreq);
498819079203SIlya Dryomov 	ret = linger_reg_commit_wait(lreq);
498919079203SIlya Dryomov 	if (!ret)
4990e6e28432SIlya Dryomov 		ret = linger_notify_finish_wait(lreq,
4991e6e28432SIlya Dryomov 				 msecs_to_jiffies(2 * timeout * MSEC_PER_SEC));
499219079203SIlya Dryomov 	else
499319079203SIlya Dryomov 		dout("lreq %p failed to initiate notify %d\n", lreq, ret);
499419079203SIlya Dryomov 
499519079203SIlya Dryomov 	linger_cancel(lreq);
499619079203SIlya Dryomov out_put_lreq:
499719079203SIlya Dryomov 	linger_put(lreq);
499819079203SIlya Dryomov 	return ret;
499919079203SIlya Dryomov }
500019079203SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_notify);
500119079203SIlya Dryomov 
50023d14c5d2SYehuda Sadeh /*
5003b07d3c4bSIlya Dryomov  * Return the number of milliseconds since the watch was last
5004b07d3c4bSIlya Dryomov  * confirmed, or an error.  If there is an error, the watch is no
5005b07d3c4bSIlya Dryomov  * longer valid, and should be destroyed with ceph_osdc_unwatch().
5006b07d3c4bSIlya Dryomov  */
ceph_osdc_watch_check(struct ceph_osd_client * osdc,struct ceph_osd_linger_request * lreq)5007b07d3c4bSIlya Dryomov int ceph_osdc_watch_check(struct ceph_osd_client *osdc,
5008b07d3c4bSIlya Dryomov 			  struct ceph_osd_linger_request *lreq)
5009b07d3c4bSIlya Dryomov {
5010b07d3c4bSIlya Dryomov 	unsigned long stamp, age;
5011b07d3c4bSIlya Dryomov 	int ret;
5012b07d3c4bSIlya Dryomov 
5013b07d3c4bSIlya Dryomov 	down_read(&osdc->lock);
5014b07d3c4bSIlya Dryomov 	mutex_lock(&lreq->lock);
5015b07d3c4bSIlya Dryomov 	stamp = lreq->watch_valid_thru;
5016b07d3c4bSIlya Dryomov 	if (!list_empty(&lreq->pending_lworks)) {
5017b07d3c4bSIlya Dryomov 		struct linger_work *lwork =
5018b07d3c4bSIlya Dryomov 		    list_first_entry(&lreq->pending_lworks,
5019b07d3c4bSIlya Dryomov 				     struct linger_work,
5020b07d3c4bSIlya Dryomov 				     pending_item);
5021b07d3c4bSIlya Dryomov 
5022b07d3c4bSIlya Dryomov 		if (time_before(lwork->queued_stamp, stamp))
5023b07d3c4bSIlya Dryomov 			stamp = lwork->queued_stamp;
5024b07d3c4bSIlya Dryomov 	}
5025b07d3c4bSIlya Dryomov 	age = jiffies - stamp;
5026b07d3c4bSIlya Dryomov 	dout("%s lreq %p linger_id %llu age %lu last_error %d\n", __func__,
5027b07d3c4bSIlya Dryomov 	     lreq, lreq->linger_id, age, lreq->last_error);
5028b07d3c4bSIlya Dryomov 	/* we are truncating to msecs, so return a safe upper bound */
5029b07d3c4bSIlya Dryomov 	ret = lreq->last_error ?: 1 + jiffies_to_msecs(age);
5030b07d3c4bSIlya Dryomov 
5031b07d3c4bSIlya Dryomov 	mutex_unlock(&lreq->lock);
5032b07d3c4bSIlya Dryomov 	up_read(&osdc->lock);
5033b07d3c4bSIlya Dryomov 	return ret;
5034b07d3c4bSIlya Dryomov }
5035b07d3c4bSIlya Dryomov 
decode_watcher(void ** p,void * end,struct ceph_watch_item * item)5036a4ed38d7SDouglas Fuller static int decode_watcher(void **p, void *end, struct ceph_watch_item *item)
5037a4ed38d7SDouglas Fuller {
5038a4ed38d7SDouglas Fuller 	u8 struct_v;
5039a4ed38d7SDouglas Fuller 	u32 struct_len;
5040a4ed38d7SDouglas Fuller 	int ret;
5041a4ed38d7SDouglas Fuller 
5042a4ed38d7SDouglas Fuller 	ret = ceph_start_decoding(p, end, 2, "watch_item_t",
5043a4ed38d7SDouglas Fuller 				  &struct_v, &struct_len);
5044a4ed38d7SDouglas Fuller 	if (ret)
504551fc7ab4SJeff Layton 		goto bad;
5046a4ed38d7SDouglas Fuller 
504751fc7ab4SJeff Layton 	ret = -EINVAL;
504851fc7ab4SJeff Layton 	ceph_decode_copy_safe(p, end, &item->name, sizeof(item->name), bad);
504951fc7ab4SJeff Layton 	ceph_decode_64_safe(p, end, item->cookie, bad);
505051fc7ab4SJeff Layton 	ceph_decode_skip_32(p, end, bad); /* skip timeout seconds */
505151fc7ab4SJeff Layton 
5052a4ed38d7SDouglas Fuller 	if (struct_v >= 2) {
505351fc7ab4SJeff Layton 		ret = ceph_decode_entity_addr(p, end, &item->addr);
505451fc7ab4SJeff Layton 		if (ret)
505551fc7ab4SJeff Layton 			goto bad;
505651fc7ab4SJeff Layton 	} else {
505751fc7ab4SJeff Layton 		ret = 0;
5058a4ed38d7SDouglas Fuller 	}
5059a4ed38d7SDouglas Fuller 
5060a4ed38d7SDouglas Fuller 	dout("%s %s%llu cookie %llu addr %s\n", __func__,
5061a4ed38d7SDouglas Fuller 	     ENTITY_NAME(item->name), item->cookie,
5062b726ec97SJeff Layton 	     ceph_pr_addr(&item->addr));
506351fc7ab4SJeff Layton bad:
506451fc7ab4SJeff Layton 	return ret;
5065a4ed38d7SDouglas Fuller }
5066a4ed38d7SDouglas Fuller 
decode_watchers(void ** p,void * end,struct ceph_watch_item ** watchers,u32 * num_watchers)5067a4ed38d7SDouglas Fuller static int decode_watchers(void **p, void *end,
5068a4ed38d7SDouglas Fuller 			   struct ceph_watch_item **watchers,
5069a4ed38d7SDouglas Fuller 			   u32 *num_watchers)
5070a4ed38d7SDouglas Fuller {
5071a4ed38d7SDouglas Fuller 	u8 struct_v;
5072a4ed38d7SDouglas Fuller 	u32 struct_len;
5073a4ed38d7SDouglas Fuller 	int i;
5074a4ed38d7SDouglas Fuller 	int ret;
5075a4ed38d7SDouglas Fuller 
5076a4ed38d7SDouglas Fuller 	ret = ceph_start_decoding(p, end, 1, "obj_list_watch_response_t",
5077a4ed38d7SDouglas Fuller 				  &struct_v, &struct_len);
5078a4ed38d7SDouglas Fuller 	if (ret)
5079a4ed38d7SDouglas Fuller 		return ret;
5080a4ed38d7SDouglas Fuller 
5081a4ed38d7SDouglas Fuller 	*num_watchers = ceph_decode_32(p);
5082a4ed38d7SDouglas Fuller 	*watchers = kcalloc(*num_watchers, sizeof(**watchers), GFP_NOIO);
5083a4ed38d7SDouglas Fuller 	if (!*watchers)
5084a4ed38d7SDouglas Fuller 		return -ENOMEM;
5085a4ed38d7SDouglas Fuller 
5086a4ed38d7SDouglas Fuller 	for (i = 0; i < *num_watchers; i++) {
5087a4ed38d7SDouglas Fuller 		ret = decode_watcher(p, end, *watchers + i);
5088a4ed38d7SDouglas Fuller 		if (ret) {
5089a4ed38d7SDouglas Fuller 			kfree(*watchers);
5090a4ed38d7SDouglas Fuller 			return ret;
5091a4ed38d7SDouglas Fuller 		}
5092a4ed38d7SDouglas Fuller 	}
5093a4ed38d7SDouglas Fuller 
5094a4ed38d7SDouglas Fuller 	return 0;
5095a4ed38d7SDouglas Fuller }
5096a4ed38d7SDouglas Fuller 
5097a4ed38d7SDouglas Fuller /*
5098a4ed38d7SDouglas Fuller  * On success, the caller is responsible for:
5099a4ed38d7SDouglas Fuller  *
5100a4ed38d7SDouglas Fuller  *     kfree(watchers);
5101a4ed38d7SDouglas Fuller  */
ceph_osdc_list_watchers(struct ceph_osd_client * osdc,struct ceph_object_id * oid,struct ceph_object_locator * oloc,struct ceph_watch_item ** watchers,u32 * num_watchers)5102a4ed38d7SDouglas Fuller int ceph_osdc_list_watchers(struct ceph_osd_client *osdc,
5103a4ed38d7SDouglas Fuller 			    struct ceph_object_id *oid,
5104a4ed38d7SDouglas Fuller 			    struct ceph_object_locator *oloc,
5105a4ed38d7SDouglas Fuller 			    struct ceph_watch_item **watchers,
5106a4ed38d7SDouglas Fuller 			    u32 *num_watchers)
5107a4ed38d7SDouglas Fuller {
5108a4ed38d7SDouglas Fuller 	struct ceph_osd_request *req;
5109a4ed38d7SDouglas Fuller 	struct page **pages;
5110a4ed38d7SDouglas Fuller 	int ret;
5111a4ed38d7SDouglas Fuller 
5112a4ed38d7SDouglas Fuller 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
5113a4ed38d7SDouglas Fuller 	if (!req)
5114a4ed38d7SDouglas Fuller 		return -ENOMEM;
5115a4ed38d7SDouglas Fuller 
5116a4ed38d7SDouglas Fuller 	ceph_oid_copy(&req->r_base_oid, oid);
5117a4ed38d7SDouglas Fuller 	ceph_oloc_copy(&req->r_base_oloc, oloc);
5118a4ed38d7SDouglas Fuller 	req->r_flags = CEPH_OSD_FLAG_READ;
5119a4ed38d7SDouglas Fuller 
5120a4ed38d7SDouglas Fuller 	pages = ceph_alloc_page_vector(1, GFP_NOIO);
5121a4ed38d7SDouglas Fuller 	if (IS_ERR(pages)) {
5122a4ed38d7SDouglas Fuller 		ret = PTR_ERR(pages);
5123a4ed38d7SDouglas Fuller 		goto out_put_req;
5124a4ed38d7SDouglas Fuller 	}
5125a4ed38d7SDouglas Fuller 
5126a4ed38d7SDouglas Fuller 	osd_req_op_init(req, 0, CEPH_OSD_OP_LIST_WATCHERS, 0);
5127a4ed38d7SDouglas Fuller 	ceph_osd_data_pages_init(osd_req_op_data(req, 0, list_watchers,
5128a4ed38d7SDouglas Fuller 						 response_data),
5129a4ed38d7SDouglas Fuller 				 pages, PAGE_SIZE, 0, false, true);
5130a4ed38d7SDouglas Fuller 
513126f887e0SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
513226f887e0SIlya Dryomov 	if (ret)
513326f887e0SIlya Dryomov 		goto out_put_req;
513426f887e0SIlya Dryomov 
5135a8af0d68SJeff Layton 	ceph_osdc_start_request(osdc, req);
5136a4ed38d7SDouglas Fuller 	ret = ceph_osdc_wait_request(osdc, req);
5137a4ed38d7SDouglas Fuller 	if (ret >= 0) {
5138a4ed38d7SDouglas Fuller 		void *p = page_address(pages[0]);
5139a4ed38d7SDouglas Fuller 		void *const end = p + req->r_ops[0].outdata_len;
5140a4ed38d7SDouglas Fuller 
5141a4ed38d7SDouglas Fuller 		ret = decode_watchers(&p, end, watchers, num_watchers);
5142a4ed38d7SDouglas Fuller 	}
5143a4ed38d7SDouglas Fuller 
5144a4ed38d7SDouglas Fuller out_put_req:
5145a4ed38d7SDouglas Fuller 	ceph_osdc_put_request(req);
5146a4ed38d7SDouglas Fuller 	return ret;
5147a4ed38d7SDouglas Fuller }
5148a4ed38d7SDouglas Fuller EXPORT_SYMBOL(ceph_osdc_list_watchers);
5149a4ed38d7SDouglas Fuller 
5150b07d3c4bSIlya Dryomov /*
5151dd935f44SJosh Durgin  * Call all pending notify callbacks - for use after a watch is
5152dd935f44SJosh Durgin  * unregistered, to make sure no more callbacks for it will be invoked
5153dd935f44SJosh Durgin  */
ceph_osdc_flush_notifies(struct ceph_osd_client * osdc)5154f6479449Sstephen hemminger void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
5155dd935f44SJosh Durgin {
515699d16943SIlya Dryomov 	dout("%s osdc %p\n", __func__, osdc);
5157dd935f44SJosh Durgin 	flush_workqueue(osdc->notify_wq);
5158dd935f44SJosh Durgin }
5159dd935f44SJosh Durgin EXPORT_SYMBOL(ceph_osdc_flush_notifies);
5160dd935f44SJosh Durgin 
ceph_osdc_maybe_request_map(struct ceph_osd_client * osdc)51617cca78c9SIlya Dryomov void ceph_osdc_maybe_request_map(struct ceph_osd_client *osdc)
51627cca78c9SIlya Dryomov {
51637cca78c9SIlya Dryomov 	down_read(&osdc->lock);
51647cca78c9SIlya Dryomov 	maybe_request_map(osdc);
51657cca78c9SIlya Dryomov 	up_read(&osdc->lock);
51667cca78c9SIlya Dryomov }
51677cca78c9SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_maybe_request_map);
5168dd935f44SJosh Durgin 
5169dd935f44SJosh Durgin /*
5170428a7158SDouglas Fuller  * Execute an OSD class method on an object.
5171428a7158SDouglas Fuller  *
5172428a7158SDouglas Fuller  * @flags: CEPH_OSD_FLAG_*
51732544a020SIlya Dryomov  * @resp_len: in/out param for reply length
5174428a7158SDouglas Fuller  */
ceph_osdc_call(struct ceph_osd_client * osdc,struct ceph_object_id * oid,struct ceph_object_locator * oloc,const char * class,const char * method,unsigned int flags,struct page * req_page,size_t req_len,struct page ** resp_pages,size_t * resp_len)5175428a7158SDouglas Fuller int ceph_osdc_call(struct ceph_osd_client *osdc,
5176428a7158SDouglas Fuller 		   struct ceph_object_id *oid,
5177428a7158SDouglas Fuller 		   struct ceph_object_locator *oloc,
5178428a7158SDouglas Fuller 		   const char *class, const char *method,
5179428a7158SDouglas Fuller 		   unsigned int flags,
5180428a7158SDouglas Fuller 		   struct page *req_page, size_t req_len,
518168ada915SIlya Dryomov 		   struct page **resp_pages, size_t *resp_len)
5182428a7158SDouglas Fuller {
5183428a7158SDouglas Fuller 	struct ceph_osd_request *req;
5184428a7158SDouglas Fuller 	int ret;
5185428a7158SDouglas Fuller 
518668ada915SIlya Dryomov 	if (req_len > PAGE_SIZE)
51872544a020SIlya Dryomov 		return -E2BIG;
51882544a020SIlya Dryomov 
5189428a7158SDouglas Fuller 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
5190428a7158SDouglas Fuller 	if (!req)
5191428a7158SDouglas Fuller 		return -ENOMEM;
5192428a7158SDouglas Fuller 
5193428a7158SDouglas Fuller 	ceph_oid_copy(&req->r_base_oid, oid);
5194428a7158SDouglas Fuller 	ceph_oloc_copy(&req->r_base_oloc, oloc);
5195428a7158SDouglas Fuller 	req->r_flags = flags;
5196428a7158SDouglas Fuller 
519724639ce5SIlya Dryomov 	ret = osd_req_op_cls_init(req, 0, class, method);
5198fe943d50SChengguang Xu 	if (ret)
5199fe943d50SChengguang Xu 		goto out_put_req;
5200fe943d50SChengguang Xu 
5201428a7158SDouglas Fuller 	if (req_page)
5202428a7158SDouglas Fuller 		osd_req_op_cls_request_data_pages(req, 0, &req_page, req_len,
5203428a7158SDouglas Fuller 						  0, false, false);
520468ada915SIlya Dryomov 	if (resp_pages)
520568ada915SIlya Dryomov 		osd_req_op_cls_response_data_pages(req, 0, resp_pages,
52062544a020SIlya Dryomov 						   *resp_len, 0, false, false);
5207428a7158SDouglas Fuller 
520826f887e0SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
520926f887e0SIlya Dryomov 	if (ret)
521026f887e0SIlya Dryomov 		goto out_put_req;
521126f887e0SIlya Dryomov 
5212a8af0d68SJeff Layton 	ceph_osdc_start_request(osdc, req);
5213428a7158SDouglas Fuller 	ret = ceph_osdc_wait_request(osdc, req);
5214428a7158SDouglas Fuller 	if (ret >= 0) {
5215428a7158SDouglas Fuller 		ret = req->r_ops[0].rval;
521668ada915SIlya Dryomov 		if (resp_pages)
5217428a7158SDouglas Fuller 			*resp_len = req->r_ops[0].outdata_len;
5218428a7158SDouglas Fuller 	}
5219428a7158SDouglas Fuller 
5220428a7158SDouglas Fuller out_put_req:
5221428a7158SDouglas Fuller 	ceph_osdc_put_request(req);
5222428a7158SDouglas Fuller 	return ret;
5223428a7158SDouglas Fuller }
5224428a7158SDouglas Fuller EXPORT_SYMBOL(ceph_osdc_call);
5225428a7158SDouglas Fuller 
5226428a7158SDouglas Fuller /*
5227120a75eaSYan, Zheng  * reset all osd connections
5228120a75eaSYan, Zheng  */
ceph_osdc_reopen_osds(struct ceph_osd_client * osdc)5229120a75eaSYan, Zheng void ceph_osdc_reopen_osds(struct ceph_osd_client *osdc)
5230120a75eaSYan, Zheng {
5231120a75eaSYan, Zheng 	struct rb_node *n;
5232120a75eaSYan, Zheng 
5233120a75eaSYan, Zheng 	down_write(&osdc->lock);
5234120a75eaSYan, Zheng 	for (n = rb_first(&osdc->osds); n; ) {
5235120a75eaSYan, Zheng 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
5236120a75eaSYan, Zheng 
5237120a75eaSYan, Zheng 		n = rb_next(n);
5238120a75eaSYan, Zheng 		if (!reopen_osd(osd))
5239120a75eaSYan, Zheng 			kick_osd_requests(osd);
5240120a75eaSYan, Zheng 	}
5241120a75eaSYan, Zheng 	up_write(&osdc->lock);
5242120a75eaSYan, Zheng }
5243120a75eaSYan, Zheng 
5244120a75eaSYan, Zheng /*
52453d14c5d2SYehuda Sadeh  * init, shutdown
52463d14c5d2SYehuda Sadeh  */
ceph_osdc_init(struct ceph_osd_client * osdc,struct ceph_client * client)52473d14c5d2SYehuda Sadeh int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
52483d14c5d2SYehuda Sadeh {
52493d14c5d2SYehuda Sadeh 	int err;
52503d14c5d2SYehuda Sadeh 
52513d14c5d2SYehuda Sadeh 	dout("init\n");
52523d14c5d2SYehuda Sadeh 	osdc->client = client;
52535aea3dcdSIlya Dryomov 	init_rwsem(&osdc->lock);
52543d14c5d2SYehuda Sadeh 	osdc->osds = RB_ROOT;
52553d14c5d2SYehuda Sadeh 	INIT_LIST_HEAD(&osdc->osd_lru);
52569dd2845cSIlya Dryomov 	spin_lock_init(&osdc->osd_lru_lock);
52575aea3dcdSIlya Dryomov 	osd_init(&osdc->homeless_osd);
52585aea3dcdSIlya Dryomov 	osdc->homeless_osd.o_osdc = osdc;
52595aea3dcdSIlya Dryomov 	osdc->homeless_osd.o_osd = CEPH_HOMELESS_OSD;
5260264048afSIlya Dryomov 	osdc->last_linger_id = CEPH_LINGER_ID_START;
5261922dab61SIlya Dryomov 	osdc->linger_requests = RB_ROOT;
52624609245eSIlya Dryomov 	osdc->map_checks = RB_ROOT;
52634609245eSIlya Dryomov 	osdc->linger_map_checks = RB_ROOT;
52643d14c5d2SYehuda Sadeh 	INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
52653d14c5d2SYehuda Sadeh 	INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
52663d14c5d2SYehuda Sadeh 
52673d14c5d2SYehuda Sadeh 	err = -ENOMEM;
5268e5253a7bSIlya Dryomov 	osdc->osdmap = ceph_osdmap_alloc();
5269e5253a7bSIlya Dryomov 	if (!osdc->osdmap)
5270e5253a7bSIlya Dryomov 		goto out;
5271e5253a7bSIlya Dryomov 
52729e767adbSIlya Dryomov 	osdc->req_mempool = mempool_create_slab_pool(10,
52739e767adbSIlya Dryomov 						     ceph_osd_request_cache);
52743d14c5d2SYehuda Sadeh 	if (!osdc->req_mempool)
5275e5253a7bSIlya Dryomov 		goto out_map;
52763d14c5d2SYehuda Sadeh 
5277d50b409fSSage Weil 	err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
52780d9c1ab3SIlya Dryomov 				PAGE_SIZE, CEPH_OSD_SLAB_OPS, 10, "osd_op");
52793d14c5d2SYehuda Sadeh 	if (err < 0)
52803d14c5d2SYehuda Sadeh 		goto out_mempool;
5281d50b409fSSage Weil 	err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
52820d9c1ab3SIlya Dryomov 				PAGE_SIZE, CEPH_OSD_SLAB_OPS, 10,
52830d9c1ab3SIlya Dryomov 				"osd_op_reply");
52843d14c5d2SYehuda Sadeh 	if (err < 0)
52853d14c5d2SYehuda Sadeh 		goto out_msgpool;
5286a40c4f10SYehuda Sadeh 
5287dbcae088SDan Carpenter 	err = -ENOMEM;
5288a40c4f10SYehuda Sadeh 	osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
5289dbcae088SDan Carpenter 	if (!osdc->notify_wq)
5290c172ec5cSIlya Dryomov 		goto out_msgpool_reply;
5291c172ec5cSIlya Dryomov 
529288bc1922SIlya Dryomov 	osdc->completion_wq = create_singlethread_workqueue("ceph-completion");
529388bc1922SIlya Dryomov 	if (!osdc->completion_wq)
529488bc1922SIlya Dryomov 		goto out_notify_wq;
529588bc1922SIlya Dryomov 
5296fbca9635SIlya Dryomov 	schedule_delayed_work(&osdc->timeout_work,
5297fbca9635SIlya Dryomov 			      osdc->client->options->osd_keepalive_timeout);
5298b37ee1b9SIlya Dryomov 	schedule_delayed_work(&osdc->osds_timeout_work,
5299b37ee1b9SIlya Dryomov 	    round_jiffies_relative(osdc->client->options->osd_idle_ttl));
5300b37ee1b9SIlya Dryomov 
53013d14c5d2SYehuda Sadeh 	return 0;
53023d14c5d2SYehuda Sadeh 
530388bc1922SIlya Dryomov out_notify_wq:
530488bc1922SIlya Dryomov 	destroy_workqueue(osdc->notify_wq);
5305c172ec5cSIlya Dryomov out_msgpool_reply:
5306c172ec5cSIlya Dryomov 	ceph_msgpool_destroy(&osdc->msgpool_op_reply);
53073d14c5d2SYehuda Sadeh out_msgpool:
53083d14c5d2SYehuda Sadeh 	ceph_msgpool_destroy(&osdc->msgpool_op);
53093d14c5d2SYehuda Sadeh out_mempool:
53103d14c5d2SYehuda Sadeh 	mempool_destroy(osdc->req_mempool);
5311e5253a7bSIlya Dryomov out_map:
5312e5253a7bSIlya Dryomov 	ceph_osdmap_destroy(osdc->osdmap);
53133d14c5d2SYehuda Sadeh out:
53143d14c5d2SYehuda Sadeh 	return err;
53153d14c5d2SYehuda Sadeh }
53163d14c5d2SYehuda Sadeh 
ceph_osdc_stop(struct ceph_osd_client * osdc)53173d14c5d2SYehuda Sadeh void ceph_osdc_stop(struct ceph_osd_client *osdc)
53183d14c5d2SYehuda Sadeh {
531988bc1922SIlya Dryomov 	destroy_workqueue(osdc->completion_wq);
5320a40c4f10SYehuda Sadeh 	destroy_workqueue(osdc->notify_wq);
53213d14c5d2SYehuda Sadeh 	cancel_delayed_work_sync(&osdc->timeout_work);
53223d14c5d2SYehuda Sadeh 	cancel_delayed_work_sync(&osdc->osds_timeout_work);
532342a2c09fSIlya Dryomov 
53245aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
532542a2c09fSIlya Dryomov 	while (!RB_EMPTY_ROOT(&osdc->osds)) {
532642a2c09fSIlya Dryomov 		struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
532742a2c09fSIlya Dryomov 						struct ceph_osd, o_node);
53285aea3dcdSIlya Dryomov 		close_osd(osd);
532942a2c09fSIlya Dryomov 	}
53305aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
533102113a0fSElena Reshetova 	WARN_ON(refcount_read(&osdc->homeless_osd.o_ref) != 1);
53325aea3dcdSIlya Dryomov 	osd_cleanup(&osdc->homeless_osd);
53335aea3dcdSIlya Dryomov 
53345aea3dcdSIlya Dryomov 	WARN_ON(!list_empty(&osdc->osd_lru));
5335922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_requests));
53364609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osdc->map_checks));
53374609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_map_checks));
53385aea3dcdSIlya Dryomov 	WARN_ON(atomic_read(&osdc->num_requests));
53395aea3dcdSIlya Dryomov 	WARN_ON(atomic_read(&osdc->num_homeless));
534042a2c09fSIlya Dryomov 
53413d14c5d2SYehuda Sadeh 	ceph_osdmap_destroy(osdc->osdmap);
53423d14c5d2SYehuda Sadeh 	mempool_destroy(osdc->req_mempool);
53433d14c5d2SYehuda Sadeh 	ceph_msgpool_destroy(&osdc->msgpool_op);
53443d14c5d2SYehuda Sadeh 	ceph_msgpool_destroy(&osdc->msgpool_op_reply);
53453d14c5d2SYehuda Sadeh }
53463d14c5d2SYehuda Sadeh 
osd_req_op_copy_from_init(struct ceph_osd_request * req,u64 src_snapid,u64 src_version,struct ceph_object_id * src_oid,struct ceph_object_locator * src_oloc,u32 src_fadvise_flags,u32 dst_fadvise_flags,u32 truncate_seq,u64 truncate_size,u8 copy_from_flags)5347aca39d9eSLuís Henriques int osd_req_op_copy_from_init(struct ceph_osd_request *req,
534823ddf9beSLuis Henriques 			      u64 src_snapid, u64 src_version,
534923ddf9beSLuis Henriques 			      struct ceph_object_id *src_oid,
535023ddf9beSLuis Henriques 			      struct ceph_object_locator *src_oloc,
535123ddf9beSLuis Henriques 			      u32 src_fadvise_flags,
535223ddf9beSLuis Henriques 			      u32 dst_fadvise_flags,
535378beb0ffSLuis Henriques 			      u32 truncate_seq, u64 truncate_size,
535423ddf9beSLuis Henriques 			      u8 copy_from_flags)
535523ddf9beSLuis Henriques {
535623ddf9beSLuis Henriques 	struct ceph_osd_req_op *op;
535723ddf9beSLuis Henriques 	struct page **pages;
535823ddf9beSLuis Henriques 	void *p, *end;
535923ddf9beSLuis Henriques 
536023ddf9beSLuis Henriques 	pages = ceph_alloc_page_vector(1, GFP_KERNEL);
536123ddf9beSLuis Henriques 	if (IS_ERR(pages))
536223ddf9beSLuis Henriques 		return PTR_ERR(pages);
536323ddf9beSLuis Henriques 
5364042f6498SJeff Layton 	op = osd_req_op_init(req, 0, CEPH_OSD_OP_COPY_FROM2,
536578beb0ffSLuis Henriques 			     dst_fadvise_flags);
536623ddf9beSLuis Henriques 	op->copy_from.snapid = src_snapid;
536723ddf9beSLuis Henriques 	op->copy_from.src_version = src_version;
536823ddf9beSLuis Henriques 	op->copy_from.flags = copy_from_flags;
536923ddf9beSLuis Henriques 	op->copy_from.src_fadvise_flags = src_fadvise_flags;
537023ddf9beSLuis Henriques 
537123ddf9beSLuis Henriques 	p = page_address(pages[0]);
537223ddf9beSLuis Henriques 	end = p + PAGE_SIZE;
537323ddf9beSLuis Henriques 	ceph_encode_string(&p, end, src_oid->name, src_oid->name_len);
537423ddf9beSLuis Henriques 	encode_oloc(&p, end, src_oloc);
537578beb0ffSLuis Henriques 	ceph_encode_32(&p, truncate_seq);
537678beb0ffSLuis Henriques 	ceph_encode_64(&p, truncate_size);
537723ddf9beSLuis Henriques 	op->indata_len = PAGE_SIZE - (end - p);
537823ddf9beSLuis Henriques 
537923ddf9beSLuis Henriques 	ceph_osd_data_pages_init(&op->copy_from.osd_data, pages,
538023ddf9beSLuis Henriques 				 op->indata_len, 0, false, true);
538123ddf9beSLuis Henriques 	return 0;
538223ddf9beSLuis Henriques }
5383aca39d9eSLuís Henriques EXPORT_SYMBOL(osd_req_op_copy_from_init);
538423ddf9beSLuis Henriques 
ceph_osdc_setup(void)538557a35dfbSChengguang Xu int __init ceph_osdc_setup(void)
53865522ae0bSAlex Elder {
53873f1af42aSIlya Dryomov 	size_t size = sizeof(struct ceph_osd_request) +
53883f1af42aSIlya Dryomov 	    CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
53893f1af42aSIlya Dryomov 
53905522ae0bSAlex Elder 	BUG_ON(ceph_osd_request_cache);
53913f1af42aSIlya Dryomov 	ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
53923f1af42aSIlya Dryomov 						   0, 0, NULL);
53935522ae0bSAlex Elder 
53945522ae0bSAlex Elder 	return ceph_osd_request_cache ? 0 : -ENOMEM;
53955522ae0bSAlex Elder }
53965522ae0bSAlex Elder 
ceph_osdc_cleanup(void)53975522ae0bSAlex Elder void ceph_osdc_cleanup(void)
53985522ae0bSAlex Elder {
53995522ae0bSAlex Elder 	BUG_ON(!ceph_osd_request_cache);
54005522ae0bSAlex Elder 	kmem_cache_destroy(ceph_osd_request_cache);
54015522ae0bSAlex Elder 	ceph_osd_request_cache = NULL;
54025522ae0bSAlex Elder }
54035522ae0bSAlex Elder 
54043d14c5d2SYehuda Sadeh /*
54053d14c5d2SYehuda Sadeh  * handle incoming message
54063d14c5d2SYehuda Sadeh  */
osd_dispatch(struct ceph_connection * con,struct ceph_msg * msg)54074972cf60SIlya Dryomov static void osd_dispatch(struct ceph_connection *con, struct ceph_msg *msg)
54083d14c5d2SYehuda Sadeh {
54093d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
54105aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
54113d14c5d2SYehuda Sadeh 	int type = le16_to_cpu(msg->hdr.type);
54123d14c5d2SYehuda Sadeh 
54133d14c5d2SYehuda Sadeh 	switch (type) {
54143d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_MAP:
54153d14c5d2SYehuda Sadeh 		ceph_osdc_handle_map(osdc, msg);
54163d14c5d2SYehuda Sadeh 		break;
54173d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_OPREPLY:
54185aea3dcdSIlya Dryomov 		handle_reply(osd, msg);
54193d14c5d2SYehuda Sadeh 		break;
5420a02a946dSIlya Dryomov 	case CEPH_MSG_OSD_BACKOFF:
5421a02a946dSIlya Dryomov 		handle_backoff(osd, msg);
5422a02a946dSIlya Dryomov 		break;
5423a40c4f10SYehuda Sadeh 	case CEPH_MSG_WATCH_NOTIFY:
5424a40c4f10SYehuda Sadeh 		handle_watch_notify(osdc, msg);
5425a40c4f10SYehuda Sadeh 		break;
54263d14c5d2SYehuda Sadeh 
54273d14c5d2SYehuda Sadeh 	default:
54283d14c5d2SYehuda Sadeh 		pr_err("received unknown message type %d %s\n", type,
54293d14c5d2SYehuda Sadeh 		       ceph_msg_type_name(type));
54303d14c5d2SYehuda Sadeh 	}
54315aea3dcdSIlya Dryomov 
54323d14c5d2SYehuda Sadeh 	ceph_msg_put(msg);
54333d14c5d2SYehuda Sadeh }
54343d14c5d2SYehuda Sadeh 
5435f628d799SJeff Layton /* How much sparse data was requested? */
sparse_data_requested(struct ceph_osd_request * req)5436f628d799SJeff Layton static u64 sparse_data_requested(struct ceph_osd_request *req)
5437f628d799SJeff Layton {
5438f628d799SJeff Layton 	u64 len = 0;
5439f628d799SJeff Layton 
5440f628d799SJeff Layton 	if (req->r_flags & CEPH_OSD_FLAG_READ) {
5441f628d799SJeff Layton 		int i;
5442f628d799SJeff Layton 
5443f628d799SJeff Layton 		for (i = 0; i < req->r_num_ops; ++i) {
5444f628d799SJeff Layton 			struct ceph_osd_req_op *op = &req->r_ops[i];
5445f628d799SJeff Layton 
5446f628d799SJeff Layton 			if (op->op == CEPH_OSD_OP_SPARSE_READ)
5447f628d799SJeff Layton 				len += op->extent.length;
5448f628d799SJeff Layton 		}
5449f628d799SJeff Layton 	}
5450f628d799SJeff Layton 	return len;
5451f628d799SJeff Layton }
5452f628d799SJeff Layton 
54533d14c5d2SYehuda Sadeh /*
5454d15f9d69SIlya Dryomov  * Lookup and return message for incoming reply.  Don't try to do
5455d15f9d69SIlya Dryomov  * anything about a larger than preallocated data portion of the
5456d15f9d69SIlya Dryomov  * message at the moment - for now, just skip the message.
54573d14c5d2SYehuda Sadeh  */
get_reply(struct ceph_connection * con,struct ceph_msg_header * hdr,int * skip)54583d14c5d2SYehuda Sadeh static struct ceph_msg *get_reply(struct ceph_connection *con,
54593d14c5d2SYehuda Sadeh 				  struct ceph_msg_header *hdr,
54603d14c5d2SYehuda Sadeh 				  int *skip)
54613d14c5d2SYehuda Sadeh {
54623d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
54633d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = osd->o_osdc;
54645aea3dcdSIlya Dryomov 	struct ceph_msg *m = NULL;
54653d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
54663f0a4ac5SIlya Dryomov 	int front_len = le32_to_cpu(hdr->front_len);
54673d14c5d2SYehuda Sadeh 	int data_len = le32_to_cpu(hdr->data_len);
54685aea3dcdSIlya Dryomov 	u64 tid = le64_to_cpu(hdr->tid);
5469f628d799SJeff Layton 	u64 srlen;
54703d14c5d2SYehuda Sadeh 
54715aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
54725aea3dcdSIlya Dryomov 	if (!osd_registered(osd)) {
54735aea3dcdSIlya Dryomov 		dout("%s osd%d unknown, skipping\n", __func__, osd->o_osd);
54745aea3dcdSIlya Dryomov 		*skip = 1;
54755aea3dcdSIlya Dryomov 		goto out_unlock_osdc;
54765aea3dcdSIlya Dryomov 	}
54775aea3dcdSIlya Dryomov 	WARN_ON(osd->o_osd != le64_to_cpu(hdr->src.num));
54785aea3dcdSIlya Dryomov 
54795aea3dcdSIlya Dryomov 	mutex_lock(&osd->lock);
54805aea3dcdSIlya Dryomov 	req = lookup_request(&osd->o_requests, tid);
54813d14c5d2SYehuda Sadeh 	if (!req) {
5482cd8140c6SIlya Dryomov 		dout("%s osd%d tid %llu unknown, skipping\n", __func__,
5483cd8140c6SIlya Dryomov 		     osd->o_osd, tid);
5484d15f9d69SIlya Dryomov 		*skip = 1;
54855aea3dcdSIlya Dryomov 		goto out_unlock_session;
54863d14c5d2SYehuda Sadeh 	}
54873d14c5d2SYehuda Sadeh 
54888921d114SAlex Elder 	ceph_msg_revoke_incoming(req->r_reply);
54893d14c5d2SYehuda Sadeh 
5490f2be82b0SIlya Dryomov 	if (front_len > req->r_reply->front_alloc_len) {
5491d15f9d69SIlya Dryomov 		pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
5492d15f9d69SIlya Dryomov 			__func__, osd->o_osd, req->r_tid, front_len,
5493d15f9d69SIlya Dryomov 			req->r_reply->front_alloc_len);
54943f0a4ac5SIlya Dryomov 		m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
54953f0a4ac5SIlya Dryomov 				 false);
54963d14c5d2SYehuda Sadeh 		if (!m)
54975aea3dcdSIlya Dryomov 			goto out_unlock_session;
54983d14c5d2SYehuda Sadeh 		ceph_msg_put(req->r_reply);
54993d14c5d2SYehuda Sadeh 		req->r_reply = m;
55003d14c5d2SYehuda Sadeh 	}
55013d14c5d2SYehuda Sadeh 
5502f628d799SJeff Layton 	srlen = sparse_data_requested(req);
5503f628d799SJeff Layton 	if (!srlen && data_len > req->r_reply->data_length) {
5504d15f9d69SIlya Dryomov 		pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
5505d15f9d69SIlya Dryomov 			__func__, osd->o_osd, req->r_tid, data_len,
5506d15f9d69SIlya Dryomov 			req->r_reply->data_length);
55073d14c5d2SYehuda Sadeh 		m = NULL;
5508d15f9d69SIlya Dryomov 		*skip = 1;
55095aea3dcdSIlya Dryomov 		goto out_unlock_session;
55103d14c5d2SYehuda Sadeh 	}
5511d15f9d69SIlya Dryomov 
5512d15f9d69SIlya Dryomov 	m = ceph_msg_get(req->r_reply);
5513da9c33a7SXiubo Li 	m->sparse_read_total = srlen;
5514f628d799SJeff Layton 
55153d14c5d2SYehuda Sadeh 	dout("get_reply tid %lld %p\n", tid, m);
55163d14c5d2SYehuda Sadeh 
55175aea3dcdSIlya Dryomov out_unlock_session:
55185aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
55195aea3dcdSIlya Dryomov out_unlock_osdc:
55205aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
55213d14c5d2SYehuda Sadeh 	return m;
55223d14c5d2SYehuda Sadeh }
55233d14c5d2SYehuda Sadeh 
alloc_msg_with_page_vector(struct ceph_msg_header * hdr)552419079203SIlya Dryomov static struct ceph_msg *alloc_msg_with_page_vector(struct ceph_msg_header *hdr)
552519079203SIlya Dryomov {
552619079203SIlya Dryomov 	struct ceph_msg *m;
552719079203SIlya Dryomov 	int type = le16_to_cpu(hdr->type);
552819079203SIlya Dryomov 	u32 front_len = le32_to_cpu(hdr->front_len);
552919079203SIlya Dryomov 	u32 data_len = le32_to_cpu(hdr->data_len);
553019079203SIlya Dryomov 
55310d9c1ab3SIlya Dryomov 	m = ceph_msg_new2(type, front_len, 1, GFP_NOIO, false);
553219079203SIlya Dryomov 	if (!m)
553319079203SIlya Dryomov 		return NULL;
553419079203SIlya Dryomov 
553519079203SIlya Dryomov 	if (data_len) {
553619079203SIlya Dryomov 		struct page **pages;
553719079203SIlya Dryomov 
553819079203SIlya Dryomov 		pages = ceph_alloc_page_vector(calc_pages_for(0, data_len),
553919079203SIlya Dryomov 					       GFP_NOIO);
5540c22e853aSWei Yongjun 		if (IS_ERR(pages)) {
554119079203SIlya Dryomov 			ceph_msg_put(m);
554219079203SIlya Dryomov 			return NULL;
554319079203SIlya Dryomov 		}
554419079203SIlya Dryomov 
5545e8862740SIlya Dryomov 		ceph_msg_data_add_pages(m, pages, data_len, 0, true);
554619079203SIlya Dryomov 	}
554719079203SIlya Dryomov 
554819079203SIlya Dryomov 	return m;
554919079203SIlya Dryomov }
555019079203SIlya Dryomov 
osd_alloc_msg(struct ceph_connection * con,struct ceph_msg_header * hdr,int * skip)55514972cf60SIlya Dryomov static struct ceph_msg *osd_alloc_msg(struct ceph_connection *con,
55523d14c5d2SYehuda Sadeh 				      struct ceph_msg_header *hdr,
55533d14c5d2SYehuda Sadeh 				      int *skip)
55543d14c5d2SYehuda Sadeh {
55553d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
55563d14c5d2SYehuda Sadeh 	int type = le16_to_cpu(hdr->type);
55573d14c5d2SYehuda Sadeh 
55581c20f2d2SAlex Elder 	*skip = 0;
55593d14c5d2SYehuda Sadeh 	switch (type) {
55603d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_MAP:
5561a02a946dSIlya Dryomov 	case CEPH_MSG_OSD_BACKOFF:
5562a40c4f10SYehuda Sadeh 	case CEPH_MSG_WATCH_NOTIFY:
556319079203SIlya Dryomov 		return alloc_msg_with_page_vector(hdr);
55643d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_OPREPLY:
55653d14c5d2SYehuda Sadeh 		return get_reply(con, hdr, skip);
55663d14c5d2SYehuda Sadeh 	default:
55675aea3dcdSIlya Dryomov 		pr_warn("%s osd%d unknown msg type %d, skipping\n", __func__,
55685aea3dcdSIlya Dryomov 			osd->o_osd, type);
55693d14c5d2SYehuda Sadeh 		*skip = 1;
55703d14c5d2SYehuda Sadeh 		return NULL;
55713d14c5d2SYehuda Sadeh 	}
55723d14c5d2SYehuda Sadeh }
55733d14c5d2SYehuda Sadeh 
55743d14c5d2SYehuda Sadeh /*
55753d14c5d2SYehuda Sadeh  * Wrappers to refcount containing ceph_osd struct
55763d14c5d2SYehuda Sadeh  */
osd_get_con(struct ceph_connection * con)55774972cf60SIlya Dryomov static struct ceph_connection *osd_get_con(struct ceph_connection *con)
55783d14c5d2SYehuda Sadeh {
55793d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
55803d14c5d2SYehuda Sadeh 	if (get_osd(osd))
55813d14c5d2SYehuda Sadeh 		return con;
55823d14c5d2SYehuda Sadeh 	return NULL;
55833d14c5d2SYehuda Sadeh }
55843d14c5d2SYehuda Sadeh 
osd_put_con(struct ceph_connection * con)55854972cf60SIlya Dryomov static void osd_put_con(struct ceph_connection *con)
55863d14c5d2SYehuda Sadeh {
55873d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
55883d14c5d2SYehuda Sadeh 	put_osd(osd);
55893d14c5d2SYehuda Sadeh }
55903d14c5d2SYehuda Sadeh 
55913d14c5d2SYehuda Sadeh /*
55923d14c5d2SYehuda Sadeh  * authentication
55933d14c5d2SYehuda Sadeh  */
5594cd1a677cSIlya Dryomov 
5595a3530df3SAlex Elder /*
5596a3530df3SAlex Elder  * Note: returned pointer is the address of a structure that's
5597a3530df3SAlex Elder  * managed separately.  Caller must *not* attempt to free it.
5598a3530df3SAlex Elder  */
55994972cf60SIlya Dryomov static struct ceph_auth_handshake *
osd_get_authorizer(struct ceph_connection * con,int * proto,int force_new)56004972cf60SIlya Dryomov osd_get_authorizer(struct ceph_connection *con, int *proto, int force_new)
56013d14c5d2SYehuda Sadeh {
56023d14c5d2SYehuda Sadeh 	struct ceph_osd *o = con->private;
56033d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = o->o_osdc;
56043d14c5d2SYehuda Sadeh 	struct ceph_auth_client *ac = osdc->client->monc.auth;
560574f1869fSAlex Elder 	struct ceph_auth_handshake *auth = &o->o_auth;
5606ce287162SIlya Dryomov 	int ret;
56073d14c5d2SYehuda Sadeh 
5608ce287162SIlya Dryomov 	ret = __ceph_auth_get_authorizer(ac, auth, CEPH_ENTITY_TYPE_OSD,
5609ce287162SIlya Dryomov 					 force_new, proto, NULL, NULL);
56103d14c5d2SYehuda Sadeh 	if (ret)
5611a3530df3SAlex Elder 		return ERR_PTR(ret);
561274f1869fSAlex Elder 
5613a3530df3SAlex Elder 	return auth;
56143d14c5d2SYehuda Sadeh }
56153d14c5d2SYehuda Sadeh 
osd_add_authorizer_challenge(struct ceph_connection * con,void * challenge_buf,int challenge_buf_len)56164972cf60SIlya Dryomov static int osd_add_authorizer_challenge(struct ceph_connection *con,
56176daca13dSIlya Dryomov 				    void *challenge_buf, int challenge_buf_len)
56186daca13dSIlya Dryomov {
56196daca13dSIlya Dryomov 	struct ceph_osd *o = con->private;
56206daca13dSIlya Dryomov 	struct ceph_osd_client *osdc = o->o_osdc;
56216daca13dSIlya Dryomov 	struct ceph_auth_client *ac = osdc->client->monc.auth;
56226daca13dSIlya Dryomov 
56236daca13dSIlya Dryomov 	return ceph_auth_add_authorizer_challenge(ac, o->o_auth.authorizer,
56246daca13dSIlya Dryomov 					    challenge_buf, challenge_buf_len);
56256daca13dSIlya Dryomov }
56263d14c5d2SYehuda Sadeh 
osd_verify_authorizer_reply(struct ceph_connection * con)56274972cf60SIlya Dryomov static int osd_verify_authorizer_reply(struct ceph_connection *con)
56283d14c5d2SYehuda Sadeh {
56293d14c5d2SYehuda Sadeh 	struct ceph_osd *o = con->private;
56303d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = o->o_osdc;
56313d14c5d2SYehuda Sadeh 	struct ceph_auth_client *ac = osdc->client->monc.auth;
5632285ea34fSIlya Dryomov 	struct ceph_auth_handshake *auth = &o->o_auth;
56333d14c5d2SYehuda Sadeh 
5634285ea34fSIlya Dryomov 	return ceph_auth_verify_authorizer_reply(ac, auth->authorizer,
5635285ea34fSIlya Dryomov 		auth->authorizer_reply_buf, auth->authorizer_reply_buf_len,
5636285ea34fSIlya Dryomov 		NULL, NULL, NULL, NULL);
56373d14c5d2SYehuda Sadeh }
56383d14c5d2SYehuda Sadeh 
osd_invalidate_authorizer(struct ceph_connection * con)56394972cf60SIlya Dryomov static int osd_invalidate_authorizer(struct ceph_connection *con)
56403d14c5d2SYehuda Sadeh {
56413d14c5d2SYehuda Sadeh 	struct ceph_osd *o = con->private;
56423d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = o->o_osdc;
56433d14c5d2SYehuda Sadeh 	struct ceph_auth_client *ac = osdc->client->monc.auth;
56443d14c5d2SYehuda Sadeh 
564527859f97SSage Weil 	ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
56463d14c5d2SYehuda Sadeh 	return ceph_monc_validate_auth(&osdc->client->monc);
56473d14c5d2SYehuda Sadeh }
56483d14c5d2SYehuda Sadeh 
osd_get_auth_request(struct ceph_connection * con,void * buf,int * buf_len,void ** authorizer,int * authorizer_len)5649cd1a677cSIlya Dryomov static int osd_get_auth_request(struct ceph_connection *con,
5650cd1a677cSIlya Dryomov 				void *buf, int *buf_len,
5651cd1a677cSIlya Dryomov 				void **authorizer, int *authorizer_len)
5652cd1a677cSIlya Dryomov {
5653cd1a677cSIlya Dryomov 	struct ceph_osd *o = con->private;
5654cd1a677cSIlya Dryomov 	struct ceph_auth_client *ac = o->o_osdc->client->monc.auth;
5655cd1a677cSIlya Dryomov 	struct ceph_auth_handshake *auth = &o->o_auth;
5656cd1a677cSIlya Dryomov 	int ret;
5657cd1a677cSIlya Dryomov 
5658cd1a677cSIlya Dryomov 	ret = ceph_auth_get_authorizer(ac, auth, CEPH_ENTITY_TYPE_OSD,
5659cd1a677cSIlya Dryomov 				       buf, buf_len);
5660cd1a677cSIlya Dryomov 	if (ret)
5661cd1a677cSIlya Dryomov 		return ret;
5662cd1a677cSIlya Dryomov 
5663cd1a677cSIlya Dryomov 	*authorizer = auth->authorizer_buf;
5664cd1a677cSIlya Dryomov 	*authorizer_len = auth->authorizer_buf_len;
5665cd1a677cSIlya Dryomov 	return 0;
5666cd1a677cSIlya Dryomov }
5667cd1a677cSIlya Dryomov 
osd_handle_auth_reply_more(struct ceph_connection * con,void * reply,int reply_len,void * buf,int * buf_len,void ** authorizer,int * authorizer_len)5668cd1a677cSIlya Dryomov static int osd_handle_auth_reply_more(struct ceph_connection *con,
5669cd1a677cSIlya Dryomov 				      void *reply, int reply_len,
5670cd1a677cSIlya Dryomov 				      void *buf, int *buf_len,
5671cd1a677cSIlya Dryomov 				      void **authorizer, int *authorizer_len)
5672cd1a677cSIlya Dryomov {
5673cd1a677cSIlya Dryomov 	struct ceph_osd *o = con->private;
5674cd1a677cSIlya Dryomov 	struct ceph_auth_client *ac = o->o_osdc->client->monc.auth;
5675cd1a677cSIlya Dryomov 	struct ceph_auth_handshake *auth = &o->o_auth;
5676cd1a677cSIlya Dryomov 	int ret;
5677cd1a677cSIlya Dryomov 
5678cd1a677cSIlya Dryomov 	ret = ceph_auth_handle_svc_reply_more(ac, auth, reply, reply_len,
5679cd1a677cSIlya Dryomov 					      buf, buf_len);
5680cd1a677cSIlya Dryomov 	if (ret)
5681cd1a677cSIlya Dryomov 		return ret;
5682cd1a677cSIlya Dryomov 
5683cd1a677cSIlya Dryomov 	*authorizer = auth->authorizer_buf;
5684cd1a677cSIlya Dryomov 	*authorizer_len = auth->authorizer_buf_len;
5685cd1a677cSIlya Dryomov 	return 0;
5686cd1a677cSIlya Dryomov }
5687cd1a677cSIlya Dryomov 
osd_handle_auth_done(struct ceph_connection * con,u64 global_id,void * reply,int reply_len,u8 * session_key,int * session_key_len,u8 * con_secret,int * con_secret_len)5688cd1a677cSIlya Dryomov static int osd_handle_auth_done(struct ceph_connection *con,
5689cd1a677cSIlya Dryomov 				u64 global_id, void *reply, int reply_len,
5690cd1a677cSIlya Dryomov 				u8 *session_key, int *session_key_len,
5691cd1a677cSIlya Dryomov 				u8 *con_secret, int *con_secret_len)
5692cd1a677cSIlya Dryomov {
5693cd1a677cSIlya Dryomov 	struct ceph_osd *o = con->private;
5694cd1a677cSIlya Dryomov 	struct ceph_auth_client *ac = o->o_osdc->client->monc.auth;
5695cd1a677cSIlya Dryomov 	struct ceph_auth_handshake *auth = &o->o_auth;
5696cd1a677cSIlya Dryomov 
5697cd1a677cSIlya Dryomov 	return ceph_auth_handle_svc_reply_done(ac, auth, reply, reply_len,
5698cd1a677cSIlya Dryomov 					       session_key, session_key_len,
5699cd1a677cSIlya Dryomov 					       con_secret, con_secret_len);
5700cd1a677cSIlya Dryomov }
5701cd1a677cSIlya Dryomov 
osd_handle_auth_bad_method(struct ceph_connection * con,int used_proto,int result,const int * allowed_protos,int proto_cnt,const int * allowed_modes,int mode_cnt)5702cd1a677cSIlya Dryomov static int osd_handle_auth_bad_method(struct ceph_connection *con,
5703cd1a677cSIlya Dryomov 				      int used_proto, int result,
5704cd1a677cSIlya Dryomov 				      const int *allowed_protos, int proto_cnt,
5705cd1a677cSIlya Dryomov 				      const int *allowed_modes, int mode_cnt)
5706cd1a677cSIlya Dryomov {
5707cd1a677cSIlya Dryomov 	struct ceph_osd *o = con->private;
5708cd1a677cSIlya Dryomov 	struct ceph_mon_client *monc = &o->o_osdc->client->monc;
5709cd1a677cSIlya Dryomov 	int ret;
5710cd1a677cSIlya Dryomov 
5711cd1a677cSIlya Dryomov 	if (ceph_auth_handle_bad_authorizer(monc->auth, CEPH_ENTITY_TYPE_OSD,
5712cd1a677cSIlya Dryomov 					    used_proto, result,
5713cd1a677cSIlya Dryomov 					    allowed_protos, proto_cnt,
5714cd1a677cSIlya Dryomov 					    allowed_modes, mode_cnt)) {
5715cd1a677cSIlya Dryomov 		ret = ceph_monc_validate_auth(monc);
5716cd1a677cSIlya Dryomov 		if (ret)
5717cd1a677cSIlya Dryomov 			return ret;
5718cd1a677cSIlya Dryomov 	}
5719cd1a677cSIlya Dryomov 
5720cd1a677cSIlya Dryomov 	return -EACCES;
5721cd1a677cSIlya Dryomov }
5722cd1a677cSIlya Dryomov 
osd_reencode_message(struct ceph_msg * msg)57238cb441c0SIlya Dryomov static void osd_reencode_message(struct ceph_msg *msg)
57248cb441c0SIlya Dryomov {
5725914902afSIlya Dryomov 	int type = le16_to_cpu(msg->hdr.type);
5726914902afSIlya Dryomov 
5727914902afSIlya Dryomov 	if (type == CEPH_MSG_OSD_OP)
57288cb441c0SIlya Dryomov 		encode_request_finish(msg);
57298cb441c0SIlya Dryomov }
57308cb441c0SIlya Dryomov 
osd_sign_message(struct ceph_msg * msg)573179dbd1baSIlya Dryomov static int osd_sign_message(struct ceph_msg *msg)
573233d07337SYan, Zheng {
573379dbd1baSIlya Dryomov 	struct ceph_osd *o = msg->con->private;
573433d07337SYan, Zheng 	struct ceph_auth_handshake *auth = &o->o_auth;
573579dbd1baSIlya Dryomov 
573633d07337SYan, Zheng 	return ceph_auth_sign_message(auth, msg);
573733d07337SYan, Zheng }
573833d07337SYan, Zheng 
osd_check_message_signature(struct ceph_msg * msg)573979dbd1baSIlya Dryomov static int osd_check_message_signature(struct ceph_msg *msg)
574033d07337SYan, Zheng {
574179dbd1baSIlya Dryomov 	struct ceph_osd *o = msg->con->private;
574233d07337SYan, Zheng 	struct ceph_auth_handshake *auth = &o->o_auth;
574379dbd1baSIlya Dryomov 
574433d07337SYan, Zheng 	return ceph_auth_check_message_signature(auth, msg);
574533d07337SYan, Zheng }
574633d07337SYan, Zheng 
advance_cursor(struct ceph_msg_data_cursor * cursor,size_t len,bool zero)5747f628d799SJeff Layton static void advance_cursor(struct ceph_msg_data_cursor *cursor, size_t len,
5748f628d799SJeff Layton 			   bool zero)
5749f628d799SJeff Layton {
5750f628d799SJeff Layton 	while (len) {
5751f628d799SJeff Layton 		struct page *page;
5752f628d799SJeff Layton 		size_t poff, plen;
5753f628d799SJeff Layton 
5754f628d799SJeff Layton 		page = ceph_msg_data_next(cursor, &poff, &plen);
5755f628d799SJeff Layton 		if (plen > len)
5756f628d799SJeff Layton 			plen = len;
5757f628d799SJeff Layton 		if (zero)
5758f628d799SJeff Layton 			zero_user_segment(page, poff, poff + plen);
5759f628d799SJeff Layton 		len -= plen;
5760f628d799SJeff Layton 		ceph_msg_data_advance(cursor, plen);
5761f628d799SJeff Layton 	}
5762f628d799SJeff Layton }
5763f628d799SJeff Layton 
prep_next_sparse_read(struct ceph_connection * con,struct ceph_msg_data_cursor * cursor)5764f628d799SJeff Layton static int prep_next_sparse_read(struct ceph_connection *con,
5765f628d799SJeff Layton 				 struct ceph_msg_data_cursor *cursor)
5766f628d799SJeff Layton {
5767f628d799SJeff Layton 	struct ceph_osd *o = con->private;
5768f628d799SJeff Layton 	struct ceph_sparse_read *sr = &o->o_sparse_read;
5769f628d799SJeff Layton 	struct ceph_osd_request *req;
5770f628d799SJeff Layton 	struct ceph_osd_req_op *op;
5771f628d799SJeff Layton 
5772f628d799SJeff Layton 	spin_lock(&o->o_requests_lock);
5773f628d799SJeff Layton 	req = lookup_request(&o->o_requests, le64_to_cpu(con->in_msg->hdr.tid));
5774f628d799SJeff Layton 	if (!req) {
5775f628d799SJeff Layton 		spin_unlock(&o->o_requests_lock);
5776f628d799SJeff Layton 		return -EBADR;
5777f628d799SJeff Layton 	}
5778f628d799SJeff Layton 
5779f628d799SJeff Layton 	if (o->o_sparse_op_idx < 0) {
5780da9c33a7SXiubo Li 		dout("%s: [%d] starting new sparse read req\n",
5781da9c33a7SXiubo Li 		     __func__, o->o_osd);
5782f628d799SJeff Layton 	} else {
5783f628d799SJeff Layton 		u64 end;
5784f628d799SJeff Layton 
5785f628d799SJeff Layton 		op = &req->r_ops[o->o_sparse_op_idx];
5786f628d799SJeff Layton 
5787f628d799SJeff Layton 		WARN_ON_ONCE(op->extent.sparse_ext);
5788f628d799SJeff Layton 
5789f628d799SJeff Layton 		/* hand back buffer we took earlier */
5790f628d799SJeff Layton 		op->extent.sparse_ext = sr->sr_extent;
5791f628d799SJeff Layton 		sr->sr_extent = NULL;
5792f628d799SJeff Layton 		op->extent.sparse_ext_cnt = sr->sr_count;
5793f628d799SJeff Layton 		sr->sr_ext_len = 0;
5794f628d799SJeff Layton 		dout("%s: [%d] completed extent array len %d cursor->resid %zd\n",
5795f628d799SJeff Layton 		     __func__, o->o_osd, op->extent.sparse_ext_cnt, cursor->resid);
5796f628d799SJeff Layton 		/* Advance to end of data for this operation */
5797f628d799SJeff Layton 		end = ceph_sparse_ext_map_end(op);
5798f628d799SJeff Layton 		if (end < sr->sr_req_len)
5799f628d799SJeff Layton 			advance_cursor(cursor, sr->sr_req_len - end, false);
5800f628d799SJeff Layton 	}
5801f628d799SJeff Layton 
5802f628d799SJeff Layton 	ceph_init_sparse_read(sr);
5803f628d799SJeff Layton 
5804f628d799SJeff Layton 	/* find next op in this request (if any) */
5805f628d799SJeff Layton 	while (++o->o_sparse_op_idx < req->r_num_ops) {
5806f628d799SJeff Layton 		op = &req->r_ops[o->o_sparse_op_idx];
5807f628d799SJeff Layton 		if (op->op == CEPH_OSD_OP_SPARSE_READ)
5808f628d799SJeff Layton 			goto found;
5809f628d799SJeff Layton 	}
5810f628d799SJeff Layton 
5811f628d799SJeff Layton 	/* reset for next sparse read request */
5812f628d799SJeff Layton 	spin_unlock(&o->o_requests_lock);
5813f628d799SJeff Layton 	o->o_sparse_op_idx = -1;
5814f628d799SJeff Layton 	return 0;
5815f628d799SJeff Layton found:
5816f628d799SJeff Layton 	sr->sr_req_off = op->extent.offset;
5817f628d799SJeff Layton 	sr->sr_req_len = op->extent.length;
5818f628d799SJeff Layton 	sr->sr_pos = sr->sr_req_off;
5819f628d799SJeff Layton 	dout("%s: [%d] new sparse read op at idx %d 0x%llx~0x%llx\n", __func__,
5820f628d799SJeff Layton 	     o->o_osd, o->o_sparse_op_idx, sr->sr_req_off, sr->sr_req_len);
5821f628d799SJeff Layton 
5822f628d799SJeff Layton 	/* hand off request's sparse extent map buffer */
5823f628d799SJeff Layton 	sr->sr_ext_len = op->extent.sparse_ext_cnt;
5824f628d799SJeff Layton 	op->extent.sparse_ext_cnt = 0;
5825f628d799SJeff Layton 	sr->sr_extent = op->extent.sparse_ext;
5826f628d799SJeff Layton 	op->extent.sparse_ext = NULL;
5827f628d799SJeff Layton 
5828f628d799SJeff Layton 	spin_unlock(&o->o_requests_lock);
5829f628d799SJeff Layton 	return 1;
5830f628d799SJeff Layton }
5831f628d799SJeff Layton 
5832f628d799SJeff Layton #ifdef __BIG_ENDIAN
convert_extent_map(struct ceph_sparse_read * sr)5833f628d799SJeff Layton static inline void convert_extent_map(struct ceph_sparse_read *sr)
5834f628d799SJeff Layton {
5835f628d799SJeff Layton 	int i;
5836f628d799SJeff Layton 
5837f628d799SJeff Layton 	for (i = 0; i < sr->sr_count; i++) {
5838f628d799SJeff Layton 		struct ceph_sparse_extent *ext = &sr->sr_extent[i];
5839f628d799SJeff Layton 
5840f628d799SJeff Layton 		ext->off = le64_to_cpu((__force __le64)ext->off);
5841f628d799SJeff Layton 		ext->len = le64_to_cpu((__force __le64)ext->len);
5842f628d799SJeff Layton 	}
5843f628d799SJeff Layton }
5844f628d799SJeff Layton #else
convert_extent_map(struct ceph_sparse_read * sr)5845f628d799SJeff Layton static inline void convert_extent_map(struct ceph_sparse_read *sr)
5846f628d799SJeff Layton {
5847f628d799SJeff Layton }
5848f628d799SJeff Layton #endif
5849f628d799SJeff Layton 
5850f628d799SJeff Layton #define MAX_EXTENTS 4096
5851f628d799SJeff Layton 
osd_sparse_read(struct ceph_connection * con,struct ceph_msg_data_cursor * cursor,char ** pbuf)5852f628d799SJeff Layton static int osd_sparse_read(struct ceph_connection *con,
5853f628d799SJeff Layton 			   struct ceph_msg_data_cursor *cursor,
5854f628d799SJeff Layton 			   char **pbuf)
5855f628d799SJeff Layton {
5856f628d799SJeff Layton 	struct ceph_osd *o = con->private;
5857f628d799SJeff Layton 	struct ceph_sparse_read *sr = &o->o_sparse_read;
5858f628d799SJeff Layton 	u32 count = sr->sr_count;
5859*7d7046a6SXiubo Li 	u64 eoff, elen, len = 0;
5860*7d7046a6SXiubo Li 	int i, ret;
5861f628d799SJeff Layton 
5862f628d799SJeff Layton 	switch (sr->sr_state) {
5863f628d799SJeff Layton 	case CEPH_SPARSE_READ_HDR:
5864f628d799SJeff Layton next_op:
5865f628d799SJeff Layton 		ret = prep_next_sparse_read(con, cursor);
5866f628d799SJeff Layton 		if (ret <= 0)
5867f628d799SJeff Layton 			return ret;
5868f628d799SJeff Layton 
5869f628d799SJeff Layton 		/* number of extents */
5870f628d799SJeff Layton 		ret = sizeof(sr->sr_count);
5871f628d799SJeff Layton 		*pbuf = (char *)&sr->sr_count;
5872f628d799SJeff Layton 		sr->sr_state = CEPH_SPARSE_READ_EXTENTS;
5873f628d799SJeff Layton 		break;
5874f628d799SJeff Layton 	case CEPH_SPARSE_READ_EXTENTS:
5875f628d799SJeff Layton 		/* Convert sr_count to host-endian */
5876f628d799SJeff Layton 		count = le32_to_cpu((__force __le32)sr->sr_count);
5877f628d799SJeff Layton 		sr->sr_count = count;
5878f628d799SJeff Layton 		dout("[%d] got %u extents\n", o->o_osd, count);
5879f628d799SJeff Layton 
5880f628d799SJeff Layton 		if (count > 0) {
5881f628d799SJeff Layton 			if (!sr->sr_extent || count > sr->sr_ext_len) {
5882f628d799SJeff Layton 				/*
5883f628d799SJeff Layton 				 * Apply a hard cap to the number of extents.
5884f628d799SJeff Layton 				 * If we have more, assume something is wrong.
5885f628d799SJeff Layton 				 */
5886f628d799SJeff Layton 				if (count > MAX_EXTENTS) {
5887f628d799SJeff Layton 					dout("%s: OSD returned 0x%x extents in a single reply!\n",
5888f628d799SJeff Layton 					     __func__, count);
5889f628d799SJeff Layton 					return -EREMOTEIO;
5890f628d799SJeff Layton 				}
5891f628d799SJeff Layton 
5892f628d799SJeff Layton 				/* no extent array provided, or too short */
5893f628d799SJeff Layton 				kfree(sr->sr_extent);
5894f628d799SJeff Layton 				sr->sr_extent = kmalloc_array(count,
5895f628d799SJeff Layton 							      sizeof(*sr->sr_extent),
5896f628d799SJeff Layton 							      GFP_NOIO);
5897f628d799SJeff Layton 				if (!sr->sr_extent)
5898f628d799SJeff Layton 					return -ENOMEM;
5899f628d799SJeff Layton 				sr->sr_ext_len = count;
5900f628d799SJeff Layton 			}
5901f628d799SJeff Layton 			ret = count * sizeof(*sr->sr_extent);
5902f628d799SJeff Layton 			*pbuf = (char *)sr->sr_extent;
5903f628d799SJeff Layton 			sr->sr_state = CEPH_SPARSE_READ_DATA_LEN;
5904f628d799SJeff Layton 			break;
5905f628d799SJeff Layton 		}
5906f628d799SJeff Layton 		/* No extents? Read data len */
5907f628d799SJeff Layton 		fallthrough;
5908f628d799SJeff Layton 	case CEPH_SPARSE_READ_DATA_LEN:
5909f628d799SJeff Layton 		convert_extent_map(sr);
5910f628d799SJeff Layton 		ret = sizeof(sr->sr_datalen);
5911f628d799SJeff Layton 		*pbuf = (char *)&sr->sr_datalen;
5912*7d7046a6SXiubo Li 		sr->sr_state = CEPH_SPARSE_READ_DATA_PRE;
5913f628d799SJeff Layton 		break;
5914*7d7046a6SXiubo Li 	case CEPH_SPARSE_READ_DATA_PRE:
5915*7d7046a6SXiubo Li 		/* Convert sr_datalen to host-endian */
5916*7d7046a6SXiubo Li 		sr->sr_datalen = le32_to_cpu((__force __le32)sr->sr_datalen);
5917*7d7046a6SXiubo Li 		for (i = 0; i < count; i++)
5918*7d7046a6SXiubo Li 			len += sr->sr_extent[i].len;
5919*7d7046a6SXiubo Li 		if (sr->sr_datalen != len) {
5920*7d7046a6SXiubo Li 			pr_warn_ratelimited("data len %u != extent len %llu\n",
5921*7d7046a6SXiubo Li 					    sr->sr_datalen, len);
5922*7d7046a6SXiubo Li 			return -EREMOTEIO;
5923*7d7046a6SXiubo Li 		}
5924*7d7046a6SXiubo Li 		sr->sr_state = CEPH_SPARSE_READ_DATA;
5925*7d7046a6SXiubo Li 		fallthrough;
5926f628d799SJeff Layton 	case CEPH_SPARSE_READ_DATA:
5927f628d799SJeff Layton 		if (sr->sr_index >= count) {
5928f628d799SJeff Layton 			sr->sr_state = CEPH_SPARSE_READ_HDR;
5929f628d799SJeff Layton 			goto next_op;
5930f628d799SJeff Layton 		}
5931f628d799SJeff Layton 
5932f628d799SJeff Layton 		eoff = sr->sr_extent[sr->sr_index].off;
5933f628d799SJeff Layton 		elen = sr->sr_extent[sr->sr_index].len;
5934f628d799SJeff Layton 
5935f628d799SJeff Layton 		dout("[%d] ext %d off 0x%llx len 0x%llx\n",
5936f628d799SJeff Layton 		     o->o_osd, sr->sr_index, eoff, elen);
5937f628d799SJeff Layton 
5938f628d799SJeff Layton 		if (elen > INT_MAX) {
5939f628d799SJeff Layton 			dout("Sparse read extent length too long (0x%llx)\n",
5940f628d799SJeff Layton 			     elen);
5941f628d799SJeff Layton 			return -EREMOTEIO;
5942f628d799SJeff Layton 		}
5943f628d799SJeff Layton 
5944f628d799SJeff Layton 		/* zero out anything from sr_pos to start of extent */
5945f628d799SJeff Layton 		if (sr->sr_pos < eoff)
5946f628d799SJeff Layton 			advance_cursor(cursor, eoff - sr->sr_pos, true);
5947f628d799SJeff Layton 
5948f628d799SJeff Layton 		/* Set position to end of extent */
5949f628d799SJeff Layton 		sr->sr_pos = eoff + elen;
5950f628d799SJeff Layton 
5951f628d799SJeff Layton 		/* send back the new length and nullify the ptr */
5952f628d799SJeff Layton 		cursor->sr_resid = elen;
5953f628d799SJeff Layton 		ret = elen;
5954f628d799SJeff Layton 		*pbuf = NULL;
5955f628d799SJeff Layton 
5956f628d799SJeff Layton 		/* Bump the array index */
5957f628d799SJeff Layton 		++sr->sr_index;
5958f628d799SJeff Layton 		break;
5959f628d799SJeff Layton 	}
5960f628d799SJeff Layton 	return ret;
5961f628d799SJeff Layton }
5962f628d799SJeff Layton 
59633d14c5d2SYehuda Sadeh static const struct ceph_connection_operations osd_con_ops = {
59644972cf60SIlya Dryomov 	.get = osd_get_con,
59654972cf60SIlya Dryomov 	.put = osd_put_con,
5966f628d799SJeff Layton 	.sparse_read = osd_sparse_read,
59674972cf60SIlya Dryomov 	.alloc_msg = osd_alloc_msg,
59684972cf60SIlya Dryomov 	.dispatch = osd_dispatch,
59694972cf60SIlya Dryomov 	.fault = osd_fault,
59708cb441c0SIlya Dryomov 	.reencode_message = osd_reencode_message,
59714972cf60SIlya Dryomov 	.get_authorizer = osd_get_authorizer,
59724972cf60SIlya Dryomov 	.add_authorizer_challenge = osd_add_authorizer_challenge,
59734972cf60SIlya Dryomov 	.verify_authorizer_reply = osd_verify_authorizer_reply,
59744972cf60SIlya Dryomov 	.invalidate_authorizer = osd_invalidate_authorizer,
597579dbd1baSIlya Dryomov 	.sign_message = osd_sign_message,
597679dbd1baSIlya Dryomov 	.check_message_signature = osd_check_message_signature,
5977cd1a677cSIlya Dryomov 	.get_auth_request = osd_get_auth_request,
5978cd1a677cSIlya Dryomov 	.handle_auth_reply_more = osd_handle_auth_reply_more,
5979cd1a677cSIlya Dryomov 	.handle_auth_done = osd_handle_auth_done,
5980cd1a677cSIlya Dryomov 	.handle_auth_bad_method = osd_handle_auth_bad_method,
59813d14c5d2SYehuda Sadeh };
5982