xref: /openbmc/linux/net/ceph/osd_client.c (revision 57a35dfb)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2a4ce40a9SAlex Elder 
33d14c5d2SYehuda Sadeh #include <linux/ceph/ceph_debug.h>
43d14c5d2SYehuda Sadeh 
53d14c5d2SYehuda Sadeh #include <linux/module.h>
63d14c5d2SYehuda Sadeh #include <linux/err.h>
73d14c5d2SYehuda Sadeh #include <linux/highmem.h>
83d14c5d2SYehuda Sadeh #include <linux/mm.h>
93d14c5d2SYehuda Sadeh #include <linux/pagemap.h>
103d14c5d2SYehuda Sadeh #include <linux/slab.h>
113d14c5d2SYehuda Sadeh #include <linux/uaccess.h>
123d14c5d2SYehuda Sadeh #ifdef CONFIG_BLOCK
133d14c5d2SYehuda Sadeh #include <linux/bio.h>
143d14c5d2SYehuda Sadeh #endif
153d14c5d2SYehuda Sadeh 
168cb441c0SIlya Dryomov #include <linux/ceph/ceph_features.h>
173d14c5d2SYehuda Sadeh #include <linux/ceph/libceph.h>
183d14c5d2SYehuda Sadeh #include <linux/ceph/osd_client.h>
193d14c5d2SYehuda Sadeh #include <linux/ceph/messenger.h>
203d14c5d2SYehuda Sadeh #include <linux/ceph/decode.h>
213d14c5d2SYehuda Sadeh #include <linux/ceph/auth.h>
223d14c5d2SYehuda Sadeh #include <linux/ceph/pagelist.h>
2308c1ac50SIlya Dryomov #include <linux/ceph/striper.h>
243d14c5d2SYehuda Sadeh 
253d14c5d2SYehuda Sadeh #define OSD_OPREPLY_FRONT_LEN	512
263d14c5d2SYehuda Sadeh 
275522ae0bSAlex Elder static struct kmem_cache	*ceph_osd_request_cache;
285522ae0bSAlex Elder 
293d14c5d2SYehuda Sadeh static const struct ceph_connection_operations osd_con_ops;
303d14c5d2SYehuda Sadeh 
313d14c5d2SYehuda Sadeh /*
323d14c5d2SYehuda Sadeh  * Implement client access to distributed object storage cluster.
333d14c5d2SYehuda Sadeh  *
343d14c5d2SYehuda Sadeh  * All data objects are stored within a cluster/cloud of OSDs, or
353d14c5d2SYehuda Sadeh  * "object storage devices."  (Note that Ceph OSDs have _nothing_ to
363d14c5d2SYehuda Sadeh  * do with the T10 OSD extensions to SCSI.)  Ceph OSDs are simply
373d14c5d2SYehuda Sadeh  * remote daemons serving up and coordinating consistent and safe
383d14c5d2SYehuda Sadeh  * access to storage.
393d14c5d2SYehuda Sadeh  *
403d14c5d2SYehuda Sadeh  * Cluster membership and the mapping of data objects onto storage devices
413d14c5d2SYehuda Sadeh  * are described by the osd map.
423d14c5d2SYehuda Sadeh  *
433d14c5d2SYehuda Sadeh  * We keep track of pending OSD requests (read, write), resubmit
443d14c5d2SYehuda Sadeh  * requests to different OSDs when the cluster topology/data layout
453d14c5d2SYehuda Sadeh  * change, or retry the affected requests when the communications
463d14c5d2SYehuda Sadeh  * channel with an OSD is reset.
473d14c5d2SYehuda Sadeh  */
483d14c5d2SYehuda Sadeh 
495aea3dcdSIlya Dryomov static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req);
505aea3dcdSIlya Dryomov static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req);
51922dab61SIlya Dryomov static void link_linger(struct ceph_osd *osd,
52922dab61SIlya Dryomov 			struct ceph_osd_linger_request *lreq);
53922dab61SIlya Dryomov static void unlink_linger(struct ceph_osd *osd,
54922dab61SIlya Dryomov 			  struct ceph_osd_linger_request *lreq);
55a02a946dSIlya Dryomov static void clear_backoffs(struct ceph_osd *osd);
565aea3dcdSIlya Dryomov 
575aea3dcdSIlya Dryomov #if 1
585aea3dcdSIlya Dryomov static inline bool rwsem_is_wrlocked(struct rw_semaphore *sem)
595aea3dcdSIlya Dryomov {
605aea3dcdSIlya Dryomov 	bool wrlocked = true;
615aea3dcdSIlya Dryomov 
625aea3dcdSIlya Dryomov 	if (unlikely(down_read_trylock(sem))) {
635aea3dcdSIlya Dryomov 		wrlocked = false;
645aea3dcdSIlya Dryomov 		up_read(sem);
655aea3dcdSIlya Dryomov 	}
665aea3dcdSIlya Dryomov 
675aea3dcdSIlya Dryomov 	return wrlocked;
685aea3dcdSIlya Dryomov }
695aea3dcdSIlya Dryomov static inline void verify_osdc_locked(struct ceph_osd_client *osdc)
705aea3dcdSIlya Dryomov {
715aea3dcdSIlya Dryomov 	WARN_ON(!rwsem_is_locked(&osdc->lock));
725aea3dcdSIlya Dryomov }
735aea3dcdSIlya Dryomov static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc)
745aea3dcdSIlya Dryomov {
755aea3dcdSIlya Dryomov 	WARN_ON(!rwsem_is_wrlocked(&osdc->lock));
765aea3dcdSIlya Dryomov }
775aea3dcdSIlya Dryomov static inline void verify_osd_locked(struct ceph_osd *osd)
785aea3dcdSIlya Dryomov {
795aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
805aea3dcdSIlya Dryomov 
815aea3dcdSIlya Dryomov 	WARN_ON(!(mutex_is_locked(&osd->lock) &&
825aea3dcdSIlya Dryomov 		  rwsem_is_locked(&osdc->lock)) &&
835aea3dcdSIlya Dryomov 		!rwsem_is_wrlocked(&osdc->lock));
845aea3dcdSIlya Dryomov }
85922dab61SIlya Dryomov static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq)
86922dab61SIlya Dryomov {
87922dab61SIlya Dryomov 	WARN_ON(!mutex_is_locked(&lreq->lock));
88922dab61SIlya Dryomov }
895aea3dcdSIlya Dryomov #else
905aea3dcdSIlya Dryomov static inline void verify_osdc_locked(struct ceph_osd_client *osdc) { }
915aea3dcdSIlya Dryomov static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc) { }
925aea3dcdSIlya Dryomov static inline void verify_osd_locked(struct ceph_osd *osd) { }
93922dab61SIlya Dryomov static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq) { }
945aea3dcdSIlya Dryomov #endif
955aea3dcdSIlya Dryomov 
963d14c5d2SYehuda Sadeh /*
973d14c5d2SYehuda Sadeh  * calculate the mapping of a file extent onto an object, and fill out the
983d14c5d2SYehuda Sadeh  * request accordingly.  shorten extent as necessary if it crosses an
993d14c5d2SYehuda Sadeh  * object boundary.
1003d14c5d2SYehuda Sadeh  *
1013d14c5d2SYehuda Sadeh  * fill osd op in request message.
1023d14c5d2SYehuda Sadeh  */
103dbe0fc41SAlex Elder static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
104a19dadfbSAlex Elder 			u64 *objnum, u64 *objoff, u64 *objlen)
1053d14c5d2SYehuda Sadeh {
10660e56f13SAlex Elder 	u64 orig_len = *plen;
107dccbf080SIlya Dryomov 	u32 xlen;
1083d14c5d2SYehuda Sadeh 
10960e56f13SAlex Elder 	/* object extent? */
110dccbf080SIlya Dryomov 	ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
111dccbf080SIlya Dryomov 					  objoff, &xlen);
112dccbf080SIlya Dryomov 	*objlen = xlen;
11375d1c941SAlex Elder 	if (*objlen < orig_len) {
11475d1c941SAlex Elder 		*plen = *objlen;
11560e56f13SAlex Elder 		dout(" skipping last %llu, final file extent %llu~%llu\n",
11660e56f13SAlex Elder 		     orig_len - *plen, off, *plen);
11760e56f13SAlex Elder 	}
11860e56f13SAlex Elder 
11975d1c941SAlex Elder 	dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
1203ff5f385SAlex Elder 	return 0;
1213d14c5d2SYehuda Sadeh }
1223d14c5d2SYehuda Sadeh 
123c54d47bfSAlex Elder static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
124c54d47bfSAlex Elder {
125c54d47bfSAlex Elder 	memset(osd_data, 0, sizeof (*osd_data));
126c54d47bfSAlex Elder 	osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
127c54d47bfSAlex Elder }
128c54d47bfSAlex Elder 
129a4ce40a9SAlex Elder static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
13043bfe5deSAlex Elder 			struct page **pages, u64 length, u32 alignment,
13143bfe5deSAlex Elder 			bool pages_from_pool, bool own_pages)
13243bfe5deSAlex Elder {
13343bfe5deSAlex Elder 	osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
13443bfe5deSAlex Elder 	osd_data->pages = pages;
13543bfe5deSAlex Elder 	osd_data->length = length;
13643bfe5deSAlex Elder 	osd_data->alignment = alignment;
13743bfe5deSAlex Elder 	osd_data->pages_from_pool = pages_from_pool;
13843bfe5deSAlex Elder 	osd_data->own_pages = own_pages;
13943bfe5deSAlex Elder }
14043bfe5deSAlex Elder 
141a4ce40a9SAlex Elder static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
14243bfe5deSAlex Elder 			struct ceph_pagelist *pagelist)
14343bfe5deSAlex Elder {
14443bfe5deSAlex Elder 	osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
14543bfe5deSAlex Elder 	osd_data->pagelist = pagelist;
14643bfe5deSAlex Elder }
14743bfe5deSAlex Elder 
14843bfe5deSAlex Elder #ifdef CONFIG_BLOCK
149a4ce40a9SAlex Elder static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
1505359a17dSIlya Dryomov 				   struct ceph_bio_iter *bio_pos,
1515359a17dSIlya Dryomov 				   u32 bio_length)
15243bfe5deSAlex Elder {
15343bfe5deSAlex Elder 	osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
1545359a17dSIlya Dryomov 	osd_data->bio_pos = *bio_pos;
15543bfe5deSAlex Elder 	osd_data->bio_length = bio_length;
15643bfe5deSAlex Elder }
15743bfe5deSAlex Elder #endif /* CONFIG_BLOCK */
15843bfe5deSAlex Elder 
159b9e281c2SIlya Dryomov static void ceph_osd_data_bvecs_init(struct ceph_osd_data *osd_data,
160b9e281c2SIlya Dryomov 				     struct ceph_bvec_iter *bvec_pos)
161b9e281c2SIlya Dryomov {
162b9e281c2SIlya Dryomov 	osd_data->type = CEPH_OSD_DATA_TYPE_BVECS;
163b9e281c2SIlya Dryomov 	osd_data->bvec_pos = *bvec_pos;
164b9e281c2SIlya Dryomov }
165b9e281c2SIlya Dryomov 
166863c7eb5SAlex Elder #define osd_req_op_data(oreq, whch, typ, fld)				\
167863c7eb5SAlex Elder ({									\
1688a703a38SIoana Ciornei 	struct ceph_osd_request *__oreq = (oreq);			\
1698a703a38SIoana Ciornei 	unsigned int __whch = (whch);					\
1708a703a38SIoana Ciornei 	BUG_ON(__whch >= __oreq->r_num_ops);				\
1718a703a38SIoana Ciornei 	&__oreq->r_ops[__whch].typ.fld;					\
172863c7eb5SAlex Elder })
173863c7eb5SAlex Elder 
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 
240b9e281c2SIlya Dryomov void osd_req_op_extent_osd_data_bvec_pos(struct ceph_osd_request *osd_req,
241b9e281c2SIlya Dryomov 					 unsigned int which,
242b9e281c2SIlya Dryomov 					 struct ceph_bvec_iter *bvec_pos)
243b9e281c2SIlya Dryomov {
244b9e281c2SIlya Dryomov 	struct ceph_osd_data *osd_data;
245b9e281c2SIlya Dryomov 
246b9e281c2SIlya Dryomov 	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
247b9e281c2SIlya Dryomov 	ceph_osd_data_bvecs_init(osd_data, bvec_pos);
248b9e281c2SIlya Dryomov }
249b9e281c2SIlya Dryomov EXPORT_SYMBOL(osd_req_op_extent_osd_data_bvec_pos);
250b9e281c2SIlya Dryomov 
251a4ce40a9SAlex Elder static void osd_req_op_cls_request_info_pagelist(
252a4ce40a9SAlex Elder 			struct ceph_osd_request *osd_req,
253a4ce40a9SAlex Elder 			unsigned int which, struct ceph_pagelist *pagelist)
254a4ce40a9SAlex Elder {
255a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
256a4ce40a9SAlex Elder 
257863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, request_info);
258a4ce40a9SAlex Elder 	ceph_osd_data_pagelist_init(osd_data, pagelist);
259a4ce40a9SAlex Elder }
260a4ce40a9SAlex Elder 
26104017e29SAlex Elder void osd_req_op_cls_request_data_pagelist(
26204017e29SAlex Elder 			struct ceph_osd_request *osd_req,
26304017e29SAlex Elder 			unsigned int which, struct ceph_pagelist *pagelist)
26404017e29SAlex Elder {
26504017e29SAlex Elder 	struct ceph_osd_data *osd_data;
26604017e29SAlex Elder 
267863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, request_data);
26804017e29SAlex Elder 	ceph_osd_data_pagelist_init(osd_data, pagelist);
269bb873b53SIlya Dryomov 	osd_req->r_ops[which].cls.indata_len += pagelist->length;
270bb873b53SIlya Dryomov 	osd_req->r_ops[which].indata_len += pagelist->length;
27104017e29SAlex Elder }
27204017e29SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
27304017e29SAlex Elder 
2746c57b554SAlex Elder void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
2756c57b554SAlex Elder 			unsigned int which, struct page **pages, u64 length,
2766c57b554SAlex Elder 			u32 alignment, bool pages_from_pool, bool own_pages)
2776c57b554SAlex Elder {
2786c57b554SAlex Elder 	struct ceph_osd_data *osd_data;
2796c57b554SAlex Elder 
2806c57b554SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, request_data);
2816c57b554SAlex Elder 	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
2826c57b554SAlex Elder 				pages_from_pool, own_pages);
283bb873b53SIlya Dryomov 	osd_req->r_ops[which].cls.indata_len += length;
284bb873b53SIlya Dryomov 	osd_req->r_ops[which].indata_len += length;
2856c57b554SAlex Elder }
2866c57b554SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
2876c57b554SAlex Elder 
288b9e281c2SIlya Dryomov void osd_req_op_cls_request_data_bvecs(struct ceph_osd_request *osd_req,
289b9e281c2SIlya Dryomov 				       unsigned int which,
290b9e281c2SIlya Dryomov 				       struct bio_vec *bvecs, u32 bytes)
291b9e281c2SIlya Dryomov {
292b9e281c2SIlya Dryomov 	struct ceph_osd_data *osd_data;
293b9e281c2SIlya Dryomov 	struct ceph_bvec_iter it = {
294b9e281c2SIlya Dryomov 		.bvecs = bvecs,
295b9e281c2SIlya Dryomov 		.iter = { .bi_size = bytes },
296b9e281c2SIlya Dryomov 	};
297b9e281c2SIlya Dryomov 
298b9e281c2SIlya Dryomov 	osd_data = osd_req_op_data(osd_req, which, cls, request_data);
299b9e281c2SIlya Dryomov 	ceph_osd_data_bvecs_init(osd_data, &it);
300b9e281c2SIlya Dryomov 	osd_req->r_ops[which].cls.indata_len += bytes;
301b9e281c2SIlya Dryomov 	osd_req->r_ops[which].indata_len += bytes;
302b9e281c2SIlya Dryomov }
303b9e281c2SIlya Dryomov EXPORT_SYMBOL(osd_req_op_cls_request_data_bvecs);
304b9e281c2SIlya Dryomov 
305a4ce40a9SAlex Elder void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
306a4ce40a9SAlex Elder 			unsigned int which, struct page **pages, u64 length,
307a4ce40a9SAlex Elder 			u32 alignment, bool pages_from_pool, bool own_pages)
308a4ce40a9SAlex Elder {
309a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
310a4ce40a9SAlex Elder 
311863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, response_data);
312a4ce40a9SAlex Elder 	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
313a4ce40a9SAlex Elder 				pages_from_pool, own_pages);
314a4ce40a9SAlex Elder }
315a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
316a4ce40a9SAlex Elder 
31723c08a9cSAlex Elder static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
31823c08a9cSAlex Elder {
31923c08a9cSAlex Elder 	switch (osd_data->type) {
32023c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_NONE:
32123c08a9cSAlex Elder 		return 0;
32223c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_PAGES:
32323c08a9cSAlex Elder 		return osd_data->length;
32423c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_PAGELIST:
32523c08a9cSAlex Elder 		return (u64)osd_data->pagelist->length;
32623c08a9cSAlex Elder #ifdef CONFIG_BLOCK
32723c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_BIO:
32823c08a9cSAlex Elder 		return (u64)osd_data->bio_length;
32923c08a9cSAlex Elder #endif /* CONFIG_BLOCK */
330b9e281c2SIlya Dryomov 	case CEPH_OSD_DATA_TYPE_BVECS:
331b9e281c2SIlya Dryomov 		return osd_data->bvec_pos.iter.bi_size;
33223c08a9cSAlex Elder 	default:
33323c08a9cSAlex Elder 		WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
33423c08a9cSAlex Elder 		return 0;
33523c08a9cSAlex Elder 	}
33623c08a9cSAlex Elder }
33723c08a9cSAlex Elder 
338c54d47bfSAlex Elder static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
339c54d47bfSAlex Elder {
3405476492fSAlex Elder 	if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
341c54d47bfSAlex Elder 		int num_pages;
342c54d47bfSAlex Elder 
343c54d47bfSAlex Elder 		num_pages = calc_pages_for((u64)osd_data->alignment,
344c54d47bfSAlex Elder 						(u64)osd_data->length);
345c54d47bfSAlex Elder 		ceph_release_page_vector(osd_data->pages, num_pages);
346c54d47bfSAlex Elder 	}
3475476492fSAlex Elder 	ceph_osd_data_init(osd_data);
3485476492fSAlex Elder }
3495476492fSAlex Elder 
3505476492fSAlex Elder static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
3515476492fSAlex Elder 			unsigned int which)
3525476492fSAlex Elder {
3535476492fSAlex Elder 	struct ceph_osd_req_op *op;
3545476492fSAlex Elder 
3555476492fSAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
3565476492fSAlex Elder 	op = &osd_req->r_ops[which];
3575476492fSAlex Elder 
3585476492fSAlex Elder 	switch (op->op) {
3595476492fSAlex Elder 	case CEPH_OSD_OP_READ:
3605476492fSAlex Elder 	case CEPH_OSD_OP_WRITE:
361e30b7577SIlya Dryomov 	case CEPH_OSD_OP_WRITEFULL:
3625476492fSAlex Elder 		ceph_osd_data_release(&op->extent.osd_data);
3635476492fSAlex Elder 		break;
3645476492fSAlex Elder 	case CEPH_OSD_OP_CALL:
3655476492fSAlex Elder 		ceph_osd_data_release(&op->cls.request_info);
36604017e29SAlex Elder 		ceph_osd_data_release(&op->cls.request_data);
3675476492fSAlex Elder 		ceph_osd_data_release(&op->cls.response_data);
3685476492fSAlex Elder 		break;
369d74b50beSYan, Zheng 	case CEPH_OSD_OP_SETXATTR:
370d74b50beSYan, Zheng 	case CEPH_OSD_OP_CMPXATTR:
371d74b50beSYan, Zheng 		ceph_osd_data_release(&op->xattr.osd_data);
372d74b50beSYan, Zheng 		break;
37366ba609fSYan, Zheng 	case CEPH_OSD_OP_STAT:
37466ba609fSYan, Zheng 		ceph_osd_data_release(&op->raw_data_in);
37566ba609fSYan, Zheng 		break;
376922dab61SIlya Dryomov 	case CEPH_OSD_OP_NOTIFY_ACK:
377922dab61SIlya Dryomov 		ceph_osd_data_release(&op->notify_ack.request_data);
378922dab61SIlya Dryomov 		break;
37919079203SIlya Dryomov 	case CEPH_OSD_OP_NOTIFY:
38019079203SIlya Dryomov 		ceph_osd_data_release(&op->notify.request_data);
38119079203SIlya Dryomov 		ceph_osd_data_release(&op->notify.response_data);
38219079203SIlya Dryomov 		break;
383a4ed38d7SDouglas Fuller 	case CEPH_OSD_OP_LIST_WATCHERS:
384a4ed38d7SDouglas Fuller 		ceph_osd_data_release(&op->list_watchers.response_data);
385a4ed38d7SDouglas Fuller 		break;
3865476492fSAlex Elder 	default:
3875476492fSAlex Elder 		break;
3885476492fSAlex Elder 	}
389c54d47bfSAlex Elder }
390c54d47bfSAlex Elder 
3913d14c5d2SYehuda Sadeh /*
39263244fa1SIlya Dryomov  * Assumes @t is zero-initialized.
39363244fa1SIlya Dryomov  */
39463244fa1SIlya Dryomov static void target_init(struct ceph_osd_request_target *t)
39563244fa1SIlya Dryomov {
39663244fa1SIlya Dryomov 	ceph_oid_init(&t->base_oid);
39763244fa1SIlya Dryomov 	ceph_oloc_init(&t->base_oloc);
39863244fa1SIlya Dryomov 	ceph_oid_init(&t->target_oid);
39963244fa1SIlya Dryomov 	ceph_oloc_init(&t->target_oloc);
40063244fa1SIlya Dryomov 
40163244fa1SIlya Dryomov 	ceph_osds_init(&t->acting);
40263244fa1SIlya Dryomov 	ceph_osds_init(&t->up);
40363244fa1SIlya Dryomov 	t->size = -1;
40463244fa1SIlya Dryomov 	t->min_size = -1;
40563244fa1SIlya Dryomov 
40663244fa1SIlya Dryomov 	t->osd = CEPH_HOMELESS_OSD;
40763244fa1SIlya Dryomov }
40863244fa1SIlya Dryomov 
409922dab61SIlya Dryomov static void target_copy(struct ceph_osd_request_target *dest,
410922dab61SIlya Dryomov 			const struct ceph_osd_request_target *src)
411922dab61SIlya Dryomov {
412922dab61SIlya Dryomov 	ceph_oid_copy(&dest->base_oid, &src->base_oid);
413922dab61SIlya Dryomov 	ceph_oloc_copy(&dest->base_oloc, &src->base_oloc);
414922dab61SIlya Dryomov 	ceph_oid_copy(&dest->target_oid, &src->target_oid);
415922dab61SIlya Dryomov 	ceph_oloc_copy(&dest->target_oloc, &src->target_oloc);
416922dab61SIlya Dryomov 
417922dab61SIlya Dryomov 	dest->pgid = src->pgid; /* struct */
418dc98ff72SIlya Dryomov 	dest->spgid = src->spgid; /* struct */
419922dab61SIlya Dryomov 	dest->pg_num = src->pg_num;
420922dab61SIlya Dryomov 	dest->pg_num_mask = src->pg_num_mask;
421922dab61SIlya Dryomov 	ceph_osds_copy(&dest->acting, &src->acting);
422922dab61SIlya Dryomov 	ceph_osds_copy(&dest->up, &src->up);
423922dab61SIlya Dryomov 	dest->size = src->size;
424922dab61SIlya Dryomov 	dest->min_size = src->min_size;
425922dab61SIlya Dryomov 	dest->sort_bitwise = src->sort_bitwise;
426922dab61SIlya Dryomov 
427922dab61SIlya Dryomov 	dest->flags = src->flags;
428922dab61SIlya Dryomov 	dest->paused = src->paused;
429922dab61SIlya Dryomov 
43004c7d789SIlya Dryomov 	dest->epoch = src->epoch;
431dc93e0e2SIlya Dryomov 	dest->last_force_resend = src->last_force_resend;
432dc93e0e2SIlya Dryomov 
433922dab61SIlya Dryomov 	dest->osd = src->osd;
434922dab61SIlya Dryomov }
435922dab61SIlya Dryomov 
43663244fa1SIlya Dryomov static void target_destroy(struct ceph_osd_request_target *t)
43763244fa1SIlya Dryomov {
43863244fa1SIlya Dryomov 	ceph_oid_destroy(&t->base_oid);
43930c156d9SYan, Zheng 	ceph_oloc_destroy(&t->base_oloc);
44063244fa1SIlya Dryomov 	ceph_oid_destroy(&t->target_oid);
44130c156d9SYan, Zheng 	ceph_oloc_destroy(&t->target_oloc);
44263244fa1SIlya Dryomov }
44363244fa1SIlya Dryomov 
44463244fa1SIlya Dryomov /*
4453d14c5d2SYehuda Sadeh  * requests
4463d14c5d2SYehuda Sadeh  */
4473540bfdbSIlya Dryomov static void request_release_checks(struct ceph_osd_request *req)
4483540bfdbSIlya Dryomov {
4493540bfdbSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&req->r_node));
4504609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&req->r_mc_node));
4513540bfdbSIlya Dryomov 	WARN_ON(!list_empty(&req->r_unsafe_item));
4523540bfdbSIlya Dryomov 	WARN_ON(req->r_osd);
4533540bfdbSIlya Dryomov }
4543540bfdbSIlya Dryomov 
4559e94af20SIlya Dryomov static void ceph_osdc_release_request(struct kref *kref)
4563d14c5d2SYehuda Sadeh {
4579e94af20SIlya Dryomov 	struct ceph_osd_request *req = container_of(kref,
4589e94af20SIlya Dryomov 					    struct ceph_osd_request, r_kref);
4595476492fSAlex Elder 	unsigned int which;
4603d14c5d2SYehuda Sadeh 
4619e94af20SIlya Dryomov 	dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
4629e94af20SIlya Dryomov 	     req->r_request, req->r_reply);
4633540bfdbSIlya Dryomov 	request_release_checks(req);
4649e94af20SIlya Dryomov 
4653d14c5d2SYehuda Sadeh 	if (req->r_request)
4663d14c5d2SYehuda Sadeh 		ceph_msg_put(req->r_request);
4675aea3dcdSIlya Dryomov 	if (req->r_reply)
468ab8cb34aSAlex Elder 		ceph_msg_put(req->r_reply);
4690fff87ecSAlex Elder 
4705476492fSAlex Elder 	for (which = 0; which < req->r_num_ops; which++)
4715476492fSAlex Elder 		osd_req_op_data_release(req, which);
4720fff87ecSAlex Elder 
473a66dd383SIlya Dryomov 	target_destroy(&req->r_t);
4743d14c5d2SYehuda Sadeh 	ceph_put_snap_context(req->r_snapc);
475d30291b9SIlya Dryomov 
4763d14c5d2SYehuda Sadeh 	if (req->r_mempool)
4773d14c5d2SYehuda Sadeh 		mempool_free(req, req->r_osdc->req_mempool);
4783f1af42aSIlya Dryomov 	else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS)
4795522ae0bSAlex Elder 		kmem_cache_free(ceph_osd_request_cache, req);
4803f1af42aSIlya Dryomov 	else
4813f1af42aSIlya Dryomov 		kfree(req);
4823d14c5d2SYehuda Sadeh }
4839e94af20SIlya Dryomov 
4849e94af20SIlya Dryomov void ceph_osdc_get_request(struct ceph_osd_request *req)
4859e94af20SIlya Dryomov {
4869e94af20SIlya Dryomov 	dout("%s %p (was %d)\n", __func__, req,
4872c935bc5SPeter Zijlstra 	     kref_read(&req->r_kref));
4889e94af20SIlya Dryomov 	kref_get(&req->r_kref);
4899e94af20SIlya Dryomov }
4909e94af20SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_get_request);
4919e94af20SIlya Dryomov 
4929e94af20SIlya Dryomov void ceph_osdc_put_request(struct ceph_osd_request *req)
4939e94af20SIlya Dryomov {
4943ed97d63SIlya Dryomov 	if (req) {
4959e94af20SIlya Dryomov 		dout("%s %p (was %d)\n", __func__, req,
4962c935bc5SPeter Zijlstra 		     kref_read(&req->r_kref));
4979e94af20SIlya Dryomov 		kref_put(&req->r_kref, ceph_osdc_release_request);
4989e94af20SIlya Dryomov 	}
4993ed97d63SIlya Dryomov }
5009e94af20SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_put_request);
5013d14c5d2SYehuda Sadeh 
5023540bfdbSIlya Dryomov static void request_init(struct ceph_osd_request *req)
5033540bfdbSIlya Dryomov {
5043540bfdbSIlya Dryomov 	/* req only, each op is zeroed in _osd_req_op_init() */
5053540bfdbSIlya Dryomov 	memset(req, 0, sizeof(*req));
5063540bfdbSIlya Dryomov 
5073540bfdbSIlya Dryomov 	kref_init(&req->r_kref);
5083540bfdbSIlya Dryomov 	init_completion(&req->r_completion);
5093540bfdbSIlya Dryomov 	RB_CLEAR_NODE(&req->r_node);
5104609245eSIlya Dryomov 	RB_CLEAR_NODE(&req->r_mc_node);
5113540bfdbSIlya Dryomov 	INIT_LIST_HEAD(&req->r_unsafe_item);
5123540bfdbSIlya Dryomov 
5133540bfdbSIlya Dryomov 	target_init(&req->r_t);
5143540bfdbSIlya Dryomov }
5153540bfdbSIlya Dryomov 
516922dab61SIlya Dryomov /*
517922dab61SIlya Dryomov  * This is ugly, but it allows us to reuse linger registration and ping
518922dab61SIlya Dryomov  * requests, keeping the structure of the code around send_linger{_ping}()
519922dab61SIlya Dryomov  * reasonable.  Setting up a min_nr=2 mempool for each linger request
520922dab61SIlya Dryomov  * and dealing with copying ops (this blasts req only, watch op remains
521922dab61SIlya Dryomov  * intact) isn't any better.
522922dab61SIlya Dryomov  */
523922dab61SIlya Dryomov static void request_reinit(struct ceph_osd_request *req)
524922dab61SIlya Dryomov {
525922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
526922dab61SIlya Dryomov 	bool mempool = req->r_mempool;
527922dab61SIlya Dryomov 	unsigned int num_ops = req->r_num_ops;
528922dab61SIlya Dryomov 	u64 snapid = req->r_snapid;
529922dab61SIlya Dryomov 	struct ceph_snap_context *snapc = req->r_snapc;
530922dab61SIlya Dryomov 	bool linger = req->r_linger;
531922dab61SIlya Dryomov 	struct ceph_msg *request_msg = req->r_request;
532922dab61SIlya Dryomov 	struct ceph_msg *reply_msg = req->r_reply;
533922dab61SIlya Dryomov 
534922dab61SIlya Dryomov 	dout("%s req %p\n", __func__, req);
5352c935bc5SPeter Zijlstra 	WARN_ON(kref_read(&req->r_kref) != 1);
536922dab61SIlya Dryomov 	request_release_checks(req);
537922dab61SIlya Dryomov 
5382c935bc5SPeter Zijlstra 	WARN_ON(kref_read(&request_msg->kref) != 1);
5392c935bc5SPeter Zijlstra 	WARN_ON(kref_read(&reply_msg->kref) != 1);
540922dab61SIlya Dryomov 	target_destroy(&req->r_t);
541922dab61SIlya Dryomov 
542922dab61SIlya Dryomov 	request_init(req);
543922dab61SIlya Dryomov 	req->r_osdc = osdc;
544922dab61SIlya Dryomov 	req->r_mempool = mempool;
545922dab61SIlya Dryomov 	req->r_num_ops = num_ops;
546922dab61SIlya Dryomov 	req->r_snapid = snapid;
547922dab61SIlya Dryomov 	req->r_snapc = snapc;
548922dab61SIlya Dryomov 	req->r_linger = linger;
549922dab61SIlya Dryomov 	req->r_request = request_msg;
550922dab61SIlya Dryomov 	req->r_reply = reply_msg;
551922dab61SIlya Dryomov }
552922dab61SIlya Dryomov 
5533d14c5d2SYehuda Sadeh struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
5543d14c5d2SYehuda Sadeh 					       struct ceph_snap_context *snapc,
5551b83bef2SSage Weil 					       unsigned int num_ops,
5563d14c5d2SYehuda Sadeh 					       bool use_mempool,
55754a54007SAlex Elder 					       gfp_t gfp_flags)
5583d14c5d2SYehuda Sadeh {
5593d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
5603d14c5d2SYehuda Sadeh 
5613d14c5d2SYehuda Sadeh 	if (use_mempool) {
5623f1af42aSIlya Dryomov 		BUG_ON(num_ops > CEPH_OSD_SLAB_OPS);
5633d14c5d2SYehuda Sadeh 		req = mempool_alloc(osdc->req_mempool, gfp_flags);
5643f1af42aSIlya Dryomov 	} else if (num_ops <= CEPH_OSD_SLAB_OPS) {
5653f1af42aSIlya Dryomov 		req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
5663d14c5d2SYehuda Sadeh 	} else {
5673f1af42aSIlya Dryomov 		BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
5683f1af42aSIlya Dryomov 		req = kmalloc(sizeof(*req) + num_ops * sizeof(req->r_ops[0]),
5693f1af42aSIlya Dryomov 			      gfp_flags);
5703d14c5d2SYehuda Sadeh 	}
5713f1af42aSIlya Dryomov 	if (unlikely(!req))
5723d14c5d2SYehuda Sadeh 		return NULL;
5733d14c5d2SYehuda Sadeh 
5743540bfdbSIlya Dryomov 	request_init(req);
5753d14c5d2SYehuda Sadeh 	req->r_osdc = osdc;
5763d14c5d2SYehuda Sadeh 	req->r_mempool = use_mempool;
57779528734SAlex Elder 	req->r_num_ops = num_ops;
57884127282SIlya Dryomov 	req->r_snapid = CEPH_NOSNAP;
57984127282SIlya Dryomov 	req->r_snapc = ceph_get_snap_context(snapc);
5803d14c5d2SYehuda Sadeh 
58113d1ad16SIlya Dryomov 	dout("%s req %p\n", __func__, req);
58213d1ad16SIlya Dryomov 	return req;
5833f1af42aSIlya Dryomov }
58413d1ad16SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_alloc_request);
5853f1af42aSIlya Dryomov 
5862e59ffd1SIlya Dryomov static int ceph_oloc_encoding_size(const struct ceph_object_locator *oloc)
58730c156d9SYan, Zheng {
58830c156d9SYan, Zheng 	return 8 + 4 + 4 + 4 + (oloc->pool_ns ? oloc->pool_ns->len : 0);
58930c156d9SYan, Zheng }
59030c156d9SYan, Zheng 
59113d1ad16SIlya Dryomov int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp)
59213d1ad16SIlya Dryomov {
59313d1ad16SIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
59413d1ad16SIlya Dryomov 	struct ceph_msg *msg;
59513d1ad16SIlya Dryomov 	int msg_size;
5963d14c5d2SYehuda Sadeh 
597d30291b9SIlya Dryomov 	WARN_ON(ceph_oid_empty(&req->r_base_oid));
59830c156d9SYan, Zheng 	WARN_ON(ceph_oloc_empty(&req->r_base_oloc));
599d30291b9SIlya Dryomov 
60013d1ad16SIlya Dryomov 	/* create request message */
6018cb441c0SIlya Dryomov 	msg_size = CEPH_ENCODING_START_BLK_LEN +
6028cb441c0SIlya Dryomov 			CEPH_PGID_ENCODING_LEN + 1; /* spgid */
6038cb441c0SIlya Dryomov 	msg_size += 4 + 4 + 4; /* hash, osdmap_epoch, flags */
6048cb441c0SIlya Dryomov 	msg_size += CEPH_ENCODING_START_BLK_LEN +
6058cb441c0SIlya Dryomov 			sizeof(struct ceph_osd_reqid); /* reqid */
6068cb441c0SIlya Dryomov 	msg_size += sizeof(struct ceph_blkin_trace_info); /* trace */
6078cb441c0SIlya Dryomov 	msg_size += 4 + sizeof(struct ceph_timespec); /* client_inc, mtime */
60830c156d9SYan, Zheng 	msg_size += CEPH_ENCODING_START_BLK_LEN +
60930c156d9SYan, Zheng 			ceph_oloc_encoding_size(&req->r_base_oloc); /* oloc */
61013d1ad16SIlya Dryomov 	msg_size += 4 + req->r_base_oid.name_len; /* oid */
61113d1ad16SIlya Dryomov 	msg_size += 2 + req->r_num_ops * sizeof(struct ceph_osd_op);
612ae458f5aSIlya Dryomov 	msg_size += 8; /* snapid */
613ae458f5aSIlya Dryomov 	msg_size += 8; /* snap_seq */
61413d1ad16SIlya Dryomov 	msg_size += 4 + 8 * (req->r_snapc ? req->r_snapc->num_snaps : 0);
6158cb441c0SIlya Dryomov 	msg_size += 4 + 8; /* retry_attempt, features */
616ae458f5aSIlya Dryomov 
61713d1ad16SIlya Dryomov 	if (req->r_mempool)
6183d14c5d2SYehuda Sadeh 		msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
6193d14c5d2SYehuda Sadeh 	else
62013d1ad16SIlya Dryomov 		msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp, true);
62113d1ad16SIlya Dryomov 	if (!msg)
62213d1ad16SIlya Dryomov 		return -ENOMEM;
6233d14c5d2SYehuda Sadeh 
6243d14c5d2SYehuda Sadeh 	memset(msg->front.iov_base, 0, msg->front.iov_len);
6253d14c5d2SYehuda Sadeh 	req->r_request = msg;
6263d14c5d2SYehuda Sadeh 
62713d1ad16SIlya Dryomov 	/* create reply message */
62813d1ad16SIlya Dryomov 	msg_size = OSD_OPREPLY_FRONT_LEN;
629711da55dSIlya Dryomov 	msg_size += req->r_base_oid.name_len;
630711da55dSIlya Dryomov 	msg_size += req->r_num_ops * sizeof(struct ceph_osd_op);
63113d1ad16SIlya Dryomov 
63213d1ad16SIlya Dryomov 	if (req->r_mempool)
63313d1ad16SIlya Dryomov 		msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
63413d1ad16SIlya Dryomov 	else
63513d1ad16SIlya Dryomov 		msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, msg_size, gfp, true);
63613d1ad16SIlya Dryomov 	if (!msg)
63713d1ad16SIlya Dryomov 		return -ENOMEM;
63813d1ad16SIlya Dryomov 
63913d1ad16SIlya Dryomov 	req->r_reply = msg;
64013d1ad16SIlya Dryomov 
64113d1ad16SIlya Dryomov 	return 0;
64213d1ad16SIlya Dryomov }
64313d1ad16SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_alloc_messages);
6443d14c5d2SYehuda Sadeh 
645a8dd0a37SAlex Elder static bool osd_req_opcode_valid(u16 opcode)
646a8dd0a37SAlex Elder {
647a8dd0a37SAlex Elder 	switch (opcode) {
64870b5bfa3SIlya Dryomov #define GENERATE_CASE(op, opcode, str)	case CEPH_OSD_OP_##op: return true;
64970b5bfa3SIlya Dryomov __CEPH_FORALL_OSD_OPS(GENERATE_CASE)
65070b5bfa3SIlya Dryomov #undef GENERATE_CASE
651a8dd0a37SAlex Elder 	default:
652a8dd0a37SAlex Elder 		return false;
653a8dd0a37SAlex Elder 	}
654a8dd0a37SAlex Elder }
655a8dd0a37SAlex Elder 
65633803f33SAlex Elder /*
65733803f33SAlex Elder  * This is an osd op init function for opcodes that have no data or
65833803f33SAlex Elder  * other information associated with them.  It also serves as a
65933803f33SAlex Elder  * common init routine for all the other init functions, below.
66033803f33SAlex Elder  */
661c99d2d4aSAlex Elder static struct ceph_osd_req_op *
66249719778SAlex Elder _osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
663144cba14SYan, Zheng 		 u16 opcode, u32 flags)
66433803f33SAlex Elder {
665c99d2d4aSAlex Elder 	struct ceph_osd_req_op *op;
666c99d2d4aSAlex Elder 
667c99d2d4aSAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
66833803f33SAlex Elder 	BUG_ON(!osd_req_opcode_valid(opcode));
66933803f33SAlex Elder 
670c99d2d4aSAlex Elder 	op = &osd_req->r_ops[which];
67133803f33SAlex Elder 	memset(op, 0, sizeof (*op));
67233803f33SAlex Elder 	op->op = opcode;
673144cba14SYan, Zheng 	op->flags = flags;
674c99d2d4aSAlex Elder 
675c99d2d4aSAlex Elder 	return op;
67633803f33SAlex Elder }
67733803f33SAlex Elder 
67849719778SAlex Elder void osd_req_op_init(struct ceph_osd_request *osd_req,
679144cba14SYan, Zheng 		     unsigned int which, u16 opcode, u32 flags)
68049719778SAlex Elder {
681144cba14SYan, Zheng 	(void)_osd_req_op_init(osd_req, which, opcode, flags);
68249719778SAlex Elder }
68349719778SAlex Elder EXPORT_SYMBOL(osd_req_op_init);
68449719778SAlex Elder 
685c99d2d4aSAlex Elder void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
686c99d2d4aSAlex Elder 				unsigned int which, u16 opcode,
68733803f33SAlex Elder 				u64 offset, u64 length,
68833803f33SAlex Elder 				u64 truncate_size, u32 truncate_seq)
68933803f33SAlex Elder {
690144cba14SYan, Zheng 	struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
691144cba14SYan, Zheng 						      opcode, 0);
69233803f33SAlex Elder 	size_t payload_len = 0;
69333803f33SAlex Elder 
694ad7a60deSLi Wang 	BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
695e30b7577SIlya Dryomov 	       opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
696e30b7577SIlya Dryomov 	       opcode != CEPH_OSD_OP_TRUNCATE);
69733803f33SAlex Elder 
69833803f33SAlex Elder 	op->extent.offset = offset;
69933803f33SAlex Elder 	op->extent.length = length;
70033803f33SAlex Elder 	op->extent.truncate_size = truncate_size;
70133803f33SAlex Elder 	op->extent.truncate_seq = truncate_seq;
702e30b7577SIlya Dryomov 	if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
70333803f33SAlex Elder 		payload_len += length;
70433803f33SAlex Elder 
705de2aa102SIlya Dryomov 	op->indata_len = payload_len;
70633803f33SAlex Elder }
70733803f33SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_init);
70833803f33SAlex Elder 
709c99d2d4aSAlex Elder void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
710c99d2d4aSAlex Elder 				unsigned int which, u64 length)
711e5975c7cSAlex Elder {
712c99d2d4aSAlex Elder 	struct ceph_osd_req_op *op;
713c99d2d4aSAlex Elder 	u64 previous;
714c99d2d4aSAlex Elder 
715c99d2d4aSAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
716c99d2d4aSAlex Elder 	op = &osd_req->r_ops[which];
717c99d2d4aSAlex Elder 	previous = op->extent.length;
718e5975c7cSAlex Elder 
719e5975c7cSAlex Elder 	if (length == previous)
720e5975c7cSAlex Elder 		return;		/* Nothing to do */
721e5975c7cSAlex Elder 	BUG_ON(length > previous);
722e5975c7cSAlex Elder 
723e5975c7cSAlex Elder 	op->extent.length = length;
724d641df81SYan, Zheng 	if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
725de2aa102SIlya Dryomov 		op->indata_len -= previous - length;
726e5975c7cSAlex Elder }
727e5975c7cSAlex Elder EXPORT_SYMBOL(osd_req_op_extent_update);
728e5975c7cSAlex Elder 
7292c63f49aSYan, Zheng void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
7302c63f49aSYan, Zheng 				unsigned int which, u64 offset_inc)
7312c63f49aSYan, Zheng {
7322c63f49aSYan, Zheng 	struct ceph_osd_req_op *op, *prev_op;
7332c63f49aSYan, Zheng 
7342c63f49aSYan, Zheng 	BUG_ON(which + 1 >= osd_req->r_num_ops);
7352c63f49aSYan, Zheng 
7362c63f49aSYan, Zheng 	prev_op = &osd_req->r_ops[which];
7372c63f49aSYan, Zheng 	op = _osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags);
7382c63f49aSYan, Zheng 	/* dup previous one */
7392c63f49aSYan, Zheng 	op->indata_len = prev_op->indata_len;
7402c63f49aSYan, Zheng 	op->outdata_len = prev_op->outdata_len;
7412c63f49aSYan, Zheng 	op->extent = prev_op->extent;
7422c63f49aSYan, Zheng 	/* adjust offset */
7432c63f49aSYan, Zheng 	op->extent.offset += offset_inc;
7442c63f49aSYan, Zheng 	op->extent.length -= offset_inc;
7452c63f49aSYan, Zheng 
7462c63f49aSYan, Zheng 	if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
7472c63f49aSYan, Zheng 		op->indata_len -= offset_inc;
7482c63f49aSYan, Zheng }
7492c63f49aSYan, Zheng EXPORT_SYMBOL(osd_req_op_extent_dup_last);
7502c63f49aSYan, Zheng 
751c99d2d4aSAlex Elder void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
75204017e29SAlex Elder 			u16 opcode, const char *class, const char *method)
75333803f33SAlex Elder {
754144cba14SYan, Zheng 	struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
755144cba14SYan, Zheng 						      opcode, 0);
7565f562df5SAlex Elder 	struct ceph_pagelist *pagelist;
75733803f33SAlex Elder 	size_t payload_len = 0;
75833803f33SAlex Elder 	size_t size;
75933803f33SAlex Elder 
76033803f33SAlex Elder 	BUG_ON(opcode != CEPH_OSD_OP_CALL);
76133803f33SAlex Elder 
7625f562df5SAlex Elder 	pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
7635f562df5SAlex Elder 	BUG_ON(!pagelist);
7645f562df5SAlex Elder 	ceph_pagelist_init(pagelist);
7655f562df5SAlex Elder 
76633803f33SAlex Elder 	op->cls.class_name = class;
76733803f33SAlex Elder 	size = strlen(class);
76833803f33SAlex Elder 	BUG_ON(size > (size_t) U8_MAX);
76933803f33SAlex Elder 	op->cls.class_len = size;
7705f562df5SAlex Elder 	ceph_pagelist_append(pagelist, class, size);
77133803f33SAlex Elder 	payload_len += size;
77233803f33SAlex Elder 
77333803f33SAlex Elder 	op->cls.method_name = method;
77433803f33SAlex Elder 	size = strlen(method);
77533803f33SAlex Elder 	BUG_ON(size > (size_t) U8_MAX);
77633803f33SAlex Elder 	op->cls.method_len = size;
7775f562df5SAlex Elder 	ceph_pagelist_append(pagelist, method, size);
77833803f33SAlex Elder 	payload_len += size;
77933803f33SAlex Elder 
780a4ce40a9SAlex Elder 	osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
7815f562df5SAlex Elder 
782de2aa102SIlya Dryomov 	op->indata_len = payload_len;
78333803f33SAlex Elder }
78433803f33SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_init);
7858c042b0dSAlex Elder 
786d74b50beSYan, Zheng int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
787d74b50beSYan, Zheng 			  u16 opcode, const char *name, const void *value,
788d74b50beSYan, Zheng 			  size_t size, u8 cmp_op, u8 cmp_mode)
789d74b50beSYan, Zheng {
790144cba14SYan, Zheng 	struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
791144cba14SYan, Zheng 						      opcode, 0);
792d74b50beSYan, Zheng 	struct ceph_pagelist *pagelist;
793d74b50beSYan, Zheng 	size_t payload_len;
794d74b50beSYan, Zheng 
795d74b50beSYan, Zheng 	BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
796d74b50beSYan, Zheng 
797d74b50beSYan, Zheng 	pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
798d74b50beSYan, Zheng 	if (!pagelist)
799d74b50beSYan, Zheng 		return -ENOMEM;
800d74b50beSYan, Zheng 
801d74b50beSYan, Zheng 	ceph_pagelist_init(pagelist);
802d74b50beSYan, Zheng 
803d74b50beSYan, Zheng 	payload_len = strlen(name);
804d74b50beSYan, Zheng 	op->xattr.name_len = payload_len;
805d74b50beSYan, Zheng 	ceph_pagelist_append(pagelist, name, payload_len);
806d74b50beSYan, Zheng 
807d74b50beSYan, Zheng 	op->xattr.value_len = size;
808d74b50beSYan, Zheng 	ceph_pagelist_append(pagelist, value, size);
809d74b50beSYan, Zheng 	payload_len += size;
810d74b50beSYan, Zheng 
811d74b50beSYan, Zheng 	op->xattr.cmp_op = cmp_op;
812d74b50beSYan, Zheng 	op->xattr.cmp_mode = cmp_mode;
813d74b50beSYan, Zheng 
814d74b50beSYan, Zheng 	ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
815de2aa102SIlya Dryomov 	op->indata_len = payload_len;
816d74b50beSYan, Zheng 	return 0;
817d74b50beSYan, Zheng }
818d74b50beSYan, Zheng EXPORT_SYMBOL(osd_req_op_xattr_init);
819d74b50beSYan, Zheng 
820922dab61SIlya Dryomov /*
821922dab61SIlya Dryomov  * @watch_opcode: CEPH_OSD_WATCH_OP_*
822922dab61SIlya Dryomov  */
823922dab61SIlya Dryomov static void osd_req_op_watch_init(struct ceph_osd_request *req, int which,
824922dab61SIlya Dryomov 				  u64 cookie, u8 watch_opcode)
82533803f33SAlex Elder {
826922dab61SIlya Dryomov 	struct ceph_osd_req_op *op;
82733803f33SAlex Elder 
828922dab61SIlya Dryomov 	op = _osd_req_op_init(req, which, CEPH_OSD_OP_WATCH, 0);
82933803f33SAlex Elder 	op->watch.cookie = cookie;
830922dab61SIlya Dryomov 	op->watch.op = watch_opcode;
831922dab61SIlya Dryomov 	op->watch.gen = 0;
83233803f33SAlex Elder }
83333803f33SAlex Elder 
834c647b8a8SIlya Dryomov void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
835c647b8a8SIlya Dryomov 				unsigned int which,
836c647b8a8SIlya Dryomov 				u64 expected_object_size,
837c647b8a8SIlya Dryomov 				u64 expected_write_size)
838c647b8a8SIlya Dryomov {
839c647b8a8SIlya Dryomov 	struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
840144cba14SYan, Zheng 						      CEPH_OSD_OP_SETALLOCHINT,
841144cba14SYan, Zheng 						      0);
842c647b8a8SIlya Dryomov 
843c647b8a8SIlya Dryomov 	op->alloc_hint.expected_object_size = expected_object_size;
844c647b8a8SIlya Dryomov 	op->alloc_hint.expected_write_size = expected_write_size;
845c647b8a8SIlya Dryomov 
846c647b8a8SIlya Dryomov 	/*
847c647b8a8SIlya Dryomov 	 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
848c647b8a8SIlya Dryomov 	 * not worth a feature bit.  Set FAILOK per-op flag to make
849c647b8a8SIlya Dryomov 	 * sure older osds don't trip over an unsupported opcode.
850c647b8a8SIlya Dryomov 	 */
851c647b8a8SIlya Dryomov 	op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
852c647b8a8SIlya Dryomov }
853c647b8a8SIlya Dryomov EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
854c647b8a8SIlya Dryomov 
85590af3602SAlex Elder static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
856ec9123c5SAlex Elder 				struct ceph_osd_data *osd_data)
857ec9123c5SAlex Elder {
858ec9123c5SAlex Elder 	u64 length = ceph_osd_data_length(osd_data);
859ec9123c5SAlex Elder 
860ec9123c5SAlex Elder 	if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
861ec9123c5SAlex Elder 		BUG_ON(length > (u64) SIZE_MAX);
862ec9123c5SAlex Elder 		if (length)
86390af3602SAlex Elder 			ceph_msg_data_add_pages(msg, osd_data->pages,
864ec9123c5SAlex Elder 					length, osd_data->alignment);
865ec9123c5SAlex Elder 	} else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
866ec9123c5SAlex Elder 		BUG_ON(!length);
86790af3602SAlex Elder 		ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
868ec9123c5SAlex Elder #ifdef CONFIG_BLOCK
869ec9123c5SAlex Elder 	} else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
8705359a17dSIlya Dryomov 		ceph_msg_data_add_bio(msg, &osd_data->bio_pos, length);
871ec9123c5SAlex Elder #endif
872b9e281c2SIlya Dryomov 	} else if (osd_data->type == CEPH_OSD_DATA_TYPE_BVECS) {
873b9e281c2SIlya Dryomov 		ceph_msg_data_add_bvecs(msg, &osd_data->bvec_pos);
874ec9123c5SAlex Elder 	} else {
875ec9123c5SAlex Elder 		BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
876ec9123c5SAlex Elder 	}
877ec9123c5SAlex Elder }
878ec9123c5SAlex Elder 
879bb873b53SIlya Dryomov static u32 osd_req_encode_op(struct ceph_osd_op *dst,
880bb873b53SIlya Dryomov 			     const struct ceph_osd_req_op *src)
8813d14c5d2SYehuda Sadeh {
882a8dd0a37SAlex Elder 	if (WARN_ON(!osd_req_opcode_valid(src->op))) {
883a8dd0a37SAlex Elder 		pr_err("unrecognized osd opcode %d\n", src->op);
884a8dd0a37SAlex Elder 
885a8dd0a37SAlex Elder 		return 0;
886a8dd0a37SAlex Elder 	}
8873d14c5d2SYehuda Sadeh 
888065a68f9SAlex Elder 	switch (src->op) {
889fbfab539SAlex Elder 	case CEPH_OSD_OP_STAT:
890fbfab539SAlex Elder 		break;
8913d14c5d2SYehuda Sadeh 	case CEPH_OSD_OP_READ:
8923d14c5d2SYehuda Sadeh 	case CEPH_OSD_OP_WRITE:
893e30b7577SIlya Dryomov 	case CEPH_OSD_OP_WRITEFULL:
894ad7a60deSLi Wang 	case CEPH_OSD_OP_ZERO:
895ad7a60deSLi Wang 	case CEPH_OSD_OP_TRUNCATE:
896175face2SAlex Elder 		dst->extent.offset = cpu_to_le64(src->extent.offset);
897175face2SAlex Elder 		dst->extent.length = cpu_to_le64(src->extent.length);
8983d14c5d2SYehuda Sadeh 		dst->extent.truncate_size =
8993d14c5d2SYehuda Sadeh 			cpu_to_le64(src->extent.truncate_size);
9003d14c5d2SYehuda Sadeh 		dst->extent.truncate_seq =
9013d14c5d2SYehuda Sadeh 			cpu_to_le32(src->extent.truncate_seq);
9023d14c5d2SYehuda Sadeh 		break;
9033d14c5d2SYehuda Sadeh 	case CEPH_OSD_OP_CALL:
9043d14c5d2SYehuda Sadeh 		dst->cls.class_len = src->cls.class_len;
9053d14c5d2SYehuda Sadeh 		dst->cls.method_len = src->cls.method_len;
906bb873b53SIlya Dryomov 		dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
9073d14c5d2SYehuda Sadeh 		break;
908a40c4f10SYehuda Sadeh 	case CEPH_OSD_OP_WATCH:
909a40c4f10SYehuda Sadeh 		dst->watch.cookie = cpu_to_le64(src->watch.cookie);
910922dab61SIlya Dryomov 		dst->watch.ver = cpu_to_le64(0);
911922dab61SIlya Dryomov 		dst->watch.op = src->watch.op;
912922dab61SIlya Dryomov 		dst->watch.gen = cpu_to_le32(src->watch.gen);
913922dab61SIlya Dryomov 		break;
914922dab61SIlya Dryomov 	case CEPH_OSD_OP_NOTIFY_ACK:
915a40c4f10SYehuda Sadeh 		break;
91619079203SIlya Dryomov 	case CEPH_OSD_OP_NOTIFY:
91719079203SIlya Dryomov 		dst->notify.cookie = cpu_to_le64(src->notify.cookie);
91819079203SIlya Dryomov 		break;
919a4ed38d7SDouglas Fuller 	case CEPH_OSD_OP_LIST_WATCHERS:
920a4ed38d7SDouglas Fuller 		break;
921c647b8a8SIlya Dryomov 	case CEPH_OSD_OP_SETALLOCHINT:
922c647b8a8SIlya Dryomov 		dst->alloc_hint.expected_object_size =
923c647b8a8SIlya Dryomov 		    cpu_to_le64(src->alloc_hint.expected_object_size);
924c647b8a8SIlya Dryomov 		dst->alloc_hint.expected_write_size =
925c647b8a8SIlya Dryomov 		    cpu_to_le64(src->alloc_hint.expected_write_size);
926c647b8a8SIlya Dryomov 		break;
927d74b50beSYan, Zheng 	case CEPH_OSD_OP_SETXATTR:
928d74b50beSYan, Zheng 	case CEPH_OSD_OP_CMPXATTR:
929d74b50beSYan, Zheng 		dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
930d74b50beSYan, Zheng 		dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
931d74b50beSYan, Zheng 		dst->xattr.cmp_op = src->xattr.cmp_op;
932d74b50beSYan, Zheng 		dst->xattr.cmp_mode = src->xattr.cmp_mode;
933d74b50beSYan, Zheng 		break;
934864e9197SYan, Zheng 	case CEPH_OSD_OP_CREATE:
935864e9197SYan, Zheng 	case CEPH_OSD_OP_DELETE:
936864e9197SYan, Zheng 		break;
9373d14c5d2SYehuda Sadeh 	default:
9384c46459cSAlex Elder 		pr_err("unsupported osd opcode %s\n",
9398f63ca2dSAlex Elder 			ceph_osd_op_name(src->op));
9404c46459cSAlex Elder 		WARN_ON(1);
941a8dd0a37SAlex Elder 
942a8dd0a37SAlex Elder 		return 0;
9433d14c5d2SYehuda Sadeh 	}
9447b25bf5fSIlya Dryomov 
945a8dd0a37SAlex Elder 	dst->op = cpu_to_le16(src->op);
9467b25bf5fSIlya Dryomov 	dst->flags = cpu_to_le32(src->flags);
947de2aa102SIlya Dryomov 	dst->payload_len = cpu_to_le32(src->indata_len);
948175face2SAlex Elder 
949bb873b53SIlya Dryomov 	return src->indata_len;
9503d14c5d2SYehuda Sadeh }
9513d14c5d2SYehuda Sadeh 
9523d14c5d2SYehuda Sadeh /*
9533d14c5d2SYehuda Sadeh  * build new request AND message, calculate layout, and adjust file
9543d14c5d2SYehuda Sadeh  * extent as needed.
9553d14c5d2SYehuda Sadeh  *
9563d14c5d2SYehuda Sadeh  * if the file was recently truncated, we include information about its
9573d14c5d2SYehuda Sadeh  * old and new size so that the object can be updated appropriately.  (we
9583d14c5d2SYehuda Sadeh  * avoid synchronously deleting truncated objects because it's slow.)
9593d14c5d2SYehuda Sadeh  */
9603d14c5d2SYehuda Sadeh struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
9613d14c5d2SYehuda Sadeh 					       struct ceph_file_layout *layout,
9623d14c5d2SYehuda Sadeh 					       struct ceph_vino vino,
963715e4cd4SYan, Zheng 					       u64 off, u64 *plen,
964715e4cd4SYan, Zheng 					       unsigned int which, int num_ops,
9653d14c5d2SYehuda Sadeh 					       int opcode, int flags,
9663d14c5d2SYehuda Sadeh 					       struct ceph_snap_context *snapc,
9673d14c5d2SYehuda Sadeh 					       u32 truncate_seq,
9683d14c5d2SYehuda Sadeh 					       u64 truncate_size,
969153e5167SAlex Elder 					       bool use_mempool)
9703d14c5d2SYehuda Sadeh {
9713d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
97275d1c941SAlex Elder 	u64 objnum = 0;
97375d1c941SAlex Elder 	u64 objoff = 0;
97475d1c941SAlex Elder 	u64 objlen = 0;
9756816282dSSage Weil 	int r;
9763d14c5d2SYehuda Sadeh 
977ad7a60deSLi Wang 	BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
978864e9197SYan, Zheng 	       opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
979864e9197SYan, Zheng 	       opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
9803d14c5d2SYehuda Sadeh 
981acead002SAlex Elder 	req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
982ae7ca4a3SAlex Elder 					GFP_NOFS);
98313d1ad16SIlya Dryomov 	if (!req) {
98413d1ad16SIlya Dryomov 		r = -ENOMEM;
98513d1ad16SIlya Dryomov 		goto fail;
98613d1ad16SIlya Dryomov 	}
98779528734SAlex Elder 
9883d14c5d2SYehuda Sadeh 	/* calculate max write size */
989a19dadfbSAlex Elder 	r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
99013d1ad16SIlya Dryomov 	if (r)
99113d1ad16SIlya Dryomov 		goto fail;
992a19dadfbSAlex Elder 
993864e9197SYan, Zheng 	if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
994144cba14SYan, Zheng 		osd_req_op_init(req, which, opcode, 0);
995864e9197SYan, Zheng 	} else {
9967627151eSYan, Zheng 		u32 object_size = layout->object_size;
997864e9197SYan, Zheng 		u32 object_base = off - objoff;
998ccca4e37SYan, Zheng 		if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
999d18d1e28SAlex Elder 			if (truncate_size <= object_base) {
1000d18d1e28SAlex Elder 				truncate_size = 0;
1001d18d1e28SAlex Elder 			} else {
1002d18d1e28SAlex Elder 				truncate_size -= object_base;
1003d18d1e28SAlex Elder 				if (truncate_size > object_size)
1004d18d1e28SAlex Elder 					truncate_size = object_size;
1005d18d1e28SAlex Elder 			}
1006ccca4e37SYan, Zheng 		}
1007715e4cd4SYan, Zheng 		osd_req_op_extent_init(req, which, opcode, objoff, objlen,
1008b0270324SAlex Elder 				       truncate_size, truncate_seq);
1009864e9197SYan, Zheng 	}
1010d18d1e28SAlex Elder 
1011a1f4020aSJeff Layton 	req->r_abort_on_full = true;
1012bb873b53SIlya Dryomov 	req->r_flags = flags;
10137627151eSYan, Zheng 	req->r_base_oloc.pool = layout->pool_id;
101430c156d9SYan, Zheng 	req->r_base_oloc.pool_ns = ceph_try_get_string(layout->pool_ns);
1015d30291b9SIlya Dryomov 	ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum);
1016dbe0fc41SAlex Elder 
1017bb873b53SIlya Dryomov 	req->r_snapid = vino.snap;
1018bb873b53SIlya Dryomov 	if (flags & CEPH_OSD_FLAG_WRITE)
1019bb873b53SIlya Dryomov 		req->r_data_offset = off;
1020bb873b53SIlya Dryomov 
102113d1ad16SIlya Dryomov 	r = ceph_osdc_alloc_messages(req, GFP_NOFS);
102213d1ad16SIlya Dryomov 	if (r)
102313d1ad16SIlya Dryomov 		goto fail;
102413d1ad16SIlya Dryomov 
10253d14c5d2SYehuda Sadeh 	return req;
102613d1ad16SIlya Dryomov 
102713d1ad16SIlya Dryomov fail:
102813d1ad16SIlya Dryomov 	ceph_osdc_put_request(req);
102913d1ad16SIlya Dryomov 	return ERR_PTR(r);
10303d14c5d2SYehuda Sadeh }
10313d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_new_request);
10323d14c5d2SYehuda Sadeh 
10333d14c5d2SYehuda Sadeh /*
10343d14c5d2SYehuda Sadeh  * We keep osd requests in an rbtree, sorted by ->r_tid.
10353d14c5d2SYehuda Sadeh  */
1036fcd00b68SIlya Dryomov DEFINE_RB_FUNCS(request, struct ceph_osd_request, r_tid, r_node)
10374609245eSIlya Dryomov DEFINE_RB_FUNCS(request_mc, struct ceph_osd_request, r_tid, r_mc_node)
10383d14c5d2SYehuda Sadeh 
10390247a0cfSIlya Dryomov static bool osd_homeless(struct ceph_osd *osd)
10400247a0cfSIlya Dryomov {
10410247a0cfSIlya Dryomov 	return osd->o_osd == CEPH_HOMELESS_OSD;
10420247a0cfSIlya Dryomov }
10430247a0cfSIlya Dryomov 
10445aea3dcdSIlya Dryomov static bool osd_registered(struct ceph_osd *osd)
10453d14c5d2SYehuda Sadeh {
10465aea3dcdSIlya Dryomov 	verify_osdc_locked(osd->o_osdc);
10473d14c5d2SYehuda Sadeh 
10485aea3dcdSIlya Dryomov 	return !RB_EMPTY_NODE(&osd->o_node);
10493d14c5d2SYehuda Sadeh }
10503d14c5d2SYehuda Sadeh 
10513d14c5d2SYehuda Sadeh /*
10520247a0cfSIlya Dryomov  * Assumes @osd is zero-initialized.
10530247a0cfSIlya Dryomov  */
10540247a0cfSIlya Dryomov static void osd_init(struct ceph_osd *osd)
10550247a0cfSIlya Dryomov {
105602113a0fSElena Reshetova 	refcount_set(&osd->o_ref, 1);
10570247a0cfSIlya Dryomov 	RB_CLEAR_NODE(&osd->o_node);
10585aea3dcdSIlya Dryomov 	osd->o_requests = RB_ROOT;
1059922dab61SIlya Dryomov 	osd->o_linger_requests = RB_ROOT;
1060a02a946dSIlya Dryomov 	osd->o_backoff_mappings = RB_ROOT;
1061a02a946dSIlya Dryomov 	osd->o_backoffs_by_id = RB_ROOT;
10620247a0cfSIlya Dryomov 	INIT_LIST_HEAD(&osd->o_osd_lru);
10630247a0cfSIlya Dryomov 	INIT_LIST_HEAD(&osd->o_keepalive_item);
10640247a0cfSIlya Dryomov 	osd->o_incarnation = 1;
10655aea3dcdSIlya Dryomov 	mutex_init(&osd->lock);
10660247a0cfSIlya Dryomov }
10670247a0cfSIlya Dryomov 
10680247a0cfSIlya Dryomov static void osd_cleanup(struct ceph_osd *osd)
10690247a0cfSIlya Dryomov {
10700247a0cfSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&osd->o_node));
10715aea3dcdSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
1072922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
1073a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoff_mappings));
1074a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoffs_by_id));
10750247a0cfSIlya Dryomov 	WARN_ON(!list_empty(&osd->o_osd_lru));
10760247a0cfSIlya Dryomov 	WARN_ON(!list_empty(&osd->o_keepalive_item));
10770247a0cfSIlya Dryomov 
10780247a0cfSIlya Dryomov 	if (osd->o_auth.authorizer) {
10790247a0cfSIlya Dryomov 		WARN_ON(osd_homeless(osd));
10800247a0cfSIlya Dryomov 		ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
10810247a0cfSIlya Dryomov 	}
10820247a0cfSIlya Dryomov }
10830247a0cfSIlya Dryomov 
10840247a0cfSIlya Dryomov /*
10853d14c5d2SYehuda Sadeh  * Track open sessions with osds.
10863d14c5d2SYehuda Sadeh  */
1087e10006f8SAlex Elder static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
10883d14c5d2SYehuda Sadeh {
10893d14c5d2SYehuda Sadeh 	struct ceph_osd *osd;
10903d14c5d2SYehuda Sadeh 
10910247a0cfSIlya Dryomov 	WARN_ON(onum == CEPH_HOMELESS_OSD);
10920247a0cfSIlya Dryomov 
10937a28f59bSIlya Dryomov 	osd = kzalloc(sizeof(*osd), GFP_NOIO | __GFP_NOFAIL);
10940247a0cfSIlya Dryomov 	osd_init(osd);
10953d14c5d2SYehuda Sadeh 	osd->o_osdc = osdc;
1096e10006f8SAlex Elder 	osd->o_osd = onum;
10973d14c5d2SYehuda Sadeh 
1098b7a9e5ddSSage Weil 	ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
10993d14c5d2SYehuda Sadeh 
11003d14c5d2SYehuda Sadeh 	return osd;
11013d14c5d2SYehuda Sadeh }
11023d14c5d2SYehuda Sadeh 
11033d14c5d2SYehuda Sadeh static struct ceph_osd *get_osd(struct ceph_osd *osd)
11043d14c5d2SYehuda Sadeh {
110502113a0fSElena Reshetova 	if (refcount_inc_not_zero(&osd->o_ref)) {
110602113a0fSElena Reshetova 		dout("get_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref)-1,
110702113a0fSElena Reshetova 		     refcount_read(&osd->o_ref));
11083d14c5d2SYehuda Sadeh 		return osd;
11093d14c5d2SYehuda Sadeh 	} else {
11103d14c5d2SYehuda Sadeh 		dout("get_osd %p FAIL\n", osd);
11113d14c5d2SYehuda Sadeh 		return NULL;
11123d14c5d2SYehuda Sadeh 	}
11133d14c5d2SYehuda Sadeh }
11143d14c5d2SYehuda Sadeh 
11153d14c5d2SYehuda Sadeh static void put_osd(struct ceph_osd *osd)
11163d14c5d2SYehuda Sadeh {
111702113a0fSElena Reshetova 	dout("put_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref),
111802113a0fSElena Reshetova 	     refcount_read(&osd->o_ref) - 1);
111902113a0fSElena Reshetova 	if (refcount_dec_and_test(&osd->o_ref)) {
11200247a0cfSIlya Dryomov 		osd_cleanup(osd);
11213d14c5d2SYehuda Sadeh 		kfree(osd);
11223d14c5d2SYehuda Sadeh 	}
11233d14c5d2SYehuda Sadeh }
11243d14c5d2SYehuda Sadeh 
1125fcd00b68SIlya Dryomov DEFINE_RB_FUNCS(osd, struct ceph_osd, o_osd, o_node)
1126fcd00b68SIlya Dryomov 
11279dd2845cSIlya Dryomov static void __move_osd_to_lru(struct ceph_osd *osd)
11283d14c5d2SYehuda Sadeh {
11299dd2845cSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
11309dd2845cSIlya Dryomov 
11319dd2845cSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
11323d14c5d2SYehuda Sadeh 	BUG_ON(!list_empty(&osd->o_osd_lru));
1133bbf37ec3SIlya Dryomov 
11349dd2845cSIlya Dryomov 	spin_lock(&osdc->osd_lru_lock);
11353d14c5d2SYehuda Sadeh 	list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
11369dd2845cSIlya Dryomov 	spin_unlock(&osdc->osd_lru_lock);
11379dd2845cSIlya Dryomov 
1138a319bf56SIlya Dryomov 	osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
11393d14c5d2SYehuda Sadeh }
11403d14c5d2SYehuda Sadeh 
11419dd2845cSIlya Dryomov static void maybe_move_osd_to_lru(struct ceph_osd *osd)
1142bbf37ec3SIlya Dryomov {
11435aea3dcdSIlya Dryomov 	if (RB_EMPTY_ROOT(&osd->o_requests) &&
1144922dab61SIlya Dryomov 	    RB_EMPTY_ROOT(&osd->o_linger_requests))
11459dd2845cSIlya Dryomov 		__move_osd_to_lru(osd);
1146bbf37ec3SIlya Dryomov }
1147bbf37ec3SIlya Dryomov 
11483d14c5d2SYehuda Sadeh static void __remove_osd_from_lru(struct ceph_osd *osd)
11493d14c5d2SYehuda Sadeh {
11509dd2845cSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
11519dd2845cSIlya Dryomov 
11529dd2845cSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
11539dd2845cSIlya Dryomov 
11549dd2845cSIlya Dryomov 	spin_lock(&osdc->osd_lru_lock);
11553d14c5d2SYehuda Sadeh 	if (!list_empty(&osd->o_osd_lru))
11563d14c5d2SYehuda Sadeh 		list_del_init(&osd->o_osd_lru);
11579dd2845cSIlya Dryomov 	spin_unlock(&osdc->osd_lru_lock);
11583d14c5d2SYehuda Sadeh }
11593d14c5d2SYehuda Sadeh 
11603d14c5d2SYehuda Sadeh /*
11615aea3dcdSIlya Dryomov  * Close the connection and assign any leftover requests to the
11625aea3dcdSIlya Dryomov  * homeless session.
11635aea3dcdSIlya Dryomov  */
11645aea3dcdSIlya Dryomov static void close_osd(struct ceph_osd *osd)
11655aea3dcdSIlya Dryomov {
11665aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
11675aea3dcdSIlya Dryomov 	struct rb_node *n;
11685aea3dcdSIlya Dryomov 
11695aea3dcdSIlya Dryomov 	verify_osdc_wrlocked(osdc);
11705aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
11715aea3dcdSIlya Dryomov 
11725aea3dcdSIlya Dryomov 	ceph_con_close(&osd->o_con);
11735aea3dcdSIlya Dryomov 
11745aea3dcdSIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; ) {
11755aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
11765aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
11775aea3dcdSIlya Dryomov 
11785aea3dcdSIlya Dryomov 		n = rb_next(n); /* unlink_request() */
11795aea3dcdSIlya Dryomov 
11805aea3dcdSIlya Dryomov 		dout(" reassigning req %p tid %llu\n", req, req->r_tid);
11815aea3dcdSIlya Dryomov 		unlink_request(osd, req);
11825aea3dcdSIlya Dryomov 		link_request(&osdc->homeless_osd, req);
11835aea3dcdSIlya Dryomov 	}
1184922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_linger_requests); n; ) {
1185922dab61SIlya Dryomov 		struct ceph_osd_linger_request *lreq =
1186922dab61SIlya Dryomov 		    rb_entry(n, struct ceph_osd_linger_request, node);
1187922dab61SIlya Dryomov 
1188922dab61SIlya Dryomov 		n = rb_next(n); /* unlink_linger() */
1189922dab61SIlya Dryomov 
1190922dab61SIlya Dryomov 		dout(" reassigning lreq %p linger_id %llu\n", lreq,
1191922dab61SIlya Dryomov 		     lreq->linger_id);
1192922dab61SIlya Dryomov 		unlink_linger(osd, lreq);
1193922dab61SIlya Dryomov 		link_linger(&osdc->homeless_osd, lreq);
1194922dab61SIlya Dryomov 	}
1195a02a946dSIlya Dryomov 	clear_backoffs(osd);
11965aea3dcdSIlya Dryomov 
11975aea3dcdSIlya Dryomov 	__remove_osd_from_lru(osd);
11985aea3dcdSIlya Dryomov 	erase_osd(&osdc->osds, osd);
11995aea3dcdSIlya Dryomov 	put_osd(osd);
12005aea3dcdSIlya Dryomov }
12015aea3dcdSIlya Dryomov 
12025aea3dcdSIlya Dryomov /*
12033d14c5d2SYehuda Sadeh  * reset osd connect
12043d14c5d2SYehuda Sadeh  */
12055aea3dcdSIlya Dryomov static int reopen_osd(struct ceph_osd *osd)
12063d14c5d2SYehuda Sadeh {
1207c3acb181SAlex Elder 	struct ceph_entity_addr *peer_addr;
12083d14c5d2SYehuda Sadeh 
12095aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
12105aea3dcdSIlya Dryomov 
12115aea3dcdSIlya Dryomov 	if (RB_EMPTY_ROOT(&osd->o_requests) &&
1212922dab61SIlya Dryomov 	    RB_EMPTY_ROOT(&osd->o_linger_requests)) {
12135aea3dcdSIlya Dryomov 		close_osd(osd);
1214c3acb181SAlex Elder 		return -ENODEV;
1215c3acb181SAlex Elder 	}
1216c3acb181SAlex Elder 
12175aea3dcdSIlya Dryomov 	peer_addr = &osd->o_osdc->osdmap->osd_addr[osd->o_osd];
1218c3acb181SAlex Elder 	if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
12193d14c5d2SYehuda Sadeh 			!ceph_con_opened(&osd->o_con)) {
12205aea3dcdSIlya Dryomov 		struct rb_node *n;
1221c3acb181SAlex Elder 
12223d14c5d2SYehuda Sadeh 		dout("osd addr hasn't changed and connection never opened, "
12230b4af2e8SIlya Dryomov 		     "letting msgr retry\n");
12243d14c5d2SYehuda Sadeh 		/* touch each r_stamp for handle_timeout()'s benfit */
12255aea3dcdSIlya Dryomov 		for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
12265aea3dcdSIlya Dryomov 			struct ceph_osd_request *req =
12275aea3dcdSIlya Dryomov 			    rb_entry(n, struct ceph_osd_request, r_node);
12283d14c5d2SYehuda Sadeh 			req->r_stamp = jiffies;
12295aea3dcdSIlya Dryomov 		}
1230c3acb181SAlex Elder 
1231c3acb181SAlex Elder 		return -EAGAIN;
12323d14c5d2SYehuda Sadeh 	}
1233c3acb181SAlex Elder 
1234c3acb181SAlex Elder 	ceph_con_close(&osd->o_con);
1235c3acb181SAlex Elder 	ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1236c3acb181SAlex Elder 	osd->o_incarnation++;
1237c3acb181SAlex Elder 
1238c3acb181SAlex Elder 	return 0;
12393d14c5d2SYehuda Sadeh }
12403d14c5d2SYehuda Sadeh 
12415aea3dcdSIlya Dryomov static struct ceph_osd *lookup_create_osd(struct ceph_osd_client *osdc, int o,
12425aea3dcdSIlya Dryomov 					  bool wrlocked)
12433d14c5d2SYehuda Sadeh {
12445aea3dcdSIlya Dryomov 	struct ceph_osd *osd;
12455aea3dcdSIlya Dryomov 
12465aea3dcdSIlya Dryomov 	if (wrlocked)
12475aea3dcdSIlya Dryomov 		verify_osdc_wrlocked(osdc);
12485aea3dcdSIlya Dryomov 	else
12495aea3dcdSIlya Dryomov 		verify_osdc_locked(osdc);
12505aea3dcdSIlya Dryomov 
12515aea3dcdSIlya Dryomov 	if (o != CEPH_HOMELESS_OSD)
12525aea3dcdSIlya Dryomov 		osd = lookup_osd(&osdc->osds, o);
12535aea3dcdSIlya Dryomov 	else
12545aea3dcdSIlya Dryomov 		osd = &osdc->homeless_osd;
12555aea3dcdSIlya Dryomov 	if (!osd) {
12565aea3dcdSIlya Dryomov 		if (!wrlocked)
12575aea3dcdSIlya Dryomov 			return ERR_PTR(-EAGAIN);
12585aea3dcdSIlya Dryomov 
12595aea3dcdSIlya Dryomov 		osd = create_osd(osdc, o);
12605aea3dcdSIlya Dryomov 		insert_osd(&osdc->osds, osd);
12615aea3dcdSIlya Dryomov 		ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
12625aea3dcdSIlya Dryomov 			      &osdc->osdmap->osd_addr[osd->o_osd]);
12635aea3dcdSIlya Dryomov 	}
12645aea3dcdSIlya Dryomov 
12655aea3dcdSIlya Dryomov 	dout("%s osdc %p osd%d -> osd %p\n", __func__, osdc, o, osd);
12665aea3dcdSIlya Dryomov 	return osd;
1267a40c4f10SYehuda Sadeh }
1268a40c4f10SYehuda Sadeh 
12693d14c5d2SYehuda Sadeh /*
12705aea3dcdSIlya Dryomov  * Create request <-> OSD session relation.
12715aea3dcdSIlya Dryomov  *
12725aea3dcdSIlya Dryomov  * @req has to be assigned a tid, @osd may be homeless.
12733d14c5d2SYehuda Sadeh  */
12745aea3dcdSIlya Dryomov static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req)
12753d14c5d2SYehuda Sadeh {
12765aea3dcdSIlya Dryomov 	verify_osd_locked(osd);
12775aea3dcdSIlya Dryomov 	WARN_ON(!req->r_tid || req->r_osd);
12785aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
127935f9f8a0SSage Weil 	     req, req->r_tid);
12805aea3dcdSIlya Dryomov 
12815aea3dcdSIlya Dryomov 	if (!osd_homeless(osd))
12825aea3dcdSIlya Dryomov 		__remove_osd_from_lru(osd);
12835aea3dcdSIlya Dryomov 	else
12845aea3dcdSIlya Dryomov 		atomic_inc(&osd->o_osdc->num_homeless);
12855aea3dcdSIlya Dryomov 
12865aea3dcdSIlya Dryomov 	get_osd(osd);
12875aea3dcdSIlya Dryomov 	insert_request(&osd->o_requests, req);
12885aea3dcdSIlya Dryomov 	req->r_osd = osd;
128935f9f8a0SSage Weil }
129035f9f8a0SSage Weil 
12915aea3dcdSIlya Dryomov static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req)
12923d14c5d2SYehuda Sadeh {
12935aea3dcdSIlya Dryomov 	verify_osd_locked(osd);
12945aea3dcdSIlya Dryomov 	WARN_ON(req->r_osd != osd);
12955aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
12965aea3dcdSIlya Dryomov 	     req, req->r_tid);
12975aea3dcdSIlya Dryomov 
12985aea3dcdSIlya Dryomov 	req->r_osd = NULL;
12995aea3dcdSIlya Dryomov 	erase_request(&osd->o_requests, req);
13005aea3dcdSIlya Dryomov 	put_osd(osd);
13015aea3dcdSIlya Dryomov 
13025aea3dcdSIlya Dryomov 	if (!osd_homeless(osd))
13035aea3dcdSIlya Dryomov 		maybe_move_osd_to_lru(osd);
13045aea3dcdSIlya Dryomov 	else
13055aea3dcdSIlya Dryomov 		atomic_dec(&osd->o_osdc->num_homeless);
13063d14c5d2SYehuda Sadeh }
13073d14c5d2SYehuda Sadeh 
130863244fa1SIlya Dryomov static bool __pool_full(struct ceph_pg_pool_info *pi)
130963244fa1SIlya Dryomov {
131063244fa1SIlya Dryomov 	return pi->flags & CEPH_POOL_FLAG_FULL;
131163244fa1SIlya Dryomov }
131263244fa1SIlya Dryomov 
131342c1b124SIlya Dryomov static bool have_pool_full(struct ceph_osd_client *osdc)
131442c1b124SIlya Dryomov {
131542c1b124SIlya Dryomov 	struct rb_node *n;
131642c1b124SIlya Dryomov 
131742c1b124SIlya Dryomov 	for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
131842c1b124SIlya Dryomov 		struct ceph_pg_pool_info *pi =
131942c1b124SIlya Dryomov 		    rb_entry(n, struct ceph_pg_pool_info, node);
132042c1b124SIlya Dryomov 
132142c1b124SIlya Dryomov 		if (__pool_full(pi))
132242c1b124SIlya Dryomov 			return true;
132342c1b124SIlya Dryomov 	}
132442c1b124SIlya Dryomov 
132542c1b124SIlya Dryomov 	return false;
132642c1b124SIlya Dryomov }
132742c1b124SIlya Dryomov 
13285aea3dcdSIlya Dryomov static bool pool_full(struct ceph_osd_client *osdc, s64 pool_id)
13295aea3dcdSIlya Dryomov {
13305aea3dcdSIlya Dryomov 	struct ceph_pg_pool_info *pi;
13315aea3dcdSIlya Dryomov 
13325aea3dcdSIlya Dryomov 	pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
13335aea3dcdSIlya Dryomov 	if (!pi)
13345aea3dcdSIlya Dryomov 		return false;
13355aea3dcdSIlya Dryomov 
13365aea3dcdSIlya Dryomov 	return __pool_full(pi);
13375aea3dcdSIlya Dryomov }
13385aea3dcdSIlya Dryomov 
13393d14c5d2SYehuda Sadeh /*
1340d29adb34SJosh Durgin  * Returns whether a request should be blocked from being sent
1341d29adb34SJosh Durgin  * based on the current osdmap and osd_client settings.
1342d29adb34SJosh Durgin  */
134363244fa1SIlya Dryomov static bool target_should_be_paused(struct ceph_osd_client *osdc,
134463244fa1SIlya Dryomov 				    const struct ceph_osd_request_target *t,
134563244fa1SIlya Dryomov 				    struct ceph_pg_pool_info *pi)
134663244fa1SIlya Dryomov {
1347b7ec35b3SIlya Dryomov 	bool pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
1348b7ec35b3SIlya Dryomov 	bool pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
1349b7ec35b3SIlya Dryomov 		       ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
135063244fa1SIlya Dryomov 		       __pool_full(pi);
135163244fa1SIlya Dryomov 
13526d637a54SIlya Dryomov 	WARN_ON(pi->id != t->target_oloc.pool);
135358eb7932SJeff Layton 	return ((t->flags & CEPH_OSD_FLAG_READ) && pauserd) ||
135458eb7932SJeff Layton 	       ((t->flags & CEPH_OSD_FLAG_WRITE) && pausewr) ||
135558eb7932SJeff Layton 	       (osdc->osdmap->epoch < osdc->epoch_barrier);
135663244fa1SIlya Dryomov }
135763244fa1SIlya Dryomov 
135863244fa1SIlya Dryomov enum calc_target_result {
135963244fa1SIlya Dryomov 	CALC_TARGET_NO_ACTION = 0,
136063244fa1SIlya Dryomov 	CALC_TARGET_NEED_RESEND,
136163244fa1SIlya Dryomov 	CALC_TARGET_POOL_DNE,
136263244fa1SIlya Dryomov };
136363244fa1SIlya Dryomov 
136463244fa1SIlya Dryomov static enum calc_target_result calc_target(struct ceph_osd_client *osdc,
136563244fa1SIlya Dryomov 					   struct ceph_osd_request_target *t,
13667de030d6SIlya Dryomov 					   struct ceph_connection *con,
136763244fa1SIlya Dryomov 					   bool any_change)
136863244fa1SIlya Dryomov {
136963244fa1SIlya Dryomov 	struct ceph_pg_pool_info *pi;
137063244fa1SIlya Dryomov 	struct ceph_pg pgid, last_pgid;
137163244fa1SIlya Dryomov 	struct ceph_osds up, acting;
137263244fa1SIlya Dryomov 	bool force_resend = false;
137384ed45dfSIlya Dryomov 	bool unpaused = false;
137484ed45dfSIlya Dryomov 	bool legacy_change;
13757de030d6SIlya Dryomov 	bool split = false;
1376b7ec35b3SIlya Dryomov 	bool sort_bitwise = ceph_osdmap_flag(osdc, CEPH_OSDMAP_SORTBITWISE);
1377ae78dd81SIlya Dryomov 	bool recovery_deletes = ceph_osdmap_flag(osdc,
1378ae78dd81SIlya Dryomov 						 CEPH_OSDMAP_RECOVERY_DELETES);
137963244fa1SIlya Dryomov 	enum calc_target_result ct_res;
138063244fa1SIlya Dryomov 	int ret;
138163244fa1SIlya Dryomov 
138204c7d789SIlya Dryomov 	t->epoch = osdc->osdmap->epoch;
138363244fa1SIlya Dryomov 	pi = ceph_pg_pool_by_id(osdc->osdmap, t->base_oloc.pool);
138463244fa1SIlya Dryomov 	if (!pi) {
138563244fa1SIlya Dryomov 		t->osd = CEPH_HOMELESS_OSD;
138663244fa1SIlya Dryomov 		ct_res = CALC_TARGET_POOL_DNE;
138763244fa1SIlya Dryomov 		goto out;
138863244fa1SIlya Dryomov 	}
138963244fa1SIlya Dryomov 
139063244fa1SIlya Dryomov 	if (osdc->osdmap->epoch == pi->last_force_request_resend) {
1391dc93e0e2SIlya Dryomov 		if (t->last_force_resend < pi->last_force_request_resend) {
1392dc93e0e2SIlya Dryomov 			t->last_force_resend = pi->last_force_request_resend;
139363244fa1SIlya Dryomov 			force_resend = true;
1394dc93e0e2SIlya Dryomov 		} else if (t->last_force_resend == 0) {
139563244fa1SIlya Dryomov 			force_resend = true;
139663244fa1SIlya Dryomov 		}
139763244fa1SIlya Dryomov 	}
139863244fa1SIlya Dryomov 
1399db098ec4SIlya Dryomov 	/* apply tiering */
1400db098ec4SIlya Dryomov 	ceph_oid_copy(&t->target_oid, &t->base_oid);
1401db098ec4SIlya Dryomov 	ceph_oloc_copy(&t->target_oloc, &t->base_oloc);
1402db098ec4SIlya Dryomov 	if ((t->flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
140363244fa1SIlya Dryomov 		if (t->flags & CEPH_OSD_FLAG_READ && pi->read_tier >= 0)
140463244fa1SIlya Dryomov 			t->target_oloc.pool = pi->read_tier;
140563244fa1SIlya Dryomov 		if (t->flags & CEPH_OSD_FLAG_WRITE && pi->write_tier >= 0)
140663244fa1SIlya Dryomov 			t->target_oloc.pool = pi->write_tier;
14076d637a54SIlya Dryomov 
14086d637a54SIlya Dryomov 		pi = ceph_pg_pool_by_id(osdc->osdmap, t->target_oloc.pool);
14096d637a54SIlya Dryomov 		if (!pi) {
14106d637a54SIlya Dryomov 			t->osd = CEPH_HOMELESS_OSD;
14116d637a54SIlya Dryomov 			ct_res = CALC_TARGET_POOL_DNE;
14126d637a54SIlya Dryomov 			goto out;
14136d637a54SIlya Dryomov 		}
141463244fa1SIlya Dryomov 	}
141563244fa1SIlya Dryomov 
1416df28152dSIlya Dryomov 	ret = __ceph_object_locator_to_pg(pi, &t->target_oid, &t->target_oloc,
1417df28152dSIlya Dryomov 					  &pgid);
141863244fa1SIlya Dryomov 	if (ret) {
141963244fa1SIlya Dryomov 		WARN_ON(ret != -ENOENT);
142063244fa1SIlya Dryomov 		t->osd = CEPH_HOMELESS_OSD;
142163244fa1SIlya Dryomov 		ct_res = CALC_TARGET_POOL_DNE;
142263244fa1SIlya Dryomov 		goto out;
142363244fa1SIlya Dryomov 	}
142463244fa1SIlya Dryomov 	last_pgid.pool = pgid.pool;
142563244fa1SIlya Dryomov 	last_pgid.seed = ceph_stable_mod(pgid.seed, t->pg_num, t->pg_num_mask);
142663244fa1SIlya Dryomov 
1427df28152dSIlya Dryomov 	ceph_pg_to_up_acting_osds(osdc->osdmap, pi, &pgid, &up, &acting);
142863244fa1SIlya Dryomov 	if (any_change &&
142963244fa1SIlya Dryomov 	    ceph_is_new_interval(&t->acting,
143063244fa1SIlya Dryomov 				 &acting,
143163244fa1SIlya Dryomov 				 &t->up,
143263244fa1SIlya Dryomov 				 &up,
143363244fa1SIlya Dryomov 				 t->size,
143463244fa1SIlya Dryomov 				 pi->size,
143563244fa1SIlya Dryomov 				 t->min_size,
143663244fa1SIlya Dryomov 				 pi->min_size,
143763244fa1SIlya Dryomov 				 t->pg_num,
143863244fa1SIlya Dryomov 				 pi->pg_num,
143963244fa1SIlya Dryomov 				 t->sort_bitwise,
144063244fa1SIlya Dryomov 				 sort_bitwise,
1441ae78dd81SIlya Dryomov 				 t->recovery_deletes,
1442ae78dd81SIlya Dryomov 				 recovery_deletes,
144363244fa1SIlya Dryomov 				 &last_pgid))
144463244fa1SIlya Dryomov 		force_resend = true;
144563244fa1SIlya Dryomov 
144663244fa1SIlya Dryomov 	if (t->paused && !target_should_be_paused(osdc, t, pi)) {
144763244fa1SIlya Dryomov 		t->paused = false;
144884ed45dfSIlya Dryomov 		unpaused = true;
144963244fa1SIlya Dryomov 	}
145084ed45dfSIlya Dryomov 	legacy_change = ceph_pg_compare(&t->pgid, &pgid) ||
145184ed45dfSIlya Dryomov 			ceph_osds_changed(&t->acting, &acting, any_change);
14527de030d6SIlya Dryomov 	if (t->pg_num)
14537de030d6SIlya Dryomov 		split = ceph_pg_is_split(&last_pgid, t->pg_num, pi->pg_num);
145463244fa1SIlya Dryomov 
14557de030d6SIlya Dryomov 	if (legacy_change || force_resend || split) {
145663244fa1SIlya Dryomov 		t->pgid = pgid; /* struct */
1457df28152dSIlya Dryomov 		ceph_pg_to_primary_shard(osdc->osdmap, pi, &pgid, &t->spgid);
145863244fa1SIlya Dryomov 		ceph_osds_copy(&t->acting, &acting);
145963244fa1SIlya Dryomov 		ceph_osds_copy(&t->up, &up);
146063244fa1SIlya Dryomov 		t->size = pi->size;
146163244fa1SIlya Dryomov 		t->min_size = pi->min_size;
146263244fa1SIlya Dryomov 		t->pg_num = pi->pg_num;
146363244fa1SIlya Dryomov 		t->pg_num_mask = pi->pg_num_mask;
146463244fa1SIlya Dryomov 		t->sort_bitwise = sort_bitwise;
1465ae78dd81SIlya Dryomov 		t->recovery_deletes = recovery_deletes;
146663244fa1SIlya Dryomov 
146763244fa1SIlya Dryomov 		t->osd = acting.primary;
146863244fa1SIlya Dryomov 	}
146963244fa1SIlya Dryomov 
14707de030d6SIlya Dryomov 	if (unpaused || legacy_change || force_resend ||
14717de030d6SIlya Dryomov 	    (split && con && CEPH_HAVE_FEATURE(con->peer_features,
14727de030d6SIlya Dryomov 					       RESEND_ON_SPLIT)))
147384ed45dfSIlya Dryomov 		ct_res = CALC_TARGET_NEED_RESEND;
147484ed45dfSIlya Dryomov 	else
147584ed45dfSIlya Dryomov 		ct_res = CALC_TARGET_NO_ACTION;
147684ed45dfSIlya Dryomov 
147763244fa1SIlya Dryomov out:
147863244fa1SIlya Dryomov 	dout("%s t %p -> ct_res %d osd %d\n", __func__, t, ct_res, t->osd);
147963244fa1SIlya Dryomov 	return ct_res;
148063244fa1SIlya Dryomov }
148163244fa1SIlya Dryomov 
1482a02a946dSIlya Dryomov static struct ceph_spg_mapping *alloc_spg_mapping(void)
1483a02a946dSIlya Dryomov {
1484a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
1485a02a946dSIlya Dryomov 
1486a02a946dSIlya Dryomov 	spg = kmalloc(sizeof(*spg), GFP_NOIO);
1487a02a946dSIlya Dryomov 	if (!spg)
1488a02a946dSIlya Dryomov 		return NULL;
1489a02a946dSIlya Dryomov 
1490a02a946dSIlya Dryomov 	RB_CLEAR_NODE(&spg->node);
1491a02a946dSIlya Dryomov 	spg->backoffs = RB_ROOT;
1492a02a946dSIlya Dryomov 	return spg;
1493a02a946dSIlya Dryomov }
1494a02a946dSIlya Dryomov 
1495a02a946dSIlya Dryomov static void free_spg_mapping(struct ceph_spg_mapping *spg)
1496a02a946dSIlya Dryomov {
1497a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&spg->node));
1498a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&spg->backoffs));
1499a02a946dSIlya Dryomov 
1500a02a946dSIlya Dryomov 	kfree(spg);
1501a02a946dSIlya Dryomov }
1502a02a946dSIlya Dryomov 
1503a02a946dSIlya Dryomov /*
1504a02a946dSIlya Dryomov  * rbtree of ceph_spg_mapping for handling map<spg_t, ...>, similar to
1505a02a946dSIlya Dryomov  * ceph_pg_mapping.  Used to track OSD backoffs -- a backoff [range] is
1506a02a946dSIlya Dryomov  * defined only within a specific spgid; it does not pass anything to
1507a02a946dSIlya Dryomov  * children on split, or to another primary.
1508a02a946dSIlya Dryomov  */
1509a02a946dSIlya Dryomov DEFINE_RB_FUNCS2(spg_mapping, struct ceph_spg_mapping, spgid, ceph_spg_compare,
1510a02a946dSIlya Dryomov 		 RB_BYPTR, const struct ceph_spg *, node)
1511a02a946dSIlya Dryomov 
1512a02a946dSIlya Dryomov static u64 hoid_get_bitwise_key(const struct ceph_hobject_id *hoid)
1513a02a946dSIlya Dryomov {
1514a02a946dSIlya Dryomov 	return hoid->is_max ? 0x100000000ull : hoid->hash_reverse_bits;
1515a02a946dSIlya Dryomov }
1516a02a946dSIlya Dryomov 
1517a02a946dSIlya Dryomov static void hoid_get_effective_key(const struct ceph_hobject_id *hoid,
1518a02a946dSIlya Dryomov 				   void **pkey, size_t *pkey_len)
1519a02a946dSIlya Dryomov {
1520a02a946dSIlya Dryomov 	if (hoid->key_len) {
1521a02a946dSIlya Dryomov 		*pkey = hoid->key;
1522a02a946dSIlya Dryomov 		*pkey_len = hoid->key_len;
1523a02a946dSIlya Dryomov 	} else {
1524a02a946dSIlya Dryomov 		*pkey = hoid->oid;
1525a02a946dSIlya Dryomov 		*pkey_len = hoid->oid_len;
1526a02a946dSIlya Dryomov 	}
1527a02a946dSIlya Dryomov }
1528a02a946dSIlya Dryomov 
1529a02a946dSIlya Dryomov static int compare_names(const void *name1, size_t name1_len,
1530a02a946dSIlya Dryomov 			 const void *name2, size_t name2_len)
1531a02a946dSIlya Dryomov {
1532a02a946dSIlya Dryomov 	int ret;
1533a02a946dSIlya Dryomov 
1534a02a946dSIlya Dryomov 	ret = memcmp(name1, name2, min(name1_len, name2_len));
1535a02a946dSIlya Dryomov 	if (!ret) {
1536a02a946dSIlya Dryomov 		if (name1_len < name2_len)
1537a02a946dSIlya Dryomov 			ret = -1;
1538a02a946dSIlya Dryomov 		else if (name1_len > name2_len)
1539a02a946dSIlya Dryomov 			ret = 1;
1540a02a946dSIlya Dryomov 	}
1541a02a946dSIlya Dryomov 	return ret;
1542a02a946dSIlya Dryomov }
1543a02a946dSIlya Dryomov 
1544a02a946dSIlya Dryomov static int hoid_compare(const struct ceph_hobject_id *lhs,
1545a02a946dSIlya Dryomov 			const struct ceph_hobject_id *rhs)
1546a02a946dSIlya Dryomov {
1547a02a946dSIlya Dryomov 	void *effective_key1, *effective_key2;
1548a02a946dSIlya Dryomov 	size_t effective_key1_len, effective_key2_len;
1549a02a946dSIlya Dryomov 	int ret;
1550a02a946dSIlya Dryomov 
1551a02a946dSIlya Dryomov 	if (lhs->is_max < rhs->is_max)
1552a02a946dSIlya Dryomov 		return -1;
1553a02a946dSIlya Dryomov 	if (lhs->is_max > rhs->is_max)
1554a02a946dSIlya Dryomov 		return 1;
1555a02a946dSIlya Dryomov 
1556a02a946dSIlya Dryomov 	if (lhs->pool < rhs->pool)
1557a02a946dSIlya Dryomov 		return -1;
1558a02a946dSIlya Dryomov 	if (lhs->pool > rhs->pool)
1559a02a946dSIlya Dryomov 		return 1;
1560a02a946dSIlya Dryomov 
1561a02a946dSIlya Dryomov 	if (hoid_get_bitwise_key(lhs) < hoid_get_bitwise_key(rhs))
1562a02a946dSIlya Dryomov 		return -1;
1563a02a946dSIlya Dryomov 	if (hoid_get_bitwise_key(lhs) > hoid_get_bitwise_key(rhs))
1564a02a946dSIlya Dryomov 		return 1;
1565a02a946dSIlya Dryomov 
1566a02a946dSIlya Dryomov 	ret = compare_names(lhs->nspace, lhs->nspace_len,
1567a02a946dSIlya Dryomov 			    rhs->nspace, rhs->nspace_len);
1568a02a946dSIlya Dryomov 	if (ret)
1569a02a946dSIlya Dryomov 		return ret;
1570a02a946dSIlya Dryomov 
1571a02a946dSIlya Dryomov 	hoid_get_effective_key(lhs, &effective_key1, &effective_key1_len);
1572a02a946dSIlya Dryomov 	hoid_get_effective_key(rhs, &effective_key2, &effective_key2_len);
1573a02a946dSIlya Dryomov 	ret = compare_names(effective_key1, effective_key1_len,
1574a02a946dSIlya Dryomov 			    effective_key2, effective_key2_len);
1575a02a946dSIlya Dryomov 	if (ret)
1576a02a946dSIlya Dryomov 		return ret;
1577a02a946dSIlya Dryomov 
1578a02a946dSIlya Dryomov 	ret = compare_names(lhs->oid, lhs->oid_len, rhs->oid, rhs->oid_len);
1579a02a946dSIlya Dryomov 	if (ret)
1580a02a946dSIlya Dryomov 		return ret;
1581a02a946dSIlya Dryomov 
1582a02a946dSIlya Dryomov 	if (lhs->snapid < rhs->snapid)
1583a02a946dSIlya Dryomov 		return -1;
1584a02a946dSIlya Dryomov 	if (lhs->snapid > rhs->snapid)
1585a02a946dSIlya Dryomov 		return 1;
1586a02a946dSIlya Dryomov 
1587a02a946dSIlya Dryomov 	return 0;
1588a02a946dSIlya Dryomov }
1589a02a946dSIlya Dryomov 
1590a02a946dSIlya Dryomov /*
1591a02a946dSIlya Dryomov  * For decoding ->begin and ->end of MOSDBackoff only -- no MIN/MAX
1592a02a946dSIlya Dryomov  * compat stuff here.
1593a02a946dSIlya Dryomov  *
1594a02a946dSIlya Dryomov  * Assumes @hoid is zero-initialized.
1595a02a946dSIlya Dryomov  */
1596a02a946dSIlya Dryomov static int decode_hoid(void **p, void *end, struct ceph_hobject_id *hoid)
1597a02a946dSIlya Dryomov {
1598a02a946dSIlya Dryomov 	u8 struct_v;
1599a02a946dSIlya Dryomov 	u32 struct_len;
1600a02a946dSIlya Dryomov 	int ret;
1601a02a946dSIlya Dryomov 
1602a02a946dSIlya Dryomov 	ret = ceph_start_decoding(p, end, 4, "hobject_t", &struct_v,
1603a02a946dSIlya Dryomov 				  &struct_len);
1604a02a946dSIlya Dryomov 	if (ret)
1605a02a946dSIlya Dryomov 		return ret;
1606a02a946dSIlya Dryomov 
1607a02a946dSIlya Dryomov 	if (struct_v < 4) {
1608a02a946dSIlya Dryomov 		pr_err("got struct_v %d < 4 of hobject_t\n", struct_v);
1609a02a946dSIlya Dryomov 		goto e_inval;
1610a02a946dSIlya Dryomov 	}
1611a02a946dSIlya Dryomov 
1612a02a946dSIlya Dryomov 	hoid->key = ceph_extract_encoded_string(p, end, &hoid->key_len,
1613a02a946dSIlya Dryomov 						GFP_NOIO);
1614a02a946dSIlya Dryomov 	if (IS_ERR(hoid->key)) {
1615a02a946dSIlya Dryomov 		ret = PTR_ERR(hoid->key);
1616a02a946dSIlya Dryomov 		hoid->key = NULL;
1617a02a946dSIlya Dryomov 		return ret;
1618a02a946dSIlya Dryomov 	}
1619a02a946dSIlya Dryomov 
1620a02a946dSIlya Dryomov 	hoid->oid = ceph_extract_encoded_string(p, end, &hoid->oid_len,
1621a02a946dSIlya Dryomov 						GFP_NOIO);
1622a02a946dSIlya Dryomov 	if (IS_ERR(hoid->oid)) {
1623a02a946dSIlya Dryomov 		ret = PTR_ERR(hoid->oid);
1624a02a946dSIlya Dryomov 		hoid->oid = NULL;
1625a02a946dSIlya Dryomov 		return ret;
1626a02a946dSIlya Dryomov 	}
1627a02a946dSIlya Dryomov 
1628a02a946dSIlya Dryomov 	ceph_decode_64_safe(p, end, hoid->snapid, e_inval);
1629a02a946dSIlya Dryomov 	ceph_decode_32_safe(p, end, hoid->hash, e_inval);
1630a02a946dSIlya Dryomov 	ceph_decode_8_safe(p, end, hoid->is_max, e_inval);
1631a02a946dSIlya Dryomov 
1632a02a946dSIlya Dryomov 	hoid->nspace = ceph_extract_encoded_string(p, end, &hoid->nspace_len,
1633a02a946dSIlya Dryomov 						   GFP_NOIO);
1634a02a946dSIlya Dryomov 	if (IS_ERR(hoid->nspace)) {
1635a02a946dSIlya Dryomov 		ret = PTR_ERR(hoid->nspace);
1636a02a946dSIlya Dryomov 		hoid->nspace = NULL;
1637a02a946dSIlya Dryomov 		return ret;
1638a02a946dSIlya Dryomov 	}
1639a02a946dSIlya Dryomov 
1640a02a946dSIlya Dryomov 	ceph_decode_64_safe(p, end, hoid->pool, e_inval);
1641a02a946dSIlya Dryomov 
1642a02a946dSIlya Dryomov 	ceph_hoid_build_hash_cache(hoid);
1643a02a946dSIlya Dryomov 	return 0;
1644a02a946dSIlya Dryomov 
1645a02a946dSIlya Dryomov e_inval:
1646a02a946dSIlya Dryomov 	return -EINVAL;
1647a02a946dSIlya Dryomov }
1648a02a946dSIlya Dryomov 
1649a02a946dSIlya Dryomov static int hoid_encoding_size(const struct ceph_hobject_id *hoid)
1650a02a946dSIlya Dryomov {
1651a02a946dSIlya Dryomov 	return 8 + 4 + 1 + 8 + /* snapid, hash, is_max, pool */
1652a02a946dSIlya Dryomov 	       4 + hoid->key_len + 4 + hoid->oid_len + 4 + hoid->nspace_len;
1653a02a946dSIlya Dryomov }
1654a02a946dSIlya Dryomov 
1655a02a946dSIlya Dryomov static void encode_hoid(void **p, void *end, const struct ceph_hobject_id *hoid)
1656a02a946dSIlya Dryomov {
1657a02a946dSIlya Dryomov 	ceph_start_encoding(p, 4, 3, hoid_encoding_size(hoid));
1658a02a946dSIlya Dryomov 	ceph_encode_string(p, end, hoid->key, hoid->key_len);
1659a02a946dSIlya Dryomov 	ceph_encode_string(p, end, hoid->oid, hoid->oid_len);
1660a02a946dSIlya Dryomov 	ceph_encode_64(p, hoid->snapid);
1661a02a946dSIlya Dryomov 	ceph_encode_32(p, hoid->hash);
1662a02a946dSIlya Dryomov 	ceph_encode_8(p, hoid->is_max);
1663a02a946dSIlya Dryomov 	ceph_encode_string(p, end, hoid->nspace, hoid->nspace_len);
1664a02a946dSIlya Dryomov 	ceph_encode_64(p, hoid->pool);
1665a02a946dSIlya Dryomov }
1666a02a946dSIlya Dryomov 
1667a02a946dSIlya Dryomov static void free_hoid(struct ceph_hobject_id *hoid)
1668a02a946dSIlya Dryomov {
1669a02a946dSIlya Dryomov 	if (hoid) {
1670a02a946dSIlya Dryomov 		kfree(hoid->key);
1671a02a946dSIlya Dryomov 		kfree(hoid->oid);
1672a02a946dSIlya Dryomov 		kfree(hoid->nspace);
1673a02a946dSIlya Dryomov 		kfree(hoid);
1674a02a946dSIlya Dryomov 	}
1675a02a946dSIlya Dryomov }
1676a02a946dSIlya Dryomov 
1677a02a946dSIlya Dryomov static struct ceph_osd_backoff *alloc_backoff(void)
1678a02a946dSIlya Dryomov {
1679a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
1680a02a946dSIlya Dryomov 
1681a02a946dSIlya Dryomov 	backoff = kzalloc(sizeof(*backoff), GFP_NOIO);
1682a02a946dSIlya Dryomov 	if (!backoff)
1683a02a946dSIlya Dryomov 		return NULL;
1684a02a946dSIlya Dryomov 
1685a02a946dSIlya Dryomov 	RB_CLEAR_NODE(&backoff->spg_node);
1686a02a946dSIlya Dryomov 	RB_CLEAR_NODE(&backoff->id_node);
1687a02a946dSIlya Dryomov 	return backoff;
1688a02a946dSIlya Dryomov }
1689a02a946dSIlya Dryomov 
1690a02a946dSIlya Dryomov static void free_backoff(struct ceph_osd_backoff *backoff)
1691a02a946dSIlya Dryomov {
1692a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&backoff->spg_node));
1693a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&backoff->id_node));
1694a02a946dSIlya Dryomov 
1695a02a946dSIlya Dryomov 	free_hoid(backoff->begin);
1696a02a946dSIlya Dryomov 	free_hoid(backoff->end);
1697a02a946dSIlya Dryomov 	kfree(backoff);
1698a02a946dSIlya Dryomov }
1699a02a946dSIlya Dryomov 
1700a02a946dSIlya Dryomov /*
1701a02a946dSIlya Dryomov  * Within a specific spgid, backoffs are managed by ->begin hoid.
1702a02a946dSIlya Dryomov  */
1703a02a946dSIlya Dryomov DEFINE_RB_INSDEL_FUNCS2(backoff, struct ceph_osd_backoff, begin, hoid_compare,
1704a02a946dSIlya Dryomov 			RB_BYVAL, spg_node);
1705a02a946dSIlya Dryomov 
1706a02a946dSIlya Dryomov static struct ceph_osd_backoff *lookup_containing_backoff(struct rb_root *root,
1707a02a946dSIlya Dryomov 					    const struct ceph_hobject_id *hoid)
1708a02a946dSIlya Dryomov {
1709a02a946dSIlya Dryomov 	struct rb_node *n = root->rb_node;
1710a02a946dSIlya Dryomov 
1711a02a946dSIlya Dryomov 	while (n) {
1712a02a946dSIlya Dryomov 		struct ceph_osd_backoff *cur =
1713a02a946dSIlya Dryomov 		    rb_entry(n, struct ceph_osd_backoff, spg_node);
1714a02a946dSIlya Dryomov 		int cmp;
1715a02a946dSIlya Dryomov 
1716a02a946dSIlya Dryomov 		cmp = hoid_compare(hoid, cur->begin);
1717a02a946dSIlya Dryomov 		if (cmp < 0) {
1718a02a946dSIlya Dryomov 			n = n->rb_left;
1719a02a946dSIlya Dryomov 		} else if (cmp > 0) {
1720a02a946dSIlya Dryomov 			if (hoid_compare(hoid, cur->end) < 0)
1721a02a946dSIlya Dryomov 				return cur;
1722a02a946dSIlya Dryomov 
1723a02a946dSIlya Dryomov 			n = n->rb_right;
1724a02a946dSIlya Dryomov 		} else {
1725a02a946dSIlya Dryomov 			return cur;
1726a02a946dSIlya Dryomov 		}
1727a02a946dSIlya Dryomov 	}
1728a02a946dSIlya Dryomov 
1729a02a946dSIlya Dryomov 	return NULL;
1730a02a946dSIlya Dryomov }
1731a02a946dSIlya Dryomov 
1732a02a946dSIlya Dryomov /*
1733a02a946dSIlya Dryomov  * Each backoff has a unique id within its OSD session.
1734a02a946dSIlya Dryomov  */
1735a02a946dSIlya Dryomov DEFINE_RB_FUNCS(backoff_by_id, struct ceph_osd_backoff, id, id_node)
1736a02a946dSIlya Dryomov 
1737a02a946dSIlya Dryomov static void clear_backoffs(struct ceph_osd *osd)
1738a02a946dSIlya Dryomov {
1739a02a946dSIlya Dryomov 	while (!RB_EMPTY_ROOT(&osd->o_backoff_mappings)) {
1740a02a946dSIlya Dryomov 		struct ceph_spg_mapping *spg =
1741a02a946dSIlya Dryomov 		    rb_entry(rb_first(&osd->o_backoff_mappings),
1742a02a946dSIlya Dryomov 			     struct ceph_spg_mapping, node);
1743a02a946dSIlya Dryomov 
1744a02a946dSIlya Dryomov 		while (!RB_EMPTY_ROOT(&spg->backoffs)) {
1745a02a946dSIlya Dryomov 			struct ceph_osd_backoff *backoff =
1746a02a946dSIlya Dryomov 			    rb_entry(rb_first(&spg->backoffs),
1747a02a946dSIlya Dryomov 				     struct ceph_osd_backoff, spg_node);
1748a02a946dSIlya Dryomov 
1749a02a946dSIlya Dryomov 			erase_backoff(&spg->backoffs, backoff);
1750a02a946dSIlya Dryomov 			erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
1751a02a946dSIlya Dryomov 			free_backoff(backoff);
1752a02a946dSIlya Dryomov 		}
1753a02a946dSIlya Dryomov 		erase_spg_mapping(&osd->o_backoff_mappings, spg);
1754a02a946dSIlya Dryomov 		free_spg_mapping(spg);
1755a02a946dSIlya Dryomov 	}
1756a02a946dSIlya Dryomov }
1757a02a946dSIlya Dryomov 
1758a02a946dSIlya Dryomov /*
1759a02a946dSIlya Dryomov  * Set up a temporary, non-owning view into @t.
1760a02a946dSIlya Dryomov  */
1761a02a946dSIlya Dryomov static void hoid_fill_from_target(struct ceph_hobject_id *hoid,
1762a02a946dSIlya Dryomov 				  const struct ceph_osd_request_target *t)
1763a02a946dSIlya Dryomov {
1764a02a946dSIlya Dryomov 	hoid->key = NULL;
1765a02a946dSIlya Dryomov 	hoid->key_len = 0;
1766a02a946dSIlya Dryomov 	hoid->oid = t->target_oid.name;
1767a02a946dSIlya Dryomov 	hoid->oid_len = t->target_oid.name_len;
1768a02a946dSIlya Dryomov 	hoid->snapid = CEPH_NOSNAP;
1769a02a946dSIlya Dryomov 	hoid->hash = t->pgid.seed;
1770a02a946dSIlya Dryomov 	hoid->is_max = false;
1771a02a946dSIlya Dryomov 	if (t->target_oloc.pool_ns) {
1772a02a946dSIlya Dryomov 		hoid->nspace = t->target_oloc.pool_ns->str;
1773a02a946dSIlya Dryomov 		hoid->nspace_len = t->target_oloc.pool_ns->len;
1774a02a946dSIlya Dryomov 	} else {
1775a02a946dSIlya Dryomov 		hoid->nspace = NULL;
1776a02a946dSIlya Dryomov 		hoid->nspace_len = 0;
1777a02a946dSIlya Dryomov 	}
1778a02a946dSIlya Dryomov 	hoid->pool = t->target_oloc.pool;
1779a02a946dSIlya Dryomov 	ceph_hoid_build_hash_cache(hoid);
1780a02a946dSIlya Dryomov }
1781a02a946dSIlya Dryomov 
1782a02a946dSIlya Dryomov static bool should_plug_request(struct ceph_osd_request *req)
1783a02a946dSIlya Dryomov {
1784a02a946dSIlya Dryomov 	struct ceph_osd *osd = req->r_osd;
1785a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
1786a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
1787a02a946dSIlya Dryomov 	struct ceph_hobject_id hoid;
1788a02a946dSIlya Dryomov 
1789a02a946dSIlya Dryomov 	spg = lookup_spg_mapping(&osd->o_backoff_mappings, &req->r_t.spgid);
1790a02a946dSIlya Dryomov 	if (!spg)
1791a02a946dSIlya Dryomov 		return false;
1792a02a946dSIlya Dryomov 
1793a02a946dSIlya Dryomov 	hoid_fill_from_target(&hoid, &req->r_t);
1794a02a946dSIlya Dryomov 	backoff = lookup_containing_backoff(&spg->backoffs, &hoid);
1795a02a946dSIlya Dryomov 	if (!backoff)
1796a02a946dSIlya Dryomov 		return false;
1797a02a946dSIlya Dryomov 
1798a02a946dSIlya Dryomov 	dout("%s req %p tid %llu backoff osd%d spgid %llu.%xs%d id %llu\n",
1799a02a946dSIlya Dryomov 	     __func__, req, req->r_tid, osd->o_osd, backoff->spgid.pgid.pool,
1800a02a946dSIlya Dryomov 	     backoff->spgid.pgid.seed, backoff->spgid.shard, backoff->id);
1801a02a946dSIlya Dryomov 	return true;
1802a02a946dSIlya Dryomov }
1803a02a946dSIlya Dryomov 
1804bb873b53SIlya Dryomov static void setup_request_data(struct ceph_osd_request *req,
1805bb873b53SIlya Dryomov 			       struct ceph_msg *msg)
18063d14c5d2SYehuda Sadeh {
1807bb873b53SIlya Dryomov 	u32 data_len = 0;
1808bb873b53SIlya Dryomov 	int i;
18093d14c5d2SYehuda Sadeh 
1810bb873b53SIlya Dryomov 	if (!list_empty(&msg->data))
1811bb873b53SIlya Dryomov 		return;
18123d14c5d2SYehuda Sadeh 
1813bb873b53SIlya Dryomov 	WARN_ON(msg->data_length);
1814bb873b53SIlya Dryomov 	for (i = 0; i < req->r_num_ops; i++) {
1815bb873b53SIlya Dryomov 		struct ceph_osd_req_op *op = &req->r_ops[i];
1816bb873b53SIlya Dryomov 
1817bb873b53SIlya Dryomov 		switch (op->op) {
1818bb873b53SIlya Dryomov 		/* request */
1819bb873b53SIlya Dryomov 		case CEPH_OSD_OP_WRITE:
1820bb873b53SIlya Dryomov 		case CEPH_OSD_OP_WRITEFULL:
1821bb873b53SIlya Dryomov 			WARN_ON(op->indata_len != op->extent.length);
1822bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(msg, &op->extent.osd_data);
1823bb873b53SIlya Dryomov 			break;
1824bb873b53SIlya Dryomov 		case CEPH_OSD_OP_SETXATTR:
1825bb873b53SIlya Dryomov 		case CEPH_OSD_OP_CMPXATTR:
1826bb873b53SIlya Dryomov 			WARN_ON(op->indata_len != op->xattr.name_len +
1827bb873b53SIlya Dryomov 						  op->xattr.value_len);
1828bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(msg, &op->xattr.osd_data);
1829bb873b53SIlya Dryomov 			break;
1830922dab61SIlya Dryomov 		case CEPH_OSD_OP_NOTIFY_ACK:
1831922dab61SIlya Dryomov 			ceph_osdc_msg_data_add(msg,
1832922dab61SIlya Dryomov 					       &op->notify_ack.request_data);
1833922dab61SIlya Dryomov 			break;
1834bb873b53SIlya Dryomov 
1835bb873b53SIlya Dryomov 		/* reply */
1836bb873b53SIlya Dryomov 		case CEPH_OSD_OP_STAT:
1837bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(req->r_reply,
1838bb873b53SIlya Dryomov 					       &op->raw_data_in);
1839bb873b53SIlya Dryomov 			break;
1840bb873b53SIlya Dryomov 		case CEPH_OSD_OP_READ:
1841bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(req->r_reply,
1842bb873b53SIlya Dryomov 					       &op->extent.osd_data);
1843bb873b53SIlya Dryomov 			break;
1844a4ed38d7SDouglas Fuller 		case CEPH_OSD_OP_LIST_WATCHERS:
1845a4ed38d7SDouglas Fuller 			ceph_osdc_msg_data_add(req->r_reply,
1846a4ed38d7SDouglas Fuller 					       &op->list_watchers.response_data);
1847a4ed38d7SDouglas Fuller 			break;
1848bb873b53SIlya Dryomov 
1849bb873b53SIlya Dryomov 		/* both */
1850bb873b53SIlya Dryomov 		case CEPH_OSD_OP_CALL:
1851bb873b53SIlya Dryomov 			WARN_ON(op->indata_len != op->cls.class_len +
1852bb873b53SIlya Dryomov 						  op->cls.method_len +
1853bb873b53SIlya Dryomov 						  op->cls.indata_len);
1854bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(msg, &op->cls.request_info);
1855bb873b53SIlya Dryomov 			/* optional, can be NONE */
1856bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(msg, &op->cls.request_data);
1857bb873b53SIlya Dryomov 			/* optional, can be NONE */
1858bb873b53SIlya Dryomov 			ceph_osdc_msg_data_add(req->r_reply,
1859bb873b53SIlya Dryomov 					       &op->cls.response_data);
1860bb873b53SIlya Dryomov 			break;
186119079203SIlya Dryomov 		case CEPH_OSD_OP_NOTIFY:
186219079203SIlya Dryomov 			ceph_osdc_msg_data_add(msg,
186319079203SIlya Dryomov 					       &op->notify.request_data);
186419079203SIlya Dryomov 			ceph_osdc_msg_data_add(req->r_reply,
186519079203SIlya Dryomov 					       &op->notify.response_data);
186619079203SIlya Dryomov 			break;
1867bb873b53SIlya Dryomov 		}
1868bb873b53SIlya Dryomov 
1869bb873b53SIlya Dryomov 		data_len += op->indata_len;
1870bb873b53SIlya Dryomov 	}
1871bb873b53SIlya Dryomov 
1872bb873b53SIlya Dryomov 	WARN_ON(data_len != msg->data_length);
1873bb873b53SIlya Dryomov }
1874bb873b53SIlya Dryomov 
18752e59ffd1SIlya Dryomov static void encode_pgid(void **p, const struct ceph_pg *pgid)
18762e59ffd1SIlya Dryomov {
18772e59ffd1SIlya Dryomov 	ceph_encode_8(p, 1);
18782e59ffd1SIlya Dryomov 	ceph_encode_64(p, pgid->pool);
18792e59ffd1SIlya Dryomov 	ceph_encode_32(p, pgid->seed);
18802e59ffd1SIlya Dryomov 	ceph_encode_32(p, -1); /* preferred */
18812e59ffd1SIlya Dryomov }
18822e59ffd1SIlya Dryomov 
18838cb441c0SIlya Dryomov static void encode_spgid(void **p, const struct ceph_spg *spgid)
18848cb441c0SIlya Dryomov {
18858cb441c0SIlya Dryomov 	ceph_start_encoding(p, 1, 1, CEPH_PGID_ENCODING_LEN + 1);
18868cb441c0SIlya Dryomov 	encode_pgid(p, &spgid->pgid);
18878cb441c0SIlya Dryomov 	ceph_encode_8(p, spgid->shard);
18888cb441c0SIlya Dryomov }
18898cb441c0SIlya Dryomov 
18902e59ffd1SIlya Dryomov static void encode_oloc(void **p, void *end,
18912e59ffd1SIlya Dryomov 			const struct ceph_object_locator *oloc)
18922e59ffd1SIlya Dryomov {
18932e59ffd1SIlya Dryomov 	ceph_start_encoding(p, 5, 4, ceph_oloc_encoding_size(oloc));
18942e59ffd1SIlya Dryomov 	ceph_encode_64(p, oloc->pool);
18952e59ffd1SIlya Dryomov 	ceph_encode_32(p, -1); /* preferred */
18962e59ffd1SIlya Dryomov 	ceph_encode_32(p, 0);  /* key len */
18972e59ffd1SIlya Dryomov 	if (oloc->pool_ns)
18982e59ffd1SIlya Dryomov 		ceph_encode_string(p, end, oloc->pool_ns->str,
18992e59ffd1SIlya Dryomov 				   oloc->pool_ns->len);
19002e59ffd1SIlya Dryomov 	else
19012e59ffd1SIlya Dryomov 		ceph_encode_32(p, 0);
19022e59ffd1SIlya Dryomov }
19032e59ffd1SIlya Dryomov 
19048cb441c0SIlya Dryomov static void encode_request_partial(struct ceph_osd_request *req,
19058cb441c0SIlya Dryomov 				   struct ceph_msg *msg)
1906bb873b53SIlya Dryomov {
1907bb873b53SIlya Dryomov 	void *p = msg->front.iov_base;
1908bb873b53SIlya Dryomov 	void *const end = p + msg->front_alloc_len;
1909bb873b53SIlya Dryomov 	u32 data_len = 0;
1910bb873b53SIlya Dryomov 	int i;
1911bb873b53SIlya Dryomov 
1912bb873b53SIlya Dryomov 	if (req->r_flags & CEPH_OSD_FLAG_WRITE) {
1913bb873b53SIlya Dryomov 		/* snapshots aren't writeable */
1914bb873b53SIlya Dryomov 		WARN_ON(req->r_snapid != CEPH_NOSNAP);
1915bb873b53SIlya Dryomov 	} else {
1916bb873b53SIlya Dryomov 		WARN_ON(req->r_mtime.tv_sec || req->r_mtime.tv_nsec ||
1917bb873b53SIlya Dryomov 			req->r_data_offset || req->r_snapc);
1918bb873b53SIlya Dryomov 	}
1919bb873b53SIlya Dryomov 
1920bb873b53SIlya Dryomov 	setup_request_data(req, msg);
1921bb873b53SIlya Dryomov 
19228cb441c0SIlya Dryomov 	encode_spgid(&p, &req->r_t.spgid); /* actual spg */
19238cb441c0SIlya Dryomov 	ceph_encode_32(&p, req->r_t.pgid.seed); /* raw hash */
1924bb873b53SIlya Dryomov 	ceph_encode_32(&p, req->r_osdc->osdmap->epoch);
1925bb873b53SIlya Dryomov 	ceph_encode_32(&p, req->r_flags);
19268cb441c0SIlya Dryomov 
19278cb441c0SIlya Dryomov 	/* reqid */
19288cb441c0SIlya Dryomov 	ceph_start_encoding(&p, 2, 2, sizeof(struct ceph_osd_reqid));
19298cb441c0SIlya Dryomov 	memset(p, 0, sizeof(struct ceph_osd_reqid));
19308cb441c0SIlya Dryomov 	p += sizeof(struct ceph_osd_reqid);
19318cb441c0SIlya Dryomov 
19328cb441c0SIlya Dryomov 	/* trace */
19338cb441c0SIlya Dryomov 	memset(p, 0, sizeof(struct ceph_blkin_trace_info));
19348cb441c0SIlya Dryomov 	p += sizeof(struct ceph_blkin_trace_info);
19358cb441c0SIlya Dryomov 
19368cb441c0SIlya Dryomov 	ceph_encode_32(&p, 0); /* client_inc, always 0 */
1937bb873b53SIlya Dryomov 	ceph_encode_timespec(p, &req->r_mtime);
1938bb873b53SIlya Dryomov 	p += sizeof(struct ceph_timespec);
1939aa26d662SJeff Layton 
19402e59ffd1SIlya Dryomov 	encode_oloc(&p, end, &req->r_t.target_oloc);
19412e59ffd1SIlya Dryomov 	ceph_encode_string(&p, end, req->r_t.target_oid.name,
19422e59ffd1SIlya Dryomov 			   req->r_t.target_oid.name_len);
1943bb873b53SIlya Dryomov 
1944bb873b53SIlya Dryomov 	/* ops, can imply data */
1945bb873b53SIlya Dryomov 	ceph_encode_16(&p, req->r_num_ops);
1946bb873b53SIlya Dryomov 	for (i = 0; i < req->r_num_ops; i++) {
1947bb873b53SIlya Dryomov 		data_len += osd_req_encode_op(p, &req->r_ops[i]);
1948bb873b53SIlya Dryomov 		p += sizeof(struct ceph_osd_op);
1949bb873b53SIlya Dryomov 	}
1950bb873b53SIlya Dryomov 
1951bb873b53SIlya Dryomov 	ceph_encode_64(&p, req->r_snapid); /* snapid */
1952bb873b53SIlya Dryomov 	if (req->r_snapc) {
1953bb873b53SIlya Dryomov 		ceph_encode_64(&p, req->r_snapc->seq);
1954bb873b53SIlya Dryomov 		ceph_encode_32(&p, req->r_snapc->num_snaps);
1955bb873b53SIlya Dryomov 		for (i = 0; i < req->r_snapc->num_snaps; i++)
1956bb873b53SIlya Dryomov 			ceph_encode_64(&p, req->r_snapc->snaps[i]);
1957bb873b53SIlya Dryomov 	} else {
1958bb873b53SIlya Dryomov 		ceph_encode_64(&p, 0); /* snap_seq */
1959bb873b53SIlya Dryomov 		ceph_encode_32(&p, 0); /* snaps len */
1960bb873b53SIlya Dryomov 	}
1961bb873b53SIlya Dryomov 
1962bb873b53SIlya Dryomov 	ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
1963986e8989SIlya Dryomov 	BUG_ON(p > end - 8); /* space for features */
1964bb873b53SIlya Dryomov 
19658cb441c0SIlya Dryomov 	msg->hdr.version = cpu_to_le16(8); /* MOSDOp v8 */
19668cb441c0SIlya Dryomov 	/* front_len is finalized in encode_request_finish() */
1967986e8989SIlya Dryomov 	msg->front.iov_len = p - msg->front.iov_base;
1968986e8989SIlya Dryomov 	msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1969bb873b53SIlya Dryomov 	msg->hdr.data_len = cpu_to_le32(data_len);
1970bb873b53SIlya Dryomov 	/*
1971bb873b53SIlya Dryomov 	 * The header "data_off" is a hint to the receiver allowing it
1972bb873b53SIlya Dryomov 	 * to align received data into its buffers such that there's no
1973bb873b53SIlya Dryomov 	 * need to re-copy it before writing it to disk (direct I/O).
1974bb873b53SIlya Dryomov 	 */
1975bb873b53SIlya Dryomov 	msg->hdr.data_off = cpu_to_le16(req->r_data_offset);
1976bb873b53SIlya Dryomov 
19778cb441c0SIlya Dryomov 	dout("%s req %p msg %p oid %s oid_len %d\n", __func__, req, msg,
19788cb441c0SIlya Dryomov 	     req->r_t.target_oid.name, req->r_t.target_oid.name_len);
19798cb441c0SIlya Dryomov }
19808cb441c0SIlya Dryomov 
19818cb441c0SIlya Dryomov static void encode_request_finish(struct ceph_msg *msg)
19828cb441c0SIlya Dryomov {
19838cb441c0SIlya Dryomov 	void *p = msg->front.iov_base;
1984986e8989SIlya Dryomov 	void *const partial_end = p + msg->front.iov_len;
19858cb441c0SIlya Dryomov 	void *const end = p + msg->front_alloc_len;
19868cb441c0SIlya Dryomov 
19878cb441c0SIlya Dryomov 	if (CEPH_HAVE_FEATURE(msg->con->peer_features, RESEND_ON_SPLIT)) {
19888cb441c0SIlya Dryomov 		/* luminous OSD -- encode features and be done */
1989986e8989SIlya Dryomov 		p = partial_end;
19908cb441c0SIlya Dryomov 		ceph_encode_64(&p, msg->con->peer_features);
19918cb441c0SIlya Dryomov 	} else {
19928cb441c0SIlya Dryomov 		struct {
19938cb441c0SIlya Dryomov 			char spgid[CEPH_ENCODING_START_BLK_LEN +
19948cb441c0SIlya Dryomov 				   CEPH_PGID_ENCODING_LEN + 1];
19958cb441c0SIlya Dryomov 			__le32 hash;
19968cb441c0SIlya Dryomov 			__le32 epoch;
19978cb441c0SIlya Dryomov 			__le32 flags;
19988cb441c0SIlya Dryomov 			char reqid[CEPH_ENCODING_START_BLK_LEN +
19998cb441c0SIlya Dryomov 				   sizeof(struct ceph_osd_reqid)];
20008cb441c0SIlya Dryomov 			char trace[sizeof(struct ceph_blkin_trace_info)];
20018cb441c0SIlya Dryomov 			__le32 client_inc;
20028cb441c0SIlya Dryomov 			struct ceph_timespec mtime;
20038cb441c0SIlya Dryomov 		} __packed head;
20048cb441c0SIlya Dryomov 		struct ceph_pg pgid;
20058cb441c0SIlya Dryomov 		void *oloc, *oid, *tail;
20068cb441c0SIlya Dryomov 		int oloc_len, oid_len, tail_len;
20078cb441c0SIlya Dryomov 		int len;
20088cb441c0SIlya Dryomov 
20098cb441c0SIlya Dryomov 		/*
20108cb441c0SIlya Dryomov 		 * Pre-luminous OSD -- reencode v8 into v4 using @head
20118cb441c0SIlya Dryomov 		 * as a temporary buffer.  Encode the raw PG; the rest
20128cb441c0SIlya Dryomov 		 * is just a matter of moving oloc, oid and tail blobs
20138cb441c0SIlya Dryomov 		 * around.
20148cb441c0SIlya Dryomov 		 */
20158cb441c0SIlya Dryomov 		memcpy(&head, p, sizeof(head));
20168cb441c0SIlya Dryomov 		p += sizeof(head);
20178cb441c0SIlya Dryomov 
20188cb441c0SIlya Dryomov 		oloc = p;
20198cb441c0SIlya Dryomov 		p += CEPH_ENCODING_START_BLK_LEN;
20208cb441c0SIlya Dryomov 		pgid.pool = ceph_decode_64(&p);
20218cb441c0SIlya Dryomov 		p += 4 + 4; /* preferred, key len */
20228cb441c0SIlya Dryomov 		len = ceph_decode_32(&p);
20238cb441c0SIlya Dryomov 		p += len;   /* nspace */
20248cb441c0SIlya Dryomov 		oloc_len = p - oloc;
20258cb441c0SIlya Dryomov 
20268cb441c0SIlya Dryomov 		oid = p;
20278cb441c0SIlya Dryomov 		len = ceph_decode_32(&p);
20288cb441c0SIlya Dryomov 		p += len;
20298cb441c0SIlya Dryomov 		oid_len = p - oid;
20308cb441c0SIlya Dryomov 
20318cb441c0SIlya Dryomov 		tail = p;
2032986e8989SIlya Dryomov 		tail_len = partial_end - p;
20338cb441c0SIlya Dryomov 
20348cb441c0SIlya Dryomov 		p = msg->front.iov_base;
20358cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.client_inc, sizeof(head.client_inc));
20368cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.epoch, sizeof(head.epoch));
20378cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.flags, sizeof(head.flags));
20388cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.mtime, sizeof(head.mtime));
20398cb441c0SIlya Dryomov 
20408cb441c0SIlya Dryomov 		/* reassert_version */
20418cb441c0SIlya Dryomov 		memset(p, 0, sizeof(struct ceph_eversion));
20428cb441c0SIlya Dryomov 		p += sizeof(struct ceph_eversion);
20438cb441c0SIlya Dryomov 
20448cb441c0SIlya Dryomov 		BUG_ON(p >= oloc);
20458cb441c0SIlya Dryomov 		memmove(p, oloc, oloc_len);
20468cb441c0SIlya Dryomov 		p += oloc_len;
20478cb441c0SIlya Dryomov 
20488cb441c0SIlya Dryomov 		pgid.seed = le32_to_cpu(head.hash);
20498cb441c0SIlya Dryomov 		encode_pgid(&p, &pgid); /* raw pg */
20508cb441c0SIlya Dryomov 
20518cb441c0SIlya Dryomov 		BUG_ON(p >= oid);
20528cb441c0SIlya Dryomov 		memmove(p, oid, oid_len);
20538cb441c0SIlya Dryomov 		p += oid_len;
20548cb441c0SIlya Dryomov 
20558cb441c0SIlya Dryomov 		/* tail -- ops, snapid, snapc, retry_attempt */
20568cb441c0SIlya Dryomov 		BUG_ON(p >= tail);
20578cb441c0SIlya Dryomov 		memmove(p, tail, tail_len);
20588cb441c0SIlya Dryomov 		p += tail_len;
20598cb441c0SIlya Dryomov 
20608cb441c0SIlya Dryomov 		msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
20618cb441c0SIlya Dryomov 	}
20628cb441c0SIlya Dryomov 
20638cb441c0SIlya Dryomov 	BUG_ON(p > end);
20648cb441c0SIlya Dryomov 	msg->front.iov_len = p - msg->front.iov_base;
20658cb441c0SIlya Dryomov 	msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
20668cb441c0SIlya Dryomov 
20678cb441c0SIlya Dryomov 	dout("%s msg %p tid %llu %u+%u+%u v%d\n", __func__, msg,
20688cb441c0SIlya Dryomov 	     le64_to_cpu(msg->hdr.tid), le32_to_cpu(msg->hdr.front_len),
20698cb441c0SIlya Dryomov 	     le32_to_cpu(msg->hdr.middle_len), le32_to_cpu(msg->hdr.data_len),
20708cb441c0SIlya Dryomov 	     le16_to_cpu(msg->hdr.version));
2071bb873b53SIlya Dryomov }
2072bb873b53SIlya Dryomov 
2073bb873b53SIlya Dryomov /*
2074bb873b53SIlya Dryomov  * @req has to be assigned a tid and registered.
2075bb873b53SIlya Dryomov  */
2076bb873b53SIlya Dryomov static void send_request(struct ceph_osd_request *req)
2077bb873b53SIlya Dryomov {
2078bb873b53SIlya Dryomov 	struct ceph_osd *osd = req->r_osd;
2079bb873b53SIlya Dryomov 
20805aea3dcdSIlya Dryomov 	verify_osd_locked(osd);
2081bb873b53SIlya Dryomov 	WARN_ON(osd->o_osd != req->r_t.osd);
2082bb873b53SIlya Dryomov 
2083a02a946dSIlya Dryomov 	/* backoff? */
2084a02a946dSIlya Dryomov 	if (should_plug_request(req))
2085a02a946dSIlya Dryomov 		return;
2086a02a946dSIlya Dryomov 
20875aea3dcdSIlya Dryomov 	/*
20885aea3dcdSIlya Dryomov 	 * We may have a previously queued request message hanging
20895aea3dcdSIlya Dryomov 	 * around.  Cancel it to avoid corrupting the msgr.
20905aea3dcdSIlya Dryomov 	 */
20915aea3dcdSIlya Dryomov 	if (req->r_sent)
20925aea3dcdSIlya Dryomov 		ceph_msg_revoke(req->r_request);
20935aea3dcdSIlya Dryomov 
2094bb873b53SIlya Dryomov 	req->r_flags |= CEPH_OSD_FLAG_KNOWN_REDIR;
2095bb873b53SIlya Dryomov 	if (req->r_attempts)
2096bb873b53SIlya Dryomov 		req->r_flags |= CEPH_OSD_FLAG_RETRY;
2097bb873b53SIlya Dryomov 	else
2098bb873b53SIlya Dryomov 		WARN_ON(req->r_flags & CEPH_OSD_FLAG_RETRY);
2099bb873b53SIlya Dryomov 
21008cb441c0SIlya Dryomov 	encode_request_partial(req, req->r_request);
2101bb873b53SIlya Dryomov 
210204c7d789SIlya 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",
2103bb873b53SIlya Dryomov 	     __func__, req, req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed,
2104dc98ff72SIlya Dryomov 	     req->r_t.spgid.pgid.pool, req->r_t.spgid.pgid.seed,
210504c7d789SIlya Dryomov 	     req->r_t.spgid.shard, osd->o_osd, req->r_t.epoch, req->r_flags,
210604c7d789SIlya Dryomov 	     req->r_attempts);
2107bb873b53SIlya Dryomov 
2108bb873b53SIlya Dryomov 	req->r_t.paused = false;
21093d14c5d2SYehuda Sadeh 	req->r_stamp = jiffies;
2110bb873b53SIlya Dryomov 	req->r_attempts++;
21113d14c5d2SYehuda Sadeh 
2112bb873b53SIlya Dryomov 	req->r_sent = osd->o_incarnation;
2113bb873b53SIlya Dryomov 	req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
2114bb873b53SIlya Dryomov 	ceph_con_send(&osd->o_con, ceph_msg_get(req->r_request));
21153d14c5d2SYehuda Sadeh }
21163d14c5d2SYehuda Sadeh 
211742c1b124SIlya Dryomov static void maybe_request_map(struct ceph_osd_client *osdc)
211842c1b124SIlya Dryomov {
211942c1b124SIlya Dryomov 	bool continuous = false;
212042c1b124SIlya Dryomov 
21215aea3dcdSIlya Dryomov 	verify_osdc_locked(osdc);
212242c1b124SIlya Dryomov 	WARN_ON(!osdc->osdmap->epoch);
212342c1b124SIlya Dryomov 
2124b7ec35b3SIlya Dryomov 	if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2125b7ec35b3SIlya Dryomov 	    ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD) ||
2126b7ec35b3SIlya Dryomov 	    ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
212742c1b124SIlya Dryomov 		dout("%s osdc %p continuous\n", __func__, osdc);
212842c1b124SIlya Dryomov 		continuous = true;
212942c1b124SIlya Dryomov 	} else {
213042c1b124SIlya Dryomov 		dout("%s osdc %p onetime\n", __func__, osdc);
213142c1b124SIlya Dryomov 	}
213242c1b124SIlya Dryomov 
213342c1b124SIlya Dryomov 	if (ceph_monc_want_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
213442c1b124SIlya Dryomov 			       osdc->osdmap->epoch + 1, continuous))
213542c1b124SIlya Dryomov 		ceph_monc_renew_subs(&osdc->client->monc);
213642c1b124SIlya Dryomov }
213742c1b124SIlya Dryomov 
2138a1f4020aSJeff Layton static void complete_request(struct ceph_osd_request *req, int err);
21394609245eSIlya Dryomov static void send_map_check(struct ceph_osd_request *req);
21404609245eSIlya Dryomov 
21415aea3dcdSIlya Dryomov static void __submit_request(struct ceph_osd_request *req, bool wrlocked)
21420bbfdfe8SIlya Dryomov {
21435aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
21445aea3dcdSIlya Dryomov 	struct ceph_osd *osd;
21454609245eSIlya Dryomov 	enum calc_target_result ct_res;
21465aea3dcdSIlya Dryomov 	bool need_send = false;
21475aea3dcdSIlya Dryomov 	bool promoted = false;
2148a1f4020aSJeff Layton 	bool need_abort = false;
21490bbfdfe8SIlya Dryomov 
2150b18b9550SIlya Dryomov 	WARN_ON(req->r_tid);
21515aea3dcdSIlya Dryomov 	dout("%s req %p wrlocked %d\n", __func__, req, wrlocked);
21525aea3dcdSIlya Dryomov 
21535aea3dcdSIlya Dryomov again:
21547de030d6SIlya Dryomov 	ct_res = calc_target(osdc, &req->r_t, NULL, false);
21554609245eSIlya Dryomov 	if (ct_res == CALC_TARGET_POOL_DNE && !wrlocked)
21564609245eSIlya Dryomov 		goto promote;
21574609245eSIlya Dryomov 
21585aea3dcdSIlya Dryomov 	osd = lookup_create_osd(osdc, req->r_t.osd, wrlocked);
21595aea3dcdSIlya Dryomov 	if (IS_ERR(osd)) {
21605aea3dcdSIlya Dryomov 		WARN_ON(PTR_ERR(osd) != -EAGAIN || wrlocked);
21615aea3dcdSIlya Dryomov 		goto promote;
21625aea3dcdSIlya Dryomov 	}
21635aea3dcdSIlya Dryomov 
216458eb7932SJeff Layton 	if (osdc->osdmap->epoch < osdc->epoch_barrier) {
216558eb7932SJeff Layton 		dout("req %p epoch %u barrier %u\n", req, osdc->osdmap->epoch,
216658eb7932SJeff Layton 		     osdc->epoch_barrier);
216758eb7932SJeff Layton 		req->r_t.paused = true;
216858eb7932SJeff Layton 		maybe_request_map(osdc);
216958eb7932SJeff Layton 	} else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2170b7ec35b3SIlya Dryomov 		   ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
21715aea3dcdSIlya Dryomov 		dout("req %p pausewr\n", req);
21725aea3dcdSIlya Dryomov 		req->r_t.paused = true;
21735aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
21745aea3dcdSIlya Dryomov 	} else if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
2175b7ec35b3SIlya Dryomov 		   ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
21765aea3dcdSIlya Dryomov 		dout("req %p pauserd\n", req);
21775aea3dcdSIlya Dryomov 		req->r_t.paused = true;
21785aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
21795aea3dcdSIlya Dryomov 	} else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
21805aea3dcdSIlya Dryomov 		   !(req->r_flags & (CEPH_OSD_FLAG_FULL_TRY |
21815aea3dcdSIlya Dryomov 				     CEPH_OSD_FLAG_FULL_FORCE)) &&
2182b7ec35b3SIlya Dryomov 		   (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
21835aea3dcdSIlya Dryomov 		    pool_full(osdc, req->r_t.base_oloc.pool))) {
21845aea3dcdSIlya Dryomov 		dout("req %p full/pool_full\n", req);
21855aea3dcdSIlya Dryomov 		pr_warn_ratelimited("FULL or reached pool quota\n");
21865aea3dcdSIlya Dryomov 		req->r_t.paused = true;
21875aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
2188a1f4020aSJeff Layton 		if (req->r_abort_on_full)
2189a1f4020aSJeff Layton 			need_abort = true;
21905aea3dcdSIlya Dryomov 	} else if (!osd_homeless(osd)) {
21915aea3dcdSIlya Dryomov 		need_send = true;
21920bbfdfe8SIlya Dryomov 	} else {
21935aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
21940bbfdfe8SIlya Dryomov 	}
21950bbfdfe8SIlya Dryomov 
21965aea3dcdSIlya Dryomov 	mutex_lock(&osd->lock);
21975aea3dcdSIlya Dryomov 	/*
21985aea3dcdSIlya Dryomov 	 * Assign the tid atomically with send_request() to protect
21995aea3dcdSIlya Dryomov 	 * multiple writes to the same object from racing with each
22005aea3dcdSIlya Dryomov 	 * other, resulting in out of order ops on the OSDs.
22015aea3dcdSIlya Dryomov 	 */
22025aea3dcdSIlya Dryomov 	req->r_tid = atomic64_inc_return(&osdc->last_tid);
22035aea3dcdSIlya Dryomov 	link_request(osd, req);
22045aea3dcdSIlya Dryomov 	if (need_send)
22055aea3dcdSIlya Dryomov 		send_request(req);
2206a1f4020aSJeff Layton 	else if (need_abort)
2207a1f4020aSJeff Layton 		complete_request(req, -ENOSPC);
22085aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
22095aea3dcdSIlya Dryomov 
22104609245eSIlya Dryomov 	if (ct_res == CALC_TARGET_POOL_DNE)
22114609245eSIlya Dryomov 		send_map_check(req);
22124609245eSIlya Dryomov 
22135aea3dcdSIlya Dryomov 	if (promoted)
22145aea3dcdSIlya Dryomov 		downgrade_write(&osdc->lock);
22155aea3dcdSIlya Dryomov 	return;
22165aea3dcdSIlya Dryomov 
22175aea3dcdSIlya Dryomov promote:
22185aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
22195aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
22205aea3dcdSIlya Dryomov 	wrlocked = true;
22215aea3dcdSIlya Dryomov 	promoted = true;
22225aea3dcdSIlya Dryomov 	goto again;
22230bbfdfe8SIlya Dryomov }
22240bbfdfe8SIlya Dryomov 
22255aea3dcdSIlya Dryomov static void account_request(struct ceph_osd_request *req)
22265aea3dcdSIlya Dryomov {
222754ea0046SIlya Dryomov 	WARN_ON(req->r_flags & (CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK));
2228b18b9550SIlya Dryomov 	WARN_ON(!(req->r_flags & (CEPH_OSD_FLAG_READ | CEPH_OSD_FLAG_WRITE)));
22295aea3dcdSIlya Dryomov 
2230b18b9550SIlya Dryomov 	req->r_flags |= CEPH_OSD_FLAG_ONDISK;
22315aea3dcdSIlya Dryomov 	atomic_inc(&req->r_osdc->num_requests);
22327cc5e38fSIlya Dryomov 
22337cc5e38fSIlya Dryomov 	req->r_start_stamp = jiffies;
22345aea3dcdSIlya Dryomov }
22355aea3dcdSIlya Dryomov 
22365aea3dcdSIlya Dryomov static void submit_request(struct ceph_osd_request *req, bool wrlocked)
22375aea3dcdSIlya Dryomov {
22385aea3dcdSIlya Dryomov 	ceph_osdc_get_request(req);
22395aea3dcdSIlya Dryomov 	account_request(req);
22405aea3dcdSIlya Dryomov 	__submit_request(req, wrlocked);
22415aea3dcdSIlya Dryomov }
22425aea3dcdSIlya Dryomov 
224345ee2c1dSIlya Dryomov static void finish_request(struct ceph_osd_request *req)
22445aea3dcdSIlya Dryomov {
22455aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
22465aea3dcdSIlya Dryomov 
22474609245eSIlya Dryomov 	WARN_ON(lookup_request_mc(&osdc->map_checks, req->r_tid));
224804c7d789SIlya Dryomov 	dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
224904c7d789SIlya Dryomov 
225004c7d789SIlya Dryomov 	if (req->r_osd)
225104c7d789SIlya Dryomov 		unlink_request(req->r_osd, req);
22525aea3dcdSIlya Dryomov 	atomic_dec(&osdc->num_requests);
22535aea3dcdSIlya Dryomov 
22545aea3dcdSIlya Dryomov 	/*
22555aea3dcdSIlya Dryomov 	 * If an OSD has failed or returned and a request has been sent
22565aea3dcdSIlya Dryomov 	 * twice, it's possible to get a reply and end up here while the
22575aea3dcdSIlya Dryomov 	 * request message is queued for delivery.  We will ignore the
22585aea3dcdSIlya Dryomov 	 * reply, so not a big deal, but better to try and catch it.
22595aea3dcdSIlya Dryomov 	 */
22605aea3dcdSIlya Dryomov 	ceph_msg_revoke(req->r_request);
22615aea3dcdSIlya Dryomov 	ceph_msg_revoke_incoming(req->r_reply);
22625aea3dcdSIlya Dryomov }
22635aea3dcdSIlya Dryomov 
2264fe5da05eSIlya Dryomov static void __complete_request(struct ceph_osd_request *req)
2265fe5da05eSIlya Dryomov {
2266b18b9550SIlya Dryomov 	if (req->r_callback) {
2267b18b9550SIlya Dryomov 		dout("%s req %p tid %llu cb %pf result %d\n", __func__, req,
2268b18b9550SIlya Dryomov 		     req->r_tid, req->r_callback, req->r_result);
2269fe5da05eSIlya Dryomov 		req->r_callback(req);
2270b18b9550SIlya Dryomov 	}
2271fe5da05eSIlya Dryomov }
2272fe5da05eSIlya Dryomov 
22734609245eSIlya Dryomov /*
2274b18b9550SIlya Dryomov  * This is open-coded in handle_reply().
22754609245eSIlya Dryomov  */
22764609245eSIlya Dryomov static void complete_request(struct ceph_osd_request *req, int err)
22774609245eSIlya Dryomov {
22784609245eSIlya Dryomov 	dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
22794609245eSIlya Dryomov 
22804609245eSIlya Dryomov 	req->r_result = err;
228145ee2c1dSIlya Dryomov 	finish_request(req);
22824609245eSIlya Dryomov 	__complete_request(req);
2283b18b9550SIlya Dryomov 	complete_all(&req->r_completion);
22844609245eSIlya Dryomov 	ceph_osdc_put_request(req);
22854609245eSIlya Dryomov }
22864609245eSIlya Dryomov 
22874609245eSIlya Dryomov static void cancel_map_check(struct ceph_osd_request *req)
22884609245eSIlya Dryomov {
22894609245eSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
22904609245eSIlya Dryomov 	struct ceph_osd_request *lookup_req;
22914609245eSIlya Dryomov 
22924609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
22934609245eSIlya Dryomov 
22944609245eSIlya Dryomov 	lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
22954609245eSIlya Dryomov 	if (!lookup_req)
22964609245eSIlya Dryomov 		return;
22974609245eSIlya Dryomov 
22984609245eSIlya Dryomov 	WARN_ON(lookup_req != req);
22994609245eSIlya Dryomov 	erase_request_mc(&osdc->map_checks, req);
23004609245eSIlya Dryomov 	ceph_osdc_put_request(req);
23014609245eSIlya Dryomov }
23024609245eSIlya Dryomov 
23035aea3dcdSIlya Dryomov static void cancel_request(struct ceph_osd_request *req)
23045aea3dcdSIlya Dryomov {
23055aea3dcdSIlya Dryomov 	dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
23065aea3dcdSIlya Dryomov 
23074609245eSIlya Dryomov 	cancel_map_check(req);
230845ee2c1dSIlya Dryomov 	finish_request(req);
2309b18b9550SIlya Dryomov 	complete_all(&req->r_completion);
2310c297eb42SIlya Dryomov 	ceph_osdc_put_request(req);
23115aea3dcdSIlya Dryomov }
23125aea3dcdSIlya Dryomov 
23137cc5e38fSIlya Dryomov static void abort_request(struct ceph_osd_request *req, int err)
23147cc5e38fSIlya Dryomov {
23157cc5e38fSIlya Dryomov 	dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
23167cc5e38fSIlya Dryomov 
23177cc5e38fSIlya Dryomov 	cancel_map_check(req);
23187cc5e38fSIlya Dryomov 	complete_request(req, err);
23197cc5e38fSIlya Dryomov }
23207cc5e38fSIlya Dryomov 
232158eb7932SJeff Layton static void update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
232258eb7932SJeff Layton {
232358eb7932SJeff Layton 	if (likely(eb > osdc->epoch_barrier)) {
232458eb7932SJeff Layton 		dout("updating epoch_barrier from %u to %u\n",
232558eb7932SJeff Layton 				osdc->epoch_barrier, eb);
232658eb7932SJeff Layton 		osdc->epoch_barrier = eb;
232758eb7932SJeff Layton 		/* Request map if we're not to the barrier yet */
232858eb7932SJeff Layton 		if (eb > osdc->osdmap->epoch)
232958eb7932SJeff Layton 			maybe_request_map(osdc);
233058eb7932SJeff Layton 	}
233158eb7932SJeff Layton }
233258eb7932SJeff Layton 
233358eb7932SJeff Layton void ceph_osdc_update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
233458eb7932SJeff Layton {
233558eb7932SJeff Layton 	down_read(&osdc->lock);
233658eb7932SJeff Layton 	if (unlikely(eb > osdc->epoch_barrier)) {
233758eb7932SJeff Layton 		up_read(&osdc->lock);
233858eb7932SJeff Layton 		down_write(&osdc->lock);
233958eb7932SJeff Layton 		update_epoch_barrier(osdc, eb);
234058eb7932SJeff Layton 		up_write(&osdc->lock);
234158eb7932SJeff Layton 	} else {
234258eb7932SJeff Layton 		up_read(&osdc->lock);
234358eb7932SJeff Layton 	}
234458eb7932SJeff Layton }
234558eb7932SJeff Layton EXPORT_SYMBOL(ceph_osdc_update_epoch_barrier);
234658eb7932SJeff Layton 
2347fc36d0a4SJeff Layton /*
2348fc36d0a4SJeff Layton  * Drop all pending requests that are stalled waiting on a full condition to
234958eb7932SJeff Layton  * clear, and complete them with ENOSPC as the return code. Set the
235058eb7932SJeff Layton  * osdc->epoch_barrier to the latest map epoch that we've seen if any were
235158eb7932SJeff Layton  * cancelled.
2352fc36d0a4SJeff Layton  */
2353fc36d0a4SJeff Layton static void ceph_osdc_abort_on_full(struct ceph_osd_client *osdc)
2354fc36d0a4SJeff Layton {
2355fc36d0a4SJeff Layton 	struct rb_node *n;
235658eb7932SJeff Layton 	bool victims = false;
2357fc36d0a4SJeff Layton 
2358fc36d0a4SJeff Layton 	dout("enter abort_on_full\n");
2359fc36d0a4SJeff Layton 
2360fc36d0a4SJeff Layton 	if (!ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) && !have_pool_full(osdc))
2361fc36d0a4SJeff Layton 		goto out;
2362fc36d0a4SJeff Layton 
236358eb7932SJeff Layton 	/* Scan list and see if there is anything to abort */
236458eb7932SJeff Layton 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
236558eb7932SJeff Layton 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
236658eb7932SJeff Layton 		struct rb_node *m;
236758eb7932SJeff Layton 
236858eb7932SJeff Layton 		m = rb_first(&osd->o_requests);
236958eb7932SJeff Layton 		while (m) {
237058eb7932SJeff Layton 			struct ceph_osd_request *req = rb_entry(m,
237158eb7932SJeff Layton 					struct ceph_osd_request, r_node);
237258eb7932SJeff Layton 			m = rb_next(m);
237358eb7932SJeff Layton 
237458eb7932SJeff Layton 			if (req->r_abort_on_full) {
237558eb7932SJeff Layton 				victims = true;
237658eb7932SJeff Layton 				break;
237758eb7932SJeff Layton 			}
237858eb7932SJeff Layton 		}
237958eb7932SJeff Layton 		if (victims)
238058eb7932SJeff Layton 			break;
238158eb7932SJeff Layton 	}
238258eb7932SJeff Layton 
238358eb7932SJeff Layton 	if (!victims)
238458eb7932SJeff Layton 		goto out;
238558eb7932SJeff Layton 
238658eb7932SJeff Layton 	/*
238758eb7932SJeff Layton 	 * Update the barrier to current epoch if it's behind that point,
238858eb7932SJeff Layton 	 * since we know we have some calls to be aborted in the tree.
238958eb7932SJeff Layton 	 */
239058eb7932SJeff Layton 	update_epoch_barrier(osdc, osdc->osdmap->epoch);
239158eb7932SJeff Layton 
2392fc36d0a4SJeff Layton 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
2393fc36d0a4SJeff Layton 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
2394fc36d0a4SJeff Layton 		struct rb_node *m;
2395fc36d0a4SJeff Layton 
2396fc36d0a4SJeff Layton 		m = rb_first(&osd->o_requests);
2397fc36d0a4SJeff Layton 		while (m) {
2398fc36d0a4SJeff Layton 			struct ceph_osd_request *req = rb_entry(m,
2399fc36d0a4SJeff Layton 					struct ceph_osd_request, r_node);
2400fc36d0a4SJeff Layton 			m = rb_next(m);
2401fc36d0a4SJeff Layton 
2402fc36d0a4SJeff Layton 			if (req->r_abort_on_full &&
2403fc36d0a4SJeff Layton 			    (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2404fc36d0a4SJeff Layton 			     pool_full(osdc, req->r_t.target_oloc.pool)))
2405fc36d0a4SJeff Layton 				abort_request(req, -ENOSPC);
2406fc36d0a4SJeff Layton 		}
2407fc36d0a4SJeff Layton 	}
2408fc36d0a4SJeff Layton out:
240958eb7932SJeff Layton 	dout("return abort_on_full barrier=%u\n", osdc->epoch_barrier);
2410fc36d0a4SJeff Layton }
2411fc36d0a4SJeff Layton 
24124609245eSIlya Dryomov static void check_pool_dne(struct ceph_osd_request *req)
24134609245eSIlya Dryomov {
24144609245eSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
24154609245eSIlya Dryomov 	struct ceph_osdmap *map = osdc->osdmap;
24164609245eSIlya Dryomov 
24174609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
24184609245eSIlya Dryomov 	WARN_ON(!map->epoch);
24194609245eSIlya Dryomov 
24204609245eSIlya Dryomov 	if (req->r_attempts) {
24214609245eSIlya Dryomov 		/*
24224609245eSIlya Dryomov 		 * We sent a request earlier, which means that
24234609245eSIlya Dryomov 		 * previously the pool existed, and now it does not
24244609245eSIlya Dryomov 		 * (i.e., it was deleted).
24254609245eSIlya Dryomov 		 */
24264609245eSIlya Dryomov 		req->r_map_dne_bound = map->epoch;
24274609245eSIlya Dryomov 		dout("%s req %p tid %llu pool disappeared\n", __func__, req,
24284609245eSIlya Dryomov 		     req->r_tid);
24294609245eSIlya Dryomov 	} else {
24304609245eSIlya Dryomov 		dout("%s req %p tid %llu map_dne_bound %u have %u\n", __func__,
24314609245eSIlya Dryomov 		     req, req->r_tid, req->r_map_dne_bound, map->epoch);
24324609245eSIlya Dryomov 	}
24334609245eSIlya Dryomov 
24344609245eSIlya Dryomov 	if (req->r_map_dne_bound) {
24354609245eSIlya Dryomov 		if (map->epoch >= req->r_map_dne_bound) {
24364609245eSIlya Dryomov 			/* we had a new enough map */
24374609245eSIlya Dryomov 			pr_info_ratelimited("tid %llu pool does not exist\n",
24384609245eSIlya Dryomov 					    req->r_tid);
24394609245eSIlya Dryomov 			complete_request(req, -ENOENT);
24404609245eSIlya Dryomov 		}
24414609245eSIlya Dryomov 	} else {
24424609245eSIlya Dryomov 		send_map_check(req);
24434609245eSIlya Dryomov 	}
24444609245eSIlya Dryomov }
24454609245eSIlya Dryomov 
24464609245eSIlya Dryomov static void map_check_cb(struct ceph_mon_generic_request *greq)
24474609245eSIlya Dryomov {
24484609245eSIlya Dryomov 	struct ceph_osd_client *osdc = &greq->monc->client->osdc;
24494609245eSIlya Dryomov 	struct ceph_osd_request *req;
24504609245eSIlya Dryomov 	u64 tid = greq->private_data;
24514609245eSIlya Dryomov 
24524609245eSIlya Dryomov 	WARN_ON(greq->result || !greq->u.newest);
24534609245eSIlya Dryomov 
24544609245eSIlya Dryomov 	down_write(&osdc->lock);
24554609245eSIlya Dryomov 	req = lookup_request_mc(&osdc->map_checks, tid);
24564609245eSIlya Dryomov 	if (!req) {
24574609245eSIlya Dryomov 		dout("%s tid %llu dne\n", __func__, tid);
24584609245eSIlya Dryomov 		goto out_unlock;
24594609245eSIlya Dryomov 	}
24604609245eSIlya Dryomov 
24614609245eSIlya Dryomov 	dout("%s req %p tid %llu map_dne_bound %u newest %llu\n", __func__,
24624609245eSIlya Dryomov 	     req, req->r_tid, req->r_map_dne_bound, greq->u.newest);
24634609245eSIlya Dryomov 	if (!req->r_map_dne_bound)
24644609245eSIlya Dryomov 		req->r_map_dne_bound = greq->u.newest;
24654609245eSIlya Dryomov 	erase_request_mc(&osdc->map_checks, req);
24664609245eSIlya Dryomov 	check_pool_dne(req);
24674609245eSIlya Dryomov 
24684609245eSIlya Dryomov 	ceph_osdc_put_request(req);
24694609245eSIlya Dryomov out_unlock:
24704609245eSIlya Dryomov 	up_write(&osdc->lock);
24714609245eSIlya Dryomov }
24724609245eSIlya Dryomov 
24734609245eSIlya Dryomov static void send_map_check(struct ceph_osd_request *req)
24744609245eSIlya Dryomov {
24754609245eSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
24764609245eSIlya Dryomov 	struct ceph_osd_request *lookup_req;
24774609245eSIlya Dryomov 	int ret;
24784609245eSIlya Dryomov 
24794609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
24804609245eSIlya Dryomov 
24814609245eSIlya Dryomov 	lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
24824609245eSIlya Dryomov 	if (lookup_req) {
24834609245eSIlya Dryomov 		WARN_ON(lookup_req != req);
24844609245eSIlya Dryomov 		return;
24854609245eSIlya Dryomov 	}
24864609245eSIlya Dryomov 
24874609245eSIlya Dryomov 	ceph_osdc_get_request(req);
24884609245eSIlya Dryomov 	insert_request_mc(&osdc->map_checks, req);
24894609245eSIlya Dryomov 	ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
24904609245eSIlya Dryomov 					  map_check_cb, req->r_tid);
24914609245eSIlya Dryomov 	WARN_ON(ret);
24924609245eSIlya Dryomov }
24934609245eSIlya Dryomov 
24940bbfdfe8SIlya Dryomov /*
2495922dab61SIlya Dryomov  * lingering requests, watch/notify v2 infrastructure
2496922dab61SIlya Dryomov  */
2497922dab61SIlya Dryomov static void linger_release(struct kref *kref)
2498922dab61SIlya Dryomov {
2499922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq =
2500922dab61SIlya Dryomov 	    container_of(kref, struct ceph_osd_linger_request, kref);
2501922dab61SIlya Dryomov 
2502922dab61SIlya Dryomov 	dout("%s lreq %p reg_req %p ping_req %p\n", __func__, lreq,
2503922dab61SIlya Dryomov 	     lreq->reg_req, lreq->ping_req);
2504922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&lreq->node));
2505922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&lreq->osdc_node));
25064609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&lreq->mc_node));
2507922dab61SIlya Dryomov 	WARN_ON(!list_empty(&lreq->scan_item));
2508b07d3c4bSIlya Dryomov 	WARN_ON(!list_empty(&lreq->pending_lworks));
2509922dab61SIlya Dryomov 	WARN_ON(lreq->osd);
2510922dab61SIlya Dryomov 
2511922dab61SIlya Dryomov 	if (lreq->reg_req)
2512922dab61SIlya Dryomov 		ceph_osdc_put_request(lreq->reg_req);
2513922dab61SIlya Dryomov 	if (lreq->ping_req)
2514922dab61SIlya Dryomov 		ceph_osdc_put_request(lreq->ping_req);
2515922dab61SIlya Dryomov 	target_destroy(&lreq->t);
2516922dab61SIlya Dryomov 	kfree(lreq);
2517922dab61SIlya Dryomov }
2518922dab61SIlya Dryomov 
2519922dab61SIlya Dryomov static void linger_put(struct ceph_osd_linger_request *lreq)
2520922dab61SIlya Dryomov {
2521922dab61SIlya Dryomov 	if (lreq)
2522922dab61SIlya Dryomov 		kref_put(&lreq->kref, linger_release);
2523922dab61SIlya Dryomov }
2524922dab61SIlya Dryomov 
2525922dab61SIlya Dryomov static struct ceph_osd_linger_request *
2526922dab61SIlya Dryomov linger_get(struct ceph_osd_linger_request *lreq)
2527922dab61SIlya Dryomov {
2528922dab61SIlya Dryomov 	kref_get(&lreq->kref);
2529922dab61SIlya Dryomov 	return lreq;
2530922dab61SIlya Dryomov }
2531922dab61SIlya Dryomov 
2532922dab61SIlya Dryomov static struct ceph_osd_linger_request *
2533922dab61SIlya Dryomov linger_alloc(struct ceph_osd_client *osdc)
2534922dab61SIlya Dryomov {
2535922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
2536922dab61SIlya Dryomov 
2537922dab61SIlya Dryomov 	lreq = kzalloc(sizeof(*lreq), GFP_NOIO);
2538922dab61SIlya Dryomov 	if (!lreq)
2539922dab61SIlya Dryomov 		return NULL;
2540922dab61SIlya Dryomov 
2541922dab61SIlya Dryomov 	kref_init(&lreq->kref);
2542922dab61SIlya Dryomov 	mutex_init(&lreq->lock);
2543922dab61SIlya Dryomov 	RB_CLEAR_NODE(&lreq->node);
2544922dab61SIlya Dryomov 	RB_CLEAR_NODE(&lreq->osdc_node);
25454609245eSIlya Dryomov 	RB_CLEAR_NODE(&lreq->mc_node);
2546922dab61SIlya Dryomov 	INIT_LIST_HEAD(&lreq->scan_item);
2547b07d3c4bSIlya Dryomov 	INIT_LIST_HEAD(&lreq->pending_lworks);
2548922dab61SIlya Dryomov 	init_completion(&lreq->reg_commit_wait);
254919079203SIlya Dryomov 	init_completion(&lreq->notify_finish_wait);
2550922dab61SIlya Dryomov 
2551922dab61SIlya Dryomov 	lreq->osdc = osdc;
2552922dab61SIlya Dryomov 	target_init(&lreq->t);
2553922dab61SIlya Dryomov 
2554922dab61SIlya Dryomov 	dout("%s lreq %p\n", __func__, lreq);
2555922dab61SIlya Dryomov 	return lreq;
2556922dab61SIlya Dryomov }
2557922dab61SIlya Dryomov 
2558922dab61SIlya Dryomov DEFINE_RB_INSDEL_FUNCS(linger, struct ceph_osd_linger_request, linger_id, node)
2559922dab61SIlya Dryomov DEFINE_RB_FUNCS(linger_osdc, struct ceph_osd_linger_request, linger_id, osdc_node)
25604609245eSIlya Dryomov DEFINE_RB_FUNCS(linger_mc, struct ceph_osd_linger_request, linger_id, mc_node)
2561922dab61SIlya Dryomov 
2562922dab61SIlya Dryomov /*
2563922dab61SIlya Dryomov  * Create linger request <-> OSD session relation.
2564922dab61SIlya Dryomov  *
2565922dab61SIlya Dryomov  * @lreq has to be registered, @osd may be homeless.
2566922dab61SIlya Dryomov  */
2567922dab61SIlya Dryomov static void link_linger(struct ceph_osd *osd,
2568922dab61SIlya Dryomov 			struct ceph_osd_linger_request *lreq)
2569922dab61SIlya Dryomov {
2570922dab61SIlya Dryomov 	verify_osd_locked(osd);
2571922dab61SIlya Dryomov 	WARN_ON(!lreq->linger_id || lreq->osd);
2572922dab61SIlya Dryomov 	dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2573922dab61SIlya Dryomov 	     osd->o_osd, lreq, lreq->linger_id);
2574922dab61SIlya Dryomov 
2575922dab61SIlya Dryomov 	if (!osd_homeless(osd))
2576922dab61SIlya Dryomov 		__remove_osd_from_lru(osd);
2577922dab61SIlya Dryomov 	else
2578922dab61SIlya Dryomov 		atomic_inc(&osd->o_osdc->num_homeless);
2579922dab61SIlya Dryomov 
2580922dab61SIlya Dryomov 	get_osd(osd);
2581922dab61SIlya Dryomov 	insert_linger(&osd->o_linger_requests, lreq);
2582922dab61SIlya Dryomov 	lreq->osd = osd;
2583922dab61SIlya Dryomov }
2584922dab61SIlya Dryomov 
2585922dab61SIlya Dryomov static void unlink_linger(struct ceph_osd *osd,
2586922dab61SIlya Dryomov 			  struct ceph_osd_linger_request *lreq)
2587922dab61SIlya Dryomov {
2588922dab61SIlya Dryomov 	verify_osd_locked(osd);
2589922dab61SIlya Dryomov 	WARN_ON(lreq->osd != osd);
2590922dab61SIlya Dryomov 	dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2591922dab61SIlya Dryomov 	     osd->o_osd, lreq, lreq->linger_id);
2592922dab61SIlya Dryomov 
2593922dab61SIlya Dryomov 	lreq->osd = NULL;
2594922dab61SIlya Dryomov 	erase_linger(&osd->o_linger_requests, lreq);
2595922dab61SIlya Dryomov 	put_osd(osd);
2596922dab61SIlya Dryomov 
2597922dab61SIlya Dryomov 	if (!osd_homeless(osd))
2598922dab61SIlya Dryomov 		maybe_move_osd_to_lru(osd);
2599922dab61SIlya Dryomov 	else
2600922dab61SIlya Dryomov 		atomic_dec(&osd->o_osdc->num_homeless);
2601922dab61SIlya Dryomov }
2602922dab61SIlya Dryomov 
2603922dab61SIlya Dryomov static bool __linger_registered(struct ceph_osd_linger_request *lreq)
2604922dab61SIlya Dryomov {
2605922dab61SIlya Dryomov 	verify_osdc_locked(lreq->osdc);
2606922dab61SIlya Dryomov 
2607922dab61SIlya Dryomov 	return !RB_EMPTY_NODE(&lreq->osdc_node);
2608922dab61SIlya Dryomov }
2609922dab61SIlya Dryomov 
2610922dab61SIlya Dryomov static bool linger_registered(struct ceph_osd_linger_request *lreq)
2611922dab61SIlya Dryomov {
2612922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2613922dab61SIlya Dryomov 	bool registered;
2614922dab61SIlya Dryomov 
2615922dab61SIlya Dryomov 	down_read(&osdc->lock);
2616922dab61SIlya Dryomov 	registered = __linger_registered(lreq);
2617922dab61SIlya Dryomov 	up_read(&osdc->lock);
2618922dab61SIlya Dryomov 
2619922dab61SIlya Dryomov 	return registered;
2620922dab61SIlya Dryomov }
2621922dab61SIlya Dryomov 
2622922dab61SIlya Dryomov static void linger_register(struct ceph_osd_linger_request *lreq)
2623922dab61SIlya Dryomov {
2624922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2625922dab61SIlya Dryomov 
2626922dab61SIlya Dryomov 	verify_osdc_wrlocked(osdc);
2627922dab61SIlya Dryomov 	WARN_ON(lreq->linger_id);
2628922dab61SIlya Dryomov 
2629922dab61SIlya Dryomov 	linger_get(lreq);
2630922dab61SIlya Dryomov 	lreq->linger_id = ++osdc->last_linger_id;
2631922dab61SIlya Dryomov 	insert_linger_osdc(&osdc->linger_requests, lreq);
2632922dab61SIlya Dryomov }
2633922dab61SIlya Dryomov 
2634922dab61SIlya Dryomov static void linger_unregister(struct ceph_osd_linger_request *lreq)
2635922dab61SIlya Dryomov {
2636922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2637922dab61SIlya Dryomov 
2638922dab61SIlya Dryomov 	verify_osdc_wrlocked(osdc);
2639922dab61SIlya Dryomov 
2640922dab61SIlya Dryomov 	erase_linger_osdc(&osdc->linger_requests, lreq);
2641922dab61SIlya Dryomov 	linger_put(lreq);
2642922dab61SIlya Dryomov }
2643922dab61SIlya Dryomov 
2644922dab61SIlya Dryomov static void cancel_linger_request(struct ceph_osd_request *req)
2645922dab61SIlya Dryomov {
2646922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
2647922dab61SIlya Dryomov 
2648922dab61SIlya Dryomov 	WARN_ON(!req->r_linger);
2649922dab61SIlya Dryomov 	cancel_request(req);
2650922dab61SIlya Dryomov 	linger_put(lreq);
2651922dab61SIlya Dryomov }
2652922dab61SIlya Dryomov 
2653922dab61SIlya Dryomov struct linger_work {
2654922dab61SIlya Dryomov 	struct work_struct work;
2655922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
2656b07d3c4bSIlya Dryomov 	struct list_head pending_item;
2657b07d3c4bSIlya Dryomov 	unsigned long queued_stamp;
2658922dab61SIlya Dryomov 
2659922dab61SIlya Dryomov 	union {
2660922dab61SIlya Dryomov 		struct {
2661922dab61SIlya Dryomov 			u64 notify_id;
2662922dab61SIlya Dryomov 			u64 notifier_id;
2663922dab61SIlya Dryomov 			void *payload; /* points into @msg front */
2664922dab61SIlya Dryomov 			size_t payload_len;
2665922dab61SIlya Dryomov 
2666922dab61SIlya Dryomov 			struct ceph_msg *msg; /* for ceph_msg_put() */
2667922dab61SIlya Dryomov 		} notify;
2668922dab61SIlya Dryomov 		struct {
2669922dab61SIlya Dryomov 			int err;
2670922dab61SIlya Dryomov 		} error;
2671922dab61SIlya Dryomov 	};
2672922dab61SIlya Dryomov };
2673922dab61SIlya Dryomov 
2674922dab61SIlya Dryomov static struct linger_work *lwork_alloc(struct ceph_osd_linger_request *lreq,
2675922dab61SIlya Dryomov 				       work_func_t workfn)
2676922dab61SIlya Dryomov {
2677922dab61SIlya Dryomov 	struct linger_work *lwork;
2678922dab61SIlya Dryomov 
2679922dab61SIlya Dryomov 	lwork = kzalloc(sizeof(*lwork), GFP_NOIO);
2680922dab61SIlya Dryomov 	if (!lwork)
2681922dab61SIlya Dryomov 		return NULL;
2682922dab61SIlya Dryomov 
2683922dab61SIlya Dryomov 	INIT_WORK(&lwork->work, workfn);
2684b07d3c4bSIlya Dryomov 	INIT_LIST_HEAD(&lwork->pending_item);
2685922dab61SIlya Dryomov 	lwork->lreq = linger_get(lreq);
2686922dab61SIlya Dryomov 
2687922dab61SIlya Dryomov 	return lwork;
2688922dab61SIlya Dryomov }
2689922dab61SIlya Dryomov 
2690922dab61SIlya Dryomov static void lwork_free(struct linger_work *lwork)
2691922dab61SIlya Dryomov {
2692922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2693922dab61SIlya Dryomov 
2694b07d3c4bSIlya Dryomov 	mutex_lock(&lreq->lock);
2695b07d3c4bSIlya Dryomov 	list_del(&lwork->pending_item);
2696b07d3c4bSIlya Dryomov 	mutex_unlock(&lreq->lock);
2697b07d3c4bSIlya Dryomov 
2698922dab61SIlya Dryomov 	linger_put(lreq);
2699922dab61SIlya Dryomov 	kfree(lwork);
2700922dab61SIlya Dryomov }
2701922dab61SIlya Dryomov 
2702922dab61SIlya Dryomov static void lwork_queue(struct linger_work *lwork)
2703922dab61SIlya Dryomov {
2704922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2705922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2706922dab61SIlya Dryomov 
2707922dab61SIlya Dryomov 	verify_lreq_locked(lreq);
2708b07d3c4bSIlya Dryomov 	WARN_ON(!list_empty(&lwork->pending_item));
2709b07d3c4bSIlya Dryomov 
2710b07d3c4bSIlya Dryomov 	lwork->queued_stamp = jiffies;
2711b07d3c4bSIlya Dryomov 	list_add_tail(&lwork->pending_item, &lreq->pending_lworks);
2712922dab61SIlya Dryomov 	queue_work(osdc->notify_wq, &lwork->work);
2713922dab61SIlya Dryomov }
2714922dab61SIlya Dryomov 
2715922dab61SIlya Dryomov static void do_watch_notify(struct work_struct *w)
2716922dab61SIlya Dryomov {
2717922dab61SIlya Dryomov 	struct linger_work *lwork = container_of(w, struct linger_work, work);
2718922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2719922dab61SIlya Dryomov 
2720922dab61SIlya Dryomov 	if (!linger_registered(lreq)) {
2721922dab61SIlya Dryomov 		dout("%s lreq %p not registered\n", __func__, lreq);
2722922dab61SIlya Dryomov 		goto out;
2723922dab61SIlya Dryomov 	}
2724922dab61SIlya Dryomov 
272519079203SIlya Dryomov 	WARN_ON(!lreq->is_watch);
2726922dab61SIlya Dryomov 	dout("%s lreq %p notify_id %llu notifier_id %llu payload_len %zu\n",
2727922dab61SIlya Dryomov 	     __func__, lreq, lwork->notify.notify_id, lwork->notify.notifier_id,
2728922dab61SIlya Dryomov 	     lwork->notify.payload_len);
2729922dab61SIlya Dryomov 	lreq->wcb(lreq->data, lwork->notify.notify_id, lreq->linger_id,
2730922dab61SIlya Dryomov 		  lwork->notify.notifier_id, lwork->notify.payload,
2731922dab61SIlya Dryomov 		  lwork->notify.payload_len);
2732922dab61SIlya Dryomov 
2733922dab61SIlya Dryomov out:
2734922dab61SIlya Dryomov 	ceph_msg_put(lwork->notify.msg);
2735922dab61SIlya Dryomov 	lwork_free(lwork);
2736922dab61SIlya Dryomov }
2737922dab61SIlya Dryomov 
2738922dab61SIlya Dryomov static void do_watch_error(struct work_struct *w)
2739922dab61SIlya Dryomov {
2740922dab61SIlya Dryomov 	struct linger_work *lwork = container_of(w, struct linger_work, work);
2741922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2742922dab61SIlya Dryomov 
2743922dab61SIlya Dryomov 	if (!linger_registered(lreq)) {
2744922dab61SIlya Dryomov 		dout("%s lreq %p not registered\n", __func__, lreq);
2745922dab61SIlya Dryomov 		goto out;
2746922dab61SIlya Dryomov 	}
2747922dab61SIlya Dryomov 
2748922dab61SIlya Dryomov 	dout("%s lreq %p err %d\n", __func__, lreq, lwork->error.err);
2749922dab61SIlya Dryomov 	lreq->errcb(lreq->data, lreq->linger_id, lwork->error.err);
2750922dab61SIlya Dryomov 
2751922dab61SIlya Dryomov out:
2752922dab61SIlya Dryomov 	lwork_free(lwork);
2753922dab61SIlya Dryomov }
2754922dab61SIlya Dryomov 
2755922dab61SIlya Dryomov static void queue_watch_error(struct ceph_osd_linger_request *lreq)
2756922dab61SIlya Dryomov {
2757922dab61SIlya Dryomov 	struct linger_work *lwork;
2758922dab61SIlya Dryomov 
2759922dab61SIlya Dryomov 	lwork = lwork_alloc(lreq, do_watch_error);
2760922dab61SIlya Dryomov 	if (!lwork) {
2761922dab61SIlya Dryomov 		pr_err("failed to allocate error-lwork\n");
2762922dab61SIlya Dryomov 		return;
2763922dab61SIlya Dryomov 	}
2764922dab61SIlya Dryomov 
2765922dab61SIlya Dryomov 	lwork->error.err = lreq->last_error;
2766922dab61SIlya Dryomov 	lwork_queue(lwork);
2767922dab61SIlya Dryomov }
2768922dab61SIlya Dryomov 
2769922dab61SIlya Dryomov static void linger_reg_commit_complete(struct ceph_osd_linger_request *lreq,
2770922dab61SIlya Dryomov 				       int result)
2771922dab61SIlya Dryomov {
2772922dab61SIlya Dryomov 	if (!completion_done(&lreq->reg_commit_wait)) {
2773922dab61SIlya Dryomov 		lreq->reg_commit_error = (result <= 0 ? result : 0);
2774922dab61SIlya Dryomov 		complete_all(&lreq->reg_commit_wait);
2775922dab61SIlya Dryomov 	}
2776922dab61SIlya Dryomov }
2777922dab61SIlya Dryomov 
2778922dab61SIlya Dryomov static void linger_commit_cb(struct ceph_osd_request *req)
2779922dab61SIlya Dryomov {
2780922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
2781922dab61SIlya Dryomov 
2782922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
2783922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu result %d\n", __func__, lreq,
2784922dab61SIlya Dryomov 	     lreq->linger_id, req->r_result);
2785922dab61SIlya Dryomov 	linger_reg_commit_complete(lreq, req->r_result);
2786922dab61SIlya Dryomov 	lreq->committed = true;
2787922dab61SIlya Dryomov 
278819079203SIlya Dryomov 	if (!lreq->is_watch) {
278919079203SIlya Dryomov 		struct ceph_osd_data *osd_data =
279019079203SIlya Dryomov 		    osd_req_op_data(req, 0, notify, response_data);
279119079203SIlya Dryomov 		void *p = page_address(osd_data->pages[0]);
279219079203SIlya Dryomov 
279319079203SIlya Dryomov 		WARN_ON(req->r_ops[0].op != CEPH_OSD_OP_NOTIFY ||
279419079203SIlya Dryomov 			osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
279519079203SIlya Dryomov 
279619079203SIlya Dryomov 		/* make note of the notify_id */
279719079203SIlya Dryomov 		if (req->r_ops[0].outdata_len >= sizeof(u64)) {
279819079203SIlya Dryomov 			lreq->notify_id = ceph_decode_64(&p);
279919079203SIlya Dryomov 			dout("lreq %p notify_id %llu\n", lreq,
280019079203SIlya Dryomov 			     lreq->notify_id);
280119079203SIlya Dryomov 		} else {
280219079203SIlya Dryomov 			dout("lreq %p no notify_id\n", lreq);
280319079203SIlya Dryomov 		}
280419079203SIlya Dryomov 	}
280519079203SIlya Dryomov 
2806922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
2807922dab61SIlya Dryomov 	linger_put(lreq);
2808922dab61SIlya Dryomov }
2809922dab61SIlya Dryomov 
2810922dab61SIlya Dryomov static int normalize_watch_error(int err)
2811922dab61SIlya Dryomov {
2812922dab61SIlya Dryomov 	/*
2813922dab61SIlya Dryomov 	 * Translate ENOENT -> ENOTCONN so that a delete->disconnection
2814922dab61SIlya Dryomov 	 * notification and a failure to reconnect because we raced with
2815922dab61SIlya Dryomov 	 * the delete appear the same to the user.
2816922dab61SIlya Dryomov 	 */
2817922dab61SIlya Dryomov 	if (err == -ENOENT)
2818922dab61SIlya Dryomov 		err = -ENOTCONN;
2819922dab61SIlya Dryomov 
2820922dab61SIlya Dryomov 	return err;
2821922dab61SIlya Dryomov }
2822922dab61SIlya Dryomov 
2823922dab61SIlya Dryomov static void linger_reconnect_cb(struct ceph_osd_request *req)
2824922dab61SIlya Dryomov {
2825922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
2826922dab61SIlya Dryomov 
2827922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
2828922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu result %d last_error %d\n", __func__,
2829922dab61SIlya Dryomov 	     lreq, lreq->linger_id, req->r_result, lreq->last_error);
2830922dab61SIlya Dryomov 	if (req->r_result < 0) {
2831922dab61SIlya Dryomov 		if (!lreq->last_error) {
2832922dab61SIlya Dryomov 			lreq->last_error = normalize_watch_error(req->r_result);
2833922dab61SIlya Dryomov 			queue_watch_error(lreq);
2834922dab61SIlya Dryomov 		}
2835922dab61SIlya Dryomov 	}
2836922dab61SIlya Dryomov 
2837922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
2838922dab61SIlya Dryomov 	linger_put(lreq);
2839922dab61SIlya Dryomov }
2840922dab61SIlya Dryomov 
2841922dab61SIlya Dryomov static void send_linger(struct ceph_osd_linger_request *lreq)
2842922dab61SIlya Dryomov {
2843922dab61SIlya Dryomov 	struct ceph_osd_request *req = lreq->reg_req;
2844922dab61SIlya Dryomov 	struct ceph_osd_req_op *op = &req->r_ops[0];
2845922dab61SIlya Dryomov 
2846922dab61SIlya Dryomov 	verify_osdc_wrlocked(req->r_osdc);
2847922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2848922dab61SIlya Dryomov 
2849922dab61SIlya Dryomov 	if (req->r_osd)
2850922dab61SIlya Dryomov 		cancel_linger_request(req);
2851922dab61SIlya Dryomov 
2852922dab61SIlya Dryomov 	request_reinit(req);
2853922dab61SIlya Dryomov 	ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
2854922dab61SIlya Dryomov 	ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
2855922dab61SIlya Dryomov 	req->r_flags = lreq->t.flags;
2856922dab61SIlya Dryomov 	req->r_mtime = lreq->mtime;
2857922dab61SIlya Dryomov 
2858922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
285919079203SIlya Dryomov 	if (lreq->is_watch && lreq->committed) {
2860922dab61SIlya Dryomov 		WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2861922dab61SIlya Dryomov 			op->watch.cookie != lreq->linger_id);
2862922dab61SIlya Dryomov 		op->watch.op = CEPH_OSD_WATCH_OP_RECONNECT;
2863922dab61SIlya Dryomov 		op->watch.gen = ++lreq->register_gen;
2864922dab61SIlya Dryomov 		dout("lreq %p reconnect register_gen %u\n", lreq,
2865922dab61SIlya Dryomov 		     op->watch.gen);
2866922dab61SIlya Dryomov 		req->r_callback = linger_reconnect_cb;
2867922dab61SIlya Dryomov 	} else {
286819079203SIlya Dryomov 		if (!lreq->is_watch)
286919079203SIlya Dryomov 			lreq->notify_id = 0;
287019079203SIlya Dryomov 		else
2871922dab61SIlya Dryomov 			WARN_ON(op->watch.op != CEPH_OSD_WATCH_OP_WATCH);
2872922dab61SIlya Dryomov 		dout("lreq %p register\n", lreq);
2873922dab61SIlya Dryomov 		req->r_callback = linger_commit_cb;
2874922dab61SIlya Dryomov 	}
2875922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
2876922dab61SIlya Dryomov 
2877922dab61SIlya Dryomov 	req->r_priv = linger_get(lreq);
2878922dab61SIlya Dryomov 	req->r_linger = true;
2879922dab61SIlya Dryomov 
2880922dab61SIlya Dryomov 	submit_request(req, true);
2881922dab61SIlya Dryomov }
2882922dab61SIlya Dryomov 
2883922dab61SIlya Dryomov static void linger_ping_cb(struct ceph_osd_request *req)
2884922dab61SIlya Dryomov {
2885922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
2886922dab61SIlya Dryomov 
2887922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
2888922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu result %d ping_sent %lu last_error %d\n",
2889922dab61SIlya Dryomov 	     __func__, lreq, lreq->linger_id, req->r_result, lreq->ping_sent,
2890922dab61SIlya Dryomov 	     lreq->last_error);
2891922dab61SIlya Dryomov 	if (lreq->register_gen == req->r_ops[0].watch.gen) {
2892b07d3c4bSIlya Dryomov 		if (!req->r_result) {
2893b07d3c4bSIlya Dryomov 			lreq->watch_valid_thru = lreq->ping_sent;
2894b07d3c4bSIlya Dryomov 		} else if (!lreq->last_error) {
2895922dab61SIlya Dryomov 			lreq->last_error = normalize_watch_error(req->r_result);
2896922dab61SIlya Dryomov 			queue_watch_error(lreq);
2897922dab61SIlya Dryomov 		}
2898922dab61SIlya Dryomov 	} else {
2899922dab61SIlya Dryomov 		dout("lreq %p register_gen %u ignoring old pong %u\n", lreq,
2900922dab61SIlya Dryomov 		     lreq->register_gen, req->r_ops[0].watch.gen);
2901922dab61SIlya Dryomov 	}
2902922dab61SIlya Dryomov 
2903922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
2904922dab61SIlya Dryomov 	linger_put(lreq);
2905922dab61SIlya Dryomov }
2906922dab61SIlya Dryomov 
2907922dab61SIlya Dryomov static void send_linger_ping(struct ceph_osd_linger_request *lreq)
2908922dab61SIlya Dryomov {
2909922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2910922dab61SIlya Dryomov 	struct ceph_osd_request *req = lreq->ping_req;
2911922dab61SIlya Dryomov 	struct ceph_osd_req_op *op = &req->r_ops[0];
2912922dab61SIlya Dryomov 
2913b7ec35b3SIlya Dryomov 	if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
2914922dab61SIlya Dryomov 		dout("%s PAUSERD\n", __func__);
2915922dab61SIlya Dryomov 		return;
2916922dab61SIlya Dryomov 	}
2917922dab61SIlya Dryomov 
2918922dab61SIlya Dryomov 	lreq->ping_sent = jiffies;
2919922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu ping_sent %lu register_gen %u\n",
2920922dab61SIlya Dryomov 	     __func__, lreq, lreq->linger_id, lreq->ping_sent,
2921922dab61SIlya Dryomov 	     lreq->register_gen);
2922922dab61SIlya Dryomov 
2923922dab61SIlya Dryomov 	if (req->r_osd)
2924922dab61SIlya Dryomov 		cancel_linger_request(req);
2925922dab61SIlya Dryomov 
2926922dab61SIlya Dryomov 	request_reinit(req);
2927922dab61SIlya Dryomov 	target_copy(&req->r_t, &lreq->t);
2928922dab61SIlya Dryomov 
2929922dab61SIlya Dryomov 	WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2930922dab61SIlya Dryomov 		op->watch.cookie != lreq->linger_id ||
2931922dab61SIlya Dryomov 		op->watch.op != CEPH_OSD_WATCH_OP_PING);
2932922dab61SIlya Dryomov 	op->watch.gen = lreq->register_gen;
2933922dab61SIlya Dryomov 	req->r_callback = linger_ping_cb;
2934922dab61SIlya Dryomov 	req->r_priv = linger_get(lreq);
2935922dab61SIlya Dryomov 	req->r_linger = true;
2936922dab61SIlya Dryomov 
2937922dab61SIlya Dryomov 	ceph_osdc_get_request(req);
2938922dab61SIlya Dryomov 	account_request(req);
2939922dab61SIlya Dryomov 	req->r_tid = atomic64_inc_return(&osdc->last_tid);
2940922dab61SIlya Dryomov 	link_request(lreq->osd, req);
2941922dab61SIlya Dryomov 	send_request(req);
2942922dab61SIlya Dryomov }
2943922dab61SIlya Dryomov 
2944922dab61SIlya Dryomov static void linger_submit(struct ceph_osd_linger_request *lreq)
2945922dab61SIlya Dryomov {
2946922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2947922dab61SIlya Dryomov 	struct ceph_osd *osd;
2948922dab61SIlya Dryomov 
29497de030d6SIlya Dryomov 	calc_target(osdc, &lreq->t, NULL, false);
2950922dab61SIlya Dryomov 	osd = lookup_create_osd(osdc, lreq->t.osd, true);
2951922dab61SIlya Dryomov 	link_linger(osd, lreq);
2952922dab61SIlya Dryomov 
2953922dab61SIlya Dryomov 	send_linger(lreq);
2954922dab61SIlya Dryomov }
2955922dab61SIlya Dryomov 
29564609245eSIlya Dryomov static void cancel_linger_map_check(struct ceph_osd_linger_request *lreq)
29574609245eSIlya Dryomov {
29584609245eSIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
29594609245eSIlya Dryomov 	struct ceph_osd_linger_request *lookup_lreq;
29604609245eSIlya Dryomov 
29614609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
29624609245eSIlya Dryomov 
29634609245eSIlya Dryomov 	lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
29644609245eSIlya Dryomov 				       lreq->linger_id);
29654609245eSIlya Dryomov 	if (!lookup_lreq)
29664609245eSIlya Dryomov 		return;
29674609245eSIlya Dryomov 
29684609245eSIlya Dryomov 	WARN_ON(lookup_lreq != lreq);
29694609245eSIlya Dryomov 	erase_linger_mc(&osdc->linger_map_checks, lreq);
29704609245eSIlya Dryomov 	linger_put(lreq);
29714609245eSIlya Dryomov }
29724609245eSIlya Dryomov 
2973922dab61SIlya Dryomov /*
2974922dab61SIlya Dryomov  * @lreq has to be both registered and linked.
2975922dab61SIlya Dryomov  */
2976922dab61SIlya Dryomov static void __linger_cancel(struct ceph_osd_linger_request *lreq)
2977922dab61SIlya Dryomov {
297819079203SIlya Dryomov 	if (lreq->is_watch && lreq->ping_req->r_osd)
2979922dab61SIlya Dryomov 		cancel_linger_request(lreq->ping_req);
2980922dab61SIlya Dryomov 	if (lreq->reg_req->r_osd)
2981922dab61SIlya Dryomov 		cancel_linger_request(lreq->reg_req);
29824609245eSIlya Dryomov 	cancel_linger_map_check(lreq);
2983922dab61SIlya Dryomov 	unlink_linger(lreq->osd, lreq);
2984922dab61SIlya Dryomov 	linger_unregister(lreq);
2985922dab61SIlya Dryomov }
2986922dab61SIlya Dryomov 
2987922dab61SIlya Dryomov static void linger_cancel(struct ceph_osd_linger_request *lreq)
2988922dab61SIlya Dryomov {
2989922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2990922dab61SIlya Dryomov 
2991922dab61SIlya Dryomov 	down_write(&osdc->lock);
2992922dab61SIlya Dryomov 	if (__linger_registered(lreq))
2993922dab61SIlya Dryomov 		__linger_cancel(lreq);
2994922dab61SIlya Dryomov 	up_write(&osdc->lock);
2995922dab61SIlya Dryomov }
2996922dab61SIlya Dryomov 
29974609245eSIlya Dryomov static void send_linger_map_check(struct ceph_osd_linger_request *lreq);
29984609245eSIlya Dryomov 
29994609245eSIlya Dryomov static void check_linger_pool_dne(struct ceph_osd_linger_request *lreq)
30004609245eSIlya Dryomov {
30014609245eSIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
30024609245eSIlya Dryomov 	struct ceph_osdmap *map = osdc->osdmap;
30034609245eSIlya Dryomov 
30044609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
30054609245eSIlya Dryomov 	WARN_ON(!map->epoch);
30064609245eSIlya Dryomov 
30074609245eSIlya Dryomov 	if (lreq->register_gen) {
30084609245eSIlya Dryomov 		lreq->map_dne_bound = map->epoch;
30094609245eSIlya Dryomov 		dout("%s lreq %p linger_id %llu pool disappeared\n", __func__,
30104609245eSIlya Dryomov 		     lreq, lreq->linger_id);
30114609245eSIlya Dryomov 	} else {
30124609245eSIlya Dryomov 		dout("%s lreq %p linger_id %llu map_dne_bound %u have %u\n",
30134609245eSIlya Dryomov 		     __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
30144609245eSIlya Dryomov 		     map->epoch);
30154609245eSIlya Dryomov 	}
30164609245eSIlya Dryomov 
30174609245eSIlya Dryomov 	if (lreq->map_dne_bound) {
30184609245eSIlya Dryomov 		if (map->epoch >= lreq->map_dne_bound) {
30194609245eSIlya Dryomov 			/* we had a new enough map */
30204609245eSIlya Dryomov 			pr_info("linger_id %llu pool does not exist\n",
30214609245eSIlya Dryomov 				lreq->linger_id);
30224609245eSIlya Dryomov 			linger_reg_commit_complete(lreq, -ENOENT);
30234609245eSIlya Dryomov 			__linger_cancel(lreq);
30244609245eSIlya Dryomov 		}
30254609245eSIlya Dryomov 	} else {
30264609245eSIlya Dryomov 		send_linger_map_check(lreq);
30274609245eSIlya Dryomov 	}
30284609245eSIlya Dryomov }
30294609245eSIlya Dryomov 
30304609245eSIlya Dryomov static void linger_map_check_cb(struct ceph_mon_generic_request *greq)
30314609245eSIlya Dryomov {
30324609245eSIlya Dryomov 	struct ceph_osd_client *osdc = &greq->monc->client->osdc;
30334609245eSIlya Dryomov 	struct ceph_osd_linger_request *lreq;
30344609245eSIlya Dryomov 	u64 linger_id = greq->private_data;
30354609245eSIlya Dryomov 
30364609245eSIlya Dryomov 	WARN_ON(greq->result || !greq->u.newest);
30374609245eSIlya Dryomov 
30384609245eSIlya Dryomov 	down_write(&osdc->lock);
30394609245eSIlya Dryomov 	lreq = lookup_linger_mc(&osdc->linger_map_checks, linger_id);
30404609245eSIlya Dryomov 	if (!lreq) {
30414609245eSIlya Dryomov 		dout("%s linger_id %llu dne\n", __func__, linger_id);
30424609245eSIlya Dryomov 		goto out_unlock;
30434609245eSIlya Dryomov 	}
30444609245eSIlya Dryomov 
30454609245eSIlya Dryomov 	dout("%s lreq %p linger_id %llu map_dne_bound %u newest %llu\n",
30464609245eSIlya Dryomov 	     __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
30474609245eSIlya Dryomov 	     greq->u.newest);
30484609245eSIlya Dryomov 	if (!lreq->map_dne_bound)
30494609245eSIlya Dryomov 		lreq->map_dne_bound = greq->u.newest;
30504609245eSIlya Dryomov 	erase_linger_mc(&osdc->linger_map_checks, lreq);
30514609245eSIlya Dryomov 	check_linger_pool_dne(lreq);
30524609245eSIlya Dryomov 
30534609245eSIlya Dryomov 	linger_put(lreq);
30544609245eSIlya Dryomov out_unlock:
30554609245eSIlya Dryomov 	up_write(&osdc->lock);
30564609245eSIlya Dryomov }
30574609245eSIlya Dryomov 
30584609245eSIlya Dryomov static void send_linger_map_check(struct ceph_osd_linger_request *lreq)
30594609245eSIlya Dryomov {
30604609245eSIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
30614609245eSIlya Dryomov 	struct ceph_osd_linger_request *lookup_lreq;
30624609245eSIlya Dryomov 	int ret;
30634609245eSIlya Dryomov 
30644609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
30654609245eSIlya Dryomov 
30664609245eSIlya Dryomov 	lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
30674609245eSIlya Dryomov 				       lreq->linger_id);
30684609245eSIlya Dryomov 	if (lookup_lreq) {
30694609245eSIlya Dryomov 		WARN_ON(lookup_lreq != lreq);
30704609245eSIlya Dryomov 		return;
30714609245eSIlya Dryomov 	}
30724609245eSIlya Dryomov 
30734609245eSIlya Dryomov 	linger_get(lreq);
30744609245eSIlya Dryomov 	insert_linger_mc(&osdc->linger_map_checks, lreq);
30754609245eSIlya Dryomov 	ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
30764609245eSIlya Dryomov 					  linger_map_check_cb, lreq->linger_id);
30774609245eSIlya Dryomov 	WARN_ON(ret);
30784609245eSIlya Dryomov }
30794609245eSIlya Dryomov 
3080922dab61SIlya Dryomov static int linger_reg_commit_wait(struct ceph_osd_linger_request *lreq)
3081922dab61SIlya Dryomov {
3082922dab61SIlya Dryomov 	int ret;
3083922dab61SIlya Dryomov 
3084922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3085922dab61SIlya Dryomov 	ret = wait_for_completion_interruptible(&lreq->reg_commit_wait);
3086922dab61SIlya Dryomov 	return ret ?: lreq->reg_commit_error;
3087922dab61SIlya Dryomov }
3088922dab61SIlya Dryomov 
308919079203SIlya Dryomov static int linger_notify_finish_wait(struct ceph_osd_linger_request *lreq)
309019079203SIlya Dryomov {
309119079203SIlya Dryomov 	int ret;
309219079203SIlya Dryomov 
309319079203SIlya Dryomov 	dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
309419079203SIlya Dryomov 	ret = wait_for_completion_interruptible(&lreq->notify_finish_wait);
309519079203SIlya Dryomov 	return ret ?: lreq->notify_finish_error;
309619079203SIlya Dryomov }
309719079203SIlya Dryomov 
3098922dab61SIlya Dryomov /*
3099fbca9635SIlya Dryomov  * Timeout callback, called every N seconds.  When 1 or more OSD
3100fbca9635SIlya Dryomov  * requests has been active for more than N seconds, we send a keepalive
3101fbca9635SIlya Dryomov  * (tag + timestamp) to its OSD to ensure any communications channel
3102fbca9635SIlya Dryomov  * reset is detected.
31033d14c5d2SYehuda Sadeh  */
31043d14c5d2SYehuda Sadeh static void handle_timeout(struct work_struct *work)
31053d14c5d2SYehuda Sadeh {
31063d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc =
31073d14c5d2SYehuda Sadeh 		container_of(work, struct ceph_osd_client, timeout_work.work);
3108a319bf56SIlya Dryomov 	struct ceph_options *opts = osdc->client->options;
31095aea3dcdSIlya Dryomov 	unsigned long cutoff = jiffies - opts->osd_keepalive_timeout;
31107cc5e38fSIlya Dryomov 	unsigned long expiry_cutoff = jiffies - opts->osd_request_timeout;
31115aea3dcdSIlya Dryomov 	LIST_HEAD(slow_osds);
31125aea3dcdSIlya Dryomov 	struct rb_node *n, *p;
31133d14c5d2SYehuda Sadeh 
31145aea3dcdSIlya Dryomov 	dout("%s osdc %p\n", __func__, osdc);
31155aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
31163d14c5d2SYehuda Sadeh 
31173d14c5d2SYehuda Sadeh 	/*
31183d14c5d2SYehuda Sadeh 	 * ping osds that are a bit slow.  this ensures that if there
31193d14c5d2SYehuda Sadeh 	 * is a break in the TCP connection we will notice, and reopen
31203d14c5d2SYehuda Sadeh 	 * a connection with that osd (from the fault callback).
31213d14c5d2SYehuda Sadeh 	 */
31225aea3dcdSIlya Dryomov 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
31235aea3dcdSIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
31245aea3dcdSIlya Dryomov 		bool found = false;
31253d14c5d2SYehuda Sadeh 
31267cc5e38fSIlya Dryomov 		for (p = rb_first(&osd->o_requests); p; ) {
31275aea3dcdSIlya Dryomov 			struct ceph_osd_request *req =
31285aea3dcdSIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
31295aea3dcdSIlya Dryomov 
31307cc5e38fSIlya Dryomov 			p = rb_next(p); /* abort_request() */
31317cc5e38fSIlya Dryomov 
31325aea3dcdSIlya Dryomov 			if (time_before(req->r_stamp, cutoff)) {
31335aea3dcdSIlya Dryomov 				dout(" req %p tid %llu on osd%d is laggy\n",
31345aea3dcdSIlya Dryomov 				     req, req->r_tid, osd->o_osd);
31355aea3dcdSIlya Dryomov 				found = true;
31365aea3dcdSIlya Dryomov 			}
31377cc5e38fSIlya Dryomov 			if (opts->osd_request_timeout &&
31387cc5e38fSIlya Dryomov 			    time_before(req->r_start_stamp, expiry_cutoff)) {
31397cc5e38fSIlya Dryomov 				pr_err_ratelimited("tid %llu on osd%d timeout\n",
31407cc5e38fSIlya Dryomov 				       req->r_tid, osd->o_osd);
31417cc5e38fSIlya Dryomov 				abort_request(req, -ETIMEDOUT);
31427cc5e38fSIlya Dryomov 			}
31435aea3dcdSIlya Dryomov 		}
3144922dab61SIlya Dryomov 		for (p = rb_first(&osd->o_linger_requests); p; p = rb_next(p)) {
3145922dab61SIlya Dryomov 			struct ceph_osd_linger_request *lreq =
3146922dab61SIlya Dryomov 			    rb_entry(p, struct ceph_osd_linger_request, node);
3147922dab61SIlya Dryomov 
3148922dab61SIlya Dryomov 			dout(" lreq %p linger_id %llu is served by osd%d\n",
3149922dab61SIlya Dryomov 			     lreq, lreq->linger_id, osd->o_osd);
3150922dab61SIlya Dryomov 			found = true;
3151922dab61SIlya Dryomov 
3152922dab61SIlya Dryomov 			mutex_lock(&lreq->lock);
315319079203SIlya Dryomov 			if (lreq->is_watch && lreq->committed && !lreq->last_error)
3154922dab61SIlya Dryomov 				send_linger_ping(lreq);
3155922dab61SIlya Dryomov 			mutex_unlock(&lreq->lock);
3156922dab61SIlya Dryomov 		}
31575aea3dcdSIlya Dryomov 
31585aea3dcdSIlya Dryomov 		if (found)
31593d14c5d2SYehuda Sadeh 			list_move_tail(&osd->o_keepalive_item, &slow_osds);
31603d14c5d2SYehuda Sadeh 	}
31615aea3dcdSIlya Dryomov 
31627cc5e38fSIlya Dryomov 	if (opts->osd_request_timeout) {
31637cc5e38fSIlya Dryomov 		for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
31647cc5e38fSIlya Dryomov 			struct ceph_osd_request *req =
31657cc5e38fSIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
31667cc5e38fSIlya Dryomov 
31677cc5e38fSIlya Dryomov 			p = rb_next(p); /* abort_request() */
31687cc5e38fSIlya Dryomov 
31697cc5e38fSIlya Dryomov 			if (time_before(req->r_start_stamp, expiry_cutoff)) {
31707cc5e38fSIlya Dryomov 				pr_err_ratelimited("tid %llu on osd%d timeout\n",
31717cc5e38fSIlya Dryomov 				       req->r_tid, osdc->homeless_osd.o_osd);
31727cc5e38fSIlya Dryomov 				abort_request(req, -ETIMEDOUT);
31737cc5e38fSIlya Dryomov 			}
31747cc5e38fSIlya Dryomov 		}
31757cc5e38fSIlya Dryomov 	}
31767cc5e38fSIlya Dryomov 
31775aea3dcdSIlya Dryomov 	if (atomic_read(&osdc->num_homeless) || !list_empty(&slow_osds))
31785aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
31795aea3dcdSIlya Dryomov 
31803d14c5d2SYehuda Sadeh 	while (!list_empty(&slow_osds)) {
31815aea3dcdSIlya Dryomov 		struct ceph_osd *osd = list_first_entry(&slow_osds,
31825aea3dcdSIlya Dryomov 							struct ceph_osd,
31833d14c5d2SYehuda Sadeh 							o_keepalive_item);
31843d14c5d2SYehuda Sadeh 		list_del_init(&osd->o_keepalive_item);
31853d14c5d2SYehuda Sadeh 		ceph_con_keepalive(&osd->o_con);
31863d14c5d2SYehuda Sadeh 	}
31873d14c5d2SYehuda Sadeh 
31885aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
3189fbca9635SIlya Dryomov 	schedule_delayed_work(&osdc->timeout_work,
3190fbca9635SIlya Dryomov 			      osdc->client->options->osd_keepalive_timeout);
31913d14c5d2SYehuda Sadeh }
31923d14c5d2SYehuda Sadeh 
31933d14c5d2SYehuda Sadeh static void handle_osds_timeout(struct work_struct *work)
31943d14c5d2SYehuda Sadeh {
31953d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc =
31963d14c5d2SYehuda Sadeh 		container_of(work, struct ceph_osd_client,
31973d14c5d2SYehuda Sadeh 			     osds_timeout_work.work);
3198a319bf56SIlya Dryomov 	unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
319942a2c09fSIlya Dryomov 	struct ceph_osd *osd, *nosd;
32003d14c5d2SYehuda Sadeh 
320142a2c09fSIlya Dryomov 	dout("%s osdc %p\n", __func__, osdc);
32025aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
320342a2c09fSIlya Dryomov 	list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
320442a2c09fSIlya Dryomov 		if (time_before(jiffies, osd->lru_ttl))
320542a2c09fSIlya Dryomov 			break;
320642a2c09fSIlya Dryomov 
32075aea3dcdSIlya Dryomov 		WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
3208922dab61SIlya Dryomov 		WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
32095aea3dcdSIlya Dryomov 		close_osd(osd);
321042a2c09fSIlya Dryomov 	}
321142a2c09fSIlya Dryomov 
32125aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
32133d14c5d2SYehuda Sadeh 	schedule_delayed_work(&osdc->osds_timeout_work,
32143d14c5d2SYehuda Sadeh 			      round_jiffies_relative(delay));
32153d14c5d2SYehuda Sadeh }
32163d14c5d2SYehuda Sadeh 
3217205ee118SIlya Dryomov static int ceph_oloc_decode(void **p, void *end,
3218205ee118SIlya Dryomov 			    struct ceph_object_locator *oloc)
3219205ee118SIlya Dryomov {
3220205ee118SIlya Dryomov 	u8 struct_v, struct_cv;
3221205ee118SIlya Dryomov 	u32 len;
3222205ee118SIlya Dryomov 	void *struct_end;
3223205ee118SIlya Dryomov 	int ret = 0;
3224205ee118SIlya Dryomov 
3225205ee118SIlya Dryomov 	ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3226205ee118SIlya Dryomov 	struct_v = ceph_decode_8(p);
3227205ee118SIlya Dryomov 	struct_cv = ceph_decode_8(p);
3228205ee118SIlya Dryomov 	if (struct_v < 3) {
3229205ee118SIlya Dryomov 		pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
3230205ee118SIlya Dryomov 			struct_v, struct_cv);
3231205ee118SIlya Dryomov 		goto e_inval;
3232205ee118SIlya Dryomov 	}
3233205ee118SIlya Dryomov 	if (struct_cv > 6) {
3234205ee118SIlya Dryomov 		pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
3235205ee118SIlya Dryomov 			struct_v, struct_cv);
3236205ee118SIlya Dryomov 		goto e_inval;
3237205ee118SIlya Dryomov 	}
3238205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3239205ee118SIlya Dryomov 	ceph_decode_need(p, end, len, e_inval);
3240205ee118SIlya Dryomov 	struct_end = *p + len;
3241205ee118SIlya Dryomov 
3242205ee118SIlya Dryomov 	oloc->pool = ceph_decode_64(p);
3243205ee118SIlya Dryomov 	*p += 4; /* skip preferred */
3244205ee118SIlya Dryomov 
3245205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3246205ee118SIlya Dryomov 	if (len > 0) {
3247205ee118SIlya Dryomov 		pr_warn("ceph_object_locator::key is set\n");
3248205ee118SIlya Dryomov 		goto e_inval;
3249205ee118SIlya Dryomov 	}
3250205ee118SIlya Dryomov 
3251205ee118SIlya Dryomov 	if (struct_v >= 5) {
3252cd08e0a2SYan, Zheng 		bool changed = false;
3253cd08e0a2SYan, Zheng 
3254205ee118SIlya Dryomov 		len = ceph_decode_32(p);
3255205ee118SIlya Dryomov 		if (len > 0) {
325630c156d9SYan, Zheng 			ceph_decode_need(p, end, len, e_inval);
3257cd08e0a2SYan, Zheng 			if (!oloc->pool_ns ||
3258cd08e0a2SYan, Zheng 			    ceph_compare_string(oloc->pool_ns, *p, len))
3259cd08e0a2SYan, Zheng 				changed = true;
326030c156d9SYan, Zheng 			*p += len;
3261cd08e0a2SYan, Zheng 		} else {
3262cd08e0a2SYan, Zheng 			if (oloc->pool_ns)
3263cd08e0a2SYan, Zheng 				changed = true;
3264cd08e0a2SYan, Zheng 		}
3265cd08e0a2SYan, Zheng 		if (changed) {
3266cd08e0a2SYan, Zheng 			/* redirect changes namespace */
3267cd08e0a2SYan, Zheng 			pr_warn("ceph_object_locator::nspace is changed\n");
3268cd08e0a2SYan, Zheng 			goto e_inval;
3269205ee118SIlya Dryomov 		}
3270205ee118SIlya Dryomov 	}
3271205ee118SIlya Dryomov 
3272205ee118SIlya Dryomov 	if (struct_v >= 6) {
3273205ee118SIlya Dryomov 		s64 hash = ceph_decode_64(p);
3274205ee118SIlya Dryomov 		if (hash != -1) {
3275205ee118SIlya Dryomov 			pr_warn("ceph_object_locator::hash is set\n");
3276205ee118SIlya Dryomov 			goto e_inval;
3277205ee118SIlya Dryomov 		}
3278205ee118SIlya Dryomov 	}
3279205ee118SIlya Dryomov 
3280205ee118SIlya Dryomov 	/* skip the rest */
3281205ee118SIlya Dryomov 	*p = struct_end;
3282205ee118SIlya Dryomov out:
3283205ee118SIlya Dryomov 	return ret;
3284205ee118SIlya Dryomov 
3285205ee118SIlya Dryomov e_inval:
3286205ee118SIlya Dryomov 	ret = -EINVAL;
3287205ee118SIlya Dryomov 	goto out;
3288205ee118SIlya Dryomov }
3289205ee118SIlya Dryomov 
3290205ee118SIlya Dryomov static int ceph_redirect_decode(void **p, void *end,
3291205ee118SIlya Dryomov 				struct ceph_request_redirect *redir)
3292205ee118SIlya Dryomov {
3293205ee118SIlya Dryomov 	u8 struct_v, struct_cv;
3294205ee118SIlya Dryomov 	u32 len;
3295205ee118SIlya Dryomov 	void *struct_end;
3296205ee118SIlya Dryomov 	int ret;
3297205ee118SIlya Dryomov 
3298205ee118SIlya Dryomov 	ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3299205ee118SIlya Dryomov 	struct_v = ceph_decode_8(p);
3300205ee118SIlya Dryomov 	struct_cv = ceph_decode_8(p);
3301205ee118SIlya Dryomov 	if (struct_cv > 1) {
3302205ee118SIlya Dryomov 		pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
3303205ee118SIlya Dryomov 			struct_v, struct_cv);
3304205ee118SIlya Dryomov 		goto e_inval;
3305205ee118SIlya Dryomov 	}
3306205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3307205ee118SIlya Dryomov 	ceph_decode_need(p, end, len, e_inval);
3308205ee118SIlya Dryomov 	struct_end = *p + len;
3309205ee118SIlya Dryomov 
3310205ee118SIlya Dryomov 	ret = ceph_oloc_decode(p, end, &redir->oloc);
3311205ee118SIlya Dryomov 	if (ret)
3312205ee118SIlya Dryomov 		goto out;
3313205ee118SIlya Dryomov 
3314205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3315205ee118SIlya Dryomov 	if (len > 0) {
3316205ee118SIlya Dryomov 		pr_warn("ceph_request_redirect::object_name is set\n");
3317205ee118SIlya Dryomov 		goto e_inval;
3318205ee118SIlya Dryomov 	}
3319205ee118SIlya Dryomov 
3320205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3321205ee118SIlya Dryomov 	*p += len; /* skip osd_instructions */
3322205ee118SIlya Dryomov 
3323205ee118SIlya Dryomov 	/* skip the rest */
3324205ee118SIlya Dryomov 	*p = struct_end;
3325205ee118SIlya Dryomov out:
3326205ee118SIlya Dryomov 	return ret;
3327205ee118SIlya Dryomov 
3328205ee118SIlya Dryomov e_inval:
3329205ee118SIlya Dryomov 	ret = -EINVAL;
3330205ee118SIlya Dryomov 	goto out;
3331205ee118SIlya Dryomov }
3332205ee118SIlya Dryomov 
3333fe5da05eSIlya Dryomov struct MOSDOpReply {
3334fe5da05eSIlya Dryomov 	struct ceph_pg pgid;
3335fe5da05eSIlya Dryomov 	u64 flags;
3336fe5da05eSIlya Dryomov 	int result;
3337fe5da05eSIlya Dryomov 	u32 epoch;
3338fe5da05eSIlya Dryomov 	int num_ops;
3339fe5da05eSIlya Dryomov 	u32 outdata_len[CEPH_OSD_MAX_OPS];
3340fe5da05eSIlya Dryomov 	s32 rval[CEPH_OSD_MAX_OPS];
3341fe5da05eSIlya Dryomov 	int retry_attempt;
3342fe5da05eSIlya Dryomov 	struct ceph_eversion replay_version;
3343fe5da05eSIlya Dryomov 	u64 user_version;
3344fe5da05eSIlya Dryomov 	struct ceph_request_redirect redirect;
3345fe5da05eSIlya Dryomov };
334625845472SSage Weil 
3347fe5da05eSIlya Dryomov static int decode_MOSDOpReply(const struct ceph_msg *msg, struct MOSDOpReply *m)
33483d14c5d2SYehuda Sadeh {
3349fe5da05eSIlya Dryomov 	void *p = msg->front.iov_base;
3350fe5da05eSIlya Dryomov 	void *const end = p + msg->front.iov_len;
3351fe5da05eSIlya Dryomov 	u16 version = le16_to_cpu(msg->hdr.version);
3352fe5da05eSIlya Dryomov 	struct ceph_eversion bad_replay_version;
3353b0b31a8fSIlya Dryomov 	u8 decode_redir;
3354fe5da05eSIlya Dryomov 	u32 len;
3355fe5da05eSIlya Dryomov 	int ret;
3356fe5da05eSIlya Dryomov 	int i;
33573d14c5d2SYehuda Sadeh 
3358fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, len, e_inval);
3359fe5da05eSIlya Dryomov 	ceph_decode_need(&p, end, len, e_inval);
3360fe5da05eSIlya Dryomov 	p += len; /* skip oid */
33611b83bef2SSage Weil 
3362fe5da05eSIlya Dryomov 	ret = ceph_decode_pgid(&p, end, &m->pgid);
3363fe5da05eSIlya Dryomov 	if (ret)
3364fe5da05eSIlya Dryomov 		return ret;
33651b83bef2SSage Weil 
3366fe5da05eSIlya Dryomov 	ceph_decode_64_safe(&p, end, m->flags, e_inval);
3367fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->result, e_inval);
3368fe5da05eSIlya Dryomov 	ceph_decode_need(&p, end, sizeof(bad_replay_version), e_inval);
3369fe5da05eSIlya Dryomov 	memcpy(&bad_replay_version, p, sizeof(bad_replay_version));
3370fe5da05eSIlya Dryomov 	p += sizeof(bad_replay_version);
3371fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->epoch, e_inval);
33721b83bef2SSage Weil 
3373fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->num_ops, e_inval);
3374fe5da05eSIlya Dryomov 	if (m->num_ops > ARRAY_SIZE(m->outdata_len))
3375fe5da05eSIlya Dryomov 		goto e_inval;
33761b83bef2SSage Weil 
3377fe5da05eSIlya Dryomov 	ceph_decode_need(&p, end, m->num_ops * sizeof(struct ceph_osd_op),
3378fe5da05eSIlya Dryomov 			 e_inval);
3379fe5da05eSIlya Dryomov 	for (i = 0; i < m->num_ops; i++) {
33801b83bef2SSage Weil 		struct ceph_osd_op *op = p;
33811b83bef2SSage Weil 
3382fe5da05eSIlya Dryomov 		m->outdata_len[i] = le32_to_cpu(op->payload_len);
33831b83bef2SSage Weil 		p += sizeof(*op);
33841b83bef2SSage Weil 	}
3385fe5da05eSIlya Dryomov 
3386fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->retry_attempt, e_inval);
3387fe5da05eSIlya Dryomov 	for (i = 0; i < m->num_ops; i++)
3388fe5da05eSIlya Dryomov 		ceph_decode_32_safe(&p, end, m->rval[i], e_inval);
3389fe5da05eSIlya Dryomov 
3390fe5da05eSIlya Dryomov 	if (version >= 5) {
3391fe5da05eSIlya Dryomov 		ceph_decode_need(&p, end, sizeof(m->replay_version), e_inval);
3392fe5da05eSIlya Dryomov 		memcpy(&m->replay_version, p, sizeof(m->replay_version));
3393fe5da05eSIlya Dryomov 		p += sizeof(m->replay_version);
3394fe5da05eSIlya Dryomov 		ceph_decode_64_safe(&p, end, m->user_version, e_inval);
3395fe5da05eSIlya Dryomov 	} else {
3396fe5da05eSIlya Dryomov 		m->replay_version = bad_replay_version; /* struct */
3397fe5da05eSIlya Dryomov 		m->user_version = le64_to_cpu(m->replay_version.version);
33981b83bef2SSage Weil 	}
33991b83bef2SSage Weil 
3400fe5da05eSIlya Dryomov 	if (version >= 6) {
3401fe5da05eSIlya Dryomov 		if (version >= 7)
3402fe5da05eSIlya Dryomov 			ceph_decode_8_safe(&p, end, decode_redir, e_inval);
3403b0b31a8fSIlya Dryomov 		else
3404b0b31a8fSIlya Dryomov 			decode_redir = 1;
3405b0b31a8fSIlya Dryomov 	} else {
3406b0b31a8fSIlya Dryomov 		decode_redir = 0;
3407b0b31a8fSIlya Dryomov 	}
3408b0b31a8fSIlya Dryomov 
3409b0b31a8fSIlya Dryomov 	if (decode_redir) {
3410fe5da05eSIlya Dryomov 		ret = ceph_redirect_decode(&p, end, &m->redirect);
3411fe5da05eSIlya Dryomov 		if (ret)
3412fe5da05eSIlya Dryomov 			return ret;
3413205ee118SIlya Dryomov 	} else {
3414fe5da05eSIlya Dryomov 		ceph_oloc_init(&m->redirect.oloc);
3415205ee118SIlya Dryomov 	}
3416205ee118SIlya Dryomov 
3417fe5da05eSIlya Dryomov 	return 0;
3418205ee118SIlya Dryomov 
3419fe5da05eSIlya Dryomov e_inval:
3420fe5da05eSIlya Dryomov 	return -EINVAL;
3421fe5da05eSIlya Dryomov }
3422fe5da05eSIlya Dryomov 
3423fe5da05eSIlya Dryomov /*
3424b18b9550SIlya Dryomov  * Handle MOSDOpReply.  Set ->r_result and call the callback if it is
3425b18b9550SIlya Dryomov  * specified.
3426fe5da05eSIlya Dryomov  */
34275aea3dcdSIlya Dryomov static void handle_reply(struct ceph_osd *osd, struct ceph_msg *msg)
3428fe5da05eSIlya Dryomov {
34295aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
3430fe5da05eSIlya Dryomov 	struct ceph_osd_request *req;
3431fe5da05eSIlya Dryomov 	struct MOSDOpReply m;
3432fe5da05eSIlya Dryomov 	u64 tid = le64_to_cpu(msg->hdr.tid);
3433fe5da05eSIlya Dryomov 	u32 data_len = 0;
3434fe5da05eSIlya Dryomov 	int ret;
3435fe5da05eSIlya Dryomov 	int i;
3436fe5da05eSIlya Dryomov 
3437fe5da05eSIlya Dryomov 	dout("%s msg %p tid %llu\n", __func__, msg, tid);
3438fe5da05eSIlya Dryomov 
34395aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
34405aea3dcdSIlya Dryomov 	if (!osd_registered(osd)) {
34415aea3dcdSIlya Dryomov 		dout("%s osd%d unknown\n", __func__, osd->o_osd);
34425aea3dcdSIlya Dryomov 		goto out_unlock_osdc;
3443fe5da05eSIlya Dryomov 	}
34445aea3dcdSIlya Dryomov 	WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
34455aea3dcdSIlya Dryomov 
34465aea3dcdSIlya Dryomov 	mutex_lock(&osd->lock);
34475aea3dcdSIlya Dryomov 	req = lookup_request(&osd->o_requests, tid);
34485aea3dcdSIlya Dryomov 	if (!req) {
34495aea3dcdSIlya Dryomov 		dout("%s osd%d tid %llu unknown\n", __func__, osd->o_osd, tid);
34505aea3dcdSIlya Dryomov 		goto out_unlock_session;
34515aea3dcdSIlya Dryomov 	}
3452fe5da05eSIlya Dryomov 
3453cd08e0a2SYan, Zheng 	m.redirect.oloc.pool_ns = req->r_t.target_oloc.pool_ns;
3454fe5da05eSIlya Dryomov 	ret = decode_MOSDOpReply(msg, &m);
3455cd08e0a2SYan, Zheng 	m.redirect.oloc.pool_ns = NULL;
3456fe5da05eSIlya Dryomov 	if (ret) {
3457fe5da05eSIlya Dryomov 		pr_err("failed to decode MOSDOpReply for tid %llu: %d\n",
3458fe5da05eSIlya Dryomov 		       req->r_tid, ret);
3459fe5da05eSIlya Dryomov 		ceph_msg_dump(msg);
3460fe5da05eSIlya Dryomov 		goto fail_request;
3461fe5da05eSIlya Dryomov 	}
3462fe5da05eSIlya Dryomov 	dout("%s req %p tid %llu flags 0x%llx pgid %llu.%x epoch %u attempt %d v %u'%llu uv %llu\n",
3463fe5da05eSIlya Dryomov 	     __func__, req, req->r_tid, m.flags, m.pgid.pool, m.pgid.seed,
3464fe5da05eSIlya Dryomov 	     m.epoch, m.retry_attempt, le32_to_cpu(m.replay_version.epoch),
3465fe5da05eSIlya Dryomov 	     le64_to_cpu(m.replay_version.version), m.user_version);
3466fe5da05eSIlya Dryomov 
3467fe5da05eSIlya Dryomov 	if (m.retry_attempt >= 0) {
3468fe5da05eSIlya Dryomov 		if (m.retry_attempt != req->r_attempts - 1) {
3469fe5da05eSIlya Dryomov 			dout("req %p tid %llu retry_attempt %d != %d, ignoring\n",
3470fe5da05eSIlya Dryomov 			     req, req->r_tid, m.retry_attempt,
3471fe5da05eSIlya Dryomov 			     req->r_attempts - 1);
34725aea3dcdSIlya Dryomov 			goto out_unlock_session;
3473fe5da05eSIlya Dryomov 		}
3474fe5da05eSIlya Dryomov 	} else {
3475fe5da05eSIlya Dryomov 		WARN_ON(1); /* MOSDOpReply v4 is assumed */
3476fe5da05eSIlya Dryomov 	}
3477fe5da05eSIlya Dryomov 
3478fe5da05eSIlya Dryomov 	if (!ceph_oloc_empty(&m.redirect.oloc)) {
3479fe5da05eSIlya Dryomov 		dout("req %p tid %llu redirect pool %lld\n", req, req->r_tid,
3480fe5da05eSIlya Dryomov 		     m.redirect.oloc.pool);
34815aea3dcdSIlya Dryomov 		unlink_request(osd, req);
34825aea3dcdSIlya Dryomov 		mutex_unlock(&osd->lock);
3483205ee118SIlya Dryomov 
348430c156d9SYan, Zheng 		/*
348530c156d9SYan, Zheng 		 * Not ceph_oloc_copy() - changing pool_ns is not
348630c156d9SYan, Zheng 		 * supported.
348730c156d9SYan, Zheng 		 */
348830c156d9SYan, Zheng 		req->r_t.target_oloc.pool = m.redirect.oloc.pool;
34895aea3dcdSIlya Dryomov 		req->r_flags |= CEPH_OSD_FLAG_REDIRECTED;
34905aea3dcdSIlya Dryomov 		req->r_tid = 0;
34915aea3dcdSIlya Dryomov 		__submit_request(req, false);
34925aea3dcdSIlya Dryomov 		goto out_unlock_osdc;
3493205ee118SIlya Dryomov 	}
3494205ee118SIlya Dryomov 
3495fe5da05eSIlya Dryomov 	if (m.num_ops != req->r_num_ops) {
3496fe5da05eSIlya Dryomov 		pr_err("num_ops %d != %d for tid %llu\n", m.num_ops,
3497fe5da05eSIlya Dryomov 		       req->r_num_ops, req->r_tid);
3498fe5da05eSIlya Dryomov 		goto fail_request;
3499fe5da05eSIlya Dryomov 	}
3500fe5da05eSIlya Dryomov 	for (i = 0; i < req->r_num_ops; i++) {
3501fe5da05eSIlya Dryomov 		dout(" req %p tid %llu op %d rval %d len %u\n", req,
3502fe5da05eSIlya Dryomov 		     req->r_tid, i, m.rval[i], m.outdata_len[i]);
3503fe5da05eSIlya Dryomov 		req->r_ops[i].rval = m.rval[i];
3504fe5da05eSIlya Dryomov 		req->r_ops[i].outdata_len = m.outdata_len[i];
3505fe5da05eSIlya Dryomov 		data_len += m.outdata_len[i];
3506fe5da05eSIlya Dryomov 	}
3507fe5da05eSIlya Dryomov 	if (data_len != le32_to_cpu(msg->hdr.data_len)) {
3508fe5da05eSIlya Dryomov 		pr_err("sum of lens %u != %u for tid %llu\n", data_len,
3509fe5da05eSIlya Dryomov 		       le32_to_cpu(msg->hdr.data_len), req->r_tid);
3510fe5da05eSIlya Dryomov 		goto fail_request;
3511fe5da05eSIlya Dryomov 	}
3512b18b9550SIlya Dryomov 	dout("%s req %p tid %llu result %d data_len %u\n", __func__,
3513b18b9550SIlya Dryomov 	     req, req->r_tid, m.result, data_len);
35143d14c5d2SYehuda Sadeh 
3515b18b9550SIlya Dryomov 	/*
3516b18b9550SIlya Dryomov 	 * Since we only ever request ONDISK, we should only ever get
3517b18b9550SIlya Dryomov 	 * one (type of) reply back.
3518b18b9550SIlya Dryomov 	 */
3519b18b9550SIlya Dryomov 	WARN_ON(!(m.flags & CEPH_OSD_FLAG_ONDISK));
3520fe5da05eSIlya Dryomov 	req->r_result = m.result ?: data_len;
352145ee2c1dSIlya Dryomov 	finish_request(req);
35225aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
35235aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
35243d14c5d2SYehuda Sadeh 
3525fe5da05eSIlya Dryomov 	__complete_request(req);
3526b18b9550SIlya Dryomov 	complete_all(&req->r_completion);
3527dc045a91SIlya Dryomov 	ceph_osdc_put_request(req);
35283d14c5d2SYehuda Sadeh 	return;
3529fe5da05eSIlya Dryomov 
3530fe5da05eSIlya Dryomov fail_request:
35314609245eSIlya Dryomov 	complete_request(req, -EIO);
35325aea3dcdSIlya Dryomov out_unlock_session:
35335aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
35345aea3dcdSIlya Dryomov out_unlock_osdc:
35355aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
35363d14c5d2SYehuda Sadeh }
35373d14c5d2SYehuda Sadeh 
353842c1b124SIlya Dryomov static void set_pool_was_full(struct ceph_osd_client *osdc)
353942c1b124SIlya Dryomov {
354042c1b124SIlya Dryomov 	struct rb_node *n;
354142c1b124SIlya Dryomov 
354242c1b124SIlya Dryomov 	for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
354342c1b124SIlya Dryomov 		struct ceph_pg_pool_info *pi =
354442c1b124SIlya Dryomov 		    rb_entry(n, struct ceph_pg_pool_info, node);
354542c1b124SIlya Dryomov 
354642c1b124SIlya Dryomov 		pi->was_full = __pool_full(pi);
354742c1b124SIlya Dryomov 	}
354842c1b124SIlya Dryomov }
354942c1b124SIlya Dryomov 
35505aea3dcdSIlya Dryomov static bool pool_cleared_full(struct ceph_osd_client *osdc, s64 pool_id)
35513d14c5d2SYehuda Sadeh {
35525aea3dcdSIlya Dryomov 	struct ceph_pg_pool_info *pi;
35533d14c5d2SYehuda Sadeh 
35545aea3dcdSIlya Dryomov 	pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
35555aea3dcdSIlya Dryomov 	if (!pi)
35565aea3dcdSIlya Dryomov 		return false;
35573d14c5d2SYehuda Sadeh 
35585aea3dcdSIlya Dryomov 	return pi->was_full && !__pool_full(pi);
35593d14c5d2SYehuda Sadeh }
35603d14c5d2SYehuda Sadeh 
3561922dab61SIlya Dryomov static enum calc_target_result
3562922dab61SIlya Dryomov recalc_linger_target(struct ceph_osd_linger_request *lreq)
3563922dab61SIlya Dryomov {
3564922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
3565922dab61SIlya Dryomov 	enum calc_target_result ct_res;
3566922dab61SIlya Dryomov 
35677de030d6SIlya Dryomov 	ct_res = calc_target(osdc, &lreq->t, NULL, true);
3568922dab61SIlya Dryomov 	if (ct_res == CALC_TARGET_NEED_RESEND) {
3569922dab61SIlya Dryomov 		struct ceph_osd *osd;
3570922dab61SIlya Dryomov 
3571922dab61SIlya Dryomov 		osd = lookup_create_osd(osdc, lreq->t.osd, true);
3572922dab61SIlya Dryomov 		if (osd != lreq->osd) {
3573922dab61SIlya Dryomov 			unlink_linger(lreq->osd, lreq);
3574922dab61SIlya Dryomov 			link_linger(osd, lreq);
3575922dab61SIlya Dryomov 		}
3576922dab61SIlya Dryomov 	}
3577922dab61SIlya Dryomov 
3578922dab61SIlya Dryomov 	return ct_res;
3579922dab61SIlya Dryomov }
3580922dab61SIlya Dryomov 
35813d14c5d2SYehuda Sadeh /*
35825aea3dcdSIlya Dryomov  * Requeue requests whose mapping to an OSD has changed.
35833d14c5d2SYehuda Sadeh  */
35845aea3dcdSIlya Dryomov static void scan_requests(struct ceph_osd *osd,
35855aea3dcdSIlya Dryomov 			  bool force_resend,
35865aea3dcdSIlya Dryomov 			  bool cleared_full,
35875aea3dcdSIlya Dryomov 			  bool check_pool_cleared_full,
35885aea3dcdSIlya Dryomov 			  struct rb_root *need_resend,
35895aea3dcdSIlya Dryomov 			  struct list_head *need_resend_linger)
35903d14c5d2SYehuda Sadeh {
35915aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
35925aea3dcdSIlya Dryomov 	struct rb_node *n;
35935aea3dcdSIlya Dryomov 	bool force_resend_writes;
35943d14c5d2SYehuda Sadeh 
3595922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_linger_requests); n; ) {
3596922dab61SIlya Dryomov 		struct ceph_osd_linger_request *lreq =
3597922dab61SIlya Dryomov 		    rb_entry(n, struct ceph_osd_linger_request, node);
3598922dab61SIlya Dryomov 		enum calc_target_result ct_res;
3599922dab61SIlya Dryomov 
3600922dab61SIlya Dryomov 		n = rb_next(n); /* recalc_linger_target() */
3601922dab61SIlya Dryomov 
3602922dab61SIlya Dryomov 		dout("%s lreq %p linger_id %llu\n", __func__, lreq,
3603922dab61SIlya Dryomov 		     lreq->linger_id);
3604922dab61SIlya Dryomov 		ct_res = recalc_linger_target(lreq);
3605922dab61SIlya Dryomov 		switch (ct_res) {
3606922dab61SIlya Dryomov 		case CALC_TARGET_NO_ACTION:
3607922dab61SIlya Dryomov 			force_resend_writes = cleared_full ||
3608922dab61SIlya Dryomov 			    (check_pool_cleared_full &&
3609922dab61SIlya Dryomov 			     pool_cleared_full(osdc, lreq->t.base_oloc.pool));
3610922dab61SIlya Dryomov 			if (!force_resend && !force_resend_writes)
3611922dab61SIlya Dryomov 				break;
3612922dab61SIlya Dryomov 
3613922dab61SIlya Dryomov 			/* fall through */
3614922dab61SIlya Dryomov 		case CALC_TARGET_NEED_RESEND:
36154609245eSIlya Dryomov 			cancel_linger_map_check(lreq);
3616922dab61SIlya Dryomov 			/*
3617922dab61SIlya Dryomov 			 * scan_requests() for the previous epoch(s)
3618922dab61SIlya Dryomov 			 * may have already added it to the list, since
3619922dab61SIlya Dryomov 			 * it's not unlinked here.
3620922dab61SIlya Dryomov 			 */
3621922dab61SIlya Dryomov 			if (list_empty(&lreq->scan_item))
3622922dab61SIlya Dryomov 				list_add_tail(&lreq->scan_item, need_resend_linger);
3623922dab61SIlya Dryomov 			break;
3624922dab61SIlya Dryomov 		case CALC_TARGET_POOL_DNE:
3625a10bcb19SIlya Dryomov 			list_del_init(&lreq->scan_item);
36264609245eSIlya Dryomov 			check_linger_pool_dne(lreq);
3627922dab61SIlya Dryomov 			break;
3628922dab61SIlya Dryomov 		}
3629922dab61SIlya Dryomov 	}
3630922dab61SIlya Dryomov 
36315aea3dcdSIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; ) {
36325aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
36335aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
36345aea3dcdSIlya Dryomov 		enum calc_target_result ct_res;
3635ab60b16dSAlex Elder 
36364609245eSIlya Dryomov 		n = rb_next(n); /* unlink_request(), check_pool_dne() */
3637ab60b16dSAlex Elder 
36385aea3dcdSIlya Dryomov 		dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
36397de030d6SIlya Dryomov 		ct_res = calc_target(osdc, &req->r_t, &req->r_osd->o_con,
36407de030d6SIlya Dryomov 				     false);
36415aea3dcdSIlya Dryomov 		switch (ct_res) {
36425aea3dcdSIlya Dryomov 		case CALC_TARGET_NO_ACTION:
36435aea3dcdSIlya Dryomov 			force_resend_writes = cleared_full ||
36445aea3dcdSIlya Dryomov 			    (check_pool_cleared_full &&
36455aea3dcdSIlya Dryomov 			     pool_cleared_full(osdc, req->r_t.base_oloc.pool));
36465aea3dcdSIlya Dryomov 			if (!force_resend &&
36475aea3dcdSIlya Dryomov 			    (!(req->r_flags & CEPH_OSD_FLAG_WRITE) ||
36485aea3dcdSIlya Dryomov 			     !force_resend_writes))
36495aea3dcdSIlya Dryomov 				break;
3650a40c4f10SYehuda Sadeh 
36515aea3dcdSIlya Dryomov 			/* fall through */
36525aea3dcdSIlya Dryomov 		case CALC_TARGET_NEED_RESEND:
36534609245eSIlya Dryomov 			cancel_map_check(req);
36545aea3dcdSIlya Dryomov 			unlink_request(osd, req);
36555aea3dcdSIlya Dryomov 			insert_request(need_resend, req);
36565aea3dcdSIlya Dryomov 			break;
36575aea3dcdSIlya Dryomov 		case CALC_TARGET_POOL_DNE:
36584609245eSIlya Dryomov 			check_pool_dne(req);
36595aea3dcdSIlya Dryomov 			break;
3660a40c4f10SYehuda Sadeh 		}
36613d14c5d2SYehuda Sadeh 	}
36623d14c5d2SYehuda Sadeh }
36636f6c7006SSage Weil 
366442c1b124SIlya Dryomov static int handle_one_map(struct ceph_osd_client *osdc,
36655aea3dcdSIlya Dryomov 			  void *p, void *end, bool incremental,
36665aea3dcdSIlya Dryomov 			  struct rb_root *need_resend,
36675aea3dcdSIlya Dryomov 			  struct list_head *need_resend_linger)
366842c1b124SIlya Dryomov {
366942c1b124SIlya Dryomov 	struct ceph_osdmap *newmap;
367042c1b124SIlya Dryomov 	struct rb_node *n;
367142c1b124SIlya Dryomov 	bool skipped_map = false;
367242c1b124SIlya Dryomov 	bool was_full;
367342c1b124SIlya Dryomov 
3674b7ec35b3SIlya Dryomov 	was_full = ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
367542c1b124SIlya Dryomov 	set_pool_was_full(osdc);
367642c1b124SIlya Dryomov 
367742c1b124SIlya Dryomov 	if (incremental)
367842c1b124SIlya Dryomov 		newmap = osdmap_apply_incremental(&p, end, osdc->osdmap);
367942c1b124SIlya Dryomov 	else
368042c1b124SIlya Dryomov 		newmap = ceph_osdmap_decode(&p, end);
368142c1b124SIlya Dryomov 	if (IS_ERR(newmap))
368242c1b124SIlya Dryomov 		return PTR_ERR(newmap);
368342c1b124SIlya Dryomov 
368442c1b124SIlya Dryomov 	if (newmap != osdc->osdmap) {
368542c1b124SIlya Dryomov 		/*
368642c1b124SIlya Dryomov 		 * Preserve ->was_full before destroying the old map.
368742c1b124SIlya Dryomov 		 * For pools that weren't in the old map, ->was_full
368842c1b124SIlya Dryomov 		 * should be false.
368942c1b124SIlya Dryomov 		 */
369042c1b124SIlya Dryomov 		for (n = rb_first(&newmap->pg_pools); n; n = rb_next(n)) {
369142c1b124SIlya Dryomov 			struct ceph_pg_pool_info *pi =
369242c1b124SIlya Dryomov 			    rb_entry(n, struct ceph_pg_pool_info, node);
369342c1b124SIlya Dryomov 			struct ceph_pg_pool_info *old_pi;
369442c1b124SIlya Dryomov 
369542c1b124SIlya Dryomov 			old_pi = ceph_pg_pool_by_id(osdc->osdmap, pi->id);
369642c1b124SIlya Dryomov 			if (old_pi)
369742c1b124SIlya Dryomov 				pi->was_full = old_pi->was_full;
369842c1b124SIlya Dryomov 			else
369942c1b124SIlya Dryomov 				WARN_ON(pi->was_full);
370042c1b124SIlya Dryomov 		}
370142c1b124SIlya Dryomov 
370242c1b124SIlya Dryomov 		if (osdc->osdmap->epoch &&
370342c1b124SIlya Dryomov 		    osdc->osdmap->epoch + 1 < newmap->epoch) {
370442c1b124SIlya Dryomov 			WARN_ON(incremental);
370542c1b124SIlya Dryomov 			skipped_map = true;
370642c1b124SIlya Dryomov 		}
370742c1b124SIlya Dryomov 
370842c1b124SIlya Dryomov 		ceph_osdmap_destroy(osdc->osdmap);
370942c1b124SIlya Dryomov 		osdc->osdmap = newmap;
371042c1b124SIlya Dryomov 	}
371142c1b124SIlya Dryomov 
3712b7ec35b3SIlya Dryomov 	was_full &= !ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
37135aea3dcdSIlya Dryomov 	scan_requests(&osdc->homeless_osd, skipped_map, was_full, true,
37145aea3dcdSIlya Dryomov 		      need_resend, need_resend_linger);
37155aea3dcdSIlya Dryomov 
37165aea3dcdSIlya Dryomov 	for (n = rb_first(&osdc->osds); n; ) {
37175aea3dcdSIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
37185aea3dcdSIlya Dryomov 
37195aea3dcdSIlya Dryomov 		n = rb_next(n); /* close_osd() */
37205aea3dcdSIlya Dryomov 
37215aea3dcdSIlya Dryomov 		scan_requests(osd, skipped_map, was_full, true, need_resend,
37225aea3dcdSIlya Dryomov 			      need_resend_linger);
37235aea3dcdSIlya Dryomov 		if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
37245aea3dcdSIlya Dryomov 		    memcmp(&osd->o_con.peer_addr,
37255aea3dcdSIlya Dryomov 			   ceph_osd_addr(osdc->osdmap, osd->o_osd),
37265aea3dcdSIlya Dryomov 			   sizeof(struct ceph_entity_addr)))
37275aea3dcdSIlya Dryomov 			close_osd(osd);
37285aea3dcdSIlya Dryomov 	}
372942c1b124SIlya Dryomov 
373042c1b124SIlya Dryomov 	return 0;
373142c1b124SIlya Dryomov }
37326f6c7006SSage Weil 
37335aea3dcdSIlya Dryomov static void kick_requests(struct ceph_osd_client *osdc,
37345aea3dcdSIlya Dryomov 			  struct rb_root *need_resend,
37355aea3dcdSIlya Dryomov 			  struct list_head *need_resend_linger)
37365aea3dcdSIlya Dryomov {
3737922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq, *nlreq;
373804c7d789SIlya Dryomov 	enum calc_target_result ct_res;
37395aea3dcdSIlya Dryomov 	struct rb_node *n;
37405aea3dcdSIlya Dryomov 
374104c7d789SIlya Dryomov 	/* make sure need_resend targets reflect latest map */
374204c7d789SIlya Dryomov 	for (n = rb_first(need_resend); n; ) {
374304c7d789SIlya Dryomov 		struct ceph_osd_request *req =
374404c7d789SIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
374504c7d789SIlya Dryomov 
374604c7d789SIlya Dryomov 		n = rb_next(n);
374704c7d789SIlya Dryomov 
374804c7d789SIlya Dryomov 		if (req->r_t.epoch < osdc->osdmap->epoch) {
374904c7d789SIlya Dryomov 			ct_res = calc_target(osdc, &req->r_t, NULL, false);
375004c7d789SIlya Dryomov 			if (ct_res == CALC_TARGET_POOL_DNE) {
375104c7d789SIlya Dryomov 				erase_request(need_resend, req);
375204c7d789SIlya Dryomov 				check_pool_dne(req);
375304c7d789SIlya Dryomov 			}
375404c7d789SIlya Dryomov 		}
375504c7d789SIlya Dryomov 	}
375604c7d789SIlya Dryomov 
37575aea3dcdSIlya Dryomov 	for (n = rb_first(need_resend); n; ) {
37585aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
37595aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
37605aea3dcdSIlya Dryomov 		struct ceph_osd *osd;
37615aea3dcdSIlya Dryomov 
37625aea3dcdSIlya Dryomov 		n = rb_next(n);
37635aea3dcdSIlya Dryomov 		erase_request(need_resend, req); /* before link_request() */
37645aea3dcdSIlya Dryomov 
37655aea3dcdSIlya Dryomov 		osd = lookup_create_osd(osdc, req->r_t.osd, true);
37665aea3dcdSIlya Dryomov 		link_request(osd, req);
37675aea3dcdSIlya Dryomov 		if (!req->r_linger) {
37685aea3dcdSIlya Dryomov 			if (!osd_homeless(osd) && !req->r_t.paused)
37695aea3dcdSIlya Dryomov 				send_request(req);
3770922dab61SIlya Dryomov 		} else {
3771922dab61SIlya Dryomov 			cancel_linger_request(req);
37725aea3dcdSIlya Dryomov 		}
37735aea3dcdSIlya Dryomov 	}
3774922dab61SIlya Dryomov 
3775922dab61SIlya Dryomov 	list_for_each_entry_safe(lreq, nlreq, need_resend_linger, scan_item) {
3776922dab61SIlya Dryomov 		if (!osd_homeless(lreq->osd))
3777922dab61SIlya Dryomov 			send_linger(lreq);
3778922dab61SIlya Dryomov 
3779922dab61SIlya Dryomov 		list_del_init(&lreq->scan_item);
3780922dab61SIlya Dryomov 	}
37815aea3dcdSIlya Dryomov }
37825aea3dcdSIlya Dryomov 
37833d14c5d2SYehuda Sadeh /*
37843d14c5d2SYehuda Sadeh  * Process updated osd map.
37853d14c5d2SYehuda Sadeh  *
37863d14c5d2SYehuda Sadeh  * The message contains any number of incremental and full maps, normally
37873d14c5d2SYehuda Sadeh  * indicating some sort of topology change in the cluster.  Kick requests
37883d14c5d2SYehuda Sadeh  * off to different OSDs as needed.
37893d14c5d2SYehuda Sadeh  */
37903d14c5d2SYehuda Sadeh void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
37913d14c5d2SYehuda Sadeh {
379242c1b124SIlya Dryomov 	void *p = msg->front.iov_base;
379342c1b124SIlya Dryomov 	void *const end = p + msg->front.iov_len;
37943d14c5d2SYehuda Sadeh 	u32 nr_maps, maplen;
37953d14c5d2SYehuda Sadeh 	u32 epoch;
37963d14c5d2SYehuda Sadeh 	struct ceph_fsid fsid;
37975aea3dcdSIlya Dryomov 	struct rb_root need_resend = RB_ROOT;
37985aea3dcdSIlya Dryomov 	LIST_HEAD(need_resend_linger);
379942c1b124SIlya Dryomov 	bool handled_incremental = false;
380042c1b124SIlya Dryomov 	bool was_pauserd, was_pausewr;
380142c1b124SIlya Dryomov 	bool pauserd, pausewr;
380242c1b124SIlya Dryomov 	int err;
38033d14c5d2SYehuda Sadeh 
380442c1b124SIlya Dryomov 	dout("%s have %u\n", __func__, osdc->osdmap->epoch);
38055aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
38063d14c5d2SYehuda Sadeh 
38073d14c5d2SYehuda Sadeh 	/* verify fsid */
38083d14c5d2SYehuda Sadeh 	ceph_decode_need(&p, end, sizeof(fsid), bad);
38093d14c5d2SYehuda Sadeh 	ceph_decode_copy(&p, &fsid, sizeof(fsid));
38103d14c5d2SYehuda Sadeh 	if (ceph_check_fsid(osdc->client, &fsid) < 0)
381142c1b124SIlya Dryomov 		goto bad;
38123d14c5d2SYehuda Sadeh 
3813b7ec35b3SIlya Dryomov 	was_pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3814b7ec35b3SIlya Dryomov 	was_pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3815b7ec35b3SIlya Dryomov 		      ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
381642c1b124SIlya Dryomov 		      have_pool_full(osdc);
38179a1ea2dbSJosh Durgin 
38183d14c5d2SYehuda Sadeh 	/* incremental maps */
38193d14c5d2SYehuda Sadeh 	ceph_decode_32_safe(&p, end, nr_maps, bad);
38203d14c5d2SYehuda Sadeh 	dout(" %d inc maps\n", nr_maps);
38213d14c5d2SYehuda Sadeh 	while (nr_maps > 0) {
38223d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, 2*sizeof(u32), bad);
38233d14c5d2SYehuda Sadeh 		epoch = ceph_decode_32(&p);
38243d14c5d2SYehuda Sadeh 		maplen = ceph_decode_32(&p);
38253d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, maplen, bad);
382642c1b124SIlya Dryomov 		if (osdc->osdmap->epoch &&
382742c1b124SIlya Dryomov 		    osdc->osdmap->epoch + 1 == epoch) {
38283d14c5d2SYehuda Sadeh 			dout("applying incremental map %u len %d\n",
38293d14c5d2SYehuda Sadeh 			     epoch, maplen);
38305aea3dcdSIlya Dryomov 			err = handle_one_map(osdc, p, p + maplen, true,
38315aea3dcdSIlya Dryomov 					     &need_resend, &need_resend_linger);
383242c1b124SIlya Dryomov 			if (err)
38333d14c5d2SYehuda Sadeh 				goto bad;
383442c1b124SIlya Dryomov 			handled_incremental = true;
38353d14c5d2SYehuda Sadeh 		} else {
38363d14c5d2SYehuda Sadeh 			dout("ignoring incremental map %u len %d\n",
38373d14c5d2SYehuda Sadeh 			     epoch, maplen);
38383d14c5d2SYehuda Sadeh 		}
383942c1b124SIlya Dryomov 		p += maplen;
38403d14c5d2SYehuda Sadeh 		nr_maps--;
38413d14c5d2SYehuda Sadeh 	}
384242c1b124SIlya Dryomov 	if (handled_incremental)
38433d14c5d2SYehuda Sadeh 		goto done;
38443d14c5d2SYehuda Sadeh 
38453d14c5d2SYehuda Sadeh 	/* full maps */
38463d14c5d2SYehuda Sadeh 	ceph_decode_32_safe(&p, end, nr_maps, bad);
38473d14c5d2SYehuda Sadeh 	dout(" %d full maps\n", nr_maps);
38483d14c5d2SYehuda Sadeh 	while (nr_maps) {
38493d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, 2*sizeof(u32), bad);
38503d14c5d2SYehuda Sadeh 		epoch = ceph_decode_32(&p);
38513d14c5d2SYehuda Sadeh 		maplen = ceph_decode_32(&p);
38523d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, maplen, bad);
38533d14c5d2SYehuda Sadeh 		if (nr_maps > 1) {
38543d14c5d2SYehuda Sadeh 			dout("skipping non-latest full map %u len %d\n",
38553d14c5d2SYehuda Sadeh 			     epoch, maplen);
3856e5253a7bSIlya Dryomov 		} else if (osdc->osdmap->epoch >= epoch) {
38573d14c5d2SYehuda Sadeh 			dout("skipping full map %u len %d, "
38583d14c5d2SYehuda Sadeh 			     "older than our %u\n", epoch, maplen,
38593d14c5d2SYehuda Sadeh 			     osdc->osdmap->epoch);
38603d14c5d2SYehuda Sadeh 		} else {
38613d14c5d2SYehuda Sadeh 			dout("taking full map %u len %d\n", epoch, maplen);
38625aea3dcdSIlya Dryomov 			err = handle_one_map(osdc, p, p + maplen, false,
38635aea3dcdSIlya Dryomov 					     &need_resend, &need_resend_linger);
386442c1b124SIlya Dryomov 			if (err)
38653d14c5d2SYehuda Sadeh 				goto bad;
38663d14c5d2SYehuda Sadeh 		}
38673d14c5d2SYehuda Sadeh 		p += maplen;
38683d14c5d2SYehuda Sadeh 		nr_maps--;
38693d14c5d2SYehuda Sadeh 	}
38703d14c5d2SYehuda Sadeh 
38713d14c5d2SYehuda Sadeh done:
3872cd634fb6SSage Weil 	/*
3873cd634fb6SSage Weil 	 * subscribe to subsequent osdmap updates if full to ensure
3874cd634fb6SSage Weil 	 * we find out when we are no longer full and stop returning
3875cd634fb6SSage Weil 	 * ENOSPC.
3876cd634fb6SSage Weil 	 */
3877b7ec35b3SIlya Dryomov 	pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3878b7ec35b3SIlya Dryomov 	pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3879b7ec35b3SIlya Dryomov 		  ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
388042c1b124SIlya Dryomov 		  have_pool_full(osdc);
388158eb7932SJeff Layton 	if (was_pauserd || was_pausewr || pauserd || pausewr ||
388258eb7932SJeff Layton 	    osdc->osdmap->epoch < osdc->epoch_barrier)
388342c1b124SIlya Dryomov 		maybe_request_map(osdc);
3884cd634fb6SSage Weil 
38855aea3dcdSIlya Dryomov 	kick_requests(osdc, &need_resend, &need_resend_linger);
388642c1b124SIlya Dryomov 
3887fc36d0a4SJeff Layton 	ceph_osdc_abort_on_full(osdc);
388842c1b124SIlya Dryomov 	ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
388942c1b124SIlya Dryomov 			  osdc->osdmap->epoch);
38905aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
38913d14c5d2SYehuda Sadeh 	wake_up_all(&osdc->client->auth_wq);
38923d14c5d2SYehuda Sadeh 	return;
38933d14c5d2SYehuda Sadeh 
38943d14c5d2SYehuda Sadeh bad:
38953d14c5d2SYehuda Sadeh 	pr_err("osdc handle_map corrupt msg\n");
38963d14c5d2SYehuda Sadeh 	ceph_msg_dump(msg);
38975aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
38985aea3dcdSIlya Dryomov }
38995aea3dcdSIlya Dryomov 
39005aea3dcdSIlya Dryomov /*
39015aea3dcdSIlya Dryomov  * Resubmit requests pending on the given osd.
39025aea3dcdSIlya Dryomov  */
39035aea3dcdSIlya Dryomov static void kick_osd_requests(struct ceph_osd *osd)
39045aea3dcdSIlya Dryomov {
39055aea3dcdSIlya Dryomov 	struct rb_node *n;
39065aea3dcdSIlya Dryomov 
3907a02a946dSIlya Dryomov 	clear_backoffs(osd);
3908a02a946dSIlya Dryomov 
3909922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; ) {
39105aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
39115aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
39125aea3dcdSIlya Dryomov 
3913922dab61SIlya Dryomov 		n = rb_next(n); /* cancel_linger_request() */
3914922dab61SIlya Dryomov 
39155aea3dcdSIlya Dryomov 		if (!req->r_linger) {
39165aea3dcdSIlya Dryomov 			if (!req->r_t.paused)
39175aea3dcdSIlya Dryomov 				send_request(req);
3918922dab61SIlya Dryomov 		} else {
3919922dab61SIlya Dryomov 			cancel_linger_request(req);
39205aea3dcdSIlya Dryomov 		}
39215aea3dcdSIlya Dryomov 	}
3922922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_linger_requests); n; n = rb_next(n)) {
3923922dab61SIlya Dryomov 		struct ceph_osd_linger_request *lreq =
3924922dab61SIlya Dryomov 		    rb_entry(n, struct ceph_osd_linger_request, node);
3925922dab61SIlya Dryomov 
3926922dab61SIlya Dryomov 		send_linger(lreq);
3927922dab61SIlya Dryomov 	}
39285aea3dcdSIlya Dryomov }
39295aea3dcdSIlya Dryomov 
39305aea3dcdSIlya Dryomov /*
39315aea3dcdSIlya Dryomov  * If the osd connection drops, we need to resubmit all requests.
39325aea3dcdSIlya Dryomov  */
39335aea3dcdSIlya Dryomov static void osd_fault(struct ceph_connection *con)
39345aea3dcdSIlya Dryomov {
39355aea3dcdSIlya Dryomov 	struct ceph_osd *osd = con->private;
39365aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
39375aea3dcdSIlya Dryomov 
39385aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
39395aea3dcdSIlya Dryomov 
39405aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
39415aea3dcdSIlya Dryomov 	if (!osd_registered(osd)) {
39425aea3dcdSIlya Dryomov 		dout("%s osd%d unknown\n", __func__, osd->o_osd);
39435aea3dcdSIlya Dryomov 		goto out_unlock;
39445aea3dcdSIlya Dryomov 	}
39455aea3dcdSIlya Dryomov 
39465aea3dcdSIlya Dryomov 	if (!reopen_osd(osd))
39475aea3dcdSIlya Dryomov 		kick_osd_requests(osd);
39485aea3dcdSIlya Dryomov 	maybe_request_map(osdc);
39495aea3dcdSIlya Dryomov 
39505aea3dcdSIlya Dryomov out_unlock:
39515aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
39523d14c5d2SYehuda Sadeh }
39533d14c5d2SYehuda Sadeh 
3954a02a946dSIlya Dryomov struct MOSDBackoff {
3955a02a946dSIlya Dryomov 	struct ceph_spg spgid;
3956a02a946dSIlya Dryomov 	u32 map_epoch;
3957a02a946dSIlya Dryomov 	u8 op;
3958a02a946dSIlya Dryomov 	u64 id;
3959a02a946dSIlya Dryomov 	struct ceph_hobject_id *begin;
3960a02a946dSIlya Dryomov 	struct ceph_hobject_id *end;
3961a02a946dSIlya Dryomov };
3962a02a946dSIlya Dryomov 
3963a02a946dSIlya Dryomov static int decode_MOSDBackoff(const struct ceph_msg *msg, struct MOSDBackoff *m)
3964a02a946dSIlya Dryomov {
3965a02a946dSIlya Dryomov 	void *p = msg->front.iov_base;
3966a02a946dSIlya Dryomov 	void *const end = p + msg->front.iov_len;
3967a02a946dSIlya Dryomov 	u8 struct_v;
3968a02a946dSIlya Dryomov 	u32 struct_len;
3969a02a946dSIlya Dryomov 	int ret;
3970a02a946dSIlya Dryomov 
3971a02a946dSIlya Dryomov 	ret = ceph_start_decoding(&p, end, 1, "spg_t", &struct_v, &struct_len);
3972a02a946dSIlya Dryomov 	if (ret)
3973a02a946dSIlya Dryomov 		return ret;
3974a02a946dSIlya Dryomov 
3975a02a946dSIlya Dryomov 	ret = ceph_decode_pgid(&p, end, &m->spgid.pgid);
3976a02a946dSIlya Dryomov 	if (ret)
3977a02a946dSIlya Dryomov 		return ret;
3978a02a946dSIlya Dryomov 
3979a02a946dSIlya Dryomov 	ceph_decode_8_safe(&p, end, m->spgid.shard, e_inval);
3980a02a946dSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->map_epoch, e_inval);
3981a02a946dSIlya Dryomov 	ceph_decode_8_safe(&p, end, m->op, e_inval);
3982a02a946dSIlya Dryomov 	ceph_decode_64_safe(&p, end, m->id, e_inval);
3983a02a946dSIlya Dryomov 
3984a02a946dSIlya Dryomov 	m->begin = kzalloc(sizeof(*m->begin), GFP_NOIO);
3985a02a946dSIlya Dryomov 	if (!m->begin)
3986a02a946dSIlya Dryomov 		return -ENOMEM;
3987a02a946dSIlya Dryomov 
3988a02a946dSIlya Dryomov 	ret = decode_hoid(&p, end, m->begin);
3989a02a946dSIlya Dryomov 	if (ret) {
3990a02a946dSIlya Dryomov 		free_hoid(m->begin);
3991a02a946dSIlya Dryomov 		return ret;
3992a02a946dSIlya Dryomov 	}
3993a02a946dSIlya Dryomov 
3994a02a946dSIlya Dryomov 	m->end = kzalloc(sizeof(*m->end), GFP_NOIO);
3995a02a946dSIlya Dryomov 	if (!m->end) {
3996a02a946dSIlya Dryomov 		free_hoid(m->begin);
3997a02a946dSIlya Dryomov 		return -ENOMEM;
3998a02a946dSIlya Dryomov 	}
3999a02a946dSIlya Dryomov 
4000a02a946dSIlya Dryomov 	ret = decode_hoid(&p, end, m->end);
4001a02a946dSIlya Dryomov 	if (ret) {
4002a02a946dSIlya Dryomov 		free_hoid(m->begin);
4003a02a946dSIlya Dryomov 		free_hoid(m->end);
4004a02a946dSIlya Dryomov 		return ret;
4005a02a946dSIlya Dryomov 	}
4006a02a946dSIlya Dryomov 
4007a02a946dSIlya Dryomov 	return 0;
4008a02a946dSIlya Dryomov 
4009a02a946dSIlya Dryomov e_inval:
4010a02a946dSIlya Dryomov 	return -EINVAL;
4011a02a946dSIlya Dryomov }
4012a02a946dSIlya Dryomov 
4013a02a946dSIlya Dryomov static struct ceph_msg *create_backoff_message(
4014a02a946dSIlya Dryomov 				const struct ceph_osd_backoff *backoff,
4015a02a946dSIlya Dryomov 				u32 map_epoch)
4016a02a946dSIlya Dryomov {
4017a02a946dSIlya Dryomov 	struct ceph_msg *msg;
4018a02a946dSIlya Dryomov 	void *p, *end;
4019a02a946dSIlya Dryomov 	int msg_size;
4020a02a946dSIlya Dryomov 
4021a02a946dSIlya Dryomov 	msg_size = CEPH_ENCODING_START_BLK_LEN +
4022a02a946dSIlya Dryomov 			CEPH_PGID_ENCODING_LEN + 1; /* spgid */
4023a02a946dSIlya Dryomov 	msg_size += 4 + 1 + 8; /* map_epoch, op, id */
4024a02a946dSIlya Dryomov 	msg_size += CEPH_ENCODING_START_BLK_LEN +
4025a02a946dSIlya Dryomov 			hoid_encoding_size(backoff->begin);
4026a02a946dSIlya Dryomov 	msg_size += CEPH_ENCODING_START_BLK_LEN +
4027a02a946dSIlya Dryomov 			hoid_encoding_size(backoff->end);
4028a02a946dSIlya Dryomov 
4029a02a946dSIlya Dryomov 	msg = ceph_msg_new(CEPH_MSG_OSD_BACKOFF, msg_size, GFP_NOIO, true);
4030a02a946dSIlya Dryomov 	if (!msg)
4031a02a946dSIlya Dryomov 		return NULL;
4032a02a946dSIlya Dryomov 
4033a02a946dSIlya Dryomov 	p = msg->front.iov_base;
4034a02a946dSIlya Dryomov 	end = p + msg->front_alloc_len;
4035a02a946dSIlya Dryomov 
4036a02a946dSIlya Dryomov 	encode_spgid(&p, &backoff->spgid);
4037a02a946dSIlya Dryomov 	ceph_encode_32(&p, map_epoch);
4038a02a946dSIlya Dryomov 	ceph_encode_8(&p, CEPH_OSD_BACKOFF_OP_ACK_BLOCK);
4039a02a946dSIlya Dryomov 	ceph_encode_64(&p, backoff->id);
4040a02a946dSIlya Dryomov 	encode_hoid(&p, end, backoff->begin);
4041a02a946dSIlya Dryomov 	encode_hoid(&p, end, backoff->end);
4042a02a946dSIlya Dryomov 	BUG_ON(p != end);
4043a02a946dSIlya Dryomov 
4044a02a946dSIlya Dryomov 	msg->front.iov_len = p - msg->front.iov_base;
4045a02a946dSIlya Dryomov 	msg->hdr.version = cpu_to_le16(1); /* MOSDBackoff v1 */
4046a02a946dSIlya Dryomov 	msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
4047a02a946dSIlya Dryomov 
4048a02a946dSIlya Dryomov 	return msg;
4049a02a946dSIlya Dryomov }
4050a02a946dSIlya Dryomov 
4051a02a946dSIlya Dryomov static void handle_backoff_block(struct ceph_osd *osd, struct MOSDBackoff *m)
4052a02a946dSIlya Dryomov {
4053a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
4054a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
4055a02a946dSIlya Dryomov 	struct ceph_msg *msg;
4056a02a946dSIlya Dryomov 
4057a02a946dSIlya Dryomov 	dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4058a02a946dSIlya Dryomov 	     m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4059a02a946dSIlya Dryomov 
4060a02a946dSIlya Dryomov 	spg = lookup_spg_mapping(&osd->o_backoff_mappings, &m->spgid);
4061a02a946dSIlya Dryomov 	if (!spg) {
4062a02a946dSIlya Dryomov 		spg = alloc_spg_mapping();
4063a02a946dSIlya Dryomov 		if (!spg) {
4064a02a946dSIlya Dryomov 			pr_err("%s failed to allocate spg\n", __func__);
4065a02a946dSIlya Dryomov 			return;
4066a02a946dSIlya Dryomov 		}
4067a02a946dSIlya Dryomov 		spg->spgid = m->spgid; /* struct */
4068a02a946dSIlya Dryomov 		insert_spg_mapping(&osd->o_backoff_mappings, spg);
4069a02a946dSIlya Dryomov 	}
4070a02a946dSIlya Dryomov 
4071a02a946dSIlya Dryomov 	backoff = alloc_backoff();
4072a02a946dSIlya Dryomov 	if (!backoff) {
4073a02a946dSIlya Dryomov 		pr_err("%s failed to allocate backoff\n", __func__);
4074a02a946dSIlya Dryomov 		return;
4075a02a946dSIlya Dryomov 	}
4076a02a946dSIlya Dryomov 	backoff->spgid = m->spgid; /* struct */
4077a02a946dSIlya Dryomov 	backoff->id = m->id;
4078a02a946dSIlya Dryomov 	backoff->begin = m->begin;
4079a02a946dSIlya Dryomov 	m->begin = NULL; /* backoff now owns this */
4080a02a946dSIlya Dryomov 	backoff->end = m->end;
4081a02a946dSIlya Dryomov 	m->end = NULL;   /* ditto */
4082a02a946dSIlya Dryomov 
4083a02a946dSIlya Dryomov 	insert_backoff(&spg->backoffs, backoff);
4084a02a946dSIlya Dryomov 	insert_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4085a02a946dSIlya Dryomov 
4086a02a946dSIlya Dryomov 	/*
4087a02a946dSIlya Dryomov 	 * Ack with original backoff's epoch so that the OSD can
4088a02a946dSIlya Dryomov 	 * discard this if there was a PG split.
4089a02a946dSIlya Dryomov 	 */
4090a02a946dSIlya Dryomov 	msg = create_backoff_message(backoff, m->map_epoch);
4091a02a946dSIlya Dryomov 	if (!msg) {
4092a02a946dSIlya Dryomov 		pr_err("%s failed to allocate msg\n", __func__);
4093a02a946dSIlya Dryomov 		return;
4094a02a946dSIlya Dryomov 	}
4095a02a946dSIlya Dryomov 	ceph_con_send(&osd->o_con, msg);
4096a02a946dSIlya Dryomov }
4097a02a946dSIlya Dryomov 
4098a02a946dSIlya Dryomov static bool target_contained_by(const struct ceph_osd_request_target *t,
4099a02a946dSIlya Dryomov 				const struct ceph_hobject_id *begin,
4100a02a946dSIlya Dryomov 				const struct ceph_hobject_id *end)
4101a02a946dSIlya Dryomov {
4102a02a946dSIlya Dryomov 	struct ceph_hobject_id hoid;
4103a02a946dSIlya Dryomov 	int cmp;
4104a02a946dSIlya Dryomov 
4105a02a946dSIlya Dryomov 	hoid_fill_from_target(&hoid, t);
4106a02a946dSIlya Dryomov 	cmp = hoid_compare(&hoid, begin);
4107a02a946dSIlya Dryomov 	return !cmp || (cmp > 0 && hoid_compare(&hoid, end) < 0);
4108a02a946dSIlya Dryomov }
4109a02a946dSIlya Dryomov 
4110a02a946dSIlya Dryomov static void handle_backoff_unblock(struct ceph_osd *osd,
4111a02a946dSIlya Dryomov 				   const struct MOSDBackoff *m)
4112a02a946dSIlya Dryomov {
4113a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
4114a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
4115a02a946dSIlya Dryomov 	struct rb_node *n;
4116a02a946dSIlya Dryomov 
4117a02a946dSIlya Dryomov 	dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4118a02a946dSIlya Dryomov 	     m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4119a02a946dSIlya Dryomov 
4120a02a946dSIlya Dryomov 	backoff = lookup_backoff_by_id(&osd->o_backoffs_by_id, m->id);
4121a02a946dSIlya Dryomov 	if (!backoff) {
4122a02a946dSIlya Dryomov 		pr_err("%s osd%d spgid %llu.%xs%d id %llu backoff dne\n",
4123a02a946dSIlya Dryomov 		       __func__, osd->o_osd, m->spgid.pgid.pool,
4124a02a946dSIlya Dryomov 		       m->spgid.pgid.seed, m->spgid.shard, m->id);
4125a02a946dSIlya Dryomov 		return;
4126a02a946dSIlya Dryomov 	}
4127a02a946dSIlya Dryomov 
4128a02a946dSIlya Dryomov 	if (hoid_compare(backoff->begin, m->begin) &&
4129a02a946dSIlya Dryomov 	    hoid_compare(backoff->end, m->end)) {
4130a02a946dSIlya Dryomov 		pr_err("%s osd%d spgid %llu.%xs%d id %llu bad range?\n",
4131a02a946dSIlya Dryomov 		       __func__, osd->o_osd, m->spgid.pgid.pool,
4132a02a946dSIlya Dryomov 		       m->spgid.pgid.seed, m->spgid.shard, m->id);
4133a02a946dSIlya Dryomov 		/* unblock it anyway... */
4134a02a946dSIlya Dryomov 	}
4135a02a946dSIlya Dryomov 
4136a02a946dSIlya Dryomov 	spg = lookup_spg_mapping(&osd->o_backoff_mappings, &backoff->spgid);
4137a02a946dSIlya Dryomov 	BUG_ON(!spg);
4138a02a946dSIlya Dryomov 
4139a02a946dSIlya Dryomov 	erase_backoff(&spg->backoffs, backoff);
4140a02a946dSIlya Dryomov 	erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4141a02a946dSIlya Dryomov 	free_backoff(backoff);
4142a02a946dSIlya Dryomov 
4143a02a946dSIlya Dryomov 	if (RB_EMPTY_ROOT(&spg->backoffs)) {
4144a02a946dSIlya Dryomov 		erase_spg_mapping(&osd->o_backoff_mappings, spg);
4145a02a946dSIlya Dryomov 		free_spg_mapping(spg);
4146a02a946dSIlya Dryomov 	}
4147a02a946dSIlya Dryomov 
4148a02a946dSIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
4149a02a946dSIlya Dryomov 		struct ceph_osd_request *req =
4150a02a946dSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
4151a02a946dSIlya Dryomov 
4152a02a946dSIlya Dryomov 		if (!ceph_spg_compare(&req->r_t.spgid, &m->spgid)) {
4153a02a946dSIlya Dryomov 			/*
4154a02a946dSIlya Dryomov 			 * Match against @m, not @backoff -- the PG may
4155a02a946dSIlya Dryomov 			 * have split on the OSD.
4156a02a946dSIlya Dryomov 			 */
4157a02a946dSIlya Dryomov 			if (target_contained_by(&req->r_t, m->begin, m->end)) {
4158a02a946dSIlya Dryomov 				/*
4159a02a946dSIlya Dryomov 				 * If no other installed backoff applies,
4160a02a946dSIlya Dryomov 				 * resend.
4161a02a946dSIlya Dryomov 				 */
4162a02a946dSIlya Dryomov 				send_request(req);
4163a02a946dSIlya Dryomov 			}
4164a02a946dSIlya Dryomov 		}
4165a02a946dSIlya Dryomov 	}
4166a02a946dSIlya Dryomov }
4167a02a946dSIlya Dryomov 
4168a02a946dSIlya Dryomov static void handle_backoff(struct ceph_osd *osd, struct ceph_msg *msg)
4169a02a946dSIlya Dryomov {
4170a02a946dSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
4171a02a946dSIlya Dryomov 	struct MOSDBackoff m;
4172a02a946dSIlya Dryomov 	int ret;
4173a02a946dSIlya Dryomov 
4174a02a946dSIlya Dryomov 	down_read(&osdc->lock);
4175a02a946dSIlya Dryomov 	if (!osd_registered(osd)) {
4176a02a946dSIlya Dryomov 		dout("%s osd%d unknown\n", __func__, osd->o_osd);
4177a02a946dSIlya Dryomov 		up_read(&osdc->lock);
4178a02a946dSIlya Dryomov 		return;
4179a02a946dSIlya Dryomov 	}
4180a02a946dSIlya Dryomov 	WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
4181a02a946dSIlya Dryomov 
4182a02a946dSIlya Dryomov 	mutex_lock(&osd->lock);
4183a02a946dSIlya Dryomov 	ret = decode_MOSDBackoff(msg, &m);
4184a02a946dSIlya Dryomov 	if (ret) {
4185a02a946dSIlya Dryomov 		pr_err("failed to decode MOSDBackoff: %d\n", ret);
4186a02a946dSIlya Dryomov 		ceph_msg_dump(msg);
4187a02a946dSIlya Dryomov 		goto out_unlock;
4188a02a946dSIlya Dryomov 	}
4189a02a946dSIlya Dryomov 
4190a02a946dSIlya Dryomov 	switch (m.op) {
4191a02a946dSIlya Dryomov 	case CEPH_OSD_BACKOFF_OP_BLOCK:
4192a02a946dSIlya Dryomov 		handle_backoff_block(osd, &m);
4193a02a946dSIlya Dryomov 		break;
4194a02a946dSIlya Dryomov 	case CEPH_OSD_BACKOFF_OP_UNBLOCK:
4195a02a946dSIlya Dryomov 		handle_backoff_unblock(osd, &m);
4196a02a946dSIlya Dryomov 		break;
4197a02a946dSIlya Dryomov 	default:
4198a02a946dSIlya Dryomov 		pr_err("%s osd%d unknown op %d\n", __func__, osd->o_osd, m.op);
4199a02a946dSIlya Dryomov 	}
4200a02a946dSIlya Dryomov 
4201a02a946dSIlya Dryomov 	free_hoid(m.begin);
4202a02a946dSIlya Dryomov 	free_hoid(m.end);
4203a02a946dSIlya Dryomov 
4204a02a946dSIlya Dryomov out_unlock:
4205a02a946dSIlya Dryomov 	mutex_unlock(&osd->lock);
4206a02a946dSIlya Dryomov 	up_read(&osdc->lock);
4207a02a946dSIlya Dryomov }
4208a02a946dSIlya Dryomov 
42093d14c5d2SYehuda Sadeh /*
4210a40c4f10SYehuda Sadeh  * Process osd watch notifications
4211a40c4f10SYehuda Sadeh  */
42123c663bbdSAlex Elder static void handle_watch_notify(struct ceph_osd_client *osdc,
42133c663bbdSAlex Elder 				struct ceph_msg *msg)
4214a40c4f10SYehuda Sadeh {
4215922dab61SIlya Dryomov 	void *p = msg->front.iov_base;
4216922dab61SIlya Dryomov 	void *const end = p + msg->front.iov_len;
4217922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
4218922dab61SIlya Dryomov 	struct linger_work *lwork;
4219922dab61SIlya Dryomov 	u8 proto_ver, opcode;
4220922dab61SIlya Dryomov 	u64 cookie, notify_id;
4221922dab61SIlya Dryomov 	u64 notifier_id = 0;
422219079203SIlya Dryomov 	s32 return_code = 0;
4223922dab61SIlya Dryomov 	void *payload = NULL;
4224922dab61SIlya Dryomov 	u32 payload_len = 0;
4225a40c4f10SYehuda Sadeh 
4226a40c4f10SYehuda Sadeh 	ceph_decode_8_safe(&p, end, proto_ver, bad);
4227a40c4f10SYehuda Sadeh 	ceph_decode_8_safe(&p, end, opcode, bad);
4228a40c4f10SYehuda Sadeh 	ceph_decode_64_safe(&p, end, cookie, bad);
4229922dab61SIlya Dryomov 	p += 8; /* skip ver */
4230a40c4f10SYehuda Sadeh 	ceph_decode_64_safe(&p, end, notify_id, bad);
4231a40c4f10SYehuda Sadeh 
4232922dab61SIlya Dryomov 	if (proto_ver >= 1) {
4233922dab61SIlya Dryomov 		ceph_decode_32_safe(&p, end, payload_len, bad);
4234922dab61SIlya Dryomov 		ceph_decode_need(&p, end, payload_len, bad);
4235922dab61SIlya Dryomov 		payload = p;
4236922dab61SIlya Dryomov 		p += payload_len;
4237a40c4f10SYehuda Sadeh 	}
4238a40c4f10SYehuda Sadeh 
4239922dab61SIlya Dryomov 	if (le16_to_cpu(msg->hdr.version) >= 2)
424019079203SIlya Dryomov 		ceph_decode_32_safe(&p, end, return_code, bad);
4241922dab61SIlya Dryomov 
4242922dab61SIlya Dryomov 	if (le16_to_cpu(msg->hdr.version) >= 3)
4243922dab61SIlya Dryomov 		ceph_decode_64_safe(&p, end, notifier_id, bad);
4244922dab61SIlya Dryomov 
4245922dab61SIlya Dryomov 	down_read(&osdc->lock);
4246922dab61SIlya Dryomov 	lreq = lookup_linger_osdc(&osdc->linger_requests, cookie);
4247922dab61SIlya Dryomov 	if (!lreq) {
4248922dab61SIlya Dryomov 		dout("%s opcode %d cookie %llu dne\n", __func__, opcode,
4249922dab61SIlya Dryomov 		     cookie);
4250922dab61SIlya Dryomov 		goto out_unlock_osdc;
4251922dab61SIlya Dryomov 	}
4252922dab61SIlya Dryomov 
4253922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
425419079203SIlya Dryomov 	dout("%s opcode %d cookie %llu lreq %p is_watch %d\n", __func__,
425519079203SIlya Dryomov 	     opcode, cookie, lreq, lreq->is_watch);
4256922dab61SIlya Dryomov 	if (opcode == CEPH_WATCH_EVENT_DISCONNECT) {
4257922dab61SIlya Dryomov 		if (!lreq->last_error) {
4258922dab61SIlya Dryomov 			lreq->last_error = -ENOTCONN;
4259922dab61SIlya Dryomov 			queue_watch_error(lreq);
4260922dab61SIlya Dryomov 		}
426119079203SIlya Dryomov 	} else if (!lreq->is_watch) {
426219079203SIlya Dryomov 		/* CEPH_WATCH_EVENT_NOTIFY_COMPLETE */
426319079203SIlya Dryomov 		if (lreq->notify_id && lreq->notify_id != notify_id) {
426419079203SIlya Dryomov 			dout("lreq %p notify_id %llu != %llu, ignoring\n", lreq,
426519079203SIlya Dryomov 			     lreq->notify_id, notify_id);
426619079203SIlya Dryomov 		} else if (!completion_done(&lreq->notify_finish_wait)) {
426719079203SIlya Dryomov 			struct ceph_msg_data *data =
426819079203SIlya Dryomov 			    list_first_entry_or_null(&msg->data,
426919079203SIlya Dryomov 						     struct ceph_msg_data,
427019079203SIlya Dryomov 						     links);
427119079203SIlya Dryomov 
427219079203SIlya Dryomov 			if (data) {
427319079203SIlya Dryomov 				if (lreq->preply_pages) {
427419079203SIlya Dryomov 					WARN_ON(data->type !=
427519079203SIlya Dryomov 							CEPH_MSG_DATA_PAGES);
427619079203SIlya Dryomov 					*lreq->preply_pages = data->pages;
427719079203SIlya Dryomov 					*lreq->preply_len = data->length;
427819079203SIlya Dryomov 				} else {
427919079203SIlya Dryomov 					ceph_release_page_vector(data->pages,
428019079203SIlya Dryomov 					       calc_pages_for(0, data->length));
428119079203SIlya Dryomov 				}
428219079203SIlya Dryomov 			}
428319079203SIlya Dryomov 			lreq->notify_finish_error = return_code;
428419079203SIlya Dryomov 			complete_all(&lreq->notify_finish_wait);
428519079203SIlya Dryomov 		}
4286922dab61SIlya Dryomov 	} else {
4287922dab61SIlya Dryomov 		/* CEPH_WATCH_EVENT_NOTIFY */
4288922dab61SIlya Dryomov 		lwork = lwork_alloc(lreq, do_watch_notify);
4289922dab61SIlya Dryomov 		if (!lwork) {
4290922dab61SIlya Dryomov 			pr_err("failed to allocate notify-lwork\n");
4291922dab61SIlya Dryomov 			goto out_unlock_lreq;
4292922dab61SIlya Dryomov 		}
4293922dab61SIlya Dryomov 
4294922dab61SIlya Dryomov 		lwork->notify.notify_id = notify_id;
4295922dab61SIlya Dryomov 		lwork->notify.notifier_id = notifier_id;
4296922dab61SIlya Dryomov 		lwork->notify.payload = payload;
4297922dab61SIlya Dryomov 		lwork->notify.payload_len = payload_len;
4298922dab61SIlya Dryomov 		lwork->notify.msg = ceph_msg_get(msg);
4299922dab61SIlya Dryomov 		lwork_queue(lwork);
4300922dab61SIlya Dryomov 	}
4301922dab61SIlya Dryomov 
4302922dab61SIlya Dryomov out_unlock_lreq:
4303922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
4304922dab61SIlya Dryomov out_unlock_osdc:
4305922dab61SIlya Dryomov 	up_read(&osdc->lock);
4306a40c4f10SYehuda Sadeh 	return;
4307a40c4f10SYehuda Sadeh 
4308a40c4f10SYehuda Sadeh bad:
4309a40c4f10SYehuda Sadeh 	pr_err("osdc handle_watch_notify corrupt msg\n");
4310a40c4f10SYehuda Sadeh }
4311a40c4f10SYehuda Sadeh 
4312a40c4f10SYehuda Sadeh /*
43133d14c5d2SYehuda Sadeh  * Register request, send initial attempt.
43143d14c5d2SYehuda Sadeh  */
43153d14c5d2SYehuda Sadeh int ceph_osdc_start_request(struct ceph_osd_client *osdc,
43163d14c5d2SYehuda Sadeh 			    struct ceph_osd_request *req,
43173d14c5d2SYehuda Sadeh 			    bool nofail)
43183d14c5d2SYehuda Sadeh {
43195aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
43205aea3dcdSIlya Dryomov 	submit_request(req, false);
43215aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
43223d14c5d2SYehuda Sadeh 
43235aea3dcdSIlya Dryomov 	return 0;
43243d14c5d2SYehuda Sadeh }
43253d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_start_request);
43263d14c5d2SYehuda Sadeh 
43273d14c5d2SYehuda Sadeh /*
4328c297eb42SIlya Dryomov  * Unregister a registered request.  The request is not completed:
4329c297eb42SIlya Dryomov  * ->r_result isn't set and __complete_request() isn't called.
4330c9f9b93dSIlya Dryomov  */
4331c9f9b93dSIlya Dryomov void ceph_osdc_cancel_request(struct ceph_osd_request *req)
4332c9f9b93dSIlya Dryomov {
4333c9f9b93dSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
4334c9f9b93dSIlya Dryomov 
43355aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
43365aea3dcdSIlya Dryomov 	if (req->r_osd)
43375aea3dcdSIlya Dryomov 		cancel_request(req);
43385aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
4339c9f9b93dSIlya Dryomov }
4340c9f9b93dSIlya Dryomov EXPORT_SYMBOL(ceph_osdc_cancel_request);
4341c9f9b93dSIlya Dryomov 
4342c9f9b93dSIlya Dryomov /*
434342b06965SIlya Dryomov  * @timeout: in jiffies, 0 means "wait forever"
434442b06965SIlya Dryomov  */
434542b06965SIlya Dryomov static int wait_request_timeout(struct ceph_osd_request *req,
434642b06965SIlya Dryomov 				unsigned long timeout)
434742b06965SIlya Dryomov {
434842b06965SIlya Dryomov 	long left;
434942b06965SIlya Dryomov 
435042b06965SIlya Dryomov 	dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
43510e76abf2SYan, Zheng 	left = wait_for_completion_killable_timeout(&req->r_completion,
435242b06965SIlya Dryomov 						ceph_timeout_jiffies(timeout));
435342b06965SIlya Dryomov 	if (left <= 0) {
435442b06965SIlya Dryomov 		left = left ?: -ETIMEDOUT;
435542b06965SIlya Dryomov 		ceph_osdc_cancel_request(req);
435642b06965SIlya Dryomov 	} else {
435742b06965SIlya Dryomov 		left = req->r_result; /* completed */
435842b06965SIlya Dryomov 	}
435942b06965SIlya Dryomov 
436042b06965SIlya Dryomov 	return left;
436142b06965SIlya Dryomov }
436242b06965SIlya Dryomov 
436342b06965SIlya Dryomov /*
43643d14c5d2SYehuda Sadeh  * wait for a request to complete
43653d14c5d2SYehuda Sadeh  */
43663d14c5d2SYehuda Sadeh int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
43673d14c5d2SYehuda Sadeh 			   struct ceph_osd_request *req)
43683d14c5d2SYehuda Sadeh {
436942b06965SIlya Dryomov 	return wait_request_timeout(req, 0);
43703d14c5d2SYehuda Sadeh }
43713d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_wait_request);
43723d14c5d2SYehuda Sadeh 
43733d14c5d2SYehuda Sadeh /*
43743d14c5d2SYehuda Sadeh  * sync - wait for all in-flight requests to flush.  avoid starvation.
43753d14c5d2SYehuda Sadeh  */
43763d14c5d2SYehuda Sadeh void ceph_osdc_sync(struct ceph_osd_client *osdc)
43773d14c5d2SYehuda Sadeh {
43785aea3dcdSIlya Dryomov 	struct rb_node *n, *p;
43795aea3dcdSIlya Dryomov 	u64 last_tid = atomic64_read(&osdc->last_tid);
43803d14c5d2SYehuda Sadeh 
43815aea3dcdSIlya Dryomov again:
43825aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
43835aea3dcdSIlya Dryomov 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
43845aea3dcdSIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
43855aea3dcdSIlya Dryomov 
43865aea3dcdSIlya Dryomov 		mutex_lock(&osd->lock);
43875aea3dcdSIlya Dryomov 		for (p = rb_first(&osd->o_requests); p; p = rb_next(p)) {
43885aea3dcdSIlya Dryomov 			struct ceph_osd_request *req =
43895aea3dcdSIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
43905aea3dcdSIlya Dryomov 
43913d14c5d2SYehuda Sadeh 			if (req->r_tid > last_tid)
43923d14c5d2SYehuda Sadeh 				break;
43933d14c5d2SYehuda Sadeh 
43945aea3dcdSIlya Dryomov 			if (!(req->r_flags & CEPH_OSD_FLAG_WRITE))
43953d14c5d2SYehuda Sadeh 				continue;
43963d14c5d2SYehuda Sadeh 
43973d14c5d2SYehuda Sadeh 			ceph_osdc_get_request(req);
43985aea3dcdSIlya Dryomov 			mutex_unlock(&osd->lock);
43995aea3dcdSIlya Dryomov 			up_read(&osdc->lock);
44005aea3dcdSIlya Dryomov 			dout("%s waiting on req %p tid %llu last_tid %llu\n",
44015aea3dcdSIlya Dryomov 			     __func__, req, req->r_tid, last_tid);
4402b18b9550SIlya Dryomov 			wait_for_completion(&req->r_completion);
44033d14c5d2SYehuda Sadeh 			ceph_osdc_put_request(req);
44045aea3dcdSIlya Dryomov 			goto again;
44053d14c5d2SYehuda Sadeh 		}
44065aea3dcdSIlya Dryomov 
44075aea3dcdSIlya Dryomov 		mutex_unlock(&osd->lock);
44085aea3dcdSIlya Dryomov 	}
44095aea3dcdSIlya Dryomov 
44105aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
44115aea3dcdSIlya Dryomov 	dout("%s done last_tid %llu\n", __func__, last_tid);
44123d14c5d2SYehuda Sadeh }
44133d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_sync);
44143d14c5d2SYehuda Sadeh 
4415922dab61SIlya Dryomov static struct ceph_osd_request *
4416922dab61SIlya Dryomov alloc_linger_request(struct ceph_osd_linger_request *lreq)
4417922dab61SIlya Dryomov {
4418922dab61SIlya Dryomov 	struct ceph_osd_request *req;
4419922dab61SIlya Dryomov 
4420922dab61SIlya Dryomov 	req = ceph_osdc_alloc_request(lreq->osdc, NULL, 1, false, GFP_NOIO);
4421922dab61SIlya Dryomov 	if (!req)
4422922dab61SIlya Dryomov 		return NULL;
4423922dab61SIlya Dryomov 
4424922dab61SIlya Dryomov 	ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
4425922dab61SIlya Dryomov 	ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
4426922dab61SIlya Dryomov 
4427922dab61SIlya Dryomov 	if (ceph_osdc_alloc_messages(req, GFP_NOIO)) {
4428922dab61SIlya Dryomov 		ceph_osdc_put_request(req);
4429922dab61SIlya Dryomov 		return NULL;
4430922dab61SIlya Dryomov 	}
4431922dab61SIlya Dryomov 
4432922dab61SIlya Dryomov 	return req;
4433922dab61SIlya Dryomov }
4434922dab61SIlya Dryomov 
4435922dab61SIlya Dryomov /*
4436922dab61SIlya Dryomov  * Returns a handle, caller owns a ref.
4437922dab61SIlya Dryomov  */
4438922dab61SIlya Dryomov struct ceph_osd_linger_request *
4439922dab61SIlya Dryomov ceph_osdc_watch(struct ceph_osd_client *osdc,
4440922dab61SIlya Dryomov 		struct ceph_object_id *oid,
4441922dab61SIlya Dryomov 		struct ceph_object_locator *oloc,
4442922dab61SIlya Dryomov 		rados_watchcb2_t wcb,
4443922dab61SIlya Dryomov 		rados_watcherrcb_t errcb,
4444922dab61SIlya Dryomov 		void *data)
4445922dab61SIlya Dryomov {
4446922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
4447922dab61SIlya Dryomov 	int ret;
4448922dab61SIlya Dryomov 
4449922dab61SIlya Dryomov 	lreq = linger_alloc(osdc);
4450922dab61SIlya Dryomov 	if (!lreq)
4451922dab61SIlya Dryomov 		return ERR_PTR(-ENOMEM);
4452922dab61SIlya Dryomov 
445319079203SIlya Dryomov 	lreq->is_watch = true;
4454922dab61SIlya Dryomov 	lreq->wcb = wcb;
4455922dab61SIlya Dryomov 	lreq->errcb = errcb;
4456922dab61SIlya Dryomov 	lreq->data = data;
4457b07d3c4bSIlya Dryomov 	lreq->watch_valid_thru = jiffies;
4458922dab61SIlya Dryomov 
4459922dab61SIlya Dryomov 	ceph_oid_copy(&lreq->t.base_oid, oid);
4460922dab61SIlya Dryomov 	ceph_oloc_copy(&lreq->t.base_oloc, oloc);
446154ea0046SIlya Dryomov 	lreq->t.flags = CEPH_OSD_FLAG_WRITE;
44621134e091SDeepa Dinamani 	ktime_get_real_ts(&lreq->mtime);
4463922dab61SIlya Dryomov 
4464922dab61SIlya Dryomov 	lreq->reg_req = alloc_linger_request(lreq);
4465922dab61SIlya Dryomov 	if (!lreq->reg_req) {
4466922dab61SIlya Dryomov 		ret = -ENOMEM;
4467922dab61SIlya Dryomov 		goto err_put_lreq;
4468922dab61SIlya Dryomov 	}
4469922dab61SIlya Dryomov 
4470922dab61SIlya Dryomov 	lreq->ping_req = alloc_linger_request(lreq);
4471922dab61SIlya Dryomov 	if (!lreq->ping_req) {
4472922dab61SIlya Dryomov 		ret = -ENOMEM;
4473922dab61SIlya Dryomov 		goto err_put_lreq;
4474922dab61SIlya Dryomov 	}
4475922dab61SIlya Dryomov 
4476922dab61SIlya Dryomov 	down_write(&osdc->lock);
4477922dab61SIlya Dryomov 	linger_register(lreq); /* before osd_req_op_* */
4478922dab61SIlya Dryomov 	osd_req_op_watch_init(lreq->reg_req, 0, lreq->linger_id,
4479922dab61SIlya Dryomov 			      CEPH_OSD_WATCH_OP_WATCH);
4480922dab61SIlya Dryomov 	osd_req_op_watch_init(lreq->ping_req, 0, lreq->linger_id,
4481922dab61SIlya Dryomov 			      CEPH_OSD_WATCH_OP_PING);
4482922dab61SIlya Dryomov 	linger_submit(lreq);
4483922dab61SIlya Dryomov 	up_write(&osdc->lock);
4484922dab61SIlya Dryomov 
4485922dab61SIlya Dryomov 	ret = linger_reg_commit_wait(lreq);
4486922dab61SIlya Dryomov 	if (ret) {
4487922dab61SIlya Dryomov 		linger_cancel(lreq);
4488922dab61SIlya Dryomov 		goto err_put_lreq;
4489922dab61SIlya Dryomov 	}
4490922dab61SIlya Dryomov 
4491922dab61SIlya Dryomov 	return lreq;
4492922dab61SIlya Dryomov 
4493922dab61SIlya Dryomov err_put_lreq:
4494922dab61SIlya Dryomov 	linger_put(lreq);
4495922dab61SIlya Dryomov 	return ERR_PTR(ret);
4496922dab61SIlya Dryomov }
4497922dab61SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_watch);
4498922dab61SIlya Dryomov 
4499922dab61SIlya Dryomov /*
4500922dab61SIlya Dryomov  * Releases a ref.
4501922dab61SIlya Dryomov  *
4502922dab61SIlya Dryomov  * Times out after mount_timeout to preserve rbd unmap behaviour
4503922dab61SIlya Dryomov  * introduced in 2894e1d76974 ("rbd: timeout watch teardown on unmap
4504922dab61SIlya Dryomov  * with mount_timeout").
4505922dab61SIlya Dryomov  */
4506922dab61SIlya Dryomov int ceph_osdc_unwatch(struct ceph_osd_client *osdc,
4507922dab61SIlya Dryomov 		      struct ceph_osd_linger_request *lreq)
4508922dab61SIlya Dryomov {
4509922dab61SIlya Dryomov 	struct ceph_options *opts = osdc->client->options;
4510922dab61SIlya Dryomov 	struct ceph_osd_request *req;
4511922dab61SIlya Dryomov 	int ret;
4512922dab61SIlya Dryomov 
4513922dab61SIlya Dryomov 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4514922dab61SIlya Dryomov 	if (!req)
4515922dab61SIlya Dryomov 		return -ENOMEM;
4516922dab61SIlya Dryomov 
4517922dab61SIlya Dryomov 	ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
4518922dab61SIlya Dryomov 	ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
451954ea0046SIlya Dryomov 	req->r_flags = CEPH_OSD_FLAG_WRITE;
45201134e091SDeepa Dinamani 	ktime_get_real_ts(&req->r_mtime);
4521922dab61SIlya Dryomov 	osd_req_op_watch_init(req, 0, lreq->linger_id,
4522922dab61SIlya Dryomov 			      CEPH_OSD_WATCH_OP_UNWATCH);
4523922dab61SIlya Dryomov 
4524922dab61SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4525922dab61SIlya Dryomov 	if (ret)
4526922dab61SIlya Dryomov 		goto out_put_req;
4527922dab61SIlya Dryomov 
4528922dab61SIlya Dryomov 	ceph_osdc_start_request(osdc, req, false);
4529922dab61SIlya Dryomov 	linger_cancel(lreq);
4530922dab61SIlya Dryomov 	linger_put(lreq);
4531922dab61SIlya Dryomov 	ret = wait_request_timeout(req, opts->mount_timeout);
4532922dab61SIlya Dryomov 
4533922dab61SIlya Dryomov out_put_req:
4534922dab61SIlya Dryomov 	ceph_osdc_put_request(req);
4535922dab61SIlya Dryomov 	return ret;
4536922dab61SIlya Dryomov }
4537922dab61SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_unwatch);
4538922dab61SIlya Dryomov 
4539922dab61SIlya Dryomov static int osd_req_op_notify_ack_init(struct ceph_osd_request *req, int which,
4540922dab61SIlya Dryomov 				      u64 notify_id, u64 cookie, void *payload,
4541922dab61SIlya Dryomov 				      size_t payload_len)
4542922dab61SIlya Dryomov {
4543922dab61SIlya Dryomov 	struct ceph_osd_req_op *op;
4544922dab61SIlya Dryomov 	struct ceph_pagelist *pl;
4545922dab61SIlya Dryomov 	int ret;
4546922dab61SIlya Dryomov 
4547922dab61SIlya Dryomov 	op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY_ACK, 0);
4548922dab61SIlya Dryomov 
4549922dab61SIlya Dryomov 	pl = kmalloc(sizeof(*pl), GFP_NOIO);
4550922dab61SIlya Dryomov 	if (!pl)
4551922dab61SIlya Dryomov 		return -ENOMEM;
4552922dab61SIlya Dryomov 
4553922dab61SIlya Dryomov 	ceph_pagelist_init(pl);
4554922dab61SIlya Dryomov 	ret = ceph_pagelist_encode_64(pl, notify_id);
4555922dab61SIlya Dryomov 	ret |= ceph_pagelist_encode_64(pl, cookie);
4556922dab61SIlya Dryomov 	if (payload) {
4557922dab61SIlya Dryomov 		ret |= ceph_pagelist_encode_32(pl, payload_len);
4558922dab61SIlya Dryomov 		ret |= ceph_pagelist_append(pl, payload, payload_len);
4559922dab61SIlya Dryomov 	} else {
4560922dab61SIlya Dryomov 		ret |= ceph_pagelist_encode_32(pl, 0);
4561922dab61SIlya Dryomov 	}
4562922dab61SIlya Dryomov 	if (ret) {
4563922dab61SIlya Dryomov 		ceph_pagelist_release(pl);
4564922dab61SIlya Dryomov 		return -ENOMEM;
4565922dab61SIlya Dryomov 	}
4566922dab61SIlya Dryomov 
4567922dab61SIlya Dryomov 	ceph_osd_data_pagelist_init(&op->notify_ack.request_data, pl);
4568922dab61SIlya Dryomov 	op->indata_len = pl->length;
4569922dab61SIlya Dryomov 	return 0;
4570922dab61SIlya Dryomov }
4571922dab61SIlya Dryomov 
4572922dab61SIlya Dryomov int ceph_osdc_notify_ack(struct ceph_osd_client *osdc,
4573922dab61SIlya Dryomov 			 struct ceph_object_id *oid,
4574922dab61SIlya Dryomov 			 struct ceph_object_locator *oloc,
4575922dab61SIlya Dryomov 			 u64 notify_id,
4576922dab61SIlya Dryomov 			 u64 cookie,
4577922dab61SIlya Dryomov 			 void *payload,
4578922dab61SIlya Dryomov 			 size_t payload_len)
4579922dab61SIlya Dryomov {
4580922dab61SIlya Dryomov 	struct ceph_osd_request *req;
4581922dab61SIlya Dryomov 	int ret;
4582922dab61SIlya Dryomov 
4583922dab61SIlya Dryomov 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4584922dab61SIlya Dryomov 	if (!req)
4585922dab61SIlya Dryomov 		return -ENOMEM;
4586922dab61SIlya Dryomov 
4587922dab61SIlya Dryomov 	ceph_oid_copy(&req->r_base_oid, oid);
4588922dab61SIlya Dryomov 	ceph_oloc_copy(&req->r_base_oloc, oloc);
4589922dab61SIlya Dryomov 	req->r_flags = CEPH_OSD_FLAG_READ;
4590922dab61SIlya Dryomov 
4591922dab61SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4592922dab61SIlya Dryomov 	if (ret)
4593922dab61SIlya Dryomov 		goto out_put_req;
4594922dab61SIlya Dryomov 
4595922dab61SIlya Dryomov 	ret = osd_req_op_notify_ack_init(req, 0, notify_id, cookie, payload,
4596922dab61SIlya Dryomov 					 payload_len);
4597922dab61SIlya Dryomov 	if (ret)
4598922dab61SIlya Dryomov 		goto out_put_req;
4599922dab61SIlya Dryomov 
4600922dab61SIlya Dryomov 	ceph_osdc_start_request(osdc, req, false);
4601922dab61SIlya Dryomov 	ret = ceph_osdc_wait_request(osdc, req);
4602922dab61SIlya Dryomov 
4603922dab61SIlya Dryomov out_put_req:
4604922dab61SIlya Dryomov 	ceph_osdc_put_request(req);
4605922dab61SIlya Dryomov 	return ret;
4606922dab61SIlya Dryomov }
4607922dab61SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_notify_ack);
4608922dab61SIlya Dryomov 
460919079203SIlya Dryomov static int osd_req_op_notify_init(struct ceph_osd_request *req, int which,
461019079203SIlya Dryomov 				  u64 cookie, u32 prot_ver, u32 timeout,
461119079203SIlya Dryomov 				  void *payload, size_t payload_len)
461219079203SIlya Dryomov {
461319079203SIlya Dryomov 	struct ceph_osd_req_op *op;
461419079203SIlya Dryomov 	struct ceph_pagelist *pl;
461519079203SIlya Dryomov 	int ret;
461619079203SIlya Dryomov 
461719079203SIlya Dryomov 	op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY, 0);
461819079203SIlya Dryomov 	op->notify.cookie = cookie;
461919079203SIlya Dryomov 
462019079203SIlya Dryomov 	pl = kmalloc(sizeof(*pl), GFP_NOIO);
462119079203SIlya Dryomov 	if (!pl)
462219079203SIlya Dryomov 		return -ENOMEM;
462319079203SIlya Dryomov 
462419079203SIlya Dryomov 	ceph_pagelist_init(pl);
462519079203SIlya Dryomov 	ret = ceph_pagelist_encode_32(pl, 1); /* prot_ver */
462619079203SIlya Dryomov 	ret |= ceph_pagelist_encode_32(pl, timeout);
462719079203SIlya Dryomov 	ret |= ceph_pagelist_encode_32(pl, payload_len);
462819079203SIlya Dryomov 	ret |= ceph_pagelist_append(pl, payload, payload_len);
462919079203SIlya Dryomov 	if (ret) {
463019079203SIlya Dryomov 		ceph_pagelist_release(pl);
463119079203SIlya Dryomov 		return -ENOMEM;
463219079203SIlya Dryomov 	}
463319079203SIlya Dryomov 
463419079203SIlya Dryomov 	ceph_osd_data_pagelist_init(&op->notify.request_data, pl);
463519079203SIlya Dryomov 	op->indata_len = pl->length;
463619079203SIlya Dryomov 	return 0;
463719079203SIlya Dryomov }
463819079203SIlya Dryomov 
463919079203SIlya Dryomov /*
464019079203SIlya Dryomov  * @timeout: in seconds
464119079203SIlya Dryomov  *
464219079203SIlya Dryomov  * @preply_{pages,len} are initialized both on success and error.
464319079203SIlya Dryomov  * The caller is responsible for:
464419079203SIlya Dryomov  *
464519079203SIlya Dryomov  *     ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len))
464619079203SIlya Dryomov  */
464719079203SIlya Dryomov int ceph_osdc_notify(struct ceph_osd_client *osdc,
464819079203SIlya Dryomov 		     struct ceph_object_id *oid,
464919079203SIlya Dryomov 		     struct ceph_object_locator *oloc,
465019079203SIlya Dryomov 		     void *payload,
465119079203SIlya Dryomov 		     size_t payload_len,
465219079203SIlya Dryomov 		     u32 timeout,
465319079203SIlya Dryomov 		     struct page ***preply_pages,
465419079203SIlya Dryomov 		     size_t *preply_len)
465519079203SIlya Dryomov {
465619079203SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
465719079203SIlya Dryomov 	struct page **pages;
465819079203SIlya Dryomov 	int ret;
465919079203SIlya Dryomov 
466019079203SIlya Dryomov 	WARN_ON(!timeout);
466119079203SIlya Dryomov 	if (preply_pages) {
466219079203SIlya Dryomov 		*preply_pages = NULL;
466319079203SIlya Dryomov 		*preply_len = 0;
466419079203SIlya Dryomov 	}
466519079203SIlya Dryomov 
466619079203SIlya Dryomov 	lreq = linger_alloc(osdc);
466719079203SIlya Dryomov 	if (!lreq)
466819079203SIlya Dryomov 		return -ENOMEM;
466919079203SIlya Dryomov 
467019079203SIlya Dryomov 	lreq->preply_pages = preply_pages;
467119079203SIlya Dryomov 	lreq->preply_len = preply_len;
467219079203SIlya Dryomov 
467319079203SIlya Dryomov 	ceph_oid_copy(&lreq->t.base_oid, oid);
467419079203SIlya Dryomov 	ceph_oloc_copy(&lreq->t.base_oloc, oloc);
467519079203SIlya Dryomov 	lreq->t.flags = CEPH_OSD_FLAG_READ;
467619079203SIlya Dryomov 
467719079203SIlya Dryomov 	lreq->reg_req = alloc_linger_request(lreq);
467819079203SIlya Dryomov 	if (!lreq->reg_req) {
467919079203SIlya Dryomov 		ret = -ENOMEM;
468019079203SIlya Dryomov 		goto out_put_lreq;
468119079203SIlya Dryomov 	}
468219079203SIlya Dryomov 
468319079203SIlya Dryomov 	/* for notify_id */
468419079203SIlya Dryomov 	pages = ceph_alloc_page_vector(1, GFP_NOIO);
468519079203SIlya Dryomov 	if (IS_ERR(pages)) {
468619079203SIlya Dryomov 		ret = PTR_ERR(pages);
468719079203SIlya Dryomov 		goto out_put_lreq;
468819079203SIlya Dryomov 	}
468919079203SIlya Dryomov 
469019079203SIlya Dryomov 	down_write(&osdc->lock);
469119079203SIlya Dryomov 	linger_register(lreq); /* before osd_req_op_* */
469219079203SIlya Dryomov 	ret = osd_req_op_notify_init(lreq->reg_req, 0, lreq->linger_id, 1,
469319079203SIlya Dryomov 				     timeout, payload, payload_len);
469419079203SIlya Dryomov 	if (ret) {
469519079203SIlya Dryomov 		linger_unregister(lreq);
469619079203SIlya Dryomov 		up_write(&osdc->lock);
469719079203SIlya Dryomov 		ceph_release_page_vector(pages, 1);
469819079203SIlya Dryomov 		goto out_put_lreq;
469919079203SIlya Dryomov 	}
470019079203SIlya Dryomov 	ceph_osd_data_pages_init(osd_req_op_data(lreq->reg_req, 0, notify,
470119079203SIlya Dryomov 						 response_data),
470219079203SIlya Dryomov 				 pages, PAGE_SIZE, 0, false, true);
470319079203SIlya Dryomov 	linger_submit(lreq);
470419079203SIlya Dryomov 	up_write(&osdc->lock);
470519079203SIlya Dryomov 
470619079203SIlya Dryomov 	ret = linger_reg_commit_wait(lreq);
470719079203SIlya Dryomov 	if (!ret)
470819079203SIlya Dryomov 		ret = linger_notify_finish_wait(lreq);
470919079203SIlya Dryomov 	else
471019079203SIlya Dryomov 		dout("lreq %p failed to initiate notify %d\n", lreq, ret);
471119079203SIlya Dryomov 
471219079203SIlya Dryomov 	linger_cancel(lreq);
471319079203SIlya Dryomov out_put_lreq:
471419079203SIlya Dryomov 	linger_put(lreq);
471519079203SIlya Dryomov 	return ret;
471619079203SIlya Dryomov }
471719079203SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_notify);
471819079203SIlya Dryomov 
47193d14c5d2SYehuda Sadeh /*
4720b07d3c4bSIlya Dryomov  * Return the number of milliseconds since the watch was last
4721b07d3c4bSIlya Dryomov  * confirmed, or an error.  If there is an error, the watch is no
4722b07d3c4bSIlya Dryomov  * longer valid, and should be destroyed with ceph_osdc_unwatch().
4723b07d3c4bSIlya Dryomov  */
4724b07d3c4bSIlya Dryomov int ceph_osdc_watch_check(struct ceph_osd_client *osdc,
4725b07d3c4bSIlya Dryomov 			  struct ceph_osd_linger_request *lreq)
4726b07d3c4bSIlya Dryomov {
4727b07d3c4bSIlya Dryomov 	unsigned long stamp, age;
4728b07d3c4bSIlya Dryomov 	int ret;
4729b07d3c4bSIlya Dryomov 
4730b07d3c4bSIlya Dryomov 	down_read(&osdc->lock);
4731b07d3c4bSIlya Dryomov 	mutex_lock(&lreq->lock);
4732b07d3c4bSIlya Dryomov 	stamp = lreq->watch_valid_thru;
4733b07d3c4bSIlya Dryomov 	if (!list_empty(&lreq->pending_lworks)) {
4734b07d3c4bSIlya Dryomov 		struct linger_work *lwork =
4735b07d3c4bSIlya Dryomov 		    list_first_entry(&lreq->pending_lworks,
4736b07d3c4bSIlya Dryomov 				     struct linger_work,
4737b07d3c4bSIlya Dryomov 				     pending_item);
4738b07d3c4bSIlya Dryomov 
4739b07d3c4bSIlya Dryomov 		if (time_before(lwork->queued_stamp, stamp))
4740b07d3c4bSIlya Dryomov 			stamp = lwork->queued_stamp;
4741b07d3c4bSIlya Dryomov 	}
4742b07d3c4bSIlya Dryomov 	age = jiffies - stamp;
4743b07d3c4bSIlya Dryomov 	dout("%s lreq %p linger_id %llu age %lu last_error %d\n", __func__,
4744b07d3c4bSIlya Dryomov 	     lreq, lreq->linger_id, age, lreq->last_error);
4745b07d3c4bSIlya Dryomov 	/* we are truncating to msecs, so return a safe upper bound */
4746b07d3c4bSIlya Dryomov 	ret = lreq->last_error ?: 1 + jiffies_to_msecs(age);
4747b07d3c4bSIlya Dryomov 
4748b07d3c4bSIlya Dryomov 	mutex_unlock(&lreq->lock);
4749b07d3c4bSIlya Dryomov 	up_read(&osdc->lock);
4750b07d3c4bSIlya Dryomov 	return ret;
4751b07d3c4bSIlya Dryomov }
4752b07d3c4bSIlya Dryomov 
4753a4ed38d7SDouglas Fuller static int decode_watcher(void **p, void *end, struct ceph_watch_item *item)
4754a4ed38d7SDouglas Fuller {
4755a4ed38d7SDouglas Fuller 	u8 struct_v;
4756a4ed38d7SDouglas Fuller 	u32 struct_len;
4757a4ed38d7SDouglas Fuller 	int ret;
4758a4ed38d7SDouglas Fuller 
4759a4ed38d7SDouglas Fuller 	ret = ceph_start_decoding(p, end, 2, "watch_item_t",
4760a4ed38d7SDouglas Fuller 				  &struct_v, &struct_len);
4761a4ed38d7SDouglas Fuller 	if (ret)
4762a4ed38d7SDouglas Fuller 		return ret;
4763a4ed38d7SDouglas Fuller 
4764a4ed38d7SDouglas Fuller 	ceph_decode_copy(p, &item->name, sizeof(item->name));
4765a4ed38d7SDouglas Fuller 	item->cookie = ceph_decode_64(p);
4766a4ed38d7SDouglas Fuller 	*p += 4; /* skip timeout_seconds */
4767a4ed38d7SDouglas Fuller 	if (struct_v >= 2) {
4768a4ed38d7SDouglas Fuller 		ceph_decode_copy(p, &item->addr, sizeof(item->addr));
4769a4ed38d7SDouglas Fuller 		ceph_decode_addr(&item->addr);
4770a4ed38d7SDouglas Fuller 	}
4771a4ed38d7SDouglas Fuller 
4772a4ed38d7SDouglas Fuller 	dout("%s %s%llu cookie %llu addr %s\n", __func__,
4773a4ed38d7SDouglas Fuller 	     ENTITY_NAME(item->name), item->cookie,
4774a4ed38d7SDouglas Fuller 	     ceph_pr_addr(&item->addr.in_addr));
4775a4ed38d7SDouglas Fuller 	return 0;
4776a4ed38d7SDouglas Fuller }
4777a4ed38d7SDouglas Fuller 
4778a4ed38d7SDouglas Fuller static int decode_watchers(void **p, void *end,
4779a4ed38d7SDouglas Fuller 			   struct ceph_watch_item **watchers,
4780a4ed38d7SDouglas Fuller 			   u32 *num_watchers)
4781a4ed38d7SDouglas Fuller {
4782a4ed38d7SDouglas Fuller 	u8 struct_v;
4783a4ed38d7SDouglas Fuller 	u32 struct_len;
4784a4ed38d7SDouglas Fuller 	int i;
4785a4ed38d7SDouglas Fuller 	int ret;
4786a4ed38d7SDouglas Fuller 
4787a4ed38d7SDouglas Fuller 	ret = ceph_start_decoding(p, end, 1, "obj_list_watch_response_t",
4788a4ed38d7SDouglas Fuller 				  &struct_v, &struct_len);
4789a4ed38d7SDouglas Fuller 	if (ret)
4790a4ed38d7SDouglas Fuller 		return ret;
4791a4ed38d7SDouglas Fuller 
4792a4ed38d7SDouglas Fuller 	*num_watchers = ceph_decode_32(p);
4793a4ed38d7SDouglas Fuller 	*watchers = kcalloc(*num_watchers, sizeof(**watchers), GFP_NOIO);
4794a4ed38d7SDouglas Fuller 	if (!*watchers)
4795a4ed38d7SDouglas Fuller 		return -ENOMEM;
4796a4ed38d7SDouglas Fuller 
4797a4ed38d7SDouglas Fuller 	for (i = 0; i < *num_watchers; i++) {
4798a4ed38d7SDouglas Fuller 		ret = decode_watcher(p, end, *watchers + i);
4799a4ed38d7SDouglas Fuller 		if (ret) {
4800a4ed38d7SDouglas Fuller 			kfree(*watchers);
4801a4ed38d7SDouglas Fuller 			return ret;
4802a4ed38d7SDouglas Fuller 		}
4803a4ed38d7SDouglas Fuller 	}
4804a4ed38d7SDouglas Fuller 
4805a4ed38d7SDouglas Fuller 	return 0;
4806a4ed38d7SDouglas Fuller }
4807a4ed38d7SDouglas Fuller 
4808a4ed38d7SDouglas Fuller /*
4809a4ed38d7SDouglas Fuller  * On success, the caller is responsible for:
4810a4ed38d7SDouglas Fuller  *
4811a4ed38d7SDouglas Fuller  *     kfree(watchers);
4812a4ed38d7SDouglas Fuller  */
4813a4ed38d7SDouglas Fuller int ceph_osdc_list_watchers(struct ceph_osd_client *osdc,
4814a4ed38d7SDouglas Fuller 			    struct ceph_object_id *oid,
4815a4ed38d7SDouglas Fuller 			    struct ceph_object_locator *oloc,
4816a4ed38d7SDouglas Fuller 			    struct ceph_watch_item **watchers,
4817a4ed38d7SDouglas Fuller 			    u32 *num_watchers)
4818a4ed38d7SDouglas Fuller {
4819a4ed38d7SDouglas Fuller 	struct ceph_osd_request *req;
4820a4ed38d7SDouglas Fuller 	struct page **pages;
4821a4ed38d7SDouglas Fuller 	int ret;
4822a4ed38d7SDouglas Fuller 
4823a4ed38d7SDouglas Fuller 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4824a4ed38d7SDouglas Fuller 	if (!req)
4825a4ed38d7SDouglas Fuller 		return -ENOMEM;
4826a4ed38d7SDouglas Fuller 
4827a4ed38d7SDouglas Fuller 	ceph_oid_copy(&req->r_base_oid, oid);
4828a4ed38d7SDouglas Fuller 	ceph_oloc_copy(&req->r_base_oloc, oloc);
4829a4ed38d7SDouglas Fuller 	req->r_flags = CEPH_OSD_FLAG_READ;
4830a4ed38d7SDouglas Fuller 
4831a4ed38d7SDouglas Fuller 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4832a4ed38d7SDouglas Fuller 	if (ret)
4833a4ed38d7SDouglas Fuller 		goto out_put_req;
4834a4ed38d7SDouglas Fuller 
4835a4ed38d7SDouglas Fuller 	pages = ceph_alloc_page_vector(1, GFP_NOIO);
4836a4ed38d7SDouglas Fuller 	if (IS_ERR(pages)) {
4837a4ed38d7SDouglas Fuller 		ret = PTR_ERR(pages);
4838a4ed38d7SDouglas Fuller 		goto out_put_req;
4839a4ed38d7SDouglas Fuller 	}
4840a4ed38d7SDouglas Fuller 
4841a4ed38d7SDouglas Fuller 	osd_req_op_init(req, 0, CEPH_OSD_OP_LIST_WATCHERS, 0);
4842a4ed38d7SDouglas Fuller 	ceph_osd_data_pages_init(osd_req_op_data(req, 0, list_watchers,
4843a4ed38d7SDouglas Fuller 						 response_data),
4844a4ed38d7SDouglas Fuller 				 pages, PAGE_SIZE, 0, false, true);
4845a4ed38d7SDouglas Fuller 
4846a4ed38d7SDouglas Fuller 	ceph_osdc_start_request(osdc, req, false);
4847a4ed38d7SDouglas Fuller 	ret = ceph_osdc_wait_request(osdc, req);
4848a4ed38d7SDouglas Fuller 	if (ret >= 0) {
4849a4ed38d7SDouglas Fuller 		void *p = page_address(pages[0]);
4850a4ed38d7SDouglas Fuller 		void *const end = p + req->r_ops[0].outdata_len;
4851a4ed38d7SDouglas Fuller 
4852a4ed38d7SDouglas Fuller 		ret = decode_watchers(&p, end, watchers, num_watchers);
4853a4ed38d7SDouglas Fuller 	}
4854a4ed38d7SDouglas Fuller 
4855a4ed38d7SDouglas Fuller out_put_req:
4856a4ed38d7SDouglas Fuller 	ceph_osdc_put_request(req);
4857a4ed38d7SDouglas Fuller 	return ret;
4858a4ed38d7SDouglas Fuller }
4859a4ed38d7SDouglas Fuller EXPORT_SYMBOL(ceph_osdc_list_watchers);
4860a4ed38d7SDouglas Fuller 
4861b07d3c4bSIlya Dryomov /*
4862dd935f44SJosh Durgin  * Call all pending notify callbacks - for use after a watch is
4863dd935f44SJosh Durgin  * unregistered, to make sure no more callbacks for it will be invoked
4864dd935f44SJosh Durgin  */
4865f6479449Sstephen hemminger void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
4866dd935f44SJosh Durgin {
486799d16943SIlya Dryomov 	dout("%s osdc %p\n", __func__, osdc);
4868dd935f44SJosh Durgin 	flush_workqueue(osdc->notify_wq);
4869dd935f44SJosh Durgin }
4870dd935f44SJosh Durgin EXPORT_SYMBOL(ceph_osdc_flush_notifies);
4871dd935f44SJosh Durgin 
48727cca78c9SIlya Dryomov void ceph_osdc_maybe_request_map(struct ceph_osd_client *osdc)
48737cca78c9SIlya Dryomov {
48747cca78c9SIlya Dryomov 	down_read(&osdc->lock);
48757cca78c9SIlya Dryomov 	maybe_request_map(osdc);
48767cca78c9SIlya Dryomov 	up_read(&osdc->lock);
48777cca78c9SIlya Dryomov }
48787cca78c9SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_maybe_request_map);
4879dd935f44SJosh Durgin 
4880dd935f44SJosh Durgin /*
4881428a7158SDouglas Fuller  * Execute an OSD class method on an object.
4882428a7158SDouglas Fuller  *
4883428a7158SDouglas Fuller  * @flags: CEPH_OSD_FLAG_*
48842544a020SIlya Dryomov  * @resp_len: in/out param for reply length
4885428a7158SDouglas Fuller  */
4886428a7158SDouglas Fuller int ceph_osdc_call(struct ceph_osd_client *osdc,
4887428a7158SDouglas Fuller 		   struct ceph_object_id *oid,
4888428a7158SDouglas Fuller 		   struct ceph_object_locator *oloc,
4889428a7158SDouglas Fuller 		   const char *class, const char *method,
4890428a7158SDouglas Fuller 		   unsigned int flags,
4891428a7158SDouglas Fuller 		   struct page *req_page, size_t req_len,
4892428a7158SDouglas Fuller 		   struct page *resp_page, size_t *resp_len)
4893428a7158SDouglas Fuller {
4894428a7158SDouglas Fuller 	struct ceph_osd_request *req;
4895428a7158SDouglas Fuller 	int ret;
4896428a7158SDouglas Fuller 
48972544a020SIlya Dryomov 	if (req_len > PAGE_SIZE || (resp_page && *resp_len > PAGE_SIZE))
48982544a020SIlya Dryomov 		return -E2BIG;
48992544a020SIlya Dryomov 
4900428a7158SDouglas Fuller 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4901428a7158SDouglas Fuller 	if (!req)
4902428a7158SDouglas Fuller 		return -ENOMEM;
4903428a7158SDouglas Fuller 
4904428a7158SDouglas Fuller 	ceph_oid_copy(&req->r_base_oid, oid);
4905428a7158SDouglas Fuller 	ceph_oloc_copy(&req->r_base_oloc, oloc);
4906428a7158SDouglas Fuller 	req->r_flags = flags;
4907428a7158SDouglas Fuller 
4908428a7158SDouglas Fuller 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4909428a7158SDouglas Fuller 	if (ret)
4910428a7158SDouglas Fuller 		goto out_put_req;
4911428a7158SDouglas Fuller 
4912428a7158SDouglas Fuller 	osd_req_op_cls_init(req, 0, CEPH_OSD_OP_CALL, class, method);
4913428a7158SDouglas Fuller 	if (req_page)
4914428a7158SDouglas Fuller 		osd_req_op_cls_request_data_pages(req, 0, &req_page, req_len,
4915428a7158SDouglas Fuller 						  0, false, false);
4916428a7158SDouglas Fuller 	if (resp_page)
4917428a7158SDouglas Fuller 		osd_req_op_cls_response_data_pages(req, 0, &resp_page,
49182544a020SIlya Dryomov 						   *resp_len, 0, false, false);
4919428a7158SDouglas Fuller 
4920428a7158SDouglas Fuller 	ceph_osdc_start_request(osdc, req, false);
4921428a7158SDouglas Fuller 	ret = ceph_osdc_wait_request(osdc, req);
4922428a7158SDouglas Fuller 	if (ret >= 0) {
4923428a7158SDouglas Fuller 		ret = req->r_ops[0].rval;
4924428a7158SDouglas Fuller 		if (resp_page)
4925428a7158SDouglas Fuller 			*resp_len = req->r_ops[0].outdata_len;
4926428a7158SDouglas Fuller 	}
4927428a7158SDouglas Fuller 
4928428a7158SDouglas Fuller out_put_req:
4929428a7158SDouglas Fuller 	ceph_osdc_put_request(req);
4930428a7158SDouglas Fuller 	return ret;
4931428a7158SDouglas Fuller }
4932428a7158SDouglas Fuller EXPORT_SYMBOL(ceph_osdc_call);
4933428a7158SDouglas Fuller 
4934428a7158SDouglas Fuller /*
49353d14c5d2SYehuda Sadeh  * init, shutdown
49363d14c5d2SYehuda Sadeh  */
49373d14c5d2SYehuda Sadeh int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
49383d14c5d2SYehuda Sadeh {
49393d14c5d2SYehuda Sadeh 	int err;
49403d14c5d2SYehuda Sadeh 
49413d14c5d2SYehuda Sadeh 	dout("init\n");
49423d14c5d2SYehuda Sadeh 	osdc->client = client;
49435aea3dcdSIlya Dryomov 	init_rwsem(&osdc->lock);
49443d14c5d2SYehuda Sadeh 	osdc->osds = RB_ROOT;
49453d14c5d2SYehuda Sadeh 	INIT_LIST_HEAD(&osdc->osd_lru);
49469dd2845cSIlya Dryomov 	spin_lock_init(&osdc->osd_lru_lock);
49475aea3dcdSIlya Dryomov 	osd_init(&osdc->homeless_osd);
49485aea3dcdSIlya Dryomov 	osdc->homeless_osd.o_osdc = osdc;
49495aea3dcdSIlya Dryomov 	osdc->homeless_osd.o_osd = CEPH_HOMELESS_OSD;
4950264048afSIlya Dryomov 	osdc->last_linger_id = CEPH_LINGER_ID_START;
4951922dab61SIlya Dryomov 	osdc->linger_requests = RB_ROOT;
49524609245eSIlya Dryomov 	osdc->map_checks = RB_ROOT;
49534609245eSIlya Dryomov 	osdc->linger_map_checks = RB_ROOT;
49543d14c5d2SYehuda Sadeh 	INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
49553d14c5d2SYehuda Sadeh 	INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
49563d14c5d2SYehuda Sadeh 
49573d14c5d2SYehuda Sadeh 	err = -ENOMEM;
4958e5253a7bSIlya Dryomov 	osdc->osdmap = ceph_osdmap_alloc();
4959e5253a7bSIlya Dryomov 	if (!osdc->osdmap)
4960e5253a7bSIlya Dryomov 		goto out;
4961e5253a7bSIlya Dryomov 
49629e767adbSIlya Dryomov 	osdc->req_mempool = mempool_create_slab_pool(10,
49639e767adbSIlya Dryomov 						     ceph_osd_request_cache);
49643d14c5d2SYehuda Sadeh 	if (!osdc->req_mempool)
4965e5253a7bSIlya Dryomov 		goto out_map;
49663d14c5d2SYehuda Sadeh 
4967d50b409fSSage Weil 	err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
4968711da55dSIlya Dryomov 				PAGE_SIZE, 10, true, "osd_op");
49693d14c5d2SYehuda Sadeh 	if (err < 0)
49703d14c5d2SYehuda Sadeh 		goto out_mempool;
4971d50b409fSSage Weil 	err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
4972711da55dSIlya Dryomov 				PAGE_SIZE, 10, true, "osd_op_reply");
49733d14c5d2SYehuda Sadeh 	if (err < 0)
49743d14c5d2SYehuda Sadeh 		goto out_msgpool;
4975a40c4f10SYehuda Sadeh 
4976dbcae088SDan Carpenter 	err = -ENOMEM;
4977a40c4f10SYehuda Sadeh 	osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
4978dbcae088SDan Carpenter 	if (!osdc->notify_wq)
4979c172ec5cSIlya Dryomov 		goto out_msgpool_reply;
4980c172ec5cSIlya Dryomov 
4981fbca9635SIlya Dryomov 	schedule_delayed_work(&osdc->timeout_work,
4982fbca9635SIlya Dryomov 			      osdc->client->options->osd_keepalive_timeout);
4983b37ee1b9SIlya Dryomov 	schedule_delayed_work(&osdc->osds_timeout_work,
4984b37ee1b9SIlya Dryomov 	    round_jiffies_relative(osdc->client->options->osd_idle_ttl));
4985b37ee1b9SIlya Dryomov 
49863d14c5d2SYehuda Sadeh 	return 0;
49873d14c5d2SYehuda Sadeh 
4988c172ec5cSIlya Dryomov out_msgpool_reply:
4989c172ec5cSIlya Dryomov 	ceph_msgpool_destroy(&osdc->msgpool_op_reply);
49903d14c5d2SYehuda Sadeh out_msgpool:
49913d14c5d2SYehuda Sadeh 	ceph_msgpool_destroy(&osdc->msgpool_op);
49923d14c5d2SYehuda Sadeh out_mempool:
49933d14c5d2SYehuda Sadeh 	mempool_destroy(osdc->req_mempool);
4994e5253a7bSIlya Dryomov out_map:
4995e5253a7bSIlya Dryomov 	ceph_osdmap_destroy(osdc->osdmap);
49963d14c5d2SYehuda Sadeh out:
49973d14c5d2SYehuda Sadeh 	return err;
49983d14c5d2SYehuda Sadeh }
49993d14c5d2SYehuda Sadeh 
50003d14c5d2SYehuda Sadeh void ceph_osdc_stop(struct ceph_osd_client *osdc)
50013d14c5d2SYehuda Sadeh {
5002a40c4f10SYehuda Sadeh 	flush_workqueue(osdc->notify_wq);
5003a40c4f10SYehuda Sadeh 	destroy_workqueue(osdc->notify_wq);
50043d14c5d2SYehuda Sadeh 	cancel_delayed_work_sync(&osdc->timeout_work);
50053d14c5d2SYehuda Sadeh 	cancel_delayed_work_sync(&osdc->osds_timeout_work);
500642a2c09fSIlya Dryomov 
50075aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
500842a2c09fSIlya Dryomov 	while (!RB_EMPTY_ROOT(&osdc->osds)) {
500942a2c09fSIlya Dryomov 		struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
501042a2c09fSIlya Dryomov 						struct ceph_osd, o_node);
50115aea3dcdSIlya Dryomov 		close_osd(osd);
501242a2c09fSIlya Dryomov 	}
50135aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
501402113a0fSElena Reshetova 	WARN_ON(refcount_read(&osdc->homeless_osd.o_ref) != 1);
50155aea3dcdSIlya Dryomov 	osd_cleanup(&osdc->homeless_osd);
50165aea3dcdSIlya Dryomov 
50175aea3dcdSIlya Dryomov 	WARN_ON(!list_empty(&osdc->osd_lru));
5018922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_requests));
50194609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osdc->map_checks));
50204609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_map_checks));
50215aea3dcdSIlya Dryomov 	WARN_ON(atomic_read(&osdc->num_requests));
50225aea3dcdSIlya Dryomov 	WARN_ON(atomic_read(&osdc->num_homeless));
502342a2c09fSIlya Dryomov 
50243d14c5d2SYehuda Sadeh 	ceph_osdmap_destroy(osdc->osdmap);
50253d14c5d2SYehuda Sadeh 	mempool_destroy(osdc->req_mempool);
50263d14c5d2SYehuda Sadeh 	ceph_msgpool_destroy(&osdc->msgpool_op);
50273d14c5d2SYehuda Sadeh 	ceph_msgpool_destroy(&osdc->msgpool_op_reply);
50283d14c5d2SYehuda Sadeh }
50293d14c5d2SYehuda Sadeh 
50303d14c5d2SYehuda Sadeh /*
50313d14c5d2SYehuda Sadeh  * Read some contiguous pages.  If we cross a stripe boundary, shorten
50323d14c5d2SYehuda Sadeh  * *plen.  Return number of bytes read, or error.
50333d14c5d2SYehuda Sadeh  */
50343d14c5d2SYehuda Sadeh int ceph_osdc_readpages(struct ceph_osd_client *osdc,
50353d14c5d2SYehuda Sadeh 			struct ceph_vino vino, struct ceph_file_layout *layout,
50363d14c5d2SYehuda Sadeh 			u64 off, u64 *plen,
50373d14c5d2SYehuda Sadeh 			u32 truncate_seq, u64 truncate_size,
5038b7495fc2SSage Weil 			struct page **pages, int num_pages, int page_align)
50393d14c5d2SYehuda Sadeh {
50403d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
50413d14c5d2SYehuda Sadeh 	int rc = 0;
50423d14c5d2SYehuda Sadeh 
50433d14c5d2SYehuda Sadeh 	dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
50443d14c5d2SYehuda Sadeh 	     vino.snap, off, *plen);
5045715e4cd4SYan, Zheng 	req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
50463d14c5d2SYehuda Sadeh 				    CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
5047acead002SAlex Elder 				    NULL, truncate_seq, truncate_size,
5048153e5167SAlex Elder 				    false);
50496816282dSSage Weil 	if (IS_ERR(req))
50506816282dSSage Weil 		return PTR_ERR(req);
50513d14c5d2SYehuda Sadeh 
50523d14c5d2SYehuda Sadeh 	/* it may be a short read due to an object boundary */
5053406e2c9fSAlex Elder 	osd_req_op_extent_osd_data_pages(req, 0,
5054a4ce40a9SAlex Elder 				pages, *plen, page_align, false, false);
50553d14c5d2SYehuda Sadeh 
5056e0c59487SAlex Elder 	dout("readpages  final extent is %llu~%llu (%llu bytes align %d)\n",
505743bfe5deSAlex Elder 	     off, *plen, *plen, page_align);
50583d14c5d2SYehuda Sadeh 
50593d14c5d2SYehuda Sadeh 	rc = ceph_osdc_start_request(osdc, req, false);
50603d14c5d2SYehuda Sadeh 	if (!rc)
50613d14c5d2SYehuda Sadeh 		rc = ceph_osdc_wait_request(osdc, req);
50623d14c5d2SYehuda Sadeh 
50633d14c5d2SYehuda Sadeh 	ceph_osdc_put_request(req);
50643d14c5d2SYehuda Sadeh 	dout("readpages result %d\n", rc);
50653d14c5d2SYehuda Sadeh 	return rc;
50663d14c5d2SYehuda Sadeh }
50673d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_readpages);
50683d14c5d2SYehuda Sadeh 
50693d14c5d2SYehuda Sadeh /*
50703d14c5d2SYehuda Sadeh  * do a synchronous write on N pages
50713d14c5d2SYehuda Sadeh  */
50723d14c5d2SYehuda Sadeh int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
50733d14c5d2SYehuda Sadeh 			 struct ceph_file_layout *layout,
50743d14c5d2SYehuda Sadeh 			 struct ceph_snap_context *snapc,
50753d14c5d2SYehuda Sadeh 			 u64 off, u64 len,
50763d14c5d2SYehuda Sadeh 			 u32 truncate_seq, u64 truncate_size,
50773d14c5d2SYehuda Sadeh 			 struct timespec *mtime,
507824808826SAlex Elder 			 struct page **pages, int num_pages)
50793d14c5d2SYehuda Sadeh {
50803d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
50813d14c5d2SYehuda Sadeh 	int rc = 0;
5082b7495fc2SSage Weil 	int page_align = off & ~PAGE_MASK;
50833d14c5d2SYehuda Sadeh 
5084715e4cd4SYan, Zheng 	req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
508554ea0046SIlya Dryomov 				    CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
5086acead002SAlex Elder 				    snapc, truncate_seq, truncate_size,
5087153e5167SAlex Elder 				    true);
50886816282dSSage Weil 	if (IS_ERR(req))
50896816282dSSage Weil 		return PTR_ERR(req);
50903d14c5d2SYehuda Sadeh 
50913d14c5d2SYehuda Sadeh 	/* it may be a short write due to an object boundary */
5092406e2c9fSAlex Elder 	osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
509343bfe5deSAlex Elder 				false, false);
509443bfe5deSAlex Elder 	dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
50953d14c5d2SYehuda Sadeh 
5096bb873b53SIlya Dryomov 	req->r_mtime = *mtime;
509787f979d3SAlex Elder 	rc = ceph_osdc_start_request(osdc, req, true);
50983d14c5d2SYehuda Sadeh 	if (!rc)
50993d14c5d2SYehuda Sadeh 		rc = ceph_osdc_wait_request(osdc, req);
51003d14c5d2SYehuda Sadeh 
51013d14c5d2SYehuda Sadeh 	ceph_osdc_put_request(req);
51023d14c5d2SYehuda Sadeh 	if (rc == 0)
51033d14c5d2SYehuda Sadeh 		rc = len;
51043d14c5d2SYehuda Sadeh 	dout("writepages result %d\n", rc);
51053d14c5d2SYehuda Sadeh 	return rc;
51063d14c5d2SYehuda Sadeh }
51073d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_writepages);
51083d14c5d2SYehuda Sadeh 
510957a35dfbSChengguang Xu int __init ceph_osdc_setup(void)
51105522ae0bSAlex Elder {
51113f1af42aSIlya Dryomov 	size_t size = sizeof(struct ceph_osd_request) +
51123f1af42aSIlya Dryomov 	    CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
51133f1af42aSIlya Dryomov 
51145522ae0bSAlex Elder 	BUG_ON(ceph_osd_request_cache);
51153f1af42aSIlya Dryomov 	ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
51163f1af42aSIlya Dryomov 						   0, 0, NULL);
51175522ae0bSAlex Elder 
51185522ae0bSAlex Elder 	return ceph_osd_request_cache ? 0 : -ENOMEM;
51195522ae0bSAlex Elder }
51205522ae0bSAlex Elder 
51215522ae0bSAlex Elder void ceph_osdc_cleanup(void)
51225522ae0bSAlex Elder {
51235522ae0bSAlex Elder 	BUG_ON(!ceph_osd_request_cache);
51245522ae0bSAlex Elder 	kmem_cache_destroy(ceph_osd_request_cache);
51255522ae0bSAlex Elder 	ceph_osd_request_cache = NULL;
51265522ae0bSAlex Elder }
51275522ae0bSAlex Elder 
51283d14c5d2SYehuda Sadeh /*
51293d14c5d2SYehuda Sadeh  * handle incoming message
51303d14c5d2SYehuda Sadeh  */
51313d14c5d2SYehuda Sadeh static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
51323d14c5d2SYehuda Sadeh {
51333d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
51345aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
51353d14c5d2SYehuda Sadeh 	int type = le16_to_cpu(msg->hdr.type);
51363d14c5d2SYehuda Sadeh 
51373d14c5d2SYehuda Sadeh 	switch (type) {
51383d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_MAP:
51393d14c5d2SYehuda Sadeh 		ceph_osdc_handle_map(osdc, msg);
51403d14c5d2SYehuda Sadeh 		break;
51413d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_OPREPLY:
51425aea3dcdSIlya Dryomov 		handle_reply(osd, msg);
51433d14c5d2SYehuda Sadeh 		break;
5144a02a946dSIlya Dryomov 	case CEPH_MSG_OSD_BACKOFF:
5145a02a946dSIlya Dryomov 		handle_backoff(osd, msg);
5146a02a946dSIlya Dryomov 		break;
5147a40c4f10SYehuda Sadeh 	case CEPH_MSG_WATCH_NOTIFY:
5148a40c4f10SYehuda Sadeh 		handle_watch_notify(osdc, msg);
5149a40c4f10SYehuda Sadeh 		break;
51503d14c5d2SYehuda Sadeh 
51513d14c5d2SYehuda Sadeh 	default:
51523d14c5d2SYehuda Sadeh 		pr_err("received unknown message type %d %s\n", type,
51533d14c5d2SYehuda Sadeh 		       ceph_msg_type_name(type));
51543d14c5d2SYehuda Sadeh 	}
51555aea3dcdSIlya Dryomov 
51563d14c5d2SYehuda Sadeh 	ceph_msg_put(msg);
51573d14c5d2SYehuda Sadeh }
51583d14c5d2SYehuda Sadeh 
51593d14c5d2SYehuda Sadeh /*
5160d15f9d69SIlya Dryomov  * Lookup and return message for incoming reply.  Don't try to do
5161d15f9d69SIlya Dryomov  * anything about a larger than preallocated data portion of the
5162d15f9d69SIlya Dryomov  * message at the moment - for now, just skip the message.
51633d14c5d2SYehuda Sadeh  */
51643d14c5d2SYehuda Sadeh static struct ceph_msg *get_reply(struct ceph_connection *con,
51653d14c5d2SYehuda Sadeh 				  struct ceph_msg_header *hdr,
51663d14c5d2SYehuda Sadeh 				  int *skip)
51673d14c5d2SYehuda Sadeh {
51683d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
51693d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = osd->o_osdc;
51705aea3dcdSIlya Dryomov 	struct ceph_msg *m = NULL;
51713d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
51723f0a4ac5SIlya Dryomov 	int front_len = le32_to_cpu(hdr->front_len);
51733d14c5d2SYehuda Sadeh 	int data_len = le32_to_cpu(hdr->data_len);
51745aea3dcdSIlya Dryomov 	u64 tid = le64_to_cpu(hdr->tid);
51753d14c5d2SYehuda Sadeh 
51765aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
51775aea3dcdSIlya Dryomov 	if (!osd_registered(osd)) {
51785aea3dcdSIlya Dryomov 		dout("%s osd%d unknown, skipping\n", __func__, osd->o_osd);
51795aea3dcdSIlya Dryomov 		*skip = 1;
51805aea3dcdSIlya Dryomov 		goto out_unlock_osdc;
51815aea3dcdSIlya Dryomov 	}
51825aea3dcdSIlya Dryomov 	WARN_ON(osd->o_osd != le64_to_cpu(hdr->src.num));
51835aea3dcdSIlya Dryomov 
51845aea3dcdSIlya Dryomov 	mutex_lock(&osd->lock);
51855aea3dcdSIlya Dryomov 	req = lookup_request(&osd->o_requests, tid);
51863d14c5d2SYehuda Sadeh 	if (!req) {
5187cd8140c6SIlya Dryomov 		dout("%s osd%d tid %llu unknown, skipping\n", __func__,
5188cd8140c6SIlya Dryomov 		     osd->o_osd, tid);
5189d15f9d69SIlya Dryomov 		*skip = 1;
51905aea3dcdSIlya Dryomov 		goto out_unlock_session;
51913d14c5d2SYehuda Sadeh 	}
51923d14c5d2SYehuda Sadeh 
51938921d114SAlex Elder 	ceph_msg_revoke_incoming(req->r_reply);
51943d14c5d2SYehuda Sadeh 
5195f2be82b0SIlya Dryomov 	if (front_len > req->r_reply->front_alloc_len) {
5196d15f9d69SIlya Dryomov 		pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
5197d15f9d69SIlya Dryomov 			__func__, osd->o_osd, req->r_tid, front_len,
5198d15f9d69SIlya Dryomov 			req->r_reply->front_alloc_len);
51993f0a4ac5SIlya Dryomov 		m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
52003f0a4ac5SIlya Dryomov 				 false);
52013d14c5d2SYehuda Sadeh 		if (!m)
52025aea3dcdSIlya Dryomov 			goto out_unlock_session;
52033d14c5d2SYehuda Sadeh 		ceph_msg_put(req->r_reply);
52043d14c5d2SYehuda Sadeh 		req->r_reply = m;
52053d14c5d2SYehuda Sadeh 	}
52063d14c5d2SYehuda Sadeh 
5207d15f9d69SIlya Dryomov 	if (data_len > req->r_reply->data_length) {
5208d15f9d69SIlya Dryomov 		pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
5209d15f9d69SIlya Dryomov 			__func__, osd->o_osd, req->r_tid, data_len,
5210d15f9d69SIlya Dryomov 			req->r_reply->data_length);
52113d14c5d2SYehuda Sadeh 		m = NULL;
5212d15f9d69SIlya Dryomov 		*skip = 1;
52135aea3dcdSIlya Dryomov 		goto out_unlock_session;
52143d14c5d2SYehuda Sadeh 	}
5215d15f9d69SIlya Dryomov 
5216d15f9d69SIlya Dryomov 	m = ceph_msg_get(req->r_reply);
52173d14c5d2SYehuda Sadeh 	dout("get_reply tid %lld %p\n", tid, m);
52183d14c5d2SYehuda Sadeh 
52195aea3dcdSIlya Dryomov out_unlock_session:
52205aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
52215aea3dcdSIlya Dryomov out_unlock_osdc:
52225aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
52233d14c5d2SYehuda Sadeh 	return m;
52243d14c5d2SYehuda Sadeh }
52253d14c5d2SYehuda Sadeh 
522619079203SIlya Dryomov /*
522719079203SIlya Dryomov  * TODO: switch to a msg-owned pagelist
522819079203SIlya Dryomov  */
522919079203SIlya Dryomov static struct ceph_msg *alloc_msg_with_page_vector(struct ceph_msg_header *hdr)
523019079203SIlya Dryomov {
523119079203SIlya Dryomov 	struct ceph_msg *m;
523219079203SIlya Dryomov 	int type = le16_to_cpu(hdr->type);
523319079203SIlya Dryomov 	u32 front_len = le32_to_cpu(hdr->front_len);
523419079203SIlya Dryomov 	u32 data_len = le32_to_cpu(hdr->data_len);
523519079203SIlya Dryomov 
523619079203SIlya Dryomov 	m = ceph_msg_new(type, front_len, GFP_NOIO, false);
523719079203SIlya Dryomov 	if (!m)
523819079203SIlya Dryomov 		return NULL;
523919079203SIlya Dryomov 
524019079203SIlya Dryomov 	if (data_len) {
524119079203SIlya Dryomov 		struct page **pages;
524219079203SIlya Dryomov 		struct ceph_osd_data osd_data;
524319079203SIlya Dryomov 
524419079203SIlya Dryomov 		pages = ceph_alloc_page_vector(calc_pages_for(0, data_len),
524519079203SIlya Dryomov 					       GFP_NOIO);
5246c22e853aSWei Yongjun 		if (IS_ERR(pages)) {
524719079203SIlya Dryomov 			ceph_msg_put(m);
524819079203SIlya Dryomov 			return NULL;
524919079203SIlya Dryomov 		}
525019079203SIlya Dryomov 
525119079203SIlya Dryomov 		ceph_osd_data_pages_init(&osd_data, pages, data_len, 0, false,
525219079203SIlya Dryomov 					 false);
525319079203SIlya Dryomov 		ceph_osdc_msg_data_add(m, &osd_data);
525419079203SIlya Dryomov 	}
525519079203SIlya Dryomov 
525619079203SIlya Dryomov 	return m;
525719079203SIlya Dryomov }
525819079203SIlya Dryomov 
52593d14c5d2SYehuda Sadeh static struct ceph_msg *alloc_msg(struct ceph_connection *con,
52603d14c5d2SYehuda Sadeh 				  struct ceph_msg_header *hdr,
52613d14c5d2SYehuda Sadeh 				  int *skip)
52623d14c5d2SYehuda Sadeh {
52633d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
52643d14c5d2SYehuda Sadeh 	int type = le16_to_cpu(hdr->type);
52653d14c5d2SYehuda Sadeh 
52661c20f2d2SAlex Elder 	*skip = 0;
52673d14c5d2SYehuda Sadeh 	switch (type) {
52683d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_MAP:
5269a02a946dSIlya Dryomov 	case CEPH_MSG_OSD_BACKOFF:
5270a40c4f10SYehuda Sadeh 	case CEPH_MSG_WATCH_NOTIFY:
527119079203SIlya Dryomov 		return alloc_msg_with_page_vector(hdr);
52723d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_OPREPLY:
52733d14c5d2SYehuda Sadeh 		return get_reply(con, hdr, skip);
52743d14c5d2SYehuda Sadeh 	default:
52755aea3dcdSIlya Dryomov 		pr_warn("%s osd%d unknown msg type %d, skipping\n", __func__,
52765aea3dcdSIlya Dryomov 			osd->o_osd, type);
52773d14c5d2SYehuda Sadeh 		*skip = 1;
52783d14c5d2SYehuda Sadeh 		return NULL;
52793d14c5d2SYehuda Sadeh 	}
52803d14c5d2SYehuda Sadeh }
52813d14c5d2SYehuda Sadeh 
52823d14c5d2SYehuda Sadeh /*
52833d14c5d2SYehuda Sadeh  * Wrappers to refcount containing ceph_osd struct
52843d14c5d2SYehuda Sadeh  */
52853d14c5d2SYehuda Sadeh static struct ceph_connection *get_osd_con(struct ceph_connection *con)
52863d14c5d2SYehuda Sadeh {
52873d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
52883d14c5d2SYehuda Sadeh 	if (get_osd(osd))
52893d14c5d2SYehuda Sadeh 		return con;
52903d14c5d2SYehuda Sadeh 	return NULL;
52913d14c5d2SYehuda Sadeh }
52923d14c5d2SYehuda Sadeh 
52933d14c5d2SYehuda Sadeh static void put_osd_con(struct ceph_connection *con)
52943d14c5d2SYehuda Sadeh {
52953d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
52963d14c5d2SYehuda Sadeh 	put_osd(osd);
52973d14c5d2SYehuda Sadeh }
52983d14c5d2SYehuda Sadeh 
52993d14c5d2SYehuda Sadeh /*
53003d14c5d2SYehuda Sadeh  * authentication
53013d14c5d2SYehuda Sadeh  */
5302a3530df3SAlex Elder /*
5303a3530df3SAlex Elder  * Note: returned pointer is the address of a structure that's
5304a3530df3SAlex Elder  * managed separately.  Caller must *not* attempt to free it.
5305a3530df3SAlex Elder  */
5306a3530df3SAlex Elder static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
53078f43fb53SAlex Elder 					int *proto, int force_new)
53083d14c5d2SYehuda Sadeh {
53093d14c5d2SYehuda Sadeh 	struct ceph_osd *o = con->private;
53103d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = o->o_osdc;
53113d14c5d2SYehuda Sadeh 	struct ceph_auth_client *ac = osdc->client->monc.auth;
531274f1869fSAlex Elder 	struct ceph_auth_handshake *auth = &o->o_auth;
53133d14c5d2SYehuda Sadeh 
531474f1869fSAlex Elder 	if (force_new && auth->authorizer) {
53156c1ea260SIlya Dryomov 		ceph_auth_destroy_authorizer(auth->authorizer);
531674f1869fSAlex Elder 		auth->authorizer = NULL;
53173d14c5d2SYehuda Sadeh 	}
531827859f97SSage Weil 	if (!auth->authorizer) {
531927859f97SSage Weil 		int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
5320a3530df3SAlex Elder 						      auth);
53213d14c5d2SYehuda Sadeh 		if (ret)
5322a3530df3SAlex Elder 			return ERR_PTR(ret);
532327859f97SSage Weil 	} else {
532427859f97SSage Weil 		int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
53250bed9b5cSSage Weil 						     auth);
53260bed9b5cSSage Weil 		if (ret)
53270bed9b5cSSage Weil 			return ERR_PTR(ret);
53283d14c5d2SYehuda Sadeh 	}
53293d14c5d2SYehuda Sadeh 	*proto = ac->protocol;
533074f1869fSAlex Elder 
5331a3530df3SAlex Elder 	return auth;
53323d14c5d2SYehuda Sadeh }
53333d14c5d2SYehuda Sadeh 
53343d14c5d2SYehuda Sadeh 
53350dde5848SIlya Dryomov static int verify_authorizer_reply(struct ceph_connection *con)
53363d14c5d2SYehuda Sadeh {
53373d14c5d2SYehuda Sadeh 	struct ceph_osd *o = con->private;
53383d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = o->o_osdc;
53393d14c5d2SYehuda Sadeh 	struct ceph_auth_client *ac = osdc->client->monc.auth;
53403d14c5d2SYehuda Sadeh 
53410dde5848SIlya Dryomov 	return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer);
53423d14c5d2SYehuda Sadeh }
53433d14c5d2SYehuda Sadeh 
53443d14c5d2SYehuda Sadeh static int invalidate_authorizer(struct ceph_connection *con)
53453d14c5d2SYehuda Sadeh {
53463d14c5d2SYehuda Sadeh 	struct ceph_osd *o = con->private;
53473d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = o->o_osdc;
53483d14c5d2SYehuda Sadeh 	struct ceph_auth_client *ac = osdc->client->monc.auth;
53493d14c5d2SYehuda Sadeh 
535027859f97SSage Weil 	ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
53513d14c5d2SYehuda Sadeh 	return ceph_monc_validate_auth(&osdc->client->monc);
53523d14c5d2SYehuda Sadeh }
53533d14c5d2SYehuda Sadeh 
53548cb441c0SIlya Dryomov static void osd_reencode_message(struct ceph_msg *msg)
53558cb441c0SIlya Dryomov {
5356914902afSIlya Dryomov 	int type = le16_to_cpu(msg->hdr.type);
5357914902afSIlya Dryomov 
5358914902afSIlya Dryomov 	if (type == CEPH_MSG_OSD_OP)
53598cb441c0SIlya Dryomov 		encode_request_finish(msg);
53608cb441c0SIlya Dryomov }
53618cb441c0SIlya Dryomov 
536279dbd1baSIlya Dryomov static int osd_sign_message(struct ceph_msg *msg)
536333d07337SYan, Zheng {
536479dbd1baSIlya Dryomov 	struct ceph_osd *o = msg->con->private;
536533d07337SYan, Zheng 	struct ceph_auth_handshake *auth = &o->o_auth;
536679dbd1baSIlya Dryomov 
536733d07337SYan, Zheng 	return ceph_auth_sign_message(auth, msg);
536833d07337SYan, Zheng }
536933d07337SYan, Zheng 
537079dbd1baSIlya Dryomov static int osd_check_message_signature(struct ceph_msg *msg)
537133d07337SYan, Zheng {
537279dbd1baSIlya Dryomov 	struct ceph_osd *o = msg->con->private;
537333d07337SYan, Zheng 	struct ceph_auth_handshake *auth = &o->o_auth;
537479dbd1baSIlya Dryomov 
537533d07337SYan, Zheng 	return ceph_auth_check_message_signature(auth, msg);
537633d07337SYan, Zheng }
537733d07337SYan, Zheng 
53783d14c5d2SYehuda Sadeh static const struct ceph_connection_operations osd_con_ops = {
53793d14c5d2SYehuda Sadeh 	.get = get_osd_con,
53803d14c5d2SYehuda Sadeh 	.put = put_osd_con,
53813d14c5d2SYehuda Sadeh 	.dispatch = dispatch,
53823d14c5d2SYehuda Sadeh 	.get_authorizer = get_authorizer,
53833d14c5d2SYehuda Sadeh 	.verify_authorizer_reply = verify_authorizer_reply,
53843d14c5d2SYehuda Sadeh 	.invalidate_authorizer = invalidate_authorizer,
53853d14c5d2SYehuda Sadeh 	.alloc_msg = alloc_msg,
53868cb441c0SIlya Dryomov 	.reencode_message = osd_reencode_message,
538779dbd1baSIlya Dryomov 	.sign_message = osd_sign_message,
538879dbd1baSIlya Dryomov 	.check_message_signature = osd_check_message_signature,
53895aea3dcdSIlya Dryomov 	.fault = osd_fault,
53903d14c5d2SYehuda Sadeh };
5391