xref: /openbmc/linux/net/ceph/osd_client.c (revision e6e28432)
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);
11805aea3dcdSIlya Dryomov 	osd->o_requests = RB_ROOT;
1181922dab61SIlya Dryomov 	osd->o_linger_requests = RB_ROOT;
1182a02a946dSIlya Dryomov 	osd->o_backoff_mappings = RB_ROOT;
1183a02a946dSIlya Dryomov 	osd->o_backoffs_by_id = RB_ROOT;
11840247a0cfSIlya Dryomov 	INIT_LIST_HEAD(&osd->o_osd_lru);
11850247a0cfSIlya Dryomov 	INIT_LIST_HEAD(&osd->o_keepalive_item);
11860247a0cfSIlya Dryomov 	osd->o_incarnation = 1;
11875aea3dcdSIlya Dryomov 	mutex_init(&osd->lock);
11880247a0cfSIlya Dryomov }
11890247a0cfSIlya Dryomov 
11900247a0cfSIlya Dryomov static void osd_cleanup(struct ceph_osd *osd)
11910247a0cfSIlya Dryomov {
11920247a0cfSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&osd->o_node));
11935aea3dcdSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
1194922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
1195a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoff_mappings));
1196a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoffs_by_id));
11970247a0cfSIlya Dryomov 	WARN_ON(!list_empty(&osd->o_osd_lru));
11980247a0cfSIlya Dryomov 	WARN_ON(!list_empty(&osd->o_keepalive_item));
11990247a0cfSIlya Dryomov 
12000247a0cfSIlya Dryomov 	if (osd->o_auth.authorizer) {
12010247a0cfSIlya Dryomov 		WARN_ON(osd_homeless(osd));
12020247a0cfSIlya Dryomov 		ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
12030247a0cfSIlya Dryomov 	}
12040247a0cfSIlya Dryomov }
12050247a0cfSIlya Dryomov 
12060247a0cfSIlya Dryomov /*
12073d14c5d2SYehuda Sadeh  * Track open sessions with osds.
12083d14c5d2SYehuda Sadeh  */
1209e10006f8SAlex Elder static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
12103d14c5d2SYehuda Sadeh {
12113d14c5d2SYehuda Sadeh 	struct ceph_osd *osd;
12123d14c5d2SYehuda Sadeh 
12130247a0cfSIlya Dryomov 	WARN_ON(onum == CEPH_HOMELESS_OSD);
12140247a0cfSIlya Dryomov 
12157a28f59bSIlya Dryomov 	osd = kzalloc(sizeof(*osd), GFP_NOIO | __GFP_NOFAIL);
12160247a0cfSIlya Dryomov 	osd_init(osd);
12173d14c5d2SYehuda Sadeh 	osd->o_osdc = osdc;
1218e10006f8SAlex Elder 	osd->o_osd = onum;
12193d14c5d2SYehuda Sadeh 
1220b7a9e5ddSSage Weil 	ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
12213d14c5d2SYehuda Sadeh 
12223d14c5d2SYehuda Sadeh 	return osd;
12233d14c5d2SYehuda Sadeh }
12243d14c5d2SYehuda Sadeh 
12253d14c5d2SYehuda Sadeh static struct ceph_osd *get_osd(struct ceph_osd *osd)
12263d14c5d2SYehuda Sadeh {
122702113a0fSElena Reshetova 	if (refcount_inc_not_zero(&osd->o_ref)) {
122802113a0fSElena Reshetova 		dout("get_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref)-1,
122902113a0fSElena Reshetova 		     refcount_read(&osd->o_ref));
12303d14c5d2SYehuda Sadeh 		return osd;
12313d14c5d2SYehuda Sadeh 	} else {
12323d14c5d2SYehuda Sadeh 		dout("get_osd %p FAIL\n", osd);
12333d14c5d2SYehuda Sadeh 		return NULL;
12343d14c5d2SYehuda Sadeh 	}
12353d14c5d2SYehuda Sadeh }
12363d14c5d2SYehuda Sadeh 
12373d14c5d2SYehuda Sadeh static void put_osd(struct ceph_osd *osd)
12383d14c5d2SYehuda Sadeh {
123902113a0fSElena Reshetova 	dout("put_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref),
124002113a0fSElena Reshetova 	     refcount_read(&osd->o_ref) - 1);
124102113a0fSElena Reshetova 	if (refcount_dec_and_test(&osd->o_ref)) {
12420247a0cfSIlya Dryomov 		osd_cleanup(osd);
12433d14c5d2SYehuda Sadeh 		kfree(osd);
12443d14c5d2SYehuda Sadeh 	}
12453d14c5d2SYehuda Sadeh }
12463d14c5d2SYehuda Sadeh 
1247fcd00b68SIlya Dryomov DEFINE_RB_FUNCS(osd, struct ceph_osd, o_osd, o_node)
1248fcd00b68SIlya Dryomov 
12499dd2845cSIlya Dryomov static void __move_osd_to_lru(struct ceph_osd *osd)
12503d14c5d2SYehuda Sadeh {
12519dd2845cSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
12529dd2845cSIlya Dryomov 
12539dd2845cSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
12543d14c5d2SYehuda Sadeh 	BUG_ON(!list_empty(&osd->o_osd_lru));
1255bbf37ec3SIlya Dryomov 
12569dd2845cSIlya Dryomov 	spin_lock(&osdc->osd_lru_lock);
12573d14c5d2SYehuda Sadeh 	list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
12589dd2845cSIlya Dryomov 	spin_unlock(&osdc->osd_lru_lock);
12599dd2845cSIlya Dryomov 
1260a319bf56SIlya Dryomov 	osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
12613d14c5d2SYehuda Sadeh }
12623d14c5d2SYehuda Sadeh 
12639dd2845cSIlya Dryomov static void maybe_move_osd_to_lru(struct ceph_osd *osd)
1264bbf37ec3SIlya Dryomov {
12655aea3dcdSIlya Dryomov 	if (RB_EMPTY_ROOT(&osd->o_requests) &&
1266922dab61SIlya Dryomov 	    RB_EMPTY_ROOT(&osd->o_linger_requests))
12679dd2845cSIlya Dryomov 		__move_osd_to_lru(osd);
1268bbf37ec3SIlya Dryomov }
1269bbf37ec3SIlya Dryomov 
12703d14c5d2SYehuda Sadeh static void __remove_osd_from_lru(struct ceph_osd *osd)
12713d14c5d2SYehuda Sadeh {
12729dd2845cSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
12739dd2845cSIlya Dryomov 
12749dd2845cSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
12759dd2845cSIlya Dryomov 
12769dd2845cSIlya Dryomov 	spin_lock(&osdc->osd_lru_lock);
12773d14c5d2SYehuda Sadeh 	if (!list_empty(&osd->o_osd_lru))
12783d14c5d2SYehuda Sadeh 		list_del_init(&osd->o_osd_lru);
12799dd2845cSIlya Dryomov 	spin_unlock(&osdc->osd_lru_lock);
12803d14c5d2SYehuda Sadeh }
12813d14c5d2SYehuda Sadeh 
12823d14c5d2SYehuda Sadeh /*
12835aea3dcdSIlya Dryomov  * Close the connection and assign any leftover requests to the
12845aea3dcdSIlya Dryomov  * homeless session.
12855aea3dcdSIlya Dryomov  */
12865aea3dcdSIlya Dryomov static void close_osd(struct ceph_osd *osd)
12875aea3dcdSIlya Dryomov {
12885aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
12895aea3dcdSIlya Dryomov 	struct rb_node *n;
12905aea3dcdSIlya Dryomov 
12915aea3dcdSIlya Dryomov 	verify_osdc_wrlocked(osdc);
12925aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
12935aea3dcdSIlya Dryomov 
12945aea3dcdSIlya Dryomov 	ceph_con_close(&osd->o_con);
12955aea3dcdSIlya Dryomov 
12965aea3dcdSIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; ) {
12975aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
12985aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
12995aea3dcdSIlya Dryomov 
13005aea3dcdSIlya Dryomov 		n = rb_next(n); /* unlink_request() */
13015aea3dcdSIlya Dryomov 
13025aea3dcdSIlya Dryomov 		dout(" reassigning req %p tid %llu\n", req, req->r_tid);
13035aea3dcdSIlya Dryomov 		unlink_request(osd, req);
13045aea3dcdSIlya Dryomov 		link_request(&osdc->homeless_osd, req);
13055aea3dcdSIlya Dryomov 	}
1306922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_linger_requests); n; ) {
1307922dab61SIlya Dryomov 		struct ceph_osd_linger_request *lreq =
1308922dab61SIlya Dryomov 		    rb_entry(n, struct ceph_osd_linger_request, node);
1309922dab61SIlya Dryomov 
1310922dab61SIlya Dryomov 		n = rb_next(n); /* unlink_linger() */
1311922dab61SIlya Dryomov 
1312922dab61SIlya Dryomov 		dout(" reassigning lreq %p linger_id %llu\n", lreq,
1313922dab61SIlya Dryomov 		     lreq->linger_id);
1314922dab61SIlya Dryomov 		unlink_linger(osd, lreq);
1315922dab61SIlya Dryomov 		link_linger(&osdc->homeless_osd, lreq);
1316922dab61SIlya Dryomov 	}
1317a02a946dSIlya Dryomov 	clear_backoffs(osd);
13185aea3dcdSIlya Dryomov 
13195aea3dcdSIlya Dryomov 	__remove_osd_from_lru(osd);
13205aea3dcdSIlya Dryomov 	erase_osd(&osdc->osds, osd);
13215aea3dcdSIlya Dryomov 	put_osd(osd);
13225aea3dcdSIlya Dryomov }
13235aea3dcdSIlya Dryomov 
13245aea3dcdSIlya Dryomov /*
13253d14c5d2SYehuda Sadeh  * reset osd connect
13263d14c5d2SYehuda Sadeh  */
13275aea3dcdSIlya Dryomov static int reopen_osd(struct ceph_osd *osd)
13283d14c5d2SYehuda Sadeh {
1329c3acb181SAlex Elder 	struct ceph_entity_addr *peer_addr;
13303d14c5d2SYehuda Sadeh 
13315aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
13325aea3dcdSIlya Dryomov 
13335aea3dcdSIlya Dryomov 	if (RB_EMPTY_ROOT(&osd->o_requests) &&
1334922dab61SIlya Dryomov 	    RB_EMPTY_ROOT(&osd->o_linger_requests)) {
13355aea3dcdSIlya Dryomov 		close_osd(osd);
1336c3acb181SAlex Elder 		return -ENODEV;
1337c3acb181SAlex Elder 	}
1338c3acb181SAlex Elder 
13395aea3dcdSIlya Dryomov 	peer_addr = &osd->o_osdc->osdmap->osd_addr[osd->o_osd];
1340c3acb181SAlex Elder 	if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
13413d14c5d2SYehuda Sadeh 			!ceph_con_opened(&osd->o_con)) {
13425aea3dcdSIlya Dryomov 		struct rb_node *n;
1343c3acb181SAlex Elder 
13443d14c5d2SYehuda Sadeh 		dout("osd addr hasn't changed and connection never opened, "
13450b4af2e8SIlya Dryomov 		     "letting msgr retry\n");
13463d14c5d2SYehuda Sadeh 		/* touch each r_stamp for handle_timeout()'s benfit */
13475aea3dcdSIlya Dryomov 		for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
13485aea3dcdSIlya Dryomov 			struct ceph_osd_request *req =
13495aea3dcdSIlya Dryomov 			    rb_entry(n, struct ceph_osd_request, r_node);
13503d14c5d2SYehuda Sadeh 			req->r_stamp = jiffies;
13515aea3dcdSIlya Dryomov 		}
1352c3acb181SAlex Elder 
1353c3acb181SAlex Elder 		return -EAGAIN;
13543d14c5d2SYehuda Sadeh 	}
1355c3acb181SAlex Elder 
1356c3acb181SAlex Elder 	ceph_con_close(&osd->o_con);
1357c3acb181SAlex Elder 	ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1358c3acb181SAlex Elder 	osd->o_incarnation++;
1359c3acb181SAlex Elder 
1360c3acb181SAlex Elder 	return 0;
13613d14c5d2SYehuda Sadeh }
13623d14c5d2SYehuda Sadeh 
13635aea3dcdSIlya Dryomov static struct ceph_osd *lookup_create_osd(struct ceph_osd_client *osdc, int o,
13645aea3dcdSIlya Dryomov 					  bool wrlocked)
13653d14c5d2SYehuda Sadeh {
13665aea3dcdSIlya Dryomov 	struct ceph_osd *osd;
13675aea3dcdSIlya Dryomov 
13685aea3dcdSIlya Dryomov 	if (wrlocked)
13695aea3dcdSIlya Dryomov 		verify_osdc_wrlocked(osdc);
13705aea3dcdSIlya Dryomov 	else
13715aea3dcdSIlya Dryomov 		verify_osdc_locked(osdc);
13725aea3dcdSIlya Dryomov 
13735aea3dcdSIlya Dryomov 	if (o != CEPH_HOMELESS_OSD)
13745aea3dcdSIlya Dryomov 		osd = lookup_osd(&osdc->osds, o);
13755aea3dcdSIlya Dryomov 	else
13765aea3dcdSIlya Dryomov 		osd = &osdc->homeless_osd;
13775aea3dcdSIlya Dryomov 	if (!osd) {
13785aea3dcdSIlya Dryomov 		if (!wrlocked)
13795aea3dcdSIlya Dryomov 			return ERR_PTR(-EAGAIN);
13805aea3dcdSIlya Dryomov 
13815aea3dcdSIlya Dryomov 		osd = create_osd(osdc, o);
13825aea3dcdSIlya Dryomov 		insert_osd(&osdc->osds, osd);
13835aea3dcdSIlya Dryomov 		ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
13845aea3dcdSIlya Dryomov 			      &osdc->osdmap->osd_addr[osd->o_osd]);
13855aea3dcdSIlya Dryomov 	}
13865aea3dcdSIlya Dryomov 
13875aea3dcdSIlya Dryomov 	dout("%s osdc %p osd%d -> osd %p\n", __func__, osdc, o, osd);
13885aea3dcdSIlya Dryomov 	return osd;
1389a40c4f10SYehuda Sadeh }
1390a40c4f10SYehuda Sadeh 
13913d14c5d2SYehuda Sadeh /*
13925aea3dcdSIlya Dryomov  * Create request <-> OSD session relation.
13935aea3dcdSIlya Dryomov  *
13945aea3dcdSIlya Dryomov  * @req has to be assigned a tid, @osd may be homeless.
13953d14c5d2SYehuda Sadeh  */
13965aea3dcdSIlya Dryomov static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req)
13973d14c5d2SYehuda Sadeh {
13985aea3dcdSIlya Dryomov 	verify_osd_locked(osd);
13995aea3dcdSIlya Dryomov 	WARN_ON(!req->r_tid || req->r_osd);
14005aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
140135f9f8a0SSage Weil 	     req, req->r_tid);
14025aea3dcdSIlya Dryomov 
14035aea3dcdSIlya Dryomov 	if (!osd_homeless(osd))
14045aea3dcdSIlya Dryomov 		__remove_osd_from_lru(osd);
14055aea3dcdSIlya Dryomov 	else
14065aea3dcdSIlya Dryomov 		atomic_inc(&osd->o_osdc->num_homeless);
14075aea3dcdSIlya Dryomov 
14085aea3dcdSIlya Dryomov 	get_osd(osd);
14095aea3dcdSIlya Dryomov 	insert_request(&osd->o_requests, req);
14105aea3dcdSIlya Dryomov 	req->r_osd = osd;
141135f9f8a0SSage Weil }
141235f9f8a0SSage Weil 
14135aea3dcdSIlya Dryomov static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req)
14143d14c5d2SYehuda Sadeh {
14155aea3dcdSIlya Dryomov 	verify_osd_locked(osd);
14165aea3dcdSIlya Dryomov 	WARN_ON(req->r_osd != osd);
14175aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
14185aea3dcdSIlya Dryomov 	     req, req->r_tid);
14195aea3dcdSIlya Dryomov 
14205aea3dcdSIlya Dryomov 	req->r_osd = NULL;
14215aea3dcdSIlya Dryomov 	erase_request(&osd->o_requests, req);
14225aea3dcdSIlya Dryomov 	put_osd(osd);
14235aea3dcdSIlya Dryomov 
14245aea3dcdSIlya Dryomov 	if (!osd_homeless(osd))
14255aea3dcdSIlya Dryomov 		maybe_move_osd_to_lru(osd);
14265aea3dcdSIlya Dryomov 	else
14275aea3dcdSIlya Dryomov 		atomic_dec(&osd->o_osdc->num_homeless);
14283d14c5d2SYehuda Sadeh }
14293d14c5d2SYehuda Sadeh 
143063244fa1SIlya Dryomov static bool __pool_full(struct ceph_pg_pool_info *pi)
143163244fa1SIlya Dryomov {
143263244fa1SIlya Dryomov 	return pi->flags & CEPH_POOL_FLAG_FULL;
143363244fa1SIlya Dryomov }
143463244fa1SIlya Dryomov 
143542c1b124SIlya Dryomov static bool have_pool_full(struct ceph_osd_client *osdc)
143642c1b124SIlya Dryomov {
143742c1b124SIlya Dryomov 	struct rb_node *n;
143842c1b124SIlya Dryomov 
143942c1b124SIlya Dryomov 	for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
144042c1b124SIlya Dryomov 		struct ceph_pg_pool_info *pi =
144142c1b124SIlya Dryomov 		    rb_entry(n, struct ceph_pg_pool_info, node);
144242c1b124SIlya Dryomov 
144342c1b124SIlya Dryomov 		if (__pool_full(pi))
144442c1b124SIlya Dryomov 			return true;
144542c1b124SIlya Dryomov 	}
144642c1b124SIlya Dryomov 
144742c1b124SIlya Dryomov 	return false;
144842c1b124SIlya Dryomov }
144942c1b124SIlya Dryomov 
14505aea3dcdSIlya Dryomov static bool pool_full(struct ceph_osd_client *osdc, s64 pool_id)
14515aea3dcdSIlya Dryomov {
14525aea3dcdSIlya Dryomov 	struct ceph_pg_pool_info *pi;
14535aea3dcdSIlya Dryomov 
14545aea3dcdSIlya Dryomov 	pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
14555aea3dcdSIlya Dryomov 	if (!pi)
14565aea3dcdSIlya Dryomov 		return false;
14575aea3dcdSIlya Dryomov 
14585aea3dcdSIlya Dryomov 	return __pool_full(pi);
14595aea3dcdSIlya Dryomov }
14605aea3dcdSIlya Dryomov 
14613d14c5d2SYehuda Sadeh /*
1462d29adb34SJosh Durgin  * Returns whether a request should be blocked from being sent
1463d29adb34SJosh Durgin  * based on the current osdmap and osd_client settings.
1464d29adb34SJosh Durgin  */
146563244fa1SIlya Dryomov static bool target_should_be_paused(struct ceph_osd_client *osdc,
146663244fa1SIlya Dryomov 				    const struct ceph_osd_request_target *t,
146763244fa1SIlya Dryomov 				    struct ceph_pg_pool_info *pi)
146863244fa1SIlya Dryomov {
1469b7ec35b3SIlya Dryomov 	bool pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
1470b7ec35b3SIlya Dryomov 	bool pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
1471b7ec35b3SIlya Dryomov 		       ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
147263244fa1SIlya Dryomov 		       __pool_full(pi);
147363244fa1SIlya Dryomov 
14746d637a54SIlya Dryomov 	WARN_ON(pi->id != t->target_oloc.pool);
147558eb7932SJeff Layton 	return ((t->flags & CEPH_OSD_FLAG_READ) && pauserd) ||
147658eb7932SJeff Layton 	       ((t->flags & CEPH_OSD_FLAG_WRITE) && pausewr) ||
147758eb7932SJeff Layton 	       (osdc->osdmap->epoch < osdc->epoch_barrier);
147863244fa1SIlya Dryomov }
147963244fa1SIlya Dryomov 
1480117d96a0SIlya Dryomov static int pick_random_replica(const struct ceph_osds *acting)
1481117d96a0SIlya Dryomov {
14828032bf12SJason A. Donenfeld 	int i = get_random_u32_below(acting->size);
1483117d96a0SIlya Dryomov 
1484117d96a0SIlya Dryomov 	dout("%s picked osd%d, primary osd%d\n", __func__,
1485117d96a0SIlya Dryomov 	     acting->osds[i], acting->primary);
1486117d96a0SIlya Dryomov 	return i;
1487117d96a0SIlya Dryomov }
1488117d96a0SIlya Dryomov 
1489117d96a0SIlya Dryomov /*
1490117d96a0SIlya Dryomov  * Picks the closest replica based on client's location given by
1491117d96a0SIlya Dryomov  * crush_location option.  Prefers the primary if the locality is
1492117d96a0SIlya Dryomov  * the same.
1493117d96a0SIlya Dryomov  */
1494117d96a0SIlya Dryomov static int pick_closest_replica(struct ceph_osd_client *osdc,
1495117d96a0SIlya Dryomov 				const struct ceph_osds *acting)
1496117d96a0SIlya Dryomov {
1497117d96a0SIlya Dryomov 	struct ceph_options *opt = osdc->client->options;
1498117d96a0SIlya Dryomov 	int best_i, best_locality;
1499117d96a0SIlya Dryomov 	int i = 0, locality;
1500117d96a0SIlya Dryomov 
1501117d96a0SIlya Dryomov 	do {
1502117d96a0SIlya Dryomov 		locality = ceph_get_crush_locality(osdc->osdmap,
1503117d96a0SIlya Dryomov 						   acting->osds[i],
1504117d96a0SIlya Dryomov 						   &opt->crush_locs);
1505117d96a0SIlya Dryomov 		if (i == 0 ||
1506117d96a0SIlya Dryomov 		    (locality >= 0 && best_locality < 0) ||
1507117d96a0SIlya Dryomov 		    (locality >= 0 && best_locality >= 0 &&
1508117d96a0SIlya Dryomov 		     locality < best_locality)) {
1509117d96a0SIlya Dryomov 			best_i = i;
1510117d96a0SIlya Dryomov 			best_locality = locality;
1511117d96a0SIlya Dryomov 		}
1512117d96a0SIlya Dryomov 	} while (++i < acting->size);
1513117d96a0SIlya Dryomov 
1514117d96a0SIlya Dryomov 	dout("%s picked osd%d with locality %d, primary osd%d\n", __func__,
1515117d96a0SIlya Dryomov 	     acting->osds[best_i], best_locality, acting->primary);
1516117d96a0SIlya Dryomov 	return best_i;
1517117d96a0SIlya Dryomov }
1518117d96a0SIlya Dryomov 
151963244fa1SIlya Dryomov enum calc_target_result {
152063244fa1SIlya Dryomov 	CALC_TARGET_NO_ACTION = 0,
152163244fa1SIlya Dryomov 	CALC_TARGET_NEED_RESEND,
152263244fa1SIlya Dryomov 	CALC_TARGET_POOL_DNE,
152363244fa1SIlya Dryomov };
152463244fa1SIlya Dryomov 
152563244fa1SIlya Dryomov static enum calc_target_result calc_target(struct ceph_osd_client *osdc,
152663244fa1SIlya Dryomov 					   struct ceph_osd_request_target *t,
152763244fa1SIlya Dryomov 					   bool any_change)
152863244fa1SIlya Dryomov {
152963244fa1SIlya Dryomov 	struct ceph_pg_pool_info *pi;
153063244fa1SIlya Dryomov 	struct ceph_pg pgid, last_pgid;
153163244fa1SIlya Dryomov 	struct ceph_osds up, acting;
1532117d96a0SIlya Dryomov 	bool is_read = t->flags & CEPH_OSD_FLAG_READ;
1533117d96a0SIlya Dryomov 	bool is_write = t->flags & CEPH_OSD_FLAG_WRITE;
153463244fa1SIlya Dryomov 	bool force_resend = false;
153584ed45dfSIlya Dryomov 	bool unpaused = false;
1536a5613724SIlya Dryomov 	bool legacy_change = false;
15377de030d6SIlya Dryomov 	bool split = false;
1538b7ec35b3SIlya Dryomov 	bool sort_bitwise = ceph_osdmap_flag(osdc, CEPH_OSDMAP_SORTBITWISE);
1539ae78dd81SIlya Dryomov 	bool recovery_deletes = ceph_osdmap_flag(osdc,
1540ae78dd81SIlya Dryomov 						 CEPH_OSDMAP_RECOVERY_DELETES);
154163244fa1SIlya Dryomov 	enum calc_target_result ct_res;
154263244fa1SIlya Dryomov 
154304c7d789SIlya Dryomov 	t->epoch = osdc->osdmap->epoch;
154463244fa1SIlya Dryomov 	pi = ceph_pg_pool_by_id(osdc->osdmap, t->base_oloc.pool);
154563244fa1SIlya Dryomov 	if (!pi) {
154663244fa1SIlya Dryomov 		t->osd = CEPH_HOMELESS_OSD;
154763244fa1SIlya Dryomov 		ct_res = CALC_TARGET_POOL_DNE;
154863244fa1SIlya Dryomov 		goto out;
154963244fa1SIlya Dryomov 	}
155063244fa1SIlya Dryomov 
155163244fa1SIlya Dryomov 	if (osdc->osdmap->epoch == pi->last_force_request_resend) {
1552dc93e0e2SIlya Dryomov 		if (t->last_force_resend < pi->last_force_request_resend) {
1553dc93e0e2SIlya Dryomov 			t->last_force_resend = pi->last_force_request_resend;
155463244fa1SIlya Dryomov 			force_resend = true;
1555dc93e0e2SIlya Dryomov 		} else if (t->last_force_resend == 0) {
155663244fa1SIlya Dryomov 			force_resend = true;
155763244fa1SIlya Dryomov 		}
155863244fa1SIlya Dryomov 	}
155963244fa1SIlya Dryomov 
1560db098ec4SIlya Dryomov 	/* apply tiering */
1561db098ec4SIlya Dryomov 	ceph_oid_copy(&t->target_oid, &t->base_oid);
1562db098ec4SIlya Dryomov 	ceph_oloc_copy(&t->target_oloc, &t->base_oloc);
1563db098ec4SIlya Dryomov 	if ((t->flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
1564117d96a0SIlya Dryomov 		if (is_read && pi->read_tier >= 0)
156563244fa1SIlya Dryomov 			t->target_oloc.pool = pi->read_tier;
1566117d96a0SIlya Dryomov 		if (is_write && pi->write_tier >= 0)
156763244fa1SIlya Dryomov 			t->target_oloc.pool = pi->write_tier;
15686d637a54SIlya Dryomov 
15696d637a54SIlya Dryomov 		pi = ceph_pg_pool_by_id(osdc->osdmap, t->target_oloc.pool);
15706d637a54SIlya Dryomov 		if (!pi) {
15716d637a54SIlya Dryomov 			t->osd = CEPH_HOMELESS_OSD;
15726d637a54SIlya Dryomov 			ct_res = CALC_TARGET_POOL_DNE;
15736d637a54SIlya Dryomov 			goto out;
15746d637a54SIlya Dryomov 		}
157563244fa1SIlya Dryomov 	}
157663244fa1SIlya Dryomov 
1577a86f009fSIlya Dryomov 	__ceph_object_locator_to_pg(pi, &t->target_oid, &t->target_oloc, &pgid);
157863244fa1SIlya Dryomov 	last_pgid.pool = pgid.pool;
157963244fa1SIlya Dryomov 	last_pgid.seed = ceph_stable_mod(pgid.seed, t->pg_num, t->pg_num_mask);
158063244fa1SIlya Dryomov 
1581df28152dSIlya Dryomov 	ceph_pg_to_up_acting_osds(osdc->osdmap, pi, &pgid, &up, &acting);
158263244fa1SIlya Dryomov 	if (any_change &&
158363244fa1SIlya Dryomov 	    ceph_is_new_interval(&t->acting,
158463244fa1SIlya Dryomov 				 &acting,
158563244fa1SIlya Dryomov 				 &t->up,
158663244fa1SIlya Dryomov 				 &up,
158763244fa1SIlya Dryomov 				 t->size,
158863244fa1SIlya Dryomov 				 pi->size,
158963244fa1SIlya Dryomov 				 t->min_size,
159063244fa1SIlya Dryomov 				 pi->min_size,
159163244fa1SIlya Dryomov 				 t->pg_num,
159263244fa1SIlya Dryomov 				 pi->pg_num,
159363244fa1SIlya Dryomov 				 t->sort_bitwise,
159463244fa1SIlya Dryomov 				 sort_bitwise,
1595ae78dd81SIlya Dryomov 				 t->recovery_deletes,
1596ae78dd81SIlya Dryomov 				 recovery_deletes,
159763244fa1SIlya Dryomov 				 &last_pgid))
159863244fa1SIlya Dryomov 		force_resend = true;
159963244fa1SIlya Dryomov 
160063244fa1SIlya Dryomov 	if (t->paused && !target_should_be_paused(osdc, t, pi)) {
160163244fa1SIlya Dryomov 		t->paused = false;
160284ed45dfSIlya Dryomov 		unpaused = true;
160363244fa1SIlya Dryomov 	}
160484ed45dfSIlya Dryomov 	legacy_change = ceph_pg_compare(&t->pgid, &pgid) ||
1605117d96a0SIlya Dryomov 			ceph_osds_changed(&t->acting, &acting,
1606117d96a0SIlya Dryomov 					  t->used_replica || any_change);
16077de030d6SIlya Dryomov 	if (t->pg_num)
16087de030d6SIlya Dryomov 		split = ceph_pg_is_split(&last_pgid, t->pg_num, pi->pg_num);
160963244fa1SIlya Dryomov 
16107de030d6SIlya Dryomov 	if (legacy_change || force_resend || split) {
161163244fa1SIlya Dryomov 		t->pgid = pgid; /* struct */
1612df28152dSIlya Dryomov 		ceph_pg_to_primary_shard(osdc->osdmap, pi, &pgid, &t->spgid);
161363244fa1SIlya Dryomov 		ceph_osds_copy(&t->acting, &acting);
161463244fa1SIlya Dryomov 		ceph_osds_copy(&t->up, &up);
161563244fa1SIlya Dryomov 		t->size = pi->size;
161663244fa1SIlya Dryomov 		t->min_size = pi->min_size;
161763244fa1SIlya Dryomov 		t->pg_num = pi->pg_num;
161863244fa1SIlya Dryomov 		t->pg_num_mask = pi->pg_num_mask;
161963244fa1SIlya Dryomov 		t->sort_bitwise = sort_bitwise;
1620ae78dd81SIlya Dryomov 		t->recovery_deletes = recovery_deletes;
162163244fa1SIlya Dryomov 
1622117d96a0SIlya Dryomov 		if ((t->flags & (CEPH_OSD_FLAG_BALANCE_READS |
1623117d96a0SIlya Dryomov 				 CEPH_OSD_FLAG_LOCALIZE_READS)) &&
1624117d96a0SIlya Dryomov 		    !is_write && pi->type == CEPH_POOL_TYPE_REP &&
1625117d96a0SIlya Dryomov 		    acting.size > 1) {
1626117d96a0SIlya Dryomov 			int pos;
1627117d96a0SIlya Dryomov 
1628117d96a0SIlya Dryomov 			WARN_ON(!is_read || acting.osds[0] != acting.primary);
1629117d96a0SIlya Dryomov 			if (t->flags & CEPH_OSD_FLAG_BALANCE_READS) {
1630117d96a0SIlya Dryomov 				pos = pick_random_replica(&acting);
1631117d96a0SIlya Dryomov 			} else {
1632117d96a0SIlya Dryomov 				pos = pick_closest_replica(osdc, &acting);
1633117d96a0SIlya Dryomov 			}
1634117d96a0SIlya Dryomov 			t->osd = acting.osds[pos];
1635117d96a0SIlya Dryomov 			t->used_replica = pos > 0;
1636117d96a0SIlya Dryomov 		} else {
163763244fa1SIlya Dryomov 			t->osd = acting.primary;
1638117d96a0SIlya Dryomov 			t->used_replica = false;
1639117d96a0SIlya Dryomov 		}
164063244fa1SIlya Dryomov 	}
164163244fa1SIlya Dryomov 
1642a5613724SIlya Dryomov 	if (unpaused || legacy_change || force_resend || split)
164384ed45dfSIlya Dryomov 		ct_res = CALC_TARGET_NEED_RESEND;
164484ed45dfSIlya Dryomov 	else
164584ed45dfSIlya Dryomov 		ct_res = CALC_TARGET_NO_ACTION;
164684ed45dfSIlya Dryomov 
164763244fa1SIlya Dryomov out:
1648a5613724SIlya Dryomov 	dout("%s t %p -> %d%d%d%d ct_res %d osd%d\n", __func__, t, unpaused,
1649a5613724SIlya Dryomov 	     legacy_change, force_resend, split, ct_res, t->osd);
165063244fa1SIlya Dryomov 	return ct_res;
165163244fa1SIlya Dryomov }
165263244fa1SIlya Dryomov 
1653a02a946dSIlya Dryomov static struct ceph_spg_mapping *alloc_spg_mapping(void)
1654a02a946dSIlya Dryomov {
1655a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
1656a02a946dSIlya Dryomov 
1657a02a946dSIlya Dryomov 	spg = kmalloc(sizeof(*spg), GFP_NOIO);
1658a02a946dSIlya Dryomov 	if (!spg)
1659a02a946dSIlya Dryomov 		return NULL;
1660a02a946dSIlya Dryomov 
1661a02a946dSIlya Dryomov 	RB_CLEAR_NODE(&spg->node);
1662a02a946dSIlya Dryomov 	spg->backoffs = RB_ROOT;
1663a02a946dSIlya Dryomov 	return spg;
1664a02a946dSIlya Dryomov }
1665a02a946dSIlya Dryomov 
1666a02a946dSIlya Dryomov static void free_spg_mapping(struct ceph_spg_mapping *spg)
1667a02a946dSIlya Dryomov {
1668a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&spg->node));
1669a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&spg->backoffs));
1670a02a946dSIlya Dryomov 
1671a02a946dSIlya Dryomov 	kfree(spg);
1672a02a946dSIlya Dryomov }
1673a02a946dSIlya Dryomov 
1674a02a946dSIlya Dryomov /*
1675a02a946dSIlya Dryomov  * rbtree of ceph_spg_mapping for handling map<spg_t, ...>, similar to
1676a02a946dSIlya Dryomov  * ceph_pg_mapping.  Used to track OSD backoffs -- a backoff [range] is
1677a02a946dSIlya Dryomov  * defined only within a specific spgid; it does not pass anything to
1678a02a946dSIlya Dryomov  * children on split, or to another primary.
1679a02a946dSIlya Dryomov  */
1680a02a946dSIlya Dryomov DEFINE_RB_FUNCS2(spg_mapping, struct ceph_spg_mapping, spgid, ceph_spg_compare,
1681a02a946dSIlya Dryomov 		 RB_BYPTR, const struct ceph_spg *, node)
1682a02a946dSIlya Dryomov 
1683a02a946dSIlya Dryomov static u64 hoid_get_bitwise_key(const struct ceph_hobject_id *hoid)
1684a02a946dSIlya Dryomov {
1685a02a946dSIlya Dryomov 	return hoid->is_max ? 0x100000000ull : hoid->hash_reverse_bits;
1686a02a946dSIlya Dryomov }
1687a02a946dSIlya Dryomov 
1688a02a946dSIlya Dryomov static void hoid_get_effective_key(const struct ceph_hobject_id *hoid,
1689a02a946dSIlya Dryomov 				   void **pkey, size_t *pkey_len)
1690a02a946dSIlya Dryomov {
1691a02a946dSIlya Dryomov 	if (hoid->key_len) {
1692a02a946dSIlya Dryomov 		*pkey = hoid->key;
1693a02a946dSIlya Dryomov 		*pkey_len = hoid->key_len;
1694a02a946dSIlya Dryomov 	} else {
1695a02a946dSIlya Dryomov 		*pkey = hoid->oid;
1696a02a946dSIlya Dryomov 		*pkey_len = hoid->oid_len;
1697a02a946dSIlya Dryomov 	}
1698a02a946dSIlya Dryomov }
1699a02a946dSIlya Dryomov 
1700a02a946dSIlya Dryomov static int compare_names(const void *name1, size_t name1_len,
1701a02a946dSIlya Dryomov 			 const void *name2, size_t name2_len)
1702a02a946dSIlya Dryomov {
1703a02a946dSIlya Dryomov 	int ret;
1704a02a946dSIlya Dryomov 
1705a02a946dSIlya Dryomov 	ret = memcmp(name1, name2, min(name1_len, name2_len));
1706a02a946dSIlya Dryomov 	if (!ret) {
1707a02a946dSIlya Dryomov 		if (name1_len < name2_len)
1708a02a946dSIlya Dryomov 			ret = -1;
1709a02a946dSIlya Dryomov 		else if (name1_len > name2_len)
1710a02a946dSIlya Dryomov 			ret = 1;
1711a02a946dSIlya Dryomov 	}
1712a02a946dSIlya Dryomov 	return ret;
1713a02a946dSIlya Dryomov }
1714a02a946dSIlya Dryomov 
1715a02a946dSIlya Dryomov static int hoid_compare(const struct ceph_hobject_id *lhs,
1716a02a946dSIlya Dryomov 			const struct ceph_hobject_id *rhs)
1717a02a946dSIlya Dryomov {
1718a02a946dSIlya Dryomov 	void *effective_key1, *effective_key2;
1719a02a946dSIlya Dryomov 	size_t effective_key1_len, effective_key2_len;
1720a02a946dSIlya Dryomov 	int ret;
1721a02a946dSIlya Dryomov 
1722a02a946dSIlya Dryomov 	if (lhs->is_max < rhs->is_max)
1723a02a946dSIlya Dryomov 		return -1;
1724a02a946dSIlya Dryomov 	if (lhs->is_max > rhs->is_max)
1725a02a946dSIlya Dryomov 		return 1;
1726a02a946dSIlya Dryomov 
1727a02a946dSIlya Dryomov 	if (lhs->pool < rhs->pool)
1728a02a946dSIlya Dryomov 		return -1;
1729a02a946dSIlya Dryomov 	if (lhs->pool > rhs->pool)
1730a02a946dSIlya Dryomov 		return 1;
1731a02a946dSIlya Dryomov 
1732a02a946dSIlya Dryomov 	if (hoid_get_bitwise_key(lhs) < hoid_get_bitwise_key(rhs))
1733a02a946dSIlya Dryomov 		return -1;
1734a02a946dSIlya Dryomov 	if (hoid_get_bitwise_key(lhs) > hoid_get_bitwise_key(rhs))
1735a02a946dSIlya Dryomov 		return 1;
1736a02a946dSIlya Dryomov 
1737a02a946dSIlya Dryomov 	ret = compare_names(lhs->nspace, lhs->nspace_len,
1738a02a946dSIlya Dryomov 			    rhs->nspace, rhs->nspace_len);
1739a02a946dSIlya Dryomov 	if (ret)
1740a02a946dSIlya Dryomov 		return ret;
1741a02a946dSIlya Dryomov 
1742a02a946dSIlya Dryomov 	hoid_get_effective_key(lhs, &effective_key1, &effective_key1_len);
1743a02a946dSIlya Dryomov 	hoid_get_effective_key(rhs, &effective_key2, &effective_key2_len);
1744a02a946dSIlya Dryomov 	ret = compare_names(effective_key1, effective_key1_len,
1745a02a946dSIlya Dryomov 			    effective_key2, effective_key2_len);
1746a02a946dSIlya Dryomov 	if (ret)
1747a02a946dSIlya Dryomov 		return ret;
1748a02a946dSIlya Dryomov 
1749a02a946dSIlya Dryomov 	ret = compare_names(lhs->oid, lhs->oid_len, rhs->oid, rhs->oid_len);
1750a02a946dSIlya Dryomov 	if (ret)
1751a02a946dSIlya Dryomov 		return ret;
1752a02a946dSIlya Dryomov 
1753a02a946dSIlya Dryomov 	if (lhs->snapid < rhs->snapid)
1754a02a946dSIlya Dryomov 		return -1;
1755a02a946dSIlya Dryomov 	if (lhs->snapid > rhs->snapid)
1756a02a946dSIlya Dryomov 		return 1;
1757a02a946dSIlya Dryomov 
1758a02a946dSIlya Dryomov 	return 0;
1759a02a946dSIlya Dryomov }
1760a02a946dSIlya Dryomov 
1761a02a946dSIlya Dryomov /*
1762a02a946dSIlya Dryomov  * For decoding ->begin and ->end of MOSDBackoff only -- no MIN/MAX
1763a02a946dSIlya Dryomov  * compat stuff here.
1764a02a946dSIlya Dryomov  *
1765a02a946dSIlya Dryomov  * Assumes @hoid is zero-initialized.
1766a02a946dSIlya Dryomov  */
1767a02a946dSIlya Dryomov static int decode_hoid(void **p, void *end, struct ceph_hobject_id *hoid)
1768a02a946dSIlya Dryomov {
1769a02a946dSIlya Dryomov 	u8 struct_v;
1770a02a946dSIlya Dryomov 	u32 struct_len;
1771a02a946dSIlya Dryomov 	int ret;
1772a02a946dSIlya Dryomov 
1773a02a946dSIlya Dryomov 	ret = ceph_start_decoding(p, end, 4, "hobject_t", &struct_v,
1774a02a946dSIlya Dryomov 				  &struct_len);
1775a02a946dSIlya Dryomov 	if (ret)
1776a02a946dSIlya Dryomov 		return ret;
1777a02a946dSIlya Dryomov 
1778a02a946dSIlya Dryomov 	if (struct_v < 4) {
1779a02a946dSIlya Dryomov 		pr_err("got struct_v %d < 4 of hobject_t\n", struct_v);
1780a02a946dSIlya Dryomov 		goto e_inval;
1781a02a946dSIlya Dryomov 	}
1782a02a946dSIlya Dryomov 
1783a02a946dSIlya Dryomov 	hoid->key = ceph_extract_encoded_string(p, end, &hoid->key_len,
1784a02a946dSIlya Dryomov 						GFP_NOIO);
1785a02a946dSIlya Dryomov 	if (IS_ERR(hoid->key)) {
1786a02a946dSIlya Dryomov 		ret = PTR_ERR(hoid->key);
1787a02a946dSIlya Dryomov 		hoid->key = NULL;
1788a02a946dSIlya Dryomov 		return ret;
1789a02a946dSIlya Dryomov 	}
1790a02a946dSIlya Dryomov 
1791a02a946dSIlya Dryomov 	hoid->oid = ceph_extract_encoded_string(p, end, &hoid->oid_len,
1792a02a946dSIlya Dryomov 						GFP_NOIO);
1793a02a946dSIlya Dryomov 	if (IS_ERR(hoid->oid)) {
1794a02a946dSIlya Dryomov 		ret = PTR_ERR(hoid->oid);
1795a02a946dSIlya Dryomov 		hoid->oid = NULL;
1796a02a946dSIlya Dryomov 		return ret;
1797a02a946dSIlya Dryomov 	}
1798a02a946dSIlya Dryomov 
1799a02a946dSIlya Dryomov 	ceph_decode_64_safe(p, end, hoid->snapid, e_inval);
1800a02a946dSIlya Dryomov 	ceph_decode_32_safe(p, end, hoid->hash, e_inval);
1801a02a946dSIlya Dryomov 	ceph_decode_8_safe(p, end, hoid->is_max, e_inval);
1802a02a946dSIlya Dryomov 
1803a02a946dSIlya Dryomov 	hoid->nspace = ceph_extract_encoded_string(p, end, &hoid->nspace_len,
1804a02a946dSIlya Dryomov 						   GFP_NOIO);
1805a02a946dSIlya Dryomov 	if (IS_ERR(hoid->nspace)) {
1806a02a946dSIlya Dryomov 		ret = PTR_ERR(hoid->nspace);
1807a02a946dSIlya Dryomov 		hoid->nspace = NULL;
1808a02a946dSIlya Dryomov 		return ret;
1809a02a946dSIlya Dryomov 	}
1810a02a946dSIlya Dryomov 
1811a02a946dSIlya Dryomov 	ceph_decode_64_safe(p, end, hoid->pool, e_inval);
1812a02a946dSIlya Dryomov 
1813a02a946dSIlya Dryomov 	ceph_hoid_build_hash_cache(hoid);
1814a02a946dSIlya Dryomov 	return 0;
1815a02a946dSIlya Dryomov 
1816a02a946dSIlya Dryomov e_inval:
1817a02a946dSIlya Dryomov 	return -EINVAL;
1818a02a946dSIlya Dryomov }
1819a02a946dSIlya Dryomov 
1820a02a946dSIlya Dryomov static int hoid_encoding_size(const struct ceph_hobject_id *hoid)
1821a02a946dSIlya Dryomov {
1822a02a946dSIlya Dryomov 	return 8 + 4 + 1 + 8 + /* snapid, hash, is_max, pool */
1823a02a946dSIlya Dryomov 	       4 + hoid->key_len + 4 + hoid->oid_len + 4 + hoid->nspace_len;
1824a02a946dSIlya Dryomov }
1825a02a946dSIlya Dryomov 
1826a02a946dSIlya Dryomov static void encode_hoid(void **p, void *end, const struct ceph_hobject_id *hoid)
1827a02a946dSIlya Dryomov {
1828a02a946dSIlya Dryomov 	ceph_start_encoding(p, 4, 3, hoid_encoding_size(hoid));
1829a02a946dSIlya Dryomov 	ceph_encode_string(p, end, hoid->key, hoid->key_len);
1830a02a946dSIlya Dryomov 	ceph_encode_string(p, end, hoid->oid, hoid->oid_len);
1831a02a946dSIlya Dryomov 	ceph_encode_64(p, hoid->snapid);
1832a02a946dSIlya Dryomov 	ceph_encode_32(p, hoid->hash);
1833a02a946dSIlya Dryomov 	ceph_encode_8(p, hoid->is_max);
1834a02a946dSIlya Dryomov 	ceph_encode_string(p, end, hoid->nspace, hoid->nspace_len);
1835a02a946dSIlya Dryomov 	ceph_encode_64(p, hoid->pool);
1836a02a946dSIlya Dryomov }
1837a02a946dSIlya Dryomov 
1838a02a946dSIlya Dryomov static void free_hoid(struct ceph_hobject_id *hoid)
1839a02a946dSIlya Dryomov {
1840a02a946dSIlya Dryomov 	if (hoid) {
1841a02a946dSIlya Dryomov 		kfree(hoid->key);
1842a02a946dSIlya Dryomov 		kfree(hoid->oid);
1843a02a946dSIlya Dryomov 		kfree(hoid->nspace);
1844a02a946dSIlya Dryomov 		kfree(hoid);
1845a02a946dSIlya Dryomov 	}
1846a02a946dSIlya Dryomov }
1847a02a946dSIlya Dryomov 
1848a02a946dSIlya Dryomov static struct ceph_osd_backoff *alloc_backoff(void)
1849a02a946dSIlya Dryomov {
1850a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
1851a02a946dSIlya Dryomov 
1852a02a946dSIlya Dryomov 	backoff = kzalloc(sizeof(*backoff), GFP_NOIO);
1853a02a946dSIlya Dryomov 	if (!backoff)
1854a02a946dSIlya Dryomov 		return NULL;
1855a02a946dSIlya Dryomov 
1856a02a946dSIlya Dryomov 	RB_CLEAR_NODE(&backoff->spg_node);
1857a02a946dSIlya Dryomov 	RB_CLEAR_NODE(&backoff->id_node);
1858a02a946dSIlya Dryomov 	return backoff;
1859a02a946dSIlya Dryomov }
1860a02a946dSIlya Dryomov 
1861a02a946dSIlya Dryomov static void free_backoff(struct ceph_osd_backoff *backoff)
1862a02a946dSIlya Dryomov {
1863a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&backoff->spg_node));
1864a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&backoff->id_node));
1865a02a946dSIlya Dryomov 
1866a02a946dSIlya Dryomov 	free_hoid(backoff->begin);
1867a02a946dSIlya Dryomov 	free_hoid(backoff->end);
1868a02a946dSIlya Dryomov 	kfree(backoff);
1869a02a946dSIlya Dryomov }
1870a02a946dSIlya Dryomov 
1871a02a946dSIlya Dryomov /*
1872a02a946dSIlya Dryomov  * Within a specific spgid, backoffs are managed by ->begin hoid.
1873a02a946dSIlya Dryomov  */
1874a02a946dSIlya Dryomov DEFINE_RB_INSDEL_FUNCS2(backoff, struct ceph_osd_backoff, begin, hoid_compare,
1875a02a946dSIlya Dryomov 			RB_BYVAL, spg_node);
1876a02a946dSIlya Dryomov 
1877a02a946dSIlya Dryomov static struct ceph_osd_backoff *lookup_containing_backoff(struct rb_root *root,
1878a02a946dSIlya Dryomov 					    const struct ceph_hobject_id *hoid)
1879a02a946dSIlya Dryomov {
1880a02a946dSIlya Dryomov 	struct rb_node *n = root->rb_node;
1881a02a946dSIlya Dryomov 
1882a02a946dSIlya Dryomov 	while (n) {
1883a02a946dSIlya Dryomov 		struct ceph_osd_backoff *cur =
1884a02a946dSIlya Dryomov 		    rb_entry(n, struct ceph_osd_backoff, spg_node);
1885a02a946dSIlya Dryomov 		int cmp;
1886a02a946dSIlya Dryomov 
1887a02a946dSIlya Dryomov 		cmp = hoid_compare(hoid, cur->begin);
1888a02a946dSIlya Dryomov 		if (cmp < 0) {
1889a02a946dSIlya Dryomov 			n = n->rb_left;
1890a02a946dSIlya Dryomov 		} else if (cmp > 0) {
1891a02a946dSIlya Dryomov 			if (hoid_compare(hoid, cur->end) < 0)
1892a02a946dSIlya Dryomov 				return cur;
1893a02a946dSIlya Dryomov 
1894a02a946dSIlya Dryomov 			n = n->rb_right;
1895a02a946dSIlya Dryomov 		} else {
1896a02a946dSIlya Dryomov 			return cur;
1897a02a946dSIlya Dryomov 		}
1898a02a946dSIlya Dryomov 	}
1899a02a946dSIlya Dryomov 
1900a02a946dSIlya Dryomov 	return NULL;
1901a02a946dSIlya Dryomov }
1902a02a946dSIlya Dryomov 
1903a02a946dSIlya Dryomov /*
1904a02a946dSIlya Dryomov  * Each backoff has a unique id within its OSD session.
1905a02a946dSIlya Dryomov  */
1906a02a946dSIlya Dryomov DEFINE_RB_FUNCS(backoff_by_id, struct ceph_osd_backoff, id, id_node)
1907a02a946dSIlya Dryomov 
1908a02a946dSIlya Dryomov static void clear_backoffs(struct ceph_osd *osd)
1909a02a946dSIlya Dryomov {
1910a02a946dSIlya Dryomov 	while (!RB_EMPTY_ROOT(&osd->o_backoff_mappings)) {
1911a02a946dSIlya Dryomov 		struct ceph_spg_mapping *spg =
1912a02a946dSIlya Dryomov 		    rb_entry(rb_first(&osd->o_backoff_mappings),
1913a02a946dSIlya Dryomov 			     struct ceph_spg_mapping, node);
1914a02a946dSIlya Dryomov 
1915a02a946dSIlya Dryomov 		while (!RB_EMPTY_ROOT(&spg->backoffs)) {
1916a02a946dSIlya Dryomov 			struct ceph_osd_backoff *backoff =
1917a02a946dSIlya Dryomov 			    rb_entry(rb_first(&spg->backoffs),
1918a02a946dSIlya Dryomov 				     struct ceph_osd_backoff, spg_node);
1919a02a946dSIlya Dryomov 
1920a02a946dSIlya Dryomov 			erase_backoff(&spg->backoffs, backoff);
1921a02a946dSIlya Dryomov 			erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
1922a02a946dSIlya Dryomov 			free_backoff(backoff);
1923a02a946dSIlya Dryomov 		}
1924a02a946dSIlya Dryomov 		erase_spg_mapping(&osd->o_backoff_mappings, spg);
1925a02a946dSIlya Dryomov 		free_spg_mapping(spg);
1926a02a946dSIlya Dryomov 	}
1927a02a946dSIlya Dryomov }
1928a02a946dSIlya Dryomov 
1929a02a946dSIlya Dryomov /*
1930a02a946dSIlya Dryomov  * Set up a temporary, non-owning view into @t.
1931a02a946dSIlya Dryomov  */
1932a02a946dSIlya Dryomov static void hoid_fill_from_target(struct ceph_hobject_id *hoid,
1933a02a946dSIlya Dryomov 				  const struct ceph_osd_request_target *t)
1934a02a946dSIlya Dryomov {
1935a02a946dSIlya Dryomov 	hoid->key = NULL;
1936a02a946dSIlya Dryomov 	hoid->key_len = 0;
1937a02a946dSIlya Dryomov 	hoid->oid = t->target_oid.name;
1938a02a946dSIlya Dryomov 	hoid->oid_len = t->target_oid.name_len;
1939a02a946dSIlya Dryomov 	hoid->snapid = CEPH_NOSNAP;
1940a02a946dSIlya Dryomov 	hoid->hash = t->pgid.seed;
1941a02a946dSIlya Dryomov 	hoid->is_max = false;
1942a02a946dSIlya Dryomov 	if (t->target_oloc.pool_ns) {
1943a02a946dSIlya Dryomov 		hoid->nspace = t->target_oloc.pool_ns->str;
1944a02a946dSIlya Dryomov 		hoid->nspace_len = t->target_oloc.pool_ns->len;
1945a02a946dSIlya Dryomov 	} else {
1946a02a946dSIlya Dryomov 		hoid->nspace = NULL;
1947a02a946dSIlya Dryomov 		hoid->nspace_len = 0;
1948a02a946dSIlya Dryomov 	}
1949a02a946dSIlya Dryomov 	hoid->pool = t->target_oloc.pool;
1950a02a946dSIlya Dryomov 	ceph_hoid_build_hash_cache(hoid);
1951a02a946dSIlya Dryomov }
1952a02a946dSIlya Dryomov 
1953a02a946dSIlya Dryomov static bool should_plug_request(struct ceph_osd_request *req)
1954a02a946dSIlya Dryomov {
1955a02a946dSIlya Dryomov 	struct ceph_osd *osd = req->r_osd;
1956a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
1957a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
1958a02a946dSIlya Dryomov 	struct ceph_hobject_id hoid;
1959a02a946dSIlya Dryomov 
1960a02a946dSIlya Dryomov 	spg = lookup_spg_mapping(&osd->o_backoff_mappings, &req->r_t.spgid);
1961a02a946dSIlya Dryomov 	if (!spg)
1962a02a946dSIlya Dryomov 		return false;
1963a02a946dSIlya Dryomov 
1964a02a946dSIlya Dryomov 	hoid_fill_from_target(&hoid, &req->r_t);
1965a02a946dSIlya Dryomov 	backoff = lookup_containing_backoff(&spg->backoffs, &hoid);
1966a02a946dSIlya Dryomov 	if (!backoff)
1967a02a946dSIlya Dryomov 		return false;
1968a02a946dSIlya Dryomov 
1969a02a946dSIlya Dryomov 	dout("%s req %p tid %llu backoff osd%d spgid %llu.%xs%d id %llu\n",
1970a02a946dSIlya Dryomov 	     __func__, req, req->r_tid, osd->o_osd, backoff->spgid.pgid.pool,
1971a02a946dSIlya Dryomov 	     backoff->spgid.pgid.seed, backoff->spgid.shard, backoff->id);
1972a02a946dSIlya Dryomov 	return true;
1973a02a946dSIlya Dryomov }
1974a02a946dSIlya Dryomov 
19750d9c1ab3SIlya Dryomov /*
19760d9c1ab3SIlya Dryomov  * Keep get_num_data_items() in sync with this function.
19770d9c1ab3SIlya Dryomov  */
197898c4bfe9SIlya Dryomov static void setup_request_data(struct ceph_osd_request *req)
19793d14c5d2SYehuda Sadeh {
198098c4bfe9SIlya Dryomov 	struct ceph_msg *request_msg = req->r_request;
198198c4bfe9SIlya Dryomov 	struct ceph_msg *reply_msg = req->r_reply;
198298c4bfe9SIlya Dryomov 	struct ceph_osd_req_op *op;
19833d14c5d2SYehuda Sadeh 
198498c4bfe9SIlya Dryomov 	if (req->r_request->num_data_items || req->r_reply->num_data_items)
1985bb873b53SIlya Dryomov 		return;
19863d14c5d2SYehuda Sadeh 
198798c4bfe9SIlya Dryomov 	WARN_ON(request_msg->data_length || reply_msg->data_length);
198898c4bfe9SIlya Dryomov 	for (op = req->r_ops; op != &req->r_ops[req->r_num_ops]; op++) {
1989bb873b53SIlya Dryomov 		switch (op->op) {
1990bb873b53SIlya Dryomov 		/* request */
1991bb873b53SIlya Dryomov 		case CEPH_OSD_OP_WRITE:
1992bb873b53SIlya Dryomov 		case CEPH_OSD_OP_WRITEFULL:
1993bb873b53SIlya Dryomov 			WARN_ON(op->indata_len != op->extent.length);
199498c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(request_msg,
199598c4bfe9SIlya Dryomov 					       &op->extent.osd_data);
1996bb873b53SIlya Dryomov 			break;
1997bb873b53SIlya Dryomov 		case CEPH_OSD_OP_SETXATTR:
1998bb873b53SIlya Dryomov 		case CEPH_OSD_OP_CMPXATTR:
1999bb873b53SIlya Dryomov 			WARN_ON(op->indata_len != op->xattr.name_len +
2000bb873b53SIlya Dryomov 						  op->xattr.value_len);
200198c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(request_msg,
200298c4bfe9SIlya Dryomov 					       &op->xattr.osd_data);
2003bb873b53SIlya Dryomov 			break;
2004922dab61SIlya Dryomov 		case CEPH_OSD_OP_NOTIFY_ACK:
200598c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(request_msg,
2006922dab61SIlya Dryomov 					       &op->notify_ack.request_data);
2007922dab61SIlya Dryomov 			break;
200878beb0ffSLuis Henriques 		case CEPH_OSD_OP_COPY_FROM2:
200923ddf9beSLuis Henriques 			ceph_osdc_msg_data_add(request_msg,
201023ddf9beSLuis Henriques 					       &op->copy_from.osd_data);
201123ddf9beSLuis Henriques 			break;
2012bb873b53SIlya Dryomov 
2013bb873b53SIlya Dryomov 		/* reply */
2014bb873b53SIlya Dryomov 		case CEPH_OSD_OP_STAT:
201598c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(reply_msg,
2016bb873b53SIlya Dryomov 					       &op->raw_data_in);
2017bb873b53SIlya Dryomov 			break;
2018bb873b53SIlya Dryomov 		case CEPH_OSD_OP_READ:
201998c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(reply_msg,
2020bb873b53SIlya Dryomov 					       &op->extent.osd_data);
2021bb873b53SIlya Dryomov 			break;
2022a4ed38d7SDouglas Fuller 		case CEPH_OSD_OP_LIST_WATCHERS:
202398c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(reply_msg,
2024a4ed38d7SDouglas Fuller 					       &op->list_watchers.response_data);
2025a4ed38d7SDouglas Fuller 			break;
2026bb873b53SIlya Dryomov 
2027bb873b53SIlya Dryomov 		/* both */
2028bb873b53SIlya Dryomov 		case CEPH_OSD_OP_CALL:
2029bb873b53SIlya Dryomov 			WARN_ON(op->indata_len != op->cls.class_len +
2030bb873b53SIlya Dryomov 						  op->cls.method_len +
2031bb873b53SIlya Dryomov 						  op->cls.indata_len);
203298c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(request_msg,
203398c4bfe9SIlya Dryomov 					       &op->cls.request_info);
2034bb873b53SIlya Dryomov 			/* optional, can be NONE */
203598c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(request_msg,
203698c4bfe9SIlya Dryomov 					       &op->cls.request_data);
2037bb873b53SIlya Dryomov 			/* optional, can be NONE */
203898c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(reply_msg,
2039bb873b53SIlya Dryomov 					       &op->cls.response_data);
2040bb873b53SIlya Dryomov 			break;
204119079203SIlya Dryomov 		case CEPH_OSD_OP_NOTIFY:
204298c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(request_msg,
204319079203SIlya Dryomov 					       &op->notify.request_data);
204498c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(reply_msg,
204519079203SIlya Dryomov 					       &op->notify.response_data);
204619079203SIlya Dryomov 			break;
2047bb873b53SIlya Dryomov 		}
2048bb873b53SIlya Dryomov 	}
2049bb873b53SIlya Dryomov }
2050bb873b53SIlya Dryomov 
20512e59ffd1SIlya Dryomov static void encode_pgid(void **p, const struct ceph_pg *pgid)
20522e59ffd1SIlya Dryomov {
20532e59ffd1SIlya Dryomov 	ceph_encode_8(p, 1);
20542e59ffd1SIlya Dryomov 	ceph_encode_64(p, pgid->pool);
20552e59ffd1SIlya Dryomov 	ceph_encode_32(p, pgid->seed);
20562e59ffd1SIlya Dryomov 	ceph_encode_32(p, -1); /* preferred */
20572e59ffd1SIlya Dryomov }
20582e59ffd1SIlya Dryomov 
20598cb441c0SIlya Dryomov static void encode_spgid(void **p, const struct ceph_spg *spgid)
20608cb441c0SIlya Dryomov {
20618cb441c0SIlya Dryomov 	ceph_start_encoding(p, 1, 1, CEPH_PGID_ENCODING_LEN + 1);
20628cb441c0SIlya Dryomov 	encode_pgid(p, &spgid->pgid);
20638cb441c0SIlya Dryomov 	ceph_encode_8(p, spgid->shard);
20648cb441c0SIlya Dryomov }
20658cb441c0SIlya Dryomov 
20662e59ffd1SIlya Dryomov static void encode_oloc(void **p, void *end,
20672e59ffd1SIlya Dryomov 			const struct ceph_object_locator *oloc)
20682e59ffd1SIlya Dryomov {
20692e59ffd1SIlya Dryomov 	ceph_start_encoding(p, 5, 4, ceph_oloc_encoding_size(oloc));
20702e59ffd1SIlya Dryomov 	ceph_encode_64(p, oloc->pool);
20712e59ffd1SIlya Dryomov 	ceph_encode_32(p, -1); /* preferred */
20722e59ffd1SIlya Dryomov 	ceph_encode_32(p, 0);  /* key len */
20732e59ffd1SIlya Dryomov 	if (oloc->pool_ns)
20742e59ffd1SIlya Dryomov 		ceph_encode_string(p, end, oloc->pool_ns->str,
20752e59ffd1SIlya Dryomov 				   oloc->pool_ns->len);
20762e59ffd1SIlya Dryomov 	else
20772e59ffd1SIlya Dryomov 		ceph_encode_32(p, 0);
20782e59ffd1SIlya Dryomov }
20792e59ffd1SIlya Dryomov 
20808cb441c0SIlya Dryomov static void encode_request_partial(struct ceph_osd_request *req,
20818cb441c0SIlya Dryomov 				   struct ceph_msg *msg)
2082bb873b53SIlya Dryomov {
2083bb873b53SIlya Dryomov 	void *p = msg->front.iov_base;
2084bb873b53SIlya Dryomov 	void *const end = p + msg->front_alloc_len;
2085bb873b53SIlya Dryomov 	u32 data_len = 0;
2086bb873b53SIlya Dryomov 	int i;
2087bb873b53SIlya Dryomov 
2088bb873b53SIlya Dryomov 	if (req->r_flags & CEPH_OSD_FLAG_WRITE) {
2089bb873b53SIlya Dryomov 		/* snapshots aren't writeable */
2090bb873b53SIlya Dryomov 		WARN_ON(req->r_snapid != CEPH_NOSNAP);
2091bb873b53SIlya Dryomov 	} else {
2092bb873b53SIlya Dryomov 		WARN_ON(req->r_mtime.tv_sec || req->r_mtime.tv_nsec ||
2093bb873b53SIlya Dryomov 			req->r_data_offset || req->r_snapc);
2094bb873b53SIlya Dryomov 	}
2095bb873b53SIlya Dryomov 
209698c4bfe9SIlya Dryomov 	setup_request_data(req);
2097bb873b53SIlya Dryomov 
20988cb441c0SIlya Dryomov 	encode_spgid(&p, &req->r_t.spgid); /* actual spg */
20998cb441c0SIlya Dryomov 	ceph_encode_32(&p, req->r_t.pgid.seed); /* raw hash */
2100bb873b53SIlya Dryomov 	ceph_encode_32(&p, req->r_osdc->osdmap->epoch);
2101bb873b53SIlya Dryomov 	ceph_encode_32(&p, req->r_flags);
21028cb441c0SIlya Dryomov 
21038cb441c0SIlya Dryomov 	/* reqid */
21048cb441c0SIlya Dryomov 	ceph_start_encoding(&p, 2, 2, sizeof(struct ceph_osd_reqid));
21058cb441c0SIlya Dryomov 	memset(p, 0, sizeof(struct ceph_osd_reqid));
21068cb441c0SIlya Dryomov 	p += sizeof(struct ceph_osd_reqid);
21078cb441c0SIlya Dryomov 
21088cb441c0SIlya Dryomov 	/* trace */
21098cb441c0SIlya Dryomov 	memset(p, 0, sizeof(struct ceph_blkin_trace_info));
21108cb441c0SIlya Dryomov 	p += sizeof(struct ceph_blkin_trace_info);
21118cb441c0SIlya Dryomov 
21128cb441c0SIlya Dryomov 	ceph_encode_32(&p, 0); /* client_inc, always 0 */
2113fac02ddfSArnd Bergmann 	ceph_encode_timespec64(p, &req->r_mtime);
2114bb873b53SIlya Dryomov 	p += sizeof(struct ceph_timespec);
2115aa26d662SJeff Layton 
21162e59ffd1SIlya Dryomov 	encode_oloc(&p, end, &req->r_t.target_oloc);
21172e59ffd1SIlya Dryomov 	ceph_encode_string(&p, end, req->r_t.target_oid.name,
21182e59ffd1SIlya Dryomov 			   req->r_t.target_oid.name_len);
2119bb873b53SIlya Dryomov 
2120bb873b53SIlya Dryomov 	/* ops, can imply data */
2121bb873b53SIlya Dryomov 	ceph_encode_16(&p, req->r_num_ops);
2122bb873b53SIlya Dryomov 	for (i = 0; i < req->r_num_ops; i++) {
2123bb873b53SIlya Dryomov 		data_len += osd_req_encode_op(p, &req->r_ops[i]);
2124bb873b53SIlya Dryomov 		p += sizeof(struct ceph_osd_op);
2125bb873b53SIlya Dryomov 	}
2126bb873b53SIlya Dryomov 
2127bb873b53SIlya Dryomov 	ceph_encode_64(&p, req->r_snapid); /* snapid */
2128bb873b53SIlya Dryomov 	if (req->r_snapc) {
2129bb873b53SIlya Dryomov 		ceph_encode_64(&p, req->r_snapc->seq);
2130bb873b53SIlya Dryomov 		ceph_encode_32(&p, req->r_snapc->num_snaps);
2131bb873b53SIlya Dryomov 		for (i = 0; i < req->r_snapc->num_snaps; i++)
2132bb873b53SIlya Dryomov 			ceph_encode_64(&p, req->r_snapc->snaps[i]);
2133bb873b53SIlya Dryomov 	} else {
2134bb873b53SIlya Dryomov 		ceph_encode_64(&p, 0); /* snap_seq */
2135bb873b53SIlya Dryomov 		ceph_encode_32(&p, 0); /* snaps len */
2136bb873b53SIlya Dryomov 	}
2137bb873b53SIlya Dryomov 
2138bb873b53SIlya Dryomov 	ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
2139986e8989SIlya Dryomov 	BUG_ON(p > end - 8); /* space for features */
2140bb873b53SIlya Dryomov 
21418cb441c0SIlya Dryomov 	msg->hdr.version = cpu_to_le16(8); /* MOSDOp v8 */
21428cb441c0SIlya Dryomov 	/* front_len is finalized in encode_request_finish() */
2143986e8989SIlya Dryomov 	msg->front.iov_len = p - msg->front.iov_base;
2144986e8989SIlya Dryomov 	msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2145bb873b53SIlya Dryomov 	msg->hdr.data_len = cpu_to_le32(data_len);
2146bb873b53SIlya Dryomov 	/*
2147bb873b53SIlya Dryomov 	 * The header "data_off" is a hint to the receiver allowing it
2148bb873b53SIlya Dryomov 	 * to align received data into its buffers such that there's no
2149bb873b53SIlya Dryomov 	 * need to re-copy it before writing it to disk (direct I/O).
2150bb873b53SIlya Dryomov 	 */
2151bb873b53SIlya Dryomov 	msg->hdr.data_off = cpu_to_le16(req->r_data_offset);
2152bb873b53SIlya Dryomov 
21538cb441c0SIlya Dryomov 	dout("%s req %p msg %p oid %s oid_len %d\n", __func__, req, msg,
21548cb441c0SIlya Dryomov 	     req->r_t.target_oid.name, req->r_t.target_oid.name_len);
21558cb441c0SIlya Dryomov }
21568cb441c0SIlya Dryomov 
21578cb441c0SIlya Dryomov static void encode_request_finish(struct ceph_msg *msg)
21588cb441c0SIlya Dryomov {
21598cb441c0SIlya Dryomov 	void *p = msg->front.iov_base;
2160986e8989SIlya Dryomov 	void *const partial_end = p + msg->front.iov_len;
21618cb441c0SIlya Dryomov 	void *const end = p + msg->front_alloc_len;
21628cb441c0SIlya Dryomov 
21638cb441c0SIlya Dryomov 	if (CEPH_HAVE_FEATURE(msg->con->peer_features, RESEND_ON_SPLIT)) {
21648cb441c0SIlya Dryomov 		/* luminous OSD -- encode features and be done */
2165986e8989SIlya Dryomov 		p = partial_end;
21668cb441c0SIlya Dryomov 		ceph_encode_64(&p, msg->con->peer_features);
21678cb441c0SIlya Dryomov 	} else {
21688cb441c0SIlya Dryomov 		struct {
21698cb441c0SIlya Dryomov 			char spgid[CEPH_ENCODING_START_BLK_LEN +
21708cb441c0SIlya Dryomov 				   CEPH_PGID_ENCODING_LEN + 1];
21718cb441c0SIlya Dryomov 			__le32 hash;
21728cb441c0SIlya Dryomov 			__le32 epoch;
21738cb441c0SIlya Dryomov 			__le32 flags;
21748cb441c0SIlya Dryomov 			char reqid[CEPH_ENCODING_START_BLK_LEN +
21758cb441c0SIlya Dryomov 				   sizeof(struct ceph_osd_reqid)];
21768cb441c0SIlya Dryomov 			char trace[sizeof(struct ceph_blkin_trace_info)];
21778cb441c0SIlya Dryomov 			__le32 client_inc;
21788cb441c0SIlya Dryomov 			struct ceph_timespec mtime;
21798cb441c0SIlya Dryomov 		} __packed head;
21808cb441c0SIlya Dryomov 		struct ceph_pg pgid;
21818cb441c0SIlya Dryomov 		void *oloc, *oid, *tail;
21828cb441c0SIlya Dryomov 		int oloc_len, oid_len, tail_len;
21838cb441c0SIlya Dryomov 		int len;
21848cb441c0SIlya Dryomov 
21858cb441c0SIlya Dryomov 		/*
21868cb441c0SIlya Dryomov 		 * Pre-luminous OSD -- reencode v8 into v4 using @head
21878cb441c0SIlya Dryomov 		 * as a temporary buffer.  Encode the raw PG; the rest
21888cb441c0SIlya Dryomov 		 * is just a matter of moving oloc, oid and tail blobs
21898cb441c0SIlya Dryomov 		 * around.
21908cb441c0SIlya Dryomov 		 */
21918cb441c0SIlya Dryomov 		memcpy(&head, p, sizeof(head));
21928cb441c0SIlya Dryomov 		p += sizeof(head);
21938cb441c0SIlya Dryomov 
21948cb441c0SIlya Dryomov 		oloc = p;
21958cb441c0SIlya Dryomov 		p += CEPH_ENCODING_START_BLK_LEN;
21968cb441c0SIlya Dryomov 		pgid.pool = ceph_decode_64(&p);
21978cb441c0SIlya Dryomov 		p += 4 + 4; /* preferred, key len */
21988cb441c0SIlya Dryomov 		len = ceph_decode_32(&p);
21998cb441c0SIlya Dryomov 		p += len;   /* nspace */
22008cb441c0SIlya Dryomov 		oloc_len = p - oloc;
22018cb441c0SIlya Dryomov 
22028cb441c0SIlya Dryomov 		oid = p;
22038cb441c0SIlya Dryomov 		len = ceph_decode_32(&p);
22048cb441c0SIlya Dryomov 		p += len;
22058cb441c0SIlya Dryomov 		oid_len = p - oid;
22068cb441c0SIlya Dryomov 
22078cb441c0SIlya Dryomov 		tail = p;
2208986e8989SIlya Dryomov 		tail_len = partial_end - p;
22098cb441c0SIlya Dryomov 
22108cb441c0SIlya Dryomov 		p = msg->front.iov_base;
22118cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.client_inc, sizeof(head.client_inc));
22128cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.epoch, sizeof(head.epoch));
22138cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.flags, sizeof(head.flags));
22148cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.mtime, sizeof(head.mtime));
22158cb441c0SIlya Dryomov 
22168cb441c0SIlya Dryomov 		/* reassert_version */
22178cb441c0SIlya Dryomov 		memset(p, 0, sizeof(struct ceph_eversion));
22188cb441c0SIlya Dryomov 		p += sizeof(struct ceph_eversion);
22198cb441c0SIlya Dryomov 
22208cb441c0SIlya Dryomov 		BUG_ON(p >= oloc);
22218cb441c0SIlya Dryomov 		memmove(p, oloc, oloc_len);
22228cb441c0SIlya Dryomov 		p += oloc_len;
22238cb441c0SIlya Dryomov 
22248cb441c0SIlya Dryomov 		pgid.seed = le32_to_cpu(head.hash);
22258cb441c0SIlya Dryomov 		encode_pgid(&p, &pgid); /* raw pg */
22268cb441c0SIlya Dryomov 
22278cb441c0SIlya Dryomov 		BUG_ON(p >= oid);
22288cb441c0SIlya Dryomov 		memmove(p, oid, oid_len);
22298cb441c0SIlya Dryomov 		p += oid_len;
22308cb441c0SIlya Dryomov 
22318cb441c0SIlya Dryomov 		/* tail -- ops, snapid, snapc, retry_attempt */
22328cb441c0SIlya Dryomov 		BUG_ON(p >= tail);
22338cb441c0SIlya Dryomov 		memmove(p, tail, tail_len);
22348cb441c0SIlya Dryomov 		p += tail_len;
22358cb441c0SIlya Dryomov 
22368cb441c0SIlya Dryomov 		msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
22378cb441c0SIlya Dryomov 	}
22388cb441c0SIlya Dryomov 
22398cb441c0SIlya Dryomov 	BUG_ON(p > end);
22408cb441c0SIlya Dryomov 	msg->front.iov_len = p - msg->front.iov_base;
22418cb441c0SIlya Dryomov 	msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
22428cb441c0SIlya Dryomov 
22438cb441c0SIlya Dryomov 	dout("%s msg %p tid %llu %u+%u+%u v%d\n", __func__, msg,
22448cb441c0SIlya Dryomov 	     le64_to_cpu(msg->hdr.tid), le32_to_cpu(msg->hdr.front_len),
22458cb441c0SIlya Dryomov 	     le32_to_cpu(msg->hdr.middle_len), le32_to_cpu(msg->hdr.data_len),
22468cb441c0SIlya Dryomov 	     le16_to_cpu(msg->hdr.version));
2247bb873b53SIlya Dryomov }
2248bb873b53SIlya Dryomov 
2249bb873b53SIlya Dryomov /*
2250bb873b53SIlya Dryomov  * @req has to be assigned a tid and registered.
2251bb873b53SIlya Dryomov  */
2252bb873b53SIlya Dryomov static void send_request(struct ceph_osd_request *req)
2253bb873b53SIlya Dryomov {
2254bb873b53SIlya Dryomov 	struct ceph_osd *osd = req->r_osd;
2255bb873b53SIlya Dryomov 
22565aea3dcdSIlya Dryomov 	verify_osd_locked(osd);
2257bb873b53SIlya Dryomov 	WARN_ON(osd->o_osd != req->r_t.osd);
2258bb873b53SIlya Dryomov 
2259a02a946dSIlya Dryomov 	/* backoff? */
2260a02a946dSIlya Dryomov 	if (should_plug_request(req))
2261a02a946dSIlya Dryomov 		return;
2262a02a946dSIlya Dryomov 
22635aea3dcdSIlya Dryomov 	/*
22645aea3dcdSIlya Dryomov 	 * We may have a previously queued request message hanging
22655aea3dcdSIlya Dryomov 	 * around.  Cancel it to avoid corrupting the msgr.
22665aea3dcdSIlya Dryomov 	 */
22675aea3dcdSIlya Dryomov 	if (req->r_sent)
22685aea3dcdSIlya Dryomov 		ceph_msg_revoke(req->r_request);
22695aea3dcdSIlya Dryomov 
2270bb873b53SIlya Dryomov 	req->r_flags |= CEPH_OSD_FLAG_KNOWN_REDIR;
2271bb873b53SIlya Dryomov 	if (req->r_attempts)
2272bb873b53SIlya Dryomov 		req->r_flags |= CEPH_OSD_FLAG_RETRY;
2273bb873b53SIlya Dryomov 	else
2274bb873b53SIlya Dryomov 		WARN_ON(req->r_flags & CEPH_OSD_FLAG_RETRY);
2275bb873b53SIlya Dryomov 
22768cb441c0SIlya Dryomov 	encode_request_partial(req, req->r_request);
2277bb873b53SIlya Dryomov 
227804c7d789SIlya 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",
2279bb873b53SIlya Dryomov 	     __func__, req, req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed,
2280dc98ff72SIlya Dryomov 	     req->r_t.spgid.pgid.pool, req->r_t.spgid.pgid.seed,
228104c7d789SIlya Dryomov 	     req->r_t.spgid.shard, osd->o_osd, req->r_t.epoch, req->r_flags,
228204c7d789SIlya Dryomov 	     req->r_attempts);
2283bb873b53SIlya Dryomov 
2284bb873b53SIlya Dryomov 	req->r_t.paused = false;
22853d14c5d2SYehuda Sadeh 	req->r_stamp = jiffies;
2286bb873b53SIlya Dryomov 	req->r_attempts++;
22873d14c5d2SYehuda Sadeh 
2288bb873b53SIlya Dryomov 	req->r_sent = osd->o_incarnation;
2289bb873b53SIlya Dryomov 	req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
2290bb873b53SIlya Dryomov 	ceph_con_send(&osd->o_con, ceph_msg_get(req->r_request));
22913d14c5d2SYehuda Sadeh }
22923d14c5d2SYehuda Sadeh 
229342c1b124SIlya Dryomov static void maybe_request_map(struct ceph_osd_client *osdc)
229442c1b124SIlya Dryomov {
229542c1b124SIlya Dryomov 	bool continuous = false;
229642c1b124SIlya Dryomov 
22975aea3dcdSIlya Dryomov 	verify_osdc_locked(osdc);
229842c1b124SIlya Dryomov 	WARN_ON(!osdc->osdmap->epoch);
229942c1b124SIlya Dryomov 
2300b7ec35b3SIlya Dryomov 	if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2301b7ec35b3SIlya Dryomov 	    ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD) ||
2302b7ec35b3SIlya Dryomov 	    ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
230342c1b124SIlya Dryomov 		dout("%s osdc %p continuous\n", __func__, osdc);
230442c1b124SIlya Dryomov 		continuous = true;
230542c1b124SIlya Dryomov 	} else {
230642c1b124SIlya Dryomov 		dout("%s osdc %p onetime\n", __func__, osdc);
230742c1b124SIlya Dryomov 	}
230842c1b124SIlya Dryomov 
230942c1b124SIlya Dryomov 	if (ceph_monc_want_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
231042c1b124SIlya Dryomov 			       osdc->osdmap->epoch + 1, continuous))
231142c1b124SIlya Dryomov 		ceph_monc_renew_subs(&osdc->client->monc);
231242c1b124SIlya Dryomov }
231342c1b124SIlya Dryomov 
2314a1f4020aSJeff Layton static void complete_request(struct ceph_osd_request *req, int err);
23154609245eSIlya Dryomov static void send_map_check(struct ceph_osd_request *req);
23164609245eSIlya Dryomov 
23175aea3dcdSIlya Dryomov static void __submit_request(struct ceph_osd_request *req, bool wrlocked)
23180bbfdfe8SIlya Dryomov {
23195aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
23205aea3dcdSIlya Dryomov 	struct ceph_osd *osd;
23214609245eSIlya Dryomov 	enum calc_target_result ct_res;
232266850df5SIlya Dryomov 	int err = 0;
23235aea3dcdSIlya Dryomov 	bool need_send = false;
23245aea3dcdSIlya Dryomov 	bool promoted = false;
23250bbfdfe8SIlya Dryomov 
2326b18b9550SIlya Dryomov 	WARN_ON(req->r_tid);
23275aea3dcdSIlya Dryomov 	dout("%s req %p wrlocked %d\n", __func__, req, wrlocked);
23285aea3dcdSIlya Dryomov 
23295aea3dcdSIlya Dryomov again:
23308edf84baSIlya Dryomov 	ct_res = calc_target(osdc, &req->r_t, false);
23314609245eSIlya Dryomov 	if (ct_res == CALC_TARGET_POOL_DNE && !wrlocked)
23324609245eSIlya Dryomov 		goto promote;
23334609245eSIlya Dryomov 
23345aea3dcdSIlya Dryomov 	osd = lookup_create_osd(osdc, req->r_t.osd, wrlocked);
23355aea3dcdSIlya Dryomov 	if (IS_ERR(osd)) {
23365aea3dcdSIlya Dryomov 		WARN_ON(PTR_ERR(osd) != -EAGAIN || wrlocked);
23375aea3dcdSIlya Dryomov 		goto promote;
23385aea3dcdSIlya Dryomov 	}
23395aea3dcdSIlya Dryomov 
234066850df5SIlya Dryomov 	if (osdc->abort_err) {
234166850df5SIlya Dryomov 		dout("req %p abort_err %d\n", req, osdc->abort_err);
234266850df5SIlya Dryomov 		err = osdc->abort_err;
234366850df5SIlya Dryomov 	} else if (osdc->osdmap->epoch < osdc->epoch_barrier) {
234458eb7932SJeff Layton 		dout("req %p epoch %u barrier %u\n", req, osdc->osdmap->epoch,
234558eb7932SJeff Layton 		     osdc->epoch_barrier);
234658eb7932SJeff Layton 		req->r_t.paused = true;
234758eb7932SJeff Layton 		maybe_request_map(osdc);
234858eb7932SJeff Layton 	} else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2349b7ec35b3SIlya Dryomov 		   ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
23505aea3dcdSIlya Dryomov 		dout("req %p pausewr\n", req);
23515aea3dcdSIlya Dryomov 		req->r_t.paused = true;
23525aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
23535aea3dcdSIlya Dryomov 	} else if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
2354b7ec35b3SIlya Dryomov 		   ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
23555aea3dcdSIlya Dryomov 		dout("req %p pauserd\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_WRITE) &&
23595aea3dcdSIlya Dryomov 		   !(req->r_flags & (CEPH_OSD_FLAG_FULL_TRY |
23605aea3dcdSIlya Dryomov 				     CEPH_OSD_FLAG_FULL_FORCE)) &&
2361b7ec35b3SIlya Dryomov 		   (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
23625aea3dcdSIlya Dryomov 		    pool_full(osdc, req->r_t.base_oloc.pool))) {
23635aea3dcdSIlya Dryomov 		dout("req %p full/pool_full\n", req);
236402b2f549SDongsheng Yang 		if (ceph_test_opt(osdc->client, ABORT_ON_FULL)) {
236529e87820SIlya Dryomov 			err = -ENOSPC;
236629e87820SIlya Dryomov 		} else {
2367dc9b0dc4SIlya Dryomov 			if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL))
2368dc9b0dc4SIlya Dryomov 				pr_warn_ratelimited("cluster is full (osdmap FULL)\n");
2369dc9b0dc4SIlya Dryomov 			else
2370dc9b0dc4SIlya Dryomov 				pr_warn_ratelimited("pool %lld is full or reached quota\n",
2371dc9b0dc4SIlya Dryomov 						    req->r_t.base_oloc.pool);
23725aea3dcdSIlya Dryomov 			req->r_t.paused = true;
23735aea3dcdSIlya Dryomov 			maybe_request_map(osdc);
237429e87820SIlya Dryomov 		}
23755aea3dcdSIlya Dryomov 	} else if (!osd_homeless(osd)) {
23765aea3dcdSIlya Dryomov 		need_send = true;
23770bbfdfe8SIlya Dryomov 	} else {
23785aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
23790bbfdfe8SIlya Dryomov 	}
23800bbfdfe8SIlya Dryomov 
23815aea3dcdSIlya Dryomov 	mutex_lock(&osd->lock);
23825aea3dcdSIlya Dryomov 	/*
23835aea3dcdSIlya Dryomov 	 * Assign the tid atomically with send_request() to protect
23845aea3dcdSIlya Dryomov 	 * multiple writes to the same object from racing with each
23855aea3dcdSIlya Dryomov 	 * other, resulting in out of order ops on the OSDs.
23865aea3dcdSIlya Dryomov 	 */
23875aea3dcdSIlya Dryomov 	req->r_tid = atomic64_inc_return(&osdc->last_tid);
23885aea3dcdSIlya Dryomov 	link_request(osd, req);
23895aea3dcdSIlya Dryomov 	if (need_send)
23905aea3dcdSIlya Dryomov 		send_request(req);
239166850df5SIlya Dryomov 	else if (err)
239266850df5SIlya Dryomov 		complete_request(req, err);
23935aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
23945aea3dcdSIlya Dryomov 
23956001567cSIlya Dryomov 	if (!err && ct_res == CALC_TARGET_POOL_DNE)
23964609245eSIlya Dryomov 		send_map_check(req);
23974609245eSIlya Dryomov 
23985aea3dcdSIlya Dryomov 	if (promoted)
23995aea3dcdSIlya Dryomov 		downgrade_write(&osdc->lock);
24005aea3dcdSIlya Dryomov 	return;
24015aea3dcdSIlya Dryomov 
24025aea3dcdSIlya Dryomov promote:
24035aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
24045aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
24055aea3dcdSIlya Dryomov 	wrlocked = true;
24065aea3dcdSIlya Dryomov 	promoted = true;
24075aea3dcdSIlya Dryomov 	goto again;
24080bbfdfe8SIlya Dryomov }
24090bbfdfe8SIlya Dryomov 
24105aea3dcdSIlya Dryomov static void account_request(struct ceph_osd_request *req)
24115aea3dcdSIlya Dryomov {
241254ea0046SIlya Dryomov 	WARN_ON(req->r_flags & (CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK));
2413b18b9550SIlya Dryomov 	WARN_ON(!(req->r_flags & (CEPH_OSD_FLAG_READ | CEPH_OSD_FLAG_WRITE)));
24145aea3dcdSIlya Dryomov 
2415b18b9550SIlya Dryomov 	req->r_flags |= CEPH_OSD_FLAG_ONDISK;
241622d2cfdfSIlya Dryomov 	atomic_inc(&req->r_osdc->num_requests);
24177cc5e38fSIlya Dryomov 
24187cc5e38fSIlya Dryomov 	req->r_start_stamp = jiffies;
241997e27aaaSXiubo Li 	req->r_start_latency = ktime_get();
24205aea3dcdSIlya Dryomov }
24215aea3dcdSIlya Dryomov 
24225aea3dcdSIlya Dryomov static void submit_request(struct ceph_osd_request *req, bool wrlocked)
24235aea3dcdSIlya Dryomov {
24245aea3dcdSIlya Dryomov 	ceph_osdc_get_request(req);
24255aea3dcdSIlya Dryomov 	account_request(req);
24265aea3dcdSIlya Dryomov 	__submit_request(req, wrlocked);
24275aea3dcdSIlya Dryomov }
24285aea3dcdSIlya Dryomov 
242945ee2c1dSIlya Dryomov static void finish_request(struct ceph_osd_request *req)
24305aea3dcdSIlya Dryomov {
24315aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
24325aea3dcdSIlya Dryomov 
24334609245eSIlya Dryomov 	WARN_ON(lookup_request_mc(&osdc->map_checks, req->r_tid));
243404c7d789SIlya Dryomov 	dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
243504c7d789SIlya Dryomov 
243697e27aaaSXiubo Li 	req->r_end_latency = ktime_get();
243797e27aaaSXiubo Li 
243804c7d789SIlya Dryomov 	if (req->r_osd)
243904c7d789SIlya Dryomov 		unlink_request(req->r_osd, req);
24405aea3dcdSIlya Dryomov 	atomic_dec(&osdc->num_requests);
24415aea3dcdSIlya Dryomov 
24425aea3dcdSIlya Dryomov 	/*
24435aea3dcdSIlya Dryomov 	 * If an OSD has failed or returned and a request has been sent
24445aea3dcdSIlya Dryomov 	 * twice, it's possible to get a reply and end up here while the
24455aea3dcdSIlya Dryomov 	 * request message is queued for delivery.  We will ignore the
24465aea3dcdSIlya Dryomov 	 * reply, so not a big deal, but better to try and catch it.
24475aea3dcdSIlya Dryomov 	 */
24485aea3dcdSIlya Dryomov 	ceph_msg_revoke(req->r_request);
24495aea3dcdSIlya Dryomov 	ceph_msg_revoke_incoming(req->r_reply);
24505aea3dcdSIlya Dryomov }
24515aea3dcdSIlya Dryomov 
2452fe5da05eSIlya Dryomov static void __complete_request(struct ceph_osd_request *req)
2453fe5da05eSIlya Dryomov {
2454d75f773cSSakari Ailus 	dout("%s req %p tid %llu cb %ps result %d\n", __func__, req,
2455b18b9550SIlya Dryomov 	     req->r_tid, req->r_callback, req->r_result);
245626df726bSIlya Dryomov 
245726df726bSIlya Dryomov 	if (req->r_callback)
2458fe5da05eSIlya Dryomov 		req->r_callback(req);
245926df726bSIlya Dryomov 	complete_all(&req->r_completion);
246026df726bSIlya Dryomov 	ceph_osdc_put_request(req);
2461b18b9550SIlya Dryomov }
2462fe5da05eSIlya Dryomov 
246388bc1922SIlya Dryomov static void complete_request_workfn(struct work_struct *work)
246488bc1922SIlya Dryomov {
246588bc1922SIlya Dryomov 	struct ceph_osd_request *req =
246688bc1922SIlya Dryomov 	    container_of(work, struct ceph_osd_request, r_complete_work);
246788bc1922SIlya Dryomov 
246888bc1922SIlya Dryomov 	__complete_request(req);
24690bbfdfe8SIlya Dryomov }
24700bbfdfe8SIlya Dryomov 
24714609245eSIlya Dryomov /*
2472b18b9550SIlya Dryomov  * This is open-coded in handle_reply().
24734609245eSIlya Dryomov  */
24744609245eSIlya Dryomov static void complete_request(struct ceph_osd_request *req, int err)
24754609245eSIlya Dryomov {
24764609245eSIlya Dryomov 	dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
24774609245eSIlya Dryomov 
24784609245eSIlya Dryomov 	req->r_result = err;
247945ee2c1dSIlya Dryomov 	finish_request(req);
248088bc1922SIlya Dryomov 
248188bc1922SIlya Dryomov 	INIT_WORK(&req->r_complete_work, complete_request_workfn);
248288bc1922SIlya Dryomov 	queue_work(req->r_osdc->completion_wq, &req->r_complete_work);
24834609245eSIlya Dryomov }
24844609245eSIlya Dryomov 
24854609245eSIlya Dryomov static void cancel_map_check(struct ceph_osd_request *req)
24864609245eSIlya Dryomov {
24874609245eSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
24884609245eSIlya Dryomov 	struct ceph_osd_request *lookup_req;
24894609245eSIlya Dryomov 
24904609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
24914609245eSIlya Dryomov 
24924609245eSIlya Dryomov 	lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
24934609245eSIlya Dryomov 	if (!lookup_req)
24944609245eSIlya Dryomov 		return;
24954609245eSIlya Dryomov 
24964609245eSIlya Dryomov 	WARN_ON(lookup_req != req);
24974609245eSIlya Dryomov 	erase_request_mc(&osdc->map_checks, req);
24984609245eSIlya Dryomov 	ceph_osdc_put_request(req);
24994609245eSIlya Dryomov }
25004609245eSIlya Dryomov 
25015aea3dcdSIlya Dryomov static void cancel_request(struct ceph_osd_request *req)
25025aea3dcdSIlya Dryomov {
25035aea3dcdSIlya Dryomov 	dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
25045aea3dcdSIlya Dryomov 
25054609245eSIlya Dryomov 	cancel_map_check(req);
250645ee2c1dSIlya Dryomov 	finish_request(req);
2507b18b9550SIlya Dryomov 	complete_all(&req->r_completion);
2508c297eb42SIlya Dryomov 	ceph_osdc_put_request(req);
25095aea3dcdSIlya Dryomov }
25105aea3dcdSIlya Dryomov 
25117cc5e38fSIlya Dryomov static void abort_request(struct ceph_osd_request *req, int err)
25127cc5e38fSIlya Dryomov {
25137cc5e38fSIlya Dryomov 	dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
25147cc5e38fSIlya Dryomov 
25157cc5e38fSIlya Dryomov 	cancel_map_check(req);
25167cc5e38fSIlya Dryomov 	complete_request(req, err);
25177cc5e38fSIlya Dryomov }
25187cc5e38fSIlya Dryomov 
251966850df5SIlya Dryomov static int abort_fn(struct ceph_osd_request *req, void *arg)
252066850df5SIlya Dryomov {
252166850df5SIlya Dryomov 	int err = *(int *)arg;
252266850df5SIlya Dryomov 
252366850df5SIlya Dryomov 	abort_request(req, err);
252466850df5SIlya Dryomov 	return 0; /* continue iteration */
252566850df5SIlya Dryomov }
252666850df5SIlya Dryomov 
252766850df5SIlya Dryomov /*
252866850df5SIlya Dryomov  * Abort all in-flight requests with @err and arrange for all future
252966850df5SIlya Dryomov  * requests to be failed immediately.
253066850df5SIlya Dryomov  */
253166850df5SIlya Dryomov void ceph_osdc_abort_requests(struct ceph_osd_client *osdc, int err)
253266850df5SIlya Dryomov {
253366850df5SIlya Dryomov 	dout("%s osdc %p err %d\n", __func__, osdc, err);
253466850df5SIlya Dryomov 	down_write(&osdc->lock);
253566850df5SIlya Dryomov 	for_each_request(osdc, abort_fn, &err);
253666850df5SIlya Dryomov 	osdc->abort_err = err;
253766850df5SIlya Dryomov 	up_write(&osdc->lock);
253866850df5SIlya Dryomov }
253966850df5SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_abort_requests);
254066850df5SIlya Dryomov 
25412cef0ba8SYan, Zheng void ceph_osdc_clear_abort_err(struct ceph_osd_client *osdc)
25422cef0ba8SYan, Zheng {
25432cef0ba8SYan, Zheng 	down_write(&osdc->lock);
25442cef0ba8SYan, Zheng 	osdc->abort_err = 0;
25452cef0ba8SYan, Zheng 	up_write(&osdc->lock);
25462cef0ba8SYan, Zheng }
25472cef0ba8SYan, Zheng EXPORT_SYMBOL(ceph_osdc_clear_abort_err);
25482cef0ba8SYan, Zheng 
254958eb7932SJeff Layton static void update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
255058eb7932SJeff Layton {
255158eb7932SJeff Layton 	if (likely(eb > osdc->epoch_barrier)) {
255258eb7932SJeff Layton 		dout("updating epoch_barrier from %u to %u\n",
255358eb7932SJeff Layton 				osdc->epoch_barrier, eb);
255458eb7932SJeff Layton 		osdc->epoch_barrier = eb;
255558eb7932SJeff Layton 		/* Request map if we're not to the barrier yet */
255658eb7932SJeff Layton 		if (eb > osdc->osdmap->epoch)
255758eb7932SJeff Layton 			maybe_request_map(osdc);
255858eb7932SJeff Layton 	}
255958eb7932SJeff Layton }
256058eb7932SJeff Layton 
256158eb7932SJeff Layton void ceph_osdc_update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
256258eb7932SJeff Layton {
256358eb7932SJeff Layton 	down_read(&osdc->lock);
256458eb7932SJeff Layton 	if (unlikely(eb > osdc->epoch_barrier)) {
256558eb7932SJeff Layton 		up_read(&osdc->lock);
256658eb7932SJeff Layton 		down_write(&osdc->lock);
256758eb7932SJeff Layton 		update_epoch_barrier(osdc, eb);
256858eb7932SJeff Layton 		up_write(&osdc->lock);
256958eb7932SJeff Layton 	} else {
257058eb7932SJeff Layton 		up_read(&osdc->lock);
257158eb7932SJeff Layton 	}
257258eb7932SJeff Layton }
257358eb7932SJeff Layton EXPORT_SYMBOL(ceph_osdc_update_epoch_barrier);
257458eb7932SJeff Layton 
2575fc36d0a4SJeff Layton /*
25764eea0fefSIlya Dryomov  * We can end up releasing caps as a result of abort_request().
25774eea0fefSIlya Dryomov  * In that case, we probably want to ensure that the cap release message
25784eea0fefSIlya Dryomov  * has an updated epoch barrier in it, so set the epoch barrier prior to
25794eea0fefSIlya Dryomov  * aborting the first request.
25804eea0fefSIlya Dryomov  */
25814eea0fefSIlya Dryomov static int abort_on_full_fn(struct ceph_osd_request *req, void *arg)
25824eea0fefSIlya Dryomov {
25834eea0fefSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
25844eea0fefSIlya Dryomov 	bool *victims = arg;
25854eea0fefSIlya Dryomov 
2586c843d13cSIlya Dryomov 	if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
25874eea0fefSIlya Dryomov 	    (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2588690f951dSIlya Dryomov 	     pool_full(osdc, req->r_t.base_oloc.pool))) {
25894eea0fefSIlya Dryomov 		if (!*victims) {
25904eea0fefSIlya Dryomov 			update_epoch_barrier(osdc, osdc->osdmap->epoch);
25914eea0fefSIlya Dryomov 			*victims = true;
25924eea0fefSIlya Dryomov 		}
25934eea0fefSIlya Dryomov 		abort_request(req, -ENOSPC);
25944eea0fefSIlya Dryomov 	}
25954eea0fefSIlya Dryomov 
25964eea0fefSIlya Dryomov 	return 0; /* continue iteration */
25974eea0fefSIlya Dryomov }
25984eea0fefSIlya Dryomov 
25994eea0fefSIlya Dryomov /*
2600fc36d0a4SJeff Layton  * Drop all pending requests that are stalled waiting on a full condition to
260158eb7932SJeff Layton  * clear, and complete them with ENOSPC as the return code. Set the
260258eb7932SJeff Layton  * osdc->epoch_barrier to the latest map epoch that we've seen if any were
260358eb7932SJeff Layton  * cancelled.
2604fc36d0a4SJeff Layton  */
2605fc36d0a4SJeff Layton static void ceph_osdc_abort_on_full(struct ceph_osd_client *osdc)
2606fc36d0a4SJeff Layton {
260758eb7932SJeff Layton 	bool victims = false;
2608fc36d0a4SJeff Layton 
260902b2f549SDongsheng Yang 	if (ceph_test_opt(osdc->client, ABORT_ON_FULL) &&
2610c843d13cSIlya Dryomov 	    (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) || have_pool_full(osdc)))
26114eea0fefSIlya Dryomov 		for_each_request(osdc, abort_on_full_fn, &victims);
2612fc36d0a4SJeff Layton }
2613fc36d0a4SJeff Layton 
26144609245eSIlya Dryomov static void check_pool_dne(struct ceph_osd_request *req)
26154609245eSIlya Dryomov {
26164609245eSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
26174609245eSIlya Dryomov 	struct ceph_osdmap *map = osdc->osdmap;
26184609245eSIlya Dryomov 
26194609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
26204609245eSIlya Dryomov 	WARN_ON(!map->epoch);
26214609245eSIlya Dryomov 
26224609245eSIlya Dryomov 	if (req->r_attempts) {
26234609245eSIlya Dryomov 		/*
26244609245eSIlya Dryomov 		 * We sent a request earlier, which means that
26254609245eSIlya Dryomov 		 * previously the pool existed, and now it does not
26264609245eSIlya Dryomov 		 * (i.e., it was deleted).
26274609245eSIlya Dryomov 		 */
26284609245eSIlya Dryomov 		req->r_map_dne_bound = map->epoch;
26294609245eSIlya Dryomov 		dout("%s req %p tid %llu pool disappeared\n", __func__, req,
26304609245eSIlya Dryomov 		     req->r_tid);
26314609245eSIlya Dryomov 	} else {
26324609245eSIlya Dryomov 		dout("%s req %p tid %llu map_dne_bound %u have %u\n", __func__,
26334609245eSIlya Dryomov 		     req, req->r_tid, req->r_map_dne_bound, map->epoch);
26344609245eSIlya Dryomov 	}
26354609245eSIlya Dryomov 
26364609245eSIlya Dryomov 	if (req->r_map_dne_bound) {
26374609245eSIlya Dryomov 		if (map->epoch >= req->r_map_dne_bound) {
26384609245eSIlya Dryomov 			/* we had a new enough map */
26394609245eSIlya Dryomov 			pr_info_ratelimited("tid %llu pool does not exist\n",
26404609245eSIlya Dryomov 					    req->r_tid);
26414609245eSIlya Dryomov 			complete_request(req, -ENOENT);
26424609245eSIlya Dryomov 		}
26434609245eSIlya Dryomov 	} else {
26444609245eSIlya Dryomov 		send_map_check(req);
26454609245eSIlya Dryomov 	}
26464609245eSIlya Dryomov }
26474609245eSIlya Dryomov 
26484609245eSIlya Dryomov static void map_check_cb(struct ceph_mon_generic_request *greq)
26494609245eSIlya Dryomov {
26504609245eSIlya Dryomov 	struct ceph_osd_client *osdc = &greq->monc->client->osdc;
26514609245eSIlya Dryomov 	struct ceph_osd_request *req;
26524609245eSIlya Dryomov 	u64 tid = greq->private_data;
26534609245eSIlya Dryomov 
26544609245eSIlya Dryomov 	WARN_ON(greq->result || !greq->u.newest);
26554609245eSIlya Dryomov 
26564609245eSIlya Dryomov 	down_write(&osdc->lock);
26574609245eSIlya Dryomov 	req = lookup_request_mc(&osdc->map_checks, tid);
26584609245eSIlya Dryomov 	if (!req) {
26594609245eSIlya Dryomov 		dout("%s tid %llu dne\n", __func__, tid);
26604609245eSIlya Dryomov 		goto out_unlock;
26614609245eSIlya Dryomov 	}
26624609245eSIlya Dryomov 
26634609245eSIlya Dryomov 	dout("%s req %p tid %llu map_dne_bound %u newest %llu\n", __func__,
26644609245eSIlya Dryomov 	     req, req->r_tid, req->r_map_dne_bound, greq->u.newest);
26654609245eSIlya Dryomov 	if (!req->r_map_dne_bound)
26664609245eSIlya Dryomov 		req->r_map_dne_bound = greq->u.newest;
26674609245eSIlya Dryomov 	erase_request_mc(&osdc->map_checks, req);
26684609245eSIlya Dryomov 	check_pool_dne(req);
26694609245eSIlya Dryomov 
26704609245eSIlya Dryomov 	ceph_osdc_put_request(req);
26714609245eSIlya Dryomov out_unlock:
26724609245eSIlya Dryomov 	up_write(&osdc->lock);
26734609245eSIlya Dryomov }
26744609245eSIlya Dryomov 
26754609245eSIlya Dryomov static void send_map_check(struct ceph_osd_request *req)
26764609245eSIlya Dryomov {
26774609245eSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
26784609245eSIlya Dryomov 	struct ceph_osd_request *lookup_req;
26794609245eSIlya Dryomov 	int ret;
26804609245eSIlya Dryomov 
26814609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
26824609245eSIlya Dryomov 
26834609245eSIlya Dryomov 	lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
26844609245eSIlya Dryomov 	if (lookup_req) {
26854609245eSIlya Dryomov 		WARN_ON(lookup_req != req);
26864609245eSIlya Dryomov 		return;
26874609245eSIlya Dryomov 	}
26884609245eSIlya Dryomov 
26894609245eSIlya Dryomov 	ceph_osdc_get_request(req);
26904609245eSIlya Dryomov 	insert_request_mc(&osdc->map_checks, req);
26914609245eSIlya Dryomov 	ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
26924609245eSIlya Dryomov 					  map_check_cb, req->r_tid);
26934609245eSIlya Dryomov 	WARN_ON(ret);
26944609245eSIlya Dryomov }
26954609245eSIlya Dryomov 
26960bbfdfe8SIlya Dryomov /*
2697922dab61SIlya Dryomov  * lingering requests, watch/notify v2 infrastructure
2698922dab61SIlya Dryomov  */
2699922dab61SIlya Dryomov static void linger_release(struct kref *kref)
2700922dab61SIlya Dryomov {
2701922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq =
2702922dab61SIlya Dryomov 	    container_of(kref, struct ceph_osd_linger_request, kref);
2703922dab61SIlya Dryomov 
2704922dab61SIlya Dryomov 	dout("%s lreq %p reg_req %p ping_req %p\n", __func__, lreq,
2705922dab61SIlya Dryomov 	     lreq->reg_req, lreq->ping_req);
2706922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&lreq->node));
2707922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&lreq->osdc_node));
27084609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&lreq->mc_node));
2709922dab61SIlya Dryomov 	WARN_ON(!list_empty(&lreq->scan_item));
2710b07d3c4bSIlya Dryomov 	WARN_ON(!list_empty(&lreq->pending_lworks));
2711922dab61SIlya Dryomov 	WARN_ON(lreq->osd);
2712922dab61SIlya Dryomov 
271375dbb685SIlya Dryomov 	if (lreq->request_pl)
271475dbb685SIlya Dryomov 		ceph_pagelist_release(lreq->request_pl);
271575dbb685SIlya Dryomov 	if (lreq->notify_id_pages)
271675dbb685SIlya Dryomov 		ceph_release_page_vector(lreq->notify_id_pages, 1);
271775dbb685SIlya Dryomov 
2718922dab61SIlya Dryomov 	ceph_osdc_put_request(lreq->reg_req);
2719922dab61SIlya Dryomov 	ceph_osdc_put_request(lreq->ping_req);
2720922dab61SIlya Dryomov 	target_destroy(&lreq->t);
2721922dab61SIlya Dryomov 	kfree(lreq);
2722922dab61SIlya Dryomov }
2723922dab61SIlya Dryomov 
2724922dab61SIlya Dryomov static void linger_put(struct ceph_osd_linger_request *lreq)
2725922dab61SIlya Dryomov {
2726922dab61SIlya Dryomov 	if (lreq)
2727922dab61SIlya Dryomov 		kref_put(&lreq->kref, linger_release);
2728922dab61SIlya Dryomov }
2729922dab61SIlya Dryomov 
2730922dab61SIlya Dryomov static struct ceph_osd_linger_request *
2731922dab61SIlya Dryomov linger_get(struct ceph_osd_linger_request *lreq)
2732922dab61SIlya Dryomov {
2733922dab61SIlya Dryomov 	kref_get(&lreq->kref);
2734922dab61SIlya Dryomov 	return lreq;
2735922dab61SIlya Dryomov }
2736922dab61SIlya Dryomov 
2737922dab61SIlya Dryomov static struct ceph_osd_linger_request *
2738922dab61SIlya Dryomov linger_alloc(struct ceph_osd_client *osdc)
2739922dab61SIlya Dryomov {
2740922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
2741922dab61SIlya Dryomov 
2742922dab61SIlya Dryomov 	lreq = kzalloc(sizeof(*lreq), GFP_NOIO);
2743922dab61SIlya Dryomov 	if (!lreq)
2744922dab61SIlya Dryomov 		return NULL;
2745922dab61SIlya Dryomov 
2746922dab61SIlya Dryomov 	kref_init(&lreq->kref);
2747922dab61SIlya Dryomov 	mutex_init(&lreq->lock);
2748922dab61SIlya Dryomov 	RB_CLEAR_NODE(&lreq->node);
2749922dab61SIlya Dryomov 	RB_CLEAR_NODE(&lreq->osdc_node);
27504609245eSIlya Dryomov 	RB_CLEAR_NODE(&lreq->mc_node);
2751922dab61SIlya Dryomov 	INIT_LIST_HEAD(&lreq->scan_item);
2752b07d3c4bSIlya Dryomov 	INIT_LIST_HEAD(&lreq->pending_lworks);
2753922dab61SIlya Dryomov 	init_completion(&lreq->reg_commit_wait);
275419079203SIlya Dryomov 	init_completion(&lreq->notify_finish_wait);
2755922dab61SIlya Dryomov 
2756922dab61SIlya Dryomov 	lreq->osdc = osdc;
2757922dab61SIlya Dryomov 	target_init(&lreq->t);
2758922dab61SIlya Dryomov 
2759922dab61SIlya Dryomov 	dout("%s lreq %p\n", __func__, lreq);
2760922dab61SIlya Dryomov 	return lreq;
2761922dab61SIlya Dryomov }
2762922dab61SIlya Dryomov 
2763922dab61SIlya Dryomov DEFINE_RB_INSDEL_FUNCS(linger, struct ceph_osd_linger_request, linger_id, node)
2764922dab61SIlya Dryomov DEFINE_RB_FUNCS(linger_osdc, struct ceph_osd_linger_request, linger_id, osdc_node)
27654609245eSIlya Dryomov DEFINE_RB_FUNCS(linger_mc, struct ceph_osd_linger_request, linger_id, mc_node)
2766922dab61SIlya Dryomov 
2767922dab61SIlya Dryomov /*
2768922dab61SIlya Dryomov  * Create linger request <-> OSD session relation.
2769922dab61SIlya Dryomov  *
2770922dab61SIlya Dryomov  * @lreq has to be registered, @osd may be homeless.
2771922dab61SIlya Dryomov  */
2772922dab61SIlya Dryomov static void link_linger(struct ceph_osd *osd,
2773922dab61SIlya Dryomov 			struct ceph_osd_linger_request *lreq)
2774922dab61SIlya Dryomov {
2775922dab61SIlya Dryomov 	verify_osd_locked(osd);
2776922dab61SIlya Dryomov 	WARN_ON(!lreq->linger_id || lreq->osd);
2777922dab61SIlya Dryomov 	dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2778922dab61SIlya Dryomov 	     osd->o_osd, lreq, lreq->linger_id);
2779922dab61SIlya Dryomov 
2780922dab61SIlya Dryomov 	if (!osd_homeless(osd))
2781922dab61SIlya Dryomov 		__remove_osd_from_lru(osd);
2782922dab61SIlya Dryomov 	else
2783922dab61SIlya Dryomov 		atomic_inc(&osd->o_osdc->num_homeless);
2784922dab61SIlya Dryomov 
2785922dab61SIlya Dryomov 	get_osd(osd);
2786922dab61SIlya Dryomov 	insert_linger(&osd->o_linger_requests, lreq);
2787922dab61SIlya Dryomov 	lreq->osd = osd;
2788922dab61SIlya Dryomov }
2789922dab61SIlya Dryomov 
2790922dab61SIlya Dryomov static void unlink_linger(struct ceph_osd *osd,
2791922dab61SIlya Dryomov 			  struct ceph_osd_linger_request *lreq)
2792922dab61SIlya Dryomov {
2793922dab61SIlya Dryomov 	verify_osd_locked(osd);
2794922dab61SIlya Dryomov 	WARN_ON(lreq->osd != osd);
2795922dab61SIlya Dryomov 	dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2796922dab61SIlya Dryomov 	     osd->o_osd, lreq, lreq->linger_id);
2797922dab61SIlya Dryomov 
2798922dab61SIlya Dryomov 	lreq->osd = NULL;
2799922dab61SIlya Dryomov 	erase_linger(&osd->o_linger_requests, lreq);
2800922dab61SIlya Dryomov 	put_osd(osd);
2801922dab61SIlya Dryomov 
2802922dab61SIlya Dryomov 	if (!osd_homeless(osd))
2803922dab61SIlya Dryomov 		maybe_move_osd_to_lru(osd);
2804922dab61SIlya Dryomov 	else
2805922dab61SIlya Dryomov 		atomic_dec(&osd->o_osdc->num_homeless);
2806922dab61SIlya Dryomov }
2807922dab61SIlya Dryomov 
2808922dab61SIlya Dryomov static bool __linger_registered(struct ceph_osd_linger_request *lreq)
2809922dab61SIlya Dryomov {
2810922dab61SIlya Dryomov 	verify_osdc_locked(lreq->osdc);
2811922dab61SIlya Dryomov 
2812922dab61SIlya Dryomov 	return !RB_EMPTY_NODE(&lreq->osdc_node);
2813922dab61SIlya Dryomov }
2814922dab61SIlya Dryomov 
2815922dab61SIlya Dryomov static bool linger_registered(struct ceph_osd_linger_request *lreq)
2816922dab61SIlya Dryomov {
2817922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2818922dab61SIlya Dryomov 	bool registered;
2819922dab61SIlya Dryomov 
2820922dab61SIlya Dryomov 	down_read(&osdc->lock);
2821922dab61SIlya Dryomov 	registered = __linger_registered(lreq);
2822922dab61SIlya Dryomov 	up_read(&osdc->lock);
2823922dab61SIlya Dryomov 
2824922dab61SIlya Dryomov 	return registered;
2825922dab61SIlya Dryomov }
2826922dab61SIlya Dryomov 
2827922dab61SIlya Dryomov static void linger_register(struct ceph_osd_linger_request *lreq)
2828922dab61SIlya Dryomov {
2829922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2830922dab61SIlya Dryomov 
2831922dab61SIlya Dryomov 	verify_osdc_wrlocked(osdc);
2832922dab61SIlya Dryomov 	WARN_ON(lreq->linger_id);
2833922dab61SIlya Dryomov 
2834922dab61SIlya Dryomov 	linger_get(lreq);
2835922dab61SIlya Dryomov 	lreq->linger_id = ++osdc->last_linger_id;
2836922dab61SIlya Dryomov 	insert_linger_osdc(&osdc->linger_requests, lreq);
2837922dab61SIlya Dryomov }
2838922dab61SIlya Dryomov 
2839922dab61SIlya Dryomov static void linger_unregister(struct ceph_osd_linger_request *lreq)
2840922dab61SIlya Dryomov {
2841922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2842922dab61SIlya Dryomov 
2843922dab61SIlya Dryomov 	verify_osdc_wrlocked(osdc);
2844922dab61SIlya Dryomov 
2845922dab61SIlya Dryomov 	erase_linger_osdc(&osdc->linger_requests, lreq);
2846922dab61SIlya Dryomov 	linger_put(lreq);
2847922dab61SIlya Dryomov }
2848922dab61SIlya Dryomov 
2849922dab61SIlya Dryomov static void cancel_linger_request(struct ceph_osd_request *req)
2850922dab61SIlya Dryomov {
2851922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
2852922dab61SIlya Dryomov 
2853922dab61SIlya Dryomov 	WARN_ON(!req->r_linger);
2854922dab61SIlya Dryomov 	cancel_request(req);
2855922dab61SIlya Dryomov 	linger_put(lreq);
2856922dab61SIlya Dryomov }
2857922dab61SIlya Dryomov 
2858922dab61SIlya Dryomov struct linger_work {
2859922dab61SIlya Dryomov 	struct work_struct work;
2860922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
2861b07d3c4bSIlya Dryomov 	struct list_head pending_item;
2862b07d3c4bSIlya Dryomov 	unsigned long queued_stamp;
2863922dab61SIlya Dryomov 
2864922dab61SIlya Dryomov 	union {
2865922dab61SIlya Dryomov 		struct {
2866922dab61SIlya Dryomov 			u64 notify_id;
2867922dab61SIlya Dryomov 			u64 notifier_id;
2868922dab61SIlya Dryomov 			void *payload; /* points into @msg front */
2869922dab61SIlya Dryomov 			size_t payload_len;
2870922dab61SIlya Dryomov 
2871922dab61SIlya Dryomov 			struct ceph_msg *msg; /* for ceph_msg_put() */
2872922dab61SIlya Dryomov 		} notify;
2873922dab61SIlya Dryomov 		struct {
2874922dab61SIlya Dryomov 			int err;
2875922dab61SIlya Dryomov 		} error;
2876922dab61SIlya Dryomov 	};
2877922dab61SIlya Dryomov };
2878922dab61SIlya Dryomov 
2879922dab61SIlya Dryomov static struct linger_work *lwork_alloc(struct ceph_osd_linger_request *lreq,
2880922dab61SIlya Dryomov 				       work_func_t workfn)
2881922dab61SIlya Dryomov {
2882922dab61SIlya Dryomov 	struct linger_work *lwork;
2883922dab61SIlya Dryomov 
2884922dab61SIlya Dryomov 	lwork = kzalloc(sizeof(*lwork), GFP_NOIO);
2885922dab61SIlya Dryomov 	if (!lwork)
2886922dab61SIlya Dryomov 		return NULL;
2887922dab61SIlya Dryomov 
2888922dab61SIlya Dryomov 	INIT_WORK(&lwork->work, workfn);
2889b07d3c4bSIlya Dryomov 	INIT_LIST_HEAD(&lwork->pending_item);
2890922dab61SIlya Dryomov 	lwork->lreq = linger_get(lreq);
2891922dab61SIlya Dryomov 
2892922dab61SIlya Dryomov 	return lwork;
2893922dab61SIlya Dryomov }
2894922dab61SIlya Dryomov 
2895922dab61SIlya Dryomov static void lwork_free(struct linger_work *lwork)
2896922dab61SIlya Dryomov {
2897922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2898922dab61SIlya Dryomov 
2899b07d3c4bSIlya Dryomov 	mutex_lock(&lreq->lock);
2900b07d3c4bSIlya Dryomov 	list_del(&lwork->pending_item);
2901b07d3c4bSIlya Dryomov 	mutex_unlock(&lreq->lock);
2902b07d3c4bSIlya Dryomov 
2903922dab61SIlya Dryomov 	linger_put(lreq);
2904922dab61SIlya Dryomov 	kfree(lwork);
2905922dab61SIlya Dryomov }
2906922dab61SIlya Dryomov 
2907922dab61SIlya Dryomov static void lwork_queue(struct linger_work *lwork)
2908922dab61SIlya Dryomov {
2909922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2910922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2911922dab61SIlya Dryomov 
2912922dab61SIlya Dryomov 	verify_lreq_locked(lreq);
2913b07d3c4bSIlya Dryomov 	WARN_ON(!list_empty(&lwork->pending_item));
2914b07d3c4bSIlya Dryomov 
2915b07d3c4bSIlya Dryomov 	lwork->queued_stamp = jiffies;
2916b07d3c4bSIlya Dryomov 	list_add_tail(&lwork->pending_item, &lreq->pending_lworks);
2917922dab61SIlya Dryomov 	queue_work(osdc->notify_wq, &lwork->work);
2918922dab61SIlya Dryomov }
2919922dab61SIlya Dryomov 
2920922dab61SIlya Dryomov static void do_watch_notify(struct work_struct *w)
2921922dab61SIlya Dryomov {
2922922dab61SIlya Dryomov 	struct linger_work *lwork = container_of(w, struct linger_work, work);
2923922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2924922dab61SIlya Dryomov 
2925922dab61SIlya Dryomov 	if (!linger_registered(lreq)) {
2926922dab61SIlya Dryomov 		dout("%s lreq %p not registered\n", __func__, lreq);
2927922dab61SIlya Dryomov 		goto out;
2928922dab61SIlya Dryomov 	}
2929922dab61SIlya Dryomov 
293019079203SIlya Dryomov 	WARN_ON(!lreq->is_watch);
2931922dab61SIlya Dryomov 	dout("%s lreq %p notify_id %llu notifier_id %llu payload_len %zu\n",
2932922dab61SIlya Dryomov 	     __func__, lreq, lwork->notify.notify_id, lwork->notify.notifier_id,
2933922dab61SIlya Dryomov 	     lwork->notify.payload_len);
2934922dab61SIlya Dryomov 	lreq->wcb(lreq->data, lwork->notify.notify_id, lreq->linger_id,
2935922dab61SIlya Dryomov 		  lwork->notify.notifier_id, lwork->notify.payload,
2936922dab61SIlya Dryomov 		  lwork->notify.payload_len);
2937922dab61SIlya Dryomov 
2938922dab61SIlya Dryomov out:
2939922dab61SIlya Dryomov 	ceph_msg_put(lwork->notify.msg);
2940922dab61SIlya Dryomov 	lwork_free(lwork);
2941922dab61SIlya Dryomov }
2942922dab61SIlya Dryomov 
2943922dab61SIlya Dryomov static void do_watch_error(struct work_struct *w)
2944922dab61SIlya Dryomov {
2945922dab61SIlya Dryomov 	struct linger_work *lwork = container_of(w, struct linger_work, work);
2946922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2947922dab61SIlya Dryomov 
2948922dab61SIlya Dryomov 	if (!linger_registered(lreq)) {
2949922dab61SIlya Dryomov 		dout("%s lreq %p not registered\n", __func__, lreq);
2950922dab61SIlya Dryomov 		goto out;
2951922dab61SIlya Dryomov 	}
2952922dab61SIlya Dryomov 
2953922dab61SIlya Dryomov 	dout("%s lreq %p err %d\n", __func__, lreq, lwork->error.err);
2954922dab61SIlya Dryomov 	lreq->errcb(lreq->data, lreq->linger_id, lwork->error.err);
2955922dab61SIlya Dryomov 
2956922dab61SIlya Dryomov out:
2957922dab61SIlya Dryomov 	lwork_free(lwork);
2958922dab61SIlya Dryomov }
2959922dab61SIlya Dryomov 
2960922dab61SIlya Dryomov static void queue_watch_error(struct ceph_osd_linger_request *lreq)
2961922dab61SIlya Dryomov {
2962922dab61SIlya Dryomov 	struct linger_work *lwork;
2963922dab61SIlya Dryomov 
2964922dab61SIlya Dryomov 	lwork = lwork_alloc(lreq, do_watch_error);
2965922dab61SIlya Dryomov 	if (!lwork) {
2966922dab61SIlya Dryomov 		pr_err("failed to allocate error-lwork\n");
2967922dab61SIlya Dryomov 		return;
2968922dab61SIlya Dryomov 	}
2969922dab61SIlya Dryomov 
2970922dab61SIlya Dryomov 	lwork->error.err = lreq->last_error;
2971922dab61SIlya Dryomov 	lwork_queue(lwork);
2972922dab61SIlya Dryomov }
2973922dab61SIlya Dryomov 
2974922dab61SIlya Dryomov static void linger_reg_commit_complete(struct ceph_osd_linger_request *lreq,
2975922dab61SIlya Dryomov 				       int result)
2976922dab61SIlya Dryomov {
2977922dab61SIlya Dryomov 	if (!completion_done(&lreq->reg_commit_wait)) {
2978922dab61SIlya Dryomov 		lreq->reg_commit_error = (result <= 0 ? result : 0);
2979922dab61SIlya Dryomov 		complete_all(&lreq->reg_commit_wait);
2980922dab61SIlya Dryomov 	}
2981922dab61SIlya Dryomov }
2982922dab61SIlya Dryomov 
2983922dab61SIlya Dryomov static void linger_commit_cb(struct ceph_osd_request *req)
2984922dab61SIlya Dryomov {
2985922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
2986922dab61SIlya Dryomov 
2987922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
298875dbb685SIlya Dryomov 	if (req != lreq->reg_req) {
298975dbb685SIlya Dryomov 		dout("%s lreq %p linger_id %llu unknown req (%p != %p)\n",
299075dbb685SIlya Dryomov 		     __func__, lreq, lreq->linger_id, req, lreq->reg_req);
299175dbb685SIlya Dryomov 		goto out;
299275dbb685SIlya Dryomov 	}
299375dbb685SIlya Dryomov 
2994922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu result %d\n", __func__, lreq,
2995922dab61SIlya Dryomov 	     lreq->linger_id, req->r_result);
2996922dab61SIlya Dryomov 	linger_reg_commit_complete(lreq, req->r_result);
2997922dab61SIlya Dryomov 	lreq->committed = true;
2998922dab61SIlya Dryomov 
299919079203SIlya Dryomov 	if (!lreq->is_watch) {
300019079203SIlya Dryomov 		struct ceph_osd_data *osd_data =
300119079203SIlya Dryomov 		    osd_req_op_data(req, 0, notify, response_data);
300219079203SIlya Dryomov 		void *p = page_address(osd_data->pages[0]);
300319079203SIlya Dryomov 
300419079203SIlya Dryomov 		WARN_ON(req->r_ops[0].op != CEPH_OSD_OP_NOTIFY ||
300519079203SIlya Dryomov 			osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
300619079203SIlya Dryomov 
300719079203SIlya Dryomov 		/* make note of the notify_id */
300819079203SIlya Dryomov 		if (req->r_ops[0].outdata_len >= sizeof(u64)) {
300919079203SIlya Dryomov 			lreq->notify_id = ceph_decode_64(&p);
301019079203SIlya Dryomov 			dout("lreq %p notify_id %llu\n", lreq,
301119079203SIlya Dryomov 			     lreq->notify_id);
301219079203SIlya Dryomov 		} else {
301319079203SIlya Dryomov 			dout("lreq %p no notify_id\n", lreq);
301419079203SIlya Dryomov 		}
301519079203SIlya Dryomov 	}
301619079203SIlya Dryomov 
301775dbb685SIlya Dryomov out:
3018922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
3019922dab61SIlya Dryomov 	linger_put(lreq);
3020922dab61SIlya Dryomov }
3021922dab61SIlya Dryomov 
3022922dab61SIlya Dryomov static int normalize_watch_error(int err)
3023922dab61SIlya Dryomov {
3024922dab61SIlya Dryomov 	/*
3025922dab61SIlya Dryomov 	 * Translate ENOENT -> ENOTCONN so that a delete->disconnection
3026922dab61SIlya Dryomov 	 * notification and a failure to reconnect because we raced with
3027922dab61SIlya Dryomov 	 * the delete appear the same to the user.
3028922dab61SIlya Dryomov 	 */
3029922dab61SIlya Dryomov 	if (err == -ENOENT)
3030922dab61SIlya Dryomov 		err = -ENOTCONN;
3031922dab61SIlya Dryomov 
3032922dab61SIlya Dryomov 	return err;
3033922dab61SIlya Dryomov }
3034922dab61SIlya Dryomov 
3035922dab61SIlya Dryomov static void linger_reconnect_cb(struct ceph_osd_request *req)
3036922dab61SIlya Dryomov {
3037922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
3038922dab61SIlya Dryomov 
3039922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
304075dbb685SIlya Dryomov 	if (req != lreq->reg_req) {
304175dbb685SIlya Dryomov 		dout("%s lreq %p linger_id %llu unknown req (%p != %p)\n",
304275dbb685SIlya Dryomov 		     __func__, lreq, lreq->linger_id, req, lreq->reg_req);
304375dbb685SIlya Dryomov 		goto out;
304475dbb685SIlya Dryomov 	}
304575dbb685SIlya Dryomov 
3046922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu result %d last_error %d\n", __func__,
3047922dab61SIlya Dryomov 	     lreq, lreq->linger_id, req->r_result, lreq->last_error);
3048922dab61SIlya Dryomov 	if (req->r_result < 0) {
3049922dab61SIlya Dryomov 		if (!lreq->last_error) {
3050922dab61SIlya Dryomov 			lreq->last_error = normalize_watch_error(req->r_result);
3051922dab61SIlya Dryomov 			queue_watch_error(lreq);
3052922dab61SIlya Dryomov 		}
3053922dab61SIlya Dryomov 	}
3054922dab61SIlya Dryomov 
305575dbb685SIlya Dryomov out:
3056922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
3057922dab61SIlya Dryomov 	linger_put(lreq);
3058922dab61SIlya Dryomov }
3059922dab61SIlya Dryomov 
3060922dab61SIlya Dryomov static void send_linger(struct ceph_osd_linger_request *lreq)
3061922dab61SIlya Dryomov {
306275dbb685SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
306375dbb685SIlya Dryomov 	struct ceph_osd_request *req;
306475dbb685SIlya Dryomov 	int ret;
3065922dab61SIlya Dryomov 
306675dbb685SIlya Dryomov 	verify_osdc_wrlocked(osdc);
306775dbb685SIlya Dryomov 	mutex_lock(&lreq->lock);
3068922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3069922dab61SIlya Dryomov 
307075dbb685SIlya Dryomov 	if (lreq->reg_req) {
307175dbb685SIlya Dryomov 		if (lreq->reg_req->r_osd)
307275dbb685SIlya Dryomov 			cancel_linger_request(lreq->reg_req);
307375dbb685SIlya Dryomov 		ceph_osdc_put_request(lreq->reg_req);
307475dbb685SIlya Dryomov 	}
3075922dab61SIlya Dryomov 
307675dbb685SIlya Dryomov 	req = ceph_osdc_alloc_request(osdc, NULL, 1, true, GFP_NOIO);
307775dbb685SIlya Dryomov 	BUG_ON(!req);
307875dbb685SIlya Dryomov 
30795133ba8fSIlya Dryomov 	target_copy(&req->r_t, &lreq->t);
3080922dab61SIlya Dryomov 	req->r_mtime = lreq->mtime;
3081922dab61SIlya Dryomov 
308219079203SIlya Dryomov 	if (lreq->is_watch && lreq->committed) {
308375dbb685SIlya Dryomov 		osd_req_op_watch_init(req, 0, CEPH_OSD_WATCH_OP_RECONNECT,
308475dbb685SIlya Dryomov 				      lreq->linger_id, ++lreq->register_gen);
3085922dab61SIlya Dryomov 		dout("lreq %p reconnect register_gen %u\n", lreq,
308675dbb685SIlya Dryomov 		     req->r_ops[0].watch.gen);
3087922dab61SIlya Dryomov 		req->r_callback = linger_reconnect_cb;
3088922dab61SIlya Dryomov 	} else {
308975dbb685SIlya Dryomov 		if (lreq->is_watch) {
309075dbb685SIlya Dryomov 			osd_req_op_watch_init(req, 0, CEPH_OSD_WATCH_OP_WATCH,
309175dbb685SIlya Dryomov 					      lreq->linger_id, 0);
309275dbb685SIlya Dryomov 		} else {
309319079203SIlya Dryomov 			lreq->notify_id = 0;
309475dbb685SIlya Dryomov 
309575dbb685SIlya Dryomov 			refcount_inc(&lreq->request_pl->refcnt);
309675dbb685SIlya Dryomov 			osd_req_op_notify_init(req, 0, lreq->linger_id,
309775dbb685SIlya Dryomov 					       lreq->request_pl);
309875dbb685SIlya Dryomov 			ceph_osd_data_pages_init(
309975dbb685SIlya Dryomov 			    osd_req_op_data(req, 0, notify, response_data),
310075dbb685SIlya Dryomov 			    lreq->notify_id_pages, PAGE_SIZE, 0, false, false);
310175dbb685SIlya Dryomov 		}
3102922dab61SIlya Dryomov 		dout("lreq %p register\n", lreq);
3103922dab61SIlya Dryomov 		req->r_callback = linger_commit_cb;
3104922dab61SIlya Dryomov 	}
310575dbb685SIlya Dryomov 
310675dbb685SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
310775dbb685SIlya Dryomov 	BUG_ON(ret);
3108922dab61SIlya Dryomov 
3109922dab61SIlya Dryomov 	req->r_priv = linger_get(lreq);
3110922dab61SIlya Dryomov 	req->r_linger = true;
311175dbb685SIlya Dryomov 	lreq->reg_req = req;
311275dbb685SIlya Dryomov 	mutex_unlock(&lreq->lock);
3113922dab61SIlya Dryomov 
3114922dab61SIlya Dryomov 	submit_request(req, true);
3115922dab61SIlya Dryomov }
3116922dab61SIlya Dryomov 
3117922dab61SIlya Dryomov static void linger_ping_cb(struct ceph_osd_request *req)
3118922dab61SIlya Dryomov {
3119922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
3120922dab61SIlya Dryomov 
3121922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
312275dbb685SIlya Dryomov 	if (req != lreq->ping_req) {
312375dbb685SIlya Dryomov 		dout("%s lreq %p linger_id %llu unknown req (%p != %p)\n",
312475dbb685SIlya Dryomov 		     __func__, lreq, lreq->linger_id, req, lreq->ping_req);
312575dbb685SIlya Dryomov 		goto out;
312675dbb685SIlya Dryomov 	}
312775dbb685SIlya Dryomov 
3128922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu result %d ping_sent %lu last_error %d\n",
3129922dab61SIlya Dryomov 	     __func__, lreq, lreq->linger_id, req->r_result, lreq->ping_sent,
3130922dab61SIlya Dryomov 	     lreq->last_error);
3131922dab61SIlya Dryomov 	if (lreq->register_gen == req->r_ops[0].watch.gen) {
3132b07d3c4bSIlya Dryomov 		if (!req->r_result) {
3133b07d3c4bSIlya Dryomov 			lreq->watch_valid_thru = lreq->ping_sent;
3134b07d3c4bSIlya Dryomov 		} else if (!lreq->last_error) {
3135922dab61SIlya Dryomov 			lreq->last_error = normalize_watch_error(req->r_result);
3136922dab61SIlya Dryomov 			queue_watch_error(lreq);
3137922dab61SIlya Dryomov 		}
3138922dab61SIlya Dryomov 	} else {
3139922dab61SIlya Dryomov 		dout("lreq %p register_gen %u ignoring old pong %u\n", lreq,
3140922dab61SIlya Dryomov 		     lreq->register_gen, req->r_ops[0].watch.gen);
3141922dab61SIlya Dryomov 	}
3142922dab61SIlya Dryomov 
314375dbb685SIlya Dryomov out:
3144922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
3145922dab61SIlya Dryomov 	linger_put(lreq);
3146922dab61SIlya Dryomov }
3147922dab61SIlya Dryomov 
3148922dab61SIlya Dryomov static void send_linger_ping(struct ceph_osd_linger_request *lreq)
3149922dab61SIlya Dryomov {
3150922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
315175dbb685SIlya Dryomov 	struct ceph_osd_request *req;
315275dbb685SIlya Dryomov 	int ret;
3153922dab61SIlya Dryomov 
3154b7ec35b3SIlya Dryomov 	if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
3155922dab61SIlya Dryomov 		dout("%s PAUSERD\n", __func__);
3156922dab61SIlya Dryomov 		return;
3157922dab61SIlya Dryomov 	}
3158922dab61SIlya Dryomov 
3159922dab61SIlya Dryomov 	lreq->ping_sent = jiffies;
3160922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu ping_sent %lu register_gen %u\n",
3161922dab61SIlya Dryomov 	     __func__, lreq, lreq->linger_id, lreq->ping_sent,
3162922dab61SIlya Dryomov 	     lreq->register_gen);
3163922dab61SIlya Dryomov 
316475dbb685SIlya Dryomov 	if (lreq->ping_req) {
316575dbb685SIlya Dryomov 		if (lreq->ping_req->r_osd)
316675dbb685SIlya Dryomov 			cancel_linger_request(lreq->ping_req);
316775dbb685SIlya Dryomov 		ceph_osdc_put_request(lreq->ping_req);
316875dbb685SIlya Dryomov 	}
3169922dab61SIlya Dryomov 
317075dbb685SIlya Dryomov 	req = ceph_osdc_alloc_request(osdc, NULL, 1, true, GFP_NOIO);
317175dbb685SIlya Dryomov 	BUG_ON(!req);
317275dbb685SIlya Dryomov 
3173922dab61SIlya Dryomov 	target_copy(&req->r_t, &lreq->t);
317475dbb685SIlya Dryomov 	osd_req_op_watch_init(req, 0, CEPH_OSD_WATCH_OP_PING, lreq->linger_id,
317575dbb685SIlya Dryomov 			      lreq->register_gen);
3176922dab61SIlya Dryomov 	req->r_callback = linger_ping_cb;
317775dbb685SIlya Dryomov 
317875dbb685SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
317975dbb685SIlya Dryomov 	BUG_ON(ret);
318075dbb685SIlya Dryomov 
3181922dab61SIlya Dryomov 	req->r_priv = linger_get(lreq);
3182922dab61SIlya Dryomov 	req->r_linger = true;
318375dbb685SIlya Dryomov 	lreq->ping_req = req;
3184922dab61SIlya Dryomov 
3185922dab61SIlya Dryomov 	ceph_osdc_get_request(req);
3186922dab61SIlya Dryomov 	account_request(req);
3187922dab61SIlya Dryomov 	req->r_tid = atomic64_inc_return(&osdc->last_tid);
3188922dab61SIlya Dryomov 	link_request(lreq->osd, req);
3189922dab61SIlya Dryomov 	send_request(req);
3190922dab61SIlya Dryomov }
3191922dab61SIlya Dryomov 
3192922dab61SIlya Dryomov static void linger_submit(struct ceph_osd_linger_request *lreq)
3193922dab61SIlya Dryomov {
3194922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
3195922dab61SIlya Dryomov 	struct ceph_osd *osd;
3196922dab61SIlya Dryomov 
319781c65213SIlya Dryomov 	down_write(&osdc->lock);
319881c65213SIlya Dryomov 	linger_register(lreq);
319981c65213SIlya Dryomov 
32008edf84baSIlya Dryomov 	calc_target(osdc, &lreq->t, false);
3201922dab61SIlya Dryomov 	osd = lookup_create_osd(osdc, lreq->t.osd, true);
3202922dab61SIlya Dryomov 	link_linger(osd, lreq);
3203922dab61SIlya Dryomov 
3204922dab61SIlya Dryomov 	send_linger(lreq);
320581c65213SIlya Dryomov 	up_write(&osdc->lock);
3206922dab61SIlya Dryomov }
3207922dab61SIlya Dryomov 
32084609245eSIlya Dryomov static void cancel_linger_map_check(struct ceph_osd_linger_request *lreq)
32094609245eSIlya Dryomov {
32104609245eSIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
32114609245eSIlya Dryomov 	struct ceph_osd_linger_request *lookup_lreq;
32124609245eSIlya Dryomov 
32134609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
32144609245eSIlya Dryomov 
32154609245eSIlya Dryomov 	lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
32164609245eSIlya Dryomov 				       lreq->linger_id);
32174609245eSIlya Dryomov 	if (!lookup_lreq)
32184609245eSIlya Dryomov 		return;
32194609245eSIlya Dryomov 
32204609245eSIlya Dryomov 	WARN_ON(lookup_lreq != lreq);
32214609245eSIlya Dryomov 	erase_linger_mc(&osdc->linger_map_checks, lreq);
32224609245eSIlya Dryomov 	linger_put(lreq);
32234609245eSIlya Dryomov }
32244609245eSIlya Dryomov 
3225922dab61SIlya Dryomov /*
3226922dab61SIlya Dryomov  * @lreq has to be both registered and linked.
3227922dab61SIlya Dryomov  */
3228922dab61SIlya Dryomov static void __linger_cancel(struct ceph_osd_linger_request *lreq)
3229922dab61SIlya Dryomov {
323075dbb685SIlya Dryomov 	if (lreq->ping_req && lreq->ping_req->r_osd)
3231922dab61SIlya Dryomov 		cancel_linger_request(lreq->ping_req);
323275dbb685SIlya Dryomov 	if (lreq->reg_req && lreq->reg_req->r_osd)
3233922dab61SIlya Dryomov 		cancel_linger_request(lreq->reg_req);
32344609245eSIlya Dryomov 	cancel_linger_map_check(lreq);
3235922dab61SIlya Dryomov 	unlink_linger(lreq->osd, lreq);
3236922dab61SIlya Dryomov 	linger_unregister(lreq);
3237922dab61SIlya Dryomov }
3238922dab61SIlya Dryomov 
3239922dab61SIlya Dryomov static void linger_cancel(struct ceph_osd_linger_request *lreq)
3240922dab61SIlya Dryomov {
3241922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
3242922dab61SIlya Dryomov 
3243922dab61SIlya Dryomov 	down_write(&osdc->lock);
3244922dab61SIlya Dryomov 	if (__linger_registered(lreq))
3245922dab61SIlya Dryomov 		__linger_cancel(lreq);
3246922dab61SIlya Dryomov 	up_write(&osdc->lock);
3247922dab61SIlya Dryomov }
3248922dab61SIlya Dryomov 
32494609245eSIlya Dryomov static void send_linger_map_check(struct ceph_osd_linger_request *lreq);
32504609245eSIlya Dryomov 
32514609245eSIlya Dryomov static void check_linger_pool_dne(struct ceph_osd_linger_request *lreq)
32524609245eSIlya Dryomov {
32534609245eSIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
32544609245eSIlya Dryomov 	struct ceph_osdmap *map = osdc->osdmap;
32554609245eSIlya Dryomov 
32564609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
32574609245eSIlya Dryomov 	WARN_ON(!map->epoch);
32584609245eSIlya Dryomov 
32594609245eSIlya Dryomov 	if (lreq->register_gen) {
32604609245eSIlya Dryomov 		lreq->map_dne_bound = map->epoch;
32614609245eSIlya Dryomov 		dout("%s lreq %p linger_id %llu pool disappeared\n", __func__,
32624609245eSIlya Dryomov 		     lreq, lreq->linger_id);
32634609245eSIlya Dryomov 	} else {
32644609245eSIlya Dryomov 		dout("%s lreq %p linger_id %llu map_dne_bound %u have %u\n",
32654609245eSIlya Dryomov 		     __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
32664609245eSIlya Dryomov 		     map->epoch);
32674609245eSIlya Dryomov 	}
32684609245eSIlya Dryomov 
32694609245eSIlya Dryomov 	if (lreq->map_dne_bound) {
32704609245eSIlya Dryomov 		if (map->epoch >= lreq->map_dne_bound) {
32714609245eSIlya Dryomov 			/* we had a new enough map */
32724609245eSIlya Dryomov 			pr_info("linger_id %llu pool does not exist\n",
32734609245eSIlya Dryomov 				lreq->linger_id);
32744609245eSIlya Dryomov 			linger_reg_commit_complete(lreq, -ENOENT);
32754609245eSIlya Dryomov 			__linger_cancel(lreq);
32764609245eSIlya Dryomov 		}
32774609245eSIlya Dryomov 	} else {
32784609245eSIlya Dryomov 		send_linger_map_check(lreq);
32794609245eSIlya Dryomov 	}
32804609245eSIlya Dryomov }
32814609245eSIlya Dryomov 
32824609245eSIlya Dryomov static void linger_map_check_cb(struct ceph_mon_generic_request *greq)
32834609245eSIlya Dryomov {
32844609245eSIlya Dryomov 	struct ceph_osd_client *osdc = &greq->monc->client->osdc;
32854609245eSIlya Dryomov 	struct ceph_osd_linger_request *lreq;
32864609245eSIlya Dryomov 	u64 linger_id = greq->private_data;
32874609245eSIlya Dryomov 
32884609245eSIlya Dryomov 	WARN_ON(greq->result || !greq->u.newest);
32894609245eSIlya Dryomov 
32904609245eSIlya Dryomov 	down_write(&osdc->lock);
32914609245eSIlya Dryomov 	lreq = lookup_linger_mc(&osdc->linger_map_checks, linger_id);
32924609245eSIlya Dryomov 	if (!lreq) {
32934609245eSIlya Dryomov 		dout("%s linger_id %llu dne\n", __func__, linger_id);
32944609245eSIlya Dryomov 		goto out_unlock;
32954609245eSIlya Dryomov 	}
32964609245eSIlya Dryomov 
32974609245eSIlya Dryomov 	dout("%s lreq %p linger_id %llu map_dne_bound %u newest %llu\n",
32984609245eSIlya Dryomov 	     __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
32994609245eSIlya Dryomov 	     greq->u.newest);
33004609245eSIlya Dryomov 	if (!lreq->map_dne_bound)
33014609245eSIlya Dryomov 		lreq->map_dne_bound = greq->u.newest;
33024609245eSIlya Dryomov 	erase_linger_mc(&osdc->linger_map_checks, lreq);
33034609245eSIlya Dryomov 	check_linger_pool_dne(lreq);
33044609245eSIlya Dryomov 
33054609245eSIlya Dryomov 	linger_put(lreq);
33064609245eSIlya Dryomov out_unlock:
33074609245eSIlya Dryomov 	up_write(&osdc->lock);
33084609245eSIlya Dryomov }
33094609245eSIlya Dryomov 
33104609245eSIlya Dryomov static void send_linger_map_check(struct ceph_osd_linger_request *lreq)
33114609245eSIlya Dryomov {
33124609245eSIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
33134609245eSIlya Dryomov 	struct ceph_osd_linger_request *lookup_lreq;
33144609245eSIlya Dryomov 	int ret;
33154609245eSIlya Dryomov 
33164609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
33174609245eSIlya Dryomov 
33184609245eSIlya Dryomov 	lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
33194609245eSIlya Dryomov 				       lreq->linger_id);
33204609245eSIlya Dryomov 	if (lookup_lreq) {
33214609245eSIlya Dryomov 		WARN_ON(lookup_lreq != lreq);
33224609245eSIlya Dryomov 		return;
33234609245eSIlya Dryomov 	}
33244609245eSIlya Dryomov 
33254609245eSIlya Dryomov 	linger_get(lreq);
33264609245eSIlya Dryomov 	insert_linger_mc(&osdc->linger_map_checks, lreq);
33274609245eSIlya Dryomov 	ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
33284609245eSIlya Dryomov 					  linger_map_check_cb, lreq->linger_id);
33294609245eSIlya Dryomov 	WARN_ON(ret);
33304609245eSIlya Dryomov }
33314609245eSIlya Dryomov 
3332922dab61SIlya Dryomov static int linger_reg_commit_wait(struct ceph_osd_linger_request *lreq)
3333922dab61SIlya Dryomov {
3334922dab61SIlya Dryomov 	int ret;
3335922dab61SIlya Dryomov 
3336922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3337*e6e28432SIlya Dryomov 	ret = wait_for_completion_killable(&lreq->reg_commit_wait);
3338922dab61SIlya Dryomov 	return ret ?: lreq->reg_commit_error;
3339922dab61SIlya Dryomov }
3340922dab61SIlya Dryomov 
3341*e6e28432SIlya Dryomov static int linger_notify_finish_wait(struct ceph_osd_linger_request *lreq,
3342*e6e28432SIlya Dryomov 				     unsigned long timeout)
334319079203SIlya Dryomov {
3344*e6e28432SIlya Dryomov 	long left;
334519079203SIlya Dryomov 
334619079203SIlya Dryomov 	dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3347*e6e28432SIlya Dryomov 	left = wait_for_completion_killable_timeout(&lreq->notify_finish_wait,
3348*e6e28432SIlya Dryomov 						ceph_timeout_jiffies(timeout));
3349*e6e28432SIlya Dryomov 	if (left <= 0)
3350*e6e28432SIlya Dryomov 		left = left ?: -ETIMEDOUT;
3351*e6e28432SIlya Dryomov 	else
3352*e6e28432SIlya Dryomov 		left = lreq->notify_finish_error; /* completed */
3353*e6e28432SIlya Dryomov 
3354*e6e28432SIlya Dryomov 	return left;
335519079203SIlya Dryomov }
335619079203SIlya Dryomov 
3357922dab61SIlya Dryomov /*
3358fbca9635SIlya Dryomov  * Timeout callback, called every N seconds.  When 1 or more OSD
3359fbca9635SIlya Dryomov  * requests has been active for more than N seconds, we send a keepalive
3360fbca9635SIlya Dryomov  * (tag + timestamp) to its OSD to ensure any communications channel
3361fbca9635SIlya Dryomov  * reset is detected.
33623d14c5d2SYehuda Sadeh  */
33633d14c5d2SYehuda Sadeh static void handle_timeout(struct work_struct *work)
33643d14c5d2SYehuda Sadeh {
33653d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc =
33663d14c5d2SYehuda Sadeh 		container_of(work, struct ceph_osd_client, timeout_work.work);
3367a319bf56SIlya Dryomov 	struct ceph_options *opts = osdc->client->options;
33685aea3dcdSIlya Dryomov 	unsigned long cutoff = jiffies - opts->osd_keepalive_timeout;
33697cc5e38fSIlya Dryomov 	unsigned long expiry_cutoff = jiffies - opts->osd_request_timeout;
33705aea3dcdSIlya Dryomov 	LIST_HEAD(slow_osds);
33715aea3dcdSIlya Dryomov 	struct rb_node *n, *p;
33723d14c5d2SYehuda Sadeh 
33735aea3dcdSIlya Dryomov 	dout("%s osdc %p\n", __func__, osdc);
33745aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
33753d14c5d2SYehuda Sadeh 
33763d14c5d2SYehuda Sadeh 	/*
33773d14c5d2SYehuda Sadeh 	 * ping osds that are a bit slow.  this ensures that if there
33783d14c5d2SYehuda Sadeh 	 * is a break in the TCP connection we will notice, and reopen
33793d14c5d2SYehuda Sadeh 	 * a connection with that osd (from the fault callback).
33803d14c5d2SYehuda Sadeh 	 */
33815aea3dcdSIlya Dryomov 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
33825aea3dcdSIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
33835aea3dcdSIlya Dryomov 		bool found = false;
33843d14c5d2SYehuda Sadeh 
33857cc5e38fSIlya Dryomov 		for (p = rb_first(&osd->o_requests); p; ) {
33865aea3dcdSIlya Dryomov 			struct ceph_osd_request *req =
33875aea3dcdSIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
33885aea3dcdSIlya Dryomov 
33897cc5e38fSIlya Dryomov 			p = rb_next(p); /* abort_request() */
33907cc5e38fSIlya Dryomov 
33915aea3dcdSIlya Dryomov 			if (time_before(req->r_stamp, cutoff)) {
33925aea3dcdSIlya Dryomov 				dout(" req %p tid %llu on osd%d is laggy\n",
33935aea3dcdSIlya Dryomov 				     req, req->r_tid, osd->o_osd);
33945aea3dcdSIlya Dryomov 				found = true;
33955aea3dcdSIlya Dryomov 			}
33967cc5e38fSIlya Dryomov 			if (opts->osd_request_timeout &&
33977cc5e38fSIlya Dryomov 			    time_before(req->r_start_stamp, expiry_cutoff)) {
33987cc5e38fSIlya Dryomov 				pr_err_ratelimited("tid %llu on osd%d timeout\n",
33997cc5e38fSIlya Dryomov 				       req->r_tid, osd->o_osd);
34007cc5e38fSIlya Dryomov 				abort_request(req, -ETIMEDOUT);
34017cc5e38fSIlya Dryomov 			}
34025aea3dcdSIlya Dryomov 		}
3403922dab61SIlya Dryomov 		for (p = rb_first(&osd->o_linger_requests); p; p = rb_next(p)) {
3404922dab61SIlya Dryomov 			struct ceph_osd_linger_request *lreq =
3405922dab61SIlya Dryomov 			    rb_entry(p, struct ceph_osd_linger_request, node);
3406922dab61SIlya Dryomov 
3407922dab61SIlya Dryomov 			dout(" lreq %p linger_id %llu is served by osd%d\n",
3408922dab61SIlya Dryomov 			     lreq, lreq->linger_id, osd->o_osd);
3409922dab61SIlya Dryomov 			found = true;
3410922dab61SIlya Dryomov 
3411922dab61SIlya Dryomov 			mutex_lock(&lreq->lock);
341219079203SIlya Dryomov 			if (lreq->is_watch && lreq->committed && !lreq->last_error)
3413922dab61SIlya Dryomov 				send_linger_ping(lreq);
3414922dab61SIlya Dryomov 			mutex_unlock(&lreq->lock);
3415922dab61SIlya Dryomov 		}
34165aea3dcdSIlya Dryomov 
34175aea3dcdSIlya Dryomov 		if (found)
34183d14c5d2SYehuda Sadeh 			list_move_tail(&osd->o_keepalive_item, &slow_osds);
34193d14c5d2SYehuda Sadeh 	}
34205aea3dcdSIlya Dryomov 
34217cc5e38fSIlya Dryomov 	if (opts->osd_request_timeout) {
34227cc5e38fSIlya Dryomov 		for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
34237cc5e38fSIlya Dryomov 			struct ceph_osd_request *req =
34247cc5e38fSIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
34257cc5e38fSIlya Dryomov 
34267cc5e38fSIlya Dryomov 			p = rb_next(p); /* abort_request() */
34277cc5e38fSIlya Dryomov 
34287cc5e38fSIlya Dryomov 			if (time_before(req->r_start_stamp, expiry_cutoff)) {
34297cc5e38fSIlya Dryomov 				pr_err_ratelimited("tid %llu on osd%d timeout\n",
34307cc5e38fSIlya Dryomov 				       req->r_tid, osdc->homeless_osd.o_osd);
34317cc5e38fSIlya Dryomov 				abort_request(req, -ETIMEDOUT);
34327cc5e38fSIlya Dryomov 			}
34337cc5e38fSIlya Dryomov 		}
34347cc5e38fSIlya Dryomov 	}
34357cc5e38fSIlya Dryomov 
34365aea3dcdSIlya Dryomov 	if (atomic_read(&osdc->num_homeless) || !list_empty(&slow_osds))
34375aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
34385aea3dcdSIlya Dryomov 
34393d14c5d2SYehuda Sadeh 	while (!list_empty(&slow_osds)) {
34405aea3dcdSIlya Dryomov 		struct ceph_osd *osd = list_first_entry(&slow_osds,
34415aea3dcdSIlya Dryomov 							struct ceph_osd,
34423d14c5d2SYehuda Sadeh 							o_keepalive_item);
34433d14c5d2SYehuda Sadeh 		list_del_init(&osd->o_keepalive_item);
34443d14c5d2SYehuda Sadeh 		ceph_con_keepalive(&osd->o_con);
34453d14c5d2SYehuda Sadeh 	}
34463d14c5d2SYehuda Sadeh 
34475aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
3448fbca9635SIlya Dryomov 	schedule_delayed_work(&osdc->timeout_work,
3449fbca9635SIlya Dryomov 			      osdc->client->options->osd_keepalive_timeout);
34503d14c5d2SYehuda Sadeh }
34513d14c5d2SYehuda Sadeh 
34523d14c5d2SYehuda Sadeh static void handle_osds_timeout(struct work_struct *work)
34533d14c5d2SYehuda Sadeh {
34543d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc =
34553d14c5d2SYehuda Sadeh 		container_of(work, struct ceph_osd_client,
34563d14c5d2SYehuda Sadeh 			     osds_timeout_work.work);
3457a319bf56SIlya Dryomov 	unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
345842a2c09fSIlya Dryomov 	struct ceph_osd *osd, *nosd;
34593d14c5d2SYehuda Sadeh 
346042a2c09fSIlya Dryomov 	dout("%s osdc %p\n", __func__, osdc);
34615aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
346242a2c09fSIlya Dryomov 	list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
346342a2c09fSIlya Dryomov 		if (time_before(jiffies, osd->lru_ttl))
346442a2c09fSIlya Dryomov 			break;
346542a2c09fSIlya Dryomov 
34665aea3dcdSIlya Dryomov 		WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
3467922dab61SIlya Dryomov 		WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
34685aea3dcdSIlya Dryomov 		close_osd(osd);
346942a2c09fSIlya Dryomov 	}
347042a2c09fSIlya Dryomov 
34715aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
34723d14c5d2SYehuda Sadeh 	schedule_delayed_work(&osdc->osds_timeout_work,
34733d14c5d2SYehuda Sadeh 			      round_jiffies_relative(delay));
34743d14c5d2SYehuda Sadeh }
34753d14c5d2SYehuda Sadeh 
3476205ee118SIlya Dryomov static int ceph_oloc_decode(void **p, void *end,
3477205ee118SIlya Dryomov 			    struct ceph_object_locator *oloc)
3478205ee118SIlya Dryomov {
3479205ee118SIlya Dryomov 	u8 struct_v, struct_cv;
3480205ee118SIlya Dryomov 	u32 len;
3481205ee118SIlya Dryomov 	void *struct_end;
3482205ee118SIlya Dryomov 	int ret = 0;
3483205ee118SIlya Dryomov 
3484205ee118SIlya Dryomov 	ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3485205ee118SIlya Dryomov 	struct_v = ceph_decode_8(p);
3486205ee118SIlya Dryomov 	struct_cv = ceph_decode_8(p);
3487205ee118SIlya Dryomov 	if (struct_v < 3) {
3488205ee118SIlya Dryomov 		pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
3489205ee118SIlya Dryomov 			struct_v, struct_cv);
3490205ee118SIlya Dryomov 		goto e_inval;
3491205ee118SIlya Dryomov 	}
3492205ee118SIlya Dryomov 	if (struct_cv > 6) {
3493205ee118SIlya Dryomov 		pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
3494205ee118SIlya Dryomov 			struct_v, struct_cv);
3495205ee118SIlya Dryomov 		goto e_inval;
3496205ee118SIlya Dryomov 	}
3497205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3498205ee118SIlya Dryomov 	ceph_decode_need(p, end, len, e_inval);
3499205ee118SIlya Dryomov 	struct_end = *p + len;
3500205ee118SIlya Dryomov 
3501205ee118SIlya Dryomov 	oloc->pool = ceph_decode_64(p);
3502205ee118SIlya Dryomov 	*p += 4; /* skip preferred */
3503205ee118SIlya Dryomov 
3504205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3505205ee118SIlya Dryomov 	if (len > 0) {
3506205ee118SIlya Dryomov 		pr_warn("ceph_object_locator::key is set\n");
3507205ee118SIlya Dryomov 		goto e_inval;
3508205ee118SIlya Dryomov 	}
3509205ee118SIlya Dryomov 
3510205ee118SIlya Dryomov 	if (struct_v >= 5) {
3511cd08e0a2SYan, Zheng 		bool changed = false;
3512cd08e0a2SYan, Zheng 
3513205ee118SIlya Dryomov 		len = ceph_decode_32(p);
3514205ee118SIlya Dryomov 		if (len > 0) {
351530c156d9SYan, Zheng 			ceph_decode_need(p, end, len, e_inval);
3516cd08e0a2SYan, Zheng 			if (!oloc->pool_ns ||
3517cd08e0a2SYan, Zheng 			    ceph_compare_string(oloc->pool_ns, *p, len))
3518cd08e0a2SYan, Zheng 				changed = true;
351930c156d9SYan, Zheng 			*p += len;
3520cd08e0a2SYan, Zheng 		} else {
3521cd08e0a2SYan, Zheng 			if (oloc->pool_ns)
3522cd08e0a2SYan, Zheng 				changed = true;
3523cd08e0a2SYan, Zheng 		}
3524cd08e0a2SYan, Zheng 		if (changed) {
3525cd08e0a2SYan, Zheng 			/* redirect changes namespace */
3526cd08e0a2SYan, Zheng 			pr_warn("ceph_object_locator::nspace is changed\n");
3527cd08e0a2SYan, Zheng 			goto e_inval;
3528205ee118SIlya Dryomov 		}
3529205ee118SIlya Dryomov 	}
3530205ee118SIlya Dryomov 
3531205ee118SIlya Dryomov 	if (struct_v >= 6) {
3532205ee118SIlya Dryomov 		s64 hash = ceph_decode_64(p);
3533205ee118SIlya Dryomov 		if (hash != -1) {
3534205ee118SIlya Dryomov 			pr_warn("ceph_object_locator::hash is set\n");
3535205ee118SIlya Dryomov 			goto e_inval;
3536205ee118SIlya Dryomov 		}
3537205ee118SIlya Dryomov 	}
3538205ee118SIlya Dryomov 
3539205ee118SIlya Dryomov 	/* skip the rest */
3540205ee118SIlya Dryomov 	*p = struct_end;
3541205ee118SIlya Dryomov out:
3542205ee118SIlya Dryomov 	return ret;
3543205ee118SIlya Dryomov 
3544205ee118SIlya Dryomov e_inval:
3545205ee118SIlya Dryomov 	ret = -EINVAL;
3546205ee118SIlya Dryomov 	goto out;
3547205ee118SIlya Dryomov }
3548205ee118SIlya Dryomov 
3549205ee118SIlya Dryomov static int ceph_redirect_decode(void **p, void *end,
3550205ee118SIlya Dryomov 				struct ceph_request_redirect *redir)
3551205ee118SIlya Dryomov {
3552205ee118SIlya Dryomov 	u8 struct_v, struct_cv;
3553205ee118SIlya Dryomov 	u32 len;
3554205ee118SIlya Dryomov 	void *struct_end;
3555205ee118SIlya Dryomov 	int ret;
3556205ee118SIlya Dryomov 
3557205ee118SIlya Dryomov 	ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3558205ee118SIlya Dryomov 	struct_v = ceph_decode_8(p);
3559205ee118SIlya Dryomov 	struct_cv = ceph_decode_8(p);
3560205ee118SIlya Dryomov 	if (struct_cv > 1) {
3561205ee118SIlya Dryomov 		pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
3562205ee118SIlya Dryomov 			struct_v, struct_cv);
3563205ee118SIlya Dryomov 		goto e_inval;
3564205ee118SIlya Dryomov 	}
3565205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3566205ee118SIlya Dryomov 	ceph_decode_need(p, end, len, e_inval);
3567205ee118SIlya Dryomov 	struct_end = *p + len;
3568205ee118SIlya Dryomov 
3569205ee118SIlya Dryomov 	ret = ceph_oloc_decode(p, end, &redir->oloc);
3570205ee118SIlya Dryomov 	if (ret)
3571205ee118SIlya Dryomov 		goto out;
3572205ee118SIlya Dryomov 
3573205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3574205ee118SIlya Dryomov 	if (len > 0) {
3575205ee118SIlya Dryomov 		pr_warn("ceph_request_redirect::object_name is set\n");
3576205ee118SIlya Dryomov 		goto e_inval;
3577205ee118SIlya Dryomov 	}
3578205ee118SIlya Dryomov 
3579205ee118SIlya Dryomov 	/* skip the rest */
3580205ee118SIlya Dryomov 	*p = struct_end;
3581205ee118SIlya Dryomov out:
3582205ee118SIlya Dryomov 	return ret;
3583205ee118SIlya Dryomov 
3584205ee118SIlya Dryomov e_inval:
3585205ee118SIlya Dryomov 	ret = -EINVAL;
3586205ee118SIlya Dryomov 	goto out;
3587205ee118SIlya Dryomov }
3588205ee118SIlya Dryomov 
3589fe5da05eSIlya Dryomov struct MOSDOpReply {
3590fe5da05eSIlya Dryomov 	struct ceph_pg pgid;
3591fe5da05eSIlya Dryomov 	u64 flags;
3592fe5da05eSIlya Dryomov 	int result;
3593fe5da05eSIlya Dryomov 	u32 epoch;
3594fe5da05eSIlya Dryomov 	int num_ops;
3595fe5da05eSIlya Dryomov 	u32 outdata_len[CEPH_OSD_MAX_OPS];
3596fe5da05eSIlya Dryomov 	s32 rval[CEPH_OSD_MAX_OPS];
3597fe5da05eSIlya Dryomov 	int retry_attempt;
3598fe5da05eSIlya Dryomov 	struct ceph_eversion replay_version;
3599fe5da05eSIlya Dryomov 	u64 user_version;
3600fe5da05eSIlya Dryomov 	struct ceph_request_redirect redirect;
3601fe5da05eSIlya Dryomov };
360225845472SSage Weil 
3603fe5da05eSIlya Dryomov static int decode_MOSDOpReply(const struct ceph_msg *msg, struct MOSDOpReply *m)
36043d14c5d2SYehuda Sadeh {
3605fe5da05eSIlya Dryomov 	void *p = msg->front.iov_base;
3606fe5da05eSIlya Dryomov 	void *const end = p + msg->front.iov_len;
3607fe5da05eSIlya Dryomov 	u16 version = le16_to_cpu(msg->hdr.version);
3608fe5da05eSIlya Dryomov 	struct ceph_eversion bad_replay_version;
3609b0b31a8fSIlya Dryomov 	u8 decode_redir;
3610fe5da05eSIlya Dryomov 	u32 len;
3611fe5da05eSIlya Dryomov 	int ret;
3612fe5da05eSIlya Dryomov 	int i;
36133d14c5d2SYehuda Sadeh 
3614fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, len, e_inval);
3615fe5da05eSIlya Dryomov 	ceph_decode_need(&p, end, len, e_inval);
3616fe5da05eSIlya Dryomov 	p += len; /* skip oid */
36171b83bef2SSage Weil 
3618fe5da05eSIlya Dryomov 	ret = ceph_decode_pgid(&p, end, &m->pgid);
3619fe5da05eSIlya Dryomov 	if (ret)
3620fe5da05eSIlya Dryomov 		return ret;
36211b83bef2SSage Weil 
3622fe5da05eSIlya Dryomov 	ceph_decode_64_safe(&p, end, m->flags, e_inval);
3623fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->result, e_inval);
3624fe5da05eSIlya Dryomov 	ceph_decode_need(&p, end, sizeof(bad_replay_version), e_inval);
3625fe5da05eSIlya Dryomov 	memcpy(&bad_replay_version, p, sizeof(bad_replay_version));
3626fe5da05eSIlya Dryomov 	p += sizeof(bad_replay_version);
3627fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->epoch, e_inval);
36281b83bef2SSage Weil 
3629fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->num_ops, e_inval);
3630fe5da05eSIlya Dryomov 	if (m->num_ops > ARRAY_SIZE(m->outdata_len))
3631fe5da05eSIlya Dryomov 		goto e_inval;
36321b83bef2SSage Weil 
3633fe5da05eSIlya Dryomov 	ceph_decode_need(&p, end, m->num_ops * sizeof(struct ceph_osd_op),
3634fe5da05eSIlya Dryomov 			 e_inval);
3635fe5da05eSIlya Dryomov 	for (i = 0; i < m->num_ops; i++) {
36361b83bef2SSage Weil 		struct ceph_osd_op *op = p;
36371b83bef2SSage Weil 
3638fe5da05eSIlya Dryomov 		m->outdata_len[i] = le32_to_cpu(op->payload_len);
36391b83bef2SSage Weil 		p += sizeof(*op);
36401b83bef2SSage Weil 	}
3641fe5da05eSIlya Dryomov 
3642fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->retry_attempt, e_inval);
3643fe5da05eSIlya Dryomov 	for (i = 0; i < m->num_ops; i++)
3644fe5da05eSIlya Dryomov 		ceph_decode_32_safe(&p, end, m->rval[i], e_inval);
3645fe5da05eSIlya Dryomov 
3646fe5da05eSIlya Dryomov 	if (version >= 5) {
3647fe5da05eSIlya Dryomov 		ceph_decode_need(&p, end, sizeof(m->replay_version), e_inval);
3648fe5da05eSIlya Dryomov 		memcpy(&m->replay_version, p, sizeof(m->replay_version));
3649fe5da05eSIlya Dryomov 		p += sizeof(m->replay_version);
3650fe5da05eSIlya Dryomov 		ceph_decode_64_safe(&p, end, m->user_version, e_inval);
3651fe5da05eSIlya Dryomov 	} else {
3652fe5da05eSIlya Dryomov 		m->replay_version = bad_replay_version; /* struct */
3653fe5da05eSIlya Dryomov 		m->user_version = le64_to_cpu(m->replay_version.version);
36541b83bef2SSage Weil 	}
36551b83bef2SSage Weil 
3656fe5da05eSIlya Dryomov 	if (version >= 6) {
3657fe5da05eSIlya Dryomov 		if (version >= 7)
3658fe5da05eSIlya Dryomov 			ceph_decode_8_safe(&p, end, decode_redir, e_inval);
3659b0b31a8fSIlya Dryomov 		else
3660b0b31a8fSIlya Dryomov 			decode_redir = 1;
3661b0b31a8fSIlya Dryomov 	} else {
3662b0b31a8fSIlya Dryomov 		decode_redir = 0;
3663b0b31a8fSIlya Dryomov 	}
3664b0b31a8fSIlya Dryomov 
3665b0b31a8fSIlya Dryomov 	if (decode_redir) {
3666fe5da05eSIlya Dryomov 		ret = ceph_redirect_decode(&p, end, &m->redirect);
3667fe5da05eSIlya Dryomov 		if (ret)
3668fe5da05eSIlya Dryomov 			return ret;
3669205ee118SIlya Dryomov 	} else {
3670fe5da05eSIlya Dryomov 		ceph_oloc_init(&m->redirect.oloc);
3671205ee118SIlya Dryomov 	}
3672205ee118SIlya Dryomov 
3673fe5da05eSIlya Dryomov 	return 0;
3674205ee118SIlya Dryomov 
3675fe5da05eSIlya Dryomov e_inval:
3676fe5da05eSIlya Dryomov 	return -EINVAL;
3677fe5da05eSIlya Dryomov }
3678fe5da05eSIlya Dryomov 
3679fe5da05eSIlya Dryomov /*
3680b18b9550SIlya Dryomov  * Handle MOSDOpReply.  Set ->r_result and call the callback if it is
3681b18b9550SIlya Dryomov  * specified.
3682fe5da05eSIlya Dryomov  */
36835aea3dcdSIlya Dryomov static void handle_reply(struct ceph_osd *osd, struct ceph_msg *msg)
3684fe5da05eSIlya Dryomov {
36855aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
3686fe5da05eSIlya Dryomov 	struct ceph_osd_request *req;
3687fe5da05eSIlya Dryomov 	struct MOSDOpReply m;
3688fe5da05eSIlya Dryomov 	u64 tid = le64_to_cpu(msg->hdr.tid);
3689fe5da05eSIlya Dryomov 	u32 data_len = 0;
3690fe5da05eSIlya Dryomov 	int ret;
3691fe5da05eSIlya Dryomov 	int i;
3692fe5da05eSIlya Dryomov 
3693fe5da05eSIlya Dryomov 	dout("%s msg %p tid %llu\n", __func__, msg, tid);
3694fe5da05eSIlya Dryomov 
36955aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
36965aea3dcdSIlya Dryomov 	if (!osd_registered(osd)) {
36975aea3dcdSIlya Dryomov 		dout("%s osd%d unknown\n", __func__, osd->o_osd);
36985aea3dcdSIlya Dryomov 		goto out_unlock_osdc;
3699fe5da05eSIlya Dryomov 	}
37005aea3dcdSIlya Dryomov 	WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
37015aea3dcdSIlya Dryomov 
37025aea3dcdSIlya Dryomov 	mutex_lock(&osd->lock);
37035aea3dcdSIlya Dryomov 	req = lookup_request(&osd->o_requests, tid);
37045aea3dcdSIlya Dryomov 	if (!req) {
37055aea3dcdSIlya Dryomov 		dout("%s osd%d tid %llu unknown\n", __func__, osd->o_osd, tid);
37065aea3dcdSIlya Dryomov 		goto out_unlock_session;
37075aea3dcdSIlya Dryomov 	}
3708fe5da05eSIlya Dryomov 
3709cd08e0a2SYan, Zheng 	m.redirect.oloc.pool_ns = req->r_t.target_oloc.pool_ns;
3710fe5da05eSIlya Dryomov 	ret = decode_MOSDOpReply(msg, &m);
3711cd08e0a2SYan, Zheng 	m.redirect.oloc.pool_ns = NULL;
3712fe5da05eSIlya Dryomov 	if (ret) {
3713fe5da05eSIlya Dryomov 		pr_err("failed to decode MOSDOpReply for tid %llu: %d\n",
3714fe5da05eSIlya Dryomov 		       req->r_tid, ret);
3715fe5da05eSIlya Dryomov 		ceph_msg_dump(msg);
3716fe5da05eSIlya Dryomov 		goto fail_request;
3717fe5da05eSIlya Dryomov 	}
3718fe5da05eSIlya Dryomov 	dout("%s req %p tid %llu flags 0x%llx pgid %llu.%x epoch %u attempt %d v %u'%llu uv %llu\n",
3719fe5da05eSIlya Dryomov 	     __func__, req, req->r_tid, m.flags, m.pgid.pool, m.pgid.seed,
3720fe5da05eSIlya Dryomov 	     m.epoch, m.retry_attempt, le32_to_cpu(m.replay_version.epoch),
3721fe5da05eSIlya Dryomov 	     le64_to_cpu(m.replay_version.version), m.user_version);
3722fe5da05eSIlya Dryomov 
3723fe5da05eSIlya Dryomov 	if (m.retry_attempt >= 0) {
3724fe5da05eSIlya Dryomov 		if (m.retry_attempt != req->r_attempts - 1) {
3725fe5da05eSIlya Dryomov 			dout("req %p tid %llu retry_attempt %d != %d, ignoring\n",
3726fe5da05eSIlya Dryomov 			     req, req->r_tid, m.retry_attempt,
3727fe5da05eSIlya Dryomov 			     req->r_attempts - 1);
37285aea3dcdSIlya Dryomov 			goto out_unlock_session;
3729fe5da05eSIlya Dryomov 		}
3730fe5da05eSIlya Dryomov 	} else {
3731fe5da05eSIlya Dryomov 		WARN_ON(1); /* MOSDOpReply v4 is assumed */
3732fe5da05eSIlya Dryomov 	}
3733fe5da05eSIlya Dryomov 
3734fe5da05eSIlya Dryomov 	if (!ceph_oloc_empty(&m.redirect.oloc)) {
3735fe5da05eSIlya Dryomov 		dout("req %p tid %llu redirect pool %lld\n", req, req->r_tid,
3736fe5da05eSIlya Dryomov 		     m.redirect.oloc.pool);
37375aea3dcdSIlya Dryomov 		unlink_request(osd, req);
37385aea3dcdSIlya Dryomov 		mutex_unlock(&osd->lock);
3739205ee118SIlya Dryomov 
374030c156d9SYan, Zheng 		/*
374130c156d9SYan, Zheng 		 * Not ceph_oloc_copy() - changing pool_ns is not
374230c156d9SYan, Zheng 		 * supported.
374330c156d9SYan, Zheng 		 */
374430c156d9SYan, Zheng 		req->r_t.target_oloc.pool = m.redirect.oloc.pool;
3745890bd0f8SJerry Lee 		req->r_flags |= CEPH_OSD_FLAG_REDIRECTED |
3746890bd0f8SJerry Lee 				CEPH_OSD_FLAG_IGNORE_OVERLAY |
3747890bd0f8SJerry Lee 				CEPH_OSD_FLAG_IGNORE_CACHE;
37485aea3dcdSIlya Dryomov 		req->r_tid = 0;
37495aea3dcdSIlya Dryomov 		__submit_request(req, false);
37505aea3dcdSIlya Dryomov 		goto out_unlock_osdc;
3751205ee118SIlya Dryomov 	}
3752205ee118SIlya Dryomov 
3753117d96a0SIlya Dryomov 	if (m.result == -EAGAIN) {
3754117d96a0SIlya Dryomov 		dout("req %p tid %llu EAGAIN\n", req, req->r_tid);
3755117d96a0SIlya Dryomov 		unlink_request(osd, req);
3756117d96a0SIlya Dryomov 		mutex_unlock(&osd->lock);
3757117d96a0SIlya Dryomov 
3758117d96a0SIlya Dryomov 		/*
3759117d96a0SIlya Dryomov 		 * The object is missing on the replica or not (yet)
3760117d96a0SIlya Dryomov 		 * readable.  Clear pgid to force a resend to the primary
3761117d96a0SIlya Dryomov 		 * via legacy_change.
3762117d96a0SIlya Dryomov 		 */
3763117d96a0SIlya Dryomov 		req->r_t.pgid.pool = 0;
3764117d96a0SIlya Dryomov 		req->r_t.pgid.seed = 0;
3765117d96a0SIlya Dryomov 		WARN_ON(!req->r_t.used_replica);
3766117d96a0SIlya Dryomov 		req->r_flags &= ~(CEPH_OSD_FLAG_BALANCE_READS |
3767117d96a0SIlya Dryomov 				  CEPH_OSD_FLAG_LOCALIZE_READS);
3768117d96a0SIlya Dryomov 		req->r_tid = 0;
3769117d96a0SIlya Dryomov 		__submit_request(req, false);
3770117d96a0SIlya Dryomov 		goto out_unlock_osdc;
3771117d96a0SIlya Dryomov 	}
3772117d96a0SIlya Dryomov 
3773fe5da05eSIlya Dryomov 	if (m.num_ops != req->r_num_ops) {
3774fe5da05eSIlya Dryomov 		pr_err("num_ops %d != %d for tid %llu\n", m.num_ops,
3775fe5da05eSIlya Dryomov 		       req->r_num_ops, req->r_tid);
3776fe5da05eSIlya Dryomov 		goto fail_request;
3777fe5da05eSIlya Dryomov 	}
3778fe5da05eSIlya Dryomov 	for (i = 0; i < req->r_num_ops; i++) {
3779fe5da05eSIlya Dryomov 		dout(" req %p tid %llu op %d rval %d len %u\n", req,
3780fe5da05eSIlya Dryomov 		     req->r_tid, i, m.rval[i], m.outdata_len[i]);
3781fe5da05eSIlya Dryomov 		req->r_ops[i].rval = m.rval[i];
3782fe5da05eSIlya Dryomov 		req->r_ops[i].outdata_len = m.outdata_len[i];
3783fe5da05eSIlya Dryomov 		data_len += m.outdata_len[i];
3784fe5da05eSIlya Dryomov 	}
3785fe5da05eSIlya Dryomov 	if (data_len != le32_to_cpu(msg->hdr.data_len)) {
3786fe5da05eSIlya Dryomov 		pr_err("sum of lens %u != %u for tid %llu\n", data_len,
3787fe5da05eSIlya Dryomov 		       le32_to_cpu(msg->hdr.data_len), req->r_tid);
3788fe5da05eSIlya Dryomov 		goto fail_request;
3789fe5da05eSIlya Dryomov 	}
3790b18b9550SIlya Dryomov 	dout("%s req %p tid %llu result %d data_len %u\n", __func__,
3791b18b9550SIlya Dryomov 	     req, req->r_tid, m.result, data_len);
37923d14c5d2SYehuda Sadeh 
3793b18b9550SIlya Dryomov 	/*
3794b18b9550SIlya Dryomov 	 * Since we only ever request ONDISK, we should only ever get
3795b18b9550SIlya Dryomov 	 * one (type of) reply back.
3796b18b9550SIlya Dryomov 	 */
3797b18b9550SIlya Dryomov 	WARN_ON(!(m.flags & CEPH_OSD_FLAG_ONDISK));
3798fe5da05eSIlya Dryomov 	req->r_result = m.result ?: data_len;
379945ee2c1dSIlya Dryomov 	finish_request(req);
38005aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
38015aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
38023d14c5d2SYehuda Sadeh 
3803fe5da05eSIlya Dryomov 	__complete_request(req);
38043d14c5d2SYehuda Sadeh 	return;
3805fe5da05eSIlya Dryomov 
3806fe5da05eSIlya Dryomov fail_request:
38074609245eSIlya Dryomov 	complete_request(req, -EIO);
38085aea3dcdSIlya Dryomov out_unlock_session:
38095aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
38105aea3dcdSIlya Dryomov out_unlock_osdc:
38115aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
38123d14c5d2SYehuda Sadeh }
38133d14c5d2SYehuda Sadeh 
381442c1b124SIlya Dryomov static void set_pool_was_full(struct ceph_osd_client *osdc)
381542c1b124SIlya Dryomov {
381642c1b124SIlya Dryomov 	struct rb_node *n;
381742c1b124SIlya Dryomov 
381842c1b124SIlya Dryomov 	for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
381942c1b124SIlya Dryomov 		struct ceph_pg_pool_info *pi =
382042c1b124SIlya Dryomov 		    rb_entry(n, struct ceph_pg_pool_info, node);
382142c1b124SIlya Dryomov 
382242c1b124SIlya Dryomov 		pi->was_full = __pool_full(pi);
382342c1b124SIlya Dryomov 	}
382442c1b124SIlya Dryomov }
382542c1b124SIlya Dryomov 
38265aea3dcdSIlya Dryomov static bool pool_cleared_full(struct ceph_osd_client *osdc, s64 pool_id)
38273d14c5d2SYehuda Sadeh {
38285aea3dcdSIlya Dryomov 	struct ceph_pg_pool_info *pi;
38293d14c5d2SYehuda Sadeh 
38305aea3dcdSIlya Dryomov 	pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
38315aea3dcdSIlya Dryomov 	if (!pi)
38325aea3dcdSIlya Dryomov 		return false;
38333d14c5d2SYehuda Sadeh 
38345aea3dcdSIlya Dryomov 	return pi->was_full && !__pool_full(pi);
38353d14c5d2SYehuda Sadeh }
38363d14c5d2SYehuda Sadeh 
3837922dab61SIlya Dryomov static enum calc_target_result
3838922dab61SIlya Dryomov recalc_linger_target(struct ceph_osd_linger_request *lreq)
3839922dab61SIlya Dryomov {
3840922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
3841922dab61SIlya Dryomov 	enum calc_target_result ct_res;
3842922dab61SIlya Dryomov 
38438edf84baSIlya Dryomov 	ct_res = calc_target(osdc, &lreq->t, true);
3844922dab61SIlya Dryomov 	if (ct_res == CALC_TARGET_NEED_RESEND) {
3845922dab61SIlya Dryomov 		struct ceph_osd *osd;
3846922dab61SIlya Dryomov 
3847922dab61SIlya Dryomov 		osd = lookup_create_osd(osdc, lreq->t.osd, true);
3848922dab61SIlya Dryomov 		if (osd != lreq->osd) {
3849922dab61SIlya Dryomov 			unlink_linger(lreq->osd, lreq);
3850922dab61SIlya Dryomov 			link_linger(osd, lreq);
3851922dab61SIlya Dryomov 		}
3852922dab61SIlya Dryomov 	}
3853922dab61SIlya Dryomov 
3854922dab61SIlya Dryomov 	return ct_res;
3855922dab61SIlya Dryomov }
3856922dab61SIlya Dryomov 
38573d14c5d2SYehuda Sadeh /*
38585aea3dcdSIlya Dryomov  * Requeue requests whose mapping to an OSD has changed.
38593d14c5d2SYehuda Sadeh  */
38605aea3dcdSIlya Dryomov static void scan_requests(struct ceph_osd *osd,
38615aea3dcdSIlya Dryomov 			  bool force_resend,
38625aea3dcdSIlya Dryomov 			  bool cleared_full,
38635aea3dcdSIlya Dryomov 			  bool check_pool_cleared_full,
38645aea3dcdSIlya Dryomov 			  struct rb_root *need_resend,
38655aea3dcdSIlya Dryomov 			  struct list_head *need_resend_linger)
38663d14c5d2SYehuda Sadeh {
38675aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
38685aea3dcdSIlya Dryomov 	struct rb_node *n;
38695aea3dcdSIlya Dryomov 	bool force_resend_writes;
38703d14c5d2SYehuda Sadeh 
3871922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_linger_requests); n; ) {
3872922dab61SIlya Dryomov 		struct ceph_osd_linger_request *lreq =
3873922dab61SIlya Dryomov 		    rb_entry(n, struct ceph_osd_linger_request, node);
3874922dab61SIlya Dryomov 		enum calc_target_result ct_res;
3875922dab61SIlya Dryomov 
3876922dab61SIlya Dryomov 		n = rb_next(n); /* recalc_linger_target() */
3877922dab61SIlya Dryomov 
3878922dab61SIlya Dryomov 		dout("%s lreq %p linger_id %llu\n", __func__, lreq,
3879922dab61SIlya Dryomov 		     lreq->linger_id);
3880922dab61SIlya Dryomov 		ct_res = recalc_linger_target(lreq);
3881922dab61SIlya Dryomov 		switch (ct_res) {
3882922dab61SIlya Dryomov 		case CALC_TARGET_NO_ACTION:
3883922dab61SIlya Dryomov 			force_resend_writes = cleared_full ||
3884922dab61SIlya Dryomov 			    (check_pool_cleared_full &&
3885922dab61SIlya Dryomov 			     pool_cleared_full(osdc, lreq->t.base_oloc.pool));
3886922dab61SIlya Dryomov 			if (!force_resend && !force_resend_writes)
3887922dab61SIlya Dryomov 				break;
3888922dab61SIlya Dryomov 
3889df561f66SGustavo A. R. Silva 			fallthrough;
3890922dab61SIlya Dryomov 		case CALC_TARGET_NEED_RESEND:
38914609245eSIlya Dryomov 			cancel_linger_map_check(lreq);
3892922dab61SIlya Dryomov 			/*
3893922dab61SIlya Dryomov 			 * scan_requests() for the previous epoch(s)
3894922dab61SIlya Dryomov 			 * may have already added it to the list, since
3895922dab61SIlya Dryomov 			 * it's not unlinked here.
3896922dab61SIlya Dryomov 			 */
3897922dab61SIlya Dryomov 			if (list_empty(&lreq->scan_item))
3898922dab61SIlya Dryomov 				list_add_tail(&lreq->scan_item, need_resend_linger);
3899922dab61SIlya Dryomov 			break;
3900922dab61SIlya Dryomov 		case CALC_TARGET_POOL_DNE:
3901a10bcb19SIlya Dryomov 			list_del_init(&lreq->scan_item);
39024609245eSIlya Dryomov 			check_linger_pool_dne(lreq);
3903922dab61SIlya Dryomov 			break;
3904922dab61SIlya Dryomov 		}
3905922dab61SIlya Dryomov 	}
3906922dab61SIlya Dryomov 
39075aea3dcdSIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; ) {
39085aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
39095aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
39105aea3dcdSIlya Dryomov 		enum calc_target_result ct_res;
3911ab60b16dSAlex Elder 
39124609245eSIlya Dryomov 		n = rb_next(n); /* unlink_request(), check_pool_dne() */
3913ab60b16dSAlex Elder 
39145aea3dcdSIlya Dryomov 		dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
39158edf84baSIlya Dryomov 		ct_res = calc_target(osdc, &req->r_t, false);
39165aea3dcdSIlya Dryomov 		switch (ct_res) {
39175aea3dcdSIlya Dryomov 		case CALC_TARGET_NO_ACTION:
39185aea3dcdSIlya Dryomov 			force_resend_writes = cleared_full ||
39195aea3dcdSIlya Dryomov 			    (check_pool_cleared_full &&
39205aea3dcdSIlya Dryomov 			     pool_cleared_full(osdc, req->r_t.base_oloc.pool));
39215aea3dcdSIlya Dryomov 			if (!force_resend &&
39225aea3dcdSIlya Dryomov 			    (!(req->r_flags & CEPH_OSD_FLAG_WRITE) ||
39235aea3dcdSIlya Dryomov 			     !force_resend_writes))
39245aea3dcdSIlya Dryomov 				break;
3925a40c4f10SYehuda Sadeh 
3926df561f66SGustavo A. R. Silva 			fallthrough;
39275aea3dcdSIlya Dryomov 		case CALC_TARGET_NEED_RESEND:
39284609245eSIlya Dryomov 			cancel_map_check(req);
39295aea3dcdSIlya Dryomov 			unlink_request(osd, req);
39305aea3dcdSIlya Dryomov 			insert_request(need_resend, req);
39315aea3dcdSIlya Dryomov 			break;
39325aea3dcdSIlya Dryomov 		case CALC_TARGET_POOL_DNE:
39334609245eSIlya Dryomov 			check_pool_dne(req);
39345aea3dcdSIlya Dryomov 			break;
3935a40c4f10SYehuda Sadeh 		}
39363d14c5d2SYehuda Sadeh 	}
39373d14c5d2SYehuda Sadeh }
39386f6c7006SSage Weil 
393942c1b124SIlya Dryomov static int handle_one_map(struct ceph_osd_client *osdc,
39405aea3dcdSIlya Dryomov 			  void *p, void *end, bool incremental,
39415aea3dcdSIlya Dryomov 			  struct rb_root *need_resend,
39425aea3dcdSIlya Dryomov 			  struct list_head *need_resend_linger)
394342c1b124SIlya Dryomov {
394442c1b124SIlya Dryomov 	struct ceph_osdmap *newmap;
394542c1b124SIlya Dryomov 	struct rb_node *n;
394642c1b124SIlya Dryomov 	bool skipped_map = false;
394742c1b124SIlya Dryomov 	bool was_full;
394842c1b124SIlya Dryomov 
3949b7ec35b3SIlya Dryomov 	was_full = ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
395042c1b124SIlya Dryomov 	set_pool_was_full(osdc);
395142c1b124SIlya Dryomov 
395242c1b124SIlya Dryomov 	if (incremental)
3953cd1a677cSIlya Dryomov 		newmap = osdmap_apply_incremental(&p, end,
3954cd1a677cSIlya Dryomov 						  ceph_msgr2(osdc->client),
3955cd1a677cSIlya Dryomov 						  osdc->osdmap);
395642c1b124SIlya Dryomov 	else
3957cd1a677cSIlya Dryomov 		newmap = ceph_osdmap_decode(&p, end, ceph_msgr2(osdc->client));
395842c1b124SIlya Dryomov 	if (IS_ERR(newmap))
395942c1b124SIlya Dryomov 		return PTR_ERR(newmap);
396042c1b124SIlya Dryomov 
396142c1b124SIlya Dryomov 	if (newmap != osdc->osdmap) {
396242c1b124SIlya Dryomov 		/*
396342c1b124SIlya Dryomov 		 * Preserve ->was_full before destroying the old map.
396442c1b124SIlya Dryomov 		 * For pools that weren't in the old map, ->was_full
396542c1b124SIlya Dryomov 		 * should be false.
396642c1b124SIlya Dryomov 		 */
396742c1b124SIlya Dryomov 		for (n = rb_first(&newmap->pg_pools); n; n = rb_next(n)) {
396842c1b124SIlya Dryomov 			struct ceph_pg_pool_info *pi =
396942c1b124SIlya Dryomov 			    rb_entry(n, struct ceph_pg_pool_info, node);
397042c1b124SIlya Dryomov 			struct ceph_pg_pool_info *old_pi;
397142c1b124SIlya Dryomov 
397242c1b124SIlya Dryomov 			old_pi = ceph_pg_pool_by_id(osdc->osdmap, pi->id);
397342c1b124SIlya Dryomov 			if (old_pi)
397442c1b124SIlya Dryomov 				pi->was_full = old_pi->was_full;
397542c1b124SIlya Dryomov 			else
397642c1b124SIlya Dryomov 				WARN_ON(pi->was_full);
397742c1b124SIlya Dryomov 		}
397842c1b124SIlya Dryomov 
397942c1b124SIlya Dryomov 		if (osdc->osdmap->epoch &&
398042c1b124SIlya Dryomov 		    osdc->osdmap->epoch + 1 < newmap->epoch) {
398142c1b124SIlya Dryomov 			WARN_ON(incremental);
398242c1b124SIlya Dryomov 			skipped_map = true;
398342c1b124SIlya Dryomov 		}
398442c1b124SIlya Dryomov 
398542c1b124SIlya Dryomov 		ceph_osdmap_destroy(osdc->osdmap);
398642c1b124SIlya Dryomov 		osdc->osdmap = newmap;
398742c1b124SIlya Dryomov 	}
398842c1b124SIlya Dryomov 
3989b7ec35b3SIlya Dryomov 	was_full &= !ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
39905aea3dcdSIlya Dryomov 	scan_requests(&osdc->homeless_osd, skipped_map, was_full, true,
39915aea3dcdSIlya Dryomov 		      need_resend, need_resend_linger);
39925aea3dcdSIlya Dryomov 
39935aea3dcdSIlya Dryomov 	for (n = rb_first(&osdc->osds); n; ) {
39945aea3dcdSIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
39955aea3dcdSIlya Dryomov 
39965aea3dcdSIlya Dryomov 		n = rb_next(n); /* close_osd() */
39975aea3dcdSIlya Dryomov 
39985aea3dcdSIlya Dryomov 		scan_requests(osd, skipped_map, was_full, true, need_resend,
39995aea3dcdSIlya Dryomov 			      need_resend_linger);
40005aea3dcdSIlya Dryomov 		if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
40015aea3dcdSIlya Dryomov 		    memcmp(&osd->o_con.peer_addr,
40025aea3dcdSIlya Dryomov 			   ceph_osd_addr(osdc->osdmap, osd->o_osd),
40035aea3dcdSIlya Dryomov 			   sizeof(struct ceph_entity_addr)))
40045aea3dcdSIlya Dryomov 			close_osd(osd);
40055aea3dcdSIlya Dryomov 	}
400642c1b124SIlya Dryomov 
400742c1b124SIlya Dryomov 	return 0;
400842c1b124SIlya Dryomov }
40096f6c7006SSage Weil 
40105aea3dcdSIlya Dryomov static void kick_requests(struct ceph_osd_client *osdc,
40115aea3dcdSIlya Dryomov 			  struct rb_root *need_resend,
40125aea3dcdSIlya Dryomov 			  struct list_head *need_resend_linger)
40135aea3dcdSIlya Dryomov {
4014922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq, *nlreq;
401504c7d789SIlya Dryomov 	enum calc_target_result ct_res;
40165aea3dcdSIlya Dryomov 	struct rb_node *n;
40175aea3dcdSIlya Dryomov 
401804c7d789SIlya Dryomov 	/* make sure need_resend targets reflect latest map */
401904c7d789SIlya Dryomov 	for (n = rb_first(need_resend); n; ) {
402004c7d789SIlya Dryomov 		struct ceph_osd_request *req =
402104c7d789SIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
402204c7d789SIlya Dryomov 
402304c7d789SIlya Dryomov 		n = rb_next(n);
402404c7d789SIlya Dryomov 
402504c7d789SIlya Dryomov 		if (req->r_t.epoch < osdc->osdmap->epoch) {
40268edf84baSIlya Dryomov 			ct_res = calc_target(osdc, &req->r_t, false);
402704c7d789SIlya Dryomov 			if (ct_res == CALC_TARGET_POOL_DNE) {
402804c7d789SIlya Dryomov 				erase_request(need_resend, req);
402904c7d789SIlya Dryomov 				check_pool_dne(req);
403004c7d789SIlya Dryomov 			}
403104c7d789SIlya Dryomov 		}
403204c7d789SIlya Dryomov 	}
403304c7d789SIlya Dryomov 
40345aea3dcdSIlya Dryomov 	for (n = rb_first(need_resend); n; ) {
40355aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
40365aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
40375aea3dcdSIlya Dryomov 		struct ceph_osd *osd;
40385aea3dcdSIlya Dryomov 
40395aea3dcdSIlya Dryomov 		n = rb_next(n);
40405aea3dcdSIlya Dryomov 		erase_request(need_resend, req); /* before link_request() */
40415aea3dcdSIlya Dryomov 
40425aea3dcdSIlya Dryomov 		osd = lookup_create_osd(osdc, req->r_t.osd, true);
40435aea3dcdSIlya Dryomov 		link_request(osd, req);
40445aea3dcdSIlya Dryomov 		if (!req->r_linger) {
40455aea3dcdSIlya Dryomov 			if (!osd_homeless(osd) && !req->r_t.paused)
40465aea3dcdSIlya Dryomov 				send_request(req);
4047922dab61SIlya Dryomov 		} else {
4048922dab61SIlya Dryomov 			cancel_linger_request(req);
40495aea3dcdSIlya Dryomov 		}
40505aea3dcdSIlya Dryomov 	}
4051922dab61SIlya Dryomov 
4052922dab61SIlya Dryomov 	list_for_each_entry_safe(lreq, nlreq, need_resend_linger, scan_item) {
4053922dab61SIlya Dryomov 		if (!osd_homeless(lreq->osd))
4054922dab61SIlya Dryomov 			send_linger(lreq);
4055922dab61SIlya Dryomov 
4056922dab61SIlya Dryomov 		list_del_init(&lreq->scan_item);
4057922dab61SIlya Dryomov 	}
40585aea3dcdSIlya Dryomov }
40595aea3dcdSIlya Dryomov 
40603d14c5d2SYehuda Sadeh /*
40613d14c5d2SYehuda Sadeh  * Process updated osd map.
40623d14c5d2SYehuda Sadeh  *
40633d14c5d2SYehuda Sadeh  * The message contains any number of incremental and full maps, normally
40643d14c5d2SYehuda Sadeh  * indicating some sort of topology change in the cluster.  Kick requests
40653d14c5d2SYehuda Sadeh  * off to different OSDs as needed.
40663d14c5d2SYehuda Sadeh  */
40673d14c5d2SYehuda Sadeh void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
40683d14c5d2SYehuda Sadeh {
406942c1b124SIlya Dryomov 	void *p = msg->front.iov_base;
407042c1b124SIlya Dryomov 	void *const end = p + msg->front.iov_len;
40713d14c5d2SYehuda Sadeh 	u32 nr_maps, maplen;
40723d14c5d2SYehuda Sadeh 	u32 epoch;
40733d14c5d2SYehuda Sadeh 	struct ceph_fsid fsid;
40745aea3dcdSIlya Dryomov 	struct rb_root need_resend = RB_ROOT;
40755aea3dcdSIlya Dryomov 	LIST_HEAD(need_resend_linger);
407642c1b124SIlya Dryomov 	bool handled_incremental = false;
407742c1b124SIlya Dryomov 	bool was_pauserd, was_pausewr;
407842c1b124SIlya Dryomov 	bool pauserd, pausewr;
407942c1b124SIlya Dryomov 	int err;
40803d14c5d2SYehuda Sadeh 
408142c1b124SIlya Dryomov 	dout("%s have %u\n", __func__, osdc->osdmap->epoch);
40825aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
40833d14c5d2SYehuda Sadeh 
40843d14c5d2SYehuda Sadeh 	/* verify fsid */
40853d14c5d2SYehuda Sadeh 	ceph_decode_need(&p, end, sizeof(fsid), bad);
40863d14c5d2SYehuda Sadeh 	ceph_decode_copy(&p, &fsid, sizeof(fsid));
40873d14c5d2SYehuda Sadeh 	if (ceph_check_fsid(osdc->client, &fsid) < 0)
408842c1b124SIlya Dryomov 		goto bad;
40893d14c5d2SYehuda Sadeh 
4090b7ec35b3SIlya Dryomov 	was_pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
4091b7ec35b3SIlya Dryomov 	was_pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
4092b7ec35b3SIlya Dryomov 		      ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
409342c1b124SIlya Dryomov 		      have_pool_full(osdc);
40949a1ea2dbSJosh Durgin 
40953d14c5d2SYehuda Sadeh 	/* incremental maps */
40963d14c5d2SYehuda Sadeh 	ceph_decode_32_safe(&p, end, nr_maps, bad);
40973d14c5d2SYehuda Sadeh 	dout(" %d inc maps\n", nr_maps);
40983d14c5d2SYehuda Sadeh 	while (nr_maps > 0) {
40993d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, 2*sizeof(u32), bad);
41003d14c5d2SYehuda Sadeh 		epoch = ceph_decode_32(&p);
41013d14c5d2SYehuda Sadeh 		maplen = ceph_decode_32(&p);
41023d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, maplen, bad);
410342c1b124SIlya Dryomov 		if (osdc->osdmap->epoch &&
410442c1b124SIlya Dryomov 		    osdc->osdmap->epoch + 1 == epoch) {
41053d14c5d2SYehuda Sadeh 			dout("applying incremental map %u len %d\n",
41063d14c5d2SYehuda Sadeh 			     epoch, maplen);
41075aea3dcdSIlya Dryomov 			err = handle_one_map(osdc, p, p + maplen, true,
41085aea3dcdSIlya Dryomov 					     &need_resend, &need_resend_linger);
410942c1b124SIlya Dryomov 			if (err)
41103d14c5d2SYehuda Sadeh 				goto bad;
411142c1b124SIlya Dryomov 			handled_incremental = true;
41123d14c5d2SYehuda Sadeh 		} else {
41133d14c5d2SYehuda Sadeh 			dout("ignoring incremental map %u len %d\n",
41143d14c5d2SYehuda Sadeh 			     epoch, maplen);
41153d14c5d2SYehuda Sadeh 		}
411642c1b124SIlya Dryomov 		p += maplen;
41173d14c5d2SYehuda Sadeh 		nr_maps--;
41183d14c5d2SYehuda Sadeh 	}
411942c1b124SIlya Dryomov 	if (handled_incremental)
41203d14c5d2SYehuda Sadeh 		goto done;
41213d14c5d2SYehuda Sadeh 
41223d14c5d2SYehuda Sadeh 	/* full maps */
41233d14c5d2SYehuda Sadeh 	ceph_decode_32_safe(&p, end, nr_maps, bad);
41243d14c5d2SYehuda Sadeh 	dout(" %d full maps\n", nr_maps);
41253d14c5d2SYehuda Sadeh 	while (nr_maps) {
41263d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, 2*sizeof(u32), bad);
41273d14c5d2SYehuda Sadeh 		epoch = ceph_decode_32(&p);
41283d14c5d2SYehuda Sadeh 		maplen = ceph_decode_32(&p);
41293d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, maplen, bad);
41303d14c5d2SYehuda Sadeh 		if (nr_maps > 1) {
41313d14c5d2SYehuda Sadeh 			dout("skipping non-latest full map %u len %d\n",
41323d14c5d2SYehuda Sadeh 			     epoch, maplen);
4133e5253a7bSIlya Dryomov 		} else if (osdc->osdmap->epoch >= epoch) {
41343d14c5d2SYehuda Sadeh 			dout("skipping full map %u len %d, "
41353d14c5d2SYehuda Sadeh 			     "older than our %u\n", epoch, maplen,
41363d14c5d2SYehuda Sadeh 			     osdc->osdmap->epoch);
41373d14c5d2SYehuda Sadeh 		} else {
41383d14c5d2SYehuda Sadeh 			dout("taking full map %u len %d\n", epoch, maplen);
41395aea3dcdSIlya Dryomov 			err = handle_one_map(osdc, p, p + maplen, false,
41405aea3dcdSIlya Dryomov 					     &need_resend, &need_resend_linger);
414142c1b124SIlya Dryomov 			if (err)
41423d14c5d2SYehuda Sadeh 				goto bad;
41433d14c5d2SYehuda Sadeh 		}
41443d14c5d2SYehuda Sadeh 		p += maplen;
41453d14c5d2SYehuda Sadeh 		nr_maps--;
41463d14c5d2SYehuda Sadeh 	}
41473d14c5d2SYehuda Sadeh 
41483d14c5d2SYehuda Sadeh done:
4149cd634fb6SSage Weil 	/*
4150cd634fb6SSage Weil 	 * subscribe to subsequent osdmap updates if full to ensure
4151cd634fb6SSage Weil 	 * we find out when we are no longer full and stop returning
4152cd634fb6SSage Weil 	 * ENOSPC.
4153cd634fb6SSage Weil 	 */
4154b7ec35b3SIlya Dryomov 	pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
4155b7ec35b3SIlya Dryomov 	pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
4156b7ec35b3SIlya Dryomov 		  ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
415742c1b124SIlya Dryomov 		  have_pool_full(osdc);
415858eb7932SJeff Layton 	if (was_pauserd || was_pausewr || pauserd || pausewr ||
415958eb7932SJeff Layton 	    osdc->osdmap->epoch < osdc->epoch_barrier)
416042c1b124SIlya Dryomov 		maybe_request_map(osdc);
4161cd634fb6SSage Weil 
41625aea3dcdSIlya Dryomov 	kick_requests(osdc, &need_resend, &need_resend_linger);
416342c1b124SIlya Dryomov 
4164fc36d0a4SJeff Layton 	ceph_osdc_abort_on_full(osdc);
416542c1b124SIlya Dryomov 	ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
416642c1b124SIlya Dryomov 			  osdc->osdmap->epoch);
41675aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
41683d14c5d2SYehuda Sadeh 	wake_up_all(&osdc->client->auth_wq);
41693d14c5d2SYehuda Sadeh 	return;
41703d14c5d2SYehuda Sadeh 
41713d14c5d2SYehuda Sadeh bad:
41723d14c5d2SYehuda Sadeh 	pr_err("osdc handle_map corrupt msg\n");
41733d14c5d2SYehuda Sadeh 	ceph_msg_dump(msg);
41745aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
41755aea3dcdSIlya Dryomov }
41765aea3dcdSIlya Dryomov 
41775aea3dcdSIlya Dryomov /*
41785aea3dcdSIlya Dryomov  * Resubmit requests pending on the given osd.
41795aea3dcdSIlya Dryomov  */
41805aea3dcdSIlya Dryomov static void kick_osd_requests(struct ceph_osd *osd)
41815aea3dcdSIlya Dryomov {
41825aea3dcdSIlya Dryomov 	struct rb_node *n;
41835aea3dcdSIlya Dryomov 
4184a02a946dSIlya Dryomov 	clear_backoffs(osd);
4185a02a946dSIlya Dryomov 
4186922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; ) {
41875aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
41885aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
41895aea3dcdSIlya Dryomov 
4190922dab61SIlya Dryomov 		n = rb_next(n); /* cancel_linger_request() */
4191922dab61SIlya Dryomov 
41925aea3dcdSIlya Dryomov 		if (!req->r_linger) {
41935aea3dcdSIlya Dryomov 			if (!req->r_t.paused)
41945aea3dcdSIlya Dryomov 				send_request(req);
4195922dab61SIlya Dryomov 		} else {
4196922dab61SIlya Dryomov 			cancel_linger_request(req);
41975aea3dcdSIlya Dryomov 		}
41985aea3dcdSIlya Dryomov 	}
4199922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_linger_requests); n; n = rb_next(n)) {
4200922dab61SIlya Dryomov 		struct ceph_osd_linger_request *lreq =
4201922dab61SIlya Dryomov 		    rb_entry(n, struct ceph_osd_linger_request, node);
4202922dab61SIlya Dryomov 
4203922dab61SIlya Dryomov 		send_linger(lreq);
4204922dab61SIlya Dryomov 	}
42055aea3dcdSIlya Dryomov }
42065aea3dcdSIlya Dryomov 
42075aea3dcdSIlya Dryomov /*
42085aea3dcdSIlya Dryomov  * If the osd connection drops, we need to resubmit all requests.
42095aea3dcdSIlya Dryomov  */
42105aea3dcdSIlya Dryomov static void osd_fault(struct ceph_connection *con)
42115aea3dcdSIlya Dryomov {
42125aea3dcdSIlya Dryomov 	struct ceph_osd *osd = con->private;
42135aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
42145aea3dcdSIlya Dryomov 
42155aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
42165aea3dcdSIlya Dryomov 
42175aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
42185aea3dcdSIlya Dryomov 	if (!osd_registered(osd)) {
42195aea3dcdSIlya Dryomov 		dout("%s osd%d unknown\n", __func__, osd->o_osd);
42205aea3dcdSIlya Dryomov 		goto out_unlock;
42215aea3dcdSIlya Dryomov 	}
42225aea3dcdSIlya Dryomov 
42235aea3dcdSIlya Dryomov 	if (!reopen_osd(osd))
42245aea3dcdSIlya Dryomov 		kick_osd_requests(osd);
42255aea3dcdSIlya Dryomov 	maybe_request_map(osdc);
42265aea3dcdSIlya Dryomov 
42275aea3dcdSIlya Dryomov out_unlock:
42285aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
42293d14c5d2SYehuda Sadeh }
42303d14c5d2SYehuda Sadeh 
4231a02a946dSIlya Dryomov struct MOSDBackoff {
4232a02a946dSIlya Dryomov 	struct ceph_spg spgid;
4233a02a946dSIlya Dryomov 	u32 map_epoch;
4234a02a946dSIlya Dryomov 	u8 op;
4235a02a946dSIlya Dryomov 	u64 id;
4236a02a946dSIlya Dryomov 	struct ceph_hobject_id *begin;
4237a02a946dSIlya Dryomov 	struct ceph_hobject_id *end;
4238a02a946dSIlya Dryomov };
4239a02a946dSIlya Dryomov 
4240a02a946dSIlya Dryomov static int decode_MOSDBackoff(const struct ceph_msg *msg, struct MOSDBackoff *m)
4241a02a946dSIlya Dryomov {
4242a02a946dSIlya Dryomov 	void *p = msg->front.iov_base;
4243a02a946dSIlya Dryomov 	void *const end = p + msg->front.iov_len;
4244a02a946dSIlya Dryomov 	u8 struct_v;
4245a02a946dSIlya Dryomov 	u32 struct_len;
4246a02a946dSIlya Dryomov 	int ret;
4247a02a946dSIlya Dryomov 
4248a02a946dSIlya Dryomov 	ret = ceph_start_decoding(&p, end, 1, "spg_t", &struct_v, &struct_len);
4249a02a946dSIlya Dryomov 	if (ret)
4250a02a946dSIlya Dryomov 		return ret;
4251a02a946dSIlya Dryomov 
4252a02a946dSIlya Dryomov 	ret = ceph_decode_pgid(&p, end, &m->spgid.pgid);
4253a02a946dSIlya Dryomov 	if (ret)
4254a02a946dSIlya Dryomov 		return ret;
4255a02a946dSIlya Dryomov 
4256a02a946dSIlya Dryomov 	ceph_decode_8_safe(&p, end, m->spgid.shard, e_inval);
4257a02a946dSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->map_epoch, e_inval);
4258a02a946dSIlya Dryomov 	ceph_decode_8_safe(&p, end, m->op, e_inval);
4259a02a946dSIlya Dryomov 	ceph_decode_64_safe(&p, end, m->id, e_inval);
4260a02a946dSIlya Dryomov 
4261a02a946dSIlya Dryomov 	m->begin = kzalloc(sizeof(*m->begin), GFP_NOIO);
4262a02a946dSIlya Dryomov 	if (!m->begin)
4263a02a946dSIlya Dryomov 		return -ENOMEM;
4264a02a946dSIlya Dryomov 
4265a02a946dSIlya Dryomov 	ret = decode_hoid(&p, end, m->begin);
4266a02a946dSIlya Dryomov 	if (ret) {
4267a02a946dSIlya Dryomov 		free_hoid(m->begin);
4268a02a946dSIlya Dryomov 		return ret;
4269a02a946dSIlya Dryomov 	}
4270a02a946dSIlya Dryomov 
4271a02a946dSIlya Dryomov 	m->end = kzalloc(sizeof(*m->end), GFP_NOIO);
4272a02a946dSIlya Dryomov 	if (!m->end) {
4273a02a946dSIlya Dryomov 		free_hoid(m->begin);
4274a02a946dSIlya Dryomov 		return -ENOMEM;
4275a02a946dSIlya Dryomov 	}
4276a02a946dSIlya Dryomov 
4277a02a946dSIlya Dryomov 	ret = decode_hoid(&p, end, m->end);
4278a02a946dSIlya Dryomov 	if (ret) {
4279a02a946dSIlya Dryomov 		free_hoid(m->begin);
4280a02a946dSIlya Dryomov 		free_hoid(m->end);
4281a02a946dSIlya Dryomov 		return ret;
4282a02a946dSIlya Dryomov 	}
4283a02a946dSIlya Dryomov 
4284a02a946dSIlya Dryomov 	return 0;
4285a02a946dSIlya Dryomov 
4286a02a946dSIlya Dryomov e_inval:
4287a02a946dSIlya Dryomov 	return -EINVAL;
4288a02a946dSIlya Dryomov }
4289a02a946dSIlya Dryomov 
4290a02a946dSIlya Dryomov static struct ceph_msg *create_backoff_message(
4291a02a946dSIlya Dryomov 				const struct ceph_osd_backoff *backoff,
4292a02a946dSIlya Dryomov 				u32 map_epoch)
4293a02a946dSIlya Dryomov {
4294a02a946dSIlya Dryomov 	struct ceph_msg *msg;
4295a02a946dSIlya Dryomov 	void *p, *end;
4296a02a946dSIlya Dryomov 	int msg_size;
4297a02a946dSIlya Dryomov 
4298a02a946dSIlya Dryomov 	msg_size = CEPH_ENCODING_START_BLK_LEN +
4299a02a946dSIlya Dryomov 			CEPH_PGID_ENCODING_LEN + 1; /* spgid */
4300a02a946dSIlya Dryomov 	msg_size += 4 + 1 + 8; /* map_epoch, op, id */
4301a02a946dSIlya Dryomov 	msg_size += CEPH_ENCODING_START_BLK_LEN +
4302a02a946dSIlya Dryomov 			hoid_encoding_size(backoff->begin);
4303a02a946dSIlya Dryomov 	msg_size += CEPH_ENCODING_START_BLK_LEN +
4304a02a946dSIlya Dryomov 			hoid_encoding_size(backoff->end);
4305a02a946dSIlya Dryomov 
4306a02a946dSIlya Dryomov 	msg = ceph_msg_new(CEPH_MSG_OSD_BACKOFF, msg_size, GFP_NOIO, true);
4307a02a946dSIlya Dryomov 	if (!msg)
4308a02a946dSIlya Dryomov 		return NULL;
4309a02a946dSIlya Dryomov 
4310a02a946dSIlya Dryomov 	p = msg->front.iov_base;
4311a02a946dSIlya Dryomov 	end = p + msg->front_alloc_len;
4312a02a946dSIlya Dryomov 
4313a02a946dSIlya Dryomov 	encode_spgid(&p, &backoff->spgid);
4314a02a946dSIlya Dryomov 	ceph_encode_32(&p, map_epoch);
4315a02a946dSIlya Dryomov 	ceph_encode_8(&p, CEPH_OSD_BACKOFF_OP_ACK_BLOCK);
4316a02a946dSIlya Dryomov 	ceph_encode_64(&p, backoff->id);
4317a02a946dSIlya Dryomov 	encode_hoid(&p, end, backoff->begin);
4318a02a946dSIlya Dryomov 	encode_hoid(&p, end, backoff->end);
4319a02a946dSIlya Dryomov 	BUG_ON(p != end);
4320a02a946dSIlya Dryomov 
4321a02a946dSIlya Dryomov 	msg->front.iov_len = p - msg->front.iov_base;
4322a02a946dSIlya Dryomov 	msg->hdr.version = cpu_to_le16(1); /* MOSDBackoff v1 */
4323a02a946dSIlya Dryomov 	msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
4324a02a946dSIlya Dryomov 
4325a02a946dSIlya Dryomov 	return msg;
4326a02a946dSIlya Dryomov }
4327a02a946dSIlya Dryomov 
4328a02a946dSIlya Dryomov static void handle_backoff_block(struct ceph_osd *osd, struct MOSDBackoff *m)
4329a02a946dSIlya Dryomov {
4330a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
4331a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
4332a02a946dSIlya Dryomov 	struct ceph_msg *msg;
4333a02a946dSIlya Dryomov 
4334a02a946dSIlya Dryomov 	dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4335a02a946dSIlya Dryomov 	     m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4336a02a946dSIlya Dryomov 
4337a02a946dSIlya Dryomov 	spg = lookup_spg_mapping(&osd->o_backoff_mappings, &m->spgid);
4338a02a946dSIlya Dryomov 	if (!spg) {
4339a02a946dSIlya Dryomov 		spg = alloc_spg_mapping();
4340a02a946dSIlya Dryomov 		if (!spg) {
4341a02a946dSIlya Dryomov 			pr_err("%s failed to allocate spg\n", __func__);
4342a02a946dSIlya Dryomov 			return;
4343a02a946dSIlya Dryomov 		}
4344a02a946dSIlya Dryomov 		spg->spgid = m->spgid; /* struct */
4345a02a946dSIlya Dryomov 		insert_spg_mapping(&osd->o_backoff_mappings, spg);
4346a02a946dSIlya Dryomov 	}
4347a02a946dSIlya Dryomov 
4348a02a946dSIlya Dryomov 	backoff = alloc_backoff();
4349a02a946dSIlya Dryomov 	if (!backoff) {
4350a02a946dSIlya Dryomov 		pr_err("%s failed to allocate backoff\n", __func__);
4351a02a946dSIlya Dryomov 		return;
4352a02a946dSIlya Dryomov 	}
4353a02a946dSIlya Dryomov 	backoff->spgid = m->spgid; /* struct */
4354a02a946dSIlya Dryomov 	backoff->id = m->id;
4355a02a946dSIlya Dryomov 	backoff->begin = m->begin;
4356a02a946dSIlya Dryomov 	m->begin = NULL; /* backoff now owns this */
4357a02a946dSIlya Dryomov 	backoff->end = m->end;
4358a02a946dSIlya Dryomov 	m->end = NULL;   /* ditto */
4359a02a946dSIlya Dryomov 
4360a02a946dSIlya Dryomov 	insert_backoff(&spg->backoffs, backoff);
4361a02a946dSIlya Dryomov 	insert_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4362a02a946dSIlya Dryomov 
4363a02a946dSIlya Dryomov 	/*
4364a02a946dSIlya Dryomov 	 * Ack with original backoff's epoch so that the OSD can
4365a02a946dSIlya Dryomov 	 * discard this if there was a PG split.
4366a02a946dSIlya Dryomov 	 */
4367a02a946dSIlya Dryomov 	msg = create_backoff_message(backoff, m->map_epoch);
4368a02a946dSIlya Dryomov 	if (!msg) {
4369a02a946dSIlya Dryomov 		pr_err("%s failed to allocate msg\n", __func__);
4370a02a946dSIlya Dryomov 		return;
4371a02a946dSIlya Dryomov 	}
4372a02a946dSIlya Dryomov 	ceph_con_send(&osd->o_con, msg);
4373a02a946dSIlya Dryomov }
4374a02a946dSIlya Dryomov 
4375a02a946dSIlya Dryomov static bool target_contained_by(const struct ceph_osd_request_target *t,
4376a02a946dSIlya Dryomov 				const struct ceph_hobject_id *begin,
4377a02a946dSIlya Dryomov 				const struct ceph_hobject_id *end)
4378a02a946dSIlya Dryomov {
4379a02a946dSIlya Dryomov 	struct ceph_hobject_id hoid;
4380a02a946dSIlya Dryomov 	int cmp;
4381a02a946dSIlya Dryomov 
4382a02a946dSIlya Dryomov 	hoid_fill_from_target(&hoid, t);
4383a02a946dSIlya Dryomov 	cmp = hoid_compare(&hoid, begin);
4384a02a946dSIlya Dryomov 	return !cmp || (cmp > 0 && hoid_compare(&hoid, end) < 0);
4385a02a946dSIlya Dryomov }
4386a02a946dSIlya Dryomov 
4387a02a946dSIlya Dryomov static void handle_backoff_unblock(struct ceph_osd *osd,
4388a02a946dSIlya Dryomov 				   const struct MOSDBackoff *m)
4389a02a946dSIlya Dryomov {
4390a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
4391a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
4392a02a946dSIlya Dryomov 	struct rb_node *n;
4393a02a946dSIlya Dryomov 
4394a02a946dSIlya Dryomov 	dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4395a02a946dSIlya Dryomov 	     m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4396a02a946dSIlya Dryomov 
4397a02a946dSIlya Dryomov 	backoff = lookup_backoff_by_id(&osd->o_backoffs_by_id, m->id);
4398a02a946dSIlya Dryomov 	if (!backoff) {
4399a02a946dSIlya Dryomov 		pr_err("%s osd%d spgid %llu.%xs%d id %llu backoff dne\n",
4400a02a946dSIlya Dryomov 		       __func__, osd->o_osd, m->spgid.pgid.pool,
4401a02a946dSIlya Dryomov 		       m->spgid.pgid.seed, m->spgid.shard, m->id);
4402a02a946dSIlya Dryomov 		return;
4403a02a946dSIlya Dryomov 	}
4404a02a946dSIlya Dryomov 
4405a02a946dSIlya Dryomov 	if (hoid_compare(backoff->begin, m->begin) &&
4406a02a946dSIlya Dryomov 	    hoid_compare(backoff->end, m->end)) {
4407a02a946dSIlya Dryomov 		pr_err("%s osd%d spgid %llu.%xs%d id %llu bad range?\n",
4408a02a946dSIlya Dryomov 		       __func__, osd->o_osd, m->spgid.pgid.pool,
4409a02a946dSIlya Dryomov 		       m->spgid.pgid.seed, m->spgid.shard, m->id);
4410a02a946dSIlya Dryomov 		/* unblock it anyway... */
4411a02a946dSIlya Dryomov 	}
4412a02a946dSIlya Dryomov 
4413a02a946dSIlya Dryomov 	spg = lookup_spg_mapping(&osd->o_backoff_mappings, &backoff->spgid);
4414a02a946dSIlya Dryomov 	BUG_ON(!spg);
4415a02a946dSIlya Dryomov 
4416a02a946dSIlya Dryomov 	erase_backoff(&spg->backoffs, backoff);
4417a02a946dSIlya Dryomov 	erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4418a02a946dSIlya Dryomov 	free_backoff(backoff);
4419a02a946dSIlya Dryomov 
4420a02a946dSIlya Dryomov 	if (RB_EMPTY_ROOT(&spg->backoffs)) {
4421a02a946dSIlya Dryomov 		erase_spg_mapping(&osd->o_backoff_mappings, spg);
4422a02a946dSIlya Dryomov 		free_spg_mapping(spg);
4423a02a946dSIlya Dryomov 	}
4424a02a946dSIlya Dryomov 
4425a02a946dSIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
4426a02a946dSIlya Dryomov 		struct ceph_osd_request *req =
4427a02a946dSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
4428a02a946dSIlya Dryomov 
4429a02a946dSIlya Dryomov 		if (!ceph_spg_compare(&req->r_t.spgid, &m->spgid)) {
4430a02a946dSIlya Dryomov 			/*
4431a02a946dSIlya Dryomov 			 * Match against @m, not @backoff -- the PG may
4432a02a946dSIlya Dryomov 			 * have split on the OSD.
4433a02a946dSIlya Dryomov 			 */
4434a02a946dSIlya Dryomov 			if (target_contained_by(&req->r_t, m->begin, m->end)) {
4435a02a946dSIlya Dryomov 				/*
4436a02a946dSIlya Dryomov 				 * If no other installed backoff applies,
4437a02a946dSIlya Dryomov 				 * resend.
4438a02a946dSIlya Dryomov 				 */
4439a02a946dSIlya Dryomov 				send_request(req);
4440a02a946dSIlya Dryomov 			}
4441a02a946dSIlya Dryomov 		}
4442a02a946dSIlya Dryomov 	}
4443a02a946dSIlya Dryomov }
4444a02a946dSIlya Dryomov 
4445a02a946dSIlya Dryomov static void handle_backoff(struct ceph_osd *osd, struct ceph_msg *msg)
4446a02a946dSIlya Dryomov {
4447a02a946dSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
4448a02a946dSIlya Dryomov 	struct MOSDBackoff m;
4449a02a946dSIlya Dryomov 	int ret;
4450a02a946dSIlya Dryomov 
4451a02a946dSIlya Dryomov 	down_read(&osdc->lock);
4452a02a946dSIlya Dryomov 	if (!osd_registered(osd)) {
4453a02a946dSIlya Dryomov 		dout("%s osd%d unknown\n", __func__, osd->o_osd);
4454a02a946dSIlya Dryomov 		up_read(&osdc->lock);
4455a02a946dSIlya Dryomov 		return;
4456a02a946dSIlya Dryomov 	}
4457a02a946dSIlya Dryomov 	WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
4458a02a946dSIlya Dryomov 
4459a02a946dSIlya Dryomov 	mutex_lock(&osd->lock);
4460a02a946dSIlya Dryomov 	ret = decode_MOSDBackoff(msg, &m);
4461a02a946dSIlya Dryomov 	if (ret) {
4462a02a946dSIlya Dryomov 		pr_err("failed to decode MOSDBackoff: %d\n", ret);
4463a02a946dSIlya Dryomov 		ceph_msg_dump(msg);
4464a02a946dSIlya Dryomov 		goto out_unlock;
4465a02a946dSIlya Dryomov 	}
4466a02a946dSIlya Dryomov 
4467a02a946dSIlya Dryomov 	switch (m.op) {
4468a02a946dSIlya Dryomov 	case CEPH_OSD_BACKOFF_OP_BLOCK:
4469a02a946dSIlya Dryomov 		handle_backoff_block(osd, &m);
4470a02a946dSIlya Dryomov 		break;
4471a02a946dSIlya Dryomov 	case CEPH_OSD_BACKOFF_OP_UNBLOCK:
4472a02a946dSIlya Dryomov 		handle_backoff_unblock(osd, &m);
4473a02a946dSIlya Dryomov 		break;
4474a02a946dSIlya Dryomov 	default:
4475a02a946dSIlya Dryomov 		pr_err("%s osd%d unknown op %d\n", __func__, osd->o_osd, m.op);
4476a02a946dSIlya Dryomov 	}
4477a02a946dSIlya Dryomov 
4478a02a946dSIlya Dryomov 	free_hoid(m.begin);
4479a02a946dSIlya Dryomov 	free_hoid(m.end);
4480a02a946dSIlya Dryomov 
4481a02a946dSIlya Dryomov out_unlock:
4482a02a946dSIlya Dryomov 	mutex_unlock(&osd->lock);
4483a02a946dSIlya Dryomov 	up_read(&osdc->lock);
4484a02a946dSIlya Dryomov }
4485a02a946dSIlya Dryomov 
44863d14c5d2SYehuda Sadeh /*
4487a40c4f10SYehuda Sadeh  * Process osd watch notifications
4488a40c4f10SYehuda Sadeh  */
44893c663bbdSAlex Elder static void handle_watch_notify(struct ceph_osd_client *osdc,
44903c663bbdSAlex Elder 				struct ceph_msg *msg)
4491a40c4f10SYehuda Sadeh {
4492922dab61SIlya Dryomov 	void *p = msg->front.iov_base;
4493922dab61SIlya Dryomov 	void *const end = p + msg->front.iov_len;
4494922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
4495922dab61SIlya Dryomov 	struct linger_work *lwork;
4496922dab61SIlya Dryomov 	u8 proto_ver, opcode;
4497922dab61SIlya Dryomov 	u64 cookie, notify_id;
4498922dab61SIlya Dryomov 	u64 notifier_id = 0;
449919079203SIlya Dryomov 	s32 return_code = 0;
4500922dab61SIlya Dryomov 	void *payload = NULL;
4501922dab61SIlya Dryomov 	u32 payload_len = 0;
4502a40c4f10SYehuda Sadeh 
4503a40c4f10SYehuda Sadeh 	ceph_decode_8_safe(&p, end, proto_ver, bad);
4504a40c4f10SYehuda Sadeh 	ceph_decode_8_safe(&p, end, opcode, bad);
4505a40c4f10SYehuda Sadeh 	ceph_decode_64_safe(&p, end, cookie, bad);
4506922dab61SIlya Dryomov 	p += 8; /* skip ver */
4507a40c4f10SYehuda Sadeh 	ceph_decode_64_safe(&p, end, notify_id, bad);
4508a40c4f10SYehuda Sadeh 
4509922dab61SIlya Dryomov 	if (proto_ver >= 1) {
4510922dab61SIlya Dryomov 		ceph_decode_32_safe(&p, end, payload_len, bad);
4511922dab61SIlya Dryomov 		ceph_decode_need(&p, end, payload_len, bad);
4512922dab61SIlya Dryomov 		payload = p;
4513922dab61SIlya Dryomov 		p += payload_len;
4514a40c4f10SYehuda Sadeh 	}
4515a40c4f10SYehuda Sadeh 
4516922dab61SIlya Dryomov 	if (le16_to_cpu(msg->hdr.version) >= 2)
451719079203SIlya Dryomov 		ceph_decode_32_safe(&p, end, return_code, bad);
4518922dab61SIlya Dryomov 
4519922dab61SIlya Dryomov 	if (le16_to_cpu(msg->hdr.version) >= 3)
4520922dab61SIlya Dryomov 		ceph_decode_64_safe(&p, end, notifier_id, bad);
4521922dab61SIlya Dryomov 
4522922dab61SIlya Dryomov 	down_read(&osdc->lock);
4523922dab61SIlya Dryomov 	lreq = lookup_linger_osdc(&osdc->linger_requests, cookie);
4524922dab61SIlya Dryomov 	if (!lreq) {
4525922dab61SIlya Dryomov 		dout("%s opcode %d cookie %llu dne\n", __func__, opcode,
4526922dab61SIlya Dryomov 		     cookie);
4527922dab61SIlya Dryomov 		goto out_unlock_osdc;
4528922dab61SIlya Dryomov 	}
4529922dab61SIlya Dryomov 
4530922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
453119079203SIlya Dryomov 	dout("%s opcode %d cookie %llu lreq %p is_watch %d\n", __func__,
453219079203SIlya Dryomov 	     opcode, cookie, lreq, lreq->is_watch);
4533922dab61SIlya Dryomov 	if (opcode == CEPH_WATCH_EVENT_DISCONNECT) {
4534922dab61SIlya Dryomov 		if (!lreq->last_error) {
4535922dab61SIlya Dryomov 			lreq->last_error = -ENOTCONN;
4536922dab61SIlya Dryomov 			queue_watch_error(lreq);
4537922dab61SIlya Dryomov 		}
453819079203SIlya Dryomov 	} else if (!lreq->is_watch) {
453919079203SIlya Dryomov 		/* CEPH_WATCH_EVENT_NOTIFY_COMPLETE */
454019079203SIlya Dryomov 		if (lreq->notify_id && lreq->notify_id != notify_id) {
454119079203SIlya Dryomov 			dout("lreq %p notify_id %llu != %llu, ignoring\n", lreq,
454219079203SIlya Dryomov 			     lreq->notify_id, notify_id);
454319079203SIlya Dryomov 		} else if (!completion_done(&lreq->notify_finish_wait)) {
454419079203SIlya Dryomov 			struct ceph_msg_data *data =
45450d9c1ab3SIlya Dryomov 			    msg->num_data_items ? &msg->data[0] : NULL;
454619079203SIlya Dryomov 
454719079203SIlya Dryomov 			if (data) {
454819079203SIlya Dryomov 				if (lreq->preply_pages) {
454919079203SIlya Dryomov 					WARN_ON(data->type !=
455019079203SIlya Dryomov 							CEPH_MSG_DATA_PAGES);
455119079203SIlya Dryomov 					*lreq->preply_pages = data->pages;
455219079203SIlya Dryomov 					*lreq->preply_len = data->length;
4553e8862740SIlya Dryomov 					data->own_pages = false;
455419079203SIlya Dryomov 				}
455519079203SIlya Dryomov 			}
455619079203SIlya Dryomov 			lreq->notify_finish_error = return_code;
455719079203SIlya Dryomov 			complete_all(&lreq->notify_finish_wait);
455819079203SIlya Dryomov 		}
4559922dab61SIlya Dryomov 	} else {
4560922dab61SIlya Dryomov 		/* CEPH_WATCH_EVENT_NOTIFY */
4561922dab61SIlya Dryomov 		lwork = lwork_alloc(lreq, do_watch_notify);
4562922dab61SIlya Dryomov 		if (!lwork) {
4563922dab61SIlya Dryomov 			pr_err("failed to allocate notify-lwork\n");
4564922dab61SIlya Dryomov 			goto out_unlock_lreq;
4565922dab61SIlya Dryomov 		}
4566922dab61SIlya Dryomov 
4567922dab61SIlya Dryomov 		lwork->notify.notify_id = notify_id;
4568922dab61SIlya Dryomov 		lwork->notify.notifier_id = notifier_id;
4569922dab61SIlya Dryomov 		lwork->notify.payload = payload;
4570922dab61SIlya Dryomov 		lwork->notify.payload_len = payload_len;
4571922dab61SIlya Dryomov 		lwork->notify.msg = ceph_msg_get(msg);
4572922dab61SIlya Dryomov 		lwork_queue(lwork);
4573922dab61SIlya Dryomov 	}
4574922dab61SIlya Dryomov 
4575922dab61SIlya Dryomov out_unlock_lreq:
4576922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
4577922dab61SIlya Dryomov out_unlock_osdc:
4578922dab61SIlya Dryomov 	up_read(&osdc->lock);
4579a40c4f10SYehuda Sadeh 	return;
4580a40c4f10SYehuda Sadeh 
4581a40c4f10SYehuda Sadeh bad:
4582a40c4f10SYehuda Sadeh 	pr_err("osdc handle_watch_notify corrupt msg\n");
4583a40c4f10SYehuda Sadeh }
4584a40c4f10SYehuda Sadeh 
4585a40c4f10SYehuda Sadeh /*
45863d14c5d2SYehuda Sadeh  * Register request, send initial attempt.
45873d14c5d2SYehuda Sadeh  */
4588a8af0d68SJeff Layton void ceph_osdc_start_request(struct ceph_osd_client *osdc,
4589a8af0d68SJeff Layton 			     struct ceph_osd_request *req)
45903d14c5d2SYehuda Sadeh {
45915aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
45925aea3dcdSIlya Dryomov 	submit_request(req, false);
45935aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
45943d14c5d2SYehuda Sadeh }
45953d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_start_request);
45963d14c5d2SYehuda Sadeh 
45973d14c5d2SYehuda Sadeh /*
4598d0bb883cSIlya Dryomov  * Unregister request.  If @req was registered, it isn't completed:
4599d0bb883cSIlya Dryomov  * r_result isn't set and __complete_request() isn't invoked.
4600d0bb883cSIlya Dryomov  *
4601d0bb883cSIlya Dryomov  * If @req wasn't registered, this call may have raced with
4602d0bb883cSIlya Dryomov  * handle_reply(), in which case r_result would already be set and
4603d0bb883cSIlya Dryomov  * __complete_request() would be getting invoked, possibly even
4604d0bb883cSIlya Dryomov  * concurrently with this call.
4605c9f9b93dSIlya Dryomov  */
4606c9f9b93dSIlya Dryomov void ceph_osdc_cancel_request(struct ceph_osd_request *req)
4607c9f9b93dSIlya Dryomov {
4608c9f9b93dSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
4609c9f9b93dSIlya Dryomov 
46105aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
46115aea3dcdSIlya Dryomov 	if (req->r_osd)
46125aea3dcdSIlya Dryomov 		cancel_request(req);
46135aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
4614c9f9b93dSIlya Dryomov }
4615c9f9b93dSIlya Dryomov EXPORT_SYMBOL(ceph_osdc_cancel_request);
4616c9f9b93dSIlya Dryomov 
4617c9f9b93dSIlya Dryomov /*
461842b06965SIlya Dryomov  * @timeout: in jiffies, 0 means "wait forever"
461942b06965SIlya Dryomov  */
462042b06965SIlya Dryomov static int wait_request_timeout(struct ceph_osd_request *req,
462142b06965SIlya Dryomov 				unsigned long timeout)
462242b06965SIlya Dryomov {
462342b06965SIlya Dryomov 	long left;
462442b06965SIlya Dryomov 
462542b06965SIlya Dryomov 	dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
46260e76abf2SYan, Zheng 	left = wait_for_completion_killable_timeout(&req->r_completion,
462742b06965SIlya Dryomov 						ceph_timeout_jiffies(timeout));
462842b06965SIlya Dryomov 	if (left <= 0) {
462942b06965SIlya Dryomov 		left = left ?: -ETIMEDOUT;
463042b06965SIlya Dryomov 		ceph_osdc_cancel_request(req);
463142b06965SIlya Dryomov 	} else {
463242b06965SIlya Dryomov 		left = req->r_result; /* completed */
463342b06965SIlya Dryomov 	}
463442b06965SIlya Dryomov 
463542b06965SIlya Dryomov 	return left;
463642b06965SIlya Dryomov }
463742b06965SIlya Dryomov 
463842b06965SIlya Dryomov /*
46393d14c5d2SYehuda Sadeh  * wait for a request to complete
46403d14c5d2SYehuda Sadeh  */
46413d14c5d2SYehuda Sadeh int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
46423d14c5d2SYehuda Sadeh 			   struct ceph_osd_request *req)
46433d14c5d2SYehuda Sadeh {
464442b06965SIlya Dryomov 	return wait_request_timeout(req, 0);
46453d14c5d2SYehuda Sadeh }
46463d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_wait_request);
46473d14c5d2SYehuda Sadeh 
46483d14c5d2SYehuda Sadeh /*
46493d14c5d2SYehuda Sadeh  * sync - wait for all in-flight requests to flush.  avoid starvation.
46503d14c5d2SYehuda Sadeh  */
46513d14c5d2SYehuda Sadeh void ceph_osdc_sync(struct ceph_osd_client *osdc)
46523d14c5d2SYehuda Sadeh {
46535aea3dcdSIlya Dryomov 	struct rb_node *n, *p;
46545aea3dcdSIlya Dryomov 	u64 last_tid = atomic64_read(&osdc->last_tid);
46553d14c5d2SYehuda Sadeh 
46565aea3dcdSIlya Dryomov again:
46575aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
46585aea3dcdSIlya Dryomov 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
46595aea3dcdSIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
46605aea3dcdSIlya Dryomov 
46615aea3dcdSIlya Dryomov 		mutex_lock(&osd->lock);
46625aea3dcdSIlya Dryomov 		for (p = rb_first(&osd->o_requests); p; p = rb_next(p)) {
46635aea3dcdSIlya Dryomov 			struct ceph_osd_request *req =
46645aea3dcdSIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
46655aea3dcdSIlya Dryomov 
46663d14c5d2SYehuda Sadeh 			if (req->r_tid > last_tid)
46673d14c5d2SYehuda Sadeh 				break;
46683d14c5d2SYehuda Sadeh 
46695aea3dcdSIlya Dryomov 			if (!(req->r_flags & CEPH_OSD_FLAG_WRITE))
46703d14c5d2SYehuda Sadeh 				continue;
46713d14c5d2SYehuda Sadeh 
46723d14c5d2SYehuda Sadeh 			ceph_osdc_get_request(req);
46735aea3dcdSIlya Dryomov 			mutex_unlock(&osd->lock);
46745aea3dcdSIlya Dryomov 			up_read(&osdc->lock);
46755aea3dcdSIlya Dryomov 			dout("%s waiting on req %p tid %llu last_tid %llu\n",
46765aea3dcdSIlya Dryomov 			     __func__, req, req->r_tid, last_tid);
4677b18b9550SIlya Dryomov 			wait_for_completion(&req->r_completion);
46783d14c5d2SYehuda Sadeh 			ceph_osdc_put_request(req);
46795aea3dcdSIlya Dryomov 			goto again;
46803d14c5d2SYehuda Sadeh 		}
46815aea3dcdSIlya Dryomov 
46825aea3dcdSIlya Dryomov 		mutex_unlock(&osd->lock);
46835aea3dcdSIlya Dryomov 	}
46845aea3dcdSIlya Dryomov 
46855aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
46865aea3dcdSIlya Dryomov 	dout("%s done last_tid %llu\n", __func__, last_tid);
46873d14c5d2SYehuda Sadeh }
46883d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_sync);
46893d14c5d2SYehuda Sadeh 
4690922dab61SIlya Dryomov /*
4691922dab61SIlya Dryomov  * Returns a handle, caller owns a ref.
4692922dab61SIlya Dryomov  */
4693922dab61SIlya Dryomov struct ceph_osd_linger_request *
4694922dab61SIlya Dryomov ceph_osdc_watch(struct ceph_osd_client *osdc,
4695922dab61SIlya Dryomov 		struct ceph_object_id *oid,
4696922dab61SIlya Dryomov 		struct ceph_object_locator *oloc,
4697922dab61SIlya Dryomov 		rados_watchcb2_t wcb,
4698922dab61SIlya Dryomov 		rados_watcherrcb_t errcb,
4699922dab61SIlya Dryomov 		void *data)
4700922dab61SIlya Dryomov {
4701922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
4702922dab61SIlya Dryomov 	int ret;
4703922dab61SIlya Dryomov 
4704922dab61SIlya Dryomov 	lreq = linger_alloc(osdc);
4705922dab61SIlya Dryomov 	if (!lreq)
4706922dab61SIlya Dryomov 		return ERR_PTR(-ENOMEM);
4707922dab61SIlya Dryomov 
470819079203SIlya Dryomov 	lreq->is_watch = true;
4709922dab61SIlya Dryomov 	lreq->wcb = wcb;
4710922dab61SIlya Dryomov 	lreq->errcb = errcb;
4711922dab61SIlya Dryomov 	lreq->data = data;
4712b07d3c4bSIlya Dryomov 	lreq->watch_valid_thru = jiffies;
4713922dab61SIlya Dryomov 
4714922dab61SIlya Dryomov 	ceph_oid_copy(&lreq->t.base_oid, oid);
4715922dab61SIlya Dryomov 	ceph_oloc_copy(&lreq->t.base_oloc, oloc);
471654ea0046SIlya Dryomov 	lreq->t.flags = CEPH_OSD_FLAG_WRITE;
4717fac02ddfSArnd Bergmann 	ktime_get_real_ts64(&lreq->mtime);
4718922dab61SIlya Dryomov 
471981c65213SIlya Dryomov 	linger_submit(lreq);
4720922dab61SIlya Dryomov 	ret = linger_reg_commit_wait(lreq);
4721922dab61SIlya Dryomov 	if (ret) {
4722922dab61SIlya Dryomov 		linger_cancel(lreq);
4723922dab61SIlya Dryomov 		goto err_put_lreq;
4724922dab61SIlya Dryomov 	}
4725922dab61SIlya Dryomov 
4726922dab61SIlya Dryomov 	return lreq;
4727922dab61SIlya Dryomov 
4728922dab61SIlya Dryomov err_put_lreq:
4729922dab61SIlya Dryomov 	linger_put(lreq);
4730922dab61SIlya Dryomov 	return ERR_PTR(ret);
4731922dab61SIlya Dryomov }
4732922dab61SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_watch);
4733922dab61SIlya Dryomov 
4734922dab61SIlya Dryomov /*
4735922dab61SIlya Dryomov  * Releases a ref.
4736922dab61SIlya Dryomov  *
4737922dab61SIlya Dryomov  * Times out after mount_timeout to preserve rbd unmap behaviour
4738922dab61SIlya Dryomov  * introduced in 2894e1d76974 ("rbd: timeout watch teardown on unmap
4739922dab61SIlya Dryomov  * with mount_timeout").
4740922dab61SIlya Dryomov  */
4741922dab61SIlya Dryomov int ceph_osdc_unwatch(struct ceph_osd_client *osdc,
4742922dab61SIlya Dryomov 		      struct ceph_osd_linger_request *lreq)
4743922dab61SIlya Dryomov {
4744922dab61SIlya Dryomov 	struct ceph_options *opts = osdc->client->options;
4745922dab61SIlya Dryomov 	struct ceph_osd_request *req;
4746922dab61SIlya Dryomov 	int ret;
4747922dab61SIlya Dryomov 
4748922dab61SIlya Dryomov 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4749922dab61SIlya Dryomov 	if (!req)
4750922dab61SIlya Dryomov 		return -ENOMEM;
4751922dab61SIlya Dryomov 
4752922dab61SIlya Dryomov 	ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
4753922dab61SIlya Dryomov 	ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
475454ea0046SIlya Dryomov 	req->r_flags = CEPH_OSD_FLAG_WRITE;
4755fac02ddfSArnd Bergmann 	ktime_get_real_ts64(&req->r_mtime);
475675dbb685SIlya Dryomov 	osd_req_op_watch_init(req, 0, CEPH_OSD_WATCH_OP_UNWATCH,
475775dbb685SIlya Dryomov 			      lreq->linger_id, 0);
4758922dab61SIlya Dryomov 
4759922dab61SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4760922dab61SIlya Dryomov 	if (ret)
4761922dab61SIlya Dryomov 		goto out_put_req;
4762922dab61SIlya Dryomov 
4763a8af0d68SJeff Layton 	ceph_osdc_start_request(osdc, req);
4764922dab61SIlya Dryomov 	linger_cancel(lreq);
4765922dab61SIlya Dryomov 	linger_put(lreq);
4766922dab61SIlya Dryomov 	ret = wait_request_timeout(req, opts->mount_timeout);
4767922dab61SIlya Dryomov 
4768922dab61SIlya Dryomov out_put_req:
4769922dab61SIlya Dryomov 	ceph_osdc_put_request(req);
4770922dab61SIlya Dryomov 	return ret;
4771922dab61SIlya Dryomov }
4772922dab61SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_unwatch);
4773922dab61SIlya Dryomov 
4774922dab61SIlya Dryomov static int osd_req_op_notify_ack_init(struct ceph_osd_request *req, int which,
4775922dab61SIlya Dryomov 				      u64 notify_id, u64 cookie, void *payload,
47766d54228fSIlya Dryomov 				      u32 payload_len)
4777922dab61SIlya Dryomov {
4778922dab61SIlya Dryomov 	struct ceph_osd_req_op *op;
4779922dab61SIlya Dryomov 	struct ceph_pagelist *pl;
4780922dab61SIlya Dryomov 	int ret;
4781922dab61SIlya Dryomov 
4782042f6498SJeff Layton 	op = osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY_ACK, 0);
4783922dab61SIlya Dryomov 
478433165d47SIlya Dryomov 	pl = ceph_pagelist_alloc(GFP_NOIO);
4785922dab61SIlya Dryomov 	if (!pl)
4786922dab61SIlya Dryomov 		return -ENOMEM;
4787922dab61SIlya Dryomov 
4788922dab61SIlya Dryomov 	ret = ceph_pagelist_encode_64(pl, notify_id);
4789922dab61SIlya Dryomov 	ret |= ceph_pagelist_encode_64(pl, cookie);
4790922dab61SIlya Dryomov 	if (payload) {
4791922dab61SIlya Dryomov 		ret |= ceph_pagelist_encode_32(pl, payload_len);
4792922dab61SIlya Dryomov 		ret |= ceph_pagelist_append(pl, payload, payload_len);
4793922dab61SIlya Dryomov 	} else {
4794922dab61SIlya Dryomov 		ret |= ceph_pagelist_encode_32(pl, 0);
4795922dab61SIlya Dryomov 	}
4796922dab61SIlya Dryomov 	if (ret) {
4797922dab61SIlya Dryomov 		ceph_pagelist_release(pl);
4798922dab61SIlya Dryomov 		return -ENOMEM;
4799922dab61SIlya Dryomov 	}
4800922dab61SIlya Dryomov 
4801922dab61SIlya Dryomov 	ceph_osd_data_pagelist_init(&op->notify_ack.request_data, pl);
4802922dab61SIlya Dryomov 	op->indata_len = pl->length;
4803922dab61SIlya Dryomov 	return 0;
4804922dab61SIlya Dryomov }
4805922dab61SIlya Dryomov 
4806922dab61SIlya Dryomov int ceph_osdc_notify_ack(struct ceph_osd_client *osdc,
4807922dab61SIlya Dryomov 			 struct ceph_object_id *oid,
4808922dab61SIlya Dryomov 			 struct ceph_object_locator *oloc,
4809922dab61SIlya Dryomov 			 u64 notify_id,
4810922dab61SIlya Dryomov 			 u64 cookie,
4811922dab61SIlya Dryomov 			 void *payload,
48126d54228fSIlya Dryomov 			 u32 payload_len)
4813922dab61SIlya Dryomov {
4814922dab61SIlya Dryomov 	struct ceph_osd_request *req;
4815922dab61SIlya Dryomov 	int ret;
4816922dab61SIlya Dryomov 
4817922dab61SIlya Dryomov 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4818922dab61SIlya Dryomov 	if (!req)
4819922dab61SIlya Dryomov 		return -ENOMEM;
4820922dab61SIlya Dryomov 
4821922dab61SIlya Dryomov 	ceph_oid_copy(&req->r_base_oid, oid);
4822922dab61SIlya Dryomov 	ceph_oloc_copy(&req->r_base_oloc, oloc);
4823922dab61SIlya Dryomov 	req->r_flags = CEPH_OSD_FLAG_READ;
4824922dab61SIlya Dryomov 
482526f887e0SIlya Dryomov 	ret = osd_req_op_notify_ack_init(req, 0, notify_id, cookie, payload,
482626f887e0SIlya Dryomov 					 payload_len);
4827922dab61SIlya Dryomov 	if (ret)
4828922dab61SIlya Dryomov 		goto out_put_req;
4829922dab61SIlya Dryomov 
483026f887e0SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4831922dab61SIlya Dryomov 	if (ret)
4832922dab61SIlya Dryomov 		goto out_put_req;
4833922dab61SIlya Dryomov 
4834a8af0d68SJeff Layton 	ceph_osdc_start_request(osdc, req);
4835922dab61SIlya Dryomov 	ret = ceph_osdc_wait_request(osdc, req);
4836922dab61SIlya Dryomov 
4837922dab61SIlya Dryomov out_put_req:
4838922dab61SIlya Dryomov 	ceph_osdc_put_request(req);
4839922dab61SIlya Dryomov 	return ret;
4840922dab61SIlya Dryomov }
4841922dab61SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_notify_ack);
4842922dab61SIlya Dryomov 
484319079203SIlya Dryomov /*
484419079203SIlya Dryomov  * @timeout: in seconds
484519079203SIlya Dryomov  *
484619079203SIlya Dryomov  * @preply_{pages,len} are initialized both on success and error.
484719079203SIlya Dryomov  * The caller is responsible for:
484819079203SIlya Dryomov  *
484919079203SIlya Dryomov  *     ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len))
485019079203SIlya Dryomov  */
485119079203SIlya Dryomov int ceph_osdc_notify(struct ceph_osd_client *osdc,
485219079203SIlya Dryomov 		     struct ceph_object_id *oid,
485319079203SIlya Dryomov 		     struct ceph_object_locator *oloc,
485419079203SIlya Dryomov 		     void *payload,
48556d54228fSIlya Dryomov 		     u32 payload_len,
485619079203SIlya Dryomov 		     u32 timeout,
485719079203SIlya Dryomov 		     struct page ***preply_pages,
485819079203SIlya Dryomov 		     size_t *preply_len)
485919079203SIlya Dryomov {
486019079203SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
486119079203SIlya Dryomov 	int ret;
486219079203SIlya Dryomov 
486319079203SIlya Dryomov 	WARN_ON(!timeout);
486419079203SIlya Dryomov 	if (preply_pages) {
486519079203SIlya Dryomov 		*preply_pages = NULL;
486619079203SIlya Dryomov 		*preply_len = 0;
486719079203SIlya Dryomov 	}
486819079203SIlya Dryomov 
486919079203SIlya Dryomov 	lreq = linger_alloc(osdc);
487019079203SIlya Dryomov 	if (!lreq)
487119079203SIlya Dryomov 		return -ENOMEM;
487219079203SIlya Dryomov 
487375dbb685SIlya Dryomov 	lreq->request_pl = ceph_pagelist_alloc(GFP_NOIO);
487475dbb685SIlya Dryomov 	if (!lreq->request_pl) {
487575dbb685SIlya Dryomov 		ret = -ENOMEM;
487675dbb685SIlya Dryomov 		goto out_put_lreq;
487775dbb685SIlya Dryomov 	}
487875dbb685SIlya Dryomov 
487975dbb685SIlya Dryomov 	ret = ceph_pagelist_encode_32(lreq->request_pl, 1); /* prot_ver */
488075dbb685SIlya Dryomov 	ret |= ceph_pagelist_encode_32(lreq->request_pl, timeout);
488175dbb685SIlya Dryomov 	ret |= ceph_pagelist_encode_32(lreq->request_pl, payload_len);
488275dbb685SIlya Dryomov 	ret |= ceph_pagelist_append(lreq->request_pl, payload, payload_len);
488375dbb685SIlya Dryomov 	if (ret) {
488475dbb685SIlya Dryomov 		ret = -ENOMEM;
488575dbb685SIlya Dryomov 		goto out_put_lreq;
488675dbb685SIlya Dryomov 	}
488775dbb685SIlya Dryomov 
488875dbb685SIlya Dryomov 	/* for notify_id */
488975dbb685SIlya Dryomov 	lreq->notify_id_pages = ceph_alloc_page_vector(1, GFP_NOIO);
489075dbb685SIlya Dryomov 	if (IS_ERR(lreq->notify_id_pages)) {
489175dbb685SIlya Dryomov 		ret = PTR_ERR(lreq->notify_id_pages);
489275dbb685SIlya Dryomov 		lreq->notify_id_pages = NULL;
489375dbb685SIlya Dryomov 		goto out_put_lreq;
489475dbb685SIlya Dryomov 	}
489575dbb685SIlya Dryomov 
489619079203SIlya Dryomov 	lreq->preply_pages = preply_pages;
489719079203SIlya Dryomov 	lreq->preply_len = preply_len;
489819079203SIlya Dryomov 
489919079203SIlya Dryomov 	ceph_oid_copy(&lreq->t.base_oid, oid);
490019079203SIlya Dryomov 	ceph_oloc_copy(&lreq->t.base_oloc, oloc);
490119079203SIlya Dryomov 	lreq->t.flags = CEPH_OSD_FLAG_READ;
490219079203SIlya Dryomov 
490381c65213SIlya Dryomov 	linger_submit(lreq);
490419079203SIlya Dryomov 	ret = linger_reg_commit_wait(lreq);
490519079203SIlya Dryomov 	if (!ret)
4906*e6e28432SIlya Dryomov 		ret = linger_notify_finish_wait(lreq,
4907*e6e28432SIlya Dryomov 				 msecs_to_jiffies(2 * timeout * MSEC_PER_SEC));
490819079203SIlya Dryomov 	else
490919079203SIlya Dryomov 		dout("lreq %p failed to initiate notify %d\n", lreq, ret);
491019079203SIlya Dryomov 
491119079203SIlya Dryomov 	linger_cancel(lreq);
491219079203SIlya Dryomov out_put_lreq:
491319079203SIlya Dryomov 	linger_put(lreq);
491419079203SIlya Dryomov 	return ret;
491519079203SIlya Dryomov }
491619079203SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_notify);
491719079203SIlya Dryomov 
49183d14c5d2SYehuda Sadeh /*
4919b07d3c4bSIlya Dryomov  * Return the number of milliseconds since the watch was last
4920b07d3c4bSIlya Dryomov  * confirmed, or an error.  If there is an error, the watch is no
4921b07d3c4bSIlya Dryomov  * longer valid, and should be destroyed with ceph_osdc_unwatch().
4922b07d3c4bSIlya Dryomov  */
4923b07d3c4bSIlya Dryomov int ceph_osdc_watch_check(struct ceph_osd_client *osdc,
4924b07d3c4bSIlya Dryomov 			  struct ceph_osd_linger_request *lreq)
4925b07d3c4bSIlya Dryomov {
4926b07d3c4bSIlya Dryomov 	unsigned long stamp, age;
4927b07d3c4bSIlya Dryomov 	int ret;
4928b07d3c4bSIlya Dryomov 
4929b07d3c4bSIlya Dryomov 	down_read(&osdc->lock);
4930b07d3c4bSIlya Dryomov 	mutex_lock(&lreq->lock);
4931b07d3c4bSIlya Dryomov 	stamp = lreq->watch_valid_thru;
4932b07d3c4bSIlya Dryomov 	if (!list_empty(&lreq->pending_lworks)) {
4933b07d3c4bSIlya Dryomov 		struct linger_work *lwork =
4934b07d3c4bSIlya Dryomov 		    list_first_entry(&lreq->pending_lworks,
4935b07d3c4bSIlya Dryomov 				     struct linger_work,
4936b07d3c4bSIlya Dryomov 				     pending_item);
4937b07d3c4bSIlya Dryomov 
4938b07d3c4bSIlya Dryomov 		if (time_before(lwork->queued_stamp, stamp))
4939b07d3c4bSIlya Dryomov 			stamp = lwork->queued_stamp;
4940b07d3c4bSIlya Dryomov 	}
4941b07d3c4bSIlya Dryomov 	age = jiffies - stamp;
4942b07d3c4bSIlya Dryomov 	dout("%s lreq %p linger_id %llu age %lu last_error %d\n", __func__,
4943b07d3c4bSIlya Dryomov 	     lreq, lreq->linger_id, age, lreq->last_error);
4944b07d3c4bSIlya Dryomov 	/* we are truncating to msecs, so return a safe upper bound */
4945b07d3c4bSIlya Dryomov 	ret = lreq->last_error ?: 1 + jiffies_to_msecs(age);
4946b07d3c4bSIlya Dryomov 
4947b07d3c4bSIlya Dryomov 	mutex_unlock(&lreq->lock);
4948b07d3c4bSIlya Dryomov 	up_read(&osdc->lock);
4949b07d3c4bSIlya Dryomov 	return ret;
4950b07d3c4bSIlya Dryomov }
4951b07d3c4bSIlya Dryomov 
4952a4ed38d7SDouglas Fuller static int decode_watcher(void **p, void *end, struct ceph_watch_item *item)
4953a4ed38d7SDouglas Fuller {
4954a4ed38d7SDouglas Fuller 	u8 struct_v;
4955a4ed38d7SDouglas Fuller 	u32 struct_len;
4956a4ed38d7SDouglas Fuller 	int ret;
4957a4ed38d7SDouglas Fuller 
4958a4ed38d7SDouglas Fuller 	ret = ceph_start_decoding(p, end, 2, "watch_item_t",
4959a4ed38d7SDouglas Fuller 				  &struct_v, &struct_len);
4960a4ed38d7SDouglas Fuller 	if (ret)
496151fc7ab4SJeff Layton 		goto bad;
4962a4ed38d7SDouglas Fuller 
496351fc7ab4SJeff Layton 	ret = -EINVAL;
496451fc7ab4SJeff Layton 	ceph_decode_copy_safe(p, end, &item->name, sizeof(item->name), bad);
496551fc7ab4SJeff Layton 	ceph_decode_64_safe(p, end, item->cookie, bad);
496651fc7ab4SJeff Layton 	ceph_decode_skip_32(p, end, bad); /* skip timeout seconds */
496751fc7ab4SJeff Layton 
4968a4ed38d7SDouglas Fuller 	if (struct_v >= 2) {
496951fc7ab4SJeff Layton 		ret = ceph_decode_entity_addr(p, end, &item->addr);
497051fc7ab4SJeff Layton 		if (ret)
497151fc7ab4SJeff Layton 			goto bad;
497251fc7ab4SJeff Layton 	} else {
497351fc7ab4SJeff Layton 		ret = 0;
4974a4ed38d7SDouglas Fuller 	}
4975a4ed38d7SDouglas Fuller 
4976a4ed38d7SDouglas Fuller 	dout("%s %s%llu cookie %llu addr %s\n", __func__,
4977a4ed38d7SDouglas Fuller 	     ENTITY_NAME(item->name), item->cookie,
4978b726ec97SJeff Layton 	     ceph_pr_addr(&item->addr));
497951fc7ab4SJeff Layton bad:
498051fc7ab4SJeff Layton 	return ret;
4981a4ed38d7SDouglas Fuller }
4982a4ed38d7SDouglas Fuller 
4983a4ed38d7SDouglas Fuller static int decode_watchers(void **p, void *end,
4984a4ed38d7SDouglas Fuller 			   struct ceph_watch_item **watchers,
4985a4ed38d7SDouglas Fuller 			   u32 *num_watchers)
4986a4ed38d7SDouglas Fuller {
4987a4ed38d7SDouglas Fuller 	u8 struct_v;
4988a4ed38d7SDouglas Fuller 	u32 struct_len;
4989a4ed38d7SDouglas Fuller 	int i;
4990a4ed38d7SDouglas Fuller 	int ret;
4991a4ed38d7SDouglas Fuller 
4992a4ed38d7SDouglas Fuller 	ret = ceph_start_decoding(p, end, 1, "obj_list_watch_response_t",
4993a4ed38d7SDouglas Fuller 				  &struct_v, &struct_len);
4994a4ed38d7SDouglas Fuller 	if (ret)
4995a4ed38d7SDouglas Fuller 		return ret;
4996a4ed38d7SDouglas Fuller 
4997a4ed38d7SDouglas Fuller 	*num_watchers = ceph_decode_32(p);
4998a4ed38d7SDouglas Fuller 	*watchers = kcalloc(*num_watchers, sizeof(**watchers), GFP_NOIO);
4999a4ed38d7SDouglas Fuller 	if (!*watchers)
5000a4ed38d7SDouglas Fuller 		return -ENOMEM;
5001a4ed38d7SDouglas Fuller 
5002a4ed38d7SDouglas Fuller 	for (i = 0; i < *num_watchers; i++) {
5003a4ed38d7SDouglas Fuller 		ret = decode_watcher(p, end, *watchers + i);
5004a4ed38d7SDouglas Fuller 		if (ret) {
5005a4ed38d7SDouglas Fuller 			kfree(*watchers);
5006a4ed38d7SDouglas Fuller 			return ret;
5007a4ed38d7SDouglas Fuller 		}
5008a4ed38d7SDouglas Fuller 	}
5009a4ed38d7SDouglas Fuller 
5010a4ed38d7SDouglas Fuller 	return 0;
5011a4ed38d7SDouglas Fuller }
5012a4ed38d7SDouglas Fuller 
5013a4ed38d7SDouglas Fuller /*
5014a4ed38d7SDouglas Fuller  * On success, the caller is responsible for:
5015a4ed38d7SDouglas Fuller  *
5016a4ed38d7SDouglas Fuller  *     kfree(watchers);
5017a4ed38d7SDouglas Fuller  */
5018a4ed38d7SDouglas Fuller int ceph_osdc_list_watchers(struct ceph_osd_client *osdc,
5019a4ed38d7SDouglas Fuller 			    struct ceph_object_id *oid,
5020a4ed38d7SDouglas Fuller 			    struct ceph_object_locator *oloc,
5021a4ed38d7SDouglas Fuller 			    struct ceph_watch_item **watchers,
5022a4ed38d7SDouglas Fuller 			    u32 *num_watchers)
5023a4ed38d7SDouglas Fuller {
5024a4ed38d7SDouglas Fuller 	struct ceph_osd_request *req;
5025a4ed38d7SDouglas Fuller 	struct page **pages;
5026a4ed38d7SDouglas Fuller 	int ret;
5027a4ed38d7SDouglas Fuller 
5028a4ed38d7SDouglas Fuller 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
5029a4ed38d7SDouglas Fuller 	if (!req)
5030a4ed38d7SDouglas Fuller 		return -ENOMEM;
5031a4ed38d7SDouglas Fuller 
5032a4ed38d7SDouglas Fuller 	ceph_oid_copy(&req->r_base_oid, oid);
5033a4ed38d7SDouglas Fuller 	ceph_oloc_copy(&req->r_base_oloc, oloc);
5034a4ed38d7SDouglas Fuller 	req->r_flags = CEPH_OSD_FLAG_READ;
5035a4ed38d7SDouglas Fuller 
5036a4ed38d7SDouglas Fuller 	pages = ceph_alloc_page_vector(1, GFP_NOIO);
5037a4ed38d7SDouglas Fuller 	if (IS_ERR(pages)) {
5038a4ed38d7SDouglas Fuller 		ret = PTR_ERR(pages);
5039a4ed38d7SDouglas Fuller 		goto out_put_req;
5040a4ed38d7SDouglas Fuller 	}
5041a4ed38d7SDouglas Fuller 
5042a4ed38d7SDouglas Fuller 	osd_req_op_init(req, 0, CEPH_OSD_OP_LIST_WATCHERS, 0);
5043a4ed38d7SDouglas Fuller 	ceph_osd_data_pages_init(osd_req_op_data(req, 0, list_watchers,
5044a4ed38d7SDouglas Fuller 						 response_data),
5045a4ed38d7SDouglas Fuller 				 pages, PAGE_SIZE, 0, false, true);
5046a4ed38d7SDouglas Fuller 
504726f887e0SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
504826f887e0SIlya Dryomov 	if (ret)
504926f887e0SIlya Dryomov 		goto out_put_req;
505026f887e0SIlya Dryomov 
5051a8af0d68SJeff Layton 	ceph_osdc_start_request(osdc, req);
5052a4ed38d7SDouglas Fuller 	ret = ceph_osdc_wait_request(osdc, req);
5053a4ed38d7SDouglas Fuller 	if (ret >= 0) {
5054a4ed38d7SDouglas Fuller 		void *p = page_address(pages[0]);
5055a4ed38d7SDouglas Fuller 		void *const end = p + req->r_ops[0].outdata_len;
5056a4ed38d7SDouglas Fuller 
5057a4ed38d7SDouglas Fuller 		ret = decode_watchers(&p, end, watchers, num_watchers);
5058a4ed38d7SDouglas Fuller 	}
5059a4ed38d7SDouglas Fuller 
5060a4ed38d7SDouglas Fuller out_put_req:
5061a4ed38d7SDouglas Fuller 	ceph_osdc_put_request(req);
5062a4ed38d7SDouglas Fuller 	return ret;
5063a4ed38d7SDouglas Fuller }
5064a4ed38d7SDouglas Fuller EXPORT_SYMBOL(ceph_osdc_list_watchers);
5065a4ed38d7SDouglas Fuller 
5066b07d3c4bSIlya Dryomov /*
5067dd935f44SJosh Durgin  * Call all pending notify callbacks - for use after a watch is
5068dd935f44SJosh Durgin  * unregistered, to make sure no more callbacks for it will be invoked
5069dd935f44SJosh Durgin  */
5070f6479449Sstephen hemminger void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
5071dd935f44SJosh Durgin {
507299d16943SIlya Dryomov 	dout("%s osdc %p\n", __func__, osdc);
5073dd935f44SJosh Durgin 	flush_workqueue(osdc->notify_wq);
5074dd935f44SJosh Durgin }
5075dd935f44SJosh Durgin EXPORT_SYMBOL(ceph_osdc_flush_notifies);
5076dd935f44SJosh Durgin 
50777cca78c9SIlya Dryomov void ceph_osdc_maybe_request_map(struct ceph_osd_client *osdc)
50787cca78c9SIlya Dryomov {
50797cca78c9SIlya Dryomov 	down_read(&osdc->lock);
50807cca78c9SIlya Dryomov 	maybe_request_map(osdc);
50817cca78c9SIlya Dryomov 	up_read(&osdc->lock);
50827cca78c9SIlya Dryomov }
50837cca78c9SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_maybe_request_map);
5084dd935f44SJosh Durgin 
5085dd935f44SJosh Durgin /*
5086428a7158SDouglas Fuller  * Execute an OSD class method on an object.
5087428a7158SDouglas Fuller  *
5088428a7158SDouglas Fuller  * @flags: CEPH_OSD_FLAG_*
50892544a020SIlya Dryomov  * @resp_len: in/out param for reply length
5090428a7158SDouglas Fuller  */
5091428a7158SDouglas Fuller int ceph_osdc_call(struct ceph_osd_client *osdc,
5092428a7158SDouglas Fuller 		   struct ceph_object_id *oid,
5093428a7158SDouglas Fuller 		   struct ceph_object_locator *oloc,
5094428a7158SDouglas Fuller 		   const char *class, const char *method,
5095428a7158SDouglas Fuller 		   unsigned int flags,
5096428a7158SDouglas Fuller 		   struct page *req_page, size_t req_len,
509768ada915SIlya Dryomov 		   struct page **resp_pages, size_t *resp_len)
5098428a7158SDouglas Fuller {
5099428a7158SDouglas Fuller 	struct ceph_osd_request *req;
5100428a7158SDouglas Fuller 	int ret;
5101428a7158SDouglas Fuller 
510268ada915SIlya Dryomov 	if (req_len > PAGE_SIZE)
51032544a020SIlya Dryomov 		return -E2BIG;
51042544a020SIlya Dryomov 
5105428a7158SDouglas Fuller 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
5106428a7158SDouglas Fuller 	if (!req)
5107428a7158SDouglas Fuller 		return -ENOMEM;
5108428a7158SDouglas Fuller 
5109428a7158SDouglas Fuller 	ceph_oid_copy(&req->r_base_oid, oid);
5110428a7158SDouglas Fuller 	ceph_oloc_copy(&req->r_base_oloc, oloc);
5111428a7158SDouglas Fuller 	req->r_flags = flags;
5112428a7158SDouglas Fuller 
511324639ce5SIlya Dryomov 	ret = osd_req_op_cls_init(req, 0, class, method);
5114fe943d50SChengguang Xu 	if (ret)
5115fe943d50SChengguang Xu 		goto out_put_req;
5116fe943d50SChengguang Xu 
5117428a7158SDouglas Fuller 	if (req_page)
5118428a7158SDouglas Fuller 		osd_req_op_cls_request_data_pages(req, 0, &req_page, req_len,
5119428a7158SDouglas Fuller 						  0, false, false);
512068ada915SIlya Dryomov 	if (resp_pages)
512168ada915SIlya Dryomov 		osd_req_op_cls_response_data_pages(req, 0, resp_pages,
51222544a020SIlya Dryomov 						   *resp_len, 0, false, false);
5123428a7158SDouglas Fuller 
512426f887e0SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
512526f887e0SIlya Dryomov 	if (ret)
512626f887e0SIlya Dryomov 		goto out_put_req;
512726f887e0SIlya Dryomov 
5128a8af0d68SJeff Layton 	ceph_osdc_start_request(osdc, req);
5129428a7158SDouglas Fuller 	ret = ceph_osdc_wait_request(osdc, req);
5130428a7158SDouglas Fuller 	if (ret >= 0) {
5131428a7158SDouglas Fuller 		ret = req->r_ops[0].rval;
513268ada915SIlya Dryomov 		if (resp_pages)
5133428a7158SDouglas Fuller 			*resp_len = req->r_ops[0].outdata_len;
5134428a7158SDouglas Fuller 	}
5135428a7158SDouglas Fuller 
5136428a7158SDouglas Fuller out_put_req:
5137428a7158SDouglas Fuller 	ceph_osdc_put_request(req);
5138428a7158SDouglas Fuller 	return ret;
5139428a7158SDouglas Fuller }
5140428a7158SDouglas Fuller EXPORT_SYMBOL(ceph_osdc_call);
5141428a7158SDouglas Fuller 
5142428a7158SDouglas Fuller /*
5143120a75eaSYan, Zheng  * reset all osd connections
5144120a75eaSYan, Zheng  */
5145120a75eaSYan, Zheng void ceph_osdc_reopen_osds(struct ceph_osd_client *osdc)
5146120a75eaSYan, Zheng {
5147120a75eaSYan, Zheng 	struct rb_node *n;
5148120a75eaSYan, Zheng 
5149120a75eaSYan, Zheng 	down_write(&osdc->lock);
5150120a75eaSYan, Zheng 	for (n = rb_first(&osdc->osds); n; ) {
5151120a75eaSYan, Zheng 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
5152120a75eaSYan, Zheng 
5153120a75eaSYan, Zheng 		n = rb_next(n);
5154120a75eaSYan, Zheng 		if (!reopen_osd(osd))
5155120a75eaSYan, Zheng 			kick_osd_requests(osd);
5156120a75eaSYan, Zheng 	}
5157120a75eaSYan, Zheng 	up_write(&osdc->lock);
5158120a75eaSYan, Zheng }
5159120a75eaSYan, Zheng 
5160120a75eaSYan, Zheng /*
51613d14c5d2SYehuda Sadeh  * init, shutdown
51623d14c5d2SYehuda Sadeh  */
51633d14c5d2SYehuda Sadeh int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
51643d14c5d2SYehuda Sadeh {
51653d14c5d2SYehuda Sadeh 	int err;
51663d14c5d2SYehuda Sadeh 
51673d14c5d2SYehuda Sadeh 	dout("init\n");
51683d14c5d2SYehuda Sadeh 	osdc->client = client;
51695aea3dcdSIlya Dryomov 	init_rwsem(&osdc->lock);
51703d14c5d2SYehuda Sadeh 	osdc->osds = RB_ROOT;
51713d14c5d2SYehuda Sadeh 	INIT_LIST_HEAD(&osdc->osd_lru);
51729dd2845cSIlya Dryomov 	spin_lock_init(&osdc->osd_lru_lock);
51735aea3dcdSIlya Dryomov 	osd_init(&osdc->homeless_osd);
51745aea3dcdSIlya Dryomov 	osdc->homeless_osd.o_osdc = osdc;
51755aea3dcdSIlya Dryomov 	osdc->homeless_osd.o_osd = CEPH_HOMELESS_OSD;
5176264048afSIlya Dryomov 	osdc->last_linger_id = CEPH_LINGER_ID_START;
5177922dab61SIlya Dryomov 	osdc->linger_requests = RB_ROOT;
51784609245eSIlya Dryomov 	osdc->map_checks = RB_ROOT;
51794609245eSIlya Dryomov 	osdc->linger_map_checks = RB_ROOT;
51803d14c5d2SYehuda Sadeh 	INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
51813d14c5d2SYehuda Sadeh 	INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
51823d14c5d2SYehuda Sadeh 
51833d14c5d2SYehuda Sadeh 	err = -ENOMEM;
5184e5253a7bSIlya Dryomov 	osdc->osdmap = ceph_osdmap_alloc();
5185e5253a7bSIlya Dryomov 	if (!osdc->osdmap)
5186e5253a7bSIlya Dryomov 		goto out;
5187e5253a7bSIlya Dryomov 
51889e767adbSIlya Dryomov 	osdc->req_mempool = mempool_create_slab_pool(10,
51899e767adbSIlya Dryomov 						     ceph_osd_request_cache);
51903d14c5d2SYehuda Sadeh 	if (!osdc->req_mempool)
5191e5253a7bSIlya Dryomov 		goto out_map;
51923d14c5d2SYehuda Sadeh 
5193d50b409fSSage Weil 	err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
51940d9c1ab3SIlya Dryomov 				PAGE_SIZE, CEPH_OSD_SLAB_OPS, 10, "osd_op");
51953d14c5d2SYehuda Sadeh 	if (err < 0)
51963d14c5d2SYehuda Sadeh 		goto out_mempool;
5197d50b409fSSage Weil 	err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
51980d9c1ab3SIlya Dryomov 				PAGE_SIZE, CEPH_OSD_SLAB_OPS, 10,
51990d9c1ab3SIlya Dryomov 				"osd_op_reply");
52003d14c5d2SYehuda Sadeh 	if (err < 0)
52013d14c5d2SYehuda Sadeh 		goto out_msgpool;
5202a40c4f10SYehuda Sadeh 
5203dbcae088SDan Carpenter 	err = -ENOMEM;
5204a40c4f10SYehuda Sadeh 	osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
5205dbcae088SDan Carpenter 	if (!osdc->notify_wq)
5206c172ec5cSIlya Dryomov 		goto out_msgpool_reply;
5207c172ec5cSIlya Dryomov 
520888bc1922SIlya Dryomov 	osdc->completion_wq = create_singlethread_workqueue("ceph-completion");
520988bc1922SIlya Dryomov 	if (!osdc->completion_wq)
521088bc1922SIlya Dryomov 		goto out_notify_wq;
521188bc1922SIlya Dryomov 
5212fbca9635SIlya Dryomov 	schedule_delayed_work(&osdc->timeout_work,
5213fbca9635SIlya Dryomov 			      osdc->client->options->osd_keepalive_timeout);
5214b37ee1b9SIlya Dryomov 	schedule_delayed_work(&osdc->osds_timeout_work,
5215b37ee1b9SIlya Dryomov 	    round_jiffies_relative(osdc->client->options->osd_idle_ttl));
5216b37ee1b9SIlya Dryomov 
52173d14c5d2SYehuda Sadeh 	return 0;
52183d14c5d2SYehuda Sadeh 
521988bc1922SIlya Dryomov out_notify_wq:
522088bc1922SIlya Dryomov 	destroy_workqueue(osdc->notify_wq);
5221c172ec5cSIlya Dryomov out_msgpool_reply:
5222c172ec5cSIlya Dryomov 	ceph_msgpool_destroy(&osdc->msgpool_op_reply);
52233d14c5d2SYehuda Sadeh out_msgpool:
52243d14c5d2SYehuda Sadeh 	ceph_msgpool_destroy(&osdc->msgpool_op);
52253d14c5d2SYehuda Sadeh out_mempool:
52263d14c5d2SYehuda Sadeh 	mempool_destroy(osdc->req_mempool);
5227e5253a7bSIlya Dryomov out_map:
5228e5253a7bSIlya Dryomov 	ceph_osdmap_destroy(osdc->osdmap);
52293d14c5d2SYehuda Sadeh out:
52303d14c5d2SYehuda Sadeh 	return err;
52313d14c5d2SYehuda Sadeh }
52323d14c5d2SYehuda Sadeh 
52333d14c5d2SYehuda Sadeh void ceph_osdc_stop(struct ceph_osd_client *osdc)
52343d14c5d2SYehuda Sadeh {
523588bc1922SIlya Dryomov 	destroy_workqueue(osdc->completion_wq);
5236a40c4f10SYehuda Sadeh 	destroy_workqueue(osdc->notify_wq);
52373d14c5d2SYehuda Sadeh 	cancel_delayed_work_sync(&osdc->timeout_work);
52383d14c5d2SYehuda Sadeh 	cancel_delayed_work_sync(&osdc->osds_timeout_work);
523942a2c09fSIlya Dryomov 
52405aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
524142a2c09fSIlya Dryomov 	while (!RB_EMPTY_ROOT(&osdc->osds)) {
524242a2c09fSIlya Dryomov 		struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
524342a2c09fSIlya Dryomov 						struct ceph_osd, o_node);
52445aea3dcdSIlya Dryomov 		close_osd(osd);
524542a2c09fSIlya Dryomov 	}
52465aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
524702113a0fSElena Reshetova 	WARN_ON(refcount_read(&osdc->homeless_osd.o_ref) != 1);
52485aea3dcdSIlya Dryomov 	osd_cleanup(&osdc->homeless_osd);
52495aea3dcdSIlya Dryomov 
52505aea3dcdSIlya Dryomov 	WARN_ON(!list_empty(&osdc->osd_lru));
5251922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_requests));
52524609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osdc->map_checks));
52534609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_map_checks));
52545aea3dcdSIlya Dryomov 	WARN_ON(atomic_read(&osdc->num_requests));
52555aea3dcdSIlya Dryomov 	WARN_ON(atomic_read(&osdc->num_homeless));
525642a2c09fSIlya Dryomov 
52573d14c5d2SYehuda Sadeh 	ceph_osdmap_destroy(osdc->osdmap);
52583d14c5d2SYehuda Sadeh 	mempool_destroy(osdc->req_mempool);
52593d14c5d2SYehuda Sadeh 	ceph_msgpool_destroy(&osdc->msgpool_op);
52603d14c5d2SYehuda Sadeh 	ceph_msgpool_destroy(&osdc->msgpool_op_reply);
52613d14c5d2SYehuda Sadeh }
52623d14c5d2SYehuda Sadeh 
5263aca39d9eSLuís Henriques int osd_req_op_copy_from_init(struct ceph_osd_request *req,
526423ddf9beSLuis Henriques 			      u64 src_snapid, u64 src_version,
526523ddf9beSLuis Henriques 			      struct ceph_object_id *src_oid,
526623ddf9beSLuis Henriques 			      struct ceph_object_locator *src_oloc,
526723ddf9beSLuis Henriques 			      u32 src_fadvise_flags,
526823ddf9beSLuis Henriques 			      u32 dst_fadvise_flags,
526978beb0ffSLuis Henriques 			      u32 truncate_seq, u64 truncate_size,
527023ddf9beSLuis Henriques 			      u8 copy_from_flags)
527123ddf9beSLuis Henriques {
527223ddf9beSLuis Henriques 	struct ceph_osd_req_op *op;
527323ddf9beSLuis Henriques 	struct page **pages;
527423ddf9beSLuis Henriques 	void *p, *end;
527523ddf9beSLuis Henriques 
527623ddf9beSLuis Henriques 	pages = ceph_alloc_page_vector(1, GFP_KERNEL);
527723ddf9beSLuis Henriques 	if (IS_ERR(pages))
527823ddf9beSLuis Henriques 		return PTR_ERR(pages);
527923ddf9beSLuis Henriques 
5280042f6498SJeff Layton 	op = osd_req_op_init(req, 0, CEPH_OSD_OP_COPY_FROM2,
528178beb0ffSLuis Henriques 			     dst_fadvise_flags);
528223ddf9beSLuis Henriques 	op->copy_from.snapid = src_snapid;
528323ddf9beSLuis Henriques 	op->copy_from.src_version = src_version;
528423ddf9beSLuis Henriques 	op->copy_from.flags = copy_from_flags;
528523ddf9beSLuis Henriques 	op->copy_from.src_fadvise_flags = src_fadvise_flags;
528623ddf9beSLuis Henriques 
528723ddf9beSLuis Henriques 	p = page_address(pages[0]);
528823ddf9beSLuis Henriques 	end = p + PAGE_SIZE;
528923ddf9beSLuis Henriques 	ceph_encode_string(&p, end, src_oid->name, src_oid->name_len);
529023ddf9beSLuis Henriques 	encode_oloc(&p, end, src_oloc);
529178beb0ffSLuis Henriques 	ceph_encode_32(&p, truncate_seq);
529278beb0ffSLuis Henriques 	ceph_encode_64(&p, truncate_size);
529323ddf9beSLuis Henriques 	op->indata_len = PAGE_SIZE - (end - p);
529423ddf9beSLuis Henriques 
529523ddf9beSLuis Henriques 	ceph_osd_data_pages_init(&op->copy_from.osd_data, pages,
529623ddf9beSLuis Henriques 				 op->indata_len, 0, false, true);
529723ddf9beSLuis Henriques 	return 0;
529823ddf9beSLuis Henriques }
5299aca39d9eSLuís Henriques EXPORT_SYMBOL(osd_req_op_copy_from_init);
530023ddf9beSLuis Henriques 
530157a35dfbSChengguang Xu int __init ceph_osdc_setup(void)
53025522ae0bSAlex Elder {
53033f1af42aSIlya Dryomov 	size_t size = sizeof(struct ceph_osd_request) +
53043f1af42aSIlya Dryomov 	    CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
53053f1af42aSIlya Dryomov 
53065522ae0bSAlex Elder 	BUG_ON(ceph_osd_request_cache);
53073f1af42aSIlya Dryomov 	ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
53083f1af42aSIlya Dryomov 						   0, 0, NULL);
53095522ae0bSAlex Elder 
53105522ae0bSAlex Elder 	return ceph_osd_request_cache ? 0 : -ENOMEM;
53115522ae0bSAlex Elder }
53125522ae0bSAlex Elder 
53135522ae0bSAlex Elder void ceph_osdc_cleanup(void)
53145522ae0bSAlex Elder {
53155522ae0bSAlex Elder 	BUG_ON(!ceph_osd_request_cache);
53165522ae0bSAlex Elder 	kmem_cache_destroy(ceph_osd_request_cache);
53175522ae0bSAlex Elder 	ceph_osd_request_cache = NULL;
53185522ae0bSAlex Elder }
53195522ae0bSAlex Elder 
53203d14c5d2SYehuda Sadeh /*
53213d14c5d2SYehuda Sadeh  * handle incoming message
53223d14c5d2SYehuda Sadeh  */
53234972cf60SIlya Dryomov static void osd_dispatch(struct ceph_connection *con, struct ceph_msg *msg)
53243d14c5d2SYehuda Sadeh {
53253d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
53265aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
53273d14c5d2SYehuda Sadeh 	int type = le16_to_cpu(msg->hdr.type);
53283d14c5d2SYehuda Sadeh 
53293d14c5d2SYehuda Sadeh 	switch (type) {
53303d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_MAP:
53313d14c5d2SYehuda Sadeh 		ceph_osdc_handle_map(osdc, msg);
53323d14c5d2SYehuda Sadeh 		break;
53333d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_OPREPLY:
53345aea3dcdSIlya Dryomov 		handle_reply(osd, msg);
53353d14c5d2SYehuda Sadeh 		break;
5336a02a946dSIlya Dryomov 	case CEPH_MSG_OSD_BACKOFF:
5337a02a946dSIlya Dryomov 		handle_backoff(osd, msg);
5338a02a946dSIlya Dryomov 		break;
5339a40c4f10SYehuda Sadeh 	case CEPH_MSG_WATCH_NOTIFY:
5340a40c4f10SYehuda Sadeh 		handle_watch_notify(osdc, msg);
5341a40c4f10SYehuda Sadeh 		break;
53423d14c5d2SYehuda Sadeh 
53433d14c5d2SYehuda Sadeh 	default:
53443d14c5d2SYehuda Sadeh 		pr_err("received unknown message type %d %s\n", type,
53453d14c5d2SYehuda Sadeh 		       ceph_msg_type_name(type));
53463d14c5d2SYehuda Sadeh 	}
53475aea3dcdSIlya Dryomov 
53483d14c5d2SYehuda Sadeh 	ceph_msg_put(msg);
53493d14c5d2SYehuda Sadeh }
53503d14c5d2SYehuda Sadeh 
53513d14c5d2SYehuda Sadeh /*
5352d15f9d69SIlya Dryomov  * Lookup and return message for incoming reply.  Don't try to do
5353d15f9d69SIlya Dryomov  * anything about a larger than preallocated data portion of the
5354d15f9d69SIlya Dryomov  * message at the moment - for now, just skip the message.
53553d14c5d2SYehuda Sadeh  */
53563d14c5d2SYehuda Sadeh static struct ceph_msg *get_reply(struct ceph_connection *con,
53573d14c5d2SYehuda Sadeh 				  struct ceph_msg_header *hdr,
53583d14c5d2SYehuda Sadeh 				  int *skip)
53593d14c5d2SYehuda Sadeh {
53603d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
53613d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = osd->o_osdc;
53625aea3dcdSIlya Dryomov 	struct ceph_msg *m = NULL;
53633d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
53643f0a4ac5SIlya Dryomov 	int front_len = le32_to_cpu(hdr->front_len);
53653d14c5d2SYehuda Sadeh 	int data_len = le32_to_cpu(hdr->data_len);
53665aea3dcdSIlya Dryomov 	u64 tid = le64_to_cpu(hdr->tid);
53673d14c5d2SYehuda Sadeh 
53685aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
53695aea3dcdSIlya Dryomov 	if (!osd_registered(osd)) {
53705aea3dcdSIlya Dryomov 		dout("%s osd%d unknown, skipping\n", __func__, osd->o_osd);
53715aea3dcdSIlya Dryomov 		*skip = 1;
53725aea3dcdSIlya Dryomov 		goto out_unlock_osdc;
53735aea3dcdSIlya Dryomov 	}
53745aea3dcdSIlya Dryomov 	WARN_ON(osd->o_osd != le64_to_cpu(hdr->src.num));
53755aea3dcdSIlya Dryomov 
53765aea3dcdSIlya Dryomov 	mutex_lock(&osd->lock);
53775aea3dcdSIlya Dryomov 	req = lookup_request(&osd->o_requests, tid);
53783d14c5d2SYehuda Sadeh 	if (!req) {
5379cd8140c6SIlya Dryomov 		dout("%s osd%d tid %llu unknown, skipping\n", __func__,
5380cd8140c6SIlya Dryomov 		     osd->o_osd, tid);
5381d15f9d69SIlya Dryomov 		*skip = 1;
53825aea3dcdSIlya Dryomov 		goto out_unlock_session;
53833d14c5d2SYehuda Sadeh 	}
53843d14c5d2SYehuda Sadeh 
53858921d114SAlex Elder 	ceph_msg_revoke_incoming(req->r_reply);
53863d14c5d2SYehuda Sadeh 
5387f2be82b0SIlya Dryomov 	if (front_len > req->r_reply->front_alloc_len) {
5388d15f9d69SIlya Dryomov 		pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
5389d15f9d69SIlya Dryomov 			__func__, osd->o_osd, req->r_tid, front_len,
5390d15f9d69SIlya Dryomov 			req->r_reply->front_alloc_len);
53913f0a4ac5SIlya Dryomov 		m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
53923f0a4ac5SIlya Dryomov 				 false);
53933d14c5d2SYehuda Sadeh 		if (!m)
53945aea3dcdSIlya Dryomov 			goto out_unlock_session;
53953d14c5d2SYehuda Sadeh 		ceph_msg_put(req->r_reply);
53963d14c5d2SYehuda Sadeh 		req->r_reply = m;
53973d14c5d2SYehuda Sadeh 	}
53983d14c5d2SYehuda Sadeh 
5399d15f9d69SIlya Dryomov 	if (data_len > req->r_reply->data_length) {
5400d15f9d69SIlya Dryomov 		pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
5401d15f9d69SIlya Dryomov 			__func__, osd->o_osd, req->r_tid, data_len,
5402d15f9d69SIlya Dryomov 			req->r_reply->data_length);
54033d14c5d2SYehuda Sadeh 		m = NULL;
5404d15f9d69SIlya Dryomov 		*skip = 1;
54055aea3dcdSIlya Dryomov 		goto out_unlock_session;
54063d14c5d2SYehuda Sadeh 	}
5407d15f9d69SIlya Dryomov 
5408d15f9d69SIlya Dryomov 	m = ceph_msg_get(req->r_reply);
54093d14c5d2SYehuda Sadeh 	dout("get_reply tid %lld %p\n", tid, m);
54103d14c5d2SYehuda Sadeh 
54115aea3dcdSIlya Dryomov out_unlock_session:
54125aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
54135aea3dcdSIlya Dryomov out_unlock_osdc:
54145aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
54153d14c5d2SYehuda Sadeh 	return m;
54163d14c5d2SYehuda Sadeh }
54173d14c5d2SYehuda Sadeh 
541819079203SIlya Dryomov static struct ceph_msg *alloc_msg_with_page_vector(struct ceph_msg_header *hdr)
541919079203SIlya Dryomov {
542019079203SIlya Dryomov 	struct ceph_msg *m;
542119079203SIlya Dryomov 	int type = le16_to_cpu(hdr->type);
542219079203SIlya Dryomov 	u32 front_len = le32_to_cpu(hdr->front_len);
542319079203SIlya Dryomov 	u32 data_len = le32_to_cpu(hdr->data_len);
542419079203SIlya Dryomov 
54250d9c1ab3SIlya Dryomov 	m = ceph_msg_new2(type, front_len, 1, GFP_NOIO, false);
542619079203SIlya Dryomov 	if (!m)
542719079203SIlya Dryomov 		return NULL;
542819079203SIlya Dryomov 
542919079203SIlya Dryomov 	if (data_len) {
543019079203SIlya Dryomov 		struct page **pages;
543119079203SIlya Dryomov 
543219079203SIlya Dryomov 		pages = ceph_alloc_page_vector(calc_pages_for(0, data_len),
543319079203SIlya Dryomov 					       GFP_NOIO);
5434c22e853aSWei Yongjun 		if (IS_ERR(pages)) {
543519079203SIlya Dryomov 			ceph_msg_put(m);
543619079203SIlya Dryomov 			return NULL;
543719079203SIlya Dryomov 		}
543819079203SIlya Dryomov 
5439e8862740SIlya Dryomov 		ceph_msg_data_add_pages(m, pages, data_len, 0, true);
544019079203SIlya Dryomov 	}
544119079203SIlya Dryomov 
544219079203SIlya Dryomov 	return m;
544319079203SIlya Dryomov }
544419079203SIlya Dryomov 
54454972cf60SIlya Dryomov static struct ceph_msg *osd_alloc_msg(struct ceph_connection *con,
54463d14c5d2SYehuda Sadeh 				      struct ceph_msg_header *hdr,
54473d14c5d2SYehuda Sadeh 				      int *skip)
54483d14c5d2SYehuda Sadeh {
54493d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
54503d14c5d2SYehuda Sadeh 	int type = le16_to_cpu(hdr->type);
54513d14c5d2SYehuda Sadeh 
54521c20f2d2SAlex Elder 	*skip = 0;
54533d14c5d2SYehuda Sadeh 	switch (type) {
54543d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_MAP:
5455a02a946dSIlya Dryomov 	case CEPH_MSG_OSD_BACKOFF:
5456a40c4f10SYehuda Sadeh 	case CEPH_MSG_WATCH_NOTIFY:
545719079203SIlya Dryomov 		return alloc_msg_with_page_vector(hdr);
54583d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_OPREPLY:
54593d14c5d2SYehuda Sadeh 		return get_reply(con, hdr, skip);
54603d14c5d2SYehuda Sadeh 	default:
54615aea3dcdSIlya Dryomov 		pr_warn("%s osd%d unknown msg type %d, skipping\n", __func__,
54625aea3dcdSIlya Dryomov 			osd->o_osd, type);
54633d14c5d2SYehuda Sadeh 		*skip = 1;
54643d14c5d2SYehuda Sadeh 		return NULL;
54653d14c5d2SYehuda Sadeh 	}
54663d14c5d2SYehuda Sadeh }
54673d14c5d2SYehuda Sadeh 
54683d14c5d2SYehuda Sadeh /*
54693d14c5d2SYehuda Sadeh  * Wrappers to refcount containing ceph_osd struct
54703d14c5d2SYehuda Sadeh  */
54714972cf60SIlya Dryomov static struct ceph_connection *osd_get_con(struct ceph_connection *con)
54723d14c5d2SYehuda Sadeh {
54733d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
54743d14c5d2SYehuda Sadeh 	if (get_osd(osd))
54753d14c5d2SYehuda Sadeh 		return con;
54763d14c5d2SYehuda Sadeh 	return NULL;
54773d14c5d2SYehuda Sadeh }
54783d14c5d2SYehuda Sadeh 
54794972cf60SIlya Dryomov static void osd_put_con(struct ceph_connection *con)
54803d14c5d2SYehuda Sadeh {
54813d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
54823d14c5d2SYehuda Sadeh 	put_osd(osd);
54833d14c5d2SYehuda Sadeh }
54843d14c5d2SYehuda Sadeh 
54853d14c5d2SYehuda Sadeh /*
54863d14c5d2SYehuda Sadeh  * authentication
54873d14c5d2SYehuda Sadeh  */
5488cd1a677cSIlya Dryomov 
5489a3530df3SAlex Elder /*
5490a3530df3SAlex Elder  * Note: returned pointer is the address of a structure that's
5491a3530df3SAlex Elder  * managed separately.  Caller must *not* attempt to free it.
5492a3530df3SAlex Elder  */
54934972cf60SIlya Dryomov static struct ceph_auth_handshake *
54944972cf60SIlya Dryomov osd_get_authorizer(struct ceph_connection *con, int *proto, int force_new)
54953d14c5d2SYehuda Sadeh {
54963d14c5d2SYehuda Sadeh 	struct ceph_osd *o = con->private;
54973d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = o->o_osdc;
54983d14c5d2SYehuda Sadeh 	struct ceph_auth_client *ac = osdc->client->monc.auth;
549974f1869fSAlex Elder 	struct ceph_auth_handshake *auth = &o->o_auth;
5500ce287162SIlya Dryomov 	int ret;
55013d14c5d2SYehuda Sadeh 
5502ce287162SIlya Dryomov 	ret = __ceph_auth_get_authorizer(ac, auth, CEPH_ENTITY_TYPE_OSD,
5503ce287162SIlya Dryomov 					 force_new, proto, NULL, NULL);
55043d14c5d2SYehuda Sadeh 	if (ret)
5505a3530df3SAlex Elder 		return ERR_PTR(ret);
550674f1869fSAlex Elder 
5507a3530df3SAlex Elder 	return auth;
55083d14c5d2SYehuda Sadeh }
55093d14c5d2SYehuda Sadeh 
55104972cf60SIlya Dryomov static int osd_add_authorizer_challenge(struct ceph_connection *con,
55116daca13dSIlya Dryomov 				    void *challenge_buf, int challenge_buf_len)
55126daca13dSIlya Dryomov {
55136daca13dSIlya Dryomov 	struct ceph_osd *o = con->private;
55146daca13dSIlya Dryomov 	struct ceph_osd_client *osdc = o->o_osdc;
55156daca13dSIlya Dryomov 	struct ceph_auth_client *ac = osdc->client->monc.auth;
55166daca13dSIlya Dryomov 
55176daca13dSIlya Dryomov 	return ceph_auth_add_authorizer_challenge(ac, o->o_auth.authorizer,
55186daca13dSIlya Dryomov 					    challenge_buf, challenge_buf_len);
55196daca13dSIlya Dryomov }
55203d14c5d2SYehuda Sadeh 
55214972cf60SIlya Dryomov static int osd_verify_authorizer_reply(struct ceph_connection *con)
55223d14c5d2SYehuda Sadeh {
55233d14c5d2SYehuda Sadeh 	struct ceph_osd *o = con->private;
55243d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = o->o_osdc;
55253d14c5d2SYehuda Sadeh 	struct ceph_auth_client *ac = osdc->client->monc.auth;
5526285ea34fSIlya Dryomov 	struct ceph_auth_handshake *auth = &o->o_auth;
55273d14c5d2SYehuda Sadeh 
5528285ea34fSIlya Dryomov 	return ceph_auth_verify_authorizer_reply(ac, auth->authorizer,
5529285ea34fSIlya Dryomov 		auth->authorizer_reply_buf, auth->authorizer_reply_buf_len,
5530285ea34fSIlya Dryomov 		NULL, NULL, NULL, NULL);
55313d14c5d2SYehuda Sadeh }
55323d14c5d2SYehuda Sadeh 
55334972cf60SIlya Dryomov static int osd_invalidate_authorizer(struct ceph_connection *con)
55343d14c5d2SYehuda Sadeh {
55353d14c5d2SYehuda Sadeh 	struct ceph_osd *o = con->private;
55363d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = o->o_osdc;
55373d14c5d2SYehuda Sadeh 	struct ceph_auth_client *ac = osdc->client->monc.auth;
55383d14c5d2SYehuda Sadeh 
553927859f97SSage Weil 	ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
55403d14c5d2SYehuda Sadeh 	return ceph_monc_validate_auth(&osdc->client->monc);
55413d14c5d2SYehuda Sadeh }
55423d14c5d2SYehuda Sadeh 
5543cd1a677cSIlya Dryomov static int osd_get_auth_request(struct ceph_connection *con,
5544cd1a677cSIlya Dryomov 				void *buf, int *buf_len,
5545cd1a677cSIlya Dryomov 				void **authorizer, int *authorizer_len)
5546cd1a677cSIlya Dryomov {
5547cd1a677cSIlya Dryomov 	struct ceph_osd *o = con->private;
5548cd1a677cSIlya Dryomov 	struct ceph_auth_client *ac = o->o_osdc->client->monc.auth;
5549cd1a677cSIlya Dryomov 	struct ceph_auth_handshake *auth = &o->o_auth;
5550cd1a677cSIlya Dryomov 	int ret;
5551cd1a677cSIlya Dryomov 
5552cd1a677cSIlya Dryomov 	ret = ceph_auth_get_authorizer(ac, auth, CEPH_ENTITY_TYPE_OSD,
5553cd1a677cSIlya Dryomov 				       buf, buf_len);
5554cd1a677cSIlya Dryomov 	if (ret)
5555cd1a677cSIlya Dryomov 		return ret;
5556cd1a677cSIlya Dryomov 
5557cd1a677cSIlya Dryomov 	*authorizer = auth->authorizer_buf;
5558cd1a677cSIlya Dryomov 	*authorizer_len = auth->authorizer_buf_len;
5559cd1a677cSIlya Dryomov 	return 0;
5560cd1a677cSIlya Dryomov }
5561cd1a677cSIlya Dryomov 
5562cd1a677cSIlya Dryomov static int osd_handle_auth_reply_more(struct ceph_connection *con,
5563cd1a677cSIlya Dryomov 				      void *reply, int reply_len,
5564cd1a677cSIlya Dryomov 				      void *buf, int *buf_len,
5565cd1a677cSIlya Dryomov 				      void **authorizer, int *authorizer_len)
5566cd1a677cSIlya Dryomov {
5567cd1a677cSIlya Dryomov 	struct ceph_osd *o = con->private;
5568cd1a677cSIlya Dryomov 	struct ceph_auth_client *ac = o->o_osdc->client->monc.auth;
5569cd1a677cSIlya Dryomov 	struct ceph_auth_handshake *auth = &o->o_auth;
5570cd1a677cSIlya Dryomov 	int ret;
5571cd1a677cSIlya Dryomov 
5572cd1a677cSIlya Dryomov 	ret = ceph_auth_handle_svc_reply_more(ac, auth, reply, reply_len,
5573cd1a677cSIlya Dryomov 					      buf, buf_len);
5574cd1a677cSIlya Dryomov 	if (ret)
5575cd1a677cSIlya Dryomov 		return ret;
5576cd1a677cSIlya Dryomov 
5577cd1a677cSIlya Dryomov 	*authorizer = auth->authorizer_buf;
5578cd1a677cSIlya Dryomov 	*authorizer_len = auth->authorizer_buf_len;
5579cd1a677cSIlya Dryomov 	return 0;
5580cd1a677cSIlya Dryomov }
5581cd1a677cSIlya Dryomov 
5582cd1a677cSIlya Dryomov static int osd_handle_auth_done(struct ceph_connection *con,
5583cd1a677cSIlya Dryomov 				u64 global_id, void *reply, int reply_len,
5584cd1a677cSIlya Dryomov 				u8 *session_key, int *session_key_len,
5585cd1a677cSIlya Dryomov 				u8 *con_secret, int *con_secret_len)
5586cd1a677cSIlya Dryomov {
5587cd1a677cSIlya Dryomov 	struct ceph_osd *o = con->private;
5588cd1a677cSIlya Dryomov 	struct ceph_auth_client *ac = o->o_osdc->client->monc.auth;
5589cd1a677cSIlya Dryomov 	struct ceph_auth_handshake *auth = &o->o_auth;
5590cd1a677cSIlya Dryomov 
5591cd1a677cSIlya Dryomov 	return ceph_auth_handle_svc_reply_done(ac, auth, reply, reply_len,
5592cd1a677cSIlya Dryomov 					       session_key, session_key_len,
5593cd1a677cSIlya Dryomov 					       con_secret, con_secret_len);
5594cd1a677cSIlya Dryomov }
5595cd1a677cSIlya Dryomov 
5596cd1a677cSIlya Dryomov static int osd_handle_auth_bad_method(struct ceph_connection *con,
5597cd1a677cSIlya Dryomov 				      int used_proto, int result,
5598cd1a677cSIlya Dryomov 				      const int *allowed_protos, int proto_cnt,
5599cd1a677cSIlya Dryomov 				      const int *allowed_modes, int mode_cnt)
5600cd1a677cSIlya Dryomov {
5601cd1a677cSIlya Dryomov 	struct ceph_osd *o = con->private;
5602cd1a677cSIlya Dryomov 	struct ceph_mon_client *monc = &o->o_osdc->client->monc;
5603cd1a677cSIlya Dryomov 	int ret;
5604cd1a677cSIlya Dryomov 
5605cd1a677cSIlya Dryomov 	if (ceph_auth_handle_bad_authorizer(monc->auth, CEPH_ENTITY_TYPE_OSD,
5606cd1a677cSIlya Dryomov 					    used_proto, result,
5607cd1a677cSIlya Dryomov 					    allowed_protos, proto_cnt,
5608cd1a677cSIlya Dryomov 					    allowed_modes, mode_cnt)) {
5609cd1a677cSIlya Dryomov 		ret = ceph_monc_validate_auth(monc);
5610cd1a677cSIlya Dryomov 		if (ret)
5611cd1a677cSIlya Dryomov 			return ret;
5612cd1a677cSIlya Dryomov 	}
5613cd1a677cSIlya Dryomov 
5614cd1a677cSIlya Dryomov 	return -EACCES;
5615cd1a677cSIlya Dryomov }
5616cd1a677cSIlya Dryomov 
56178cb441c0SIlya Dryomov static void osd_reencode_message(struct ceph_msg *msg)
56188cb441c0SIlya Dryomov {
5619914902afSIlya Dryomov 	int type = le16_to_cpu(msg->hdr.type);
5620914902afSIlya Dryomov 
5621914902afSIlya Dryomov 	if (type == CEPH_MSG_OSD_OP)
56228cb441c0SIlya Dryomov 		encode_request_finish(msg);
56238cb441c0SIlya Dryomov }
56248cb441c0SIlya Dryomov 
562579dbd1baSIlya Dryomov static int osd_sign_message(struct ceph_msg *msg)
562633d07337SYan, Zheng {
562779dbd1baSIlya Dryomov 	struct ceph_osd *o = msg->con->private;
562833d07337SYan, Zheng 	struct ceph_auth_handshake *auth = &o->o_auth;
562979dbd1baSIlya Dryomov 
563033d07337SYan, Zheng 	return ceph_auth_sign_message(auth, msg);
563133d07337SYan, Zheng }
563233d07337SYan, Zheng 
563379dbd1baSIlya Dryomov static int osd_check_message_signature(struct ceph_msg *msg)
563433d07337SYan, Zheng {
563579dbd1baSIlya Dryomov 	struct ceph_osd *o = msg->con->private;
563633d07337SYan, Zheng 	struct ceph_auth_handshake *auth = &o->o_auth;
563779dbd1baSIlya Dryomov 
563833d07337SYan, Zheng 	return ceph_auth_check_message_signature(auth, msg);
563933d07337SYan, Zheng }
564033d07337SYan, Zheng 
56413d14c5d2SYehuda Sadeh static const struct ceph_connection_operations osd_con_ops = {
56424972cf60SIlya Dryomov 	.get = osd_get_con,
56434972cf60SIlya Dryomov 	.put = osd_put_con,
56444972cf60SIlya Dryomov 	.alloc_msg = osd_alloc_msg,
56454972cf60SIlya Dryomov 	.dispatch = osd_dispatch,
56464972cf60SIlya Dryomov 	.fault = osd_fault,
56478cb441c0SIlya Dryomov 	.reencode_message = osd_reencode_message,
56484972cf60SIlya Dryomov 	.get_authorizer = osd_get_authorizer,
56494972cf60SIlya Dryomov 	.add_authorizer_challenge = osd_add_authorizer_challenge,
56504972cf60SIlya Dryomov 	.verify_authorizer_reply = osd_verify_authorizer_reply,
56514972cf60SIlya Dryomov 	.invalidate_authorizer = osd_invalidate_authorizer,
565279dbd1baSIlya Dryomov 	.sign_message = osd_sign_message,
565379dbd1baSIlya Dryomov 	.check_message_signature = osd_check_message_signature,
5654cd1a677cSIlya Dryomov 	.get_auth_request = osd_get_auth_request,
5655cd1a677cSIlya Dryomov 	.handle_auth_reply_more = osd_handle_auth_reply_more,
5656cd1a677cSIlya Dryomov 	.handle_auth_done = osd_handle_auth_done,
5657cd1a677cSIlya Dryomov 	.handle_auth_bad_method = osd_handle_auth_bad_method,
56583d14c5d2SYehuda Sadeh };
5659