xref: /openbmc/linux/net/ceph/osd_client.c (revision 08b8a044)
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;
4482f3fead6SIlya Dryomov 	dest->recovery_deletes = src->recovery_deletes;
449922dab61SIlya Dryomov 
450922dab61SIlya Dryomov 	dest->flags = src->flags;
4517ed286f3SIlya Dryomov 	dest->used_replica = src->used_replica;
452922dab61SIlya Dryomov 	dest->paused = src->paused;
453922dab61SIlya Dryomov 
45404c7d789SIlya Dryomov 	dest->epoch = src->epoch;
455dc93e0e2SIlya Dryomov 	dest->last_force_resend = src->last_force_resend;
456dc93e0e2SIlya Dryomov 
457922dab61SIlya Dryomov 	dest->osd = src->osd;
458922dab61SIlya Dryomov }
459922dab61SIlya Dryomov 
46063244fa1SIlya Dryomov static void target_destroy(struct ceph_osd_request_target *t)
46163244fa1SIlya Dryomov {
46263244fa1SIlya Dryomov 	ceph_oid_destroy(&t->base_oid);
46330c156d9SYan, Zheng 	ceph_oloc_destroy(&t->base_oloc);
46463244fa1SIlya Dryomov 	ceph_oid_destroy(&t->target_oid);
46530c156d9SYan, Zheng 	ceph_oloc_destroy(&t->target_oloc);
46663244fa1SIlya Dryomov }
46763244fa1SIlya Dryomov 
46863244fa1SIlya Dryomov /*
4693d14c5d2SYehuda Sadeh  * requests
4703d14c5d2SYehuda Sadeh  */
4713540bfdbSIlya Dryomov static void request_release_checks(struct ceph_osd_request *req)
4723540bfdbSIlya Dryomov {
4733540bfdbSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&req->r_node));
4744609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&req->r_mc_node));
47594e85771SIlya Dryomov 	WARN_ON(!list_empty(&req->r_private_item));
4763540bfdbSIlya Dryomov 	WARN_ON(req->r_osd);
4773540bfdbSIlya Dryomov }
4783540bfdbSIlya Dryomov 
4799e94af20SIlya Dryomov static void ceph_osdc_release_request(struct kref *kref)
4803d14c5d2SYehuda Sadeh {
4819e94af20SIlya Dryomov 	struct ceph_osd_request *req = container_of(kref,
4829e94af20SIlya Dryomov 					    struct ceph_osd_request, r_kref);
4835476492fSAlex Elder 	unsigned int which;
4843d14c5d2SYehuda Sadeh 
4859e94af20SIlya Dryomov 	dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
4869e94af20SIlya Dryomov 	     req->r_request, req->r_reply);
4873540bfdbSIlya Dryomov 	request_release_checks(req);
4889e94af20SIlya Dryomov 
4893d14c5d2SYehuda Sadeh 	if (req->r_request)
4903d14c5d2SYehuda Sadeh 		ceph_msg_put(req->r_request);
4915aea3dcdSIlya Dryomov 	if (req->r_reply)
492ab8cb34aSAlex Elder 		ceph_msg_put(req->r_reply);
4930fff87ecSAlex Elder 
4945476492fSAlex Elder 	for (which = 0; which < req->r_num_ops; which++)
4955476492fSAlex Elder 		osd_req_op_data_release(req, which);
4960fff87ecSAlex Elder 
497a66dd383SIlya Dryomov 	target_destroy(&req->r_t);
4983d14c5d2SYehuda Sadeh 	ceph_put_snap_context(req->r_snapc);
499d30291b9SIlya Dryomov 
5003d14c5d2SYehuda Sadeh 	if (req->r_mempool)
5013d14c5d2SYehuda Sadeh 		mempool_free(req, req->r_osdc->req_mempool);
5023f1af42aSIlya Dryomov 	else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS)
5035522ae0bSAlex Elder 		kmem_cache_free(ceph_osd_request_cache, req);
5043f1af42aSIlya Dryomov 	else
5053f1af42aSIlya Dryomov 		kfree(req);
5063d14c5d2SYehuda Sadeh }
5079e94af20SIlya Dryomov 
5089e94af20SIlya Dryomov void ceph_osdc_get_request(struct ceph_osd_request *req)
5099e94af20SIlya Dryomov {
5109e94af20SIlya Dryomov 	dout("%s %p (was %d)\n", __func__, req,
5112c935bc5SPeter Zijlstra 	     kref_read(&req->r_kref));
5129e94af20SIlya Dryomov 	kref_get(&req->r_kref);
5139e94af20SIlya Dryomov }
5149e94af20SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_get_request);
5159e94af20SIlya Dryomov 
5169e94af20SIlya Dryomov void ceph_osdc_put_request(struct ceph_osd_request *req)
5179e94af20SIlya Dryomov {
5183ed97d63SIlya Dryomov 	if (req) {
5199e94af20SIlya Dryomov 		dout("%s %p (was %d)\n", __func__, req,
5202c935bc5SPeter Zijlstra 		     kref_read(&req->r_kref));
5219e94af20SIlya Dryomov 		kref_put(&req->r_kref, ceph_osdc_release_request);
5229e94af20SIlya Dryomov 	}
5233ed97d63SIlya Dryomov }
5249e94af20SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_put_request);
5253d14c5d2SYehuda Sadeh 
5263540bfdbSIlya Dryomov static void request_init(struct ceph_osd_request *req)
5273540bfdbSIlya Dryomov {
528042f6498SJeff Layton 	/* req only, each op is zeroed in osd_req_op_init() */
5293540bfdbSIlya Dryomov 	memset(req, 0, sizeof(*req));
5303540bfdbSIlya Dryomov 
5313540bfdbSIlya Dryomov 	kref_init(&req->r_kref);
5323540bfdbSIlya Dryomov 	init_completion(&req->r_completion);
5333540bfdbSIlya Dryomov 	RB_CLEAR_NODE(&req->r_node);
5344609245eSIlya Dryomov 	RB_CLEAR_NODE(&req->r_mc_node);
53594e85771SIlya Dryomov 	INIT_LIST_HEAD(&req->r_private_item);
5363540bfdbSIlya Dryomov 
5373540bfdbSIlya Dryomov 	target_init(&req->r_t);
5383540bfdbSIlya Dryomov }
5393540bfdbSIlya Dryomov 
5403d14c5d2SYehuda Sadeh struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
5413d14c5d2SYehuda Sadeh 					       struct ceph_snap_context *snapc,
5421b83bef2SSage Weil 					       unsigned int num_ops,
5433d14c5d2SYehuda Sadeh 					       bool use_mempool,
54454a54007SAlex Elder 					       gfp_t gfp_flags)
5453d14c5d2SYehuda Sadeh {
5463d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
5473d14c5d2SYehuda Sadeh 
5483d14c5d2SYehuda Sadeh 	if (use_mempool) {
5493f1af42aSIlya Dryomov 		BUG_ON(num_ops > CEPH_OSD_SLAB_OPS);
5503d14c5d2SYehuda Sadeh 		req = mempool_alloc(osdc->req_mempool, gfp_flags);
5513f1af42aSIlya Dryomov 	} else if (num_ops <= CEPH_OSD_SLAB_OPS) {
5523f1af42aSIlya Dryomov 		req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
5533d14c5d2SYehuda Sadeh 	} else {
5543f1af42aSIlya Dryomov 		BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
555acafe7e3SKees Cook 		req = kmalloc(struct_size(req, r_ops, num_ops), gfp_flags);
5563d14c5d2SYehuda Sadeh 	}
5573f1af42aSIlya Dryomov 	if (unlikely(!req))
5583d14c5d2SYehuda Sadeh 		return NULL;
5593d14c5d2SYehuda Sadeh 
5603540bfdbSIlya Dryomov 	request_init(req);
5613d14c5d2SYehuda Sadeh 	req->r_osdc = osdc;
5623d14c5d2SYehuda Sadeh 	req->r_mempool = use_mempool;
56379528734SAlex Elder 	req->r_num_ops = num_ops;
56484127282SIlya Dryomov 	req->r_snapid = CEPH_NOSNAP;
56584127282SIlya Dryomov 	req->r_snapc = ceph_get_snap_context(snapc);
5663d14c5d2SYehuda Sadeh 
56713d1ad16SIlya Dryomov 	dout("%s req %p\n", __func__, req);
56813d1ad16SIlya Dryomov 	return req;
5693f1af42aSIlya Dryomov }
57013d1ad16SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_alloc_request);
5713f1af42aSIlya Dryomov 
5722e59ffd1SIlya Dryomov static int ceph_oloc_encoding_size(const struct ceph_object_locator *oloc)
57330c156d9SYan, Zheng {
57430c156d9SYan, Zheng 	return 8 + 4 + 4 + 4 + (oloc->pool_ns ? oloc->pool_ns->len : 0);
57530c156d9SYan, Zheng }
57630c156d9SYan, Zheng 
5770d9c1ab3SIlya Dryomov static int __ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp,
5780d9c1ab3SIlya Dryomov 				      int num_request_data_items,
5790d9c1ab3SIlya Dryomov 				      int num_reply_data_items)
58013d1ad16SIlya Dryomov {
58113d1ad16SIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
58213d1ad16SIlya Dryomov 	struct ceph_msg *msg;
58313d1ad16SIlya Dryomov 	int msg_size;
5843d14c5d2SYehuda Sadeh 
5850d9c1ab3SIlya Dryomov 	WARN_ON(req->r_request || req->r_reply);
586d30291b9SIlya Dryomov 	WARN_ON(ceph_oid_empty(&req->r_base_oid));
58730c156d9SYan, Zheng 	WARN_ON(ceph_oloc_empty(&req->r_base_oloc));
588d30291b9SIlya Dryomov 
58913d1ad16SIlya Dryomov 	/* create request message */
5908cb441c0SIlya Dryomov 	msg_size = CEPH_ENCODING_START_BLK_LEN +
5918cb441c0SIlya Dryomov 			CEPH_PGID_ENCODING_LEN + 1; /* spgid */
5928cb441c0SIlya Dryomov 	msg_size += 4 + 4 + 4; /* hash, osdmap_epoch, flags */
5938cb441c0SIlya Dryomov 	msg_size += CEPH_ENCODING_START_BLK_LEN +
5948cb441c0SIlya Dryomov 			sizeof(struct ceph_osd_reqid); /* reqid */
5958cb441c0SIlya Dryomov 	msg_size += sizeof(struct ceph_blkin_trace_info); /* trace */
5968cb441c0SIlya Dryomov 	msg_size += 4 + sizeof(struct ceph_timespec); /* client_inc, mtime */
59730c156d9SYan, Zheng 	msg_size += CEPH_ENCODING_START_BLK_LEN +
59830c156d9SYan, Zheng 			ceph_oloc_encoding_size(&req->r_base_oloc); /* oloc */
59913d1ad16SIlya Dryomov 	msg_size += 4 + req->r_base_oid.name_len; /* oid */
60013d1ad16SIlya Dryomov 	msg_size += 2 + req->r_num_ops * sizeof(struct ceph_osd_op);
601ae458f5aSIlya Dryomov 	msg_size += 8; /* snapid */
602ae458f5aSIlya Dryomov 	msg_size += 8; /* snap_seq */
60313d1ad16SIlya Dryomov 	msg_size += 4 + 8 * (req->r_snapc ? req->r_snapc->num_snaps : 0);
6048cb441c0SIlya Dryomov 	msg_size += 4 + 8; /* retry_attempt, features */
605ae458f5aSIlya Dryomov 
60613d1ad16SIlya Dryomov 	if (req->r_mempool)
6070d9c1ab3SIlya Dryomov 		msg = ceph_msgpool_get(&osdc->msgpool_op, msg_size,
6080d9c1ab3SIlya Dryomov 				       num_request_data_items);
6093d14c5d2SYehuda Sadeh 	else
6100d9c1ab3SIlya Dryomov 		msg = ceph_msg_new2(CEPH_MSG_OSD_OP, msg_size,
6110d9c1ab3SIlya Dryomov 				    num_request_data_items, gfp, true);
61213d1ad16SIlya Dryomov 	if (!msg)
61313d1ad16SIlya Dryomov 		return -ENOMEM;
6143d14c5d2SYehuda Sadeh 
6153d14c5d2SYehuda Sadeh 	memset(msg->front.iov_base, 0, msg->front.iov_len);
6163d14c5d2SYehuda Sadeh 	req->r_request = msg;
6173d14c5d2SYehuda Sadeh 
61813d1ad16SIlya Dryomov 	/* create reply message */
61913d1ad16SIlya Dryomov 	msg_size = OSD_OPREPLY_FRONT_LEN;
620711da55dSIlya Dryomov 	msg_size += req->r_base_oid.name_len;
621711da55dSIlya Dryomov 	msg_size += req->r_num_ops * sizeof(struct ceph_osd_op);
62213d1ad16SIlya Dryomov 
62313d1ad16SIlya Dryomov 	if (req->r_mempool)
6240d9c1ab3SIlya Dryomov 		msg = ceph_msgpool_get(&osdc->msgpool_op_reply, msg_size,
6250d9c1ab3SIlya Dryomov 				       num_reply_data_items);
62613d1ad16SIlya Dryomov 	else
6270d9c1ab3SIlya Dryomov 		msg = ceph_msg_new2(CEPH_MSG_OSD_OPREPLY, msg_size,
6280d9c1ab3SIlya Dryomov 				    num_reply_data_items, gfp, true);
62913d1ad16SIlya Dryomov 	if (!msg)
63013d1ad16SIlya Dryomov 		return -ENOMEM;
63113d1ad16SIlya Dryomov 
63213d1ad16SIlya Dryomov 	req->r_reply = msg;
63313d1ad16SIlya Dryomov 
63413d1ad16SIlya Dryomov 	return 0;
63513d1ad16SIlya Dryomov }
6363d14c5d2SYehuda Sadeh 
637a8dd0a37SAlex Elder static bool osd_req_opcode_valid(u16 opcode)
638a8dd0a37SAlex Elder {
639a8dd0a37SAlex Elder 	switch (opcode) {
64070b5bfa3SIlya Dryomov #define GENERATE_CASE(op, opcode, str)	case CEPH_OSD_OP_##op: return true;
64170b5bfa3SIlya Dryomov __CEPH_FORALL_OSD_OPS(GENERATE_CASE)
64270b5bfa3SIlya Dryomov #undef GENERATE_CASE
643a8dd0a37SAlex Elder 	default:
644a8dd0a37SAlex Elder 		return false;
645a8dd0a37SAlex Elder 	}
646a8dd0a37SAlex Elder }
647a8dd0a37SAlex Elder 
6480d9c1ab3SIlya Dryomov static void get_num_data_items(struct ceph_osd_request *req,
6490d9c1ab3SIlya Dryomov 			       int *num_request_data_items,
6500d9c1ab3SIlya Dryomov 			       int *num_reply_data_items)
6510d9c1ab3SIlya Dryomov {
6520d9c1ab3SIlya Dryomov 	struct ceph_osd_req_op *op;
6530d9c1ab3SIlya Dryomov 
6540d9c1ab3SIlya Dryomov 	*num_request_data_items = 0;
6550d9c1ab3SIlya Dryomov 	*num_reply_data_items = 0;
6560d9c1ab3SIlya Dryomov 
6570d9c1ab3SIlya Dryomov 	for (op = req->r_ops; op != &req->r_ops[req->r_num_ops]; op++) {
6580d9c1ab3SIlya Dryomov 		switch (op->op) {
6590d9c1ab3SIlya Dryomov 		/* request */
6600d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_WRITE:
6610d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_WRITEFULL:
6620d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_SETXATTR:
6630d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_CMPXATTR:
6640d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_NOTIFY_ACK:
66578beb0ffSLuis Henriques 		case CEPH_OSD_OP_COPY_FROM2:
6660d9c1ab3SIlya Dryomov 			*num_request_data_items += 1;
6670d9c1ab3SIlya Dryomov 			break;
6680d9c1ab3SIlya Dryomov 
6690d9c1ab3SIlya Dryomov 		/* reply */
6700d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_STAT:
6710d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_READ:
6720d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_LIST_WATCHERS:
6730d9c1ab3SIlya Dryomov 			*num_reply_data_items += 1;
6740d9c1ab3SIlya Dryomov 			break;
6750d9c1ab3SIlya Dryomov 
6760d9c1ab3SIlya Dryomov 		/* both */
6770d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_NOTIFY:
6780d9c1ab3SIlya Dryomov 			*num_request_data_items += 1;
6790d9c1ab3SIlya Dryomov 			*num_reply_data_items += 1;
6800d9c1ab3SIlya Dryomov 			break;
6810d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_CALL:
6820d9c1ab3SIlya Dryomov 			*num_request_data_items += 2;
6830d9c1ab3SIlya Dryomov 			*num_reply_data_items += 1;
6840d9c1ab3SIlya Dryomov 			break;
6850d9c1ab3SIlya Dryomov 
6860d9c1ab3SIlya Dryomov 		default:
6870d9c1ab3SIlya Dryomov 			WARN_ON(!osd_req_opcode_valid(op->op));
6880d9c1ab3SIlya Dryomov 			break;
6890d9c1ab3SIlya Dryomov 		}
6900d9c1ab3SIlya Dryomov 	}
6910d9c1ab3SIlya Dryomov }
6920d9c1ab3SIlya Dryomov 
6930d9c1ab3SIlya Dryomov /*
6940d9c1ab3SIlya Dryomov  * oid, oloc and OSD op opcode(s) must be filled in before this function
6950d9c1ab3SIlya Dryomov  * is called.
6960d9c1ab3SIlya Dryomov  */
6970d9c1ab3SIlya Dryomov int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp)
6980d9c1ab3SIlya Dryomov {
6990d9c1ab3SIlya Dryomov 	int num_request_data_items, num_reply_data_items;
7000d9c1ab3SIlya Dryomov 
7010d9c1ab3SIlya Dryomov 	get_num_data_items(req, &num_request_data_items, &num_reply_data_items);
7020d9c1ab3SIlya Dryomov 	return __ceph_osdc_alloc_messages(req, gfp, num_request_data_items,
7030d9c1ab3SIlya Dryomov 					  num_reply_data_items);
7040d9c1ab3SIlya Dryomov }
7050d9c1ab3SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_alloc_messages);
7060d9c1ab3SIlya Dryomov 
70733803f33SAlex Elder /*
70833803f33SAlex Elder  * This is an osd op init function for opcodes that have no data or
70933803f33SAlex Elder  * other information associated with them.  It also serves as a
71033803f33SAlex Elder  * common init routine for all the other init functions, below.
71133803f33SAlex Elder  */
712042f6498SJeff Layton struct ceph_osd_req_op *
713042f6498SJeff Layton osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
714144cba14SYan, Zheng 		 u16 opcode, u32 flags)
71533803f33SAlex Elder {
716c99d2d4aSAlex Elder 	struct ceph_osd_req_op *op;
717c99d2d4aSAlex Elder 
718c99d2d4aSAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
71933803f33SAlex Elder 	BUG_ON(!osd_req_opcode_valid(opcode));
72033803f33SAlex Elder 
721c99d2d4aSAlex Elder 	op = &osd_req->r_ops[which];
72233803f33SAlex Elder 	memset(op, 0, sizeof (*op));
72333803f33SAlex Elder 	op->op = opcode;
724144cba14SYan, Zheng 	op->flags = flags;
725c99d2d4aSAlex Elder 
726c99d2d4aSAlex Elder 	return op;
72733803f33SAlex Elder }
72849719778SAlex Elder EXPORT_SYMBOL(osd_req_op_init);
72949719778SAlex Elder 
730c99d2d4aSAlex Elder void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
731c99d2d4aSAlex Elder 				unsigned int which, u16 opcode,
73233803f33SAlex Elder 				u64 offset, u64 length,
73333803f33SAlex Elder 				u64 truncate_size, u32 truncate_seq)
73433803f33SAlex Elder {
735042f6498SJeff Layton 	struct ceph_osd_req_op *op = osd_req_op_init(osd_req, which,
736144cba14SYan, Zheng 						     opcode, 0);
73733803f33SAlex Elder 	size_t payload_len = 0;
73833803f33SAlex Elder 
739ad7a60deSLi Wang 	BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
740e30b7577SIlya Dryomov 	       opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
741e30b7577SIlya Dryomov 	       opcode != CEPH_OSD_OP_TRUNCATE);
74233803f33SAlex Elder 
74333803f33SAlex Elder 	op->extent.offset = offset;
74433803f33SAlex Elder 	op->extent.length = length;
74533803f33SAlex Elder 	op->extent.truncate_size = truncate_size;
74633803f33SAlex Elder 	op->extent.truncate_seq = truncate_seq;
747e30b7577SIlya Dryomov 	if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
74833803f33SAlex Elder 		payload_len += length;
74933803f33SAlex Elder 
750de2aa102SIlya Dryomov 	op->indata_len = payload_len;
75133803f33SAlex Elder }
75233803f33SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_init);
75333803f33SAlex Elder 
754c99d2d4aSAlex Elder void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
755c99d2d4aSAlex Elder 				unsigned int which, u64 length)
756e5975c7cSAlex Elder {
757c99d2d4aSAlex Elder 	struct ceph_osd_req_op *op;
758c99d2d4aSAlex Elder 	u64 previous;
759c99d2d4aSAlex Elder 
760c99d2d4aSAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
761c99d2d4aSAlex Elder 	op = &osd_req->r_ops[which];
762c99d2d4aSAlex Elder 	previous = op->extent.length;
763e5975c7cSAlex Elder 
764e5975c7cSAlex Elder 	if (length == previous)
765e5975c7cSAlex Elder 		return;		/* Nothing to do */
766e5975c7cSAlex Elder 	BUG_ON(length > previous);
767e5975c7cSAlex Elder 
768e5975c7cSAlex Elder 	op->extent.length = length;
769d641df81SYan, Zheng 	if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
770de2aa102SIlya Dryomov 		op->indata_len -= previous - length;
771e5975c7cSAlex Elder }
772e5975c7cSAlex Elder EXPORT_SYMBOL(osd_req_op_extent_update);
773e5975c7cSAlex Elder 
7742c63f49aSYan, Zheng void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
7752c63f49aSYan, Zheng 				unsigned int which, u64 offset_inc)
7762c63f49aSYan, Zheng {
7772c63f49aSYan, Zheng 	struct ceph_osd_req_op *op, *prev_op;
7782c63f49aSYan, Zheng 
7792c63f49aSYan, Zheng 	BUG_ON(which + 1 >= osd_req->r_num_ops);
7802c63f49aSYan, Zheng 
7812c63f49aSYan, Zheng 	prev_op = &osd_req->r_ops[which];
782042f6498SJeff Layton 	op = osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags);
7832c63f49aSYan, Zheng 	/* dup previous one */
7842c63f49aSYan, Zheng 	op->indata_len = prev_op->indata_len;
7852c63f49aSYan, Zheng 	op->outdata_len = prev_op->outdata_len;
7862c63f49aSYan, Zheng 	op->extent = prev_op->extent;
7872c63f49aSYan, Zheng 	/* adjust offset */
7882c63f49aSYan, Zheng 	op->extent.offset += offset_inc;
7892c63f49aSYan, Zheng 	op->extent.length -= offset_inc;
7902c63f49aSYan, Zheng 
7912c63f49aSYan, Zheng 	if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
7922c63f49aSYan, Zheng 		op->indata_len -= offset_inc;
7932c63f49aSYan, Zheng }
7942c63f49aSYan, Zheng EXPORT_SYMBOL(osd_req_op_extent_dup_last);
7952c63f49aSYan, Zheng 
796fe943d50SChengguang Xu int osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
79724639ce5SIlya Dryomov 			const char *class, const char *method)
79833803f33SAlex Elder {
79924639ce5SIlya Dryomov 	struct ceph_osd_req_op *op;
8005f562df5SAlex Elder 	struct ceph_pagelist *pagelist;
80133803f33SAlex Elder 	size_t payload_len = 0;
80233803f33SAlex Elder 	size_t size;
8034766815bSDavid Disseldorp 	int ret;
80433803f33SAlex Elder 
805042f6498SJeff Layton 	op = osd_req_op_init(osd_req, which, CEPH_OSD_OP_CALL, 0);
80633803f33SAlex Elder 
80733165d47SIlya Dryomov 	pagelist = ceph_pagelist_alloc(GFP_NOFS);
808fe943d50SChengguang Xu 	if (!pagelist)
809fe943d50SChengguang Xu 		return -ENOMEM;
810fe943d50SChengguang Xu 
81133803f33SAlex Elder 	op->cls.class_name = class;
81233803f33SAlex Elder 	size = strlen(class);
81333803f33SAlex Elder 	BUG_ON(size > (size_t) U8_MAX);
81433803f33SAlex Elder 	op->cls.class_len = size;
8154766815bSDavid Disseldorp 	ret = ceph_pagelist_append(pagelist, class, size);
8164766815bSDavid Disseldorp 	if (ret)
8174766815bSDavid Disseldorp 		goto err_pagelist_free;
81833803f33SAlex Elder 	payload_len += size;
81933803f33SAlex Elder 
82033803f33SAlex Elder 	op->cls.method_name = method;
82133803f33SAlex Elder 	size = strlen(method);
82233803f33SAlex Elder 	BUG_ON(size > (size_t) U8_MAX);
82333803f33SAlex Elder 	op->cls.method_len = size;
8244766815bSDavid Disseldorp 	ret = ceph_pagelist_append(pagelist, method, size);
8254766815bSDavid Disseldorp 	if (ret)
8264766815bSDavid Disseldorp 		goto err_pagelist_free;
82733803f33SAlex Elder 	payload_len += size;
82833803f33SAlex Elder 
829a4ce40a9SAlex Elder 	osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
830de2aa102SIlya Dryomov 	op->indata_len = payload_len;
831fe943d50SChengguang Xu 	return 0;
8324766815bSDavid Disseldorp 
8334766815bSDavid Disseldorp err_pagelist_free:
8344766815bSDavid Disseldorp 	ceph_pagelist_release(pagelist);
8354766815bSDavid Disseldorp 	return ret;
83633803f33SAlex Elder }
83733803f33SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_init);
8388c042b0dSAlex Elder 
839d74b50beSYan, Zheng int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
840d74b50beSYan, Zheng 			  u16 opcode, const char *name, const void *value,
841d74b50beSYan, Zheng 			  size_t size, u8 cmp_op, u8 cmp_mode)
842d74b50beSYan, Zheng {
843042f6498SJeff Layton 	struct ceph_osd_req_op *op = osd_req_op_init(osd_req, which,
844144cba14SYan, Zheng 						     opcode, 0);
845d74b50beSYan, Zheng 	struct ceph_pagelist *pagelist;
846d74b50beSYan, Zheng 	size_t payload_len;
8474766815bSDavid Disseldorp 	int ret;
848d74b50beSYan, Zheng 
849d74b50beSYan, Zheng 	BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
850d74b50beSYan, Zheng 
85133165d47SIlya Dryomov 	pagelist = ceph_pagelist_alloc(GFP_NOFS);
852d74b50beSYan, Zheng 	if (!pagelist)
853d74b50beSYan, Zheng 		return -ENOMEM;
854d74b50beSYan, Zheng 
855d74b50beSYan, Zheng 	payload_len = strlen(name);
856d74b50beSYan, Zheng 	op->xattr.name_len = payload_len;
8574766815bSDavid Disseldorp 	ret = ceph_pagelist_append(pagelist, name, payload_len);
8584766815bSDavid Disseldorp 	if (ret)
8594766815bSDavid Disseldorp 		goto err_pagelist_free;
860d74b50beSYan, Zheng 
861d74b50beSYan, Zheng 	op->xattr.value_len = size;
8624766815bSDavid Disseldorp 	ret = ceph_pagelist_append(pagelist, value, size);
8634766815bSDavid Disseldorp 	if (ret)
8644766815bSDavid Disseldorp 		goto err_pagelist_free;
865d74b50beSYan, Zheng 	payload_len += size;
866d74b50beSYan, Zheng 
867d74b50beSYan, Zheng 	op->xattr.cmp_op = cmp_op;
868d74b50beSYan, Zheng 	op->xattr.cmp_mode = cmp_mode;
869d74b50beSYan, Zheng 
870d74b50beSYan, Zheng 	ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
871de2aa102SIlya Dryomov 	op->indata_len = payload_len;
872d74b50beSYan, Zheng 	return 0;
8734766815bSDavid Disseldorp 
8744766815bSDavid Disseldorp err_pagelist_free:
8754766815bSDavid Disseldorp 	ceph_pagelist_release(pagelist);
8764766815bSDavid Disseldorp 	return ret;
877d74b50beSYan, Zheng }
878d74b50beSYan, Zheng EXPORT_SYMBOL(osd_req_op_xattr_init);
879d74b50beSYan, Zheng 
880922dab61SIlya Dryomov /*
881922dab61SIlya Dryomov  * @watch_opcode: CEPH_OSD_WATCH_OP_*
882922dab61SIlya Dryomov  */
883922dab61SIlya Dryomov static void osd_req_op_watch_init(struct ceph_osd_request *req, int which,
88475dbb685SIlya Dryomov 				  u8 watch_opcode, u64 cookie, u32 gen)
88533803f33SAlex Elder {
886922dab61SIlya Dryomov 	struct ceph_osd_req_op *op;
88733803f33SAlex Elder 
888042f6498SJeff Layton 	op = osd_req_op_init(req, which, CEPH_OSD_OP_WATCH, 0);
88933803f33SAlex Elder 	op->watch.cookie = cookie;
890922dab61SIlya Dryomov 	op->watch.op = watch_opcode;
89175dbb685SIlya Dryomov 	op->watch.gen = gen;
89275dbb685SIlya Dryomov }
89375dbb685SIlya Dryomov 
89475dbb685SIlya Dryomov /*
89575dbb685SIlya Dryomov  * prot_ver, timeout and notify payload (may be empty) should already be
89675dbb685SIlya Dryomov  * encoded in @request_pl
89775dbb685SIlya Dryomov  */
89875dbb685SIlya Dryomov static void osd_req_op_notify_init(struct ceph_osd_request *req, int which,
89975dbb685SIlya Dryomov 				   u64 cookie, struct ceph_pagelist *request_pl)
90075dbb685SIlya Dryomov {
90175dbb685SIlya Dryomov 	struct ceph_osd_req_op *op;
90275dbb685SIlya Dryomov 
90375dbb685SIlya Dryomov 	op = osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY, 0);
90475dbb685SIlya Dryomov 	op->notify.cookie = cookie;
90575dbb685SIlya Dryomov 
90675dbb685SIlya Dryomov 	ceph_osd_data_pagelist_init(&op->notify.request_data, request_pl);
90775dbb685SIlya Dryomov 	op->indata_len = request_pl->length;
90833803f33SAlex Elder }
90933803f33SAlex Elder 
910d3798accSIlya Dryomov /*
911d3798accSIlya Dryomov  * @flags: CEPH_OSD_OP_ALLOC_HINT_FLAG_*
912d3798accSIlya Dryomov  */
913c647b8a8SIlya Dryomov void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
914c647b8a8SIlya Dryomov 				unsigned int which,
915c647b8a8SIlya Dryomov 				u64 expected_object_size,
916d3798accSIlya Dryomov 				u64 expected_write_size,
917d3798accSIlya Dryomov 				u32 flags)
918c647b8a8SIlya Dryomov {
919042f6498SJeff Layton 	struct ceph_osd_req_op *op;
920c647b8a8SIlya Dryomov 
921042f6498SJeff Layton 	op = osd_req_op_init(osd_req, which, CEPH_OSD_OP_SETALLOCHINT, 0);
922c647b8a8SIlya Dryomov 	op->alloc_hint.expected_object_size = expected_object_size;
923c647b8a8SIlya Dryomov 	op->alloc_hint.expected_write_size = expected_write_size;
924d3798accSIlya Dryomov 	op->alloc_hint.flags = flags;
925c647b8a8SIlya Dryomov 
926c647b8a8SIlya Dryomov 	/*
927c647b8a8SIlya Dryomov 	 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
928c647b8a8SIlya Dryomov 	 * not worth a feature bit.  Set FAILOK per-op flag to make
929c647b8a8SIlya Dryomov 	 * sure older osds don't trip over an unsupported opcode.
930c647b8a8SIlya Dryomov 	 */
931c647b8a8SIlya Dryomov 	op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
932c647b8a8SIlya Dryomov }
933c647b8a8SIlya Dryomov EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
934c647b8a8SIlya Dryomov 
93590af3602SAlex Elder static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
936ec9123c5SAlex Elder 				struct ceph_osd_data *osd_data)
937ec9123c5SAlex Elder {
938ec9123c5SAlex Elder 	u64 length = ceph_osd_data_length(osd_data);
939ec9123c5SAlex Elder 
940ec9123c5SAlex Elder 	if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
941ec9123c5SAlex Elder 		BUG_ON(length > (u64) SIZE_MAX);
942ec9123c5SAlex Elder 		if (length)
94390af3602SAlex Elder 			ceph_msg_data_add_pages(msg, osd_data->pages,
944e8862740SIlya Dryomov 					length, osd_data->alignment, false);
945ec9123c5SAlex Elder 	} else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
946ec9123c5SAlex Elder 		BUG_ON(!length);
94790af3602SAlex Elder 		ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
948ec9123c5SAlex Elder #ifdef CONFIG_BLOCK
949ec9123c5SAlex Elder 	} else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
9505359a17dSIlya Dryomov 		ceph_msg_data_add_bio(msg, &osd_data->bio_pos, length);
951ec9123c5SAlex Elder #endif
952b9e281c2SIlya Dryomov 	} else if (osd_data->type == CEPH_OSD_DATA_TYPE_BVECS) {
953b9e281c2SIlya Dryomov 		ceph_msg_data_add_bvecs(msg, &osd_data->bvec_pos);
954ec9123c5SAlex Elder 	} else {
955ec9123c5SAlex Elder 		BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
956ec9123c5SAlex Elder 	}
957ec9123c5SAlex Elder }
958ec9123c5SAlex Elder 
959bb873b53SIlya Dryomov static u32 osd_req_encode_op(struct ceph_osd_op *dst,
960bb873b53SIlya Dryomov 			     const struct ceph_osd_req_op *src)
9613d14c5d2SYehuda Sadeh {
962065a68f9SAlex Elder 	switch (src->op) {
963fbfab539SAlex Elder 	case CEPH_OSD_OP_STAT:
964fbfab539SAlex Elder 		break;
9653d14c5d2SYehuda Sadeh 	case CEPH_OSD_OP_READ:
9663d14c5d2SYehuda Sadeh 	case CEPH_OSD_OP_WRITE:
967e30b7577SIlya Dryomov 	case CEPH_OSD_OP_WRITEFULL:
968ad7a60deSLi Wang 	case CEPH_OSD_OP_ZERO:
969ad7a60deSLi Wang 	case CEPH_OSD_OP_TRUNCATE:
970175face2SAlex Elder 		dst->extent.offset = cpu_to_le64(src->extent.offset);
971175face2SAlex Elder 		dst->extent.length = cpu_to_le64(src->extent.length);
9723d14c5d2SYehuda Sadeh 		dst->extent.truncate_size =
9733d14c5d2SYehuda Sadeh 			cpu_to_le64(src->extent.truncate_size);
9743d14c5d2SYehuda Sadeh 		dst->extent.truncate_seq =
9753d14c5d2SYehuda Sadeh 			cpu_to_le32(src->extent.truncate_seq);
9763d14c5d2SYehuda Sadeh 		break;
9773d14c5d2SYehuda Sadeh 	case CEPH_OSD_OP_CALL:
9783d14c5d2SYehuda Sadeh 		dst->cls.class_len = src->cls.class_len;
9793d14c5d2SYehuda Sadeh 		dst->cls.method_len = src->cls.method_len;
980bb873b53SIlya Dryomov 		dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
9813d14c5d2SYehuda Sadeh 		break;
982a40c4f10SYehuda Sadeh 	case CEPH_OSD_OP_WATCH:
983a40c4f10SYehuda Sadeh 		dst->watch.cookie = cpu_to_le64(src->watch.cookie);
984922dab61SIlya Dryomov 		dst->watch.ver = cpu_to_le64(0);
985922dab61SIlya Dryomov 		dst->watch.op = src->watch.op;
986922dab61SIlya Dryomov 		dst->watch.gen = cpu_to_le32(src->watch.gen);
987922dab61SIlya Dryomov 		break;
988922dab61SIlya Dryomov 	case CEPH_OSD_OP_NOTIFY_ACK:
989a40c4f10SYehuda Sadeh 		break;
99019079203SIlya Dryomov 	case CEPH_OSD_OP_NOTIFY:
99119079203SIlya Dryomov 		dst->notify.cookie = cpu_to_le64(src->notify.cookie);
99219079203SIlya Dryomov 		break;
993a4ed38d7SDouglas Fuller 	case CEPH_OSD_OP_LIST_WATCHERS:
994a4ed38d7SDouglas Fuller 		break;
995c647b8a8SIlya Dryomov 	case CEPH_OSD_OP_SETALLOCHINT:
996c647b8a8SIlya Dryomov 		dst->alloc_hint.expected_object_size =
997c647b8a8SIlya Dryomov 		    cpu_to_le64(src->alloc_hint.expected_object_size);
998c647b8a8SIlya Dryomov 		dst->alloc_hint.expected_write_size =
999c647b8a8SIlya Dryomov 		    cpu_to_le64(src->alloc_hint.expected_write_size);
1000d3798accSIlya Dryomov 		dst->alloc_hint.flags = cpu_to_le32(src->alloc_hint.flags);
1001c647b8a8SIlya Dryomov 		break;
1002d74b50beSYan, Zheng 	case CEPH_OSD_OP_SETXATTR:
1003d74b50beSYan, Zheng 	case CEPH_OSD_OP_CMPXATTR:
1004d74b50beSYan, Zheng 		dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
1005d74b50beSYan, Zheng 		dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
1006d74b50beSYan, Zheng 		dst->xattr.cmp_op = src->xattr.cmp_op;
1007d74b50beSYan, Zheng 		dst->xattr.cmp_mode = src->xattr.cmp_mode;
1008d74b50beSYan, Zheng 		break;
1009864e9197SYan, Zheng 	case CEPH_OSD_OP_CREATE:
1010864e9197SYan, Zheng 	case CEPH_OSD_OP_DELETE:
1011864e9197SYan, Zheng 		break;
101278beb0ffSLuis Henriques 	case CEPH_OSD_OP_COPY_FROM2:
101323ddf9beSLuis Henriques 		dst->copy_from.snapid = cpu_to_le64(src->copy_from.snapid);
101423ddf9beSLuis Henriques 		dst->copy_from.src_version =
101523ddf9beSLuis Henriques 			cpu_to_le64(src->copy_from.src_version);
101623ddf9beSLuis Henriques 		dst->copy_from.flags = src->copy_from.flags;
101723ddf9beSLuis Henriques 		dst->copy_from.src_fadvise_flags =
101823ddf9beSLuis Henriques 			cpu_to_le32(src->copy_from.src_fadvise_flags);
101923ddf9beSLuis Henriques 		break;
10203d14c5d2SYehuda Sadeh 	default:
10214c46459cSAlex Elder 		pr_err("unsupported osd opcode %s\n",
10228f63ca2dSAlex Elder 			ceph_osd_op_name(src->op));
10234c46459cSAlex Elder 		WARN_ON(1);
1024a8dd0a37SAlex Elder 
1025a8dd0a37SAlex Elder 		return 0;
10263d14c5d2SYehuda Sadeh 	}
10277b25bf5fSIlya Dryomov 
1028a8dd0a37SAlex Elder 	dst->op = cpu_to_le16(src->op);
10297b25bf5fSIlya Dryomov 	dst->flags = cpu_to_le32(src->flags);
1030de2aa102SIlya Dryomov 	dst->payload_len = cpu_to_le32(src->indata_len);
1031175face2SAlex Elder 
1032bb873b53SIlya Dryomov 	return src->indata_len;
10333d14c5d2SYehuda Sadeh }
10343d14c5d2SYehuda Sadeh 
10353d14c5d2SYehuda Sadeh /*
10363d14c5d2SYehuda Sadeh  * build new request AND message, calculate layout, and adjust file
10373d14c5d2SYehuda Sadeh  * extent as needed.
10383d14c5d2SYehuda Sadeh  *
10393d14c5d2SYehuda Sadeh  * if the file was recently truncated, we include information about its
10403d14c5d2SYehuda Sadeh  * old and new size so that the object can be updated appropriately.  (we
10413d14c5d2SYehuda Sadeh  * avoid synchronously deleting truncated objects because it's slow.)
10423d14c5d2SYehuda Sadeh  */
10433d14c5d2SYehuda Sadeh struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
10443d14c5d2SYehuda Sadeh 					       struct ceph_file_layout *layout,
10453d14c5d2SYehuda Sadeh 					       struct ceph_vino vino,
1046715e4cd4SYan, Zheng 					       u64 off, u64 *plen,
1047715e4cd4SYan, Zheng 					       unsigned int which, int num_ops,
10483d14c5d2SYehuda Sadeh 					       int opcode, int flags,
10493d14c5d2SYehuda Sadeh 					       struct ceph_snap_context *snapc,
10503d14c5d2SYehuda Sadeh 					       u32 truncate_seq,
10513d14c5d2SYehuda Sadeh 					       u64 truncate_size,
1052153e5167SAlex Elder 					       bool use_mempool)
10533d14c5d2SYehuda Sadeh {
10543d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
105575d1c941SAlex Elder 	u64 objnum = 0;
105675d1c941SAlex Elder 	u64 objoff = 0;
105775d1c941SAlex Elder 	u64 objlen = 0;
10586816282dSSage Weil 	int r;
10593d14c5d2SYehuda Sadeh 
1060ad7a60deSLi Wang 	BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
1061864e9197SYan, Zheng 	       opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
1062864e9197SYan, Zheng 	       opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
10633d14c5d2SYehuda Sadeh 
1064acead002SAlex Elder 	req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
1065ae7ca4a3SAlex Elder 					GFP_NOFS);
106613d1ad16SIlya Dryomov 	if (!req) {
106713d1ad16SIlya Dryomov 		r = -ENOMEM;
106813d1ad16SIlya Dryomov 		goto fail;
106913d1ad16SIlya Dryomov 	}
107079528734SAlex Elder 
10713d14c5d2SYehuda Sadeh 	/* calculate max write size */
1072a19dadfbSAlex Elder 	r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
107313d1ad16SIlya Dryomov 	if (r)
107413d1ad16SIlya Dryomov 		goto fail;
1075a19dadfbSAlex Elder 
1076864e9197SYan, Zheng 	if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
1077144cba14SYan, Zheng 		osd_req_op_init(req, which, opcode, 0);
1078864e9197SYan, Zheng 	} else {
10797627151eSYan, Zheng 		u32 object_size = layout->object_size;
1080864e9197SYan, Zheng 		u32 object_base = off - objoff;
1081ccca4e37SYan, Zheng 		if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
1082d18d1e28SAlex Elder 			if (truncate_size <= object_base) {
1083d18d1e28SAlex Elder 				truncate_size = 0;
1084d18d1e28SAlex Elder 			} else {
1085d18d1e28SAlex Elder 				truncate_size -= object_base;
1086d18d1e28SAlex Elder 				if (truncate_size > object_size)
1087d18d1e28SAlex Elder 					truncate_size = object_size;
1088d18d1e28SAlex Elder 			}
1089ccca4e37SYan, Zheng 		}
1090715e4cd4SYan, Zheng 		osd_req_op_extent_init(req, which, opcode, objoff, objlen,
1091b0270324SAlex Elder 				       truncate_size, truncate_seq);
1092864e9197SYan, Zheng 	}
1093d18d1e28SAlex Elder 
10947627151eSYan, Zheng 	req->r_base_oloc.pool = layout->pool_id;
109530c156d9SYan, Zheng 	req->r_base_oloc.pool_ns = ceph_try_get_string(layout->pool_ns);
1096d30291b9SIlya Dryomov 	ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum);
109722d2cfdfSIlya Dryomov 	req->r_flags = flags | osdc->client->options->read_from_replica;
1098dbe0fc41SAlex Elder 
1099bb873b53SIlya Dryomov 	req->r_snapid = vino.snap;
1100bb873b53SIlya Dryomov 	if (flags & CEPH_OSD_FLAG_WRITE)
1101bb873b53SIlya Dryomov 		req->r_data_offset = off;
1102bb873b53SIlya Dryomov 
11030d9c1ab3SIlya Dryomov 	if (num_ops > 1)
11040d9c1ab3SIlya Dryomov 		/*
11050d9c1ab3SIlya Dryomov 		 * This is a special case for ceph_writepages_start(), but it
11060d9c1ab3SIlya Dryomov 		 * also covers ceph_uninline_data().  If more multi-op request
11070d9c1ab3SIlya Dryomov 		 * use cases emerge, we will need a separate helper.
11080d9c1ab3SIlya Dryomov 		 */
11090d9c1ab3SIlya Dryomov 		r = __ceph_osdc_alloc_messages(req, GFP_NOFS, num_ops, 0);
11100d9c1ab3SIlya Dryomov 	else
111113d1ad16SIlya Dryomov 		r = ceph_osdc_alloc_messages(req, GFP_NOFS);
111213d1ad16SIlya Dryomov 	if (r)
111313d1ad16SIlya Dryomov 		goto fail;
111413d1ad16SIlya Dryomov 
11153d14c5d2SYehuda Sadeh 	return req;
111613d1ad16SIlya Dryomov 
111713d1ad16SIlya Dryomov fail:
111813d1ad16SIlya Dryomov 	ceph_osdc_put_request(req);
111913d1ad16SIlya Dryomov 	return ERR_PTR(r);
11203d14c5d2SYehuda Sadeh }
11213d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_new_request);
11223d14c5d2SYehuda Sadeh 
11233d14c5d2SYehuda Sadeh /*
11243d14c5d2SYehuda Sadeh  * We keep osd requests in an rbtree, sorted by ->r_tid.
11253d14c5d2SYehuda Sadeh  */
1126fcd00b68SIlya Dryomov DEFINE_RB_FUNCS(request, struct ceph_osd_request, r_tid, r_node)
11274609245eSIlya Dryomov DEFINE_RB_FUNCS(request_mc, struct ceph_osd_request, r_tid, r_mc_node)
11283d14c5d2SYehuda Sadeh 
112966850df5SIlya Dryomov /*
113066850df5SIlya Dryomov  * Call @fn on each OSD request as long as @fn returns 0.
113166850df5SIlya Dryomov  */
113266850df5SIlya Dryomov static void for_each_request(struct ceph_osd_client *osdc,
113366850df5SIlya Dryomov 			int (*fn)(struct ceph_osd_request *req, void *arg),
113466850df5SIlya Dryomov 			void *arg)
113566850df5SIlya Dryomov {
113666850df5SIlya Dryomov 	struct rb_node *n, *p;
113766850df5SIlya Dryomov 
113866850df5SIlya Dryomov 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
113966850df5SIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
114066850df5SIlya Dryomov 
114166850df5SIlya Dryomov 		for (p = rb_first(&osd->o_requests); p; ) {
114266850df5SIlya Dryomov 			struct ceph_osd_request *req =
114366850df5SIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
114466850df5SIlya Dryomov 
114566850df5SIlya Dryomov 			p = rb_next(p);
114666850df5SIlya Dryomov 			if (fn(req, arg))
114766850df5SIlya Dryomov 				return;
114866850df5SIlya Dryomov 		}
114966850df5SIlya Dryomov 	}
115066850df5SIlya Dryomov 
115166850df5SIlya Dryomov 	for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
115266850df5SIlya Dryomov 		struct ceph_osd_request *req =
115366850df5SIlya Dryomov 		    rb_entry(p, struct ceph_osd_request, r_node);
115466850df5SIlya Dryomov 
115566850df5SIlya Dryomov 		p = rb_next(p);
115666850df5SIlya Dryomov 		if (fn(req, arg))
115766850df5SIlya Dryomov 			return;
115866850df5SIlya Dryomov 	}
115966850df5SIlya Dryomov }
116066850df5SIlya Dryomov 
11610247a0cfSIlya Dryomov static bool osd_homeless(struct ceph_osd *osd)
11620247a0cfSIlya Dryomov {
11630247a0cfSIlya Dryomov 	return osd->o_osd == CEPH_HOMELESS_OSD;
11640247a0cfSIlya Dryomov }
11650247a0cfSIlya Dryomov 
11665aea3dcdSIlya Dryomov static bool osd_registered(struct ceph_osd *osd)
11673d14c5d2SYehuda Sadeh {
11685aea3dcdSIlya Dryomov 	verify_osdc_locked(osd->o_osdc);
11693d14c5d2SYehuda Sadeh 
11705aea3dcdSIlya Dryomov 	return !RB_EMPTY_NODE(&osd->o_node);
11713d14c5d2SYehuda Sadeh }
11723d14c5d2SYehuda Sadeh 
11733d14c5d2SYehuda Sadeh /*
11740247a0cfSIlya Dryomov  * Assumes @osd is zero-initialized.
11750247a0cfSIlya Dryomov  */
11760247a0cfSIlya Dryomov static void osd_init(struct ceph_osd *osd)
11770247a0cfSIlya Dryomov {
117802113a0fSElena Reshetova 	refcount_set(&osd->o_ref, 1);
11790247a0cfSIlya Dryomov 	RB_CLEAR_NODE(&osd->o_node);
1180*08b8a044SJeff Layton 	spin_lock_init(&osd->o_requests_lock);
11815aea3dcdSIlya Dryomov 	osd->o_requests = RB_ROOT;
1182922dab61SIlya Dryomov 	osd->o_linger_requests = RB_ROOT;
1183a02a946dSIlya Dryomov 	osd->o_backoff_mappings = RB_ROOT;
1184a02a946dSIlya Dryomov 	osd->o_backoffs_by_id = RB_ROOT;
11850247a0cfSIlya Dryomov 	INIT_LIST_HEAD(&osd->o_osd_lru);
11860247a0cfSIlya Dryomov 	INIT_LIST_HEAD(&osd->o_keepalive_item);
11870247a0cfSIlya Dryomov 	osd->o_incarnation = 1;
11885aea3dcdSIlya Dryomov 	mutex_init(&osd->lock);
11890247a0cfSIlya Dryomov }
11900247a0cfSIlya Dryomov 
11910247a0cfSIlya Dryomov static void osd_cleanup(struct ceph_osd *osd)
11920247a0cfSIlya Dryomov {
11930247a0cfSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&osd->o_node));
11945aea3dcdSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
1195922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
1196a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoff_mappings));
1197a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoffs_by_id));
11980247a0cfSIlya Dryomov 	WARN_ON(!list_empty(&osd->o_osd_lru));
11990247a0cfSIlya Dryomov 	WARN_ON(!list_empty(&osd->o_keepalive_item));
12000247a0cfSIlya Dryomov 
12010247a0cfSIlya Dryomov 	if (osd->o_auth.authorizer) {
12020247a0cfSIlya Dryomov 		WARN_ON(osd_homeless(osd));
12030247a0cfSIlya Dryomov 		ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
12040247a0cfSIlya Dryomov 	}
12050247a0cfSIlya Dryomov }
12060247a0cfSIlya Dryomov 
12070247a0cfSIlya Dryomov /*
12083d14c5d2SYehuda Sadeh  * Track open sessions with osds.
12093d14c5d2SYehuda Sadeh  */
1210e10006f8SAlex Elder static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
12113d14c5d2SYehuda Sadeh {
12123d14c5d2SYehuda Sadeh 	struct ceph_osd *osd;
12133d14c5d2SYehuda Sadeh 
12140247a0cfSIlya Dryomov 	WARN_ON(onum == CEPH_HOMELESS_OSD);
12150247a0cfSIlya Dryomov 
12167a28f59bSIlya Dryomov 	osd = kzalloc(sizeof(*osd), GFP_NOIO | __GFP_NOFAIL);
12170247a0cfSIlya Dryomov 	osd_init(osd);
12183d14c5d2SYehuda Sadeh 	osd->o_osdc = osdc;
1219e10006f8SAlex Elder 	osd->o_osd = onum;
12203d14c5d2SYehuda Sadeh 
1221b7a9e5ddSSage Weil 	ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
12223d14c5d2SYehuda Sadeh 
12233d14c5d2SYehuda Sadeh 	return osd;
12243d14c5d2SYehuda Sadeh }
12253d14c5d2SYehuda Sadeh 
12263d14c5d2SYehuda Sadeh static struct ceph_osd *get_osd(struct ceph_osd *osd)
12273d14c5d2SYehuda Sadeh {
122802113a0fSElena Reshetova 	if (refcount_inc_not_zero(&osd->o_ref)) {
122902113a0fSElena Reshetova 		dout("get_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref)-1,
123002113a0fSElena Reshetova 		     refcount_read(&osd->o_ref));
12313d14c5d2SYehuda Sadeh 		return osd;
12323d14c5d2SYehuda Sadeh 	} else {
12333d14c5d2SYehuda Sadeh 		dout("get_osd %p FAIL\n", osd);
12343d14c5d2SYehuda Sadeh 		return NULL;
12353d14c5d2SYehuda Sadeh 	}
12363d14c5d2SYehuda Sadeh }
12373d14c5d2SYehuda Sadeh 
12383d14c5d2SYehuda Sadeh static void put_osd(struct ceph_osd *osd)
12393d14c5d2SYehuda Sadeh {
124002113a0fSElena Reshetova 	dout("put_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref),
124102113a0fSElena Reshetova 	     refcount_read(&osd->o_ref) - 1);
124202113a0fSElena Reshetova 	if (refcount_dec_and_test(&osd->o_ref)) {
12430247a0cfSIlya Dryomov 		osd_cleanup(osd);
12443d14c5d2SYehuda Sadeh 		kfree(osd);
12453d14c5d2SYehuda Sadeh 	}
12463d14c5d2SYehuda Sadeh }
12473d14c5d2SYehuda Sadeh 
1248fcd00b68SIlya Dryomov DEFINE_RB_FUNCS(osd, struct ceph_osd, o_osd, o_node)
1249fcd00b68SIlya Dryomov 
12509dd2845cSIlya Dryomov static void __move_osd_to_lru(struct ceph_osd *osd)
12513d14c5d2SYehuda Sadeh {
12529dd2845cSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
12539dd2845cSIlya Dryomov 
12549dd2845cSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
12553d14c5d2SYehuda Sadeh 	BUG_ON(!list_empty(&osd->o_osd_lru));
1256bbf37ec3SIlya Dryomov 
12579dd2845cSIlya Dryomov 	spin_lock(&osdc->osd_lru_lock);
12583d14c5d2SYehuda Sadeh 	list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
12599dd2845cSIlya Dryomov 	spin_unlock(&osdc->osd_lru_lock);
12609dd2845cSIlya Dryomov 
1261a319bf56SIlya Dryomov 	osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
12623d14c5d2SYehuda Sadeh }
12633d14c5d2SYehuda Sadeh 
12649dd2845cSIlya Dryomov static void maybe_move_osd_to_lru(struct ceph_osd *osd)
1265bbf37ec3SIlya Dryomov {
12665aea3dcdSIlya Dryomov 	if (RB_EMPTY_ROOT(&osd->o_requests) &&
1267922dab61SIlya Dryomov 	    RB_EMPTY_ROOT(&osd->o_linger_requests))
12689dd2845cSIlya Dryomov 		__move_osd_to_lru(osd);
1269bbf37ec3SIlya Dryomov }
1270bbf37ec3SIlya Dryomov 
12713d14c5d2SYehuda Sadeh static void __remove_osd_from_lru(struct ceph_osd *osd)
12723d14c5d2SYehuda Sadeh {
12739dd2845cSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
12749dd2845cSIlya Dryomov 
12759dd2845cSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
12769dd2845cSIlya Dryomov 
12779dd2845cSIlya Dryomov 	spin_lock(&osdc->osd_lru_lock);
12783d14c5d2SYehuda Sadeh 	if (!list_empty(&osd->o_osd_lru))
12793d14c5d2SYehuda Sadeh 		list_del_init(&osd->o_osd_lru);
12809dd2845cSIlya Dryomov 	spin_unlock(&osdc->osd_lru_lock);
12813d14c5d2SYehuda Sadeh }
12823d14c5d2SYehuda Sadeh 
12833d14c5d2SYehuda Sadeh /*
12845aea3dcdSIlya Dryomov  * Close the connection and assign any leftover requests to the
12855aea3dcdSIlya Dryomov  * homeless session.
12865aea3dcdSIlya Dryomov  */
12875aea3dcdSIlya Dryomov static void close_osd(struct ceph_osd *osd)
12885aea3dcdSIlya Dryomov {
12895aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
12905aea3dcdSIlya Dryomov 	struct rb_node *n;
12915aea3dcdSIlya Dryomov 
12925aea3dcdSIlya Dryomov 	verify_osdc_wrlocked(osdc);
12935aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
12945aea3dcdSIlya Dryomov 
12955aea3dcdSIlya Dryomov 	ceph_con_close(&osd->o_con);
12965aea3dcdSIlya Dryomov 
12975aea3dcdSIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; ) {
12985aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
12995aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
13005aea3dcdSIlya Dryomov 
13015aea3dcdSIlya Dryomov 		n = rb_next(n); /* unlink_request() */
13025aea3dcdSIlya Dryomov 
13035aea3dcdSIlya Dryomov 		dout(" reassigning req %p tid %llu\n", req, req->r_tid);
13045aea3dcdSIlya Dryomov 		unlink_request(osd, req);
13055aea3dcdSIlya Dryomov 		link_request(&osdc->homeless_osd, req);
13065aea3dcdSIlya Dryomov 	}
1307922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_linger_requests); n; ) {
1308922dab61SIlya Dryomov 		struct ceph_osd_linger_request *lreq =
1309922dab61SIlya Dryomov 		    rb_entry(n, struct ceph_osd_linger_request, node);
1310922dab61SIlya Dryomov 
1311922dab61SIlya Dryomov 		n = rb_next(n); /* unlink_linger() */
1312922dab61SIlya Dryomov 
1313922dab61SIlya Dryomov 		dout(" reassigning lreq %p linger_id %llu\n", lreq,
1314922dab61SIlya Dryomov 		     lreq->linger_id);
1315922dab61SIlya Dryomov 		unlink_linger(osd, lreq);
1316922dab61SIlya Dryomov 		link_linger(&osdc->homeless_osd, lreq);
1317922dab61SIlya Dryomov 	}
1318a02a946dSIlya Dryomov 	clear_backoffs(osd);
13195aea3dcdSIlya Dryomov 
13205aea3dcdSIlya Dryomov 	__remove_osd_from_lru(osd);
13215aea3dcdSIlya Dryomov 	erase_osd(&osdc->osds, osd);
13225aea3dcdSIlya Dryomov 	put_osd(osd);
13235aea3dcdSIlya Dryomov }
13245aea3dcdSIlya Dryomov 
13255aea3dcdSIlya Dryomov /*
13263d14c5d2SYehuda Sadeh  * reset osd connect
13273d14c5d2SYehuda Sadeh  */
13285aea3dcdSIlya Dryomov static int reopen_osd(struct ceph_osd *osd)
13293d14c5d2SYehuda Sadeh {
1330c3acb181SAlex Elder 	struct ceph_entity_addr *peer_addr;
13313d14c5d2SYehuda Sadeh 
13325aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
13335aea3dcdSIlya Dryomov 
13345aea3dcdSIlya Dryomov 	if (RB_EMPTY_ROOT(&osd->o_requests) &&
1335922dab61SIlya Dryomov 	    RB_EMPTY_ROOT(&osd->o_linger_requests)) {
13365aea3dcdSIlya Dryomov 		close_osd(osd);
1337c3acb181SAlex Elder 		return -ENODEV;
1338c3acb181SAlex Elder 	}
1339c3acb181SAlex Elder 
13405aea3dcdSIlya Dryomov 	peer_addr = &osd->o_osdc->osdmap->osd_addr[osd->o_osd];
1341c3acb181SAlex Elder 	if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
13423d14c5d2SYehuda Sadeh 			!ceph_con_opened(&osd->o_con)) {
13435aea3dcdSIlya Dryomov 		struct rb_node *n;
1344c3acb181SAlex Elder 
13453d14c5d2SYehuda Sadeh 		dout("osd addr hasn't changed and connection never opened, "
13460b4af2e8SIlya Dryomov 		     "letting msgr retry\n");
13473d14c5d2SYehuda Sadeh 		/* touch each r_stamp for handle_timeout()'s benfit */
13485aea3dcdSIlya Dryomov 		for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
13495aea3dcdSIlya Dryomov 			struct ceph_osd_request *req =
13505aea3dcdSIlya Dryomov 			    rb_entry(n, struct ceph_osd_request, r_node);
13513d14c5d2SYehuda Sadeh 			req->r_stamp = jiffies;
13525aea3dcdSIlya Dryomov 		}
1353c3acb181SAlex Elder 
1354c3acb181SAlex Elder 		return -EAGAIN;
13553d14c5d2SYehuda Sadeh 	}
1356c3acb181SAlex Elder 
1357c3acb181SAlex Elder 	ceph_con_close(&osd->o_con);
1358c3acb181SAlex Elder 	ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1359c3acb181SAlex Elder 	osd->o_incarnation++;
1360c3acb181SAlex Elder 
1361c3acb181SAlex Elder 	return 0;
13623d14c5d2SYehuda Sadeh }
13633d14c5d2SYehuda Sadeh 
13645aea3dcdSIlya Dryomov static struct ceph_osd *lookup_create_osd(struct ceph_osd_client *osdc, int o,
13655aea3dcdSIlya Dryomov 					  bool wrlocked)
13663d14c5d2SYehuda Sadeh {
13675aea3dcdSIlya Dryomov 	struct ceph_osd *osd;
13685aea3dcdSIlya Dryomov 
13695aea3dcdSIlya Dryomov 	if (wrlocked)
13705aea3dcdSIlya Dryomov 		verify_osdc_wrlocked(osdc);
13715aea3dcdSIlya Dryomov 	else
13725aea3dcdSIlya Dryomov 		verify_osdc_locked(osdc);
13735aea3dcdSIlya Dryomov 
13745aea3dcdSIlya Dryomov 	if (o != CEPH_HOMELESS_OSD)
13755aea3dcdSIlya Dryomov 		osd = lookup_osd(&osdc->osds, o);
13765aea3dcdSIlya Dryomov 	else
13775aea3dcdSIlya Dryomov 		osd = &osdc->homeless_osd;
13785aea3dcdSIlya Dryomov 	if (!osd) {
13795aea3dcdSIlya Dryomov 		if (!wrlocked)
13805aea3dcdSIlya Dryomov 			return ERR_PTR(-EAGAIN);
13815aea3dcdSIlya Dryomov 
13825aea3dcdSIlya Dryomov 		osd = create_osd(osdc, o);
13835aea3dcdSIlya Dryomov 		insert_osd(&osdc->osds, osd);
13845aea3dcdSIlya Dryomov 		ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
13855aea3dcdSIlya Dryomov 			      &osdc->osdmap->osd_addr[osd->o_osd]);
13865aea3dcdSIlya Dryomov 	}
13875aea3dcdSIlya Dryomov 
13885aea3dcdSIlya Dryomov 	dout("%s osdc %p osd%d -> osd %p\n", __func__, osdc, o, osd);
13895aea3dcdSIlya Dryomov 	return osd;
1390a40c4f10SYehuda Sadeh }
1391a40c4f10SYehuda Sadeh 
13923d14c5d2SYehuda Sadeh /*
13935aea3dcdSIlya Dryomov  * Create request <-> OSD session relation.
13945aea3dcdSIlya Dryomov  *
13955aea3dcdSIlya Dryomov  * @req has to be assigned a tid, @osd may be homeless.
13963d14c5d2SYehuda Sadeh  */
13975aea3dcdSIlya Dryomov static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req)
13983d14c5d2SYehuda Sadeh {
13995aea3dcdSIlya Dryomov 	verify_osd_locked(osd);
14005aea3dcdSIlya Dryomov 	WARN_ON(!req->r_tid || req->r_osd);
14015aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
140235f9f8a0SSage Weil 	     req, req->r_tid);
14035aea3dcdSIlya Dryomov 
14045aea3dcdSIlya Dryomov 	if (!osd_homeless(osd))
14055aea3dcdSIlya Dryomov 		__remove_osd_from_lru(osd);
14065aea3dcdSIlya Dryomov 	else
14075aea3dcdSIlya Dryomov 		atomic_inc(&osd->o_osdc->num_homeless);
14085aea3dcdSIlya Dryomov 
14095aea3dcdSIlya Dryomov 	get_osd(osd);
1410*08b8a044SJeff Layton 	spin_lock(&osd->o_requests_lock);
14115aea3dcdSIlya Dryomov 	insert_request(&osd->o_requests, req);
1412*08b8a044SJeff Layton 	spin_unlock(&osd->o_requests_lock);
14135aea3dcdSIlya Dryomov 	req->r_osd = osd;
141435f9f8a0SSage Weil }
141535f9f8a0SSage Weil 
14165aea3dcdSIlya Dryomov static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req)
14173d14c5d2SYehuda Sadeh {
14185aea3dcdSIlya Dryomov 	verify_osd_locked(osd);
14195aea3dcdSIlya Dryomov 	WARN_ON(req->r_osd != osd);
14205aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
14215aea3dcdSIlya Dryomov 	     req, req->r_tid);
14225aea3dcdSIlya Dryomov 
14235aea3dcdSIlya Dryomov 	req->r_osd = NULL;
1424*08b8a044SJeff Layton 	spin_lock(&osd->o_requests_lock);
14255aea3dcdSIlya Dryomov 	erase_request(&osd->o_requests, req);
1426*08b8a044SJeff Layton 	spin_unlock(&osd->o_requests_lock);
14275aea3dcdSIlya Dryomov 	put_osd(osd);
14285aea3dcdSIlya Dryomov 
14295aea3dcdSIlya Dryomov 	if (!osd_homeless(osd))
14305aea3dcdSIlya Dryomov 		maybe_move_osd_to_lru(osd);
14315aea3dcdSIlya Dryomov 	else
14325aea3dcdSIlya Dryomov 		atomic_dec(&osd->o_osdc->num_homeless);
14333d14c5d2SYehuda Sadeh }
14343d14c5d2SYehuda Sadeh 
143563244fa1SIlya Dryomov static bool __pool_full(struct ceph_pg_pool_info *pi)
143663244fa1SIlya Dryomov {
143763244fa1SIlya Dryomov 	return pi->flags & CEPH_POOL_FLAG_FULL;
143863244fa1SIlya Dryomov }
143963244fa1SIlya Dryomov 
144042c1b124SIlya Dryomov static bool have_pool_full(struct ceph_osd_client *osdc)
144142c1b124SIlya Dryomov {
144242c1b124SIlya Dryomov 	struct rb_node *n;
144342c1b124SIlya Dryomov 
144442c1b124SIlya Dryomov 	for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
144542c1b124SIlya Dryomov 		struct ceph_pg_pool_info *pi =
144642c1b124SIlya Dryomov 		    rb_entry(n, struct ceph_pg_pool_info, node);
144742c1b124SIlya Dryomov 
144842c1b124SIlya Dryomov 		if (__pool_full(pi))
144942c1b124SIlya Dryomov 			return true;
145042c1b124SIlya Dryomov 	}
145142c1b124SIlya Dryomov 
145242c1b124SIlya Dryomov 	return false;
145342c1b124SIlya Dryomov }
145442c1b124SIlya Dryomov 
14555aea3dcdSIlya Dryomov static bool pool_full(struct ceph_osd_client *osdc, s64 pool_id)
14565aea3dcdSIlya Dryomov {
14575aea3dcdSIlya Dryomov 	struct ceph_pg_pool_info *pi;
14585aea3dcdSIlya Dryomov 
14595aea3dcdSIlya Dryomov 	pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
14605aea3dcdSIlya Dryomov 	if (!pi)
14615aea3dcdSIlya Dryomov 		return false;
14625aea3dcdSIlya Dryomov 
14635aea3dcdSIlya Dryomov 	return __pool_full(pi);
14645aea3dcdSIlya Dryomov }
14655aea3dcdSIlya Dryomov 
14663d14c5d2SYehuda Sadeh /*
1467d29adb34SJosh Durgin  * Returns whether a request should be blocked from being sent
1468d29adb34SJosh Durgin  * based on the current osdmap and osd_client settings.
1469d29adb34SJosh Durgin  */
147063244fa1SIlya Dryomov static bool target_should_be_paused(struct ceph_osd_client *osdc,
147163244fa1SIlya Dryomov 				    const struct ceph_osd_request_target *t,
147263244fa1SIlya Dryomov 				    struct ceph_pg_pool_info *pi)
147363244fa1SIlya Dryomov {
1474b7ec35b3SIlya Dryomov 	bool pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
1475b7ec35b3SIlya Dryomov 	bool pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
1476b7ec35b3SIlya Dryomov 		       ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
147763244fa1SIlya Dryomov 		       __pool_full(pi);
147863244fa1SIlya Dryomov 
14796d637a54SIlya Dryomov 	WARN_ON(pi->id != t->target_oloc.pool);
148058eb7932SJeff Layton 	return ((t->flags & CEPH_OSD_FLAG_READ) && pauserd) ||
148158eb7932SJeff Layton 	       ((t->flags & CEPH_OSD_FLAG_WRITE) && pausewr) ||
148258eb7932SJeff Layton 	       (osdc->osdmap->epoch < osdc->epoch_barrier);
148363244fa1SIlya Dryomov }
148463244fa1SIlya Dryomov 
1485117d96a0SIlya Dryomov static int pick_random_replica(const struct ceph_osds *acting)
1486117d96a0SIlya Dryomov {
14878032bf12SJason A. Donenfeld 	int i = get_random_u32_below(acting->size);
1488117d96a0SIlya Dryomov 
1489117d96a0SIlya Dryomov 	dout("%s picked osd%d, primary osd%d\n", __func__,
1490117d96a0SIlya Dryomov 	     acting->osds[i], acting->primary);
1491117d96a0SIlya Dryomov 	return i;
1492117d96a0SIlya Dryomov }
1493117d96a0SIlya Dryomov 
1494117d96a0SIlya Dryomov /*
1495117d96a0SIlya Dryomov  * Picks the closest replica based on client's location given by
1496117d96a0SIlya Dryomov  * crush_location option.  Prefers the primary if the locality is
1497117d96a0SIlya Dryomov  * the same.
1498117d96a0SIlya Dryomov  */
1499117d96a0SIlya Dryomov static int pick_closest_replica(struct ceph_osd_client *osdc,
1500117d96a0SIlya Dryomov 				const struct ceph_osds *acting)
1501117d96a0SIlya Dryomov {
1502117d96a0SIlya Dryomov 	struct ceph_options *opt = osdc->client->options;
1503117d96a0SIlya Dryomov 	int best_i, best_locality;
1504117d96a0SIlya Dryomov 	int i = 0, locality;
1505117d96a0SIlya Dryomov 
1506117d96a0SIlya Dryomov 	do {
1507117d96a0SIlya Dryomov 		locality = ceph_get_crush_locality(osdc->osdmap,
1508117d96a0SIlya Dryomov 						   acting->osds[i],
1509117d96a0SIlya Dryomov 						   &opt->crush_locs);
1510117d96a0SIlya Dryomov 		if (i == 0 ||
1511117d96a0SIlya Dryomov 		    (locality >= 0 && best_locality < 0) ||
1512117d96a0SIlya Dryomov 		    (locality >= 0 && best_locality >= 0 &&
1513117d96a0SIlya Dryomov 		     locality < best_locality)) {
1514117d96a0SIlya Dryomov 			best_i = i;
1515117d96a0SIlya Dryomov 			best_locality = locality;
1516117d96a0SIlya Dryomov 		}
1517117d96a0SIlya Dryomov 	} while (++i < acting->size);
1518117d96a0SIlya Dryomov 
1519117d96a0SIlya Dryomov 	dout("%s picked osd%d with locality %d, primary osd%d\n", __func__,
1520117d96a0SIlya Dryomov 	     acting->osds[best_i], best_locality, acting->primary);
1521117d96a0SIlya Dryomov 	return best_i;
1522117d96a0SIlya Dryomov }
1523117d96a0SIlya Dryomov 
152463244fa1SIlya Dryomov enum calc_target_result {
152563244fa1SIlya Dryomov 	CALC_TARGET_NO_ACTION = 0,
152663244fa1SIlya Dryomov 	CALC_TARGET_NEED_RESEND,
152763244fa1SIlya Dryomov 	CALC_TARGET_POOL_DNE,
152863244fa1SIlya Dryomov };
152963244fa1SIlya Dryomov 
153063244fa1SIlya Dryomov static enum calc_target_result calc_target(struct ceph_osd_client *osdc,
153163244fa1SIlya Dryomov 					   struct ceph_osd_request_target *t,
153263244fa1SIlya Dryomov 					   bool any_change)
153363244fa1SIlya Dryomov {
153463244fa1SIlya Dryomov 	struct ceph_pg_pool_info *pi;
153563244fa1SIlya Dryomov 	struct ceph_pg pgid, last_pgid;
153663244fa1SIlya Dryomov 	struct ceph_osds up, acting;
1537117d96a0SIlya Dryomov 	bool is_read = t->flags & CEPH_OSD_FLAG_READ;
1538117d96a0SIlya Dryomov 	bool is_write = t->flags & CEPH_OSD_FLAG_WRITE;
153963244fa1SIlya Dryomov 	bool force_resend = false;
154084ed45dfSIlya Dryomov 	bool unpaused = false;
1541a5613724SIlya Dryomov 	bool legacy_change = false;
15427de030d6SIlya Dryomov 	bool split = false;
1543b7ec35b3SIlya Dryomov 	bool sort_bitwise = ceph_osdmap_flag(osdc, CEPH_OSDMAP_SORTBITWISE);
1544ae78dd81SIlya Dryomov 	bool recovery_deletes = ceph_osdmap_flag(osdc,
1545ae78dd81SIlya Dryomov 						 CEPH_OSDMAP_RECOVERY_DELETES);
154663244fa1SIlya Dryomov 	enum calc_target_result ct_res;
154763244fa1SIlya Dryomov 
154804c7d789SIlya Dryomov 	t->epoch = osdc->osdmap->epoch;
154963244fa1SIlya Dryomov 	pi = ceph_pg_pool_by_id(osdc->osdmap, t->base_oloc.pool);
155063244fa1SIlya Dryomov 	if (!pi) {
155163244fa1SIlya Dryomov 		t->osd = CEPH_HOMELESS_OSD;
155263244fa1SIlya Dryomov 		ct_res = CALC_TARGET_POOL_DNE;
155363244fa1SIlya Dryomov 		goto out;
155463244fa1SIlya Dryomov 	}
155563244fa1SIlya Dryomov 
155663244fa1SIlya Dryomov 	if (osdc->osdmap->epoch == pi->last_force_request_resend) {
1557dc93e0e2SIlya Dryomov 		if (t->last_force_resend < pi->last_force_request_resend) {
1558dc93e0e2SIlya Dryomov 			t->last_force_resend = pi->last_force_request_resend;
155963244fa1SIlya Dryomov 			force_resend = true;
1560dc93e0e2SIlya Dryomov 		} else if (t->last_force_resend == 0) {
156163244fa1SIlya Dryomov 			force_resend = true;
156263244fa1SIlya Dryomov 		}
156363244fa1SIlya Dryomov 	}
156463244fa1SIlya Dryomov 
1565db098ec4SIlya Dryomov 	/* apply tiering */
1566db098ec4SIlya Dryomov 	ceph_oid_copy(&t->target_oid, &t->base_oid);
1567db098ec4SIlya Dryomov 	ceph_oloc_copy(&t->target_oloc, &t->base_oloc);
1568db098ec4SIlya Dryomov 	if ((t->flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
1569117d96a0SIlya Dryomov 		if (is_read && pi->read_tier >= 0)
157063244fa1SIlya Dryomov 			t->target_oloc.pool = pi->read_tier;
1571117d96a0SIlya Dryomov 		if (is_write && pi->write_tier >= 0)
157263244fa1SIlya Dryomov 			t->target_oloc.pool = pi->write_tier;
15736d637a54SIlya Dryomov 
15746d637a54SIlya Dryomov 		pi = ceph_pg_pool_by_id(osdc->osdmap, t->target_oloc.pool);
15756d637a54SIlya Dryomov 		if (!pi) {
15766d637a54SIlya Dryomov 			t->osd = CEPH_HOMELESS_OSD;
15776d637a54SIlya Dryomov 			ct_res = CALC_TARGET_POOL_DNE;
15786d637a54SIlya Dryomov 			goto out;
15796d637a54SIlya Dryomov 		}
158063244fa1SIlya Dryomov 	}
158163244fa1SIlya Dryomov 
1582a86f009fSIlya Dryomov 	__ceph_object_locator_to_pg(pi, &t->target_oid, &t->target_oloc, &pgid);
158363244fa1SIlya Dryomov 	last_pgid.pool = pgid.pool;
158463244fa1SIlya Dryomov 	last_pgid.seed = ceph_stable_mod(pgid.seed, t->pg_num, t->pg_num_mask);
158563244fa1SIlya Dryomov 
1586df28152dSIlya Dryomov 	ceph_pg_to_up_acting_osds(osdc->osdmap, pi, &pgid, &up, &acting);
158763244fa1SIlya Dryomov 	if (any_change &&
158863244fa1SIlya Dryomov 	    ceph_is_new_interval(&t->acting,
158963244fa1SIlya Dryomov 				 &acting,
159063244fa1SIlya Dryomov 				 &t->up,
159163244fa1SIlya Dryomov 				 &up,
159263244fa1SIlya Dryomov 				 t->size,
159363244fa1SIlya Dryomov 				 pi->size,
159463244fa1SIlya Dryomov 				 t->min_size,
159563244fa1SIlya Dryomov 				 pi->min_size,
159663244fa1SIlya Dryomov 				 t->pg_num,
159763244fa1SIlya Dryomov 				 pi->pg_num,
159863244fa1SIlya Dryomov 				 t->sort_bitwise,
159963244fa1SIlya Dryomov 				 sort_bitwise,
1600ae78dd81SIlya Dryomov 				 t->recovery_deletes,
1601ae78dd81SIlya Dryomov 				 recovery_deletes,
160263244fa1SIlya Dryomov 				 &last_pgid))
160363244fa1SIlya Dryomov 		force_resend = true;
160463244fa1SIlya Dryomov 
160563244fa1SIlya Dryomov 	if (t->paused && !target_should_be_paused(osdc, t, pi)) {
160663244fa1SIlya Dryomov 		t->paused = false;
160784ed45dfSIlya Dryomov 		unpaused = true;
160863244fa1SIlya Dryomov 	}
160984ed45dfSIlya Dryomov 	legacy_change = ceph_pg_compare(&t->pgid, &pgid) ||
1610117d96a0SIlya Dryomov 			ceph_osds_changed(&t->acting, &acting,
1611117d96a0SIlya Dryomov 					  t->used_replica || any_change);
16127de030d6SIlya Dryomov 	if (t->pg_num)
16137de030d6SIlya Dryomov 		split = ceph_pg_is_split(&last_pgid, t->pg_num, pi->pg_num);
161463244fa1SIlya Dryomov 
16157de030d6SIlya Dryomov 	if (legacy_change || force_resend || split) {
161663244fa1SIlya Dryomov 		t->pgid = pgid; /* struct */
1617df28152dSIlya Dryomov 		ceph_pg_to_primary_shard(osdc->osdmap, pi, &pgid, &t->spgid);
161863244fa1SIlya Dryomov 		ceph_osds_copy(&t->acting, &acting);
161963244fa1SIlya Dryomov 		ceph_osds_copy(&t->up, &up);
162063244fa1SIlya Dryomov 		t->size = pi->size;
162163244fa1SIlya Dryomov 		t->min_size = pi->min_size;
162263244fa1SIlya Dryomov 		t->pg_num = pi->pg_num;
162363244fa1SIlya Dryomov 		t->pg_num_mask = pi->pg_num_mask;
162463244fa1SIlya Dryomov 		t->sort_bitwise = sort_bitwise;
1625ae78dd81SIlya Dryomov 		t->recovery_deletes = recovery_deletes;
162663244fa1SIlya Dryomov 
1627117d96a0SIlya Dryomov 		if ((t->flags & (CEPH_OSD_FLAG_BALANCE_READS |
1628117d96a0SIlya Dryomov 				 CEPH_OSD_FLAG_LOCALIZE_READS)) &&
1629117d96a0SIlya Dryomov 		    !is_write && pi->type == CEPH_POOL_TYPE_REP &&
1630117d96a0SIlya Dryomov 		    acting.size > 1) {
1631117d96a0SIlya Dryomov 			int pos;
1632117d96a0SIlya Dryomov 
1633117d96a0SIlya Dryomov 			WARN_ON(!is_read || acting.osds[0] != acting.primary);
1634117d96a0SIlya Dryomov 			if (t->flags & CEPH_OSD_FLAG_BALANCE_READS) {
1635117d96a0SIlya Dryomov 				pos = pick_random_replica(&acting);
1636117d96a0SIlya Dryomov 			} else {
1637117d96a0SIlya Dryomov 				pos = pick_closest_replica(osdc, &acting);
1638117d96a0SIlya Dryomov 			}
1639117d96a0SIlya Dryomov 			t->osd = acting.osds[pos];
1640117d96a0SIlya Dryomov 			t->used_replica = pos > 0;
1641117d96a0SIlya Dryomov 		} else {
164263244fa1SIlya Dryomov 			t->osd = acting.primary;
1643117d96a0SIlya Dryomov 			t->used_replica = false;
1644117d96a0SIlya Dryomov 		}
164563244fa1SIlya Dryomov 	}
164663244fa1SIlya Dryomov 
1647a5613724SIlya Dryomov 	if (unpaused || legacy_change || force_resend || split)
164884ed45dfSIlya Dryomov 		ct_res = CALC_TARGET_NEED_RESEND;
164984ed45dfSIlya Dryomov 	else
165084ed45dfSIlya Dryomov 		ct_res = CALC_TARGET_NO_ACTION;
165184ed45dfSIlya Dryomov 
165263244fa1SIlya Dryomov out:
1653a5613724SIlya Dryomov 	dout("%s t %p -> %d%d%d%d ct_res %d osd%d\n", __func__, t, unpaused,
1654a5613724SIlya Dryomov 	     legacy_change, force_resend, split, ct_res, t->osd);
165563244fa1SIlya Dryomov 	return ct_res;
165663244fa1SIlya Dryomov }
165763244fa1SIlya Dryomov 
1658a02a946dSIlya Dryomov static struct ceph_spg_mapping *alloc_spg_mapping(void)
1659a02a946dSIlya Dryomov {
1660a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
1661a02a946dSIlya Dryomov 
1662a02a946dSIlya Dryomov 	spg = kmalloc(sizeof(*spg), GFP_NOIO);
1663a02a946dSIlya Dryomov 	if (!spg)
1664a02a946dSIlya Dryomov 		return NULL;
1665a02a946dSIlya Dryomov 
1666a02a946dSIlya Dryomov 	RB_CLEAR_NODE(&spg->node);
1667a02a946dSIlya Dryomov 	spg->backoffs = RB_ROOT;
1668a02a946dSIlya Dryomov 	return spg;
1669a02a946dSIlya Dryomov }
1670a02a946dSIlya Dryomov 
1671a02a946dSIlya Dryomov static void free_spg_mapping(struct ceph_spg_mapping *spg)
1672a02a946dSIlya Dryomov {
1673a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&spg->node));
1674a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&spg->backoffs));
1675a02a946dSIlya Dryomov 
1676a02a946dSIlya Dryomov 	kfree(spg);
1677a02a946dSIlya Dryomov }
1678a02a946dSIlya Dryomov 
1679a02a946dSIlya Dryomov /*
1680a02a946dSIlya Dryomov  * rbtree of ceph_spg_mapping for handling map<spg_t, ...>, similar to
1681a02a946dSIlya Dryomov  * ceph_pg_mapping.  Used to track OSD backoffs -- a backoff [range] is
1682a02a946dSIlya Dryomov  * defined only within a specific spgid; it does not pass anything to
1683a02a946dSIlya Dryomov  * children on split, or to another primary.
1684a02a946dSIlya Dryomov  */
1685a02a946dSIlya Dryomov DEFINE_RB_FUNCS2(spg_mapping, struct ceph_spg_mapping, spgid, ceph_spg_compare,
1686a02a946dSIlya Dryomov 		 RB_BYPTR, const struct ceph_spg *, node)
1687a02a946dSIlya Dryomov 
1688a02a946dSIlya Dryomov static u64 hoid_get_bitwise_key(const struct ceph_hobject_id *hoid)
1689a02a946dSIlya Dryomov {
1690a02a946dSIlya Dryomov 	return hoid->is_max ? 0x100000000ull : hoid->hash_reverse_bits;
1691a02a946dSIlya Dryomov }
1692a02a946dSIlya Dryomov 
1693a02a946dSIlya Dryomov static void hoid_get_effective_key(const struct ceph_hobject_id *hoid,
1694a02a946dSIlya Dryomov 				   void **pkey, size_t *pkey_len)
1695a02a946dSIlya Dryomov {
1696a02a946dSIlya Dryomov 	if (hoid->key_len) {
1697a02a946dSIlya Dryomov 		*pkey = hoid->key;
1698a02a946dSIlya Dryomov 		*pkey_len = hoid->key_len;
1699a02a946dSIlya Dryomov 	} else {
1700a02a946dSIlya Dryomov 		*pkey = hoid->oid;
1701a02a946dSIlya Dryomov 		*pkey_len = hoid->oid_len;
1702a02a946dSIlya Dryomov 	}
1703a02a946dSIlya Dryomov }
1704a02a946dSIlya Dryomov 
1705a02a946dSIlya Dryomov static int compare_names(const void *name1, size_t name1_len,
1706a02a946dSIlya Dryomov 			 const void *name2, size_t name2_len)
1707a02a946dSIlya Dryomov {
1708a02a946dSIlya Dryomov 	int ret;
1709a02a946dSIlya Dryomov 
1710a02a946dSIlya Dryomov 	ret = memcmp(name1, name2, min(name1_len, name2_len));
1711a02a946dSIlya Dryomov 	if (!ret) {
1712a02a946dSIlya Dryomov 		if (name1_len < name2_len)
1713a02a946dSIlya Dryomov 			ret = -1;
1714a02a946dSIlya Dryomov 		else if (name1_len > name2_len)
1715a02a946dSIlya Dryomov 			ret = 1;
1716a02a946dSIlya Dryomov 	}
1717a02a946dSIlya Dryomov 	return ret;
1718a02a946dSIlya Dryomov }
1719a02a946dSIlya Dryomov 
1720a02a946dSIlya Dryomov static int hoid_compare(const struct ceph_hobject_id *lhs,
1721a02a946dSIlya Dryomov 			const struct ceph_hobject_id *rhs)
1722a02a946dSIlya Dryomov {
1723a02a946dSIlya Dryomov 	void *effective_key1, *effective_key2;
1724a02a946dSIlya Dryomov 	size_t effective_key1_len, effective_key2_len;
1725a02a946dSIlya Dryomov 	int ret;
1726a02a946dSIlya Dryomov 
1727a02a946dSIlya Dryomov 	if (lhs->is_max < rhs->is_max)
1728a02a946dSIlya Dryomov 		return -1;
1729a02a946dSIlya Dryomov 	if (lhs->is_max > rhs->is_max)
1730a02a946dSIlya Dryomov 		return 1;
1731a02a946dSIlya Dryomov 
1732a02a946dSIlya Dryomov 	if (lhs->pool < rhs->pool)
1733a02a946dSIlya Dryomov 		return -1;
1734a02a946dSIlya Dryomov 	if (lhs->pool > rhs->pool)
1735a02a946dSIlya Dryomov 		return 1;
1736a02a946dSIlya Dryomov 
1737a02a946dSIlya Dryomov 	if (hoid_get_bitwise_key(lhs) < hoid_get_bitwise_key(rhs))
1738a02a946dSIlya Dryomov 		return -1;
1739a02a946dSIlya Dryomov 	if (hoid_get_bitwise_key(lhs) > hoid_get_bitwise_key(rhs))
1740a02a946dSIlya Dryomov 		return 1;
1741a02a946dSIlya Dryomov 
1742a02a946dSIlya Dryomov 	ret = compare_names(lhs->nspace, lhs->nspace_len,
1743a02a946dSIlya Dryomov 			    rhs->nspace, rhs->nspace_len);
1744a02a946dSIlya Dryomov 	if (ret)
1745a02a946dSIlya Dryomov 		return ret;
1746a02a946dSIlya Dryomov 
1747a02a946dSIlya Dryomov 	hoid_get_effective_key(lhs, &effective_key1, &effective_key1_len);
1748a02a946dSIlya Dryomov 	hoid_get_effective_key(rhs, &effective_key2, &effective_key2_len);
1749a02a946dSIlya Dryomov 	ret = compare_names(effective_key1, effective_key1_len,
1750a02a946dSIlya Dryomov 			    effective_key2, effective_key2_len);
1751a02a946dSIlya Dryomov 	if (ret)
1752a02a946dSIlya Dryomov 		return ret;
1753a02a946dSIlya Dryomov 
1754a02a946dSIlya Dryomov 	ret = compare_names(lhs->oid, lhs->oid_len, rhs->oid, rhs->oid_len);
1755a02a946dSIlya Dryomov 	if (ret)
1756a02a946dSIlya Dryomov 		return ret;
1757a02a946dSIlya Dryomov 
1758a02a946dSIlya Dryomov 	if (lhs->snapid < rhs->snapid)
1759a02a946dSIlya Dryomov 		return -1;
1760a02a946dSIlya Dryomov 	if (lhs->snapid > rhs->snapid)
1761a02a946dSIlya Dryomov 		return 1;
1762a02a946dSIlya Dryomov 
1763a02a946dSIlya Dryomov 	return 0;
1764a02a946dSIlya Dryomov }
1765a02a946dSIlya Dryomov 
1766a02a946dSIlya Dryomov /*
1767a02a946dSIlya Dryomov  * For decoding ->begin and ->end of MOSDBackoff only -- no MIN/MAX
1768a02a946dSIlya Dryomov  * compat stuff here.
1769a02a946dSIlya Dryomov  *
1770a02a946dSIlya Dryomov  * Assumes @hoid is zero-initialized.
1771a02a946dSIlya Dryomov  */
1772a02a946dSIlya Dryomov static int decode_hoid(void **p, void *end, struct ceph_hobject_id *hoid)
1773a02a946dSIlya Dryomov {
1774a02a946dSIlya Dryomov 	u8 struct_v;
1775a02a946dSIlya Dryomov 	u32 struct_len;
1776a02a946dSIlya Dryomov 	int ret;
1777a02a946dSIlya Dryomov 
1778a02a946dSIlya Dryomov 	ret = ceph_start_decoding(p, end, 4, "hobject_t", &struct_v,
1779a02a946dSIlya Dryomov 				  &struct_len);
1780a02a946dSIlya Dryomov 	if (ret)
1781a02a946dSIlya Dryomov 		return ret;
1782a02a946dSIlya Dryomov 
1783a02a946dSIlya Dryomov 	if (struct_v < 4) {
1784a02a946dSIlya Dryomov 		pr_err("got struct_v %d < 4 of hobject_t\n", struct_v);
1785a02a946dSIlya Dryomov 		goto e_inval;
1786a02a946dSIlya Dryomov 	}
1787a02a946dSIlya Dryomov 
1788a02a946dSIlya Dryomov 	hoid->key = ceph_extract_encoded_string(p, end, &hoid->key_len,
1789a02a946dSIlya Dryomov 						GFP_NOIO);
1790a02a946dSIlya Dryomov 	if (IS_ERR(hoid->key)) {
1791a02a946dSIlya Dryomov 		ret = PTR_ERR(hoid->key);
1792a02a946dSIlya Dryomov 		hoid->key = NULL;
1793a02a946dSIlya Dryomov 		return ret;
1794a02a946dSIlya Dryomov 	}
1795a02a946dSIlya Dryomov 
1796a02a946dSIlya Dryomov 	hoid->oid = ceph_extract_encoded_string(p, end, &hoid->oid_len,
1797a02a946dSIlya Dryomov 						GFP_NOIO);
1798a02a946dSIlya Dryomov 	if (IS_ERR(hoid->oid)) {
1799a02a946dSIlya Dryomov 		ret = PTR_ERR(hoid->oid);
1800a02a946dSIlya Dryomov 		hoid->oid = NULL;
1801a02a946dSIlya Dryomov 		return ret;
1802a02a946dSIlya Dryomov 	}
1803a02a946dSIlya Dryomov 
1804a02a946dSIlya Dryomov 	ceph_decode_64_safe(p, end, hoid->snapid, e_inval);
1805a02a946dSIlya Dryomov 	ceph_decode_32_safe(p, end, hoid->hash, e_inval);
1806a02a946dSIlya Dryomov 	ceph_decode_8_safe(p, end, hoid->is_max, e_inval);
1807a02a946dSIlya Dryomov 
1808a02a946dSIlya Dryomov 	hoid->nspace = ceph_extract_encoded_string(p, end, &hoid->nspace_len,
1809a02a946dSIlya Dryomov 						   GFP_NOIO);
1810a02a946dSIlya Dryomov 	if (IS_ERR(hoid->nspace)) {
1811a02a946dSIlya Dryomov 		ret = PTR_ERR(hoid->nspace);
1812a02a946dSIlya Dryomov 		hoid->nspace = NULL;
1813a02a946dSIlya Dryomov 		return ret;
1814a02a946dSIlya Dryomov 	}
1815a02a946dSIlya Dryomov 
1816a02a946dSIlya Dryomov 	ceph_decode_64_safe(p, end, hoid->pool, e_inval);
1817a02a946dSIlya Dryomov 
1818a02a946dSIlya Dryomov 	ceph_hoid_build_hash_cache(hoid);
1819a02a946dSIlya Dryomov 	return 0;
1820a02a946dSIlya Dryomov 
1821a02a946dSIlya Dryomov e_inval:
1822a02a946dSIlya Dryomov 	return -EINVAL;
1823a02a946dSIlya Dryomov }
1824a02a946dSIlya Dryomov 
1825a02a946dSIlya Dryomov static int hoid_encoding_size(const struct ceph_hobject_id *hoid)
1826a02a946dSIlya Dryomov {
1827a02a946dSIlya Dryomov 	return 8 + 4 + 1 + 8 + /* snapid, hash, is_max, pool */
1828a02a946dSIlya Dryomov 	       4 + hoid->key_len + 4 + hoid->oid_len + 4 + hoid->nspace_len;
1829a02a946dSIlya Dryomov }
1830a02a946dSIlya Dryomov 
1831a02a946dSIlya Dryomov static void encode_hoid(void **p, void *end, const struct ceph_hobject_id *hoid)
1832a02a946dSIlya Dryomov {
1833a02a946dSIlya Dryomov 	ceph_start_encoding(p, 4, 3, hoid_encoding_size(hoid));
1834a02a946dSIlya Dryomov 	ceph_encode_string(p, end, hoid->key, hoid->key_len);
1835a02a946dSIlya Dryomov 	ceph_encode_string(p, end, hoid->oid, hoid->oid_len);
1836a02a946dSIlya Dryomov 	ceph_encode_64(p, hoid->snapid);
1837a02a946dSIlya Dryomov 	ceph_encode_32(p, hoid->hash);
1838a02a946dSIlya Dryomov 	ceph_encode_8(p, hoid->is_max);
1839a02a946dSIlya Dryomov 	ceph_encode_string(p, end, hoid->nspace, hoid->nspace_len);
1840a02a946dSIlya Dryomov 	ceph_encode_64(p, hoid->pool);
1841a02a946dSIlya Dryomov }
1842a02a946dSIlya Dryomov 
1843a02a946dSIlya Dryomov static void free_hoid(struct ceph_hobject_id *hoid)
1844a02a946dSIlya Dryomov {
1845a02a946dSIlya Dryomov 	if (hoid) {
1846a02a946dSIlya Dryomov 		kfree(hoid->key);
1847a02a946dSIlya Dryomov 		kfree(hoid->oid);
1848a02a946dSIlya Dryomov 		kfree(hoid->nspace);
1849a02a946dSIlya Dryomov 		kfree(hoid);
1850a02a946dSIlya Dryomov 	}
1851a02a946dSIlya Dryomov }
1852a02a946dSIlya Dryomov 
1853a02a946dSIlya Dryomov static struct ceph_osd_backoff *alloc_backoff(void)
1854a02a946dSIlya Dryomov {
1855a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
1856a02a946dSIlya Dryomov 
1857a02a946dSIlya Dryomov 	backoff = kzalloc(sizeof(*backoff), GFP_NOIO);
1858a02a946dSIlya Dryomov 	if (!backoff)
1859a02a946dSIlya Dryomov 		return NULL;
1860a02a946dSIlya Dryomov 
1861a02a946dSIlya Dryomov 	RB_CLEAR_NODE(&backoff->spg_node);
1862a02a946dSIlya Dryomov 	RB_CLEAR_NODE(&backoff->id_node);
1863a02a946dSIlya Dryomov 	return backoff;
1864a02a946dSIlya Dryomov }
1865a02a946dSIlya Dryomov 
1866a02a946dSIlya Dryomov static void free_backoff(struct ceph_osd_backoff *backoff)
1867a02a946dSIlya Dryomov {
1868a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&backoff->spg_node));
1869a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&backoff->id_node));
1870a02a946dSIlya Dryomov 
1871a02a946dSIlya Dryomov 	free_hoid(backoff->begin);
1872a02a946dSIlya Dryomov 	free_hoid(backoff->end);
1873a02a946dSIlya Dryomov 	kfree(backoff);
1874a02a946dSIlya Dryomov }
1875a02a946dSIlya Dryomov 
1876a02a946dSIlya Dryomov /*
1877a02a946dSIlya Dryomov  * Within a specific spgid, backoffs are managed by ->begin hoid.
1878a02a946dSIlya Dryomov  */
1879a02a946dSIlya Dryomov DEFINE_RB_INSDEL_FUNCS2(backoff, struct ceph_osd_backoff, begin, hoid_compare,
1880a02a946dSIlya Dryomov 			RB_BYVAL, spg_node);
1881a02a946dSIlya Dryomov 
1882a02a946dSIlya Dryomov static struct ceph_osd_backoff *lookup_containing_backoff(struct rb_root *root,
1883a02a946dSIlya Dryomov 					    const struct ceph_hobject_id *hoid)
1884a02a946dSIlya Dryomov {
1885a02a946dSIlya Dryomov 	struct rb_node *n = root->rb_node;
1886a02a946dSIlya Dryomov 
1887a02a946dSIlya Dryomov 	while (n) {
1888a02a946dSIlya Dryomov 		struct ceph_osd_backoff *cur =
1889a02a946dSIlya Dryomov 		    rb_entry(n, struct ceph_osd_backoff, spg_node);
1890a02a946dSIlya Dryomov 		int cmp;
1891a02a946dSIlya Dryomov 
1892a02a946dSIlya Dryomov 		cmp = hoid_compare(hoid, cur->begin);
1893a02a946dSIlya Dryomov 		if (cmp < 0) {
1894a02a946dSIlya Dryomov 			n = n->rb_left;
1895a02a946dSIlya Dryomov 		} else if (cmp > 0) {
1896a02a946dSIlya Dryomov 			if (hoid_compare(hoid, cur->end) < 0)
1897a02a946dSIlya Dryomov 				return cur;
1898a02a946dSIlya Dryomov 
1899a02a946dSIlya Dryomov 			n = n->rb_right;
1900a02a946dSIlya Dryomov 		} else {
1901a02a946dSIlya Dryomov 			return cur;
1902a02a946dSIlya Dryomov 		}
1903a02a946dSIlya Dryomov 	}
1904a02a946dSIlya Dryomov 
1905a02a946dSIlya Dryomov 	return NULL;
1906a02a946dSIlya Dryomov }
1907a02a946dSIlya Dryomov 
1908a02a946dSIlya Dryomov /*
1909a02a946dSIlya Dryomov  * Each backoff has a unique id within its OSD session.
1910a02a946dSIlya Dryomov  */
1911a02a946dSIlya Dryomov DEFINE_RB_FUNCS(backoff_by_id, struct ceph_osd_backoff, id, id_node)
1912a02a946dSIlya Dryomov 
1913a02a946dSIlya Dryomov static void clear_backoffs(struct ceph_osd *osd)
1914a02a946dSIlya Dryomov {
1915a02a946dSIlya Dryomov 	while (!RB_EMPTY_ROOT(&osd->o_backoff_mappings)) {
1916a02a946dSIlya Dryomov 		struct ceph_spg_mapping *spg =
1917a02a946dSIlya Dryomov 		    rb_entry(rb_first(&osd->o_backoff_mappings),
1918a02a946dSIlya Dryomov 			     struct ceph_spg_mapping, node);
1919a02a946dSIlya Dryomov 
1920a02a946dSIlya Dryomov 		while (!RB_EMPTY_ROOT(&spg->backoffs)) {
1921a02a946dSIlya Dryomov 			struct ceph_osd_backoff *backoff =
1922a02a946dSIlya Dryomov 			    rb_entry(rb_first(&spg->backoffs),
1923a02a946dSIlya Dryomov 				     struct ceph_osd_backoff, spg_node);
1924a02a946dSIlya Dryomov 
1925a02a946dSIlya Dryomov 			erase_backoff(&spg->backoffs, backoff);
1926a02a946dSIlya Dryomov 			erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
1927a02a946dSIlya Dryomov 			free_backoff(backoff);
1928a02a946dSIlya Dryomov 		}
1929a02a946dSIlya Dryomov 		erase_spg_mapping(&osd->o_backoff_mappings, spg);
1930a02a946dSIlya Dryomov 		free_spg_mapping(spg);
1931a02a946dSIlya Dryomov 	}
1932a02a946dSIlya Dryomov }
1933a02a946dSIlya Dryomov 
1934a02a946dSIlya Dryomov /*
1935a02a946dSIlya Dryomov  * Set up a temporary, non-owning view into @t.
1936a02a946dSIlya Dryomov  */
1937a02a946dSIlya Dryomov static void hoid_fill_from_target(struct ceph_hobject_id *hoid,
1938a02a946dSIlya Dryomov 				  const struct ceph_osd_request_target *t)
1939a02a946dSIlya Dryomov {
1940a02a946dSIlya Dryomov 	hoid->key = NULL;
1941a02a946dSIlya Dryomov 	hoid->key_len = 0;
1942a02a946dSIlya Dryomov 	hoid->oid = t->target_oid.name;
1943a02a946dSIlya Dryomov 	hoid->oid_len = t->target_oid.name_len;
1944a02a946dSIlya Dryomov 	hoid->snapid = CEPH_NOSNAP;
1945a02a946dSIlya Dryomov 	hoid->hash = t->pgid.seed;
1946a02a946dSIlya Dryomov 	hoid->is_max = false;
1947a02a946dSIlya Dryomov 	if (t->target_oloc.pool_ns) {
1948a02a946dSIlya Dryomov 		hoid->nspace = t->target_oloc.pool_ns->str;
1949a02a946dSIlya Dryomov 		hoid->nspace_len = t->target_oloc.pool_ns->len;
1950a02a946dSIlya Dryomov 	} else {
1951a02a946dSIlya Dryomov 		hoid->nspace = NULL;
1952a02a946dSIlya Dryomov 		hoid->nspace_len = 0;
1953a02a946dSIlya Dryomov 	}
1954a02a946dSIlya Dryomov 	hoid->pool = t->target_oloc.pool;
1955a02a946dSIlya Dryomov 	ceph_hoid_build_hash_cache(hoid);
1956a02a946dSIlya Dryomov }
1957a02a946dSIlya Dryomov 
1958a02a946dSIlya Dryomov static bool should_plug_request(struct ceph_osd_request *req)
1959a02a946dSIlya Dryomov {
1960a02a946dSIlya Dryomov 	struct ceph_osd *osd = req->r_osd;
1961a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
1962a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
1963a02a946dSIlya Dryomov 	struct ceph_hobject_id hoid;
1964a02a946dSIlya Dryomov 
1965a02a946dSIlya Dryomov 	spg = lookup_spg_mapping(&osd->o_backoff_mappings, &req->r_t.spgid);
1966a02a946dSIlya Dryomov 	if (!spg)
1967a02a946dSIlya Dryomov 		return false;
1968a02a946dSIlya Dryomov 
1969a02a946dSIlya Dryomov 	hoid_fill_from_target(&hoid, &req->r_t);
1970a02a946dSIlya Dryomov 	backoff = lookup_containing_backoff(&spg->backoffs, &hoid);
1971a02a946dSIlya Dryomov 	if (!backoff)
1972a02a946dSIlya Dryomov 		return false;
1973a02a946dSIlya Dryomov 
1974a02a946dSIlya Dryomov 	dout("%s req %p tid %llu backoff osd%d spgid %llu.%xs%d id %llu\n",
1975a02a946dSIlya Dryomov 	     __func__, req, req->r_tid, osd->o_osd, backoff->spgid.pgid.pool,
1976a02a946dSIlya Dryomov 	     backoff->spgid.pgid.seed, backoff->spgid.shard, backoff->id);
1977a02a946dSIlya Dryomov 	return true;
1978a02a946dSIlya Dryomov }
1979a02a946dSIlya Dryomov 
19800d9c1ab3SIlya Dryomov /*
19810d9c1ab3SIlya Dryomov  * Keep get_num_data_items() in sync with this function.
19820d9c1ab3SIlya Dryomov  */
198398c4bfe9SIlya Dryomov static void setup_request_data(struct ceph_osd_request *req)
19843d14c5d2SYehuda Sadeh {
198598c4bfe9SIlya Dryomov 	struct ceph_msg *request_msg = req->r_request;
198698c4bfe9SIlya Dryomov 	struct ceph_msg *reply_msg = req->r_reply;
198798c4bfe9SIlya Dryomov 	struct ceph_osd_req_op *op;
19883d14c5d2SYehuda Sadeh 
198998c4bfe9SIlya Dryomov 	if (req->r_request->num_data_items || req->r_reply->num_data_items)
1990bb873b53SIlya Dryomov 		return;
19913d14c5d2SYehuda Sadeh 
199298c4bfe9SIlya Dryomov 	WARN_ON(request_msg->data_length || reply_msg->data_length);
199398c4bfe9SIlya Dryomov 	for (op = req->r_ops; op != &req->r_ops[req->r_num_ops]; op++) {
1994bb873b53SIlya Dryomov 		switch (op->op) {
1995bb873b53SIlya Dryomov 		/* request */
1996bb873b53SIlya Dryomov 		case CEPH_OSD_OP_WRITE:
1997bb873b53SIlya Dryomov 		case CEPH_OSD_OP_WRITEFULL:
1998bb873b53SIlya Dryomov 			WARN_ON(op->indata_len != op->extent.length);
199998c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(request_msg,
200098c4bfe9SIlya Dryomov 					       &op->extent.osd_data);
2001bb873b53SIlya Dryomov 			break;
2002bb873b53SIlya Dryomov 		case CEPH_OSD_OP_SETXATTR:
2003bb873b53SIlya Dryomov 		case CEPH_OSD_OP_CMPXATTR:
2004bb873b53SIlya Dryomov 			WARN_ON(op->indata_len != op->xattr.name_len +
2005bb873b53SIlya Dryomov 						  op->xattr.value_len);
200698c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(request_msg,
200798c4bfe9SIlya Dryomov 					       &op->xattr.osd_data);
2008bb873b53SIlya Dryomov 			break;
2009922dab61SIlya Dryomov 		case CEPH_OSD_OP_NOTIFY_ACK:
201098c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(request_msg,
2011922dab61SIlya Dryomov 					       &op->notify_ack.request_data);
2012922dab61SIlya Dryomov 			break;
201378beb0ffSLuis Henriques 		case CEPH_OSD_OP_COPY_FROM2:
201423ddf9beSLuis Henriques 			ceph_osdc_msg_data_add(request_msg,
201523ddf9beSLuis Henriques 					       &op->copy_from.osd_data);
201623ddf9beSLuis Henriques 			break;
2017bb873b53SIlya Dryomov 
2018bb873b53SIlya Dryomov 		/* reply */
2019bb873b53SIlya Dryomov 		case CEPH_OSD_OP_STAT:
202098c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(reply_msg,
2021bb873b53SIlya Dryomov 					       &op->raw_data_in);
2022bb873b53SIlya Dryomov 			break;
2023bb873b53SIlya Dryomov 		case CEPH_OSD_OP_READ:
202498c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(reply_msg,
2025bb873b53SIlya Dryomov 					       &op->extent.osd_data);
2026bb873b53SIlya Dryomov 			break;
2027a4ed38d7SDouglas Fuller 		case CEPH_OSD_OP_LIST_WATCHERS:
202898c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(reply_msg,
2029a4ed38d7SDouglas Fuller 					       &op->list_watchers.response_data);
2030a4ed38d7SDouglas Fuller 			break;
2031bb873b53SIlya Dryomov 
2032bb873b53SIlya Dryomov 		/* both */
2033bb873b53SIlya Dryomov 		case CEPH_OSD_OP_CALL:
2034bb873b53SIlya Dryomov 			WARN_ON(op->indata_len != op->cls.class_len +
2035bb873b53SIlya Dryomov 						  op->cls.method_len +
2036bb873b53SIlya Dryomov 						  op->cls.indata_len);
203798c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(request_msg,
203898c4bfe9SIlya Dryomov 					       &op->cls.request_info);
2039bb873b53SIlya Dryomov 			/* optional, can be NONE */
204098c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(request_msg,
204198c4bfe9SIlya Dryomov 					       &op->cls.request_data);
2042bb873b53SIlya Dryomov 			/* optional, can be NONE */
204398c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(reply_msg,
2044bb873b53SIlya Dryomov 					       &op->cls.response_data);
2045bb873b53SIlya Dryomov 			break;
204619079203SIlya Dryomov 		case CEPH_OSD_OP_NOTIFY:
204798c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(request_msg,
204819079203SIlya Dryomov 					       &op->notify.request_data);
204998c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(reply_msg,
205019079203SIlya Dryomov 					       &op->notify.response_data);
205119079203SIlya Dryomov 			break;
2052bb873b53SIlya Dryomov 		}
2053bb873b53SIlya Dryomov 	}
2054bb873b53SIlya Dryomov }
2055bb873b53SIlya Dryomov 
20562e59ffd1SIlya Dryomov static void encode_pgid(void **p, const struct ceph_pg *pgid)
20572e59ffd1SIlya Dryomov {
20582e59ffd1SIlya Dryomov 	ceph_encode_8(p, 1);
20592e59ffd1SIlya Dryomov 	ceph_encode_64(p, pgid->pool);
20602e59ffd1SIlya Dryomov 	ceph_encode_32(p, pgid->seed);
20612e59ffd1SIlya Dryomov 	ceph_encode_32(p, -1); /* preferred */
20622e59ffd1SIlya Dryomov }
20632e59ffd1SIlya Dryomov 
20648cb441c0SIlya Dryomov static void encode_spgid(void **p, const struct ceph_spg *spgid)
20658cb441c0SIlya Dryomov {
20668cb441c0SIlya Dryomov 	ceph_start_encoding(p, 1, 1, CEPH_PGID_ENCODING_LEN + 1);
20678cb441c0SIlya Dryomov 	encode_pgid(p, &spgid->pgid);
20688cb441c0SIlya Dryomov 	ceph_encode_8(p, spgid->shard);
20698cb441c0SIlya Dryomov }
20708cb441c0SIlya Dryomov 
20712e59ffd1SIlya Dryomov static void encode_oloc(void **p, void *end,
20722e59ffd1SIlya Dryomov 			const struct ceph_object_locator *oloc)
20732e59ffd1SIlya Dryomov {
20742e59ffd1SIlya Dryomov 	ceph_start_encoding(p, 5, 4, ceph_oloc_encoding_size(oloc));
20752e59ffd1SIlya Dryomov 	ceph_encode_64(p, oloc->pool);
20762e59ffd1SIlya Dryomov 	ceph_encode_32(p, -1); /* preferred */
20772e59ffd1SIlya Dryomov 	ceph_encode_32(p, 0);  /* key len */
20782e59ffd1SIlya Dryomov 	if (oloc->pool_ns)
20792e59ffd1SIlya Dryomov 		ceph_encode_string(p, end, oloc->pool_ns->str,
20802e59ffd1SIlya Dryomov 				   oloc->pool_ns->len);
20812e59ffd1SIlya Dryomov 	else
20822e59ffd1SIlya Dryomov 		ceph_encode_32(p, 0);
20832e59ffd1SIlya Dryomov }
20842e59ffd1SIlya Dryomov 
20858cb441c0SIlya Dryomov static void encode_request_partial(struct ceph_osd_request *req,
20868cb441c0SIlya Dryomov 				   struct ceph_msg *msg)
2087bb873b53SIlya Dryomov {
2088bb873b53SIlya Dryomov 	void *p = msg->front.iov_base;
2089bb873b53SIlya Dryomov 	void *const end = p + msg->front_alloc_len;
2090bb873b53SIlya Dryomov 	u32 data_len = 0;
2091bb873b53SIlya Dryomov 	int i;
2092bb873b53SIlya Dryomov 
2093bb873b53SIlya Dryomov 	if (req->r_flags & CEPH_OSD_FLAG_WRITE) {
2094bb873b53SIlya Dryomov 		/* snapshots aren't writeable */
2095bb873b53SIlya Dryomov 		WARN_ON(req->r_snapid != CEPH_NOSNAP);
2096bb873b53SIlya Dryomov 	} else {
2097bb873b53SIlya Dryomov 		WARN_ON(req->r_mtime.tv_sec || req->r_mtime.tv_nsec ||
2098bb873b53SIlya Dryomov 			req->r_data_offset || req->r_snapc);
2099bb873b53SIlya Dryomov 	}
2100bb873b53SIlya Dryomov 
210198c4bfe9SIlya Dryomov 	setup_request_data(req);
2102bb873b53SIlya Dryomov 
21038cb441c0SIlya Dryomov 	encode_spgid(&p, &req->r_t.spgid); /* actual spg */
21048cb441c0SIlya Dryomov 	ceph_encode_32(&p, req->r_t.pgid.seed); /* raw hash */
2105bb873b53SIlya Dryomov 	ceph_encode_32(&p, req->r_osdc->osdmap->epoch);
2106bb873b53SIlya Dryomov 	ceph_encode_32(&p, req->r_flags);
21078cb441c0SIlya Dryomov 
21088cb441c0SIlya Dryomov 	/* reqid */
21098cb441c0SIlya Dryomov 	ceph_start_encoding(&p, 2, 2, sizeof(struct ceph_osd_reqid));
21108cb441c0SIlya Dryomov 	memset(p, 0, sizeof(struct ceph_osd_reqid));
21118cb441c0SIlya Dryomov 	p += sizeof(struct ceph_osd_reqid);
21128cb441c0SIlya Dryomov 
21138cb441c0SIlya Dryomov 	/* trace */
21148cb441c0SIlya Dryomov 	memset(p, 0, sizeof(struct ceph_blkin_trace_info));
21158cb441c0SIlya Dryomov 	p += sizeof(struct ceph_blkin_trace_info);
21168cb441c0SIlya Dryomov 
21178cb441c0SIlya Dryomov 	ceph_encode_32(&p, 0); /* client_inc, always 0 */
2118fac02ddfSArnd Bergmann 	ceph_encode_timespec64(p, &req->r_mtime);
2119bb873b53SIlya Dryomov 	p += sizeof(struct ceph_timespec);
2120aa26d662SJeff Layton 
21212e59ffd1SIlya Dryomov 	encode_oloc(&p, end, &req->r_t.target_oloc);
21222e59ffd1SIlya Dryomov 	ceph_encode_string(&p, end, req->r_t.target_oid.name,
21232e59ffd1SIlya Dryomov 			   req->r_t.target_oid.name_len);
2124bb873b53SIlya Dryomov 
2125bb873b53SIlya Dryomov 	/* ops, can imply data */
2126bb873b53SIlya Dryomov 	ceph_encode_16(&p, req->r_num_ops);
2127bb873b53SIlya Dryomov 	for (i = 0; i < req->r_num_ops; i++) {
2128bb873b53SIlya Dryomov 		data_len += osd_req_encode_op(p, &req->r_ops[i]);
2129bb873b53SIlya Dryomov 		p += sizeof(struct ceph_osd_op);
2130bb873b53SIlya Dryomov 	}
2131bb873b53SIlya Dryomov 
2132bb873b53SIlya Dryomov 	ceph_encode_64(&p, req->r_snapid); /* snapid */
2133bb873b53SIlya Dryomov 	if (req->r_snapc) {
2134bb873b53SIlya Dryomov 		ceph_encode_64(&p, req->r_snapc->seq);
2135bb873b53SIlya Dryomov 		ceph_encode_32(&p, req->r_snapc->num_snaps);
2136bb873b53SIlya Dryomov 		for (i = 0; i < req->r_snapc->num_snaps; i++)
2137bb873b53SIlya Dryomov 			ceph_encode_64(&p, req->r_snapc->snaps[i]);
2138bb873b53SIlya Dryomov 	} else {
2139bb873b53SIlya Dryomov 		ceph_encode_64(&p, 0); /* snap_seq */
2140bb873b53SIlya Dryomov 		ceph_encode_32(&p, 0); /* snaps len */
2141bb873b53SIlya Dryomov 	}
2142bb873b53SIlya Dryomov 
2143bb873b53SIlya Dryomov 	ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
2144986e8989SIlya Dryomov 	BUG_ON(p > end - 8); /* space for features */
2145bb873b53SIlya Dryomov 
21468cb441c0SIlya Dryomov 	msg->hdr.version = cpu_to_le16(8); /* MOSDOp v8 */
21478cb441c0SIlya Dryomov 	/* front_len is finalized in encode_request_finish() */
2148986e8989SIlya Dryomov 	msg->front.iov_len = p - msg->front.iov_base;
2149986e8989SIlya Dryomov 	msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2150bb873b53SIlya Dryomov 	msg->hdr.data_len = cpu_to_le32(data_len);
2151bb873b53SIlya Dryomov 	/*
2152bb873b53SIlya Dryomov 	 * The header "data_off" is a hint to the receiver allowing it
2153bb873b53SIlya Dryomov 	 * to align received data into its buffers such that there's no
2154bb873b53SIlya Dryomov 	 * need to re-copy it before writing it to disk (direct I/O).
2155bb873b53SIlya Dryomov 	 */
2156bb873b53SIlya Dryomov 	msg->hdr.data_off = cpu_to_le16(req->r_data_offset);
2157bb873b53SIlya Dryomov 
21588cb441c0SIlya Dryomov 	dout("%s req %p msg %p oid %s oid_len %d\n", __func__, req, msg,
21598cb441c0SIlya Dryomov 	     req->r_t.target_oid.name, req->r_t.target_oid.name_len);
21608cb441c0SIlya Dryomov }
21618cb441c0SIlya Dryomov 
21628cb441c0SIlya Dryomov static void encode_request_finish(struct ceph_msg *msg)
21638cb441c0SIlya Dryomov {
21648cb441c0SIlya Dryomov 	void *p = msg->front.iov_base;
2165986e8989SIlya Dryomov 	void *const partial_end = p + msg->front.iov_len;
21668cb441c0SIlya Dryomov 	void *const end = p + msg->front_alloc_len;
21678cb441c0SIlya Dryomov 
21688cb441c0SIlya Dryomov 	if (CEPH_HAVE_FEATURE(msg->con->peer_features, RESEND_ON_SPLIT)) {
21698cb441c0SIlya Dryomov 		/* luminous OSD -- encode features and be done */
2170986e8989SIlya Dryomov 		p = partial_end;
21718cb441c0SIlya Dryomov 		ceph_encode_64(&p, msg->con->peer_features);
21728cb441c0SIlya Dryomov 	} else {
21738cb441c0SIlya Dryomov 		struct {
21748cb441c0SIlya Dryomov 			char spgid[CEPH_ENCODING_START_BLK_LEN +
21758cb441c0SIlya Dryomov 				   CEPH_PGID_ENCODING_LEN + 1];
21768cb441c0SIlya Dryomov 			__le32 hash;
21778cb441c0SIlya Dryomov 			__le32 epoch;
21788cb441c0SIlya Dryomov 			__le32 flags;
21798cb441c0SIlya Dryomov 			char reqid[CEPH_ENCODING_START_BLK_LEN +
21808cb441c0SIlya Dryomov 				   sizeof(struct ceph_osd_reqid)];
21818cb441c0SIlya Dryomov 			char trace[sizeof(struct ceph_blkin_trace_info)];
21828cb441c0SIlya Dryomov 			__le32 client_inc;
21838cb441c0SIlya Dryomov 			struct ceph_timespec mtime;
21848cb441c0SIlya Dryomov 		} __packed head;
21858cb441c0SIlya Dryomov 		struct ceph_pg pgid;
21868cb441c0SIlya Dryomov 		void *oloc, *oid, *tail;
21878cb441c0SIlya Dryomov 		int oloc_len, oid_len, tail_len;
21888cb441c0SIlya Dryomov 		int len;
21898cb441c0SIlya Dryomov 
21908cb441c0SIlya Dryomov 		/*
21918cb441c0SIlya Dryomov 		 * Pre-luminous OSD -- reencode v8 into v4 using @head
21928cb441c0SIlya Dryomov 		 * as a temporary buffer.  Encode the raw PG; the rest
21938cb441c0SIlya Dryomov 		 * is just a matter of moving oloc, oid and tail blobs
21948cb441c0SIlya Dryomov 		 * around.
21958cb441c0SIlya Dryomov 		 */
21968cb441c0SIlya Dryomov 		memcpy(&head, p, sizeof(head));
21978cb441c0SIlya Dryomov 		p += sizeof(head);
21988cb441c0SIlya Dryomov 
21998cb441c0SIlya Dryomov 		oloc = p;
22008cb441c0SIlya Dryomov 		p += CEPH_ENCODING_START_BLK_LEN;
22018cb441c0SIlya Dryomov 		pgid.pool = ceph_decode_64(&p);
22028cb441c0SIlya Dryomov 		p += 4 + 4; /* preferred, key len */
22038cb441c0SIlya Dryomov 		len = ceph_decode_32(&p);
22048cb441c0SIlya Dryomov 		p += len;   /* nspace */
22058cb441c0SIlya Dryomov 		oloc_len = p - oloc;
22068cb441c0SIlya Dryomov 
22078cb441c0SIlya Dryomov 		oid = p;
22088cb441c0SIlya Dryomov 		len = ceph_decode_32(&p);
22098cb441c0SIlya Dryomov 		p += len;
22108cb441c0SIlya Dryomov 		oid_len = p - oid;
22118cb441c0SIlya Dryomov 
22128cb441c0SIlya Dryomov 		tail = p;
2213986e8989SIlya Dryomov 		tail_len = partial_end - p;
22148cb441c0SIlya Dryomov 
22158cb441c0SIlya Dryomov 		p = msg->front.iov_base;
22168cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.client_inc, sizeof(head.client_inc));
22178cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.epoch, sizeof(head.epoch));
22188cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.flags, sizeof(head.flags));
22198cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.mtime, sizeof(head.mtime));
22208cb441c0SIlya Dryomov 
22218cb441c0SIlya Dryomov 		/* reassert_version */
22228cb441c0SIlya Dryomov 		memset(p, 0, sizeof(struct ceph_eversion));
22238cb441c0SIlya Dryomov 		p += sizeof(struct ceph_eversion);
22248cb441c0SIlya Dryomov 
22258cb441c0SIlya Dryomov 		BUG_ON(p >= oloc);
22268cb441c0SIlya Dryomov 		memmove(p, oloc, oloc_len);
22278cb441c0SIlya Dryomov 		p += oloc_len;
22288cb441c0SIlya Dryomov 
22298cb441c0SIlya Dryomov 		pgid.seed = le32_to_cpu(head.hash);
22308cb441c0SIlya Dryomov 		encode_pgid(&p, &pgid); /* raw pg */
22318cb441c0SIlya Dryomov 
22328cb441c0SIlya Dryomov 		BUG_ON(p >= oid);
22338cb441c0SIlya Dryomov 		memmove(p, oid, oid_len);
22348cb441c0SIlya Dryomov 		p += oid_len;
22358cb441c0SIlya Dryomov 
22368cb441c0SIlya Dryomov 		/* tail -- ops, snapid, snapc, retry_attempt */
22378cb441c0SIlya Dryomov 		BUG_ON(p >= tail);
22388cb441c0SIlya Dryomov 		memmove(p, tail, tail_len);
22398cb441c0SIlya Dryomov 		p += tail_len;
22408cb441c0SIlya Dryomov 
22418cb441c0SIlya Dryomov 		msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
22428cb441c0SIlya Dryomov 	}
22438cb441c0SIlya Dryomov 
22448cb441c0SIlya Dryomov 	BUG_ON(p > end);
22458cb441c0SIlya Dryomov 	msg->front.iov_len = p - msg->front.iov_base;
22468cb441c0SIlya Dryomov 	msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
22478cb441c0SIlya Dryomov 
22488cb441c0SIlya Dryomov 	dout("%s msg %p tid %llu %u+%u+%u v%d\n", __func__, msg,
22498cb441c0SIlya Dryomov 	     le64_to_cpu(msg->hdr.tid), le32_to_cpu(msg->hdr.front_len),
22508cb441c0SIlya Dryomov 	     le32_to_cpu(msg->hdr.middle_len), le32_to_cpu(msg->hdr.data_len),
22518cb441c0SIlya Dryomov 	     le16_to_cpu(msg->hdr.version));
2252bb873b53SIlya Dryomov }
2253bb873b53SIlya Dryomov 
2254bb873b53SIlya Dryomov /*
2255bb873b53SIlya Dryomov  * @req has to be assigned a tid and registered.
2256bb873b53SIlya Dryomov  */
2257bb873b53SIlya Dryomov static void send_request(struct ceph_osd_request *req)
2258bb873b53SIlya Dryomov {
2259bb873b53SIlya Dryomov 	struct ceph_osd *osd = req->r_osd;
2260bb873b53SIlya Dryomov 
22615aea3dcdSIlya Dryomov 	verify_osd_locked(osd);
2262bb873b53SIlya Dryomov 	WARN_ON(osd->o_osd != req->r_t.osd);
2263bb873b53SIlya Dryomov 
2264a02a946dSIlya Dryomov 	/* backoff? */
2265a02a946dSIlya Dryomov 	if (should_plug_request(req))
2266a02a946dSIlya Dryomov 		return;
2267a02a946dSIlya Dryomov 
22685aea3dcdSIlya Dryomov 	/*
22695aea3dcdSIlya Dryomov 	 * We may have a previously queued request message hanging
22705aea3dcdSIlya Dryomov 	 * around.  Cancel it to avoid corrupting the msgr.
22715aea3dcdSIlya Dryomov 	 */
22725aea3dcdSIlya Dryomov 	if (req->r_sent)
22735aea3dcdSIlya Dryomov 		ceph_msg_revoke(req->r_request);
22745aea3dcdSIlya Dryomov 
2275bb873b53SIlya Dryomov 	req->r_flags |= CEPH_OSD_FLAG_KNOWN_REDIR;
2276bb873b53SIlya Dryomov 	if (req->r_attempts)
2277bb873b53SIlya Dryomov 		req->r_flags |= CEPH_OSD_FLAG_RETRY;
2278bb873b53SIlya Dryomov 	else
2279bb873b53SIlya Dryomov 		WARN_ON(req->r_flags & CEPH_OSD_FLAG_RETRY);
2280bb873b53SIlya Dryomov 
22818cb441c0SIlya Dryomov 	encode_request_partial(req, req->r_request);
2282bb873b53SIlya Dryomov 
228304c7d789SIlya 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",
2284bb873b53SIlya Dryomov 	     __func__, req, req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed,
2285dc98ff72SIlya Dryomov 	     req->r_t.spgid.pgid.pool, req->r_t.spgid.pgid.seed,
228604c7d789SIlya Dryomov 	     req->r_t.spgid.shard, osd->o_osd, req->r_t.epoch, req->r_flags,
228704c7d789SIlya Dryomov 	     req->r_attempts);
2288bb873b53SIlya Dryomov 
2289bb873b53SIlya Dryomov 	req->r_t.paused = false;
22903d14c5d2SYehuda Sadeh 	req->r_stamp = jiffies;
2291bb873b53SIlya Dryomov 	req->r_attempts++;
22923d14c5d2SYehuda Sadeh 
2293bb873b53SIlya Dryomov 	req->r_sent = osd->o_incarnation;
2294bb873b53SIlya Dryomov 	req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
2295bb873b53SIlya Dryomov 	ceph_con_send(&osd->o_con, ceph_msg_get(req->r_request));
22963d14c5d2SYehuda Sadeh }
22973d14c5d2SYehuda Sadeh 
229842c1b124SIlya Dryomov static void maybe_request_map(struct ceph_osd_client *osdc)
229942c1b124SIlya Dryomov {
230042c1b124SIlya Dryomov 	bool continuous = false;
230142c1b124SIlya Dryomov 
23025aea3dcdSIlya Dryomov 	verify_osdc_locked(osdc);
230342c1b124SIlya Dryomov 	WARN_ON(!osdc->osdmap->epoch);
230442c1b124SIlya Dryomov 
2305b7ec35b3SIlya Dryomov 	if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2306b7ec35b3SIlya Dryomov 	    ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD) ||
2307b7ec35b3SIlya Dryomov 	    ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
230842c1b124SIlya Dryomov 		dout("%s osdc %p continuous\n", __func__, osdc);
230942c1b124SIlya Dryomov 		continuous = true;
231042c1b124SIlya Dryomov 	} else {
231142c1b124SIlya Dryomov 		dout("%s osdc %p onetime\n", __func__, osdc);
231242c1b124SIlya Dryomov 	}
231342c1b124SIlya Dryomov 
231442c1b124SIlya Dryomov 	if (ceph_monc_want_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
231542c1b124SIlya Dryomov 			       osdc->osdmap->epoch + 1, continuous))
231642c1b124SIlya Dryomov 		ceph_monc_renew_subs(&osdc->client->monc);
231742c1b124SIlya Dryomov }
231842c1b124SIlya Dryomov 
2319a1f4020aSJeff Layton static void complete_request(struct ceph_osd_request *req, int err);
23204609245eSIlya Dryomov static void send_map_check(struct ceph_osd_request *req);
23214609245eSIlya Dryomov 
23225aea3dcdSIlya Dryomov static void __submit_request(struct ceph_osd_request *req, bool wrlocked)
23230bbfdfe8SIlya Dryomov {
23245aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
23255aea3dcdSIlya Dryomov 	struct ceph_osd *osd;
23264609245eSIlya Dryomov 	enum calc_target_result ct_res;
232766850df5SIlya Dryomov 	int err = 0;
23285aea3dcdSIlya Dryomov 	bool need_send = false;
23295aea3dcdSIlya Dryomov 	bool promoted = false;
23300bbfdfe8SIlya Dryomov 
2331b18b9550SIlya Dryomov 	WARN_ON(req->r_tid);
23325aea3dcdSIlya Dryomov 	dout("%s req %p wrlocked %d\n", __func__, req, wrlocked);
23335aea3dcdSIlya Dryomov 
23345aea3dcdSIlya Dryomov again:
23358edf84baSIlya Dryomov 	ct_res = calc_target(osdc, &req->r_t, false);
23364609245eSIlya Dryomov 	if (ct_res == CALC_TARGET_POOL_DNE && !wrlocked)
23374609245eSIlya Dryomov 		goto promote;
23384609245eSIlya Dryomov 
23395aea3dcdSIlya Dryomov 	osd = lookup_create_osd(osdc, req->r_t.osd, wrlocked);
23405aea3dcdSIlya Dryomov 	if (IS_ERR(osd)) {
23415aea3dcdSIlya Dryomov 		WARN_ON(PTR_ERR(osd) != -EAGAIN || wrlocked);
23425aea3dcdSIlya Dryomov 		goto promote;
23435aea3dcdSIlya Dryomov 	}
23445aea3dcdSIlya Dryomov 
234566850df5SIlya Dryomov 	if (osdc->abort_err) {
234666850df5SIlya Dryomov 		dout("req %p abort_err %d\n", req, osdc->abort_err);
234766850df5SIlya Dryomov 		err = osdc->abort_err;
234866850df5SIlya Dryomov 	} else if (osdc->osdmap->epoch < osdc->epoch_barrier) {
234958eb7932SJeff Layton 		dout("req %p epoch %u barrier %u\n", req, osdc->osdmap->epoch,
235058eb7932SJeff Layton 		     osdc->epoch_barrier);
235158eb7932SJeff Layton 		req->r_t.paused = true;
235258eb7932SJeff Layton 		maybe_request_map(osdc);
235358eb7932SJeff Layton 	} else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2354b7ec35b3SIlya Dryomov 		   ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
23555aea3dcdSIlya Dryomov 		dout("req %p pausewr\n", req);
23565aea3dcdSIlya Dryomov 		req->r_t.paused = true;
23575aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
23585aea3dcdSIlya Dryomov 	} else if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
2359b7ec35b3SIlya Dryomov 		   ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
23605aea3dcdSIlya Dryomov 		dout("req %p pauserd\n", req);
23615aea3dcdSIlya Dryomov 		req->r_t.paused = true;
23625aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
23635aea3dcdSIlya Dryomov 	} else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
23645aea3dcdSIlya Dryomov 		   !(req->r_flags & (CEPH_OSD_FLAG_FULL_TRY |
23655aea3dcdSIlya Dryomov 				     CEPH_OSD_FLAG_FULL_FORCE)) &&
2366b7ec35b3SIlya Dryomov 		   (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
23675aea3dcdSIlya Dryomov 		    pool_full(osdc, req->r_t.base_oloc.pool))) {
23685aea3dcdSIlya Dryomov 		dout("req %p full/pool_full\n", req);
236902b2f549SDongsheng Yang 		if (ceph_test_opt(osdc->client, ABORT_ON_FULL)) {
237029e87820SIlya Dryomov 			err = -ENOSPC;
237129e87820SIlya Dryomov 		} else {
2372dc9b0dc4SIlya Dryomov 			if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL))
2373dc9b0dc4SIlya Dryomov 				pr_warn_ratelimited("cluster is full (osdmap FULL)\n");
2374dc9b0dc4SIlya Dryomov 			else
2375dc9b0dc4SIlya Dryomov 				pr_warn_ratelimited("pool %lld is full or reached quota\n",
2376dc9b0dc4SIlya Dryomov 						    req->r_t.base_oloc.pool);
23775aea3dcdSIlya Dryomov 			req->r_t.paused = true;
23785aea3dcdSIlya Dryomov 			maybe_request_map(osdc);
237929e87820SIlya Dryomov 		}
23805aea3dcdSIlya Dryomov 	} else if (!osd_homeless(osd)) {
23815aea3dcdSIlya Dryomov 		need_send = true;
23820bbfdfe8SIlya Dryomov 	} else {
23835aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
23840bbfdfe8SIlya Dryomov 	}
23850bbfdfe8SIlya Dryomov 
23865aea3dcdSIlya Dryomov 	mutex_lock(&osd->lock);
23875aea3dcdSIlya Dryomov 	/*
23885aea3dcdSIlya Dryomov 	 * Assign the tid atomically with send_request() to protect
23895aea3dcdSIlya Dryomov 	 * multiple writes to the same object from racing with each
23905aea3dcdSIlya Dryomov 	 * other, resulting in out of order ops on the OSDs.
23915aea3dcdSIlya Dryomov 	 */
23925aea3dcdSIlya Dryomov 	req->r_tid = atomic64_inc_return(&osdc->last_tid);
23935aea3dcdSIlya Dryomov 	link_request(osd, req);
23945aea3dcdSIlya Dryomov 	if (need_send)
23955aea3dcdSIlya Dryomov 		send_request(req);
239666850df5SIlya Dryomov 	else if (err)
239766850df5SIlya Dryomov 		complete_request(req, err);
23985aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
23995aea3dcdSIlya Dryomov 
24006001567cSIlya Dryomov 	if (!err && ct_res == CALC_TARGET_POOL_DNE)
24014609245eSIlya Dryomov 		send_map_check(req);
24024609245eSIlya Dryomov 
24035aea3dcdSIlya Dryomov 	if (promoted)
24045aea3dcdSIlya Dryomov 		downgrade_write(&osdc->lock);
24055aea3dcdSIlya Dryomov 	return;
24065aea3dcdSIlya Dryomov 
24075aea3dcdSIlya Dryomov promote:
24085aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
24095aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
24105aea3dcdSIlya Dryomov 	wrlocked = true;
24115aea3dcdSIlya Dryomov 	promoted = true;
24125aea3dcdSIlya Dryomov 	goto again;
24130bbfdfe8SIlya Dryomov }
24140bbfdfe8SIlya Dryomov 
24155aea3dcdSIlya Dryomov static void account_request(struct ceph_osd_request *req)
24165aea3dcdSIlya Dryomov {
241754ea0046SIlya Dryomov 	WARN_ON(req->r_flags & (CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK));
2418b18b9550SIlya Dryomov 	WARN_ON(!(req->r_flags & (CEPH_OSD_FLAG_READ | CEPH_OSD_FLAG_WRITE)));
24195aea3dcdSIlya Dryomov 
2420b18b9550SIlya Dryomov 	req->r_flags |= CEPH_OSD_FLAG_ONDISK;
242122d2cfdfSIlya Dryomov 	atomic_inc(&req->r_osdc->num_requests);
24227cc5e38fSIlya Dryomov 
24237cc5e38fSIlya Dryomov 	req->r_start_stamp = jiffies;
242497e27aaaSXiubo Li 	req->r_start_latency = ktime_get();
24255aea3dcdSIlya Dryomov }
24265aea3dcdSIlya Dryomov 
24275aea3dcdSIlya Dryomov static void submit_request(struct ceph_osd_request *req, bool wrlocked)
24285aea3dcdSIlya Dryomov {
24295aea3dcdSIlya Dryomov 	ceph_osdc_get_request(req);
24305aea3dcdSIlya Dryomov 	account_request(req);
24315aea3dcdSIlya Dryomov 	__submit_request(req, wrlocked);
24325aea3dcdSIlya Dryomov }
24335aea3dcdSIlya Dryomov 
243445ee2c1dSIlya Dryomov static void finish_request(struct ceph_osd_request *req)
24355aea3dcdSIlya Dryomov {
24365aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
24375aea3dcdSIlya Dryomov 
24384609245eSIlya Dryomov 	WARN_ON(lookup_request_mc(&osdc->map_checks, req->r_tid));
243904c7d789SIlya Dryomov 	dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
244004c7d789SIlya Dryomov 
244197e27aaaSXiubo Li 	req->r_end_latency = ktime_get();
244297e27aaaSXiubo Li 
244304c7d789SIlya Dryomov 	if (req->r_osd)
244404c7d789SIlya Dryomov 		unlink_request(req->r_osd, req);
24455aea3dcdSIlya Dryomov 	atomic_dec(&osdc->num_requests);
24465aea3dcdSIlya Dryomov 
24475aea3dcdSIlya Dryomov 	/*
24485aea3dcdSIlya Dryomov 	 * If an OSD has failed or returned and a request has been sent
24495aea3dcdSIlya Dryomov 	 * twice, it's possible to get a reply and end up here while the
24505aea3dcdSIlya Dryomov 	 * request message is queued for delivery.  We will ignore the
24515aea3dcdSIlya Dryomov 	 * reply, so not a big deal, but better to try and catch it.
24525aea3dcdSIlya Dryomov 	 */
24535aea3dcdSIlya Dryomov 	ceph_msg_revoke(req->r_request);
24545aea3dcdSIlya Dryomov 	ceph_msg_revoke_incoming(req->r_reply);
24555aea3dcdSIlya Dryomov }
24565aea3dcdSIlya Dryomov 
2457fe5da05eSIlya Dryomov static void __complete_request(struct ceph_osd_request *req)
2458fe5da05eSIlya Dryomov {
2459d75f773cSSakari Ailus 	dout("%s req %p tid %llu cb %ps result %d\n", __func__, req,
2460b18b9550SIlya Dryomov 	     req->r_tid, req->r_callback, req->r_result);
246126df726bSIlya Dryomov 
246226df726bSIlya Dryomov 	if (req->r_callback)
2463fe5da05eSIlya Dryomov 		req->r_callback(req);
246426df726bSIlya Dryomov 	complete_all(&req->r_completion);
246526df726bSIlya Dryomov 	ceph_osdc_put_request(req);
2466b18b9550SIlya Dryomov }
2467fe5da05eSIlya Dryomov 
246888bc1922SIlya Dryomov static void complete_request_workfn(struct work_struct *work)
246988bc1922SIlya Dryomov {
247088bc1922SIlya Dryomov 	struct ceph_osd_request *req =
247188bc1922SIlya Dryomov 	    container_of(work, struct ceph_osd_request, r_complete_work);
247288bc1922SIlya Dryomov 
247388bc1922SIlya Dryomov 	__complete_request(req);
24740bbfdfe8SIlya Dryomov }
24750bbfdfe8SIlya Dryomov 
24764609245eSIlya Dryomov /*
2477b18b9550SIlya Dryomov  * This is open-coded in handle_reply().
24784609245eSIlya Dryomov  */
24794609245eSIlya Dryomov static void complete_request(struct ceph_osd_request *req, int err)
24804609245eSIlya Dryomov {
24814609245eSIlya Dryomov 	dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
24824609245eSIlya Dryomov 
24834609245eSIlya Dryomov 	req->r_result = err;
248445ee2c1dSIlya Dryomov 	finish_request(req);
248588bc1922SIlya Dryomov 
248688bc1922SIlya Dryomov 	INIT_WORK(&req->r_complete_work, complete_request_workfn);
248788bc1922SIlya Dryomov 	queue_work(req->r_osdc->completion_wq, &req->r_complete_work);
24884609245eSIlya Dryomov }
24894609245eSIlya Dryomov 
24904609245eSIlya Dryomov static void cancel_map_check(struct ceph_osd_request *req)
24914609245eSIlya Dryomov {
24924609245eSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
24934609245eSIlya Dryomov 	struct ceph_osd_request *lookup_req;
24944609245eSIlya Dryomov 
24954609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
24964609245eSIlya Dryomov 
24974609245eSIlya Dryomov 	lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
24984609245eSIlya Dryomov 	if (!lookup_req)
24994609245eSIlya Dryomov 		return;
25004609245eSIlya Dryomov 
25014609245eSIlya Dryomov 	WARN_ON(lookup_req != req);
25024609245eSIlya Dryomov 	erase_request_mc(&osdc->map_checks, req);
25034609245eSIlya Dryomov 	ceph_osdc_put_request(req);
25044609245eSIlya Dryomov }
25054609245eSIlya Dryomov 
25065aea3dcdSIlya Dryomov static void cancel_request(struct ceph_osd_request *req)
25075aea3dcdSIlya Dryomov {
25085aea3dcdSIlya Dryomov 	dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
25095aea3dcdSIlya Dryomov 
25104609245eSIlya Dryomov 	cancel_map_check(req);
251145ee2c1dSIlya Dryomov 	finish_request(req);
2512b18b9550SIlya Dryomov 	complete_all(&req->r_completion);
2513c297eb42SIlya Dryomov 	ceph_osdc_put_request(req);
25145aea3dcdSIlya Dryomov }
25155aea3dcdSIlya Dryomov 
25167cc5e38fSIlya Dryomov static void abort_request(struct ceph_osd_request *req, int err)
25177cc5e38fSIlya Dryomov {
25187cc5e38fSIlya Dryomov 	dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
25197cc5e38fSIlya Dryomov 
25207cc5e38fSIlya Dryomov 	cancel_map_check(req);
25217cc5e38fSIlya Dryomov 	complete_request(req, err);
25227cc5e38fSIlya Dryomov }
25237cc5e38fSIlya Dryomov 
252466850df5SIlya Dryomov static int abort_fn(struct ceph_osd_request *req, void *arg)
252566850df5SIlya Dryomov {
252666850df5SIlya Dryomov 	int err = *(int *)arg;
252766850df5SIlya Dryomov 
252866850df5SIlya Dryomov 	abort_request(req, err);
252966850df5SIlya Dryomov 	return 0; /* continue iteration */
253066850df5SIlya Dryomov }
253166850df5SIlya Dryomov 
253266850df5SIlya Dryomov /*
253366850df5SIlya Dryomov  * Abort all in-flight requests with @err and arrange for all future
253466850df5SIlya Dryomov  * requests to be failed immediately.
253566850df5SIlya Dryomov  */
253666850df5SIlya Dryomov void ceph_osdc_abort_requests(struct ceph_osd_client *osdc, int err)
253766850df5SIlya Dryomov {
253866850df5SIlya Dryomov 	dout("%s osdc %p err %d\n", __func__, osdc, err);
253966850df5SIlya Dryomov 	down_write(&osdc->lock);
254066850df5SIlya Dryomov 	for_each_request(osdc, abort_fn, &err);
254166850df5SIlya Dryomov 	osdc->abort_err = err;
254266850df5SIlya Dryomov 	up_write(&osdc->lock);
254366850df5SIlya Dryomov }
254466850df5SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_abort_requests);
254566850df5SIlya Dryomov 
25462cef0ba8SYan, Zheng void ceph_osdc_clear_abort_err(struct ceph_osd_client *osdc)
25472cef0ba8SYan, Zheng {
25482cef0ba8SYan, Zheng 	down_write(&osdc->lock);
25492cef0ba8SYan, Zheng 	osdc->abort_err = 0;
25502cef0ba8SYan, Zheng 	up_write(&osdc->lock);
25512cef0ba8SYan, Zheng }
25522cef0ba8SYan, Zheng EXPORT_SYMBOL(ceph_osdc_clear_abort_err);
25532cef0ba8SYan, Zheng 
255458eb7932SJeff Layton static void update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
255558eb7932SJeff Layton {
255658eb7932SJeff Layton 	if (likely(eb > osdc->epoch_barrier)) {
255758eb7932SJeff Layton 		dout("updating epoch_barrier from %u to %u\n",
255858eb7932SJeff Layton 				osdc->epoch_barrier, eb);
255958eb7932SJeff Layton 		osdc->epoch_barrier = eb;
256058eb7932SJeff Layton 		/* Request map if we're not to the barrier yet */
256158eb7932SJeff Layton 		if (eb > osdc->osdmap->epoch)
256258eb7932SJeff Layton 			maybe_request_map(osdc);
256358eb7932SJeff Layton 	}
256458eb7932SJeff Layton }
256558eb7932SJeff Layton 
256658eb7932SJeff Layton void ceph_osdc_update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
256758eb7932SJeff Layton {
256858eb7932SJeff Layton 	down_read(&osdc->lock);
256958eb7932SJeff Layton 	if (unlikely(eb > osdc->epoch_barrier)) {
257058eb7932SJeff Layton 		up_read(&osdc->lock);
257158eb7932SJeff Layton 		down_write(&osdc->lock);
257258eb7932SJeff Layton 		update_epoch_barrier(osdc, eb);
257358eb7932SJeff Layton 		up_write(&osdc->lock);
257458eb7932SJeff Layton 	} else {
257558eb7932SJeff Layton 		up_read(&osdc->lock);
257658eb7932SJeff Layton 	}
257758eb7932SJeff Layton }
257858eb7932SJeff Layton EXPORT_SYMBOL(ceph_osdc_update_epoch_barrier);
257958eb7932SJeff Layton 
2580fc36d0a4SJeff Layton /*
25814eea0fefSIlya Dryomov  * We can end up releasing caps as a result of abort_request().
25824eea0fefSIlya Dryomov  * In that case, we probably want to ensure that the cap release message
25834eea0fefSIlya Dryomov  * has an updated epoch barrier in it, so set the epoch barrier prior to
25844eea0fefSIlya Dryomov  * aborting the first request.
25854eea0fefSIlya Dryomov  */
25864eea0fefSIlya Dryomov static int abort_on_full_fn(struct ceph_osd_request *req, void *arg)
25874eea0fefSIlya Dryomov {
25884eea0fefSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
25894eea0fefSIlya Dryomov 	bool *victims = arg;
25904eea0fefSIlya Dryomov 
2591c843d13cSIlya Dryomov 	if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
25924eea0fefSIlya Dryomov 	    (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2593690f951dSIlya Dryomov 	     pool_full(osdc, req->r_t.base_oloc.pool))) {
25944eea0fefSIlya Dryomov 		if (!*victims) {
25954eea0fefSIlya Dryomov 			update_epoch_barrier(osdc, osdc->osdmap->epoch);
25964eea0fefSIlya Dryomov 			*victims = true;
25974eea0fefSIlya Dryomov 		}
25984eea0fefSIlya Dryomov 		abort_request(req, -ENOSPC);
25994eea0fefSIlya Dryomov 	}
26004eea0fefSIlya Dryomov 
26014eea0fefSIlya Dryomov 	return 0; /* continue iteration */
26024eea0fefSIlya Dryomov }
26034eea0fefSIlya Dryomov 
26044eea0fefSIlya Dryomov /*
2605fc36d0a4SJeff Layton  * Drop all pending requests that are stalled waiting on a full condition to
260658eb7932SJeff Layton  * clear, and complete them with ENOSPC as the return code. Set the
260758eb7932SJeff Layton  * osdc->epoch_barrier to the latest map epoch that we've seen if any were
260858eb7932SJeff Layton  * cancelled.
2609fc36d0a4SJeff Layton  */
2610fc36d0a4SJeff Layton static void ceph_osdc_abort_on_full(struct ceph_osd_client *osdc)
2611fc36d0a4SJeff Layton {
261258eb7932SJeff Layton 	bool victims = false;
2613fc36d0a4SJeff Layton 
261402b2f549SDongsheng Yang 	if (ceph_test_opt(osdc->client, ABORT_ON_FULL) &&
2615c843d13cSIlya Dryomov 	    (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) || have_pool_full(osdc)))
26164eea0fefSIlya Dryomov 		for_each_request(osdc, abort_on_full_fn, &victims);
2617fc36d0a4SJeff Layton }
2618fc36d0a4SJeff Layton 
26194609245eSIlya Dryomov static void check_pool_dne(struct ceph_osd_request *req)
26204609245eSIlya Dryomov {
26214609245eSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
26224609245eSIlya Dryomov 	struct ceph_osdmap *map = osdc->osdmap;
26234609245eSIlya Dryomov 
26244609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
26254609245eSIlya Dryomov 	WARN_ON(!map->epoch);
26264609245eSIlya Dryomov 
26274609245eSIlya Dryomov 	if (req->r_attempts) {
26284609245eSIlya Dryomov 		/*
26294609245eSIlya Dryomov 		 * We sent a request earlier, which means that
26304609245eSIlya Dryomov 		 * previously the pool existed, and now it does not
26314609245eSIlya Dryomov 		 * (i.e., it was deleted).
26324609245eSIlya Dryomov 		 */
26334609245eSIlya Dryomov 		req->r_map_dne_bound = map->epoch;
26344609245eSIlya Dryomov 		dout("%s req %p tid %llu pool disappeared\n", __func__, req,
26354609245eSIlya Dryomov 		     req->r_tid);
26364609245eSIlya Dryomov 	} else {
26374609245eSIlya Dryomov 		dout("%s req %p tid %llu map_dne_bound %u have %u\n", __func__,
26384609245eSIlya Dryomov 		     req, req->r_tid, req->r_map_dne_bound, map->epoch);
26394609245eSIlya Dryomov 	}
26404609245eSIlya Dryomov 
26414609245eSIlya Dryomov 	if (req->r_map_dne_bound) {
26424609245eSIlya Dryomov 		if (map->epoch >= req->r_map_dne_bound) {
26434609245eSIlya Dryomov 			/* we had a new enough map */
26444609245eSIlya Dryomov 			pr_info_ratelimited("tid %llu pool does not exist\n",
26454609245eSIlya Dryomov 					    req->r_tid);
26464609245eSIlya Dryomov 			complete_request(req, -ENOENT);
26474609245eSIlya Dryomov 		}
26484609245eSIlya Dryomov 	} else {
26494609245eSIlya Dryomov 		send_map_check(req);
26504609245eSIlya Dryomov 	}
26514609245eSIlya Dryomov }
26524609245eSIlya Dryomov 
26534609245eSIlya Dryomov static void map_check_cb(struct ceph_mon_generic_request *greq)
26544609245eSIlya Dryomov {
26554609245eSIlya Dryomov 	struct ceph_osd_client *osdc = &greq->monc->client->osdc;
26564609245eSIlya Dryomov 	struct ceph_osd_request *req;
26574609245eSIlya Dryomov 	u64 tid = greq->private_data;
26584609245eSIlya Dryomov 
26594609245eSIlya Dryomov 	WARN_ON(greq->result || !greq->u.newest);
26604609245eSIlya Dryomov 
26614609245eSIlya Dryomov 	down_write(&osdc->lock);
26624609245eSIlya Dryomov 	req = lookup_request_mc(&osdc->map_checks, tid);
26634609245eSIlya Dryomov 	if (!req) {
26644609245eSIlya Dryomov 		dout("%s tid %llu dne\n", __func__, tid);
26654609245eSIlya Dryomov 		goto out_unlock;
26664609245eSIlya Dryomov 	}
26674609245eSIlya Dryomov 
26684609245eSIlya Dryomov 	dout("%s req %p tid %llu map_dne_bound %u newest %llu\n", __func__,
26694609245eSIlya Dryomov 	     req, req->r_tid, req->r_map_dne_bound, greq->u.newest);
26704609245eSIlya Dryomov 	if (!req->r_map_dne_bound)
26714609245eSIlya Dryomov 		req->r_map_dne_bound = greq->u.newest;
26724609245eSIlya Dryomov 	erase_request_mc(&osdc->map_checks, req);
26734609245eSIlya Dryomov 	check_pool_dne(req);
26744609245eSIlya Dryomov 
26754609245eSIlya Dryomov 	ceph_osdc_put_request(req);
26764609245eSIlya Dryomov out_unlock:
26774609245eSIlya Dryomov 	up_write(&osdc->lock);
26784609245eSIlya Dryomov }
26794609245eSIlya Dryomov 
26804609245eSIlya Dryomov static void send_map_check(struct ceph_osd_request *req)
26814609245eSIlya Dryomov {
26824609245eSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
26834609245eSIlya Dryomov 	struct ceph_osd_request *lookup_req;
26844609245eSIlya Dryomov 	int ret;
26854609245eSIlya Dryomov 
26864609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
26874609245eSIlya Dryomov 
26884609245eSIlya Dryomov 	lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
26894609245eSIlya Dryomov 	if (lookup_req) {
26904609245eSIlya Dryomov 		WARN_ON(lookup_req != req);
26914609245eSIlya Dryomov 		return;
26924609245eSIlya Dryomov 	}
26934609245eSIlya Dryomov 
26944609245eSIlya Dryomov 	ceph_osdc_get_request(req);
26954609245eSIlya Dryomov 	insert_request_mc(&osdc->map_checks, req);
26964609245eSIlya Dryomov 	ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
26974609245eSIlya Dryomov 					  map_check_cb, req->r_tid);
26984609245eSIlya Dryomov 	WARN_ON(ret);
26994609245eSIlya Dryomov }
27004609245eSIlya Dryomov 
27010bbfdfe8SIlya Dryomov /*
2702922dab61SIlya Dryomov  * lingering requests, watch/notify v2 infrastructure
2703922dab61SIlya Dryomov  */
2704922dab61SIlya Dryomov static void linger_release(struct kref *kref)
2705922dab61SIlya Dryomov {
2706922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq =
2707922dab61SIlya Dryomov 	    container_of(kref, struct ceph_osd_linger_request, kref);
2708922dab61SIlya Dryomov 
2709922dab61SIlya Dryomov 	dout("%s lreq %p reg_req %p ping_req %p\n", __func__, lreq,
2710922dab61SIlya Dryomov 	     lreq->reg_req, lreq->ping_req);
2711922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&lreq->node));
2712922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&lreq->osdc_node));
27134609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&lreq->mc_node));
2714922dab61SIlya Dryomov 	WARN_ON(!list_empty(&lreq->scan_item));
2715b07d3c4bSIlya Dryomov 	WARN_ON(!list_empty(&lreq->pending_lworks));
2716922dab61SIlya Dryomov 	WARN_ON(lreq->osd);
2717922dab61SIlya Dryomov 
271875dbb685SIlya Dryomov 	if (lreq->request_pl)
271975dbb685SIlya Dryomov 		ceph_pagelist_release(lreq->request_pl);
272075dbb685SIlya Dryomov 	if (lreq->notify_id_pages)
272175dbb685SIlya Dryomov 		ceph_release_page_vector(lreq->notify_id_pages, 1);
272275dbb685SIlya Dryomov 
2723922dab61SIlya Dryomov 	ceph_osdc_put_request(lreq->reg_req);
2724922dab61SIlya Dryomov 	ceph_osdc_put_request(lreq->ping_req);
2725922dab61SIlya Dryomov 	target_destroy(&lreq->t);
2726922dab61SIlya Dryomov 	kfree(lreq);
2727922dab61SIlya Dryomov }
2728922dab61SIlya Dryomov 
2729922dab61SIlya Dryomov static void linger_put(struct ceph_osd_linger_request *lreq)
2730922dab61SIlya Dryomov {
2731922dab61SIlya Dryomov 	if (lreq)
2732922dab61SIlya Dryomov 		kref_put(&lreq->kref, linger_release);
2733922dab61SIlya Dryomov }
2734922dab61SIlya Dryomov 
2735922dab61SIlya Dryomov static struct ceph_osd_linger_request *
2736922dab61SIlya Dryomov linger_get(struct ceph_osd_linger_request *lreq)
2737922dab61SIlya Dryomov {
2738922dab61SIlya Dryomov 	kref_get(&lreq->kref);
2739922dab61SIlya Dryomov 	return lreq;
2740922dab61SIlya Dryomov }
2741922dab61SIlya Dryomov 
2742922dab61SIlya Dryomov static struct ceph_osd_linger_request *
2743922dab61SIlya Dryomov linger_alloc(struct ceph_osd_client *osdc)
2744922dab61SIlya Dryomov {
2745922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
2746922dab61SIlya Dryomov 
2747922dab61SIlya Dryomov 	lreq = kzalloc(sizeof(*lreq), GFP_NOIO);
2748922dab61SIlya Dryomov 	if (!lreq)
2749922dab61SIlya Dryomov 		return NULL;
2750922dab61SIlya Dryomov 
2751922dab61SIlya Dryomov 	kref_init(&lreq->kref);
2752922dab61SIlya Dryomov 	mutex_init(&lreq->lock);
2753922dab61SIlya Dryomov 	RB_CLEAR_NODE(&lreq->node);
2754922dab61SIlya Dryomov 	RB_CLEAR_NODE(&lreq->osdc_node);
27554609245eSIlya Dryomov 	RB_CLEAR_NODE(&lreq->mc_node);
2756922dab61SIlya Dryomov 	INIT_LIST_HEAD(&lreq->scan_item);
2757b07d3c4bSIlya Dryomov 	INIT_LIST_HEAD(&lreq->pending_lworks);
2758922dab61SIlya Dryomov 	init_completion(&lreq->reg_commit_wait);
275919079203SIlya Dryomov 	init_completion(&lreq->notify_finish_wait);
2760922dab61SIlya Dryomov 
2761922dab61SIlya Dryomov 	lreq->osdc = osdc;
2762922dab61SIlya Dryomov 	target_init(&lreq->t);
2763922dab61SIlya Dryomov 
2764922dab61SIlya Dryomov 	dout("%s lreq %p\n", __func__, lreq);
2765922dab61SIlya Dryomov 	return lreq;
2766922dab61SIlya Dryomov }
2767922dab61SIlya Dryomov 
2768922dab61SIlya Dryomov DEFINE_RB_INSDEL_FUNCS(linger, struct ceph_osd_linger_request, linger_id, node)
2769922dab61SIlya Dryomov DEFINE_RB_FUNCS(linger_osdc, struct ceph_osd_linger_request, linger_id, osdc_node)
27704609245eSIlya Dryomov DEFINE_RB_FUNCS(linger_mc, struct ceph_osd_linger_request, linger_id, mc_node)
2771922dab61SIlya Dryomov 
2772922dab61SIlya Dryomov /*
2773922dab61SIlya Dryomov  * Create linger request <-> OSD session relation.
2774922dab61SIlya Dryomov  *
2775922dab61SIlya Dryomov  * @lreq has to be registered, @osd may be homeless.
2776922dab61SIlya Dryomov  */
2777922dab61SIlya Dryomov static void link_linger(struct ceph_osd *osd,
2778922dab61SIlya Dryomov 			struct ceph_osd_linger_request *lreq)
2779922dab61SIlya Dryomov {
2780922dab61SIlya Dryomov 	verify_osd_locked(osd);
2781922dab61SIlya Dryomov 	WARN_ON(!lreq->linger_id || lreq->osd);
2782922dab61SIlya Dryomov 	dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2783922dab61SIlya Dryomov 	     osd->o_osd, lreq, lreq->linger_id);
2784922dab61SIlya Dryomov 
2785922dab61SIlya Dryomov 	if (!osd_homeless(osd))
2786922dab61SIlya Dryomov 		__remove_osd_from_lru(osd);
2787922dab61SIlya Dryomov 	else
2788922dab61SIlya Dryomov 		atomic_inc(&osd->o_osdc->num_homeless);
2789922dab61SIlya Dryomov 
2790922dab61SIlya Dryomov 	get_osd(osd);
2791922dab61SIlya Dryomov 	insert_linger(&osd->o_linger_requests, lreq);
2792922dab61SIlya Dryomov 	lreq->osd = osd;
2793922dab61SIlya Dryomov }
2794922dab61SIlya Dryomov 
2795922dab61SIlya Dryomov static void unlink_linger(struct ceph_osd *osd,
2796922dab61SIlya Dryomov 			  struct ceph_osd_linger_request *lreq)
2797922dab61SIlya Dryomov {
2798922dab61SIlya Dryomov 	verify_osd_locked(osd);
2799922dab61SIlya Dryomov 	WARN_ON(lreq->osd != osd);
2800922dab61SIlya Dryomov 	dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2801922dab61SIlya Dryomov 	     osd->o_osd, lreq, lreq->linger_id);
2802922dab61SIlya Dryomov 
2803922dab61SIlya Dryomov 	lreq->osd = NULL;
2804922dab61SIlya Dryomov 	erase_linger(&osd->o_linger_requests, lreq);
2805922dab61SIlya Dryomov 	put_osd(osd);
2806922dab61SIlya Dryomov 
2807922dab61SIlya Dryomov 	if (!osd_homeless(osd))
2808922dab61SIlya Dryomov 		maybe_move_osd_to_lru(osd);
2809922dab61SIlya Dryomov 	else
2810922dab61SIlya Dryomov 		atomic_dec(&osd->o_osdc->num_homeless);
2811922dab61SIlya Dryomov }
2812922dab61SIlya Dryomov 
2813922dab61SIlya Dryomov static bool __linger_registered(struct ceph_osd_linger_request *lreq)
2814922dab61SIlya Dryomov {
2815922dab61SIlya Dryomov 	verify_osdc_locked(lreq->osdc);
2816922dab61SIlya Dryomov 
2817922dab61SIlya Dryomov 	return !RB_EMPTY_NODE(&lreq->osdc_node);
2818922dab61SIlya Dryomov }
2819922dab61SIlya Dryomov 
2820922dab61SIlya Dryomov static bool linger_registered(struct ceph_osd_linger_request *lreq)
2821922dab61SIlya Dryomov {
2822922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2823922dab61SIlya Dryomov 	bool registered;
2824922dab61SIlya Dryomov 
2825922dab61SIlya Dryomov 	down_read(&osdc->lock);
2826922dab61SIlya Dryomov 	registered = __linger_registered(lreq);
2827922dab61SIlya Dryomov 	up_read(&osdc->lock);
2828922dab61SIlya Dryomov 
2829922dab61SIlya Dryomov 	return registered;
2830922dab61SIlya Dryomov }
2831922dab61SIlya Dryomov 
2832922dab61SIlya Dryomov static void linger_register(struct ceph_osd_linger_request *lreq)
2833922dab61SIlya Dryomov {
2834922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2835922dab61SIlya Dryomov 
2836922dab61SIlya Dryomov 	verify_osdc_wrlocked(osdc);
2837922dab61SIlya Dryomov 	WARN_ON(lreq->linger_id);
2838922dab61SIlya Dryomov 
2839922dab61SIlya Dryomov 	linger_get(lreq);
2840922dab61SIlya Dryomov 	lreq->linger_id = ++osdc->last_linger_id;
2841922dab61SIlya Dryomov 	insert_linger_osdc(&osdc->linger_requests, lreq);
2842922dab61SIlya Dryomov }
2843922dab61SIlya Dryomov 
2844922dab61SIlya Dryomov static void linger_unregister(struct ceph_osd_linger_request *lreq)
2845922dab61SIlya Dryomov {
2846922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2847922dab61SIlya Dryomov 
2848922dab61SIlya Dryomov 	verify_osdc_wrlocked(osdc);
2849922dab61SIlya Dryomov 
2850922dab61SIlya Dryomov 	erase_linger_osdc(&osdc->linger_requests, lreq);
2851922dab61SIlya Dryomov 	linger_put(lreq);
2852922dab61SIlya Dryomov }
2853922dab61SIlya Dryomov 
2854922dab61SIlya Dryomov static void cancel_linger_request(struct ceph_osd_request *req)
2855922dab61SIlya Dryomov {
2856922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
2857922dab61SIlya Dryomov 
2858922dab61SIlya Dryomov 	WARN_ON(!req->r_linger);
2859922dab61SIlya Dryomov 	cancel_request(req);
2860922dab61SIlya Dryomov 	linger_put(lreq);
2861922dab61SIlya Dryomov }
2862922dab61SIlya Dryomov 
2863922dab61SIlya Dryomov struct linger_work {
2864922dab61SIlya Dryomov 	struct work_struct work;
2865922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
2866b07d3c4bSIlya Dryomov 	struct list_head pending_item;
2867b07d3c4bSIlya Dryomov 	unsigned long queued_stamp;
2868922dab61SIlya Dryomov 
2869922dab61SIlya Dryomov 	union {
2870922dab61SIlya Dryomov 		struct {
2871922dab61SIlya Dryomov 			u64 notify_id;
2872922dab61SIlya Dryomov 			u64 notifier_id;
2873922dab61SIlya Dryomov 			void *payload; /* points into @msg front */
2874922dab61SIlya Dryomov 			size_t payload_len;
2875922dab61SIlya Dryomov 
2876922dab61SIlya Dryomov 			struct ceph_msg *msg; /* for ceph_msg_put() */
2877922dab61SIlya Dryomov 		} notify;
2878922dab61SIlya Dryomov 		struct {
2879922dab61SIlya Dryomov 			int err;
2880922dab61SIlya Dryomov 		} error;
2881922dab61SIlya Dryomov 	};
2882922dab61SIlya Dryomov };
2883922dab61SIlya Dryomov 
2884922dab61SIlya Dryomov static struct linger_work *lwork_alloc(struct ceph_osd_linger_request *lreq,
2885922dab61SIlya Dryomov 				       work_func_t workfn)
2886922dab61SIlya Dryomov {
2887922dab61SIlya Dryomov 	struct linger_work *lwork;
2888922dab61SIlya Dryomov 
2889922dab61SIlya Dryomov 	lwork = kzalloc(sizeof(*lwork), GFP_NOIO);
2890922dab61SIlya Dryomov 	if (!lwork)
2891922dab61SIlya Dryomov 		return NULL;
2892922dab61SIlya Dryomov 
2893922dab61SIlya Dryomov 	INIT_WORK(&lwork->work, workfn);
2894b07d3c4bSIlya Dryomov 	INIT_LIST_HEAD(&lwork->pending_item);
2895922dab61SIlya Dryomov 	lwork->lreq = linger_get(lreq);
2896922dab61SIlya Dryomov 
2897922dab61SIlya Dryomov 	return lwork;
2898922dab61SIlya Dryomov }
2899922dab61SIlya Dryomov 
2900922dab61SIlya Dryomov static void lwork_free(struct linger_work *lwork)
2901922dab61SIlya Dryomov {
2902922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2903922dab61SIlya Dryomov 
2904b07d3c4bSIlya Dryomov 	mutex_lock(&lreq->lock);
2905b07d3c4bSIlya Dryomov 	list_del(&lwork->pending_item);
2906b07d3c4bSIlya Dryomov 	mutex_unlock(&lreq->lock);
2907b07d3c4bSIlya Dryomov 
2908922dab61SIlya Dryomov 	linger_put(lreq);
2909922dab61SIlya Dryomov 	kfree(lwork);
2910922dab61SIlya Dryomov }
2911922dab61SIlya Dryomov 
2912922dab61SIlya Dryomov static void lwork_queue(struct linger_work *lwork)
2913922dab61SIlya Dryomov {
2914922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2915922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2916922dab61SIlya Dryomov 
2917922dab61SIlya Dryomov 	verify_lreq_locked(lreq);
2918b07d3c4bSIlya Dryomov 	WARN_ON(!list_empty(&lwork->pending_item));
2919b07d3c4bSIlya Dryomov 
2920b07d3c4bSIlya Dryomov 	lwork->queued_stamp = jiffies;
2921b07d3c4bSIlya Dryomov 	list_add_tail(&lwork->pending_item, &lreq->pending_lworks);
2922922dab61SIlya Dryomov 	queue_work(osdc->notify_wq, &lwork->work);
2923922dab61SIlya Dryomov }
2924922dab61SIlya Dryomov 
2925922dab61SIlya Dryomov static void do_watch_notify(struct work_struct *w)
2926922dab61SIlya Dryomov {
2927922dab61SIlya Dryomov 	struct linger_work *lwork = container_of(w, struct linger_work, work);
2928922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2929922dab61SIlya Dryomov 
2930922dab61SIlya Dryomov 	if (!linger_registered(lreq)) {
2931922dab61SIlya Dryomov 		dout("%s lreq %p not registered\n", __func__, lreq);
2932922dab61SIlya Dryomov 		goto out;
2933922dab61SIlya Dryomov 	}
2934922dab61SIlya Dryomov 
293519079203SIlya Dryomov 	WARN_ON(!lreq->is_watch);
2936922dab61SIlya Dryomov 	dout("%s lreq %p notify_id %llu notifier_id %llu payload_len %zu\n",
2937922dab61SIlya Dryomov 	     __func__, lreq, lwork->notify.notify_id, lwork->notify.notifier_id,
2938922dab61SIlya Dryomov 	     lwork->notify.payload_len);
2939922dab61SIlya Dryomov 	lreq->wcb(lreq->data, lwork->notify.notify_id, lreq->linger_id,
2940922dab61SIlya Dryomov 		  lwork->notify.notifier_id, lwork->notify.payload,
2941922dab61SIlya Dryomov 		  lwork->notify.payload_len);
2942922dab61SIlya Dryomov 
2943922dab61SIlya Dryomov out:
2944922dab61SIlya Dryomov 	ceph_msg_put(lwork->notify.msg);
2945922dab61SIlya Dryomov 	lwork_free(lwork);
2946922dab61SIlya Dryomov }
2947922dab61SIlya Dryomov 
2948922dab61SIlya Dryomov static void do_watch_error(struct work_struct *w)
2949922dab61SIlya Dryomov {
2950922dab61SIlya Dryomov 	struct linger_work *lwork = container_of(w, struct linger_work, work);
2951922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2952922dab61SIlya Dryomov 
2953922dab61SIlya Dryomov 	if (!linger_registered(lreq)) {
2954922dab61SIlya Dryomov 		dout("%s lreq %p not registered\n", __func__, lreq);
2955922dab61SIlya Dryomov 		goto out;
2956922dab61SIlya Dryomov 	}
2957922dab61SIlya Dryomov 
2958922dab61SIlya Dryomov 	dout("%s lreq %p err %d\n", __func__, lreq, lwork->error.err);
2959922dab61SIlya Dryomov 	lreq->errcb(lreq->data, lreq->linger_id, lwork->error.err);
2960922dab61SIlya Dryomov 
2961922dab61SIlya Dryomov out:
2962922dab61SIlya Dryomov 	lwork_free(lwork);
2963922dab61SIlya Dryomov }
2964922dab61SIlya Dryomov 
2965922dab61SIlya Dryomov static void queue_watch_error(struct ceph_osd_linger_request *lreq)
2966922dab61SIlya Dryomov {
2967922dab61SIlya Dryomov 	struct linger_work *lwork;
2968922dab61SIlya Dryomov 
2969922dab61SIlya Dryomov 	lwork = lwork_alloc(lreq, do_watch_error);
2970922dab61SIlya Dryomov 	if (!lwork) {
2971922dab61SIlya Dryomov 		pr_err("failed to allocate error-lwork\n");
2972922dab61SIlya Dryomov 		return;
2973922dab61SIlya Dryomov 	}
2974922dab61SIlya Dryomov 
2975922dab61SIlya Dryomov 	lwork->error.err = lreq->last_error;
2976922dab61SIlya Dryomov 	lwork_queue(lwork);
2977922dab61SIlya Dryomov }
2978922dab61SIlya Dryomov 
2979922dab61SIlya Dryomov static void linger_reg_commit_complete(struct ceph_osd_linger_request *lreq,
2980922dab61SIlya Dryomov 				       int result)
2981922dab61SIlya Dryomov {
2982922dab61SIlya Dryomov 	if (!completion_done(&lreq->reg_commit_wait)) {
2983922dab61SIlya Dryomov 		lreq->reg_commit_error = (result <= 0 ? result : 0);
2984922dab61SIlya Dryomov 		complete_all(&lreq->reg_commit_wait);
2985922dab61SIlya Dryomov 	}
2986922dab61SIlya Dryomov }
2987922dab61SIlya Dryomov 
2988922dab61SIlya Dryomov static void linger_commit_cb(struct ceph_osd_request *req)
2989922dab61SIlya Dryomov {
2990922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
2991922dab61SIlya Dryomov 
2992922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
299375dbb685SIlya Dryomov 	if (req != lreq->reg_req) {
299475dbb685SIlya Dryomov 		dout("%s lreq %p linger_id %llu unknown req (%p != %p)\n",
299575dbb685SIlya Dryomov 		     __func__, lreq, lreq->linger_id, req, lreq->reg_req);
299675dbb685SIlya Dryomov 		goto out;
299775dbb685SIlya Dryomov 	}
299875dbb685SIlya Dryomov 
2999922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu result %d\n", __func__, lreq,
3000922dab61SIlya Dryomov 	     lreq->linger_id, req->r_result);
3001922dab61SIlya Dryomov 	linger_reg_commit_complete(lreq, req->r_result);
3002922dab61SIlya Dryomov 	lreq->committed = true;
3003922dab61SIlya Dryomov 
300419079203SIlya Dryomov 	if (!lreq->is_watch) {
300519079203SIlya Dryomov 		struct ceph_osd_data *osd_data =
300619079203SIlya Dryomov 		    osd_req_op_data(req, 0, notify, response_data);
300719079203SIlya Dryomov 		void *p = page_address(osd_data->pages[0]);
300819079203SIlya Dryomov 
300919079203SIlya Dryomov 		WARN_ON(req->r_ops[0].op != CEPH_OSD_OP_NOTIFY ||
301019079203SIlya Dryomov 			osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
301119079203SIlya Dryomov 
301219079203SIlya Dryomov 		/* make note of the notify_id */
301319079203SIlya Dryomov 		if (req->r_ops[0].outdata_len >= sizeof(u64)) {
301419079203SIlya Dryomov 			lreq->notify_id = ceph_decode_64(&p);
301519079203SIlya Dryomov 			dout("lreq %p notify_id %llu\n", lreq,
301619079203SIlya Dryomov 			     lreq->notify_id);
301719079203SIlya Dryomov 		} else {
301819079203SIlya Dryomov 			dout("lreq %p no notify_id\n", lreq);
301919079203SIlya Dryomov 		}
302019079203SIlya Dryomov 	}
302119079203SIlya Dryomov 
302275dbb685SIlya Dryomov out:
3023922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
3024922dab61SIlya Dryomov 	linger_put(lreq);
3025922dab61SIlya Dryomov }
3026922dab61SIlya Dryomov 
3027922dab61SIlya Dryomov static int normalize_watch_error(int err)
3028922dab61SIlya Dryomov {
3029922dab61SIlya Dryomov 	/*
3030922dab61SIlya Dryomov 	 * Translate ENOENT -> ENOTCONN so that a delete->disconnection
3031922dab61SIlya Dryomov 	 * notification and a failure to reconnect because we raced with
3032922dab61SIlya Dryomov 	 * the delete appear the same to the user.
3033922dab61SIlya Dryomov 	 */
3034922dab61SIlya Dryomov 	if (err == -ENOENT)
3035922dab61SIlya Dryomov 		err = -ENOTCONN;
3036922dab61SIlya Dryomov 
3037922dab61SIlya Dryomov 	return err;
3038922dab61SIlya Dryomov }
3039922dab61SIlya Dryomov 
3040922dab61SIlya Dryomov static void linger_reconnect_cb(struct ceph_osd_request *req)
3041922dab61SIlya Dryomov {
3042922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
3043922dab61SIlya Dryomov 
3044922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
304575dbb685SIlya Dryomov 	if (req != lreq->reg_req) {
304675dbb685SIlya Dryomov 		dout("%s lreq %p linger_id %llu unknown req (%p != %p)\n",
304775dbb685SIlya Dryomov 		     __func__, lreq, lreq->linger_id, req, lreq->reg_req);
304875dbb685SIlya Dryomov 		goto out;
304975dbb685SIlya Dryomov 	}
305075dbb685SIlya Dryomov 
3051922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu result %d last_error %d\n", __func__,
3052922dab61SIlya Dryomov 	     lreq, lreq->linger_id, req->r_result, lreq->last_error);
3053922dab61SIlya Dryomov 	if (req->r_result < 0) {
3054922dab61SIlya Dryomov 		if (!lreq->last_error) {
3055922dab61SIlya Dryomov 			lreq->last_error = normalize_watch_error(req->r_result);
3056922dab61SIlya Dryomov 			queue_watch_error(lreq);
3057922dab61SIlya Dryomov 		}
3058922dab61SIlya Dryomov 	}
3059922dab61SIlya Dryomov 
306075dbb685SIlya Dryomov out:
3061922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
3062922dab61SIlya Dryomov 	linger_put(lreq);
3063922dab61SIlya Dryomov }
3064922dab61SIlya Dryomov 
3065922dab61SIlya Dryomov static void send_linger(struct ceph_osd_linger_request *lreq)
3066922dab61SIlya Dryomov {
306775dbb685SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
306875dbb685SIlya Dryomov 	struct ceph_osd_request *req;
306975dbb685SIlya Dryomov 	int ret;
3070922dab61SIlya Dryomov 
307175dbb685SIlya Dryomov 	verify_osdc_wrlocked(osdc);
307275dbb685SIlya Dryomov 	mutex_lock(&lreq->lock);
3073922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3074922dab61SIlya Dryomov 
307575dbb685SIlya Dryomov 	if (lreq->reg_req) {
307675dbb685SIlya Dryomov 		if (lreq->reg_req->r_osd)
307775dbb685SIlya Dryomov 			cancel_linger_request(lreq->reg_req);
307875dbb685SIlya Dryomov 		ceph_osdc_put_request(lreq->reg_req);
307975dbb685SIlya Dryomov 	}
3080922dab61SIlya Dryomov 
308175dbb685SIlya Dryomov 	req = ceph_osdc_alloc_request(osdc, NULL, 1, true, GFP_NOIO);
308275dbb685SIlya Dryomov 	BUG_ON(!req);
308375dbb685SIlya Dryomov 
30845133ba8fSIlya Dryomov 	target_copy(&req->r_t, &lreq->t);
3085922dab61SIlya Dryomov 	req->r_mtime = lreq->mtime;
3086922dab61SIlya Dryomov 
308719079203SIlya Dryomov 	if (lreq->is_watch && lreq->committed) {
308875dbb685SIlya Dryomov 		osd_req_op_watch_init(req, 0, CEPH_OSD_WATCH_OP_RECONNECT,
308975dbb685SIlya Dryomov 				      lreq->linger_id, ++lreq->register_gen);
3090922dab61SIlya Dryomov 		dout("lreq %p reconnect register_gen %u\n", lreq,
309175dbb685SIlya Dryomov 		     req->r_ops[0].watch.gen);
3092922dab61SIlya Dryomov 		req->r_callback = linger_reconnect_cb;
3093922dab61SIlya Dryomov 	} else {
309475dbb685SIlya Dryomov 		if (lreq->is_watch) {
309575dbb685SIlya Dryomov 			osd_req_op_watch_init(req, 0, CEPH_OSD_WATCH_OP_WATCH,
309675dbb685SIlya Dryomov 					      lreq->linger_id, 0);
309775dbb685SIlya Dryomov 		} else {
309819079203SIlya Dryomov 			lreq->notify_id = 0;
309975dbb685SIlya Dryomov 
310075dbb685SIlya Dryomov 			refcount_inc(&lreq->request_pl->refcnt);
310175dbb685SIlya Dryomov 			osd_req_op_notify_init(req, 0, lreq->linger_id,
310275dbb685SIlya Dryomov 					       lreq->request_pl);
310375dbb685SIlya Dryomov 			ceph_osd_data_pages_init(
310475dbb685SIlya Dryomov 			    osd_req_op_data(req, 0, notify, response_data),
310575dbb685SIlya Dryomov 			    lreq->notify_id_pages, PAGE_SIZE, 0, false, false);
310675dbb685SIlya Dryomov 		}
3107922dab61SIlya Dryomov 		dout("lreq %p register\n", lreq);
3108922dab61SIlya Dryomov 		req->r_callback = linger_commit_cb;
3109922dab61SIlya Dryomov 	}
311075dbb685SIlya Dryomov 
311175dbb685SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
311275dbb685SIlya Dryomov 	BUG_ON(ret);
3113922dab61SIlya Dryomov 
3114922dab61SIlya Dryomov 	req->r_priv = linger_get(lreq);
3115922dab61SIlya Dryomov 	req->r_linger = true;
311675dbb685SIlya Dryomov 	lreq->reg_req = req;
311775dbb685SIlya Dryomov 	mutex_unlock(&lreq->lock);
3118922dab61SIlya Dryomov 
3119922dab61SIlya Dryomov 	submit_request(req, true);
3120922dab61SIlya Dryomov }
3121922dab61SIlya Dryomov 
3122922dab61SIlya Dryomov static void linger_ping_cb(struct ceph_osd_request *req)
3123922dab61SIlya Dryomov {
3124922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
3125922dab61SIlya Dryomov 
3126922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
312775dbb685SIlya Dryomov 	if (req != lreq->ping_req) {
312875dbb685SIlya Dryomov 		dout("%s lreq %p linger_id %llu unknown req (%p != %p)\n",
312975dbb685SIlya Dryomov 		     __func__, lreq, lreq->linger_id, req, lreq->ping_req);
313075dbb685SIlya Dryomov 		goto out;
313175dbb685SIlya Dryomov 	}
313275dbb685SIlya Dryomov 
3133922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu result %d ping_sent %lu last_error %d\n",
3134922dab61SIlya Dryomov 	     __func__, lreq, lreq->linger_id, req->r_result, lreq->ping_sent,
3135922dab61SIlya Dryomov 	     lreq->last_error);
3136922dab61SIlya Dryomov 	if (lreq->register_gen == req->r_ops[0].watch.gen) {
3137b07d3c4bSIlya Dryomov 		if (!req->r_result) {
3138b07d3c4bSIlya Dryomov 			lreq->watch_valid_thru = lreq->ping_sent;
3139b07d3c4bSIlya Dryomov 		} else if (!lreq->last_error) {
3140922dab61SIlya Dryomov 			lreq->last_error = normalize_watch_error(req->r_result);
3141922dab61SIlya Dryomov 			queue_watch_error(lreq);
3142922dab61SIlya Dryomov 		}
3143922dab61SIlya Dryomov 	} else {
3144922dab61SIlya Dryomov 		dout("lreq %p register_gen %u ignoring old pong %u\n", lreq,
3145922dab61SIlya Dryomov 		     lreq->register_gen, req->r_ops[0].watch.gen);
3146922dab61SIlya Dryomov 	}
3147922dab61SIlya Dryomov 
314875dbb685SIlya Dryomov out:
3149922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
3150922dab61SIlya Dryomov 	linger_put(lreq);
3151922dab61SIlya Dryomov }
3152922dab61SIlya Dryomov 
3153922dab61SIlya Dryomov static void send_linger_ping(struct ceph_osd_linger_request *lreq)
3154922dab61SIlya Dryomov {
3155922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
315675dbb685SIlya Dryomov 	struct ceph_osd_request *req;
315775dbb685SIlya Dryomov 	int ret;
3158922dab61SIlya Dryomov 
3159b7ec35b3SIlya Dryomov 	if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
3160922dab61SIlya Dryomov 		dout("%s PAUSERD\n", __func__);
3161922dab61SIlya Dryomov 		return;
3162922dab61SIlya Dryomov 	}
3163922dab61SIlya Dryomov 
3164922dab61SIlya Dryomov 	lreq->ping_sent = jiffies;
3165922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu ping_sent %lu register_gen %u\n",
3166922dab61SIlya Dryomov 	     __func__, lreq, lreq->linger_id, lreq->ping_sent,
3167922dab61SIlya Dryomov 	     lreq->register_gen);
3168922dab61SIlya Dryomov 
316975dbb685SIlya Dryomov 	if (lreq->ping_req) {
317075dbb685SIlya Dryomov 		if (lreq->ping_req->r_osd)
317175dbb685SIlya Dryomov 			cancel_linger_request(lreq->ping_req);
317275dbb685SIlya Dryomov 		ceph_osdc_put_request(lreq->ping_req);
317375dbb685SIlya Dryomov 	}
3174922dab61SIlya Dryomov 
317575dbb685SIlya Dryomov 	req = ceph_osdc_alloc_request(osdc, NULL, 1, true, GFP_NOIO);
317675dbb685SIlya Dryomov 	BUG_ON(!req);
317775dbb685SIlya Dryomov 
3178922dab61SIlya Dryomov 	target_copy(&req->r_t, &lreq->t);
317975dbb685SIlya Dryomov 	osd_req_op_watch_init(req, 0, CEPH_OSD_WATCH_OP_PING, lreq->linger_id,
318075dbb685SIlya Dryomov 			      lreq->register_gen);
3181922dab61SIlya Dryomov 	req->r_callback = linger_ping_cb;
318275dbb685SIlya Dryomov 
318375dbb685SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
318475dbb685SIlya Dryomov 	BUG_ON(ret);
318575dbb685SIlya Dryomov 
3186922dab61SIlya Dryomov 	req->r_priv = linger_get(lreq);
3187922dab61SIlya Dryomov 	req->r_linger = true;
318875dbb685SIlya Dryomov 	lreq->ping_req = req;
3189922dab61SIlya Dryomov 
3190922dab61SIlya Dryomov 	ceph_osdc_get_request(req);
3191922dab61SIlya Dryomov 	account_request(req);
3192922dab61SIlya Dryomov 	req->r_tid = atomic64_inc_return(&osdc->last_tid);
3193922dab61SIlya Dryomov 	link_request(lreq->osd, req);
3194922dab61SIlya Dryomov 	send_request(req);
3195922dab61SIlya Dryomov }
3196922dab61SIlya Dryomov 
3197922dab61SIlya Dryomov static void linger_submit(struct ceph_osd_linger_request *lreq)
3198922dab61SIlya Dryomov {
3199922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
3200922dab61SIlya Dryomov 	struct ceph_osd *osd;
3201922dab61SIlya Dryomov 
320281c65213SIlya Dryomov 	down_write(&osdc->lock);
320381c65213SIlya Dryomov 	linger_register(lreq);
320481c65213SIlya Dryomov 
32058edf84baSIlya Dryomov 	calc_target(osdc, &lreq->t, false);
3206922dab61SIlya Dryomov 	osd = lookup_create_osd(osdc, lreq->t.osd, true);
3207922dab61SIlya Dryomov 	link_linger(osd, lreq);
3208922dab61SIlya Dryomov 
3209922dab61SIlya Dryomov 	send_linger(lreq);
321081c65213SIlya Dryomov 	up_write(&osdc->lock);
3211922dab61SIlya Dryomov }
3212922dab61SIlya Dryomov 
32134609245eSIlya Dryomov static void cancel_linger_map_check(struct ceph_osd_linger_request *lreq)
32144609245eSIlya Dryomov {
32154609245eSIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
32164609245eSIlya Dryomov 	struct ceph_osd_linger_request *lookup_lreq;
32174609245eSIlya Dryomov 
32184609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
32194609245eSIlya Dryomov 
32204609245eSIlya Dryomov 	lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
32214609245eSIlya Dryomov 				       lreq->linger_id);
32224609245eSIlya Dryomov 	if (!lookup_lreq)
32234609245eSIlya Dryomov 		return;
32244609245eSIlya Dryomov 
32254609245eSIlya Dryomov 	WARN_ON(lookup_lreq != lreq);
32264609245eSIlya Dryomov 	erase_linger_mc(&osdc->linger_map_checks, lreq);
32274609245eSIlya Dryomov 	linger_put(lreq);
32284609245eSIlya Dryomov }
32294609245eSIlya Dryomov 
3230922dab61SIlya Dryomov /*
3231922dab61SIlya Dryomov  * @lreq has to be both registered and linked.
3232922dab61SIlya Dryomov  */
3233922dab61SIlya Dryomov static void __linger_cancel(struct ceph_osd_linger_request *lreq)
3234922dab61SIlya Dryomov {
323575dbb685SIlya Dryomov 	if (lreq->ping_req && lreq->ping_req->r_osd)
3236922dab61SIlya Dryomov 		cancel_linger_request(lreq->ping_req);
323775dbb685SIlya Dryomov 	if (lreq->reg_req && lreq->reg_req->r_osd)
3238922dab61SIlya Dryomov 		cancel_linger_request(lreq->reg_req);
32394609245eSIlya Dryomov 	cancel_linger_map_check(lreq);
3240922dab61SIlya Dryomov 	unlink_linger(lreq->osd, lreq);
3241922dab61SIlya Dryomov 	linger_unregister(lreq);
3242922dab61SIlya Dryomov }
3243922dab61SIlya Dryomov 
3244922dab61SIlya Dryomov static void linger_cancel(struct ceph_osd_linger_request *lreq)
3245922dab61SIlya Dryomov {
3246922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
3247922dab61SIlya Dryomov 
3248922dab61SIlya Dryomov 	down_write(&osdc->lock);
3249922dab61SIlya Dryomov 	if (__linger_registered(lreq))
3250922dab61SIlya Dryomov 		__linger_cancel(lreq);
3251922dab61SIlya Dryomov 	up_write(&osdc->lock);
3252922dab61SIlya Dryomov }
3253922dab61SIlya Dryomov 
32544609245eSIlya Dryomov static void send_linger_map_check(struct ceph_osd_linger_request *lreq);
32554609245eSIlya Dryomov 
32564609245eSIlya Dryomov static void check_linger_pool_dne(struct ceph_osd_linger_request *lreq)
32574609245eSIlya Dryomov {
32584609245eSIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
32594609245eSIlya Dryomov 	struct ceph_osdmap *map = osdc->osdmap;
32604609245eSIlya Dryomov 
32614609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
32624609245eSIlya Dryomov 	WARN_ON(!map->epoch);
32634609245eSIlya Dryomov 
32644609245eSIlya Dryomov 	if (lreq->register_gen) {
32654609245eSIlya Dryomov 		lreq->map_dne_bound = map->epoch;
32664609245eSIlya Dryomov 		dout("%s lreq %p linger_id %llu pool disappeared\n", __func__,
32674609245eSIlya Dryomov 		     lreq, lreq->linger_id);
32684609245eSIlya Dryomov 	} else {
32694609245eSIlya Dryomov 		dout("%s lreq %p linger_id %llu map_dne_bound %u have %u\n",
32704609245eSIlya Dryomov 		     __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
32714609245eSIlya Dryomov 		     map->epoch);
32724609245eSIlya Dryomov 	}
32734609245eSIlya Dryomov 
32744609245eSIlya Dryomov 	if (lreq->map_dne_bound) {
32754609245eSIlya Dryomov 		if (map->epoch >= lreq->map_dne_bound) {
32764609245eSIlya Dryomov 			/* we had a new enough map */
32774609245eSIlya Dryomov 			pr_info("linger_id %llu pool does not exist\n",
32784609245eSIlya Dryomov 				lreq->linger_id);
32794609245eSIlya Dryomov 			linger_reg_commit_complete(lreq, -ENOENT);
32804609245eSIlya Dryomov 			__linger_cancel(lreq);
32814609245eSIlya Dryomov 		}
32824609245eSIlya Dryomov 	} else {
32834609245eSIlya Dryomov 		send_linger_map_check(lreq);
32844609245eSIlya Dryomov 	}
32854609245eSIlya Dryomov }
32864609245eSIlya Dryomov 
32874609245eSIlya Dryomov static void linger_map_check_cb(struct ceph_mon_generic_request *greq)
32884609245eSIlya Dryomov {
32894609245eSIlya Dryomov 	struct ceph_osd_client *osdc = &greq->monc->client->osdc;
32904609245eSIlya Dryomov 	struct ceph_osd_linger_request *lreq;
32914609245eSIlya Dryomov 	u64 linger_id = greq->private_data;
32924609245eSIlya Dryomov 
32934609245eSIlya Dryomov 	WARN_ON(greq->result || !greq->u.newest);
32944609245eSIlya Dryomov 
32954609245eSIlya Dryomov 	down_write(&osdc->lock);
32964609245eSIlya Dryomov 	lreq = lookup_linger_mc(&osdc->linger_map_checks, linger_id);
32974609245eSIlya Dryomov 	if (!lreq) {
32984609245eSIlya Dryomov 		dout("%s linger_id %llu dne\n", __func__, linger_id);
32994609245eSIlya Dryomov 		goto out_unlock;
33004609245eSIlya Dryomov 	}
33014609245eSIlya Dryomov 
33024609245eSIlya Dryomov 	dout("%s lreq %p linger_id %llu map_dne_bound %u newest %llu\n",
33034609245eSIlya Dryomov 	     __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
33044609245eSIlya Dryomov 	     greq->u.newest);
33054609245eSIlya Dryomov 	if (!lreq->map_dne_bound)
33064609245eSIlya Dryomov 		lreq->map_dne_bound = greq->u.newest;
33074609245eSIlya Dryomov 	erase_linger_mc(&osdc->linger_map_checks, lreq);
33084609245eSIlya Dryomov 	check_linger_pool_dne(lreq);
33094609245eSIlya Dryomov 
33104609245eSIlya Dryomov 	linger_put(lreq);
33114609245eSIlya Dryomov out_unlock:
33124609245eSIlya Dryomov 	up_write(&osdc->lock);
33134609245eSIlya Dryomov }
33144609245eSIlya Dryomov 
33154609245eSIlya Dryomov static void send_linger_map_check(struct ceph_osd_linger_request *lreq)
33164609245eSIlya Dryomov {
33174609245eSIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
33184609245eSIlya Dryomov 	struct ceph_osd_linger_request *lookup_lreq;
33194609245eSIlya Dryomov 	int ret;
33204609245eSIlya Dryomov 
33214609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
33224609245eSIlya Dryomov 
33234609245eSIlya Dryomov 	lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
33244609245eSIlya Dryomov 				       lreq->linger_id);
33254609245eSIlya Dryomov 	if (lookup_lreq) {
33264609245eSIlya Dryomov 		WARN_ON(lookup_lreq != lreq);
33274609245eSIlya Dryomov 		return;
33284609245eSIlya Dryomov 	}
33294609245eSIlya Dryomov 
33304609245eSIlya Dryomov 	linger_get(lreq);
33314609245eSIlya Dryomov 	insert_linger_mc(&osdc->linger_map_checks, lreq);
33324609245eSIlya Dryomov 	ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
33334609245eSIlya Dryomov 					  linger_map_check_cb, lreq->linger_id);
33344609245eSIlya Dryomov 	WARN_ON(ret);
33354609245eSIlya Dryomov }
33364609245eSIlya Dryomov 
3337922dab61SIlya Dryomov static int linger_reg_commit_wait(struct ceph_osd_linger_request *lreq)
3338922dab61SIlya Dryomov {
3339922dab61SIlya Dryomov 	int ret;
3340922dab61SIlya Dryomov 
3341922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3342e6e28432SIlya Dryomov 	ret = wait_for_completion_killable(&lreq->reg_commit_wait);
3343922dab61SIlya Dryomov 	return ret ?: lreq->reg_commit_error;
3344922dab61SIlya Dryomov }
3345922dab61SIlya Dryomov 
3346e6e28432SIlya Dryomov static int linger_notify_finish_wait(struct ceph_osd_linger_request *lreq,
3347e6e28432SIlya Dryomov 				     unsigned long timeout)
334819079203SIlya Dryomov {
3349e6e28432SIlya Dryomov 	long left;
335019079203SIlya Dryomov 
335119079203SIlya Dryomov 	dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3352e6e28432SIlya Dryomov 	left = wait_for_completion_killable_timeout(&lreq->notify_finish_wait,
3353e6e28432SIlya Dryomov 						ceph_timeout_jiffies(timeout));
3354e6e28432SIlya Dryomov 	if (left <= 0)
3355e6e28432SIlya Dryomov 		left = left ?: -ETIMEDOUT;
3356e6e28432SIlya Dryomov 	else
3357e6e28432SIlya Dryomov 		left = lreq->notify_finish_error; /* completed */
3358e6e28432SIlya Dryomov 
3359e6e28432SIlya Dryomov 	return left;
336019079203SIlya Dryomov }
336119079203SIlya Dryomov 
3362922dab61SIlya Dryomov /*
3363fbca9635SIlya Dryomov  * Timeout callback, called every N seconds.  When 1 or more OSD
3364fbca9635SIlya Dryomov  * requests has been active for more than N seconds, we send a keepalive
3365fbca9635SIlya Dryomov  * (tag + timestamp) to its OSD to ensure any communications channel
3366fbca9635SIlya Dryomov  * reset is detected.
33673d14c5d2SYehuda Sadeh  */
33683d14c5d2SYehuda Sadeh static void handle_timeout(struct work_struct *work)
33693d14c5d2SYehuda Sadeh {
33703d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc =
33713d14c5d2SYehuda Sadeh 		container_of(work, struct ceph_osd_client, timeout_work.work);
3372a319bf56SIlya Dryomov 	struct ceph_options *opts = osdc->client->options;
33735aea3dcdSIlya Dryomov 	unsigned long cutoff = jiffies - opts->osd_keepalive_timeout;
33747cc5e38fSIlya Dryomov 	unsigned long expiry_cutoff = jiffies - opts->osd_request_timeout;
33755aea3dcdSIlya Dryomov 	LIST_HEAD(slow_osds);
33765aea3dcdSIlya Dryomov 	struct rb_node *n, *p;
33773d14c5d2SYehuda Sadeh 
33785aea3dcdSIlya Dryomov 	dout("%s osdc %p\n", __func__, osdc);
33795aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
33803d14c5d2SYehuda Sadeh 
33813d14c5d2SYehuda Sadeh 	/*
33823d14c5d2SYehuda Sadeh 	 * ping osds that are a bit slow.  this ensures that if there
33833d14c5d2SYehuda Sadeh 	 * is a break in the TCP connection we will notice, and reopen
33843d14c5d2SYehuda Sadeh 	 * a connection with that osd (from the fault callback).
33853d14c5d2SYehuda Sadeh 	 */
33865aea3dcdSIlya Dryomov 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
33875aea3dcdSIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
33885aea3dcdSIlya Dryomov 		bool found = false;
33893d14c5d2SYehuda Sadeh 
33907cc5e38fSIlya Dryomov 		for (p = rb_first(&osd->o_requests); p; ) {
33915aea3dcdSIlya Dryomov 			struct ceph_osd_request *req =
33925aea3dcdSIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
33935aea3dcdSIlya Dryomov 
33947cc5e38fSIlya Dryomov 			p = rb_next(p); /* abort_request() */
33957cc5e38fSIlya Dryomov 
33965aea3dcdSIlya Dryomov 			if (time_before(req->r_stamp, cutoff)) {
33975aea3dcdSIlya Dryomov 				dout(" req %p tid %llu on osd%d is laggy\n",
33985aea3dcdSIlya Dryomov 				     req, req->r_tid, osd->o_osd);
33995aea3dcdSIlya Dryomov 				found = true;
34005aea3dcdSIlya Dryomov 			}
34017cc5e38fSIlya Dryomov 			if (opts->osd_request_timeout &&
34027cc5e38fSIlya Dryomov 			    time_before(req->r_start_stamp, expiry_cutoff)) {
34037cc5e38fSIlya Dryomov 				pr_err_ratelimited("tid %llu on osd%d timeout\n",
34047cc5e38fSIlya Dryomov 				       req->r_tid, osd->o_osd);
34057cc5e38fSIlya Dryomov 				abort_request(req, -ETIMEDOUT);
34067cc5e38fSIlya Dryomov 			}
34075aea3dcdSIlya Dryomov 		}
3408922dab61SIlya Dryomov 		for (p = rb_first(&osd->o_linger_requests); p; p = rb_next(p)) {
3409922dab61SIlya Dryomov 			struct ceph_osd_linger_request *lreq =
3410922dab61SIlya Dryomov 			    rb_entry(p, struct ceph_osd_linger_request, node);
3411922dab61SIlya Dryomov 
3412922dab61SIlya Dryomov 			dout(" lreq %p linger_id %llu is served by osd%d\n",
3413922dab61SIlya Dryomov 			     lreq, lreq->linger_id, osd->o_osd);
3414922dab61SIlya Dryomov 			found = true;
3415922dab61SIlya Dryomov 
3416922dab61SIlya Dryomov 			mutex_lock(&lreq->lock);
341719079203SIlya Dryomov 			if (lreq->is_watch && lreq->committed && !lreq->last_error)
3418922dab61SIlya Dryomov 				send_linger_ping(lreq);
3419922dab61SIlya Dryomov 			mutex_unlock(&lreq->lock);
3420922dab61SIlya Dryomov 		}
34215aea3dcdSIlya Dryomov 
34225aea3dcdSIlya Dryomov 		if (found)
34233d14c5d2SYehuda Sadeh 			list_move_tail(&osd->o_keepalive_item, &slow_osds);
34243d14c5d2SYehuda Sadeh 	}
34255aea3dcdSIlya Dryomov 
34267cc5e38fSIlya Dryomov 	if (opts->osd_request_timeout) {
34277cc5e38fSIlya Dryomov 		for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
34287cc5e38fSIlya Dryomov 			struct ceph_osd_request *req =
34297cc5e38fSIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
34307cc5e38fSIlya Dryomov 
34317cc5e38fSIlya Dryomov 			p = rb_next(p); /* abort_request() */
34327cc5e38fSIlya Dryomov 
34337cc5e38fSIlya Dryomov 			if (time_before(req->r_start_stamp, expiry_cutoff)) {
34347cc5e38fSIlya Dryomov 				pr_err_ratelimited("tid %llu on osd%d timeout\n",
34357cc5e38fSIlya Dryomov 				       req->r_tid, osdc->homeless_osd.o_osd);
34367cc5e38fSIlya Dryomov 				abort_request(req, -ETIMEDOUT);
34377cc5e38fSIlya Dryomov 			}
34387cc5e38fSIlya Dryomov 		}
34397cc5e38fSIlya Dryomov 	}
34407cc5e38fSIlya Dryomov 
34415aea3dcdSIlya Dryomov 	if (atomic_read(&osdc->num_homeless) || !list_empty(&slow_osds))
34425aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
34435aea3dcdSIlya Dryomov 
34443d14c5d2SYehuda Sadeh 	while (!list_empty(&slow_osds)) {
34455aea3dcdSIlya Dryomov 		struct ceph_osd *osd = list_first_entry(&slow_osds,
34465aea3dcdSIlya Dryomov 							struct ceph_osd,
34473d14c5d2SYehuda Sadeh 							o_keepalive_item);
34483d14c5d2SYehuda Sadeh 		list_del_init(&osd->o_keepalive_item);
34493d14c5d2SYehuda Sadeh 		ceph_con_keepalive(&osd->o_con);
34503d14c5d2SYehuda Sadeh 	}
34513d14c5d2SYehuda Sadeh 
34525aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
3453fbca9635SIlya Dryomov 	schedule_delayed_work(&osdc->timeout_work,
3454fbca9635SIlya Dryomov 			      osdc->client->options->osd_keepalive_timeout);
34553d14c5d2SYehuda Sadeh }
34563d14c5d2SYehuda Sadeh 
34573d14c5d2SYehuda Sadeh static void handle_osds_timeout(struct work_struct *work)
34583d14c5d2SYehuda Sadeh {
34593d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc =
34603d14c5d2SYehuda Sadeh 		container_of(work, struct ceph_osd_client,
34613d14c5d2SYehuda Sadeh 			     osds_timeout_work.work);
3462a319bf56SIlya Dryomov 	unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
346342a2c09fSIlya Dryomov 	struct ceph_osd *osd, *nosd;
34643d14c5d2SYehuda Sadeh 
346542a2c09fSIlya Dryomov 	dout("%s osdc %p\n", __func__, osdc);
34665aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
346742a2c09fSIlya Dryomov 	list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
346842a2c09fSIlya Dryomov 		if (time_before(jiffies, osd->lru_ttl))
346942a2c09fSIlya Dryomov 			break;
347042a2c09fSIlya Dryomov 
34715aea3dcdSIlya Dryomov 		WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
3472922dab61SIlya Dryomov 		WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
34735aea3dcdSIlya Dryomov 		close_osd(osd);
347442a2c09fSIlya Dryomov 	}
347542a2c09fSIlya Dryomov 
34765aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
34773d14c5d2SYehuda Sadeh 	schedule_delayed_work(&osdc->osds_timeout_work,
34783d14c5d2SYehuda Sadeh 			      round_jiffies_relative(delay));
34793d14c5d2SYehuda Sadeh }
34803d14c5d2SYehuda Sadeh 
3481205ee118SIlya Dryomov static int ceph_oloc_decode(void **p, void *end,
3482205ee118SIlya Dryomov 			    struct ceph_object_locator *oloc)
3483205ee118SIlya Dryomov {
3484205ee118SIlya Dryomov 	u8 struct_v, struct_cv;
3485205ee118SIlya Dryomov 	u32 len;
3486205ee118SIlya Dryomov 	void *struct_end;
3487205ee118SIlya Dryomov 	int ret = 0;
3488205ee118SIlya Dryomov 
3489205ee118SIlya Dryomov 	ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3490205ee118SIlya Dryomov 	struct_v = ceph_decode_8(p);
3491205ee118SIlya Dryomov 	struct_cv = ceph_decode_8(p);
3492205ee118SIlya Dryomov 	if (struct_v < 3) {
3493205ee118SIlya Dryomov 		pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
3494205ee118SIlya Dryomov 			struct_v, struct_cv);
3495205ee118SIlya Dryomov 		goto e_inval;
3496205ee118SIlya Dryomov 	}
3497205ee118SIlya Dryomov 	if (struct_cv > 6) {
3498205ee118SIlya Dryomov 		pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
3499205ee118SIlya Dryomov 			struct_v, struct_cv);
3500205ee118SIlya Dryomov 		goto e_inval;
3501205ee118SIlya Dryomov 	}
3502205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3503205ee118SIlya Dryomov 	ceph_decode_need(p, end, len, e_inval);
3504205ee118SIlya Dryomov 	struct_end = *p + len;
3505205ee118SIlya Dryomov 
3506205ee118SIlya Dryomov 	oloc->pool = ceph_decode_64(p);
3507205ee118SIlya Dryomov 	*p += 4; /* skip preferred */
3508205ee118SIlya Dryomov 
3509205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3510205ee118SIlya Dryomov 	if (len > 0) {
3511205ee118SIlya Dryomov 		pr_warn("ceph_object_locator::key is set\n");
3512205ee118SIlya Dryomov 		goto e_inval;
3513205ee118SIlya Dryomov 	}
3514205ee118SIlya Dryomov 
3515205ee118SIlya Dryomov 	if (struct_v >= 5) {
3516cd08e0a2SYan, Zheng 		bool changed = false;
3517cd08e0a2SYan, Zheng 
3518205ee118SIlya Dryomov 		len = ceph_decode_32(p);
3519205ee118SIlya Dryomov 		if (len > 0) {
352030c156d9SYan, Zheng 			ceph_decode_need(p, end, len, e_inval);
3521cd08e0a2SYan, Zheng 			if (!oloc->pool_ns ||
3522cd08e0a2SYan, Zheng 			    ceph_compare_string(oloc->pool_ns, *p, len))
3523cd08e0a2SYan, Zheng 				changed = true;
352430c156d9SYan, Zheng 			*p += len;
3525cd08e0a2SYan, Zheng 		} else {
3526cd08e0a2SYan, Zheng 			if (oloc->pool_ns)
3527cd08e0a2SYan, Zheng 				changed = true;
3528cd08e0a2SYan, Zheng 		}
3529cd08e0a2SYan, Zheng 		if (changed) {
3530cd08e0a2SYan, Zheng 			/* redirect changes namespace */
3531cd08e0a2SYan, Zheng 			pr_warn("ceph_object_locator::nspace is changed\n");
3532cd08e0a2SYan, Zheng 			goto e_inval;
3533205ee118SIlya Dryomov 		}
3534205ee118SIlya Dryomov 	}
3535205ee118SIlya Dryomov 
3536205ee118SIlya Dryomov 	if (struct_v >= 6) {
3537205ee118SIlya Dryomov 		s64 hash = ceph_decode_64(p);
3538205ee118SIlya Dryomov 		if (hash != -1) {
3539205ee118SIlya Dryomov 			pr_warn("ceph_object_locator::hash is set\n");
3540205ee118SIlya Dryomov 			goto e_inval;
3541205ee118SIlya Dryomov 		}
3542205ee118SIlya Dryomov 	}
3543205ee118SIlya Dryomov 
3544205ee118SIlya Dryomov 	/* skip the rest */
3545205ee118SIlya Dryomov 	*p = struct_end;
3546205ee118SIlya Dryomov out:
3547205ee118SIlya Dryomov 	return ret;
3548205ee118SIlya Dryomov 
3549205ee118SIlya Dryomov e_inval:
3550205ee118SIlya Dryomov 	ret = -EINVAL;
3551205ee118SIlya Dryomov 	goto out;
3552205ee118SIlya Dryomov }
3553205ee118SIlya Dryomov 
3554205ee118SIlya Dryomov static int ceph_redirect_decode(void **p, void *end,
3555205ee118SIlya Dryomov 				struct ceph_request_redirect *redir)
3556205ee118SIlya Dryomov {
3557205ee118SIlya Dryomov 	u8 struct_v, struct_cv;
3558205ee118SIlya Dryomov 	u32 len;
3559205ee118SIlya Dryomov 	void *struct_end;
3560205ee118SIlya Dryomov 	int ret;
3561205ee118SIlya Dryomov 
3562205ee118SIlya Dryomov 	ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3563205ee118SIlya Dryomov 	struct_v = ceph_decode_8(p);
3564205ee118SIlya Dryomov 	struct_cv = ceph_decode_8(p);
3565205ee118SIlya Dryomov 	if (struct_cv > 1) {
3566205ee118SIlya Dryomov 		pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
3567205ee118SIlya Dryomov 			struct_v, struct_cv);
3568205ee118SIlya Dryomov 		goto e_inval;
3569205ee118SIlya Dryomov 	}
3570205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3571205ee118SIlya Dryomov 	ceph_decode_need(p, end, len, e_inval);
3572205ee118SIlya Dryomov 	struct_end = *p + len;
3573205ee118SIlya Dryomov 
3574205ee118SIlya Dryomov 	ret = ceph_oloc_decode(p, end, &redir->oloc);
3575205ee118SIlya Dryomov 	if (ret)
3576205ee118SIlya Dryomov 		goto out;
3577205ee118SIlya Dryomov 
3578205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3579205ee118SIlya Dryomov 	if (len > 0) {
3580205ee118SIlya Dryomov 		pr_warn("ceph_request_redirect::object_name is set\n");
3581205ee118SIlya Dryomov 		goto e_inval;
3582205ee118SIlya Dryomov 	}
3583205ee118SIlya Dryomov 
3584205ee118SIlya Dryomov 	/* skip the rest */
3585205ee118SIlya Dryomov 	*p = struct_end;
3586205ee118SIlya Dryomov out:
3587205ee118SIlya Dryomov 	return ret;
3588205ee118SIlya Dryomov 
3589205ee118SIlya Dryomov e_inval:
3590205ee118SIlya Dryomov 	ret = -EINVAL;
3591205ee118SIlya Dryomov 	goto out;
3592205ee118SIlya Dryomov }
3593205ee118SIlya Dryomov 
3594fe5da05eSIlya Dryomov struct MOSDOpReply {
3595fe5da05eSIlya Dryomov 	struct ceph_pg pgid;
3596fe5da05eSIlya Dryomov 	u64 flags;
3597fe5da05eSIlya Dryomov 	int result;
3598fe5da05eSIlya Dryomov 	u32 epoch;
3599fe5da05eSIlya Dryomov 	int num_ops;
3600fe5da05eSIlya Dryomov 	u32 outdata_len[CEPH_OSD_MAX_OPS];
3601fe5da05eSIlya Dryomov 	s32 rval[CEPH_OSD_MAX_OPS];
3602fe5da05eSIlya Dryomov 	int retry_attempt;
3603fe5da05eSIlya Dryomov 	struct ceph_eversion replay_version;
3604fe5da05eSIlya Dryomov 	u64 user_version;
3605fe5da05eSIlya Dryomov 	struct ceph_request_redirect redirect;
3606fe5da05eSIlya Dryomov };
360725845472SSage Weil 
3608fe5da05eSIlya Dryomov static int decode_MOSDOpReply(const struct ceph_msg *msg, struct MOSDOpReply *m)
36093d14c5d2SYehuda Sadeh {
3610fe5da05eSIlya Dryomov 	void *p = msg->front.iov_base;
3611fe5da05eSIlya Dryomov 	void *const end = p + msg->front.iov_len;
3612fe5da05eSIlya Dryomov 	u16 version = le16_to_cpu(msg->hdr.version);
3613fe5da05eSIlya Dryomov 	struct ceph_eversion bad_replay_version;
3614b0b31a8fSIlya Dryomov 	u8 decode_redir;
3615fe5da05eSIlya Dryomov 	u32 len;
3616fe5da05eSIlya Dryomov 	int ret;
3617fe5da05eSIlya Dryomov 	int i;
36183d14c5d2SYehuda Sadeh 
3619fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, len, e_inval);
3620fe5da05eSIlya Dryomov 	ceph_decode_need(&p, end, len, e_inval);
3621fe5da05eSIlya Dryomov 	p += len; /* skip oid */
36221b83bef2SSage Weil 
3623fe5da05eSIlya Dryomov 	ret = ceph_decode_pgid(&p, end, &m->pgid);
3624fe5da05eSIlya Dryomov 	if (ret)
3625fe5da05eSIlya Dryomov 		return ret;
36261b83bef2SSage Weil 
3627fe5da05eSIlya Dryomov 	ceph_decode_64_safe(&p, end, m->flags, e_inval);
3628fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->result, e_inval);
3629fe5da05eSIlya Dryomov 	ceph_decode_need(&p, end, sizeof(bad_replay_version), e_inval);
3630fe5da05eSIlya Dryomov 	memcpy(&bad_replay_version, p, sizeof(bad_replay_version));
3631fe5da05eSIlya Dryomov 	p += sizeof(bad_replay_version);
3632fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->epoch, e_inval);
36331b83bef2SSage Weil 
3634fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->num_ops, e_inval);
3635fe5da05eSIlya Dryomov 	if (m->num_ops > ARRAY_SIZE(m->outdata_len))
3636fe5da05eSIlya Dryomov 		goto e_inval;
36371b83bef2SSage Weil 
3638fe5da05eSIlya Dryomov 	ceph_decode_need(&p, end, m->num_ops * sizeof(struct ceph_osd_op),
3639fe5da05eSIlya Dryomov 			 e_inval);
3640fe5da05eSIlya Dryomov 	for (i = 0; i < m->num_ops; i++) {
36411b83bef2SSage Weil 		struct ceph_osd_op *op = p;
36421b83bef2SSage Weil 
3643fe5da05eSIlya Dryomov 		m->outdata_len[i] = le32_to_cpu(op->payload_len);
36441b83bef2SSage Weil 		p += sizeof(*op);
36451b83bef2SSage Weil 	}
3646fe5da05eSIlya Dryomov 
3647fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->retry_attempt, e_inval);
3648fe5da05eSIlya Dryomov 	for (i = 0; i < m->num_ops; i++)
3649fe5da05eSIlya Dryomov 		ceph_decode_32_safe(&p, end, m->rval[i], e_inval);
3650fe5da05eSIlya Dryomov 
3651fe5da05eSIlya Dryomov 	if (version >= 5) {
3652fe5da05eSIlya Dryomov 		ceph_decode_need(&p, end, sizeof(m->replay_version), e_inval);
3653fe5da05eSIlya Dryomov 		memcpy(&m->replay_version, p, sizeof(m->replay_version));
3654fe5da05eSIlya Dryomov 		p += sizeof(m->replay_version);
3655fe5da05eSIlya Dryomov 		ceph_decode_64_safe(&p, end, m->user_version, e_inval);
3656fe5da05eSIlya Dryomov 	} else {
3657fe5da05eSIlya Dryomov 		m->replay_version = bad_replay_version; /* struct */
3658fe5da05eSIlya Dryomov 		m->user_version = le64_to_cpu(m->replay_version.version);
36591b83bef2SSage Weil 	}
36601b83bef2SSage Weil 
3661fe5da05eSIlya Dryomov 	if (version >= 6) {
3662fe5da05eSIlya Dryomov 		if (version >= 7)
3663fe5da05eSIlya Dryomov 			ceph_decode_8_safe(&p, end, decode_redir, e_inval);
3664b0b31a8fSIlya Dryomov 		else
3665b0b31a8fSIlya Dryomov 			decode_redir = 1;
3666b0b31a8fSIlya Dryomov 	} else {
3667b0b31a8fSIlya Dryomov 		decode_redir = 0;
3668b0b31a8fSIlya Dryomov 	}
3669b0b31a8fSIlya Dryomov 
3670b0b31a8fSIlya Dryomov 	if (decode_redir) {
3671fe5da05eSIlya Dryomov 		ret = ceph_redirect_decode(&p, end, &m->redirect);
3672fe5da05eSIlya Dryomov 		if (ret)
3673fe5da05eSIlya Dryomov 			return ret;
3674205ee118SIlya Dryomov 	} else {
3675fe5da05eSIlya Dryomov 		ceph_oloc_init(&m->redirect.oloc);
3676205ee118SIlya Dryomov 	}
3677205ee118SIlya Dryomov 
3678fe5da05eSIlya Dryomov 	return 0;
3679205ee118SIlya Dryomov 
3680fe5da05eSIlya Dryomov e_inval:
3681fe5da05eSIlya Dryomov 	return -EINVAL;
3682fe5da05eSIlya Dryomov }
3683fe5da05eSIlya Dryomov 
3684fe5da05eSIlya Dryomov /*
3685b18b9550SIlya Dryomov  * Handle MOSDOpReply.  Set ->r_result and call the callback if it is
3686b18b9550SIlya Dryomov  * specified.
3687fe5da05eSIlya Dryomov  */
36885aea3dcdSIlya Dryomov static void handle_reply(struct ceph_osd *osd, struct ceph_msg *msg)
3689fe5da05eSIlya Dryomov {
36905aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
3691fe5da05eSIlya Dryomov 	struct ceph_osd_request *req;
3692fe5da05eSIlya Dryomov 	struct MOSDOpReply m;
3693fe5da05eSIlya Dryomov 	u64 tid = le64_to_cpu(msg->hdr.tid);
3694fe5da05eSIlya Dryomov 	u32 data_len = 0;
3695fe5da05eSIlya Dryomov 	int ret;
3696fe5da05eSIlya Dryomov 	int i;
3697fe5da05eSIlya Dryomov 
3698fe5da05eSIlya Dryomov 	dout("%s msg %p tid %llu\n", __func__, msg, tid);
3699fe5da05eSIlya Dryomov 
37005aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
37015aea3dcdSIlya Dryomov 	if (!osd_registered(osd)) {
37025aea3dcdSIlya Dryomov 		dout("%s osd%d unknown\n", __func__, osd->o_osd);
37035aea3dcdSIlya Dryomov 		goto out_unlock_osdc;
3704fe5da05eSIlya Dryomov 	}
37055aea3dcdSIlya Dryomov 	WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
37065aea3dcdSIlya Dryomov 
37075aea3dcdSIlya Dryomov 	mutex_lock(&osd->lock);
37085aea3dcdSIlya Dryomov 	req = lookup_request(&osd->o_requests, tid);
37095aea3dcdSIlya Dryomov 	if (!req) {
37105aea3dcdSIlya Dryomov 		dout("%s osd%d tid %llu unknown\n", __func__, osd->o_osd, tid);
37115aea3dcdSIlya Dryomov 		goto out_unlock_session;
37125aea3dcdSIlya Dryomov 	}
3713fe5da05eSIlya Dryomov 
3714cd08e0a2SYan, Zheng 	m.redirect.oloc.pool_ns = req->r_t.target_oloc.pool_ns;
3715fe5da05eSIlya Dryomov 	ret = decode_MOSDOpReply(msg, &m);
3716cd08e0a2SYan, Zheng 	m.redirect.oloc.pool_ns = NULL;
3717fe5da05eSIlya Dryomov 	if (ret) {
3718fe5da05eSIlya Dryomov 		pr_err("failed to decode MOSDOpReply for tid %llu: %d\n",
3719fe5da05eSIlya Dryomov 		       req->r_tid, ret);
3720fe5da05eSIlya Dryomov 		ceph_msg_dump(msg);
3721fe5da05eSIlya Dryomov 		goto fail_request;
3722fe5da05eSIlya Dryomov 	}
3723fe5da05eSIlya Dryomov 	dout("%s req %p tid %llu flags 0x%llx pgid %llu.%x epoch %u attempt %d v %u'%llu uv %llu\n",
3724fe5da05eSIlya Dryomov 	     __func__, req, req->r_tid, m.flags, m.pgid.pool, m.pgid.seed,
3725fe5da05eSIlya Dryomov 	     m.epoch, m.retry_attempt, le32_to_cpu(m.replay_version.epoch),
3726fe5da05eSIlya Dryomov 	     le64_to_cpu(m.replay_version.version), m.user_version);
3727fe5da05eSIlya Dryomov 
3728fe5da05eSIlya Dryomov 	if (m.retry_attempt >= 0) {
3729fe5da05eSIlya Dryomov 		if (m.retry_attempt != req->r_attempts - 1) {
3730fe5da05eSIlya Dryomov 			dout("req %p tid %llu retry_attempt %d != %d, ignoring\n",
3731fe5da05eSIlya Dryomov 			     req, req->r_tid, m.retry_attempt,
3732fe5da05eSIlya Dryomov 			     req->r_attempts - 1);
37335aea3dcdSIlya Dryomov 			goto out_unlock_session;
3734fe5da05eSIlya Dryomov 		}
3735fe5da05eSIlya Dryomov 	} else {
3736fe5da05eSIlya Dryomov 		WARN_ON(1); /* MOSDOpReply v4 is assumed */
3737fe5da05eSIlya Dryomov 	}
3738fe5da05eSIlya Dryomov 
3739fe5da05eSIlya Dryomov 	if (!ceph_oloc_empty(&m.redirect.oloc)) {
3740fe5da05eSIlya Dryomov 		dout("req %p tid %llu redirect pool %lld\n", req, req->r_tid,
3741fe5da05eSIlya Dryomov 		     m.redirect.oloc.pool);
37425aea3dcdSIlya Dryomov 		unlink_request(osd, req);
37435aea3dcdSIlya Dryomov 		mutex_unlock(&osd->lock);
3744205ee118SIlya Dryomov 
374530c156d9SYan, Zheng 		/*
374630c156d9SYan, Zheng 		 * Not ceph_oloc_copy() - changing pool_ns is not
374730c156d9SYan, Zheng 		 * supported.
374830c156d9SYan, Zheng 		 */
374930c156d9SYan, Zheng 		req->r_t.target_oloc.pool = m.redirect.oloc.pool;
3750890bd0f8SJerry Lee 		req->r_flags |= CEPH_OSD_FLAG_REDIRECTED |
3751890bd0f8SJerry Lee 				CEPH_OSD_FLAG_IGNORE_OVERLAY |
3752890bd0f8SJerry Lee 				CEPH_OSD_FLAG_IGNORE_CACHE;
37535aea3dcdSIlya Dryomov 		req->r_tid = 0;
37545aea3dcdSIlya Dryomov 		__submit_request(req, false);
37555aea3dcdSIlya Dryomov 		goto out_unlock_osdc;
3756205ee118SIlya Dryomov 	}
3757205ee118SIlya Dryomov 
3758117d96a0SIlya Dryomov 	if (m.result == -EAGAIN) {
3759117d96a0SIlya Dryomov 		dout("req %p tid %llu EAGAIN\n", req, req->r_tid);
3760117d96a0SIlya Dryomov 		unlink_request(osd, req);
3761117d96a0SIlya Dryomov 		mutex_unlock(&osd->lock);
3762117d96a0SIlya Dryomov 
3763117d96a0SIlya Dryomov 		/*
3764117d96a0SIlya Dryomov 		 * The object is missing on the replica or not (yet)
3765117d96a0SIlya Dryomov 		 * readable.  Clear pgid to force a resend to the primary
3766117d96a0SIlya Dryomov 		 * via legacy_change.
3767117d96a0SIlya Dryomov 		 */
3768117d96a0SIlya Dryomov 		req->r_t.pgid.pool = 0;
3769117d96a0SIlya Dryomov 		req->r_t.pgid.seed = 0;
3770117d96a0SIlya Dryomov 		WARN_ON(!req->r_t.used_replica);
3771117d96a0SIlya Dryomov 		req->r_flags &= ~(CEPH_OSD_FLAG_BALANCE_READS |
3772117d96a0SIlya Dryomov 				  CEPH_OSD_FLAG_LOCALIZE_READS);
3773117d96a0SIlya Dryomov 		req->r_tid = 0;
3774117d96a0SIlya Dryomov 		__submit_request(req, false);
3775117d96a0SIlya Dryomov 		goto out_unlock_osdc;
3776117d96a0SIlya Dryomov 	}
3777117d96a0SIlya Dryomov 
3778fe5da05eSIlya Dryomov 	if (m.num_ops != req->r_num_ops) {
3779fe5da05eSIlya Dryomov 		pr_err("num_ops %d != %d for tid %llu\n", m.num_ops,
3780fe5da05eSIlya Dryomov 		       req->r_num_ops, req->r_tid);
3781fe5da05eSIlya Dryomov 		goto fail_request;
3782fe5da05eSIlya Dryomov 	}
3783fe5da05eSIlya Dryomov 	for (i = 0; i < req->r_num_ops; i++) {
3784fe5da05eSIlya Dryomov 		dout(" req %p tid %llu op %d rval %d len %u\n", req,
3785fe5da05eSIlya Dryomov 		     req->r_tid, i, m.rval[i], m.outdata_len[i]);
3786fe5da05eSIlya Dryomov 		req->r_ops[i].rval = m.rval[i];
3787fe5da05eSIlya Dryomov 		req->r_ops[i].outdata_len = m.outdata_len[i];
3788fe5da05eSIlya Dryomov 		data_len += m.outdata_len[i];
3789fe5da05eSIlya Dryomov 	}
3790fe5da05eSIlya Dryomov 	if (data_len != le32_to_cpu(msg->hdr.data_len)) {
3791fe5da05eSIlya Dryomov 		pr_err("sum of lens %u != %u for tid %llu\n", data_len,
3792fe5da05eSIlya Dryomov 		       le32_to_cpu(msg->hdr.data_len), req->r_tid);
3793fe5da05eSIlya Dryomov 		goto fail_request;
3794fe5da05eSIlya Dryomov 	}
3795b18b9550SIlya Dryomov 	dout("%s req %p tid %llu result %d data_len %u\n", __func__,
3796b18b9550SIlya Dryomov 	     req, req->r_tid, m.result, data_len);
37973d14c5d2SYehuda Sadeh 
3798b18b9550SIlya Dryomov 	/*
3799b18b9550SIlya Dryomov 	 * Since we only ever request ONDISK, we should only ever get
3800b18b9550SIlya Dryomov 	 * one (type of) reply back.
3801b18b9550SIlya Dryomov 	 */
3802b18b9550SIlya Dryomov 	WARN_ON(!(m.flags & CEPH_OSD_FLAG_ONDISK));
3803fe5da05eSIlya Dryomov 	req->r_result = m.result ?: data_len;
380445ee2c1dSIlya Dryomov 	finish_request(req);
38055aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
38065aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
38073d14c5d2SYehuda Sadeh 
3808fe5da05eSIlya Dryomov 	__complete_request(req);
38093d14c5d2SYehuda Sadeh 	return;
3810fe5da05eSIlya Dryomov 
3811fe5da05eSIlya Dryomov fail_request:
38124609245eSIlya Dryomov 	complete_request(req, -EIO);
38135aea3dcdSIlya Dryomov out_unlock_session:
38145aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
38155aea3dcdSIlya Dryomov out_unlock_osdc:
38165aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
38173d14c5d2SYehuda Sadeh }
38183d14c5d2SYehuda Sadeh 
381942c1b124SIlya Dryomov static void set_pool_was_full(struct ceph_osd_client *osdc)
382042c1b124SIlya Dryomov {
382142c1b124SIlya Dryomov 	struct rb_node *n;
382242c1b124SIlya Dryomov 
382342c1b124SIlya Dryomov 	for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
382442c1b124SIlya Dryomov 		struct ceph_pg_pool_info *pi =
382542c1b124SIlya Dryomov 		    rb_entry(n, struct ceph_pg_pool_info, node);
382642c1b124SIlya Dryomov 
382742c1b124SIlya Dryomov 		pi->was_full = __pool_full(pi);
382842c1b124SIlya Dryomov 	}
382942c1b124SIlya Dryomov }
383042c1b124SIlya Dryomov 
38315aea3dcdSIlya Dryomov static bool pool_cleared_full(struct ceph_osd_client *osdc, s64 pool_id)
38323d14c5d2SYehuda Sadeh {
38335aea3dcdSIlya Dryomov 	struct ceph_pg_pool_info *pi;
38343d14c5d2SYehuda Sadeh 
38355aea3dcdSIlya Dryomov 	pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
38365aea3dcdSIlya Dryomov 	if (!pi)
38375aea3dcdSIlya Dryomov 		return false;
38383d14c5d2SYehuda Sadeh 
38395aea3dcdSIlya Dryomov 	return pi->was_full && !__pool_full(pi);
38403d14c5d2SYehuda Sadeh }
38413d14c5d2SYehuda Sadeh 
3842922dab61SIlya Dryomov static enum calc_target_result
3843922dab61SIlya Dryomov recalc_linger_target(struct ceph_osd_linger_request *lreq)
3844922dab61SIlya Dryomov {
3845922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
3846922dab61SIlya Dryomov 	enum calc_target_result ct_res;
3847922dab61SIlya Dryomov 
38488edf84baSIlya Dryomov 	ct_res = calc_target(osdc, &lreq->t, true);
3849922dab61SIlya Dryomov 	if (ct_res == CALC_TARGET_NEED_RESEND) {
3850922dab61SIlya Dryomov 		struct ceph_osd *osd;
3851922dab61SIlya Dryomov 
3852922dab61SIlya Dryomov 		osd = lookup_create_osd(osdc, lreq->t.osd, true);
3853922dab61SIlya Dryomov 		if (osd != lreq->osd) {
3854922dab61SIlya Dryomov 			unlink_linger(lreq->osd, lreq);
3855922dab61SIlya Dryomov 			link_linger(osd, lreq);
3856922dab61SIlya Dryomov 		}
3857922dab61SIlya Dryomov 	}
3858922dab61SIlya Dryomov 
3859922dab61SIlya Dryomov 	return ct_res;
3860922dab61SIlya Dryomov }
3861922dab61SIlya Dryomov 
38623d14c5d2SYehuda Sadeh /*
38635aea3dcdSIlya Dryomov  * Requeue requests whose mapping to an OSD has changed.
38643d14c5d2SYehuda Sadeh  */
38655aea3dcdSIlya Dryomov static void scan_requests(struct ceph_osd *osd,
38665aea3dcdSIlya Dryomov 			  bool force_resend,
38675aea3dcdSIlya Dryomov 			  bool cleared_full,
38685aea3dcdSIlya Dryomov 			  bool check_pool_cleared_full,
38695aea3dcdSIlya Dryomov 			  struct rb_root *need_resend,
38705aea3dcdSIlya Dryomov 			  struct list_head *need_resend_linger)
38713d14c5d2SYehuda Sadeh {
38725aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
38735aea3dcdSIlya Dryomov 	struct rb_node *n;
38745aea3dcdSIlya Dryomov 	bool force_resend_writes;
38753d14c5d2SYehuda Sadeh 
3876922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_linger_requests); n; ) {
3877922dab61SIlya Dryomov 		struct ceph_osd_linger_request *lreq =
3878922dab61SIlya Dryomov 		    rb_entry(n, struct ceph_osd_linger_request, node);
3879922dab61SIlya Dryomov 		enum calc_target_result ct_res;
3880922dab61SIlya Dryomov 
3881922dab61SIlya Dryomov 		n = rb_next(n); /* recalc_linger_target() */
3882922dab61SIlya Dryomov 
3883922dab61SIlya Dryomov 		dout("%s lreq %p linger_id %llu\n", __func__, lreq,
3884922dab61SIlya Dryomov 		     lreq->linger_id);
3885922dab61SIlya Dryomov 		ct_res = recalc_linger_target(lreq);
3886922dab61SIlya Dryomov 		switch (ct_res) {
3887922dab61SIlya Dryomov 		case CALC_TARGET_NO_ACTION:
3888922dab61SIlya Dryomov 			force_resend_writes = cleared_full ||
3889922dab61SIlya Dryomov 			    (check_pool_cleared_full &&
3890922dab61SIlya Dryomov 			     pool_cleared_full(osdc, lreq->t.base_oloc.pool));
3891922dab61SIlya Dryomov 			if (!force_resend && !force_resend_writes)
3892922dab61SIlya Dryomov 				break;
3893922dab61SIlya Dryomov 
3894df561f66SGustavo A. R. Silva 			fallthrough;
3895922dab61SIlya Dryomov 		case CALC_TARGET_NEED_RESEND:
38964609245eSIlya Dryomov 			cancel_linger_map_check(lreq);
3897922dab61SIlya Dryomov 			/*
3898922dab61SIlya Dryomov 			 * scan_requests() for the previous epoch(s)
3899922dab61SIlya Dryomov 			 * may have already added it to the list, since
3900922dab61SIlya Dryomov 			 * it's not unlinked here.
3901922dab61SIlya Dryomov 			 */
3902922dab61SIlya Dryomov 			if (list_empty(&lreq->scan_item))
3903922dab61SIlya Dryomov 				list_add_tail(&lreq->scan_item, need_resend_linger);
3904922dab61SIlya Dryomov 			break;
3905922dab61SIlya Dryomov 		case CALC_TARGET_POOL_DNE:
3906a10bcb19SIlya Dryomov 			list_del_init(&lreq->scan_item);
39074609245eSIlya Dryomov 			check_linger_pool_dne(lreq);
3908922dab61SIlya Dryomov 			break;
3909922dab61SIlya Dryomov 		}
3910922dab61SIlya Dryomov 	}
3911922dab61SIlya Dryomov 
39125aea3dcdSIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; ) {
39135aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
39145aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
39155aea3dcdSIlya Dryomov 		enum calc_target_result ct_res;
3916ab60b16dSAlex Elder 
39174609245eSIlya Dryomov 		n = rb_next(n); /* unlink_request(), check_pool_dne() */
3918ab60b16dSAlex Elder 
39195aea3dcdSIlya Dryomov 		dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
39208edf84baSIlya Dryomov 		ct_res = calc_target(osdc, &req->r_t, false);
39215aea3dcdSIlya Dryomov 		switch (ct_res) {
39225aea3dcdSIlya Dryomov 		case CALC_TARGET_NO_ACTION:
39235aea3dcdSIlya Dryomov 			force_resend_writes = cleared_full ||
39245aea3dcdSIlya Dryomov 			    (check_pool_cleared_full &&
39255aea3dcdSIlya Dryomov 			     pool_cleared_full(osdc, req->r_t.base_oloc.pool));
39265aea3dcdSIlya Dryomov 			if (!force_resend &&
39275aea3dcdSIlya Dryomov 			    (!(req->r_flags & CEPH_OSD_FLAG_WRITE) ||
39285aea3dcdSIlya Dryomov 			     !force_resend_writes))
39295aea3dcdSIlya Dryomov 				break;
3930a40c4f10SYehuda Sadeh 
3931df561f66SGustavo A. R. Silva 			fallthrough;
39325aea3dcdSIlya Dryomov 		case CALC_TARGET_NEED_RESEND:
39334609245eSIlya Dryomov 			cancel_map_check(req);
39345aea3dcdSIlya Dryomov 			unlink_request(osd, req);
39355aea3dcdSIlya Dryomov 			insert_request(need_resend, req);
39365aea3dcdSIlya Dryomov 			break;
39375aea3dcdSIlya Dryomov 		case CALC_TARGET_POOL_DNE:
39384609245eSIlya Dryomov 			check_pool_dne(req);
39395aea3dcdSIlya Dryomov 			break;
3940a40c4f10SYehuda Sadeh 		}
39413d14c5d2SYehuda Sadeh 	}
39423d14c5d2SYehuda Sadeh }
39436f6c7006SSage Weil 
394442c1b124SIlya Dryomov static int handle_one_map(struct ceph_osd_client *osdc,
39455aea3dcdSIlya Dryomov 			  void *p, void *end, bool incremental,
39465aea3dcdSIlya Dryomov 			  struct rb_root *need_resend,
39475aea3dcdSIlya Dryomov 			  struct list_head *need_resend_linger)
394842c1b124SIlya Dryomov {
394942c1b124SIlya Dryomov 	struct ceph_osdmap *newmap;
395042c1b124SIlya Dryomov 	struct rb_node *n;
395142c1b124SIlya Dryomov 	bool skipped_map = false;
395242c1b124SIlya Dryomov 	bool was_full;
395342c1b124SIlya Dryomov 
3954b7ec35b3SIlya Dryomov 	was_full = ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
395542c1b124SIlya Dryomov 	set_pool_was_full(osdc);
395642c1b124SIlya Dryomov 
395742c1b124SIlya Dryomov 	if (incremental)
3958cd1a677cSIlya Dryomov 		newmap = osdmap_apply_incremental(&p, end,
3959cd1a677cSIlya Dryomov 						  ceph_msgr2(osdc->client),
3960cd1a677cSIlya Dryomov 						  osdc->osdmap);
396142c1b124SIlya Dryomov 	else
3962cd1a677cSIlya Dryomov 		newmap = ceph_osdmap_decode(&p, end, ceph_msgr2(osdc->client));
396342c1b124SIlya Dryomov 	if (IS_ERR(newmap))
396442c1b124SIlya Dryomov 		return PTR_ERR(newmap);
396542c1b124SIlya Dryomov 
396642c1b124SIlya Dryomov 	if (newmap != osdc->osdmap) {
396742c1b124SIlya Dryomov 		/*
396842c1b124SIlya Dryomov 		 * Preserve ->was_full before destroying the old map.
396942c1b124SIlya Dryomov 		 * For pools that weren't in the old map, ->was_full
397042c1b124SIlya Dryomov 		 * should be false.
397142c1b124SIlya Dryomov 		 */
397242c1b124SIlya Dryomov 		for (n = rb_first(&newmap->pg_pools); n; n = rb_next(n)) {
397342c1b124SIlya Dryomov 			struct ceph_pg_pool_info *pi =
397442c1b124SIlya Dryomov 			    rb_entry(n, struct ceph_pg_pool_info, node);
397542c1b124SIlya Dryomov 			struct ceph_pg_pool_info *old_pi;
397642c1b124SIlya Dryomov 
397742c1b124SIlya Dryomov 			old_pi = ceph_pg_pool_by_id(osdc->osdmap, pi->id);
397842c1b124SIlya Dryomov 			if (old_pi)
397942c1b124SIlya Dryomov 				pi->was_full = old_pi->was_full;
398042c1b124SIlya Dryomov 			else
398142c1b124SIlya Dryomov 				WARN_ON(pi->was_full);
398242c1b124SIlya Dryomov 		}
398342c1b124SIlya Dryomov 
398442c1b124SIlya Dryomov 		if (osdc->osdmap->epoch &&
398542c1b124SIlya Dryomov 		    osdc->osdmap->epoch + 1 < newmap->epoch) {
398642c1b124SIlya Dryomov 			WARN_ON(incremental);
398742c1b124SIlya Dryomov 			skipped_map = true;
398842c1b124SIlya Dryomov 		}
398942c1b124SIlya Dryomov 
399042c1b124SIlya Dryomov 		ceph_osdmap_destroy(osdc->osdmap);
399142c1b124SIlya Dryomov 		osdc->osdmap = newmap;
399242c1b124SIlya Dryomov 	}
399342c1b124SIlya Dryomov 
3994b7ec35b3SIlya Dryomov 	was_full &= !ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
39955aea3dcdSIlya Dryomov 	scan_requests(&osdc->homeless_osd, skipped_map, was_full, true,
39965aea3dcdSIlya Dryomov 		      need_resend, need_resend_linger);
39975aea3dcdSIlya Dryomov 
39985aea3dcdSIlya Dryomov 	for (n = rb_first(&osdc->osds); n; ) {
39995aea3dcdSIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
40005aea3dcdSIlya Dryomov 
40015aea3dcdSIlya Dryomov 		n = rb_next(n); /* close_osd() */
40025aea3dcdSIlya Dryomov 
40035aea3dcdSIlya Dryomov 		scan_requests(osd, skipped_map, was_full, true, need_resend,
40045aea3dcdSIlya Dryomov 			      need_resend_linger);
40055aea3dcdSIlya Dryomov 		if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
40065aea3dcdSIlya Dryomov 		    memcmp(&osd->o_con.peer_addr,
40075aea3dcdSIlya Dryomov 			   ceph_osd_addr(osdc->osdmap, osd->o_osd),
40085aea3dcdSIlya Dryomov 			   sizeof(struct ceph_entity_addr)))
40095aea3dcdSIlya Dryomov 			close_osd(osd);
40105aea3dcdSIlya Dryomov 	}
401142c1b124SIlya Dryomov 
401242c1b124SIlya Dryomov 	return 0;
401342c1b124SIlya Dryomov }
40146f6c7006SSage Weil 
40155aea3dcdSIlya Dryomov static void kick_requests(struct ceph_osd_client *osdc,
40165aea3dcdSIlya Dryomov 			  struct rb_root *need_resend,
40175aea3dcdSIlya Dryomov 			  struct list_head *need_resend_linger)
40185aea3dcdSIlya Dryomov {
4019922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq, *nlreq;
402004c7d789SIlya Dryomov 	enum calc_target_result ct_res;
40215aea3dcdSIlya Dryomov 	struct rb_node *n;
40225aea3dcdSIlya Dryomov 
402304c7d789SIlya Dryomov 	/* make sure need_resend targets reflect latest map */
402404c7d789SIlya Dryomov 	for (n = rb_first(need_resend); n; ) {
402504c7d789SIlya Dryomov 		struct ceph_osd_request *req =
402604c7d789SIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
402704c7d789SIlya Dryomov 
402804c7d789SIlya Dryomov 		n = rb_next(n);
402904c7d789SIlya Dryomov 
403004c7d789SIlya Dryomov 		if (req->r_t.epoch < osdc->osdmap->epoch) {
40318edf84baSIlya Dryomov 			ct_res = calc_target(osdc, &req->r_t, false);
403204c7d789SIlya Dryomov 			if (ct_res == CALC_TARGET_POOL_DNE) {
403304c7d789SIlya Dryomov 				erase_request(need_resend, req);
403404c7d789SIlya Dryomov 				check_pool_dne(req);
403504c7d789SIlya Dryomov 			}
403604c7d789SIlya Dryomov 		}
403704c7d789SIlya Dryomov 	}
403804c7d789SIlya Dryomov 
40395aea3dcdSIlya Dryomov 	for (n = rb_first(need_resend); n; ) {
40405aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
40415aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
40425aea3dcdSIlya Dryomov 		struct ceph_osd *osd;
40435aea3dcdSIlya Dryomov 
40445aea3dcdSIlya Dryomov 		n = rb_next(n);
40455aea3dcdSIlya Dryomov 		erase_request(need_resend, req); /* before link_request() */
40465aea3dcdSIlya Dryomov 
40475aea3dcdSIlya Dryomov 		osd = lookup_create_osd(osdc, req->r_t.osd, true);
40485aea3dcdSIlya Dryomov 		link_request(osd, req);
40495aea3dcdSIlya Dryomov 		if (!req->r_linger) {
40505aea3dcdSIlya Dryomov 			if (!osd_homeless(osd) && !req->r_t.paused)
40515aea3dcdSIlya Dryomov 				send_request(req);
4052922dab61SIlya Dryomov 		} else {
4053922dab61SIlya Dryomov 			cancel_linger_request(req);
40545aea3dcdSIlya Dryomov 		}
40555aea3dcdSIlya Dryomov 	}
4056922dab61SIlya Dryomov 
4057922dab61SIlya Dryomov 	list_for_each_entry_safe(lreq, nlreq, need_resend_linger, scan_item) {
4058922dab61SIlya Dryomov 		if (!osd_homeless(lreq->osd))
4059922dab61SIlya Dryomov 			send_linger(lreq);
4060922dab61SIlya Dryomov 
4061922dab61SIlya Dryomov 		list_del_init(&lreq->scan_item);
4062922dab61SIlya Dryomov 	}
40635aea3dcdSIlya Dryomov }
40645aea3dcdSIlya Dryomov 
40653d14c5d2SYehuda Sadeh /*
40663d14c5d2SYehuda Sadeh  * Process updated osd map.
40673d14c5d2SYehuda Sadeh  *
40683d14c5d2SYehuda Sadeh  * The message contains any number of incremental and full maps, normally
40693d14c5d2SYehuda Sadeh  * indicating some sort of topology change in the cluster.  Kick requests
40703d14c5d2SYehuda Sadeh  * off to different OSDs as needed.
40713d14c5d2SYehuda Sadeh  */
40723d14c5d2SYehuda Sadeh void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
40733d14c5d2SYehuda Sadeh {
407442c1b124SIlya Dryomov 	void *p = msg->front.iov_base;
407542c1b124SIlya Dryomov 	void *const end = p + msg->front.iov_len;
40763d14c5d2SYehuda Sadeh 	u32 nr_maps, maplen;
40773d14c5d2SYehuda Sadeh 	u32 epoch;
40783d14c5d2SYehuda Sadeh 	struct ceph_fsid fsid;
40795aea3dcdSIlya Dryomov 	struct rb_root need_resend = RB_ROOT;
40805aea3dcdSIlya Dryomov 	LIST_HEAD(need_resend_linger);
408142c1b124SIlya Dryomov 	bool handled_incremental = false;
408242c1b124SIlya Dryomov 	bool was_pauserd, was_pausewr;
408342c1b124SIlya Dryomov 	bool pauserd, pausewr;
408442c1b124SIlya Dryomov 	int err;
40853d14c5d2SYehuda Sadeh 
408642c1b124SIlya Dryomov 	dout("%s have %u\n", __func__, osdc->osdmap->epoch);
40875aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
40883d14c5d2SYehuda Sadeh 
40893d14c5d2SYehuda Sadeh 	/* verify fsid */
40903d14c5d2SYehuda Sadeh 	ceph_decode_need(&p, end, sizeof(fsid), bad);
40913d14c5d2SYehuda Sadeh 	ceph_decode_copy(&p, &fsid, sizeof(fsid));
40923d14c5d2SYehuda Sadeh 	if (ceph_check_fsid(osdc->client, &fsid) < 0)
409342c1b124SIlya Dryomov 		goto bad;
40943d14c5d2SYehuda Sadeh 
4095b7ec35b3SIlya Dryomov 	was_pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
4096b7ec35b3SIlya Dryomov 	was_pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
4097b7ec35b3SIlya Dryomov 		      ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
409842c1b124SIlya Dryomov 		      have_pool_full(osdc);
40999a1ea2dbSJosh Durgin 
41003d14c5d2SYehuda Sadeh 	/* incremental maps */
41013d14c5d2SYehuda Sadeh 	ceph_decode_32_safe(&p, end, nr_maps, bad);
41023d14c5d2SYehuda Sadeh 	dout(" %d inc maps\n", nr_maps);
41033d14c5d2SYehuda Sadeh 	while (nr_maps > 0) {
41043d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, 2*sizeof(u32), bad);
41053d14c5d2SYehuda Sadeh 		epoch = ceph_decode_32(&p);
41063d14c5d2SYehuda Sadeh 		maplen = ceph_decode_32(&p);
41073d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, maplen, bad);
410842c1b124SIlya Dryomov 		if (osdc->osdmap->epoch &&
410942c1b124SIlya Dryomov 		    osdc->osdmap->epoch + 1 == epoch) {
41103d14c5d2SYehuda Sadeh 			dout("applying incremental map %u len %d\n",
41113d14c5d2SYehuda Sadeh 			     epoch, maplen);
41125aea3dcdSIlya Dryomov 			err = handle_one_map(osdc, p, p + maplen, true,
41135aea3dcdSIlya Dryomov 					     &need_resend, &need_resend_linger);
411442c1b124SIlya Dryomov 			if (err)
41153d14c5d2SYehuda Sadeh 				goto bad;
411642c1b124SIlya Dryomov 			handled_incremental = true;
41173d14c5d2SYehuda Sadeh 		} else {
41183d14c5d2SYehuda Sadeh 			dout("ignoring incremental map %u len %d\n",
41193d14c5d2SYehuda Sadeh 			     epoch, maplen);
41203d14c5d2SYehuda Sadeh 		}
412142c1b124SIlya Dryomov 		p += maplen;
41223d14c5d2SYehuda Sadeh 		nr_maps--;
41233d14c5d2SYehuda Sadeh 	}
412442c1b124SIlya Dryomov 	if (handled_incremental)
41253d14c5d2SYehuda Sadeh 		goto done;
41263d14c5d2SYehuda Sadeh 
41273d14c5d2SYehuda Sadeh 	/* full maps */
41283d14c5d2SYehuda Sadeh 	ceph_decode_32_safe(&p, end, nr_maps, bad);
41293d14c5d2SYehuda Sadeh 	dout(" %d full maps\n", nr_maps);
41303d14c5d2SYehuda Sadeh 	while (nr_maps) {
41313d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, 2*sizeof(u32), bad);
41323d14c5d2SYehuda Sadeh 		epoch = ceph_decode_32(&p);
41333d14c5d2SYehuda Sadeh 		maplen = ceph_decode_32(&p);
41343d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, maplen, bad);
41353d14c5d2SYehuda Sadeh 		if (nr_maps > 1) {
41363d14c5d2SYehuda Sadeh 			dout("skipping non-latest full map %u len %d\n",
41373d14c5d2SYehuda Sadeh 			     epoch, maplen);
4138e5253a7bSIlya Dryomov 		} else if (osdc->osdmap->epoch >= epoch) {
41393d14c5d2SYehuda Sadeh 			dout("skipping full map %u len %d, "
41403d14c5d2SYehuda Sadeh 			     "older than our %u\n", epoch, maplen,
41413d14c5d2SYehuda Sadeh 			     osdc->osdmap->epoch);
41423d14c5d2SYehuda Sadeh 		} else {
41433d14c5d2SYehuda Sadeh 			dout("taking full map %u len %d\n", epoch, maplen);
41445aea3dcdSIlya Dryomov 			err = handle_one_map(osdc, p, p + maplen, false,
41455aea3dcdSIlya Dryomov 					     &need_resend, &need_resend_linger);
414642c1b124SIlya Dryomov 			if (err)
41473d14c5d2SYehuda Sadeh 				goto bad;
41483d14c5d2SYehuda Sadeh 		}
41493d14c5d2SYehuda Sadeh 		p += maplen;
41503d14c5d2SYehuda Sadeh 		nr_maps--;
41513d14c5d2SYehuda Sadeh 	}
41523d14c5d2SYehuda Sadeh 
41533d14c5d2SYehuda Sadeh done:
4154cd634fb6SSage Weil 	/*
4155cd634fb6SSage Weil 	 * subscribe to subsequent osdmap updates if full to ensure
4156cd634fb6SSage Weil 	 * we find out when we are no longer full and stop returning
4157cd634fb6SSage Weil 	 * ENOSPC.
4158cd634fb6SSage Weil 	 */
4159b7ec35b3SIlya Dryomov 	pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
4160b7ec35b3SIlya Dryomov 	pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
4161b7ec35b3SIlya Dryomov 		  ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
416242c1b124SIlya Dryomov 		  have_pool_full(osdc);
416358eb7932SJeff Layton 	if (was_pauserd || was_pausewr || pauserd || pausewr ||
416458eb7932SJeff Layton 	    osdc->osdmap->epoch < osdc->epoch_barrier)
416542c1b124SIlya Dryomov 		maybe_request_map(osdc);
4166cd634fb6SSage Weil 
41675aea3dcdSIlya Dryomov 	kick_requests(osdc, &need_resend, &need_resend_linger);
416842c1b124SIlya Dryomov 
4169fc36d0a4SJeff Layton 	ceph_osdc_abort_on_full(osdc);
417042c1b124SIlya Dryomov 	ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
417142c1b124SIlya Dryomov 			  osdc->osdmap->epoch);
41725aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
41733d14c5d2SYehuda Sadeh 	wake_up_all(&osdc->client->auth_wq);
41743d14c5d2SYehuda Sadeh 	return;
41753d14c5d2SYehuda Sadeh 
41763d14c5d2SYehuda Sadeh bad:
41773d14c5d2SYehuda Sadeh 	pr_err("osdc handle_map corrupt msg\n");
41783d14c5d2SYehuda Sadeh 	ceph_msg_dump(msg);
41795aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
41805aea3dcdSIlya Dryomov }
41815aea3dcdSIlya Dryomov 
41825aea3dcdSIlya Dryomov /*
41835aea3dcdSIlya Dryomov  * Resubmit requests pending on the given osd.
41845aea3dcdSIlya Dryomov  */
41855aea3dcdSIlya Dryomov static void kick_osd_requests(struct ceph_osd *osd)
41865aea3dcdSIlya Dryomov {
41875aea3dcdSIlya Dryomov 	struct rb_node *n;
41885aea3dcdSIlya Dryomov 
4189a02a946dSIlya Dryomov 	clear_backoffs(osd);
4190a02a946dSIlya Dryomov 
4191922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; ) {
41925aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
41935aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
41945aea3dcdSIlya Dryomov 
4195922dab61SIlya Dryomov 		n = rb_next(n); /* cancel_linger_request() */
4196922dab61SIlya Dryomov 
41975aea3dcdSIlya Dryomov 		if (!req->r_linger) {
41985aea3dcdSIlya Dryomov 			if (!req->r_t.paused)
41995aea3dcdSIlya Dryomov 				send_request(req);
4200922dab61SIlya Dryomov 		} else {
4201922dab61SIlya Dryomov 			cancel_linger_request(req);
42025aea3dcdSIlya Dryomov 		}
42035aea3dcdSIlya Dryomov 	}
4204922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_linger_requests); n; n = rb_next(n)) {
4205922dab61SIlya Dryomov 		struct ceph_osd_linger_request *lreq =
4206922dab61SIlya Dryomov 		    rb_entry(n, struct ceph_osd_linger_request, node);
4207922dab61SIlya Dryomov 
4208922dab61SIlya Dryomov 		send_linger(lreq);
4209922dab61SIlya Dryomov 	}
42105aea3dcdSIlya Dryomov }
42115aea3dcdSIlya Dryomov 
42125aea3dcdSIlya Dryomov /*
42135aea3dcdSIlya Dryomov  * If the osd connection drops, we need to resubmit all requests.
42145aea3dcdSIlya Dryomov  */
42155aea3dcdSIlya Dryomov static void osd_fault(struct ceph_connection *con)
42165aea3dcdSIlya Dryomov {
42175aea3dcdSIlya Dryomov 	struct ceph_osd *osd = con->private;
42185aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
42195aea3dcdSIlya Dryomov 
42205aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
42215aea3dcdSIlya Dryomov 
42225aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
42235aea3dcdSIlya Dryomov 	if (!osd_registered(osd)) {
42245aea3dcdSIlya Dryomov 		dout("%s osd%d unknown\n", __func__, osd->o_osd);
42255aea3dcdSIlya Dryomov 		goto out_unlock;
42265aea3dcdSIlya Dryomov 	}
42275aea3dcdSIlya Dryomov 
42285aea3dcdSIlya Dryomov 	if (!reopen_osd(osd))
42295aea3dcdSIlya Dryomov 		kick_osd_requests(osd);
42305aea3dcdSIlya Dryomov 	maybe_request_map(osdc);
42315aea3dcdSIlya Dryomov 
42325aea3dcdSIlya Dryomov out_unlock:
42335aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
42343d14c5d2SYehuda Sadeh }
42353d14c5d2SYehuda Sadeh 
4236a02a946dSIlya Dryomov struct MOSDBackoff {
4237a02a946dSIlya Dryomov 	struct ceph_spg spgid;
4238a02a946dSIlya Dryomov 	u32 map_epoch;
4239a02a946dSIlya Dryomov 	u8 op;
4240a02a946dSIlya Dryomov 	u64 id;
4241a02a946dSIlya Dryomov 	struct ceph_hobject_id *begin;
4242a02a946dSIlya Dryomov 	struct ceph_hobject_id *end;
4243a02a946dSIlya Dryomov };
4244a02a946dSIlya Dryomov 
4245a02a946dSIlya Dryomov static int decode_MOSDBackoff(const struct ceph_msg *msg, struct MOSDBackoff *m)
4246a02a946dSIlya Dryomov {
4247a02a946dSIlya Dryomov 	void *p = msg->front.iov_base;
4248a02a946dSIlya Dryomov 	void *const end = p + msg->front.iov_len;
4249a02a946dSIlya Dryomov 	u8 struct_v;
4250a02a946dSIlya Dryomov 	u32 struct_len;
4251a02a946dSIlya Dryomov 	int ret;
4252a02a946dSIlya Dryomov 
4253a02a946dSIlya Dryomov 	ret = ceph_start_decoding(&p, end, 1, "spg_t", &struct_v, &struct_len);
4254a02a946dSIlya Dryomov 	if (ret)
4255a02a946dSIlya Dryomov 		return ret;
4256a02a946dSIlya Dryomov 
4257a02a946dSIlya Dryomov 	ret = ceph_decode_pgid(&p, end, &m->spgid.pgid);
4258a02a946dSIlya Dryomov 	if (ret)
4259a02a946dSIlya Dryomov 		return ret;
4260a02a946dSIlya Dryomov 
4261a02a946dSIlya Dryomov 	ceph_decode_8_safe(&p, end, m->spgid.shard, e_inval);
4262a02a946dSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->map_epoch, e_inval);
4263a02a946dSIlya Dryomov 	ceph_decode_8_safe(&p, end, m->op, e_inval);
4264a02a946dSIlya Dryomov 	ceph_decode_64_safe(&p, end, m->id, e_inval);
4265a02a946dSIlya Dryomov 
4266a02a946dSIlya Dryomov 	m->begin = kzalloc(sizeof(*m->begin), GFP_NOIO);
4267a02a946dSIlya Dryomov 	if (!m->begin)
4268a02a946dSIlya Dryomov 		return -ENOMEM;
4269a02a946dSIlya Dryomov 
4270a02a946dSIlya Dryomov 	ret = decode_hoid(&p, end, m->begin);
4271a02a946dSIlya Dryomov 	if (ret) {
4272a02a946dSIlya Dryomov 		free_hoid(m->begin);
4273a02a946dSIlya Dryomov 		return ret;
4274a02a946dSIlya Dryomov 	}
4275a02a946dSIlya Dryomov 
4276a02a946dSIlya Dryomov 	m->end = kzalloc(sizeof(*m->end), GFP_NOIO);
4277a02a946dSIlya Dryomov 	if (!m->end) {
4278a02a946dSIlya Dryomov 		free_hoid(m->begin);
4279a02a946dSIlya Dryomov 		return -ENOMEM;
4280a02a946dSIlya Dryomov 	}
4281a02a946dSIlya Dryomov 
4282a02a946dSIlya Dryomov 	ret = decode_hoid(&p, end, m->end);
4283a02a946dSIlya Dryomov 	if (ret) {
4284a02a946dSIlya Dryomov 		free_hoid(m->begin);
4285a02a946dSIlya Dryomov 		free_hoid(m->end);
4286a02a946dSIlya Dryomov 		return ret;
4287a02a946dSIlya Dryomov 	}
4288a02a946dSIlya Dryomov 
4289a02a946dSIlya Dryomov 	return 0;
4290a02a946dSIlya Dryomov 
4291a02a946dSIlya Dryomov e_inval:
4292a02a946dSIlya Dryomov 	return -EINVAL;
4293a02a946dSIlya Dryomov }
4294a02a946dSIlya Dryomov 
4295a02a946dSIlya Dryomov static struct ceph_msg *create_backoff_message(
4296a02a946dSIlya Dryomov 				const struct ceph_osd_backoff *backoff,
4297a02a946dSIlya Dryomov 				u32 map_epoch)
4298a02a946dSIlya Dryomov {
4299a02a946dSIlya Dryomov 	struct ceph_msg *msg;
4300a02a946dSIlya Dryomov 	void *p, *end;
4301a02a946dSIlya Dryomov 	int msg_size;
4302a02a946dSIlya Dryomov 
4303a02a946dSIlya Dryomov 	msg_size = CEPH_ENCODING_START_BLK_LEN +
4304a02a946dSIlya Dryomov 			CEPH_PGID_ENCODING_LEN + 1; /* spgid */
4305a02a946dSIlya Dryomov 	msg_size += 4 + 1 + 8; /* map_epoch, op, id */
4306a02a946dSIlya Dryomov 	msg_size += CEPH_ENCODING_START_BLK_LEN +
4307a02a946dSIlya Dryomov 			hoid_encoding_size(backoff->begin);
4308a02a946dSIlya Dryomov 	msg_size += CEPH_ENCODING_START_BLK_LEN +
4309a02a946dSIlya Dryomov 			hoid_encoding_size(backoff->end);
4310a02a946dSIlya Dryomov 
4311a02a946dSIlya Dryomov 	msg = ceph_msg_new(CEPH_MSG_OSD_BACKOFF, msg_size, GFP_NOIO, true);
4312a02a946dSIlya Dryomov 	if (!msg)
4313a02a946dSIlya Dryomov 		return NULL;
4314a02a946dSIlya Dryomov 
4315a02a946dSIlya Dryomov 	p = msg->front.iov_base;
4316a02a946dSIlya Dryomov 	end = p + msg->front_alloc_len;
4317a02a946dSIlya Dryomov 
4318a02a946dSIlya Dryomov 	encode_spgid(&p, &backoff->spgid);
4319a02a946dSIlya Dryomov 	ceph_encode_32(&p, map_epoch);
4320a02a946dSIlya Dryomov 	ceph_encode_8(&p, CEPH_OSD_BACKOFF_OP_ACK_BLOCK);
4321a02a946dSIlya Dryomov 	ceph_encode_64(&p, backoff->id);
4322a02a946dSIlya Dryomov 	encode_hoid(&p, end, backoff->begin);
4323a02a946dSIlya Dryomov 	encode_hoid(&p, end, backoff->end);
4324a02a946dSIlya Dryomov 	BUG_ON(p != end);
4325a02a946dSIlya Dryomov 
4326a02a946dSIlya Dryomov 	msg->front.iov_len = p - msg->front.iov_base;
4327a02a946dSIlya Dryomov 	msg->hdr.version = cpu_to_le16(1); /* MOSDBackoff v1 */
4328a02a946dSIlya Dryomov 	msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
4329a02a946dSIlya Dryomov 
4330a02a946dSIlya Dryomov 	return msg;
4331a02a946dSIlya Dryomov }
4332a02a946dSIlya Dryomov 
4333a02a946dSIlya Dryomov static void handle_backoff_block(struct ceph_osd *osd, struct MOSDBackoff *m)
4334a02a946dSIlya Dryomov {
4335a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
4336a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
4337a02a946dSIlya Dryomov 	struct ceph_msg *msg;
4338a02a946dSIlya Dryomov 
4339a02a946dSIlya Dryomov 	dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4340a02a946dSIlya Dryomov 	     m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4341a02a946dSIlya Dryomov 
4342a02a946dSIlya Dryomov 	spg = lookup_spg_mapping(&osd->o_backoff_mappings, &m->spgid);
4343a02a946dSIlya Dryomov 	if (!spg) {
4344a02a946dSIlya Dryomov 		spg = alloc_spg_mapping();
4345a02a946dSIlya Dryomov 		if (!spg) {
4346a02a946dSIlya Dryomov 			pr_err("%s failed to allocate spg\n", __func__);
4347a02a946dSIlya Dryomov 			return;
4348a02a946dSIlya Dryomov 		}
4349a02a946dSIlya Dryomov 		spg->spgid = m->spgid; /* struct */
4350a02a946dSIlya Dryomov 		insert_spg_mapping(&osd->o_backoff_mappings, spg);
4351a02a946dSIlya Dryomov 	}
4352a02a946dSIlya Dryomov 
4353a02a946dSIlya Dryomov 	backoff = alloc_backoff();
4354a02a946dSIlya Dryomov 	if (!backoff) {
4355a02a946dSIlya Dryomov 		pr_err("%s failed to allocate backoff\n", __func__);
4356a02a946dSIlya Dryomov 		return;
4357a02a946dSIlya Dryomov 	}
4358a02a946dSIlya Dryomov 	backoff->spgid = m->spgid; /* struct */
4359a02a946dSIlya Dryomov 	backoff->id = m->id;
4360a02a946dSIlya Dryomov 	backoff->begin = m->begin;
4361a02a946dSIlya Dryomov 	m->begin = NULL; /* backoff now owns this */
4362a02a946dSIlya Dryomov 	backoff->end = m->end;
4363a02a946dSIlya Dryomov 	m->end = NULL;   /* ditto */
4364a02a946dSIlya Dryomov 
4365a02a946dSIlya Dryomov 	insert_backoff(&spg->backoffs, backoff);
4366a02a946dSIlya Dryomov 	insert_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4367a02a946dSIlya Dryomov 
4368a02a946dSIlya Dryomov 	/*
4369a02a946dSIlya Dryomov 	 * Ack with original backoff's epoch so that the OSD can
4370a02a946dSIlya Dryomov 	 * discard this if there was a PG split.
4371a02a946dSIlya Dryomov 	 */
4372a02a946dSIlya Dryomov 	msg = create_backoff_message(backoff, m->map_epoch);
4373a02a946dSIlya Dryomov 	if (!msg) {
4374a02a946dSIlya Dryomov 		pr_err("%s failed to allocate msg\n", __func__);
4375a02a946dSIlya Dryomov 		return;
4376a02a946dSIlya Dryomov 	}
4377a02a946dSIlya Dryomov 	ceph_con_send(&osd->o_con, msg);
4378a02a946dSIlya Dryomov }
4379a02a946dSIlya Dryomov 
4380a02a946dSIlya Dryomov static bool target_contained_by(const struct ceph_osd_request_target *t,
4381a02a946dSIlya Dryomov 				const struct ceph_hobject_id *begin,
4382a02a946dSIlya Dryomov 				const struct ceph_hobject_id *end)
4383a02a946dSIlya Dryomov {
4384a02a946dSIlya Dryomov 	struct ceph_hobject_id hoid;
4385a02a946dSIlya Dryomov 	int cmp;
4386a02a946dSIlya Dryomov 
4387a02a946dSIlya Dryomov 	hoid_fill_from_target(&hoid, t);
4388a02a946dSIlya Dryomov 	cmp = hoid_compare(&hoid, begin);
4389a02a946dSIlya Dryomov 	return !cmp || (cmp > 0 && hoid_compare(&hoid, end) < 0);
4390a02a946dSIlya Dryomov }
4391a02a946dSIlya Dryomov 
4392a02a946dSIlya Dryomov static void handle_backoff_unblock(struct ceph_osd *osd,
4393a02a946dSIlya Dryomov 				   const struct MOSDBackoff *m)
4394a02a946dSIlya Dryomov {
4395a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
4396a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
4397a02a946dSIlya Dryomov 	struct rb_node *n;
4398a02a946dSIlya Dryomov 
4399a02a946dSIlya Dryomov 	dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4400a02a946dSIlya Dryomov 	     m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4401a02a946dSIlya Dryomov 
4402a02a946dSIlya Dryomov 	backoff = lookup_backoff_by_id(&osd->o_backoffs_by_id, m->id);
4403a02a946dSIlya Dryomov 	if (!backoff) {
4404a02a946dSIlya Dryomov 		pr_err("%s osd%d spgid %llu.%xs%d id %llu backoff dne\n",
4405a02a946dSIlya Dryomov 		       __func__, osd->o_osd, m->spgid.pgid.pool,
4406a02a946dSIlya Dryomov 		       m->spgid.pgid.seed, m->spgid.shard, m->id);
4407a02a946dSIlya Dryomov 		return;
4408a02a946dSIlya Dryomov 	}
4409a02a946dSIlya Dryomov 
4410a02a946dSIlya Dryomov 	if (hoid_compare(backoff->begin, m->begin) &&
4411a02a946dSIlya Dryomov 	    hoid_compare(backoff->end, m->end)) {
4412a02a946dSIlya Dryomov 		pr_err("%s osd%d spgid %llu.%xs%d id %llu bad range?\n",
4413a02a946dSIlya Dryomov 		       __func__, osd->o_osd, m->spgid.pgid.pool,
4414a02a946dSIlya Dryomov 		       m->spgid.pgid.seed, m->spgid.shard, m->id);
4415a02a946dSIlya Dryomov 		/* unblock it anyway... */
4416a02a946dSIlya Dryomov 	}
4417a02a946dSIlya Dryomov 
4418a02a946dSIlya Dryomov 	spg = lookup_spg_mapping(&osd->o_backoff_mappings, &backoff->spgid);
4419a02a946dSIlya Dryomov 	BUG_ON(!spg);
4420a02a946dSIlya Dryomov 
4421a02a946dSIlya Dryomov 	erase_backoff(&spg->backoffs, backoff);
4422a02a946dSIlya Dryomov 	erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4423a02a946dSIlya Dryomov 	free_backoff(backoff);
4424a02a946dSIlya Dryomov 
4425a02a946dSIlya Dryomov 	if (RB_EMPTY_ROOT(&spg->backoffs)) {
4426a02a946dSIlya Dryomov 		erase_spg_mapping(&osd->o_backoff_mappings, spg);
4427a02a946dSIlya Dryomov 		free_spg_mapping(spg);
4428a02a946dSIlya Dryomov 	}
4429a02a946dSIlya Dryomov 
4430a02a946dSIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
4431a02a946dSIlya Dryomov 		struct ceph_osd_request *req =
4432a02a946dSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
4433a02a946dSIlya Dryomov 
4434a02a946dSIlya Dryomov 		if (!ceph_spg_compare(&req->r_t.spgid, &m->spgid)) {
4435a02a946dSIlya Dryomov 			/*
4436a02a946dSIlya Dryomov 			 * Match against @m, not @backoff -- the PG may
4437a02a946dSIlya Dryomov 			 * have split on the OSD.
4438a02a946dSIlya Dryomov 			 */
4439a02a946dSIlya Dryomov 			if (target_contained_by(&req->r_t, m->begin, m->end)) {
4440a02a946dSIlya Dryomov 				/*
4441a02a946dSIlya Dryomov 				 * If no other installed backoff applies,
4442a02a946dSIlya Dryomov 				 * resend.
4443a02a946dSIlya Dryomov 				 */
4444a02a946dSIlya Dryomov 				send_request(req);
4445a02a946dSIlya Dryomov 			}
4446a02a946dSIlya Dryomov 		}
4447a02a946dSIlya Dryomov 	}
4448a02a946dSIlya Dryomov }
4449a02a946dSIlya Dryomov 
4450a02a946dSIlya Dryomov static void handle_backoff(struct ceph_osd *osd, struct ceph_msg *msg)
4451a02a946dSIlya Dryomov {
4452a02a946dSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
4453a02a946dSIlya Dryomov 	struct MOSDBackoff m;
4454a02a946dSIlya Dryomov 	int ret;
4455a02a946dSIlya Dryomov 
4456a02a946dSIlya Dryomov 	down_read(&osdc->lock);
4457a02a946dSIlya Dryomov 	if (!osd_registered(osd)) {
4458a02a946dSIlya Dryomov 		dout("%s osd%d unknown\n", __func__, osd->o_osd);
4459a02a946dSIlya Dryomov 		up_read(&osdc->lock);
4460a02a946dSIlya Dryomov 		return;
4461a02a946dSIlya Dryomov 	}
4462a02a946dSIlya Dryomov 	WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
4463a02a946dSIlya Dryomov 
4464a02a946dSIlya Dryomov 	mutex_lock(&osd->lock);
4465a02a946dSIlya Dryomov 	ret = decode_MOSDBackoff(msg, &m);
4466a02a946dSIlya Dryomov 	if (ret) {
4467a02a946dSIlya Dryomov 		pr_err("failed to decode MOSDBackoff: %d\n", ret);
4468a02a946dSIlya Dryomov 		ceph_msg_dump(msg);
4469a02a946dSIlya Dryomov 		goto out_unlock;
4470a02a946dSIlya Dryomov 	}
4471a02a946dSIlya Dryomov 
4472a02a946dSIlya Dryomov 	switch (m.op) {
4473a02a946dSIlya Dryomov 	case CEPH_OSD_BACKOFF_OP_BLOCK:
4474a02a946dSIlya Dryomov 		handle_backoff_block(osd, &m);
4475a02a946dSIlya Dryomov 		break;
4476a02a946dSIlya Dryomov 	case CEPH_OSD_BACKOFF_OP_UNBLOCK:
4477a02a946dSIlya Dryomov 		handle_backoff_unblock(osd, &m);
4478a02a946dSIlya Dryomov 		break;
4479a02a946dSIlya Dryomov 	default:
4480a02a946dSIlya Dryomov 		pr_err("%s osd%d unknown op %d\n", __func__, osd->o_osd, m.op);
4481a02a946dSIlya Dryomov 	}
4482a02a946dSIlya Dryomov 
4483a02a946dSIlya Dryomov 	free_hoid(m.begin);
4484a02a946dSIlya Dryomov 	free_hoid(m.end);
4485a02a946dSIlya Dryomov 
4486a02a946dSIlya Dryomov out_unlock:
4487a02a946dSIlya Dryomov 	mutex_unlock(&osd->lock);
4488a02a946dSIlya Dryomov 	up_read(&osdc->lock);
4489a02a946dSIlya Dryomov }
4490a02a946dSIlya Dryomov 
44913d14c5d2SYehuda Sadeh /*
4492a40c4f10SYehuda Sadeh  * Process osd watch notifications
4493a40c4f10SYehuda Sadeh  */
44943c663bbdSAlex Elder static void handle_watch_notify(struct ceph_osd_client *osdc,
44953c663bbdSAlex Elder 				struct ceph_msg *msg)
4496a40c4f10SYehuda Sadeh {
4497922dab61SIlya Dryomov 	void *p = msg->front.iov_base;
4498922dab61SIlya Dryomov 	void *const end = p + msg->front.iov_len;
4499922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
4500922dab61SIlya Dryomov 	struct linger_work *lwork;
4501922dab61SIlya Dryomov 	u8 proto_ver, opcode;
4502922dab61SIlya Dryomov 	u64 cookie, notify_id;
4503922dab61SIlya Dryomov 	u64 notifier_id = 0;
450419079203SIlya Dryomov 	s32 return_code = 0;
4505922dab61SIlya Dryomov 	void *payload = NULL;
4506922dab61SIlya Dryomov 	u32 payload_len = 0;
4507a40c4f10SYehuda Sadeh 
4508a40c4f10SYehuda Sadeh 	ceph_decode_8_safe(&p, end, proto_ver, bad);
4509a40c4f10SYehuda Sadeh 	ceph_decode_8_safe(&p, end, opcode, bad);
4510a40c4f10SYehuda Sadeh 	ceph_decode_64_safe(&p, end, cookie, bad);
4511922dab61SIlya Dryomov 	p += 8; /* skip ver */
4512a40c4f10SYehuda Sadeh 	ceph_decode_64_safe(&p, end, notify_id, bad);
4513a40c4f10SYehuda Sadeh 
4514922dab61SIlya Dryomov 	if (proto_ver >= 1) {
4515922dab61SIlya Dryomov 		ceph_decode_32_safe(&p, end, payload_len, bad);
4516922dab61SIlya Dryomov 		ceph_decode_need(&p, end, payload_len, bad);
4517922dab61SIlya Dryomov 		payload = p;
4518922dab61SIlya Dryomov 		p += payload_len;
4519a40c4f10SYehuda Sadeh 	}
4520a40c4f10SYehuda Sadeh 
4521922dab61SIlya Dryomov 	if (le16_to_cpu(msg->hdr.version) >= 2)
452219079203SIlya Dryomov 		ceph_decode_32_safe(&p, end, return_code, bad);
4523922dab61SIlya Dryomov 
4524922dab61SIlya Dryomov 	if (le16_to_cpu(msg->hdr.version) >= 3)
4525922dab61SIlya Dryomov 		ceph_decode_64_safe(&p, end, notifier_id, bad);
4526922dab61SIlya Dryomov 
4527922dab61SIlya Dryomov 	down_read(&osdc->lock);
4528922dab61SIlya Dryomov 	lreq = lookup_linger_osdc(&osdc->linger_requests, cookie);
4529922dab61SIlya Dryomov 	if (!lreq) {
4530922dab61SIlya Dryomov 		dout("%s opcode %d cookie %llu dne\n", __func__, opcode,
4531922dab61SIlya Dryomov 		     cookie);
4532922dab61SIlya Dryomov 		goto out_unlock_osdc;
4533922dab61SIlya Dryomov 	}
4534922dab61SIlya Dryomov 
4535922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
453619079203SIlya Dryomov 	dout("%s opcode %d cookie %llu lreq %p is_watch %d\n", __func__,
453719079203SIlya Dryomov 	     opcode, cookie, lreq, lreq->is_watch);
4538922dab61SIlya Dryomov 	if (opcode == CEPH_WATCH_EVENT_DISCONNECT) {
4539922dab61SIlya Dryomov 		if (!lreq->last_error) {
4540922dab61SIlya Dryomov 			lreq->last_error = -ENOTCONN;
4541922dab61SIlya Dryomov 			queue_watch_error(lreq);
4542922dab61SIlya Dryomov 		}
454319079203SIlya Dryomov 	} else if (!lreq->is_watch) {
454419079203SIlya Dryomov 		/* CEPH_WATCH_EVENT_NOTIFY_COMPLETE */
454519079203SIlya Dryomov 		if (lreq->notify_id && lreq->notify_id != notify_id) {
454619079203SIlya Dryomov 			dout("lreq %p notify_id %llu != %llu, ignoring\n", lreq,
454719079203SIlya Dryomov 			     lreq->notify_id, notify_id);
454819079203SIlya Dryomov 		} else if (!completion_done(&lreq->notify_finish_wait)) {
454919079203SIlya Dryomov 			struct ceph_msg_data *data =
45500d9c1ab3SIlya Dryomov 			    msg->num_data_items ? &msg->data[0] : NULL;
455119079203SIlya Dryomov 
455219079203SIlya Dryomov 			if (data) {
455319079203SIlya Dryomov 				if (lreq->preply_pages) {
455419079203SIlya Dryomov 					WARN_ON(data->type !=
455519079203SIlya Dryomov 							CEPH_MSG_DATA_PAGES);
455619079203SIlya Dryomov 					*lreq->preply_pages = data->pages;
455719079203SIlya Dryomov 					*lreq->preply_len = data->length;
4558e8862740SIlya Dryomov 					data->own_pages = false;
455919079203SIlya Dryomov 				}
456019079203SIlya Dryomov 			}
456119079203SIlya Dryomov 			lreq->notify_finish_error = return_code;
456219079203SIlya Dryomov 			complete_all(&lreq->notify_finish_wait);
456319079203SIlya Dryomov 		}
4564922dab61SIlya Dryomov 	} else {
4565922dab61SIlya Dryomov 		/* CEPH_WATCH_EVENT_NOTIFY */
4566922dab61SIlya Dryomov 		lwork = lwork_alloc(lreq, do_watch_notify);
4567922dab61SIlya Dryomov 		if (!lwork) {
4568922dab61SIlya Dryomov 			pr_err("failed to allocate notify-lwork\n");
4569922dab61SIlya Dryomov 			goto out_unlock_lreq;
4570922dab61SIlya Dryomov 		}
4571922dab61SIlya Dryomov 
4572922dab61SIlya Dryomov 		lwork->notify.notify_id = notify_id;
4573922dab61SIlya Dryomov 		lwork->notify.notifier_id = notifier_id;
4574922dab61SIlya Dryomov 		lwork->notify.payload = payload;
4575922dab61SIlya Dryomov 		lwork->notify.payload_len = payload_len;
4576922dab61SIlya Dryomov 		lwork->notify.msg = ceph_msg_get(msg);
4577922dab61SIlya Dryomov 		lwork_queue(lwork);
4578922dab61SIlya Dryomov 	}
4579922dab61SIlya Dryomov 
4580922dab61SIlya Dryomov out_unlock_lreq:
4581922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
4582922dab61SIlya Dryomov out_unlock_osdc:
4583922dab61SIlya Dryomov 	up_read(&osdc->lock);
4584a40c4f10SYehuda Sadeh 	return;
4585a40c4f10SYehuda Sadeh 
4586a40c4f10SYehuda Sadeh bad:
4587a40c4f10SYehuda Sadeh 	pr_err("osdc handle_watch_notify corrupt msg\n");
4588a40c4f10SYehuda Sadeh }
4589a40c4f10SYehuda Sadeh 
4590a40c4f10SYehuda Sadeh /*
45913d14c5d2SYehuda Sadeh  * Register request, send initial attempt.
45923d14c5d2SYehuda Sadeh  */
4593a8af0d68SJeff Layton void ceph_osdc_start_request(struct ceph_osd_client *osdc,
4594a8af0d68SJeff Layton 			     struct ceph_osd_request *req)
45953d14c5d2SYehuda Sadeh {
45965aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
45975aea3dcdSIlya Dryomov 	submit_request(req, false);
45985aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
45993d14c5d2SYehuda Sadeh }
46003d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_start_request);
46013d14c5d2SYehuda Sadeh 
46023d14c5d2SYehuda Sadeh /*
4603d0bb883cSIlya Dryomov  * Unregister request.  If @req was registered, it isn't completed:
4604d0bb883cSIlya Dryomov  * r_result isn't set and __complete_request() isn't invoked.
4605d0bb883cSIlya Dryomov  *
4606d0bb883cSIlya Dryomov  * If @req wasn't registered, this call may have raced with
4607d0bb883cSIlya Dryomov  * handle_reply(), in which case r_result would already be set and
4608d0bb883cSIlya Dryomov  * __complete_request() would be getting invoked, possibly even
4609d0bb883cSIlya Dryomov  * concurrently with this call.
4610c9f9b93dSIlya Dryomov  */
4611c9f9b93dSIlya Dryomov void ceph_osdc_cancel_request(struct ceph_osd_request *req)
4612c9f9b93dSIlya Dryomov {
4613c9f9b93dSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
4614c9f9b93dSIlya Dryomov 
46155aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
46165aea3dcdSIlya Dryomov 	if (req->r_osd)
46175aea3dcdSIlya Dryomov 		cancel_request(req);
46185aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
4619c9f9b93dSIlya Dryomov }
4620c9f9b93dSIlya Dryomov EXPORT_SYMBOL(ceph_osdc_cancel_request);
4621c9f9b93dSIlya Dryomov 
4622c9f9b93dSIlya Dryomov /*
462342b06965SIlya Dryomov  * @timeout: in jiffies, 0 means "wait forever"
462442b06965SIlya Dryomov  */
462542b06965SIlya Dryomov static int wait_request_timeout(struct ceph_osd_request *req,
462642b06965SIlya Dryomov 				unsigned long timeout)
462742b06965SIlya Dryomov {
462842b06965SIlya Dryomov 	long left;
462942b06965SIlya Dryomov 
463042b06965SIlya Dryomov 	dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
46310e76abf2SYan, Zheng 	left = wait_for_completion_killable_timeout(&req->r_completion,
463242b06965SIlya Dryomov 						ceph_timeout_jiffies(timeout));
463342b06965SIlya Dryomov 	if (left <= 0) {
463442b06965SIlya Dryomov 		left = left ?: -ETIMEDOUT;
463542b06965SIlya Dryomov 		ceph_osdc_cancel_request(req);
463642b06965SIlya Dryomov 	} else {
463742b06965SIlya Dryomov 		left = req->r_result; /* completed */
463842b06965SIlya Dryomov 	}
463942b06965SIlya Dryomov 
464042b06965SIlya Dryomov 	return left;
464142b06965SIlya Dryomov }
464242b06965SIlya Dryomov 
464342b06965SIlya Dryomov /*
46443d14c5d2SYehuda Sadeh  * wait for a request to complete
46453d14c5d2SYehuda Sadeh  */
46463d14c5d2SYehuda Sadeh int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
46473d14c5d2SYehuda Sadeh 			   struct ceph_osd_request *req)
46483d14c5d2SYehuda Sadeh {
464942b06965SIlya Dryomov 	return wait_request_timeout(req, 0);
46503d14c5d2SYehuda Sadeh }
46513d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_wait_request);
46523d14c5d2SYehuda Sadeh 
46533d14c5d2SYehuda Sadeh /*
46543d14c5d2SYehuda Sadeh  * sync - wait for all in-flight requests to flush.  avoid starvation.
46553d14c5d2SYehuda Sadeh  */
46563d14c5d2SYehuda Sadeh void ceph_osdc_sync(struct ceph_osd_client *osdc)
46573d14c5d2SYehuda Sadeh {
46585aea3dcdSIlya Dryomov 	struct rb_node *n, *p;
46595aea3dcdSIlya Dryomov 	u64 last_tid = atomic64_read(&osdc->last_tid);
46603d14c5d2SYehuda Sadeh 
46615aea3dcdSIlya Dryomov again:
46625aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
46635aea3dcdSIlya Dryomov 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
46645aea3dcdSIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
46655aea3dcdSIlya Dryomov 
46665aea3dcdSIlya Dryomov 		mutex_lock(&osd->lock);
46675aea3dcdSIlya Dryomov 		for (p = rb_first(&osd->o_requests); p; p = rb_next(p)) {
46685aea3dcdSIlya Dryomov 			struct ceph_osd_request *req =
46695aea3dcdSIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
46705aea3dcdSIlya Dryomov 
46713d14c5d2SYehuda Sadeh 			if (req->r_tid > last_tid)
46723d14c5d2SYehuda Sadeh 				break;
46733d14c5d2SYehuda Sadeh 
46745aea3dcdSIlya Dryomov 			if (!(req->r_flags & CEPH_OSD_FLAG_WRITE))
46753d14c5d2SYehuda Sadeh 				continue;
46763d14c5d2SYehuda Sadeh 
46773d14c5d2SYehuda Sadeh 			ceph_osdc_get_request(req);
46785aea3dcdSIlya Dryomov 			mutex_unlock(&osd->lock);
46795aea3dcdSIlya Dryomov 			up_read(&osdc->lock);
46805aea3dcdSIlya Dryomov 			dout("%s waiting on req %p tid %llu last_tid %llu\n",
46815aea3dcdSIlya Dryomov 			     __func__, req, req->r_tid, last_tid);
4682b18b9550SIlya Dryomov 			wait_for_completion(&req->r_completion);
46833d14c5d2SYehuda Sadeh 			ceph_osdc_put_request(req);
46845aea3dcdSIlya Dryomov 			goto again;
46853d14c5d2SYehuda Sadeh 		}
46865aea3dcdSIlya Dryomov 
46875aea3dcdSIlya Dryomov 		mutex_unlock(&osd->lock);
46885aea3dcdSIlya Dryomov 	}
46895aea3dcdSIlya Dryomov 
46905aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
46915aea3dcdSIlya Dryomov 	dout("%s done last_tid %llu\n", __func__, last_tid);
46923d14c5d2SYehuda Sadeh }
46933d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_sync);
46943d14c5d2SYehuda Sadeh 
4695922dab61SIlya Dryomov /*
4696922dab61SIlya Dryomov  * Returns a handle, caller owns a ref.
4697922dab61SIlya Dryomov  */
4698922dab61SIlya Dryomov struct ceph_osd_linger_request *
4699922dab61SIlya Dryomov ceph_osdc_watch(struct ceph_osd_client *osdc,
4700922dab61SIlya Dryomov 		struct ceph_object_id *oid,
4701922dab61SIlya Dryomov 		struct ceph_object_locator *oloc,
4702922dab61SIlya Dryomov 		rados_watchcb2_t wcb,
4703922dab61SIlya Dryomov 		rados_watcherrcb_t errcb,
4704922dab61SIlya Dryomov 		void *data)
4705922dab61SIlya Dryomov {
4706922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
4707922dab61SIlya Dryomov 	int ret;
4708922dab61SIlya Dryomov 
4709922dab61SIlya Dryomov 	lreq = linger_alloc(osdc);
4710922dab61SIlya Dryomov 	if (!lreq)
4711922dab61SIlya Dryomov 		return ERR_PTR(-ENOMEM);
4712922dab61SIlya Dryomov 
471319079203SIlya Dryomov 	lreq->is_watch = true;
4714922dab61SIlya Dryomov 	lreq->wcb = wcb;
4715922dab61SIlya Dryomov 	lreq->errcb = errcb;
4716922dab61SIlya Dryomov 	lreq->data = data;
4717b07d3c4bSIlya Dryomov 	lreq->watch_valid_thru = jiffies;
4718922dab61SIlya Dryomov 
4719922dab61SIlya Dryomov 	ceph_oid_copy(&lreq->t.base_oid, oid);
4720922dab61SIlya Dryomov 	ceph_oloc_copy(&lreq->t.base_oloc, oloc);
472154ea0046SIlya Dryomov 	lreq->t.flags = CEPH_OSD_FLAG_WRITE;
4722fac02ddfSArnd Bergmann 	ktime_get_real_ts64(&lreq->mtime);
4723922dab61SIlya Dryomov 
472481c65213SIlya Dryomov 	linger_submit(lreq);
4725922dab61SIlya Dryomov 	ret = linger_reg_commit_wait(lreq);
4726922dab61SIlya Dryomov 	if (ret) {
4727922dab61SIlya Dryomov 		linger_cancel(lreq);
4728922dab61SIlya Dryomov 		goto err_put_lreq;
4729922dab61SIlya Dryomov 	}
4730922dab61SIlya Dryomov 
4731922dab61SIlya Dryomov 	return lreq;
4732922dab61SIlya Dryomov 
4733922dab61SIlya Dryomov err_put_lreq:
4734922dab61SIlya Dryomov 	linger_put(lreq);
4735922dab61SIlya Dryomov 	return ERR_PTR(ret);
4736922dab61SIlya Dryomov }
4737922dab61SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_watch);
4738922dab61SIlya Dryomov 
4739922dab61SIlya Dryomov /*
4740922dab61SIlya Dryomov  * Releases a ref.
4741922dab61SIlya Dryomov  *
4742922dab61SIlya Dryomov  * Times out after mount_timeout to preserve rbd unmap behaviour
4743922dab61SIlya Dryomov  * introduced in 2894e1d76974 ("rbd: timeout watch teardown on unmap
4744922dab61SIlya Dryomov  * with mount_timeout").
4745922dab61SIlya Dryomov  */
4746922dab61SIlya Dryomov int ceph_osdc_unwatch(struct ceph_osd_client *osdc,
4747922dab61SIlya Dryomov 		      struct ceph_osd_linger_request *lreq)
4748922dab61SIlya Dryomov {
4749922dab61SIlya Dryomov 	struct ceph_options *opts = osdc->client->options;
4750922dab61SIlya Dryomov 	struct ceph_osd_request *req;
4751922dab61SIlya Dryomov 	int ret;
4752922dab61SIlya Dryomov 
4753922dab61SIlya Dryomov 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4754922dab61SIlya Dryomov 	if (!req)
4755922dab61SIlya Dryomov 		return -ENOMEM;
4756922dab61SIlya Dryomov 
4757922dab61SIlya Dryomov 	ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
4758922dab61SIlya Dryomov 	ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
475954ea0046SIlya Dryomov 	req->r_flags = CEPH_OSD_FLAG_WRITE;
4760fac02ddfSArnd Bergmann 	ktime_get_real_ts64(&req->r_mtime);
476175dbb685SIlya Dryomov 	osd_req_op_watch_init(req, 0, CEPH_OSD_WATCH_OP_UNWATCH,
476275dbb685SIlya Dryomov 			      lreq->linger_id, 0);
4763922dab61SIlya Dryomov 
4764922dab61SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4765922dab61SIlya Dryomov 	if (ret)
4766922dab61SIlya Dryomov 		goto out_put_req;
4767922dab61SIlya Dryomov 
4768a8af0d68SJeff Layton 	ceph_osdc_start_request(osdc, req);
4769922dab61SIlya Dryomov 	linger_cancel(lreq);
4770922dab61SIlya Dryomov 	linger_put(lreq);
4771922dab61SIlya Dryomov 	ret = wait_request_timeout(req, opts->mount_timeout);
4772922dab61SIlya Dryomov 
4773922dab61SIlya Dryomov out_put_req:
4774922dab61SIlya Dryomov 	ceph_osdc_put_request(req);
4775922dab61SIlya Dryomov 	return ret;
4776922dab61SIlya Dryomov }
4777922dab61SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_unwatch);
4778922dab61SIlya Dryomov 
4779922dab61SIlya Dryomov static int osd_req_op_notify_ack_init(struct ceph_osd_request *req, int which,
4780922dab61SIlya Dryomov 				      u64 notify_id, u64 cookie, void *payload,
47816d54228fSIlya Dryomov 				      u32 payload_len)
4782922dab61SIlya Dryomov {
4783922dab61SIlya Dryomov 	struct ceph_osd_req_op *op;
4784922dab61SIlya Dryomov 	struct ceph_pagelist *pl;
4785922dab61SIlya Dryomov 	int ret;
4786922dab61SIlya Dryomov 
4787042f6498SJeff Layton 	op = osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY_ACK, 0);
4788922dab61SIlya Dryomov 
478933165d47SIlya Dryomov 	pl = ceph_pagelist_alloc(GFP_NOIO);
4790922dab61SIlya Dryomov 	if (!pl)
4791922dab61SIlya Dryomov 		return -ENOMEM;
4792922dab61SIlya Dryomov 
4793922dab61SIlya Dryomov 	ret = ceph_pagelist_encode_64(pl, notify_id);
4794922dab61SIlya Dryomov 	ret |= ceph_pagelist_encode_64(pl, cookie);
4795922dab61SIlya Dryomov 	if (payload) {
4796922dab61SIlya Dryomov 		ret |= ceph_pagelist_encode_32(pl, payload_len);
4797922dab61SIlya Dryomov 		ret |= ceph_pagelist_append(pl, payload, payload_len);
4798922dab61SIlya Dryomov 	} else {
4799922dab61SIlya Dryomov 		ret |= ceph_pagelist_encode_32(pl, 0);
4800922dab61SIlya Dryomov 	}
4801922dab61SIlya Dryomov 	if (ret) {
4802922dab61SIlya Dryomov 		ceph_pagelist_release(pl);
4803922dab61SIlya Dryomov 		return -ENOMEM;
4804922dab61SIlya Dryomov 	}
4805922dab61SIlya Dryomov 
4806922dab61SIlya Dryomov 	ceph_osd_data_pagelist_init(&op->notify_ack.request_data, pl);
4807922dab61SIlya Dryomov 	op->indata_len = pl->length;
4808922dab61SIlya Dryomov 	return 0;
4809922dab61SIlya Dryomov }
4810922dab61SIlya Dryomov 
4811922dab61SIlya Dryomov int ceph_osdc_notify_ack(struct ceph_osd_client *osdc,
4812922dab61SIlya Dryomov 			 struct ceph_object_id *oid,
4813922dab61SIlya Dryomov 			 struct ceph_object_locator *oloc,
4814922dab61SIlya Dryomov 			 u64 notify_id,
4815922dab61SIlya Dryomov 			 u64 cookie,
4816922dab61SIlya Dryomov 			 void *payload,
48176d54228fSIlya Dryomov 			 u32 payload_len)
4818922dab61SIlya Dryomov {
4819922dab61SIlya Dryomov 	struct ceph_osd_request *req;
4820922dab61SIlya Dryomov 	int ret;
4821922dab61SIlya Dryomov 
4822922dab61SIlya Dryomov 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4823922dab61SIlya Dryomov 	if (!req)
4824922dab61SIlya Dryomov 		return -ENOMEM;
4825922dab61SIlya Dryomov 
4826922dab61SIlya Dryomov 	ceph_oid_copy(&req->r_base_oid, oid);
4827922dab61SIlya Dryomov 	ceph_oloc_copy(&req->r_base_oloc, oloc);
4828922dab61SIlya Dryomov 	req->r_flags = CEPH_OSD_FLAG_READ;
4829922dab61SIlya Dryomov 
483026f887e0SIlya Dryomov 	ret = osd_req_op_notify_ack_init(req, 0, notify_id, cookie, payload,
483126f887e0SIlya Dryomov 					 payload_len);
4832922dab61SIlya Dryomov 	if (ret)
4833922dab61SIlya Dryomov 		goto out_put_req;
4834922dab61SIlya Dryomov 
483526f887e0SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4836922dab61SIlya Dryomov 	if (ret)
4837922dab61SIlya Dryomov 		goto out_put_req;
4838922dab61SIlya Dryomov 
4839a8af0d68SJeff Layton 	ceph_osdc_start_request(osdc, req);
4840922dab61SIlya Dryomov 	ret = ceph_osdc_wait_request(osdc, req);
4841922dab61SIlya Dryomov 
4842922dab61SIlya Dryomov out_put_req:
4843922dab61SIlya Dryomov 	ceph_osdc_put_request(req);
4844922dab61SIlya Dryomov 	return ret;
4845922dab61SIlya Dryomov }
4846922dab61SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_notify_ack);
4847922dab61SIlya Dryomov 
484819079203SIlya Dryomov /*
484919079203SIlya Dryomov  * @timeout: in seconds
485019079203SIlya Dryomov  *
485119079203SIlya Dryomov  * @preply_{pages,len} are initialized both on success and error.
485219079203SIlya Dryomov  * The caller is responsible for:
485319079203SIlya Dryomov  *
485419079203SIlya Dryomov  *     ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len))
485519079203SIlya Dryomov  */
485619079203SIlya Dryomov int ceph_osdc_notify(struct ceph_osd_client *osdc,
485719079203SIlya Dryomov 		     struct ceph_object_id *oid,
485819079203SIlya Dryomov 		     struct ceph_object_locator *oloc,
485919079203SIlya Dryomov 		     void *payload,
48606d54228fSIlya Dryomov 		     u32 payload_len,
486119079203SIlya Dryomov 		     u32 timeout,
486219079203SIlya Dryomov 		     struct page ***preply_pages,
486319079203SIlya Dryomov 		     size_t *preply_len)
486419079203SIlya Dryomov {
486519079203SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
486619079203SIlya Dryomov 	int ret;
486719079203SIlya Dryomov 
486819079203SIlya Dryomov 	WARN_ON(!timeout);
486919079203SIlya Dryomov 	if (preply_pages) {
487019079203SIlya Dryomov 		*preply_pages = NULL;
487119079203SIlya Dryomov 		*preply_len = 0;
487219079203SIlya Dryomov 	}
487319079203SIlya Dryomov 
487419079203SIlya Dryomov 	lreq = linger_alloc(osdc);
487519079203SIlya Dryomov 	if (!lreq)
487619079203SIlya Dryomov 		return -ENOMEM;
487719079203SIlya Dryomov 
487875dbb685SIlya Dryomov 	lreq->request_pl = ceph_pagelist_alloc(GFP_NOIO);
487975dbb685SIlya Dryomov 	if (!lreq->request_pl) {
488075dbb685SIlya Dryomov 		ret = -ENOMEM;
488175dbb685SIlya Dryomov 		goto out_put_lreq;
488275dbb685SIlya Dryomov 	}
488375dbb685SIlya Dryomov 
488475dbb685SIlya Dryomov 	ret = ceph_pagelist_encode_32(lreq->request_pl, 1); /* prot_ver */
488575dbb685SIlya Dryomov 	ret |= ceph_pagelist_encode_32(lreq->request_pl, timeout);
488675dbb685SIlya Dryomov 	ret |= ceph_pagelist_encode_32(lreq->request_pl, payload_len);
488775dbb685SIlya Dryomov 	ret |= ceph_pagelist_append(lreq->request_pl, payload, payload_len);
488875dbb685SIlya Dryomov 	if (ret) {
488975dbb685SIlya Dryomov 		ret = -ENOMEM;
489075dbb685SIlya Dryomov 		goto out_put_lreq;
489175dbb685SIlya Dryomov 	}
489275dbb685SIlya Dryomov 
489375dbb685SIlya Dryomov 	/* for notify_id */
489475dbb685SIlya Dryomov 	lreq->notify_id_pages = ceph_alloc_page_vector(1, GFP_NOIO);
489575dbb685SIlya Dryomov 	if (IS_ERR(lreq->notify_id_pages)) {
489675dbb685SIlya Dryomov 		ret = PTR_ERR(lreq->notify_id_pages);
489775dbb685SIlya Dryomov 		lreq->notify_id_pages = NULL;
489875dbb685SIlya Dryomov 		goto out_put_lreq;
489975dbb685SIlya Dryomov 	}
490075dbb685SIlya Dryomov 
490119079203SIlya Dryomov 	lreq->preply_pages = preply_pages;
490219079203SIlya Dryomov 	lreq->preply_len = preply_len;
490319079203SIlya Dryomov 
490419079203SIlya Dryomov 	ceph_oid_copy(&lreq->t.base_oid, oid);
490519079203SIlya Dryomov 	ceph_oloc_copy(&lreq->t.base_oloc, oloc);
490619079203SIlya Dryomov 	lreq->t.flags = CEPH_OSD_FLAG_READ;
490719079203SIlya Dryomov 
490881c65213SIlya Dryomov 	linger_submit(lreq);
490919079203SIlya Dryomov 	ret = linger_reg_commit_wait(lreq);
491019079203SIlya Dryomov 	if (!ret)
4911e6e28432SIlya Dryomov 		ret = linger_notify_finish_wait(lreq,
4912e6e28432SIlya Dryomov 				 msecs_to_jiffies(2 * timeout * MSEC_PER_SEC));
491319079203SIlya Dryomov 	else
491419079203SIlya Dryomov 		dout("lreq %p failed to initiate notify %d\n", lreq, ret);
491519079203SIlya Dryomov 
491619079203SIlya Dryomov 	linger_cancel(lreq);
491719079203SIlya Dryomov out_put_lreq:
491819079203SIlya Dryomov 	linger_put(lreq);
491919079203SIlya Dryomov 	return ret;
492019079203SIlya Dryomov }
492119079203SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_notify);
492219079203SIlya Dryomov 
49233d14c5d2SYehuda Sadeh /*
4924b07d3c4bSIlya Dryomov  * Return the number of milliseconds since the watch was last
4925b07d3c4bSIlya Dryomov  * confirmed, or an error.  If there is an error, the watch is no
4926b07d3c4bSIlya Dryomov  * longer valid, and should be destroyed with ceph_osdc_unwatch().
4927b07d3c4bSIlya Dryomov  */
4928b07d3c4bSIlya Dryomov int ceph_osdc_watch_check(struct ceph_osd_client *osdc,
4929b07d3c4bSIlya Dryomov 			  struct ceph_osd_linger_request *lreq)
4930b07d3c4bSIlya Dryomov {
4931b07d3c4bSIlya Dryomov 	unsigned long stamp, age;
4932b07d3c4bSIlya Dryomov 	int ret;
4933b07d3c4bSIlya Dryomov 
4934b07d3c4bSIlya Dryomov 	down_read(&osdc->lock);
4935b07d3c4bSIlya Dryomov 	mutex_lock(&lreq->lock);
4936b07d3c4bSIlya Dryomov 	stamp = lreq->watch_valid_thru;
4937b07d3c4bSIlya Dryomov 	if (!list_empty(&lreq->pending_lworks)) {
4938b07d3c4bSIlya Dryomov 		struct linger_work *lwork =
4939b07d3c4bSIlya Dryomov 		    list_first_entry(&lreq->pending_lworks,
4940b07d3c4bSIlya Dryomov 				     struct linger_work,
4941b07d3c4bSIlya Dryomov 				     pending_item);
4942b07d3c4bSIlya Dryomov 
4943b07d3c4bSIlya Dryomov 		if (time_before(lwork->queued_stamp, stamp))
4944b07d3c4bSIlya Dryomov 			stamp = lwork->queued_stamp;
4945b07d3c4bSIlya Dryomov 	}
4946b07d3c4bSIlya Dryomov 	age = jiffies - stamp;
4947b07d3c4bSIlya Dryomov 	dout("%s lreq %p linger_id %llu age %lu last_error %d\n", __func__,
4948b07d3c4bSIlya Dryomov 	     lreq, lreq->linger_id, age, lreq->last_error);
4949b07d3c4bSIlya Dryomov 	/* we are truncating to msecs, so return a safe upper bound */
4950b07d3c4bSIlya Dryomov 	ret = lreq->last_error ?: 1 + jiffies_to_msecs(age);
4951b07d3c4bSIlya Dryomov 
4952b07d3c4bSIlya Dryomov 	mutex_unlock(&lreq->lock);
4953b07d3c4bSIlya Dryomov 	up_read(&osdc->lock);
4954b07d3c4bSIlya Dryomov 	return ret;
4955b07d3c4bSIlya Dryomov }
4956b07d3c4bSIlya Dryomov 
4957a4ed38d7SDouglas Fuller static int decode_watcher(void **p, void *end, struct ceph_watch_item *item)
4958a4ed38d7SDouglas Fuller {
4959a4ed38d7SDouglas Fuller 	u8 struct_v;
4960a4ed38d7SDouglas Fuller 	u32 struct_len;
4961a4ed38d7SDouglas Fuller 	int ret;
4962a4ed38d7SDouglas Fuller 
4963a4ed38d7SDouglas Fuller 	ret = ceph_start_decoding(p, end, 2, "watch_item_t",
4964a4ed38d7SDouglas Fuller 				  &struct_v, &struct_len);
4965a4ed38d7SDouglas Fuller 	if (ret)
496651fc7ab4SJeff Layton 		goto bad;
4967a4ed38d7SDouglas Fuller 
496851fc7ab4SJeff Layton 	ret = -EINVAL;
496951fc7ab4SJeff Layton 	ceph_decode_copy_safe(p, end, &item->name, sizeof(item->name), bad);
497051fc7ab4SJeff Layton 	ceph_decode_64_safe(p, end, item->cookie, bad);
497151fc7ab4SJeff Layton 	ceph_decode_skip_32(p, end, bad); /* skip timeout seconds */
497251fc7ab4SJeff Layton 
4973a4ed38d7SDouglas Fuller 	if (struct_v >= 2) {
497451fc7ab4SJeff Layton 		ret = ceph_decode_entity_addr(p, end, &item->addr);
497551fc7ab4SJeff Layton 		if (ret)
497651fc7ab4SJeff Layton 			goto bad;
497751fc7ab4SJeff Layton 	} else {
497851fc7ab4SJeff Layton 		ret = 0;
4979a4ed38d7SDouglas Fuller 	}
4980a4ed38d7SDouglas Fuller 
4981a4ed38d7SDouglas Fuller 	dout("%s %s%llu cookie %llu addr %s\n", __func__,
4982a4ed38d7SDouglas Fuller 	     ENTITY_NAME(item->name), item->cookie,
4983b726ec97SJeff Layton 	     ceph_pr_addr(&item->addr));
498451fc7ab4SJeff Layton bad:
498551fc7ab4SJeff Layton 	return ret;
4986a4ed38d7SDouglas Fuller }
4987a4ed38d7SDouglas Fuller 
4988a4ed38d7SDouglas Fuller static int decode_watchers(void **p, void *end,
4989a4ed38d7SDouglas Fuller 			   struct ceph_watch_item **watchers,
4990a4ed38d7SDouglas Fuller 			   u32 *num_watchers)
4991a4ed38d7SDouglas Fuller {
4992a4ed38d7SDouglas Fuller 	u8 struct_v;
4993a4ed38d7SDouglas Fuller 	u32 struct_len;
4994a4ed38d7SDouglas Fuller 	int i;
4995a4ed38d7SDouglas Fuller 	int ret;
4996a4ed38d7SDouglas Fuller 
4997a4ed38d7SDouglas Fuller 	ret = ceph_start_decoding(p, end, 1, "obj_list_watch_response_t",
4998a4ed38d7SDouglas Fuller 				  &struct_v, &struct_len);
4999a4ed38d7SDouglas Fuller 	if (ret)
5000a4ed38d7SDouglas Fuller 		return ret;
5001a4ed38d7SDouglas Fuller 
5002a4ed38d7SDouglas Fuller 	*num_watchers = ceph_decode_32(p);
5003a4ed38d7SDouglas Fuller 	*watchers = kcalloc(*num_watchers, sizeof(**watchers), GFP_NOIO);
5004a4ed38d7SDouglas Fuller 	if (!*watchers)
5005a4ed38d7SDouglas Fuller 		return -ENOMEM;
5006a4ed38d7SDouglas Fuller 
5007a4ed38d7SDouglas Fuller 	for (i = 0; i < *num_watchers; i++) {
5008a4ed38d7SDouglas Fuller 		ret = decode_watcher(p, end, *watchers + i);
5009a4ed38d7SDouglas Fuller 		if (ret) {
5010a4ed38d7SDouglas Fuller 			kfree(*watchers);
5011a4ed38d7SDouglas Fuller 			return ret;
5012a4ed38d7SDouglas Fuller 		}
5013a4ed38d7SDouglas Fuller 	}
5014a4ed38d7SDouglas Fuller 
5015a4ed38d7SDouglas Fuller 	return 0;
5016a4ed38d7SDouglas Fuller }
5017a4ed38d7SDouglas Fuller 
5018a4ed38d7SDouglas Fuller /*
5019a4ed38d7SDouglas Fuller  * On success, the caller is responsible for:
5020a4ed38d7SDouglas Fuller  *
5021a4ed38d7SDouglas Fuller  *     kfree(watchers);
5022a4ed38d7SDouglas Fuller  */
5023a4ed38d7SDouglas Fuller int ceph_osdc_list_watchers(struct ceph_osd_client *osdc,
5024a4ed38d7SDouglas Fuller 			    struct ceph_object_id *oid,
5025a4ed38d7SDouglas Fuller 			    struct ceph_object_locator *oloc,
5026a4ed38d7SDouglas Fuller 			    struct ceph_watch_item **watchers,
5027a4ed38d7SDouglas Fuller 			    u32 *num_watchers)
5028a4ed38d7SDouglas Fuller {
5029a4ed38d7SDouglas Fuller 	struct ceph_osd_request *req;
5030a4ed38d7SDouglas Fuller 	struct page **pages;
5031a4ed38d7SDouglas Fuller 	int ret;
5032a4ed38d7SDouglas Fuller 
5033a4ed38d7SDouglas Fuller 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
5034a4ed38d7SDouglas Fuller 	if (!req)
5035a4ed38d7SDouglas Fuller 		return -ENOMEM;
5036a4ed38d7SDouglas Fuller 
5037a4ed38d7SDouglas Fuller 	ceph_oid_copy(&req->r_base_oid, oid);
5038a4ed38d7SDouglas Fuller 	ceph_oloc_copy(&req->r_base_oloc, oloc);
5039a4ed38d7SDouglas Fuller 	req->r_flags = CEPH_OSD_FLAG_READ;
5040a4ed38d7SDouglas Fuller 
5041a4ed38d7SDouglas Fuller 	pages = ceph_alloc_page_vector(1, GFP_NOIO);
5042a4ed38d7SDouglas Fuller 	if (IS_ERR(pages)) {
5043a4ed38d7SDouglas Fuller 		ret = PTR_ERR(pages);
5044a4ed38d7SDouglas Fuller 		goto out_put_req;
5045a4ed38d7SDouglas Fuller 	}
5046a4ed38d7SDouglas Fuller 
5047a4ed38d7SDouglas Fuller 	osd_req_op_init(req, 0, CEPH_OSD_OP_LIST_WATCHERS, 0);
5048a4ed38d7SDouglas Fuller 	ceph_osd_data_pages_init(osd_req_op_data(req, 0, list_watchers,
5049a4ed38d7SDouglas Fuller 						 response_data),
5050a4ed38d7SDouglas Fuller 				 pages, PAGE_SIZE, 0, false, true);
5051a4ed38d7SDouglas Fuller 
505226f887e0SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
505326f887e0SIlya Dryomov 	if (ret)
505426f887e0SIlya Dryomov 		goto out_put_req;
505526f887e0SIlya Dryomov 
5056a8af0d68SJeff Layton 	ceph_osdc_start_request(osdc, req);
5057a4ed38d7SDouglas Fuller 	ret = ceph_osdc_wait_request(osdc, req);
5058a4ed38d7SDouglas Fuller 	if (ret >= 0) {
5059a4ed38d7SDouglas Fuller 		void *p = page_address(pages[0]);
5060a4ed38d7SDouglas Fuller 		void *const end = p + req->r_ops[0].outdata_len;
5061a4ed38d7SDouglas Fuller 
5062a4ed38d7SDouglas Fuller 		ret = decode_watchers(&p, end, watchers, num_watchers);
5063a4ed38d7SDouglas Fuller 	}
5064a4ed38d7SDouglas Fuller 
5065a4ed38d7SDouglas Fuller out_put_req:
5066a4ed38d7SDouglas Fuller 	ceph_osdc_put_request(req);
5067a4ed38d7SDouglas Fuller 	return ret;
5068a4ed38d7SDouglas Fuller }
5069a4ed38d7SDouglas Fuller EXPORT_SYMBOL(ceph_osdc_list_watchers);
5070a4ed38d7SDouglas Fuller 
5071b07d3c4bSIlya Dryomov /*
5072dd935f44SJosh Durgin  * Call all pending notify callbacks - for use after a watch is
5073dd935f44SJosh Durgin  * unregistered, to make sure no more callbacks for it will be invoked
5074dd935f44SJosh Durgin  */
5075f6479449Sstephen hemminger void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
5076dd935f44SJosh Durgin {
507799d16943SIlya Dryomov 	dout("%s osdc %p\n", __func__, osdc);
5078dd935f44SJosh Durgin 	flush_workqueue(osdc->notify_wq);
5079dd935f44SJosh Durgin }
5080dd935f44SJosh Durgin EXPORT_SYMBOL(ceph_osdc_flush_notifies);
5081dd935f44SJosh Durgin 
50827cca78c9SIlya Dryomov void ceph_osdc_maybe_request_map(struct ceph_osd_client *osdc)
50837cca78c9SIlya Dryomov {
50847cca78c9SIlya Dryomov 	down_read(&osdc->lock);
50857cca78c9SIlya Dryomov 	maybe_request_map(osdc);
50867cca78c9SIlya Dryomov 	up_read(&osdc->lock);
50877cca78c9SIlya Dryomov }
50887cca78c9SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_maybe_request_map);
5089dd935f44SJosh Durgin 
5090dd935f44SJosh Durgin /*
5091428a7158SDouglas Fuller  * Execute an OSD class method on an object.
5092428a7158SDouglas Fuller  *
5093428a7158SDouglas Fuller  * @flags: CEPH_OSD_FLAG_*
50942544a020SIlya Dryomov  * @resp_len: in/out param for reply length
5095428a7158SDouglas Fuller  */
5096428a7158SDouglas Fuller int ceph_osdc_call(struct ceph_osd_client *osdc,
5097428a7158SDouglas Fuller 		   struct ceph_object_id *oid,
5098428a7158SDouglas Fuller 		   struct ceph_object_locator *oloc,
5099428a7158SDouglas Fuller 		   const char *class, const char *method,
5100428a7158SDouglas Fuller 		   unsigned int flags,
5101428a7158SDouglas Fuller 		   struct page *req_page, size_t req_len,
510268ada915SIlya Dryomov 		   struct page **resp_pages, size_t *resp_len)
5103428a7158SDouglas Fuller {
5104428a7158SDouglas Fuller 	struct ceph_osd_request *req;
5105428a7158SDouglas Fuller 	int ret;
5106428a7158SDouglas Fuller 
510768ada915SIlya Dryomov 	if (req_len > PAGE_SIZE)
51082544a020SIlya Dryomov 		return -E2BIG;
51092544a020SIlya Dryomov 
5110428a7158SDouglas Fuller 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
5111428a7158SDouglas Fuller 	if (!req)
5112428a7158SDouglas Fuller 		return -ENOMEM;
5113428a7158SDouglas Fuller 
5114428a7158SDouglas Fuller 	ceph_oid_copy(&req->r_base_oid, oid);
5115428a7158SDouglas Fuller 	ceph_oloc_copy(&req->r_base_oloc, oloc);
5116428a7158SDouglas Fuller 	req->r_flags = flags;
5117428a7158SDouglas Fuller 
511824639ce5SIlya Dryomov 	ret = osd_req_op_cls_init(req, 0, class, method);
5119fe943d50SChengguang Xu 	if (ret)
5120fe943d50SChengguang Xu 		goto out_put_req;
5121fe943d50SChengguang Xu 
5122428a7158SDouglas Fuller 	if (req_page)
5123428a7158SDouglas Fuller 		osd_req_op_cls_request_data_pages(req, 0, &req_page, req_len,
5124428a7158SDouglas Fuller 						  0, false, false);
512568ada915SIlya Dryomov 	if (resp_pages)
512668ada915SIlya Dryomov 		osd_req_op_cls_response_data_pages(req, 0, resp_pages,
51272544a020SIlya Dryomov 						   *resp_len, 0, false, false);
5128428a7158SDouglas Fuller 
512926f887e0SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
513026f887e0SIlya Dryomov 	if (ret)
513126f887e0SIlya Dryomov 		goto out_put_req;
513226f887e0SIlya Dryomov 
5133a8af0d68SJeff Layton 	ceph_osdc_start_request(osdc, req);
5134428a7158SDouglas Fuller 	ret = ceph_osdc_wait_request(osdc, req);
5135428a7158SDouglas Fuller 	if (ret >= 0) {
5136428a7158SDouglas Fuller 		ret = req->r_ops[0].rval;
513768ada915SIlya Dryomov 		if (resp_pages)
5138428a7158SDouglas Fuller 			*resp_len = req->r_ops[0].outdata_len;
5139428a7158SDouglas Fuller 	}
5140428a7158SDouglas Fuller 
5141428a7158SDouglas Fuller out_put_req:
5142428a7158SDouglas Fuller 	ceph_osdc_put_request(req);
5143428a7158SDouglas Fuller 	return ret;
5144428a7158SDouglas Fuller }
5145428a7158SDouglas Fuller EXPORT_SYMBOL(ceph_osdc_call);
5146428a7158SDouglas Fuller 
5147428a7158SDouglas Fuller /*
5148120a75eaSYan, Zheng  * reset all osd connections
5149120a75eaSYan, Zheng  */
5150120a75eaSYan, Zheng void ceph_osdc_reopen_osds(struct ceph_osd_client *osdc)
5151120a75eaSYan, Zheng {
5152120a75eaSYan, Zheng 	struct rb_node *n;
5153120a75eaSYan, Zheng 
5154120a75eaSYan, Zheng 	down_write(&osdc->lock);
5155120a75eaSYan, Zheng 	for (n = rb_first(&osdc->osds); n; ) {
5156120a75eaSYan, Zheng 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
5157120a75eaSYan, Zheng 
5158120a75eaSYan, Zheng 		n = rb_next(n);
5159120a75eaSYan, Zheng 		if (!reopen_osd(osd))
5160120a75eaSYan, Zheng 			kick_osd_requests(osd);
5161120a75eaSYan, Zheng 	}
5162120a75eaSYan, Zheng 	up_write(&osdc->lock);
5163120a75eaSYan, Zheng }
5164120a75eaSYan, Zheng 
5165120a75eaSYan, Zheng /*
51663d14c5d2SYehuda Sadeh  * init, shutdown
51673d14c5d2SYehuda Sadeh  */
51683d14c5d2SYehuda Sadeh int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
51693d14c5d2SYehuda Sadeh {
51703d14c5d2SYehuda Sadeh 	int err;
51713d14c5d2SYehuda Sadeh 
51723d14c5d2SYehuda Sadeh 	dout("init\n");
51733d14c5d2SYehuda Sadeh 	osdc->client = client;
51745aea3dcdSIlya Dryomov 	init_rwsem(&osdc->lock);
51753d14c5d2SYehuda Sadeh 	osdc->osds = RB_ROOT;
51763d14c5d2SYehuda Sadeh 	INIT_LIST_HEAD(&osdc->osd_lru);
51779dd2845cSIlya Dryomov 	spin_lock_init(&osdc->osd_lru_lock);
51785aea3dcdSIlya Dryomov 	osd_init(&osdc->homeless_osd);
51795aea3dcdSIlya Dryomov 	osdc->homeless_osd.o_osdc = osdc;
51805aea3dcdSIlya Dryomov 	osdc->homeless_osd.o_osd = CEPH_HOMELESS_OSD;
5181264048afSIlya Dryomov 	osdc->last_linger_id = CEPH_LINGER_ID_START;
5182922dab61SIlya Dryomov 	osdc->linger_requests = RB_ROOT;
51834609245eSIlya Dryomov 	osdc->map_checks = RB_ROOT;
51844609245eSIlya Dryomov 	osdc->linger_map_checks = RB_ROOT;
51853d14c5d2SYehuda Sadeh 	INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
51863d14c5d2SYehuda Sadeh 	INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
51873d14c5d2SYehuda Sadeh 
51883d14c5d2SYehuda Sadeh 	err = -ENOMEM;
5189e5253a7bSIlya Dryomov 	osdc->osdmap = ceph_osdmap_alloc();
5190e5253a7bSIlya Dryomov 	if (!osdc->osdmap)
5191e5253a7bSIlya Dryomov 		goto out;
5192e5253a7bSIlya Dryomov 
51939e767adbSIlya Dryomov 	osdc->req_mempool = mempool_create_slab_pool(10,
51949e767adbSIlya Dryomov 						     ceph_osd_request_cache);
51953d14c5d2SYehuda Sadeh 	if (!osdc->req_mempool)
5196e5253a7bSIlya Dryomov 		goto out_map;
51973d14c5d2SYehuda Sadeh 
5198d50b409fSSage Weil 	err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
51990d9c1ab3SIlya Dryomov 				PAGE_SIZE, CEPH_OSD_SLAB_OPS, 10, "osd_op");
52003d14c5d2SYehuda Sadeh 	if (err < 0)
52013d14c5d2SYehuda Sadeh 		goto out_mempool;
5202d50b409fSSage Weil 	err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
52030d9c1ab3SIlya Dryomov 				PAGE_SIZE, CEPH_OSD_SLAB_OPS, 10,
52040d9c1ab3SIlya Dryomov 				"osd_op_reply");
52053d14c5d2SYehuda Sadeh 	if (err < 0)
52063d14c5d2SYehuda Sadeh 		goto out_msgpool;
5207a40c4f10SYehuda Sadeh 
5208dbcae088SDan Carpenter 	err = -ENOMEM;
5209a40c4f10SYehuda Sadeh 	osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
5210dbcae088SDan Carpenter 	if (!osdc->notify_wq)
5211c172ec5cSIlya Dryomov 		goto out_msgpool_reply;
5212c172ec5cSIlya Dryomov 
521388bc1922SIlya Dryomov 	osdc->completion_wq = create_singlethread_workqueue("ceph-completion");
521488bc1922SIlya Dryomov 	if (!osdc->completion_wq)
521588bc1922SIlya Dryomov 		goto out_notify_wq;
521688bc1922SIlya Dryomov 
5217fbca9635SIlya Dryomov 	schedule_delayed_work(&osdc->timeout_work,
5218fbca9635SIlya Dryomov 			      osdc->client->options->osd_keepalive_timeout);
5219b37ee1b9SIlya Dryomov 	schedule_delayed_work(&osdc->osds_timeout_work,
5220b37ee1b9SIlya Dryomov 	    round_jiffies_relative(osdc->client->options->osd_idle_ttl));
5221b37ee1b9SIlya Dryomov 
52223d14c5d2SYehuda Sadeh 	return 0;
52233d14c5d2SYehuda Sadeh 
522488bc1922SIlya Dryomov out_notify_wq:
522588bc1922SIlya Dryomov 	destroy_workqueue(osdc->notify_wq);
5226c172ec5cSIlya Dryomov out_msgpool_reply:
5227c172ec5cSIlya Dryomov 	ceph_msgpool_destroy(&osdc->msgpool_op_reply);
52283d14c5d2SYehuda Sadeh out_msgpool:
52293d14c5d2SYehuda Sadeh 	ceph_msgpool_destroy(&osdc->msgpool_op);
52303d14c5d2SYehuda Sadeh out_mempool:
52313d14c5d2SYehuda Sadeh 	mempool_destroy(osdc->req_mempool);
5232e5253a7bSIlya Dryomov out_map:
5233e5253a7bSIlya Dryomov 	ceph_osdmap_destroy(osdc->osdmap);
52343d14c5d2SYehuda Sadeh out:
52353d14c5d2SYehuda Sadeh 	return err;
52363d14c5d2SYehuda Sadeh }
52373d14c5d2SYehuda Sadeh 
52383d14c5d2SYehuda Sadeh void ceph_osdc_stop(struct ceph_osd_client *osdc)
52393d14c5d2SYehuda Sadeh {
524088bc1922SIlya Dryomov 	destroy_workqueue(osdc->completion_wq);
5241a40c4f10SYehuda Sadeh 	destroy_workqueue(osdc->notify_wq);
52423d14c5d2SYehuda Sadeh 	cancel_delayed_work_sync(&osdc->timeout_work);
52433d14c5d2SYehuda Sadeh 	cancel_delayed_work_sync(&osdc->osds_timeout_work);
524442a2c09fSIlya Dryomov 
52455aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
524642a2c09fSIlya Dryomov 	while (!RB_EMPTY_ROOT(&osdc->osds)) {
524742a2c09fSIlya Dryomov 		struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
524842a2c09fSIlya Dryomov 						struct ceph_osd, o_node);
52495aea3dcdSIlya Dryomov 		close_osd(osd);
525042a2c09fSIlya Dryomov 	}
52515aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
525202113a0fSElena Reshetova 	WARN_ON(refcount_read(&osdc->homeless_osd.o_ref) != 1);
52535aea3dcdSIlya Dryomov 	osd_cleanup(&osdc->homeless_osd);
52545aea3dcdSIlya Dryomov 
52555aea3dcdSIlya Dryomov 	WARN_ON(!list_empty(&osdc->osd_lru));
5256922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_requests));
52574609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osdc->map_checks));
52584609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_map_checks));
52595aea3dcdSIlya Dryomov 	WARN_ON(atomic_read(&osdc->num_requests));
52605aea3dcdSIlya Dryomov 	WARN_ON(atomic_read(&osdc->num_homeless));
526142a2c09fSIlya Dryomov 
52623d14c5d2SYehuda Sadeh 	ceph_osdmap_destroy(osdc->osdmap);
52633d14c5d2SYehuda Sadeh 	mempool_destroy(osdc->req_mempool);
52643d14c5d2SYehuda Sadeh 	ceph_msgpool_destroy(&osdc->msgpool_op);
52653d14c5d2SYehuda Sadeh 	ceph_msgpool_destroy(&osdc->msgpool_op_reply);
52663d14c5d2SYehuda Sadeh }
52673d14c5d2SYehuda Sadeh 
5268aca39d9eSLuís Henriques int osd_req_op_copy_from_init(struct ceph_osd_request *req,
526923ddf9beSLuis Henriques 			      u64 src_snapid, u64 src_version,
527023ddf9beSLuis Henriques 			      struct ceph_object_id *src_oid,
527123ddf9beSLuis Henriques 			      struct ceph_object_locator *src_oloc,
527223ddf9beSLuis Henriques 			      u32 src_fadvise_flags,
527323ddf9beSLuis Henriques 			      u32 dst_fadvise_flags,
527478beb0ffSLuis Henriques 			      u32 truncate_seq, u64 truncate_size,
527523ddf9beSLuis Henriques 			      u8 copy_from_flags)
527623ddf9beSLuis Henriques {
527723ddf9beSLuis Henriques 	struct ceph_osd_req_op *op;
527823ddf9beSLuis Henriques 	struct page **pages;
527923ddf9beSLuis Henriques 	void *p, *end;
528023ddf9beSLuis Henriques 
528123ddf9beSLuis Henriques 	pages = ceph_alloc_page_vector(1, GFP_KERNEL);
528223ddf9beSLuis Henriques 	if (IS_ERR(pages))
528323ddf9beSLuis Henriques 		return PTR_ERR(pages);
528423ddf9beSLuis Henriques 
5285042f6498SJeff Layton 	op = osd_req_op_init(req, 0, CEPH_OSD_OP_COPY_FROM2,
528678beb0ffSLuis Henriques 			     dst_fadvise_flags);
528723ddf9beSLuis Henriques 	op->copy_from.snapid = src_snapid;
528823ddf9beSLuis Henriques 	op->copy_from.src_version = src_version;
528923ddf9beSLuis Henriques 	op->copy_from.flags = copy_from_flags;
529023ddf9beSLuis Henriques 	op->copy_from.src_fadvise_flags = src_fadvise_flags;
529123ddf9beSLuis Henriques 
529223ddf9beSLuis Henriques 	p = page_address(pages[0]);
529323ddf9beSLuis Henriques 	end = p + PAGE_SIZE;
529423ddf9beSLuis Henriques 	ceph_encode_string(&p, end, src_oid->name, src_oid->name_len);
529523ddf9beSLuis Henriques 	encode_oloc(&p, end, src_oloc);
529678beb0ffSLuis Henriques 	ceph_encode_32(&p, truncate_seq);
529778beb0ffSLuis Henriques 	ceph_encode_64(&p, truncate_size);
529823ddf9beSLuis Henriques 	op->indata_len = PAGE_SIZE - (end - p);
529923ddf9beSLuis Henriques 
530023ddf9beSLuis Henriques 	ceph_osd_data_pages_init(&op->copy_from.osd_data, pages,
530123ddf9beSLuis Henriques 				 op->indata_len, 0, false, true);
530223ddf9beSLuis Henriques 	return 0;
530323ddf9beSLuis Henriques }
5304aca39d9eSLuís Henriques EXPORT_SYMBOL(osd_req_op_copy_from_init);
530523ddf9beSLuis Henriques 
530657a35dfbSChengguang Xu int __init ceph_osdc_setup(void)
53075522ae0bSAlex Elder {
53083f1af42aSIlya Dryomov 	size_t size = sizeof(struct ceph_osd_request) +
53093f1af42aSIlya Dryomov 	    CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
53103f1af42aSIlya Dryomov 
53115522ae0bSAlex Elder 	BUG_ON(ceph_osd_request_cache);
53123f1af42aSIlya Dryomov 	ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
53133f1af42aSIlya Dryomov 						   0, 0, NULL);
53145522ae0bSAlex Elder 
53155522ae0bSAlex Elder 	return ceph_osd_request_cache ? 0 : -ENOMEM;
53165522ae0bSAlex Elder }
53175522ae0bSAlex Elder 
53185522ae0bSAlex Elder void ceph_osdc_cleanup(void)
53195522ae0bSAlex Elder {
53205522ae0bSAlex Elder 	BUG_ON(!ceph_osd_request_cache);
53215522ae0bSAlex Elder 	kmem_cache_destroy(ceph_osd_request_cache);
53225522ae0bSAlex Elder 	ceph_osd_request_cache = NULL;
53235522ae0bSAlex Elder }
53245522ae0bSAlex Elder 
53253d14c5d2SYehuda Sadeh /*
53263d14c5d2SYehuda Sadeh  * handle incoming message
53273d14c5d2SYehuda Sadeh  */
53284972cf60SIlya Dryomov static void osd_dispatch(struct ceph_connection *con, struct ceph_msg *msg)
53293d14c5d2SYehuda Sadeh {
53303d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
53315aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
53323d14c5d2SYehuda Sadeh 	int type = le16_to_cpu(msg->hdr.type);
53333d14c5d2SYehuda Sadeh 
53343d14c5d2SYehuda Sadeh 	switch (type) {
53353d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_MAP:
53363d14c5d2SYehuda Sadeh 		ceph_osdc_handle_map(osdc, msg);
53373d14c5d2SYehuda Sadeh 		break;
53383d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_OPREPLY:
53395aea3dcdSIlya Dryomov 		handle_reply(osd, msg);
53403d14c5d2SYehuda Sadeh 		break;
5341a02a946dSIlya Dryomov 	case CEPH_MSG_OSD_BACKOFF:
5342a02a946dSIlya Dryomov 		handle_backoff(osd, msg);
5343a02a946dSIlya Dryomov 		break;
5344a40c4f10SYehuda Sadeh 	case CEPH_MSG_WATCH_NOTIFY:
5345a40c4f10SYehuda Sadeh 		handle_watch_notify(osdc, msg);
5346a40c4f10SYehuda Sadeh 		break;
53473d14c5d2SYehuda Sadeh 
53483d14c5d2SYehuda Sadeh 	default:
53493d14c5d2SYehuda Sadeh 		pr_err("received unknown message type %d %s\n", type,
53503d14c5d2SYehuda Sadeh 		       ceph_msg_type_name(type));
53513d14c5d2SYehuda Sadeh 	}
53525aea3dcdSIlya Dryomov 
53533d14c5d2SYehuda Sadeh 	ceph_msg_put(msg);
53543d14c5d2SYehuda Sadeh }
53553d14c5d2SYehuda Sadeh 
53563d14c5d2SYehuda Sadeh /*
5357d15f9d69SIlya Dryomov  * Lookup and return message for incoming reply.  Don't try to do
5358d15f9d69SIlya Dryomov  * anything about a larger than preallocated data portion of the
5359d15f9d69SIlya Dryomov  * message at the moment - for now, just skip the message.
53603d14c5d2SYehuda Sadeh  */
53613d14c5d2SYehuda Sadeh static struct ceph_msg *get_reply(struct ceph_connection *con,
53623d14c5d2SYehuda Sadeh 				  struct ceph_msg_header *hdr,
53633d14c5d2SYehuda Sadeh 				  int *skip)
53643d14c5d2SYehuda Sadeh {
53653d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
53663d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = osd->o_osdc;
53675aea3dcdSIlya Dryomov 	struct ceph_msg *m = NULL;
53683d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
53693f0a4ac5SIlya Dryomov 	int front_len = le32_to_cpu(hdr->front_len);
53703d14c5d2SYehuda Sadeh 	int data_len = le32_to_cpu(hdr->data_len);
53715aea3dcdSIlya Dryomov 	u64 tid = le64_to_cpu(hdr->tid);
53723d14c5d2SYehuda Sadeh 
53735aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
53745aea3dcdSIlya Dryomov 	if (!osd_registered(osd)) {
53755aea3dcdSIlya Dryomov 		dout("%s osd%d unknown, skipping\n", __func__, osd->o_osd);
53765aea3dcdSIlya Dryomov 		*skip = 1;
53775aea3dcdSIlya Dryomov 		goto out_unlock_osdc;
53785aea3dcdSIlya Dryomov 	}
53795aea3dcdSIlya Dryomov 	WARN_ON(osd->o_osd != le64_to_cpu(hdr->src.num));
53805aea3dcdSIlya Dryomov 
53815aea3dcdSIlya Dryomov 	mutex_lock(&osd->lock);
53825aea3dcdSIlya Dryomov 	req = lookup_request(&osd->o_requests, tid);
53833d14c5d2SYehuda Sadeh 	if (!req) {
5384cd8140c6SIlya Dryomov 		dout("%s osd%d tid %llu unknown, skipping\n", __func__,
5385cd8140c6SIlya Dryomov 		     osd->o_osd, tid);
5386d15f9d69SIlya Dryomov 		*skip = 1;
53875aea3dcdSIlya Dryomov 		goto out_unlock_session;
53883d14c5d2SYehuda Sadeh 	}
53893d14c5d2SYehuda Sadeh 
53908921d114SAlex Elder 	ceph_msg_revoke_incoming(req->r_reply);
53913d14c5d2SYehuda Sadeh 
5392f2be82b0SIlya Dryomov 	if (front_len > req->r_reply->front_alloc_len) {
5393d15f9d69SIlya Dryomov 		pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
5394d15f9d69SIlya Dryomov 			__func__, osd->o_osd, req->r_tid, front_len,
5395d15f9d69SIlya Dryomov 			req->r_reply->front_alloc_len);
53963f0a4ac5SIlya Dryomov 		m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
53973f0a4ac5SIlya Dryomov 				 false);
53983d14c5d2SYehuda Sadeh 		if (!m)
53995aea3dcdSIlya Dryomov 			goto out_unlock_session;
54003d14c5d2SYehuda Sadeh 		ceph_msg_put(req->r_reply);
54013d14c5d2SYehuda Sadeh 		req->r_reply = m;
54023d14c5d2SYehuda Sadeh 	}
54033d14c5d2SYehuda Sadeh 
5404d15f9d69SIlya Dryomov 	if (data_len > req->r_reply->data_length) {
5405d15f9d69SIlya Dryomov 		pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
5406d15f9d69SIlya Dryomov 			__func__, osd->o_osd, req->r_tid, data_len,
5407d15f9d69SIlya Dryomov 			req->r_reply->data_length);
54083d14c5d2SYehuda Sadeh 		m = NULL;
5409d15f9d69SIlya Dryomov 		*skip = 1;
54105aea3dcdSIlya Dryomov 		goto out_unlock_session;
54113d14c5d2SYehuda Sadeh 	}
5412d15f9d69SIlya Dryomov 
5413d15f9d69SIlya Dryomov 	m = ceph_msg_get(req->r_reply);
54143d14c5d2SYehuda Sadeh 	dout("get_reply tid %lld %p\n", tid, m);
54153d14c5d2SYehuda Sadeh 
54165aea3dcdSIlya Dryomov out_unlock_session:
54175aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
54185aea3dcdSIlya Dryomov out_unlock_osdc:
54195aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
54203d14c5d2SYehuda Sadeh 	return m;
54213d14c5d2SYehuda Sadeh }
54223d14c5d2SYehuda Sadeh 
542319079203SIlya Dryomov static struct ceph_msg *alloc_msg_with_page_vector(struct ceph_msg_header *hdr)
542419079203SIlya Dryomov {
542519079203SIlya Dryomov 	struct ceph_msg *m;
542619079203SIlya Dryomov 	int type = le16_to_cpu(hdr->type);
542719079203SIlya Dryomov 	u32 front_len = le32_to_cpu(hdr->front_len);
542819079203SIlya Dryomov 	u32 data_len = le32_to_cpu(hdr->data_len);
542919079203SIlya Dryomov 
54300d9c1ab3SIlya Dryomov 	m = ceph_msg_new2(type, front_len, 1, GFP_NOIO, false);
543119079203SIlya Dryomov 	if (!m)
543219079203SIlya Dryomov 		return NULL;
543319079203SIlya Dryomov 
543419079203SIlya Dryomov 	if (data_len) {
543519079203SIlya Dryomov 		struct page **pages;
543619079203SIlya Dryomov 
543719079203SIlya Dryomov 		pages = ceph_alloc_page_vector(calc_pages_for(0, data_len),
543819079203SIlya Dryomov 					       GFP_NOIO);
5439c22e853aSWei Yongjun 		if (IS_ERR(pages)) {
544019079203SIlya Dryomov 			ceph_msg_put(m);
544119079203SIlya Dryomov 			return NULL;
544219079203SIlya Dryomov 		}
544319079203SIlya Dryomov 
5444e8862740SIlya Dryomov 		ceph_msg_data_add_pages(m, pages, data_len, 0, true);
544519079203SIlya Dryomov 	}
544619079203SIlya Dryomov 
544719079203SIlya Dryomov 	return m;
544819079203SIlya Dryomov }
544919079203SIlya Dryomov 
54504972cf60SIlya Dryomov static struct ceph_msg *osd_alloc_msg(struct ceph_connection *con,
54513d14c5d2SYehuda Sadeh 				      struct ceph_msg_header *hdr,
54523d14c5d2SYehuda Sadeh 				      int *skip)
54533d14c5d2SYehuda Sadeh {
54543d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
54553d14c5d2SYehuda Sadeh 	int type = le16_to_cpu(hdr->type);
54563d14c5d2SYehuda Sadeh 
54571c20f2d2SAlex Elder 	*skip = 0;
54583d14c5d2SYehuda Sadeh 	switch (type) {
54593d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_MAP:
5460a02a946dSIlya Dryomov 	case CEPH_MSG_OSD_BACKOFF:
5461a40c4f10SYehuda Sadeh 	case CEPH_MSG_WATCH_NOTIFY:
546219079203SIlya Dryomov 		return alloc_msg_with_page_vector(hdr);
54633d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_OPREPLY:
54643d14c5d2SYehuda Sadeh 		return get_reply(con, hdr, skip);
54653d14c5d2SYehuda Sadeh 	default:
54665aea3dcdSIlya Dryomov 		pr_warn("%s osd%d unknown msg type %d, skipping\n", __func__,
54675aea3dcdSIlya Dryomov 			osd->o_osd, type);
54683d14c5d2SYehuda Sadeh 		*skip = 1;
54693d14c5d2SYehuda Sadeh 		return NULL;
54703d14c5d2SYehuda Sadeh 	}
54713d14c5d2SYehuda Sadeh }
54723d14c5d2SYehuda Sadeh 
54733d14c5d2SYehuda Sadeh /*
54743d14c5d2SYehuda Sadeh  * Wrappers to refcount containing ceph_osd struct
54753d14c5d2SYehuda Sadeh  */
54764972cf60SIlya Dryomov static struct ceph_connection *osd_get_con(struct ceph_connection *con)
54773d14c5d2SYehuda Sadeh {
54783d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
54793d14c5d2SYehuda Sadeh 	if (get_osd(osd))
54803d14c5d2SYehuda Sadeh 		return con;
54813d14c5d2SYehuda Sadeh 	return NULL;
54823d14c5d2SYehuda Sadeh }
54833d14c5d2SYehuda Sadeh 
54844972cf60SIlya Dryomov static void osd_put_con(struct ceph_connection *con)
54853d14c5d2SYehuda Sadeh {
54863d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
54873d14c5d2SYehuda Sadeh 	put_osd(osd);
54883d14c5d2SYehuda Sadeh }
54893d14c5d2SYehuda Sadeh 
54903d14c5d2SYehuda Sadeh /*
54913d14c5d2SYehuda Sadeh  * authentication
54923d14c5d2SYehuda Sadeh  */
5493cd1a677cSIlya Dryomov 
5494a3530df3SAlex Elder /*
5495a3530df3SAlex Elder  * Note: returned pointer is the address of a structure that's
5496a3530df3SAlex Elder  * managed separately.  Caller must *not* attempt to free it.
5497a3530df3SAlex Elder  */
54984972cf60SIlya Dryomov static struct ceph_auth_handshake *
54994972cf60SIlya Dryomov osd_get_authorizer(struct ceph_connection *con, int *proto, int force_new)
55003d14c5d2SYehuda Sadeh {
55013d14c5d2SYehuda Sadeh 	struct ceph_osd *o = con->private;
55023d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = o->o_osdc;
55033d14c5d2SYehuda Sadeh 	struct ceph_auth_client *ac = osdc->client->monc.auth;
550474f1869fSAlex Elder 	struct ceph_auth_handshake *auth = &o->o_auth;
5505ce287162SIlya Dryomov 	int ret;
55063d14c5d2SYehuda Sadeh 
5507ce287162SIlya Dryomov 	ret = __ceph_auth_get_authorizer(ac, auth, CEPH_ENTITY_TYPE_OSD,
5508ce287162SIlya Dryomov 					 force_new, proto, NULL, NULL);
55093d14c5d2SYehuda Sadeh 	if (ret)
5510a3530df3SAlex Elder 		return ERR_PTR(ret);
551174f1869fSAlex Elder 
5512a3530df3SAlex Elder 	return auth;
55133d14c5d2SYehuda Sadeh }
55143d14c5d2SYehuda Sadeh 
55154972cf60SIlya Dryomov static int osd_add_authorizer_challenge(struct ceph_connection *con,
55166daca13dSIlya Dryomov 				    void *challenge_buf, int challenge_buf_len)
55176daca13dSIlya Dryomov {
55186daca13dSIlya Dryomov 	struct ceph_osd *o = con->private;
55196daca13dSIlya Dryomov 	struct ceph_osd_client *osdc = o->o_osdc;
55206daca13dSIlya Dryomov 	struct ceph_auth_client *ac = osdc->client->monc.auth;
55216daca13dSIlya Dryomov 
55226daca13dSIlya Dryomov 	return ceph_auth_add_authorizer_challenge(ac, o->o_auth.authorizer,
55236daca13dSIlya Dryomov 					    challenge_buf, challenge_buf_len);
55246daca13dSIlya Dryomov }
55253d14c5d2SYehuda Sadeh 
55264972cf60SIlya Dryomov static int osd_verify_authorizer_reply(struct ceph_connection *con)
55273d14c5d2SYehuda Sadeh {
55283d14c5d2SYehuda Sadeh 	struct ceph_osd *o = con->private;
55293d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = o->o_osdc;
55303d14c5d2SYehuda Sadeh 	struct ceph_auth_client *ac = osdc->client->monc.auth;
5531285ea34fSIlya Dryomov 	struct ceph_auth_handshake *auth = &o->o_auth;
55323d14c5d2SYehuda Sadeh 
5533285ea34fSIlya Dryomov 	return ceph_auth_verify_authorizer_reply(ac, auth->authorizer,
5534285ea34fSIlya Dryomov 		auth->authorizer_reply_buf, auth->authorizer_reply_buf_len,
5535285ea34fSIlya Dryomov 		NULL, NULL, NULL, NULL);
55363d14c5d2SYehuda Sadeh }
55373d14c5d2SYehuda Sadeh 
55384972cf60SIlya Dryomov static int osd_invalidate_authorizer(struct ceph_connection *con)
55393d14c5d2SYehuda Sadeh {
55403d14c5d2SYehuda Sadeh 	struct ceph_osd *o = con->private;
55413d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = o->o_osdc;
55423d14c5d2SYehuda Sadeh 	struct ceph_auth_client *ac = osdc->client->monc.auth;
55433d14c5d2SYehuda Sadeh 
554427859f97SSage Weil 	ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
55453d14c5d2SYehuda Sadeh 	return ceph_monc_validate_auth(&osdc->client->monc);
55463d14c5d2SYehuda Sadeh }
55473d14c5d2SYehuda Sadeh 
5548cd1a677cSIlya Dryomov static int osd_get_auth_request(struct ceph_connection *con,
5549cd1a677cSIlya Dryomov 				void *buf, int *buf_len,
5550cd1a677cSIlya Dryomov 				void **authorizer, int *authorizer_len)
5551cd1a677cSIlya Dryomov {
5552cd1a677cSIlya Dryomov 	struct ceph_osd *o = con->private;
5553cd1a677cSIlya Dryomov 	struct ceph_auth_client *ac = o->o_osdc->client->monc.auth;
5554cd1a677cSIlya Dryomov 	struct ceph_auth_handshake *auth = &o->o_auth;
5555cd1a677cSIlya Dryomov 	int ret;
5556cd1a677cSIlya Dryomov 
5557cd1a677cSIlya Dryomov 	ret = ceph_auth_get_authorizer(ac, auth, CEPH_ENTITY_TYPE_OSD,
5558cd1a677cSIlya Dryomov 				       buf, buf_len);
5559cd1a677cSIlya Dryomov 	if (ret)
5560cd1a677cSIlya Dryomov 		return ret;
5561cd1a677cSIlya Dryomov 
5562cd1a677cSIlya Dryomov 	*authorizer = auth->authorizer_buf;
5563cd1a677cSIlya Dryomov 	*authorizer_len = auth->authorizer_buf_len;
5564cd1a677cSIlya Dryomov 	return 0;
5565cd1a677cSIlya Dryomov }
5566cd1a677cSIlya Dryomov 
5567cd1a677cSIlya Dryomov static int osd_handle_auth_reply_more(struct ceph_connection *con,
5568cd1a677cSIlya Dryomov 				      void *reply, int reply_len,
5569cd1a677cSIlya Dryomov 				      void *buf, int *buf_len,
5570cd1a677cSIlya Dryomov 				      void **authorizer, int *authorizer_len)
5571cd1a677cSIlya Dryomov {
5572cd1a677cSIlya Dryomov 	struct ceph_osd *o = con->private;
5573cd1a677cSIlya Dryomov 	struct ceph_auth_client *ac = o->o_osdc->client->monc.auth;
5574cd1a677cSIlya Dryomov 	struct ceph_auth_handshake *auth = &o->o_auth;
5575cd1a677cSIlya Dryomov 	int ret;
5576cd1a677cSIlya Dryomov 
5577cd1a677cSIlya Dryomov 	ret = ceph_auth_handle_svc_reply_more(ac, auth, reply, reply_len,
5578cd1a677cSIlya Dryomov 					      buf, buf_len);
5579cd1a677cSIlya Dryomov 	if (ret)
5580cd1a677cSIlya Dryomov 		return ret;
5581cd1a677cSIlya Dryomov 
5582cd1a677cSIlya Dryomov 	*authorizer = auth->authorizer_buf;
5583cd1a677cSIlya Dryomov 	*authorizer_len = auth->authorizer_buf_len;
5584cd1a677cSIlya Dryomov 	return 0;
5585cd1a677cSIlya Dryomov }
5586cd1a677cSIlya Dryomov 
5587cd1a677cSIlya Dryomov static int osd_handle_auth_done(struct ceph_connection *con,
5588cd1a677cSIlya Dryomov 				u64 global_id, void *reply, int reply_len,
5589cd1a677cSIlya Dryomov 				u8 *session_key, int *session_key_len,
5590cd1a677cSIlya Dryomov 				u8 *con_secret, int *con_secret_len)
5591cd1a677cSIlya Dryomov {
5592cd1a677cSIlya Dryomov 	struct ceph_osd *o = con->private;
5593cd1a677cSIlya Dryomov 	struct ceph_auth_client *ac = o->o_osdc->client->monc.auth;
5594cd1a677cSIlya Dryomov 	struct ceph_auth_handshake *auth = &o->o_auth;
5595cd1a677cSIlya Dryomov 
5596cd1a677cSIlya Dryomov 	return ceph_auth_handle_svc_reply_done(ac, auth, reply, reply_len,
5597cd1a677cSIlya Dryomov 					       session_key, session_key_len,
5598cd1a677cSIlya Dryomov 					       con_secret, con_secret_len);
5599cd1a677cSIlya Dryomov }
5600cd1a677cSIlya Dryomov 
5601cd1a677cSIlya Dryomov static int osd_handle_auth_bad_method(struct ceph_connection *con,
5602cd1a677cSIlya Dryomov 				      int used_proto, int result,
5603cd1a677cSIlya Dryomov 				      const int *allowed_protos, int proto_cnt,
5604cd1a677cSIlya Dryomov 				      const int *allowed_modes, int mode_cnt)
5605cd1a677cSIlya Dryomov {
5606cd1a677cSIlya Dryomov 	struct ceph_osd *o = con->private;
5607cd1a677cSIlya Dryomov 	struct ceph_mon_client *monc = &o->o_osdc->client->monc;
5608cd1a677cSIlya Dryomov 	int ret;
5609cd1a677cSIlya Dryomov 
5610cd1a677cSIlya Dryomov 	if (ceph_auth_handle_bad_authorizer(monc->auth, CEPH_ENTITY_TYPE_OSD,
5611cd1a677cSIlya Dryomov 					    used_proto, result,
5612cd1a677cSIlya Dryomov 					    allowed_protos, proto_cnt,
5613cd1a677cSIlya Dryomov 					    allowed_modes, mode_cnt)) {
5614cd1a677cSIlya Dryomov 		ret = ceph_monc_validate_auth(monc);
5615cd1a677cSIlya Dryomov 		if (ret)
5616cd1a677cSIlya Dryomov 			return ret;
5617cd1a677cSIlya Dryomov 	}
5618cd1a677cSIlya Dryomov 
5619cd1a677cSIlya Dryomov 	return -EACCES;
5620cd1a677cSIlya Dryomov }
5621cd1a677cSIlya Dryomov 
56228cb441c0SIlya Dryomov static void osd_reencode_message(struct ceph_msg *msg)
56238cb441c0SIlya Dryomov {
5624914902afSIlya Dryomov 	int type = le16_to_cpu(msg->hdr.type);
5625914902afSIlya Dryomov 
5626914902afSIlya Dryomov 	if (type == CEPH_MSG_OSD_OP)
56278cb441c0SIlya Dryomov 		encode_request_finish(msg);
56288cb441c0SIlya Dryomov }
56298cb441c0SIlya Dryomov 
563079dbd1baSIlya Dryomov static int osd_sign_message(struct ceph_msg *msg)
563133d07337SYan, Zheng {
563279dbd1baSIlya Dryomov 	struct ceph_osd *o = msg->con->private;
563333d07337SYan, Zheng 	struct ceph_auth_handshake *auth = &o->o_auth;
563479dbd1baSIlya Dryomov 
563533d07337SYan, Zheng 	return ceph_auth_sign_message(auth, msg);
563633d07337SYan, Zheng }
563733d07337SYan, Zheng 
563879dbd1baSIlya Dryomov static int osd_check_message_signature(struct ceph_msg *msg)
563933d07337SYan, Zheng {
564079dbd1baSIlya Dryomov 	struct ceph_osd *o = msg->con->private;
564133d07337SYan, Zheng 	struct ceph_auth_handshake *auth = &o->o_auth;
564279dbd1baSIlya Dryomov 
564333d07337SYan, Zheng 	return ceph_auth_check_message_signature(auth, msg);
564433d07337SYan, Zheng }
564533d07337SYan, Zheng 
56463d14c5d2SYehuda Sadeh static const struct ceph_connection_operations osd_con_ops = {
56474972cf60SIlya Dryomov 	.get = osd_get_con,
56484972cf60SIlya Dryomov 	.put = osd_put_con,
56494972cf60SIlya Dryomov 	.alloc_msg = osd_alloc_msg,
56504972cf60SIlya Dryomov 	.dispatch = osd_dispatch,
56514972cf60SIlya Dryomov 	.fault = osd_fault,
56528cb441c0SIlya Dryomov 	.reencode_message = osd_reencode_message,
56534972cf60SIlya Dryomov 	.get_authorizer = osd_get_authorizer,
56544972cf60SIlya Dryomov 	.add_authorizer_challenge = osd_add_authorizer_challenge,
56554972cf60SIlya Dryomov 	.verify_authorizer_reply = osd_verify_authorizer_reply,
56564972cf60SIlya Dryomov 	.invalidate_authorizer = osd_invalidate_authorizer,
565779dbd1baSIlya Dryomov 	.sign_message = osd_sign_message,
565879dbd1baSIlya Dryomov 	.check_message_signature = osd_check_message_signature,
5659cd1a677cSIlya Dryomov 	.get_auth_request = osd_get_auth_request,
5660cd1a677cSIlya Dryomov 	.handle_auth_reply_more = osd_handle_auth_reply_more,
5661cd1a677cSIlya Dryomov 	.handle_auth_done = osd_handle_auth_done,
5662cd1a677cSIlya Dryomov 	.handle_auth_bad_method = osd_handle_auth_bad_method,
56633d14c5d2SYehuda Sadeh };
5664