xref: /openbmc/linux/net/ceph/osd_client.c (revision 78beb0ff)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2a4ce40a9SAlex Elder 
33d14c5d2SYehuda Sadeh #include <linux/ceph/ceph_debug.h>
43d14c5d2SYehuda Sadeh 
53d14c5d2SYehuda Sadeh #include <linux/module.h>
63d14c5d2SYehuda Sadeh #include <linux/err.h>
73d14c5d2SYehuda Sadeh #include <linux/highmem.h>
83d14c5d2SYehuda Sadeh #include <linux/mm.h>
93d14c5d2SYehuda Sadeh #include <linux/pagemap.h>
103d14c5d2SYehuda Sadeh #include <linux/slab.h>
113d14c5d2SYehuda Sadeh #include <linux/uaccess.h>
123d14c5d2SYehuda Sadeh #ifdef CONFIG_BLOCK
133d14c5d2SYehuda Sadeh #include <linux/bio.h>
143d14c5d2SYehuda Sadeh #endif
153d14c5d2SYehuda Sadeh 
168cb441c0SIlya Dryomov #include <linux/ceph/ceph_features.h>
173d14c5d2SYehuda Sadeh #include <linux/ceph/libceph.h>
183d14c5d2SYehuda Sadeh #include <linux/ceph/osd_client.h>
193d14c5d2SYehuda Sadeh #include <linux/ceph/messenger.h>
203d14c5d2SYehuda Sadeh #include <linux/ceph/decode.h>
213d14c5d2SYehuda Sadeh #include <linux/ceph/auth.h>
223d14c5d2SYehuda Sadeh #include <linux/ceph/pagelist.h>
2308c1ac50SIlya Dryomov #include <linux/ceph/striper.h>
243d14c5d2SYehuda Sadeh 
253d14c5d2SYehuda Sadeh #define OSD_OPREPLY_FRONT_LEN	512
263d14c5d2SYehuda Sadeh 
275522ae0bSAlex Elder static struct kmem_cache	*ceph_osd_request_cache;
285522ae0bSAlex Elder 
293d14c5d2SYehuda Sadeh static const struct ceph_connection_operations osd_con_ops;
303d14c5d2SYehuda Sadeh 
313d14c5d2SYehuda Sadeh /*
323d14c5d2SYehuda Sadeh  * Implement client access to distributed object storage cluster.
333d14c5d2SYehuda Sadeh  *
343d14c5d2SYehuda Sadeh  * All data objects are stored within a cluster/cloud of OSDs, or
353d14c5d2SYehuda Sadeh  * "object storage devices."  (Note that Ceph OSDs have _nothing_ to
363d14c5d2SYehuda Sadeh  * do with the T10 OSD extensions to SCSI.)  Ceph OSDs are simply
373d14c5d2SYehuda Sadeh  * remote daemons serving up and coordinating consistent and safe
383d14c5d2SYehuda Sadeh  * access to storage.
393d14c5d2SYehuda Sadeh  *
403d14c5d2SYehuda Sadeh  * Cluster membership and the mapping of data objects onto storage devices
413d14c5d2SYehuda Sadeh  * are described by the osd map.
423d14c5d2SYehuda Sadeh  *
433d14c5d2SYehuda Sadeh  * We keep track of pending OSD requests (read, write), resubmit
443d14c5d2SYehuda Sadeh  * requests to different OSDs when the cluster topology/data layout
453d14c5d2SYehuda Sadeh  * change, or retry the affected requests when the communications
463d14c5d2SYehuda Sadeh  * channel with an OSD is reset.
473d14c5d2SYehuda Sadeh  */
483d14c5d2SYehuda Sadeh 
495aea3dcdSIlya Dryomov static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req);
505aea3dcdSIlya Dryomov static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req);
51922dab61SIlya Dryomov static void link_linger(struct ceph_osd *osd,
52922dab61SIlya Dryomov 			struct ceph_osd_linger_request *lreq);
53922dab61SIlya Dryomov static void unlink_linger(struct ceph_osd *osd,
54922dab61SIlya Dryomov 			  struct ceph_osd_linger_request *lreq);
55a02a946dSIlya Dryomov static void clear_backoffs(struct ceph_osd *osd);
565aea3dcdSIlya Dryomov 
575aea3dcdSIlya Dryomov #if 1
585aea3dcdSIlya Dryomov static inline bool rwsem_is_wrlocked(struct rw_semaphore *sem)
595aea3dcdSIlya Dryomov {
605aea3dcdSIlya Dryomov 	bool wrlocked = true;
615aea3dcdSIlya Dryomov 
625aea3dcdSIlya Dryomov 	if (unlikely(down_read_trylock(sem))) {
635aea3dcdSIlya Dryomov 		wrlocked = false;
645aea3dcdSIlya Dryomov 		up_read(sem);
655aea3dcdSIlya Dryomov 	}
665aea3dcdSIlya Dryomov 
675aea3dcdSIlya Dryomov 	return wrlocked;
685aea3dcdSIlya Dryomov }
695aea3dcdSIlya Dryomov static inline void verify_osdc_locked(struct ceph_osd_client *osdc)
705aea3dcdSIlya Dryomov {
715aea3dcdSIlya Dryomov 	WARN_ON(!rwsem_is_locked(&osdc->lock));
725aea3dcdSIlya Dryomov }
735aea3dcdSIlya Dryomov static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc)
745aea3dcdSIlya Dryomov {
755aea3dcdSIlya Dryomov 	WARN_ON(!rwsem_is_wrlocked(&osdc->lock));
765aea3dcdSIlya Dryomov }
775aea3dcdSIlya Dryomov static inline void verify_osd_locked(struct ceph_osd *osd)
785aea3dcdSIlya Dryomov {
795aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
805aea3dcdSIlya Dryomov 
815aea3dcdSIlya Dryomov 	WARN_ON(!(mutex_is_locked(&osd->lock) &&
825aea3dcdSIlya Dryomov 		  rwsem_is_locked(&osdc->lock)) &&
835aea3dcdSIlya Dryomov 		!rwsem_is_wrlocked(&osdc->lock));
845aea3dcdSIlya Dryomov }
85922dab61SIlya Dryomov static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq)
86922dab61SIlya Dryomov {
87922dab61SIlya Dryomov 	WARN_ON(!mutex_is_locked(&lreq->lock));
88922dab61SIlya Dryomov }
895aea3dcdSIlya Dryomov #else
905aea3dcdSIlya Dryomov static inline void verify_osdc_locked(struct ceph_osd_client *osdc) { }
915aea3dcdSIlya Dryomov static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc) { }
925aea3dcdSIlya Dryomov static inline void verify_osd_locked(struct ceph_osd *osd) { }
93922dab61SIlya Dryomov static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq) { }
945aea3dcdSIlya Dryomov #endif
955aea3dcdSIlya Dryomov 
963d14c5d2SYehuda Sadeh /*
973d14c5d2SYehuda Sadeh  * calculate the mapping of a file extent onto an object, and fill out the
983d14c5d2SYehuda Sadeh  * request accordingly.  shorten extent as necessary if it crosses an
993d14c5d2SYehuda Sadeh  * object boundary.
1003d14c5d2SYehuda Sadeh  *
1013d14c5d2SYehuda Sadeh  * fill osd op in request message.
1023d14c5d2SYehuda Sadeh  */
103dbe0fc41SAlex Elder static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
104a19dadfbSAlex Elder 			u64 *objnum, u64 *objoff, u64 *objlen)
1053d14c5d2SYehuda Sadeh {
10660e56f13SAlex Elder 	u64 orig_len = *plen;
107dccbf080SIlya Dryomov 	u32 xlen;
1083d14c5d2SYehuda Sadeh 
10960e56f13SAlex Elder 	/* object extent? */
110dccbf080SIlya Dryomov 	ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
111dccbf080SIlya Dryomov 					  objoff, &xlen);
112dccbf080SIlya Dryomov 	*objlen = xlen;
11375d1c941SAlex Elder 	if (*objlen < orig_len) {
11475d1c941SAlex Elder 		*plen = *objlen;
11560e56f13SAlex Elder 		dout(" skipping last %llu, final file extent %llu~%llu\n",
11660e56f13SAlex Elder 		     orig_len - *plen, off, *plen);
11760e56f13SAlex Elder 	}
11860e56f13SAlex Elder 
11975d1c941SAlex Elder 	dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
1203ff5f385SAlex Elder 	return 0;
1213d14c5d2SYehuda Sadeh }
1223d14c5d2SYehuda Sadeh 
123c54d47bfSAlex Elder static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
124c54d47bfSAlex Elder {
125c54d47bfSAlex Elder 	memset(osd_data, 0, sizeof (*osd_data));
126c54d47bfSAlex Elder 	osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
127c54d47bfSAlex Elder }
128c54d47bfSAlex Elder 
12989486833SIlya Dryomov /*
13089486833SIlya Dryomov  * Consumes @pages if @own_pages is true.
13189486833SIlya Dryomov  */
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  */
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
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 
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 
17449719778SAlex Elder static struct ceph_osd_data *
17549719778SAlex Elder osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
17649719778SAlex Elder {
17749719778SAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
17849719778SAlex Elder 
17949719778SAlex Elder 	return &osd_req->r_ops[which].raw_data_in;
18049719778SAlex Elder }
18149719778SAlex Elder 
182a4ce40a9SAlex Elder struct ceph_osd_data *
183a4ce40a9SAlex Elder osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
184406e2c9fSAlex Elder 			unsigned int which)
185a4ce40a9SAlex Elder {
186863c7eb5SAlex Elder 	return osd_req_op_data(osd_req, which, extent, osd_data);
187a4ce40a9SAlex Elder }
188a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_osd_data);
189a4ce40a9SAlex Elder 
19049719778SAlex Elder void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
19149719778SAlex Elder 			unsigned int which, struct page **pages,
19249719778SAlex Elder 			u64 length, u32 alignment,
19349719778SAlex Elder 			bool pages_from_pool, bool own_pages)
19449719778SAlex Elder {
19549719778SAlex Elder 	struct ceph_osd_data *osd_data;
19649719778SAlex Elder 
19749719778SAlex Elder 	osd_data = osd_req_op_raw_data_in(osd_req, which);
19849719778SAlex Elder 	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
19949719778SAlex Elder 				pages_from_pool, own_pages);
20049719778SAlex Elder }
20149719778SAlex Elder EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
20249719778SAlex Elder 
203a4ce40a9SAlex Elder void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
204406e2c9fSAlex Elder 			unsigned int which, struct page **pages,
205406e2c9fSAlex Elder 			u64 length, u32 alignment,
206a4ce40a9SAlex Elder 			bool pages_from_pool, bool own_pages)
207a4ce40a9SAlex Elder {
208a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
209a4ce40a9SAlex Elder 
210863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
211a4ce40a9SAlex Elder 	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
212a4ce40a9SAlex Elder 				pages_from_pool, own_pages);
213a4ce40a9SAlex Elder }
214a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
215a4ce40a9SAlex Elder 
216a4ce40a9SAlex Elder void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
217406e2c9fSAlex Elder 			unsigned int which, struct ceph_pagelist *pagelist)
218a4ce40a9SAlex Elder {
219a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
220a4ce40a9SAlex Elder 
221863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
222a4ce40a9SAlex Elder 	ceph_osd_data_pagelist_init(osd_data, pagelist);
223a4ce40a9SAlex Elder }
224a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
225a4ce40a9SAlex Elder 
226a4ce40a9SAlex Elder #ifdef CONFIG_BLOCK
227a4ce40a9SAlex Elder void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
2285359a17dSIlya Dryomov 				    unsigned int which,
2295359a17dSIlya Dryomov 				    struct ceph_bio_iter *bio_pos,
2305359a17dSIlya Dryomov 				    u32 bio_length)
231a4ce40a9SAlex Elder {
232a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
233863c7eb5SAlex Elder 
234863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
2355359a17dSIlya Dryomov 	ceph_osd_data_bio_init(osd_data, bio_pos, bio_length);
236a4ce40a9SAlex Elder }
237a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
238a4ce40a9SAlex Elder #endif /* CONFIG_BLOCK */
239a4ce40a9SAlex Elder 
2400010f705SIlya Dryomov void osd_req_op_extent_osd_data_bvecs(struct ceph_osd_request *osd_req,
2410010f705SIlya Dryomov 				      unsigned int which,
2420010f705SIlya Dryomov 				      struct bio_vec *bvecs, u32 num_bvecs,
2430010f705SIlya Dryomov 				      u32 bytes)
2440010f705SIlya Dryomov {
2450010f705SIlya Dryomov 	struct ceph_osd_data *osd_data;
2460010f705SIlya Dryomov 	struct ceph_bvec_iter it = {
2470010f705SIlya Dryomov 		.bvecs = bvecs,
2480010f705SIlya Dryomov 		.iter = { .bi_size = bytes },
2490010f705SIlya Dryomov 	};
2500010f705SIlya Dryomov 
2510010f705SIlya Dryomov 	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
2520010f705SIlya Dryomov 	ceph_osd_data_bvecs_init(osd_data, &it, num_bvecs);
2530010f705SIlya Dryomov }
2540010f705SIlya Dryomov EXPORT_SYMBOL(osd_req_op_extent_osd_data_bvecs);
2550010f705SIlya Dryomov 
256b9e281c2SIlya Dryomov void osd_req_op_extent_osd_data_bvec_pos(struct ceph_osd_request *osd_req,
257b9e281c2SIlya Dryomov 					 unsigned int which,
258b9e281c2SIlya Dryomov 					 struct ceph_bvec_iter *bvec_pos)
259b9e281c2SIlya Dryomov {
260b9e281c2SIlya Dryomov 	struct ceph_osd_data *osd_data;
261b9e281c2SIlya Dryomov 
262b9e281c2SIlya Dryomov 	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
2630010f705SIlya Dryomov 	ceph_osd_data_bvecs_init(osd_data, bvec_pos, 0);
264b9e281c2SIlya Dryomov }
265b9e281c2SIlya Dryomov EXPORT_SYMBOL(osd_req_op_extent_osd_data_bvec_pos);
266b9e281c2SIlya Dryomov 
267a4ce40a9SAlex Elder static void osd_req_op_cls_request_info_pagelist(
268a4ce40a9SAlex Elder 			struct ceph_osd_request *osd_req,
269a4ce40a9SAlex Elder 			unsigned int which, struct ceph_pagelist *pagelist)
270a4ce40a9SAlex Elder {
271a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
272a4ce40a9SAlex Elder 
273863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, request_info);
274a4ce40a9SAlex Elder 	ceph_osd_data_pagelist_init(osd_data, pagelist);
275a4ce40a9SAlex Elder }
276a4ce40a9SAlex Elder 
27704017e29SAlex Elder void osd_req_op_cls_request_data_pagelist(
27804017e29SAlex Elder 			struct ceph_osd_request *osd_req,
27904017e29SAlex Elder 			unsigned int which, struct ceph_pagelist *pagelist)
28004017e29SAlex Elder {
28104017e29SAlex Elder 	struct ceph_osd_data *osd_data;
28204017e29SAlex Elder 
283863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, request_data);
28404017e29SAlex Elder 	ceph_osd_data_pagelist_init(osd_data, pagelist);
285bb873b53SIlya Dryomov 	osd_req->r_ops[which].cls.indata_len += pagelist->length;
286bb873b53SIlya Dryomov 	osd_req->r_ops[which].indata_len += pagelist->length;
28704017e29SAlex Elder }
28804017e29SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
28904017e29SAlex Elder 
2906c57b554SAlex Elder void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
2916c57b554SAlex Elder 			unsigned int which, struct page **pages, u64 length,
2926c57b554SAlex Elder 			u32 alignment, bool pages_from_pool, bool own_pages)
2936c57b554SAlex Elder {
2946c57b554SAlex Elder 	struct ceph_osd_data *osd_data;
2956c57b554SAlex Elder 
2966c57b554SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, request_data);
2976c57b554SAlex Elder 	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
2986c57b554SAlex Elder 				pages_from_pool, own_pages);
299bb873b53SIlya Dryomov 	osd_req->r_ops[which].cls.indata_len += length;
300bb873b53SIlya Dryomov 	osd_req->r_ops[which].indata_len += length;
3016c57b554SAlex Elder }
3026c57b554SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
3036c57b554SAlex Elder 
304b9e281c2SIlya Dryomov void osd_req_op_cls_request_data_bvecs(struct ceph_osd_request *osd_req,
305b9e281c2SIlya Dryomov 				       unsigned int which,
3060010f705SIlya Dryomov 				       struct bio_vec *bvecs, u32 num_bvecs,
3070010f705SIlya Dryomov 				       u32 bytes)
308b9e281c2SIlya Dryomov {
309b9e281c2SIlya Dryomov 	struct ceph_osd_data *osd_data;
310b9e281c2SIlya Dryomov 	struct ceph_bvec_iter it = {
311b9e281c2SIlya Dryomov 		.bvecs = bvecs,
312b9e281c2SIlya Dryomov 		.iter = { .bi_size = bytes },
313b9e281c2SIlya Dryomov 	};
314b9e281c2SIlya Dryomov 
315b9e281c2SIlya Dryomov 	osd_data = osd_req_op_data(osd_req, which, cls, request_data);
3160010f705SIlya Dryomov 	ceph_osd_data_bvecs_init(osd_data, &it, num_bvecs);
317b9e281c2SIlya Dryomov 	osd_req->r_ops[which].cls.indata_len += bytes;
318b9e281c2SIlya Dryomov 	osd_req->r_ops[which].indata_len += bytes;
319b9e281c2SIlya Dryomov }
320b9e281c2SIlya Dryomov EXPORT_SYMBOL(osd_req_op_cls_request_data_bvecs);
321b9e281c2SIlya Dryomov 
322a4ce40a9SAlex Elder void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
323a4ce40a9SAlex Elder 			unsigned int which, struct page **pages, u64 length,
324a4ce40a9SAlex Elder 			u32 alignment, bool pages_from_pool, bool own_pages)
325a4ce40a9SAlex Elder {
326a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
327a4ce40a9SAlex Elder 
328863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, response_data);
329a4ce40a9SAlex Elder 	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
330a4ce40a9SAlex Elder 				pages_from_pool, own_pages);
331a4ce40a9SAlex Elder }
332a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
333a4ce40a9SAlex Elder 
33423c08a9cSAlex Elder static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
33523c08a9cSAlex Elder {
33623c08a9cSAlex Elder 	switch (osd_data->type) {
33723c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_NONE:
33823c08a9cSAlex Elder 		return 0;
33923c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_PAGES:
34023c08a9cSAlex Elder 		return osd_data->length;
34123c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_PAGELIST:
34223c08a9cSAlex Elder 		return (u64)osd_data->pagelist->length;
34323c08a9cSAlex Elder #ifdef CONFIG_BLOCK
34423c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_BIO:
34523c08a9cSAlex Elder 		return (u64)osd_data->bio_length;
34623c08a9cSAlex Elder #endif /* CONFIG_BLOCK */
347b9e281c2SIlya Dryomov 	case CEPH_OSD_DATA_TYPE_BVECS:
348b9e281c2SIlya Dryomov 		return osd_data->bvec_pos.iter.bi_size;
34923c08a9cSAlex Elder 	default:
35023c08a9cSAlex Elder 		WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
35123c08a9cSAlex Elder 		return 0;
35223c08a9cSAlex Elder 	}
35323c08a9cSAlex Elder }
35423c08a9cSAlex Elder 
355c54d47bfSAlex Elder static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
356c54d47bfSAlex Elder {
3575476492fSAlex Elder 	if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
358c54d47bfSAlex Elder 		int num_pages;
359c54d47bfSAlex Elder 
360c54d47bfSAlex Elder 		num_pages = calc_pages_for((u64)osd_data->alignment,
361c54d47bfSAlex Elder 						(u64)osd_data->length);
362c54d47bfSAlex Elder 		ceph_release_page_vector(osd_data->pages, num_pages);
36389486833SIlya Dryomov 	} else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
36489486833SIlya Dryomov 		ceph_pagelist_release(osd_data->pagelist);
365c54d47bfSAlex Elder 	}
3665476492fSAlex Elder 	ceph_osd_data_init(osd_data);
3675476492fSAlex Elder }
3685476492fSAlex Elder 
3695476492fSAlex Elder static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
3705476492fSAlex Elder 			unsigned int which)
3715476492fSAlex Elder {
3725476492fSAlex Elder 	struct ceph_osd_req_op *op;
3735476492fSAlex Elder 
3745476492fSAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
3755476492fSAlex Elder 	op = &osd_req->r_ops[which];
3765476492fSAlex Elder 
3775476492fSAlex Elder 	switch (op->op) {
3785476492fSAlex Elder 	case CEPH_OSD_OP_READ:
3795476492fSAlex Elder 	case CEPH_OSD_OP_WRITE:
380e30b7577SIlya Dryomov 	case CEPH_OSD_OP_WRITEFULL:
3815476492fSAlex Elder 		ceph_osd_data_release(&op->extent.osd_data);
3825476492fSAlex Elder 		break;
3835476492fSAlex Elder 	case CEPH_OSD_OP_CALL:
3845476492fSAlex Elder 		ceph_osd_data_release(&op->cls.request_info);
38504017e29SAlex Elder 		ceph_osd_data_release(&op->cls.request_data);
3865476492fSAlex Elder 		ceph_osd_data_release(&op->cls.response_data);
3875476492fSAlex Elder 		break;
388d74b50beSYan, Zheng 	case CEPH_OSD_OP_SETXATTR:
389d74b50beSYan, Zheng 	case CEPH_OSD_OP_CMPXATTR:
390d74b50beSYan, Zheng 		ceph_osd_data_release(&op->xattr.osd_data);
391d74b50beSYan, Zheng 		break;
39266ba609fSYan, Zheng 	case CEPH_OSD_OP_STAT:
39366ba609fSYan, Zheng 		ceph_osd_data_release(&op->raw_data_in);
39466ba609fSYan, Zheng 		break;
395922dab61SIlya Dryomov 	case CEPH_OSD_OP_NOTIFY_ACK:
396922dab61SIlya Dryomov 		ceph_osd_data_release(&op->notify_ack.request_data);
397922dab61SIlya Dryomov 		break;
39819079203SIlya Dryomov 	case CEPH_OSD_OP_NOTIFY:
39919079203SIlya Dryomov 		ceph_osd_data_release(&op->notify.request_data);
40019079203SIlya Dryomov 		ceph_osd_data_release(&op->notify.response_data);
40119079203SIlya Dryomov 		break;
402a4ed38d7SDouglas Fuller 	case CEPH_OSD_OP_LIST_WATCHERS:
403a4ed38d7SDouglas Fuller 		ceph_osd_data_release(&op->list_watchers.response_data);
404a4ed38d7SDouglas Fuller 		break;
40578beb0ffSLuis Henriques 	case CEPH_OSD_OP_COPY_FROM2:
40623ddf9beSLuis Henriques 		ceph_osd_data_release(&op->copy_from.osd_data);
40723ddf9beSLuis Henriques 		break;
4085476492fSAlex Elder 	default:
4095476492fSAlex Elder 		break;
4105476492fSAlex Elder 	}
411c54d47bfSAlex Elder }
412c54d47bfSAlex Elder 
4133d14c5d2SYehuda Sadeh /*
41463244fa1SIlya Dryomov  * Assumes @t is zero-initialized.
41563244fa1SIlya Dryomov  */
41663244fa1SIlya Dryomov static void target_init(struct ceph_osd_request_target *t)
41763244fa1SIlya Dryomov {
41863244fa1SIlya Dryomov 	ceph_oid_init(&t->base_oid);
41963244fa1SIlya Dryomov 	ceph_oloc_init(&t->base_oloc);
42063244fa1SIlya Dryomov 	ceph_oid_init(&t->target_oid);
42163244fa1SIlya Dryomov 	ceph_oloc_init(&t->target_oloc);
42263244fa1SIlya Dryomov 
42363244fa1SIlya Dryomov 	ceph_osds_init(&t->acting);
42463244fa1SIlya Dryomov 	ceph_osds_init(&t->up);
42563244fa1SIlya Dryomov 	t->size = -1;
42663244fa1SIlya Dryomov 	t->min_size = -1;
42763244fa1SIlya Dryomov 
42863244fa1SIlya Dryomov 	t->osd = CEPH_HOMELESS_OSD;
42963244fa1SIlya Dryomov }
43063244fa1SIlya Dryomov 
431922dab61SIlya Dryomov static void target_copy(struct ceph_osd_request_target *dest,
432922dab61SIlya Dryomov 			const struct ceph_osd_request_target *src)
433922dab61SIlya Dryomov {
434922dab61SIlya Dryomov 	ceph_oid_copy(&dest->base_oid, &src->base_oid);
435922dab61SIlya Dryomov 	ceph_oloc_copy(&dest->base_oloc, &src->base_oloc);
436922dab61SIlya Dryomov 	ceph_oid_copy(&dest->target_oid, &src->target_oid);
437922dab61SIlya Dryomov 	ceph_oloc_copy(&dest->target_oloc, &src->target_oloc);
438922dab61SIlya Dryomov 
439922dab61SIlya Dryomov 	dest->pgid = src->pgid; /* struct */
440dc98ff72SIlya Dryomov 	dest->spgid = src->spgid; /* struct */
441922dab61SIlya Dryomov 	dest->pg_num = src->pg_num;
442922dab61SIlya Dryomov 	dest->pg_num_mask = src->pg_num_mask;
443922dab61SIlya Dryomov 	ceph_osds_copy(&dest->acting, &src->acting);
444922dab61SIlya Dryomov 	ceph_osds_copy(&dest->up, &src->up);
445922dab61SIlya Dryomov 	dest->size = src->size;
446922dab61SIlya Dryomov 	dest->min_size = src->min_size;
447922dab61SIlya Dryomov 	dest->sort_bitwise = src->sort_bitwise;
448922dab61SIlya Dryomov 
449922dab61SIlya Dryomov 	dest->flags = src->flags;
450922dab61SIlya Dryomov 	dest->paused = src->paused;
451922dab61SIlya Dryomov 
45204c7d789SIlya Dryomov 	dest->epoch = src->epoch;
453dc93e0e2SIlya Dryomov 	dest->last_force_resend = src->last_force_resend;
454dc93e0e2SIlya Dryomov 
455922dab61SIlya Dryomov 	dest->osd = src->osd;
456922dab61SIlya Dryomov }
457922dab61SIlya Dryomov 
45863244fa1SIlya Dryomov static void target_destroy(struct ceph_osd_request_target *t)
45963244fa1SIlya Dryomov {
46063244fa1SIlya Dryomov 	ceph_oid_destroy(&t->base_oid);
46130c156d9SYan, Zheng 	ceph_oloc_destroy(&t->base_oloc);
46263244fa1SIlya Dryomov 	ceph_oid_destroy(&t->target_oid);
46330c156d9SYan, Zheng 	ceph_oloc_destroy(&t->target_oloc);
46463244fa1SIlya Dryomov }
46563244fa1SIlya Dryomov 
46663244fa1SIlya Dryomov /*
4673d14c5d2SYehuda Sadeh  * requests
4683d14c5d2SYehuda Sadeh  */
4693540bfdbSIlya Dryomov static void request_release_checks(struct ceph_osd_request *req)
4703540bfdbSIlya Dryomov {
4713540bfdbSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&req->r_node));
4724609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&req->r_mc_node));
47394e85771SIlya Dryomov 	WARN_ON(!list_empty(&req->r_private_item));
4743540bfdbSIlya Dryomov 	WARN_ON(req->r_osd);
4753540bfdbSIlya Dryomov }
4763540bfdbSIlya Dryomov 
4779e94af20SIlya Dryomov static void ceph_osdc_release_request(struct kref *kref)
4783d14c5d2SYehuda Sadeh {
4799e94af20SIlya Dryomov 	struct ceph_osd_request *req = container_of(kref,
4809e94af20SIlya Dryomov 					    struct ceph_osd_request, r_kref);
4815476492fSAlex Elder 	unsigned int which;
4823d14c5d2SYehuda Sadeh 
4839e94af20SIlya Dryomov 	dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
4849e94af20SIlya Dryomov 	     req->r_request, req->r_reply);
4853540bfdbSIlya Dryomov 	request_release_checks(req);
4869e94af20SIlya Dryomov 
4873d14c5d2SYehuda Sadeh 	if (req->r_request)
4883d14c5d2SYehuda Sadeh 		ceph_msg_put(req->r_request);
4895aea3dcdSIlya Dryomov 	if (req->r_reply)
490ab8cb34aSAlex Elder 		ceph_msg_put(req->r_reply);
4910fff87ecSAlex Elder 
4925476492fSAlex Elder 	for (which = 0; which < req->r_num_ops; which++)
4935476492fSAlex Elder 		osd_req_op_data_release(req, which);
4940fff87ecSAlex Elder 
495a66dd383SIlya Dryomov 	target_destroy(&req->r_t);
4963d14c5d2SYehuda Sadeh 	ceph_put_snap_context(req->r_snapc);
497d30291b9SIlya Dryomov 
4983d14c5d2SYehuda Sadeh 	if (req->r_mempool)
4993d14c5d2SYehuda Sadeh 		mempool_free(req, req->r_osdc->req_mempool);
5003f1af42aSIlya Dryomov 	else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS)
5015522ae0bSAlex Elder 		kmem_cache_free(ceph_osd_request_cache, req);
5023f1af42aSIlya Dryomov 	else
5033f1af42aSIlya Dryomov 		kfree(req);
5043d14c5d2SYehuda Sadeh }
5059e94af20SIlya Dryomov 
5069e94af20SIlya Dryomov void ceph_osdc_get_request(struct ceph_osd_request *req)
5079e94af20SIlya Dryomov {
5089e94af20SIlya Dryomov 	dout("%s %p (was %d)\n", __func__, req,
5092c935bc5SPeter Zijlstra 	     kref_read(&req->r_kref));
5109e94af20SIlya Dryomov 	kref_get(&req->r_kref);
5119e94af20SIlya Dryomov }
5129e94af20SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_get_request);
5139e94af20SIlya Dryomov 
5149e94af20SIlya Dryomov void ceph_osdc_put_request(struct ceph_osd_request *req)
5159e94af20SIlya Dryomov {
5163ed97d63SIlya Dryomov 	if (req) {
5179e94af20SIlya Dryomov 		dout("%s %p (was %d)\n", __func__, req,
5182c935bc5SPeter Zijlstra 		     kref_read(&req->r_kref));
5199e94af20SIlya Dryomov 		kref_put(&req->r_kref, ceph_osdc_release_request);
5209e94af20SIlya Dryomov 	}
5213ed97d63SIlya Dryomov }
5229e94af20SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_put_request);
5233d14c5d2SYehuda Sadeh 
5243540bfdbSIlya Dryomov static void request_init(struct ceph_osd_request *req)
5253540bfdbSIlya Dryomov {
5263540bfdbSIlya Dryomov 	/* req only, each op is zeroed in _osd_req_op_init() */
5273540bfdbSIlya Dryomov 	memset(req, 0, sizeof(*req));
5283540bfdbSIlya Dryomov 
5293540bfdbSIlya Dryomov 	kref_init(&req->r_kref);
5303540bfdbSIlya Dryomov 	init_completion(&req->r_completion);
5313540bfdbSIlya Dryomov 	RB_CLEAR_NODE(&req->r_node);
5324609245eSIlya Dryomov 	RB_CLEAR_NODE(&req->r_mc_node);
53394e85771SIlya Dryomov 	INIT_LIST_HEAD(&req->r_private_item);
5343540bfdbSIlya Dryomov 
5353540bfdbSIlya Dryomov 	target_init(&req->r_t);
5363540bfdbSIlya Dryomov }
5373540bfdbSIlya Dryomov 
538922dab61SIlya Dryomov /*
539922dab61SIlya Dryomov  * This is ugly, but it allows us to reuse linger registration and ping
540922dab61SIlya Dryomov  * requests, keeping the structure of the code around send_linger{_ping}()
541922dab61SIlya Dryomov  * reasonable.  Setting up a min_nr=2 mempool for each linger request
542922dab61SIlya Dryomov  * and dealing with copying ops (this blasts req only, watch op remains
543922dab61SIlya Dryomov  * intact) isn't any better.
544922dab61SIlya Dryomov  */
545922dab61SIlya Dryomov static void request_reinit(struct ceph_osd_request *req)
546922dab61SIlya Dryomov {
547922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
548922dab61SIlya Dryomov 	bool mempool = req->r_mempool;
549922dab61SIlya Dryomov 	unsigned int num_ops = req->r_num_ops;
550922dab61SIlya Dryomov 	u64 snapid = req->r_snapid;
551922dab61SIlya Dryomov 	struct ceph_snap_context *snapc = req->r_snapc;
552922dab61SIlya Dryomov 	bool linger = req->r_linger;
553922dab61SIlya Dryomov 	struct ceph_msg *request_msg = req->r_request;
554922dab61SIlya Dryomov 	struct ceph_msg *reply_msg = req->r_reply;
555922dab61SIlya Dryomov 
556922dab61SIlya Dryomov 	dout("%s req %p\n", __func__, req);
5572c935bc5SPeter Zijlstra 	WARN_ON(kref_read(&req->r_kref) != 1);
558922dab61SIlya Dryomov 	request_release_checks(req);
559922dab61SIlya Dryomov 
5602c935bc5SPeter Zijlstra 	WARN_ON(kref_read(&request_msg->kref) != 1);
5612c935bc5SPeter Zijlstra 	WARN_ON(kref_read(&reply_msg->kref) != 1);
562922dab61SIlya Dryomov 	target_destroy(&req->r_t);
563922dab61SIlya Dryomov 
564922dab61SIlya Dryomov 	request_init(req);
565922dab61SIlya Dryomov 	req->r_osdc = osdc;
566922dab61SIlya Dryomov 	req->r_mempool = mempool;
567922dab61SIlya Dryomov 	req->r_num_ops = num_ops;
568922dab61SIlya Dryomov 	req->r_snapid = snapid;
569922dab61SIlya Dryomov 	req->r_snapc = snapc;
570922dab61SIlya Dryomov 	req->r_linger = linger;
571922dab61SIlya Dryomov 	req->r_request = request_msg;
572922dab61SIlya Dryomov 	req->r_reply = reply_msg;
573922dab61SIlya Dryomov }
574922dab61SIlya Dryomov 
5753d14c5d2SYehuda Sadeh struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
5763d14c5d2SYehuda Sadeh 					       struct ceph_snap_context *snapc,
5771b83bef2SSage Weil 					       unsigned int num_ops,
5783d14c5d2SYehuda Sadeh 					       bool use_mempool,
57954a54007SAlex Elder 					       gfp_t gfp_flags)
5803d14c5d2SYehuda Sadeh {
5813d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
5823d14c5d2SYehuda Sadeh 
5833d14c5d2SYehuda Sadeh 	if (use_mempool) {
5843f1af42aSIlya Dryomov 		BUG_ON(num_ops > CEPH_OSD_SLAB_OPS);
5853d14c5d2SYehuda Sadeh 		req = mempool_alloc(osdc->req_mempool, gfp_flags);
5863f1af42aSIlya Dryomov 	} else if (num_ops <= CEPH_OSD_SLAB_OPS) {
5873f1af42aSIlya Dryomov 		req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
5883d14c5d2SYehuda Sadeh 	} else {
5893f1af42aSIlya Dryomov 		BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
590acafe7e3SKees Cook 		req = kmalloc(struct_size(req, r_ops, num_ops), gfp_flags);
5913d14c5d2SYehuda Sadeh 	}
5923f1af42aSIlya Dryomov 	if (unlikely(!req))
5933d14c5d2SYehuda Sadeh 		return NULL;
5943d14c5d2SYehuda Sadeh 
5953540bfdbSIlya Dryomov 	request_init(req);
5963d14c5d2SYehuda Sadeh 	req->r_osdc = osdc;
5973d14c5d2SYehuda Sadeh 	req->r_mempool = use_mempool;
59879528734SAlex Elder 	req->r_num_ops = num_ops;
59984127282SIlya Dryomov 	req->r_snapid = CEPH_NOSNAP;
60084127282SIlya Dryomov 	req->r_snapc = ceph_get_snap_context(snapc);
6013d14c5d2SYehuda Sadeh 
60213d1ad16SIlya Dryomov 	dout("%s req %p\n", __func__, req);
60313d1ad16SIlya Dryomov 	return req;
6043f1af42aSIlya Dryomov }
60513d1ad16SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_alloc_request);
6063f1af42aSIlya Dryomov 
6072e59ffd1SIlya Dryomov static int ceph_oloc_encoding_size(const struct ceph_object_locator *oloc)
60830c156d9SYan, Zheng {
60930c156d9SYan, Zheng 	return 8 + 4 + 4 + 4 + (oloc->pool_ns ? oloc->pool_ns->len : 0);
61030c156d9SYan, Zheng }
61130c156d9SYan, Zheng 
6120d9c1ab3SIlya Dryomov static int __ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp,
6130d9c1ab3SIlya Dryomov 				      int num_request_data_items,
6140d9c1ab3SIlya Dryomov 				      int num_reply_data_items)
61513d1ad16SIlya Dryomov {
61613d1ad16SIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
61713d1ad16SIlya Dryomov 	struct ceph_msg *msg;
61813d1ad16SIlya Dryomov 	int msg_size;
6193d14c5d2SYehuda Sadeh 
6200d9c1ab3SIlya Dryomov 	WARN_ON(req->r_request || req->r_reply);
621d30291b9SIlya Dryomov 	WARN_ON(ceph_oid_empty(&req->r_base_oid));
62230c156d9SYan, Zheng 	WARN_ON(ceph_oloc_empty(&req->r_base_oloc));
623d30291b9SIlya Dryomov 
62413d1ad16SIlya Dryomov 	/* create request message */
6258cb441c0SIlya Dryomov 	msg_size = CEPH_ENCODING_START_BLK_LEN +
6268cb441c0SIlya Dryomov 			CEPH_PGID_ENCODING_LEN + 1; /* spgid */
6278cb441c0SIlya Dryomov 	msg_size += 4 + 4 + 4; /* hash, osdmap_epoch, flags */
6288cb441c0SIlya Dryomov 	msg_size += CEPH_ENCODING_START_BLK_LEN +
6298cb441c0SIlya Dryomov 			sizeof(struct ceph_osd_reqid); /* reqid */
6308cb441c0SIlya Dryomov 	msg_size += sizeof(struct ceph_blkin_trace_info); /* trace */
6318cb441c0SIlya Dryomov 	msg_size += 4 + sizeof(struct ceph_timespec); /* client_inc, mtime */
63230c156d9SYan, Zheng 	msg_size += CEPH_ENCODING_START_BLK_LEN +
63330c156d9SYan, Zheng 			ceph_oloc_encoding_size(&req->r_base_oloc); /* oloc */
63413d1ad16SIlya Dryomov 	msg_size += 4 + req->r_base_oid.name_len; /* oid */
63513d1ad16SIlya Dryomov 	msg_size += 2 + req->r_num_ops * sizeof(struct ceph_osd_op);
636ae458f5aSIlya Dryomov 	msg_size += 8; /* snapid */
637ae458f5aSIlya Dryomov 	msg_size += 8; /* snap_seq */
63813d1ad16SIlya Dryomov 	msg_size += 4 + 8 * (req->r_snapc ? req->r_snapc->num_snaps : 0);
6398cb441c0SIlya Dryomov 	msg_size += 4 + 8; /* retry_attempt, features */
640ae458f5aSIlya Dryomov 
64113d1ad16SIlya Dryomov 	if (req->r_mempool)
6420d9c1ab3SIlya Dryomov 		msg = ceph_msgpool_get(&osdc->msgpool_op, msg_size,
6430d9c1ab3SIlya Dryomov 				       num_request_data_items);
6443d14c5d2SYehuda Sadeh 	else
6450d9c1ab3SIlya Dryomov 		msg = ceph_msg_new2(CEPH_MSG_OSD_OP, msg_size,
6460d9c1ab3SIlya Dryomov 				    num_request_data_items, gfp, true);
64713d1ad16SIlya Dryomov 	if (!msg)
64813d1ad16SIlya Dryomov 		return -ENOMEM;
6493d14c5d2SYehuda Sadeh 
6503d14c5d2SYehuda Sadeh 	memset(msg->front.iov_base, 0, msg->front.iov_len);
6513d14c5d2SYehuda Sadeh 	req->r_request = msg;
6523d14c5d2SYehuda Sadeh 
65313d1ad16SIlya Dryomov 	/* create reply message */
65413d1ad16SIlya Dryomov 	msg_size = OSD_OPREPLY_FRONT_LEN;
655711da55dSIlya Dryomov 	msg_size += req->r_base_oid.name_len;
656711da55dSIlya Dryomov 	msg_size += req->r_num_ops * sizeof(struct ceph_osd_op);
65713d1ad16SIlya Dryomov 
65813d1ad16SIlya Dryomov 	if (req->r_mempool)
6590d9c1ab3SIlya Dryomov 		msg = ceph_msgpool_get(&osdc->msgpool_op_reply, msg_size,
6600d9c1ab3SIlya Dryomov 				       num_reply_data_items);
66113d1ad16SIlya Dryomov 	else
6620d9c1ab3SIlya Dryomov 		msg = ceph_msg_new2(CEPH_MSG_OSD_OPREPLY, msg_size,
6630d9c1ab3SIlya Dryomov 				    num_reply_data_items, gfp, true);
66413d1ad16SIlya Dryomov 	if (!msg)
66513d1ad16SIlya Dryomov 		return -ENOMEM;
66613d1ad16SIlya Dryomov 
66713d1ad16SIlya Dryomov 	req->r_reply = msg;
66813d1ad16SIlya Dryomov 
66913d1ad16SIlya Dryomov 	return 0;
67013d1ad16SIlya Dryomov }
6713d14c5d2SYehuda Sadeh 
672a8dd0a37SAlex Elder static bool osd_req_opcode_valid(u16 opcode)
673a8dd0a37SAlex Elder {
674a8dd0a37SAlex Elder 	switch (opcode) {
67570b5bfa3SIlya Dryomov #define GENERATE_CASE(op, opcode, str)	case CEPH_OSD_OP_##op: return true;
67670b5bfa3SIlya Dryomov __CEPH_FORALL_OSD_OPS(GENERATE_CASE)
67770b5bfa3SIlya Dryomov #undef GENERATE_CASE
678a8dd0a37SAlex Elder 	default:
679a8dd0a37SAlex Elder 		return false;
680a8dd0a37SAlex Elder 	}
681a8dd0a37SAlex Elder }
682a8dd0a37SAlex Elder 
6830d9c1ab3SIlya Dryomov static void get_num_data_items(struct ceph_osd_request *req,
6840d9c1ab3SIlya Dryomov 			       int *num_request_data_items,
6850d9c1ab3SIlya Dryomov 			       int *num_reply_data_items)
6860d9c1ab3SIlya Dryomov {
6870d9c1ab3SIlya Dryomov 	struct ceph_osd_req_op *op;
6880d9c1ab3SIlya Dryomov 
6890d9c1ab3SIlya Dryomov 	*num_request_data_items = 0;
6900d9c1ab3SIlya Dryomov 	*num_reply_data_items = 0;
6910d9c1ab3SIlya Dryomov 
6920d9c1ab3SIlya Dryomov 	for (op = req->r_ops; op != &req->r_ops[req->r_num_ops]; op++) {
6930d9c1ab3SIlya Dryomov 		switch (op->op) {
6940d9c1ab3SIlya Dryomov 		/* request */
6950d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_WRITE:
6960d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_WRITEFULL:
6970d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_SETXATTR:
6980d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_CMPXATTR:
6990d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_NOTIFY_ACK:
70078beb0ffSLuis Henriques 		case CEPH_OSD_OP_COPY_FROM2:
7010d9c1ab3SIlya Dryomov 			*num_request_data_items += 1;
7020d9c1ab3SIlya Dryomov 			break;
7030d9c1ab3SIlya Dryomov 
7040d9c1ab3SIlya Dryomov 		/* reply */
7050d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_STAT:
7060d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_READ:
7070d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_LIST_WATCHERS:
7080d9c1ab3SIlya Dryomov 			*num_reply_data_items += 1;
7090d9c1ab3SIlya Dryomov 			break;
7100d9c1ab3SIlya Dryomov 
7110d9c1ab3SIlya Dryomov 		/* both */
7120d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_NOTIFY:
7130d9c1ab3SIlya Dryomov 			*num_request_data_items += 1;
7140d9c1ab3SIlya Dryomov 			*num_reply_data_items += 1;
7150d9c1ab3SIlya Dryomov 			break;
7160d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_CALL:
7170d9c1ab3SIlya Dryomov 			*num_request_data_items += 2;
7180d9c1ab3SIlya Dryomov 			*num_reply_data_items += 1;
7190d9c1ab3SIlya Dryomov 			break;
7200d9c1ab3SIlya Dryomov 
7210d9c1ab3SIlya Dryomov 		default:
7220d9c1ab3SIlya Dryomov 			WARN_ON(!osd_req_opcode_valid(op->op));
7230d9c1ab3SIlya Dryomov 			break;
7240d9c1ab3SIlya Dryomov 		}
7250d9c1ab3SIlya Dryomov 	}
7260d9c1ab3SIlya Dryomov }
7270d9c1ab3SIlya Dryomov 
7280d9c1ab3SIlya Dryomov /*
7290d9c1ab3SIlya Dryomov  * oid, oloc and OSD op opcode(s) must be filled in before this function
7300d9c1ab3SIlya Dryomov  * is called.
7310d9c1ab3SIlya Dryomov  */
7320d9c1ab3SIlya Dryomov int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp)
7330d9c1ab3SIlya Dryomov {
7340d9c1ab3SIlya Dryomov 	int num_request_data_items, num_reply_data_items;
7350d9c1ab3SIlya Dryomov 
7360d9c1ab3SIlya Dryomov 	get_num_data_items(req, &num_request_data_items, &num_reply_data_items);
7370d9c1ab3SIlya Dryomov 	return __ceph_osdc_alloc_messages(req, gfp, num_request_data_items,
7380d9c1ab3SIlya Dryomov 					  num_reply_data_items);
7390d9c1ab3SIlya Dryomov }
7400d9c1ab3SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_alloc_messages);
7410d9c1ab3SIlya Dryomov 
74233803f33SAlex Elder /*
74333803f33SAlex Elder  * This is an osd op init function for opcodes that have no data or
74433803f33SAlex Elder  * other information associated with them.  It also serves as a
74533803f33SAlex Elder  * common init routine for all the other init functions, below.
74633803f33SAlex Elder  */
747c99d2d4aSAlex Elder static struct ceph_osd_req_op *
74849719778SAlex Elder _osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
749144cba14SYan, Zheng 		 u16 opcode, u32 flags)
75033803f33SAlex Elder {
751c99d2d4aSAlex Elder 	struct ceph_osd_req_op *op;
752c99d2d4aSAlex Elder 
753c99d2d4aSAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
75433803f33SAlex Elder 	BUG_ON(!osd_req_opcode_valid(opcode));
75533803f33SAlex Elder 
756c99d2d4aSAlex Elder 	op = &osd_req->r_ops[which];
75733803f33SAlex Elder 	memset(op, 0, sizeof (*op));
75833803f33SAlex Elder 	op->op = opcode;
759144cba14SYan, Zheng 	op->flags = flags;
760c99d2d4aSAlex Elder 
761c99d2d4aSAlex Elder 	return op;
76233803f33SAlex Elder }
76333803f33SAlex Elder 
76449719778SAlex Elder void osd_req_op_init(struct ceph_osd_request *osd_req,
765144cba14SYan, Zheng 		     unsigned int which, u16 opcode, u32 flags)
76649719778SAlex Elder {
767144cba14SYan, Zheng 	(void)_osd_req_op_init(osd_req, which, opcode, flags);
76849719778SAlex Elder }
76949719778SAlex Elder EXPORT_SYMBOL(osd_req_op_init);
77049719778SAlex Elder 
771c99d2d4aSAlex Elder void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
772c99d2d4aSAlex Elder 				unsigned int which, u16 opcode,
77333803f33SAlex Elder 				u64 offset, u64 length,
77433803f33SAlex Elder 				u64 truncate_size, u32 truncate_seq)
77533803f33SAlex Elder {
776144cba14SYan, Zheng 	struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
777144cba14SYan, Zheng 						      opcode, 0);
77833803f33SAlex Elder 	size_t payload_len = 0;
77933803f33SAlex Elder 
780ad7a60deSLi Wang 	BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
781e30b7577SIlya Dryomov 	       opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
782e30b7577SIlya Dryomov 	       opcode != CEPH_OSD_OP_TRUNCATE);
78333803f33SAlex Elder 
78433803f33SAlex Elder 	op->extent.offset = offset;
78533803f33SAlex Elder 	op->extent.length = length;
78633803f33SAlex Elder 	op->extent.truncate_size = truncate_size;
78733803f33SAlex Elder 	op->extent.truncate_seq = truncate_seq;
788e30b7577SIlya Dryomov 	if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
78933803f33SAlex Elder 		payload_len += length;
79033803f33SAlex Elder 
791de2aa102SIlya Dryomov 	op->indata_len = payload_len;
79233803f33SAlex Elder }
79333803f33SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_init);
79433803f33SAlex Elder 
795c99d2d4aSAlex Elder void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
796c99d2d4aSAlex Elder 				unsigned int which, u64 length)
797e5975c7cSAlex Elder {
798c99d2d4aSAlex Elder 	struct ceph_osd_req_op *op;
799c99d2d4aSAlex Elder 	u64 previous;
800c99d2d4aSAlex Elder 
801c99d2d4aSAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
802c99d2d4aSAlex Elder 	op = &osd_req->r_ops[which];
803c99d2d4aSAlex Elder 	previous = op->extent.length;
804e5975c7cSAlex Elder 
805e5975c7cSAlex Elder 	if (length == previous)
806e5975c7cSAlex Elder 		return;		/* Nothing to do */
807e5975c7cSAlex Elder 	BUG_ON(length > previous);
808e5975c7cSAlex Elder 
809e5975c7cSAlex Elder 	op->extent.length = length;
810d641df81SYan, Zheng 	if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
811de2aa102SIlya Dryomov 		op->indata_len -= previous - length;
812e5975c7cSAlex Elder }
813e5975c7cSAlex Elder EXPORT_SYMBOL(osd_req_op_extent_update);
814e5975c7cSAlex Elder 
8152c63f49aSYan, Zheng void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
8162c63f49aSYan, Zheng 				unsigned int which, u64 offset_inc)
8172c63f49aSYan, Zheng {
8182c63f49aSYan, Zheng 	struct ceph_osd_req_op *op, *prev_op;
8192c63f49aSYan, Zheng 
8202c63f49aSYan, Zheng 	BUG_ON(which + 1 >= osd_req->r_num_ops);
8212c63f49aSYan, Zheng 
8222c63f49aSYan, Zheng 	prev_op = &osd_req->r_ops[which];
8232c63f49aSYan, Zheng 	op = _osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags);
8242c63f49aSYan, Zheng 	/* dup previous one */
8252c63f49aSYan, Zheng 	op->indata_len = prev_op->indata_len;
8262c63f49aSYan, Zheng 	op->outdata_len = prev_op->outdata_len;
8272c63f49aSYan, Zheng 	op->extent = prev_op->extent;
8282c63f49aSYan, Zheng 	/* adjust offset */
8292c63f49aSYan, Zheng 	op->extent.offset += offset_inc;
8302c63f49aSYan, Zheng 	op->extent.length -= offset_inc;
8312c63f49aSYan, Zheng 
8322c63f49aSYan, Zheng 	if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
8332c63f49aSYan, Zheng 		op->indata_len -= offset_inc;
8342c63f49aSYan, Zheng }
8352c63f49aSYan, Zheng EXPORT_SYMBOL(osd_req_op_extent_dup_last);
8362c63f49aSYan, Zheng 
837fe943d50SChengguang Xu int osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
83824639ce5SIlya Dryomov 			const char *class, const char *method)
83933803f33SAlex Elder {
84024639ce5SIlya Dryomov 	struct ceph_osd_req_op *op;
8415f562df5SAlex Elder 	struct ceph_pagelist *pagelist;
84233803f33SAlex Elder 	size_t payload_len = 0;
84333803f33SAlex Elder 	size_t size;
8444766815bSDavid Disseldorp 	int ret;
84533803f33SAlex Elder 
84624639ce5SIlya Dryomov 	op = _osd_req_op_init(osd_req, which, CEPH_OSD_OP_CALL, 0);
84733803f33SAlex Elder 
84833165d47SIlya Dryomov 	pagelist = ceph_pagelist_alloc(GFP_NOFS);
849fe943d50SChengguang Xu 	if (!pagelist)
850fe943d50SChengguang Xu 		return -ENOMEM;
851fe943d50SChengguang Xu 
85233803f33SAlex Elder 	op->cls.class_name = class;
85333803f33SAlex Elder 	size = strlen(class);
85433803f33SAlex Elder 	BUG_ON(size > (size_t) U8_MAX);
85533803f33SAlex Elder 	op->cls.class_len = size;
8564766815bSDavid Disseldorp 	ret = ceph_pagelist_append(pagelist, class, size);
8574766815bSDavid Disseldorp 	if (ret)
8584766815bSDavid Disseldorp 		goto err_pagelist_free;
85933803f33SAlex Elder 	payload_len += size;
86033803f33SAlex Elder 
86133803f33SAlex Elder 	op->cls.method_name = method;
86233803f33SAlex Elder 	size = strlen(method);
86333803f33SAlex Elder 	BUG_ON(size > (size_t) U8_MAX);
86433803f33SAlex Elder 	op->cls.method_len = size;
8654766815bSDavid Disseldorp 	ret = ceph_pagelist_append(pagelist, method, size);
8664766815bSDavid Disseldorp 	if (ret)
8674766815bSDavid Disseldorp 		goto err_pagelist_free;
86833803f33SAlex Elder 	payload_len += size;
86933803f33SAlex Elder 
870a4ce40a9SAlex Elder 	osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
871de2aa102SIlya Dryomov 	op->indata_len = payload_len;
872fe943d50SChengguang Xu 	return 0;
8734766815bSDavid Disseldorp 
8744766815bSDavid Disseldorp err_pagelist_free:
8754766815bSDavid Disseldorp 	ceph_pagelist_release(pagelist);
8764766815bSDavid Disseldorp 	return ret;
87733803f33SAlex Elder }
87833803f33SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_init);
8798c042b0dSAlex Elder 
880d74b50beSYan, Zheng int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
881d74b50beSYan, Zheng 			  u16 opcode, const char *name, const void *value,
882d74b50beSYan, Zheng 			  size_t size, u8 cmp_op, u8 cmp_mode)
883d74b50beSYan, Zheng {
884144cba14SYan, Zheng 	struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
885144cba14SYan, Zheng 						      opcode, 0);
886d74b50beSYan, Zheng 	struct ceph_pagelist *pagelist;
887d74b50beSYan, Zheng 	size_t payload_len;
8884766815bSDavid Disseldorp 	int ret;
889d74b50beSYan, Zheng 
890d74b50beSYan, Zheng 	BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
891d74b50beSYan, Zheng 
89233165d47SIlya Dryomov 	pagelist = ceph_pagelist_alloc(GFP_NOFS);
893d74b50beSYan, Zheng 	if (!pagelist)
894d74b50beSYan, Zheng 		return -ENOMEM;
895d74b50beSYan, Zheng 
896d74b50beSYan, Zheng 	payload_len = strlen(name);
897d74b50beSYan, Zheng 	op->xattr.name_len = payload_len;
8984766815bSDavid Disseldorp 	ret = ceph_pagelist_append(pagelist, name, payload_len);
8994766815bSDavid Disseldorp 	if (ret)
9004766815bSDavid Disseldorp 		goto err_pagelist_free;
901d74b50beSYan, Zheng 
902d74b50beSYan, Zheng 	op->xattr.value_len = size;
9034766815bSDavid Disseldorp 	ret = ceph_pagelist_append(pagelist, value, size);
9044766815bSDavid Disseldorp 	if (ret)
9054766815bSDavid Disseldorp 		goto err_pagelist_free;
906d74b50beSYan, Zheng 	payload_len += size;
907d74b50beSYan, Zheng 
908d74b50beSYan, Zheng 	op->xattr.cmp_op = cmp_op;
909d74b50beSYan, Zheng 	op->xattr.cmp_mode = cmp_mode;
910d74b50beSYan, Zheng 
911d74b50beSYan, Zheng 	ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
912de2aa102SIlya Dryomov 	op->indata_len = payload_len;
913d74b50beSYan, Zheng 	return 0;
9144766815bSDavid Disseldorp 
9154766815bSDavid Disseldorp err_pagelist_free:
9164766815bSDavid Disseldorp 	ceph_pagelist_release(pagelist);
9174766815bSDavid Disseldorp 	return ret;
918d74b50beSYan, Zheng }
919d74b50beSYan, Zheng EXPORT_SYMBOL(osd_req_op_xattr_init);
920d74b50beSYan, Zheng 
921922dab61SIlya Dryomov /*
922922dab61SIlya Dryomov  * @watch_opcode: CEPH_OSD_WATCH_OP_*
923922dab61SIlya Dryomov  */
924922dab61SIlya Dryomov static void osd_req_op_watch_init(struct ceph_osd_request *req, int which,
925922dab61SIlya Dryomov 				  u64 cookie, u8 watch_opcode)
92633803f33SAlex Elder {
927922dab61SIlya Dryomov 	struct ceph_osd_req_op *op;
92833803f33SAlex Elder 
929922dab61SIlya Dryomov 	op = _osd_req_op_init(req, which, CEPH_OSD_OP_WATCH, 0);
93033803f33SAlex Elder 	op->watch.cookie = cookie;
931922dab61SIlya Dryomov 	op->watch.op = watch_opcode;
932922dab61SIlya Dryomov 	op->watch.gen = 0;
93333803f33SAlex Elder }
93433803f33SAlex Elder 
935c647b8a8SIlya Dryomov void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
936c647b8a8SIlya Dryomov 				unsigned int which,
937c647b8a8SIlya Dryomov 				u64 expected_object_size,
938c647b8a8SIlya Dryomov 				u64 expected_write_size)
939c647b8a8SIlya Dryomov {
940c647b8a8SIlya Dryomov 	struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
941144cba14SYan, Zheng 						      CEPH_OSD_OP_SETALLOCHINT,
942144cba14SYan, Zheng 						      0);
943c647b8a8SIlya Dryomov 
944c647b8a8SIlya Dryomov 	op->alloc_hint.expected_object_size = expected_object_size;
945c647b8a8SIlya Dryomov 	op->alloc_hint.expected_write_size = expected_write_size;
946c647b8a8SIlya Dryomov 
947c647b8a8SIlya Dryomov 	/*
948c647b8a8SIlya Dryomov 	 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
949c647b8a8SIlya Dryomov 	 * not worth a feature bit.  Set FAILOK per-op flag to make
950c647b8a8SIlya Dryomov 	 * sure older osds don't trip over an unsupported opcode.
951c647b8a8SIlya Dryomov 	 */
952c647b8a8SIlya Dryomov 	op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
953c647b8a8SIlya Dryomov }
954c647b8a8SIlya Dryomov EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
955c647b8a8SIlya Dryomov 
95690af3602SAlex Elder static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
957ec9123c5SAlex Elder 				struct ceph_osd_data *osd_data)
958ec9123c5SAlex Elder {
959ec9123c5SAlex Elder 	u64 length = ceph_osd_data_length(osd_data);
960ec9123c5SAlex Elder 
961ec9123c5SAlex Elder 	if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
962ec9123c5SAlex Elder 		BUG_ON(length > (u64) SIZE_MAX);
963ec9123c5SAlex Elder 		if (length)
96490af3602SAlex Elder 			ceph_msg_data_add_pages(msg, osd_data->pages,
965ec9123c5SAlex Elder 					length, osd_data->alignment);
966ec9123c5SAlex Elder 	} else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
967ec9123c5SAlex Elder 		BUG_ON(!length);
96890af3602SAlex Elder 		ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
969ec9123c5SAlex Elder #ifdef CONFIG_BLOCK
970ec9123c5SAlex Elder 	} else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
9715359a17dSIlya Dryomov 		ceph_msg_data_add_bio(msg, &osd_data->bio_pos, length);
972ec9123c5SAlex Elder #endif
973b9e281c2SIlya Dryomov 	} else if (osd_data->type == CEPH_OSD_DATA_TYPE_BVECS) {
974b9e281c2SIlya Dryomov 		ceph_msg_data_add_bvecs(msg, &osd_data->bvec_pos);
975ec9123c5SAlex Elder 	} else {
976ec9123c5SAlex Elder 		BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
977ec9123c5SAlex Elder 	}
978ec9123c5SAlex Elder }
979ec9123c5SAlex Elder 
980bb873b53SIlya Dryomov static u32 osd_req_encode_op(struct ceph_osd_op *dst,
981bb873b53SIlya Dryomov 			     const struct ceph_osd_req_op *src)
9823d14c5d2SYehuda Sadeh {
983065a68f9SAlex Elder 	switch (src->op) {
984fbfab539SAlex Elder 	case CEPH_OSD_OP_STAT:
985fbfab539SAlex Elder 		break;
9863d14c5d2SYehuda Sadeh 	case CEPH_OSD_OP_READ:
9873d14c5d2SYehuda Sadeh 	case CEPH_OSD_OP_WRITE:
988e30b7577SIlya Dryomov 	case CEPH_OSD_OP_WRITEFULL:
989ad7a60deSLi Wang 	case CEPH_OSD_OP_ZERO:
990ad7a60deSLi Wang 	case CEPH_OSD_OP_TRUNCATE:
991175face2SAlex Elder 		dst->extent.offset = cpu_to_le64(src->extent.offset);
992175face2SAlex Elder 		dst->extent.length = cpu_to_le64(src->extent.length);
9933d14c5d2SYehuda Sadeh 		dst->extent.truncate_size =
9943d14c5d2SYehuda Sadeh 			cpu_to_le64(src->extent.truncate_size);
9953d14c5d2SYehuda Sadeh 		dst->extent.truncate_seq =
9963d14c5d2SYehuda Sadeh 			cpu_to_le32(src->extent.truncate_seq);
9973d14c5d2SYehuda Sadeh 		break;
9983d14c5d2SYehuda Sadeh 	case CEPH_OSD_OP_CALL:
9993d14c5d2SYehuda Sadeh 		dst->cls.class_len = src->cls.class_len;
10003d14c5d2SYehuda Sadeh 		dst->cls.method_len = src->cls.method_len;
1001bb873b53SIlya Dryomov 		dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
10023d14c5d2SYehuda Sadeh 		break;
1003a40c4f10SYehuda Sadeh 	case CEPH_OSD_OP_WATCH:
1004a40c4f10SYehuda Sadeh 		dst->watch.cookie = cpu_to_le64(src->watch.cookie);
1005922dab61SIlya Dryomov 		dst->watch.ver = cpu_to_le64(0);
1006922dab61SIlya Dryomov 		dst->watch.op = src->watch.op;
1007922dab61SIlya Dryomov 		dst->watch.gen = cpu_to_le32(src->watch.gen);
1008922dab61SIlya Dryomov 		break;
1009922dab61SIlya Dryomov 	case CEPH_OSD_OP_NOTIFY_ACK:
1010a40c4f10SYehuda Sadeh 		break;
101119079203SIlya Dryomov 	case CEPH_OSD_OP_NOTIFY:
101219079203SIlya Dryomov 		dst->notify.cookie = cpu_to_le64(src->notify.cookie);
101319079203SIlya Dryomov 		break;
1014a4ed38d7SDouglas Fuller 	case CEPH_OSD_OP_LIST_WATCHERS:
1015a4ed38d7SDouglas Fuller 		break;
1016c647b8a8SIlya Dryomov 	case CEPH_OSD_OP_SETALLOCHINT:
1017c647b8a8SIlya Dryomov 		dst->alloc_hint.expected_object_size =
1018c647b8a8SIlya Dryomov 		    cpu_to_le64(src->alloc_hint.expected_object_size);
1019c647b8a8SIlya Dryomov 		dst->alloc_hint.expected_write_size =
1020c647b8a8SIlya Dryomov 		    cpu_to_le64(src->alloc_hint.expected_write_size);
1021c647b8a8SIlya Dryomov 		break;
1022d74b50beSYan, Zheng 	case CEPH_OSD_OP_SETXATTR:
1023d74b50beSYan, Zheng 	case CEPH_OSD_OP_CMPXATTR:
1024d74b50beSYan, Zheng 		dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
1025d74b50beSYan, Zheng 		dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
1026d74b50beSYan, Zheng 		dst->xattr.cmp_op = src->xattr.cmp_op;
1027d74b50beSYan, Zheng 		dst->xattr.cmp_mode = src->xattr.cmp_mode;
1028d74b50beSYan, Zheng 		break;
1029864e9197SYan, Zheng 	case CEPH_OSD_OP_CREATE:
1030864e9197SYan, Zheng 	case CEPH_OSD_OP_DELETE:
1031864e9197SYan, Zheng 		break;
103278beb0ffSLuis Henriques 	case CEPH_OSD_OP_COPY_FROM2:
103323ddf9beSLuis Henriques 		dst->copy_from.snapid = cpu_to_le64(src->copy_from.snapid);
103423ddf9beSLuis Henriques 		dst->copy_from.src_version =
103523ddf9beSLuis Henriques 			cpu_to_le64(src->copy_from.src_version);
103623ddf9beSLuis Henriques 		dst->copy_from.flags = src->copy_from.flags;
103723ddf9beSLuis Henriques 		dst->copy_from.src_fadvise_flags =
103823ddf9beSLuis Henriques 			cpu_to_le32(src->copy_from.src_fadvise_flags);
103923ddf9beSLuis Henriques 		break;
10403d14c5d2SYehuda Sadeh 	default:
10414c46459cSAlex Elder 		pr_err("unsupported osd opcode %s\n",
10428f63ca2dSAlex Elder 			ceph_osd_op_name(src->op));
10434c46459cSAlex Elder 		WARN_ON(1);
1044a8dd0a37SAlex Elder 
1045a8dd0a37SAlex Elder 		return 0;
10463d14c5d2SYehuda Sadeh 	}
10477b25bf5fSIlya Dryomov 
1048a8dd0a37SAlex Elder 	dst->op = cpu_to_le16(src->op);
10497b25bf5fSIlya Dryomov 	dst->flags = cpu_to_le32(src->flags);
1050de2aa102SIlya Dryomov 	dst->payload_len = cpu_to_le32(src->indata_len);
1051175face2SAlex Elder 
1052bb873b53SIlya Dryomov 	return src->indata_len;
10533d14c5d2SYehuda Sadeh }
10543d14c5d2SYehuda Sadeh 
10553d14c5d2SYehuda Sadeh /*
10563d14c5d2SYehuda Sadeh  * build new request AND message, calculate layout, and adjust file
10573d14c5d2SYehuda Sadeh  * extent as needed.
10583d14c5d2SYehuda Sadeh  *
10593d14c5d2SYehuda Sadeh  * if the file was recently truncated, we include information about its
10603d14c5d2SYehuda Sadeh  * old and new size so that the object can be updated appropriately.  (we
10613d14c5d2SYehuda Sadeh  * avoid synchronously deleting truncated objects because it's slow.)
10623d14c5d2SYehuda Sadeh  */
10633d14c5d2SYehuda Sadeh struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
10643d14c5d2SYehuda Sadeh 					       struct ceph_file_layout *layout,
10653d14c5d2SYehuda Sadeh 					       struct ceph_vino vino,
1066715e4cd4SYan, Zheng 					       u64 off, u64 *plen,
1067715e4cd4SYan, Zheng 					       unsigned int which, int num_ops,
10683d14c5d2SYehuda Sadeh 					       int opcode, int flags,
10693d14c5d2SYehuda Sadeh 					       struct ceph_snap_context *snapc,
10703d14c5d2SYehuda Sadeh 					       u32 truncate_seq,
10713d14c5d2SYehuda Sadeh 					       u64 truncate_size,
1072153e5167SAlex Elder 					       bool use_mempool)
10733d14c5d2SYehuda Sadeh {
10743d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
107575d1c941SAlex Elder 	u64 objnum = 0;
107675d1c941SAlex Elder 	u64 objoff = 0;
107775d1c941SAlex Elder 	u64 objlen = 0;
10786816282dSSage Weil 	int r;
10793d14c5d2SYehuda Sadeh 
1080ad7a60deSLi Wang 	BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
1081864e9197SYan, Zheng 	       opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
1082864e9197SYan, Zheng 	       opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
10833d14c5d2SYehuda Sadeh 
1084acead002SAlex Elder 	req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
1085ae7ca4a3SAlex Elder 					GFP_NOFS);
108613d1ad16SIlya Dryomov 	if (!req) {
108713d1ad16SIlya Dryomov 		r = -ENOMEM;
108813d1ad16SIlya Dryomov 		goto fail;
108913d1ad16SIlya Dryomov 	}
109079528734SAlex Elder 
10913d14c5d2SYehuda Sadeh 	/* calculate max write size */
1092a19dadfbSAlex Elder 	r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
109313d1ad16SIlya Dryomov 	if (r)
109413d1ad16SIlya Dryomov 		goto fail;
1095a19dadfbSAlex Elder 
1096864e9197SYan, Zheng 	if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
1097144cba14SYan, Zheng 		osd_req_op_init(req, which, opcode, 0);
1098864e9197SYan, Zheng 	} else {
10997627151eSYan, Zheng 		u32 object_size = layout->object_size;
1100864e9197SYan, Zheng 		u32 object_base = off - objoff;
1101ccca4e37SYan, Zheng 		if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
1102d18d1e28SAlex Elder 			if (truncate_size <= object_base) {
1103d18d1e28SAlex Elder 				truncate_size = 0;
1104d18d1e28SAlex Elder 			} else {
1105d18d1e28SAlex Elder 				truncate_size -= object_base;
1106d18d1e28SAlex Elder 				if (truncate_size > object_size)
1107d18d1e28SAlex Elder 					truncate_size = object_size;
1108d18d1e28SAlex Elder 			}
1109ccca4e37SYan, Zheng 		}
1110715e4cd4SYan, Zheng 		osd_req_op_extent_init(req, which, opcode, objoff, objlen,
1111b0270324SAlex Elder 				       truncate_size, truncate_seq);
1112864e9197SYan, Zheng 	}
1113d18d1e28SAlex Elder 
1114bb873b53SIlya Dryomov 	req->r_flags = flags;
11157627151eSYan, Zheng 	req->r_base_oloc.pool = layout->pool_id;
111630c156d9SYan, Zheng 	req->r_base_oloc.pool_ns = ceph_try_get_string(layout->pool_ns);
1117d30291b9SIlya Dryomov 	ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum);
1118dbe0fc41SAlex Elder 
1119bb873b53SIlya Dryomov 	req->r_snapid = vino.snap;
1120bb873b53SIlya Dryomov 	if (flags & CEPH_OSD_FLAG_WRITE)
1121bb873b53SIlya Dryomov 		req->r_data_offset = off;
1122bb873b53SIlya Dryomov 
11230d9c1ab3SIlya Dryomov 	if (num_ops > 1)
11240d9c1ab3SIlya Dryomov 		/*
11250d9c1ab3SIlya Dryomov 		 * This is a special case for ceph_writepages_start(), but it
11260d9c1ab3SIlya Dryomov 		 * also covers ceph_uninline_data().  If more multi-op request
11270d9c1ab3SIlya Dryomov 		 * use cases emerge, we will need a separate helper.
11280d9c1ab3SIlya Dryomov 		 */
11290d9c1ab3SIlya Dryomov 		r = __ceph_osdc_alloc_messages(req, GFP_NOFS, num_ops, 0);
11300d9c1ab3SIlya Dryomov 	else
113113d1ad16SIlya Dryomov 		r = ceph_osdc_alloc_messages(req, GFP_NOFS);
113213d1ad16SIlya Dryomov 	if (r)
113313d1ad16SIlya Dryomov 		goto fail;
113413d1ad16SIlya Dryomov 
11353d14c5d2SYehuda Sadeh 	return req;
113613d1ad16SIlya Dryomov 
113713d1ad16SIlya Dryomov fail:
113813d1ad16SIlya Dryomov 	ceph_osdc_put_request(req);
113913d1ad16SIlya Dryomov 	return ERR_PTR(r);
11403d14c5d2SYehuda Sadeh }
11413d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_new_request);
11423d14c5d2SYehuda Sadeh 
11433d14c5d2SYehuda Sadeh /*
11443d14c5d2SYehuda Sadeh  * We keep osd requests in an rbtree, sorted by ->r_tid.
11453d14c5d2SYehuda Sadeh  */
1146fcd00b68SIlya Dryomov DEFINE_RB_FUNCS(request, struct ceph_osd_request, r_tid, r_node)
11474609245eSIlya Dryomov DEFINE_RB_FUNCS(request_mc, struct ceph_osd_request, r_tid, r_mc_node)
11483d14c5d2SYehuda Sadeh 
114966850df5SIlya Dryomov /*
115066850df5SIlya Dryomov  * Call @fn on each OSD request as long as @fn returns 0.
115166850df5SIlya Dryomov  */
115266850df5SIlya Dryomov static void for_each_request(struct ceph_osd_client *osdc,
115366850df5SIlya Dryomov 			int (*fn)(struct ceph_osd_request *req, void *arg),
115466850df5SIlya Dryomov 			void *arg)
115566850df5SIlya Dryomov {
115666850df5SIlya Dryomov 	struct rb_node *n, *p;
115766850df5SIlya Dryomov 
115866850df5SIlya Dryomov 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
115966850df5SIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
116066850df5SIlya Dryomov 
116166850df5SIlya Dryomov 		for (p = rb_first(&osd->o_requests); p; ) {
116266850df5SIlya Dryomov 			struct ceph_osd_request *req =
116366850df5SIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
116466850df5SIlya Dryomov 
116566850df5SIlya Dryomov 			p = rb_next(p);
116666850df5SIlya Dryomov 			if (fn(req, arg))
116766850df5SIlya Dryomov 				return;
116866850df5SIlya Dryomov 		}
116966850df5SIlya Dryomov 	}
117066850df5SIlya Dryomov 
117166850df5SIlya Dryomov 	for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
117266850df5SIlya Dryomov 		struct ceph_osd_request *req =
117366850df5SIlya Dryomov 		    rb_entry(p, struct ceph_osd_request, r_node);
117466850df5SIlya Dryomov 
117566850df5SIlya Dryomov 		p = rb_next(p);
117666850df5SIlya Dryomov 		if (fn(req, arg))
117766850df5SIlya Dryomov 			return;
117866850df5SIlya Dryomov 	}
117966850df5SIlya Dryomov }
118066850df5SIlya Dryomov 
11810247a0cfSIlya Dryomov static bool osd_homeless(struct ceph_osd *osd)
11820247a0cfSIlya Dryomov {
11830247a0cfSIlya Dryomov 	return osd->o_osd == CEPH_HOMELESS_OSD;
11840247a0cfSIlya Dryomov }
11850247a0cfSIlya Dryomov 
11865aea3dcdSIlya Dryomov static bool osd_registered(struct ceph_osd *osd)
11873d14c5d2SYehuda Sadeh {
11885aea3dcdSIlya Dryomov 	verify_osdc_locked(osd->o_osdc);
11893d14c5d2SYehuda Sadeh 
11905aea3dcdSIlya Dryomov 	return !RB_EMPTY_NODE(&osd->o_node);
11913d14c5d2SYehuda Sadeh }
11923d14c5d2SYehuda Sadeh 
11933d14c5d2SYehuda Sadeh /*
11940247a0cfSIlya Dryomov  * Assumes @osd is zero-initialized.
11950247a0cfSIlya Dryomov  */
11960247a0cfSIlya Dryomov static void osd_init(struct ceph_osd *osd)
11970247a0cfSIlya Dryomov {
119802113a0fSElena Reshetova 	refcount_set(&osd->o_ref, 1);
11990247a0cfSIlya Dryomov 	RB_CLEAR_NODE(&osd->o_node);
12005aea3dcdSIlya Dryomov 	osd->o_requests = RB_ROOT;
1201922dab61SIlya Dryomov 	osd->o_linger_requests = RB_ROOT;
1202a02a946dSIlya Dryomov 	osd->o_backoff_mappings = RB_ROOT;
1203a02a946dSIlya Dryomov 	osd->o_backoffs_by_id = RB_ROOT;
12040247a0cfSIlya Dryomov 	INIT_LIST_HEAD(&osd->o_osd_lru);
12050247a0cfSIlya Dryomov 	INIT_LIST_HEAD(&osd->o_keepalive_item);
12060247a0cfSIlya Dryomov 	osd->o_incarnation = 1;
12075aea3dcdSIlya Dryomov 	mutex_init(&osd->lock);
12080247a0cfSIlya Dryomov }
12090247a0cfSIlya Dryomov 
12100247a0cfSIlya Dryomov static void osd_cleanup(struct ceph_osd *osd)
12110247a0cfSIlya Dryomov {
12120247a0cfSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&osd->o_node));
12135aea3dcdSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
1214922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
1215a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoff_mappings));
1216a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoffs_by_id));
12170247a0cfSIlya Dryomov 	WARN_ON(!list_empty(&osd->o_osd_lru));
12180247a0cfSIlya Dryomov 	WARN_ON(!list_empty(&osd->o_keepalive_item));
12190247a0cfSIlya Dryomov 
12200247a0cfSIlya Dryomov 	if (osd->o_auth.authorizer) {
12210247a0cfSIlya Dryomov 		WARN_ON(osd_homeless(osd));
12220247a0cfSIlya Dryomov 		ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
12230247a0cfSIlya Dryomov 	}
12240247a0cfSIlya Dryomov }
12250247a0cfSIlya Dryomov 
12260247a0cfSIlya Dryomov /*
12273d14c5d2SYehuda Sadeh  * Track open sessions with osds.
12283d14c5d2SYehuda Sadeh  */
1229e10006f8SAlex Elder static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
12303d14c5d2SYehuda Sadeh {
12313d14c5d2SYehuda Sadeh 	struct ceph_osd *osd;
12323d14c5d2SYehuda Sadeh 
12330247a0cfSIlya Dryomov 	WARN_ON(onum == CEPH_HOMELESS_OSD);
12340247a0cfSIlya Dryomov 
12357a28f59bSIlya Dryomov 	osd = kzalloc(sizeof(*osd), GFP_NOIO | __GFP_NOFAIL);
12360247a0cfSIlya Dryomov 	osd_init(osd);
12373d14c5d2SYehuda Sadeh 	osd->o_osdc = osdc;
1238e10006f8SAlex Elder 	osd->o_osd = onum;
12393d14c5d2SYehuda Sadeh 
1240b7a9e5ddSSage Weil 	ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
12413d14c5d2SYehuda Sadeh 
12423d14c5d2SYehuda Sadeh 	return osd;
12433d14c5d2SYehuda Sadeh }
12443d14c5d2SYehuda Sadeh 
12453d14c5d2SYehuda Sadeh static struct ceph_osd *get_osd(struct ceph_osd *osd)
12463d14c5d2SYehuda Sadeh {
124702113a0fSElena Reshetova 	if (refcount_inc_not_zero(&osd->o_ref)) {
124802113a0fSElena Reshetova 		dout("get_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref)-1,
124902113a0fSElena Reshetova 		     refcount_read(&osd->o_ref));
12503d14c5d2SYehuda Sadeh 		return osd;
12513d14c5d2SYehuda Sadeh 	} else {
12523d14c5d2SYehuda Sadeh 		dout("get_osd %p FAIL\n", osd);
12533d14c5d2SYehuda Sadeh 		return NULL;
12543d14c5d2SYehuda Sadeh 	}
12553d14c5d2SYehuda Sadeh }
12563d14c5d2SYehuda Sadeh 
12573d14c5d2SYehuda Sadeh static void put_osd(struct ceph_osd *osd)
12583d14c5d2SYehuda Sadeh {
125902113a0fSElena Reshetova 	dout("put_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref),
126002113a0fSElena Reshetova 	     refcount_read(&osd->o_ref) - 1);
126102113a0fSElena Reshetova 	if (refcount_dec_and_test(&osd->o_ref)) {
12620247a0cfSIlya Dryomov 		osd_cleanup(osd);
12633d14c5d2SYehuda Sadeh 		kfree(osd);
12643d14c5d2SYehuda Sadeh 	}
12653d14c5d2SYehuda Sadeh }
12663d14c5d2SYehuda Sadeh 
1267fcd00b68SIlya Dryomov DEFINE_RB_FUNCS(osd, struct ceph_osd, o_osd, o_node)
1268fcd00b68SIlya Dryomov 
12699dd2845cSIlya Dryomov static void __move_osd_to_lru(struct ceph_osd *osd)
12703d14c5d2SYehuda Sadeh {
12719dd2845cSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
12729dd2845cSIlya Dryomov 
12739dd2845cSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
12743d14c5d2SYehuda Sadeh 	BUG_ON(!list_empty(&osd->o_osd_lru));
1275bbf37ec3SIlya Dryomov 
12769dd2845cSIlya Dryomov 	spin_lock(&osdc->osd_lru_lock);
12773d14c5d2SYehuda Sadeh 	list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
12789dd2845cSIlya Dryomov 	spin_unlock(&osdc->osd_lru_lock);
12799dd2845cSIlya Dryomov 
1280a319bf56SIlya Dryomov 	osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
12813d14c5d2SYehuda Sadeh }
12823d14c5d2SYehuda Sadeh 
12839dd2845cSIlya Dryomov static void maybe_move_osd_to_lru(struct ceph_osd *osd)
1284bbf37ec3SIlya Dryomov {
12855aea3dcdSIlya Dryomov 	if (RB_EMPTY_ROOT(&osd->o_requests) &&
1286922dab61SIlya Dryomov 	    RB_EMPTY_ROOT(&osd->o_linger_requests))
12879dd2845cSIlya Dryomov 		__move_osd_to_lru(osd);
1288bbf37ec3SIlya Dryomov }
1289bbf37ec3SIlya Dryomov 
12903d14c5d2SYehuda Sadeh static void __remove_osd_from_lru(struct ceph_osd *osd)
12913d14c5d2SYehuda Sadeh {
12929dd2845cSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
12939dd2845cSIlya Dryomov 
12949dd2845cSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
12959dd2845cSIlya Dryomov 
12969dd2845cSIlya Dryomov 	spin_lock(&osdc->osd_lru_lock);
12973d14c5d2SYehuda Sadeh 	if (!list_empty(&osd->o_osd_lru))
12983d14c5d2SYehuda Sadeh 		list_del_init(&osd->o_osd_lru);
12999dd2845cSIlya Dryomov 	spin_unlock(&osdc->osd_lru_lock);
13003d14c5d2SYehuda Sadeh }
13013d14c5d2SYehuda Sadeh 
13023d14c5d2SYehuda Sadeh /*
13035aea3dcdSIlya Dryomov  * Close the connection and assign any leftover requests to the
13045aea3dcdSIlya Dryomov  * homeless session.
13055aea3dcdSIlya Dryomov  */
13065aea3dcdSIlya Dryomov static void close_osd(struct ceph_osd *osd)
13075aea3dcdSIlya Dryomov {
13085aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
13095aea3dcdSIlya Dryomov 	struct rb_node *n;
13105aea3dcdSIlya Dryomov 
13115aea3dcdSIlya Dryomov 	verify_osdc_wrlocked(osdc);
13125aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
13135aea3dcdSIlya Dryomov 
13145aea3dcdSIlya Dryomov 	ceph_con_close(&osd->o_con);
13155aea3dcdSIlya Dryomov 
13165aea3dcdSIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; ) {
13175aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
13185aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
13195aea3dcdSIlya Dryomov 
13205aea3dcdSIlya Dryomov 		n = rb_next(n); /* unlink_request() */
13215aea3dcdSIlya Dryomov 
13225aea3dcdSIlya Dryomov 		dout(" reassigning req %p tid %llu\n", req, req->r_tid);
13235aea3dcdSIlya Dryomov 		unlink_request(osd, req);
13245aea3dcdSIlya Dryomov 		link_request(&osdc->homeless_osd, req);
13255aea3dcdSIlya Dryomov 	}
1326922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_linger_requests); n; ) {
1327922dab61SIlya Dryomov 		struct ceph_osd_linger_request *lreq =
1328922dab61SIlya Dryomov 		    rb_entry(n, struct ceph_osd_linger_request, node);
1329922dab61SIlya Dryomov 
1330922dab61SIlya Dryomov 		n = rb_next(n); /* unlink_linger() */
1331922dab61SIlya Dryomov 
1332922dab61SIlya Dryomov 		dout(" reassigning lreq %p linger_id %llu\n", lreq,
1333922dab61SIlya Dryomov 		     lreq->linger_id);
1334922dab61SIlya Dryomov 		unlink_linger(osd, lreq);
1335922dab61SIlya Dryomov 		link_linger(&osdc->homeless_osd, lreq);
1336922dab61SIlya Dryomov 	}
1337a02a946dSIlya Dryomov 	clear_backoffs(osd);
13385aea3dcdSIlya Dryomov 
13395aea3dcdSIlya Dryomov 	__remove_osd_from_lru(osd);
13405aea3dcdSIlya Dryomov 	erase_osd(&osdc->osds, osd);
13415aea3dcdSIlya Dryomov 	put_osd(osd);
13425aea3dcdSIlya Dryomov }
13435aea3dcdSIlya Dryomov 
13445aea3dcdSIlya Dryomov /*
13453d14c5d2SYehuda Sadeh  * reset osd connect
13463d14c5d2SYehuda Sadeh  */
13475aea3dcdSIlya Dryomov static int reopen_osd(struct ceph_osd *osd)
13483d14c5d2SYehuda Sadeh {
1349c3acb181SAlex Elder 	struct ceph_entity_addr *peer_addr;
13503d14c5d2SYehuda Sadeh 
13515aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
13525aea3dcdSIlya Dryomov 
13535aea3dcdSIlya Dryomov 	if (RB_EMPTY_ROOT(&osd->o_requests) &&
1354922dab61SIlya Dryomov 	    RB_EMPTY_ROOT(&osd->o_linger_requests)) {
13555aea3dcdSIlya Dryomov 		close_osd(osd);
1356c3acb181SAlex Elder 		return -ENODEV;
1357c3acb181SAlex Elder 	}
1358c3acb181SAlex Elder 
13595aea3dcdSIlya Dryomov 	peer_addr = &osd->o_osdc->osdmap->osd_addr[osd->o_osd];
1360c3acb181SAlex Elder 	if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
13613d14c5d2SYehuda Sadeh 			!ceph_con_opened(&osd->o_con)) {
13625aea3dcdSIlya Dryomov 		struct rb_node *n;
1363c3acb181SAlex Elder 
13643d14c5d2SYehuda Sadeh 		dout("osd addr hasn't changed and connection never opened, "
13650b4af2e8SIlya Dryomov 		     "letting msgr retry\n");
13663d14c5d2SYehuda Sadeh 		/* touch each r_stamp for handle_timeout()'s benfit */
13675aea3dcdSIlya Dryomov 		for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
13685aea3dcdSIlya Dryomov 			struct ceph_osd_request *req =
13695aea3dcdSIlya Dryomov 			    rb_entry(n, struct ceph_osd_request, r_node);
13703d14c5d2SYehuda Sadeh 			req->r_stamp = jiffies;
13715aea3dcdSIlya Dryomov 		}
1372c3acb181SAlex Elder 
1373c3acb181SAlex Elder 		return -EAGAIN;
13743d14c5d2SYehuda Sadeh 	}
1375c3acb181SAlex Elder 
1376c3acb181SAlex Elder 	ceph_con_close(&osd->o_con);
1377c3acb181SAlex Elder 	ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1378c3acb181SAlex Elder 	osd->o_incarnation++;
1379c3acb181SAlex Elder 
1380c3acb181SAlex Elder 	return 0;
13813d14c5d2SYehuda Sadeh }
13823d14c5d2SYehuda Sadeh 
13835aea3dcdSIlya Dryomov static struct ceph_osd *lookup_create_osd(struct ceph_osd_client *osdc, int o,
13845aea3dcdSIlya Dryomov 					  bool wrlocked)
13853d14c5d2SYehuda Sadeh {
13865aea3dcdSIlya Dryomov 	struct ceph_osd *osd;
13875aea3dcdSIlya Dryomov 
13885aea3dcdSIlya Dryomov 	if (wrlocked)
13895aea3dcdSIlya Dryomov 		verify_osdc_wrlocked(osdc);
13905aea3dcdSIlya Dryomov 	else
13915aea3dcdSIlya Dryomov 		verify_osdc_locked(osdc);
13925aea3dcdSIlya Dryomov 
13935aea3dcdSIlya Dryomov 	if (o != CEPH_HOMELESS_OSD)
13945aea3dcdSIlya Dryomov 		osd = lookup_osd(&osdc->osds, o);
13955aea3dcdSIlya Dryomov 	else
13965aea3dcdSIlya Dryomov 		osd = &osdc->homeless_osd;
13975aea3dcdSIlya Dryomov 	if (!osd) {
13985aea3dcdSIlya Dryomov 		if (!wrlocked)
13995aea3dcdSIlya Dryomov 			return ERR_PTR(-EAGAIN);
14005aea3dcdSIlya Dryomov 
14015aea3dcdSIlya Dryomov 		osd = create_osd(osdc, o);
14025aea3dcdSIlya Dryomov 		insert_osd(&osdc->osds, osd);
14035aea3dcdSIlya Dryomov 		ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
14045aea3dcdSIlya Dryomov 			      &osdc->osdmap->osd_addr[osd->o_osd]);
14055aea3dcdSIlya Dryomov 	}
14065aea3dcdSIlya Dryomov 
14075aea3dcdSIlya Dryomov 	dout("%s osdc %p osd%d -> osd %p\n", __func__, osdc, o, osd);
14085aea3dcdSIlya Dryomov 	return osd;
1409a40c4f10SYehuda Sadeh }
1410a40c4f10SYehuda Sadeh 
14113d14c5d2SYehuda Sadeh /*
14125aea3dcdSIlya Dryomov  * Create request <-> OSD session relation.
14135aea3dcdSIlya Dryomov  *
14145aea3dcdSIlya Dryomov  * @req has to be assigned a tid, @osd may be homeless.
14153d14c5d2SYehuda Sadeh  */
14165aea3dcdSIlya Dryomov static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req)
14173d14c5d2SYehuda Sadeh {
14185aea3dcdSIlya Dryomov 	verify_osd_locked(osd);
14195aea3dcdSIlya Dryomov 	WARN_ON(!req->r_tid || req->r_osd);
14205aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
142135f9f8a0SSage Weil 	     req, req->r_tid);
14225aea3dcdSIlya Dryomov 
14235aea3dcdSIlya Dryomov 	if (!osd_homeless(osd))
14245aea3dcdSIlya Dryomov 		__remove_osd_from_lru(osd);
14255aea3dcdSIlya Dryomov 	else
14265aea3dcdSIlya Dryomov 		atomic_inc(&osd->o_osdc->num_homeless);
14275aea3dcdSIlya Dryomov 
14285aea3dcdSIlya Dryomov 	get_osd(osd);
14295aea3dcdSIlya Dryomov 	insert_request(&osd->o_requests, req);
14305aea3dcdSIlya Dryomov 	req->r_osd = osd;
143135f9f8a0SSage Weil }
143235f9f8a0SSage Weil 
14335aea3dcdSIlya Dryomov static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req)
14343d14c5d2SYehuda Sadeh {
14355aea3dcdSIlya Dryomov 	verify_osd_locked(osd);
14365aea3dcdSIlya Dryomov 	WARN_ON(req->r_osd != osd);
14375aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
14385aea3dcdSIlya Dryomov 	     req, req->r_tid);
14395aea3dcdSIlya Dryomov 
14405aea3dcdSIlya Dryomov 	req->r_osd = NULL;
14415aea3dcdSIlya Dryomov 	erase_request(&osd->o_requests, req);
14425aea3dcdSIlya Dryomov 	put_osd(osd);
14435aea3dcdSIlya Dryomov 
14445aea3dcdSIlya Dryomov 	if (!osd_homeless(osd))
14455aea3dcdSIlya Dryomov 		maybe_move_osd_to_lru(osd);
14465aea3dcdSIlya Dryomov 	else
14475aea3dcdSIlya Dryomov 		atomic_dec(&osd->o_osdc->num_homeless);
14483d14c5d2SYehuda Sadeh }
14493d14c5d2SYehuda Sadeh 
145063244fa1SIlya Dryomov static bool __pool_full(struct ceph_pg_pool_info *pi)
145163244fa1SIlya Dryomov {
145263244fa1SIlya Dryomov 	return pi->flags & CEPH_POOL_FLAG_FULL;
145363244fa1SIlya Dryomov }
145463244fa1SIlya Dryomov 
145542c1b124SIlya Dryomov static bool have_pool_full(struct ceph_osd_client *osdc)
145642c1b124SIlya Dryomov {
145742c1b124SIlya Dryomov 	struct rb_node *n;
145842c1b124SIlya Dryomov 
145942c1b124SIlya Dryomov 	for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
146042c1b124SIlya Dryomov 		struct ceph_pg_pool_info *pi =
146142c1b124SIlya Dryomov 		    rb_entry(n, struct ceph_pg_pool_info, node);
146242c1b124SIlya Dryomov 
146342c1b124SIlya Dryomov 		if (__pool_full(pi))
146442c1b124SIlya Dryomov 			return true;
146542c1b124SIlya Dryomov 	}
146642c1b124SIlya Dryomov 
146742c1b124SIlya Dryomov 	return false;
146842c1b124SIlya Dryomov }
146942c1b124SIlya Dryomov 
14705aea3dcdSIlya Dryomov static bool pool_full(struct ceph_osd_client *osdc, s64 pool_id)
14715aea3dcdSIlya Dryomov {
14725aea3dcdSIlya Dryomov 	struct ceph_pg_pool_info *pi;
14735aea3dcdSIlya Dryomov 
14745aea3dcdSIlya Dryomov 	pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
14755aea3dcdSIlya Dryomov 	if (!pi)
14765aea3dcdSIlya Dryomov 		return false;
14775aea3dcdSIlya Dryomov 
14785aea3dcdSIlya Dryomov 	return __pool_full(pi);
14795aea3dcdSIlya Dryomov }
14805aea3dcdSIlya Dryomov 
14813d14c5d2SYehuda Sadeh /*
1482d29adb34SJosh Durgin  * Returns whether a request should be blocked from being sent
1483d29adb34SJosh Durgin  * based on the current osdmap and osd_client settings.
1484d29adb34SJosh Durgin  */
148563244fa1SIlya Dryomov static bool target_should_be_paused(struct ceph_osd_client *osdc,
148663244fa1SIlya Dryomov 				    const struct ceph_osd_request_target *t,
148763244fa1SIlya Dryomov 				    struct ceph_pg_pool_info *pi)
148863244fa1SIlya Dryomov {
1489b7ec35b3SIlya Dryomov 	bool pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
1490b7ec35b3SIlya Dryomov 	bool pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
1491b7ec35b3SIlya Dryomov 		       ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
149263244fa1SIlya Dryomov 		       __pool_full(pi);
149363244fa1SIlya Dryomov 
14946d637a54SIlya Dryomov 	WARN_ON(pi->id != t->target_oloc.pool);
149558eb7932SJeff Layton 	return ((t->flags & CEPH_OSD_FLAG_READ) && pauserd) ||
149658eb7932SJeff Layton 	       ((t->flags & CEPH_OSD_FLAG_WRITE) && pausewr) ||
149758eb7932SJeff Layton 	       (osdc->osdmap->epoch < osdc->epoch_barrier);
149863244fa1SIlya Dryomov }
149963244fa1SIlya Dryomov 
150063244fa1SIlya Dryomov enum calc_target_result {
150163244fa1SIlya Dryomov 	CALC_TARGET_NO_ACTION = 0,
150263244fa1SIlya Dryomov 	CALC_TARGET_NEED_RESEND,
150363244fa1SIlya Dryomov 	CALC_TARGET_POOL_DNE,
150463244fa1SIlya Dryomov };
150563244fa1SIlya Dryomov 
150663244fa1SIlya Dryomov static enum calc_target_result calc_target(struct ceph_osd_client *osdc,
150763244fa1SIlya Dryomov 					   struct ceph_osd_request_target *t,
150863244fa1SIlya Dryomov 					   bool any_change)
150963244fa1SIlya Dryomov {
151063244fa1SIlya Dryomov 	struct ceph_pg_pool_info *pi;
151163244fa1SIlya Dryomov 	struct ceph_pg pgid, last_pgid;
151263244fa1SIlya Dryomov 	struct ceph_osds up, acting;
151363244fa1SIlya Dryomov 	bool force_resend = false;
151484ed45dfSIlya Dryomov 	bool unpaused = false;
1515a5613724SIlya Dryomov 	bool legacy_change = false;
15167de030d6SIlya Dryomov 	bool split = false;
1517b7ec35b3SIlya Dryomov 	bool sort_bitwise = ceph_osdmap_flag(osdc, CEPH_OSDMAP_SORTBITWISE);
1518ae78dd81SIlya Dryomov 	bool recovery_deletes = ceph_osdmap_flag(osdc,
1519ae78dd81SIlya Dryomov 						 CEPH_OSDMAP_RECOVERY_DELETES);
152063244fa1SIlya Dryomov 	enum calc_target_result ct_res;
152163244fa1SIlya Dryomov 
152204c7d789SIlya Dryomov 	t->epoch = osdc->osdmap->epoch;
152363244fa1SIlya Dryomov 	pi = ceph_pg_pool_by_id(osdc->osdmap, t->base_oloc.pool);
152463244fa1SIlya Dryomov 	if (!pi) {
152563244fa1SIlya Dryomov 		t->osd = CEPH_HOMELESS_OSD;
152663244fa1SIlya Dryomov 		ct_res = CALC_TARGET_POOL_DNE;
152763244fa1SIlya Dryomov 		goto out;
152863244fa1SIlya Dryomov 	}
152963244fa1SIlya Dryomov 
153063244fa1SIlya Dryomov 	if (osdc->osdmap->epoch == pi->last_force_request_resend) {
1531dc93e0e2SIlya Dryomov 		if (t->last_force_resend < pi->last_force_request_resend) {
1532dc93e0e2SIlya Dryomov 			t->last_force_resend = pi->last_force_request_resend;
153363244fa1SIlya Dryomov 			force_resend = true;
1534dc93e0e2SIlya Dryomov 		} else if (t->last_force_resend == 0) {
153563244fa1SIlya Dryomov 			force_resend = true;
153663244fa1SIlya Dryomov 		}
153763244fa1SIlya Dryomov 	}
153863244fa1SIlya Dryomov 
1539db098ec4SIlya Dryomov 	/* apply tiering */
1540db098ec4SIlya Dryomov 	ceph_oid_copy(&t->target_oid, &t->base_oid);
1541db098ec4SIlya Dryomov 	ceph_oloc_copy(&t->target_oloc, &t->base_oloc);
1542db098ec4SIlya Dryomov 	if ((t->flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
154363244fa1SIlya Dryomov 		if (t->flags & CEPH_OSD_FLAG_READ && pi->read_tier >= 0)
154463244fa1SIlya Dryomov 			t->target_oloc.pool = pi->read_tier;
154563244fa1SIlya Dryomov 		if (t->flags & CEPH_OSD_FLAG_WRITE && pi->write_tier >= 0)
154663244fa1SIlya Dryomov 			t->target_oloc.pool = pi->write_tier;
15476d637a54SIlya Dryomov 
15486d637a54SIlya Dryomov 		pi = ceph_pg_pool_by_id(osdc->osdmap, t->target_oloc.pool);
15496d637a54SIlya Dryomov 		if (!pi) {
15506d637a54SIlya Dryomov 			t->osd = CEPH_HOMELESS_OSD;
15516d637a54SIlya Dryomov 			ct_res = CALC_TARGET_POOL_DNE;
15526d637a54SIlya Dryomov 			goto out;
15536d637a54SIlya Dryomov 		}
155463244fa1SIlya Dryomov 	}
155563244fa1SIlya Dryomov 
1556a86f009fSIlya Dryomov 	__ceph_object_locator_to_pg(pi, &t->target_oid, &t->target_oloc, &pgid);
155763244fa1SIlya Dryomov 	last_pgid.pool = pgid.pool;
155863244fa1SIlya Dryomov 	last_pgid.seed = ceph_stable_mod(pgid.seed, t->pg_num, t->pg_num_mask);
155963244fa1SIlya Dryomov 
1560df28152dSIlya Dryomov 	ceph_pg_to_up_acting_osds(osdc->osdmap, pi, &pgid, &up, &acting);
156163244fa1SIlya Dryomov 	if (any_change &&
156263244fa1SIlya Dryomov 	    ceph_is_new_interval(&t->acting,
156363244fa1SIlya Dryomov 				 &acting,
156463244fa1SIlya Dryomov 				 &t->up,
156563244fa1SIlya Dryomov 				 &up,
156663244fa1SIlya Dryomov 				 t->size,
156763244fa1SIlya Dryomov 				 pi->size,
156863244fa1SIlya Dryomov 				 t->min_size,
156963244fa1SIlya Dryomov 				 pi->min_size,
157063244fa1SIlya Dryomov 				 t->pg_num,
157163244fa1SIlya Dryomov 				 pi->pg_num,
157263244fa1SIlya Dryomov 				 t->sort_bitwise,
157363244fa1SIlya Dryomov 				 sort_bitwise,
1574ae78dd81SIlya Dryomov 				 t->recovery_deletes,
1575ae78dd81SIlya Dryomov 				 recovery_deletes,
157663244fa1SIlya Dryomov 				 &last_pgid))
157763244fa1SIlya Dryomov 		force_resend = true;
157863244fa1SIlya Dryomov 
157963244fa1SIlya Dryomov 	if (t->paused && !target_should_be_paused(osdc, t, pi)) {
158063244fa1SIlya Dryomov 		t->paused = false;
158184ed45dfSIlya Dryomov 		unpaused = true;
158263244fa1SIlya Dryomov 	}
158384ed45dfSIlya Dryomov 	legacy_change = ceph_pg_compare(&t->pgid, &pgid) ||
158484ed45dfSIlya Dryomov 			ceph_osds_changed(&t->acting, &acting, any_change);
15857de030d6SIlya Dryomov 	if (t->pg_num)
15867de030d6SIlya Dryomov 		split = ceph_pg_is_split(&last_pgid, t->pg_num, pi->pg_num);
158763244fa1SIlya Dryomov 
15887de030d6SIlya Dryomov 	if (legacy_change || force_resend || split) {
158963244fa1SIlya Dryomov 		t->pgid = pgid; /* struct */
1590df28152dSIlya Dryomov 		ceph_pg_to_primary_shard(osdc->osdmap, pi, &pgid, &t->spgid);
159163244fa1SIlya Dryomov 		ceph_osds_copy(&t->acting, &acting);
159263244fa1SIlya Dryomov 		ceph_osds_copy(&t->up, &up);
159363244fa1SIlya Dryomov 		t->size = pi->size;
159463244fa1SIlya Dryomov 		t->min_size = pi->min_size;
159563244fa1SIlya Dryomov 		t->pg_num = pi->pg_num;
159663244fa1SIlya Dryomov 		t->pg_num_mask = pi->pg_num_mask;
159763244fa1SIlya Dryomov 		t->sort_bitwise = sort_bitwise;
1598ae78dd81SIlya Dryomov 		t->recovery_deletes = recovery_deletes;
159963244fa1SIlya Dryomov 
160063244fa1SIlya Dryomov 		t->osd = acting.primary;
160163244fa1SIlya Dryomov 	}
160263244fa1SIlya Dryomov 
1603a5613724SIlya Dryomov 	if (unpaused || legacy_change || force_resend || split)
160484ed45dfSIlya Dryomov 		ct_res = CALC_TARGET_NEED_RESEND;
160584ed45dfSIlya Dryomov 	else
160684ed45dfSIlya Dryomov 		ct_res = CALC_TARGET_NO_ACTION;
160784ed45dfSIlya Dryomov 
160863244fa1SIlya Dryomov out:
1609a5613724SIlya Dryomov 	dout("%s t %p -> %d%d%d%d ct_res %d osd%d\n", __func__, t, unpaused,
1610a5613724SIlya Dryomov 	     legacy_change, force_resend, split, ct_res, t->osd);
161163244fa1SIlya Dryomov 	return ct_res;
161263244fa1SIlya Dryomov }
161363244fa1SIlya Dryomov 
1614a02a946dSIlya Dryomov static struct ceph_spg_mapping *alloc_spg_mapping(void)
1615a02a946dSIlya Dryomov {
1616a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
1617a02a946dSIlya Dryomov 
1618a02a946dSIlya Dryomov 	spg = kmalloc(sizeof(*spg), GFP_NOIO);
1619a02a946dSIlya Dryomov 	if (!spg)
1620a02a946dSIlya Dryomov 		return NULL;
1621a02a946dSIlya Dryomov 
1622a02a946dSIlya Dryomov 	RB_CLEAR_NODE(&spg->node);
1623a02a946dSIlya Dryomov 	spg->backoffs = RB_ROOT;
1624a02a946dSIlya Dryomov 	return spg;
1625a02a946dSIlya Dryomov }
1626a02a946dSIlya Dryomov 
1627a02a946dSIlya Dryomov static void free_spg_mapping(struct ceph_spg_mapping *spg)
1628a02a946dSIlya Dryomov {
1629a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&spg->node));
1630a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&spg->backoffs));
1631a02a946dSIlya Dryomov 
1632a02a946dSIlya Dryomov 	kfree(spg);
1633a02a946dSIlya Dryomov }
1634a02a946dSIlya Dryomov 
1635a02a946dSIlya Dryomov /*
1636a02a946dSIlya Dryomov  * rbtree of ceph_spg_mapping for handling map<spg_t, ...>, similar to
1637a02a946dSIlya Dryomov  * ceph_pg_mapping.  Used to track OSD backoffs -- a backoff [range] is
1638a02a946dSIlya Dryomov  * defined only within a specific spgid; it does not pass anything to
1639a02a946dSIlya Dryomov  * children on split, or to another primary.
1640a02a946dSIlya Dryomov  */
1641a02a946dSIlya Dryomov DEFINE_RB_FUNCS2(spg_mapping, struct ceph_spg_mapping, spgid, ceph_spg_compare,
1642a02a946dSIlya Dryomov 		 RB_BYPTR, const struct ceph_spg *, node)
1643a02a946dSIlya Dryomov 
1644a02a946dSIlya Dryomov static u64 hoid_get_bitwise_key(const struct ceph_hobject_id *hoid)
1645a02a946dSIlya Dryomov {
1646a02a946dSIlya Dryomov 	return hoid->is_max ? 0x100000000ull : hoid->hash_reverse_bits;
1647a02a946dSIlya Dryomov }
1648a02a946dSIlya Dryomov 
1649a02a946dSIlya Dryomov static void hoid_get_effective_key(const struct ceph_hobject_id *hoid,
1650a02a946dSIlya Dryomov 				   void **pkey, size_t *pkey_len)
1651a02a946dSIlya Dryomov {
1652a02a946dSIlya Dryomov 	if (hoid->key_len) {
1653a02a946dSIlya Dryomov 		*pkey = hoid->key;
1654a02a946dSIlya Dryomov 		*pkey_len = hoid->key_len;
1655a02a946dSIlya Dryomov 	} else {
1656a02a946dSIlya Dryomov 		*pkey = hoid->oid;
1657a02a946dSIlya Dryomov 		*pkey_len = hoid->oid_len;
1658a02a946dSIlya Dryomov 	}
1659a02a946dSIlya Dryomov }
1660a02a946dSIlya Dryomov 
1661a02a946dSIlya Dryomov static int compare_names(const void *name1, size_t name1_len,
1662a02a946dSIlya Dryomov 			 const void *name2, size_t name2_len)
1663a02a946dSIlya Dryomov {
1664a02a946dSIlya Dryomov 	int ret;
1665a02a946dSIlya Dryomov 
1666a02a946dSIlya Dryomov 	ret = memcmp(name1, name2, min(name1_len, name2_len));
1667a02a946dSIlya Dryomov 	if (!ret) {
1668a02a946dSIlya Dryomov 		if (name1_len < name2_len)
1669a02a946dSIlya Dryomov 			ret = -1;
1670a02a946dSIlya Dryomov 		else if (name1_len > name2_len)
1671a02a946dSIlya Dryomov 			ret = 1;
1672a02a946dSIlya Dryomov 	}
1673a02a946dSIlya Dryomov 	return ret;
1674a02a946dSIlya Dryomov }
1675a02a946dSIlya Dryomov 
1676a02a946dSIlya Dryomov static int hoid_compare(const struct ceph_hobject_id *lhs,
1677a02a946dSIlya Dryomov 			const struct ceph_hobject_id *rhs)
1678a02a946dSIlya Dryomov {
1679a02a946dSIlya Dryomov 	void *effective_key1, *effective_key2;
1680a02a946dSIlya Dryomov 	size_t effective_key1_len, effective_key2_len;
1681a02a946dSIlya Dryomov 	int ret;
1682a02a946dSIlya Dryomov 
1683a02a946dSIlya Dryomov 	if (lhs->is_max < rhs->is_max)
1684a02a946dSIlya Dryomov 		return -1;
1685a02a946dSIlya Dryomov 	if (lhs->is_max > rhs->is_max)
1686a02a946dSIlya Dryomov 		return 1;
1687a02a946dSIlya Dryomov 
1688a02a946dSIlya Dryomov 	if (lhs->pool < rhs->pool)
1689a02a946dSIlya Dryomov 		return -1;
1690a02a946dSIlya Dryomov 	if (lhs->pool > rhs->pool)
1691a02a946dSIlya Dryomov 		return 1;
1692a02a946dSIlya Dryomov 
1693a02a946dSIlya Dryomov 	if (hoid_get_bitwise_key(lhs) < hoid_get_bitwise_key(rhs))
1694a02a946dSIlya Dryomov 		return -1;
1695a02a946dSIlya Dryomov 	if (hoid_get_bitwise_key(lhs) > hoid_get_bitwise_key(rhs))
1696a02a946dSIlya Dryomov 		return 1;
1697a02a946dSIlya Dryomov 
1698a02a946dSIlya Dryomov 	ret = compare_names(lhs->nspace, lhs->nspace_len,
1699a02a946dSIlya Dryomov 			    rhs->nspace, rhs->nspace_len);
1700a02a946dSIlya Dryomov 	if (ret)
1701a02a946dSIlya Dryomov 		return ret;
1702a02a946dSIlya Dryomov 
1703a02a946dSIlya Dryomov 	hoid_get_effective_key(lhs, &effective_key1, &effective_key1_len);
1704a02a946dSIlya Dryomov 	hoid_get_effective_key(rhs, &effective_key2, &effective_key2_len);
1705a02a946dSIlya Dryomov 	ret = compare_names(effective_key1, effective_key1_len,
1706a02a946dSIlya Dryomov 			    effective_key2, effective_key2_len);
1707a02a946dSIlya Dryomov 	if (ret)
1708a02a946dSIlya Dryomov 		return ret;
1709a02a946dSIlya Dryomov 
1710a02a946dSIlya Dryomov 	ret = compare_names(lhs->oid, lhs->oid_len, rhs->oid, rhs->oid_len);
1711a02a946dSIlya Dryomov 	if (ret)
1712a02a946dSIlya Dryomov 		return ret;
1713a02a946dSIlya Dryomov 
1714a02a946dSIlya Dryomov 	if (lhs->snapid < rhs->snapid)
1715a02a946dSIlya Dryomov 		return -1;
1716a02a946dSIlya Dryomov 	if (lhs->snapid > rhs->snapid)
1717a02a946dSIlya Dryomov 		return 1;
1718a02a946dSIlya Dryomov 
1719a02a946dSIlya Dryomov 	return 0;
1720a02a946dSIlya Dryomov }
1721a02a946dSIlya Dryomov 
1722a02a946dSIlya Dryomov /*
1723a02a946dSIlya Dryomov  * For decoding ->begin and ->end of MOSDBackoff only -- no MIN/MAX
1724a02a946dSIlya Dryomov  * compat stuff here.
1725a02a946dSIlya Dryomov  *
1726a02a946dSIlya Dryomov  * Assumes @hoid is zero-initialized.
1727a02a946dSIlya Dryomov  */
1728a02a946dSIlya Dryomov static int decode_hoid(void **p, void *end, struct ceph_hobject_id *hoid)
1729a02a946dSIlya Dryomov {
1730a02a946dSIlya Dryomov 	u8 struct_v;
1731a02a946dSIlya Dryomov 	u32 struct_len;
1732a02a946dSIlya Dryomov 	int ret;
1733a02a946dSIlya Dryomov 
1734a02a946dSIlya Dryomov 	ret = ceph_start_decoding(p, end, 4, "hobject_t", &struct_v,
1735a02a946dSIlya Dryomov 				  &struct_len);
1736a02a946dSIlya Dryomov 	if (ret)
1737a02a946dSIlya Dryomov 		return ret;
1738a02a946dSIlya Dryomov 
1739a02a946dSIlya Dryomov 	if (struct_v < 4) {
1740a02a946dSIlya Dryomov 		pr_err("got struct_v %d < 4 of hobject_t\n", struct_v);
1741a02a946dSIlya Dryomov 		goto e_inval;
1742a02a946dSIlya Dryomov 	}
1743a02a946dSIlya Dryomov 
1744a02a946dSIlya Dryomov 	hoid->key = ceph_extract_encoded_string(p, end, &hoid->key_len,
1745a02a946dSIlya Dryomov 						GFP_NOIO);
1746a02a946dSIlya Dryomov 	if (IS_ERR(hoid->key)) {
1747a02a946dSIlya Dryomov 		ret = PTR_ERR(hoid->key);
1748a02a946dSIlya Dryomov 		hoid->key = NULL;
1749a02a946dSIlya Dryomov 		return ret;
1750a02a946dSIlya Dryomov 	}
1751a02a946dSIlya Dryomov 
1752a02a946dSIlya Dryomov 	hoid->oid = ceph_extract_encoded_string(p, end, &hoid->oid_len,
1753a02a946dSIlya Dryomov 						GFP_NOIO);
1754a02a946dSIlya Dryomov 	if (IS_ERR(hoid->oid)) {
1755a02a946dSIlya Dryomov 		ret = PTR_ERR(hoid->oid);
1756a02a946dSIlya Dryomov 		hoid->oid = NULL;
1757a02a946dSIlya Dryomov 		return ret;
1758a02a946dSIlya Dryomov 	}
1759a02a946dSIlya Dryomov 
1760a02a946dSIlya Dryomov 	ceph_decode_64_safe(p, end, hoid->snapid, e_inval);
1761a02a946dSIlya Dryomov 	ceph_decode_32_safe(p, end, hoid->hash, e_inval);
1762a02a946dSIlya Dryomov 	ceph_decode_8_safe(p, end, hoid->is_max, e_inval);
1763a02a946dSIlya Dryomov 
1764a02a946dSIlya Dryomov 	hoid->nspace = ceph_extract_encoded_string(p, end, &hoid->nspace_len,
1765a02a946dSIlya Dryomov 						   GFP_NOIO);
1766a02a946dSIlya Dryomov 	if (IS_ERR(hoid->nspace)) {
1767a02a946dSIlya Dryomov 		ret = PTR_ERR(hoid->nspace);
1768a02a946dSIlya Dryomov 		hoid->nspace = NULL;
1769a02a946dSIlya Dryomov 		return ret;
1770a02a946dSIlya Dryomov 	}
1771a02a946dSIlya Dryomov 
1772a02a946dSIlya Dryomov 	ceph_decode_64_safe(p, end, hoid->pool, e_inval);
1773a02a946dSIlya Dryomov 
1774a02a946dSIlya Dryomov 	ceph_hoid_build_hash_cache(hoid);
1775a02a946dSIlya Dryomov 	return 0;
1776a02a946dSIlya Dryomov 
1777a02a946dSIlya Dryomov e_inval:
1778a02a946dSIlya Dryomov 	return -EINVAL;
1779a02a946dSIlya Dryomov }
1780a02a946dSIlya Dryomov 
1781a02a946dSIlya Dryomov static int hoid_encoding_size(const struct ceph_hobject_id *hoid)
1782a02a946dSIlya Dryomov {
1783a02a946dSIlya Dryomov 	return 8 + 4 + 1 + 8 + /* snapid, hash, is_max, pool */
1784a02a946dSIlya Dryomov 	       4 + hoid->key_len + 4 + hoid->oid_len + 4 + hoid->nspace_len;
1785a02a946dSIlya Dryomov }
1786a02a946dSIlya Dryomov 
1787a02a946dSIlya Dryomov static void encode_hoid(void **p, void *end, const struct ceph_hobject_id *hoid)
1788a02a946dSIlya Dryomov {
1789a02a946dSIlya Dryomov 	ceph_start_encoding(p, 4, 3, hoid_encoding_size(hoid));
1790a02a946dSIlya Dryomov 	ceph_encode_string(p, end, hoid->key, hoid->key_len);
1791a02a946dSIlya Dryomov 	ceph_encode_string(p, end, hoid->oid, hoid->oid_len);
1792a02a946dSIlya Dryomov 	ceph_encode_64(p, hoid->snapid);
1793a02a946dSIlya Dryomov 	ceph_encode_32(p, hoid->hash);
1794a02a946dSIlya Dryomov 	ceph_encode_8(p, hoid->is_max);
1795a02a946dSIlya Dryomov 	ceph_encode_string(p, end, hoid->nspace, hoid->nspace_len);
1796a02a946dSIlya Dryomov 	ceph_encode_64(p, hoid->pool);
1797a02a946dSIlya Dryomov }
1798a02a946dSIlya Dryomov 
1799a02a946dSIlya Dryomov static void free_hoid(struct ceph_hobject_id *hoid)
1800a02a946dSIlya Dryomov {
1801a02a946dSIlya Dryomov 	if (hoid) {
1802a02a946dSIlya Dryomov 		kfree(hoid->key);
1803a02a946dSIlya Dryomov 		kfree(hoid->oid);
1804a02a946dSIlya Dryomov 		kfree(hoid->nspace);
1805a02a946dSIlya Dryomov 		kfree(hoid);
1806a02a946dSIlya Dryomov 	}
1807a02a946dSIlya Dryomov }
1808a02a946dSIlya Dryomov 
1809a02a946dSIlya Dryomov static struct ceph_osd_backoff *alloc_backoff(void)
1810a02a946dSIlya Dryomov {
1811a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
1812a02a946dSIlya Dryomov 
1813a02a946dSIlya Dryomov 	backoff = kzalloc(sizeof(*backoff), GFP_NOIO);
1814a02a946dSIlya Dryomov 	if (!backoff)
1815a02a946dSIlya Dryomov 		return NULL;
1816a02a946dSIlya Dryomov 
1817a02a946dSIlya Dryomov 	RB_CLEAR_NODE(&backoff->spg_node);
1818a02a946dSIlya Dryomov 	RB_CLEAR_NODE(&backoff->id_node);
1819a02a946dSIlya Dryomov 	return backoff;
1820a02a946dSIlya Dryomov }
1821a02a946dSIlya Dryomov 
1822a02a946dSIlya Dryomov static void free_backoff(struct ceph_osd_backoff *backoff)
1823a02a946dSIlya Dryomov {
1824a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&backoff->spg_node));
1825a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&backoff->id_node));
1826a02a946dSIlya Dryomov 
1827a02a946dSIlya Dryomov 	free_hoid(backoff->begin);
1828a02a946dSIlya Dryomov 	free_hoid(backoff->end);
1829a02a946dSIlya Dryomov 	kfree(backoff);
1830a02a946dSIlya Dryomov }
1831a02a946dSIlya Dryomov 
1832a02a946dSIlya Dryomov /*
1833a02a946dSIlya Dryomov  * Within a specific spgid, backoffs are managed by ->begin hoid.
1834a02a946dSIlya Dryomov  */
1835a02a946dSIlya Dryomov DEFINE_RB_INSDEL_FUNCS2(backoff, struct ceph_osd_backoff, begin, hoid_compare,
1836a02a946dSIlya Dryomov 			RB_BYVAL, spg_node);
1837a02a946dSIlya Dryomov 
1838a02a946dSIlya Dryomov static struct ceph_osd_backoff *lookup_containing_backoff(struct rb_root *root,
1839a02a946dSIlya Dryomov 					    const struct ceph_hobject_id *hoid)
1840a02a946dSIlya Dryomov {
1841a02a946dSIlya Dryomov 	struct rb_node *n = root->rb_node;
1842a02a946dSIlya Dryomov 
1843a02a946dSIlya Dryomov 	while (n) {
1844a02a946dSIlya Dryomov 		struct ceph_osd_backoff *cur =
1845a02a946dSIlya Dryomov 		    rb_entry(n, struct ceph_osd_backoff, spg_node);
1846a02a946dSIlya Dryomov 		int cmp;
1847a02a946dSIlya Dryomov 
1848a02a946dSIlya Dryomov 		cmp = hoid_compare(hoid, cur->begin);
1849a02a946dSIlya Dryomov 		if (cmp < 0) {
1850a02a946dSIlya Dryomov 			n = n->rb_left;
1851a02a946dSIlya Dryomov 		} else if (cmp > 0) {
1852a02a946dSIlya Dryomov 			if (hoid_compare(hoid, cur->end) < 0)
1853a02a946dSIlya Dryomov 				return cur;
1854a02a946dSIlya Dryomov 
1855a02a946dSIlya Dryomov 			n = n->rb_right;
1856a02a946dSIlya Dryomov 		} else {
1857a02a946dSIlya Dryomov 			return cur;
1858a02a946dSIlya Dryomov 		}
1859a02a946dSIlya Dryomov 	}
1860a02a946dSIlya Dryomov 
1861a02a946dSIlya Dryomov 	return NULL;
1862a02a946dSIlya Dryomov }
1863a02a946dSIlya Dryomov 
1864a02a946dSIlya Dryomov /*
1865a02a946dSIlya Dryomov  * Each backoff has a unique id within its OSD session.
1866a02a946dSIlya Dryomov  */
1867a02a946dSIlya Dryomov DEFINE_RB_FUNCS(backoff_by_id, struct ceph_osd_backoff, id, id_node)
1868a02a946dSIlya Dryomov 
1869a02a946dSIlya Dryomov static void clear_backoffs(struct ceph_osd *osd)
1870a02a946dSIlya Dryomov {
1871a02a946dSIlya Dryomov 	while (!RB_EMPTY_ROOT(&osd->o_backoff_mappings)) {
1872a02a946dSIlya Dryomov 		struct ceph_spg_mapping *spg =
1873a02a946dSIlya Dryomov 		    rb_entry(rb_first(&osd->o_backoff_mappings),
1874a02a946dSIlya Dryomov 			     struct ceph_spg_mapping, node);
1875a02a946dSIlya Dryomov 
1876a02a946dSIlya Dryomov 		while (!RB_EMPTY_ROOT(&spg->backoffs)) {
1877a02a946dSIlya Dryomov 			struct ceph_osd_backoff *backoff =
1878a02a946dSIlya Dryomov 			    rb_entry(rb_first(&spg->backoffs),
1879a02a946dSIlya Dryomov 				     struct ceph_osd_backoff, spg_node);
1880a02a946dSIlya Dryomov 
1881a02a946dSIlya Dryomov 			erase_backoff(&spg->backoffs, backoff);
1882a02a946dSIlya Dryomov 			erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
1883a02a946dSIlya Dryomov 			free_backoff(backoff);
1884a02a946dSIlya Dryomov 		}
1885a02a946dSIlya Dryomov 		erase_spg_mapping(&osd->o_backoff_mappings, spg);
1886a02a946dSIlya Dryomov 		free_spg_mapping(spg);
1887a02a946dSIlya Dryomov 	}
1888a02a946dSIlya Dryomov }
1889a02a946dSIlya Dryomov 
1890a02a946dSIlya Dryomov /*
1891a02a946dSIlya Dryomov  * Set up a temporary, non-owning view into @t.
1892a02a946dSIlya Dryomov  */
1893a02a946dSIlya Dryomov static void hoid_fill_from_target(struct ceph_hobject_id *hoid,
1894a02a946dSIlya Dryomov 				  const struct ceph_osd_request_target *t)
1895a02a946dSIlya Dryomov {
1896a02a946dSIlya Dryomov 	hoid->key = NULL;
1897a02a946dSIlya Dryomov 	hoid->key_len = 0;
1898a02a946dSIlya Dryomov 	hoid->oid = t->target_oid.name;
1899a02a946dSIlya Dryomov 	hoid->oid_len = t->target_oid.name_len;
1900a02a946dSIlya Dryomov 	hoid->snapid = CEPH_NOSNAP;
1901a02a946dSIlya Dryomov 	hoid->hash = t->pgid.seed;
1902a02a946dSIlya Dryomov 	hoid->is_max = false;
1903a02a946dSIlya Dryomov 	if (t->target_oloc.pool_ns) {
1904a02a946dSIlya Dryomov 		hoid->nspace = t->target_oloc.pool_ns->str;
1905a02a946dSIlya Dryomov 		hoid->nspace_len = t->target_oloc.pool_ns->len;
1906a02a946dSIlya Dryomov 	} else {
1907a02a946dSIlya Dryomov 		hoid->nspace = NULL;
1908a02a946dSIlya Dryomov 		hoid->nspace_len = 0;
1909a02a946dSIlya Dryomov 	}
1910a02a946dSIlya Dryomov 	hoid->pool = t->target_oloc.pool;
1911a02a946dSIlya Dryomov 	ceph_hoid_build_hash_cache(hoid);
1912a02a946dSIlya Dryomov }
1913a02a946dSIlya Dryomov 
1914a02a946dSIlya Dryomov static bool should_plug_request(struct ceph_osd_request *req)
1915a02a946dSIlya Dryomov {
1916a02a946dSIlya Dryomov 	struct ceph_osd *osd = req->r_osd;
1917a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
1918a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
1919a02a946dSIlya Dryomov 	struct ceph_hobject_id hoid;
1920a02a946dSIlya Dryomov 
1921a02a946dSIlya Dryomov 	spg = lookup_spg_mapping(&osd->o_backoff_mappings, &req->r_t.spgid);
1922a02a946dSIlya Dryomov 	if (!spg)
1923a02a946dSIlya Dryomov 		return false;
1924a02a946dSIlya Dryomov 
1925a02a946dSIlya Dryomov 	hoid_fill_from_target(&hoid, &req->r_t);
1926a02a946dSIlya Dryomov 	backoff = lookup_containing_backoff(&spg->backoffs, &hoid);
1927a02a946dSIlya Dryomov 	if (!backoff)
1928a02a946dSIlya Dryomov 		return false;
1929a02a946dSIlya Dryomov 
1930a02a946dSIlya Dryomov 	dout("%s req %p tid %llu backoff osd%d spgid %llu.%xs%d id %llu\n",
1931a02a946dSIlya Dryomov 	     __func__, req, req->r_tid, osd->o_osd, backoff->spgid.pgid.pool,
1932a02a946dSIlya Dryomov 	     backoff->spgid.pgid.seed, backoff->spgid.shard, backoff->id);
1933a02a946dSIlya Dryomov 	return true;
1934a02a946dSIlya Dryomov }
1935a02a946dSIlya Dryomov 
19360d9c1ab3SIlya Dryomov /*
19370d9c1ab3SIlya Dryomov  * Keep get_num_data_items() in sync with this function.
19380d9c1ab3SIlya Dryomov  */
193998c4bfe9SIlya Dryomov static void setup_request_data(struct ceph_osd_request *req)
19403d14c5d2SYehuda Sadeh {
194198c4bfe9SIlya Dryomov 	struct ceph_msg *request_msg = req->r_request;
194298c4bfe9SIlya Dryomov 	struct ceph_msg *reply_msg = req->r_reply;
194398c4bfe9SIlya Dryomov 	struct ceph_osd_req_op *op;
19443d14c5d2SYehuda Sadeh 
194598c4bfe9SIlya Dryomov 	if (req->r_request->num_data_items || req->r_reply->num_data_items)
1946bb873b53SIlya Dryomov 		return;
19473d14c5d2SYehuda Sadeh 
194898c4bfe9SIlya Dryomov 	WARN_ON(request_msg->data_length || reply_msg->data_length);
194998c4bfe9SIlya Dryomov 	for (op = req->r_ops; op != &req->r_ops[req->r_num_ops]; op++) {
1950bb873b53SIlya Dryomov 		switch (op->op) {
1951bb873b53SIlya Dryomov 		/* request */
1952bb873b53SIlya Dryomov 		case CEPH_OSD_OP_WRITE:
1953bb873b53SIlya Dryomov 		case CEPH_OSD_OP_WRITEFULL:
1954bb873b53SIlya Dryomov 			WARN_ON(op->indata_len != op->extent.length);
195598c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(request_msg,
195698c4bfe9SIlya Dryomov 					       &op->extent.osd_data);
1957bb873b53SIlya Dryomov 			break;
1958bb873b53SIlya Dryomov 		case CEPH_OSD_OP_SETXATTR:
1959bb873b53SIlya Dryomov 		case CEPH_OSD_OP_CMPXATTR:
1960bb873b53SIlya Dryomov 			WARN_ON(op->indata_len != op->xattr.name_len +
1961bb873b53SIlya Dryomov 						  op->xattr.value_len);
196298c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(request_msg,
196398c4bfe9SIlya Dryomov 					       &op->xattr.osd_data);
1964bb873b53SIlya Dryomov 			break;
1965922dab61SIlya Dryomov 		case CEPH_OSD_OP_NOTIFY_ACK:
196698c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(request_msg,
1967922dab61SIlya Dryomov 					       &op->notify_ack.request_data);
1968922dab61SIlya Dryomov 			break;
196978beb0ffSLuis Henriques 		case CEPH_OSD_OP_COPY_FROM2:
197023ddf9beSLuis Henriques 			ceph_osdc_msg_data_add(request_msg,
197123ddf9beSLuis Henriques 					       &op->copy_from.osd_data);
197223ddf9beSLuis Henriques 			break;
1973bb873b53SIlya Dryomov 
1974bb873b53SIlya Dryomov 		/* reply */
1975bb873b53SIlya Dryomov 		case CEPH_OSD_OP_STAT:
197698c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(reply_msg,
1977bb873b53SIlya Dryomov 					       &op->raw_data_in);
1978bb873b53SIlya Dryomov 			break;
1979bb873b53SIlya Dryomov 		case CEPH_OSD_OP_READ:
198098c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(reply_msg,
1981bb873b53SIlya Dryomov 					       &op->extent.osd_data);
1982bb873b53SIlya Dryomov 			break;
1983a4ed38d7SDouglas Fuller 		case CEPH_OSD_OP_LIST_WATCHERS:
198498c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(reply_msg,
1985a4ed38d7SDouglas Fuller 					       &op->list_watchers.response_data);
1986a4ed38d7SDouglas Fuller 			break;
1987bb873b53SIlya Dryomov 
1988bb873b53SIlya Dryomov 		/* both */
1989bb873b53SIlya Dryomov 		case CEPH_OSD_OP_CALL:
1990bb873b53SIlya Dryomov 			WARN_ON(op->indata_len != op->cls.class_len +
1991bb873b53SIlya Dryomov 						  op->cls.method_len +
1992bb873b53SIlya Dryomov 						  op->cls.indata_len);
199398c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(request_msg,
199498c4bfe9SIlya Dryomov 					       &op->cls.request_info);
1995bb873b53SIlya Dryomov 			/* optional, can be NONE */
199698c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(request_msg,
199798c4bfe9SIlya Dryomov 					       &op->cls.request_data);
1998bb873b53SIlya Dryomov 			/* optional, can be NONE */
199998c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(reply_msg,
2000bb873b53SIlya Dryomov 					       &op->cls.response_data);
2001bb873b53SIlya Dryomov 			break;
200219079203SIlya Dryomov 		case CEPH_OSD_OP_NOTIFY:
200398c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(request_msg,
200419079203SIlya Dryomov 					       &op->notify.request_data);
200598c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(reply_msg,
200619079203SIlya Dryomov 					       &op->notify.response_data);
200719079203SIlya Dryomov 			break;
2008bb873b53SIlya Dryomov 		}
2009bb873b53SIlya Dryomov 	}
2010bb873b53SIlya Dryomov }
2011bb873b53SIlya Dryomov 
20122e59ffd1SIlya Dryomov static void encode_pgid(void **p, const struct ceph_pg *pgid)
20132e59ffd1SIlya Dryomov {
20142e59ffd1SIlya Dryomov 	ceph_encode_8(p, 1);
20152e59ffd1SIlya Dryomov 	ceph_encode_64(p, pgid->pool);
20162e59ffd1SIlya Dryomov 	ceph_encode_32(p, pgid->seed);
20172e59ffd1SIlya Dryomov 	ceph_encode_32(p, -1); /* preferred */
20182e59ffd1SIlya Dryomov }
20192e59ffd1SIlya Dryomov 
20208cb441c0SIlya Dryomov static void encode_spgid(void **p, const struct ceph_spg *spgid)
20218cb441c0SIlya Dryomov {
20228cb441c0SIlya Dryomov 	ceph_start_encoding(p, 1, 1, CEPH_PGID_ENCODING_LEN + 1);
20238cb441c0SIlya Dryomov 	encode_pgid(p, &spgid->pgid);
20248cb441c0SIlya Dryomov 	ceph_encode_8(p, spgid->shard);
20258cb441c0SIlya Dryomov }
20268cb441c0SIlya Dryomov 
20272e59ffd1SIlya Dryomov static void encode_oloc(void **p, void *end,
20282e59ffd1SIlya Dryomov 			const struct ceph_object_locator *oloc)
20292e59ffd1SIlya Dryomov {
20302e59ffd1SIlya Dryomov 	ceph_start_encoding(p, 5, 4, ceph_oloc_encoding_size(oloc));
20312e59ffd1SIlya Dryomov 	ceph_encode_64(p, oloc->pool);
20322e59ffd1SIlya Dryomov 	ceph_encode_32(p, -1); /* preferred */
20332e59ffd1SIlya Dryomov 	ceph_encode_32(p, 0);  /* key len */
20342e59ffd1SIlya Dryomov 	if (oloc->pool_ns)
20352e59ffd1SIlya Dryomov 		ceph_encode_string(p, end, oloc->pool_ns->str,
20362e59ffd1SIlya Dryomov 				   oloc->pool_ns->len);
20372e59ffd1SIlya Dryomov 	else
20382e59ffd1SIlya Dryomov 		ceph_encode_32(p, 0);
20392e59ffd1SIlya Dryomov }
20402e59ffd1SIlya Dryomov 
20418cb441c0SIlya Dryomov static void encode_request_partial(struct ceph_osd_request *req,
20428cb441c0SIlya Dryomov 				   struct ceph_msg *msg)
2043bb873b53SIlya Dryomov {
2044bb873b53SIlya Dryomov 	void *p = msg->front.iov_base;
2045bb873b53SIlya Dryomov 	void *const end = p + msg->front_alloc_len;
2046bb873b53SIlya Dryomov 	u32 data_len = 0;
2047bb873b53SIlya Dryomov 	int i;
2048bb873b53SIlya Dryomov 
2049bb873b53SIlya Dryomov 	if (req->r_flags & CEPH_OSD_FLAG_WRITE) {
2050bb873b53SIlya Dryomov 		/* snapshots aren't writeable */
2051bb873b53SIlya Dryomov 		WARN_ON(req->r_snapid != CEPH_NOSNAP);
2052bb873b53SIlya Dryomov 	} else {
2053bb873b53SIlya Dryomov 		WARN_ON(req->r_mtime.tv_sec || req->r_mtime.tv_nsec ||
2054bb873b53SIlya Dryomov 			req->r_data_offset || req->r_snapc);
2055bb873b53SIlya Dryomov 	}
2056bb873b53SIlya Dryomov 
205798c4bfe9SIlya Dryomov 	setup_request_data(req);
2058bb873b53SIlya Dryomov 
20598cb441c0SIlya Dryomov 	encode_spgid(&p, &req->r_t.spgid); /* actual spg */
20608cb441c0SIlya Dryomov 	ceph_encode_32(&p, req->r_t.pgid.seed); /* raw hash */
2061bb873b53SIlya Dryomov 	ceph_encode_32(&p, req->r_osdc->osdmap->epoch);
2062bb873b53SIlya Dryomov 	ceph_encode_32(&p, req->r_flags);
20638cb441c0SIlya Dryomov 
20648cb441c0SIlya Dryomov 	/* reqid */
20658cb441c0SIlya Dryomov 	ceph_start_encoding(&p, 2, 2, sizeof(struct ceph_osd_reqid));
20668cb441c0SIlya Dryomov 	memset(p, 0, sizeof(struct ceph_osd_reqid));
20678cb441c0SIlya Dryomov 	p += sizeof(struct ceph_osd_reqid);
20688cb441c0SIlya Dryomov 
20698cb441c0SIlya Dryomov 	/* trace */
20708cb441c0SIlya Dryomov 	memset(p, 0, sizeof(struct ceph_blkin_trace_info));
20718cb441c0SIlya Dryomov 	p += sizeof(struct ceph_blkin_trace_info);
20728cb441c0SIlya Dryomov 
20738cb441c0SIlya Dryomov 	ceph_encode_32(&p, 0); /* client_inc, always 0 */
2074fac02ddfSArnd Bergmann 	ceph_encode_timespec64(p, &req->r_mtime);
2075bb873b53SIlya Dryomov 	p += sizeof(struct ceph_timespec);
2076aa26d662SJeff Layton 
20772e59ffd1SIlya Dryomov 	encode_oloc(&p, end, &req->r_t.target_oloc);
20782e59ffd1SIlya Dryomov 	ceph_encode_string(&p, end, req->r_t.target_oid.name,
20792e59ffd1SIlya Dryomov 			   req->r_t.target_oid.name_len);
2080bb873b53SIlya Dryomov 
2081bb873b53SIlya Dryomov 	/* ops, can imply data */
2082bb873b53SIlya Dryomov 	ceph_encode_16(&p, req->r_num_ops);
2083bb873b53SIlya Dryomov 	for (i = 0; i < req->r_num_ops; i++) {
2084bb873b53SIlya Dryomov 		data_len += osd_req_encode_op(p, &req->r_ops[i]);
2085bb873b53SIlya Dryomov 		p += sizeof(struct ceph_osd_op);
2086bb873b53SIlya Dryomov 	}
2087bb873b53SIlya Dryomov 
2088bb873b53SIlya Dryomov 	ceph_encode_64(&p, req->r_snapid); /* snapid */
2089bb873b53SIlya Dryomov 	if (req->r_snapc) {
2090bb873b53SIlya Dryomov 		ceph_encode_64(&p, req->r_snapc->seq);
2091bb873b53SIlya Dryomov 		ceph_encode_32(&p, req->r_snapc->num_snaps);
2092bb873b53SIlya Dryomov 		for (i = 0; i < req->r_snapc->num_snaps; i++)
2093bb873b53SIlya Dryomov 			ceph_encode_64(&p, req->r_snapc->snaps[i]);
2094bb873b53SIlya Dryomov 	} else {
2095bb873b53SIlya Dryomov 		ceph_encode_64(&p, 0); /* snap_seq */
2096bb873b53SIlya Dryomov 		ceph_encode_32(&p, 0); /* snaps len */
2097bb873b53SIlya Dryomov 	}
2098bb873b53SIlya Dryomov 
2099bb873b53SIlya Dryomov 	ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
2100986e8989SIlya Dryomov 	BUG_ON(p > end - 8); /* space for features */
2101bb873b53SIlya Dryomov 
21028cb441c0SIlya Dryomov 	msg->hdr.version = cpu_to_le16(8); /* MOSDOp v8 */
21038cb441c0SIlya Dryomov 	/* front_len is finalized in encode_request_finish() */
2104986e8989SIlya Dryomov 	msg->front.iov_len = p - msg->front.iov_base;
2105986e8989SIlya Dryomov 	msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2106bb873b53SIlya Dryomov 	msg->hdr.data_len = cpu_to_le32(data_len);
2107bb873b53SIlya Dryomov 	/*
2108bb873b53SIlya Dryomov 	 * The header "data_off" is a hint to the receiver allowing it
2109bb873b53SIlya Dryomov 	 * to align received data into its buffers such that there's no
2110bb873b53SIlya Dryomov 	 * need to re-copy it before writing it to disk (direct I/O).
2111bb873b53SIlya Dryomov 	 */
2112bb873b53SIlya Dryomov 	msg->hdr.data_off = cpu_to_le16(req->r_data_offset);
2113bb873b53SIlya Dryomov 
21148cb441c0SIlya Dryomov 	dout("%s req %p msg %p oid %s oid_len %d\n", __func__, req, msg,
21158cb441c0SIlya Dryomov 	     req->r_t.target_oid.name, req->r_t.target_oid.name_len);
21168cb441c0SIlya Dryomov }
21178cb441c0SIlya Dryomov 
21188cb441c0SIlya Dryomov static void encode_request_finish(struct ceph_msg *msg)
21198cb441c0SIlya Dryomov {
21208cb441c0SIlya Dryomov 	void *p = msg->front.iov_base;
2121986e8989SIlya Dryomov 	void *const partial_end = p + msg->front.iov_len;
21228cb441c0SIlya Dryomov 	void *const end = p + msg->front_alloc_len;
21238cb441c0SIlya Dryomov 
21248cb441c0SIlya Dryomov 	if (CEPH_HAVE_FEATURE(msg->con->peer_features, RESEND_ON_SPLIT)) {
21258cb441c0SIlya Dryomov 		/* luminous OSD -- encode features and be done */
2126986e8989SIlya Dryomov 		p = partial_end;
21278cb441c0SIlya Dryomov 		ceph_encode_64(&p, msg->con->peer_features);
21288cb441c0SIlya Dryomov 	} else {
21298cb441c0SIlya Dryomov 		struct {
21308cb441c0SIlya Dryomov 			char spgid[CEPH_ENCODING_START_BLK_LEN +
21318cb441c0SIlya Dryomov 				   CEPH_PGID_ENCODING_LEN + 1];
21328cb441c0SIlya Dryomov 			__le32 hash;
21338cb441c0SIlya Dryomov 			__le32 epoch;
21348cb441c0SIlya Dryomov 			__le32 flags;
21358cb441c0SIlya Dryomov 			char reqid[CEPH_ENCODING_START_BLK_LEN +
21368cb441c0SIlya Dryomov 				   sizeof(struct ceph_osd_reqid)];
21378cb441c0SIlya Dryomov 			char trace[sizeof(struct ceph_blkin_trace_info)];
21388cb441c0SIlya Dryomov 			__le32 client_inc;
21398cb441c0SIlya Dryomov 			struct ceph_timespec mtime;
21408cb441c0SIlya Dryomov 		} __packed head;
21418cb441c0SIlya Dryomov 		struct ceph_pg pgid;
21428cb441c0SIlya Dryomov 		void *oloc, *oid, *tail;
21438cb441c0SIlya Dryomov 		int oloc_len, oid_len, tail_len;
21448cb441c0SIlya Dryomov 		int len;
21458cb441c0SIlya Dryomov 
21468cb441c0SIlya Dryomov 		/*
21478cb441c0SIlya Dryomov 		 * Pre-luminous OSD -- reencode v8 into v4 using @head
21488cb441c0SIlya Dryomov 		 * as a temporary buffer.  Encode the raw PG; the rest
21498cb441c0SIlya Dryomov 		 * is just a matter of moving oloc, oid and tail blobs
21508cb441c0SIlya Dryomov 		 * around.
21518cb441c0SIlya Dryomov 		 */
21528cb441c0SIlya Dryomov 		memcpy(&head, p, sizeof(head));
21538cb441c0SIlya Dryomov 		p += sizeof(head);
21548cb441c0SIlya Dryomov 
21558cb441c0SIlya Dryomov 		oloc = p;
21568cb441c0SIlya Dryomov 		p += CEPH_ENCODING_START_BLK_LEN;
21578cb441c0SIlya Dryomov 		pgid.pool = ceph_decode_64(&p);
21588cb441c0SIlya Dryomov 		p += 4 + 4; /* preferred, key len */
21598cb441c0SIlya Dryomov 		len = ceph_decode_32(&p);
21608cb441c0SIlya Dryomov 		p += len;   /* nspace */
21618cb441c0SIlya Dryomov 		oloc_len = p - oloc;
21628cb441c0SIlya Dryomov 
21638cb441c0SIlya Dryomov 		oid = p;
21648cb441c0SIlya Dryomov 		len = ceph_decode_32(&p);
21658cb441c0SIlya Dryomov 		p += len;
21668cb441c0SIlya Dryomov 		oid_len = p - oid;
21678cb441c0SIlya Dryomov 
21688cb441c0SIlya Dryomov 		tail = p;
2169986e8989SIlya Dryomov 		tail_len = partial_end - p;
21708cb441c0SIlya Dryomov 
21718cb441c0SIlya Dryomov 		p = msg->front.iov_base;
21728cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.client_inc, sizeof(head.client_inc));
21738cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.epoch, sizeof(head.epoch));
21748cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.flags, sizeof(head.flags));
21758cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.mtime, sizeof(head.mtime));
21768cb441c0SIlya Dryomov 
21778cb441c0SIlya Dryomov 		/* reassert_version */
21788cb441c0SIlya Dryomov 		memset(p, 0, sizeof(struct ceph_eversion));
21798cb441c0SIlya Dryomov 		p += sizeof(struct ceph_eversion);
21808cb441c0SIlya Dryomov 
21818cb441c0SIlya Dryomov 		BUG_ON(p >= oloc);
21828cb441c0SIlya Dryomov 		memmove(p, oloc, oloc_len);
21838cb441c0SIlya Dryomov 		p += oloc_len;
21848cb441c0SIlya Dryomov 
21858cb441c0SIlya Dryomov 		pgid.seed = le32_to_cpu(head.hash);
21868cb441c0SIlya Dryomov 		encode_pgid(&p, &pgid); /* raw pg */
21878cb441c0SIlya Dryomov 
21888cb441c0SIlya Dryomov 		BUG_ON(p >= oid);
21898cb441c0SIlya Dryomov 		memmove(p, oid, oid_len);
21908cb441c0SIlya Dryomov 		p += oid_len;
21918cb441c0SIlya Dryomov 
21928cb441c0SIlya Dryomov 		/* tail -- ops, snapid, snapc, retry_attempt */
21938cb441c0SIlya Dryomov 		BUG_ON(p >= tail);
21948cb441c0SIlya Dryomov 		memmove(p, tail, tail_len);
21958cb441c0SIlya Dryomov 		p += tail_len;
21968cb441c0SIlya Dryomov 
21978cb441c0SIlya Dryomov 		msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
21988cb441c0SIlya Dryomov 	}
21998cb441c0SIlya Dryomov 
22008cb441c0SIlya Dryomov 	BUG_ON(p > end);
22018cb441c0SIlya Dryomov 	msg->front.iov_len = p - msg->front.iov_base;
22028cb441c0SIlya Dryomov 	msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
22038cb441c0SIlya Dryomov 
22048cb441c0SIlya Dryomov 	dout("%s msg %p tid %llu %u+%u+%u v%d\n", __func__, msg,
22058cb441c0SIlya Dryomov 	     le64_to_cpu(msg->hdr.tid), le32_to_cpu(msg->hdr.front_len),
22068cb441c0SIlya Dryomov 	     le32_to_cpu(msg->hdr.middle_len), le32_to_cpu(msg->hdr.data_len),
22078cb441c0SIlya Dryomov 	     le16_to_cpu(msg->hdr.version));
2208bb873b53SIlya Dryomov }
2209bb873b53SIlya Dryomov 
2210bb873b53SIlya Dryomov /*
2211bb873b53SIlya Dryomov  * @req has to be assigned a tid and registered.
2212bb873b53SIlya Dryomov  */
2213bb873b53SIlya Dryomov static void send_request(struct ceph_osd_request *req)
2214bb873b53SIlya Dryomov {
2215bb873b53SIlya Dryomov 	struct ceph_osd *osd = req->r_osd;
2216bb873b53SIlya Dryomov 
22175aea3dcdSIlya Dryomov 	verify_osd_locked(osd);
2218bb873b53SIlya Dryomov 	WARN_ON(osd->o_osd != req->r_t.osd);
2219bb873b53SIlya Dryomov 
2220a02a946dSIlya Dryomov 	/* backoff? */
2221a02a946dSIlya Dryomov 	if (should_plug_request(req))
2222a02a946dSIlya Dryomov 		return;
2223a02a946dSIlya Dryomov 
22245aea3dcdSIlya Dryomov 	/*
22255aea3dcdSIlya Dryomov 	 * We may have a previously queued request message hanging
22265aea3dcdSIlya Dryomov 	 * around.  Cancel it to avoid corrupting the msgr.
22275aea3dcdSIlya Dryomov 	 */
22285aea3dcdSIlya Dryomov 	if (req->r_sent)
22295aea3dcdSIlya Dryomov 		ceph_msg_revoke(req->r_request);
22305aea3dcdSIlya Dryomov 
2231bb873b53SIlya Dryomov 	req->r_flags |= CEPH_OSD_FLAG_KNOWN_REDIR;
2232bb873b53SIlya Dryomov 	if (req->r_attempts)
2233bb873b53SIlya Dryomov 		req->r_flags |= CEPH_OSD_FLAG_RETRY;
2234bb873b53SIlya Dryomov 	else
2235bb873b53SIlya Dryomov 		WARN_ON(req->r_flags & CEPH_OSD_FLAG_RETRY);
2236bb873b53SIlya Dryomov 
22378cb441c0SIlya Dryomov 	encode_request_partial(req, req->r_request);
2238bb873b53SIlya Dryomov 
223904c7d789SIlya 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",
2240bb873b53SIlya Dryomov 	     __func__, req, req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed,
2241dc98ff72SIlya Dryomov 	     req->r_t.spgid.pgid.pool, req->r_t.spgid.pgid.seed,
224204c7d789SIlya Dryomov 	     req->r_t.spgid.shard, osd->o_osd, req->r_t.epoch, req->r_flags,
224304c7d789SIlya Dryomov 	     req->r_attempts);
2244bb873b53SIlya Dryomov 
2245bb873b53SIlya Dryomov 	req->r_t.paused = false;
22463d14c5d2SYehuda Sadeh 	req->r_stamp = jiffies;
2247bb873b53SIlya Dryomov 	req->r_attempts++;
22483d14c5d2SYehuda Sadeh 
2249bb873b53SIlya Dryomov 	req->r_sent = osd->o_incarnation;
2250bb873b53SIlya Dryomov 	req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
2251bb873b53SIlya Dryomov 	ceph_con_send(&osd->o_con, ceph_msg_get(req->r_request));
22523d14c5d2SYehuda Sadeh }
22533d14c5d2SYehuda Sadeh 
225442c1b124SIlya Dryomov static void maybe_request_map(struct ceph_osd_client *osdc)
225542c1b124SIlya Dryomov {
225642c1b124SIlya Dryomov 	bool continuous = false;
225742c1b124SIlya Dryomov 
22585aea3dcdSIlya Dryomov 	verify_osdc_locked(osdc);
225942c1b124SIlya Dryomov 	WARN_ON(!osdc->osdmap->epoch);
226042c1b124SIlya Dryomov 
2261b7ec35b3SIlya Dryomov 	if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2262b7ec35b3SIlya Dryomov 	    ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD) ||
2263b7ec35b3SIlya Dryomov 	    ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
226442c1b124SIlya Dryomov 		dout("%s osdc %p continuous\n", __func__, osdc);
226542c1b124SIlya Dryomov 		continuous = true;
226642c1b124SIlya Dryomov 	} else {
226742c1b124SIlya Dryomov 		dout("%s osdc %p onetime\n", __func__, osdc);
226842c1b124SIlya Dryomov 	}
226942c1b124SIlya Dryomov 
227042c1b124SIlya Dryomov 	if (ceph_monc_want_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
227142c1b124SIlya Dryomov 			       osdc->osdmap->epoch + 1, continuous))
227242c1b124SIlya Dryomov 		ceph_monc_renew_subs(&osdc->client->monc);
227342c1b124SIlya Dryomov }
227442c1b124SIlya Dryomov 
2275a1f4020aSJeff Layton static void complete_request(struct ceph_osd_request *req, int err);
22764609245eSIlya Dryomov static void send_map_check(struct ceph_osd_request *req);
22774609245eSIlya Dryomov 
22785aea3dcdSIlya Dryomov static void __submit_request(struct ceph_osd_request *req, bool wrlocked)
22790bbfdfe8SIlya Dryomov {
22805aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
22815aea3dcdSIlya Dryomov 	struct ceph_osd *osd;
22824609245eSIlya Dryomov 	enum calc_target_result ct_res;
228366850df5SIlya Dryomov 	int err = 0;
22845aea3dcdSIlya Dryomov 	bool need_send = false;
22855aea3dcdSIlya Dryomov 	bool promoted = false;
22860bbfdfe8SIlya Dryomov 
2287b18b9550SIlya Dryomov 	WARN_ON(req->r_tid);
22885aea3dcdSIlya Dryomov 	dout("%s req %p wrlocked %d\n", __func__, req, wrlocked);
22895aea3dcdSIlya Dryomov 
22905aea3dcdSIlya Dryomov again:
22918edf84baSIlya Dryomov 	ct_res = calc_target(osdc, &req->r_t, false);
22924609245eSIlya Dryomov 	if (ct_res == CALC_TARGET_POOL_DNE && !wrlocked)
22934609245eSIlya Dryomov 		goto promote;
22944609245eSIlya Dryomov 
22955aea3dcdSIlya Dryomov 	osd = lookup_create_osd(osdc, req->r_t.osd, wrlocked);
22965aea3dcdSIlya Dryomov 	if (IS_ERR(osd)) {
22975aea3dcdSIlya Dryomov 		WARN_ON(PTR_ERR(osd) != -EAGAIN || wrlocked);
22985aea3dcdSIlya Dryomov 		goto promote;
22995aea3dcdSIlya Dryomov 	}
23005aea3dcdSIlya Dryomov 
230166850df5SIlya Dryomov 	if (osdc->abort_err) {
230266850df5SIlya Dryomov 		dout("req %p abort_err %d\n", req, osdc->abort_err);
230366850df5SIlya Dryomov 		err = osdc->abort_err;
230466850df5SIlya Dryomov 	} else if (osdc->osdmap->epoch < osdc->epoch_barrier) {
230558eb7932SJeff Layton 		dout("req %p epoch %u barrier %u\n", req, osdc->osdmap->epoch,
230658eb7932SJeff Layton 		     osdc->epoch_barrier);
230758eb7932SJeff Layton 		req->r_t.paused = true;
230858eb7932SJeff Layton 		maybe_request_map(osdc);
230958eb7932SJeff Layton 	} else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2310b7ec35b3SIlya Dryomov 		   ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
23115aea3dcdSIlya Dryomov 		dout("req %p pausewr\n", req);
23125aea3dcdSIlya Dryomov 		req->r_t.paused = true;
23135aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
23145aea3dcdSIlya Dryomov 	} else if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
2315b7ec35b3SIlya Dryomov 		   ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
23165aea3dcdSIlya Dryomov 		dout("req %p pauserd\n", req);
23175aea3dcdSIlya Dryomov 		req->r_t.paused = true;
23185aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
23195aea3dcdSIlya Dryomov 	} else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
23205aea3dcdSIlya Dryomov 		   !(req->r_flags & (CEPH_OSD_FLAG_FULL_TRY |
23215aea3dcdSIlya Dryomov 				     CEPH_OSD_FLAG_FULL_FORCE)) &&
2322b7ec35b3SIlya Dryomov 		   (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
23235aea3dcdSIlya Dryomov 		    pool_full(osdc, req->r_t.base_oloc.pool))) {
23245aea3dcdSIlya Dryomov 		dout("req %p full/pool_full\n", req);
232502b2f549SDongsheng Yang 		if (ceph_test_opt(osdc->client, ABORT_ON_FULL)) {
232629e87820SIlya Dryomov 			err = -ENOSPC;
232729e87820SIlya Dryomov 		} else {
23285aea3dcdSIlya Dryomov 			pr_warn_ratelimited("FULL or reached pool quota\n");
23295aea3dcdSIlya Dryomov 			req->r_t.paused = true;
23305aea3dcdSIlya Dryomov 			maybe_request_map(osdc);
233129e87820SIlya Dryomov 		}
23325aea3dcdSIlya Dryomov 	} else if (!osd_homeless(osd)) {
23335aea3dcdSIlya Dryomov 		need_send = true;
23340bbfdfe8SIlya Dryomov 	} else {
23355aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
23360bbfdfe8SIlya Dryomov 	}
23370bbfdfe8SIlya Dryomov 
23385aea3dcdSIlya Dryomov 	mutex_lock(&osd->lock);
23395aea3dcdSIlya Dryomov 	/*
23405aea3dcdSIlya Dryomov 	 * Assign the tid atomically with send_request() to protect
23415aea3dcdSIlya Dryomov 	 * multiple writes to the same object from racing with each
23425aea3dcdSIlya Dryomov 	 * other, resulting in out of order ops on the OSDs.
23435aea3dcdSIlya Dryomov 	 */
23445aea3dcdSIlya Dryomov 	req->r_tid = atomic64_inc_return(&osdc->last_tid);
23455aea3dcdSIlya Dryomov 	link_request(osd, req);
23465aea3dcdSIlya Dryomov 	if (need_send)
23475aea3dcdSIlya Dryomov 		send_request(req);
234866850df5SIlya Dryomov 	else if (err)
234966850df5SIlya Dryomov 		complete_request(req, err);
23505aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
23515aea3dcdSIlya Dryomov 
23526001567cSIlya Dryomov 	if (!err && ct_res == CALC_TARGET_POOL_DNE)
23534609245eSIlya Dryomov 		send_map_check(req);
23544609245eSIlya Dryomov 
23555aea3dcdSIlya Dryomov 	if (promoted)
23565aea3dcdSIlya Dryomov 		downgrade_write(&osdc->lock);
23575aea3dcdSIlya Dryomov 	return;
23585aea3dcdSIlya Dryomov 
23595aea3dcdSIlya Dryomov promote:
23605aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
23615aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
23625aea3dcdSIlya Dryomov 	wrlocked = true;
23635aea3dcdSIlya Dryomov 	promoted = true;
23645aea3dcdSIlya Dryomov 	goto again;
23650bbfdfe8SIlya Dryomov }
23660bbfdfe8SIlya Dryomov 
23675aea3dcdSIlya Dryomov static void account_request(struct ceph_osd_request *req)
23685aea3dcdSIlya Dryomov {
236954ea0046SIlya Dryomov 	WARN_ON(req->r_flags & (CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK));
2370b18b9550SIlya Dryomov 	WARN_ON(!(req->r_flags & (CEPH_OSD_FLAG_READ | CEPH_OSD_FLAG_WRITE)));
23715aea3dcdSIlya Dryomov 
2372b18b9550SIlya Dryomov 	req->r_flags |= CEPH_OSD_FLAG_ONDISK;
23735aea3dcdSIlya Dryomov 	atomic_inc(&req->r_osdc->num_requests);
23747cc5e38fSIlya Dryomov 
23757cc5e38fSIlya Dryomov 	req->r_start_stamp = jiffies;
23765aea3dcdSIlya Dryomov }
23775aea3dcdSIlya Dryomov 
23785aea3dcdSIlya Dryomov static void submit_request(struct ceph_osd_request *req, bool wrlocked)
23795aea3dcdSIlya Dryomov {
23805aea3dcdSIlya Dryomov 	ceph_osdc_get_request(req);
23815aea3dcdSIlya Dryomov 	account_request(req);
23825aea3dcdSIlya Dryomov 	__submit_request(req, wrlocked);
23835aea3dcdSIlya Dryomov }
23845aea3dcdSIlya Dryomov 
238545ee2c1dSIlya Dryomov static void finish_request(struct ceph_osd_request *req)
23865aea3dcdSIlya Dryomov {
23875aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
23885aea3dcdSIlya Dryomov 
23894609245eSIlya Dryomov 	WARN_ON(lookup_request_mc(&osdc->map_checks, req->r_tid));
239004c7d789SIlya Dryomov 	dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
239104c7d789SIlya Dryomov 
239204c7d789SIlya Dryomov 	if (req->r_osd)
239304c7d789SIlya Dryomov 		unlink_request(req->r_osd, req);
23945aea3dcdSIlya Dryomov 	atomic_dec(&osdc->num_requests);
23955aea3dcdSIlya Dryomov 
23965aea3dcdSIlya Dryomov 	/*
23975aea3dcdSIlya Dryomov 	 * If an OSD has failed or returned and a request has been sent
23985aea3dcdSIlya Dryomov 	 * twice, it's possible to get a reply and end up here while the
23995aea3dcdSIlya Dryomov 	 * request message is queued for delivery.  We will ignore the
24005aea3dcdSIlya Dryomov 	 * reply, so not a big deal, but better to try and catch it.
24015aea3dcdSIlya Dryomov 	 */
24025aea3dcdSIlya Dryomov 	ceph_msg_revoke(req->r_request);
24035aea3dcdSIlya Dryomov 	ceph_msg_revoke_incoming(req->r_reply);
24045aea3dcdSIlya Dryomov }
24055aea3dcdSIlya Dryomov 
2406fe5da05eSIlya Dryomov static void __complete_request(struct ceph_osd_request *req)
2407fe5da05eSIlya Dryomov {
2408d75f773cSSakari Ailus 	dout("%s req %p tid %llu cb %ps result %d\n", __func__, req,
2409b18b9550SIlya Dryomov 	     req->r_tid, req->r_callback, req->r_result);
241026df726bSIlya Dryomov 
241126df726bSIlya Dryomov 	if (req->r_callback)
2412fe5da05eSIlya Dryomov 		req->r_callback(req);
241326df726bSIlya Dryomov 	complete_all(&req->r_completion);
241426df726bSIlya Dryomov 	ceph_osdc_put_request(req);
2415b18b9550SIlya Dryomov }
2416fe5da05eSIlya Dryomov 
241788bc1922SIlya Dryomov static void complete_request_workfn(struct work_struct *work)
241888bc1922SIlya Dryomov {
241988bc1922SIlya Dryomov 	struct ceph_osd_request *req =
242088bc1922SIlya Dryomov 	    container_of(work, struct ceph_osd_request, r_complete_work);
242188bc1922SIlya Dryomov 
242288bc1922SIlya Dryomov 	__complete_request(req);
24230bbfdfe8SIlya Dryomov }
24240bbfdfe8SIlya Dryomov 
24254609245eSIlya Dryomov /*
2426b18b9550SIlya Dryomov  * This is open-coded in handle_reply().
24274609245eSIlya Dryomov  */
24284609245eSIlya Dryomov static void complete_request(struct ceph_osd_request *req, int err)
24294609245eSIlya Dryomov {
24304609245eSIlya Dryomov 	dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
24314609245eSIlya Dryomov 
24324609245eSIlya Dryomov 	req->r_result = err;
243345ee2c1dSIlya Dryomov 	finish_request(req);
243488bc1922SIlya Dryomov 
243588bc1922SIlya Dryomov 	INIT_WORK(&req->r_complete_work, complete_request_workfn);
243688bc1922SIlya Dryomov 	queue_work(req->r_osdc->completion_wq, &req->r_complete_work);
24374609245eSIlya Dryomov }
24384609245eSIlya Dryomov 
24394609245eSIlya Dryomov static void cancel_map_check(struct ceph_osd_request *req)
24404609245eSIlya Dryomov {
24414609245eSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
24424609245eSIlya Dryomov 	struct ceph_osd_request *lookup_req;
24434609245eSIlya Dryomov 
24444609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
24454609245eSIlya Dryomov 
24464609245eSIlya Dryomov 	lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
24474609245eSIlya Dryomov 	if (!lookup_req)
24484609245eSIlya Dryomov 		return;
24494609245eSIlya Dryomov 
24504609245eSIlya Dryomov 	WARN_ON(lookup_req != req);
24514609245eSIlya Dryomov 	erase_request_mc(&osdc->map_checks, req);
24524609245eSIlya Dryomov 	ceph_osdc_put_request(req);
24534609245eSIlya Dryomov }
24544609245eSIlya Dryomov 
24555aea3dcdSIlya Dryomov static void cancel_request(struct ceph_osd_request *req)
24565aea3dcdSIlya Dryomov {
24575aea3dcdSIlya Dryomov 	dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
24585aea3dcdSIlya Dryomov 
24594609245eSIlya Dryomov 	cancel_map_check(req);
246045ee2c1dSIlya Dryomov 	finish_request(req);
2461b18b9550SIlya Dryomov 	complete_all(&req->r_completion);
2462c297eb42SIlya Dryomov 	ceph_osdc_put_request(req);
24635aea3dcdSIlya Dryomov }
24645aea3dcdSIlya Dryomov 
24657cc5e38fSIlya Dryomov static void abort_request(struct ceph_osd_request *req, int err)
24667cc5e38fSIlya Dryomov {
24677cc5e38fSIlya Dryomov 	dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
24687cc5e38fSIlya Dryomov 
24697cc5e38fSIlya Dryomov 	cancel_map_check(req);
24707cc5e38fSIlya Dryomov 	complete_request(req, err);
24717cc5e38fSIlya Dryomov }
24727cc5e38fSIlya Dryomov 
247366850df5SIlya Dryomov static int abort_fn(struct ceph_osd_request *req, void *arg)
247466850df5SIlya Dryomov {
247566850df5SIlya Dryomov 	int err = *(int *)arg;
247666850df5SIlya Dryomov 
247766850df5SIlya Dryomov 	abort_request(req, err);
247866850df5SIlya Dryomov 	return 0; /* continue iteration */
247966850df5SIlya Dryomov }
248066850df5SIlya Dryomov 
248166850df5SIlya Dryomov /*
248266850df5SIlya Dryomov  * Abort all in-flight requests with @err and arrange for all future
248366850df5SIlya Dryomov  * requests to be failed immediately.
248466850df5SIlya Dryomov  */
248566850df5SIlya Dryomov void ceph_osdc_abort_requests(struct ceph_osd_client *osdc, int err)
248666850df5SIlya Dryomov {
248766850df5SIlya Dryomov 	dout("%s osdc %p err %d\n", __func__, osdc, err);
248866850df5SIlya Dryomov 	down_write(&osdc->lock);
248966850df5SIlya Dryomov 	for_each_request(osdc, abort_fn, &err);
249066850df5SIlya Dryomov 	osdc->abort_err = err;
249166850df5SIlya Dryomov 	up_write(&osdc->lock);
249266850df5SIlya Dryomov }
249366850df5SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_abort_requests);
249466850df5SIlya Dryomov 
24952cef0ba8SYan, Zheng void ceph_osdc_clear_abort_err(struct ceph_osd_client *osdc)
24962cef0ba8SYan, Zheng {
24972cef0ba8SYan, Zheng 	down_write(&osdc->lock);
24982cef0ba8SYan, Zheng 	osdc->abort_err = 0;
24992cef0ba8SYan, Zheng 	up_write(&osdc->lock);
25002cef0ba8SYan, Zheng }
25012cef0ba8SYan, Zheng EXPORT_SYMBOL(ceph_osdc_clear_abort_err);
25022cef0ba8SYan, Zheng 
250358eb7932SJeff Layton static void update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
250458eb7932SJeff Layton {
250558eb7932SJeff Layton 	if (likely(eb > osdc->epoch_barrier)) {
250658eb7932SJeff Layton 		dout("updating epoch_barrier from %u to %u\n",
250758eb7932SJeff Layton 				osdc->epoch_barrier, eb);
250858eb7932SJeff Layton 		osdc->epoch_barrier = eb;
250958eb7932SJeff Layton 		/* Request map if we're not to the barrier yet */
251058eb7932SJeff Layton 		if (eb > osdc->osdmap->epoch)
251158eb7932SJeff Layton 			maybe_request_map(osdc);
251258eb7932SJeff Layton 	}
251358eb7932SJeff Layton }
251458eb7932SJeff Layton 
251558eb7932SJeff Layton void ceph_osdc_update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
251658eb7932SJeff Layton {
251758eb7932SJeff Layton 	down_read(&osdc->lock);
251858eb7932SJeff Layton 	if (unlikely(eb > osdc->epoch_barrier)) {
251958eb7932SJeff Layton 		up_read(&osdc->lock);
252058eb7932SJeff Layton 		down_write(&osdc->lock);
252158eb7932SJeff Layton 		update_epoch_barrier(osdc, eb);
252258eb7932SJeff Layton 		up_write(&osdc->lock);
252358eb7932SJeff Layton 	} else {
252458eb7932SJeff Layton 		up_read(&osdc->lock);
252558eb7932SJeff Layton 	}
252658eb7932SJeff Layton }
252758eb7932SJeff Layton EXPORT_SYMBOL(ceph_osdc_update_epoch_barrier);
252858eb7932SJeff Layton 
2529fc36d0a4SJeff Layton /*
25304eea0fefSIlya Dryomov  * We can end up releasing caps as a result of abort_request().
25314eea0fefSIlya Dryomov  * In that case, we probably want to ensure that the cap release message
25324eea0fefSIlya Dryomov  * has an updated epoch barrier in it, so set the epoch barrier prior to
25334eea0fefSIlya Dryomov  * aborting the first request.
25344eea0fefSIlya Dryomov  */
25354eea0fefSIlya Dryomov static int abort_on_full_fn(struct ceph_osd_request *req, void *arg)
25364eea0fefSIlya Dryomov {
25374eea0fefSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
25384eea0fefSIlya Dryomov 	bool *victims = arg;
25394eea0fefSIlya Dryomov 
2540c843d13cSIlya Dryomov 	if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
25414eea0fefSIlya Dryomov 	    (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2542690f951dSIlya Dryomov 	     pool_full(osdc, req->r_t.base_oloc.pool))) {
25434eea0fefSIlya Dryomov 		if (!*victims) {
25444eea0fefSIlya Dryomov 			update_epoch_barrier(osdc, osdc->osdmap->epoch);
25454eea0fefSIlya Dryomov 			*victims = true;
25464eea0fefSIlya Dryomov 		}
25474eea0fefSIlya Dryomov 		abort_request(req, -ENOSPC);
25484eea0fefSIlya Dryomov 	}
25494eea0fefSIlya Dryomov 
25504eea0fefSIlya Dryomov 	return 0; /* continue iteration */
25514eea0fefSIlya Dryomov }
25524eea0fefSIlya Dryomov 
25534eea0fefSIlya Dryomov /*
2554fc36d0a4SJeff Layton  * Drop all pending requests that are stalled waiting on a full condition to
255558eb7932SJeff Layton  * clear, and complete them with ENOSPC as the return code. Set the
255658eb7932SJeff Layton  * osdc->epoch_barrier to the latest map epoch that we've seen if any were
255758eb7932SJeff Layton  * cancelled.
2558fc36d0a4SJeff Layton  */
2559fc36d0a4SJeff Layton static void ceph_osdc_abort_on_full(struct ceph_osd_client *osdc)
2560fc36d0a4SJeff Layton {
256158eb7932SJeff Layton 	bool victims = false;
2562fc36d0a4SJeff Layton 
256302b2f549SDongsheng Yang 	if (ceph_test_opt(osdc->client, ABORT_ON_FULL) &&
2564c843d13cSIlya Dryomov 	    (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) || have_pool_full(osdc)))
25654eea0fefSIlya Dryomov 		for_each_request(osdc, abort_on_full_fn, &victims);
2566fc36d0a4SJeff Layton }
2567fc36d0a4SJeff Layton 
25684609245eSIlya Dryomov static void check_pool_dne(struct ceph_osd_request *req)
25694609245eSIlya Dryomov {
25704609245eSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
25714609245eSIlya Dryomov 	struct ceph_osdmap *map = osdc->osdmap;
25724609245eSIlya Dryomov 
25734609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
25744609245eSIlya Dryomov 	WARN_ON(!map->epoch);
25754609245eSIlya Dryomov 
25764609245eSIlya Dryomov 	if (req->r_attempts) {
25774609245eSIlya Dryomov 		/*
25784609245eSIlya Dryomov 		 * We sent a request earlier, which means that
25794609245eSIlya Dryomov 		 * previously the pool existed, and now it does not
25804609245eSIlya Dryomov 		 * (i.e., it was deleted).
25814609245eSIlya Dryomov 		 */
25824609245eSIlya Dryomov 		req->r_map_dne_bound = map->epoch;
25834609245eSIlya Dryomov 		dout("%s req %p tid %llu pool disappeared\n", __func__, req,
25844609245eSIlya Dryomov 		     req->r_tid);
25854609245eSIlya Dryomov 	} else {
25864609245eSIlya Dryomov 		dout("%s req %p tid %llu map_dne_bound %u have %u\n", __func__,
25874609245eSIlya Dryomov 		     req, req->r_tid, req->r_map_dne_bound, map->epoch);
25884609245eSIlya Dryomov 	}
25894609245eSIlya Dryomov 
25904609245eSIlya Dryomov 	if (req->r_map_dne_bound) {
25914609245eSIlya Dryomov 		if (map->epoch >= req->r_map_dne_bound) {
25924609245eSIlya Dryomov 			/* we had a new enough map */
25934609245eSIlya Dryomov 			pr_info_ratelimited("tid %llu pool does not exist\n",
25944609245eSIlya Dryomov 					    req->r_tid);
25954609245eSIlya Dryomov 			complete_request(req, -ENOENT);
25964609245eSIlya Dryomov 		}
25974609245eSIlya Dryomov 	} else {
25984609245eSIlya Dryomov 		send_map_check(req);
25994609245eSIlya Dryomov 	}
26004609245eSIlya Dryomov }
26014609245eSIlya Dryomov 
26024609245eSIlya Dryomov static void map_check_cb(struct ceph_mon_generic_request *greq)
26034609245eSIlya Dryomov {
26044609245eSIlya Dryomov 	struct ceph_osd_client *osdc = &greq->monc->client->osdc;
26054609245eSIlya Dryomov 	struct ceph_osd_request *req;
26064609245eSIlya Dryomov 	u64 tid = greq->private_data;
26074609245eSIlya Dryomov 
26084609245eSIlya Dryomov 	WARN_ON(greq->result || !greq->u.newest);
26094609245eSIlya Dryomov 
26104609245eSIlya Dryomov 	down_write(&osdc->lock);
26114609245eSIlya Dryomov 	req = lookup_request_mc(&osdc->map_checks, tid);
26124609245eSIlya Dryomov 	if (!req) {
26134609245eSIlya Dryomov 		dout("%s tid %llu dne\n", __func__, tid);
26144609245eSIlya Dryomov 		goto out_unlock;
26154609245eSIlya Dryomov 	}
26164609245eSIlya Dryomov 
26174609245eSIlya Dryomov 	dout("%s req %p tid %llu map_dne_bound %u newest %llu\n", __func__,
26184609245eSIlya Dryomov 	     req, req->r_tid, req->r_map_dne_bound, greq->u.newest);
26194609245eSIlya Dryomov 	if (!req->r_map_dne_bound)
26204609245eSIlya Dryomov 		req->r_map_dne_bound = greq->u.newest;
26214609245eSIlya Dryomov 	erase_request_mc(&osdc->map_checks, req);
26224609245eSIlya Dryomov 	check_pool_dne(req);
26234609245eSIlya Dryomov 
26244609245eSIlya Dryomov 	ceph_osdc_put_request(req);
26254609245eSIlya Dryomov out_unlock:
26264609245eSIlya Dryomov 	up_write(&osdc->lock);
26274609245eSIlya Dryomov }
26284609245eSIlya Dryomov 
26294609245eSIlya Dryomov static void send_map_check(struct ceph_osd_request *req)
26304609245eSIlya Dryomov {
26314609245eSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
26324609245eSIlya Dryomov 	struct ceph_osd_request *lookup_req;
26334609245eSIlya Dryomov 	int ret;
26344609245eSIlya Dryomov 
26354609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
26364609245eSIlya Dryomov 
26374609245eSIlya Dryomov 	lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
26384609245eSIlya Dryomov 	if (lookup_req) {
26394609245eSIlya Dryomov 		WARN_ON(lookup_req != req);
26404609245eSIlya Dryomov 		return;
26414609245eSIlya Dryomov 	}
26424609245eSIlya Dryomov 
26434609245eSIlya Dryomov 	ceph_osdc_get_request(req);
26444609245eSIlya Dryomov 	insert_request_mc(&osdc->map_checks, req);
26454609245eSIlya Dryomov 	ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
26464609245eSIlya Dryomov 					  map_check_cb, req->r_tid);
26474609245eSIlya Dryomov 	WARN_ON(ret);
26484609245eSIlya Dryomov }
26494609245eSIlya Dryomov 
26500bbfdfe8SIlya Dryomov /*
2651922dab61SIlya Dryomov  * lingering requests, watch/notify v2 infrastructure
2652922dab61SIlya Dryomov  */
2653922dab61SIlya Dryomov static void linger_release(struct kref *kref)
2654922dab61SIlya Dryomov {
2655922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq =
2656922dab61SIlya Dryomov 	    container_of(kref, struct ceph_osd_linger_request, kref);
2657922dab61SIlya Dryomov 
2658922dab61SIlya Dryomov 	dout("%s lreq %p reg_req %p ping_req %p\n", __func__, lreq,
2659922dab61SIlya Dryomov 	     lreq->reg_req, lreq->ping_req);
2660922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&lreq->node));
2661922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&lreq->osdc_node));
26624609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&lreq->mc_node));
2663922dab61SIlya Dryomov 	WARN_ON(!list_empty(&lreq->scan_item));
2664b07d3c4bSIlya Dryomov 	WARN_ON(!list_empty(&lreq->pending_lworks));
2665922dab61SIlya Dryomov 	WARN_ON(lreq->osd);
2666922dab61SIlya Dryomov 
2667922dab61SIlya Dryomov 	if (lreq->reg_req)
2668922dab61SIlya Dryomov 		ceph_osdc_put_request(lreq->reg_req);
2669922dab61SIlya Dryomov 	if (lreq->ping_req)
2670922dab61SIlya Dryomov 		ceph_osdc_put_request(lreq->ping_req);
2671922dab61SIlya Dryomov 	target_destroy(&lreq->t);
2672922dab61SIlya Dryomov 	kfree(lreq);
2673922dab61SIlya Dryomov }
2674922dab61SIlya Dryomov 
2675922dab61SIlya Dryomov static void linger_put(struct ceph_osd_linger_request *lreq)
2676922dab61SIlya Dryomov {
2677922dab61SIlya Dryomov 	if (lreq)
2678922dab61SIlya Dryomov 		kref_put(&lreq->kref, linger_release);
2679922dab61SIlya Dryomov }
2680922dab61SIlya Dryomov 
2681922dab61SIlya Dryomov static struct ceph_osd_linger_request *
2682922dab61SIlya Dryomov linger_get(struct ceph_osd_linger_request *lreq)
2683922dab61SIlya Dryomov {
2684922dab61SIlya Dryomov 	kref_get(&lreq->kref);
2685922dab61SIlya Dryomov 	return lreq;
2686922dab61SIlya Dryomov }
2687922dab61SIlya Dryomov 
2688922dab61SIlya Dryomov static struct ceph_osd_linger_request *
2689922dab61SIlya Dryomov linger_alloc(struct ceph_osd_client *osdc)
2690922dab61SIlya Dryomov {
2691922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
2692922dab61SIlya Dryomov 
2693922dab61SIlya Dryomov 	lreq = kzalloc(sizeof(*lreq), GFP_NOIO);
2694922dab61SIlya Dryomov 	if (!lreq)
2695922dab61SIlya Dryomov 		return NULL;
2696922dab61SIlya Dryomov 
2697922dab61SIlya Dryomov 	kref_init(&lreq->kref);
2698922dab61SIlya Dryomov 	mutex_init(&lreq->lock);
2699922dab61SIlya Dryomov 	RB_CLEAR_NODE(&lreq->node);
2700922dab61SIlya Dryomov 	RB_CLEAR_NODE(&lreq->osdc_node);
27014609245eSIlya Dryomov 	RB_CLEAR_NODE(&lreq->mc_node);
2702922dab61SIlya Dryomov 	INIT_LIST_HEAD(&lreq->scan_item);
2703b07d3c4bSIlya Dryomov 	INIT_LIST_HEAD(&lreq->pending_lworks);
2704922dab61SIlya Dryomov 	init_completion(&lreq->reg_commit_wait);
270519079203SIlya Dryomov 	init_completion(&lreq->notify_finish_wait);
2706922dab61SIlya Dryomov 
2707922dab61SIlya Dryomov 	lreq->osdc = osdc;
2708922dab61SIlya Dryomov 	target_init(&lreq->t);
2709922dab61SIlya Dryomov 
2710922dab61SIlya Dryomov 	dout("%s lreq %p\n", __func__, lreq);
2711922dab61SIlya Dryomov 	return lreq;
2712922dab61SIlya Dryomov }
2713922dab61SIlya Dryomov 
2714922dab61SIlya Dryomov DEFINE_RB_INSDEL_FUNCS(linger, struct ceph_osd_linger_request, linger_id, node)
2715922dab61SIlya Dryomov DEFINE_RB_FUNCS(linger_osdc, struct ceph_osd_linger_request, linger_id, osdc_node)
27164609245eSIlya Dryomov DEFINE_RB_FUNCS(linger_mc, struct ceph_osd_linger_request, linger_id, mc_node)
2717922dab61SIlya Dryomov 
2718922dab61SIlya Dryomov /*
2719922dab61SIlya Dryomov  * Create linger request <-> OSD session relation.
2720922dab61SIlya Dryomov  *
2721922dab61SIlya Dryomov  * @lreq has to be registered, @osd may be homeless.
2722922dab61SIlya Dryomov  */
2723922dab61SIlya Dryomov static void link_linger(struct ceph_osd *osd,
2724922dab61SIlya Dryomov 			struct ceph_osd_linger_request *lreq)
2725922dab61SIlya Dryomov {
2726922dab61SIlya Dryomov 	verify_osd_locked(osd);
2727922dab61SIlya Dryomov 	WARN_ON(!lreq->linger_id || lreq->osd);
2728922dab61SIlya Dryomov 	dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2729922dab61SIlya Dryomov 	     osd->o_osd, lreq, lreq->linger_id);
2730922dab61SIlya Dryomov 
2731922dab61SIlya Dryomov 	if (!osd_homeless(osd))
2732922dab61SIlya Dryomov 		__remove_osd_from_lru(osd);
2733922dab61SIlya Dryomov 	else
2734922dab61SIlya Dryomov 		atomic_inc(&osd->o_osdc->num_homeless);
2735922dab61SIlya Dryomov 
2736922dab61SIlya Dryomov 	get_osd(osd);
2737922dab61SIlya Dryomov 	insert_linger(&osd->o_linger_requests, lreq);
2738922dab61SIlya Dryomov 	lreq->osd = osd;
2739922dab61SIlya Dryomov }
2740922dab61SIlya Dryomov 
2741922dab61SIlya Dryomov static void unlink_linger(struct ceph_osd *osd,
2742922dab61SIlya Dryomov 			  struct ceph_osd_linger_request *lreq)
2743922dab61SIlya Dryomov {
2744922dab61SIlya Dryomov 	verify_osd_locked(osd);
2745922dab61SIlya Dryomov 	WARN_ON(lreq->osd != osd);
2746922dab61SIlya Dryomov 	dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2747922dab61SIlya Dryomov 	     osd->o_osd, lreq, lreq->linger_id);
2748922dab61SIlya Dryomov 
2749922dab61SIlya Dryomov 	lreq->osd = NULL;
2750922dab61SIlya Dryomov 	erase_linger(&osd->o_linger_requests, lreq);
2751922dab61SIlya Dryomov 	put_osd(osd);
2752922dab61SIlya Dryomov 
2753922dab61SIlya Dryomov 	if (!osd_homeless(osd))
2754922dab61SIlya Dryomov 		maybe_move_osd_to_lru(osd);
2755922dab61SIlya Dryomov 	else
2756922dab61SIlya Dryomov 		atomic_dec(&osd->o_osdc->num_homeless);
2757922dab61SIlya Dryomov }
2758922dab61SIlya Dryomov 
2759922dab61SIlya Dryomov static bool __linger_registered(struct ceph_osd_linger_request *lreq)
2760922dab61SIlya Dryomov {
2761922dab61SIlya Dryomov 	verify_osdc_locked(lreq->osdc);
2762922dab61SIlya Dryomov 
2763922dab61SIlya Dryomov 	return !RB_EMPTY_NODE(&lreq->osdc_node);
2764922dab61SIlya Dryomov }
2765922dab61SIlya Dryomov 
2766922dab61SIlya Dryomov static bool linger_registered(struct ceph_osd_linger_request *lreq)
2767922dab61SIlya Dryomov {
2768922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2769922dab61SIlya Dryomov 	bool registered;
2770922dab61SIlya Dryomov 
2771922dab61SIlya Dryomov 	down_read(&osdc->lock);
2772922dab61SIlya Dryomov 	registered = __linger_registered(lreq);
2773922dab61SIlya Dryomov 	up_read(&osdc->lock);
2774922dab61SIlya Dryomov 
2775922dab61SIlya Dryomov 	return registered;
2776922dab61SIlya Dryomov }
2777922dab61SIlya Dryomov 
2778922dab61SIlya Dryomov static void linger_register(struct ceph_osd_linger_request *lreq)
2779922dab61SIlya Dryomov {
2780922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2781922dab61SIlya Dryomov 
2782922dab61SIlya Dryomov 	verify_osdc_wrlocked(osdc);
2783922dab61SIlya Dryomov 	WARN_ON(lreq->linger_id);
2784922dab61SIlya Dryomov 
2785922dab61SIlya Dryomov 	linger_get(lreq);
2786922dab61SIlya Dryomov 	lreq->linger_id = ++osdc->last_linger_id;
2787922dab61SIlya Dryomov 	insert_linger_osdc(&osdc->linger_requests, lreq);
2788922dab61SIlya Dryomov }
2789922dab61SIlya Dryomov 
2790922dab61SIlya Dryomov static void linger_unregister(struct ceph_osd_linger_request *lreq)
2791922dab61SIlya Dryomov {
2792922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2793922dab61SIlya Dryomov 
2794922dab61SIlya Dryomov 	verify_osdc_wrlocked(osdc);
2795922dab61SIlya Dryomov 
2796922dab61SIlya Dryomov 	erase_linger_osdc(&osdc->linger_requests, lreq);
2797922dab61SIlya Dryomov 	linger_put(lreq);
2798922dab61SIlya Dryomov }
2799922dab61SIlya Dryomov 
2800922dab61SIlya Dryomov static void cancel_linger_request(struct ceph_osd_request *req)
2801922dab61SIlya Dryomov {
2802922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
2803922dab61SIlya Dryomov 
2804922dab61SIlya Dryomov 	WARN_ON(!req->r_linger);
2805922dab61SIlya Dryomov 	cancel_request(req);
2806922dab61SIlya Dryomov 	linger_put(lreq);
2807922dab61SIlya Dryomov }
2808922dab61SIlya Dryomov 
2809922dab61SIlya Dryomov struct linger_work {
2810922dab61SIlya Dryomov 	struct work_struct work;
2811922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
2812b07d3c4bSIlya Dryomov 	struct list_head pending_item;
2813b07d3c4bSIlya Dryomov 	unsigned long queued_stamp;
2814922dab61SIlya Dryomov 
2815922dab61SIlya Dryomov 	union {
2816922dab61SIlya Dryomov 		struct {
2817922dab61SIlya Dryomov 			u64 notify_id;
2818922dab61SIlya Dryomov 			u64 notifier_id;
2819922dab61SIlya Dryomov 			void *payload; /* points into @msg front */
2820922dab61SIlya Dryomov 			size_t payload_len;
2821922dab61SIlya Dryomov 
2822922dab61SIlya Dryomov 			struct ceph_msg *msg; /* for ceph_msg_put() */
2823922dab61SIlya Dryomov 		} notify;
2824922dab61SIlya Dryomov 		struct {
2825922dab61SIlya Dryomov 			int err;
2826922dab61SIlya Dryomov 		} error;
2827922dab61SIlya Dryomov 	};
2828922dab61SIlya Dryomov };
2829922dab61SIlya Dryomov 
2830922dab61SIlya Dryomov static struct linger_work *lwork_alloc(struct ceph_osd_linger_request *lreq,
2831922dab61SIlya Dryomov 				       work_func_t workfn)
2832922dab61SIlya Dryomov {
2833922dab61SIlya Dryomov 	struct linger_work *lwork;
2834922dab61SIlya Dryomov 
2835922dab61SIlya Dryomov 	lwork = kzalloc(sizeof(*lwork), GFP_NOIO);
2836922dab61SIlya Dryomov 	if (!lwork)
2837922dab61SIlya Dryomov 		return NULL;
2838922dab61SIlya Dryomov 
2839922dab61SIlya Dryomov 	INIT_WORK(&lwork->work, workfn);
2840b07d3c4bSIlya Dryomov 	INIT_LIST_HEAD(&lwork->pending_item);
2841922dab61SIlya Dryomov 	lwork->lreq = linger_get(lreq);
2842922dab61SIlya Dryomov 
2843922dab61SIlya Dryomov 	return lwork;
2844922dab61SIlya Dryomov }
2845922dab61SIlya Dryomov 
2846922dab61SIlya Dryomov static void lwork_free(struct linger_work *lwork)
2847922dab61SIlya Dryomov {
2848922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2849922dab61SIlya Dryomov 
2850b07d3c4bSIlya Dryomov 	mutex_lock(&lreq->lock);
2851b07d3c4bSIlya Dryomov 	list_del(&lwork->pending_item);
2852b07d3c4bSIlya Dryomov 	mutex_unlock(&lreq->lock);
2853b07d3c4bSIlya Dryomov 
2854922dab61SIlya Dryomov 	linger_put(lreq);
2855922dab61SIlya Dryomov 	kfree(lwork);
2856922dab61SIlya Dryomov }
2857922dab61SIlya Dryomov 
2858922dab61SIlya Dryomov static void lwork_queue(struct linger_work *lwork)
2859922dab61SIlya Dryomov {
2860922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2861922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2862922dab61SIlya Dryomov 
2863922dab61SIlya Dryomov 	verify_lreq_locked(lreq);
2864b07d3c4bSIlya Dryomov 	WARN_ON(!list_empty(&lwork->pending_item));
2865b07d3c4bSIlya Dryomov 
2866b07d3c4bSIlya Dryomov 	lwork->queued_stamp = jiffies;
2867b07d3c4bSIlya Dryomov 	list_add_tail(&lwork->pending_item, &lreq->pending_lworks);
2868922dab61SIlya Dryomov 	queue_work(osdc->notify_wq, &lwork->work);
2869922dab61SIlya Dryomov }
2870922dab61SIlya Dryomov 
2871922dab61SIlya Dryomov static void do_watch_notify(struct work_struct *w)
2872922dab61SIlya Dryomov {
2873922dab61SIlya Dryomov 	struct linger_work *lwork = container_of(w, struct linger_work, work);
2874922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2875922dab61SIlya Dryomov 
2876922dab61SIlya Dryomov 	if (!linger_registered(lreq)) {
2877922dab61SIlya Dryomov 		dout("%s lreq %p not registered\n", __func__, lreq);
2878922dab61SIlya Dryomov 		goto out;
2879922dab61SIlya Dryomov 	}
2880922dab61SIlya Dryomov 
288119079203SIlya Dryomov 	WARN_ON(!lreq->is_watch);
2882922dab61SIlya Dryomov 	dout("%s lreq %p notify_id %llu notifier_id %llu payload_len %zu\n",
2883922dab61SIlya Dryomov 	     __func__, lreq, lwork->notify.notify_id, lwork->notify.notifier_id,
2884922dab61SIlya Dryomov 	     lwork->notify.payload_len);
2885922dab61SIlya Dryomov 	lreq->wcb(lreq->data, lwork->notify.notify_id, lreq->linger_id,
2886922dab61SIlya Dryomov 		  lwork->notify.notifier_id, lwork->notify.payload,
2887922dab61SIlya Dryomov 		  lwork->notify.payload_len);
2888922dab61SIlya Dryomov 
2889922dab61SIlya Dryomov out:
2890922dab61SIlya Dryomov 	ceph_msg_put(lwork->notify.msg);
2891922dab61SIlya Dryomov 	lwork_free(lwork);
2892922dab61SIlya Dryomov }
2893922dab61SIlya Dryomov 
2894922dab61SIlya Dryomov static void do_watch_error(struct work_struct *w)
2895922dab61SIlya Dryomov {
2896922dab61SIlya Dryomov 	struct linger_work *lwork = container_of(w, struct linger_work, work);
2897922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2898922dab61SIlya Dryomov 
2899922dab61SIlya Dryomov 	if (!linger_registered(lreq)) {
2900922dab61SIlya Dryomov 		dout("%s lreq %p not registered\n", __func__, lreq);
2901922dab61SIlya Dryomov 		goto out;
2902922dab61SIlya Dryomov 	}
2903922dab61SIlya Dryomov 
2904922dab61SIlya Dryomov 	dout("%s lreq %p err %d\n", __func__, lreq, lwork->error.err);
2905922dab61SIlya Dryomov 	lreq->errcb(lreq->data, lreq->linger_id, lwork->error.err);
2906922dab61SIlya Dryomov 
2907922dab61SIlya Dryomov out:
2908922dab61SIlya Dryomov 	lwork_free(lwork);
2909922dab61SIlya Dryomov }
2910922dab61SIlya Dryomov 
2911922dab61SIlya Dryomov static void queue_watch_error(struct ceph_osd_linger_request *lreq)
2912922dab61SIlya Dryomov {
2913922dab61SIlya Dryomov 	struct linger_work *lwork;
2914922dab61SIlya Dryomov 
2915922dab61SIlya Dryomov 	lwork = lwork_alloc(lreq, do_watch_error);
2916922dab61SIlya Dryomov 	if (!lwork) {
2917922dab61SIlya Dryomov 		pr_err("failed to allocate error-lwork\n");
2918922dab61SIlya Dryomov 		return;
2919922dab61SIlya Dryomov 	}
2920922dab61SIlya Dryomov 
2921922dab61SIlya Dryomov 	lwork->error.err = lreq->last_error;
2922922dab61SIlya Dryomov 	lwork_queue(lwork);
2923922dab61SIlya Dryomov }
2924922dab61SIlya Dryomov 
2925922dab61SIlya Dryomov static void linger_reg_commit_complete(struct ceph_osd_linger_request *lreq,
2926922dab61SIlya Dryomov 				       int result)
2927922dab61SIlya Dryomov {
2928922dab61SIlya Dryomov 	if (!completion_done(&lreq->reg_commit_wait)) {
2929922dab61SIlya Dryomov 		lreq->reg_commit_error = (result <= 0 ? result : 0);
2930922dab61SIlya Dryomov 		complete_all(&lreq->reg_commit_wait);
2931922dab61SIlya Dryomov 	}
2932922dab61SIlya Dryomov }
2933922dab61SIlya Dryomov 
2934922dab61SIlya Dryomov static void linger_commit_cb(struct ceph_osd_request *req)
2935922dab61SIlya Dryomov {
2936922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
2937922dab61SIlya Dryomov 
2938922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
2939922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu result %d\n", __func__, lreq,
2940922dab61SIlya Dryomov 	     lreq->linger_id, req->r_result);
2941922dab61SIlya Dryomov 	linger_reg_commit_complete(lreq, req->r_result);
2942922dab61SIlya Dryomov 	lreq->committed = true;
2943922dab61SIlya Dryomov 
294419079203SIlya Dryomov 	if (!lreq->is_watch) {
294519079203SIlya Dryomov 		struct ceph_osd_data *osd_data =
294619079203SIlya Dryomov 		    osd_req_op_data(req, 0, notify, response_data);
294719079203SIlya Dryomov 		void *p = page_address(osd_data->pages[0]);
294819079203SIlya Dryomov 
294919079203SIlya Dryomov 		WARN_ON(req->r_ops[0].op != CEPH_OSD_OP_NOTIFY ||
295019079203SIlya Dryomov 			osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
295119079203SIlya Dryomov 
295219079203SIlya Dryomov 		/* make note of the notify_id */
295319079203SIlya Dryomov 		if (req->r_ops[0].outdata_len >= sizeof(u64)) {
295419079203SIlya Dryomov 			lreq->notify_id = ceph_decode_64(&p);
295519079203SIlya Dryomov 			dout("lreq %p notify_id %llu\n", lreq,
295619079203SIlya Dryomov 			     lreq->notify_id);
295719079203SIlya Dryomov 		} else {
295819079203SIlya Dryomov 			dout("lreq %p no notify_id\n", lreq);
295919079203SIlya Dryomov 		}
296019079203SIlya Dryomov 	}
296119079203SIlya Dryomov 
2962922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
2963922dab61SIlya Dryomov 	linger_put(lreq);
2964922dab61SIlya Dryomov }
2965922dab61SIlya Dryomov 
2966922dab61SIlya Dryomov static int normalize_watch_error(int err)
2967922dab61SIlya Dryomov {
2968922dab61SIlya Dryomov 	/*
2969922dab61SIlya Dryomov 	 * Translate ENOENT -> ENOTCONN so that a delete->disconnection
2970922dab61SIlya Dryomov 	 * notification and a failure to reconnect because we raced with
2971922dab61SIlya Dryomov 	 * the delete appear the same to the user.
2972922dab61SIlya Dryomov 	 */
2973922dab61SIlya Dryomov 	if (err == -ENOENT)
2974922dab61SIlya Dryomov 		err = -ENOTCONN;
2975922dab61SIlya Dryomov 
2976922dab61SIlya Dryomov 	return err;
2977922dab61SIlya Dryomov }
2978922dab61SIlya Dryomov 
2979922dab61SIlya Dryomov static void linger_reconnect_cb(struct ceph_osd_request *req)
2980922dab61SIlya Dryomov {
2981922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
2982922dab61SIlya Dryomov 
2983922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
2984922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu result %d last_error %d\n", __func__,
2985922dab61SIlya Dryomov 	     lreq, lreq->linger_id, req->r_result, lreq->last_error);
2986922dab61SIlya Dryomov 	if (req->r_result < 0) {
2987922dab61SIlya Dryomov 		if (!lreq->last_error) {
2988922dab61SIlya Dryomov 			lreq->last_error = normalize_watch_error(req->r_result);
2989922dab61SIlya Dryomov 			queue_watch_error(lreq);
2990922dab61SIlya Dryomov 		}
2991922dab61SIlya Dryomov 	}
2992922dab61SIlya Dryomov 
2993922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
2994922dab61SIlya Dryomov 	linger_put(lreq);
2995922dab61SIlya Dryomov }
2996922dab61SIlya Dryomov 
2997922dab61SIlya Dryomov static void send_linger(struct ceph_osd_linger_request *lreq)
2998922dab61SIlya Dryomov {
2999922dab61SIlya Dryomov 	struct ceph_osd_request *req = lreq->reg_req;
3000922dab61SIlya Dryomov 	struct ceph_osd_req_op *op = &req->r_ops[0];
3001922dab61SIlya Dryomov 
3002922dab61SIlya Dryomov 	verify_osdc_wrlocked(req->r_osdc);
3003922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3004922dab61SIlya Dryomov 
3005922dab61SIlya Dryomov 	if (req->r_osd)
3006922dab61SIlya Dryomov 		cancel_linger_request(req);
3007922dab61SIlya Dryomov 
3008922dab61SIlya Dryomov 	request_reinit(req);
3009922dab61SIlya Dryomov 	ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
3010922dab61SIlya Dryomov 	ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
3011922dab61SIlya Dryomov 	req->r_flags = lreq->t.flags;
3012922dab61SIlya Dryomov 	req->r_mtime = lreq->mtime;
3013922dab61SIlya Dryomov 
3014922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
301519079203SIlya Dryomov 	if (lreq->is_watch && lreq->committed) {
3016922dab61SIlya Dryomov 		WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
3017922dab61SIlya Dryomov 			op->watch.cookie != lreq->linger_id);
3018922dab61SIlya Dryomov 		op->watch.op = CEPH_OSD_WATCH_OP_RECONNECT;
3019922dab61SIlya Dryomov 		op->watch.gen = ++lreq->register_gen;
3020922dab61SIlya Dryomov 		dout("lreq %p reconnect register_gen %u\n", lreq,
3021922dab61SIlya Dryomov 		     op->watch.gen);
3022922dab61SIlya Dryomov 		req->r_callback = linger_reconnect_cb;
3023922dab61SIlya Dryomov 	} else {
302419079203SIlya Dryomov 		if (!lreq->is_watch)
302519079203SIlya Dryomov 			lreq->notify_id = 0;
302619079203SIlya Dryomov 		else
3027922dab61SIlya Dryomov 			WARN_ON(op->watch.op != CEPH_OSD_WATCH_OP_WATCH);
3028922dab61SIlya Dryomov 		dout("lreq %p register\n", lreq);
3029922dab61SIlya Dryomov 		req->r_callback = linger_commit_cb;
3030922dab61SIlya Dryomov 	}
3031922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
3032922dab61SIlya Dryomov 
3033922dab61SIlya Dryomov 	req->r_priv = linger_get(lreq);
3034922dab61SIlya Dryomov 	req->r_linger = true;
3035922dab61SIlya Dryomov 
3036922dab61SIlya Dryomov 	submit_request(req, true);
3037922dab61SIlya Dryomov }
3038922dab61SIlya Dryomov 
3039922dab61SIlya Dryomov static void linger_ping_cb(struct ceph_osd_request *req)
3040922dab61SIlya Dryomov {
3041922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
3042922dab61SIlya Dryomov 
3043922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
3044922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu result %d ping_sent %lu last_error %d\n",
3045922dab61SIlya Dryomov 	     __func__, lreq, lreq->linger_id, req->r_result, lreq->ping_sent,
3046922dab61SIlya Dryomov 	     lreq->last_error);
3047922dab61SIlya Dryomov 	if (lreq->register_gen == req->r_ops[0].watch.gen) {
3048b07d3c4bSIlya Dryomov 		if (!req->r_result) {
3049b07d3c4bSIlya Dryomov 			lreq->watch_valid_thru = lreq->ping_sent;
3050b07d3c4bSIlya Dryomov 		} else if (!lreq->last_error) {
3051922dab61SIlya Dryomov 			lreq->last_error = normalize_watch_error(req->r_result);
3052922dab61SIlya Dryomov 			queue_watch_error(lreq);
3053922dab61SIlya Dryomov 		}
3054922dab61SIlya Dryomov 	} else {
3055922dab61SIlya Dryomov 		dout("lreq %p register_gen %u ignoring old pong %u\n", lreq,
3056922dab61SIlya Dryomov 		     lreq->register_gen, req->r_ops[0].watch.gen);
3057922dab61SIlya Dryomov 	}
3058922dab61SIlya Dryomov 
3059922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
3060922dab61SIlya Dryomov 	linger_put(lreq);
3061922dab61SIlya Dryomov }
3062922dab61SIlya Dryomov 
3063922dab61SIlya Dryomov static void send_linger_ping(struct ceph_osd_linger_request *lreq)
3064922dab61SIlya Dryomov {
3065922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
3066922dab61SIlya Dryomov 	struct ceph_osd_request *req = lreq->ping_req;
3067922dab61SIlya Dryomov 	struct ceph_osd_req_op *op = &req->r_ops[0];
3068922dab61SIlya Dryomov 
3069b7ec35b3SIlya Dryomov 	if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
3070922dab61SIlya Dryomov 		dout("%s PAUSERD\n", __func__);
3071922dab61SIlya Dryomov 		return;
3072922dab61SIlya Dryomov 	}
3073922dab61SIlya Dryomov 
3074922dab61SIlya Dryomov 	lreq->ping_sent = jiffies;
3075922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu ping_sent %lu register_gen %u\n",
3076922dab61SIlya Dryomov 	     __func__, lreq, lreq->linger_id, lreq->ping_sent,
3077922dab61SIlya Dryomov 	     lreq->register_gen);
3078922dab61SIlya Dryomov 
3079922dab61SIlya Dryomov 	if (req->r_osd)
3080922dab61SIlya Dryomov 		cancel_linger_request(req);
3081922dab61SIlya Dryomov 
3082922dab61SIlya Dryomov 	request_reinit(req);
3083922dab61SIlya Dryomov 	target_copy(&req->r_t, &lreq->t);
3084922dab61SIlya Dryomov 
3085922dab61SIlya Dryomov 	WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
3086922dab61SIlya Dryomov 		op->watch.cookie != lreq->linger_id ||
3087922dab61SIlya Dryomov 		op->watch.op != CEPH_OSD_WATCH_OP_PING);
3088922dab61SIlya Dryomov 	op->watch.gen = lreq->register_gen;
3089922dab61SIlya Dryomov 	req->r_callback = linger_ping_cb;
3090922dab61SIlya Dryomov 	req->r_priv = linger_get(lreq);
3091922dab61SIlya Dryomov 	req->r_linger = true;
3092922dab61SIlya Dryomov 
3093922dab61SIlya Dryomov 	ceph_osdc_get_request(req);
3094922dab61SIlya Dryomov 	account_request(req);
3095922dab61SIlya Dryomov 	req->r_tid = atomic64_inc_return(&osdc->last_tid);
3096922dab61SIlya Dryomov 	link_request(lreq->osd, req);
3097922dab61SIlya Dryomov 	send_request(req);
3098922dab61SIlya Dryomov }
3099922dab61SIlya Dryomov 
3100922dab61SIlya Dryomov static void linger_submit(struct ceph_osd_linger_request *lreq)
3101922dab61SIlya Dryomov {
3102922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
3103922dab61SIlya Dryomov 	struct ceph_osd *osd;
3104922dab61SIlya Dryomov 
310581c65213SIlya Dryomov 	down_write(&osdc->lock);
310681c65213SIlya Dryomov 	linger_register(lreq);
310781c65213SIlya Dryomov 	if (lreq->is_watch) {
310881c65213SIlya Dryomov 		lreq->reg_req->r_ops[0].watch.cookie = lreq->linger_id;
310981c65213SIlya Dryomov 		lreq->ping_req->r_ops[0].watch.cookie = lreq->linger_id;
311081c65213SIlya Dryomov 	} else {
311181c65213SIlya Dryomov 		lreq->reg_req->r_ops[0].notify.cookie = lreq->linger_id;
311281c65213SIlya Dryomov 	}
311381c65213SIlya Dryomov 
31148edf84baSIlya Dryomov 	calc_target(osdc, &lreq->t, false);
3115922dab61SIlya Dryomov 	osd = lookup_create_osd(osdc, lreq->t.osd, true);
3116922dab61SIlya Dryomov 	link_linger(osd, lreq);
3117922dab61SIlya Dryomov 
3118922dab61SIlya Dryomov 	send_linger(lreq);
311981c65213SIlya Dryomov 	up_write(&osdc->lock);
3120922dab61SIlya Dryomov }
3121922dab61SIlya Dryomov 
31224609245eSIlya Dryomov static void cancel_linger_map_check(struct ceph_osd_linger_request *lreq)
31234609245eSIlya Dryomov {
31244609245eSIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
31254609245eSIlya Dryomov 	struct ceph_osd_linger_request *lookup_lreq;
31264609245eSIlya Dryomov 
31274609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
31284609245eSIlya Dryomov 
31294609245eSIlya Dryomov 	lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
31304609245eSIlya Dryomov 				       lreq->linger_id);
31314609245eSIlya Dryomov 	if (!lookup_lreq)
31324609245eSIlya Dryomov 		return;
31334609245eSIlya Dryomov 
31344609245eSIlya Dryomov 	WARN_ON(lookup_lreq != lreq);
31354609245eSIlya Dryomov 	erase_linger_mc(&osdc->linger_map_checks, lreq);
31364609245eSIlya Dryomov 	linger_put(lreq);
31374609245eSIlya Dryomov }
31384609245eSIlya Dryomov 
3139922dab61SIlya Dryomov /*
3140922dab61SIlya Dryomov  * @lreq has to be both registered and linked.
3141922dab61SIlya Dryomov  */
3142922dab61SIlya Dryomov static void __linger_cancel(struct ceph_osd_linger_request *lreq)
3143922dab61SIlya Dryomov {
314419079203SIlya Dryomov 	if (lreq->is_watch && lreq->ping_req->r_osd)
3145922dab61SIlya Dryomov 		cancel_linger_request(lreq->ping_req);
3146922dab61SIlya Dryomov 	if (lreq->reg_req->r_osd)
3147922dab61SIlya Dryomov 		cancel_linger_request(lreq->reg_req);
31484609245eSIlya Dryomov 	cancel_linger_map_check(lreq);
3149922dab61SIlya Dryomov 	unlink_linger(lreq->osd, lreq);
3150922dab61SIlya Dryomov 	linger_unregister(lreq);
3151922dab61SIlya Dryomov }
3152922dab61SIlya Dryomov 
3153922dab61SIlya Dryomov static void linger_cancel(struct ceph_osd_linger_request *lreq)
3154922dab61SIlya Dryomov {
3155922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
3156922dab61SIlya Dryomov 
3157922dab61SIlya Dryomov 	down_write(&osdc->lock);
3158922dab61SIlya Dryomov 	if (__linger_registered(lreq))
3159922dab61SIlya Dryomov 		__linger_cancel(lreq);
3160922dab61SIlya Dryomov 	up_write(&osdc->lock);
3161922dab61SIlya Dryomov }
3162922dab61SIlya Dryomov 
31634609245eSIlya Dryomov static void send_linger_map_check(struct ceph_osd_linger_request *lreq);
31644609245eSIlya Dryomov 
31654609245eSIlya Dryomov static void check_linger_pool_dne(struct ceph_osd_linger_request *lreq)
31664609245eSIlya Dryomov {
31674609245eSIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
31684609245eSIlya Dryomov 	struct ceph_osdmap *map = osdc->osdmap;
31694609245eSIlya Dryomov 
31704609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
31714609245eSIlya Dryomov 	WARN_ON(!map->epoch);
31724609245eSIlya Dryomov 
31734609245eSIlya Dryomov 	if (lreq->register_gen) {
31744609245eSIlya Dryomov 		lreq->map_dne_bound = map->epoch;
31754609245eSIlya Dryomov 		dout("%s lreq %p linger_id %llu pool disappeared\n", __func__,
31764609245eSIlya Dryomov 		     lreq, lreq->linger_id);
31774609245eSIlya Dryomov 	} else {
31784609245eSIlya Dryomov 		dout("%s lreq %p linger_id %llu map_dne_bound %u have %u\n",
31794609245eSIlya Dryomov 		     __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
31804609245eSIlya Dryomov 		     map->epoch);
31814609245eSIlya Dryomov 	}
31824609245eSIlya Dryomov 
31834609245eSIlya Dryomov 	if (lreq->map_dne_bound) {
31844609245eSIlya Dryomov 		if (map->epoch >= lreq->map_dne_bound) {
31854609245eSIlya Dryomov 			/* we had a new enough map */
31864609245eSIlya Dryomov 			pr_info("linger_id %llu pool does not exist\n",
31874609245eSIlya Dryomov 				lreq->linger_id);
31884609245eSIlya Dryomov 			linger_reg_commit_complete(lreq, -ENOENT);
31894609245eSIlya Dryomov 			__linger_cancel(lreq);
31904609245eSIlya Dryomov 		}
31914609245eSIlya Dryomov 	} else {
31924609245eSIlya Dryomov 		send_linger_map_check(lreq);
31934609245eSIlya Dryomov 	}
31944609245eSIlya Dryomov }
31954609245eSIlya Dryomov 
31964609245eSIlya Dryomov static void linger_map_check_cb(struct ceph_mon_generic_request *greq)
31974609245eSIlya Dryomov {
31984609245eSIlya Dryomov 	struct ceph_osd_client *osdc = &greq->monc->client->osdc;
31994609245eSIlya Dryomov 	struct ceph_osd_linger_request *lreq;
32004609245eSIlya Dryomov 	u64 linger_id = greq->private_data;
32014609245eSIlya Dryomov 
32024609245eSIlya Dryomov 	WARN_ON(greq->result || !greq->u.newest);
32034609245eSIlya Dryomov 
32044609245eSIlya Dryomov 	down_write(&osdc->lock);
32054609245eSIlya Dryomov 	lreq = lookup_linger_mc(&osdc->linger_map_checks, linger_id);
32064609245eSIlya Dryomov 	if (!lreq) {
32074609245eSIlya Dryomov 		dout("%s linger_id %llu dne\n", __func__, linger_id);
32084609245eSIlya Dryomov 		goto out_unlock;
32094609245eSIlya Dryomov 	}
32104609245eSIlya Dryomov 
32114609245eSIlya Dryomov 	dout("%s lreq %p linger_id %llu map_dne_bound %u newest %llu\n",
32124609245eSIlya Dryomov 	     __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
32134609245eSIlya Dryomov 	     greq->u.newest);
32144609245eSIlya Dryomov 	if (!lreq->map_dne_bound)
32154609245eSIlya Dryomov 		lreq->map_dne_bound = greq->u.newest;
32164609245eSIlya Dryomov 	erase_linger_mc(&osdc->linger_map_checks, lreq);
32174609245eSIlya Dryomov 	check_linger_pool_dne(lreq);
32184609245eSIlya Dryomov 
32194609245eSIlya Dryomov 	linger_put(lreq);
32204609245eSIlya Dryomov out_unlock:
32214609245eSIlya Dryomov 	up_write(&osdc->lock);
32224609245eSIlya Dryomov }
32234609245eSIlya Dryomov 
32244609245eSIlya Dryomov static void send_linger_map_check(struct ceph_osd_linger_request *lreq)
32254609245eSIlya Dryomov {
32264609245eSIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
32274609245eSIlya Dryomov 	struct ceph_osd_linger_request *lookup_lreq;
32284609245eSIlya Dryomov 	int ret;
32294609245eSIlya Dryomov 
32304609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
32314609245eSIlya Dryomov 
32324609245eSIlya Dryomov 	lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
32334609245eSIlya Dryomov 				       lreq->linger_id);
32344609245eSIlya Dryomov 	if (lookup_lreq) {
32354609245eSIlya Dryomov 		WARN_ON(lookup_lreq != lreq);
32364609245eSIlya Dryomov 		return;
32374609245eSIlya Dryomov 	}
32384609245eSIlya Dryomov 
32394609245eSIlya Dryomov 	linger_get(lreq);
32404609245eSIlya Dryomov 	insert_linger_mc(&osdc->linger_map_checks, lreq);
32414609245eSIlya Dryomov 	ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
32424609245eSIlya Dryomov 					  linger_map_check_cb, lreq->linger_id);
32434609245eSIlya Dryomov 	WARN_ON(ret);
32444609245eSIlya Dryomov }
32454609245eSIlya Dryomov 
3246922dab61SIlya Dryomov static int linger_reg_commit_wait(struct ceph_osd_linger_request *lreq)
3247922dab61SIlya Dryomov {
3248922dab61SIlya Dryomov 	int ret;
3249922dab61SIlya Dryomov 
3250922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3251922dab61SIlya Dryomov 	ret = wait_for_completion_interruptible(&lreq->reg_commit_wait);
3252922dab61SIlya Dryomov 	return ret ?: lreq->reg_commit_error;
3253922dab61SIlya Dryomov }
3254922dab61SIlya Dryomov 
325519079203SIlya Dryomov static int linger_notify_finish_wait(struct ceph_osd_linger_request *lreq)
325619079203SIlya Dryomov {
325719079203SIlya Dryomov 	int ret;
325819079203SIlya Dryomov 
325919079203SIlya Dryomov 	dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
326019079203SIlya Dryomov 	ret = wait_for_completion_interruptible(&lreq->notify_finish_wait);
326119079203SIlya Dryomov 	return ret ?: lreq->notify_finish_error;
326219079203SIlya Dryomov }
326319079203SIlya Dryomov 
3264922dab61SIlya Dryomov /*
3265fbca9635SIlya Dryomov  * Timeout callback, called every N seconds.  When 1 or more OSD
3266fbca9635SIlya Dryomov  * requests has been active for more than N seconds, we send a keepalive
3267fbca9635SIlya Dryomov  * (tag + timestamp) to its OSD to ensure any communications channel
3268fbca9635SIlya Dryomov  * reset is detected.
32693d14c5d2SYehuda Sadeh  */
32703d14c5d2SYehuda Sadeh static void handle_timeout(struct work_struct *work)
32713d14c5d2SYehuda Sadeh {
32723d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc =
32733d14c5d2SYehuda Sadeh 		container_of(work, struct ceph_osd_client, timeout_work.work);
3274a319bf56SIlya Dryomov 	struct ceph_options *opts = osdc->client->options;
32755aea3dcdSIlya Dryomov 	unsigned long cutoff = jiffies - opts->osd_keepalive_timeout;
32767cc5e38fSIlya Dryomov 	unsigned long expiry_cutoff = jiffies - opts->osd_request_timeout;
32775aea3dcdSIlya Dryomov 	LIST_HEAD(slow_osds);
32785aea3dcdSIlya Dryomov 	struct rb_node *n, *p;
32793d14c5d2SYehuda Sadeh 
32805aea3dcdSIlya Dryomov 	dout("%s osdc %p\n", __func__, osdc);
32815aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
32823d14c5d2SYehuda Sadeh 
32833d14c5d2SYehuda Sadeh 	/*
32843d14c5d2SYehuda Sadeh 	 * ping osds that are a bit slow.  this ensures that if there
32853d14c5d2SYehuda Sadeh 	 * is a break in the TCP connection we will notice, and reopen
32863d14c5d2SYehuda Sadeh 	 * a connection with that osd (from the fault callback).
32873d14c5d2SYehuda Sadeh 	 */
32885aea3dcdSIlya Dryomov 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
32895aea3dcdSIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
32905aea3dcdSIlya Dryomov 		bool found = false;
32913d14c5d2SYehuda Sadeh 
32927cc5e38fSIlya Dryomov 		for (p = rb_first(&osd->o_requests); p; ) {
32935aea3dcdSIlya Dryomov 			struct ceph_osd_request *req =
32945aea3dcdSIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
32955aea3dcdSIlya Dryomov 
32967cc5e38fSIlya Dryomov 			p = rb_next(p); /* abort_request() */
32977cc5e38fSIlya Dryomov 
32985aea3dcdSIlya Dryomov 			if (time_before(req->r_stamp, cutoff)) {
32995aea3dcdSIlya Dryomov 				dout(" req %p tid %llu on osd%d is laggy\n",
33005aea3dcdSIlya Dryomov 				     req, req->r_tid, osd->o_osd);
33015aea3dcdSIlya Dryomov 				found = true;
33025aea3dcdSIlya Dryomov 			}
33037cc5e38fSIlya Dryomov 			if (opts->osd_request_timeout &&
33047cc5e38fSIlya Dryomov 			    time_before(req->r_start_stamp, expiry_cutoff)) {
33057cc5e38fSIlya Dryomov 				pr_err_ratelimited("tid %llu on osd%d timeout\n",
33067cc5e38fSIlya Dryomov 				       req->r_tid, osd->o_osd);
33077cc5e38fSIlya Dryomov 				abort_request(req, -ETIMEDOUT);
33087cc5e38fSIlya Dryomov 			}
33095aea3dcdSIlya Dryomov 		}
3310922dab61SIlya Dryomov 		for (p = rb_first(&osd->o_linger_requests); p; p = rb_next(p)) {
3311922dab61SIlya Dryomov 			struct ceph_osd_linger_request *lreq =
3312922dab61SIlya Dryomov 			    rb_entry(p, struct ceph_osd_linger_request, node);
3313922dab61SIlya Dryomov 
3314922dab61SIlya Dryomov 			dout(" lreq %p linger_id %llu is served by osd%d\n",
3315922dab61SIlya Dryomov 			     lreq, lreq->linger_id, osd->o_osd);
3316922dab61SIlya Dryomov 			found = true;
3317922dab61SIlya Dryomov 
3318922dab61SIlya Dryomov 			mutex_lock(&lreq->lock);
331919079203SIlya Dryomov 			if (lreq->is_watch && lreq->committed && !lreq->last_error)
3320922dab61SIlya Dryomov 				send_linger_ping(lreq);
3321922dab61SIlya Dryomov 			mutex_unlock(&lreq->lock);
3322922dab61SIlya Dryomov 		}
33235aea3dcdSIlya Dryomov 
33245aea3dcdSIlya Dryomov 		if (found)
33253d14c5d2SYehuda Sadeh 			list_move_tail(&osd->o_keepalive_item, &slow_osds);
33263d14c5d2SYehuda Sadeh 	}
33275aea3dcdSIlya Dryomov 
33287cc5e38fSIlya Dryomov 	if (opts->osd_request_timeout) {
33297cc5e38fSIlya Dryomov 		for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
33307cc5e38fSIlya Dryomov 			struct ceph_osd_request *req =
33317cc5e38fSIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
33327cc5e38fSIlya Dryomov 
33337cc5e38fSIlya Dryomov 			p = rb_next(p); /* abort_request() */
33347cc5e38fSIlya Dryomov 
33357cc5e38fSIlya Dryomov 			if (time_before(req->r_start_stamp, expiry_cutoff)) {
33367cc5e38fSIlya Dryomov 				pr_err_ratelimited("tid %llu on osd%d timeout\n",
33377cc5e38fSIlya Dryomov 				       req->r_tid, osdc->homeless_osd.o_osd);
33387cc5e38fSIlya Dryomov 				abort_request(req, -ETIMEDOUT);
33397cc5e38fSIlya Dryomov 			}
33407cc5e38fSIlya Dryomov 		}
33417cc5e38fSIlya Dryomov 	}
33427cc5e38fSIlya Dryomov 
33435aea3dcdSIlya Dryomov 	if (atomic_read(&osdc->num_homeless) || !list_empty(&slow_osds))
33445aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
33455aea3dcdSIlya Dryomov 
33463d14c5d2SYehuda Sadeh 	while (!list_empty(&slow_osds)) {
33475aea3dcdSIlya Dryomov 		struct ceph_osd *osd = list_first_entry(&slow_osds,
33485aea3dcdSIlya Dryomov 							struct ceph_osd,
33493d14c5d2SYehuda Sadeh 							o_keepalive_item);
33503d14c5d2SYehuda Sadeh 		list_del_init(&osd->o_keepalive_item);
33513d14c5d2SYehuda Sadeh 		ceph_con_keepalive(&osd->o_con);
33523d14c5d2SYehuda Sadeh 	}
33533d14c5d2SYehuda Sadeh 
33545aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
3355fbca9635SIlya Dryomov 	schedule_delayed_work(&osdc->timeout_work,
3356fbca9635SIlya Dryomov 			      osdc->client->options->osd_keepalive_timeout);
33573d14c5d2SYehuda Sadeh }
33583d14c5d2SYehuda Sadeh 
33593d14c5d2SYehuda Sadeh static void handle_osds_timeout(struct work_struct *work)
33603d14c5d2SYehuda Sadeh {
33613d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc =
33623d14c5d2SYehuda Sadeh 		container_of(work, struct ceph_osd_client,
33633d14c5d2SYehuda Sadeh 			     osds_timeout_work.work);
3364a319bf56SIlya Dryomov 	unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
336542a2c09fSIlya Dryomov 	struct ceph_osd *osd, *nosd;
33663d14c5d2SYehuda Sadeh 
336742a2c09fSIlya Dryomov 	dout("%s osdc %p\n", __func__, osdc);
33685aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
336942a2c09fSIlya Dryomov 	list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
337042a2c09fSIlya Dryomov 		if (time_before(jiffies, osd->lru_ttl))
337142a2c09fSIlya Dryomov 			break;
337242a2c09fSIlya Dryomov 
33735aea3dcdSIlya Dryomov 		WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
3374922dab61SIlya Dryomov 		WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
33755aea3dcdSIlya Dryomov 		close_osd(osd);
337642a2c09fSIlya Dryomov 	}
337742a2c09fSIlya Dryomov 
33785aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
33793d14c5d2SYehuda Sadeh 	schedule_delayed_work(&osdc->osds_timeout_work,
33803d14c5d2SYehuda Sadeh 			      round_jiffies_relative(delay));
33813d14c5d2SYehuda Sadeh }
33823d14c5d2SYehuda Sadeh 
3383205ee118SIlya Dryomov static int ceph_oloc_decode(void **p, void *end,
3384205ee118SIlya Dryomov 			    struct ceph_object_locator *oloc)
3385205ee118SIlya Dryomov {
3386205ee118SIlya Dryomov 	u8 struct_v, struct_cv;
3387205ee118SIlya Dryomov 	u32 len;
3388205ee118SIlya Dryomov 	void *struct_end;
3389205ee118SIlya Dryomov 	int ret = 0;
3390205ee118SIlya Dryomov 
3391205ee118SIlya Dryomov 	ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3392205ee118SIlya Dryomov 	struct_v = ceph_decode_8(p);
3393205ee118SIlya Dryomov 	struct_cv = ceph_decode_8(p);
3394205ee118SIlya Dryomov 	if (struct_v < 3) {
3395205ee118SIlya Dryomov 		pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
3396205ee118SIlya Dryomov 			struct_v, struct_cv);
3397205ee118SIlya Dryomov 		goto e_inval;
3398205ee118SIlya Dryomov 	}
3399205ee118SIlya Dryomov 	if (struct_cv > 6) {
3400205ee118SIlya Dryomov 		pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
3401205ee118SIlya Dryomov 			struct_v, struct_cv);
3402205ee118SIlya Dryomov 		goto e_inval;
3403205ee118SIlya Dryomov 	}
3404205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3405205ee118SIlya Dryomov 	ceph_decode_need(p, end, len, e_inval);
3406205ee118SIlya Dryomov 	struct_end = *p + len;
3407205ee118SIlya Dryomov 
3408205ee118SIlya Dryomov 	oloc->pool = ceph_decode_64(p);
3409205ee118SIlya Dryomov 	*p += 4; /* skip preferred */
3410205ee118SIlya Dryomov 
3411205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3412205ee118SIlya Dryomov 	if (len > 0) {
3413205ee118SIlya Dryomov 		pr_warn("ceph_object_locator::key is set\n");
3414205ee118SIlya Dryomov 		goto e_inval;
3415205ee118SIlya Dryomov 	}
3416205ee118SIlya Dryomov 
3417205ee118SIlya Dryomov 	if (struct_v >= 5) {
3418cd08e0a2SYan, Zheng 		bool changed = false;
3419cd08e0a2SYan, Zheng 
3420205ee118SIlya Dryomov 		len = ceph_decode_32(p);
3421205ee118SIlya Dryomov 		if (len > 0) {
342230c156d9SYan, Zheng 			ceph_decode_need(p, end, len, e_inval);
3423cd08e0a2SYan, Zheng 			if (!oloc->pool_ns ||
3424cd08e0a2SYan, Zheng 			    ceph_compare_string(oloc->pool_ns, *p, len))
3425cd08e0a2SYan, Zheng 				changed = true;
342630c156d9SYan, Zheng 			*p += len;
3427cd08e0a2SYan, Zheng 		} else {
3428cd08e0a2SYan, Zheng 			if (oloc->pool_ns)
3429cd08e0a2SYan, Zheng 				changed = true;
3430cd08e0a2SYan, Zheng 		}
3431cd08e0a2SYan, Zheng 		if (changed) {
3432cd08e0a2SYan, Zheng 			/* redirect changes namespace */
3433cd08e0a2SYan, Zheng 			pr_warn("ceph_object_locator::nspace is changed\n");
3434cd08e0a2SYan, Zheng 			goto e_inval;
3435205ee118SIlya Dryomov 		}
3436205ee118SIlya Dryomov 	}
3437205ee118SIlya Dryomov 
3438205ee118SIlya Dryomov 	if (struct_v >= 6) {
3439205ee118SIlya Dryomov 		s64 hash = ceph_decode_64(p);
3440205ee118SIlya Dryomov 		if (hash != -1) {
3441205ee118SIlya Dryomov 			pr_warn("ceph_object_locator::hash is set\n");
3442205ee118SIlya Dryomov 			goto e_inval;
3443205ee118SIlya Dryomov 		}
3444205ee118SIlya Dryomov 	}
3445205ee118SIlya Dryomov 
3446205ee118SIlya Dryomov 	/* skip the rest */
3447205ee118SIlya Dryomov 	*p = struct_end;
3448205ee118SIlya Dryomov out:
3449205ee118SIlya Dryomov 	return ret;
3450205ee118SIlya Dryomov 
3451205ee118SIlya Dryomov e_inval:
3452205ee118SIlya Dryomov 	ret = -EINVAL;
3453205ee118SIlya Dryomov 	goto out;
3454205ee118SIlya Dryomov }
3455205ee118SIlya Dryomov 
3456205ee118SIlya Dryomov static int ceph_redirect_decode(void **p, void *end,
3457205ee118SIlya Dryomov 				struct ceph_request_redirect *redir)
3458205ee118SIlya Dryomov {
3459205ee118SIlya Dryomov 	u8 struct_v, struct_cv;
3460205ee118SIlya Dryomov 	u32 len;
3461205ee118SIlya Dryomov 	void *struct_end;
3462205ee118SIlya Dryomov 	int ret;
3463205ee118SIlya Dryomov 
3464205ee118SIlya Dryomov 	ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3465205ee118SIlya Dryomov 	struct_v = ceph_decode_8(p);
3466205ee118SIlya Dryomov 	struct_cv = ceph_decode_8(p);
3467205ee118SIlya Dryomov 	if (struct_cv > 1) {
3468205ee118SIlya Dryomov 		pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
3469205ee118SIlya Dryomov 			struct_v, struct_cv);
3470205ee118SIlya Dryomov 		goto e_inval;
3471205ee118SIlya Dryomov 	}
3472205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3473205ee118SIlya Dryomov 	ceph_decode_need(p, end, len, e_inval);
3474205ee118SIlya Dryomov 	struct_end = *p + len;
3475205ee118SIlya Dryomov 
3476205ee118SIlya Dryomov 	ret = ceph_oloc_decode(p, end, &redir->oloc);
3477205ee118SIlya Dryomov 	if (ret)
3478205ee118SIlya Dryomov 		goto out;
3479205ee118SIlya Dryomov 
3480205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3481205ee118SIlya Dryomov 	if (len > 0) {
3482205ee118SIlya Dryomov 		pr_warn("ceph_request_redirect::object_name is set\n");
3483205ee118SIlya Dryomov 		goto e_inval;
3484205ee118SIlya Dryomov 	}
3485205ee118SIlya Dryomov 
3486205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3487205ee118SIlya Dryomov 	*p += len; /* skip osd_instructions */
3488205ee118SIlya Dryomov 
3489205ee118SIlya Dryomov 	/* skip the rest */
3490205ee118SIlya Dryomov 	*p = struct_end;
3491205ee118SIlya Dryomov out:
3492205ee118SIlya Dryomov 	return ret;
3493205ee118SIlya Dryomov 
3494205ee118SIlya Dryomov e_inval:
3495205ee118SIlya Dryomov 	ret = -EINVAL;
3496205ee118SIlya Dryomov 	goto out;
3497205ee118SIlya Dryomov }
3498205ee118SIlya Dryomov 
3499fe5da05eSIlya Dryomov struct MOSDOpReply {
3500fe5da05eSIlya Dryomov 	struct ceph_pg pgid;
3501fe5da05eSIlya Dryomov 	u64 flags;
3502fe5da05eSIlya Dryomov 	int result;
3503fe5da05eSIlya Dryomov 	u32 epoch;
3504fe5da05eSIlya Dryomov 	int num_ops;
3505fe5da05eSIlya Dryomov 	u32 outdata_len[CEPH_OSD_MAX_OPS];
3506fe5da05eSIlya Dryomov 	s32 rval[CEPH_OSD_MAX_OPS];
3507fe5da05eSIlya Dryomov 	int retry_attempt;
3508fe5da05eSIlya Dryomov 	struct ceph_eversion replay_version;
3509fe5da05eSIlya Dryomov 	u64 user_version;
3510fe5da05eSIlya Dryomov 	struct ceph_request_redirect redirect;
3511fe5da05eSIlya Dryomov };
351225845472SSage Weil 
3513fe5da05eSIlya Dryomov static int decode_MOSDOpReply(const struct ceph_msg *msg, struct MOSDOpReply *m)
35143d14c5d2SYehuda Sadeh {
3515fe5da05eSIlya Dryomov 	void *p = msg->front.iov_base;
3516fe5da05eSIlya Dryomov 	void *const end = p + msg->front.iov_len;
3517fe5da05eSIlya Dryomov 	u16 version = le16_to_cpu(msg->hdr.version);
3518fe5da05eSIlya Dryomov 	struct ceph_eversion bad_replay_version;
3519b0b31a8fSIlya Dryomov 	u8 decode_redir;
3520fe5da05eSIlya Dryomov 	u32 len;
3521fe5da05eSIlya Dryomov 	int ret;
3522fe5da05eSIlya Dryomov 	int i;
35233d14c5d2SYehuda Sadeh 
3524fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, len, e_inval);
3525fe5da05eSIlya Dryomov 	ceph_decode_need(&p, end, len, e_inval);
3526fe5da05eSIlya Dryomov 	p += len; /* skip oid */
35271b83bef2SSage Weil 
3528fe5da05eSIlya Dryomov 	ret = ceph_decode_pgid(&p, end, &m->pgid);
3529fe5da05eSIlya Dryomov 	if (ret)
3530fe5da05eSIlya Dryomov 		return ret;
35311b83bef2SSage Weil 
3532fe5da05eSIlya Dryomov 	ceph_decode_64_safe(&p, end, m->flags, e_inval);
3533fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->result, e_inval);
3534fe5da05eSIlya Dryomov 	ceph_decode_need(&p, end, sizeof(bad_replay_version), e_inval);
3535fe5da05eSIlya Dryomov 	memcpy(&bad_replay_version, p, sizeof(bad_replay_version));
3536fe5da05eSIlya Dryomov 	p += sizeof(bad_replay_version);
3537fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->epoch, e_inval);
35381b83bef2SSage Weil 
3539fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->num_ops, e_inval);
3540fe5da05eSIlya Dryomov 	if (m->num_ops > ARRAY_SIZE(m->outdata_len))
3541fe5da05eSIlya Dryomov 		goto e_inval;
35421b83bef2SSage Weil 
3543fe5da05eSIlya Dryomov 	ceph_decode_need(&p, end, m->num_ops * sizeof(struct ceph_osd_op),
3544fe5da05eSIlya Dryomov 			 e_inval);
3545fe5da05eSIlya Dryomov 	for (i = 0; i < m->num_ops; i++) {
35461b83bef2SSage Weil 		struct ceph_osd_op *op = p;
35471b83bef2SSage Weil 
3548fe5da05eSIlya Dryomov 		m->outdata_len[i] = le32_to_cpu(op->payload_len);
35491b83bef2SSage Weil 		p += sizeof(*op);
35501b83bef2SSage Weil 	}
3551fe5da05eSIlya Dryomov 
3552fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->retry_attempt, e_inval);
3553fe5da05eSIlya Dryomov 	for (i = 0; i < m->num_ops; i++)
3554fe5da05eSIlya Dryomov 		ceph_decode_32_safe(&p, end, m->rval[i], e_inval);
3555fe5da05eSIlya Dryomov 
3556fe5da05eSIlya Dryomov 	if (version >= 5) {
3557fe5da05eSIlya Dryomov 		ceph_decode_need(&p, end, sizeof(m->replay_version), e_inval);
3558fe5da05eSIlya Dryomov 		memcpy(&m->replay_version, p, sizeof(m->replay_version));
3559fe5da05eSIlya Dryomov 		p += sizeof(m->replay_version);
3560fe5da05eSIlya Dryomov 		ceph_decode_64_safe(&p, end, m->user_version, e_inval);
3561fe5da05eSIlya Dryomov 	} else {
3562fe5da05eSIlya Dryomov 		m->replay_version = bad_replay_version; /* struct */
3563fe5da05eSIlya Dryomov 		m->user_version = le64_to_cpu(m->replay_version.version);
35641b83bef2SSage Weil 	}
35651b83bef2SSage Weil 
3566fe5da05eSIlya Dryomov 	if (version >= 6) {
3567fe5da05eSIlya Dryomov 		if (version >= 7)
3568fe5da05eSIlya Dryomov 			ceph_decode_8_safe(&p, end, decode_redir, e_inval);
3569b0b31a8fSIlya Dryomov 		else
3570b0b31a8fSIlya Dryomov 			decode_redir = 1;
3571b0b31a8fSIlya Dryomov 	} else {
3572b0b31a8fSIlya Dryomov 		decode_redir = 0;
3573b0b31a8fSIlya Dryomov 	}
3574b0b31a8fSIlya Dryomov 
3575b0b31a8fSIlya Dryomov 	if (decode_redir) {
3576fe5da05eSIlya Dryomov 		ret = ceph_redirect_decode(&p, end, &m->redirect);
3577fe5da05eSIlya Dryomov 		if (ret)
3578fe5da05eSIlya Dryomov 			return ret;
3579205ee118SIlya Dryomov 	} else {
3580fe5da05eSIlya Dryomov 		ceph_oloc_init(&m->redirect.oloc);
3581205ee118SIlya Dryomov 	}
3582205ee118SIlya Dryomov 
3583fe5da05eSIlya Dryomov 	return 0;
3584205ee118SIlya Dryomov 
3585fe5da05eSIlya Dryomov e_inval:
3586fe5da05eSIlya Dryomov 	return -EINVAL;
3587fe5da05eSIlya Dryomov }
3588fe5da05eSIlya Dryomov 
3589fe5da05eSIlya Dryomov /*
3590b18b9550SIlya Dryomov  * Handle MOSDOpReply.  Set ->r_result and call the callback if it is
3591b18b9550SIlya Dryomov  * specified.
3592fe5da05eSIlya Dryomov  */
35935aea3dcdSIlya Dryomov static void handle_reply(struct ceph_osd *osd, struct ceph_msg *msg)
3594fe5da05eSIlya Dryomov {
35955aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
3596fe5da05eSIlya Dryomov 	struct ceph_osd_request *req;
3597fe5da05eSIlya Dryomov 	struct MOSDOpReply m;
3598fe5da05eSIlya Dryomov 	u64 tid = le64_to_cpu(msg->hdr.tid);
3599fe5da05eSIlya Dryomov 	u32 data_len = 0;
3600fe5da05eSIlya Dryomov 	int ret;
3601fe5da05eSIlya Dryomov 	int i;
3602fe5da05eSIlya Dryomov 
3603fe5da05eSIlya Dryomov 	dout("%s msg %p tid %llu\n", __func__, msg, tid);
3604fe5da05eSIlya Dryomov 
36055aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
36065aea3dcdSIlya Dryomov 	if (!osd_registered(osd)) {
36075aea3dcdSIlya Dryomov 		dout("%s osd%d unknown\n", __func__, osd->o_osd);
36085aea3dcdSIlya Dryomov 		goto out_unlock_osdc;
3609fe5da05eSIlya Dryomov 	}
36105aea3dcdSIlya Dryomov 	WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
36115aea3dcdSIlya Dryomov 
36125aea3dcdSIlya Dryomov 	mutex_lock(&osd->lock);
36135aea3dcdSIlya Dryomov 	req = lookup_request(&osd->o_requests, tid);
36145aea3dcdSIlya Dryomov 	if (!req) {
36155aea3dcdSIlya Dryomov 		dout("%s osd%d tid %llu unknown\n", __func__, osd->o_osd, tid);
36165aea3dcdSIlya Dryomov 		goto out_unlock_session;
36175aea3dcdSIlya Dryomov 	}
3618fe5da05eSIlya Dryomov 
3619cd08e0a2SYan, Zheng 	m.redirect.oloc.pool_ns = req->r_t.target_oloc.pool_ns;
3620fe5da05eSIlya Dryomov 	ret = decode_MOSDOpReply(msg, &m);
3621cd08e0a2SYan, Zheng 	m.redirect.oloc.pool_ns = NULL;
3622fe5da05eSIlya Dryomov 	if (ret) {
3623fe5da05eSIlya Dryomov 		pr_err("failed to decode MOSDOpReply for tid %llu: %d\n",
3624fe5da05eSIlya Dryomov 		       req->r_tid, ret);
3625fe5da05eSIlya Dryomov 		ceph_msg_dump(msg);
3626fe5da05eSIlya Dryomov 		goto fail_request;
3627fe5da05eSIlya Dryomov 	}
3628fe5da05eSIlya Dryomov 	dout("%s req %p tid %llu flags 0x%llx pgid %llu.%x epoch %u attempt %d v %u'%llu uv %llu\n",
3629fe5da05eSIlya Dryomov 	     __func__, req, req->r_tid, m.flags, m.pgid.pool, m.pgid.seed,
3630fe5da05eSIlya Dryomov 	     m.epoch, m.retry_attempt, le32_to_cpu(m.replay_version.epoch),
3631fe5da05eSIlya Dryomov 	     le64_to_cpu(m.replay_version.version), m.user_version);
3632fe5da05eSIlya Dryomov 
3633fe5da05eSIlya Dryomov 	if (m.retry_attempt >= 0) {
3634fe5da05eSIlya Dryomov 		if (m.retry_attempt != req->r_attempts - 1) {
3635fe5da05eSIlya Dryomov 			dout("req %p tid %llu retry_attempt %d != %d, ignoring\n",
3636fe5da05eSIlya Dryomov 			     req, req->r_tid, m.retry_attempt,
3637fe5da05eSIlya Dryomov 			     req->r_attempts - 1);
36385aea3dcdSIlya Dryomov 			goto out_unlock_session;
3639fe5da05eSIlya Dryomov 		}
3640fe5da05eSIlya Dryomov 	} else {
3641fe5da05eSIlya Dryomov 		WARN_ON(1); /* MOSDOpReply v4 is assumed */
3642fe5da05eSIlya Dryomov 	}
3643fe5da05eSIlya Dryomov 
3644fe5da05eSIlya Dryomov 	if (!ceph_oloc_empty(&m.redirect.oloc)) {
3645fe5da05eSIlya Dryomov 		dout("req %p tid %llu redirect pool %lld\n", req, req->r_tid,
3646fe5da05eSIlya Dryomov 		     m.redirect.oloc.pool);
36475aea3dcdSIlya Dryomov 		unlink_request(osd, req);
36485aea3dcdSIlya Dryomov 		mutex_unlock(&osd->lock);
3649205ee118SIlya Dryomov 
365030c156d9SYan, Zheng 		/*
365130c156d9SYan, Zheng 		 * Not ceph_oloc_copy() - changing pool_ns is not
365230c156d9SYan, Zheng 		 * supported.
365330c156d9SYan, Zheng 		 */
365430c156d9SYan, Zheng 		req->r_t.target_oloc.pool = m.redirect.oloc.pool;
36555aea3dcdSIlya Dryomov 		req->r_flags |= CEPH_OSD_FLAG_REDIRECTED;
36565aea3dcdSIlya Dryomov 		req->r_tid = 0;
36575aea3dcdSIlya Dryomov 		__submit_request(req, false);
36585aea3dcdSIlya Dryomov 		goto out_unlock_osdc;
3659205ee118SIlya Dryomov 	}
3660205ee118SIlya Dryomov 
3661fe5da05eSIlya Dryomov 	if (m.num_ops != req->r_num_ops) {
3662fe5da05eSIlya Dryomov 		pr_err("num_ops %d != %d for tid %llu\n", m.num_ops,
3663fe5da05eSIlya Dryomov 		       req->r_num_ops, req->r_tid);
3664fe5da05eSIlya Dryomov 		goto fail_request;
3665fe5da05eSIlya Dryomov 	}
3666fe5da05eSIlya Dryomov 	for (i = 0; i < req->r_num_ops; i++) {
3667fe5da05eSIlya Dryomov 		dout(" req %p tid %llu op %d rval %d len %u\n", req,
3668fe5da05eSIlya Dryomov 		     req->r_tid, i, m.rval[i], m.outdata_len[i]);
3669fe5da05eSIlya Dryomov 		req->r_ops[i].rval = m.rval[i];
3670fe5da05eSIlya Dryomov 		req->r_ops[i].outdata_len = m.outdata_len[i];
3671fe5da05eSIlya Dryomov 		data_len += m.outdata_len[i];
3672fe5da05eSIlya Dryomov 	}
3673fe5da05eSIlya Dryomov 	if (data_len != le32_to_cpu(msg->hdr.data_len)) {
3674fe5da05eSIlya Dryomov 		pr_err("sum of lens %u != %u for tid %llu\n", data_len,
3675fe5da05eSIlya Dryomov 		       le32_to_cpu(msg->hdr.data_len), req->r_tid);
3676fe5da05eSIlya Dryomov 		goto fail_request;
3677fe5da05eSIlya Dryomov 	}
3678b18b9550SIlya Dryomov 	dout("%s req %p tid %llu result %d data_len %u\n", __func__,
3679b18b9550SIlya Dryomov 	     req, req->r_tid, m.result, data_len);
36803d14c5d2SYehuda Sadeh 
3681b18b9550SIlya Dryomov 	/*
3682b18b9550SIlya Dryomov 	 * Since we only ever request ONDISK, we should only ever get
3683b18b9550SIlya Dryomov 	 * one (type of) reply back.
3684b18b9550SIlya Dryomov 	 */
3685b18b9550SIlya Dryomov 	WARN_ON(!(m.flags & CEPH_OSD_FLAG_ONDISK));
3686fe5da05eSIlya Dryomov 	req->r_result = m.result ?: data_len;
368745ee2c1dSIlya Dryomov 	finish_request(req);
36885aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
36895aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
36903d14c5d2SYehuda Sadeh 
3691fe5da05eSIlya Dryomov 	__complete_request(req);
36923d14c5d2SYehuda Sadeh 	return;
3693fe5da05eSIlya Dryomov 
3694fe5da05eSIlya Dryomov fail_request:
36954609245eSIlya Dryomov 	complete_request(req, -EIO);
36965aea3dcdSIlya Dryomov out_unlock_session:
36975aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
36985aea3dcdSIlya Dryomov out_unlock_osdc:
36995aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
37003d14c5d2SYehuda Sadeh }
37013d14c5d2SYehuda Sadeh 
370242c1b124SIlya Dryomov static void set_pool_was_full(struct ceph_osd_client *osdc)
370342c1b124SIlya Dryomov {
370442c1b124SIlya Dryomov 	struct rb_node *n;
370542c1b124SIlya Dryomov 
370642c1b124SIlya Dryomov 	for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
370742c1b124SIlya Dryomov 		struct ceph_pg_pool_info *pi =
370842c1b124SIlya Dryomov 		    rb_entry(n, struct ceph_pg_pool_info, node);
370942c1b124SIlya Dryomov 
371042c1b124SIlya Dryomov 		pi->was_full = __pool_full(pi);
371142c1b124SIlya Dryomov 	}
371242c1b124SIlya Dryomov }
371342c1b124SIlya Dryomov 
37145aea3dcdSIlya Dryomov static bool pool_cleared_full(struct ceph_osd_client *osdc, s64 pool_id)
37153d14c5d2SYehuda Sadeh {
37165aea3dcdSIlya Dryomov 	struct ceph_pg_pool_info *pi;
37173d14c5d2SYehuda Sadeh 
37185aea3dcdSIlya Dryomov 	pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
37195aea3dcdSIlya Dryomov 	if (!pi)
37205aea3dcdSIlya Dryomov 		return false;
37213d14c5d2SYehuda Sadeh 
37225aea3dcdSIlya Dryomov 	return pi->was_full && !__pool_full(pi);
37233d14c5d2SYehuda Sadeh }
37243d14c5d2SYehuda Sadeh 
3725922dab61SIlya Dryomov static enum calc_target_result
3726922dab61SIlya Dryomov recalc_linger_target(struct ceph_osd_linger_request *lreq)
3727922dab61SIlya Dryomov {
3728922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
3729922dab61SIlya Dryomov 	enum calc_target_result ct_res;
3730922dab61SIlya Dryomov 
37318edf84baSIlya Dryomov 	ct_res = calc_target(osdc, &lreq->t, true);
3732922dab61SIlya Dryomov 	if (ct_res == CALC_TARGET_NEED_RESEND) {
3733922dab61SIlya Dryomov 		struct ceph_osd *osd;
3734922dab61SIlya Dryomov 
3735922dab61SIlya Dryomov 		osd = lookup_create_osd(osdc, lreq->t.osd, true);
3736922dab61SIlya Dryomov 		if (osd != lreq->osd) {
3737922dab61SIlya Dryomov 			unlink_linger(lreq->osd, lreq);
3738922dab61SIlya Dryomov 			link_linger(osd, lreq);
3739922dab61SIlya Dryomov 		}
3740922dab61SIlya Dryomov 	}
3741922dab61SIlya Dryomov 
3742922dab61SIlya Dryomov 	return ct_res;
3743922dab61SIlya Dryomov }
3744922dab61SIlya Dryomov 
37453d14c5d2SYehuda Sadeh /*
37465aea3dcdSIlya Dryomov  * Requeue requests whose mapping to an OSD has changed.
37473d14c5d2SYehuda Sadeh  */
37485aea3dcdSIlya Dryomov static void scan_requests(struct ceph_osd *osd,
37495aea3dcdSIlya Dryomov 			  bool force_resend,
37505aea3dcdSIlya Dryomov 			  bool cleared_full,
37515aea3dcdSIlya Dryomov 			  bool check_pool_cleared_full,
37525aea3dcdSIlya Dryomov 			  struct rb_root *need_resend,
37535aea3dcdSIlya Dryomov 			  struct list_head *need_resend_linger)
37543d14c5d2SYehuda Sadeh {
37555aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
37565aea3dcdSIlya Dryomov 	struct rb_node *n;
37575aea3dcdSIlya Dryomov 	bool force_resend_writes;
37583d14c5d2SYehuda Sadeh 
3759922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_linger_requests); n; ) {
3760922dab61SIlya Dryomov 		struct ceph_osd_linger_request *lreq =
3761922dab61SIlya Dryomov 		    rb_entry(n, struct ceph_osd_linger_request, node);
3762922dab61SIlya Dryomov 		enum calc_target_result ct_res;
3763922dab61SIlya Dryomov 
3764922dab61SIlya Dryomov 		n = rb_next(n); /* recalc_linger_target() */
3765922dab61SIlya Dryomov 
3766922dab61SIlya Dryomov 		dout("%s lreq %p linger_id %llu\n", __func__, lreq,
3767922dab61SIlya Dryomov 		     lreq->linger_id);
3768922dab61SIlya Dryomov 		ct_res = recalc_linger_target(lreq);
3769922dab61SIlya Dryomov 		switch (ct_res) {
3770922dab61SIlya Dryomov 		case CALC_TARGET_NO_ACTION:
3771922dab61SIlya Dryomov 			force_resend_writes = cleared_full ||
3772922dab61SIlya Dryomov 			    (check_pool_cleared_full &&
3773922dab61SIlya Dryomov 			     pool_cleared_full(osdc, lreq->t.base_oloc.pool));
3774922dab61SIlya Dryomov 			if (!force_resend && !force_resend_writes)
3775922dab61SIlya Dryomov 				break;
3776922dab61SIlya Dryomov 
3777922dab61SIlya Dryomov 			/* fall through */
3778922dab61SIlya Dryomov 		case CALC_TARGET_NEED_RESEND:
37794609245eSIlya Dryomov 			cancel_linger_map_check(lreq);
3780922dab61SIlya Dryomov 			/*
3781922dab61SIlya Dryomov 			 * scan_requests() for the previous epoch(s)
3782922dab61SIlya Dryomov 			 * may have already added it to the list, since
3783922dab61SIlya Dryomov 			 * it's not unlinked here.
3784922dab61SIlya Dryomov 			 */
3785922dab61SIlya Dryomov 			if (list_empty(&lreq->scan_item))
3786922dab61SIlya Dryomov 				list_add_tail(&lreq->scan_item, need_resend_linger);
3787922dab61SIlya Dryomov 			break;
3788922dab61SIlya Dryomov 		case CALC_TARGET_POOL_DNE:
3789a10bcb19SIlya Dryomov 			list_del_init(&lreq->scan_item);
37904609245eSIlya Dryomov 			check_linger_pool_dne(lreq);
3791922dab61SIlya Dryomov 			break;
3792922dab61SIlya Dryomov 		}
3793922dab61SIlya Dryomov 	}
3794922dab61SIlya Dryomov 
37955aea3dcdSIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; ) {
37965aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
37975aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
37985aea3dcdSIlya Dryomov 		enum calc_target_result ct_res;
3799ab60b16dSAlex Elder 
38004609245eSIlya Dryomov 		n = rb_next(n); /* unlink_request(), check_pool_dne() */
3801ab60b16dSAlex Elder 
38025aea3dcdSIlya Dryomov 		dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
38038edf84baSIlya Dryomov 		ct_res = calc_target(osdc, &req->r_t, false);
38045aea3dcdSIlya Dryomov 		switch (ct_res) {
38055aea3dcdSIlya Dryomov 		case CALC_TARGET_NO_ACTION:
38065aea3dcdSIlya Dryomov 			force_resend_writes = cleared_full ||
38075aea3dcdSIlya Dryomov 			    (check_pool_cleared_full &&
38085aea3dcdSIlya Dryomov 			     pool_cleared_full(osdc, req->r_t.base_oloc.pool));
38095aea3dcdSIlya Dryomov 			if (!force_resend &&
38105aea3dcdSIlya Dryomov 			    (!(req->r_flags & CEPH_OSD_FLAG_WRITE) ||
38115aea3dcdSIlya Dryomov 			     !force_resend_writes))
38125aea3dcdSIlya Dryomov 				break;
3813a40c4f10SYehuda Sadeh 
38145aea3dcdSIlya Dryomov 			/* fall through */
38155aea3dcdSIlya Dryomov 		case CALC_TARGET_NEED_RESEND:
38164609245eSIlya Dryomov 			cancel_map_check(req);
38175aea3dcdSIlya Dryomov 			unlink_request(osd, req);
38185aea3dcdSIlya Dryomov 			insert_request(need_resend, req);
38195aea3dcdSIlya Dryomov 			break;
38205aea3dcdSIlya Dryomov 		case CALC_TARGET_POOL_DNE:
38214609245eSIlya Dryomov 			check_pool_dne(req);
38225aea3dcdSIlya Dryomov 			break;
3823a40c4f10SYehuda Sadeh 		}
38243d14c5d2SYehuda Sadeh 	}
38253d14c5d2SYehuda Sadeh }
38266f6c7006SSage Weil 
382742c1b124SIlya Dryomov static int handle_one_map(struct ceph_osd_client *osdc,
38285aea3dcdSIlya Dryomov 			  void *p, void *end, bool incremental,
38295aea3dcdSIlya Dryomov 			  struct rb_root *need_resend,
38305aea3dcdSIlya Dryomov 			  struct list_head *need_resend_linger)
383142c1b124SIlya Dryomov {
383242c1b124SIlya Dryomov 	struct ceph_osdmap *newmap;
383342c1b124SIlya Dryomov 	struct rb_node *n;
383442c1b124SIlya Dryomov 	bool skipped_map = false;
383542c1b124SIlya Dryomov 	bool was_full;
383642c1b124SIlya Dryomov 
3837b7ec35b3SIlya Dryomov 	was_full = ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
383842c1b124SIlya Dryomov 	set_pool_was_full(osdc);
383942c1b124SIlya Dryomov 
384042c1b124SIlya Dryomov 	if (incremental)
384142c1b124SIlya Dryomov 		newmap = osdmap_apply_incremental(&p, end, osdc->osdmap);
384242c1b124SIlya Dryomov 	else
384342c1b124SIlya Dryomov 		newmap = ceph_osdmap_decode(&p, end);
384442c1b124SIlya Dryomov 	if (IS_ERR(newmap))
384542c1b124SIlya Dryomov 		return PTR_ERR(newmap);
384642c1b124SIlya Dryomov 
384742c1b124SIlya Dryomov 	if (newmap != osdc->osdmap) {
384842c1b124SIlya Dryomov 		/*
384942c1b124SIlya Dryomov 		 * Preserve ->was_full before destroying the old map.
385042c1b124SIlya Dryomov 		 * For pools that weren't in the old map, ->was_full
385142c1b124SIlya Dryomov 		 * should be false.
385242c1b124SIlya Dryomov 		 */
385342c1b124SIlya Dryomov 		for (n = rb_first(&newmap->pg_pools); n; n = rb_next(n)) {
385442c1b124SIlya Dryomov 			struct ceph_pg_pool_info *pi =
385542c1b124SIlya Dryomov 			    rb_entry(n, struct ceph_pg_pool_info, node);
385642c1b124SIlya Dryomov 			struct ceph_pg_pool_info *old_pi;
385742c1b124SIlya Dryomov 
385842c1b124SIlya Dryomov 			old_pi = ceph_pg_pool_by_id(osdc->osdmap, pi->id);
385942c1b124SIlya Dryomov 			if (old_pi)
386042c1b124SIlya Dryomov 				pi->was_full = old_pi->was_full;
386142c1b124SIlya Dryomov 			else
386242c1b124SIlya Dryomov 				WARN_ON(pi->was_full);
386342c1b124SIlya Dryomov 		}
386442c1b124SIlya Dryomov 
386542c1b124SIlya Dryomov 		if (osdc->osdmap->epoch &&
386642c1b124SIlya Dryomov 		    osdc->osdmap->epoch + 1 < newmap->epoch) {
386742c1b124SIlya Dryomov 			WARN_ON(incremental);
386842c1b124SIlya Dryomov 			skipped_map = true;
386942c1b124SIlya Dryomov 		}
387042c1b124SIlya Dryomov 
387142c1b124SIlya Dryomov 		ceph_osdmap_destroy(osdc->osdmap);
387242c1b124SIlya Dryomov 		osdc->osdmap = newmap;
387342c1b124SIlya Dryomov 	}
387442c1b124SIlya Dryomov 
3875b7ec35b3SIlya Dryomov 	was_full &= !ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
38765aea3dcdSIlya Dryomov 	scan_requests(&osdc->homeless_osd, skipped_map, was_full, true,
38775aea3dcdSIlya Dryomov 		      need_resend, need_resend_linger);
38785aea3dcdSIlya Dryomov 
38795aea3dcdSIlya Dryomov 	for (n = rb_first(&osdc->osds); n; ) {
38805aea3dcdSIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
38815aea3dcdSIlya Dryomov 
38825aea3dcdSIlya Dryomov 		n = rb_next(n); /* close_osd() */
38835aea3dcdSIlya Dryomov 
38845aea3dcdSIlya Dryomov 		scan_requests(osd, skipped_map, was_full, true, need_resend,
38855aea3dcdSIlya Dryomov 			      need_resend_linger);
38865aea3dcdSIlya Dryomov 		if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
38875aea3dcdSIlya Dryomov 		    memcmp(&osd->o_con.peer_addr,
38885aea3dcdSIlya Dryomov 			   ceph_osd_addr(osdc->osdmap, osd->o_osd),
38895aea3dcdSIlya Dryomov 			   sizeof(struct ceph_entity_addr)))
38905aea3dcdSIlya Dryomov 			close_osd(osd);
38915aea3dcdSIlya Dryomov 	}
389242c1b124SIlya Dryomov 
389342c1b124SIlya Dryomov 	return 0;
389442c1b124SIlya Dryomov }
38956f6c7006SSage Weil 
38965aea3dcdSIlya Dryomov static void kick_requests(struct ceph_osd_client *osdc,
38975aea3dcdSIlya Dryomov 			  struct rb_root *need_resend,
38985aea3dcdSIlya Dryomov 			  struct list_head *need_resend_linger)
38995aea3dcdSIlya Dryomov {
3900922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq, *nlreq;
390104c7d789SIlya Dryomov 	enum calc_target_result ct_res;
39025aea3dcdSIlya Dryomov 	struct rb_node *n;
39035aea3dcdSIlya Dryomov 
390404c7d789SIlya Dryomov 	/* make sure need_resend targets reflect latest map */
390504c7d789SIlya Dryomov 	for (n = rb_first(need_resend); n; ) {
390604c7d789SIlya Dryomov 		struct ceph_osd_request *req =
390704c7d789SIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
390804c7d789SIlya Dryomov 
390904c7d789SIlya Dryomov 		n = rb_next(n);
391004c7d789SIlya Dryomov 
391104c7d789SIlya Dryomov 		if (req->r_t.epoch < osdc->osdmap->epoch) {
39128edf84baSIlya Dryomov 			ct_res = calc_target(osdc, &req->r_t, false);
391304c7d789SIlya Dryomov 			if (ct_res == CALC_TARGET_POOL_DNE) {
391404c7d789SIlya Dryomov 				erase_request(need_resend, req);
391504c7d789SIlya Dryomov 				check_pool_dne(req);
391604c7d789SIlya Dryomov 			}
391704c7d789SIlya Dryomov 		}
391804c7d789SIlya Dryomov 	}
391904c7d789SIlya Dryomov 
39205aea3dcdSIlya Dryomov 	for (n = rb_first(need_resend); n; ) {
39215aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
39225aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
39235aea3dcdSIlya Dryomov 		struct ceph_osd *osd;
39245aea3dcdSIlya Dryomov 
39255aea3dcdSIlya Dryomov 		n = rb_next(n);
39265aea3dcdSIlya Dryomov 		erase_request(need_resend, req); /* before link_request() */
39275aea3dcdSIlya Dryomov 
39285aea3dcdSIlya Dryomov 		osd = lookup_create_osd(osdc, req->r_t.osd, true);
39295aea3dcdSIlya Dryomov 		link_request(osd, req);
39305aea3dcdSIlya Dryomov 		if (!req->r_linger) {
39315aea3dcdSIlya Dryomov 			if (!osd_homeless(osd) && !req->r_t.paused)
39325aea3dcdSIlya Dryomov 				send_request(req);
3933922dab61SIlya Dryomov 		} else {
3934922dab61SIlya Dryomov 			cancel_linger_request(req);
39355aea3dcdSIlya Dryomov 		}
39365aea3dcdSIlya Dryomov 	}
3937922dab61SIlya Dryomov 
3938922dab61SIlya Dryomov 	list_for_each_entry_safe(lreq, nlreq, need_resend_linger, scan_item) {
3939922dab61SIlya Dryomov 		if (!osd_homeless(lreq->osd))
3940922dab61SIlya Dryomov 			send_linger(lreq);
3941922dab61SIlya Dryomov 
3942922dab61SIlya Dryomov 		list_del_init(&lreq->scan_item);
3943922dab61SIlya Dryomov 	}
39445aea3dcdSIlya Dryomov }
39455aea3dcdSIlya Dryomov 
39463d14c5d2SYehuda Sadeh /*
39473d14c5d2SYehuda Sadeh  * Process updated osd map.
39483d14c5d2SYehuda Sadeh  *
39493d14c5d2SYehuda Sadeh  * The message contains any number of incremental and full maps, normally
39503d14c5d2SYehuda Sadeh  * indicating some sort of topology change in the cluster.  Kick requests
39513d14c5d2SYehuda Sadeh  * off to different OSDs as needed.
39523d14c5d2SYehuda Sadeh  */
39533d14c5d2SYehuda Sadeh void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
39543d14c5d2SYehuda Sadeh {
395542c1b124SIlya Dryomov 	void *p = msg->front.iov_base;
395642c1b124SIlya Dryomov 	void *const end = p + msg->front.iov_len;
39573d14c5d2SYehuda Sadeh 	u32 nr_maps, maplen;
39583d14c5d2SYehuda Sadeh 	u32 epoch;
39593d14c5d2SYehuda Sadeh 	struct ceph_fsid fsid;
39605aea3dcdSIlya Dryomov 	struct rb_root need_resend = RB_ROOT;
39615aea3dcdSIlya Dryomov 	LIST_HEAD(need_resend_linger);
396242c1b124SIlya Dryomov 	bool handled_incremental = false;
396342c1b124SIlya Dryomov 	bool was_pauserd, was_pausewr;
396442c1b124SIlya Dryomov 	bool pauserd, pausewr;
396542c1b124SIlya Dryomov 	int err;
39663d14c5d2SYehuda Sadeh 
396742c1b124SIlya Dryomov 	dout("%s have %u\n", __func__, osdc->osdmap->epoch);
39685aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
39693d14c5d2SYehuda Sadeh 
39703d14c5d2SYehuda Sadeh 	/* verify fsid */
39713d14c5d2SYehuda Sadeh 	ceph_decode_need(&p, end, sizeof(fsid), bad);
39723d14c5d2SYehuda Sadeh 	ceph_decode_copy(&p, &fsid, sizeof(fsid));
39733d14c5d2SYehuda Sadeh 	if (ceph_check_fsid(osdc->client, &fsid) < 0)
397442c1b124SIlya Dryomov 		goto bad;
39753d14c5d2SYehuda Sadeh 
3976b7ec35b3SIlya Dryomov 	was_pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3977b7ec35b3SIlya Dryomov 	was_pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3978b7ec35b3SIlya Dryomov 		      ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
397942c1b124SIlya Dryomov 		      have_pool_full(osdc);
39809a1ea2dbSJosh Durgin 
39813d14c5d2SYehuda Sadeh 	/* incremental maps */
39823d14c5d2SYehuda Sadeh 	ceph_decode_32_safe(&p, end, nr_maps, bad);
39833d14c5d2SYehuda Sadeh 	dout(" %d inc maps\n", nr_maps);
39843d14c5d2SYehuda Sadeh 	while (nr_maps > 0) {
39853d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, 2*sizeof(u32), bad);
39863d14c5d2SYehuda Sadeh 		epoch = ceph_decode_32(&p);
39873d14c5d2SYehuda Sadeh 		maplen = ceph_decode_32(&p);
39883d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, maplen, bad);
398942c1b124SIlya Dryomov 		if (osdc->osdmap->epoch &&
399042c1b124SIlya Dryomov 		    osdc->osdmap->epoch + 1 == epoch) {
39913d14c5d2SYehuda Sadeh 			dout("applying incremental map %u len %d\n",
39923d14c5d2SYehuda Sadeh 			     epoch, maplen);
39935aea3dcdSIlya Dryomov 			err = handle_one_map(osdc, p, p + maplen, true,
39945aea3dcdSIlya Dryomov 					     &need_resend, &need_resend_linger);
399542c1b124SIlya Dryomov 			if (err)
39963d14c5d2SYehuda Sadeh 				goto bad;
399742c1b124SIlya Dryomov 			handled_incremental = true;
39983d14c5d2SYehuda Sadeh 		} else {
39993d14c5d2SYehuda Sadeh 			dout("ignoring incremental map %u len %d\n",
40003d14c5d2SYehuda Sadeh 			     epoch, maplen);
40013d14c5d2SYehuda Sadeh 		}
400242c1b124SIlya Dryomov 		p += maplen;
40033d14c5d2SYehuda Sadeh 		nr_maps--;
40043d14c5d2SYehuda Sadeh 	}
400542c1b124SIlya Dryomov 	if (handled_incremental)
40063d14c5d2SYehuda Sadeh 		goto done;
40073d14c5d2SYehuda Sadeh 
40083d14c5d2SYehuda Sadeh 	/* full maps */
40093d14c5d2SYehuda Sadeh 	ceph_decode_32_safe(&p, end, nr_maps, bad);
40103d14c5d2SYehuda Sadeh 	dout(" %d full maps\n", nr_maps);
40113d14c5d2SYehuda Sadeh 	while (nr_maps) {
40123d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, 2*sizeof(u32), bad);
40133d14c5d2SYehuda Sadeh 		epoch = ceph_decode_32(&p);
40143d14c5d2SYehuda Sadeh 		maplen = ceph_decode_32(&p);
40153d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, maplen, bad);
40163d14c5d2SYehuda Sadeh 		if (nr_maps > 1) {
40173d14c5d2SYehuda Sadeh 			dout("skipping non-latest full map %u len %d\n",
40183d14c5d2SYehuda Sadeh 			     epoch, maplen);
4019e5253a7bSIlya Dryomov 		} else if (osdc->osdmap->epoch >= epoch) {
40203d14c5d2SYehuda Sadeh 			dout("skipping full map %u len %d, "
40213d14c5d2SYehuda Sadeh 			     "older than our %u\n", epoch, maplen,
40223d14c5d2SYehuda Sadeh 			     osdc->osdmap->epoch);
40233d14c5d2SYehuda Sadeh 		} else {
40243d14c5d2SYehuda Sadeh 			dout("taking full map %u len %d\n", epoch, maplen);
40255aea3dcdSIlya Dryomov 			err = handle_one_map(osdc, p, p + maplen, false,
40265aea3dcdSIlya Dryomov 					     &need_resend, &need_resend_linger);
402742c1b124SIlya Dryomov 			if (err)
40283d14c5d2SYehuda Sadeh 				goto bad;
40293d14c5d2SYehuda Sadeh 		}
40303d14c5d2SYehuda Sadeh 		p += maplen;
40313d14c5d2SYehuda Sadeh 		nr_maps--;
40323d14c5d2SYehuda Sadeh 	}
40333d14c5d2SYehuda Sadeh 
40343d14c5d2SYehuda Sadeh done:
4035cd634fb6SSage Weil 	/*
4036cd634fb6SSage Weil 	 * subscribe to subsequent osdmap updates if full to ensure
4037cd634fb6SSage Weil 	 * we find out when we are no longer full and stop returning
4038cd634fb6SSage Weil 	 * ENOSPC.
4039cd634fb6SSage Weil 	 */
4040b7ec35b3SIlya Dryomov 	pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
4041b7ec35b3SIlya Dryomov 	pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
4042b7ec35b3SIlya Dryomov 		  ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
404342c1b124SIlya Dryomov 		  have_pool_full(osdc);
404458eb7932SJeff Layton 	if (was_pauserd || was_pausewr || pauserd || pausewr ||
404558eb7932SJeff Layton 	    osdc->osdmap->epoch < osdc->epoch_barrier)
404642c1b124SIlya Dryomov 		maybe_request_map(osdc);
4047cd634fb6SSage Weil 
40485aea3dcdSIlya Dryomov 	kick_requests(osdc, &need_resend, &need_resend_linger);
404942c1b124SIlya Dryomov 
4050fc36d0a4SJeff Layton 	ceph_osdc_abort_on_full(osdc);
405142c1b124SIlya Dryomov 	ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
405242c1b124SIlya Dryomov 			  osdc->osdmap->epoch);
40535aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
40543d14c5d2SYehuda Sadeh 	wake_up_all(&osdc->client->auth_wq);
40553d14c5d2SYehuda Sadeh 	return;
40563d14c5d2SYehuda Sadeh 
40573d14c5d2SYehuda Sadeh bad:
40583d14c5d2SYehuda Sadeh 	pr_err("osdc handle_map corrupt msg\n");
40593d14c5d2SYehuda Sadeh 	ceph_msg_dump(msg);
40605aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
40615aea3dcdSIlya Dryomov }
40625aea3dcdSIlya Dryomov 
40635aea3dcdSIlya Dryomov /*
40645aea3dcdSIlya Dryomov  * Resubmit requests pending on the given osd.
40655aea3dcdSIlya Dryomov  */
40665aea3dcdSIlya Dryomov static void kick_osd_requests(struct ceph_osd *osd)
40675aea3dcdSIlya Dryomov {
40685aea3dcdSIlya Dryomov 	struct rb_node *n;
40695aea3dcdSIlya Dryomov 
4070a02a946dSIlya Dryomov 	clear_backoffs(osd);
4071a02a946dSIlya Dryomov 
4072922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; ) {
40735aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
40745aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
40755aea3dcdSIlya Dryomov 
4076922dab61SIlya Dryomov 		n = rb_next(n); /* cancel_linger_request() */
4077922dab61SIlya Dryomov 
40785aea3dcdSIlya Dryomov 		if (!req->r_linger) {
40795aea3dcdSIlya Dryomov 			if (!req->r_t.paused)
40805aea3dcdSIlya Dryomov 				send_request(req);
4081922dab61SIlya Dryomov 		} else {
4082922dab61SIlya Dryomov 			cancel_linger_request(req);
40835aea3dcdSIlya Dryomov 		}
40845aea3dcdSIlya Dryomov 	}
4085922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_linger_requests); n; n = rb_next(n)) {
4086922dab61SIlya Dryomov 		struct ceph_osd_linger_request *lreq =
4087922dab61SIlya Dryomov 		    rb_entry(n, struct ceph_osd_linger_request, node);
4088922dab61SIlya Dryomov 
4089922dab61SIlya Dryomov 		send_linger(lreq);
4090922dab61SIlya Dryomov 	}
40915aea3dcdSIlya Dryomov }
40925aea3dcdSIlya Dryomov 
40935aea3dcdSIlya Dryomov /*
40945aea3dcdSIlya Dryomov  * If the osd connection drops, we need to resubmit all requests.
40955aea3dcdSIlya Dryomov  */
40965aea3dcdSIlya Dryomov static void osd_fault(struct ceph_connection *con)
40975aea3dcdSIlya Dryomov {
40985aea3dcdSIlya Dryomov 	struct ceph_osd *osd = con->private;
40995aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
41005aea3dcdSIlya Dryomov 
41015aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
41025aea3dcdSIlya Dryomov 
41035aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
41045aea3dcdSIlya Dryomov 	if (!osd_registered(osd)) {
41055aea3dcdSIlya Dryomov 		dout("%s osd%d unknown\n", __func__, osd->o_osd);
41065aea3dcdSIlya Dryomov 		goto out_unlock;
41075aea3dcdSIlya Dryomov 	}
41085aea3dcdSIlya Dryomov 
41095aea3dcdSIlya Dryomov 	if (!reopen_osd(osd))
41105aea3dcdSIlya Dryomov 		kick_osd_requests(osd);
41115aea3dcdSIlya Dryomov 	maybe_request_map(osdc);
41125aea3dcdSIlya Dryomov 
41135aea3dcdSIlya Dryomov out_unlock:
41145aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
41153d14c5d2SYehuda Sadeh }
41163d14c5d2SYehuda Sadeh 
4117a02a946dSIlya Dryomov struct MOSDBackoff {
4118a02a946dSIlya Dryomov 	struct ceph_spg spgid;
4119a02a946dSIlya Dryomov 	u32 map_epoch;
4120a02a946dSIlya Dryomov 	u8 op;
4121a02a946dSIlya Dryomov 	u64 id;
4122a02a946dSIlya Dryomov 	struct ceph_hobject_id *begin;
4123a02a946dSIlya Dryomov 	struct ceph_hobject_id *end;
4124a02a946dSIlya Dryomov };
4125a02a946dSIlya Dryomov 
4126a02a946dSIlya Dryomov static int decode_MOSDBackoff(const struct ceph_msg *msg, struct MOSDBackoff *m)
4127a02a946dSIlya Dryomov {
4128a02a946dSIlya Dryomov 	void *p = msg->front.iov_base;
4129a02a946dSIlya Dryomov 	void *const end = p + msg->front.iov_len;
4130a02a946dSIlya Dryomov 	u8 struct_v;
4131a02a946dSIlya Dryomov 	u32 struct_len;
4132a02a946dSIlya Dryomov 	int ret;
4133a02a946dSIlya Dryomov 
4134a02a946dSIlya Dryomov 	ret = ceph_start_decoding(&p, end, 1, "spg_t", &struct_v, &struct_len);
4135a02a946dSIlya Dryomov 	if (ret)
4136a02a946dSIlya Dryomov 		return ret;
4137a02a946dSIlya Dryomov 
4138a02a946dSIlya Dryomov 	ret = ceph_decode_pgid(&p, end, &m->spgid.pgid);
4139a02a946dSIlya Dryomov 	if (ret)
4140a02a946dSIlya Dryomov 		return ret;
4141a02a946dSIlya Dryomov 
4142a02a946dSIlya Dryomov 	ceph_decode_8_safe(&p, end, m->spgid.shard, e_inval);
4143a02a946dSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->map_epoch, e_inval);
4144a02a946dSIlya Dryomov 	ceph_decode_8_safe(&p, end, m->op, e_inval);
4145a02a946dSIlya Dryomov 	ceph_decode_64_safe(&p, end, m->id, e_inval);
4146a02a946dSIlya Dryomov 
4147a02a946dSIlya Dryomov 	m->begin = kzalloc(sizeof(*m->begin), GFP_NOIO);
4148a02a946dSIlya Dryomov 	if (!m->begin)
4149a02a946dSIlya Dryomov 		return -ENOMEM;
4150a02a946dSIlya Dryomov 
4151a02a946dSIlya Dryomov 	ret = decode_hoid(&p, end, m->begin);
4152a02a946dSIlya Dryomov 	if (ret) {
4153a02a946dSIlya Dryomov 		free_hoid(m->begin);
4154a02a946dSIlya Dryomov 		return ret;
4155a02a946dSIlya Dryomov 	}
4156a02a946dSIlya Dryomov 
4157a02a946dSIlya Dryomov 	m->end = kzalloc(sizeof(*m->end), GFP_NOIO);
4158a02a946dSIlya Dryomov 	if (!m->end) {
4159a02a946dSIlya Dryomov 		free_hoid(m->begin);
4160a02a946dSIlya Dryomov 		return -ENOMEM;
4161a02a946dSIlya Dryomov 	}
4162a02a946dSIlya Dryomov 
4163a02a946dSIlya Dryomov 	ret = decode_hoid(&p, end, m->end);
4164a02a946dSIlya Dryomov 	if (ret) {
4165a02a946dSIlya Dryomov 		free_hoid(m->begin);
4166a02a946dSIlya Dryomov 		free_hoid(m->end);
4167a02a946dSIlya Dryomov 		return ret;
4168a02a946dSIlya Dryomov 	}
4169a02a946dSIlya Dryomov 
4170a02a946dSIlya Dryomov 	return 0;
4171a02a946dSIlya Dryomov 
4172a02a946dSIlya Dryomov e_inval:
4173a02a946dSIlya Dryomov 	return -EINVAL;
4174a02a946dSIlya Dryomov }
4175a02a946dSIlya Dryomov 
4176a02a946dSIlya Dryomov static struct ceph_msg *create_backoff_message(
4177a02a946dSIlya Dryomov 				const struct ceph_osd_backoff *backoff,
4178a02a946dSIlya Dryomov 				u32 map_epoch)
4179a02a946dSIlya Dryomov {
4180a02a946dSIlya Dryomov 	struct ceph_msg *msg;
4181a02a946dSIlya Dryomov 	void *p, *end;
4182a02a946dSIlya Dryomov 	int msg_size;
4183a02a946dSIlya Dryomov 
4184a02a946dSIlya Dryomov 	msg_size = CEPH_ENCODING_START_BLK_LEN +
4185a02a946dSIlya Dryomov 			CEPH_PGID_ENCODING_LEN + 1; /* spgid */
4186a02a946dSIlya Dryomov 	msg_size += 4 + 1 + 8; /* map_epoch, op, id */
4187a02a946dSIlya Dryomov 	msg_size += CEPH_ENCODING_START_BLK_LEN +
4188a02a946dSIlya Dryomov 			hoid_encoding_size(backoff->begin);
4189a02a946dSIlya Dryomov 	msg_size += CEPH_ENCODING_START_BLK_LEN +
4190a02a946dSIlya Dryomov 			hoid_encoding_size(backoff->end);
4191a02a946dSIlya Dryomov 
4192a02a946dSIlya Dryomov 	msg = ceph_msg_new(CEPH_MSG_OSD_BACKOFF, msg_size, GFP_NOIO, true);
4193a02a946dSIlya Dryomov 	if (!msg)
4194a02a946dSIlya Dryomov 		return NULL;
4195a02a946dSIlya Dryomov 
4196a02a946dSIlya Dryomov 	p = msg->front.iov_base;
4197a02a946dSIlya Dryomov 	end = p + msg->front_alloc_len;
4198a02a946dSIlya Dryomov 
4199a02a946dSIlya Dryomov 	encode_spgid(&p, &backoff->spgid);
4200a02a946dSIlya Dryomov 	ceph_encode_32(&p, map_epoch);
4201a02a946dSIlya Dryomov 	ceph_encode_8(&p, CEPH_OSD_BACKOFF_OP_ACK_BLOCK);
4202a02a946dSIlya Dryomov 	ceph_encode_64(&p, backoff->id);
4203a02a946dSIlya Dryomov 	encode_hoid(&p, end, backoff->begin);
4204a02a946dSIlya Dryomov 	encode_hoid(&p, end, backoff->end);
4205a02a946dSIlya Dryomov 	BUG_ON(p != end);
4206a02a946dSIlya Dryomov 
4207a02a946dSIlya Dryomov 	msg->front.iov_len = p - msg->front.iov_base;
4208a02a946dSIlya Dryomov 	msg->hdr.version = cpu_to_le16(1); /* MOSDBackoff v1 */
4209a02a946dSIlya Dryomov 	msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
4210a02a946dSIlya Dryomov 
4211a02a946dSIlya Dryomov 	return msg;
4212a02a946dSIlya Dryomov }
4213a02a946dSIlya Dryomov 
4214a02a946dSIlya Dryomov static void handle_backoff_block(struct ceph_osd *osd, struct MOSDBackoff *m)
4215a02a946dSIlya Dryomov {
4216a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
4217a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
4218a02a946dSIlya Dryomov 	struct ceph_msg *msg;
4219a02a946dSIlya Dryomov 
4220a02a946dSIlya Dryomov 	dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4221a02a946dSIlya Dryomov 	     m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4222a02a946dSIlya Dryomov 
4223a02a946dSIlya Dryomov 	spg = lookup_spg_mapping(&osd->o_backoff_mappings, &m->spgid);
4224a02a946dSIlya Dryomov 	if (!spg) {
4225a02a946dSIlya Dryomov 		spg = alloc_spg_mapping();
4226a02a946dSIlya Dryomov 		if (!spg) {
4227a02a946dSIlya Dryomov 			pr_err("%s failed to allocate spg\n", __func__);
4228a02a946dSIlya Dryomov 			return;
4229a02a946dSIlya Dryomov 		}
4230a02a946dSIlya Dryomov 		spg->spgid = m->spgid; /* struct */
4231a02a946dSIlya Dryomov 		insert_spg_mapping(&osd->o_backoff_mappings, spg);
4232a02a946dSIlya Dryomov 	}
4233a02a946dSIlya Dryomov 
4234a02a946dSIlya Dryomov 	backoff = alloc_backoff();
4235a02a946dSIlya Dryomov 	if (!backoff) {
4236a02a946dSIlya Dryomov 		pr_err("%s failed to allocate backoff\n", __func__);
4237a02a946dSIlya Dryomov 		return;
4238a02a946dSIlya Dryomov 	}
4239a02a946dSIlya Dryomov 	backoff->spgid = m->spgid; /* struct */
4240a02a946dSIlya Dryomov 	backoff->id = m->id;
4241a02a946dSIlya Dryomov 	backoff->begin = m->begin;
4242a02a946dSIlya Dryomov 	m->begin = NULL; /* backoff now owns this */
4243a02a946dSIlya Dryomov 	backoff->end = m->end;
4244a02a946dSIlya Dryomov 	m->end = NULL;   /* ditto */
4245a02a946dSIlya Dryomov 
4246a02a946dSIlya Dryomov 	insert_backoff(&spg->backoffs, backoff);
4247a02a946dSIlya Dryomov 	insert_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4248a02a946dSIlya Dryomov 
4249a02a946dSIlya Dryomov 	/*
4250a02a946dSIlya Dryomov 	 * Ack with original backoff's epoch so that the OSD can
4251a02a946dSIlya Dryomov 	 * discard this if there was a PG split.
4252a02a946dSIlya Dryomov 	 */
4253a02a946dSIlya Dryomov 	msg = create_backoff_message(backoff, m->map_epoch);
4254a02a946dSIlya Dryomov 	if (!msg) {
4255a02a946dSIlya Dryomov 		pr_err("%s failed to allocate msg\n", __func__);
4256a02a946dSIlya Dryomov 		return;
4257a02a946dSIlya Dryomov 	}
4258a02a946dSIlya Dryomov 	ceph_con_send(&osd->o_con, msg);
4259a02a946dSIlya Dryomov }
4260a02a946dSIlya Dryomov 
4261a02a946dSIlya Dryomov static bool target_contained_by(const struct ceph_osd_request_target *t,
4262a02a946dSIlya Dryomov 				const struct ceph_hobject_id *begin,
4263a02a946dSIlya Dryomov 				const struct ceph_hobject_id *end)
4264a02a946dSIlya Dryomov {
4265a02a946dSIlya Dryomov 	struct ceph_hobject_id hoid;
4266a02a946dSIlya Dryomov 	int cmp;
4267a02a946dSIlya Dryomov 
4268a02a946dSIlya Dryomov 	hoid_fill_from_target(&hoid, t);
4269a02a946dSIlya Dryomov 	cmp = hoid_compare(&hoid, begin);
4270a02a946dSIlya Dryomov 	return !cmp || (cmp > 0 && hoid_compare(&hoid, end) < 0);
4271a02a946dSIlya Dryomov }
4272a02a946dSIlya Dryomov 
4273a02a946dSIlya Dryomov static void handle_backoff_unblock(struct ceph_osd *osd,
4274a02a946dSIlya Dryomov 				   const struct MOSDBackoff *m)
4275a02a946dSIlya Dryomov {
4276a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
4277a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
4278a02a946dSIlya Dryomov 	struct rb_node *n;
4279a02a946dSIlya Dryomov 
4280a02a946dSIlya Dryomov 	dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4281a02a946dSIlya Dryomov 	     m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4282a02a946dSIlya Dryomov 
4283a02a946dSIlya Dryomov 	backoff = lookup_backoff_by_id(&osd->o_backoffs_by_id, m->id);
4284a02a946dSIlya Dryomov 	if (!backoff) {
4285a02a946dSIlya Dryomov 		pr_err("%s osd%d spgid %llu.%xs%d id %llu backoff dne\n",
4286a02a946dSIlya Dryomov 		       __func__, osd->o_osd, m->spgid.pgid.pool,
4287a02a946dSIlya Dryomov 		       m->spgid.pgid.seed, m->spgid.shard, m->id);
4288a02a946dSIlya Dryomov 		return;
4289a02a946dSIlya Dryomov 	}
4290a02a946dSIlya Dryomov 
4291a02a946dSIlya Dryomov 	if (hoid_compare(backoff->begin, m->begin) &&
4292a02a946dSIlya Dryomov 	    hoid_compare(backoff->end, m->end)) {
4293a02a946dSIlya Dryomov 		pr_err("%s osd%d spgid %llu.%xs%d id %llu bad range?\n",
4294a02a946dSIlya Dryomov 		       __func__, osd->o_osd, m->spgid.pgid.pool,
4295a02a946dSIlya Dryomov 		       m->spgid.pgid.seed, m->spgid.shard, m->id);
4296a02a946dSIlya Dryomov 		/* unblock it anyway... */
4297a02a946dSIlya Dryomov 	}
4298a02a946dSIlya Dryomov 
4299a02a946dSIlya Dryomov 	spg = lookup_spg_mapping(&osd->o_backoff_mappings, &backoff->spgid);
4300a02a946dSIlya Dryomov 	BUG_ON(!spg);
4301a02a946dSIlya Dryomov 
4302a02a946dSIlya Dryomov 	erase_backoff(&spg->backoffs, backoff);
4303a02a946dSIlya Dryomov 	erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4304a02a946dSIlya Dryomov 	free_backoff(backoff);
4305a02a946dSIlya Dryomov 
4306a02a946dSIlya Dryomov 	if (RB_EMPTY_ROOT(&spg->backoffs)) {
4307a02a946dSIlya Dryomov 		erase_spg_mapping(&osd->o_backoff_mappings, spg);
4308a02a946dSIlya Dryomov 		free_spg_mapping(spg);
4309a02a946dSIlya Dryomov 	}
4310a02a946dSIlya Dryomov 
4311a02a946dSIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
4312a02a946dSIlya Dryomov 		struct ceph_osd_request *req =
4313a02a946dSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
4314a02a946dSIlya Dryomov 
4315a02a946dSIlya Dryomov 		if (!ceph_spg_compare(&req->r_t.spgid, &m->spgid)) {
4316a02a946dSIlya Dryomov 			/*
4317a02a946dSIlya Dryomov 			 * Match against @m, not @backoff -- the PG may
4318a02a946dSIlya Dryomov 			 * have split on the OSD.
4319a02a946dSIlya Dryomov 			 */
4320a02a946dSIlya Dryomov 			if (target_contained_by(&req->r_t, m->begin, m->end)) {
4321a02a946dSIlya Dryomov 				/*
4322a02a946dSIlya Dryomov 				 * If no other installed backoff applies,
4323a02a946dSIlya Dryomov 				 * resend.
4324a02a946dSIlya Dryomov 				 */
4325a02a946dSIlya Dryomov 				send_request(req);
4326a02a946dSIlya Dryomov 			}
4327a02a946dSIlya Dryomov 		}
4328a02a946dSIlya Dryomov 	}
4329a02a946dSIlya Dryomov }
4330a02a946dSIlya Dryomov 
4331a02a946dSIlya Dryomov static void handle_backoff(struct ceph_osd *osd, struct ceph_msg *msg)
4332a02a946dSIlya Dryomov {
4333a02a946dSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
4334a02a946dSIlya Dryomov 	struct MOSDBackoff m;
4335a02a946dSIlya Dryomov 	int ret;
4336a02a946dSIlya Dryomov 
4337a02a946dSIlya Dryomov 	down_read(&osdc->lock);
4338a02a946dSIlya Dryomov 	if (!osd_registered(osd)) {
4339a02a946dSIlya Dryomov 		dout("%s osd%d unknown\n", __func__, osd->o_osd);
4340a02a946dSIlya Dryomov 		up_read(&osdc->lock);
4341a02a946dSIlya Dryomov 		return;
4342a02a946dSIlya Dryomov 	}
4343a02a946dSIlya Dryomov 	WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
4344a02a946dSIlya Dryomov 
4345a02a946dSIlya Dryomov 	mutex_lock(&osd->lock);
4346a02a946dSIlya Dryomov 	ret = decode_MOSDBackoff(msg, &m);
4347a02a946dSIlya Dryomov 	if (ret) {
4348a02a946dSIlya Dryomov 		pr_err("failed to decode MOSDBackoff: %d\n", ret);
4349a02a946dSIlya Dryomov 		ceph_msg_dump(msg);
4350a02a946dSIlya Dryomov 		goto out_unlock;
4351a02a946dSIlya Dryomov 	}
4352a02a946dSIlya Dryomov 
4353a02a946dSIlya Dryomov 	switch (m.op) {
4354a02a946dSIlya Dryomov 	case CEPH_OSD_BACKOFF_OP_BLOCK:
4355a02a946dSIlya Dryomov 		handle_backoff_block(osd, &m);
4356a02a946dSIlya Dryomov 		break;
4357a02a946dSIlya Dryomov 	case CEPH_OSD_BACKOFF_OP_UNBLOCK:
4358a02a946dSIlya Dryomov 		handle_backoff_unblock(osd, &m);
4359a02a946dSIlya Dryomov 		break;
4360a02a946dSIlya Dryomov 	default:
4361a02a946dSIlya Dryomov 		pr_err("%s osd%d unknown op %d\n", __func__, osd->o_osd, m.op);
4362a02a946dSIlya Dryomov 	}
4363a02a946dSIlya Dryomov 
4364a02a946dSIlya Dryomov 	free_hoid(m.begin);
4365a02a946dSIlya Dryomov 	free_hoid(m.end);
4366a02a946dSIlya Dryomov 
4367a02a946dSIlya Dryomov out_unlock:
4368a02a946dSIlya Dryomov 	mutex_unlock(&osd->lock);
4369a02a946dSIlya Dryomov 	up_read(&osdc->lock);
4370a02a946dSIlya Dryomov }
4371a02a946dSIlya Dryomov 
43723d14c5d2SYehuda Sadeh /*
4373a40c4f10SYehuda Sadeh  * Process osd watch notifications
4374a40c4f10SYehuda Sadeh  */
43753c663bbdSAlex Elder static void handle_watch_notify(struct ceph_osd_client *osdc,
43763c663bbdSAlex Elder 				struct ceph_msg *msg)
4377a40c4f10SYehuda Sadeh {
4378922dab61SIlya Dryomov 	void *p = msg->front.iov_base;
4379922dab61SIlya Dryomov 	void *const end = p + msg->front.iov_len;
4380922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
4381922dab61SIlya Dryomov 	struct linger_work *lwork;
4382922dab61SIlya Dryomov 	u8 proto_ver, opcode;
4383922dab61SIlya Dryomov 	u64 cookie, notify_id;
4384922dab61SIlya Dryomov 	u64 notifier_id = 0;
438519079203SIlya Dryomov 	s32 return_code = 0;
4386922dab61SIlya Dryomov 	void *payload = NULL;
4387922dab61SIlya Dryomov 	u32 payload_len = 0;
4388a40c4f10SYehuda Sadeh 
4389a40c4f10SYehuda Sadeh 	ceph_decode_8_safe(&p, end, proto_ver, bad);
4390a40c4f10SYehuda Sadeh 	ceph_decode_8_safe(&p, end, opcode, bad);
4391a40c4f10SYehuda Sadeh 	ceph_decode_64_safe(&p, end, cookie, bad);
4392922dab61SIlya Dryomov 	p += 8; /* skip ver */
4393a40c4f10SYehuda Sadeh 	ceph_decode_64_safe(&p, end, notify_id, bad);
4394a40c4f10SYehuda Sadeh 
4395922dab61SIlya Dryomov 	if (proto_ver >= 1) {
4396922dab61SIlya Dryomov 		ceph_decode_32_safe(&p, end, payload_len, bad);
4397922dab61SIlya Dryomov 		ceph_decode_need(&p, end, payload_len, bad);
4398922dab61SIlya Dryomov 		payload = p;
4399922dab61SIlya Dryomov 		p += payload_len;
4400a40c4f10SYehuda Sadeh 	}
4401a40c4f10SYehuda Sadeh 
4402922dab61SIlya Dryomov 	if (le16_to_cpu(msg->hdr.version) >= 2)
440319079203SIlya Dryomov 		ceph_decode_32_safe(&p, end, return_code, bad);
4404922dab61SIlya Dryomov 
4405922dab61SIlya Dryomov 	if (le16_to_cpu(msg->hdr.version) >= 3)
4406922dab61SIlya Dryomov 		ceph_decode_64_safe(&p, end, notifier_id, bad);
4407922dab61SIlya Dryomov 
4408922dab61SIlya Dryomov 	down_read(&osdc->lock);
4409922dab61SIlya Dryomov 	lreq = lookup_linger_osdc(&osdc->linger_requests, cookie);
4410922dab61SIlya Dryomov 	if (!lreq) {
4411922dab61SIlya Dryomov 		dout("%s opcode %d cookie %llu dne\n", __func__, opcode,
4412922dab61SIlya Dryomov 		     cookie);
4413922dab61SIlya Dryomov 		goto out_unlock_osdc;
4414922dab61SIlya Dryomov 	}
4415922dab61SIlya Dryomov 
4416922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
441719079203SIlya Dryomov 	dout("%s opcode %d cookie %llu lreq %p is_watch %d\n", __func__,
441819079203SIlya Dryomov 	     opcode, cookie, lreq, lreq->is_watch);
4419922dab61SIlya Dryomov 	if (opcode == CEPH_WATCH_EVENT_DISCONNECT) {
4420922dab61SIlya Dryomov 		if (!lreq->last_error) {
4421922dab61SIlya Dryomov 			lreq->last_error = -ENOTCONN;
4422922dab61SIlya Dryomov 			queue_watch_error(lreq);
4423922dab61SIlya Dryomov 		}
442419079203SIlya Dryomov 	} else if (!lreq->is_watch) {
442519079203SIlya Dryomov 		/* CEPH_WATCH_EVENT_NOTIFY_COMPLETE */
442619079203SIlya Dryomov 		if (lreq->notify_id && lreq->notify_id != notify_id) {
442719079203SIlya Dryomov 			dout("lreq %p notify_id %llu != %llu, ignoring\n", lreq,
442819079203SIlya Dryomov 			     lreq->notify_id, notify_id);
442919079203SIlya Dryomov 		} else if (!completion_done(&lreq->notify_finish_wait)) {
443019079203SIlya Dryomov 			struct ceph_msg_data *data =
44310d9c1ab3SIlya Dryomov 			    msg->num_data_items ? &msg->data[0] : NULL;
443219079203SIlya Dryomov 
443319079203SIlya Dryomov 			if (data) {
443419079203SIlya Dryomov 				if (lreq->preply_pages) {
443519079203SIlya Dryomov 					WARN_ON(data->type !=
443619079203SIlya Dryomov 							CEPH_MSG_DATA_PAGES);
443719079203SIlya Dryomov 					*lreq->preply_pages = data->pages;
443819079203SIlya Dryomov 					*lreq->preply_len = data->length;
443919079203SIlya Dryomov 				} else {
444019079203SIlya Dryomov 					ceph_release_page_vector(data->pages,
444119079203SIlya Dryomov 					       calc_pages_for(0, data->length));
444219079203SIlya Dryomov 				}
444319079203SIlya Dryomov 			}
444419079203SIlya Dryomov 			lreq->notify_finish_error = return_code;
444519079203SIlya Dryomov 			complete_all(&lreq->notify_finish_wait);
444619079203SIlya Dryomov 		}
4447922dab61SIlya Dryomov 	} else {
4448922dab61SIlya Dryomov 		/* CEPH_WATCH_EVENT_NOTIFY */
4449922dab61SIlya Dryomov 		lwork = lwork_alloc(lreq, do_watch_notify);
4450922dab61SIlya Dryomov 		if (!lwork) {
4451922dab61SIlya Dryomov 			pr_err("failed to allocate notify-lwork\n");
4452922dab61SIlya Dryomov 			goto out_unlock_lreq;
4453922dab61SIlya Dryomov 		}
4454922dab61SIlya Dryomov 
4455922dab61SIlya Dryomov 		lwork->notify.notify_id = notify_id;
4456922dab61SIlya Dryomov 		lwork->notify.notifier_id = notifier_id;
4457922dab61SIlya Dryomov 		lwork->notify.payload = payload;
4458922dab61SIlya Dryomov 		lwork->notify.payload_len = payload_len;
4459922dab61SIlya Dryomov 		lwork->notify.msg = ceph_msg_get(msg);
4460922dab61SIlya Dryomov 		lwork_queue(lwork);
4461922dab61SIlya Dryomov 	}
4462922dab61SIlya Dryomov 
4463922dab61SIlya Dryomov out_unlock_lreq:
4464922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
4465922dab61SIlya Dryomov out_unlock_osdc:
4466922dab61SIlya Dryomov 	up_read(&osdc->lock);
4467a40c4f10SYehuda Sadeh 	return;
4468a40c4f10SYehuda Sadeh 
4469a40c4f10SYehuda Sadeh bad:
4470a40c4f10SYehuda Sadeh 	pr_err("osdc handle_watch_notify corrupt msg\n");
4471a40c4f10SYehuda Sadeh }
4472a40c4f10SYehuda Sadeh 
4473a40c4f10SYehuda Sadeh /*
44743d14c5d2SYehuda Sadeh  * Register request, send initial attempt.
44753d14c5d2SYehuda Sadeh  */
44763d14c5d2SYehuda Sadeh int ceph_osdc_start_request(struct ceph_osd_client *osdc,
44773d14c5d2SYehuda Sadeh 			    struct ceph_osd_request *req,
44783d14c5d2SYehuda Sadeh 			    bool nofail)
44793d14c5d2SYehuda Sadeh {
44805aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
44815aea3dcdSIlya Dryomov 	submit_request(req, false);
44825aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
44833d14c5d2SYehuda Sadeh 
44845aea3dcdSIlya Dryomov 	return 0;
44853d14c5d2SYehuda Sadeh }
44863d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_start_request);
44873d14c5d2SYehuda Sadeh 
44883d14c5d2SYehuda Sadeh /*
4489c297eb42SIlya Dryomov  * Unregister a registered request.  The request is not completed:
4490c297eb42SIlya Dryomov  * ->r_result isn't set and __complete_request() isn't called.
4491c9f9b93dSIlya Dryomov  */
4492c9f9b93dSIlya Dryomov void ceph_osdc_cancel_request(struct ceph_osd_request *req)
4493c9f9b93dSIlya Dryomov {
4494c9f9b93dSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
4495c9f9b93dSIlya Dryomov 
44965aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
44975aea3dcdSIlya Dryomov 	if (req->r_osd)
44985aea3dcdSIlya Dryomov 		cancel_request(req);
44995aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
4500c9f9b93dSIlya Dryomov }
4501c9f9b93dSIlya Dryomov EXPORT_SYMBOL(ceph_osdc_cancel_request);
4502c9f9b93dSIlya Dryomov 
4503c9f9b93dSIlya Dryomov /*
450442b06965SIlya Dryomov  * @timeout: in jiffies, 0 means "wait forever"
450542b06965SIlya Dryomov  */
450642b06965SIlya Dryomov static int wait_request_timeout(struct ceph_osd_request *req,
450742b06965SIlya Dryomov 				unsigned long timeout)
450842b06965SIlya Dryomov {
450942b06965SIlya Dryomov 	long left;
451042b06965SIlya Dryomov 
451142b06965SIlya Dryomov 	dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
45120e76abf2SYan, Zheng 	left = wait_for_completion_killable_timeout(&req->r_completion,
451342b06965SIlya Dryomov 						ceph_timeout_jiffies(timeout));
451442b06965SIlya Dryomov 	if (left <= 0) {
451542b06965SIlya Dryomov 		left = left ?: -ETIMEDOUT;
451642b06965SIlya Dryomov 		ceph_osdc_cancel_request(req);
451742b06965SIlya Dryomov 	} else {
451842b06965SIlya Dryomov 		left = req->r_result; /* completed */
451942b06965SIlya Dryomov 	}
452042b06965SIlya Dryomov 
452142b06965SIlya Dryomov 	return left;
452242b06965SIlya Dryomov }
452342b06965SIlya Dryomov 
452442b06965SIlya Dryomov /*
45253d14c5d2SYehuda Sadeh  * wait for a request to complete
45263d14c5d2SYehuda Sadeh  */
45273d14c5d2SYehuda Sadeh int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
45283d14c5d2SYehuda Sadeh 			   struct ceph_osd_request *req)
45293d14c5d2SYehuda Sadeh {
453042b06965SIlya Dryomov 	return wait_request_timeout(req, 0);
45313d14c5d2SYehuda Sadeh }
45323d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_wait_request);
45333d14c5d2SYehuda Sadeh 
45343d14c5d2SYehuda Sadeh /*
45353d14c5d2SYehuda Sadeh  * sync - wait for all in-flight requests to flush.  avoid starvation.
45363d14c5d2SYehuda Sadeh  */
45373d14c5d2SYehuda Sadeh void ceph_osdc_sync(struct ceph_osd_client *osdc)
45383d14c5d2SYehuda Sadeh {
45395aea3dcdSIlya Dryomov 	struct rb_node *n, *p;
45405aea3dcdSIlya Dryomov 	u64 last_tid = atomic64_read(&osdc->last_tid);
45413d14c5d2SYehuda Sadeh 
45425aea3dcdSIlya Dryomov again:
45435aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
45445aea3dcdSIlya Dryomov 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
45455aea3dcdSIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
45465aea3dcdSIlya Dryomov 
45475aea3dcdSIlya Dryomov 		mutex_lock(&osd->lock);
45485aea3dcdSIlya Dryomov 		for (p = rb_first(&osd->o_requests); p; p = rb_next(p)) {
45495aea3dcdSIlya Dryomov 			struct ceph_osd_request *req =
45505aea3dcdSIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
45515aea3dcdSIlya Dryomov 
45523d14c5d2SYehuda Sadeh 			if (req->r_tid > last_tid)
45533d14c5d2SYehuda Sadeh 				break;
45543d14c5d2SYehuda Sadeh 
45555aea3dcdSIlya Dryomov 			if (!(req->r_flags & CEPH_OSD_FLAG_WRITE))
45563d14c5d2SYehuda Sadeh 				continue;
45573d14c5d2SYehuda Sadeh 
45583d14c5d2SYehuda Sadeh 			ceph_osdc_get_request(req);
45595aea3dcdSIlya Dryomov 			mutex_unlock(&osd->lock);
45605aea3dcdSIlya Dryomov 			up_read(&osdc->lock);
45615aea3dcdSIlya Dryomov 			dout("%s waiting on req %p tid %llu last_tid %llu\n",
45625aea3dcdSIlya Dryomov 			     __func__, req, req->r_tid, last_tid);
4563b18b9550SIlya Dryomov 			wait_for_completion(&req->r_completion);
45643d14c5d2SYehuda Sadeh 			ceph_osdc_put_request(req);
45655aea3dcdSIlya Dryomov 			goto again;
45663d14c5d2SYehuda Sadeh 		}
45675aea3dcdSIlya Dryomov 
45685aea3dcdSIlya Dryomov 		mutex_unlock(&osd->lock);
45695aea3dcdSIlya Dryomov 	}
45705aea3dcdSIlya Dryomov 
45715aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
45725aea3dcdSIlya Dryomov 	dout("%s done last_tid %llu\n", __func__, last_tid);
45733d14c5d2SYehuda Sadeh }
45743d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_sync);
45753d14c5d2SYehuda Sadeh 
4576922dab61SIlya Dryomov static struct ceph_osd_request *
4577922dab61SIlya Dryomov alloc_linger_request(struct ceph_osd_linger_request *lreq)
4578922dab61SIlya Dryomov {
4579922dab61SIlya Dryomov 	struct ceph_osd_request *req;
4580922dab61SIlya Dryomov 
4581922dab61SIlya Dryomov 	req = ceph_osdc_alloc_request(lreq->osdc, NULL, 1, false, GFP_NOIO);
4582922dab61SIlya Dryomov 	if (!req)
4583922dab61SIlya Dryomov 		return NULL;
4584922dab61SIlya Dryomov 
4585922dab61SIlya Dryomov 	ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
4586922dab61SIlya Dryomov 	ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
4587922dab61SIlya Dryomov 	return req;
4588922dab61SIlya Dryomov }
4589922dab61SIlya Dryomov 
459039e58c34SIlya Dryomov static struct ceph_osd_request *
459139e58c34SIlya Dryomov alloc_watch_request(struct ceph_osd_linger_request *lreq, u8 watch_opcode)
459239e58c34SIlya Dryomov {
459339e58c34SIlya Dryomov 	struct ceph_osd_request *req;
459439e58c34SIlya Dryomov 
459539e58c34SIlya Dryomov 	req = alloc_linger_request(lreq);
459639e58c34SIlya Dryomov 	if (!req)
459739e58c34SIlya Dryomov 		return NULL;
459839e58c34SIlya Dryomov 
459939e58c34SIlya Dryomov 	/*
460039e58c34SIlya Dryomov 	 * Pass 0 for cookie because we don't know it yet, it will be
460139e58c34SIlya Dryomov 	 * filled in by linger_submit().
460239e58c34SIlya Dryomov 	 */
460339e58c34SIlya Dryomov 	osd_req_op_watch_init(req, 0, 0, watch_opcode);
460426f887e0SIlya Dryomov 
460526f887e0SIlya Dryomov 	if (ceph_osdc_alloc_messages(req, GFP_NOIO)) {
460626f887e0SIlya Dryomov 		ceph_osdc_put_request(req);
460726f887e0SIlya Dryomov 		return NULL;
460826f887e0SIlya Dryomov 	}
460926f887e0SIlya Dryomov 
461039e58c34SIlya Dryomov 	return req;
461139e58c34SIlya Dryomov }
461239e58c34SIlya Dryomov 
4613922dab61SIlya Dryomov /*
4614922dab61SIlya Dryomov  * Returns a handle, caller owns a ref.
4615922dab61SIlya Dryomov  */
4616922dab61SIlya Dryomov struct ceph_osd_linger_request *
4617922dab61SIlya Dryomov ceph_osdc_watch(struct ceph_osd_client *osdc,
4618922dab61SIlya Dryomov 		struct ceph_object_id *oid,
4619922dab61SIlya Dryomov 		struct ceph_object_locator *oloc,
4620922dab61SIlya Dryomov 		rados_watchcb2_t wcb,
4621922dab61SIlya Dryomov 		rados_watcherrcb_t errcb,
4622922dab61SIlya Dryomov 		void *data)
4623922dab61SIlya Dryomov {
4624922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
4625922dab61SIlya Dryomov 	int ret;
4626922dab61SIlya Dryomov 
4627922dab61SIlya Dryomov 	lreq = linger_alloc(osdc);
4628922dab61SIlya Dryomov 	if (!lreq)
4629922dab61SIlya Dryomov 		return ERR_PTR(-ENOMEM);
4630922dab61SIlya Dryomov 
463119079203SIlya Dryomov 	lreq->is_watch = true;
4632922dab61SIlya Dryomov 	lreq->wcb = wcb;
4633922dab61SIlya Dryomov 	lreq->errcb = errcb;
4634922dab61SIlya Dryomov 	lreq->data = data;
4635b07d3c4bSIlya Dryomov 	lreq->watch_valid_thru = jiffies;
4636922dab61SIlya Dryomov 
4637922dab61SIlya Dryomov 	ceph_oid_copy(&lreq->t.base_oid, oid);
4638922dab61SIlya Dryomov 	ceph_oloc_copy(&lreq->t.base_oloc, oloc);
463954ea0046SIlya Dryomov 	lreq->t.flags = CEPH_OSD_FLAG_WRITE;
4640fac02ddfSArnd Bergmann 	ktime_get_real_ts64(&lreq->mtime);
4641922dab61SIlya Dryomov 
464239e58c34SIlya Dryomov 	lreq->reg_req = alloc_watch_request(lreq, CEPH_OSD_WATCH_OP_WATCH);
4643922dab61SIlya Dryomov 	if (!lreq->reg_req) {
4644922dab61SIlya Dryomov 		ret = -ENOMEM;
4645922dab61SIlya Dryomov 		goto err_put_lreq;
4646922dab61SIlya Dryomov 	}
4647922dab61SIlya Dryomov 
464839e58c34SIlya Dryomov 	lreq->ping_req = alloc_watch_request(lreq, CEPH_OSD_WATCH_OP_PING);
4649922dab61SIlya Dryomov 	if (!lreq->ping_req) {
4650922dab61SIlya Dryomov 		ret = -ENOMEM;
4651922dab61SIlya Dryomov 		goto err_put_lreq;
4652922dab61SIlya Dryomov 	}
4653922dab61SIlya Dryomov 
465481c65213SIlya Dryomov 	linger_submit(lreq);
4655922dab61SIlya Dryomov 	ret = linger_reg_commit_wait(lreq);
4656922dab61SIlya Dryomov 	if (ret) {
4657922dab61SIlya Dryomov 		linger_cancel(lreq);
4658922dab61SIlya Dryomov 		goto err_put_lreq;
4659922dab61SIlya Dryomov 	}
4660922dab61SIlya Dryomov 
4661922dab61SIlya Dryomov 	return lreq;
4662922dab61SIlya Dryomov 
4663922dab61SIlya Dryomov err_put_lreq:
4664922dab61SIlya Dryomov 	linger_put(lreq);
4665922dab61SIlya Dryomov 	return ERR_PTR(ret);
4666922dab61SIlya Dryomov }
4667922dab61SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_watch);
4668922dab61SIlya Dryomov 
4669922dab61SIlya Dryomov /*
4670922dab61SIlya Dryomov  * Releases a ref.
4671922dab61SIlya Dryomov  *
4672922dab61SIlya Dryomov  * Times out after mount_timeout to preserve rbd unmap behaviour
4673922dab61SIlya Dryomov  * introduced in 2894e1d76974 ("rbd: timeout watch teardown on unmap
4674922dab61SIlya Dryomov  * with mount_timeout").
4675922dab61SIlya Dryomov  */
4676922dab61SIlya Dryomov int ceph_osdc_unwatch(struct ceph_osd_client *osdc,
4677922dab61SIlya Dryomov 		      struct ceph_osd_linger_request *lreq)
4678922dab61SIlya Dryomov {
4679922dab61SIlya Dryomov 	struct ceph_options *opts = osdc->client->options;
4680922dab61SIlya Dryomov 	struct ceph_osd_request *req;
4681922dab61SIlya Dryomov 	int ret;
4682922dab61SIlya Dryomov 
4683922dab61SIlya Dryomov 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4684922dab61SIlya Dryomov 	if (!req)
4685922dab61SIlya Dryomov 		return -ENOMEM;
4686922dab61SIlya Dryomov 
4687922dab61SIlya Dryomov 	ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
4688922dab61SIlya Dryomov 	ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
468954ea0046SIlya Dryomov 	req->r_flags = CEPH_OSD_FLAG_WRITE;
4690fac02ddfSArnd Bergmann 	ktime_get_real_ts64(&req->r_mtime);
4691922dab61SIlya Dryomov 	osd_req_op_watch_init(req, 0, lreq->linger_id,
4692922dab61SIlya Dryomov 			      CEPH_OSD_WATCH_OP_UNWATCH);
4693922dab61SIlya Dryomov 
4694922dab61SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4695922dab61SIlya Dryomov 	if (ret)
4696922dab61SIlya Dryomov 		goto out_put_req;
4697922dab61SIlya Dryomov 
4698922dab61SIlya Dryomov 	ceph_osdc_start_request(osdc, req, false);
4699922dab61SIlya Dryomov 	linger_cancel(lreq);
4700922dab61SIlya Dryomov 	linger_put(lreq);
4701922dab61SIlya Dryomov 	ret = wait_request_timeout(req, opts->mount_timeout);
4702922dab61SIlya Dryomov 
4703922dab61SIlya Dryomov out_put_req:
4704922dab61SIlya Dryomov 	ceph_osdc_put_request(req);
4705922dab61SIlya Dryomov 	return ret;
4706922dab61SIlya Dryomov }
4707922dab61SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_unwatch);
4708922dab61SIlya Dryomov 
4709922dab61SIlya Dryomov static int osd_req_op_notify_ack_init(struct ceph_osd_request *req, int which,
4710922dab61SIlya Dryomov 				      u64 notify_id, u64 cookie, void *payload,
47116d54228fSIlya Dryomov 				      u32 payload_len)
4712922dab61SIlya Dryomov {
4713922dab61SIlya Dryomov 	struct ceph_osd_req_op *op;
4714922dab61SIlya Dryomov 	struct ceph_pagelist *pl;
4715922dab61SIlya Dryomov 	int ret;
4716922dab61SIlya Dryomov 
4717922dab61SIlya Dryomov 	op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY_ACK, 0);
4718922dab61SIlya Dryomov 
471933165d47SIlya Dryomov 	pl = ceph_pagelist_alloc(GFP_NOIO);
4720922dab61SIlya Dryomov 	if (!pl)
4721922dab61SIlya Dryomov 		return -ENOMEM;
4722922dab61SIlya Dryomov 
4723922dab61SIlya Dryomov 	ret = ceph_pagelist_encode_64(pl, notify_id);
4724922dab61SIlya Dryomov 	ret |= ceph_pagelist_encode_64(pl, cookie);
4725922dab61SIlya Dryomov 	if (payload) {
4726922dab61SIlya Dryomov 		ret |= ceph_pagelist_encode_32(pl, payload_len);
4727922dab61SIlya Dryomov 		ret |= ceph_pagelist_append(pl, payload, payload_len);
4728922dab61SIlya Dryomov 	} else {
4729922dab61SIlya Dryomov 		ret |= ceph_pagelist_encode_32(pl, 0);
4730922dab61SIlya Dryomov 	}
4731922dab61SIlya Dryomov 	if (ret) {
4732922dab61SIlya Dryomov 		ceph_pagelist_release(pl);
4733922dab61SIlya Dryomov 		return -ENOMEM;
4734922dab61SIlya Dryomov 	}
4735922dab61SIlya Dryomov 
4736922dab61SIlya Dryomov 	ceph_osd_data_pagelist_init(&op->notify_ack.request_data, pl);
4737922dab61SIlya Dryomov 	op->indata_len = pl->length;
4738922dab61SIlya Dryomov 	return 0;
4739922dab61SIlya Dryomov }
4740922dab61SIlya Dryomov 
4741922dab61SIlya Dryomov int ceph_osdc_notify_ack(struct ceph_osd_client *osdc,
4742922dab61SIlya Dryomov 			 struct ceph_object_id *oid,
4743922dab61SIlya Dryomov 			 struct ceph_object_locator *oloc,
4744922dab61SIlya Dryomov 			 u64 notify_id,
4745922dab61SIlya Dryomov 			 u64 cookie,
4746922dab61SIlya Dryomov 			 void *payload,
47476d54228fSIlya Dryomov 			 u32 payload_len)
4748922dab61SIlya Dryomov {
4749922dab61SIlya Dryomov 	struct ceph_osd_request *req;
4750922dab61SIlya Dryomov 	int ret;
4751922dab61SIlya Dryomov 
4752922dab61SIlya Dryomov 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4753922dab61SIlya Dryomov 	if (!req)
4754922dab61SIlya Dryomov 		return -ENOMEM;
4755922dab61SIlya Dryomov 
4756922dab61SIlya Dryomov 	ceph_oid_copy(&req->r_base_oid, oid);
4757922dab61SIlya Dryomov 	ceph_oloc_copy(&req->r_base_oloc, oloc);
4758922dab61SIlya Dryomov 	req->r_flags = CEPH_OSD_FLAG_READ;
4759922dab61SIlya Dryomov 
476026f887e0SIlya Dryomov 	ret = osd_req_op_notify_ack_init(req, 0, notify_id, cookie, payload,
476126f887e0SIlya Dryomov 					 payload_len);
4762922dab61SIlya Dryomov 	if (ret)
4763922dab61SIlya Dryomov 		goto out_put_req;
4764922dab61SIlya Dryomov 
476526f887e0SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4766922dab61SIlya Dryomov 	if (ret)
4767922dab61SIlya Dryomov 		goto out_put_req;
4768922dab61SIlya Dryomov 
4769922dab61SIlya Dryomov 	ceph_osdc_start_request(osdc, req, false);
4770922dab61SIlya Dryomov 	ret = ceph_osdc_wait_request(osdc, req);
4771922dab61SIlya Dryomov 
4772922dab61SIlya Dryomov out_put_req:
4773922dab61SIlya Dryomov 	ceph_osdc_put_request(req);
4774922dab61SIlya Dryomov 	return ret;
4775922dab61SIlya Dryomov }
4776922dab61SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_notify_ack);
4777922dab61SIlya Dryomov 
477819079203SIlya Dryomov static int osd_req_op_notify_init(struct ceph_osd_request *req, int which,
477919079203SIlya Dryomov 				  u64 cookie, u32 prot_ver, u32 timeout,
47806d54228fSIlya Dryomov 				  void *payload, u32 payload_len)
478119079203SIlya Dryomov {
478219079203SIlya Dryomov 	struct ceph_osd_req_op *op;
478319079203SIlya Dryomov 	struct ceph_pagelist *pl;
478419079203SIlya Dryomov 	int ret;
478519079203SIlya Dryomov 
478619079203SIlya Dryomov 	op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY, 0);
478719079203SIlya Dryomov 	op->notify.cookie = cookie;
478819079203SIlya Dryomov 
478933165d47SIlya Dryomov 	pl = ceph_pagelist_alloc(GFP_NOIO);
479019079203SIlya Dryomov 	if (!pl)
479119079203SIlya Dryomov 		return -ENOMEM;
479219079203SIlya Dryomov 
479319079203SIlya Dryomov 	ret = ceph_pagelist_encode_32(pl, 1); /* prot_ver */
479419079203SIlya Dryomov 	ret |= ceph_pagelist_encode_32(pl, timeout);
479519079203SIlya Dryomov 	ret |= ceph_pagelist_encode_32(pl, payload_len);
479619079203SIlya Dryomov 	ret |= ceph_pagelist_append(pl, payload, payload_len);
479719079203SIlya Dryomov 	if (ret) {
479819079203SIlya Dryomov 		ceph_pagelist_release(pl);
479919079203SIlya Dryomov 		return -ENOMEM;
480019079203SIlya Dryomov 	}
480119079203SIlya Dryomov 
480219079203SIlya Dryomov 	ceph_osd_data_pagelist_init(&op->notify.request_data, pl);
480319079203SIlya Dryomov 	op->indata_len = pl->length;
480419079203SIlya Dryomov 	return 0;
480519079203SIlya Dryomov }
480619079203SIlya Dryomov 
480719079203SIlya Dryomov /*
480819079203SIlya Dryomov  * @timeout: in seconds
480919079203SIlya Dryomov  *
481019079203SIlya Dryomov  * @preply_{pages,len} are initialized both on success and error.
481119079203SIlya Dryomov  * The caller is responsible for:
481219079203SIlya Dryomov  *
481319079203SIlya Dryomov  *     ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len))
481419079203SIlya Dryomov  */
481519079203SIlya Dryomov int ceph_osdc_notify(struct ceph_osd_client *osdc,
481619079203SIlya Dryomov 		     struct ceph_object_id *oid,
481719079203SIlya Dryomov 		     struct ceph_object_locator *oloc,
481819079203SIlya Dryomov 		     void *payload,
48196d54228fSIlya Dryomov 		     u32 payload_len,
482019079203SIlya Dryomov 		     u32 timeout,
482119079203SIlya Dryomov 		     struct page ***preply_pages,
482219079203SIlya Dryomov 		     size_t *preply_len)
482319079203SIlya Dryomov {
482419079203SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
482519079203SIlya Dryomov 	struct page **pages;
482619079203SIlya Dryomov 	int ret;
482719079203SIlya Dryomov 
482819079203SIlya Dryomov 	WARN_ON(!timeout);
482919079203SIlya Dryomov 	if (preply_pages) {
483019079203SIlya Dryomov 		*preply_pages = NULL;
483119079203SIlya Dryomov 		*preply_len = 0;
483219079203SIlya Dryomov 	}
483319079203SIlya Dryomov 
483419079203SIlya Dryomov 	lreq = linger_alloc(osdc);
483519079203SIlya Dryomov 	if (!lreq)
483619079203SIlya Dryomov 		return -ENOMEM;
483719079203SIlya Dryomov 
483819079203SIlya Dryomov 	lreq->preply_pages = preply_pages;
483919079203SIlya Dryomov 	lreq->preply_len = preply_len;
484019079203SIlya Dryomov 
484119079203SIlya Dryomov 	ceph_oid_copy(&lreq->t.base_oid, oid);
484219079203SIlya Dryomov 	ceph_oloc_copy(&lreq->t.base_oloc, oloc);
484319079203SIlya Dryomov 	lreq->t.flags = CEPH_OSD_FLAG_READ;
484419079203SIlya Dryomov 
484519079203SIlya Dryomov 	lreq->reg_req = alloc_linger_request(lreq);
484619079203SIlya Dryomov 	if (!lreq->reg_req) {
484719079203SIlya Dryomov 		ret = -ENOMEM;
484819079203SIlya Dryomov 		goto out_put_lreq;
484919079203SIlya Dryomov 	}
485019079203SIlya Dryomov 
485181c65213SIlya Dryomov 	/*
485281c65213SIlya Dryomov 	 * Pass 0 for cookie because we don't know it yet, it will be
485381c65213SIlya Dryomov 	 * filled in by linger_submit().
485481c65213SIlya Dryomov 	 */
485581c65213SIlya Dryomov 	ret = osd_req_op_notify_init(lreq->reg_req, 0, 0, 1, timeout,
485681c65213SIlya Dryomov 				     payload, payload_len);
485781c65213SIlya Dryomov 	if (ret)
485881c65213SIlya Dryomov 		goto out_put_lreq;
485981c65213SIlya Dryomov 
486019079203SIlya Dryomov 	/* for notify_id */
486119079203SIlya Dryomov 	pages = ceph_alloc_page_vector(1, GFP_NOIO);
486219079203SIlya Dryomov 	if (IS_ERR(pages)) {
486319079203SIlya Dryomov 		ret = PTR_ERR(pages);
486419079203SIlya Dryomov 		goto out_put_lreq;
486519079203SIlya Dryomov 	}
486619079203SIlya Dryomov 	ceph_osd_data_pages_init(osd_req_op_data(lreq->reg_req, 0, notify,
486719079203SIlya Dryomov 						 response_data),
486819079203SIlya Dryomov 				 pages, PAGE_SIZE, 0, false, true);
486919079203SIlya Dryomov 
487026f887e0SIlya Dryomov 	ret = ceph_osdc_alloc_messages(lreq->reg_req, GFP_NOIO);
487126f887e0SIlya Dryomov 	if (ret)
487226f887e0SIlya Dryomov 		goto out_put_lreq;
487326f887e0SIlya Dryomov 
487481c65213SIlya Dryomov 	linger_submit(lreq);
487519079203SIlya Dryomov 	ret = linger_reg_commit_wait(lreq);
487619079203SIlya Dryomov 	if (!ret)
487719079203SIlya Dryomov 		ret = linger_notify_finish_wait(lreq);
487819079203SIlya Dryomov 	else
487919079203SIlya Dryomov 		dout("lreq %p failed to initiate notify %d\n", lreq, ret);
488019079203SIlya Dryomov 
488119079203SIlya Dryomov 	linger_cancel(lreq);
488219079203SIlya Dryomov out_put_lreq:
488319079203SIlya Dryomov 	linger_put(lreq);
488419079203SIlya Dryomov 	return ret;
488519079203SIlya Dryomov }
488619079203SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_notify);
488719079203SIlya Dryomov 
48883d14c5d2SYehuda Sadeh /*
4889b07d3c4bSIlya Dryomov  * Return the number of milliseconds since the watch was last
4890b07d3c4bSIlya Dryomov  * confirmed, or an error.  If there is an error, the watch is no
4891b07d3c4bSIlya Dryomov  * longer valid, and should be destroyed with ceph_osdc_unwatch().
4892b07d3c4bSIlya Dryomov  */
4893b07d3c4bSIlya Dryomov int ceph_osdc_watch_check(struct ceph_osd_client *osdc,
4894b07d3c4bSIlya Dryomov 			  struct ceph_osd_linger_request *lreq)
4895b07d3c4bSIlya Dryomov {
4896b07d3c4bSIlya Dryomov 	unsigned long stamp, age;
4897b07d3c4bSIlya Dryomov 	int ret;
4898b07d3c4bSIlya Dryomov 
4899b07d3c4bSIlya Dryomov 	down_read(&osdc->lock);
4900b07d3c4bSIlya Dryomov 	mutex_lock(&lreq->lock);
4901b07d3c4bSIlya Dryomov 	stamp = lreq->watch_valid_thru;
4902b07d3c4bSIlya Dryomov 	if (!list_empty(&lreq->pending_lworks)) {
4903b07d3c4bSIlya Dryomov 		struct linger_work *lwork =
4904b07d3c4bSIlya Dryomov 		    list_first_entry(&lreq->pending_lworks,
4905b07d3c4bSIlya Dryomov 				     struct linger_work,
4906b07d3c4bSIlya Dryomov 				     pending_item);
4907b07d3c4bSIlya Dryomov 
4908b07d3c4bSIlya Dryomov 		if (time_before(lwork->queued_stamp, stamp))
4909b07d3c4bSIlya Dryomov 			stamp = lwork->queued_stamp;
4910b07d3c4bSIlya Dryomov 	}
4911b07d3c4bSIlya Dryomov 	age = jiffies - stamp;
4912b07d3c4bSIlya Dryomov 	dout("%s lreq %p linger_id %llu age %lu last_error %d\n", __func__,
4913b07d3c4bSIlya Dryomov 	     lreq, lreq->linger_id, age, lreq->last_error);
4914b07d3c4bSIlya Dryomov 	/* we are truncating to msecs, so return a safe upper bound */
4915b07d3c4bSIlya Dryomov 	ret = lreq->last_error ?: 1 + jiffies_to_msecs(age);
4916b07d3c4bSIlya Dryomov 
4917b07d3c4bSIlya Dryomov 	mutex_unlock(&lreq->lock);
4918b07d3c4bSIlya Dryomov 	up_read(&osdc->lock);
4919b07d3c4bSIlya Dryomov 	return ret;
4920b07d3c4bSIlya Dryomov }
4921b07d3c4bSIlya Dryomov 
4922a4ed38d7SDouglas Fuller static int decode_watcher(void **p, void *end, struct ceph_watch_item *item)
4923a4ed38d7SDouglas Fuller {
4924a4ed38d7SDouglas Fuller 	u8 struct_v;
4925a4ed38d7SDouglas Fuller 	u32 struct_len;
4926a4ed38d7SDouglas Fuller 	int ret;
4927a4ed38d7SDouglas Fuller 
4928a4ed38d7SDouglas Fuller 	ret = ceph_start_decoding(p, end, 2, "watch_item_t",
4929a4ed38d7SDouglas Fuller 				  &struct_v, &struct_len);
4930a4ed38d7SDouglas Fuller 	if (ret)
493151fc7ab4SJeff Layton 		goto bad;
4932a4ed38d7SDouglas Fuller 
493351fc7ab4SJeff Layton 	ret = -EINVAL;
493451fc7ab4SJeff Layton 	ceph_decode_copy_safe(p, end, &item->name, sizeof(item->name), bad);
493551fc7ab4SJeff Layton 	ceph_decode_64_safe(p, end, item->cookie, bad);
493651fc7ab4SJeff Layton 	ceph_decode_skip_32(p, end, bad); /* skip timeout seconds */
493751fc7ab4SJeff Layton 
4938a4ed38d7SDouglas Fuller 	if (struct_v >= 2) {
493951fc7ab4SJeff Layton 		ret = ceph_decode_entity_addr(p, end, &item->addr);
494051fc7ab4SJeff Layton 		if (ret)
494151fc7ab4SJeff Layton 			goto bad;
494251fc7ab4SJeff Layton 	} else {
494351fc7ab4SJeff Layton 		ret = 0;
4944a4ed38d7SDouglas Fuller 	}
4945a4ed38d7SDouglas Fuller 
4946a4ed38d7SDouglas Fuller 	dout("%s %s%llu cookie %llu addr %s\n", __func__,
4947a4ed38d7SDouglas Fuller 	     ENTITY_NAME(item->name), item->cookie,
4948b726ec97SJeff Layton 	     ceph_pr_addr(&item->addr));
494951fc7ab4SJeff Layton bad:
495051fc7ab4SJeff Layton 	return ret;
4951a4ed38d7SDouglas Fuller }
4952a4ed38d7SDouglas Fuller 
4953a4ed38d7SDouglas Fuller static int decode_watchers(void **p, void *end,
4954a4ed38d7SDouglas Fuller 			   struct ceph_watch_item **watchers,
4955a4ed38d7SDouglas Fuller 			   u32 *num_watchers)
4956a4ed38d7SDouglas Fuller {
4957a4ed38d7SDouglas Fuller 	u8 struct_v;
4958a4ed38d7SDouglas Fuller 	u32 struct_len;
4959a4ed38d7SDouglas Fuller 	int i;
4960a4ed38d7SDouglas Fuller 	int ret;
4961a4ed38d7SDouglas Fuller 
4962a4ed38d7SDouglas Fuller 	ret = ceph_start_decoding(p, end, 1, "obj_list_watch_response_t",
4963a4ed38d7SDouglas Fuller 				  &struct_v, &struct_len);
4964a4ed38d7SDouglas Fuller 	if (ret)
4965a4ed38d7SDouglas Fuller 		return ret;
4966a4ed38d7SDouglas Fuller 
4967a4ed38d7SDouglas Fuller 	*num_watchers = ceph_decode_32(p);
4968a4ed38d7SDouglas Fuller 	*watchers = kcalloc(*num_watchers, sizeof(**watchers), GFP_NOIO);
4969a4ed38d7SDouglas Fuller 	if (!*watchers)
4970a4ed38d7SDouglas Fuller 		return -ENOMEM;
4971a4ed38d7SDouglas Fuller 
4972a4ed38d7SDouglas Fuller 	for (i = 0; i < *num_watchers; i++) {
4973a4ed38d7SDouglas Fuller 		ret = decode_watcher(p, end, *watchers + i);
4974a4ed38d7SDouglas Fuller 		if (ret) {
4975a4ed38d7SDouglas Fuller 			kfree(*watchers);
4976a4ed38d7SDouglas Fuller 			return ret;
4977a4ed38d7SDouglas Fuller 		}
4978a4ed38d7SDouglas Fuller 	}
4979a4ed38d7SDouglas Fuller 
4980a4ed38d7SDouglas Fuller 	return 0;
4981a4ed38d7SDouglas Fuller }
4982a4ed38d7SDouglas Fuller 
4983a4ed38d7SDouglas Fuller /*
4984a4ed38d7SDouglas Fuller  * On success, the caller is responsible for:
4985a4ed38d7SDouglas Fuller  *
4986a4ed38d7SDouglas Fuller  *     kfree(watchers);
4987a4ed38d7SDouglas Fuller  */
4988a4ed38d7SDouglas Fuller int ceph_osdc_list_watchers(struct ceph_osd_client *osdc,
4989a4ed38d7SDouglas Fuller 			    struct ceph_object_id *oid,
4990a4ed38d7SDouglas Fuller 			    struct ceph_object_locator *oloc,
4991a4ed38d7SDouglas Fuller 			    struct ceph_watch_item **watchers,
4992a4ed38d7SDouglas Fuller 			    u32 *num_watchers)
4993a4ed38d7SDouglas Fuller {
4994a4ed38d7SDouglas Fuller 	struct ceph_osd_request *req;
4995a4ed38d7SDouglas Fuller 	struct page **pages;
4996a4ed38d7SDouglas Fuller 	int ret;
4997a4ed38d7SDouglas Fuller 
4998a4ed38d7SDouglas Fuller 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4999a4ed38d7SDouglas Fuller 	if (!req)
5000a4ed38d7SDouglas Fuller 		return -ENOMEM;
5001a4ed38d7SDouglas Fuller 
5002a4ed38d7SDouglas Fuller 	ceph_oid_copy(&req->r_base_oid, oid);
5003a4ed38d7SDouglas Fuller 	ceph_oloc_copy(&req->r_base_oloc, oloc);
5004a4ed38d7SDouglas Fuller 	req->r_flags = CEPH_OSD_FLAG_READ;
5005a4ed38d7SDouglas Fuller 
5006a4ed38d7SDouglas Fuller 	pages = ceph_alloc_page_vector(1, GFP_NOIO);
5007a4ed38d7SDouglas Fuller 	if (IS_ERR(pages)) {
5008a4ed38d7SDouglas Fuller 		ret = PTR_ERR(pages);
5009a4ed38d7SDouglas Fuller 		goto out_put_req;
5010a4ed38d7SDouglas Fuller 	}
5011a4ed38d7SDouglas Fuller 
5012a4ed38d7SDouglas Fuller 	osd_req_op_init(req, 0, CEPH_OSD_OP_LIST_WATCHERS, 0);
5013a4ed38d7SDouglas Fuller 	ceph_osd_data_pages_init(osd_req_op_data(req, 0, list_watchers,
5014a4ed38d7SDouglas Fuller 						 response_data),
5015a4ed38d7SDouglas Fuller 				 pages, PAGE_SIZE, 0, false, true);
5016a4ed38d7SDouglas Fuller 
501726f887e0SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
501826f887e0SIlya Dryomov 	if (ret)
501926f887e0SIlya Dryomov 		goto out_put_req;
502026f887e0SIlya Dryomov 
5021a4ed38d7SDouglas Fuller 	ceph_osdc_start_request(osdc, req, false);
5022a4ed38d7SDouglas Fuller 	ret = ceph_osdc_wait_request(osdc, req);
5023a4ed38d7SDouglas Fuller 	if (ret >= 0) {
5024a4ed38d7SDouglas Fuller 		void *p = page_address(pages[0]);
5025a4ed38d7SDouglas Fuller 		void *const end = p + req->r_ops[0].outdata_len;
5026a4ed38d7SDouglas Fuller 
5027a4ed38d7SDouglas Fuller 		ret = decode_watchers(&p, end, watchers, num_watchers);
5028a4ed38d7SDouglas Fuller 	}
5029a4ed38d7SDouglas Fuller 
5030a4ed38d7SDouglas Fuller out_put_req:
5031a4ed38d7SDouglas Fuller 	ceph_osdc_put_request(req);
5032a4ed38d7SDouglas Fuller 	return ret;
5033a4ed38d7SDouglas Fuller }
5034a4ed38d7SDouglas Fuller EXPORT_SYMBOL(ceph_osdc_list_watchers);
5035a4ed38d7SDouglas Fuller 
5036b07d3c4bSIlya Dryomov /*
5037dd935f44SJosh Durgin  * Call all pending notify callbacks - for use after a watch is
5038dd935f44SJosh Durgin  * unregistered, to make sure no more callbacks for it will be invoked
5039dd935f44SJosh Durgin  */
5040f6479449Sstephen hemminger void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
5041dd935f44SJosh Durgin {
504299d16943SIlya Dryomov 	dout("%s osdc %p\n", __func__, osdc);
5043dd935f44SJosh Durgin 	flush_workqueue(osdc->notify_wq);
5044dd935f44SJosh Durgin }
5045dd935f44SJosh Durgin EXPORT_SYMBOL(ceph_osdc_flush_notifies);
5046dd935f44SJosh Durgin 
50477cca78c9SIlya Dryomov void ceph_osdc_maybe_request_map(struct ceph_osd_client *osdc)
50487cca78c9SIlya Dryomov {
50497cca78c9SIlya Dryomov 	down_read(&osdc->lock);
50507cca78c9SIlya Dryomov 	maybe_request_map(osdc);
50517cca78c9SIlya Dryomov 	up_read(&osdc->lock);
50527cca78c9SIlya Dryomov }
50537cca78c9SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_maybe_request_map);
5054dd935f44SJosh Durgin 
5055dd935f44SJosh Durgin /*
5056428a7158SDouglas Fuller  * Execute an OSD class method on an object.
5057428a7158SDouglas Fuller  *
5058428a7158SDouglas Fuller  * @flags: CEPH_OSD_FLAG_*
50592544a020SIlya Dryomov  * @resp_len: in/out param for reply length
5060428a7158SDouglas Fuller  */
5061428a7158SDouglas Fuller int ceph_osdc_call(struct ceph_osd_client *osdc,
5062428a7158SDouglas Fuller 		   struct ceph_object_id *oid,
5063428a7158SDouglas Fuller 		   struct ceph_object_locator *oloc,
5064428a7158SDouglas Fuller 		   const char *class, const char *method,
5065428a7158SDouglas Fuller 		   unsigned int flags,
5066428a7158SDouglas Fuller 		   struct page *req_page, size_t req_len,
506768ada915SIlya Dryomov 		   struct page **resp_pages, size_t *resp_len)
5068428a7158SDouglas Fuller {
5069428a7158SDouglas Fuller 	struct ceph_osd_request *req;
5070428a7158SDouglas Fuller 	int ret;
5071428a7158SDouglas Fuller 
507268ada915SIlya Dryomov 	if (req_len > PAGE_SIZE)
50732544a020SIlya Dryomov 		return -E2BIG;
50742544a020SIlya Dryomov 
5075428a7158SDouglas Fuller 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
5076428a7158SDouglas Fuller 	if (!req)
5077428a7158SDouglas Fuller 		return -ENOMEM;
5078428a7158SDouglas Fuller 
5079428a7158SDouglas Fuller 	ceph_oid_copy(&req->r_base_oid, oid);
5080428a7158SDouglas Fuller 	ceph_oloc_copy(&req->r_base_oloc, oloc);
5081428a7158SDouglas Fuller 	req->r_flags = flags;
5082428a7158SDouglas Fuller 
508324639ce5SIlya Dryomov 	ret = osd_req_op_cls_init(req, 0, class, method);
5084fe943d50SChengguang Xu 	if (ret)
5085fe943d50SChengguang Xu 		goto out_put_req;
5086fe943d50SChengguang Xu 
5087428a7158SDouglas Fuller 	if (req_page)
5088428a7158SDouglas Fuller 		osd_req_op_cls_request_data_pages(req, 0, &req_page, req_len,
5089428a7158SDouglas Fuller 						  0, false, false);
509068ada915SIlya Dryomov 	if (resp_pages)
509168ada915SIlya Dryomov 		osd_req_op_cls_response_data_pages(req, 0, resp_pages,
50922544a020SIlya Dryomov 						   *resp_len, 0, false, false);
5093428a7158SDouglas Fuller 
509426f887e0SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
509526f887e0SIlya Dryomov 	if (ret)
509626f887e0SIlya Dryomov 		goto out_put_req;
509726f887e0SIlya Dryomov 
5098428a7158SDouglas Fuller 	ceph_osdc_start_request(osdc, req, false);
5099428a7158SDouglas Fuller 	ret = ceph_osdc_wait_request(osdc, req);
5100428a7158SDouglas Fuller 	if (ret >= 0) {
5101428a7158SDouglas Fuller 		ret = req->r_ops[0].rval;
510268ada915SIlya Dryomov 		if (resp_pages)
5103428a7158SDouglas Fuller 			*resp_len = req->r_ops[0].outdata_len;
5104428a7158SDouglas Fuller 	}
5105428a7158SDouglas Fuller 
5106428a7158SDouglas Fuller out_put_req:
5107428a7158SDouglas Fuller 	ceph_osdc_put_request(req);
5108428a7158SDouglas Fuller 	return ret;
5109428a7158SDouglas Fuller }
5110428a7158SDouglas Fuller EXPORT_SYMBOL(ceph_osdc_call);
5111428a7158SDouglas Fuller 
5112428a7158SDouglas Fuller /*
5113120a75eaSYan, Zheng  * reset all osd connections
5114120a75eaSYan, Zheng  */
5115120a75eaSYan, Zheng void ceph_osdc_reopen_osds(struct ceph_osd_client *osdc)
5116120a75eaSYan, Zheng {
5117120a75eaSYan, Zheng 	struct rb_node *n;
5118120a75eaSYan, Zheng 
5119120a75eaSYan, Zheng 	down_write(&osdc->lock);
5120120a75eaSYan, Zheng 	for (n = rb_first(&osdc->osds); n; ) {
5121120a75eaSYan, Zheng 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
5122120a75eaSYan, Zheng 
5123120a75eaSYan, Zheng 		n = rb_next(n);
5124120a75eaSYan, Zheng 		if (!reopen_osd(osd))
5125120a75eaSYan, Zheng 			kick_osd_requests(osd);
5126120a75eaSYan, Zheng 	}
5127120a75eaSYan, Zheng 	up_write(&osdc->lock);
5128120a75eaSYan, Zheng }
5129120a75eaSYan, Zheng 
5130120a75eaSYan, Zheng /*
51313d14c5d2SYehuda Sadeh  * init, shutdown
51323d14c5d2SYehuda Sadeh  */
51333d14c5d2SYehuda Sadeh int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
51343d14c5d2SYehuda Sadeh {
51353d14c5d2SYehuda Sadeh 	int err;
51363d14c5d2SYehuda Sadeh 
51373d14c5d2SYehuda Sadeh 	dout("init\n");
51383d14c5d2SYehuda Sadeh 	osdc->client = client;
51395aea3dcdSIlya Dryomov 	init_rwsem(&osdc->lock);
51403d14c5d2SYehuda Sadeh 	osdc->osds = RB_ROOT;
51413d14c5d2SYehuda Sadeh 	INIT_LIST_HEAD(&osdc->osd_lru);
51429dd2845cSIlya Dryomov 	spin_lock_init(&osdc->osd_lru_lock);
51435aea3dcdSIlya Dryomov 	osd_init(&osdc->homeless_osd);
51445aea3dcdSIlya Dryomov 	osdc->homeless_osd.o_osdc = osdc;
51455aea3dcdSIlya Dryomov 	osdc->homeless_osd.o_osd = CEPH_HOMELESS_OSD;
5146264048afSIlya Dryomov 	osdc->last_linger_id = CEPH_LINGER_ID_START;
5147922dab61SIlya Dryomov 	osdc->linger_requests = RB_ROOT;
51484609245eSIlya Dryomov 	osdc->map_checks = RB_ROOT;
51494609245eSIlya Dryomov 	osdc->linger_map_checks = RB_ROOT;
51503d14c5d2SYehuda Sadeh 	INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
51513d14c5d2SYehuda Sadeh 	INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
51523d14c5d2SYehuda Sadeh 
51533d14c5d2SYehuda Sadeh 	err = -ENOMEM;
5154e5253a7bSIlya Dryomov 	osdc->osdmap = ceph_osdmap_alloc();
5155e5253a7bSIlya Dryomov 	if (!osdc->osdmap)
5156e5253a7bSIlya Dryomov 		goto out;
5157e5253a7bSIlya Dryomov 
51589e767adbSIlya Dryomov 	osdc->req_mempool = mempool_create_slab_pool(10,
51599e767adbSIlya Dryomov 						     ceph_osd_request_cache);
51603d14c5d2SYehuda Sadeh 	if (!osdc->req_mempool)
5161e5253a7bSIlya Dryomov 		goto out_map;
51623d14c5d2SYehuda Sadeh 
5163d50b409fSSage Weil 	err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
51640d9c1ab3SIlya Dryomov 				PAGE_SIZE, CEPH_OSD_SLAB_OPS, 10, "osd_op");
51653d14c5d2SYehuda Sadeh 	if (err < 0)
51663d14c5d2SYehuda Sadeh 		goto out_mempool;
5167d50b409fSSage Weil 	err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
51680d9c1ab3SIlya Dryomov 				PAGE_SIZE, CEPH_OSD_SLAB_OPS, 10,
51690d9c1ab3SIlya Dryomov 				"osd_op_reply");
51703d14c5d2SYehuda Sadeh 	if (err < 0)
51713d14c5d2SYehuda Sadeh 		goto out_msgpool;
5172a40c4f10SYehuda Sadeh 
5173dbcae088SDan Carpenter 	err = -ENOMEM;
5174a40c4f10SYehuda Sadeh 	osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
5175dbcae088SDan Carpenter 	if (!osdc->notify_wq)
5176c172ec5cSIlya Dryomov 		goto out_msgpool_reply;
5177c172ec5cSIlya Dryomov 
517888bc1922SIlya Dryomov 	osdc->completion_wq = create_singlethread_workqueue("ceph-completion");
517988bc1922SIlya Dryomov 	if (!osdc->completion_wq)
518088bc1922SIlya Dryomov 		goto out_notify_wq;
518188bc1922SIlya Dryomov 
5182fbca9635SIlya Dryomov 	schedule_delayed_work(&osdc->timeout_work,
5183fbca9635SIlya Dryomov 			      osdc->client->options->osd_keepalive_timeout);
5184b37ee1b9SIlya Dryomov 	schedule_delayed_work(&osdc->osds_timeout_work,
5185b37ee1b9SIlya Dryomov 	    round_jiffies_relative(osdc->client->options->osd_idle_ttl));
5186b37ee1b9SIlya Dryomov 
51873d14c5d2SYehuda Sadeh 	return 0;
51883d14c5d2SYehuda Sadeh 
518988bc1922SIlya Dryomov out_notify_wq:
519088bc1922SIlya Dryomov 	destroy_workqueue(osdc->notify_wq);
5191c172ec5cSIlya Dryomov out_msgpool_reply:
5192c172ec5cSIlya Dryomov 	ceph_msgpool_destroy(&osdc->msgpool_op_reply);
51933d14c5d2SYehuda Sadeh out_msgpool:
51943d14c5d2SYehuda Sadeh 	ceph_msgpool_destroy(&osdc->msgpool_op);
51953d14c5d2SYehuda Sadeh out_mempool:
51963d14c5d2SYehuda Sadeh 	mempool_destroy(osdc->req_mempool);
5197e5253a7bSIlya Dryomov out_map:
5198e5253a7bSIlya Dryomov 	ceph_osdmap_destroy(osdc->osdmap);
51993d14c5d2SYehuda Sadeh out:
52003d14c5d2SYehuda Sadeh 	return err;
52013d14c5d2SYehuda Sadeh }
52023d14c5d2SYehuda Sadeh 
52033d14c5d2SYehuda Sadeh void ceph_osdc_stop(struct ceph_osd_client *osdc)
52043d14c5d2SYehuda Sadeh {
520588bc1922SIlya Dryomov 	destroy_workqueue(osdc->completion_wq);
5206a40c4f10SYehuda Sadeh 	destroy_workqueue(osdc->notify_wq);
52073d14c5d2SYehuda Sadeh 	cancel_delayed_work_sync(&osdc->timeout_work);
52083d14c5d2SYehuda Sadeh 	cancel_delayed_work_sync(&osdc->osds_timeout_work);
520942a2c09fSIlya Dryomov 
52105aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
521142a2c09fSIlya Dryomov 	while (!RB_EMPTY_ROOT(&osdc->osds)) {
521242a2c09fSIlya Dryomov 		struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
521342a2c09fSIlya Dryomov 						struct ceph_osd, o_node);
52145aea3dcdSIlya Dryomov 		close_osd(osd);
521542a2c09fSIlya Dryomov 	}
52165aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
521702113a0fSElena Reshetova 	WARN_ON(refcount_read(&osdc->homeless_osd.o_ref) != 1);
52185aea3dcdSIlya Dryomov 	osd_cleanup(&osdc->homeless_osd);
52195aea3dcdSIlya Dryomov 
52205aea3dcdSIlya Dryomov 	WARN_ON(!list_empty(&osdc->osd_lru));
5221922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_requests));
52224609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osdc->map_checks));
52234609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_map_checks));
52245aea3dcdSIlya Dryomov 	WARN_ON(atomic_read(&osdc->num_requests));
52255aea3dcdSIlya Dryomov 	WARN_ON(atomic_read(&osdc->num_homeless));
522642a2c09fSIlya Dryomov 
52273d14c5d2SYehuda Sadeh 	ceph_osdmap_destroy(osdc->osdmap);
52283d14c5d2SYehuda Sadeh 	mempool_destroy(osdc->req_mempool);
52293d14c5d2SYehuda Sadeh 	ceph_msgpool_destroy(&osdc->msgpool_op);
52303d14c5d2SYehuda Sadeh 	ceph_msgpool_destroy(&osdc->msgpool_op_reply);
52313d14c5d2SYehuda Sadeh }
52323d14c5d2SYehuda Sadeh 
52333d14c5d2SYehuda Sadeh /*
52343d14c5d2SYehuda Sadeh  * Read some contiguous pages.  If we cross a stripe boundary, shorten
52353d14c5d2SYehuda Sadeh  * *plen.  Return number of bytes read, or error.
52363d14c5d2SYehuda Sadeh  */
52373d14c5d2SYehuda Sadeh int ceph_osdc_readpages(struct ceph_osd_client *osdc,
52383d14c5d2SYehuda Sadeh 			struct ceph_vino vino, struct ceph_file_layout *layout,
52393d14c5d2SYehuda Sadeh 			u64 off, u64 *plen,
52403d14c5d2SYehuda Sadeh 			u32 truncate_seq, u64 truncate_size,
5241b7495fc2SSage Weil 			struct page **pages, int num_pages, int page_align)
52423d14c5d2SYehuda Sadeh {
52433d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
52443d14c5d2SYehuda Sadeh 	int rc = 0;
52453d14c5d2SYehuda Sadeh 
52463d14c5d2SYehuda Sadeh 	dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
52473d14c5d2SYehuda Sadeh 	     vino.snap, off, *plen);
5248715e4cd4SYan, Zheng 	req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
52493d14c5d2SYehuda Sadeh 				    CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
5250acead002SAlex Elder 				    NULL, truncate_seq, truncate_size,
5251153e5167SAlex Elder 				    false);
52526816282dSSage Weil 	if (IS_ERR(req))
52536816282dSSage Weil 		return PTR_ERR(req);
52543d14c5d2SYehuda Sadeh 
52553d14c5d2SYehuda Sadeh 	/* it may be a short read due to an object boundary */
5256406e2c9fSAlex Elder 	osd_req_op_extent_osd_data_pages(req, 0,
5257a4ce40a9SAlex Elder 				pages, *plen, page_align, false, false);
52583d14c5d2SYehuda Sadeh 
5259e0c59487SAlex Elder 	dout("readpages  final extent is %llu~%llu (%llu bytes align %d)\n",
526043bfe5deSAlex Elder 	     off, *plen, *plen, page_align);
52613d14c5d2SYehuda Sadeh 
52623d14c5d2SYehuda Sadeh 	rc = ceph_osdc_start_request(osdc, req, false);
52633d14c5d2SYehuda Sadeh 	if (!rc)
52643d14c5d2SYehuda Sadeh 		rc = ceph_osdc_wait_request(osdc, req);
52653d14c5d2SYehuda Sadeh 
52663d14c5d2SYehuda Sadeh 	ceph_osdc_put_request(req);
52673d14c5d2SYehuda Sadeh 	dout("readpages result %d\n", rc);
52683d14c5d2SYehuda Sadeh 	return rc;
52693d14c5d2SYehuda Sadeh }
52703d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_readpages);
52713d14c5d2SYehuda Sadeh 
52723d14c5d2SYehuda Sadeh /*
52733d14c5d2SYehuda Sadeh  * do a synchronous write on N pages
52743d14c5d2SYehuda Sadeh  */
52753d14c5d2SYehuda Sadeh int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
52763d14c5d2SYehuda Sadeh 			 struct ceph_file_layout *layout,
52773d14c5d2SYehuda Sadeh 			 struct ceph_snap_context *snapc,
52783d14c5d2SYehuda Sadeh 			 u64 off, u64 len,
52793d14c5d2SYehuda Sadeh 			 u32 truncate_seq, u64 truncate_size,
5280fac02ddfSArnd Bergmann 			 struct timespec64 *mtime,
528124808826SAlex Elder 			 struct page **pages, int num_pages)
52823d14c5d2SYehuda Sadeh {
52833d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
52843d14c5d2SYehuda Sadeh 	int rc = 0;
5285b7495fc2SSage Weil 	int page_align = off & ~PAGE_MASK;
52863d14c5d2SYehuda Sadeh 
5287715e4cd4SYan, Zheng 	req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
528854ea0046SIlya Dryomov 				    CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
5289acead002SAlex Elder 				    snapc, truncate_seq, truncate_size,
5290153e5167SAlex Elder 				    true);
52916816282dSSage Weil 	if (IS_ERR(req))
52926816282dSSage Weil 		return PTR_ERR(req);
52933d14c5d2SYehuda Sadeh 
52943d14c5d2SYehuda Sadeh 	/* it may be a short write due to an object boundary */
5295406e2c9fSAlex Elder 	osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
529643bfe5deSAlex Elder 				false, false);
529743bfe5deSAlex Elder 	dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
52983d14c5d2SYehuda Sadeh 
5299bb873b53SIlya Dryomov 	req->r_mtime = *mtime;
530087f979d3SAlex Elder 	rc = ceph_osdc_start_request(osdc, req, true);
53013d14c5d2SYehuda Sadeh 	if (!rc)
53023d14c5d2SYehuda Sadeh 		rc = ceph_osdc_wait_request(osdc, req);
53033d14c5d2SYehuda Sadeh 
53043d14c5d2SYehuda Sadeh 	ceph_osdc_put_request(req);
53053d14c5d2SYehuda Sadeh 	if (rc == 0)
53063d14c5d2SYehuda Sadeh 		rc = len;
53073d14c5d2SYehuda Sadeh 	dout("writepages result %d\n", rc);
53083d14c5d2SYehuda Sadeh 	return rc;
53093d14c5d2SYehuda Sadeh }
53103d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_writepages);
53113d14c5d2SYehuda Sadeh 
531223ddf9beSLuis Henriques static int osd_req_op_copy_from_init(struct ceph_osd_request *req,
531323ddf9beSLuis Henriques 				     u64 src_snapid, u64 src_version,
531423ddf9beSLuis Henriques 				     struct ceph_object_id *src_oid,
531523ddf9beSLuis Henriques 				     struct ceph_object_locator *src_oloc,
531623ddf9beSLuis Henriques 				     u32 src_fadvise_flags,
531723ddf9beSLuis Henriques 				     u32 dst_fadvise_flags,
531878beb0ffSLuis Henriques 				     u32 truncate_seq, u64 truncate_size,
531923ddf9beSLuis Henriques 				     u8 copy_from_flags)
532023ddf9beSLuis Henriques {
532123ddf9beSLuis Henriques 	struct ceph_osd_req_op *op;
532223ddf9beSLuis Henriques 	struct page **pages;
532323ddf9beSLuis Henriques 	void *p, *end;
532423ddf9beSLuis Henriques 
532523ddf9beSLuis Henriques 	pages = ceph_alloc_page_vector(1, GFP_KERNEL);
532623ddf9beSLuis Henriques 	if (IS_ERR(pages))
532723ddf9beSLuis Henriques 		return PTR_ERR(pages);
532823ddf9beSLuis Henriques 
532978beb0ffSLuis Henriques 	op = _osd_req_op_init(req, 0, CEPH_OSD_OP_COPY_FROM2,
533078beb0ffSLuis Henriques 			      dst_fadvise_flags);
533123ddf9beSLuis Henriques 	op->copy_from.snapid = src_snapid;
533223ddf9beSLuis Henriques 	op->copy_from.src_version = src_version;
533323ddf9beSLuis Henriques 	op->copy_from.flags = copy_from_flags;
533423ddf9beSLuis Henriques 	op->copy_from.src_fadvise_flags = src_fadvise_flags;
533523ddf9beSLuis Henriques 
533623ddf9beSLuis Henriques 	p = page_address(pages[0]);
533723ddf9beSLuis Henriques 	end = p + PAGE_SIZE;
533823ddf9beSLuis Henriques 	ceph_encode_string(&p, end, src_oid->name, src_oid->name_len);
533923ddf9beSLuis Henriques 	encode_oloc(&p, end, src_oloc);
534078beb0ffSLuis Henriques 	ceph_encode_32(&p, truncate_seq);
534178beb0ffSLuis Henriques 	ceph_encode_64(&p, truncate_size);
534223ddf9beSLuis Henriques 	op->indata_len = PAGE_SIZE - (end - p);
534323ddf9beSLuis Henriques 
534423ddf9beSLuis Henriques 	ceph_osd_data_pages_init(&op->copy_from.osd_data, pages,
534523ddf9beSLuis Henriques 				 op->indata_len, 0, false, true);
534623ddf9beSLuis Henriques 	return 0;
534723ddf9beSLuis Henriques }
534823ddf9beSLuis Henriques 
534923ddf9beSLuis Henriques int ceph_osdc_copy_from(struct ceph_osd_client *osdc,
535023ddf9beSLuis Henriques 			u64 src_snapid, u64 src_version,
535123ddf9beSLuis Henriques 			struct ceph_object_id *src_oid,
535223ddf9beSLuis Henriques 			struct ceph_object_locator *src_oloc,
535323ddf9beSLuis Henriques 			u32 src_fadvise_flags,
535423ddf9beSLuis Henriques 			struct ceph_object_id *dst_oid,
535523ddf9beSLuis Henriques 			struct ceph_object_locator *dst_oloc,
535623ddf9beSLuis Henriques 			u32 dst_fadvise_flags,
535778beb0ffSLuis Henriques 			u32 truncate_seq, u64 truncate_size,
535823ddf9beSLuis Henriques 			u8 copy_from_flags)
535923ddf9beSLuis Henriques {
536023ddf9beSLuis Henriques 	struct ceph_osd_request *req;
536123ddf9beSLuis Henriques 	int ret;
536223ddf9beSLuis Henriques 
536323ddf9beSLuis Henriques 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_KERNEL);
536423ddf9beSLuis Henriques 	if (!req)
536523ddf9beSLuis Henriques 		return -ENOMEM;
536623ddf9beSLuis Henriques 
536723ddf9beSLuis Henriques 	req->r_flags = CEPH_OSD_FLAG_WRITE;
536823ddf9beSLuis Henriques 
536923ddf9beSLuis Henriques 	ceph_oloc_copy(&req->r_t.base_oloc, dst_oloc);
537023ddf9beSLuis Henriques 	ceph_oid_copy(&req->r_t.base_oid, dst_oid);
537123ddf9beSLuis Henriques 
537223ddf9beSLuis Henriques 	ret = osd_req_op_copy_from_init(req, src_snapid, src_version, src_oid,
537323ddf9beSLuis Henriques 					src_oloc, src_fadvise_flags,
537478beb0ffSLuis Henriques 					dst_fadvise_flags, truncate_seq,
537578beb0ffSLuis Henriques 					truncate_size, copy_from_flags);
537623ddf9beSLuis Henriques 	if (ret)
537723ddf9beSLuis Henriques 		goto out;
537823ddf9beSLuis Henriques 
537923ddf9beSLuis Henriques 	ret = ceph_osdc_alloc_messages(req, GFP_KERNEL);
538023ddf9beSLuis Henriques 	if (ret)
538123ddf9beSLuis Henriques 		goto out;
538223ddf9beSLuis Henriques 
538323ddf9beSLuis Henriques 	ceph_osdc_start_request(osdc, req, false);
538423ddf9beSLuis Henriques 	ret = ceph_osdc_wait_request(osdc, req);
538523ddf9beSLuis Henriques 
538623ddf9beSLuis Henriques out:
538723ddf9beSLuis Henriques 	ceph_osdc_put_request(req);
538823ddf9beSLuis Henriques 	return ret;
538923ddf9beSLuis Henriques }
539023ddf9beSLuis Henriques EXPORT_SYMBOL(ceph_osdc_copy_from);
539123ddf9beSLuis Henriques 
539257a35dfbSChengguang Xu int __init ceph_osdc_setup(void)
53935522ae0bSAlex Elder {
53943f1af42aSIlya Dryomov 	size_t size = sizeof(struct ceph_osd_request) +
53953f1af42aSIlya Dryomov 	    CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
53963f1af42aSIlya Dryomov 
53975522ae0bSAlex Elder 	BUG_ON(ceph_osd_request_cache);
53983f1af42aSIlya Dryomov 	ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
53993f1af42aSIlya Dryomov 						   0, 0, NULL);
54005522ae0bSAlex Elder 
54015522ae0bSAlex Elder 	return ceph_osd_request_cache ? 0 : -ENOMEM;
54025522ae0bSAlex Elder }
54035522ae0bSAlex Elder 
54045522ae0bSAlex Elder void ceph_osdc_cleanup(void)
54055522ae0bSAlex Elder {
54065522ae0bSAlex Elder 	BUG_ON(!ceph_osd_request_cache);
54075522ae0bSAlex Elder 	kmem_cache_destroy(ceph_osd_request_cache);
54085522ae0bSAlex Elder 	ceph_osd_request_cache = NULL;
54095522ae0bSAlex Elder }
54105522ae0bSAlex Elder 
54113d14c5d2SYehuda Sadeh /*
54123d14c5d2SYehuda Sadeh  * handle incoming message
54133d14c5d2SYehuda Sadeh  */
54143d14c5d2SYehuda Sadeh static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
54153d14c5d2SYehuda Sadeh {
54163d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
54175aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
54183d14c5d2SYehuda Sadeh 	int type = le16_to_cpu(msg->hdr.type);
54193d14c5d2SYehuda Sadeh 
54203d14c5d2SYehuda Sadeh 	switch (type) {
54213d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_MAP:
54223d14c5d2SYehuda Sadeh 		ceph_osdc_handle_map(osdc, msg);
54233d14c5d2SYehuda Sadeh 		break;
54243d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_OPREPLY:
54255aea3dcdSIlya Dryomov 		handle_reply(osd, msg);
54263d14c5d2SYehuda Sadeh 		break;
5427a02a946dSIlya Dryomov 	case CEPH_MSG_OSD_BACKOFF:
5428a02a946dSIlya Dryomov 		handle_backoff(osd, msg);
5429a02a946dSIlya Dryomov 		break;
5430a40c4f10SYehuda Sadeh 	case CEPH_MSG_WATCH_NOTIFY:
5431a40c4f10SYehuda Sadeh 		handle_watch_notify(osdc, msg);
5432a40c4f10SYehuda Sadeh 		break;
54333d14c5d2SYehuda Sadeh 
54343d14c5d2SYehuda Sadeh 	default:
54353d14c5d2SYehuda Sadeh 		pr_err("received unknown message type %d %s\n", type,
54363d14c5d2SYehuda Sadeh 		       ceph_msg_type_name(type));
54373d14c5d2SYehuda Sadeh 	}
54385aea3dcdSIlya Dryomov 
54393d14c5d2SYehuda Sadeh 	ceph_msg_put(msg);
54403d14c5d2SYehuda Sadeh }
54413d14c5d2SYehuda Sadeh 
54423d14c5d2SYehuda Sadeh /*
5443d15f9d69SIlya Dryomov  * Lookup and return message for incoming reply.  Don't try to do
5444d15f9d69SIlya Dryomov  * anything about a larger than preallocated data portion of the
5445d15f9d69SIlya Dryomov  * message at the moment - for now, just skip the message.
54463d14c5d2SYehuda Sadeh  */
54473d14c5d2SYehuda Sadeh static struct ceph_msg *get_reply(struct ceph_connection *con,
54483d14c5d2SYehuda Sadeh 				  struct ceph_msg_header *hdr,
54493d14c5d2SYehuda Sadeh 				  int *skip)
54503d14c5d2SYehuda Sadeh {
54513d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
54523d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = osd->o_osdc;
54535aea3dcdSIlya Dryomov 	struct ceph_msg *m = NULL;
54543d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
54553f0a4ac5SIlya Dryomov 	int front_len = le32_to_cpu(hdr->front_len);
54563d14c5d2SYehuda Sadeh 	int data_len = le32_to_cpu(hdr->data_len);
54575aea3dcdSIlya Dryomov 	u64 tid = le64_to_cpu(hdr->tid);
54583d14c5d2SYehuda Sadeh 
54595aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
54605aea3dcdSIlya Dryomov 	if (!osd_registered(osd)) {
54615aea3dcdSIlya Dryomov 		dout("%s osd%d unknown, skipping\n", __func__, osd->o_osd);
54625aea3dcdSIlya Dryomov 		*skip = 1;
54635aea3dcdSIlya Dryomov 		goto out_unlock_osdc;
54645aea3dcdSIlya Dryomov 	}
54655aea3dcdSIlya Dryomov 	WARN_ON(osd->o_osd != le64_to_cpu(hdr->src.num));
54665aea3dcdSIlya Dryomov 
54675aea3dcdSIlya Dryomov 	mutex_lock(&osd->lock);
54685aea3dcdSIlya Dryomov 	req = lookup_request(&osd->o_requests, tid);
54693d14c5d2SYehuda Sadeh 	if (!req) {
5470cd8140c6SIlya Dryomov 		dout("%s osd%d tid %llu unknown, skipping\n", __func__,
5471cd8140c6SIlya Dryomov 		     osd->o_osd, tid);
5472d15f9d69SIlya Dryomov 		*skip = 1;
54735aea3dcdSIlya Dryomov 		goto out_unlock_session;
54743d14c5d2SYehuda Sadeh 	}
54753d14c5d2SYehuda Sadeh 
54768921d114SAlex Elder 	ceph_msg_revoke_incoming(req->r_reply);
54773d14c5d2SYehuda Sadeh 
5478f2be82b0SIlya Dryomov 	if (front_len > req->r_reply->front_alloc_len) {
5479d15f9d69SIlya Dryomov 		pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
5480d15f9d69SIlya Dryomov 			__func__, osd->o_osd, req->r_tid, front_len,
5481d15f9d69SIlya Dryomov 			req->r_reply->front_alloc_len);
54823f0a4ac5SIlya Dryomov 		m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
54833f0a4ac5SIlya Dryomov 				 false);
54843d14c5d2SYehuda Sadeh 		if (!m)
54855aea3dcdSIlya Dryomov 			goto out_unlock_session;
54863d14c5d2SYehuda Sadeh 		ceph_msg_put(req->r_reply);
54873d14c5d2SYehuda Sadeh 		req->r_reply = m;
54883d14c5d2SYehuda Sadeh 	}
54893d14c5d2SYehuda Sadeh 
5490d15f9d69SIlya Dryomov 	if (data_len > req->r_reply->data_length) {
5491d15f9d69SIlya Dryomov 		pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
5492d15f9d69SIlya Dryomov 			__func__, osd->o_osd, req->r_tid, data_len,
5493d15f9d69SIlya Dryomov 			req->r_reply->data_length);
54943d14c5d2SYehuda Sadeh 		m = NULL;
5495d15f9d69SIlya Dryomov 		*skip = 1;
54965aea3dcdSIlya Dryomov 		goto out_unlock_session;
54973d14c5d2SYehuda Sadeh 	}
5498d15f9d69SIlya Dryomov 
5499d15f9d69SIlya Dryomov 	m = ceph_msg_get(req->r_reply);
55003d14c5d2SYehuda Sadeh 	dout("get_reply tid %lld %p\n", tid, m);
55013d14c5d2SYehuda Sadeh 
55025aea3dcdSIlya Dryomov out_unlock_session:
55035aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
55045aea3dcdSIlya Dryomov out_unlock_osdc:
55055aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
55063d14c5d2SYehuda Sadeh 	return m;
55073d14c5d2SYehuda Sadeh }
55083d14c5d2SYehuda Sadeh 
550919079203SIlya Dryomov /*
551019079203SIlya Dryomov  * TODO: switch to a msg-owned pagelist
551119079203SIlya Dryomov  */
551219079203SIlya Dryomov static struct ceph_msg *alloc_msg_with_page_vector(struct ceph_msg_header *hdr)
551319079203SIlya Dryomov {
551419079203SIlya Dryomov 	struct ceph_msg *m;
551519079203SIlya Dryomov 	int type = le16_to_cpu(hdr->type);
551619079203SIlya Dryomov 	u32 front_len = le32_to_cpu(hdr->front_len);
551719079203SIlya Dryomov 	u32 data_len = le32_to_cpu(hdr->data_len);
551819079203SIlya Dryomov 
55190d9c1ab3SIlya Dryomov 	m = ceph_msg_new2(type, front_len, 1, GFP_NOIO, false);
552019079203SIlya Dryomov 	if (!m)
552119079203SIlya Dryomov 		return NULL;
552219079203SIlya Dryomov 
552319079203SIlya Dryomov 	if (data_len) {
552419079203SIlya Dryomov 		struct page **pages;
552519079203SIlya Dryomov 		struct ceph_osd_data osd_data;
552619079203SIlya Dryomov 
552719079203SIlya Dryomov 		pages = ceph_alloc_page_vector(calc_pages_for(0, data_len),
552819079203SIlya Dryomov 					       GFP_NOIO);
5529c22e853aSWei Yongjun 		if (IS_ERR(pages)) {
553019079203SIlya Dryomov 			ceph_msg_put(m);
553119079203SIlya Dryomov 			return NULL;
553219079203SIlya Dryomov 		}
553319079203SIlya Dryomov 
553419079203SIlya Dryomov 		ceph_osd_data_pages_init(&osd_data, pages, data_len, 0, false,
553519079203SIlya Dryomov 					 false);
553619079203SIlya Dryomov 		ceph_osdc_msg_data_add(m, &osd_data);
553719079203SIlya Dryomov 	}
553819079203SIlya Dryomov 
553919079203SIlya Dryomov 	return m;
554019079203SIlya Dryomov }
554119079203SIlya Dryomov 
55423d14c5d2SYehuda Sadeh static struct ceph_msg *alloc_msg(struct ceph_connection *con,
55433d14c5d2SYehuda Sadeh 				  struct ceph_msg_header *hdr,
55443d14c5d2SYehuda Sadeh 				  int *skip)
55453d14c5d2SYehuda Sadeh {
55463d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
55473d14c5d2SYehuda Sadeh 	int type = le16_to_cpu(hdr->type);
55483d14c5d2SYehuda Sadeh 
55491c20f2d2SAlex Elder 	*skip = 0;
55503d14c5d2SYehuda Sadeh 	switch (type) {
55513d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_MAP:
5552a02a946dSIlya Dryomov 	case CEPH_MSG_OSD_BACKOFF:
5553a40c4f10SYehuda Sadeh 	case CEPH_MSG_WATCH_NOTIFY:
555419079203SIlya Dryomov 		return alloc_msg_with_page_vector(hdr);
55553d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_OPREPLY:
55563d14c5d2SYehuda Sadeh 		return get_reply(con, hdr, skip);
55573d14c5d2SYehuda Sadeh 	default:
55585aea3dcdSIlya Dryomov 		pr_warn("%s osd%d unknown msg type %d, skipping\n", __func__,
55595aea3dcdSIlya Dryomov 			osd->o_osd, type);
55603d14c5d2SYehuda Sadeh 		*skip = 1;
55613d14c5d2SYehuda Sadeh 		return NULL;
55623d14c5d2SYehuda Sadeh 	}
55633d14c5d2SYehuda Sadeh }
55643d14c5d2SYehuda Sadeh 
55653d14c5d2SYehuda Sadeh /*
55663d14c5d2SYehuda Sadeh  * Wrappers to refcount containing ceph_osd struct
55673d14c5d2SYehuda Sadeh  */
55683d14c5d2SYehuda Sadeh static struct ceph_connection *get_osd_con(struct ceph_connection *con)
55693d14c5d2SYehuda Sadeh {
55703d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
55713d14c5d2SYehuda Sadeh 	if (get_osd(osd))
55723d14c5d2SYehuda Sadeh 		return con;
55733d14c5d2SYehuda Sadeh 	return NULL;
55743d14c5d2SYehuda Sadeh }
55753d14c5d2SYehuda Sadeh 
55763d14c5d2SYehuda Sadeh static void put_osd_con(struct ceph_connection *con)
55773d14c5d2SYehuda Sadeh {
55783d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
55793d14c5d2SYehuda Sadeh 	put_osd(osd);
55803d14c5d2SYehuda Sadeh }
55813d14c5d2SYehuda Sadeh 
55823d14c5d2SYehuda Sadeh /*
55833d14c5d2SYehuda Sadeh  * authentication
55843d14c5d2SYehuda Sadeh  */
5585a3530df3SAlex Elder /*
5586a3530df3SAlex Elder  * Note: returned pointer is the address of a structure that's
5587a3530df3SAlex Elder  * managed separately.  Caller must *not* attempt to free it.
5588a3530df3SAlex Elder  */
5589a3530df3SAlex Elder static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
55908f43fb53SAlex Elder 					int *proto, int force_new)
55913d14c5d2SYehuda Sadeh {
55923d14c5d2SYehuda Sadeh 	struct ceph_osd *o = con->private;
55933d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = o->o_osdc;
55943d14c5d2SYehuda Sadeh 	struct ceph_auth_client *ac = osdc->client->monc.auth;
559574f1869fSAlex Elder 	struct ceph_auth_handshake *auth = &o->o_auth;
55963d14c5d2SYehuda Sadeh 
559774f1869fSAlex Elder 	if (force_new && auth->authorizer) {
55986c1ea260SIlya Dryomov 		ceph_auth_destroy_authorizer(auth->authorizer);
559974f1869fSAlex Elder 		auth->authorizer = NULL;
56003d14c5d2SYehuda Sadeh 	}
560127859f97SSage Weil 	if (!auth->authorizer) {
560227859f97SSage Weil 		int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
5603a3530df3SAlex Elder 						      auth);
56043d14c5d2SYehuda Sadeh 		if (ret)
5605a3530df3SAlex Elder 			return ERR_PTR(ret);
560627859f97SSage Weil 	} else {
560727859f97SSage Weil 		int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
56080bed9b5cSSage Weil 						     auth);
56090bed9b5cSSage Weil 		if (ret)
56100bed9b5cSSage Weil 			return ERR_PTR(ret);
56113d14c5d2SYehuda Sadeh 	}
56123d14c5d2SYehuda Sadeh 	*proto = ac->protocol;
561374f1869fSAlex Elder 
5614a3530df3SAlex Elder 	return auth;
56153d14c5d2SYehuda Sadeh }
56163d14c5d2SYehuda Sadeh 
56176daca13dSIlya Dryomov static int add_authorizer_challenge(struct ceph_connection *con,
56186daca13dSIlya Dryomov 				    void *challenge_buf, int challenge_buf_len)
56196daca13dSIlya Dryomov {
56206daca13dSIlya Dryomov 	struct ceph_osd *o = con->private;
56216daca13dSIlya Dryomov 	struct ceph_osd_client *osdc = o->o_osdc;
56226daca13dSIlya Dryomov 	struct ceph_auth_client *ac = osdc->client->monc.auth;
56236daca13dSIlya Dryomov 
56246daca13dSIlya Dryomov 	return ceph_auth_add_authorizer_challenge(ac, o->o_auth.authorizer,
56256daca13dSIlya Dryomov 					    challenge_buf, challenge_buf_len);
56266daca13dSIlya Dryomov }
56273d14c5d2SYehuda Sadeh 
56280dde5848SIlya Dryomov static int verify_authorizer_reply(struct ceph_connection *con)
56293d14c5d2SYehuda Sadeh {
56303d14c5d2SYehuda Sadeh 	struct ceph_osd *o = con->private;
56313d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = o->o_osdc;
56323d14c5d2SYehuda Sadeh 	struct ceph_auth_client *ac = osdc->client->monc.auth;
56333d14c5d2SYehuda Sadeh 
56340dde5848SIlya Dryomov 	return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer);
56353d14c5d2SYehuda Sadeh }
56363d14c5d2SYehuda Sadeh 
56373d14c5d2SYehuda Sadeh static int invalidate_authorizer(struct ceph_connection *con)
56383d14c5d2SYehuda Sadeh {
56393d14c5d2SYehuda Sadeh 	struct ceph_osd *o = con->private;
56403d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = o->o_osdc;
56413d14c5d2SYehuda Sadeh 	struct ceph_auth_client *ac = osdc->client->monc.auth;
56423d14c5d2SYehuda Sadeh 
564327859f97SSage Weil 	ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
56443d14c5d2SYehuda Sadeh 	return ceph_monc_validate_auth(&osdc->client->monc);
56453d14c5d2SYehuda Sadeh }
56463d14c5d2SYehuda Sadeh 
56478cb441c0SIlya Dryomov static void osd_reencode_message(struct ceph_msg *msg)
56488cb441c0SIlya Dryomov {
5649914902afSIlya Dryomov 	int type = le16_to_cpu(msg->hdr.type);
5650914902afSIlya Dryomov 
5651914902afSIlya Dryomov 	if (type == CEPH_MSG_OSD_OP)
56528cb441c0SIlya Dryomov 		encode_request_finish(msg);
56538cb441c0SIlya Dryomov }
56548cb441c0SIlya Dryomov 
565579dbd1baSIlya Dryomov static int osd_sign_message(struct ceph_msg *msg)
565633d07337SYan, Zheng {
565779dbd1baSIlya Dryomov 	struct ceph_osd *o = msg->con->private;
565833d07337SYan, Zheng 	struct ceph_auth_handshake *auth = &o->o_auth;
565979dbd1baSIlya Dryomov 
566033d07337SYan, Zheng 	return ceph_auth_sign_message(auth, msg);
566133d07337SYan, Zheng }
566233d07337SYan, Zheng 
566379dbd1baSIlya Dryomov static int osd_check_message_signature(struct ceph_msg *msg)
566433d07337SYan, Zheng {
566579dbd1baSIlya Dryomov 	struct ceph_osd *o = msg->con->private;
566633d07337SYan, Zheng 	struct ceph_auth_handshake *auth = &o->o_auth;
566779dbd1baSIlya Dryomov 
566833d07337SYan, Zheng 	return ceph_auth_check_message_signature(auth, msg);
566933d07337SYan, Zheng }
567033d07337SYan, Zheng 
56713d14c5d2SYehuda Sadeh static const struct ceph_connection_operations osd_con_ops = {
56723d14c5d2SYehuda Sadeh 	.get = get_osd_con,
56733d14c5d2SYehuda Sadeh 	.put = put_osd_con,
56743d14c5d2SYehuda Sadeh 	.dispatch = dispatch,
56753d14c5d2SYehuda Sadeh 	.get_authorizer = get_authorizer,
56766daca13dSIlya Dryomov 	.add_authorizer_challenge = add_authorizer_challenge,
56773d14c5d2SYehuda Sadeh 	.verify_authorizer_reply = verify_authorizer_reply,
56783d14c5d2SYehuda Sadeh 	.invalidate_authorizer = invalidate_authorizer,
56793d14c5d2SYehuda Sadeh 	.alloc_msg = alloc_msg,
56808cb441c0SIlya Dryomov 	.reencode_message = osd_reencode_message,
568179dbd1baSIlya Dryomov 	.sign_message = osd_sign_message,
568279dbd1baSIlya Dryomov 	.check_message_signature = osd_check_message_signature,
56835aea3dcdSIlya Dryomov 	.fault = osd_fault,
56843d14c5d2SYehuda Sadeh };
5685